Skip to content

Commit 2e0c1af

Browse files
authored
Merge pull request #1836 from jbj/xheader-undef
C++: Support x-macros that are #undef'ed in header
2 parents 853a3aa + 2c253f3 commit 2e0c1af

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

cpp/ql/src/jsf/4.07 Header Files/AV Rule 35.ql

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,32 +95,55 @@ predicate usesMacro(HeaderFile hf, string macroName) {
9595
)
9696
}
9797

98-
/**
99-
* File `f` both defines and un-defines a macro called `macroName`.
100-
*/
101-
predicate defUndef(File f, string macroName) {
98+
/** File `f` defines a macro called `macroName`. */
99+
predicate definesMacro(File f, string macroName) {
102100
exists(Macro m |
103101
m.getFile() = f and
104102
m.getName() = macroName
105-
) and exists(PreprocessorUndef ud |
103+
)
104+
}
105+
106+
/** File `f` un-defines a macro called `macroName`. */
107+
predicate undefinesMacro(File f, string macroName) {
108+
exists(PreprocessorUndef ud |
106109
ud.getFile() = f and
107110
ud.getName() = macroName
108111
)
109112
}
110113

114+
/**
115+
* File `f` both defines and un-defines a macro called `macroName`.
116+
*/
117+
predicate defUndef(File f, string macroName) {
118+
definesMacro(f, macroName) and
119+
undefinesMacro(f, macroName)
120+
}
121+
111122
/**
112123
* Header file `hf` looks like it contains an x-macro.
113124
* That is, a macro that is used to interpret the
114125
* data in `hf`, usually defined just before including that file
115126
* and undefined immediately afterwards.
116127
*/
117128
predicate hasXMacro(HeaderFile hf) {
129+
// Every header that includes `hf` both defines and undefines a macro that's
130+
// used in `hf`.
118131
exists(string macroName |
119132
usesMacro(hf, macroName) and
120133
forex(File f | f.getAnIncludedFile() = hf |
121134
defUndef(f, macroName)
122135
)
123136
)
137+
or
138+
// Every header that includes `hf` defines a macro that's used in `hf`, and
139+
// `hf` itself undefines it.
140+
exists(string macroName |
141+
usesMacro(hf, macroName) and
142+
undefinesMacro(hf, macroName) and
143+
forex(File f | f.getAnIncludedFile() = hf |
144+
definesMacro(f, macroName)
145+
)
146+
)
124147
}
125148

126149
from HeaderFile hf, string detail, MaybePreprocessorDirective detail1, MaybePreprocessorDirective detail2

cpp/ql/test/query-tests/jsf/4.07 Header Files/AV Rule 35/all1.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ enum Items {
5353
#define XMACRO2(id,desc) void use_##();
5454
#include "items2.h"
5555
#undef XMACRO2
56+
57+
58+
#define XMACRO3(id,desc) static const char * id##_item = "The " desc " item";
59+
#include "items3.h"
60+
// No #undef of XMACRO3. That's handled in items3.h.

cpp/ql/test/query-tests/jsf/4.07 Header Files/AV Rule 35/all2.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ enum Items {
3232
#define XMACRO2(id,desc) void use_##();
3333
#include "items2.h"
3434
#undef XMACRO2
35+
36+
#define XMACRO3(id,desc) static const char * id##_name = desc;
37+
#include "items3.h"
38+
// No #undef of XMACRO3. That's handled in items3.h.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
XMACRO3(shield, "Wooden Shield")
2+
XMACRO3(boots, "Leather Boots")
3+
XMACRO3(helmet, "Helmet")
4+
5+
#undef XMACRO3

0 commit comments

Comments
 (0)