/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Lex/VariadicMacroSupport.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===- VariadicMacroSupport.h - state machines and scope guards -*- C++ -*-===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file defines support types to help with preprocessing variadic macro |
10 | | // (i.e. macros that use: ellipses __VA_ARGS__ ) definitions and |
11 | | // expansions. |
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 | | /// An RAII class that tracks when the Preprocessor starts and stops lexing |
25 | | /// the definition of a (ISO C/C++) variadic macro. As an example, this is |
26 | | /// useful for unpoisoning and repoisoning certain identifiers (such as |
27 | | /// __VA_ARGS__) that are only allowed in this context. Also, being a friend |
28 | | /// of the Preprocessor class allows it to access PP's cached identifiers |
29 | | /// directly (as opposed to performing a lookup each time). |
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 | 45.1M | Ident__VA_OPT__(PP.Ident__VA_OPT__) { |
39 | 45.1M | assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned " |
40 | 45.1M | "outside an ISO C/C++ variadic " |
41 | 45.1M | "macro definition!"); |
42 | 0 | assert(Ident__VA_OPT__->isPoisoned() && "__VA_OPT__ should be poisoned!"); |
43 | 45.1M | } |
44 | | |
45 | | /// Client code should call this function just before the Preprocessor is |
46 | | /// about to Lex tokens from the definition of a variadic (ISO C/C++) macro. |
47 | 344k | void enterScope() { |
48 | 344k | Ident__VA_ARGS__->setIsPoisoned(false); |
49 | 344k | Ident__VA_OPT__->setIsPoisoned(false); |
50 | 344k | } |
51 | | |
52 | | /// Client code should call this function as soon as the Preprocessor has |
53 | | /// either completed lexing the macro's definition tokens, or an error |
54 | | /// occurred and the context is being exited. This function is idempotent |
55 | | /// (might be explicitly called, and then reinvoked via the destructor). |
56 | 45.1M | void exitScope() { |
57 | 45.1M | Ident__VA_ARGS__->setIsPoisoned(true); |
58 | 45.1M | Ident__VA_OPT__->setIsPoisoned(true); |
59 | 45.1M | } |
60 | | |
61 | 45.1M | ~VariadicMacroScopeGuard() { exitScope(); } |
62 | | }; |
63 | | |
64 | | /// A class for tracking whether we're inside a VA_OPT during a |
65 | | /// traversal of the tokens of a variadic macro definition. |
66 | | class VAOptDefinitionContext { |
67 | | /// Contains all the locations of so far unmatched lparens. |
68 | | SmallVector<SourceLocation, 8> UnmatchedOpeningParens; |
69 | | |
70 | | const IdentifierInfo *const Ident__VA_OPT__; |
71 | | |
72 | | |
73 | | public: |
74 | | VAOptDefinitionContext(Preprocessor &PP) |
75 | 27.1M | : Ident__VA_OPT__(PP.Ident__VA_OPT__) {} |
76 | | |
77 | 260M | bool isVAOptToken(const Token &T) const { |
78 | 260M | return Ident__VA_OPT__ && T.getIdentifierInfo() == Ident__VA_OPT__; |
79 | 260M | } |
80 | | |
81 | | /// Returns true if we have seen the __VA_OPT__ and '(' but before having |
82 | | /// seen the matching ')'. |
83 | 263M | bool isInVAOpt() const { return UnmatchedOpeningParens.size(); } |
84 | | |
85 | | /// Call this function as soon as you see __VA_OPT__ and '('. |
86 | 239 | void sawVAOptFollowedByOpeningParens(const SourceLocation LParenLoc) { |
87 | 239 | assert(!isInVAOpt() && "Must NOT be within VAOPT context to call this"); |
88 | 0 | UnmatchedOpeningParens.push_back(LParenLoc); |
89 | | |
90 | 239 | } |
91 | | |
92 | 6 | SourceLocation getUnmatchedOpeningParenLoc() const { |
93 | 6 | assert(isInVAOpt() && "Must be within VAOPT context to call this"); |
94 | 0 | return UnmatchedOpeningParens.back(); |
95 | 6 | } |
96 | | |
97 | | /// Call this function each time an rparen is seen. It returns true only if |
98 | | /// the rparen that was just seen was the eventual (non-nested) closing |
99 | | /// paren for VAOPT, and ejects us out of the VAOPT context. |
100 | 227 | bool sawClosingParen() { |
101 | 227 | assert(isInVAOpt() && "Must be within VAOPT context to call this"); |
102 | 0 | UnmatchedOpeningParens.pop_back(); |
103 | 227 | return !UnmatchedOpeningParens.size(); |
104 | 227 | } |
105 | | |
106 | | /// Call this function each time an lparen is seen. |
107 | 15 | void sawOpeningParen(SourceLocation LParenLoc) { |
108 | 15 | assert(isInVAOpt() && "Must be within VAOPT context to call this"); |
109 | 0 | UnmatchedOpeningParens.push_back(LParenLoc); |
110 | 15 | } |
111 | | |
112 | | /// Are we at the top level within the __VA_OPT__? |
113 | 387k | bool isAtTopLevel() const { return UnmatchedOpeningParens.size() == 1; } |
114 | | }; |
115 | | |
116 | | /// A class for tracking whether we're inside a VA_OPT during a |
117 | | /// traversal of the tokens of a macro during macro expansion. |
118 | | class VAOptExpansionContext : VAOptDefinitionContext { |
119 | | |
120 | | Token SyntheticEOFToken; |
121 | | |
122 | | // The (spelling) location of the current __VA_OPT__ in the replacement list |
123 | | // of the function-like macro being expanded. |
124 | | SourceLocation VAOptLoc; |
125 | | |
126 | | // NumOfTokensPriorToVAOpt : when != -1, contains the index *of* the first |
127 | | // token of the current VAOPT contents (so we know where to start eager |
128 | | // token-pasting and stringification) *within* the substituted tokens of |
129 | | // the function-like macro's new replacement list. |
130 | | int NumOfTokensPriorToVAOpt = -1; |
131 | | |
132 | | unsigned LeadingSpaceForStringifiedToken : 1; |
133 | | |
134 | | unsigned StringifyBefore : 1; |
135 | | unsigned CharifyBefore : 1; |
136 | | unsigned BeginsWithPlaceholder : 1; |
137 | | unsigned EndsWithPlaceholder : 1; |
138 | | |
139 | 172 | bool hasStringifyBefore() const { |
140 | 172 | assert(!isReset() && |
141 | 172 | "Must only be called if the state has not been reset"); |
142 | 0 | return StringifyBefore; |
143 | 172 | } |
144 | | |
145 | 724 | bool isReset() const { |
146 | 724 | return NumOfTokensPriorToVAOpt == -1 || |
147 | 724 | VAOptLoc.isInvalid()595 ; |
148 | 724 | } |
149 | | |
150 | | public: |
151 | | VAOptExpansionContext(Preprocessor &PP) |
152 | | : VAOptDefinitionContext(PP), LeadingSpaceForStringifiedToken(false), |
153 | | StringifyBefore(false), CharifyBefore(false), |
154 | 24.3M | BeginsWithPlaceholder(false), EndsWithPlaceholder(false) { |
155 | 24.3M | SyntheticEOFToken.startToken(); |
156 | 24.3M | SyntheticEOFToken.setKind(tok::eof); |
157 | 24.3M | } |
158 | | |
159 | 129 | void reset() { |
160 | 129 | VAOptLoc = SourceLocation(); |
161 | 129 | NumOfTokensPriorToVAOpt = -1; |
162 | 129 | LeadingSpaceForStringifiedToken = false; |
163 | 129 | StringifyBefore = false; |
164 | 129 | CharifyBefore = false; |
165 | 129 | BeginsWithPlaceholder = false; |
166 | 129 | EndsWithPlaceholder = false; |
167 | 129 | } |
168 | | |
169 | 43 | const Token &getEOFTok() const { return SyntheticEOFToken; } |
170 | | |
171 | | void sawHashOrHashAtBefore(const bool HasLeadingSpace, |
172 | 43 | const bool IsHashAt) { |
173 | | |
174 | 43 | StringifyBefore = !IsHashAt; |
175 | 43 | CharifyBefore = IsHashAt; |
176 | 43 | LeadingSpaceForStringifiedToken = HasLeadingSpace; |
177 | 43 | } |
178 | | |
179 | 7 | void hasPlaceholderAfterHashhashAtStart() { BeginsWithPlaceholder = true; } |
180 | 387k | void hasPlaceholderBeforeRParen() { |
181 | 387k | if (isAtTopLevel()) |
182 | 8 | EndsWithPlaceholder = true; |
183 | 387k | } |
184 | | |
185 | | |
186 | 45 | bool beginsWithPlaceholder() const { |
187 | 45 | assert(!isReset() && |
188 | 45 | "Must only be called if the state has not been reset"); |
189 | 0 | return BeginsWithPlaceholder; |
190 | 45 | } |
191 | 45 | bool endsWithPlaceholder() const { |
192 | 45 | assert(!isReset() && |
193 | 45 | "Must only be called if the state has not been reset"); |
194 | 0 | return EndsWithPlaceholder; |
195 | 45 | } |
196 | | |
197 | 129 | bool hasCharifyBefore() const { |
198 | 129 | assert(!isReset() && |
199 | 129 | "Must only be called if the state has not been reset"); |
200 | 0 | return CharifyBefore; |
201 | 129 | } |
202 | 129 | bool hasStringifyOrCharifyBefore() const { |
203 | 129 | return hasStringifyBefore() || hasCharifyBefore()86 ; |
204 | 129 | } |
205 | | |
206 | 161 | unsigned int getNumberOfTokensPriorToVAOpt() const { |
207 | 161 | assert(!isReset() && |
208 | 161 | "Must only be called if the state has not been reset"); |
209 | 0 | return NumOfTokensPriorToVAOpt; |
210 | 161 | } |
211 | | |
212 | 43 | bool getLeadingSpaceForStringifiedToken() const { |
213 | 43 | assert(hasStringifyBefore() && |
214 | 43 | "Must only be called if this has been marked for stringification"); |
215 | 0 | return LeadingSpaceForStringifiedToken; |
216 | 43 | } |
217 | | |
218 | | void sawVAOptFollowedByOpeningParens(const SourceLocation VAOptLoc, |
219 | 129 | const unsigned int NumPriorTokens) { |
220 | 129 | assert(VAOptLoc.isFileID() && "Must not come from a macro expansion"); |
221 | 0 | assert(isReset() && "Must only be called if the state has been reset"); |
222 | 0 | VAOptDefinitionContext::sawVAOptFollowedByOpeningParens(SourceLocation()); |
223 | 129 | this->VAOptLoc = VAOptLoc; |
224 | 129 | NumOfTokensPriorToVAOpt = NumPriorTokens; |
225 | 129 | assert(NumOfTokensPriorToVAOpt > -1 && |
226 | 129 | "Too many prior tokens"); |
227 | 129 | } |
228 | | |
229 | 43 | SourceLocation getVAOptLoc() const { |
230 | 43 | assert(!isReset() && |
231 | 43 | "Must only be called if the state has not been reset"); |
232 | 0 | assert(VAOptLoc.isValid() && "__VA_OPT__ location must be valid"); |
233 | 0 | return VAOptLoc; |
234 | 43 | } |
235 | | using VAOptDefinitionContext::isVAOptToken; |
236 | | using VAOptDefinitionContext::isInVAOpt; |
237 | | using VAOptDefinitionContext::sawClosingParen; |
238 | | using VAOptDefinitionContext::sawOpeningParen; |
239 | | |
240 | | }; |
241 | | } // end namespace clang |
242 | | |
243 | | #endif |