clang API Documentation

MacroInfo.h
Go to the documentation of this file.
00001 //===--- MacroInfo.h - Information about #defined identifiers ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the MacroInfo interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_MACROINFO_H
00015 #define LLVM_CLANG_MACROINFO_H
00016 
00017 #include "clang/Lex/Token.h"
00018 #include "llvm/ADT/SmallVector.h"
00019 #include "llvm/Support/Allocator.h"
00020 #include <cassert>
00021 
00022 namespace clang {
00023   class Preprocessor;
00024 
00025 /// MacroInfo - Each identifier that is #define'd has an instance of this class
00026 /// associated with it, used to implement macro expansion.
00027 class MacroInfo {
00028   //===--------------------------------------------------------------------===//
00029   // State set when the macro is defined.
00030 
00031   /// Location - This is the place the macro is defined.
00032   SourceLocation Location;
00033   /// EndLocation - The location of the last token in the macro.
00034   SourceLocation EndLocation;
00035 
00036   /// Arguments - The list of arguments for a function-like macro.  This can be
00037   /// empty, for, e.g. "#define X()".  In a C99-style variadic macro, this
00038   /// includes the __VA_ARGS__ identifier on the list.
00039   IdentifierInfo **ArgumentList;
00040   unsigned NumArguments;
00041 
00042   /// \brief The location at which this macro was either explicitly exported
00043   /// from its module or marked as private.
00044   ///
00045   /// If invalid, this macro has not been explicitly given any visibility.
00046   SourceLocation VisibilityLocation;
00047   
00048   /// ReplacementTokens - This is the list of tokens that the macro is defined
00049   /// to.
00050   SmallVector<Token, 8> ReplacementTokens;
00051 
00052   /// \brief Length in characters of the macro definition.
00053   mutable unsigned DefinitionLength;
00054   mutable bool IsDefinitionLengthCached : 1;
00055 
00056   /// IsFunctionLike - True if this macro is a function-like macro, false if it
00057   /// is an object-like macro.
00058   bool IsFunctionLike : 1;
00059 
00060   /// IsC99Varargs - True if this macro is of the form "#define X(...)" or
00061   /// "#define X(Y,Z,...)".  The __VA_ARGS__ token should be replaced with the
00062   /// contents of "..." in an invocation.
00063   bool IsC99Varargs : 1;
00064 
00065   /// IsGNUVarargs -  True if this macro is of the form "#define X(a...)".  The
00066   /// "a" identifier in the replacement list will be replaced with all arguments
00067   /// of the macro starting with the specified one.
00068   bool IsGNUVarargs : 1;
00069 
00070   /// IsBuiltinMacro - True if this is a builtin macro, such as __LINE__, and if
00071   /// it has not yet been redefined or undefined.
00072   bool IsBuiltinMacro : 1;
00073 
00074   /// IsFromAST - True if this macro was loaded from an AST file.
00075   bool IsFromAST : 1;
00076 
00077   /// \brief Whether this macro changed after it was loaded from an AST file.
00078   bool ChangedAfterLoad : 1;
00079   
00080 private:
00081   //===--------------------------------------------------------------------===//
00082   // State that changes as the macro is used.
00083 
00084   /// IsDisabled - True if we have started an expansion of this macro already.
00085   /// This disbles recursive expansion, which would be quite bad for things like
00086   /// #define A A.
00087   bool IsDisabled : 1;
00088 
00089   /// IsUsed - True if this macro is either defined in the main file and has
00090   /// been used, or if it is not defined in the main file.  This is used to
00091   /// emit -Wunused-macros diagnostics.
00092   bool IsUsed : 1;
00093 
00094   /// AllowRedefinitionsWithoutWarning - True if this macro can be redefined
00095   /// without emitting a warning.
00096   bool IsAllowRedefinitionsWithoutWarning : 1;
00097 
00098   /// \brief Must warn if the macro is unused at the end of translation unit.
00099   bool IsWarnIfUnused : 1;
00100    
00101   /// \brief Whether the macro has public (when described in a module).
00102   bool IsPublic : 1;
00103   
00104    ~MacroInfo() {
00105     assert(ArgumentList == 0 && "Didn't call destroy before dtor!");
00106   }
00107 
00108 public:
00109   MacroInfo(SourceLocation DefLoc);
00110   MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator);
00111   
00112   /// FreeArgumentList - Free the argument list of the macro, restoring it to a
00113   /// state where it can be reused for other devious purposes.
00114   void FreeArgumentList() {
00115     ArgumentList = 0;
00116     NumArguments = 0;
00117   }
00118 
00119   /// Destroy - destroy this MacroInfo object.
00120   void Destroy() {
00121     FreeArgumentList();
00122     this->~MacroInfo();
00123   }
00124 
00125   /// getDefinitionLoc - Return the location that the macro was defined at.
00126   ///
00127   SourceLocation getDefinitionLoc() const { return Location; }
00128 
00129   /// setDefinitionEndLoc - Set the location of the last token in the macro.
00130   ///
00131   void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; }
00132   /// getDefinitionEndLoc - Return the location of the last token in the macro.
00133   ///
00134   SourceLocation getDefinitionEndLoc() const { return EndLocation; }
00135   
00136   /// \brief Get length in characters of the macro definition.
00137   unsigned getDefinitionLength(SourceManager &SM) const {
00138     if (IsDefinitionLengthCached)
00139       return DefinitionLength;
00140     return getDefinitionLengthSlow(SM);
00141   }
00142 
00143   /// isIdenticalTo - Return true if the specified macro definition is equal to
00144   /// this macro in spelling, arguments, and whitespace.  This is used to emit
00145   /// duplicate definition warnings.  This implements the rules in C99 6.10.3.
00146   bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const;
00147 
00148   /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag.
00149   ///
00150   void setIsBuiltinMacro(bool Val = true) {
00151     IsBuiltinMacro = Val;
00152   }
00153 
00154   /// setIsUsed - Set the value of the IsUsed flag.
00155   ///
00156   void setIsUsed(bool Val) {
00157     IsUsed = Val;
00158   }
00159 
00160   /// setIsAllowRedefinitionsWithoutWarning - Set the value of the 
00161   /// IsAllowRedefinitionsWithoutWarning flag.
00162   void setIsAllowRedefinitionsWithoutWarning(bool Val) {
00163     IsAllowRedefinitionsWithoutWarning = Val;
00164   }
00165 
00166   /// \brief Set the value of the IsWarnIfUnused flag.
00167   void setIsWarnIfUnused(bool val) {
00168     IsWarnIfUnused = val;
00169   }
00170 
00171   /// setArgumentList - Set the specified list of identifiers as the argument
00172   /// list for this macro.
00173   void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs,
00174                        llvm::BumpPtrAllocator &PPAllocator) {
00175     assert(ArgumentList == 0 && NumArguments == 0 &&
00176            "Argument list already set!");
00177     if (NumArgs == 0) return;
00178 
00179     NumArguments = NumArgs;
00180     ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs);
00181     for (unsigned i = 0; i != NumArgs; ++i)
00182       ArgumentList[i] = List[i];
00183   }
00184 
00185   /// Arguments - The list of arguments for a function-like macro.  This can be
00186   /// empty, for, e.g. "#define X()".
00187   typedef IdentifierInfo* const *arg_iterator;
00188   bool arg_empty() const { return NumArguments == 0; }
00189   arg_iterator arg_begin() const { return ArgumentList; }
00190   arg_iterator arg_end() const { return ArgumentList+NumArguments; }
00191   unsigned getNumArgs() const { return NumArguments; }
00192 
00193   /// getArgumentNum - Return the argument number of the specified identifier,
00194   /// or -1 if the identifier is not a formal argument identifier.
00195   int getArgumentNum(IdentifierInfo *Arg) const {
00196     for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I)
00197       if (*I == Arg) return I-arg_begin();
00198     return -1;
00199   }
00200 
00201   /// Function/Object-likeness.  Keep track of whether this macro has formal
00202   /// parameters.
00203   void setIsFunctionLike() { IsFunctionLike = true; }
00204   bool isFunctionLike() const { return IsFunctionLike; }
00205   bool isObjectLike() const { return !IsFunctionLike; }
00206 
00207   /// Varargs querying methods.  This can only be set for function-like macros.
00208   void setIsC99Varargs() { IsC99Varargs = true; }
00209   void setIsGNUVarargs() { IsGNUVarargs = true; }
00210   bool isC99Varargs() const { return IsC99Varargs; }
00211   bool isGNUVarargs() const { return IsGNUVarargs; }
00212   bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; }
00213 
00214   /// isBuiltinMacro - Return true if this macro is a builtin macro, such as
00215   /// __LINE__, which requires processing before expansion.
00216   bool isBuiltinMacro() const { return IsBuiltinMacro; }
00217 
00218   /// isFromAST - Return true if this macro was loaded from an AST file.
00219   bool isFromAST() const { return IsFromAST; }
00220 
00221   /// setIsFromAST - Set whether this macro was loaded from an AST file.
00222   void setIsFromAST(bool FromAST = true) { IsFromAST = FromAST; }
00223 
00224   /// \brief Determine whether this macro has changed since it was loaded from
00225   /// an AST file.
00226   bool hasChangedAfterLoad() const { return ChangedAfterLoad; }
00227   
00228   /// \brief Note whether this macro has changed after it was loaded from an
00229   /// AST file.
00230   void setChangedAfterLoad(bool CAL = true) { ChangedAfterLoad = CAL; }
00231   
00232   /// isUsed - Return false if this macro is defined in the main file and has
00233   /// not yet been used.
00234   bool isUsed() const { return IsUsed; }
00235 
00236   /// isAllowRedefinitionsWithoutWarning - Return true if this macro can be
00237   /// redefined without warning.
00238   bool isAllowRedefinitionsWithoutWarning() const {
00239     return IsAllowRedefinitionsWithoutWarning;
00240   }
00241 
00242   /// \brief Return true if we should emit a warning if the macro is unused.
00243   bool isWarnIfUnused() const {
00244     return IsWarnIfUnused;
00245   }
00246 
00247   /// getNumTokens - Return the number of tokens that this macro expands to.
00248   ///
00249   unsigned getNumTokens() const {
00250     return ReplacementTokens.size();
00251   }
00252 
00253   const Token &getReplacementToken(unsigned Tok) const {
00254     assert(Tok < ReplacementTokens.size() && "Invalid token #");
00255     return ReplacementTokens[Tok];
00256   }
00257 
00258   typedef SmallVector<Token, 8>::const_iterator tokens_iterator;
00259   tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); }
00260   tokens_iterator tokens_end() const { return ReplacementTokens.end(); }
00261   bool tokens_empty() const { return ReplacementTokens.empty(); }
00262 
00263   /// AddTokenToBody - Add the specified token to the replacement text for the
00264   /// macro.
00265   void AddTokenToBody(const Token &Tok) {
00266     assert(!IsDefinitionLengthCached &&
00267           "Changing replacement tokens after definition length got calculated");
00268     ReplacementTokens.push_back(Tok);
00269   }
00270 
00271   /// isEnabled - Return true if this macro is enabled: in other words, that we
00272   /// are not currently in an expansion of this macro.
00273   bool isEnabled() const { return !IsDisabled; }
00274 
00275   void EnableMacro() {
00276     assert(IsDisabled && "Cannot enable an already-enabled macro!");
00277     IsDisabled = false;
00278   }
00279 
00280   void DisableMacro() {
00281     assert(!IsDisabled && "Cannot disable an already-disabled macro!");
00282     IsDisabled = true;
00283   }
00284 
00285   /// \brief Set the export location for this macro.
00286   void setVisibility(bool Public, SourceLocation Loc) {
00287     VisibilityLocation = Loc;
00288     IsPublic = Public;
00289   }
00290 
00291   /// \brief Determine whether this macro is part of the public API of its
00292   /// module.
00293   bool isPublic() const { return IsPublic; }
00294   
00295   /// \brief Determine the location where this macro was explicitly made
00296   /// public or private within its module.
00297   SourceLocation getVisibilityLocation() { return VisibilityLocation; }
00298   
00299 private:
00300   unsigned getDefinitionLengthSlow(SourceManager &SM) const;
00301 };
00302 
00303 }  // end namespace clang
00304 
00305 #endif