Home | History | Annotate | Download | only in Lex
      1 //===--- Lexer.h - C Language Family Lexer ----------------------*- 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 Lexer interface.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CLANG_LEXER_H
     15 #define LLVM_CLANG_LEXER_H
     16 
     17 #include "clang/Basic/LangOptions.h"
     18 #include "clang/Lex/PreprocessorLexer.h"
     19 #include "llvm/ADT/SmallVector.h"
     20 #include <cassert>
     21 #include <string>
     22 
     23 namespace clang {
     24 class DiagnosticsEngine;
     25 class SourceManager;
     26 class Preprocessor;
     27 class DiagnosticBuilder;
     28 
     29 /// ConflictMarkerKind - Kinds of conflict marker which the lexer might be
     30 /// recovering from.
     31 enum ConflictMarkerKind {
     32   /// Not within a conflict marker.
     33   CMK_None,
     34   /// A normal or diff3 conflict marker, initiated by at least 7 "<"s,
     35   /// separated by at least 7 "="s or "|"s, and terminated by at least 7 ">"s.
     36   CMK_Normal,
     37   /// A Perforce-style conflict marker, initiated by 4 ">"s,
     38   /// separated by 4 "="s, and terminated by 4 "<"s.
     39   CMK_Perforce
     40 };
     41 
     42 /// Lexer - This provides a simple interface that turns a text buffer into a
     43 /// stream of tokens.  This provides no support for file reading or buffering,
     44 /// or buffering/seeking of tokens, only forward lexing is supported.  It relies
     45 /// on the specified Preprocessor object to handle preprocessor directives, etc.
     46 class Lexer : public PreprocessorLexer {
     47   virtual void anchor();
     48 
     49   //===--------------------------------------------------------------------===//
     50   // Constant configuration values for this lexer.
     51   const char *BufferStart;       // Start of the buffer.
     52   const char *BufferEnd;         // End of the buffer.
     53   SourceLocation FileLoc;        // Location for start of file.
     54   LangOptions LangOpts;          // LangOpts enabled by this language (cache).
     55   bool Is_PragmaLexer;           // True if lexer for _Pragma handling.
     56 
     57   //===--------------------------------------------------------------------===//
     58   // Context-specific lexing flags set by the preprocessor.
     59   //
     60 
     61   /// ExtendedTokenMode - The lexer can optionally keep comments and whitespace
     62   /// and return them as tokens.  This is used for -C and -CC modes, and
     63   /// whitespace preservation can be useful for some clients that want to lex
     64   /// the file in raw mode and get every character from the file.
     65   ///
     66   /// When this is set to 2 it returns comments and whitespace.  When set to 1
     67   /// it returns comments, when it is set to 0 it returns normal tokens only.
     68   unsigned char ExtendedTokenMode;
     69 
     70   //===--------------------------------------------------------------------===//
     71   // Context that changes as the file is lexed.
     72   // NOTE: any state that mutates when in raw mode must have save/restore code
     73   // in Lexer::isNextPPTokenLParen.
     74 
     75   // BufferPtr - Current pointer into the buffer.  This is the next character
     76   // to be lexed.
     77   const char *BufferPtr;
     78 
     79   // IsAtStartOfLine - True if the next lexed token should get the "start of
     80   // line" flag set on it.
     81   bool IsAtStartOfLine;
     82 
     83   // CurrentConflictMarkerState - The kind of conflict marker we are handling.
     84   ConflictMarkerKind CurrentConflictMarkerState;
     85 
     86   Lexer(const Lexer &) LLVM_DELETED_FUNCTION;
     87   void operator=(const Lexer &) LLVM_DELETED_FUNCTION;
     88   friend class Preprocessor;
     89 
     90   void InitLexer(const char *BufStart, const char *BufPtr, const char *BufEnd);
     91 public:
     92 
     93   /// Lexer constructor - Create a new lexer object for the specified buffer
     94   /// with the specified preprocessor managing the lexing process.  This lexer
     95   /// assumes that the associated file buffer and Preprocessor objects will
     96   /// outlive it, so it doesn't take ownership of either of them.
     97   Lexer(FileID FID, const llvm::MemoryBuffer *InputBuffer, Preprocessor &PP);
     98 
     99   /// Lexer constructor - Create a new raw lexer object.  This object is only
    100   /// suitable for calls to 'LexFromRawLexer'.  This lexer assumes that the
    101   /// text range will outlive it, so it doesn't take ownership of it.
    102   Lexer(SourceLocation FileLoc, const LangOptions &LangOpts,
    103         const char *BufStart, const char *BufPtr, const char *BufEnd);
    104 
    105   /// Lexer constructor - Create a new raw lexer object.  This object is only
    106   /// suitable for calls to 'LexFromRawLexer'.  This lexer assumes that the
    107   /// text range will outlive it, so it doesn't take ownership of it.
    108   Lexer(FileID FID, const llvm::MemoryBuffer *InputBuffer,
    109         const SourceManager &SM, const LangOptions &LangOpts);
    110 
    111   /// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
    112   /// _Pragma expansion.  This has a variety of magic semantics that this method
    113   /// sets up.  It returns a new'd Lexer that must be delete'd when done.
    114   static Lexer *Create_PragmaLexer(SourceLocation SpellingLoc,
    115                                    SourceLocation ExpansionLocStart,
    116                                    SourceLocation ExpansionLocEnd,
    117                                    unsigned TokLen, Preprocessor &PP);
    118 
    119 
    120   /// getLangOpts - Return the language features currently enabled.
    121   /// NOTE: this lexer modifies features as a file is parsed!
    122   const LangOptions &getLangOpts() const { return LangOpts; }
    123 
    124   /// getFileLoc - Return the File Location for the file we are lexing out of.
    125   /// The physical location encodes the location where the characters come from,
    126   /// the virtual location encodes where we should *claim* the characters came
    127   /// from.  Currently this is only used by _Pragma handling.
    128   SourceLocation getFileLoc() const { return FileLoc; }
    129 
    130   /// Lex - Return the next token in the file.  If this is the end of file, it
    131   /// return the tok::eof token.  This implicitly involves the preprocessor.
    132   void Lex(Token &Result) {
    133     // Start a new token.
    134     Result.startToken();
    135 
    136     // NOTE, any changes here should also change code after calls to
    137     // Preprocessor::HandleDirective
    138     if (IsAtStartOfLine) {
    139       Result.setFlag(Token::StartOfLine);
    140       IsAtStartOfLine = false;
    141     }
    142 
    143     // Get a token.  Note that this may delete the current lexer if the end of
    144     // file is reached.
    145     LexTokenInternal(Result);
    146   }
    147 
    148   /// isPragmaLexer - Returns true if this Lexer is being used to lex a pragma.
    149   bool isPragmaLexer() const { return Is_PragmaLexer; }
    150 
    151   /// IndirectLex - An indirect call to 'Lex' that can be invoked via
    152   ///  the PreprocessorLexer interface.
    153   void IndirectLex(Token &Result) { Lex(Result); }
    154 
    155   /// LexFromRawLexer - Lex a token from a designated raw lexer (one with no
    156   /// associated preprocessor object.  Return true if the 'next character to
    157   /// read' pointer points at the end of the lexer buffer, false otherwise.
    158   bool LexFromRawLexer(Token &Result) {
    159     assert(LexingRawMode && "Not already in raw mode!");
    160     Lex(Result);
    161     // Note that lexing to the end of the buffer doesn't implicitly delete the
    162     // lexer when in raw mode.
    163     return BufferPtr == BufferEnd;
    164   }
    165 
    166   /// isKeepWhitespaceMode - Return true if the lexer should return tokens for
    167   /// every character in the file, including whitespace and comments.  This
    168   /// should only be used in raw mode, as the preprocessor is not prepared to
    169   /// deal with the excess tokens.
    170   bool isKeepWhitespaceMode() const {
    171     return ExtendedTokenMode > 1;
    172   }
    173 
    174   /// SetKeepWhitespaceMode - This method lets clients enable or disable
    175   /// whitespace retention mode.
    176   void SetKeepWhitespaceMode(bool Val) {
    177     assert((!Val || LexingRawMode || LangOpts.TraditionalCPP) &&
    178            "Can only retain whitespace in raw mode or -traditional-cpp");
    179     ExtendedTokenMode = Val ? 2 : 0;
    180   }
    181 
    182   /// inKeepCommentMode - Return true if the lexer should return comments as
    183   /// tokens.
    184   bool inKeepCommentMode() const {
    185     return ExtendedTokenMode > 0;
    186   }
    187 
    188   /// SetCommentRetentionMode - Change the comment retention mode of the lexer
    189   /// to the specified mode.  This is really only useful when lexing in raw
    190   /// mode, because otherwise the lexer needs to manage this.
    191   void SetCommentRetentionState(bool Mode) {
    192     assert(!isKeepWhitespaceMode() &&
    193            "Can't play with comment retention state when retaining whitespace");
    194     ExtendedTokenMode = Mode ? 1 : 0;
    195   }
    196 
    197   /// Sets the extended token mode back to its initial value, according to the
    198   /// language options and preprocessor. This controls whether the lexer
    199   /// produces comment and whitespace tokens.
    200   ///
    201   /// This requires the lexer to have an associated preprocessor. A standalone
    202   /// lexer has nothing to reset to.
    203   void resetExtendedTokenMode();
    204 
    205   /// Gets source code buffer.
    206   StringRef getBuffer() const {
    207     return StringRef(BufferStart, BufferEnd - BufferStart);
    208   }
    209 
    210   /// ReadToEndOfLine - Read the rest of the current preprocessor line as an
    211   /// uninterpreted string.  This switches the lexer out of directive mode.
    212   void ReadToEndOfLine(SmallVectorImpl<char> *Result = 0);
    213 
    214 
    215   /// Diag - Forwarding function for diagnostics.  This translate a source
    216   /// position in the current buffer into a SourceLocation object for rendering.
    217   DiagnosticBuilder Diag(const char *Loc, unsigned DiagID) const;
    218 
    219   /// getSourceLocation - Return a source location identifier for the specified
    220   /// offset in the current file.
    221   SourceLocation getSourceLocation(const char *Loc, unsigned TokLen = 1) const;
    222 
    223   /// getSourceLocation - Return a source location for the next character in
    224   /// the current file.
    225   SourceLocation getSourceLocation() { return getSourceLocation(BufferPtr); }
    226 
    227   /// \brief Return the current location in the buffer.
    228   const char *getBufferLocation() const { return BufferPtr; }
    229 
    230   /// Stringify - Convert the specified string into a C string by escaping '\'
    231   /// and " characters.  This does not add surrounding ""'s to the string.
    232   /// If Charify is true, this escapes the ' character instead of ".
    233   static std::string Stringify(const std::string &Str, bool Charify = false);
    234 
    235   /// Stringify - Convert the specified string into a C string by escaping '\'
    236   /// and " characters.  This does not add surrounding ""'s to the string.
    237   static void Stringify(SmallVectorImpl<char> &Str);
    238 
    239 
    240   /// getSpelling - This method is used to get the spelling of a token into a
    241   /// preallocated buffer, instead of as an std::string.  The caller is required
    242   /// to allocate enough space for the token, which is guaranteed to be at least
    243   /// Tok.getLength() bytes long.  The length of the actual result is returned.
    244   ///
    245   /// Note that this method may do two possible things: it may either fill in
    246   /// the buffer specified with characters, or it may *change the input pointer*
    247   /// to point to a constant buffer with the data already in it (avoiding a
    248   /// copy).  The caller is not allowed to modify the returned buffer pointer
    249   /// if an internal buffer is returned.
    250   static unsigned getSpelling(const Token &Tok, const char *&Buffer,
    251                               const SourceManager &SourceMgr,
    252                               const LangOptions &LangOpts,
    253                               bool *Invalid = 0);
    254 
    255   /// getSpelling() - Return the 'spelling' of the Tok token.  The spelling of a
    256   /// token is the characters used to represent the token in the source file
    257   /// after trigraph expansion and escaped-newline folding.  In particular, this
    258   /// wants to get the true, uncanonicalized, spelling of things like digraphs
    259   /// UCNs, etc.
    260   static std::string getSpelling(const Token &Tok,
    261                                  const SourceManager &SourceMgr,
    262                                  const LangOptions &LangOpts,
    263                                  bool *Invalid = 0);
    264 
    265   /// getSpelling - This method is used to get the spelling of the
    266   /// token at the given source location.  If, as is usually true, it
    267   /// is not necessary to copy any data, then the returned string may
    268   /// not point into the provided buffer.
    269   ///
    270   /// This method lexes at the expansion depth of the given
    271   /// location and does not jump to the expansion or spelling
    272   /// location.
    273   static StringRef getSpelling(SourceLocation loc,
    274                                SmallVectorImpl<char> &buffer,
    275                                const SourceManager &SourceMgr,
    276                                const LangOptions &LangOpts,
    277                                bool *invalid = 0);
    278 
    279   /// MeasureTokenLength - Relex the token at the specified location and return
    280   /// its length in bytes in the input file.  If the token needs cleaning (e.g.
    281   /// includes a trigraph or an escaped newline) then this count includes bytes
    282   /// that are part of that.
    283   static unsigned MeasureTokenLength(SourceLocation Loc,
    284                                      const SourceManager &SM,
    285                                      const LangOptions &LangOpts);
    286 
    287   /// \brief Relex the token at the specified location.
    288   /// \returns true if there was a failure, false on success.
    289   static bool getRawToken(SourceLocation Loc, Token &Result,
    290                           const SourceManager &SM,
    291                           const LangOptions &LangOpts);
    292 
    293   /// \brief Given a location any where in a source buffer, find the location
    294   /// that corresponds to the beginning of the token in which the original
    295   /// source location lands.
    296   static SourceLocation GetBeginningOfToken(SourceLocation Loc,
    297                                             const SourceManager &SM,
    298                                             const LangOptions &LangOpts);
    299 
    300   /// AdvanceToTokenCharacter - If the current SourceLocation specifies a
    301   /// location at the start of a token, return a new location that specifies a
    302   /// character within the token.  This handles trigraphs and escaped newlines.
    303   static SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,
    304                                                 unsigned Character,
    305                                                 const SourceManager &SM,
    306                                                 const LangOptions &LangOpts);
    307 
    308   /// \brief Computes the source location just past the end of the
    309   /// token at this source location.
    310   ///
    311   /// This routine can be used to produce a source location that
    312   /// points just past the end of the token referenced by \p Loc, and
    313   /// is generally used when a diagnostic needs to point just after a
    314   /// token where it expected something different that it received. If
    315   /// the returned source location would not be meaningful (e.g., if
    316   /// it points into a macro), this routine returns an invalid
    317   /// source location.
    318   ///
    319   /// \param Offset an offset from the end of the token, where the source
    320   /// location should refer to. The default offset (0) produces a source
    321   /// location pointing just past the end of the token; an offset of 1 produces
    322   /// a source location pointing to the last character in the token, etc.
    323   static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset,
    324                                             const SourceManager &SM,
    325                                             const LangOptions &LangOpts);
    326 
    327   /// \brief Returns true if the given MacroID location points at the first
    328   /// token of the macro expansion.
    329   ///
    330   /// \param MacroBegin If non-null and function returns true, it is set to
    331   /// begin location of the macro.
    332   static bool isAtStartOfMacroExpansion(SourceLocation loc,
    333                                         const SourceManager &SM,
    334                                         const LangOptions &LangOpts,
    335                                         SourceLocation *MacroBegin = 0);
    336 
    337   /// \brief Returns true if the given MacroID location points at the last
    338   /// token of the macro expansion.
    339   ///
    340   /// \param MacroEnd If non-null and function returns true, it is set to
    341   /// end location of the macro.
    342   static bool isAtEndOfMacroExpansion(SourceLocation loc,
    343                                       const SourceManager &SM,
    344                                       const LangOptions &LangOpts,
    345                                       SourceLocation *MacroEnd = 0);
    346 
    347   /// \brief Accepts a range and returns a character range with file locations.
    348   ///
    349   /// Returns a null range if a part of the range resides inside a macro
    350   /// expansion or the range does not reside on the same FileID.
    351   ///
    352   /// This function is trying to deal with macros and return a range based on
    353   /// file locations. The cases where it can successfully handle macros are:
    354   ///
    355   /// -begin or end range lies at the start or end of a macro expansion, in
    356   ///  which case the location will be set to the expansion point, e.g:
    357   ///    \#define M 1 2
    358   ///    a M
    359   /// If you have a range [a, 2] (where 2 came from the macro), the function
    360   /// will return a range for "a M"
    361   /// if you have range [a, 1], the function will fail because the range
    362   /// overlaps with only a part of the macro
    363   ///
    364   /// -The macro is a function macro and the range can be mapped to the macro
    365   ///  arguments, e.g:
    366   ///    \#define M 1 2
    367   ///    \#define FM(x) x
    368   ///    FM(a b M)
    369   /// if you have range [b, 2], the function will return the file range "b M"
    370   /// inside the macro arguments.
    371   /// if you have range [a, 2], the function will return the file range
    372   /// "FM(a b M)" since the range includes all of the macro expansion.
    373   static CharSourceRange makeFileCharRange(CharSourceRange Range,
    374                                            const SourceManager &SM,
    375                                            const LangOptions &LangOpts);
    376 
    377   /// \brief Returns a string for the source that the range encompasses.
    378   static StringRef getSourceText(CharSourceRange Range,
    379                                  const SourceManager &SM,
    380                                  const LangOptions &LangOpts,
    381                                  bool *Invalid = 0);
    382 
    383   /// \brief Retrieve the name of the immediate macro expansion.
    384   ///
    385   /// This routine starts from a source location, and finds the name of the macro
    386   /// responsible for its immediate expansion. It looks through any intervening
    387   /// macro argument expansions to compute this. It returns a StringRef which
    388   /// refers to the SourceManager-owned buffer of the source where that macro
    389   /// name is spelled. Thus, the result shouldn't out-live that SourceManager.
    390   static StringRef getImmediateMacroName(SourceLocation Loc,
    391                                          const SourceManager &SM,
    392                                          const LangOptions &LangOpts);
    393 
    394   /// \brief Compute the preamble of the given file.
    395   ///
    396   /// The preamble of a file contains the initial comments, include directives,
    397   /// and other preprocessor directives that occur before the code in this
    398   /// particular file actually begins. The preamble of the main source file is
    399   /// a potential prefix header.
    400   ///
    401   /// \param Buffer The memory buffer containing the file's contents.
    402   ///
    403   /// \param MaxLines If non-zero, restrict the length of the preamble
    404   /// to fewer than this number of lines.
    405   ///
    406   /// \returns The offset into the file where the preamble ends and the rest
    407   /// of the file begins along with a boolean value indicating whether
    408   /// the preamble ends at the beginning of a new line.
    409   static std::pair<unsigned, bool>
    410   ComputePreamble(const llvm::MemoryBuffer *Buffer, const LangOptions &LangOpts,
    411                   unsigned MaxLines = 0);
    412 
    413   /// \brief Checks that the given token is the first token that occurs after
    414   /// the given location (this excludes comments and whitespace). Returns the
    415   /// location immediately after the specified token. If the token is not found
    416   /// or the location is inside a macro, the returned source location will be
    417   /// invalid.
    418   static SourceLocation findLocationAfterToken(SourceLocation loc,
    419                                          tok::TokenKind TKind,
    420                                          const SourceManager &SM,
    421                                          const LangOptions &LangOpts,
    422                                          bool SkipTrailingWhitespaceAndNewLine);
    423 
    424   /// \brief Returns true if the given character could appear in an identifier.
    425   static bool isIdentifierBodyChar(char c, const LangOptions &LangOpts);
    426 
    427   /// getCharAndSizeNoWarn - Like the getCharAndSize method, but does not ever
    428   /// emit a warning.
    429   static inline char getCharAndSizeNoWarn(const char *Ptr, unsigned &Size,
    430                                           const LangOptions &LangOpts) {
    431     // If this is not a trigraph and not a UCN or escaped newline, return
    432     // quickly.
    433     if (isObviouslySimpleCharacter(Ptr[0])) {
    434       Size = 1;
    435       return *Ptr;
    436     }
    437 
    438     Size = 0;
    439     return getCharAndSizeSlowNoWarn(Ptr, Size, LangOpts);
    440   }
    441 
    442   //===--------------------------------------------------------------------===//
    443   // Internal implementation interfaces.
    444 private:
    445 
    446   /// LexTokenInternal - Internal interface to lex a preprocessing token. Called
    447   /// by Lex.
    448   ///
    449   void LexTokenInternal(Token &Result);
    450 
    451   /// Given that a token begins with the Unicode character \p C, figure out
    452   /// what kind of token it is and dispatch to the appropriate lexing helper
    453   /// function.
    454   void LexUnicode(Token &Result, uint32_t C, const char *CurPtr);
    455 
    456   /// FormTokenWithChars - When we lex a token, we have identified a span
    457   /// starting at BufferPtr, going to TokEnd that forms the token.  This method
    458   /// takes that range and assigns it to the token as its location and size.  In
    459   /// addition, since tokens cannot overlap, this also updates BufferPtr to be
    460   /// TokEnd.
    461   void FormTokenWithChars(Token &Result, const char *TokEnd,
    462                           tok::TokenKind Kind) {
    463     unsigned TokLen = TokEnd-BufferPtr;
    464     Result.setLength(TokLen);
    465     Result.setLocation(getSourceLocation(BufferPtr, TokLen));
    466     Result.setKind(Kind);
    467     BufferPtr = TokEnd;
    468   }
    469 
    470   /// isNextPPTokenLParen - Return 1 if the next unexpanded token will return a
    471   /// tok::l_paren token, 0 if it is something else and 2 if there are no more
    472   /// tokens in the buffer controlled by this lexer.
    473   unsigned isNextPPTokenLParen();
    474 
    475   //===--------------------------------------------------------------------===//
    476   // Lexer character reading interfaces.
    477 
    478   // This lexer is built on two interfaces for reading characters, both of which
    479   // automatically provide phase 1/2 translation.  getAndAdvanceChar is used
    480   // when we know that we will be reading a character from the input buffer and
    481   // that this character will be part of the result token. This occurs in (f.e.)
    482   // string processing, because we know we need to read until we find the
    483   // closing '"' character.
    484   //
    485   // The second interface is the combination of getCharAndSize with
    486   // ConsumeChar.  getCharAndSize reads a phase 1/2 translated character,
    487   // returning it and its size.  If the lexer decides that this character is
    488   // part of the current token, it calls ConsumeChar on it.  This two stage
    489   // approach allows us to emit diagnostics for characters (e.g. warnings about
    490   // trigraphs), knowing that they only are emitted if the character is
    491   // consumed.
    492 
    493   /// isObviouslySimpleCharacter - Return true if the specified character is
    494   /// obviously the same in translation phase 1 and translation phase 3.  This
    495   /// can return false for characters that end up being the same, but it will
    496   /// never return true for something that needs to be mapped.
    497   static bool isObviouslySimpleCharacter(char C) {
    498     return C != '?' && C != '\\';
    499   }
    500 
    501   /// getAndAdvanceChar - Read a single 'character' from the specified buffer,
    502   /// advance over it, and return it.  This is tricky in several cases.  Here we
    503   /// just handle the trivial case and fall-back to the non-inlined
    504   /// getCharAndSizeSlow method to handle the hard case.
    505   inline char getAndAdvanceChar(const char *&Ptr, Token &Tok) {
    506     // If this is not a trigraph and not a UCN or escaped newline, return
    507     // quickly.
    508     if (isObviouslySimpleCharacter(Ptr[0])) return *Ptr++;
    509 
    510     unsigned Size = 0;
    511     char C = getCharAndSizeSlow(Ptr, Size, &Tok);
    512     Ptr += Size;
    513     return C;
    514   }
    515 
    516   /// ConsumeChar - When a character (identified by getCharAndSize) is consumed
    517   /// and added to a given token, check to see if there are diagnostics that
    518   /// need to be emitted or flags that need to be set on the token.  If so, do
    519   /// it.
    520   const char *ConsumeChar(const char *Ptr, unsigned Size, Token &Tok) {
    521     // Normal case, we consumed exactly one token.  Just return it.
    522     if (Size == 1)
    523       return Ptr+Size;
    524 
    525     // Otherwise, re-lex the character with a current token, allowing
    526     // diagnostics to be emitted and flags to be set.
    527     Size = 0;
    528     getCharAndSizeSlow(Ptr, Size, &Tok);
    529     return Ptr+Size;
    530   }
    531 
    532   /// getCharAndSize - Peek a single 'character' from the specified buffer,
    533   /// get its size, and return it.  This is tricky in several cases.  Here we
    534   /// just handle the trivial case and fall-back to the non-inlined
    535   /// getCharAndSizeSlow method to handle the hard case.
    536   inline char getCharAndSize(const char *Ptr, unsigned &Size) {
    537     // If this is not a trigraph and not a UCN or escaped newline, return
    538     // quickly.
    539     if (isObviouslySimpleCharacter(Ptr[0])) {
    540       Size = 1;
    541       return *Ptr;
    542     }
    543 
    544     Size = 0;
    545     return getCharAndSizeSlow(Ptr, Size);
    546   }
    547 
    548   /// getCharAndSizeSlow - Handle the slow/uncommon case of the getCharAndSize
    549   /// method.
    550   char getCharAndSizeSlow(const char *Ptr, unsigned &Size, Token *Tok = 0);
    551 
    552   /// getEscapedNewLineSize - Return the size of the specified escaped newline,
    553   /// or 0 if it is not an escaped newline. P[-1] is known to be a "\" on entry
    554   /// to this function.
    555   static unsigned getEscapedNewLineSize(const char *P);
    556 
    557   /// SkipEscapedNewLines - If P points to an escaped newline (or a series of
    558   /// them), skip over them and return the first non-escaped-newline found,
    559   /// otherwise return P.
    560   static const char *SkipEscapedNewLines(const char *P);
    561 
    562   /// getCharAndSizeSlowNoWarn - Same as getCharAndSizeSlow, but never emits a
    563   /// diagnostic.
    564   static char getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
    565                                        const LangOptions &LangOpts);
    566 
    567   //===--------------------------------------------------------------------===//
    568   // Other lexer functions.
    569 
    570   void SkipBytes(unsigned Bytes, bool StartOfLine);
    571 
    572   const char *LexUDSuffix(Token &Result, const char *CurPtr,
    573                           bool IsStringLiteral);
    574 
    575   // Helper functions to lex the remainder of a token of the specific type.
    576   void LexIdentifier         (Token &Result, const char *CurPtr);
    577   void LexNumericConstant    (Token &Result, const char *CurPtr);
    578   void LexStringLiteral      (Token &Result, const char *CurPtr,
    579                               tok::TokenKind Kind);
    580   void LexRawStringLiteral   (Token &Result, const char *CurPtr,
    581                               tok::TokenKind Kind);
    582   void LexAngledStringLiteral(Token &Result, const char *CurPtr);
    583   void LexCharConstant       (Token &Result, const char *CurPtr,
    584                               tok::TokenKind Kind);
    585   bool LexEndOfFile          (Token &Result, const char *CurPtr);
    586 
    587   bool SkipWhitespace        (Token &Result, const char *CurPtr);
    588   bool SkipLineComment       (Token &Result, const char *CurPtr);
    589   bool SkipBlockComment      (Token &Result, const char *CurPtr);
    590   bool SaveLineComment       (Token &Result, const char *CurPtr);
    591 
    592   bool IsStartOfConflictMarker(const char *CurPtr);
    593   bool HandleEndOfConflictMarker(const char *CurPtr);
    594 
    595   bool isCodeCompletionPoint(const char *CurPtr) const;
    596   void cutOffLexing() { BufferPtr = BufferEnd; }
    597 
    598   bool isHexaLiteral(const char *Start, const LangOptions &LangOpts);
    599 
    600 
    601   /// Read a universal character name.
    602   ///
    603   /// \param CurPtr The position in the source buffer after the initial '\'.
    604   ///               If the UCN is syntactically well-formed (but not necessarily
    605   ///               valid), this parameter will be updated to point to the
    606   ///               character after the UCN.
    607   /// \param SlashLoc The position in the source buffer of the '\'.
    608   /// \param Tok The token being formed. Pass \c NULL to suppress diagnostics
    609   ///            and handle token formation in the caller.
    610   ///
    611   /// \return The Unicode codepoint specified by the UCN, or 0 if the UCN is
    612   ///         invalid.
    613   uint32_t tryReadUCN(const char *&CurPtr, const char *SlashLoc, Token *Tok);
    614 };
    615 
    616 
    617 }  // end namespace clang
    618 
    619 #endif
    620