/Users/buildslave/jenkins/workspace/coverage/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 and destroy these. |
121 | | MacroInfo(SourceLocation DefLoc); |
122 | | ~MacroInfo() = default; |
123 | | |
124 | | public: |
125 | | /// Return the location that the macro was defined at. |
126 | 133M | SourceLocation getDefinitionLoc() const { return Location; } |
127 | | |
128 | | /// Set the location of the last token in the macro. |
129 | 46.9M | void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; } |
130 | | |
131 | | /// Return the location of the last token in the macro. |
132 | 2.24M | SourceLocation getDefinitionEndLoc() const { return EndLocation; } |
133 | | |
134 | | /// Get length in characters of the macro definition. |
135 | 55.3M | unsigned getDefinitionLength(const SourceManager &SM) const { |
136 | 55.3M | if (IsDefinitionLengthCached) |
137 | 54.8M | return DefinitionLength; |
138 | 548k | return getDefinitionLengthSlow(SM); |
139 | 55.3M | } |
140 | | |
141 | | /// Return true if the specified macro definition is equal to |
142 | | /// this macro in spelling, arguments, and whitespace. |
143 | | /// |
144 | | /// \param Syntactically if true, the macro definitions can be identical even |
145 | | /// if they use different identifiers for the function macro parameters. |
146 | | /// Otherwise the comparison is lexical and this implements the rules in |
147 | | /// C99 6.10.3. |
148 | | bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP, |
149 | | bool Syntactically) const; |
150 | | |
151 | | /// Set or clear the isBuiltinMacro flag. |
152 | 2.39M | void setIsBuiltinMacro(bool Val = true) { IsBuiltinMacro = Val; } |
153 | | |
154 | | /// Set the value of the IsUsed flag. |
155 | 65.0M | void setIsUsed(bool Val) { IsUsed = Val; } |
156 | | |
157 | | /// Set the value of the IsAllowRedefinitionsWithoutWarning flag. |
158 | 56 | void setIsAllowRedefinitionsWithoutWarning(bool Val) { |
159 | 56 | IsAllowRedefinitionsWithoutWarning = Val; |
160 | 56 | } |
161 | | |
162 | | /// Set the value of the IsWarnIfUnused flag. |
163 | 19 | void setIsWarnIfUnused(bool val) { IsWarnIfUnused = val; } |
164 | | |
165 | | /// Set the specified list of identifiers as the parameter list for |
166 | | /// this macro. |
167 | | void setParameterList(ArrayRef<IdentifierInfo *> List, |
168 | 3.07M | llvm::BumpPtrAllocator &PPAllocator) { |
169 | 3.07M | assert(ParameterList == nullptr && NumParameters == 0 && |
170 | 3.07M | "Parameter list already set!"); |
171 | 3.07M | if (List.empty()) |
172 | 3.76k | return; |
173 | | |
174 | 3.07M | NumParameters = List.size(); |
175 | 3.07M | ParameterList = PPAllocator.Allocate<IdentifierInfo *>(List.size()); |
176 | 3.07M | std::copy(List.begin(), List.end(), ParameterList); |
177 | 3.07M | } |
178 | | |
179 | | /// Parameters - The list of parameters for a function-like macro. This can |
180 | | /// be empty, for, e.g. "#define X()". |
181 | | using param_iterator = IdentifierInfo *const *; |
182 | 6.50k | bool param_empty() const { return NumParameters == 0; } |
183 | 131M | param_iterator param_begin() const { return ParameterList; } |
184 | 94.9M | param_iterator param_end() const { return ParameterList + NumParameters; } |
185 | 98.4M | unsigned getNumParams() const { return NumParameters; } |
186 | 2.66M | ArrayRef<const IdentifierInfo *> params() const { |
187 | 2.66M | return ArrayRef<const IdentifierInfo *>(ParameterList, NumParameters); |
188 | 2.66M | } |
189 | | |
190 | | /// Return the parameter number of the specified identifier, |
191 | | /// or -1 if the identifier is not a formal parameter identifier. |
192 | 94.1M | int getParameterNum(const IdentifierInfo *Arg) const { |
193 | 201M | for (param_iterator I = param_begin(), E = param_end(); I != E; ++I107M ) |
194 | 144M | if (*I == Arg) |
195 | 36.4M | return I - param_begin(); |
196 | 57.6M | return -1; |
197 | 94.1M | } |
198 | | |
199 | | /// Function/Object-likeness. Keep track of whether this macro has formal |
200 | | /// parameters. |
201 | 3.10M | void setIsFunctionLike() { IsFunctionLike = true; } |
202 | 211M | bool isFunctionLike() const { return IsFunctionLike; } |
203 | 99.5M | bool isObjectLike() const { return !IsFunctionLike; } |
204 | | |
205 | | /// Varargs querying methods. This can only be set for function-like macros. |
206 | 354k | void setIsC99Varargs() { IsC99Varargs = true; } |
207 | 566 | void setIsGNUVarargs() { IsGNUVarargs = true; } |
208 | 3.95M | bool isC99Varargs() const { return IsC99Varargs; } |
209 | 1.13M | bool isGNUVarargs() const { return IsGNUVarargs; } |
210 | 25.0M | bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; } |
211 | | |
212 | | /// Return true if this macro requires processing before expansion. |
213 | | /// |
214 | | /// This is true only for builtin macro, such as \__LINE__, whose values |
215 | | /// are not given by fixed textual expansions. Regular predefined macros |
216 | | /// from the "<built-in>" buffer are not reported as builtins by this |
217 | | /// function. |
218 | 67.8M | bool isBuiltinMacro() const { return IsBuiltinMacro; } |
219 | | |
220 | 175k | bool hasCommaPasting() const { return HasCommaPasting; } |
221 | 4.01k | void setHasCommaPasting() { HasCommaPasting = true; } |
222 | | |
223 | | /// Return false if this macro is defined in the main file and has |
224 | | /// not yet been used. |
225 | 47.1M | bool isUsed() const { return IsUsed; } |
226 | | |
227 | | /// Return true if this macro can be redefined without warning. |
228 | 300k | bool isAllowRedefinitionsWithoutWarning() const { |
229 | 300k | return IsAllowRedefinitionsWithoutWarning; |
230 | 300k | } |
231 | | |
232 | | /// Return true if we should emit a warning if the macro is unused. |
233 | 64.2M | bool isWarnIfUnused() const { return IsWarnIfUnused; } |
234 | | |
235 | | /// Return the number of tokens that this macro expands to. |
236 | 218M | unsigned getNumTokens() const { return NumReplacementTokens; } |
237 | | |
238 | 152M | const Token &getReplacementToken(unsigned Tok) const { |
239 | 152M | assert(Tok < NumReplacementTokens && "Invalid token #"); |
240 | 0 | return ReplacementTokens[Tok]; |
241 | 152M | } |
242 | | |
243 | | using const_tokens_iterator = const Token *; |
244 | | |
245 | 115M | const_tokens_iterator tokens_begin() const { return ReplacementTokens; } |
246 | 59.0M | const_tokens_iterator tokens_end() const { |
247 | 59.0M | return ReplacementTokens + NumReplacementTokens; |
248 | 59.0M | } |
249 | 654k | bool tokens_empty() const { return NumReplacementTokens == 0; } |
250 | 1.20M | ArrayRef<Token> tokens() const { |
251 | 1.20M | return llvm::makeArrayRef(ReplacementTokens, NumReplacementTokens); |
252 | 1.20M | } |
253 | | |
254 | | llvm::MutableArrayRef<Token> |
255 | 1.73M | allocateTokens(unsigned NumTokens, llvm::BumpPtrAllocator &PPAllocator) { |
256 | 1.73M | assert(ReplacementTokens == nullptr && NumReplacementTokens == 0 && |
257 | 1.73M | "Token list already allocated!"); |
258 | 0 | NumReplacementTokens = NumTokens; |
259 | 1.73M | Token *NewReplacementTokens = PPAllocator.Allocate<Token>(NumTokens); |
260 | 1.73M | ReplacementTokens = NewReplacementTokens; |
261 | 1.73M | return llvm::makeMutableArrayRef(NewReplacementTokens, NumTokens); |
262 | 1.73M | } |
263 | | |
264 | 45.1M | void setTokens(ArrayRef<Token> Tokens, llvm::BumpPtrAllocator &PPAllocator) { |
265 | 45.1M | assert( |
266 | 45.1M | !IsDefinitionLengthCached && |
267 | 45.1M | "Changing replacement tokens after definition length got calculated"); |
268 | 0 | assert(ReplacementTokens == nullptr && NumReplacementTokens == 0 && |
269 | 45.1M | "Token list already set!"); |
270 | 45.1M | if (Tokens.empty()) |
271 | 1.70M | return; |
272 | | |
273 | 43.4M | NumReplacementTokens = Tokens.size(); |
274 | 43.4M | Token *NewReplacementTokens = PPAllocator.Allocate<Token>(Tokens.size()); |
275 | 43.4M | std::copy(Tokens.begin(), Tokens.end(), NewReplacementTokens); |
276 | 43.4M | ReplacementTokens = NewReplacementTokens; |
277 | 43.4M | } |
278 | | |
279 | | /// Return true if this macro is enabled. |
280 | | /// |
281 | | /// In other words, that we are not currently in an expansion of this macro. |
282 | 85.3M | bool isEnabled() const { return !IsDisabled; } |
283 | | |
284 | 55.3M | void EnableMacro() { |
285 | 55.3M | assert(IsDisabled && "Cannot enable an already-enabled macro!"); |
286 | 0 | IsDisabled = false; |
287 | 55.3M | } |
288 | | |
289 | 55.3M | void DisableMacro() { |
290 | 55.3M | assert(!IsDisabled && "Cannot disable an already-disabled macro!"); |
291 | 0 | IsDisabled = true; |
292 | 55.3M | } |
293 | | |
294 | | /// Determine whether this macro was used for a header guard. |
295 | 1.72M | bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; } |
296 | | |
297 | 2.24M | void setUsedForHeaderGuard(bool Val) { UsedForHeaderGuard = Val; } |
298 | | |
299 | | void dump() const; |
300 | | |
301 | | private: |
302 | | friend class Preprocessor; |
303 | | |
304 | | unsigned getDefinitionLengthSlow(const SourceManager &SM) const; |
305 | | }; |
306 | | |
307 | | /// Encapsulates changes to the "macros namespace" (the location where |
308 | | /// the macro name became active, the location where it was undefined, etc.). |
309 | | /// |
310 | | /// MacroDirectives, associated with an identifier, are used to model the macro |
311 | | /// history. Usually a macro definition (MacroInfo) is where a macro name |
312 | | /// becomes active (MacroDirective) but #pragma push_macro / pop_macro can |
313 | | /// create additional DefMacroDirectives for the same MacroInfo. |
314 | | class MacroDirective { |
315 | | public: |
316 | | enum Kind { |
317 | | MD_Define, |
318 | | MD_Undefine, |
319 | | MD_Visibility |
320 | | }; |
321 | | |
322 | | protected: |
323 | | /// Previous macro directive for the same identifier, or nullptr. |
324 | | MacroDirective *Previous = nullptr; |
325 | | |
326 | | SourceLocation Loc; |
327 | | |
328 | | /// MacroDirective kind. |
329 | | unsigned MDKind : 2; |
330 | | |
331 | | /// True if the macro directive was loaded from a PCH file. |
332 | | unsigned IsFromPCH : 1; |
333 | | |
334 | | // Used by VisibilityMacroDirective ----------------------------------------// |
335 | | |
336 | | /// Whether the macro has public visibility (when described in a |
337 | | /// module). |
338 | | unsigned IsPublic : 1; |
339 | | |
340 | | MacroDirective(Kind K, SourceLocation Loc) |
341 | 47.7M | : Loc(Loc), MDKind(K), IsFromPCH(false), IsPublic(true) {} |
342 | | |
343 | | public: |
344 | 583M | Kind getKind() const { return Kind(MDKind); } |
345 | | |
346 | 2.39M | SourceLocation getLocation() const { return Loc; } |
347 | | |
348 | | /// Set previous definition of the macro with the same name. |
349 | 47.6M | void setPrevious(MacroDirective *Prev) { Previous = Prev; } |
350 | | |
351 | | /// Get previous definition of the macro with the same name. |
352 | 0 | const MacroDirective *getPrevious() const { return Previous; } |
353 | | |
354 | | /// Get previous definition of the macro with the same name. |
355 | 48.9M | MacroDirective *getPrevious() { return Previous; } |
356 | | |
357 | | /// Return true if the macro directive was loaded from a PCH file. |
358 | 0 | bool isFromPCH() const { return IsFromPCH; } |
359 | | |
360 | 0 | void setIsFromPCH() { IsFromPCH = true; } |
361 | | |
362 | | class DefInfo { |
363 | | DefMacroDirective *DefDirective = nullptr; |
364 | | SourceLocation UndefLoc; |
365 | | bool IsPublic = true; |
366 | | |
367 | | public: |
368 | 2.73k | DefInfo() = default; |
369 | | DefInfo(DefMacroDirective *DefDirective, SourceLocation UndefLoc, |
370 | | bool isPublic) |
371 | 269M | : DefDirective(DefDirective), UndefLoc(UndefLoc), IsPublic(isPublic) {} |
372 | | |
373 | 0 | const DefMacroDirective *getDirective() const { return DefDirective; } |
374 | 19 | DefMacroDirective *getDirective() { return DefDirective; } |
375 | | |
376 | | inline SourceLocation getLocation() const; |
377 | | inline MacroInfo *getMacroInfo(); |
378 | | |
379 | 1.59M | const MacroInfo *getMacroInfo() const { |
380 | 1.59M | return const_cast<DefInfo *>(this)->getMacroInfo(); |
381 | 1.59M | } |
382 | | |
383 | 258 | SourceLocation getUndefLocation() const { return UndefLoc; } |
384 | 48.5M | bool isUndefined() const { return UndefLoc.isValid(); } |
385 | | |
386 | 0 | bool isPublic() const { return IsPublic; } |
387 | | |
388 | 270M | bool isValid() const { return DefDirective != nullptr; } |
389 | 221M | bool isInvalid() const { return !isValid(); } |
390 | | |
391 | 48.7M | explicit operator bool() const { return isValid(); } |
392 | | |
393 | | inline DefInfo getPreviousDefinition(); |
394 | | |
395 | 0 | const DefInfo getPreviousDefinition() const { |
396 | 0 | return const_cast<DefInfo *>(this)->getPreviousDefinition(); |
397 | 0 | } |
398 | | }; |
399 | | |
400 | | /// Traverses the macro directives history and returns the next |
401 | | /// macro definition directive along with info about its undefined location |
402 | | /// (if there is one) and if it is public or private. |
403 | | DefInfo getDefinition(); |
404 | 49.7M | const DefInfo getDefinition() const { |
405 | 49.7M | return const_cast<MacroDirective *>(this)->getDefinition(); |
406 | 49.7M | } |
407 | | |
408 | 48.4M | bool isDefined() const { |
409 | 48.4M | if (const DefInfo Def = getDefinition()) |
410 | 48.4M | return !Def.isUndefined(); |
411 | 1.10k | return false; |
412 | 48.4M | } |
413 | | |
414 | 1.21M | const MacroInfo *getMacroInfo() const { |
415 | 1.21M | return getDefinition().getMacroInfo(); |
416 | 1.21M | } |
417 | 219M | MacroInfo *getMacroInfo() { return getDefinition().getMacroInfo(); } |
418 | | |
419 | | /// Find macro definition active in the specified source location. If |
420 | | /// this macro was not defined there, return NULL. |
421 | | const DefInfo findDirectiveAtLoc(SourceLocation L, |
422 | | const SourceManager &SM) const; |
423 | | |
424 | | void dump() const; |
425 | | |
426 | 0 | static bool classof(const MacroDirective *) { return true; } |
427 | | }; |
428 | | |
429 | | /// A directive for a defined macro or a macro imported from a module. |
430 | | class DefMacroDirective : public MacroDirective { |
431 | | MacroInfo *Info; |
432 | | |
433 | | public: |
434 | | DefMacroDirective(MacroInfo *MI, SourceLocation Loc) |
435 | 47.6M | : MacroDirective(MD_Define, Loc), Info(MI) { |
436 | 47.6M | assert(MI && "MacroInfo is null"); |
437 | 47.6M | } |
438 | | explicit DefMacroDirective(MacroInfo *MI) |
439 | 0 | : DefMacroDirective(MI, MI->getDefinitionLoc()) {} |
440 | | |
441 | | /// The data for the macro definition. |
442 | 0 | const MacroInfo *getInfo() const { return Info; } |
443 | 222M | MacroInfo *getInfo() { return Info; } |
444 | | |
445 | 426M | static bool classof(const MacroDirective *MD) { |
446 | 426M | return MD->getKind() == MD_Define; |
447 | 426M | } |
448 | | |
449 | 0 | static bool classof(const DefMacroDirective *) { return true; } |
450 | | }; |
451 | | |
452 | | /// A directive for an undefined macro. |
453 | | class UndefMacroDirective : public MacroDirective { |
454 | | public: |
455 | | explicit UndefMacroDirective(SourceLocation UndefLoc) |
456 | 82.3k | : MacroDirective(MD_Undefine, UndefLoc) { |
457 | 82.3k | assert(UndefLoc.isValid() && "Invalid UndefLoc!"); |
458 | 82.3k | } |
459 | | |
460 | 83.1k | static bool classof(const MacroDirective *MD) { |
461 | 83.1k | return MD->getKind() == MD_Undefine; |
462 | 83.1k | } |
463 | | |
464 | 0 | static bool classof(const UndefMacroDirective *) { return true; } |
465 | | }; |
466 | | |
467 | | /// A directive for setting the module visibility of a macro. |
468 | | class VisibilityMacroDirective : public MacroDirective { |
469 | | public: |
470 | | explicit VisibilityMacroDirective(SourceLocation Loc, bool Public) |
471 | 192 | : MacroDirective(MD_Visibility, Loc) { |
472 | 192 | IsPublic = Public; |
473 | 192 | } |
474 | | |
475 | | /// Determine whether this macro is part of the public API of its |
476 | | /// module. |
477 | 693 | bool isPublic() const { return IsPublic; } |
478 | | |
479 | 155M | static bool classof(const MacroDirective *MD) { |
480 | 155M | return MD->getKind() == MD_Visibility; |
481 | 155M | } |
482 | | |
483 | 0 | static bool classof(const VisibilityMacroDirective *) { return true; } |
484 | | }; |
485 | | |
486 | 246k | inline SourceLocation MacroDirective::DefInfo::getLocation() const { |
487 | 246k | if (isInvalid()) |
488 | 0 | return {}; |
489 | 246k | return DefDirective->getLocation(); |
490 | 246k | } |
491 | | |
492 | 221M | inline MacroInfo *MacroDirective::DefInfo::getMacroInfo() { |
493 | 221M | if (isInvalid()) |
494 | 0 | return nullptr; |
495 | 221M | return DefDirective->getInfo(); |
496 | 221M | } |
497 | | |
498 | | inline MacroDirective::DefInfo |
499 | 4.69k | MacroDirective::DefInfo::getPreviousDefinition() { |
500 | 4.69k | if (isInvalid() || DefDirective->getPrevious() == nullptr) |
501 | 2.42k | return {}; |
502 | 2.27k | return DefDirective->getPrevious()->getDefinition(); |
503 | 4.69k | } |
504 | | |
505 | | /// Represents a macro directive exported by a module. |
506 | | /// |
507 | | /// There's an instance of this class for every macro #define or #undef that is |
508 | | /// the final directive for a macro name within a module. These entities also |
509 | | /// represent the macro override graph. |
510 | | /// |
511 | | /// These are stored in a FoldingSet in the preprocessor. |
512 | | class ModuleMacro : public llvm::FoldingSetNode { |
513 | | friend class Preprocessor; |
514 | | |
515 | | /// The name defined by the macro. |
516 | | IdentifierInfo *II; |
517 | | |
518 | | /// The body of the #define, or nullptr if this is a #undef. |
519 | | MacroInfo *Macro; |
520 | | |
521 | | /// The module that exports this macro. |
522 | | Module *OwningModule; |
523 | | |
524 | | /// The number of module macros that override this one. |
525 | | unsigned NumOverriddenBy = 0; |
526 | | |
527 | | /// The number of modules whose macros are directly overridden by this one. |
528 | | unsigned NumOverrides; |
529 | | |
530 | | ModuleMacro(Module *OwningModule, IdentifierInfo *II, MacroInfo *Macro, |
531 | | ArrayRef<ModuleMacro *> Overrides) |
532 | | : II(II), Macro(Macro), OwningModule(OwningModule), |
533 | 2.03M | NumOverrides(Overrides.size()) { |
534 | 2.03M | std::copy(Overrides.begin(), Overrides.end(), |
535 | 2.03M | reinterpret_cast<ModuleMacro **>(this + 1)); |
536 | 2.03M | } |
537 | | |
538 | | public: |
539 | | static ModuleMacro *create(Preprocessor &PP, Module *OwningModule, |
540 | | IdentifierInfo *II, MacroInfo *Macro, |
541 | | ArrayRef<ModuleMacro *> Overrides); |
542 | | |
543 | 6.36M | void Profile(llvm::FoldingSetNodeID &ID) const { |
544 | 6.36M | return Profile(ID, OwningModule, II); |
545 | 6.36M | } |
546 | | |
547 | | static void Profile(llvm::FoldingSetNodeID &ID, Module *OwningModule, |
548 | 8.40M | const IdentifierInfo *II) { |
549 | 8.40M | ID.AddPointer(OwningModule); |
550 | 8.40M | ID.AddPointer(II); |
551 | 8.40M | } |
552 | | |
553 | | /// Get the name of the macro. |
554 | 0 | IdentifierInfo *getName() const { return II; } |
555 | | |
556 | | /// Get the ID of the module that exports this macro. |
557 | 5.88M | Module *getOwningModule() const { return OwningModule; } |
558 | | |
559 | | /// Get definition for this exported #define, or nullptr if this |
560 | | /// represents a #undef. |
561 | 11.0M | MacroInfo *getMacroInfo() const { return Macro; } |
562 | | |
563 | | /// Iterators over the overridden module IDs. |
564 | | /// \{ |
565 | | using overrides_iterator = ModuleMacro *const *; |
566 | | |
567 | 1.40M | overrides_iterator overrides_begin() const { |
568 | 1.40M | return reinterpret_cast<overrides_iterator>(this + 1); |
569 | 1.40M | } |
570 | | |
571 | 702k | overrides_iterator overrides_end() const { |
572 | 702k | return overrides_begin() + NumOverrides; |
573 | 702k | } |
574 | | |
575 | 702k | ArrayRef<ModuleMacro *> overrides() const { |
576 | 702k | return llvm::makeArrayRef(overrides_begin(), overrides_end()); |
577 | 702k | } |
578 | | /// \} |
579 | | |
580 | | /// Get the number of macros that override this one. |
581 | 309k | unsigned getNumOverridingMacros() const { return NumOverriddenBy; } |
582 | | }; |
583 | | |
584 | | /// A description of the current definition of a macro. |
585 | | /// |
586 | | /// The definition of a macro comprises a set of (at least one) defining |
587 | | /// entities, which are either local MacroDirectives or imported ModuleMacros. |
588 | | class MacroDefinition { |
589 | | llvm::PointerIntPair<DefMacroDirective *, 1, bool> LatestLocalAndAmbiguous; |
590 | | ArrayRef<ModuleMacro *> ModuleMacros; |
591 | | |
592 | | public: |
593 | 6.48M | MacroDefinition() = default; |
594 | | MacroDefinition(DefMacroDirective *MD, ArrayRef<ModuleMacro *> MMs, |
595 | | bool IsAmbiguous) |
596 | 159M | : LatestLocalAndAmbiguous(MD, IsAmbiguous), ModuleMacros(MMs) {} |
597 | | |
598 | | /// Determine whether there is a definition of this macro. |
599 | 161M | explicit operator bool() const { |
600 | 161M | return getLocalDirective() || !ModuleMacros.empty()6.50M ; |
601 | 161M | } |
602 | | |
603 | | /// Get the MacroInfo that should be used for this definition. |
604 | 227M | MacroInfo *getMacroInfo() const { |
605 | 227M | if (!ModuleMacros.empty()) |
606 | 5.66M | return ModuleMacros.back()->getMacroInfo(); |
607 | 221M | if (auto *MD = getLocalDirective()) |
608 | 216M | return MD->getMacroInfo(); |
609 | 5.08M | return nullptr; |
610 | 221M | } |
611 | | |
612 | | /// \c true if the definition is ambiguous, \c false otherwise. |
613 | 61.8M | bool isAmbiguous() const { return LatestLocalAndAmbiguous.getInt(); } |
614 | | |
615 | | /// Get the latest non-imported, non-\#undef'd macro definition |
616 | | /// for this macro. |
617 | 382M | DefMacroDirective *getLocalDirective() const { |
618 | 382M | return LatestLocalAndAmbiguous.getPointer(); |
619 | 382M | } |
620 | | |
621 | | /// Get the active module macros for this macro. |
622 | 118 | ArrayRef<ModuleMacro *> getModuleMacros() const { return ModuleMacros; } |
623 | | |
624 | 118 | template <typename Fn> void forAllDefinitions(Fn F) const { |
625 | 118 | if (auto *MD = getLocalDirective()) |
626 | 62 | F(MD->getMacroInfo()); |
627 | 118 | for (auto *MM : getModuleMacros()) |
628 | 99 | F(MM->getMacroInfo()); |
629 | 118 | } 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 Line | Count | Source | 624 | 47 | template <typename Fn> void forAllDefinitions(Fn F) const { | 625 | 47 | if (auto *MD = getLocalDirective()) | 626 | 15 | F(MD->getMacroInfo()); | 627 | 47 | for (auto *MM : getModuleMacros()) | 628 | 79 | F(MM->getMacroInfo()); | 629 | 47 | } |
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 Line | Count | Source | 624 | 71 | template <typename Fn> void forAllDefinitions(Fn F) const { | 625 | 71 | if (auto *MD = getLocalDirective()) | 626 | 47 | F(MD->getMacroInfo()); | 627 | 71 | for (auto *MM : getModuleMacros()) | 628 | 20 | F(MM->getMacroInfo()); | 629 | 71 | } |
|
630 | | }; |
631 | | |
632 | | } // namespace clang |
633 | | |
634 | | #endif // LLVM_CLANG_LEX_MACROINFO_H |