1193326Sed//===--- Lexer.h - C Language Family Lexer ----------------------*- C++ -*-===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This file defines the Lexer interface. 11193326Sed// 12193326Sed//===----------------------------------------------------------------------===// 13193326Sed 14193326Sed#ifndef LLVM_CLANG_LEXER_H 15193326Sed#define LLVM_CLANG_LEXER_H 16193326Sed 17249423Sdim#include "clang/Basic/LangOptions.h" 18193326Sed#include "clang/Lex/PreprocessorLexer.h" 19193326Sed#include "llvm/ADT/SmallVector.h" 20249423Sdim#include <cassert> 21193326Sed#include <string> 22193326Sed 23193326Sednamespace clang { 24226633Sdimclass DiagnosticsEngine; 25193326Sedclass SourceManager; 26193326Sedclass Preprocessor; 27193326Sedclass DiagnosticBuilder; 28193326Sed 29226633Sdim/// ConflictMarkerKind - Kinds of conflict marker which the lexer might be 30226633Sdim/// recovering from. 31226633Sdimenum ConflictMarkerKind { 32226633Sdim /// Not within a conflict marker. 33226633Sdim CMK_None, 34243830Sdim /// A normal or diff3 conflict marker, initiated by at least 7 "<"s, 35243830Sdim /// separated by at least 7 "="s or "|"s, and terminated by at least 7 ">"s. 36226633Sdim CMK_Normal, 37243830Sdim /// A Perforce-style conflict marker, initiated by 4 ">"s, 38243830Sdim /// separated by 4 "="s, and terminated by 4 "<"s. 39226633Sdim CMK_Perforce 40226633Sdim}; 41226633Sdim 42193326Sed/// Lexer - This provides a simple interface that turns a text buffer into a 43193326Sed/// stream of tokens. This provides no support for file reading or buffering, 44193326Sed/// or buffering/seeking of tokens, only forward lexing is supported. It relies 45193326Sed/// on the specified Preprocessor object to handle preprocessor directives, etc. 46193326Sedclass Lexer : public PreprocessorLexer { 47234353Sdim virtual void anchor(); 48234353Sdim 49193326Sed //===--------------------------------------------------------------------===// 50193326Sed // Constant configuration values for this lexer. 51193326Sed const char *BufferStart; // Start of the buffer. 52193326Sed const char *BufferEnd; // End of the buffer. 53193326Sed SourceLocation FileLoc; // Location for start of file. 54234353Sdim LangOptions LangOpts; // LangOpts enabled by this language (cache). 55226633Sdim bool Is_PragmaLexer; // True if lexer for _Pragma handling. 56193326Sed 57193326Sed //===--------------------------------------------------------------------===// 58193326Sed // Context-specific lexing flags set by the preprocessor. 59193326Sed // 60198092Srdivacky 61193326Sed /// ExtendedTokenMode - The lexer can optionally keep comments and whitespace 62193326Sed /// and return them as tokens. This is used for -C and -CC modes, and 63193326Sed /// whitespace preservation can be useful for some clients that want to lex 64193326Sed /// the file in raw mode and get every character from the file. 65193326Sed /// 66193326Sed /// When this is set to 2 it returns comments and whitespace. When set to 1 67193326Sed /// it returns comments, when it is set to 0 it returns normal tokens only. 68193326Sed unsigned char ExtendedTokenMode; 69198092Srdivacky 70193326Sed //===--------------------------------------------------------------------===// 71193326Sed // Context that changes as the file is lexed. 72193326Sed // NOTE: any state that mutates when in raw mode must have save/restore code 73193326Sed // in Lexer::isNextPPTokenLParen. 74193326Sed 75193326Sed // BufferPtr - Current pointer into the buffer. This is the next character 76193326Sed // to be lexed. 77193326Sed const char *BufferPtr; 78193326Sed 79193326Sed // IsAtStartOfLine - True if the next lexed token should get the "start of 80193326Sed // line" flag set on it. 81193326Sed bool IsAtStartOfLine; 82198092Srdivacky 83263508Sdim bool IsAtPhysicalStartOfLine; 84263508Sdim 85263508Sdim bool HasLeadingSpace; 86263508Sdim 87263508Sdim bool HasLeadingEmptyMacro; 88263508Sdim 89226633Sdim // CurrentConflictMarkerState - The kind of conflict marker we are handling. 90226633Sdim ConflictMarkerKind CurrentConflictMarkerState; 91226633Sdim 92243830Sdim Lexer(const Lexer &) LLVM_DELETED_FUNCTION; 93243830Sdim void operator=(const Lexer &) LLVM_DELETED_FUNCTION; 94193326Sed friend class Preprocessor; 95198092Srdivacky 96193326Sed void InitLexer(const char *BufStart, const char *BufPtr, const char *BufEnd); 97193326Sedpublic: 98198092Srdivacky 99193326Sed /// Lexer constructor - Create a new lexer object for the specified buffer 100193326Sed /// with the specified preprocessor managing the lexing process. This lexer 101193326Sed /// assumes that the associated file buffer and Preprocessor objects will 102193326Sed /// outlive it, so it doesn't take ownership of either of them. 103199990Srdivacky Lexer(FileID FID, const llvm::MemoryBuffer *InputBuffer, Preprocessor &PP); 104193326Sed 105193326Sed /// Lexer constructor - Create a new raw lexer object. This object is only 106239462Sdim /// suitable for calls to 'LexFromRawLexer'. This lexer assumes that the 107239462Sdim /// text range will outlive it, so it doesn't take ownership of it. 108234353Sdim Lexer(SourceLocation FileLoc, const LangOptions &LangOpts, 109193326Sed const char *BufStart, const char *BufPtr, const char *BufEnd); 110198092Srdivacky 111193326Sed /// Lexer constructor - Create a new raw lexer object. This object is only 112239462Sdim /// suitable for calls to 'LexFromRawLexer'. This lexer assumes that the 113239462Sdim /// text range will outlive it, so it doesn't take ownership of it. 114199990Srdivacky Lexer(FileID FID, const llvm::MemoryBuffer *InputBuffer, 115234353Sdim const SourceManager &SM, const LangOptions &LangOpts); 116198092Srdivacky 117193326Sed /// Create_PragmaLexer: Lexer constructor - Create a new lexer object for 118193326Sed /// _Pragma expansion. This has a variety of magic semantics that this method 119193326Sed /// sets up. It returns a new'd Lexer that must be delete'd when done. 120198092Srdivacky static Lexer *Create_PragmaLexer(SourceLocation SpellingLoc, 121224145Sdim SourceLocation ExpansionLocStart, 122224145Sdim SourceLocation ExpansionLocEnd, 123193326Sed unsigned TokLen, Preprocessor &PP); 124198092Srdivacky 125198092Srdivacky 126234353Sdim /// getLangOpts - Return the language features currently enabled. 127234353Sdim /// NOTE: this lexer modifies features as a file is parsed! 128234353Sdim const LangOptions &getLangOpts() const { return LangOpts; } 129193326Sed 130193326Sed /// getFileLoc - Return the File Location for the file we are lexing out of. 131193326Sed /// The physical location encodes the location where the characters come from, 132193326Sed /// the virtual location encodes where we should *claim* the characters came 133193326Sed /// from. Currently this is only used by _Pragma handling. 134193326Sed SourceLocation getFileLoc() const { return FileLoc; } 135198092Srdivacky 136263508Sdimprivate: 137193326Sed /// Lex - Return the next token in the file. If this is the end of file, it 138243830Sdim /// return the tok::eof token. This implicitly involves the preprocessor. 139263508Sdim bool Lex(Token &Result); 140198092Srdivacky 141263508Sdimpublic: 142193326Sed /// isPragmaLexer - Returns true if this Lexer is being used to lex a pragma. 143193326Sed bool isPragmaLexer() const { return Is_PragmaLexer; } 144198092Srdivacky 145263508Sdimprivate: 146193326Sed /// IndirectLex - An indirect call to 'Lex' that can be invoked via 147193326Sed /// the PreprocessorLexer interface. 148193326Sed void IndirectLex(Token &Result) { Lex(Result); } 149198092Srdivacky 150263508Sdimpublic: 151193326Sed /// LexFromRawLexer - Lex a token from a designated raw lexer (one with no 152193326Sed /// associated preprocessor object. Return true if the 'next character to 153193326Sed /// read' pointer points at the end of the lexer buffer, false otherwise. 154193326Sed bool LexFromRawLexer(Token &Result) { 155193326Sed assert(LexingRawMode && "Not already in raw mode!"); 156193326Sed Lex(Result); 157193326Sed // Note that lexing to the end of the buffer doesn't implicitly delete the 158193326Sed // lexer when in raw mode. 159198092Srdivacky return BufferPtr == BufferEnd; 160193326Sed } 161193326Sed 162193326Sed /// isKeepWhitespaceMode - Return true if the lexer should return tokens for 163193326Sed /// every character in the file, including whitespace and comments. This 164193326Sed /// should only be used in raw mode, as the preprocessor is not prepared to 165193326Sed /// deal with the excess tokens. 166193326Sed bool isKeepWhitespaceMode() const { 167193326Sed return ExtendedTokenMode > 1; 168193326Sed } 169193326Sed 170193326Sed /// SetKeepWhitespaceMode - This method lets clients enable or disable 171193326Sed /// whitespace retention mode. 172193326Sed void SetKeepWhitespaceMode(bool Val) { 173249423Sdim assert((!Val || LexingRawMode || LangOpts.TraditionalCPP) && 174249423Sdim "Can only retain whitespace in raw mode or -traditional-cpp"); 175193326Sed ExtendedTokenMode = Val ? 2 : 0; 176193326Sed } 177193326Sed 178193326Sed /// inKeepCommentMode - Return true if the lexer should return comments as 179193326Sed /// tokens. 180193326Sed bool inKeepCommentMode() const { 181193326Sed return ExtendedTokenMode > 0; 182193326Sed } 183198092Srdivacky 184193326Sed /// SetCommentRetentionMode - Change the comment retention mode of the lexer 185193326Sed /// to the specified mode. This is really only useful when lexing in raw 186193326Sed /// mode, because otherwise the lexer needs to manage this. 187198092Srdivacky void SetCommentRetentionState(bool Mode) { 188193326Sed assert(!isKeepWhitespaceMode() && 189193326Sed "Can't play with comment retention state when retaining whitespace"); 190193326Sed ExtendedTokenMode = Mode ? 1 : 0; 191193326Sed } 192198092Srdivacky 193249423Sdim /// Sets the extended token mode back to its initial value, according to the 194249423Sdim /// language options and preprocessor. This controls whether the lexer 195249423Sdim /// produces comment and whitespace tokens. 196249423Sdim /// 197249423Sdim /// This requires the lexer to have an associated preprocessor. A standalone 198249423Sdim /// lexer has nothing to reset to. 199249423Sdim void resetExtendedTokenMode(); 200249423Sdim 201263508Sdim /// Gets source code buffer. 202263508Sdim StringRef getBuffer() const { 203263508Sdim return StringRef(BufferStart, BufferEnd - BufferStart); 204263508Sdim } 205198092Srdivacky 206193326Sed /// ReadToEndOfLine - Read the rest of the current preprocessor line as an 207193326Sed /// uninterpreted string. This switches the lexer out of directive mode. 208239462Sdim void ReadToEndOfLine(SmallVectorImpl<char> *Result = 0); 209198092Srdivacky 210198092Srdivacky 211193326Sed /// Diag - Forwarding function for diagnostics. This translate a source 212193326Sed /// position in the current buffer into a SourceLocation object for rendering. 213193326Sed DiagnosticBuilder Diag(const char *Loc, unsigned DiagID) const; 214193326Sed 215193326Sed /// getSourceLocation - Return a source location identifier for the specified 216193326Sed /// offset in the current file. 217193326Sed SourceLocation getSourceLocation(const char *Loc, unsigned TokLen = 1) const; 218198092Srdivacky 219193326Sed /// getSourceLocation - Return a source location for the next character in 220193326Sed /// the current file. 221193326Sed SourceLocation getSourceLocation() { return getSourceLocation(BufferPtr); } 222198092Srdivacky 223203955Srdivacky /// \brief Return the current location in the buffer. 224203955Srdivacky const char *getBufferLocation() const { return BufferPtr; } 225203955Srdivacky 226193326Sed /// Stringify - Convert the specified string into a C string by escaping '\' 227193326Sed /// and " characters. This does not add surrounding ""'s to the string. 228193326Sed /// If Charify is true, this escapes the ' character instead of ". 229193326Sed static std::string Stringify(const std::string &Str, bool Charify = false); 230198092Srdivacky 231193326Sed /// Stringify - Convert the specified string into a C string by escaping '\' 232193326Sed /// and " characters. This does not add surrounding ""'s to the string. 233226633Sdim static void Stringify(SmallVectorImpl<char> &Str); 234198092Srdivacky 235218893Sdim 236218893Sdim /// getSpelling - This method is used to get the spelling of a token into a 237218893Sdim /// preallocated buffer, instead of as an std::string. The caller is required 238218893Sdim /// to allocate enough space for the token, which is guaranteed to be at least 239218893Sdim /// Tok.getLength() bytes long. The length of the actual result is returned. 240218893Sdim /// 241218893Sdim /// Note that this method may do two possible things: it may either fill in 242218893Sdim /// the buffer specified with characters, or it may *change the input pointer* 243218893Sdim /// to point to a constant buffer with the data already in it (avoiding a 244218893Sdim /// copy). The caller is not allowed to modify the returned buffer pointer 245218893Sdim /// if an internal buffer is returned. 246218893Sdim static unsigned getSpelling(const Token &Tok, const char *&Buffer, 247218893Sdim const SourceManager &SourceMgr, 248234353Sdim const LangOptions &LangOpts, 249218893Sdim bool *Invalid = 0); 250218893Sdim 251218893Sdim /// getSpelling() - Return the 'spelling' of the Tok token. The spelling of a 252218893Sdim /// token is the characters used to represent the token in the source file 253218893Sdim /// after trigraph expansion and escaped-newline folding. In particular, this 254218893Sdim /// wants to get the true, uncanonicalized, spelling of things like digraphs 255218893Sdim /// UCNs, etc. 256218893Sdim static std::string getSpelling(const Token &Tok, 257218893Sdim const SourceManager &SourceMgr, 258234353Sdim const LangOptions &LangOpts, 259218893Sdim bool *Invalid = 0); 260221345Sdim 261221345Sdim /// getSpelling - This method is used to get the spelling of the 262221345Sdim /// token at the given source location. If, as is usually true, it 263221345Sdim /// is not necessary to copy any data, then the returned string may 264221345Sdim /// not point into the provided buffer. 265221345Sdim /// 266224145Sdim /// This method lexes at the expansion depth of the given 267224145Sdim /// location and does not jump to the expansion or spelling 268221345Sdim /// location. 269226633Sdim static StringRef getSpelling(SourceLocation loc, 270249423Sdim SmallVectorImpl<char> &buffer, 271249423Sdim const SourceManager &SourceMgr, 272249423Sdim const LangOptions &LangOpts, 273249423Sdim bool *invalid = 0); 274218893Sdim 275193326Sed /// MeasureTokenLength - Relex the token at the specified location and return 276193326Sed /// its length in bytes in the input file. If the token needs cleaning (e.g. 277193326Sed /// includes a trigraph or an escaped newline) then this count includes bytes 278193326Sed /// that are part of that. 279193326Sed static unsigned MeasureTokenLength(SourceLocation Loc, 280193326Sed const SourceManager &SM, 281193326Sed const LangOptions &LangOpts); 282198092Srdivacky 283249423Sdim /// \brief Relex the token at the specified location. 284249423Sdim /// \returns true if there was a failure, false on success. 285249423Sdim static bool getRawToken(SourceLocation Loc, Token &Result, 286249423Sdim const SourceManager &SM, 287263508Sdim const LangOptions &LangOpts, 288263508Sdim bool IgnoreWhiteSpace = false); 289249423Sdim 290212904Sdim /// \brief Given a location any where in a source buffer, find the location 291212904Sdim /// that corresponds to the beginning of the token in which the original 292212904Sdim /// source location lands. 293212904Sdim static SourceLocation GetBeginningOfToken(SourceLocation Loc, 294212904Sdim const SourceManager &SM, 295212904Sdim const LangOptions &LangOpts); 296212904Sdim 297218893Sdim /// AdvanceToTokenCharacter - If the current SourceLocation specifies a 298218893Sdim /// location at the start of a token, return a new location that specifies a 299218893Sdim /// character within the token. This handles trigraphs and escaped newlines. 300218893Sdim static SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, 301218893Sdim unsigned Character, 302218893Sdim const SourceManager &SM, 303234353Sdim const LangOptions &LangOpts); 304218893Sdim 305218893Sdim /// \brief Computes the source location just past the end of the 306218893Sdim /// token at this source location. 307218893Sdim /// 308218893Sdim /// This routine can be used to produce a source location that 309218893Sdim /// points just past the end of the token referenced by \p Loc, and 310218893Sdim /// is generally used when a diagnostic needs to point just after a 311218893Sdim /// token where it expected something different that it received. If 312218893Sdim /// the returned source location would not be meaningful (e.g., if 313218893Sdim /// it points into a macro), this routine returns an invalid 314218893Sdim /// source location. 315218893Sdim /// 316218893Sdim /// \param Offset an offset from the end of the token, where the source 317218893Sdim /// location should refer to. The default offset (0) produces a source 318218893Sdim /// location pointing just past the end of the token; an offset of 1 produces 319218893Sdim /// a source location pointing to the last character in the token, etc. 320218893Sdim static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset, 321218893Sdim const SourceManager &SM, 322234353Sdim const LangOptions &LangOpts); 323224145Sdim 324224145Sdim /// \brief Returns true if the given MacroID location points at the first 325224145Sdim /// token of the macro expansion. 326234353Sdim /// 327234353Sdim /// \param MacroBegin If non-null and function returns true, it is set to 328234353Sdim /// begin location of the macro. 329224145Sdim static bool isAtStartOfMacroExpansion(SourceLocation loc, 330234353Sdim const SourceManager &SM, 331234353Sdim const LangOptions &LangOpts, 332234353Sdim SourceLocation *MacroBegin = 0); 333224145Sdim 334224145Sdim /// \brief Returns true if the given MacroID location points at the last 335224145Sdim /// token of the macro expansion. 336234353Sdim /// 337243830Sdim /// \param MacroEnd If non-null and function returns true, it is set to 338234353Sdim /// end location of the macro. 339224145Sdim static bool isAtEndOfMacroExpansion(SourceLocation loc, 340234353Sdim const SourceManager &SM, 341234353Sdim const LangOptions &LangOpts, 342234353Sdim SourceLocation *MacroEnd = 0); 343224145Sdim 344234353Sdim /// \brief Accepts a range and returns a character range with file locations. 345234353Sdim /// 346234353Sdim /// Returns a null range if a part of the range resides inside a macro 347234353Sdim /// expansion or the range does not reside on the same FileID. 348239462Sdim /// 349239462Sdim /// This function is trying to deal with macros and return a range based on 350239462Sdim /// file locations. The cases where it can successfully handle macros are: 351239462Sdim /// 352239462Sdim /// -begin or end range lies at the start or end of a macro expansion, in 353239462Sdim /// which case the location will be set to the expansion point, e.g: 354239462Sdim /// \#define M 1 2 355239462Sdim /// a M 356239462Sdim /// If you have a range [a, 2] (where 2 came from the macro), the function 357239462Sdim /// will return a range for "a M" 358239462Sdim /// if you have range [a, 1], the function will fail because the range 359239462Sdim /// overlaps with only a part of the macro 360239462Sdim /// 361239462Sdim /// -The macro is a function macro and the range can be mapped to the macro 362239462Sdim /// arguments, e.g: 363239462Sdim /// \#define M 1 2 364239462Sdim /// \#define FM(x) x 365239462Sdim /// FM(a b M) 366239462Sdim /// if you have range [b, 2], the function will return the file range "b M" 367239462Sdim /// inside the macro arguments. 368239462Sdim /// if you have range [a, 2], the function will return the file range 369239462Sdim /// "FM(a b M)" since the range includes all of the macro expansion. 370234353Sdim static CharSourceRange makeFileCharRange(CharSourceRange Range, 371234353Sdim const SourceManager &SM, 372234353Sdim const LangOptions &LangOpts); 373234353Sdim 374234353Sdim /// \brief Returns a string for the source that the range encompasses. 375234353Sdim static StringRef getSourceText(CharSourceRange Range, 376234353Sdim const SourceManager &SM, 377234353Sdim const LangOptions &LangOpts, 378234353Sdim bool *Invalid = 0); 379234353Sdim 380234353Sdim /// \brief Retrieve the name of the immediate macro expansion. 381234353Sdim /// 382234353Sdim /// This routine starts from a source location, and finds the name of the macro 383234353Sdim /// responsible for its immediate expansion. It looks through any intervening 384234353Sdim /// macro argument expansions to compute this. It returns a StringRef which 385234353Sdim /// refers to the SourceManager-owned buffer of the source where that macro 386234353Sdim /// name is spelled. Thus, the result shouldn't out-live that SourceManager. 387234353Sdim static StringRef getImmediateMacroName(SourceLocation Loc, 388234353Sdim const SourceManager &SM, 389234353Sdim const LangOptions &LangOpts); 390234353Sdim 391212904Sdim /// \brief Compute the preamble of the given file. 392212904Sdim /// 393212904Sdim /// The preamble of a file contains the initial comments, include directives, 394212904Sdim /// and other preprocessor directives that occur before the code in this 395212904Sdim /// particular file actually begins. The preamble of the main source file is 396212904Sdim /// a potential prefix header. 397212904Sdim /// 398212904Sdim /// \param Buffer The memory buffer containing the file's contents. 399212904Sdim /// 400212904Sdim /// \param MaxLines If non-zero, restrict the length of the preamble 401212904Sdim /// to fewer than this number of lines. 402212904Sdim /// 403212904Sdim /// \returns The offset into the file where the preamble ends and the rest 404212904Sdim /// of the file begins along with a boolean value indicating whether 405212904Sdim /// the preamble ends at the beginning of a new line. 406212904Sdim static std::pair<unsigned, bool> 407234353Sdim ComputePreamble(const llvm::MemoryBuffer *Buffer, const LangOptions &LangOpts, 408226633Sdim unsigned MaxLines = 0); 409243830Sdim 410243830Sdim /// \brief Checks that the given token is the first token that occurs after 411243830Sdim /// the given location (this excludes comments and whitespace). Returns the 412243830Sdim /// location immediately after the specified token. If the token is not found 413243830Sdim /// or the location is inside a macro, the returned source location will be 414243830Sdim /// invalid. 415243830Sdim static SourceLocation findLocationAfterToken(SourceLocation loc, 416243830Sdim tok::TokenKind TKind, 417243830Sdim const SourceManager &SM, 418243830Sdim const LangOptions &LangOpts, 419243830Sdim bool SkipTrailingWhitespaceAndNewLine); 420243830Sdim 421243830Sdim /// \brief Returns true if the given character could appear in an identifier. 422243830Sdim static bool isIdentifierBodyChar(char c, const LangOptions &LangOpts); 423243830Sdim 424243830Sdim /// getCharAndSizeNoWarn - Like the getCharAndSize method, but does not ever 425243830Sdim /// emit a warning. 426243830Sdim static inline char getCharAndSizeNoWarn(const char *Ptr, unsigned &Size, 427243830Sdim const LangOptions &LangOpts) { 428243830Sdim // If this is not a trigraph and not a UCN or escaped newline, return 429243830Sdim // quickly. 430243830Sdim if (isObviouslySimpleCharacter(Ptr[0])) { 431243830Sdim Size = 1; 432243830Sdim return *Ptr; 433243830Sdim } 434243830Sdim 435243830Sdim Size = 0; 436243830Sdim return getCharAndSizeSlowNoWarn(Ptr, Size, LangOpts); 437243830Sdim } 438243830Sdim 439193326Sed //===--------------------------------------------------------------------===// 440193326Sed // Internal implementation interfaces. 441193326Sedprivate: 442193326Sed 443193326Sed /// LexTokenInternal - Internal interface to lex a preprocessing token. Called 444193326Sed /// by Lex. 445193326Sed /// 446263508Sdim bool LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine); 447193326Sed 448263508Sdim bool CheckUnicodeWhitespace(Token &Result, uint32_t C, const char *CurPtr); 449263508Sdim 450249423Sdim /// Given that a token begins with the Unicode character \p C, figure out 451249423Sdim /// what kind of token it is and dispatch to the appropriate lexing helper 452249423Sdim /// function. 453263508Sdim bool LexUnicode(Token &Result, uint32_t C, const char *CurPtr); 454249423Sdim 455193326Sed /// FormTokenWithChars - When we lex a token, we have identified a span 456193326Sed /// starting at BufferPtr, going to TokEnd that forms the token. This method 457193326Sed /// takes that range and assigns it to the token as its location and size. In 458193326Sed /// addition, since tokens cannot overlap, this also updates BufferPtr to be 459193326Sed /// TokEnd. 460198092Srdivacky void FormTokenWithChars(Token &Result, const char *TokEnd, 461193326Sed tok::TokenKind Kind) { 462193326Sed unsigned TokLen = TokEnd-BufferPtr; 463193326Sed Result.setLength(TokLen); 464193326Sed Result.setLocation(getSourceLocation(BufferPtr, TokLen)); 465193326Sed Result.setKind(Kind); 466193326Sed BufferPtr = TokEnd; 467193326Sed } 468198092Srdivacky 469193326Sed /// isNextPPTokenLParen - Return 1 if the next unexpanded token will return a 470193326Sed /// tok::l_paren token, 0 if it is something else and 2 if there are no more 471193326Sed /// tokens in the buffer controlled by this lexer. 472193326Sed unsigned isNextPPTokenLParen(); 473193326Sed 474193326Sed //===--------------------------------------------------------------------===// 475193326Sed // Lexer character reading interfaces. 476198092Srdivacky 477193326Sed // This lexer is built on two interfaces for reading characters, both of which 478193326Sed // automatically provide phase 1/2 translation. getAndAdvanceChar is used 479193326Sed // when we know that we will be reading a character from the input buffer and 480193326Sed // that this character will be part of the result token. This occurs in (f.e.) 481193326Sed // string processing, because we know we need to read until we find the 482193326Sed // closing '"' character. 483193326Sed // 484201361Srdivacky // The second interface is the combination of getCharAndSize with 485201361Srdivacky // ConsumeChar. getCharAndSize reads a phase 1/2 translated character, 486193326Sed // returning it and its size. If the lexer decides that this character is 487193326Sed // part of the current token, it calls ConsumeChar on it. This two stage 488193326Sed // approach allows us to emit diagnostics for characters (e.g. warnings about 489193326Sed // trigraphs), knowing that they only are emitted if the character is 490193326Sed // consumed. 491198092Srdivacky 492193326Sed /// isObviouslySimpleCharacter - Return true if the specified character is 493193326Sed /// obviously the same in translation phase 1 and translation phase 3. This 494193326Sed /// can return false for characters that end up being the same, but it will 495193326Sed /// never return true for something that needs to be mapped. 496193326Sed static bool isObviouslySimpleCharacter(char C) { 497193326Sed return C != '?' && C != '\\'; 498193326Sed } 499198092Srdivacky 500193326Sed /// getAndAdvanceChar - Read a single 'character' from the specified buffer, 501193326Sed /// advance over it, and return it. This is tricky in several cases. Here we 502193326Sed /// just handle the trivial case and fall-back to the non-inlined 503193326Sed /// getCharAndSizeSlow method to handle the hard case. 504193326Sed inline char getAndAdvanceChar(const char *&Ptr, Token &Tok) { 505193326Sed // If this is not a trigraph and not a UCN or escaped newline, return 506193326Sed // quickly. 507193326Sed if (isObviouslySimpleCharacter(Ptr[0])) return *Ptr++; 508198092Srdivacky 509193326Sed unsigned Size = 0; 510193326Sed char C = getCharAndSizeSlow(Ptr, Size, &Tok); 511193326Sed Ptr += Size; 512193326Sed return C; 513193326Sed } 514198092Srdivacky 515201361Srdivacky /// ConsumeChar - When a character (identified by getCharAndSize) is consumed 516193326Sed /// and added to a given token, check to see if there are diagnostics that 517193326Sed /// need to be emitted or flags that need to be set on the token. If so, do 518193326Sed /// it. 519193326Sed const char *ConsumeChar(const char *Ptr, unsigned Size, Token &Tok) { 520193326Sed // Normal case, we consumed exactly one token. Just return it. 521193326Sed if (Size == 1) 522193326Sed return Ptr+Size; 523193326Sed 524193326Sed // Otherwise, re-lex the character with a current token, allowing 525193326Sed // diagnostics to be emitted and flags to be set. 526193326Sed Size = 0; 527193326Sed getCharAndSizeSlow(Ptr, Size, &Tok); 528193326Sed return Ptr+Size; 529193326Sed } 530198092Srdivacky 531193326Sed /// getCharAndSize - Peek a single 'character' from the specified buffer, 532193326Sed /// get its size, and return it. This is tricky in several cases. Here we 533193326Sed /// just handle the trivial case and fall-back to the non-inlined 534193326Sed /// getCharAndSizeSlow method to handle the hard case. 535193326Sed inline char getCharAndSize(const char *Ptr, unsigned &Size) { 536193326Sed // If this is not a trigraph and not a UCN or escaped newline, return 537193326Sed // quickly. 538193326Sed if (isObviouslySimpleCharacter(Ptr[0])) { 539193326Sed Size = 1; 540193326Sed return *Ptr; 541193326Sed } 542198092Srdivacky 543193326Sed Size = 0; 544193326Sed return getCharAndSizeSlow(Ptr, Size); 545193326Sed } 546198092Srdivacky 547193326Sed /// getCharAndSizeSlow - Handle the slow/uncommon case of the getCharAndSize 548193326Sed /// method. 549193326Sed char getCharAndSizeSlow(const char *Ptr, unsigned &Size, Token *Tok = 0); 550198092Srdivacky 551193326Sed /// getEscapedNewLineSize - Return the size of the specified escaped newline, 552193326Sed /// or 0 if it is not an escaped newline. P[-1] is known to be a "\" on entry 553193326Sed /// to this function. 554193326Sed static unsigned getEscapedNewLineSize(const char *P); 555198092Srdivacky 556193326Sed /// SkipEscapedNewLines - If P points to an escaped newline (or a series of 557193326Sed /// them), skip over them and return the first non-escaped-newline found, 558193326Sed /// otherwise return P. 559193326Sed static const char *SkipEscapedNewLines(const char *P); 560226633Sdim 561193326Sed /// getCharAndSizeSlowNoWarn - Same as getCharAndSizeSlow, but never emits a 562193326Sed /// diagnostic. 563193326Sed static char getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size, 564234353Sdim const LangOptions &LangOpts); 565198092Srdivacky 566193326Sed //===--------------------------------------------------------------------===// 567193326Sed // Other lexer functions. 568198092Srdivacky 569212904Sdim void SkipBytes(unsigned Bytes, bool StartOfLine); 570234353Sdim 571263508Sdim void PropagateLineStartLeadingSpaceInfo(Token &Result); 572263508Sdim 573263508Sdim const char *LexUDSuffix(Token &Result, const char *CurPtr, 574263508Sdim bool IsStringLiteral); 575263508Sdim 576193326Sed // Helper functions to lex the remainder of a token of the specific type. 577263508Sdim bool LexIdentifier (Token &Result, const char *CurPtr); 578263508Sdim bool LexNumericConstant (Token &Result, const char *CurPtr); 579263508Sdim bool LexStringLiteral (Token &Result, const char *CurPtr, 580226633Sdim tok::TokenKind Kind); 581263508Sdim bool LexRawStringLiteral (Token &Result, const char *CurPtr, 582226633Sdim tok::TokenKind Kind); 583263508Sdim bool LexAngledStringLiteral(Token &Result, const char *CurPtr); 584263508Sdim bool LexCharConstant (Token &Result, const char *CurPtr, 585226633Sdim tok::TokenKind Kind); 586193326Sed bool LexEndOfFile (Token &Result, const char *CurPtr); 587263508Sdim bool SkipWhitespace (Token &Result, const char *CurPtr, 588263508Sdim bool &TokAtPhysicalStartOfLine); 589263508Sdim bool SkipLineComment (Token &Result, const char *CurPtr, 590263508Sdim bool &TokAtPhysicalStartOfLine); 591263508Sdim bool SkipBlockComment (Token &Result, const char *CurPtr, 592263508Sdim bool &TokAtPhysicalStartOfLine); 593243830Sdim bool SaveLineComment (Token &Result, const char *CurPtr); 594200583Srdivacky 595200583Srdivacky bool IsStartOfConflictMarker(const char *CurPtr); 596200583Srdivacky bool HandleEndOfConflictMarker(const char *CurPtr); 597226633Sdim 598226633Sdim bool isCodeCompletionPoint(const char *CurPtr) const; 599226633Sdim void cutOffLexing() { BufferPtr = BufferEnd; } 600243830Sdim 601243830Sdim bool isHexaLiteral(const char *Start, const LangOptions &LangOpts); 602249423Sdim 603249423Sdim 604249423Sdim /// Read a universal character name. 605249423Sdim /// 606249423Sdim /// \param CurPtr The position in the source buffer after the initial '\'. 607249423Sdim /// If the UCN is syntactically well-formed (but not necessarily 608249423Sdim /// valid), this parameter will be updated to point to the 609249423Sdim /// character after the UCN. 610249423Sdim /// \param SlashLoc The position in the source buffer of the '\'. 611249423Sdim /// \param Tok The token being formed. Pass \c NULL to suppress diagnostics 612249423Sdim /// and handle token formation in the caller. 613249423Sdim /// 614249423Sdim /// \return The Unicode codepoint specified by the UCN, or 0 if the UCN is 615249423Sdim /// invalid. 616249423Sdim uint32_t tryReadUCN(const char *&CurPtr, const char *SlashLoc, Token *Tok); 617193326Sed}; 618193326Sed 619193326Sed 620193326Sed} // end namespace clang 621193326Sed 622193326Sed#endif 623