/src/llvm-project/clang/include/clang/Lex/MacroInfo.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===- MacroInfo.h - Information about #defined identifiers -----*- 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 | | /// \file |
10 | | /// Defines the clang::MacroInfo and clang::MacroDirective classes. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #ifndef LLVM_CLANG_LEX_MACROINFO_H |
15 | | #define LLVM_CLANG_LEX_MACROINFO_H |
16 | | |
17 | | #include "clang/Lex/Token.h" |
18 | | #include "clang/Basic/LLVM.h" |
19 | | #include "clang/Basic/SourceLocation.h" |
20 | | #include "llvm/ADT/ArrayRef.h" |
21 | | #include "llvm/ADT/FoldingSet.h" |
22 | | #include "llvm/ADT/PointerIntPair.h" |
23 | | #include "llvm/ADT/SmallVector.h" |
24 | | #include "llvm/Support/Allocator.h" |
25 | | #include <algorithm> |
26 | | #include <cassert> |
27 | | |
28 | | namespace clang { |
29 | | |
30 | | class DefMacroDirective; |
31 | | class IdentifierInfo; |
32 | | class Module; |
33 | | class Preprocessor; |
34 | | class SourceManager; |
35 | | |
36 | | /// Encapsulates the data about a macro definition (e.g. its tokens). |
37 | | /// |
38 | | /// There's an instance of this class for every #define. |
39 | | class MacroInfo { |
40 | | //===--------------------------------------------------------------------===// |
41 | | // State set when the macro is defined. |
42 | | |
43 | | /// The location the macro is defined. |
44 | | SourceLocation Location; |
45 | | |
46 | | /// The location of the last token in the macro. |
47 | | SourceLocation EndLocation; |
48 | | |
49 | | /// The list of arguments for a function-like macro. |
50 | | /// |
51 | | /// ParameterList points to the first of NumParameters pointers. |
52 | | /// |
53 | | /// This can be empty, for, e.g. "#define X()". In a C99-style variadic |
54 | | /// macro, this includes the \c __VA_ARGS__ identifier on the list. |
55 | | IdentifierInfo **ParameterList = nullptr; |
56 | | |
57 | | /// This is the list of tokens that the macro is defined to. |
58 | | const Token *ReplacementTokens = nullptr; |
59 | | |
60 | | /// \see ParameterList |
61 | | unsigned NumParameters = 0; |
62 | | |
63 | | /// \see ReplacementTokens |
64 | | unsigned NumReplacementTokens = 0; |
65 | | |
66 | | /// Length in characters of the macro definition. |
67 | | mutable unsigned DefinitionLength; |
68 | | mutable bool IsDefinitionLengthCached : 1; |
69 | | |
70 | | /// True if this macro is function-like, false if it is object-like. |
71 | | bool IsFunctionLike : 1; |
72 | | |
73 | | /// True if this macro is of the form "#define X(...)" or |
74 | | /// "#define X(Y,Z,...)". |
75 | | /// |
76 | | /// The __VA_ARGS__ token should be replaced with the contents of "..." in an |
77 | | /// invocation. |
78 | | bool IsC99Varargs : 1; |
79 | | |
80 | | /// True if this macro is of the form "#define X(a...)". |
81 | | /// |
82 | | /// The "a" identifier in the replacement list will be replaced with all |
83 | | /// arguments of the macro starting with the specified one. |
84 | | bool IsGNUVarargs : 1; |
85 | | |
86 | | /// True if this macro requires processing before expansion. |
87 | | /// |
88 | | /// This is the case for builtin macros such as __LINE__, so long as they have |
89 | | /// not been redefined, but not for regular predefined macros from the |
90 | | /// "<built-in>" memory buffer (see Preprocessing::getPredefinesFileID). |
91 | | bool IsBuiltinMacro : 1; |
92 | | |
93 | | /// Whether this macro contains the sequence ", ## __VA_ARGS__" |
94 | | bool HasCommaPasting : 1; |
95 | | |
96 | | //===--------------------------------------------------------------------===// |
97 | | // State that changes as the macro is used. |
98 | | |
99 | | /// True if we have started an expansion of this macro already. |
100 | | /// |
101 | | /// This disables recursive expansion, which would be quite bad for things |
102 | | /// like \#define A A. |
103 | | bool IsDisabled : 1; |
104 | | |
105 | | /// True if this macro is either defined in the main file and has |
106 | | /// been used, or if it is not defined in the main file. |
107 | | /// |
108 | | /// This is used to emit -Wunused-macros diagnostics. |
109 | | bool IsUsed : 1; |
110 | | |
111 | | /// True if this macro can be redefined without emitting a warning. |
112 | | bool IsAllowRedefinitionsWithoutWarning : 1; |
113 | | |
114 | | /// Must warn if the macro is unused at the end of translation unit. |
115 | | bool IsWarnIfUnused : 1; |
116 | | |
117 | | /// Whether this macro was used as header guard. |
118 | | bool UsedForHeaderGuard : 1; |
119 | | |
120 | | // Only the Preprocessor gets to create these. |
121 | | MacroInfo(SourceLocation DefLoc); |
122 | | |
123 | | public: |
124 | | /// Return the location that the macro was defined at. |
125 | 56.5k | SourceLocation getDefinitionLoc() const { return Location; } |
126 | | |
127 | | /// Set the location of the last token in the macro. |
128 | 18.4k | void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; } |
129 | | |
130 | | /// Return the location of the last token in the macro. |
131 | 0 | SourceLocation getDefinitionEndLoc() const { return EndLocation; } |
132 | | |
133 | | /// Get length in characters of the macro definition. |
134 | 0 | unsigned getDefinitionLength(const SourceManager &SM) const { |
135 | 0 | if (IsDefinitionLengthCached) |
136 | 0 | return DefinitionLength; |
137 | 0 | return getDefinitionLengthSlow(SM); |
138 | 0 | } |
139 | | |
140 | | /// Return true if the specified macro definition is equal to |
141 | | /// this macro in spelling, arguments, and whitespace. |
142 | | /// |
143 | | /// \param Syntactically if true, the macro definitions can be identical even |
144 | | /// if they use different identifiers for the function macro parameters. |
145 | | /// Otherwise the comparison is lexical and this implements the rules in |
146 | | /// C99 6.10.3. |
147 | | bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP, |
148 | | bool Syntactically) const; |
149 | | |
150 | | /// Set or clear the isBuiltinMacro flag. |
151 | 1.33k | void setIsBuiltinMacro(bool Val = true) { IsBuiltinMacro = Val; } |
152 | | |
153 | | /// Set the value of the IsUsed flag. |
154 | 3 | void setIsUsed(bool Val) { IsUsed = Val; } |
155 | | |
156 | | /// Set the value of the IsAllowRedefinitionsWithoutWarning flag. |
157 | 0 | void setIsAllowRedefinitionsWithoutWarning(bool Val) { |
158 | 0 | IsAllowRedefinitionsWithoutWarning = Val; |
159 | 0 | } |
160 | | |
161 | | /// Set the value of the IsWarnIfUnused flag. |
162 | 0 | void setIsWarnIfUnused(bool val) { IsWarnIfUnused = val; } |
163 | | |
164 | | /// Set the specified list of identifiers as the parameter list for |
165 | | /// this macro. |
166 | | void setParameterList(ArrayRef<IdentifierInfo *> List, |
167 | 23 | llvm::BumpPtrAllocator &PPAllocator) { |
168 | 23 | assert(ParameterList == nullptr && NumParameters == 0 && |
169 | 23 | "Parameter list already set!"); |
170 | 23 | if (List.empty()) |
171 | 0 | return; |
172 | | |
173 | 23 | NumParameters = List.size(); |
174 | 23 | ParameterList = PPAllocator.Allocate<IdentifierInfo *>(List.size()); |
175 | 23 | std::copy(List.begin(), List.end(), ParameterList); |
176 | 23 | } |
177 | | |
178 | | /// Parameters - The list of parameters for a function-like macro. This can |
179 | | /// be empty, for, e.g. "#define X()". |
180 | | using param_iterator = IdentifierInfo *const *; |
181 | 0 | bool param_empty() const { return NumParameters == 0; } |
182 | 0 | param_iterator param_begin() const { return ParameterList; } |
183 | 0 | param_iterator param_end() const { return ParameterList + NumParameters; } |
184 | 0 | unsigned getNumParams() const { return NumParameters; } |
185 | 0 | ArrayRef<const IdentifierInfo *> params() const { |
186 | 0 | return ArrayRef<const IdentifierInfo *>(ParameterList, NumParameters); |
187 | 0 | } |
188 | | |
189 | | /// Return the parameter number of the specified identifier, |
190 | | /// or -1 if the identifier is not a formal parameter identifier. |
191 | 0 | int getParameterNum(const IdentifierInfo *Arg) const { |
192 | 0 | for (param_iterator I = param_begin(), E = param_end(); I != E; ++I) |
193 | 0 | if (*I == Arg) |
194 | 0 | return I - param_begin(); |
195 | 0 | return -1; |
196 | 0 | } |
197 | | |
198 | | /// Function/Object-likeness. Keep track of whether this macro has formal |
199 | | /// parameters. |
200 | 23 | void setIsFunctionLike() { IsFunctionLike = true; } |
201 | 6 | bool isFunctionLike() const { return IsFunctionLike; } |
202 | 18.4k | bool isObjectLike() const { return !IsFunctionLike; } |
203 | | |
204 | | /// Varargs querying methods. This can only be set for function-like macros. |
205 | 0 | void setIsC99Varargs() { IsC99Varargs = true; } |
206 | 0 | void setIsGNUVarargs() { IsGNUVarargs = true; } |
207 | 23 | bool isC99Varargs() const { return IsC99Varargs; } |
208 | 0 | bool isGNUVarargs() const { return IsGNUVarargs; } |
209 | 0 | bool isVariadic() const { return IsC99Varargs || IsGNUVarargs; } |
210 | | |
211 | | /// Return true if this macro requires processing before expansion. |
212 | | /// |
213 | | /// This is true only for builtin macro, such as \__LINE__, whose values |
214 | | /// are not given by fixed textual expansions. Regular predefined macros |
215 | | /// from the "<built-in>" buffer are not reported as builtins by this |
216 | | /// function. |
217 | 3 | bool isBuiltinMacro() const { return IsBuiltinMacro; } |
218 | | |
219 | 0 | bool hasCommaPasting() const { return HasCommaPasting; } |
220 | 0 | void setHasCommaPasting() { HasCommaPasting = true; } |
221 | | |
222 | | /// Return false if this macro is defined in the main file and has |
223 | | /// not yet been used. |
224 | 18.4k | bool isUsed() const { return IsUsed; } |
225 | | |
226 | | /// Return true if this macro can be redefined without warning. |
227 | 0 | bool isAllowRedefinitionsWithoutWarning() const { |
228 | 0 | return IsAllowRedefinitionsWithoutWarning; |
229 | 0 | } |
230 | | |
231 | | /// Return true if we should emit a warning if the macro is unused. |
232 | 3 | bool isWarnIfUnused() const { return IsWarnIfUnused; } |
233 | | |
234 | | /// Return the number of tokens that this macro expands to. |
235 | 18.4k | unsigned getNumTokens() const { return NumReplacementTokens; } |
236 | | |
237 | 36.0k | const Token &getReplacementToken(unsigned Tok) const { |
238 | 36.0k | assert(Tok < NumReplacementTokens && "Invalid token #"); |
239 | 0 | return ReplacementTokens[Tok]; |
240 | 36.0k | } |
241 | | |
242 | | using const_tokens_iterator = const Token *; |
243 | | |
244 | 0 | const_tokens_iterator tokens_begin() const { return ReplacementTokens; } |
245 | 0 | const_tokens_iterator tokens_end() const { |
246 | 0 | return ReplacementTokens + NumReplacementTokens; |
247 | 0 | } |
248 | 0 | bool tokens_empty() const { return NumReplacementTokens == 0; } |
249 | 0 | ArrayRef<Token> tokens() const { |
250 | 0 | return llvm::ArrayRef(ReplacementTokens, NumReplacementTokens); |
251 | 0 | } |
252 | | |
253 | | llvm::MutableArrayRef<Token> |
254 | 0 | allocateTokens(unsigned NumTokens, llvm::BumpPtrAllocator &PPAllocator) { |
255 | 0 | assert(ReplacementTokens == nullptr && NumReplacementTokens == 0 && |
256 | 0 | "Token list already allocated!"); |
257 | 0 | NumReplacementTokens = NumTokens; |
258 | 0 | Token *NewReplacementTokens = PPAllocator.Allocate<Token>(NumTokens); |
259 | 0 | ReplacementTokens = NewReplacementTokens; |
260 | 0 | return llvm::MutableArrayRef(NewReplacementTokens, NumTokens); |
261 | 0 | } |
262 | | |
263 | 18.4k | void setTokens(ArrayRef<Token> Tokens, llvm::BumpPtrAllocator &PPAllocator) { |
264 | 18.4k | assert( |
265 | 18.4k | !IsDefinitionLengthCached && |
266 | 18.4k | "Changing replacement tokens after definition length got calculated"); |
267 | 0 | assert(ReplacementTokens == nullptr && NumReplacementTokens == 0 && |
268 | 18.4k | "Token list already set!"); |
269 | 18.4k | if (Tokens.empty()) |
270 | 368 | return; |
271 | | |
272 | 18.0k | NumReplacementTokens = Tokens.size(); |
273 | 18.0k | Token *NewReplacementTokens = PPAllocator.Allocate<Token>(Tokens.size()); |
274 | 18.0k | std::copy(Tokens.begin(), Tokens.end(), NewReplacementTokens); |
275 | 18.0k | ReplacementTokens = NewReplacementTokens; |
276 | 18.0k | } |
277 | | |
278 | | /// Return true if this macro is enabled. |
279 | | /// |
280 | | /// In other words, that we are not currently in an expansion of this macro. |
281 | 3 | bool isEnabled() const { return !IsDisabled; } |
282 | | |
283 | 0 | void EnableMacro() { |
284 | 0 | assert(IsDisabled && "Cannot enable an already-enabled macro!"); |
285 | 0 | IsDisabled = false; |
286 | 0 | } |
287 | | |
288 | 0 | void DisableMacro() { |
289 | 0 | assert(!IsDisabled && "Cannot disable an already-disabled macro!"); |
290 | 0 | IsDisabled = true; |
291 | 0 | } |
292 | | |
293 | | /// Determine whether this macro was used for a header guard. |
294 | 0 | bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; } |
295 | | |
296 | 0 | void setUsedForHeaderGuard(bool Val) { UsedForHeaderGuard = Val; } |
297 | | |
298 | | void dump() const; |
299 | | |
300 | | private: |
301 | | friend class Preprocessor; |
302 | | |
303 | | unsigned getDefinitionLengthSlow(const SourceManager &SM) const; |
304 | | }; |
305 | | |
306 | | /// Encapsulates changes to the "macros namespace" (the location where |
307 | | /// the macro name became active, the location where it was undefined, etc.). |
308 | | /// |
309 | | /// MacroDirectives, associated with an identifier, are used to model the macro |
310 | | /// history. Usually a macro definition (MacroInfo) is where a macro name |
311 | | /// becomes active (MacroDirective) but #pragma push_macro / pop_macro can |
312 | | /// create additional DefMacroDirectives for the same MacroInfo. |
313 | | class MacroDirective { |
314 | | public: |
315 | | enum Kind { |
316 | | MD_Define, |
317 | | MD_Undefine, |
318 | | MD_Visibility |
319 | | }; |
320 | | |
321 | | protected: |
322 | | /// Previous macro directive for the same identifier, or nullptr. |
323 | | MacroDirective *Previous = nullptr; |
324 | | |
325 | | SourceLocation Loc; |
326 | | |
327 | | /// MacroDirective kind. |
328 | | LLVM_PREFERRED_TYPE(Kind) |
329 | | unsigned MDKind : 2; |
330 | | |
331 | | /// True if the macro directive was loaded from a PCH file. |
332 | | LLVM_PREFERRED_TYPE(bool) |
333 | | unsigned IsFromPCH : 1; |
334 | | |
335 | | // Used by VisibilityMacroDirective ----------------------------------------// |
336 | | |
337 | | /// Whether the macro has public visibility (when described in a |
338 | | /// module). |
339 | | LLVM_PREFERRED_TYPE(bool) |
340 | | unsigned IsPublic : 1; |
341 | | |
342 | | MacroDirective(Kind K, SourceLocation Loc) |
343 | 19.7k | : Loc(Loc), MDKind(K), IsFromPCH(false), IsPublic(true) {} |
344 | | |
345 | | public: |
346 | 20.0k | Kind getKind() const { return Kind(MDKind); } |
347 | | |
348 | 0 | SourceLocation getLocation() const { return Loc; } |
349 | | |
350 | | /// Set previous definition of the macro with the same name. |
351 | 19.7k | void setPrevious(MacroDirective *Prev) { Previous = Prev; } |
352 | | |
353 | | /// Get previous definition of the macro with the same name. |
354 | 0 | const MacroDirective *getPrevious() const { return Previous; } |
355 | | |
356 | | /// Get previous definition of the macro with the same name. |
357 | 19.7k | MacroDirective *getPrevious() { return Previous; } |
358 | | |
359 | | /// Return true if the macro directive was loaded from a PCH file. |
360 | 0 | bool isFromPCH() const { return IsFromPCH; } |
361 | | |
362 | 0 | void setIsFromPCH() { IsFromPCH = true; } |
363 | | |
364 | | class DefInfo { |
365 | | DefMacroDirective *DefDirective = nullptr; |
366 | | SourceLocation UndefLoc; |
367 | | bool IsPublic = true; |
368 | | |
369 | | public: |
370 | 0 | DefInfo() = default; |
371 | | DefInfo(DefMacroDirective *DefDirective, SourceLocation UndefLoc, |
372 | | bool isPublic) |
373 | 19.8k | : DefDirective(DefDirective), UndefLoc(UndefLoc), IsPublic(isPublic) {} |
374 | | |
375 | 0 | const DefMacroDirective *getDirective() const { return DefDirective; } |
376 | 0 | DefMacroDirective *getDirective() { return DefDirective; } |
377 | | |
378 | | inline SourceLocation getLocation() const; |
379 | | inline MacroInfo *getMacroInfo(); |
380 | | |
381 | 0 | const MacroInfo *getMacroInfo() const { |
382 | 0 | return const_cast<DefInfo *>(this)->getMacroInfo(); |
383 | 0 | } |
384 | | |
385 | 0 | SourceLocation getUndefLocation() const { return UndefLoc; } |
386 | 19.7k | bool isUndefined() const { return UndefLoc.isValid(); } |
387 | | |
388 | 0 | bool isPublic() const { return IsPublic; } |
389 | | |
390 | 19.8k | bool isValid() const { return DefDirective != nullptr; } |
391 | 98 | bool isInvalid() const { return !isValid(); } |
392 | | |
393 | 19.7k | explicit operator bool() const { return isValid(); } |
394 | | |
395 | | inline DefInfo getPreviousDefinition(); |
396 | | |
397 | 0 | const DefInfo getPreviousDefinition() const { |
398 | 0 | return const_cast<DefInfo *>(this)->getPreviousDefinition(); |
399 | 0 | } |
400 | | }; |
401 | | |
402 | | /// Traverses the macro directives history and returns the next |
403 | | /// macro definition directive along with info about its undefined location |
404 | | /// (if there is one) and if it is public or private. |
405 | | DefInfo getDefinition(); |
406 | 19.7k | const DefInfo getDefinition() const { |
407 | 19.7k | return const_cast<MacroDirective *>(this)->getDefinition(); |
408 | 19.7k | } |
409 | | |
410 | 19.7k | bool isDefined() const { |
411 | 19.7k | if (const DefInfo Def = getDefinition()) |
412 | 19.7k | return !Def.isUndefined(); |
413 | 0 | return false; |
414 | 19.7k | } |
415 | | |
416 | 0 | const MacroInfo *getMacroInfo() const { |
417 | 0 | return getDefinition().getMacroInfo(); |
418 | 0 | } |
419 | 98 | MacroInfo *getMacroInfo() { return getDefinition().getMacroInfo(); } |
420 | | |
421 | | /// Find macro definition active in the specified source location. If |
422 | | /// this macro was not defined there, return NULL. |
423 | | const DefInfo findDirectiveAtLoc(SourceLocation L, |
424 | | const SourceManager &SM) const; |
425 | | |
426 | | void dump() const; |
427 | | |
428 | 0 | static bool classof(const MacroDirective *) { return true; } |
429 | | }; |
430 | | |
431 | | /// A directive for a defined macro or a macro imported from a module. |
432 | | class DefMacroDirective : public MacroDirective { |
433 | | MacroInfo *Info; |
434 | | |
435 | | public: |
436 | | DefMacroDirective(MacroInfo *MI, SourceLocation Loc) |
437 | 19.7k | : MacroDirective(MD_Define, Loc), Info(MI) { |
438 | 19.7k | assert(MI && "MacroInfo is null"); |
439 | 19.7k | } |
440 | | explicit DefMacroDirective(MacroInfo *MI) |
441 | 0 | : DefMacroDirective(MI, MI->getDefinitionLoc()) {} |
442 | | |
443 | | /// The data for the macro definition. |
444 | 0 | const MacroInfo *getInfo() const { return Info; } |
445 | 98 | MacroInfo *getInfo() { return Info; } |
446 | | |
447 | 19.9k | static bool classof(const MacroDirective *MD) { |
448 | 19.9k | return MD->getKind() == MD_Define; |
449 | 19.9k | } |
450 | | |
451 | 0 | static bool classof(const DefMacroDirective *) { return true; } |
452 | | }; |
453 | | |
454 | | /// A directive for an undefined macro. |
455 | | class UndefMacroDirective : public MacroDirective { |
456 | | public: |
457 | | explicit UndefMacroDirective(SourceLocation UndefLoc) |
458 | 0 | : MacroDirective(MD_Undefine, UndefLoc) { |
459 | 0 | assert(UndefLoc.isValid() && "Invalid UndefLoc!"); |
460 | 0 | } |
461 | | |
462 | 0 | static bool classof(const MacroDirective *MD) { |
463 | 0 | return MD->getKind() == MD_Undefine; |
464 | 0 | } |
465 | | |
466 | 0 | static bool classof(const UndefMacroDirective *) { return true; } |
467 | | }; |
468 | | |
469 | | /// A directive for setting the module visibility of a macro. |
470 | | class VisibilityMacroDirective : public MacroDirective { |
471 | | public: |
472 | | explicit VisibilityMacroDirective(SourceLocation Loc, bool Public) |
473 | 0 | : MacroDirective(MD_Visibility, Loc) { |
474 | 0 | IsPublic = Public; |
475 | 0 | } |
476 | | |
477 | | /// Determine whether this macro is part of the public API of its |
478 | | /// module. |
479 | 0 | bool isPublic() const { return IsPublic; } |
480 | | |
481 | 95 | static bool classof(const MacroDirective *MD) { |
482 | 95 | return MD->getKind() == MD_Visibility; |
483 | 95 | } |
484 | | |
485 | 0 | static bool classof(const VisibilityMacroDirective *) { return true; } |
486 | | }; |
487 | | |
488 | 0 | inline SourceLocation MacroDirective::DefInfo::getLocation() const { |
489 | 0 | if (isInvalid()) |
490 | 0 | return {}; |
491 | 0 | return DefDirective->getLocation(); |
492 | 0 | } |
493 | | |
494 | 98 | inline MacroInfo *MacroDirective::DefInfo::getMacroInfo() { |
495 | 98 | if (isInvalid()) |
496 | 0 | return nullptr; |
497 | 98 | return DefDirective->getInfo(); |
498 | 98 | } |
499 | | |
500 | | inline MacroDirective::DefInfo |
501 | 0 | MacroDirective::DefInfo::getPreviousDefinition() { |
502 | 0 | if (isInvalid() || DefDirective->getPrevious() == nullptr) |
503 | 0 | return {}; |
504 | 0 | return DefDirective->getPrevious()->getDefinition(); |
505 | 0 | } |
506 | | |
507 | | /// Represents a macro directive exported by a module. |
508 | | /// |
509 | | /// There's an instance of this class for every macro #define or #undef that is |
510 | | /// the final directive for a macro name within a module. These entities also |
511 | | /// represent the macro override graph. |
512 | | /// |
513 | | /// These are stored in a FoldingSet in the preprocessor. |
514 | | class ModuleMacro : public llvm::FoldingSetNode { |
515 | | friend class Preprocessor; |
516 | | |
517 | | /// The name defined by the macro. |
518 | | IdentifierInfo *II; |
519 | | |
520 | | /// The body of the #define, or nullptr if this is a #undef. |
521 | | MacroInfo *Macro; |
522 | | |
523 | | /// The module that exports this macro. |
524 | | Module *OwningModule; |
525 | | |
526 | | /// The number of module macros that override this one. |
527 | | unsigned NumOverriddenBy = 0; |
528 | | |
529 | | /// The number of modules whose macros are directly overridden by this one. |
530 | | unsigned NumOverrides; |
531 | | |
532 | | ModuleMacro(Module *OwningModule, IdentifierInfo *II, MacroInfo *Macro, |
533 | | ArrayRef<ModuleMacro *> Overrides) |
534 | | : II(II), Macro(Macro), OwningModule(OwningModule), |
535 | 0 | NumOverrides(Overrides.size()) { |
536 | 0 | std::copy(Overrides.begin(), Overrides.end(), |
537 | 0 | reinterpret_cast<ModuleMacro **>(this + 1)); |
538 | 0 | } |
539 | | |
540 | | public: |
541 | | static ModuleMacro *create(Preprocessor &PP, Module *OwningModule, |
542 | | IdentifierInfo *II, MacroInfo *Macro, |
543 | | ArrayRef<ModuleMacro *> Overrides); |
544 | | |
545 | 0 | void Profile(llvm::FoldingSetNodeID &ID) const { |
546 | 0 | return Profile(ID, OwningModule, II); |
547 | 0 | } |
548 | | |
549 | | static void Profile(llvm::FoldingSetNodeID &ID, Module *OwningModule, |
550 | 0 | const IdentifierInfo *II) { |
551 | 0 | ID.AddPointer(OwningModule); |
552 | 0 | ID.AddPointer(II); |
553 | 0 | } |
554 | | |
555 | | /// Get the name of the macro. |
556 | 0 | IdentifierInfo *getName() const { return II; } |
557 | | |
558 | | /// Get the ID of the module that exports this macro. |
559 | 0 | Module *getOwningModule() const { return OwningModule; } |
560 | | |
561 | | /// Get definition for this exported #define, or nullptr if this |
562 | | /// represents a #undef. |
563 | 0 | MacroInfo *getMacroInfo() const { return Macro; } |
564 | | |
565 | | /// Iterators over the overridden module IDs. |
566 | | /// \{ |
567 | | using overrides_iterator = ModuleMacro *const *; |
568 | | |
569 | 0 | overrides_iterator overrides_begin() const { |
570 | 0 | return reinterpret_cast<overrides_iterator>(this + 1); |
571 | 0 | } |
572 | | |
573 | 0 | overrides_iterator overrides_end() const { |
574 | 0 | return overrides_begin() + NumOverrides; |
575 | 0 | } |
576 | | |
577 | 0 | ArrayRef<ModuleMacro *> overrides() const { |
578 | 0 | return llvm::ArrayRef(overrides_begin(), overrides_end()); |
579 | 0 | } |
580 | | /// \} |
581 | | |
582 | | /// Get the number of macros that override this one. |
583 | 0 | unsigned getNumOverridingMacros() const { return NumOverriddenBy; } |
584 | | }; |
585 | | |
586 | | /// A description of the current definition of a macro. |
587 | | /// |
588 | | /// The definition of a macro comprises a set of (at least one) defining |
589 | | /// entities, which are either local MacroDirectives or imported ModuleMacros. |
590 | | class MacroDefinition { |
591 | | llvm::PointerIntPair<DefMacroDirective *, 1, bool> LatestLocalAndAmbiguous; |
592 | | ArrayRef<ModuleMacro *> ModuleMacros; |
593 | | |
594 | | public: |
595 | 4 | MacroDefinition() = default; |
596 | | MacroDefinition(DefMacroDirective *MD, ArrayRef<ModuleMacro *> MMs, |
597 | | bool IsAmbiguous) |
598 | 95 | : LatestLocalAndAmbiguous(MD, IsAmbiguous), ModuleMacros(MMs) {} |
599 | | |
600 | | /// Determine whether there is a definition of this macro. |
601 | 99 | explicit operator bool() const { |
602 | 99 | return getLocalDirective() || !ModuleMacros.empty(); |
603 | 99 | } |
604 | | |
605 | | /// Get the MacroInfo that should be used for this definition. |
606 | 98 | MacroInfo *getMacroInfo() const { |
607 | 98 | if (!ModuleMacros.empty()) |
608 | 0 | return ModuleMacros.back()->getMacroInfo(); |
609 | 98 | if (auto *MD = getLocalDirective()) |
610 | 98 | return MD->getMacroInfo(); |
611 | 0 | return nullptr; |
612 | 98 | } |
613 | | |
614 | | /// \c true if the definition is ambiguous, \c false otherwise. |
615 | 3 | bool isAmbiguous() const { return LatestLocalAndAmbiguous.getInt(); } |
616 | | |
617 | | /// Get the latest non-imported, non-\#undef'd macro definition |
618 | | /// for this macro. |
619 | 197 | DefMacroDirective *getLocalDirective() const { |
620 | 197 | return LatestLocalAndAmbiguous.getPointer(); |
621 | 197 | } |
622 | | |
623 | | /// Get the active module macros for this macro. |
624 | 0 | ArrayRef<ModuleMacro *> getModuleMacros() const { return ModuleMacros; } |
625 | | |
626 | 0 | template <typename Fn> void forAllDefinitions(Fn F) const { |
627 | 0 | if (auto *MD = getLocalDirective()) |
628 | 0 | F(MD->getMacroInfo()); |
629 | 0 | for (auto *MM : getModuleMacros()) |
630 | 0 | F(MM->getMacroInfo()); |
631 | 0 | } Unexecuted instantiation: PPMacroExpansion.cpp:void clang::MacroDefinition::forAllDefinitions<clang::Preprocessor::HandleMacroExpandedIdentifier(clang::Token&, clang::MacroDefinition const&)::$_1>(clang::Preprocessor::HandleMacroExpandedIdentifier(clang::Token&, clang::MacroDefinition const&)::$_1) const Unexecuted instantiation: PreprocessingRecord.cpp:void clang::MacroDefinition::forAllDefinitions<clang::PreprocessingRecord::MacroUndefined(clang::Token const&, clang::MacroDefinition const&, clang::MacroDirective const*)::$_0>(clang::PreprocessingRecord::MacroUndefined(clang::Token const&, clang::MacroDefinition const&, clang::MacroDirective const*)::$_0) const |
632 | | }; |
633 | | |
634 | | } // namespace clang |
635 | | |
636 | | #endif // LLVM_CLANG_LEX_MACROINFO_H |