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