1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CHRE_UTIL_TOOLCHAIN_H_
18 #define CHRE_UTIL_TOOLCHAIN_H_
19 
20 /**
21  * @file
22  * Compiler/build toolchain-specific macros used by nanoapps and the CHRE system
23  */
24 
25 #if defined(__GNUC__) || defined(__clang__)
26 
27 // Promoting float to double is a necessary part of vararg-based logging, so
28 // ignore those warnings when logging via chre/util/nanoapp/log.h
29 // clang-format off
30 #define CHRE_LOG_PREAMBLE \
31     _Pragma("GCC diagnostic push") \
32     _Pragma("GCC diagnostic ignored \"-Wdouble-promotion\"")
33 // clang-format on
34 
35 #define CHRE_LOG_EPILOGUE _Pragma("GCC diagnostic pop")
36 
37 // Allows using deprecated APIs locally.
38 #define CHRE_DEPRECATED_PREAMBLE \
39   _Pragma("GCC diagnostic push") \
40       _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
41 
42 #define CHRE_DEPRECATED_EPILOGUE _Pragma("GCC diagnostic pop")
43 
44 // Instructs the compiler to warn about unused results.
45 // When annotating a function, this macro must appear before the function name
46 // in the declaration.
47 // Example Usage:
48 //    bool CHRE_MUST_USE_RESULT pleaseUseMyResult();
49 //
50 // A cast-to-void to suppress this warning only works on clang and not on gcc
51 // because of this bug:
52 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
53 //
54 // A possible workaround (other than just filtering out this macro) may be
55 // just negating the return value before a void-cast as follows::
56 //    void void_cast_should_not_warn() {
57 //      (void) !foo();
58 //      //     ^-- here
59 //    }
60 #if defined __has_attribute
61 #if __has_attribute(warn_unused_result)
62 #define CHRE_MUST_USE_RESULT __attribute__((warn_unused_result))
63 #endif
64 #endif
65 #ifndef CHRE_MUST_USE_RESULT
66 #define CHRE_MUST_USE_RESULT
67 #endif
68 
69 #elif defined(IS_CHPP_BUILD)
70 // These macros need to be defined for CHPP on other compilers
71 
72 #define CHRE_LOG_PREAMBLE
73 #define CHRE_LOG_EPILOGUE
74 #define CHRE_DEPRECATED_PREAMBLE
75 #define CHRE_DEPRECATED_EPILOGUE
76 #define CHRE_MUST_USE_RESULT
77 
78 #else
79 
80 #error Need to add support for new compiler
81 
82 #endif
83 #endif  // CHRE_UTIL_TOOLCHAIN_H_
84