zcov: / include/clang/Lex/MacroInfo.h


Files: 1 Branches Taken: 68.8% 11 / 16
Generated: 2010-02-10 01:31 Branches Executed: 100.0% 16 / 16
Line Coverage: 100.0% 65 / 65


Programs: 22 Runs 35974


       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 <vector>
      21                 : #include <cassert>
      22                 : 
      23                 : namespace clang {
      24                 :   class Preprocessor;
      25                 : 
      26                 : /// MacroInfo - Each identifier that is #define'd has an instance of this class
      27                 : /// associated with it, used to implement macro expansion.
      28                 : class MacroInfo {
      29                 :   //===--------------------------------------------------------------------===//
      30                 :   // State set when the macro is defined.
      31                 : 
      32                 :   /// Location - This is the place the macro is defined.
      33                 :   SourceLocation Location;
      34                 :   /// EndLocation - The location of the last token in the macro.
      35                 :   SourceLocation EndLocation;
      36                 : 
      37                 :   /// Arguments - The list of arguments for a function-like macro.  This can be
      38                 :   /// empty, for, e.g. "#define X()".  In a C99-style variadic macro, this
      39                 :   /// includes the __VA_ARGS__ identifier on the list.
      40                 :   IdentifierInfo **ArgumentList;
      41                 :   unsigned NumArguments;
      42                 : 
      43                 :   /// ReplacementTokens - This is the list of tokens that the macro is defined
      44                 :   /// to.
      45                 :   llvm::SmallVector<Token, 8> ReplacementTokens;
      46                 : 
      47                 :   /// IsFunctionLike - True if this macro is a function-like macro, false if it
      48                 :   /// is an object-like macro.
      49                 :   bool IsFunctionLike : 1;
      50                 : 
      51                 :   /// IsC99Varargs - True if this macro is of the form "#define X(...)" or
      52                 :   /// "#define X(Y,Z,...)".  The __VA_ARGS__ token should be replaced with the
      53                 :   /// contents of "..." in an invocation.
      54                 :   bool IsC99Varargs : 1;
      55                 : 
      56                 :   /// IsGNUVarargs -  True if this macro is of the form "#define X(a...)".  The
      57                 :   /// "a" identifier in the replacement list will be replaced with all arguments
      58                 :   /// of the macro starting with the specified one.
      59                 :   bool IsGNUVarargs : 1;
      60                 : 
      61                 :   /// IsBuiltinMacro - True if this is a builtin macro, such as __LINE__, and if
      62                 :   /// it has not yet been redefined or undefined.
      63                 :   bool IsBuiltinMacro : 1;
      64                 : 
      65                 : private:
      66                 :   //===--------------------------------------------------------------------===//
      67                 :   // State that changes as the macro is used.
      68                 : 
      69                 :   /// IsDisabled - True if we have started an expansion of this macro already.
      70                 :   /// This disbles recursive expansion, which would be quite bad for things like
      71                 :   /// #define A A.
      72                 :   bool IsDisabled : 1;
      73                 : 
      74                 :   /// IsUsed - True if this macro is either defined in the main file and has
      75                 :   /// been used, or if it is not defined in the main file.  This is used to
      76                 :   /// emit -Wunused-macros diagnostics.
      77                 :   bool IsUsed : 1;
      78                 : 
      79           302412:   ~MacroInfo() {
                        0: branch 0 not taken
                   302412: branch 1 taken
      80           302412:     assert(ArgumentList == 0 && "Didn't call destroy before dtor!");
      81           302412:   }
      82                 : 
      83                 : public:
      84                 :   MacroInfo(SourceLocation DefLoc);
      85                 : 
      86                 :   /// FreeArgumentList - Free the argument list of the macro, restoring it to a
      87                 :   /// state where it can be reused for other devious purposes.
      88           303165:   void FreeArgumentList(llvm::BumpPtrAllocator &PPAllocator) {
      89           303165:     PPAllocator.Deallocate(ArgumentList);
      90           303165:     ArgumentList = 0;
      91           303165:     NumArguments = 0;
      92           303165:   }
      93                 : 
      94                 :   /// Destroy - destroy this MacroInfo object.
      95           302412:   void Destroy(llvm::BumpPtrAllocator &PPAllocator) {
      96           302412:     FreeArgumentList(PPAllocator);
      97           302412:     this->~MacroInfo();
      98           302412:   }
      99                 : 
     100                 :   /// getDefinitionLoc - Return the location that the macro was defined at.
     101                 :   ///
     102             4873:   SourceLocation getDefinitionLoc() const { return Location; }
     103                 : 
     104                 :   /// setDefinitionEndLoc - Set the location of the last token in the macro.
     105                 :   ///
     106           269941:   void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; }
     107                 :   /// getDefinitionEndLoc - Return the location of the last token in the macro.
     108                 :   ///
     109                 :   SourceLocation getDefinitionEndLoc() const { return EndLocation; }
     110                 : 
     111                 :   /// isIdenticalTo - Return true if the specified macro definition is equal to
     112                 :   /// this macro in spelling, arguments, and whitespace.  This is used to emit
     113                 :   /// duplicate definition warnings.  This implements the rules in C99 6.10.3.
     114                 :   bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const;
     115                 : 
     116                 :   /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag.
     117                 :   ///
     118            32929:   void setIsBuiltinMacro(bool Val = true) {
     119            32929:     IsBuiltinMacro = Val;
     120            32929:   }
     121                 : 
     122                 :   /// setIsUsed - Set the value of the IsUsed flag.
     123                 :   ///
     124            14197:   void setIsUsed(bool Val) {
     125            14197:     IsUsed = Val;
     126            14197:   }
     127                 : 
     128                 :   /// setArgumentList - Set the specified list of identifiers as the argument
     129                 :   /// list for this macro.
     130                 :   void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs,
     131             1991:                        llvm::BumpPtrAllocator &PPAllocator) {
     132                 :     assert(ArgumentList == 0 && NumArguments == 0 &&
                     1991: branch 0 taken
                        0: branch 1 not taken
                        0: branch 2 not taken
                     1991: branch 3 taken
     133             1991:            "Argument list already set!");
                     1991: branch 0 taken
                        0: branch 1 not taken
     134             1991:     if (NumArgs == 0) return;
     135                 : 
     136             1991:     NumArguments = NumArgs;
     137             1991:     ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs);
                     3034: branch 0 taken
                     1991: branch 1 taken
     138             5025:     for (unsigned i = 0; i != NumArgs; ++i)
     139             3034:       ArgumentList[i] = List[i];
     140                 :   }
     141                 : 
     142                 :   /// Arguments - The list of arguments for a function-like macro.  This can be
     143                 :   /// empty, for, e.g. "#define X()".
     144                 :   typedef IdentifierInfo* const *arg_iterator;
     145               10:   bool arg_empty() const { return NumArguments == 0; }
     146            20500:   arg_iterator arg_begin() const { return ArgumentList; }
     147            13658:   arg_iterator arg_end() const { return ArgumentList+NumArguments; }
     148            13276:   unsigned getNumArgs() const { return NumArguments; }
     149                 : 
     150                 :   /// getArgumentNum - Return the argument number of the specified identifier,
     151                 :   /// or -1 if the identifier is not a formal argument identifier.
     152            13233:   int getArgumentNum(IdentifierInfo *Arg) const {
                     6413: branch 3 taken
     153            25713:     for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I)
                     6820: branch 0 taken
                    12480: branch 1 taken
     154            19300:       if (*I == Arg) return I-arg_begin();
     155             6413:     return -1;
     156                 :   }
     157                 : 
     158                 :   /// Function/Object-likeness.  Keep track of whether this macro has formal
     159                 :   /// parameters.
     160             2033:   void setIsFunctionLike() { IsFunctionLike = true; }
     161            29575:   bool isFunctionLike() const { return IsFunctionLike; }
     162           275748:   bool isObjectLike() const { return !IsFunctionLike; }
     163                 : 
     164                 :   /// Varargs querying methods.  This can only be set for function-like macros.
     165               35:   void setIsC99Varargs() { IsC99Varargs = true; }
     166               10:   void setIsGNUVarargs() { IsGNUVarargs = true; }
     167             2050:   bool isC99Varargs() const { return IsC99Varargs; }
     168               37:   bool isGNUVarargs() const { return IsGNUVarargs; }
     169             4080:   bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; }
     170                 : 
     171                 :   /// isBuiltinMacro - Return true if this macro is a builtin macro, such as
     172                 :   /// __LINE__, which requires processing before expansion.
     173            33485:   bool isBuiltinMacro() const { return IsBuiltinMacro; }
     174                 : 
     175                 :   /// isUsed - Return false if this macro is defined in the main file and has
     176                 :   /// not yet been used.
     177             4814:   bool isUsed() const { return IsUsed; }
     178                 : 
     179                 :   /// getNumTokens - Return the number of tokens that this macro expands to.
     180                 :   ///
     181           296804:   unsigned getNumTokens() const {
     182           296804:     return ReplacementTokens.size();
     183                 :   }
     184                 : 
     185           541520:   const Token &getReplacementToken(unsigned Tok) const {
                   541520: branch 1 taken
                        0: branch 2 not taken
     186           541520:     assert(Tok < ReplacementTokens.size() && "Invalid token #");
     187           541520:     return ReplacementTokens[Tok];
     188                 :   }
     189                 : 
     190                 :   typedef llvm::SmallVector<Token, 8>::const_iterator tokens_iterator;
     191            24331:   tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); }
     192            12201:   tokens_iterator tokens_end() const { return ReplacementTokens.end(); }
     193             4686:   bool tokens_empty() const { return ReplacementTokens.empty(); }
     194                 : 
     195                 :   /// AddTokenToBody - Add the specified token to the replacement text for the
     196                 :   /// macro.
     197           354349:   void AddTokenToBody(const Token &Tok) {
     198           354349:     ReplacementTokens.push_back(Tok);
     199           354349:   }
     200                 : 
     201                 :   /// isEnabled - Return true if this macro is enabled: in other words, that we
     202                 :   /// are not currently in an expansion of this macro.
     203            14191:   bool isEnabled() const { return !IsDisabled; }
     204                 : 
     205             7511:   void EnableMacro() {
     206             7511:     assert(IsDisabled && "Cannot enable an already-enabled macro!");
     207             7511:     IsDisabled = false;
     208             7511:   }
     209                 : 
     210             7515:   void DisableMacro() {
                     7515: branch 1 taken
     211             7515:     assert(!IsDisabled && "Cannot disable an already-disabled macro!");
     212             7515:     IsDisabled = true;
     213             7515:   }
     214                 : };
     215                 : 
     216                 : }  // end namespace clang
     217                 : 
     218                 : #endif

Generated: 2010-02-10 01:31 by zcov