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


Files: 1 Branches Taken: 66.7% 22 / 33
Generated: 2010-02-10 01:31 Branches Executed: 78.8% 26 / 33
Line Coverage: 100.0% 79 / 79


Programs: 75 Runs 125667


       1                 : //===--- Token.h - Token interface ------------------------------*- 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 Token interface.
      11                 : //
      12                 : //===----------------------------------------------------------------------===//
      13                 : 
      14                 : #ifndef LLVM_CLANG_TOKEN_H
      15                 : #define LLVM_CLANG_TOKEN_H
      16                 : 
      17                 : #include "clang/Basic/TemplateKinds.h"
      18                 : #include "clang/Basic/TokenKinds.h"
      19                 : #include "clang/Basic/SourceLocation.h"
      20                 : #include "clang/Basic/OperatorKinds.h"
      21                 : #include <cstdlib>
      22                 : 
      23                 : namespace clang {
      24                 : 
      25                 : class IdentifierInfo;
      26                 : 
      27                 : /// Token - This structure provides full information about a lexed token.
      28                 : /// It is not intended to be space efficient, it is intended to return as much
      29                 : /// information as possible about each returned token.  This is expected to be
      30                 : /// compressed into a smaller form if memory footprint is important.
      31                 : ///
      32                 : /// The parser can create a special "annotation token" representing a stream of
      33                 : /// tokens that were parsed and semantically resolved, e.g.: "foo::MyClass<int>"
      34                 : /// can be represented by a single typename annotation token that carries
      35                 : /// information about the SourceRange of the tokens and the type object.
      36          1343094: class Token {
      37                 :   /// The location of the token.
      38                 :   SourceLocation Loc;
      39                 : 
      40                 :   // Conceptually these next two fields could be in a union.  However, this
      41                 :   // causes gcc 4.2 to pessimize LexTokenInternal, a very performance critical
      42                 :   // routine. Keeping as separate members with casts until a more beautiful fix
      43                 :   // presents itself.
      44                 : 
      45                 :   /// UintData - This holds either the length of the token text, when
      46                 :   /// a normal token, or the end of the SourceRange when an annotation
      47                 :   /// token.
      48                 :   unsigned UintData;
      49                 : 
      50                 :   /// PtrData - This is a union of four different pointer types, which depends
      51                 :   /// on what type of token this is:
      52                 :   ///  Identifiers, keywords, etc:
      53                 :   ///    This is an IdentifierInfo*, which contains the uniqued identifier
      54                 :   ///    spelling.
      55                 :   ///  Literals:  isLiteral() returns true.
      56                 :   ///    This is a pointer to the start of the token in a text buffer, which
      57                 :   ///    may be dirty (have trigraphs / escaped newlines).
      58                 :   ///  Annotations (resolved type names, C++ scopes, etc): isAnnotation().
      59                 :   ///    This is a pointer to sema-specific data for the annotation token.
      60                 :   ///  Other:
      61                 :   ///    This is null.
      62                 :   void *PtrData;
      63                 : 
      64                 :   /// Kind - The actual flavor of token this is.
      65                 :   ///
      66                 :   unsigned char Kind; // DON'T make Kind a 'tok::TokenKind';
      67                 :                       // MSVC will treat it as a signed char and
      68                 :                       // TokenKinds > 127 won't be handled correctly.
      69                 : 
      70                 :   /// Flags - Bits we track about this token, members of the TokenFlags enum.
      71                 :   unsigned char Flags;
      72                 : public:
      73                 : 
      74                 :   // Various flags set per token:
      75                 :   enum TokenFlags {
      76                 :     StartOfLine   = 0x01,  // At start of line or only after whitespace.
      77                 :     LeadingSpace  = 0x02,  // Whitespace exists before this token.
      78                 :     DisableExpand = 0x04,  // This identifier may never be macro expanded.
      79                 :     NeedsCleaning = 0x08   // Contained an escaped newline or trigraph.
      80                 :   };
      81                 : 
      82          5082360:   tok::TokenKind getKind() const { return (tok::TokenKind)Kind; }
      83          3611877:   void setKind(tok::TokenKind K) { Kind = K; }
      84                 : 
      85                 :   /// is/isNot - Predicates to check if this token is a specific kind, as in
      86                 :   /// "if (Tok.is(tok::l_brace)) {...}".
      87         38420040:   bool is(tok::TokenKind K) const { return Kind == (unsigned) K; }
      88          1608193:   bool isNot(tok::TokenKind K) const { return Kind != (unsigned) K; }
      89                 : 
      90                 :   /// isLiteral - Return true if this is a "literal", like a numeric
      91                 :   /// constant, string, etc.
      92          2947728:   bool isLiteral() const {
      93                 :     return is(tok::numeric_constant) || is(tok::char_constant) ||
      94                 :            is(tok::string_literal) || is(tok::wide_string_literal) ||
                  2525352: branch 1 taken
                   422376: branch 2 taken
                  2524358: branch 4 taken
                      994: branch 5 taken
                  2474001: branch 7 taken
                    50357: branch 8 taken
                  2473842: branch 10 taken
                      159: branch 11 taken
                     2524: branch 13 taken
                  2471318: branch 14 taken
      95          2947728:            is(tok::angle_string_literal);
      96                 :   }
      97                 : 
      98          6723198:   bool isAnnotation() const {
      99                 :     return is(tok::annot_typename) ||
     100                 :            is(tok::annot_cxxscope) ||
                  6641670: branch 1 taken
                    81528: branch 2 taken
                  6633789: branch 4 taken
                     7881: branch 5 taken
                    13027: branch 7 taken
                  6620762: branch 8 taken
     101          6723198:            is(tok::annot_template_id);
     102                 :   }
     103                 : 
     104                 :   /// getLocation - Return a source location identifier for the specified
     105                 :   /// offset in the current file.
     106          1974540:   SourceLocation getLocation() const { return Loc; }
     107          1308278:   unsigned getLength() const {
                  1308278: branch 1 taken
                        0: branch 2 not taken
     108          1308278:     assert(!isAnnotation() && "Annotation tokens have no length field");
     109          1308278:     return UintData;
     110                 :   }
     111                 : 
     112          2768727:   void setLocation(SourceLocation L) { Loc = L; }
     113          2709389:   void setLength(unsigned Len) {
                        0: branch 1 not taken
                        0: branch 2 not taken
     114          2709389:     assert(!isAnnotation() && "Annotation tokens have no length field");
     115          2709389:     UintData = Len;
     116          2709389:   }
     117                 : 
     118            16936:   SourceLocation getAnnotationEndLoc() const {
                        0: branch 2 not taken
     119            16936:     assert(isAnnotation() && "Used AnnotEndLocID on non-annotation token");
     120            16936:     return SourceLocation::getFromRawEncoding(UintData);
     121                 :   }
     122            15743:   void setAnnotationEndLoc(SourceLocation L) {
                    15743: branch 1 taken
                        0: branch 2 not taken
     123            15743:     assert(isAnnotation() && "Used AnnotEndLocID on non-annotation token");
     124            15743:     UintData = L.getRawEncoding();
     125            15743:   }
     126                 : 
     127             1707:   SourceLocation getLastLoc() const {
                      187: branch 1 taken
                     1520: branch 2 taken
     128             1707:     return isAnnotation() ? getAnnotationEndLoc() : getLocation();
     129                 :   }
     130                 : 
     131                 :   /// getAnnotationRange - SourceRange of the group of tokens that this
     132                 :   /// annotation token represents.
     133             2465:   SourceRange getAnnotationRange() const {
     134             2465:     return SourceRange(getLocation(), getAnnotationEndLoc());
     135                 :   }
     136             1504:   void setAnnotationRange(SourceRange R) {
     137             1504:     setLocation(R.getBegin());
     138             1504:     setAnnotationEndLoc(R.getEnd());
     139             1504:   }
     140                 : 
     141                 :   const char *getName() const {
     142                 :     return tok::getTokenName( (tok::TokenKind) Kind);
     143                 :   }
     144                 : 
     145                 :   /// startToken - Reset all flags to cleared.
     146                 :   ///
     147          2428696:   void startToken() {
     148          2428696:     Kind = tok::unknown;
     149          2428696:     Flags = 0;
     150          2428696:     PtrData = 0;
     151          2428696:     Loc = SourceLocation();
     152          2428696:   }
     153                 : 
     154          2535799:   IdentifierInfo *getIdentifierInfo() const {
                        0: branch 2 not taken
                        0: branch 2 not taken
     155          2535799:     assert(!isAnnotation() && "Used IdentInfo on annotation token!");
                    76391: branch 1 taken
                  2459408: branch 2 taken
     156          2612190:     if (isLiteral()) return 0;
     157          2459408:     return (IdentifierInfo*) PtrData;
     158                 :   }
     159          3768380:   void setIdentifierInfo(IdentifierInfo *II) {
     160          3768380:     PtrData = (void*) II;
     161          3768380:   }
     162                 : 
     163                 :   /// getLiteralData - For a literal token (numeric constant, string, etc), this
     164                 :   /// returns a pointer to the start of it in the text buffer if known, null
     165                 :   /// otherwise.
     166            59764:   const char *getLiteralData() const {
                        0: branch 2 not taken
     167            59764:     assert(isLiteral() && "Cannot get literal data of non-literal");
     168            59764:     return reinterpret_cast<const char*>(PtrData);
     169                 :   }
     170           294975:   void setLiteralData(const char *Ptr) {
                        0: branch 1 not taken
                        0: branch 2 not taken
     171           294975:     assert(isLiteral() && "Cannot set literal data of non-literal");
     172           294975:     PtrData = (void*)Ptr;
     173           294975:   }
     174                 : 
     175            32946:   void *getAnnotationValue() const {
                        0: branch 1 not taken
     176            32946:     assert(isAnnotation() && "Used AnnotVal on non-annotation token");
     177            32946:     return PtrData;
     178                 :   }
     179            17603:   void setAnnotationValue(void *val) {
     180            17603:     assert(isAnnotation() && "Used AnnotVal on non-annotation token");
     181            17603:     PtrData = val;
     182            17603:   }
     183                 : 
     184                 :   /// setFlag - Set the specified flag.
     185          1783710:   void setFlag(TokenFlags Flag) {
     186          1783710:     Flags |= Flag;
     187          1783710:   }
     188                 : 
     189                 :   /// clearFlag - Unset the specified flag.
     190          3429690:   void clearFlag(TokenFlags Flag) {
     191          3429690:     Flags &= ~Flag;
     192          3429690:   }
     193                 : 
     194                 :   /// getFlags - Return the internal represtation of the flags.
     195                 :   ///  Only intended for low-level operations such as writing tokens to
     196                 :   //   disk.
     197             5891:   unsigned getFlags() const {
     198             5891:     return Flags;
     199                 :   }
     200                 : 
     201                 :   /// setFlagValue - Set a flag to either true or false.
     202            32266:   void setFlagValue(TokenFlags Flag, bool Val) {
     203            32266:     if (Val)
     204            14280:       setFlag(Flag);
     205                 :     else
     206            17986:       clearFlag(Flag);
     207            32266:   }
     208                 : 
     209                 :   /// isAtStartOfLine - Return true if this token is at the start of a line.
     210                 :   ///
     211           350918:   bool isAtStartOfLine() const { return (Flags & StartOfLine) ? true : false; }
     212                 : 
     213                 :   /// hasLeadingSpace - Return true if this token has whitespace before it.
     214                 :   ///
     215           306737:   bool hasLeadingSpace() const { return (Flags & LeadingSpace) ? true : false; }
     216                 : 
     217                 :   /// isExpandDisabled - Return true if this identifier token should never
     218                 :   /// be expanded in the future, due to C99 6.10.3.4p2.
     219            11905:   bool isExpandDisabled() const {
     220            11905:     return (Flags & DisableExpand) ? true : false;
     221                 :   }
     222                 : 
     223                 :   /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
     224                 :   bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const;
     225                 : 
     226                 :   /// getObjCKeywordID - Return the ObjC keyword kind.
     227                 :   tok::ObjCKeywordKind getObjCKeywordID() const;
     228                 : 
     229                 :   /// needsCleaning - Return true if this token has trigraphs or escaped
     230                 :   /// newlines in it.
     231                 :   ///
     232           950489:   bool needsCleaning() const { return (Flags & NeedsCleaning) ? true : false; }
     233                 : };
     234                 : 
     235                 : /// PPConditionalInfo - Information about the conditional stack (#if directives)
     236                 : /// currently active.
     237            21761: struct PPConditionalInfo {
     238                 :   /// IfLoc - Location where the conditional started.
     239                 :   ///
     240                 :   SourceLocation IfLoc;
     241                 : 
     242                 :   /// WasSkipping - True if this was contained in a skipping directive, e.g.
     243                 :   /// in a "#if 0" block.
     244                 :   bool WasSkipping;
     245                 : 
     246                 :   /// FoundNonSkip - True if we have emitted tokens already, and now we're in
     247                 :   /// an #else block or something.  Only useful in Skipping blocks.
     248                 :   bool FoundNonSkip;
     249                 : 
     250                 :   /// FoundElse - True if we've seen a #else in this block.  If so,
     251                 :   /// #elif/#else directives are not allowed.
     252                 :   bool FoundElse;
     253                 : };
     254                 : 
     255                 : }  // end namespace clang
     256                 : 
     257                 : #endif

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