1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | #ifndef LLVM_CLANG_LEX_VARIADICMACROSUPPORT_H |
16 | #define LLVM_CLANG_LEX_VARIADICMACROSUPPORT_H |
17 | |
18 | #include "clang/Lex/Preprocessor.h" |
19 | #include "llvm/ADT/SmallVector.h" |
20 | |
21 | namespace clang { |
22 | class Preprocessor; |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | class VariadicMacroScopeGuard { |
31 | const Preprocessor &PP; |
32 | IdentifierInfo *const Ident__VA_ARGS__; |
33 | IdentifierInfo *const Ident__VA_OPT__; |
34 | |
35 | public: |
36 | VariadicMacroScopeGuard(const Preprocessor &P) |
37 | : PP(P), Ident__VA_ARGS__(PP.Ident__VA_ARGS__), |
38 | Ident__VA_OPT__(PP.Ident__VA_OPT__) { |
39 | (0) . __assert_fail ("Ident__VA_ARGS__->isPoisoned() && \"__VA_ARGS__ should be poisoned \" \"outside an ISO C/C++ variadic \" \"macro definition!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 41, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned " |
40 | (0) . __assert_fail ("Ident__VA_ARGS__->isPoisoned() && \"__VA_ARGS__ should be poisoned \" \"outside an ISO C/C++ variadic \" \"macro definition!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 41, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "outside an ISO C/C++ variadic " |
41 | (0) . __assert_fail ("Ident__VA_ARGS__->isPoisoned() && \"__VA_ARGS__ should be poisoned \" \"outside an ISO C/C++ variadic \" \"macro definition!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 41, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "macro definition!"); |
42 | (0) . __assert_fail ("!Ident__VA_OPT__ || (Ident__VA_OPT__->isPoisoned() && \"__VA_OPT__ should be poisoned!\")", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 44, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert( |
43 | (0) . __assert_fail ("!Ident__VA_OPT__ || (Ident__VA_OPT__->isPoisoned() && \"__VA_OPT__ should be poisoned!\")", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 44, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> !Ident__VA_OPT__ || |
44 | (0) . __assert_fail ("!Ident__VA_OPT__ || (Ident__VA_OPT__->isPoisoned() && \"__VA_OPT__ should be poisoned!\")", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 44, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> (Ident__VA_OPT__->isPoisoned() && "__VA_OPT__ should be poisoned!")); |
45 | } |
46 | |
47 | |
48 | |
49 | void enterScope() { |
50 | Ident__VA_ARGS__->setIsPoisoned(false); |
51 | if (Ident__VA_OPT__) |
52 | Ident__VA_OPT__->setIsPoisoned(false); |
53 | } |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | void exitScope() { |
60 | Ident__VA_ARGS__->setIsPoisoned(true); |
61 | if (Ident__VA_OPT__) |
62 | Ident__VA_OPT__->setIsPoisoned(true); |
63 | } |
64 | |
65 | ~VariadicMacroScopeGuard() { exitScope(); } |
66 | }; |
67 | |
68 | |
69 | |
70 | class VAOptDefinitionContext { |
71 | |
72 | SmallVector<SourceLocation, 8> UnmatchedOpeningParens; |
73 | |
74 | const IdentifierInfo *const Ident__VA_OPT__; |
75 | |
76 | |
77 | public: |
78 | VAOptDefinitionContext(Preprocessor &PP) |
79 | : Ident__VA_OPT__(PP.Ident__VA_OPT__) {} |
80 | |
81 | bool isVAOptToken(const Token &T) const { |
82 | return Ident__VA_OPT__ && T.getIdentifierInfo() == Ident__VA_OPT__; |
83 | } |
84 | |
85 | |
86 | |
87 | bool isInVAOpt() const { return UnmatchedOpeningParens.size(); } |
88 | |
89 | |
90 | void sawVAOptFollowedByOpeningParens(const SourceLocation LParenLoc) { |
91 | (0) . __assert_fail ("!isInVAOpt() && \"Must NOT be within VAOPT context to call this\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 91, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!isInVAOpt() && "Must NOT be within VAOPT context to call this"); |
92 | UnmatchedOpeningParens.push_back(LParenLoc); |
93 | |
94 | } |
95 | |
96 | SourceLocation getUnmatchedOpeningParenLoc() const { |
97 | (0) . __assert_fail ("isInVAOpt() && \"Must be within VAOPT context to call this\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 97, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(isInVAOpt() && "Must be within VAOPT context to call this"); |
98 | return UnmatchedOpeningParens.back(); |
99 | } |
100 | |
101 | |
102 | |
103 | |
104 | bool sawClosingParen() { |
105 | (0) . __assert_fail ("isInVAOpt() && \"Must be within VAOPT context to call this\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 105, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(isInVAOpt() && "Must be within VAOPT context to call this"); |
106 | UnmatchedOpeningParens.pop_back(); |
107 | return !UnmatchedOpeningParens.size(); |
108 | } |
109 | |
110 | |
111 | void sawOpeningParen(SourceLocation LParenLoc) { |
112 | (0) . __assert_fail ("isInVAOpt() && \"Must be within VAOPT context to call this\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 112, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(isInVAOpt() && "Must be within VAOPT context to call this"); |
113 | UnmatchedOpeningParens.push_back(LParenLoc); |
114 | } |
115 | |
116 | }; |
117 | |
118 | |
119 | |
120 | class VAOptExpansionContext : VAOptDefinitionContext { |
121 | |
122 | Token SyntheticEOFToken; |
123 | |
124 | |
125 | |
126 | SourceLocation VAOptLoc; |
127 | |
128 | |
129 | |
130 | |
131 | |
132 | int NumOfTokensPriorToVAOpt = -1; |
133 | |
134 | unsigned LeadingSpaceForStringifiedToken : 1; |
135 | |
136 | unsigned StringifyBefore : 1; |
137 | unsigned CharifyBefore : 1; |
138 | |
139 | |
140 | bool hasStringifyBefore() const { |
141 | (0) . __assert_fail ("!isReset() && \"Must only be called if the state has not been reset\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 142, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!isReset() && |
142 | (0) . __assert_fail ("!isReset() && \"Must only be called if the state has not been reset\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 142, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "Must only be called if the state has not been reset"); |
143 | return StringifyBefore; |
144 | } |
145 | |
146 | bool isReset() const { |
147 | return NumOfTokensPriorToVAOpt == -1 || |
148 | VAOptLoc.isInvalid(); |
149 | } |
150 | |
151 | public: |
152 | VAOptExpansionContext(Preprocessor &PP) |
153 | : VAOptDefinitionContext(PP), LeadingSpaceForStringifiedToken(false), |
154 | StringifyBefore(false), CharifyBefore(false) { |
155 | SyntheticEOFToken.startToken(); |
156 | SyntheticEOFToken.setKind(tok::eof); |
157 | } |
158 | |
159 | void reset() { |
160 | VAOptLoc = SourceLocation(); |
161 | NumOfTokensPriorToVAOpt = -1; |
162 | LeadingSpaceForStringifiedToken = false; |
163 | StringifyBefore = false; |
164 | CharifyBefore = false; |
165 | } |
166 | |
167 | const Token &getEOFTok() const { return SyntheticEOFToken; } |
168 | |
169 | void sawHashOrHashAtBefore(const bool HasLeadingSpace, |
170 | const bool IsHashAt) { |
171 | |
172 | StringifyBefore = !IsHashAt; |
173 | CharifyBefore = IsHashAt; |
174 | LeadingSpaceForStringifiedToken = HasLeadingSpace; |
175 | } |
176 | |
177 | |
178 | |
179 | bool hasCharifyBefore() const { |
180 | (0) . __assert_fail ("!isReset() && \"Must only be called if the state has not been reset\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 181, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!isReset() && |
181 | (0) . __assert_fail ("!isReset() && \"Must only be called if the state has not been reset\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 181, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "Must only be called if the state has not been reset"); |
182 | return CharifyBefore; |
183 | } |
184 | bool hasStringifyOrCharifyBefore() const { |
185 | return hasStringifyBefore() || hasCharifyBefore(); |
186 | } |
187 | |
188 | unsigned int getNumberOfTokensPriorToVAOpt() const { |
189 | (0) . __assert_fail ("!isReset() && \"Must only be called if the state has not been reset\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 190, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!isReset() && |
190 | (0) . __assert_fail ("!isReset() && \"Must only be called if the state has not been reset\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 190, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "Must only be called if the state has not been reset"); |
191 | return NumOfTokensPriorToVAOpt; |
192 | } |
193 | |
194 | bool getLeadingSpaceForStringifiedToken() const { |
195 | (0) . __assert_fail ("hasStringifyBefore() && \"Must only be called if this has been marked for stringification\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 196, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(hasStringifyBefore() && |
196 | (0) . __assert_fail ("hasStringifyBefore() && \"Must only be called if this has been marked for stringification\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 196, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "Must only be called if this has been marked for stringification"); |
197 | return LeadingSpaceForStringifiedToken; |
198 | } |
199 | |
200 | void sawVAOptFollowedByOpeningParens(const SourceLocation VAOptLoc, |
201 | const unsigned int NumPriorTokens) { |
202 | (0) . __assert_fail ("VAOptLoc.isFileID() && \"Must not come from a macro expansion\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 202, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(VAOptLoc.isFileID() && "Must not come from a macro expansion"); |
203 | (0) . __assert_fail ("isReset() && \"Must only be called if the state has been reset\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 203, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(isReset() && "Must only be called if the state has been reset"); |
204 | VAOptDefinitionContext::sawVAOptFollowedByOpeningParens(SourceLocation()); |
205 | this->VAOptLoc = VAOptLoc; |
206 | NumOfTokensPriorToVAOpt = NumPriorTokens; |
207 | (0) . __assert_fail ("NumOfTokensPriorToVAOpt > -1 && \"Too many prior tokens\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 208, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(NumOfTokensPriorToVAOpt > -1 && |
208 | (0) . __assert_fail ("NumOfTokensPriorToVAOpt > -1 && \"Too many prior tokens\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 208, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "Too many prior tokens"); |
209 | } |
210 | |
211 | SourceLocation getVAOptLoc() const { |
212 | (0) . __assert_fail ("!isReset() && \"Must only be called if the state has not been reset\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 213, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!isReset() && |
213 | (0) . __assert_fail ("!isReset() && \"Must only be called if the state has not been reset\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 213, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "Must only be called if the state has not been reset"); |
214 | (0) . __assert_fail ("VAOptLoc.isValid() && \"__VA_OPT__ location must be valid\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/VariadicMacroSupport.h", 214, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(VAOptLoc.isValid() && "__VA_OPT__ location must be valid"); |
215 | return VAOptLoc; |
216 | } |
217 | using VAOptDefinitionContext::isVAOptToken; |
218 | using VAOptDefinitionContext::isInVAOpt; |
219 | using VAOptDefinitionContext::sawClosingParen; |
220 | using VAOptDefinitionContext::sawOpeningParen; |
221 | |
222 | }; |
223 | } |
224 | |
225 | #endif |
226 | |