Skip to content

Commit 172378f

Browse files
committed
Only include macro names when the relevant header is included
1 parent 122a020 commit 172378f

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

c/common/test/rules/declaredareservedidentifier/DeclaredAReservedIdentifier.expected

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
| test.c:11:8:11:9 | _s | Type '_s' declares a name beginning with _ which is reserved in the tag name space. |
44
| test.c:15:6:15:7 | _f | Function '_f' declares a name beginning with _ which is reserved in the ordinary name space. |
55
| test.c:19:7:19:12 | malloc | Function 'malloc' declares a name which is reserved for external linkage from the C11 standard library header 'stdlib.h'. |
6-
| test.c:25:5:25:9 | errno | Global variable 'errno' declares a name reserved for a macro from the C11 standard library header 'errno.h'. |
76
| test.c:25:5:25:9 | errno | Global variable 'errno' declares a name which is reserved for external linkage from the C11 standard library header 'errno.h'. |
87
| test.c:39:16:39:18 | log | Parameter 'log' declares a name reserved for a macro from the C11 standard library header 'tgmath.h'. |
98
| test.c:43:5:43:16 | _Test_global | Global variable '_Test_global' declares a reserved name beginning _ followed by an uppercase letter. |
@@ -28,16 +27,16 @@
2827
| test.c:79:6:79:21 | _test_lower_func | Function '_test_lower_func' declares a name beginning with _ which is reserved in the ordinary name space. |
2928
| test.c:88:8:88:19 | _test_struct | Type '_test_struct' declares a name beginning with _ which is reserved in the tag name space. |
3029
| test.c:91:1:92:3 | #define _test_macro x | Macro '_test_macro' declares a name beginning with _ which is reserved in the ordinary and tag namespaces. |
31-
| test.c:97:5:97:10 | NDEBUG | Global variable 'NDEBUG' declares a name reserved for a macro from the C11 standard library header 'assert.h'. |
32-
| test.c:98:6:98:9 | EDOM | Function 'EDOM' declares a name reserved for a macro from the C11 standard library header 'errno.h'. |
33-
| test.c:99:9:99:14 | ERANGE | Parameter 'ERANGE' declares a name reserved for a macro from the C11 standard library header 'errno.h'. |
34-
| test.c:100:7:100:12 | NDEBUG | Local variable 'NDEBUG' declares a name reserved for a macro from the C11 standard library header 'assert.h'. |
35-
| test.c:101:10:101:15 | NDEBUG | Type 'NDEBUG' declares a name reserved for a macro from the C11 standard library header 'assert.h'. |
36-
| test.c:102:9:102:14 | NDEBUG | Member variable 'NDEBUG' declares a name reserved for a macro from the C11 standard library header 'assert.h'. |
37-
| test.c:105:8:105:13 | NDEBUG | Type 'NDEBUG' declares a name reserved for a macro from the C11 standard library header 'assert.h'. |
38-
| test.c:106:7:106:12 | NDEBUG | Member variable 'NDEBUG' declares a name reserved for a macro from the C11 standard library header 'assert.h'. |
39-
| test.c:108:1:109:3 | #define NDEBUG x | Macro 'NDEBUG' declares a name reserved for a macro from the C11 standard library header 'assert.h'. |
40-
| test.c:114:5:114:8 | exit | Global variable 'exit' declares a name which is reserved for external linkage from the C11 standard library header 'stdlib.h'. |
41-
| test.c:116:6:116:9 | free | Function 'free' declares a name which is reserved for external linkage from the C11 standard library header 'stdlib.h'. |
42-
| test.c:126:1:126:16 | #define strlen 0 | Macro 'strlen' declares a reserved name from the C11 standard library header 'string.h' which is included directly or indirectly in this translation unit. |
43-
| test.c:136:1:136:16 | #define tm_sec 0 | Macro 'tm_sec' declares a reserved name from the C11 standard library header 'time.h' which is included directly or indirectly in this translation unit. |
30+
| test.c:96:5:96:14 | FE_INEXACT | Global variable 'FE_INEXACT' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
31+
| test.c:97:6:97:17 | FE_DIVBYZERO | Function 'FE_DIVBYZERO' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
32+
| test.c:98:9:98:18 | FE_INEXACT | Parameter 'FE_INEXACT' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
33+
| test.c:99:7:99:16 | FE_INVALID | Local variable 'FE_INVALID' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
34+
| test.c:100:10:100:19 | FE_INEXACT | Type 'FE_INEXACT' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
35+
| test.c:101:9:101:18 | FE_INEXACT | Member variable 'FE_INEXACT' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
36+
| test.c:104:8:104:17 | FE_INEXACT | Type 'FE_INEXACT' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
37+
| test.c:105:7:105:16 | FE_INEXACT | Member variable 'FE_INEXACT' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
38+
| test.c:107:1:108:3 | #define FE_INEXACT x | Macro 'FE_INEXACT' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
39+
| test.c:118:5:118:8 | exit | Global variable 'exit' declares a name which is reserved for external linkage from the C11 standard library header 'stdlib.h'. |
40+
| test.c:120:6:120:9 | free | Function 'free' declares a name which is reserved for external linkage from the C11 standard library header 'stdlib.h'. |
41+
| test.c:130:1:130:16 | #define strlen 0 | Macro 'strlen' declares a reserved name from the C11 standard library header 'string.h' which is included directly or indirectly in this translation unit. |
42+
| test.c:140:1:140:16 | #define tm_sec 0 | Macro 'tm_sec' declares a reserved name from the C11 standard library header 'time.h' which is included directly or indirectly in this translation unit. |

c/common/test/rules/declaredareservedidentifier/test.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void *malloc(int bytes) { // NON_COMPLIANT
2222
}
2323

2424
extern int
25-
errno; // NON_COMPLIANT - eerno is reserved as both a macro and identifier
25+
errno; // NON_COMPLIANT - errno is explicitly reserved for external linkage
2626

2727
void output(int a, int b, int c);
2828

@@ -35,7 +35,7 @@ void test() {
3535
__PRETTY_FUNCTION__; // COMPLIANT - use, not declaration of
3636
// `__PRETTY_FUNCTION__`
3737
}
38-
38+
#include <tgmath.h>
3939
void test2(int log); // NON_COMPLIANT - tgmath.h defines log as a reserved macro
4040

4141
/* Test _[A-Z] */
@@ -92,23 +92,27 @@ struct _test_struct { // NON_COMPLIANT - _ is reserved in the tag name space
9292
x // NON_COMPLIANT - _ is reserved for for file scope names and so cannot be
9393
// used as a macro name
9494

95-
/* Identify names reserved as a macro. */
96-
97-
int NDEBUG; // NON_COMPLIANT - NDEBUG is reserved as a macro name
98-
void EDOM( // NON_COMPLIANT - EDOM is reserved as a macro name
99-
int ERANGE) { // NON_COMPLIANT - ERANGE is reserved as a macro name
100-
int NDEBUG; // NON_COMPLIANT - NDEBUG is reserved as a macro name
101-
struct NDEBUG { // NON_COMPLIANT - NDEBUG is reserved as a macro name
102-
int NDEBUG; // NON_COMPLIANT - NDEBUG is reserved as a macro name
95+
/* Identify names reserved as a macro when the relevant header is included. */
96+
int FE_INEXACT; // NON_COMPLIANT - FE_INEXACT is reserved as a macro name
97+
void FE_DIVBYZERO( // NON_COMPLIANT - FE_DIVBYZERO is reserved as a macro name
98+
int FE_INEXACT) { // NON_COMPLIANT - FE_INEXACT is reserved as a macro name
99+
int FE_INVALID; // NON_COMPLIANT - FE_INEXACT is reserved as a macro name
100+
struct FE_INEXACT { // NON_COMPLIANT - FE_INEXACT is reserved as a macro name
101+
int FE_INEXACT; // NON_COMPLIANT - FE_INEXACT is reserved as a macro name
103102
};
104103
}
105-
struct NDEBUG { // NON_COMPLIANT - NDEBUG is reserved as a macro name
106-
int NDEBUG; // NON_COMPLIANT - NDEBUG is reserved as a macro name
104+
struct FE_INEXACT { // NON_COMPLIANT - FE_INEXACT is reserved as a macro name
105+
int FE_INEXACT; // NON_COMPLIANT - FE_INEXACT is reserved as a macro name
107106
};
108-
#define NDEBUG \
109-
x // NON_COMPLIANT - NDEBUG is reserved as a macro name for the standard
107+
#define FE_INEXACT \
108+
x // NON_COMPLIANT - FE_INEXACT is reserved as a macro name for the standard
110109
// library
111110

111+
// We include the header after the declarations to avoid the inbuilt macros
112+
// expanding in the declarations above. The rule is not, however, sensitive
113+
// to location, so we should still report in this case.
114+
#include <fenv.h>
115+
112116
/* Functions and objects with external linkage */
113117

114118
int exit; // NON_COMPLIANT - reserved for external linkage, even though header

cpp/common/src/codingstandards/cpp/rules/declaredareservedidentifier/DeclaredAReservedIdentifier.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ query predicate problems(Element m, string message) {
192192
// > unless explicitly stated otherwise (see 7.1.4).
193193
exists(string header |
194194
TargetedCLibrary::hasMacroName(header, name, _) and
195+
// The relevant header is included directly or transitively by the file
196+
m.getFile().getAnIncludedFile*().getBaseName() = header and
195197
reason =
196198
"declares a name reserved for a macro from the " + TargetedCLibrary::getName() +
197199
" standard library header '" + header + "'"

0 commit comments

Comments
 (0)