Lexer.h revision 1fa495304c81e03f07f278a47b5efe9317104aab
1ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//===--- Lexer.h - C Language Family Lexer ----------------------*- C++ -*-===//
2ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//
3ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//                     The LLVM Compiler Infrastructure
4ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//
5ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// This file is distributed under the University of Illinois Open Source
6ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// License. See LICENSE.TXT for details.
7ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//
8ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//===----------------------------------------------------------------------===//
9ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//
10ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//  This file defines the Lexer interface.
11ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//
12ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//===----------------------------------------------------------------------===//
13ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
14ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#ifndef LLVM_CLANG_LEXER_H
15ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#define LLVM_CLANG_LEXER_H
16ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
17ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "clang/Lex/PreprocessorLexer.h"
18ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "clang/Basic/LangOptions.h"
19b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian#include "llvm/ADT/SmallVector.h"
20b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian#include <string>
21b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian#include <vector>
22b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian#include <cassert>
23ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
24ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangnamespace clang {
25ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangclass Diagnostic;
26ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangclass SourceManager;
27ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangclass Preprocessor;
28ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangclass DiagnosticBuilder;
29ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
30ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang/// Lexer - This provides a simple interface that turns a text buffer into a
31ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang/// stream of tokens.  This provides no support for file reading or buffering,
325ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang/// or buffering/seeking of tokens, only forward lexing is supported.  It relies
339b35249446b07f40ac5fcc3205f2c048616efacchkuang/// on the specified Preprocessor object to handle preprocessor directives, etc.
349b35249446b07f40ac5fcc3205f2c048616efacchkuangclass Lexer : public PreprocessorLexer {
359b35249446b07f40ac5fcc3205f2c048616efacchkuang  //===--------------------------------------------------------------------===//
369b35249446b07f40ac5fcc3205f2c048616efacchkuang  // Constant configuration values for this lexer.
379b35249446b07f40ac5fcc3205f2c048616efacchkuang  const char *BufferStart;       // Start of the buffer.
389b35249446b07f40ac5fcc3205f2c048616efacchkuang  const char *BufferEnd;         // End of the buffer.
399b35249446b07f40ac5fcc3205f2c048616efacchkuang  SourceLocation FileLoc;        // Location for start of file.
409b35249446b07f40ac5fcc3205f2c048616efacchkuang  LangOptions Features;          // Features enabled by this language (cache).
419b35249446b07f40ac5fcc3205f2c048616efacchkuang  bool Is_PragmaLexer;           // True if lexer for _Pragma handling.
429b35249446b07f40ac5fcc3205f2c048616efacchkuang
439b35249446b07f40ac5fcc3205f2c048616efacchkuang  //===--------------------------------------------------------------------===//
449b35249446b07f40ac5fcc3205f2c048616efacchkuang  // Context-specific lexing flags set by the preprocessor.
455ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  //
46ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
47b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  /// ExtendedTokenMode - The lexer can optionally keep comments and whitespace
48ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// and return them as tokens.  This is used for -C and -CC modes, and
49ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// whitespace preservation can be useful for some clients that want to lex
50ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// the file in raw mode and get every character from the file.
51ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  ///
52ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// When this is set to 2 it returns comments and whitespace.  When set to 1
53ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// it returns comments, when it is set to 0 it returns normal tokens only.
54ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  unsigned char ExtendedTokenMode;
55ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
56ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  //===--------------------------------------------------------------------===//
57f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang  // Context that changes as the file is lexed.
585ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  // NOTE: any state that mutates when in raw mode must have save/restore code
591184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  // in Lexer::isNextPPTokenLParen.
601184aebb761cbeac9124c37189a80a1a58f04b6bhkuang
611184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  // BufferPtr - Current pointer into the buffer.  This is the next character
621184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  // to be lexed.
63b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  const char *BufferPtr;
64ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
65ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  // IsAtStartOfLine - True if the next lexed token should get the "start of
66ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  // line" flag set on it.
675ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  bool IsAtStartOfLine;
68b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian
699b35249446b07f40ac5fcc3205f2c048616efacchkuang  Lexer(const Lexer&);          // DO NOT IMPLEMENT
70b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  void operator=(const Lexer&); // DO NOT IMPLEMENT
71ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  friend class Preprocessor;
72ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
73ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  void InitLexer(const char *BufStart, const char *BufPtr, const char *BufEnd);
74ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangpublic:
7591037db265ecdd914a26e056cf69207b4f50924ehkuang
76ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// Lexer constructor - Create a new lexer object for the specified buffer
77ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// with the specified preprocessor managing the lexing process.  This lexer
78ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// assumes that the associated file buffer and Preprocessor objects will
79ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// outlive it, so it doesn't take ownership of either of them.
80ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  Lexer(FileID FID, Preprocessor &PP);
81ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
82ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// Lexer constructor - Create a new raw lexer object.  This object is only
83f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang  /// suitable for calls to 'LexRawToken'.  This lexer assumes that the text
84f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang  /// range will outlive it, so it doesn't take ownership of it.
85b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  Lexer(SourceLocation FileLoc, const LangOptions &Features,
86b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian        const char *BufStart, const char *BufPtr, const char *BufEnd);
87f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang
88ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// Lexer constructor - Create a new raw lexer object.  This object is only
89ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// suitable for calls to 'LexRawToken'.  This lexer assumes that the text
90ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// range will outlive it, so it doesn't take ownership of it.
91ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  Lexer(FileID FID, const SourceManager &SM, const LangOptions &Features);
92ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
93ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
949b35249446b07f40ac5fcc3205f2c048616efacchkuang  /// _Pragma expansion.  This has a variety of magic semantics that this method
959b35249446b07f40ac5fcc3205f2c048616efacchkuang  /// sets up.  It returns a new'd Lexer that must be delete'd when done.
969b35249446b07f40ac5fcc3205f2c048616efacchkuang  static Lexer *Create_PragmaLexer(SourceLocation SpellingLoc,
979b35249446b07f40ac5fcc3205f2c048616efacchkuang                                   SourceLocation InstantiationLocStart,
98ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang                                   SourceLocation InstantiationLocEnd,
99ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang                                   unsigned TokLen, Preprocessor &PP);
100ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
101ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
102ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// getFeatures - Return the language features currently enabled.  NOTE: this
103ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// lexer modifies features as a file is parsed!
104ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  const LangOptions &getFeatures() const { return Features; }
105ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
106ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// getFileLoc - Return the File Location for the file we are lexing out of.
107ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// The physical location encodes the location where the characters come from,
1085ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// the virtual location encodes where we should *claim* the characters came
109ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// from.  Currently this is only used by _Pragma handling.
110ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  SourceLocation getFileLoc() const { return FileLoc; }
111ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
112ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// Lex - Return the next token in the file.  If this is the end of file, it
113ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// return the tok::eof token.  Return true if an error occurred and
11491037db265ecdd914a26e056cf69207b4f50924ehkuang  /// compilation should terminate, false if normal.  This implicitly involves
115f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang  /// the preprocessor.
116b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  void Lex(Token &Result) {
117b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian    // Start a new token.
118b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian    Result.startToken();
119ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
120ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    // NOTE, any changes here should also change code after calls to
121ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    // Preprocessor::HandleDirective
122ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    if (IsAtStartOfLine) {
123ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang      Result.setFlag(Token::StartOfLine);
124ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang      IsAtStartOfLine = false;
125ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    }
126ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
127ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    // Get a token.  Note that this may delete the current lexer if the end of
128ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    // file is reached.
129ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    LexTokenInternal(Result);
130ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
131ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
132ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// isPragmaLexer - Returns true if this Lexer is being used to lex a pragma.
133ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  bool isPragmaLexer() const { return Is_PragmaLexer; }
134b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian
1355ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// IndirectLex - An indirect call to 'Lex' that can be invoked via
136b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  ///  the PreprocessorLexer interface.
1371184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  void IndirectLex(Token &Result) { Lex(Result); }
1385ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang
139ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// LexFromRawLexer - Lex a token from a designated raw lexer (one with no
1409b35249446b07f40ac5fcc3205f2c048616efacchkuang  /// associated preprocessor object.  Return true if the 'next character to
1419b35249446b07f40ac5fcc3205f2c048616efacchkuang  /// read' pointer points at the end of the lexer buffer, false otherwise.
1429b35249446b07f40ac5fcc3205f2c048616efacchkuang  bool LexFromRawLexer(Token &Result) {
1439b35249446b07f40ac5fcc3205f2c048616efacchkuang    assert(LexingRawMode && "Not already in raw mode!");
1449b35249446b07f40ac5fcc3205f2c048616efacchkuang    Lex(Result);
145ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    // Note that lexing to the end of the buffer doesn't implicitly delete the
146ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    // lexer when in raw mode.
147ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    return BufferPtr == BufferEnd;
148ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
149ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
150ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// isKeepWhitespaceMode - Return true if the lexer should return tokens for
151ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// every character in the file, including whitespace and comments.  This
1525ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// should only be used in raw mode, as the preprocessor is not prepared to
153ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// deal with the excess tokens.
154ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  bool isKeepWhitespaceMode() const {
155ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    return ExtendedTokenMode > 1;
156ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
157ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
158ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// SetKeepWhitespaceMode - This method lets clients enable or disable
159ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// whitespace retention mode.
160f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang  void SetKeepWhitespaceMode(bool Val) {
1615ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang    assert((!Val || LexingRawMode) &&
162ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang           "Can only enable whitespace retention in raw mode");
163ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    ExtendedTokenMode = Val ? 2 : 0;
164ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
165ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
1661184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  /// inKeepCommentMode - Return true if the lexer should return comments as
16791037db265ecdd914a26e056cf69207b4f50924ehkuang  /// tokens.
16891037db265ecdd914a26e056cf69207b4f50924ehkuang  bool inKeepCommentMode() const {
16991037db265ecdd914a26e056cf69207b4f50924ehkuang    return ExtendedTokenMode > 0;
1701184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  }
171ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
172ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// SetCommentRetentionMode - Change the comment retention mode of the lexer
173ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// to the specified mode.  This is really only useful when lexing in raw
174ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// mode, because otherwise the lexer needs to manage this.
175ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  void SetCommentRetentionState(bool Mode) {
176ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    assert(!isKeepWhitespaceMode() &&
177ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang           "Can't play with comment retention state when retaining whitespace");
178ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    ExtendedTokenMode = Mode ? 1 : 0;
179ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
180ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
181ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  const char *getBufferStart() const { return BufferStart; }
182ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
183ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// ReadToEndOfLine - Read the rest of the current preprocessor line as an
184ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// uninterpreted string.  This switches the lexer out of directive mode.
185ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  std::string ReadToEndOfLine();
186ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
187ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
1885ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// Diag - Forwarding function for diagnostics.  This translate a source
189ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// position in the current buffer into a SourceLocation object for rendering.
1901184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  DiagnosticBuilder Diag(const char *Loc, unsigned DiagID) const;
1911184aebb761cbeac9124c37189a80a1a58f04b6bhkuang
1921184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  /// getSourceLocation - Return a source location identifier for the specified
1931184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  /// offset in the current file.
194ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  SourceLocation getSourceLocation(const char *Loc, unsigned TokLen = 1) const;
1955ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang
1965ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// getSourceLocation - Return a source location for the next character in
1975ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// the current file.
1985ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  SourceLocation getSourceLocation() { return getSourceLocation(BufferPtr); }
1995ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang
2005ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// Stringify - Convert the specified string into a C string by escaping '\'
2015ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// and " characters.  This does not add surrounding ""'s to the string.
2025ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// If Charify is true, this escapes the ' character instead of ".
2035ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  static std::string Stringify(const std::string &Str, bool Charify = false);
2045ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang
2055ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// Stringify - Convert the specified string into a C string by escaping '\'
2069b35249446b07f40ac5fcc3205f2c048616efacchkuang  /// and " characters.  This does not add surrounding ""'s to the string.
2075ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  static void Stringify(llvm::SmallVectorImpl<char> &Str);
2089b35249446b07f40ac5fcc3205f2c048616efacchkuang
2095ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// MeasureTokenLength - Relex the token at the specified location and return
2109b35249446b07f40ac5fcc3205f2c048616efacchkuang  /// its length in bytes in the input file.  If the token needs cleaning (e.g.
2115ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// includes a trigraph or an escaped newline) then this count includes bytes
2129b35249446b07f40ac5fcc3205f2c048616efacchkuang  /// that are part of that.
2135ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  static unsigned MeasureTokenLength(SourceLocation Loc,
2149b35249446b07f40ac5fcc3205f2c048616efacchkuang                                     const SourceManager &SM);
2155ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang
2169b35249446b07f40ac5fcc3205f2c048616efacchkuang  //===--------------------------------------------------------------------===//
2175ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  // Internal implementation interfaces.
2189b35249446b07f40ac5fcc3205f2c048616efacchkuangprivate:
2195ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang
2209b35249446b07f40ac5fcc3205f2c048616efacchkuang  /// LexTokenInternal - Internal interface to lex a preprocessing token. Called
2215ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// by Lex.
2229b35249446b07f40ac5fcc3205f2c048616efacchkuang  ///
2235ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  void LexTokenInternal(Token &Result);
2249b35249446b07f40ac5fcc3205f2c048616efacchkuang
2255ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// FormTokenWithChars - When we lex a token, we have identified a span
2269b35249446b07f40ac5fcc3205f2c048616efacchkuang  /// starting at BufferPtr, going to TokEnd that forms the token.  This method
2275ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// takes that range and assigns it to the token as its location and size.  In
2289b35249446b07f40ac5fcc3205f2c048616efacchkuang  /// addition, since tokens cannot overlap, this also updates BufferPtr to be
2295ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  /// TokEnd.
2305ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  void FormTokenWithChars(Token &Result, const char *TokEnd,
2315ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang                          tok::TokenKind Kind) {
2325ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang    unsigned TokLen = TokEnd-BufferPtr;
2335ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang    Result.setLength(TokLen);
2345ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang    Result.setLocation(getSourceLocation(BufferPtr, TokLen));
235b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian    Result.setKind(Kind);
236b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian    BufferPtr = TokEnd;
237b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  }
238ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
239ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  /// isNextPPTokenLParen - Return 1 if the next unexpanded token will return a
240  /// tok::l_paren token, 0 if it is something else and 2 if there are no more
241  /// tokens in the buffer controlled by this lexer.
242  unsigned isNextPPTokenLParen();
243
244  //===--------------------------------------------------------------------===//
245  // Lexer character reading interfaces.
246public:
247
248  // This lexer is built on two interfaces for reading characters, both of which
249  // automatically provide phase 1/2 translation.  getAndAdvanceChar is used
250  // when we know that we will be reading a character from the input buffer and
251  // that this character will be part of the result token. This occurs in (f.e.)
252  // string processing, because we know we need to read until we find the
253  // closing '"' character.
254  //
255  // The second interface is the combination of PeekCharAndSize with
256  // ConsumeChar.  PeekCharAndSize reads a phase 1/2 translated character,
257  // returning it and its size.  If the lexer decides that this character is
258  // part of the current token, it calls ConsumeChar on it.  This two stage
259  // approach allows us to emit diagnostics for characters (e.g. warnings about
260  // trigraphs), knowing that they only are emitted if the character is
261  // consumed.
262
263  /// isObviouslySimpleCharacter - Return true if the specified character is
264  /// obviously the same in translation phase 1 and translation phase 3.  This
265  /// can return false for characters that end up being the same, but it will
266  /// never return true for something that needs to be mapped.
267  static bool isObviouslySimpleCharacter(char C) {
268    return C != '?' && C != '\\';
269  }
270
271  /// getAndAdvanceChar - Read a single 'character' from the specified buffer,
272  /// advance over it, and return it.  This is tricky in several cases.  Here we
273  /// just handle the trivial case and fall-back to the non-inlined
274  /// getCharAndSizeSlow method to handle the hard case.
275  inline char getAndAdvanceChar(const char *&Ptr, Token &Tok) {
276    // If this is not a trigraph and not a UCN or escaped newline, return
277    // quickly.
278    if (isObviouslySimpleCharacter(Ptr[0])) return *Ptr++;
279
280    unsigned Size = 0;
281    char C = getCharAndSizeSlow(Ptr, Size, &Tok);
282    Ptr += Size;
283    return C;
284  }
285
286private:
287  /// ConsumeChar - When a character (identified by PeekCharAndSize) is consumed
288  /// and added to a given token, check to see if there are diagnostics that
289  /// need to be emitted or flags that need to be set on the token.  If so, do
290  /// it.
291  const char *ConsumeChar(const char *Ptr, unsigned Size, Token &Tok) {
292    // Normal case, we consumed exactly one token.  Just return it.
293    if (Size == 1)
294      return Ptr+Size;
295
296    // Otherwise, re-lex the character with a current token, allowing
297    // diagnostics to be emitted and flags to be set.
298    Size = 0;
299    getCharAndSizeSlow(Ptr, Size, &Tok);
300    return Ptr+Size;
301  }
302
303  /// getCharAndSize - Peek a single 'character' from the specified buffer,
304  /// get its size, and return it.  This is tricky in several cases.  Here we
305  /// just handle the trivial case and fall-back to the non-inlined
306  /// getCharAndSizeSlow method to handle the hard case.
307  inline char getCharAndSize(const char *Ptr, unsigned &Size) {
308    // If this is not a trigraph and not a UCN or escaped newline, return
309    // quickly.
310    if (isObviouslySimpleCharacter(Ptr[0])) {
311      Size = 1;
312      return *Ptr;
313    }
314
315    Size = 0;
316    return getCharAndSizeSlow(Ptr, Size);
317  }
318
319  /// getCharAndSizeSlow - Handle the slow/uncommon case of the getCharAndSize
320  /// method.
321  char getCharAndSizeSlow(const char *Ptr, unsigned &Size, Token *Tok = 0);
322public:
323
324  /// getCharAndSizeNoWarn - Like the getCharAndSize method, but does not ever
325  /// emit a warning.
326  static inline char getCharAndSizeNoWarn(const char *Ptr, unsigned &Size,
327                                          const LangOptions &Features) {
328    // If this is not a trigraph and not a UCN or escaped newline, return
329    // quickly.
330    if (isObviouslySimpleCharacter(Ptr[0])) {
331      Size = 1;
332      return *Ptr;
333    }
334
335    Size = 0;
336    return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
337  }
338private:
339
340  /// getCharAndSizeSlowNoWarn - Same as getCharAndSizeSlow, but never emits a
341  /// diagnostic.
342  static char getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
343                                       const LangOptions &Features);
344
345  //===--------------------------------------------------------------------===//
346  // Other lexer functions.
347
348  // Helper functions to lex the remainder of a token of the specific type.
349  void LexIdentifier         (Token &Result, const char *CurPtr);
350  void LexNumericConstant    (Token &Result, const char *CurPtr);
351  void LexStringLiteral      (Token &Result, const char *CurPtr,bool Wide);
352  void LexAngledStringLiteral(Token &Result, const char *CurPtr);
353  void LexCharConstant       (Token &Result, const char *CurPtr);
354  bool LexEndOfFile          (Token &Result, const char *CurPtr);
355
356  bool SkipWhitespace        (Token &Result, const char *CurPtr);
357  bool SkipBCPLComment       (Token &Result, const char *CurPtr);
358  bool SkipBlockComment      (Token &Result, const char *CurPtr);
359  bool SaveBCPLComment       (Token &Result, const char *CurPtr);
360};
361
362
363}  // end namespace clang
364
365#endif
366