|
| 1 | +#ifndef _RESERVED_MACRO |
| 2 | +#define _RESERVED_MACRO // Not reported by this rule |
| 3 | +#endif /* _RESERVED_MACRO */ |
| 4 | + |
| 5 | +#ifndef _also_reserved_MACRO |
| 6 | +#define _also_reserved_MACRO // Not reported by this rule |
| 7 | +#endif /* _not_reserved_MACRO */ |
| 8 | + |
| 9 | +static const int INT_LIMIT_MAX = 12000; // COMPLIANT future library directions |
| 10 | + |
| 11 | +struct _s { // NON_COMPLIANT |
| 12 | + struct _s *_next; // COMPLIANT not file scope |
| 13 | +}; |
| 14 | + |
| 15 | +void _f() { // NON_COMPLIANT |
| 16 | + int _p; // COMPLIANT not file scope |
| 17 | +} |
| 18 | + |
| 19 | +void *malloc(int bytes) { // NON_COMPLIANT |
| 20 | + void *ptr; |
| 21 | + return ptr; |
| 22 | +} |
| 23 | + |
| 24 | +extern int |
| 25 | + errno; // NON_COMPLIANT - errno is explicitly reserved for external linkage |
| 26 | + |
| 27 | +void output(int a, int b, int c); |
| 28 | + |
| 29 | +#define DEBUG(...) \ |
| 30 | + output(__VA_ARGS__) // COMPLIANT - using not declaring `__VA_ARGS__` |
| 31 | + |
| 32 | +void test() { |
| 33 | + DEBUG(1, 2, 3); |
| 34 | + __FUNCTION__; // COMPLIANT - use, not declaration of `__FUNCTION__` |
| 35 | + __PRETTY_FUNCTION__; // COMPLIANT - use, not declaration of |
| 36 | + // `__PRETTY_FUNCTION__` |
| 37 | +} |
| 38 | +#include <tgmath.h> |
| 39 | +void test2(int log); // NON_COMPLIANT - tgmath.h defines log as a reserved macro |
| 40 | + |
| 41 | +/* Test _[A-Z] */ |
| 42 | + |
| 43 | +int _Test_global; // NON_COMPLIANT - _ followed by capital is reserved |
| 44 | +void _Test_func( // NON_COMPLIANT - _ followed by capital is reserved |
| 45 | + int _Test_param) { // NON_COMPLIANT - _ followed by capital is reserved |
| 46 | + int _Test_local; // NON_COMPLIANT - _ followed by capital is reserved |
| 47 | + struct _Test_struct_local { // NON_COMPLIANT - _ followed by capital is |
| 48 | + // reserved |
| 49 | + int _Test_member; // NON_COMPLIANT - _ followed by capital is reserved |
| 50 | + }; |
| 51 | +} |
| 52 | +struct _Test_struct { // NON_COMPLIANT - _ followed by capital is reserved |
| 53 | + int _Test_member; // NON_COMPLIANT - _ followed by capital is reserved |
| 54 | +}; |
| 55 | +#define _TEST_MACRO x // Not reported by this rule |
| 56 | + |
| 57 | +/* Test __ */ |
| 58 | + |
| 59 | +int __test_double_global; // NON_COMPLIANT - double _ is reserved |
| 60 | +void __test_double_func( // NON_COMPLIANT - double _ is reserved |
| 61 | + int __test_double_param) { // NON_COMPLIANT - double _ is reserved |
| 62 | + int __test_double_local; // NON_COMPLIANT - double _ is reserved |
| 63 | + struct __test_double_struct_local { // NON_COMPLIANT - double _ is reserved |
| 64 | + int __test_double_member; // NON_COMPLIANT - double _ is reserved |
| 65 | + }; |
| 66 | +} |
| 67 | +struct __test_double_struct { // NON_COMPLIANT - double _ is reserved |
| 68 | + int __test_double_member; // NON_COMPLIANT - double _ is reserved |
| 69 | +}; |
| 70 | +#define __TEST_MACRO x // Not reported by this rule |
| 71 | + |
| 72 | +/* |
| 73 | + * Test _, but not followed by underscore or upper case, which is reserved in |
| 74 | + * file scope and ordinary/tag name spaces |
| 75 | + */ |
| 76 | + |
| 77 | +int _test_lower_global; // NON_COMPLIANT - _ is reserved in ordinary name space |
| 78 | +void _test_lower_func( // NON_COMPLIANT - _ is reserved as a function name in |
| 79 | + // ordinary name space |
| 80 | + int _test_lower_param) { // COMPLIANT - _ is not reserved in the block name |
| 81 | + // space |
| 82 | + int _test; // COMPLIANT - _ is not reserved in the block name space |
| 83 | + struct _test_struct { // COMPLIANT - _ is not reserved in the block name space |
| 84 | + int _test; // COMPLIANT - _ is not reserved in the block name space |
| 85 | + }; |
| 86 | +} |
| 87 | +struct _test_struct { // NON_COMPLIANT - _ is reserved in the tag name space |
| 88 | + int _test; // COMPLIANT - _ is not reserved in the member name space |
| 89 | +}; |
| 90 | +#define _test_macro x // Not reported by this rule |
| 91 | + |
| 92 | +/* Identify names reserved as a macro when the relevant header is included. */ |
| 93 | +int FE_INEXACT; // NON_COMPLIANT - FE_INEXACT is reserved as a macro name |
| 94 | +void FE_DIVBYZERO( // NON_COMPLIANT - FE_DIVBYZERO is reserved as a macro name |
| 95 | + int FE_INEXACT) { // NON_COMPLIANT - FE_INEXACT is reserved as a macro name |
| 96 | + int FE_INVALID; // NON_COMPLIANT - FE_INEXACT is reserved as a macro name |
| 97 | + struct FE_INEXACT { // NON_COMPLIANT - FE_INEXACT is reserved as a macro name |
| 98 | + int FE_INEXACT; // NON_COMPLIANT - FE_INEXACT is reserved as a macro name |
| 99 | + }; |
| 100 | +} |
| 101 | +struct FE_INEXACT { // NON_COMPLIANT - FE_INEXACT is reserved as a macro name |
| 102 | + int FE_INEXACT; // NON_COMPLIANT - FE_INEXACT is reserved as a macro name |
| 103 | +}; |
| 104 | +#define FE_INEXACT x // Not reported by this rule |
| 105 | + |
| 106 | +// We include the header after the declarations to avoid the inbuilt macros |
| 107 | +// expanding in the declarations above. The rule is not, however, sensitive |
| 108 | +// to location, so we should still report in this case. |
| 109 | +#include <fenv.h> |
| 110 | + |
| 111 | +/* Functions and objects with external linkage */ |
| 112 | + |
| 113 | +int exit; // NON_COMPLIANT - reserved for external linkage, even though header |
| 114 | + // was not included |
| 115 | +void free() { // NON_COMPLIANT - reserved for external linkage, even though |
| 116 | + // header was not included |
| 117 | +} |
| 118 | +static int rand; // COMPLIANT - only reserved for external linkage or if random |
| 119 | + // was included |
| 120 | +static int srand() { // COMPLIANT - only reserved for external linkage or if |
| 121 | + // random was included |
| 122 | +} |
| 123 | + |
| 124 | +#include <string.h> |
| 125 | +#define strlen 0 // Not reported by this rule |
| 126 | + |
| 127 | +// The examples below are non compliant, because those symbols are reserved in |
| 128 | +// file scope when string.h is included. However, the compiler/edg rejects the |
| 129 | +// declaration cases, so we cannot test them. |
| 130 | + |
| 131 | +// static int strlen; |
| 132 | +// static void *memcpy(void *s1, const void *s2, size_t n) {} |
| 133 | + |
| 134 | +#include <time.h> |
| 135 | +#define tm_sec 0 // Not reported by this rule |
| 136 | + |
| 137 | +// The examples below are non compliant, because those symbols are reserved in |
| 138 | +// file scope when time.h is included. However, the compiler/edg rejects the |
| 139 | +// declaration cases, so we cannot test them. |
| 140 | + |
| 141 | +// struct tm { |
| 142 | +// int tm_sec; |
| 143 | +// int tm_min; |
| 144 | +// int tm_hour; |
| 145 | +// int tm_mday; |
| 146 | +// int tm_mon; |
| 147 | +// int tm_year; |
| 148 | +// int tm_wday; |
| 149 | +// int tm_yday; |
| 150 | +// int tm_isdst; |
| 151 | +// long __tm_gmtoff; |
| 152 | +// const char *__tm_zone; |
| 153 | +// }; |
| 154 | + |
| 155 | +// Examples below are compliant because threads.h is not included |
| 156 | + |
| 157 | +#define tss_set 0 // Not reported by this rule |
| 158 | +static int tss_get; // COMPLIANT - threads.h not included, not external linkage |
| 159 | +static void |
| 160 | +tss_delete( // COMPLIANT - threads.h not included, not external linkage |
| 161 | + int tss_create // COMPLIANT - threads.h not included |
| 162 | +) { |
| 163 | + int thrd_detach; // COMPLIANT - threads.h not included |
| 164 | + struct thrd_join { // COMPLIANT - threads.h not included |
| 165 | + int thrd_equal; // COMPLIANT - threads.h not included |
| 166 | + }; |
| 167 | +} |
| 168 | +struct thrd_yield { // COMPLIANT - threads.h not included |
| 169 | + int thrd_exit; // COMPLIANT - threads.h not included |
| 170 | +}; |
| 171 | + |
| 172 | +#include <sys/select.h> |
| 173 | +void test_macro() { |
| 174 | + fd_set test_set; |
| 175 | + FD_ZERO(&test_set); // COMPLIANT - macro expands to variables with `__` |
| 176 | + // prefixes, but should be excluded |
| 177 | +} |
0 commit comments