Home | History | Annotate | Download | only in Lex
      1 //===--- MacroInfo.h - Information about #defined identifiers ---*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines the MacroInfo interface.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CLANG_MACROINFO_H
     15 #define LLVM_CLANG_MACROINFO_H
     16 
     17 #include "clang/Lex/Token.h"
     18 #include "llvm/ADT/SmallVector.h"
     19 #include "llvm/Support/Allocator.h"
     20 #include <cassert>
     21 
     22 namespace clang {
     23   class Preprocessor;
     24 
     25 /// MacroInfo - Each identifier that is #define'd has an instance of this class
     26 /// associated with it, used to implement macro expansion.
     27 class MacroInfo {
     28   //===--------------------------------------------------------------------===//
     29   // State set when the macro is defined.
     30 
     31   /// Location - This is the place the macro is defined.
     32   SourceLocation Location;
     33   /// EndLocation - The location of the last token in the macro.
     34   SourceLocation EndLocation;
     35 
     36   /// Arguments - The list of arguments for a function-like macro.  This can be
     37   /// empty, for, e.g. "#define X()".  In a C99-style variadic macro, this
     38   /// includes the __VA_ARGS__ identifier on the list.
     39   IdentifierInfo **ArgumentList;
     40   unsigned NumArguments;
     41 
     42   /// \brief The location at which this macro was either explicitly exported
     43   /// from its module or marked as private.
     44   ///
     45   /// If invalid, this macro has not been explicitly given any visibility.
     46   SourceLocation VisibilityLocation;
     47 
     48   /// ReplacementTokens - This is the list of tokens that the macro is defined
     49   /// to.
     50   SmallVector<Token, 8> ReplacementTokens;
     51 
     52   /// \brief Length in characters of the macro definition.
     53   mutable unsigned DefinitionLength;
     54   mutable bool IsDefinitionLengthCached : 1;
     55 
     56   /// IsFunctionLike - True if this macro is a function-like macro, false if it
     57   /// is an object-like macro.
     58   bool IsFunctionLike : 1;
     59 
     60   /// IsC99Varargs - True if this macro is of the form "#define X(...)" or
     61   /// "#define X(Y,Z,...)".  The __VA_ARGS__ token should be replaced with the
     62   /// contents of "..." in an invocation.
     63   bool IsC99Varargs : 1;
     64 
     65   /// IsGNUVarargs -  True if this macro is of the form "#define X(a...)".  The
     66   /// "a" identifier in the replacement list will be replaced with all arguments
     67   /// of the macro starting with the specified one.
     68   bool IsGNUVarargs : 1;
     69 
     70   /// IsBuiltinMacro - True if this is a builtin macro, such as __LINE__, and if
     71   /// it has not yet been redefined or undefined.
     72   bool IsBuiltinMacro : 1;
     73 
     74   /// IsFromAST - True if this macro was loaded from an AST file.
     75   bool IsFromAST : 1;
     76 
     77   /// \brief Whether this macro changed after it was loaded from an AST file.
     78   bool ChangedAfterLoad : 1;
     79 
     80 private:
     81   //===--------------------------------------------------------------------===//
     82   // State that changes as the macro is used.
     83 
     84   /// IsDisabled - True if we have started an expansion of this macro already.
     85   /// This disbles recursive expansion, which would be quite bad for things like
     86   /// #define A A.
     87   bool IsDisabled : 1;
     88 
     89   /// IsUsed - True if this macro is either defined in the main file and has
     90   /// been used, or if it is not defined in the main file.  This is used to
     91   /// emit -Wunused-macros diagnostics.
     92   bool IsUsed : 1;
     93 
     94   /// AllowRedefinitionsWithoutWarning - True if this macro can be redefined
     95   /// without emitting a warning.
     96   bool IsAllowRedefinitionsWithoutWarning : 1;
     97 
     98   /// \brief Must warn if the macro is unused at the end of translation unit.
     99   bool IsWarnIfUnused : 1;
    100 
    101   /// \brief Whether the macro has public (when described in a module).
    102   bool IsPublic : 1;
    103 
    104    ~MacroInfo() {
    105     assert(ArgumentList == 0 && "Didn't call destroy before dtor!");
    106   }
    107 
    108 public:
    109   MacroInfo(SourceLocation DefLoc);
    110   MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator);
    111 
    112   /// FreeArgumentList - Free the argument list of the macro, restoring it to a
    113   /// state where it can be reused for other devious purposes.
    114   void FreeArgumentList() {
    115     ArgumentList = 0;
    116     NumArguments = 0;
    117   }
    118 
    119   /// Destroy - destroy this MacroInfo object.
    120   void Destroy() {
    121     FreeArgumentList();
    122     this->~MacroInfo();
    123   }
    124 
    125   /// getDefinitionLoc - Return the location that the macro was defined at.
    126   ///
    127   SourceLocation getDefinitionLoc() const { return Location; }
    128 
    129   /// setDefinitionEndLoc - Set the location of the last token in the macro.
    130   ///
    131   void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; }
    132   /// getDefinitionEndLoc - Return the location of the last token in the macro.
    133   ///
    134   SourceLocation getDefinitionEndLoc() const { return EndLocation; }
    135 
    136   /// \brief Get length in characters of the macro definition.
    137   unsigned getDefinitionLength(SourceManager &SM) const {
    138     if (IsDefinitionLengthCached)
    139       return DefinitionLength;
    140     return getDefinitionLengthSlow(SM);
    141   }
    142 
    143   /// isIdenticalTo - Return true if the specified macro definition is equal to
    144   /// this macro in spelling, arguments, and whitespace.  This is used to emit
    145   /// duplicate definition warnings.  This implements the rules in C99 6.10.3.
    146   bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const;
    147 
    148   /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag.
    149   ///
    150   void setIsBuiltinMacro(bool Val = true) {
    151     IsBuiltinMacro = Val;
    152   }
    153 
    154   /// setIsUsed - Set the value of the IsUsed flag.
    155   ///
    156   void setIsUsed(bool Val) {
    157     IsUsed = Val;
    158   }
    159 
    160   /// setIsAllowRedefinitionsWithoutWarning - Set the value of the
    161   /// IsAllowRedefinitionsWithoutWarning flag.
    162   void setIsAllowRedefinitionsWithoutWarning(bool Val) {
    163     IsAllowRedefinitionsWithoutWarning = Val;
    164   }
    165 
    166   /// \brief Set the value of the IsWarnIfUnused flag.
    167   void setIsWarnIfUnused(bool val) {
    168     IsWarnIfUnused = val;
    169   }
    170 
    171   /// setArgumentList - Set the specified list of identifiers as the argument
    172   /// list for this macro.
    173   void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs,
    174                        llvm::BumpPtrAllocator &PPAllocator) {
    175     assert(ArgumentList == 0 && NumArguments == 0 &&
    176            "Argument list already set!");
    177     if (NumArgs == 0) return;
    178 
    179     NumArguments = NumArgs;
    180     ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs);
    181     for (unsigned i = 0; i != NumArgs; ++i)
    182       ArgumentList[i] = List[i];
    183   }
    184 
    185   /// Arguments - The list of arguments for a function-like macro.  This can be
    186   /// empty, for, e.g. "#define X()".
    187   typedef IdentifierInfo* const *arg_iterator;
    188   bool arg_empty() const { return NumArguments == 0; }
    189   arg_iterator arg_begin() const { return ArgumentList; }
    190   arg_iterator arg_end() const { return ArgumentList+NumArguments; }
    191   unsigned getNumArgs() const { return NumArguments; }
    192 
    193   /// getArgumentNum - Return the argument number of the specified identifier,
    194   /// or -1 if the identifier is not a formal argument identifier.
    195   int getArgumentNum(IdentifierInfo *Arg) const {
    196     for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I)
    197       if (*I == Arg) return I-arg_begin();
    198     return -1;
    199   }
    200 
    201   /// Function/Object-likeness.  Keep track of whether this macro has formal
    202   /// parameters.
    203   void setIsFunctionLike() { IsFunctionLike = true; }
    204   bool isFunctionLike() const { return IsFunctionLike; }
    205   bool isObjectLike() const { return !IsFunctionLike; }
    206 
    207   /// Varargs querying methods.  This can only be set for function-like macros.
    208   void setIsC99Varargs() { IsC99Varargs = true; }
    209   void setIsGNUVarargs() { IsGNUVarargs = true; }
    210   bool isC99Varargs() const { return IsC99Varargs; }
    211   bool isGNUVarargs() const { return IsGNUVarargs; }
    212   bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; }
    213 
    214   /// isBuiltinMacro - Return true if this macro is a builtin macro, such as
    215   /// __LINE__, which requires processing before expansion.
    216   bool isBuiltinMacro() const { return IsBuiltinMacro; }
    217 
    218   /// isFromAST - Return true if this macro was loaded from an AST file.
    219   bool isFromAST() const { return IsFromAST; }
    220 
    221   /// setIsFromAST - Set whether this macro was loaded from an AST file.
    222   void setIsFromAST(bool FromAST = true) { IsFromAST = FromAST; }
    223 
    224   /// \brief Determine whether this macro has changed since it was loaded from
    225   /// an AST file.
    226   bool hasChangedAfterLoad() const { return ChangedAfterLoad; }
    227 
    228   /// \brief Note whether this macro has changed after it was loaded from an
    229   /// AST file.
    230   void setChangedAfterLoad(bool CAL = true) { ChangedAfterLoad = CAL; }
    231 
    232   /// isUsed - Return false if this macro is defined in the main file and has
    233   /// not yet been used.
    234   bool isUsed() const { return IsUsed; }
    235 
    236   /// isAllowRedefinitionsWithoutWarning - Return true if this macro can be
    237   /// redefined without warning.
    238   bool isAllowRedefinitionsWithoutWarning() const {
    239     return IsAllowRedefinitionsWithoutWarning;
    240   }
    241 
    242   /// \brief Return true if we should emit a warning if the macro is unused.
    243   bool isWarnIfUnused() const {
    244     return IsWarnIfUnused;
    245   }
    246 
    247   /// getNumTokens - Return the number of tokens that this macro expands to.
    248   ///
    249   unsigned getNumTokens() const {
    250     return ReplacementTokens.size();
    251   }
    252 
    253   const Token &getReplacementToken(unsigned Tok) const {
    254     assert(Tok < ReplacementTokens.size() && "Invalid token #");
    255     return ReplacementTokens[Tok];
    256   }
    257 
    258   typedef SmallVector<Token, 8>::const_iterator tokens_iterator;
    259   tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); }
    260   tokens_iterator tokens_end() const { return ReplacementTokens.end(); }
    261   bool tokens_empty() const { return ReplacementTokens.empty(); }
    262 
    263   /// AddTokenToBody - Add the specified token to the replacement text for the
    264   /// macro.
    265   void AddTokenToBody(const Token &Tok) {
    266     assert(!IsDefinitionLengthCached &&
    267           "Changing replacement tokens after definition length got calculated");
    268     ReplacementTokens.push_back(Tok);
    269   }
    270 
    271   /// isEnabled - Return true if this macro is enabled: in other words, that we
    272   /// are not currently in an expansion of this macro.
    273   bool isEnabled() const { return !IsDisabled; }
    274 
    275   void EnableMacro() {
    276     assert(IsDisabled && "Cannot enable an already-enabled macro!");
    277     IsDisabled = false;
    278   }
    279 
    280   void DisableMacro() {
    281     assert(!IsDisabled && "Cannot disable an already-disabled macro!");
    282     IsDisabled = true;
    283   }
    284 
    285   /// \brief Set the export location for this macro.
    286   void setVisibility(bool Public, SourceLocation Loc) {
    287     VisibilityLocation = Loc;
    288     IsPublic = Public;
    289   }
    290 
    291   /// \brief Determine whether this macro is part of the public API of its
    292   /// module.
    293   bool isPublic() const { return IsPublic; }
    294 
    295   /// \brief Determine the location where this macro was explicitly made
    296   /// public or private within its module.
    297   SourceLocation getVisibilityLocation() { return VisibilityLocation; }
    298 
    299 private:
    300   unsigned getDefinitionLengthSlow(SourceManager &SM) const;
    301 };
    302 
    303 }  // end namespace clang
    304 
    305 #endif
    306