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