Warning: This file is not a C or C++ file. It does not have highlighting.

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
28namespace clang {
29
30class DefMacroDirective;
31class IdentifierInfo;
32class Module;
33class Preprocessor;
34class 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.
39class 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
124public:
125 /// Return the location that the macro was defined at.
126 SourceLocation getDefinitionLoc() const { return Location; }
127
128 /// Set the location of the last token in the macro.
129 void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; }
130
131 /// Return the location of the last token in the macro.
132 SourceLocation getDefinitionEndLoc() const { return EndLocation; }
133
134 /// Get length in characters of the macro definition.
135 unsigned getDefinitionLength(const SourceManager &SM) const {
136 if (IsDefinitionLengthCached)
137 return DefinitionLength;
138 return getDefinitionLengthSlow(SM);
139 }
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 void setIsBuiltinMacro(bool Val = true) { IsBuiltinMacro = Val; }
153
154 /// Set the value of the IsUsed flag.
155 void setIsUsed(bool Val) { IsUsed = Val; }
156
157 /// Set the value of the IsAllowRedefinitionsWithoutWarning flag.
158 void setIsAllowRedefinitionsWithoutWarning(bool Val) {
159 IsAllowRedefinitionsWithoutWarning = Val;
160 }
161
162 /// Set the value of the IsWarnIfUnused flag.
163 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 llvm::BumpPtrAllocator &PPAllocator) {
169 assert(ParameterList == nullptr && NumParameters == 0 &&
170 "Parameter list already set!");
171 if (List.empty())
172 return;
173
174 NumParameters = List.size();
175 ParameterList = PPAllocator.Allocate<IdentifierInfo *>(List.size());
176 std::copy(List.begin(), List.end(), ParameterList);
177 }
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 bool param_empty() const { return NumParameters == 0; }
183 param_iterator param_begin() const { return ParameterList; }
184 param_iterator param_end() const { return ParameterList + NumParameters; }
185 unsigned getNumParams() const { return NumParameters; }
186 ArrayRef<const IdentifierInfo *> params() const {
187 return ArrayRef<const IdentifierInfo *>(ParameterList, NumParameters);
188 }
189
190 /// Return the parameter number of the specified identifier,
191 /// or -1 if the identifier is not a formal parameter identifier.
192 int getParameterNum(const IdentifierInfo *Arg) const {
193 for (param_iterator I = param_begin(), E = param_end(); I != E; ++I)
194 if (*I == Arg)
195 return I - param_begin();
196 return -1;
197 }
198
199 /// Function/Object-likeness. Keep track of whether this macro has formal
200 /// parameters.
201 void setIsFunctionLike() { IsFunctionLike = true; }
202 bool isFunctionLike() const { return IsFunctionLike; }
203 bool isObjectLike() const { return !IsFunctionLike; }
204
205 /// Varargs querying methods. This can only be set for function-like macros.
206 void setIsC99Varargs() { IsC99Varargs = true; }
207 void setIsGNUVarargs() { IsGNUVarargs = true; }
208 bool isC99Varargs() const { return IsC99Varargs; }
209 bool isGNUVarargs() const { return IsGNUVarargs; }
210 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 bool isBuiltinMacro() const { return IsBuiltinMacro; }
219
220 bool hasCommaPasting() const { return HasCommaPasting; }
221 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 bool isUsed() const { return IsUsed; }
226
227 /// Return true if this macro can be redefined without warning.
228 bool isAllowRedefinitionsWithoutWarning() const {
229 return IsAllowRedefinitionsWithoutWarning;
230 }
231
232 /// Return true if we should emit a warning if the macro is unused.
233 bool isWarnIfUnused() const { return IsWarnIfUnused; }
234
235 /// Return the number of tokens that this macro expands to.
236 unsigned getNumTokens() const { return NumReplacementTokens; }
237
238 const Token &getReplacementToken(unsigned Tok) const {
239 assert(Tok < NumReplacementTokens && "Invalid token #");
240 return ReplacementTokens[Tok];
241 }
242
243 using const_tokens_iterator = const Token *;
244
245 const_tokens_iterator tokens_begin() const { return ReplacementTokens; }
246 const_tokens_iterator tokens_end() const {
247 return ReplacementTokens + NumReplacementTokens;
248 }
249 bool tokens_empty() const { return NumReplacementTokens == 0; }
250 ArrayRef<Token> tokens() const {
251 return llvm::makeArrayRef(ReplacementTokens, NumReplacementTokens);
252 }
253
254 llvm::MutableArrayRef<Token>
255 allocateTokens(unsigned NumTokens, llvm::BumpPtrAllocator &PPAllocator) {
256 assert(ReplacementTokens == nullptr && NumReplacementTokens == 0 &&
257 "Token list already allocated!");
258 NumReplacementTokens = NumTokens;
259 Token *NewReplacementTokens = PPAllocator.Allocate<Token>(NumTokens);
260 ReplacementTokens = NewReplacementTokens;
261 return llvm::makeMutableArrayRef(NewReplacementTokens, NumTokens);
262 }
263
264 void setTokens(ArrayRef<Token> Tokens, llvm::BumpPtrAllocator &PPAllocator) {
265 assert(
266 !IsDefinitionLengthCached &&
267 "Changing replacement tokens after definition length got calculated");
268 assert(ReplacementTokens == nullptr && NumReplacementTokens == 0 &&
269 "Token list already set!");
270 if (Tokens.empty())
271 return;
272
273 NumReplacementTokens = Tokens.size();
274 Token *NewReplacementTokens = PPAllocator.Allocate<Token>(Tokens.size());
275 std::copy(Tokens.begin(), Tokens.end(), NewReplacementTokens);
276 ReplacementTokens = NewReplacementTokens;
277 }
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 bool isEnabled() const { return !IsDisabled; }
283
284 void EnableMacro() {
285 assert(IsDisabled && "Cannot enable an already-enabled macro!");
286 IsDisabled = false;
287 }
288
289 void DisableMacro() {
290 assert(!IsDisabled && "Cannot disable an already-disabled macro!");
291 IsDisabled = true;
292 }
293
294 /// Determine whether this macro was used for a header guard.
295 bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; }
296
297 void setUsedForHeaderGuard(bool Val) { UsedForHeaderGuard = Val; }
298
299 void dump() const;
300
301private:
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.
314class MacroDirective {
315public:
316 enum Kind {
317 MD_Define,
318 MD_Undefine,
319 MD_Visibility
320 };
321
322protected:
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 : Loc(Loc), MDKind(K), IsFromPCH(false), IsPublic(true) {}
342
343public:
344 Kind getKind() const { return Kind(MDKind); }
345
346 SourceLocation getLocation() const { return Loc; }
347
348 /// Set previous definition of the macro with the same name.
349 void setPrevious(MacroDirective *Prev) { Previous = Prev; }
350
351 /// Get previous definition of the macro with the same name.
352 const MacroDirective *getPrevious() const { return Previous; }
353
354 /// Get previous definition of the macro with the same name.
355 MacroDirective *getPrevious() { return Previous; }
356
357 /// Return true if the macro directive was loaded from a PCH file.
358 bool isFromPCH() const { return IsFromPCH; }
359
360 void setIsFromPCH() { IsFromPCH = true; }
361
362 class DefInfo {
363 DefMacroDirective *DefDirective = nullptr;
364 SourceLocation UndefLoc;
365 bool IsPublic = true;
366
367 public:
368 DefInfo() = default;
369 DefInfo(DefMacroDirective *DefDirective, SourceLocation UndefLoc,
370 bool isPublic)
371 : DefDirective(DefDirective), UndefLoc(UndefLoc), IsPublic(isPublic) {}
372
373 const DefMacroDirective *getDirective() const { return DefDirective; }
374 DefMacroDirective *getDirective() { return DefDirective; }
375
376 inline SourceLocation getLocation() const;
377 inline MacroInfo *getMacroInfo();
378
379 const MacroInfo *getMacroInfo() const {
380 return const_cast<DefInfo *>(this)->getMacroInfo();
381 }
382
383 SourceLocation getUndefLocation() const { return UndefLoc; }
384 bool isUndefined() const { return UndefLoc.isValid(); }
385
386 bool isPublic() const { return IsPublic; }
387
388 bool isValid() const { return DefDirective != nullptr; }
389 bool isInvalid() const { return !isValid(); }
390
391 explicit operator bool() const { return isValid(); }
392
393 inline DefInfo getPreviousDefinition();
394
395 const DefInfo getPreviousDefinition() const {
396 return const_cast<DefInfo *>(this)->getPreviousDefinition();
397 }
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 const DefInfo getDefinition() const {
405 return const_cast<MacroDirective *>(this)->getDefinition();
406 }
407
408 bool isDefined() const {
409 if (const DefInfo Def = getDefinition())
410 return !Def.isUndefined();
411 return false;
412 }
413
414 const MacroInfo *getMacroInfo() const {
415 return getDefinition().getMacroInfo();
416 }
417 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 static bool classof(const MacroDirective *) { return true; }
427};
428
429/// A directive for a defined macro or a macro imported from a module.
430class DefMacroDirective : public MacroDirective {
431 MacroInfo *Info;
432
433public:
434 DefMacroDirective(MacroInfo *MI, SourceLocation Loc)
435 : MacroDirective(MD_Define, Loc), Info(MI) {
436 assert(MI && "MacroInfo is null");
437 }
438 explicit DefMacroDirective(MacroInfo *MI)
439 : DefMacroDirective(MI, MI->getDefinitionLoc()) {}
440
441 /// The data for the macro definition.
442 const MacroInfo *getInfo() const { return Info; }
443 MacroInfo *getInfo() { return Info; }
444
445 static bool classof(const MacroDirective *MD) {
446 return MD->getKind() == MD_Define;
447 }
448
449 static bool classof(const DefMacroDirective *) { return true; }
450};
451
452/// A directive for an undefined macro.
453class UndefMacroDirective : public MacroDirective {
454public:
455 explicit UndefMacroDirective(SourceLocation UndefLoc)
456 : MacroDirective(MD_Undefine, UndefLoc) {
457 assert(UndefLoc.isValid() && "Invalid UndefLoc!");
458 }
459
460 static bool classof(const MacroDirective *MD) {
461 return MD->getKind() == MD_Undefine;
462 }
463
464 static bool classof(const UndefMacroDirective *) { return true; }
465};
466
467/// A directive for setting the module visibility of a macro.
468class VisibilityMacroDirective : public MacroDirective {
469public:
470 explicit VisibilityMacroDirective(SourceLocation Loc, bool Public)
471 : MacroDirective(MD_Visibility, Loc) {
472 IsPublic = Public;
473 }
474
475 /// Determine whether this macro is part of the public API of its
476 /// module.
477 bool isPublic() const { return IsPublic; }
478
479 static bool classof(const MacroDirective *MD) {
480 return MD->getKind() == MD_Visibility;
481 }
482
483 static bool classof(const VisibilityMacroDirective *) { return true; }
484};
485
486inline SourceLocation MacroDirective::DefInfo::getLocation() const {
487 if (isInvalid())
488 return {};
489 return DefDirective->getLocation();
490}
491
492inline MacroInfo *MacroDirective::DefInfo::getMacroInfo() {
493 if (isInvalid())
494 return nullptr;
495 return DefDirective->getInfo();
496}
497
498inline MacroDirective::DefInfo
499MacroDirective::DefInfo::getPreviousDefinition() {
500 if (isInvalid() || DefDirective->getPrevious() == nullptr)
501 return {};
502 return DefDirective->getPrevious()->getDefinition();
503}
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.
512class 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 NumOverrides(Overrides.size()) {
534 std::copy(Overrides.begin(), Overrides.end(),
535 reinterpret_cast<ModuleMacro **>(this + 1));
536 }
537
538public:
539 static ModuleMacro *create(Preprocessor &PP, Module *OwningModule,
540 IdentifierInfo *II, MacroInfo *Macro,
541 ArrayRef<ModuleMacro *> Overrides);
542
543 void Profile(llvm::FoldingSetNodeID &ID) const {
544 return Profile(ID, OwningModule, II);
545 }
546
547 static void Profile(llvm::FoldingSetNodeID &ID, Module *OwningModule,
548 const IdentifierInfo *II) {
549 ID.AddPointer(OwningModule);
550 ID.AddPointer(II);
551 }
552
553 /// Get the name of the macro.
554 IdentifierInfo *getName() const { return II; }
555
556 /// Get the ID of the module that exports this macro.
557 Module *getOwningModule() const { return OwningModule; }
558
559 /// Get definition for this exported #define, or nullptr if this
560 /// represents a #undef.
561 MacroInfo *getMacroInfo() const { return Macro; }
562
563 /// Iterators over the overridden module IDs.
564 /// \{
565 using overrides_iterator = ModuleMacro *const *;
566
567 overrides_iterator overrides_begin() const {
568 return reinterpret_cast<overrides_iterator>(this + 1);
569 }
570
571 overrides_iterator overrides_end() const {
572 return overrides_begin() + NumOverrides;
573 }
574
575 ArrayRef<ModuleMacro *> overrides() const {
576 return llvm::makeArrayRef(overrides_begin(), overrides_end());
577 }
578 /// \}
579
580 /// Get the number of macros that override this one.
581 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.
588class MacroDefinition {
589 llvm::PointerIntPair<DefMacroDirective *, 1, bool> LatestLocalAndAmbiguous;
590 ArrayRef<ModuleMacro *> ModuleMacros;
591
592public:
593 MacroDefinition() = default;
594 MacroDefinition(DefMacroDirective *MD, ArrayRef<ModuleMacro *> MMs,
595 bool IsAmbiguous)
596 : LatestLocalAndAmbiguous(MD, IsAmbiguous), ModuleMacros(MMs) {}
597
598 /// Determine whether there is a definition of this macro.
599 explicit operator bool() const {
600 return getLocalDirective() || !ModuleMacros.empty();
601 }
602
603 /// Get the MacroInfo that should be used for this definition.
604 MacroInfo *getMacroInfo() const {
605 if (!ModuleMacros.empty())
606 return ModuleMacros.back()->getMacroInfo();
607 if (auto *MD = getLocalDirective())
608 return MD->getMacroInfo();
609 return nullptr;
610 }
611
612 /// \c true if the definition is ambiguous, \c false otherwise.
613 bool isAmbiguous() const { return LatestLocalAndAmbiguous.getInt(); }
614
615 /// Get the latest non-imported, non-\#undef'd macro definition
616 /// for this macro.
617 DefMacroDirective *getLocalDirective() const {
618 return LatestLocalAndAmbiguous.getPointer();
619 }
620
621 /// Get the active module macros for this macro.
622 ArrayRef<ModuleMacro *> getModuleMacros() const { return ModuleMacros; }
623
624 template <typename Fn> void forAllDefinitions(Fn F) const {
625 if (auto *MD = getLocalDirective())
626 F(MD->getMacroInfo());
627 for (auto *MM : getModuleMacros())
628 F(MM->getMacroInfo());
629 }
630};
631
632} // namespace clang
633
634#endif // LLVM_CLANG_LEX_MACROINFO_H
635

Warning: This file is not a C or C++ file. It does not have highlighting.

source code of clang/include/clang/Lex/MacroInfo.h