Lexer.cpp revision 55fc873017f10f6f566b182b70f6fc22aefa3464
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Lexer.cpp - C Language Family Lexer ------------------------------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 10d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner// This file implements the Lexer and Token interfaces. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// TODO: GCC Diagnostics emitted by the lexer: 155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// PEDWARN: (form feed|vertical tab) in preprocessing directive 165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Universal characters, unicode, char mapping: 185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// WARNING: `%.*s' is not in NFKC 195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// WARNING: `%.*s' is not in NFC 205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Other: 225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// TODO: Options to support: 235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// -fexec-charset,-fwide-exec-charset 245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Lexer.h" 289dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner#include "clang/Basic/SourceManager.h" 2955fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/CodeCompletionHandler.h" 3055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/LexDiagnostic.h" 3155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/Preprocessor.h" 32d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis#include "llvm/ADT/STLExtras.h" 3355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/ADT/StringSwitch.h" 34409a03671224d4e5bdab1594c43baf070148f830Chris Lattner#include "llvm/Support/Compiler.h" 355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Support/MemoryBuffer.h" 362fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper#include <cstring> 375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 39a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic void InitCharacterInfo(); 405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 41dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 42dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner// Token Class Implementation 43dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 44dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier. 46dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattnerbool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const { 47bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor if (IdentifierInfo *II = getIdentifierInfo()) 48bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor return II->getObjCKeywordID() == objcKey; 49bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor return false; 50dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner} 51dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 52dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner/// getObjCKeywordID - Return the ObjC keyword kind. 53dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattnertok::ObjCKeywordKind Token::getObjCKeywordID() const { 54dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner IdentifierInfo *specId = getIdentifierInfo(); 55dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword; 56dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner} 57dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 5853702cd401b8fdca985aede7732c2f6a82ad9b1cChris Lattner 59dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 60dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner// Lexer Class Implementation 61dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 62dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 6399ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid Lexer::anchor() { } 6499ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie 651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Lexer::InitLexer(const char *BufStart, const char *BufPtr, 6622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner const char *BufEnd) { 67a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner InitCharacterInfo(); 681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner BufferStart = BufStart; 7022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner BufferPtr = BufPtr; 7122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner BufferEnd = BufEnd; 721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner assert(BufEnd[0] == 0 && 7422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner "We assume that the input buffer has a null character at the end" 7522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner " to simplify lexing!"); 761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 77156119df1d076b63609618976281961283f871dbEric Christopher // Check whether we have a BOM in the beginning of the buffer. If yes - act 78156119df1d076b63609618976281961283f871dbEric Christopher // accordingly. Right now we support only UTF-8 with and without BOM, so, just 79156119df1d076b63609618976281961283f871dbEric Christopher // skip the UTF-8 BOM if it's present. 80156119df1d076b63609618976281961283f871dbEric Christopher if (BufferStart == BufferPtr) { 81156119df1d076b63609618976281961283f871dbEric Christopher // Determine the size of the BOM. 825f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Buf(BufferStart, BufferEnd - BufferStart); 83969f9d47338fc36ebb6d24ad3a51e45eda07fd58Eli Friedman size_t BOMLength = llvm::StringSwitch<size_t>(Buf) 84156119df1d076b63609618976281961283f871dbEric Christopher .StartsWith("\xEF\xBB\xBF", 3) // UTF-8 BOM 85156119df1d076b63609618976281961283f871dbEric Christopher .Default(0); 86156119df1d076b63609618976281961283f871dbEric Christopher 87156119df1d076b63609618976281961283f871dbEric Christopher // Skip the BOM. 88156119df1d076b63609618976281961283f871dbEric Christopher BufferPtr += BOMLength; 89156119df1d076b63609618976281961283f871dbEric Christopher } 90156119df1d076b63609618976281961283f871dbEric Christopher 9122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner Is_PragmaLexer = false; 92d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith CurrentConflictMarkerState = CMK_None; 93156119df1d076b63609618976281961283f871dbEric Christopher 9422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // Start of the file is a start of line. 9522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner IsAtStartOfLine = true; 961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // We are not after parsing a #. 9822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner ParsingPreprocessorDirective = false; 991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // We are not after parsing #include. 10122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner ParsingFilename = false; 1021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // We are not in raw mode. Raw mode disables diagnostics and interpretation 10422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // of tokens (e.g. identifiers, thus disabling macro expansion). It is used 10522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block 10622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // or otherwise skipping over tokens. 10722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner LexingRawMode = false; 1081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // Default to not keeping comments. 11022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner ExtendedTokenMode = 0; 11122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner} 11222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner 1130770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// Lexer constructor - Create a new lexer object for the specified buffer 1140770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// with the specified preprocessor managing the lexing process. This lexer 1150770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// assumes that the associated file buffer and Preprocessor objects will 1160770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// outlive it, so it doesn't take ownership of either of them. 1176e2901407bff59aeb4cc301cc58b034723d0eb49Chris LattnerLexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP) 11888d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner : PreprocessorLexer(&PP, FID), 11988d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)), 1204e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie LangOpts(PP.getLangOpts()) { 1211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1220770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(), 1230770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner InputFile->getBufferEnd()); 1241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1250770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner // Default to keeping comments if the preprocessor wants them. 1260770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner SetCommentRetentionState(PP.getCommentRetentionState()); 1270770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner} 128dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 129168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner/// Lexer constructor - Create a new raw lexer object. This object is only 130092bf67e5ca560d2fc6aa70be1f172b8b3a5ff96Dmitri Gribenko/// suitable for calls to 'LexFromRawLexer'. This lexer assumes that the text 131590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner/// range will outlive it, so it doesn't take ownership of it. 1324e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid BlaikieLexer::Lexer(SourceLocation fileloc, const LangOptions &langOpts, 133de96c0f29c4cacabe6ea577c61db87c2a85aea6cChris Lattner const char *BufStart, const char *BufPtr, const char *BufEnd) 1344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie : FileLoc(fileloc), LangOpts(langOpts) { 13522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner 13622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner InitLexer(BufStart, BufPtr, BufEnd); 1371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 138168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner // We *are* in raw mode. 139168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner LexingRawMode = true; 1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 142025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// Lexer constructor - Create a new raw lexer object. This object is only 143092bf67e5ca560d2fc6aa70be1f172b8b3a5ff96Dmitri Gribenko/// suitable for calls to 'LexFromRawLexer'. This lexer assumes that the text 144025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// range will outlive it, so it doesn't take ownership of it. 1456e2901407bff59aeb4cc301cc58b034723d0eb49Chris LattnerLexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile, 1464e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const SourceManager &SM, const LangOptions &langOpts) 1474e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie : FileLoc(SM.getLocForStartOfFile(FID)), LangOpts(langOpts) { 148025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner 1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(), 150025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner FromFile->getBufferEnd()); 1511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 152025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner // We *are* in raw mode. 153025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner LexingRawMode = true; 154025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner} 155025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner 15642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for 15742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// _Pragma expansion. This has a variety of magic semantics that this method 15842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// sets up. It returns a new'd Lexer that must be delete'd when done. 15942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 16042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// On entrance to this routine, TokStartLoc is a macro location which has a 16142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// spelling loc that indicates the bytes to be lexed for the token and an 162433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// expansion location that indicates where all lexed tokens should be 16342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// "expanded from". 16442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 16542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// FIXME: It would really be nice to make _Pragma just be a wrapper around a 16642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// normal lexer that remaps tokens as they fly by. This would require making 16742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer 16842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// interface that could handle this stuff. This would pull GetMappedTokenLoc 16942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// out of the critical path of the lexer! 17042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 1711eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpLexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc, 172433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation ExpansionLocStart, 173433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation ExpansionLocEnd, 174bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner unsigned TokLen, Preprocessor &PP) { 17542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner SourceManager &SM = PP.getSourceManager(); 17642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 17742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Create the lexer as if we were going to lex the file normally. 178a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner FileID SpellingFID = SM.getFileID(SpellingLoc); 1796e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID); 1806e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner Lexer *L = new Lexer(SpellingFID, InputFile, PP); 1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Now that the lexer is created, change the start/end locations so that we 18342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // just lex the subsection of the file that we want. This is lexing from a 18442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // scratch buffer. 18542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner const char *StrData = SM.getCharacterData(SpellingLoc); 1861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->BufferPtr = StrData; 18842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->BufferEnd = StrData+TokLen; 1891fa495304c81e03f07f278a47b5efe9317104aabChris Lattner assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!"); 19042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 19142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Set the SourceLocation with the remapping information. This ensures that 19242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // GetMappedTokenLoc will remap the tokens as they are lexed. 193bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth L->FileLoc = SM.createExpansionLoc(SM.getLocForStartOfFile(SpellingFID), 194bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth ExpansionLocStart, 195bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth ExpansionLocEnd, TokLen); 1961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Ensure that the lexer thinks it is inside a directive, so that end \n will 19884021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // return an EOD token. 19942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->ParsingPreprocessorDirective = true; 2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // This lexer really is for _Pragma. 20242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->Is_PragmaLexer = true; 20342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner return L; 20442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner} 20542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 206168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner 2075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Stringify - Convert the specified string into a C string, with surrounding 2085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ""'s, and with escaped \ and " characters. 2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstd::string Lexer::Stringify(const std::string &Str, bool Charify) { 2105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer std::string Result = Str; 2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Quote = Charify ? '\'' : '"'; 2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = 0, e = Result.size(); i != e; ++i) { 2135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Result[i] == '\\' || Result[i] == Quote) { 2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result.insert(Result.begin()+i, '\\'); 2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++i; ++e; 2165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return Result; 2195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 221d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// Stringify - Convert the specified string into a C string by escaping '\' 222d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// and " characters. This does not add surrounding ""'s to the string. 2235f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid Lexer::Stringify(SmallVectorImpl<char> &Str) { 224d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner for (unsigned i = 0, e = Str.size(); i != e; ++i) { 225d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner if (Str[i] == '\\' || Str[i] == '"') { 226d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner Str.insert(Str.begin()+i, '\\'); 227d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner ++i; ++e; 228d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner } 229d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner } 230d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner} 231d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner 232b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner//===----------------------------------------------------------------------===// 233b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner// Token Spelling 234b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner//===----------------------------------------------------------------------===// 235b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 23630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith/// \brief Slow case of getSpelling. Extract the characters comprising the 23730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith/// spelling of this token from the provided input buffer. 23830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smithstatic size_t getSpellingSlow(const Token &Tok, const char *BufPtr, 23930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const LangOptions &LangOpts, char *Spelling) { 24030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith assert(Tok.needsCleaning() && "getSpellingSlow called on simple token"); 24130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 24230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith size_t Length = 0; 24330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const char *BufEnd = BufPtr + Tok.getLength(); 24430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 24530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith if (Tok.is(tok::string_literal)) { 24630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Munch the encoding-prefix and opening double-quote. 24730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith while (BufPtr < BufEnd) { 24830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith unsigned Size; 24930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Spelling[Length++] = Lexer::getCharAndSizeNoWarn(BufPtr, Size, LangOpts); 25030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith BufPtr += Size; 25130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 25230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith if (Spelling[Length - 1] == '"') 25330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith break; 25430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith } 25530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 25630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Raw string literals need special handling; trigraph expansion and line 25730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // splicing do not occur within their d-char-sequence nor within their 25830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // r-char-sequence. 25930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith if (Length >= 2 && 26030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Spelling[Length - 2] == 'R' && Spelling[Length - 1] == '"') { 26130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Search backwards from the end of the token to find the matching closing 26230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // quote. 26330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const char *RawEnd = BufEnd; 26430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith do --RawEnd; while (*RawEnd != '"'); 26530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith size_t RawLength = RawEnd - BufPtr + 1; 26630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 26730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Everything between the quotes is included verbatim in the spelling. 26830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith memcpy(Spelling + Length, BufPtr, RawLength); 26930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Length += RawLength; 27030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith BufPtr += RawLength; 27130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 27230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // The rest of the token is lexed normally. 27330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith } 27430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith } 27530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 27630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith while (BufPtr < BufEnd) { 27730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith unsigned Size; 27830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Spelling[Length++] = Lexer::getCharAndSizeNoWarn(BufPtr, Size, LangOpts); 27930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith BufPtr += Size; 28030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith } 28130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 28230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith assert(Length < Tok.getLength() && 28330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith "NeedsCleaning flag set on token that didn't need cleaning!"); 28430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith return Length; 28530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith} 28630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 287b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// getSpelling() - Return the 'spelling' of this token. The spelling of a 288b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// token are the characters used to represent the token in the source file 289b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// after trigraph expansion and escaped-newline folding. In particular, this 290b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// wants to get the true, uncanonicalized, spelling of things like digraphs 291b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// UCNs, etc. 2925f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerStringRef Lexer::getSpelling(SourceLocation loc, 29330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith SmallVectorImpl<char> &buffer, 29430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const SourceManager &SM, 29530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const LangOptions &options, 29630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith bool *invalid) { 297834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Break down the source location. 298834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc); 299834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 300834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Try to the load the file buffer. 301834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall bool invalidTemp = false; 3025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); 303834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall if (invalidTemp) { 304834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall if (invalid) *invalid = true; 3055f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return StringRef(); 306834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall } 307834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 308834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall const char *tokenBegin = file.data() + locInfo.second; 309834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 310834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Lex from the start of the given location. 311834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall Lexer lexer(SM.getLocForStartOfFile(locInfo.first), options, 312834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall file.begin(), tokenBegin, file.end()); 313834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall Token token; 314834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall lexer.LexFromRawLexer(token); 315834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 316834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall unsigned length = token.getLength(); 317834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 318834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Common case: no need for cleaning. 319834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall if (!token.needsCleaning()) 3205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return StringRef(tokenBegin, length); 321834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 32230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Hard case, we need to relex the characters into the string. 32330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith buffer.resize(length); 32430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith buffer.resize(getSpellingSlow(token, tokenBegin, options, buffer.data())); 3255f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return StringRef(buffer.data(), buffer.size()); 326834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall} 327834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 328834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// getSpelling() - Return the 'spelling' of this token. The spelling of a 329834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// token are the characters used to represent the token in the source file 330834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// after trigraph expansion and escaped-newline folding. In particular, this 331834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// wants to get the true, uncanonicalized, spelling of things like digraphs 332834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// UCNs, etc. 333b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattnerstd::string Lexer::getSpelling(const Token &Tok, const SourceManager &SourceMgr, 3344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts, bool *Invalid) { 335b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); 33630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 337b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner bool CharDataInvalid = false; 33830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation(), 339b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner &CharDataInvalid); 340b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (Invalid) 341b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner *Invalid = CharDataInvalid; 342b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (CharDataInvalid) 343b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return std::string(); 34430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 34530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // If this token contains nothing interesting, return it directly. 346b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (!Tok.needsCleaning()) 34730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith return std::string(TokStart, TokStart + Tok.getLength()); 34830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 349b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner std::string Result; 35030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Result.resize(Tok.getLength()); 35130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Result.resize(getSpellingSlow(Tok, TokStart, LangOpts, &*Result.begin())); 352b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return Result; 353b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner} 354b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 355b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// getSpelling - This method is used to get the spelling of a token into a 356b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// preallocated buffer, instead of as an std::string. The caller is required 357b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// to allocate enough space for the token, which is guaranteed to be at least 358b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// Tok.getLength() bytes long. The actual length of the token is returned. 359b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// 360b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// Note that this method may do two possible things: it may either fill in 361b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// the buffer specified with characters, or it may *change the input pointer* 362b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// to point to a constant buffer with the data already in it (avoiding a 363b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// copy). The caller is not allowed to modify the returned buffer pointer 364b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// if an internal buffer is returned. 365b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattnerunsigned Lexer::getSpelling(const Token &Tok, const char *&Buffer, 366b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner const SourceManager &SourceMgr, 3674e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts, bool *Invalid) { 368b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); 369c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 370c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara const char *TokStart = 0; 371c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // NOTE: this has to be checked *before* testing for an IdentifierInfo. 372c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara if (Tok.is(tok::raw_identifier)) 373c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara TokStart = Tok.getRawIdentifierData(); 374c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara else if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { 375c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // Just return the string from the identifier table, which is very quick. 376b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Buffer = II->getNameStart(); 377b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return II->getLength(); 378b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 379c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 380c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // NOTE: this can be checked even after testing for an IdentifierInfo. 381b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (Tok.isLiteral()) 382b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner TokStart = Tok.getLiteralData(); 383c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 384b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (TokStart == 0) { 385c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // Compute the start of the token in the input lexer buffer. 386b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner bool CharDataInvalid = false; 387b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner TokStart = SourceMgr.getCharacterData(Tok.getLocation(), &CharDataInvalid); 388b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (Invalid) 389b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner *Invalid = CharDataInvalid; 390b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (CharDataInvalid) { 391b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Buffer = ""; 392b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return 0; 393b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 394b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 395c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 396b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner // If this token contains nothing interesting, return it directly. 397b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (!Tok.needsCleaning()) { 398b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Buffer = TokStart; 399b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return Tok.getLength(); 400b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 401c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 402b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner // Otherwise, hard case, relex the characters into the string. 40330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith return getSpellingSlow(Tok, TokStart, LangOpts, const_cast<char*>(Buffer)); 404b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner} 405b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 406b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 407b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 40833e9abd21083a0191a7676a04b497006d2da184dDouglas Gregorstatic bool isWhitespace(unsigned char c); 4095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4109a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// MeasureTokenLength - Relex the token at the specified location and return 4119a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// its length in bytes in the input file. If the token needs cleaning (e.g. 4129a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// includes a trigraph or an escaped newline) then this count includes bytes 4139a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// that are part of that. 4149a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattnerunsigned Lexer::MeasureTokenLength(SourceLocation Loc, 4152c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner const SourceManager &SM, 4162c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner const LangOptions &LangOpts) { 4179a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // TODO: this could be special cased for common tokens like identifiers, ')', 4189a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // etc to make this faster, if it mattered. Just look at StrData[0] to handle 4191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // all obviously single-char tokens. This could use 4209a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // Lexer::isObviouslySimpleCharacter for example to handle identifiers or 4219a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // something. 422de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 423de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // If this comes from a macro expansion, we really do want the macro name, not 424de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // the token this macro expanded to. 425402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth Loc = SM.getExpansionLoc(Loc); 426363fdc29656cc03c1817268888f95e6343470aa8Chris Lattner std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 427f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor bool Invalid = false; 4285f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); 429f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor if (Invalid) 430aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregor return 0; 431f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer 432f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer const char *StrData = Buffer.data()+LocInfo.second; 4338350394c65b81bba3986dfe44ae17423873741deChris Lattner 43433e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor if (isWhitespace(StrData[0])) 43533e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor return 0; 43633e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor 4379a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // Create a lexer starting at the beginning of this token. 438c3526d89ef9c31639ec8b25180cfb22354344241Sebastian Redl Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, 439c3526d89ef9c31639ec8b25180cfb22354344241Sebastian Redl Buffer.begin(), StrData, Buffer.end()); 44039de7409bffb6b725a8aa64f0ba77ab51e8c9eb3Chris Lattner TheLexer.SetCommentRetentionState(true); 4419a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner Token TheTok; 442590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner TheLexer.LexFromRawLexer(TheTok); 4439a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner return TheTok.getLength(); 4449a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner} 4459a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner 4460e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidisstatic SourceLocation getBeginningOfFileToken(SourceLocation Loc, 4470e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis const SourceManager &SM, 4480e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis const LangOptions &LangOpts) { 4490e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis assert(Loc.isFileID()); 450a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 4513de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor if (LocInfo.first.isInvalid()) 4523de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor return Loc; 4533de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor 454a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor bool Invalid = false; 4555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); 456a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (Invalid) 457a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return Loc; 458a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 459a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Back up from the current location until we hit the beginning of a line 460a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // (or the buffer). We'll relex from that point. 461a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor const char *BufStart = Buffer.data(); 4623de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor if (LocInfo.second >= Buffer.size()) 4633de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor return Loc; 4643de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor 465a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor const char *StrData = BufStart+LocInfo.second; 466a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (StrData[0] == '\n' || StrData[0] == '\r') 467a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return Loc; 468a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 469a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor const char *LexStart = StrData; 470a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor while (LexStart != BufStart) { 471a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (LexStart[0] == '\n' || LexStart[0] == '\r') { 472a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor ++LexStart; 473a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor break; 474a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } 475a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 476a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor --LexStart; 477a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } 478a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 479a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Create a lexer starting at the beginning of this token. 480a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis SourceLocation LexerStartLoc = Loc.getLocWithOffset(-LocInfo.second); 481a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor Lexer TheLexer(LexerStartLoc, LangOpts, BufStart, LexStart, Buffer.end()); 482a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor TheLexer.SetCommentRetentionState(true); 483a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 484a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Lex tokens until we find the token that contains the source location. 485a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor Token TheTok; 486a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor do { 487a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor TheLexer.LexFromRawLexer(TheTok); 488a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 489a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (TheLexer.getBufferLocation() > StrData) { 490a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Lexing this token has taken the lexer past the source location we're 491a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // looking for. If the current token encompasses our source location, 492a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // return the beginning of that token. 493a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (TheLexer.getBufferLocation() - TheTok.getLength() <= StrData) 494a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return TheTok.getLocation(); 495a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 496a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // We ended up skipping over the source location entirely, which means 497a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // that it points into whitespace. We're done here. 498a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor break; 499a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } 500a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } while (TheTok.getKind() != tok::eof); 501a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 502a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // We've passed our source location; just return the original source location. 503a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return Loc; 504a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor} 505a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 5060e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios KyrtzidisSourceLocation Lexer::GetBeginningOfToken(SourceLocation Loc, 5070e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis const SourceManager &SM, 5080e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis const LangOptions &LangOpts) { 5090e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis if (Loc.isFileID()) 5100e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis return getBeginningOfFileToken(Loc, SM, LangOpts); 5110e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis 5120e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis if (!SM.isMacroArgExpansion(Loc)) 5130e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis return Loc; 5140e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis 5150e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis SourceLocation FileLoc = SM.getSpellingLoc(Loc); 5160e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis SourceLocation BeginFileLoc = getBeginningOfFileToken(FileLoc, SM, LangOpts); 5170e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis std::pair<FileID, unsigned> FileLocInfo = SM.getDecomposedLoc(FileLoc); 518ae9f85b2c062ac20512a484cfa6e66239dd3d0d9Chandler Carruth std::pair<FileID, unsigned> BeginFileLocInfo 519ae9f85b2c062ac20512a484cfa6e66239dd3d0d9Chandler Carruth = SM.getDecomposedLoc(BeginFileLoc); 5200e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis assert(FileLocInfo.first == BeginFileLocInfo.first && 5210e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis FileLocInfo.second >= BeginFileLocInfo.second); 522ae9f85b2c062ac20512a484cfa6e66239dd3d0d9Chandler Carruth return Loc.getLocWithOffset(BeginFileLocInfo.second - FileLocInfo.second); 5230e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis} 5240e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis 525f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregornamespace { 526f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor enum PreambleDirectiveKind { 527f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_Skipped, 528f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_StartIf, 529f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_EndIf, 530f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_Unknown 531f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor }; 532f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor} 533f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 534f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregorstd::pair<unsigned, bool> 53503c107a42fae79e89d0016999a1a04c07d65591aArgyrios KyrtzidisLexer::ComputePreamble(const llvm::MemoryBuffer *Buffer, 5364e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts, unsigned MaxLines) { 537f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // Create a lexer starting at the beginning of the file. Note that we use a 538f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // "fake" file source location at offset 1 so that the lexer will track our 539f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // position within the file. 540f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor const unsigned StartOffset = 1; 5411cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis SourceLocation FileLoc = SourceLocation::getFromRawEncoding(StartOffset); 5421cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis Lexer TheLexer(FileLoc, LangOpts, Buffer->getBufferStart(), 543f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Buffer->getBufferStart(), Buffer->getBufferEnd()); 5441cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis 5451cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis // StartLoc will differ from FileLoc if there is a BOM that was skipped. 5461cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis SourceLocation StartLoc = TheLexer.getSourceLocation(); 5471cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis 548f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor bool InPreprocessorDirective = false; 549f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Token TheTok; 550f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Token IfStartTok; 551f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor unsigned IfCount = 0; 552c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis 553c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis unsigned MaxLineOffset = 0; 554c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (MaxLines) { 555c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis const char *CurPtr = Buffer->getBufferStart(); 556c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis unsigned CurLine = 0; 557c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis while (CurPtr != Buffer->getBufferEnd()) { 558c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis char ch = *CurPtr++; 559c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (ch == '\n') { 560c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis ++CurLine; 561c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (CurLine == MaxLines) 562c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis break; 563c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis } 564c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis } 565c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (CurPtr != Buffer->getBufferEnd()) 566c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis MaxLineOffset = CurPtr - Buffer->getBufferStart(); 567c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis } 568df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor 569f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor do { 570f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor TheLexer.LexFromRawLexer(TheTok); 571f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 572f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (InPreprocessorDirective) { 573f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // If we've hit the end of the file, we're done. 574f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (TheTok.getKind() == tok::eof) { 575f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 576f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 577f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 578f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // If we haven't hit the end of the preprocessor directive, skip this 579f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // token. 580f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (!TheTok.isAtStartOfLine()) 581f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 582f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 583f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // We've passed the end of the preprocessor directive, and will look 584f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // at this token again below. 585f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor InPreprocessorDirective = false; 586f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 587f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 588df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // Keep track of the # of lines in the preamble. 589df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor if (TheTok.isAtStartOfLine()) { 590c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis unsigned TokOffset = TheTok.getLocation().getRawEncoding() - StartOffset; 591df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor 592df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // If we were asked to limit the number of lines in the preamble, 593df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // and we're about to exceed that limit, we're done. 594c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (MaxLineOffset && TokOffset >= MaxLineOffset) 595df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor break; 596df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor } 597df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor 598f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // Comments are okay; skip over them. 599f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (TheTok.getKind() == tok::comment) 600f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 601f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 602f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) { 603f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // This is the start of a preprocessor directive. 604f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Token HashTok = TheTok; 605f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor InPreprocessorDirective = true; 606f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 60719207f1e5f51261a33492602501fb7ada50ea546Joerg Sonnenberger // Figure out which directive this is. Since we're lexing raw tokens, 608f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // we don't have an identifier table available. Instead, just look at 609f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // the raw identifier to recognize and categorize preprocessor directives. 610f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor TheLexer.LexFromRawLexer(TheTok); 611c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara if (TheTok.getKind() == tok::raw_identifier && !TheTok.needsCleaning()) { 6125f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Keyword(TheTok.getRawIdentifierData(), 613c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara TheTok.getLength()); 614f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PreambleDirectiveKind PDK 615f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor = llvm::StringSwitch<PreambleDirectiveKind>(Keyword) 616f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("include", PDK_Skipped) 617f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("__include_macros", PDK_Skipped) 618f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("define", PDK_Skipped) 619f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("undef", PDK_Skipped) 620f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("line", PDK_Skipped) 621f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("error", PDK_Skipped) 622f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("pragma", PDK_Skipped) 623f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("import", PDK_Skipped) 624f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("include_next", PDK_Skipped) 625f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("warning", PDK_Skipped) 626f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("ident", PDK_Skipped) 627f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("sccs", PDK_Skipped) 628f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("assert", PDK_Skipped) 629f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("unassert", PDK_Skipped) 630f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("if", PDK_StartIf) 631f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("ifdef", PDK_StartIf) 632f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("ifndef", PDK_StartIf) 633f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("elif", PDK_Skipped) 634f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("else", PDK_Skipped) 635f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("endif", PDK_EndIf) 636f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Default(PDK_Unknown); 637f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 638f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor switch (PDK) { 639f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_Skipped: 640f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 641f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 642f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_StartIf: 643f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (IfCount == 0) 644f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor IfStartTok = HashTok; 645f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 646f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor ++IfCount; 647f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 648f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 649f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_EndIf: 650f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // Mismatched #endif. The preamble ends here. 651f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (IfCount == 0) 652f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 653f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 654f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor --IfCount; 655f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 656f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 657f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_Unknown: 658f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // We don't know what this directive is; stop at the '#'. 659f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 660f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 661f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 662f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 663f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // We only end up here if we didn't recognize the preprocessor 664f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // directive or it was one that can't occur in the preamble at this 665f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // point. Roll back the current token to the location of the '#'. 666f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor InPreprocessorDirective = false; 667f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor TheTok = HashTok; 668f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 669f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 670df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // We hit a token that we don't recognize as being in the 671df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // "preprocessing only" part of the file, so we're no longer in 672df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // the preamble. 673f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 674f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } while (true); 675f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 676f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation(); 677f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(), 678f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor IfCount? IfStartTok.isAtStartOfLine() 679f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor : TheTok.isAtStartOfLine()); 680f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor} 681f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 6827ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 6837ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// AdvanceToTokenCharacter - Given a location that specifies the start of a 6847ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token, return a new location that specifies a character within the token. 6857ef5c27eb6e8ebe58b52013246c06753c3613263Chris LattnerSourceLocation Lexer::AdvanceToTokenCharacter(SourceLocation TokStart, 6867ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner unsigned CharNo, 6877ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner const SourceManager &SM, 6884e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts) { 689433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth // Figure out how many physical characters away the specified expansion 6907ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // character is. This needs to take into consideration newlines and 6917ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // trigraphs. 6927ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner bool Invalid = false; 6937ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner const char *TokPtr = SM.getCharacterData(TokStart, &Invalid); 6947ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 6957ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // If they request the first char of the token, we're trivially done. 6967ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (Invalid || (CharNo == 0 && Lexer::isObviouslySimpleCharacter(*TokPtr))) 6977ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner return TokStart; 6987ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 6997ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner unsigned PhysOffset = 0; 7007ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7017ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // The usual case is that tokens don't contain anything interesting. Skip 7027ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // over the uninteresting characters. If a token only consists of simple 7037ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // chars, this method is extremely fast. 7047ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner while (Lexer::isObviouslySimpleCharacter(*TokPtr)) { 7057ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (CharNo == 0) 706a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return TokStart.getLocWithOffset(PhysOffset); 7077ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner ++TokPtr, --CharNo, ++PhysOffset; 7087ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner } 7097ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7107ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // If we have a character that may be a trigraph or escaped newline, use a 7117ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // lexer to parse it correctly. 7127ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner for (; CharNo; --CharNo) { 7137ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner unsigned Size; 7144e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie Lexer::getCharAndSizeNoWarn(TokPtr, Size, LangOpts); 7157ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner TokPtr += Size; 7167ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner PhysOffset += Size; 7177ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner } 7187ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7197ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // Final detail: if we end up on an escaped newline, we want to return the 7207ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // location of the actual byte of the token. For example foo\<newline>bar 7217ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // advanced by 3 should return the location of b, not of \\. One compounding 7227ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // detail of this is that the escape may be made by a trigraph. 7237ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (!Lexer::isObviouslySimpleCharacter(*TokPtr)) 7247ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner PhysOffset += Lexer::SkipEscapedNewLines(TokPtr)-TokPtr; 7257ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 726a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return TokStart.getLocWithOffset(PhysOffset); 7277ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner} 7287ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7297ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// \brief Computes the source location just past the end of the 7307ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token at this source location. 7317ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// 7327ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// This routine can be used to produce a source location that 7337ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// points just past the end of the token referenced by \p Loc, and 7347ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// is generally used when a diagnostic needs to point just after a 7357ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token where it expected something different that it received. If 7367ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// the returned source location would not be meaningful (e.g., if 7377ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// it points into a macro), this routine returns an invalid 7387ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// source location. 7397ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// 7407ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// \param Offset an offset from the end of the token, where the source 7417ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// location should refer to. The default offset (0) produces a source 7427ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// location pointing just past the end of the token; an offset of 1 produces 7437ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// a source location pointing to the last character in the token, etc. 7447ef5c27eb6e8ebe58b52013246c06753c3613263Chris LattnerSourceLocation Lexer::getLocForEndOfToken(SourceLocation Loc, unsigned Offset, 7457ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner const SourceManager &SM, 7464e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts) { 7477ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis if (Loc.isInvalid()) 7487ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner return SourceLocation(); 7497ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis 7507ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis if (Loc.isMacroID()) { 7514e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Offset > 0 || !isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc)) 752433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth return SourceLocation(); // Points inside the macro expansion. 7537ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis } 7547ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis 7554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie unsigned Len = Lexer::MeasureTokenLength(Loc, SM, LangOpts); 7567ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (Len > Offset) 7577ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner Len = Len - Offset; 7587ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner else 7597ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner return Loc; 7607ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 761a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return Loc.getLocWithOffset(Len); 7627ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner} 7637ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7647a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis/// \brief Returns true if the given MacroID location points at the first 765433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// token of the macro expansion. 766433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruthbool Lexer::isAtStartOfMacroExpansion(SourceLocation loc, 767f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor const SourceManager &SM, 76869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis const LangOptions &LangOpts, 76969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis SourceLocation *MacroBegin) { 7707a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); 7717a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 7727a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis std::pair<FileID, unsigned> infoLoc = SM.getDecomposedLoc(loc); 7737a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // FIXME: If the token comes from the macro token paste operator ('##') 7747a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // this function will always return false; 7757a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis if (infoLoc.second > 0) 7767a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis return false; // Does not point at the start of token. 7777a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 778433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation expansionLoc = 7791728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth SM.getSLocEntry(infoLoc.first).getExpansion().getExpansionLocStart(); 78069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (expansionLoc.isFileID()) { 78169bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis // No other macro expansions, this is the first. 78269bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (MacroBegin) 78369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis *MacroBegin = expansionLoc; 78469bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis return true; 78569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis } 7867a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 78769bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis return isAtStartOfMacroExpansion(expansionLoc, SM, LangOpts, MacroBegin); 7887a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis} 7897a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 7907a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis/// \brief Returns true if the given MacroID location points at the last 791433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// token of the macro expansion. 792433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruthbool Lexer::isAtEndOfMacroExpansion(SourceLocation loc, 79369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis const SourceManager &SM, 79469bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis const LangOptions &LangOpts, 79569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis SourceLocation *MacroEnd) { 7967a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); 7977a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 7987a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis SourceLocation spellLoc = SM.getSpellingLoc(loc); 7997a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis unsigned tokLen = MeasureTokenLength(spellLoc, SM, LangOpts); 8007a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis if (tokLen == 0) 8017a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis return false; 8027a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 8037a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis FileID FID = SM.getFileID(loc); 804a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis SourceLocation afterLoc = loc.getLocWithOffset(tokLen+1); 805f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis if (SM.isInFileID(afterLoc, FID)) 806f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis return false; // Still in the same FileID, does not point to the last token. 8077a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 8087a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // FIXME: If the token comes from the macro token paste operator ('##') 8097a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // or the stringify operator ('#') this function will always return false; 810f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis 811433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation expansionLoc = 8121728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth SM.getSLocEntry(FID).getExpansion().getExpansionLocEnd(); 81369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (expansionLoc.isFileID()) { 81469bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis // No other macro expansions. 81569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (MacroEnd) 81669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis *MacroEnd = expansionLoc; 81769bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis return true; 81869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis } 8197a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 82069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis return isAtEndOfMacroExpansion(expansionLoc, SM, LangOpts, MacroEnd); 8217a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis} 8227a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 823a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidisstatic CharSourceRange makeRangeFromFileLocs(CharSourceRange Range, 824d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis const SourceManager &SM, 825d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis const LangOptions &LangOpts) { 826a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis SourceLocation Begin = Range.getBegin(); 827a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis SourceLocation End = Range.getEnd(); 828d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis assert(Begin.isFileID() && End.isFileID()); 829a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis if (Range.isTokenRange()) { 830a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis End = Lexer::getLocForEndOfToken(End, 0, SM,LangOpts); 831a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis if (End.isInvalid()) 832a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return CharSourceRange(); 833a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis } 834d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 835d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis // Break down the source locations. 836d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis FileID FID; 837d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis unsigned BeginOffs; 838d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis llvm::tie(FID, BeginOffs) = SM.getDecomposedLoc(Begin); 839d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (FID.isInvalid()) 840d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange(); 841d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 842d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis unsigned EndOffs; 843d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (!SM.isInFileID(End, FID, &EndOffs) || 844d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis BeginOffs > EndOffs) 845d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange(); 846d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 847d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange::getCharRange(Begin, End); 848d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis} 849d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 850a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios KyrtzidisCharSourceRange Lexer::makeFileCharRange(CharSourceRange Range, 85111b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis const SourceManager &SM, 85211b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis const LangOptions &LangOpts) { 853a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis SourceLocation Begin = Range.getBegin(); 854a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis SourceLocation End = Range.getEnd(); 855d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Begin.isInvalid() || End.isInvalid()) 85611b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis return CharSourceRange(); 85711b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 858d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Begin.isFileID() && End.isFileID()) 859a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 860d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 861d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Begin.isMacroID() && End.isFileID()) { 86211b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis if (!isAtStartOfMacroExpansion(Begin, SM, LangOpts, &Begin)) 86311b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis return CharSourceRange(); 864a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setBegin(Begin); 865a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 866d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis } 86711b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 868d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Begin.isFileID() && End.isMacroID()) { 869a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis if ((Range.isTokenRange() && !isAtEndOfMacroExpansion(End, SM, LangOpts, 870a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis &End)) || 871a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis (Range.isCharRange() && !isAtStartOfMacroExpansion(End, SM, LangOpts, 872a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis &End))) 873d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange(); 874a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setEnd(End); 875a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 876d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis } 87711b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 878d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis assert(Begin.isMacroID() && End.isMacroID()); 879d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis SourceLocation MacroBegin, MacroEnd; 880d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (isAtStartOfMacroExpansion(Begin, SM, LangOpts, &MacroBegin) && 881a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis ((Range.isTokenRange() && isAtEndOfMacroExpansion(End, SM, LangOpts, 882a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis &MacroEnd)) || 883a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis (Range.isCharRange() && isAtStartOfMacroExpansion(End, SM, LangOpts, 884a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis &MacroEnd)))) { 885a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setBegin(MacroBegin); 886a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setEnd(MacroEnd); 887a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 888a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis } 889d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 890d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis FileID FID; 891d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis unsigned BeginOffs; 892d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis llvm::tie(FID, BeginOffs) = SM.getDecomposedLoc(Begin); 893d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (FID.isInvalid()) 894e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return CharSourceRange(); 895e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 89611b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis unsigned EndOffs; 897d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (!SM.isInFileID(End, FID, &EndOffs) || 898d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis BeginOffs > EndOffs) 89911b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis return CharSourceRange(); 90011b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 901d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis const SrcMgr::SLocEntry *E = &SM.getSLocEntry(FID); 902d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis const SrcMgr::ExpansionInfo &Expansion = E->getExpansion(); 903d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Expansion.isMacroArgExpansion() && 904d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis Expansion.getSpellingLoc().isFileID()) { 905d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis SourceLocation SpellLoc = Expansion.getSpellingLoc(); 906a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setBegin(SpellLoc.getLocWithOffset(BeginOffs)); 907a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setEnd(SpellLoc.getLocWithOffset(EndOffs)); 908a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 909d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis } 910d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 911d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange(); 91211b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis} 91311b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 914e64d9037658a1b95c79ea275af6167a110b3c563Argyrios KyrtzidisStringRef Lexer::getSourceText(CharSourceRange Range, 915e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis const SourceManager &SM, 916e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis const LangOptions &LangOpts, 917e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis bool *Invalid) { 918a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range = makeFileCharRange(Range, SM, LangOpts); 919a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis if (Range.isInvalid()) { 920e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = true; 921e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return StringRef(); 922e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis } 923e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 924e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis // Break down the source location. 925e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis std::pair<FileID, unsigned> beginInfo = SM.getDecomposedLoc(Range.getBegin()); 926e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (beginInfo.first.isInvalid()) { 927e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = true; 928e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return StringRef(); 929e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis } 930e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 931e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis unsigned EndOffs; 932e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (!SM.isInFileID(Range.getEnd(), beginInfo.first, &EndOffs) || 933e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis beginInfo.second > EndOffs) { 934e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = true; 935e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return StringRef(); 936e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis } 937e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 938e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis // Try to the load the file buffer. 939e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis bool invalidTemp = false; 940e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis StringRef file = SM.getBufferData(beginInfo.first, &invalidTemp); 941e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (invalidTemp) { 942e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = true; 943e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return StringRef(); 944e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis } 945e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 946e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = false; 947e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return file.substr(beginInfo.second, EndOffs - beginInfo.second); 948e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis} 949e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 950c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna ZaksStringRef Lexer::getImmediateMacroName(SourceLocation Loc, 951c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks const SourceManager &SM, 952c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks const LangOptions &LangOpts) { 953c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks assert(Loc.isMacroID() && "Only reasonble to call this on macros"); 9547f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9557f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // Find the location of the immediate macro expansion. 9567f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis while (1) { 9577f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis FileID FID = SM.getFileID(Loc); 9587f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis const SrcMgr::SLocEntry *E = &SM.getSLocEntry(FID); 9597f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis const SrcMgr::ExpansionInfo &Expansion = E->getExpansion(); 9607f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis Loc = Expansion.getExpansionLocStart(); 9617f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis if (!Expansion.isMacroArgExpansion()) 9627f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis break; 9637f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9647f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // For macro arguments we need to check that the argument did not come 9657f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // from an inner macro, e.g: "MAC1( MAC2(foo) )" 9667f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9677f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // Loc points to the argument id of the macro definition, move to the 9687f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // macro expansion. 969c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks Loc = SM.getImmediateExpansionRange(Loc).first; 9707f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis SourceLocation SpellLoc = Expansion.getSpellingLoc(); 9717f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis if (SpellLoc.isFileID()) 9727f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis break; // No inner macro. 9737f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9747f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // If spelling location resides in the same FileID as macro expansion 9757f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // location, it means there is no inner macro. 9767f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis FileID MacroFID = SM.getFileID(Loc); 9777f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis if (SM.isInFileID(SpellLoc, MacroFID)) 9787f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis break; 9797f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9807f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // Argument came from inner macro. 9817f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis Loc = SpellLoc; 9827f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis } 983c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks 984c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // Find the spelling location of the start of the non-argument expansion 985c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // range. This is where the macro name was spelled in order to begin 986c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // expanding this macro. 9877f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis Loc = SM.getSpellingLoc(Loc); 988c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks 989c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // Dig out the buffer where the macro name was spelled and the extents of the 990c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // name so that we can render it into the expansion note. 991c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks std::pair<FileID, unsigned> ExpansionInfo = SM.getDecomposedLoc(Loc); 992c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks unsigned MacroTokenLength = Lexer::MeasureTokenLength(Loc, SM, LangOpts); 993c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first); 994c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks return ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength); 995c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks} 996c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks 9975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 9985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Character information. 9995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 10005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum { 10025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0' 10035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_VERT_WS = 0x02, // '\r', '\n' 10045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_LETTER = 0x04, // a-z,A-Z 10055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_NUMBER = 0x08, // 0-9 10065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_UNDER = 0x10, // _ 10072fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CHAR_PERIOD = 0x20, // . 10082fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CHAR_RAWDEL = 0x40 // {}[]#<>%:;?*+-/^&|~!=,"' 10095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}; 10105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 101103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// Statically initialize CharInfo table based on ASCII character set 101203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// Reference: FreeBSD 7.2 /usr/share/misc/ascii 1013a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic const unsigned char CharInfo[256] = 101403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner{ 101503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 0 NUL 1 SOH 2 STX 3 ETX 101603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 4 EOT 5 ENQ 6 ACK 7 BEL 101703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 101803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 101903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 8 BS 9 HT 10 NL 11 VT 102003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//12 NP 13 CR 14 SO 15 SI 102103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS, 102203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 , 102303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//16 DLE 17 DC1 18 DC2 19 DC3 102403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//20 DC4 21 NAK 22 SYN 23 ETB 102503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 102603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 102703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//24 CAN 25 EM 26 SUB 27 ESC 102803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//28 FS 29 GS 30 RS 31 US 102903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 103003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 103103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//32 SP 33 ! 34 " 35 # 103203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//36 $ 37 % 38 & 39 ' 10332fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CHAR_HORZ_WS, CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , 10342fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 0 , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , 103503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//40 ( 41 ) 42 * 43 + 103603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//44 , 45 - 46 . 47 / 10372fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 0 , 0 , CHAR_RAWDEL , CHAR_RAWDEL , 10382fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CHAR_RAWDEL , CHAR_RAWDEL , CHAR_PERIOD , CHAR_RAWDEL , 103903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//48 0 49 1 50 2 51 3 104003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//52 4 53 5 54 6 55 7 104103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , 104203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , 104303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//56 8 57 9 58 : 59 ; 104403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//60 < 61 = 62 > 63 ? 10452fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CHAR_NUMBER , CHAR_NUMBER , CHAR_RAWDEL , CHAR_RAWDEL , 10462fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , 104703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//64 @ 65 A 66 B 67 C 104803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//68 D 69 E 70 F 71 G 104903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 105003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 105103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//72 H 73 I 74 J 75 K 105203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//76 L 77 M 78 N 79 O 105303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 105403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 105503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//80 P 81 Q 82 R 83 S 105603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//84 T 85 U 86 V 87 W 105703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 105803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 105903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//88 X 89 Y 90 Z 91 [ 106003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//92 \ 93 ] 94 ^ 95 _ 10612fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_RAWDEL , 10622fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 0 , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_UNDER , 106303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//96 ` 97 a 98 b 99 c 106403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//100 d 101 e 102 f 103 g 106503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 106603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 106703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//104 h 105 i 106 j 107 k 106803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//108 l 109 m 110 n 111 o 106903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 107003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 107103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//112 p 113 q 114 r 115 s 107203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//116 t 117 u 118 v 119 w 107303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 107403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 107503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//120 x 121 y 122 z 123 { 10762fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper//124 | 125 } 126 ~ 127 DEL 10772fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_RAWDEL , 10782fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , 0 107903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner}; 108003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 1081a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic void InitCharacterInfo() { 10825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer static bool isInited = false; 10835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isInited) return; 108403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner // check the statically-initialized CharInfo table 108503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_HORZ_WS == CharInfo[(int)' ']); 108603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_HORZ_WS == CharInfo[(int)'\t']); 108703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_HORZ_WS == CharInfo[(int)'\f']); 108803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_HORZ_WS == CharInfo[(int)'\v']); 108903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_VERT_WS == CharInfo[(int)'\n']); 109003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_VERT_WS == CharInfo[(int)'\r']); 109103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_UNDER == CharInfo[(int)'_']); 109203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_PERIOD == CharInfo[(int)'.']); 109303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner for (unsigned i = 'a'; i <= 'z'; ++i) { 109403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_LETTER == CharInfo[i]); 109503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_LETTER == CharInfo[i+'A'-'a']); 109603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner } 10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = '0'; i <= '9'; ++i) 109803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_NUMBER == CharInfo[i]); 10997b682656310b0d7e94a98d982444bc7ddedc653fSteve Naroff 110003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner isInited = true; 11015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 110303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 11045cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith/// isIdentifierHead - Return true if this is the first character of an 11055cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith/// identifier, which is [a-zA-Z_]. 11065cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smithstatic inline bool isIdentifierHead(unsigned char c) { 11075cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith return (CharInfo[c] & (CHAR_LETTER|CHAR_UNDER)) ? true : false; 11085cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith} 11095cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIdentifierBody - Return true if this is the body character of an 11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// identifier, which is [a-zA-Z0-9_]. 11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isIdentifierBody(unsigned char c) { 111395c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false; 11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isHorizontalWhitespace - Return true if this character is horizontal 1117a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// whitespace: ' ', '\\t', '\\f', '\\v'. Note that this returns false for 1118a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// '\\0'. 11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isHorizontalWhitespace(unsigned char c) { 112095c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser return (CharInfo[c] & CHAR_HORZ_WS) ? true : false; 11215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1123aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// isVerticalWhitespace - Return true if this character is vertical 1124a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// whitespace: '\\n', '\\r'. Note that this returns false for '\\0'. 1125aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaksstatic inline bool isVerticalWhitespace(unsigned char c) { 1126aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks return (CharInfo[c] & CHAR_VERT_WS) ? true : false; 1127aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks} 1128aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isWhitespace - Return true if this character is horizontal or vertical 1130a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// whitespace: ' ', '\\t', '\\f', '\\v', '\\n', '\\r'. Note that this returns 1131a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// false for '\\0'. 11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isWhitespace(unsigned char c) { 113395c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false; 11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNumberBody - Return true if this is the body character of an 11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// preprocessing number, which is [a-zA-Z0-9_.]. 11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isNumberBody(unsigned char c) { 11391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ? 114095c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser true : false; 11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11432fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// isRawStringDelimBody - Return true if this is the body character of a 11442fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// raw string delimiter. 11452fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topperstatic inline bool isRawStringDelimBody(unsigned char c) { 11462fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return (CharInfo[c] & 11472fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD|CHAR_RAWDEL)) ? 11482fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper true : false; 11492fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper} 11502fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 1151d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose// Allow external clients to make use of CharInfo. 1152d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rosebool Lexer::isIdentifierBodyChar(char c, const LangOptions &LangOpts) { 1153d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose return isIdentifierBody(c) || (c == '$' && LangOpts.DollarIdents); 1154d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose} 1155d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose 11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 11585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Diagnostics forwarding code. 11595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 11605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1161409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the 1162433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// lexer buffer was all expanded at a single point, perform the mapping. 1163409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// This is currently only used for _Pragma implementation, so it is the slow 1164409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// path of the hot getSourceLocation method. Do not allow it to be inlined. 116514bd96571ef6f0e97dc79ec4d01b547d60e8fa68Chandler Carruthstatic LLVM_ATTRIBUTE_NOINLINE SourceLocation GetMappedTokenLoc( 116614bd96571ef6f0e97dc79ec4d01b547d60e8fa68Chandler Carruth Preprocessor &PP, SourceLocation FileLoc, unsigned CharNo, unsigned TokLen); 1167409a03671224d4e5bdab1594c43baf070148f830Chris Lattnerstatic SourceLocation GetMappedTokenLoc(Preprocessor &PP, 1168409a03671224d4e5bdab1594c43baf070148f830Chris Lattner SourceLocation FileLoc, 1169de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned CharNo, unsigned TokLen) { 1170433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth assert(FileLoc.isMacroID() && "Must be a macro expansion"); 11711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1172409a03671224d4e5bdab1594c43baf070148f830Chris Lattner // Otherwise, we're lexing "mapped tokens". This is used for things like 1173433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth // _Pragma handling. Combine the expansion location of FileLoc with the 1174df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner // spelling location. 1175e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner SourceManager &SM = PP.getSourceManager(); 11761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1177433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth // Create a new SLoc which is expanded from Expansion(FileLoc) but whose 1178df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner // characters come from spelling(FileLoc)+Offset. 1179e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc); 1180a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis SpellingLoc = SpellingLoc.getLocWithOffset(CharNo); 11811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1182e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner // Figure out the expansion loc range, which is the range covered by the 1183e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner // original _Pragma(...) sequence. 1184e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner std::pair<SourceLocation,SourceLocation> II = 1185999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth SM.getImmediateExpansionRange(FileLoc); 11861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1187bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth return SM.createExpansionLoc(SpellingLoc, II.first, II.second, TokLen); 1188409a03671224d4e5bdab1594c43baf070148f830Chris Lattner} 1189409a03671224d4e5bdab1594c43baf070148f830Chris Lattner 11905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSourceLocation - Return a source location identifier for the specified 11915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// offset in the current file. 1192de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris LattnerSourceLocation Lexer::getSourceLocation(const char *Loc, 1193de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned TokLen) const { 1194448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner assert(Loc >= BufferStart && Loc <= BufferEnd && 11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer "Location out of range for this buffer!"); 11969dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner 11979dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner // In the normal case, we're just lexing from a simple file buffer, return 11989dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner // the file id from FileLoc with the offset specified. 1199448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner unsigned CharNo = Loc-BufferStart; 12009dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner if (FileLoc.isFileID()) 1201a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return FileLoc.getLocWithOffset(CharNo); 12021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12032b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner // Otherwise, this is the _Pragma lexer case, which pretends that all of the 12042b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner // tokens are lexed from where the _Pragma was defined. 1205168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner assert(PP && "This doesn't work on raw lexers"); 1206de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen); 12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 12085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Diag - Forwarding function for diagnostics. This translate a source 12105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// position in the current buffer into a SourceLocation object for rendering. 12113cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const { 12123692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner return PP->Diag(getSourceLocation(Loc), DiagID); 12135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 12145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 12165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Trigraph and Escaped Newline Handling Code. 12175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 12185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair, 12205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the decoded trigraph letter it corresponds to, or '\0' if nothing. 12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char GetTrigraphCharForLetter(char Letter) { 12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Letter) { 12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: return 0; 12245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '=': return '#'; 12255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ')': return ']'; 12265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '(': return '['; 12275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '!': return '|'; 12285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\'': return '^'; 12295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '>': return '}'; 12305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '/': return '\\'; 12315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '<': return '{'; 12325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '-': return '~'; 12335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 12355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// DecodeTrigraphChar - If the specified character is a legal trigraph when 12375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled, 12385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the result character. Finally, emit a warning about trigraph use 12395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whether trigraphs are enabled or not. 12405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char DecodeTrigraphChar(const char *CP, Lexer *L) { 12415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Res = GetTrigraphCharForLetter(*CP); 12423692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner if (!Res || !L) return Res; 12431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12444e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!L->getLangOpts().Trigraphs) { 124574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 124674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CP-2, diag::trigraph_ignored); 12473692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner return 0; 12485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 125074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 12515f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner L->Diag(CP-2, diag::trigraph_converted) << StringRef(&Res, 1); 12525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return Res; 12535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 12545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 125524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// getEscapedNewLineSize - Return the size of the specified escaped newline, 125624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a 12571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// trigraph equivalent on entry to this function. 125824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattnerunsigned Lexer::getEscapedNewLineSize(const char *Ptr) { 125924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner unsigned Size = 0; 126024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner while (isWhitespace(Ptr[Size])) { 126124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner ++Size; 12621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 126324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r') 126424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner continue; 126524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 126624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // If this is a \r\n or \n\r, skip the other half. 126724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') && 126824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr[Size-1] != Ptr[Size]) 126924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner ++Size; 12701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 127124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return Size; 12721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 12731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 127424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Not an escaped newline, must be a \t or something else. 127524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return 0; 127624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner} 127724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 1278033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// SkipEscapedNewLines - If P points to an escaped newline (or a series of 1279033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// them), skip over them and return the first non-escaped-newline found, 1280033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// otherwise return P. 1281033749571f8d4c804eeb357c70b06424aa24503bChris Lattnerconst char *Lexer::SkipEscapedNewLines(const char *P) { 1282033749571f8d4c804eeb357c70b06424aa24503bChris Lattner while (1) { 1283033749571f8d4c804eeb357c70b06424aa24503bChris Lattner const char *AfterEscape; 1284033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (*P == '\\') { 1285033749571f8d4c804eeb357c70b06424aa24503bChris Lattner AfterEscape = P+1; 1286033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } else if (*P == '?') { 1287033749571f8d4c804eeb357c70b06424aa24503bChris Lattner // If not a trigraph for escape, bail out. 1288033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (P[1] != '?' || P[2] != '/') 1289033749571f8d4c804eeb357c70b06424aa24503bChris Lattner return P; 1290033749571f8d4c804eeb357c70b06424aa24503bChris Lattner AfterEscape = P+3; 1291033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } else { 1292033749571f8d4c804eeb357c70b06424aa24503bChris Lattner return P; 1293033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } 12941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1295033749571f8d4c804eeb357c70b06424aa24503bChris Lattner unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape); 1296033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (NewLineSize == 0) return P; 1297033749571f8d4c804eeb357c70b06424aa24503bChris Lattner P = AfterEscape+NewLineSize; 1298033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } 1299033749571f8d4c804eeb357c70b06424aa24503bChris Lattner} 1300033749571f8d4c804eeb357c70b06424aa24503bChris Lattner 1301aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// \brief Checks that the given token is the first token that occurs after the 1302aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// given location (this excludes comments and whitespace). Returns the location 1303aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// immediately after the specified token. If the token is not found or the 1304aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// location is inside a macro, the returned source location will be invalid. 1305aca25bccefe56121b686706afc84c8cb5d46e65bAnna ZaksSourceLocation Lexer::findLocationAfterToken(SourceLocation Loc, 1306aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks tok::TokenKind TKind, 1307aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks const SourceManager &SM, 1308aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks const LangOptions &LangOpts, 1309aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks bool SkipTrailingWhitespaceAndNewLine) { 1310aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks if (Loc.isMacroID()) { 131169bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (!Lexer::isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc)) 1312aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks return SourceLocation(); 1313aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks } 1314aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, LangOpts); 1315aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1316aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Break down the source location. 1317aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 1318aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1319aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Try to load the file buffer. 1320aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks bool InvalidTemp = false; 1321aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks llvm::StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp); 1322aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks if (InvalidTemp) 1323aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks return SourceLocation(); 1324aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1325aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks const char *TokenBegin = File.data() + LocInfo.second; 1326aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1327aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Lex from the start of the given location. 1328aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks Lexer lexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(), 1329aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks TokenBegin, File.end()); 1330aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Find the token. 1331aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks Token Tok; 1332aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks lexer.LexFromRawLexer(Tok); 1333aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks if (Tok.isNot(TKind)) 1334aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks return SourceLocation(); 1335aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks SourceLocation TokenLoc = Tok.getLocation(); 1336aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1337aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Calculate how much whitespace needs to be skipped if any. 1338aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks unsigned NumWhitespaceChars = 0; 1339aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks if (SkipTrailingWhitespaceAndNewLine) { 1340aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks const char *TokenEnd = SM.getCharacterData(TokenLoc) + 1341aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks Tok.getLength(); 1342aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks unsigned char C = *TokenEnd; 1343aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks while (isHorizontalWhitespace(C)) { 1344aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks C = *(++TokenEnd); 1345aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks NumWhitespaceChars++; 1346aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks } 134735a2b798efd61fec425553f387d76be9c522f184Eli Friedman 134835a2b798efd61fec425553f387d76be9c522f184Eli Friedman // Skip \r, \n, \r\n, or \n\r 134935a2b798efd61fec425553f387d76be9c522f184Eli Friedman if (C == '\n' || C == '\r') { 135035a2b798efd61fec425553f387d76be9c522f184Eli Friedman char PrevC = C; 135135a2b798efd61fec425553f387d76be9c522f184Eli Friedman C = *(++TokenEnd); 1352aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks NumWhitespaceChars++; 135335a2b798efd61fec425553f387d76be9c522f184Eli Friedman if ((C == '\n' || C == '\r') && C != PrevC) 135435a2b798efd61fec425553f387d76be9c522f184Eli Friedman NumWhitespaceChars++; 135535a2b798efd61fec425553f387d76be9c522f184Eli Friedman } 1356aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks } 1357aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1358a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return TokenLoc.getLocWithOffset(Tok.getLength() + NumWhitespaceChars); 1359aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks} 136024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 13615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer, 13625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// get its size, and return it. This is tricky in several cases: 13635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 1. If currently at the start of a trigraph, we warn about the trigraph, 13645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// then either return the trigraph (skipping 3 chars) or the '?', 13655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// depending on whether trigraphs are enabled or not. 13665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 2. If this is an escaped newline (potentially with whitespace between 13675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the backslash and newline), implicitly skip the newline and return 13685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the char after it. 13695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 3. If this is a UCN, return it. FIXME: C++ UCN's? 13705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 13715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This handles the slow/uncommon case of the getCharAndSize method. Here we 13725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// know that we can accumulate into Size, and that we have already incremented 13735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Ptr by Size bytes. 13745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 13755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should 13765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match. 13775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 13785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size, 1379d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token *Tok) { 13805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, look for an escaped newline. 13815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '\\') { 13825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 13835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Ptr; 13845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash: 13855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Common case, backslash-char where the char is not whitespace. 13865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isWhitespace(Ptr[0])) return '\\'; 13871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13885636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner // See if we have optional whitespace characters between the slash and 13895636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner // newline. 139024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { 139124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Remember that this token needs to be cleaned. 139224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (Tok) Tok->setFlag(Token::NeedsCleaning); 139324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 139424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Warn if there was whitespace between the backslash and newline. 13955636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode()) 139624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Diag(Ptr, diag::backslash_newline_space); 13971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 139824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Found backslash<whitespace><newline>. Parse the char after it. 139924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Size += EscapedNewLineSize; 140024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr += EscapedNewLineSize; 1401f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis 140204a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // If the char that we finally got was a \n, then we must have had 140304a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // something like \<newline><newline>. We don't want to consume the 140404a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // second newline. 140504a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0') 140604a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis return ' '; 1407f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis 140824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Use slow version to accumulate a correct size field. 140924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return getCharAndSizeSlow(Ptr, Size, Tok); 141024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner } 14111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is not an escaped newline, just return the slash. 14135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return '\\'; 14145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a trigraph, process it. 14175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '?' && Ptr[1] == '?') { 14185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is actually a legal trigraph (not something like "??x"), emit 14195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // a trigraph warning. If so, and if trigraphs are enabled, return it. 14205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) { 14215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Remember that this token needs to be cleaned. 1422d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner if (Tok) Tok->setFlag(Token::NeedsCleaning); 14235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Ptr += 3; 14255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Size += 3; 14265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') goto Slash; 14275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return C; 14285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is neither, return a single character. 14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 14335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return *Ptr; 14345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 14355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the 14385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size, 14395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// and that we have already incremented Ptr by Size bytes. 14405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 14415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlow (above) should 14425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match. 14435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size, 14444e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts) { 14455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, look for an escaped newline. 14465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '\\') { 14475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 14485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Ptr; 14495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash: 14505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Common case, backslash-char where the char is not whitespace. 14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isWhitespace(Ptr[0])) return '\\'; 14521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // See if we have optional whitespace characters followed by a newline. 145424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { 145524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Found backslash<whitespace><newline>. Parse the char after it. 145624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Size += EscapedNewLineSize; 145724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr += EscapedNewLineSize; 14581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 145904a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // If the char that we finally got was a \n, then we must have had 146004a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // something like \<newline><newline>. We don't want to consume the 146104a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // second newline. 146204a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0') 146304a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis return ' '; 1464f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis 146524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Use slow version to accumulate a correct size field. 14664e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie return getCharAndSizeSlowNoWarn(Ptr, Size, LangOpts); 146724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner } 14681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is not an escaped newline, just return the slash. 14705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return '\\'; 14715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a trigraph, process it. 14744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') { 14755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is actually a legal trigraph (not something like "??x"), return 14765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it. 14775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (char C = GetTrigraphCharForLetter(Ptr[2])) { 14785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Ptr += 3; 14795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Size += 3; 14805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') goto Slash; 14815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return C; 14825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is neither, return a single character. 14865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 14875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return *Ptr; 14885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 14895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 14915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper methods for lexing. 14925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 14935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1494f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor/// \brief Routine that indiscriminately skips bytes in the source file. 1495f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregorvoid Lexer::SkipBytes(unsigned Bytes, bool StartOfLine) { 1496f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor BufferPtr += Bytes; 1497f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor if (BufferPtr > BufferEnd) 1498f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor BufferPtr = BufferEnd; 1499f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor IsAtStartOfLine = StartOfLine; 1500f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor} 1501f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor 1502d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexIdentifier(Token &Result, const char *CurPtr) { 15035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$] 15045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned Size; 15055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned char C = *CurPtr++; 1506cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner while (isIdentifierBody(C)) 15075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 1508cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner 15095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; // Back up over the skipped character. 15105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Fast path, no $,\,? in identifier found. '\' might be an escaped newline 15125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN. 15135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: UCNs. 1514cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner // 1515cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner // TODO: Could merge these checks into a CharInfo flag to make the comparison 1516cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner // cheaper 15174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (C != '\\' && C != '?' && (C != '$' || !LangOpts.DollarIdents)) { 15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerFinishIdentifier: 15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *IdStart = BufferPtr; 1520c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara FormTokenWithChars(Result, CurPtr, tok::raw_identifier); 1521c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara Result.setRawIdentifierData(IdStart); 15221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are in raw mode, return this identifier raw. There is no need to 15245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // look up identifier information or attempt to macro expand it. 1525c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara if (LexingRawMode) 1526c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara return; 15271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1528c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // Fill in Result.IdentifierInfo and update the token kind, 1529c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // looking up the identifier in the identifier table. 1530c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara IdentifierInfo *II = PP->LookUpIdentifierInfo(Result); 15311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Finally, now that we know we have an identifier, pass this off to the 15335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // preprocessor, which may macro expand it or something. 1534d1186fa38166a581b51975f0382a45fc3a0733d0Chris Lattner if (II->isHandleIdentifierCase()) 15356a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner PP->HandleIdentifier(Result); 15366aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 15376a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner return; 15385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, $,\,? in identifier found. Enter slower path. 15411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 15435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 15445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '$') { 15455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we hit a $ and they are not supported in identifiers, we are done. 15464e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!LangOpts.DollarIdents) goto FinishIdentifier; 15471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, emit a diagnostic and continue. 154974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 155074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr, diag::ext_dollar_in_identifier); 15515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 15525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 15535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer continue; 15545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (!isIdentifierBody(C)) { // FIXME: UCNs. 15555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Found end of identifier. 15565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto FinishIdentifier; 15575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this character is good, consume it. 15605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 15615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 15635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isIdentifierBody(C)) { // FIXME: UCNs. 15645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 15655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 15665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 15695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1570a75ec43d625753b4439b0d6f70bd988444c74617Douglas Gregor/// isHexaLiteral - Return true if Start points to a hex constant. 15714a551000bee716ac8b1bbe16134a53f0ad221a5aChris Lattner/// in microsoft mode (where this is supposed to be several different tokens). 1572e506f8a41063410c75945ebb804758bd0202947fEli Friedmanbool Lexer::isHexaLiteral(const char *Start, const LangOptions &LangOpts) { 15736ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner unsigned Size; 15744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie char C1 = Lexer::getCharAndSizeNoWarn(Start, Size, LangOpts); 15756ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner if (C1 != '0') 15766ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner return false; 15774e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie char C2 = Lexer::getCharAndSizeNoWarn(Start + Size, Size, LangOpts); 15786ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner return (C2 == 'x' || C2 == 'X'); 1579a75ec43d625753b4439b0d6f70bd988444c74617Douglas Gregor} 15805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15815253c7ff266ae79308050c9f43d60dd1a67c5fb9Nate Begeman/// LexNumericConstant - Lex the remainder of a integer or floating point 15825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. From[-1] is the first character lexed. Return the end of the 15835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. 1584d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexNumericConstant(Token &Result, const char *CurPtr) { 15855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned Size; 15865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getCharAndSize(CurPtr, Size); 15875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char PrevCh = 0; 1588dd81731d82a33a242e7b088e249e56873ef52807Nico Weber while (isNumberBody(C)) { // FIXME: UCNs in ud-suffix. 15895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 15905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer PrevCh = C; 15915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 15925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we fell out, check for a sign, due to 1e+12. If we have one, continue. 1595b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) { 1596b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner // If we are in Microsoft mode, don't continue if the constant is hex. 1597b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner // For example, MSVC will accept the following as 3 tokens: 0x1234567e+1 15984e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!LangOpts.MicrosoftExt || !isHexaLiteral(BufferPtr, LangOpts)) 1599b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); 1600b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner } 16015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a hex FP constant, continue. 1603d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p')) { 1604d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith // Outside C99, we accept hexadecimal floating point numbers as a 1605d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith // not-quite-conforming extension. Only do so if this looks like it's 1606d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith // actually meant to be a hexfloat, and not if it has a ud-suffix. 1607d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith bool IsHexFloat = true; 1608d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith if (!LangOpts.C99) { 1609d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith if (!isHexaLiteral(BufferPtr, LangOpts)) 1610d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith IsHexFloat = false; 1611d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith else if (std::find(BufferPtr, CurPtr, '_') != CurPtr) 1612d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith IsHexFloat = false; 1613d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith } 1614d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith if (IsHexFloat) 1615d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); 1616d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith } 16171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 161947246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 16209e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::numeric_constant); 162147246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 16225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 16235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16245cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith/// LexUDSuffix - Lex the ud-suffix production for user-defined literal suffixes 1625e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith/// in C++11, or warn on a ud-suffix in C++98. 16265cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smithconst char *Lexer::LexUDSuffix(Token &Result, const char *CurPtr) { 16274e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(getLangOpts().CPlusPlus); 16285cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 16295cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith // Maximally munch an identifier. FIXME: UCNs. 16305cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith unsigned Size; 16315cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith char C = getCharAndSize(CurPtr, Size); 16325cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith if (isIdentifierHead(C)) { 16334e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().CPlusPlus0x) { 1634e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith if (!isLexingRawMode()) 16352fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith Diag(CurPtr, 16362fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith C == '_' ? diag::warn_cxx11_compat_user_defined_literal 16372fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith : diag::warn_cxx11_compat_reserved_user_defined_literal) 16382fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith << FixItHint::CreateInsertion(getSourceLocation(CurPtr), " "); 16392fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith return CurPtr; 16402fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith } 16412fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith 16422fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith // C++11 [lex.ext]p10, [usrlit.suffix]p1: A program containing a ud-suffix 16432fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith // that does not start with an underscore is ill-formed. As a conforming 16442fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith // extension, we treat all such suffixes as if they had whitespace before 16452fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith // them. 16462fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith if (C != '_') { 16472fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith if (!isLexingRawMode()) 1648b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet Diag(CurPtr, getLangOpts().MicrosoftMode ? 1649b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet diag::ext_ms_reserved_user_defined_literal : 1650b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet diag::ext_reserved_user_defined_literal) 1651e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith << FixItHint::CreateInsertion(getSourceLocation(CurPtr), " "); 1652e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith return CurPtr; 1653e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith } 1654e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith 165599831e4677a7e2e051af636221694d60ba31fcdbRichard Smith Result.setFlag(Token::HasUDSuffix); 16565cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith do { 16575cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith CurPtr = ConsumeChar(CurPtr, Size, Result); 16585cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith C = getCharAndSize(CurPtr, Size); 16595cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith } while (isIdentifierBody(C)); 16605cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith } 16615cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith return CurPtr; 16625cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith} 16635cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 16645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexStringLiteral - Lex the remainder of a string literal, after having lexed 16655cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor/// either " or L" or u8" or u" or U". 16665cee1195584fa8672253139c86e922daeda69b9eDouglas Gregorvoid Lexer::LexStringLiteral(Token &Result, const char *CurPtr, 16675cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::TokenKind Kind) { 16685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this string contain the \0 character? 16691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1670661a99690bc133bbaa029da925481d4a860dec90Richard Smith if (!isLexingRawMode() && 1671661a99690bc133bbaa029da925481d4a860dec90Richard Smith (Kind == tok::utf8_string_literal || 1672661a99690bc133bbaa029da925481d4a860dec90Richard Smith Kind == tok::utf16_string_literal || 1673661a99690bc133bbaa029da925481d4a860dec90Richard Smith Kind == tok::utf32_string_literal)) 1674661a99690bc133bbaa029da925481d4a860dec90Richard Smith Diag(BufferPtr, diag::warn_cxx98_compat_unicode_literal); 1675661a99690bc133bbaa029da925481d4a860dec90Richard Smith 16765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 16775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '"') { 1678571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner // Skip escaped characters. Escaped newlines will already be processed by 1679571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner // getAndAdvanceChar. 1680571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner if (C == '\\') 16815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 168233611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor 1683571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner if (C == '\n' || C == '\r' || // Newline. 168433611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. 16854e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!isLexingRawMode() && !LangOpts.AsmPreprocessor) 1686b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith Diag(BufferPtr, diag::ext_unterminated_string); 16879e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr-1, tok::unknown); 16885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 16895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1690571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner 16917d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (C == 0) { 16927d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (isCodeCompletionPoint(CurPtr-1)) { 16937d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 16947d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis FormTokenWithChars(Result, CurPtr-1, tok::unknown); 16957d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return cutOffLexing(); 16967d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 16977d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 1698571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner NulCharacter = CurPtr-1; 16997d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 17005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 17015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17035cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith // If we are in C++11, lex the optional ud-suffix. 17044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus) 17055cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith CurPtr = LexUDSuffix(Result, CurPtr); 17065cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 17075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If a nul character existed in the string, warn about it. 170874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 170974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_string); 17105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 17115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of the token as well as the BufferPtr instance var. 171247246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 17135cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor FormTokenWithChars(Result, CurPtr, Kind); 171447246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 17155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 17165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 17172fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// LexRawStringLiteral - Lex the remainder of a raw string literal, after 17182fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// having lexed R", LR", u8R", uR", or UR". 17192fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Toppervoid Lexer::LexRawStringLiteral(Token &Result, const char *CurPtr, 17202fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::TokenKind Kind) { 17212fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // This function doesn't use getAndAdvanceChar because C++0x [lex.pptoken]p3: 17222fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Between the initial and final double quote characters of the raw string, 17232fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // any transformations performed in phases 1 and 2 (trigraphs, 17242fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // universal-character-names, and line splicing) are reverted. 17252fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 1726661a99690bc133bbaa029da925481d4a860dec90Richard Smith if (!isLexingRawMode()) 1727661a99690bc133bbaa029da925481d4a860dec90Richard Smith Diag(BufferPtr, diag::warn_cxx98_compat_raw_string_literal); 1728661a99690bc133bbaa029da925481d4a860dec90Richard Smith 17292fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper unsigned PrefixLen = 0; 17302fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17312fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper while (PrefixLen != 16 && isRawStringDelimBody(CurPtr[PrefixLen])) 17322fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ++PrefixLen; 17332fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17342fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // If the last character was not a '(', then we didn't lex a valid delimiter. 17352fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (CurPtr[PrefixLen] != '(') { 17362fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (!isLexingRawMode()) { 17372fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper const char *PrefixEnd = &CurPtr[PrefixLen]; 17382fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (PrefixLen == 16) { 17392fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Diag(PrefixEnd, diag::err_raw_delim_too_long); 17402fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } else { 17412fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Diag(PrefixEnd, diag::err_invalid_char_raw_delim) 17422fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper << StringRef(PrefixEnd, 1); 17432fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17442fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17452fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17462fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Search for the next '"' in hopes of salvaging the lexer. Unfortunately, 17472fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // it's possible the '"' was intended to be part of the raw string, but 17482fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // there's not much we can do about that. 17492fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper while (1) { 17502fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper char C = *CurPtr++; 17512fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17522fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (C == '"') 17532fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper break; 17542fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (C == 0 && CurPtr-1 == BufferEnd) { 17552fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper --CurPtr; 17562fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper break; 17572fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17582fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17592fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17602fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper FormTokenWithChars(Result, CurPtr, tok::unknown); 17612fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return; 17622fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17632fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17642fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Save prefix and move CurPtr past it 17652fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper const char *Prefix = CurPtr; 17662fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CurPtr += PrefixLen + 1; // skip over prefix and '(' 17672fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17682fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper while (1) { 17692fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper char C = *CurPtr++; 17702fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17712fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (C == ')') { 17722fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Check for prefix match and closing quote. 17732fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (strncmp(CurPtr, Prefix, PrefixLen) == 0 && CurPtr[PrefixLen] == '"') { 17742fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CurPtr += PrefixLen + 1; // skip over prefix and '"' 17752fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper break; 17762fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17772fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } else if (C == 0 && CurPtr-1 == BufferEnd) { // End of file. 17782fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (!isLexingRawMode()) 17792fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Diag(BufferPtr, diag::err_unterminated_raw_string) 17802fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper << StringRef(Prefix, PrefixLen); 17812fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper FormTokenWithChars(Result, CurPtr-1, tok::unknown); 17822fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return; 17832fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17842fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17852fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17865cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith // If we are in C++11, lex the optional ud-suffix. 17874e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus) 17885cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith CurPtr = LexUDSuffix(Result, CurPtr); 17895cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 17902fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Update the location of token as well as BufferPtr. 17912fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper const char *TokStart = BufferPtr; 17922fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper FormTokenWithChars(Result, CurPtr, Kind); 17932fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Result.setLiteralData(TokStart); 17942fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper} 17952fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexAngledStringLiteral - Lex the remainder of an angled string literal, 17975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// after having lexed the '<' character. This is used for #include filenames. 1798d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) { 17995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this string contain the \0 character? 18009cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner const char *AfterLessPos = CurPtr; 18015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 18025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '>') { 18035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip escaped characters. 18045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') { 18055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip the escaped character. 180660b202c5eb6fb2d608bfef05523f40fdf5091d48Dmitri Gribenko getAndAdvanceChar(CurPtr, Result); 18075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == '\n' || C == '\r' || // Newline. 18087d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis (C == 0 && (CurPtr-1 == BufferEnd || // End of file. 18097d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis isCodeCompletionPoint(CurPtr-1)))) { 18109cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner // If the filename is unterminated, then it must just be a lone < 18119cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner // character. Return this as such. 18129cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner FormTokenWithChars(Result, AfterLessPos, tok::less); 18135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 18145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == 0) { 18155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer NulCharacter = CurPtr-1; 18165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 18185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If a nul character existed in the string, warn about it. 182174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 182274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_string); 18231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 182547246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 18269e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::angle_string_literal); 182747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 18285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 18295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexCharConstant - Lex the remainder of a character constant, after having 18325cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor/// lexed either ' or L' or u' or U'. 18335cee1195584fa8672253139c86e922daeda69b9eDouglas Gregorvoid Lexer::LexCharConstant(Token &Result, const char *CurPtr, 18345cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::TokenKind Kind) { 18355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this character contain the \0 character? 18365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1837661a99690bc133bbaa029da925481d4a860dec90Richard Smith if (!isLexingRawMode() && 1838661a99690bc133bbaa029da925481d4a860dec90Richard Smith (Kind == tok::utf16_char_constant || Kind == tok::utf32_char_constant)) 1839661a99690bc133bbaa029da925481d4a860dec90Richard Smith Diag(BufferPtr, diag::warn_cxx98_compat_unicode_literal); 1840661a99690bc133bbaa029da925481d4a860dec90Richard Smith 18415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 18425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\'') { 18434e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!isLexingRawMode() && !LangOpts.AsmPreprocessor) 1844b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith Diag(BufferPtr, diag::ext_empty_character); 18459e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 18465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 18475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1849d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner while (C != '\'') { 1850d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner // Skip escaped characters. 18516d926ae667cca926e77ddce1734514911706ed0aNico Weber if (C == '\\') 18526d926ae667cca926e77ddce1734514911706ed0aNico Weber C = getAndAdvanceChar(CurPtr, Result); 18536d926ae667cca926e77ddce1734514911706ed0aNico Weber 18546d926ae667cca926e77ddce1734514911706ed0aNico Weber if (C == '\n' || C == '\r' || // Newline. 18556d926ae667cca926e77ddce1734514911706ed0aNico Weber (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. 18564e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!isLexingRawMode() && !LangOpts.AsmPreprocessor) 1857b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith Diag(BufferPtr, diag::ext_unterminated_char); 1858d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner FormTokenWithChars(Result, CurPtr-1, tok::unknown); 1859d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner return; 18606d926ae667cca926e77ddce1734514911706ed0aNico Weber } 18616d926ae667cca926e77ddce1734514911706ed0aNico Weber 18626d926ae667cca926e77ddce1734514911706ed0aNico Weber if (C == 0) { 18637d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (isCodeCompletionPoint(CurPtr-1)) { 18647d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 18657d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis FormTokenWithChars(Result, CurPtr-1, tok::unknown); 18667d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return cutOffLexing(); 18677d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 18687d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 1869d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner NulCharacter = CurPtr-1; 1870d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner } 1871d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner C = getAndAdvanceChar(CurPtr, Result); 18725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18745cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith // If we are in C++11, lex the optional ud-suffix. 18754e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus) 18765cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith CurPtr = LexUDSuffix(Result, CurPtr); 18775cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 1878d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner // If a nul character existed in the character, warn about it. 187974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 188074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_char); 18815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 188347246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 18845cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor FormTokenWithChars(Result, CurPtr, Kind); 188547246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 18865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 18875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipWhitespace - Efficiently skip over a series of whitespace characters. 18895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Update BufferPtr to point to the next non-whitespace character and return. 1890d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// 1891d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// This method forms a token and returns true if KeepWhitespaceMode is enabled. 1892d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// 1893d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnerbool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) { 18945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Whitespace - Skip it, then return the token after the whitespace. 18955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently. 18965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 18975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip horizontal whitespace very aggressively. 18985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isHorizontalWhitespace(Char)) 18995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = *++CurPtr; 19001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1901ddd3e8b90a92c39f620bda7a0945320c0b9e60dbDaniel Dunbar // Otherwise if we have something other than whitespace, we're done. 19025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char != '\n' && Char != '\r') 19035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 19041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 19065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // End of preprocessor directive line, let LexTokenInternal handle this. 19075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 1908d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return false; 19095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 19101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // ok, but handle newline. 19125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The returned token is at the start of the line. 1913d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 19145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // No leading whitespace seen so far. 1915d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::LeadingSpace); 19165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = *++CurPtr; 19175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 19185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 19195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this isn't immediately after a newline, there is leading space. 19205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char PrevChar = CurPtr[-1]; 19215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (PrevChar != '\n' && PrevChar != '\r') 1922d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 19235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1924d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // If the client wants us to return whitespace, return it now. 1925d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (isKeepWhitespaceMode()) { 19269e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 1927d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return true; 1928d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner } 19291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 1931d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return false; 19325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 19335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1934bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// We have just read the // characters from input. Skip until we find the 1935bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// newline character thats terminate the comment. Then update BufferPtr and 1936bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// return. 1937046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// 1938046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted 1939046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true. 1940bb23628148f555a4cf71f98c27096a7a804c085cNico Weberbool Lexer::SkipLineComment(Token &Result, const char *CurPtr) { 1941bb23628148f555a4cf71f98c27096a7a804c085cNico Weber // If Line comments aren't explicitly enabled for this language, emit an 19425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // extension warning. 1943bb23628148f555a4cf71f98c27096a7a804c085cNico Weber if (!LangOpts.LineComment && !isLexingRawMode()) { 1944bb23628148f555a4cf71f98c27096a7a804c085cNico Weber Diag(BufferPtr, diag::ext_line_comment); 19451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Mark them enabled so we only emit one warning for this translation 19475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // unit. 1948bb23628148f555a4cf71f98c27096a7a804c085cNico Weber LangOpts.LineComment = true; 19495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 19501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan over the body of the comment. The common case, when scanning, is that 19525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the comment contains normal ascii characters with nothing interesting in 19535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // them. As such, optimize for this case with the inner loop. 19545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C; 19555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer do { 19565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr; 19575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip over characters in the fast loop. 19585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != 0 && // Potentially EOF. 19595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C != '\n' && C != '\r') // Newline or DOS-style newline. 19605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *++CurPtr; 19615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 19621daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer const char *NextLine = CurPtr; 19631daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer if (C != 0) { 19641daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer // We found a newline, see if it's escaped. 19651daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer const char *EscapePtr = CurPtr-1; 19661daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer while (isHorizontalWhitespace(*EscapePtr)) // Skip whitespace. 19671daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer --EscapePtr; 19681daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer 19691daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer if (*EscapePtr == '\\') // Escaped newline. 19701daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer CurPtr = EscapePtr; 19711daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer else if (EscapePtr[0] == '/' && EscapePtr[-1] == '?' && 19721daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer EscapePtr[-2] == '?') // Trigraph-escaped newline. 19731daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer CurPtr = EscapePtr-2; 19741daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer else 19751daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer break; // This is a newline, we're done. 19761daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer } 19771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to 1979bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // properly decode the character. Read it in raw mode to avoid emitting 1980bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // diagnostics about things like trigraphs. If we see an escaped newline, 1981bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // we'll handle it below. 19825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *OldPtr = CurPtr; 1983bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner bool OldRawMode = isLexingRawMode(); 1984bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner LexingRawMode = true; 19855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 1986bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner LexingRawMode = OldRawMode; 1987ead616c5d8681a41b11273327813e61bda01907aChris Lattner 19881daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer // If we only read only one character, then no special handling is needed. 19891daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer // We're done and can skip forward to the newline. 19901daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer if (C != 0 && CurPtr == OldPtr+1) { 19911daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer CurPtr = NextLine; 19921daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer break; 19931daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer } 19941daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer 19955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we read multiple characters, and one of those characters was a \r or 19965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n, then we had an escaped newline within the comment. Emit diagnostic 19975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // unless the next line is also a // comment. 19985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') { 19995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (; OldPtr != CurPtr; ++OldPtr) 20005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (OldPtr[0] == '\n' || OldPtr[0] == '\r') { 20015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Okay, we found a // comment that ends in a newline, if the next 20025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // line is also a // comment, but has spaces, don't emit a diagnostic. 20035d6ae288bc661572e52ea71fc14b249eb5e2d9e9Benjamin Kramer if (isWhitespace(C)) { 20045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *ForwardPtr = CurPtr; 20055d6ae288bc661572e52ea71fc14b249eb5e2d9e9Benjamin Kramer while (isWhitespace(*ForwardPtr)) // Skip whitespace. 20065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++ForwardPtr; 20075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/') 20085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 20095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 20101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 201174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 2012bb23628148f555a4cf71f98c27096a7a804c085cNico Weber Diag(OldPtr-1, diag::ext_multi_line_line_comment); 20135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 20145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 20155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 20161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 201755817afdf9d453a443262a733f6caf6692dca118Douglas Gregor if (CurPtr == BufferEnd+1) { 201855817afdf9d453a443262a733f6caf6692dca118Douglas Gregor --CurPtr; 201955817afdf9d453a443262a733f6caf6692dca118Douglas Gregor break; 202055817afdf9d453a443262a733f6caf6692dca118Douglas Gregor } 20217d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 20227d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) { 20237d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 20247d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis cutOffLexing(); 20257d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return false; 20267d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 20277d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 20285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } while (C != '\n' && C != '\r'); 20295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 20303d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner // Found but did not consume the newline. Notify comment handlers about the 20313d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner // comment unless we're in a #if 0 block. 20323d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner if (PP && !isLexingRawMode() && 20333d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr), 20343d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner getSourceLocation(CurPtr)))) { 2035046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner BufferPtr = CurPtr; 2036046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return true; // A token has to be returned. 2037046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner } 20381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are returning comments as tokens, return this comment as a token. 2040fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner if (inKeepCommentMode()) 2041bb23628148f555a4cf71f98c27096a7a804c085cNico Weber return SaveLineComment(Result, CurPtr); 20425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 20435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are inside a preprocessor directive and we see the end of line, 204484021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // return immediately, so that the lexer can return this as an EOD token. 20455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective || CurPtr == BufferEnd) { 20465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 20472d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 20485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 20491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, eat the \n character. We don't care if this is a \n\r or 20517a4f004a77421af876152281b92c4f8ea24afb2dChris Lattner // \r\n sequence. This is an efficiency hack (because we know the \n can't 2052d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // contribute to another token), it isn't needed for correctness. Note that 2053d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // this is ok even in KeepWhitespaceMode, because we would have returned the 2054d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner /// comment above in that mode. 20555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 20561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The next returned token is at the start of the line. 2058d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 20595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // No leading whitespace seen so far. 2060d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::LeadingSpace); 20615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 20622d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 20635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 20645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2065bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// If in save-comment mode, package up this Line comment in an appropriate 2066bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// way and return it. 2067bb23628148f555a4cf71f98c27096a7a804c085cNico Weberbool Lexer::SaveLineComment(Token &Result, const char *CurPtr) { 20689e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // If we're not in a preprocessor directive, just return the // comment 20699e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // directly. 20709e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::comment); 20711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20728c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie if (!ParsingPreprocessorDirective || LexingRawMode) 20739e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner return true; 20741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2075bb23628148f555a4cf71f98c27096a7a804c085cNico Weber // If this Line-style comment is in a macro definition, transmogrify it into 20769e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // a C-style block comment. 2077453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor bool Invalid = false; 2078453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor std::string Spelling = PP->getSpelling(Result, &Invalid); 2079453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor if (Invalid) 2080453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor return true; 2081453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor 2082bb23628148f555a4cf71f98c27096a7a804c085cNico Weber assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not line comment?"); 20839e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Spelling[1] = '*'; // Change prefix to "/*". 20849e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Spelling += "*/"; // add suffix. 20851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20869e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Result.setKind(tok::comment); 2087374b3837d676133fcc1eb70a25c8baf8ec4a5c4aDmitri Gribenko PP->CreateString(Spelling, Result, 2088a08529cc3f00e0b47a3c028823634129ac46847bAbramo Bagnara Result.getLocation(), Result.getLocation()); 20892d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return true; 20905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 20915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 20925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline 209380d7c52653800d3338cca2c4388348010d2b1921David Blaikie/// character (either \\n or \\r) is part of an escaped newline sequence. Issue 209480d7c52653800d3338cca2c4388348010d2b1921David Blaikie/// a diagnostic if so. We know that the newline is inside of a block comment. 20951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr, 20965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Lexer *L) { 20975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(CurPtr[0] == '\n' || CurPtr[0] == '\r'); 20981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Back up off the newline. 21005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 21011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a two-character newline sequence, skip the other character. 21035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == '\n' || CurPtr[0] == '\r') { 21045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n\n or \r\r -> not escaped newline. 21055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == CurPtr[1]) 21065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 21075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n\r or \r\n -> skip the newline. 21085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 21095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 21101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have horizontal whitespace, skip over it. We allow whitespace 21125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // between the slash and newline. 21135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool HasSpace = false; 21145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) { 21155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 21165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer HasSpace = true; 21175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 21181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, we know this is an escaped newline. 21205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (*CurPtr == '\\') { 21215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[-1] != '*') return false; 21225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 21235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It isn't a slash, is it the ?? / trigraph? 21245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' || 21255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[-3] != '*') 21265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 21271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // This is the trigraph ending the comment. Emit a stern warning! 21295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr -= 2; 21305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 21315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If no trigraphs are enabled, warn that we ignored this trigraph and 21325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // ignore this * character. 21334e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!L->getLangOpts().Trigraphs) { 213474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 213574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::trigraph_ignored_block_comment); 21365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 21375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 213874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 213974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::trigraph_ends_block_comment); 21405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 21411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Warn about having an escaped newline between the */ characters. 214374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 214474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::escaped_newline_block_comment_end); 21451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If there was space between the backslash and newline, warn about it. 214774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (HasSpace && !L->isLexingRawMode()) 214874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::backslash_newline_space); 21491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 21515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 21525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 21535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__ 21545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <emmintrin.h> 21555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__ 21565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <altivec.h> 21575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#undef bool 21585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif 21595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2160ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// We have just read from input the / and * characters that started a comment. 2161ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// Read until we find the * and / characters that terminate the comment. 2162ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// Note that we don't bother decoding trigraphs or escaped newlines in block 2163ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// comments, because they cannot cause the comment to end. The only thing 2164ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// that can happen is the comment could end with an escaped newline between 2165ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// the terminating * and /. 21662d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner/// 2167046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted 2168046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true. 2169d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) { 21705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan one character past where we should, looking for a '/' character. Once 2171fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner // we find it, check to see if it was preceded by a *. This common 21725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // optimization helps people who like to put a lot of * characters in their 21735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // comments. 21748146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner 21758146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // The first character we get with newlines and trigraphs skipped to handle 21768146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // the degenerate /*/ case below correctly if the * has an escaped newline 21778146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // after it. 21788146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner unsigned CharSize; 21798146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner unsigned char C = getCharAndSize(CurPtr, CharSize); 21808146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner CurPtr += CharSize; 21815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == 0 && CurPtr == BufferEnd+1) { 21827d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (!isLexingRawMode()) 21830af574270d3be2b0e73a3379dfaa633746f8fc6fChris Lattner Diag(BufferPtr, diag::err_unterminated_block_comment); 218431f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner --CurPtr; 21851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 218631f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // KeepWhitespaceMode should return this broken comment as a token. Since 218731f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // it isn't a well formed comment, just return it as an 'unknown' token. 218831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner if (isKeepWhitespaceMode()) { 21899e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 219031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner return true; 219131f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner } 21921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 219331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner BufferPtr = CurPtr; 21942d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 21955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 21961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21978146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // Check to see if the first character after the '/*' is another /. If so, 21988146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // then this slash does not end the block comment, it is part of it. 21998146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner if (C == '/') 22008146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner C = *CurPtr++; 22011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 22035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip over all non-interesting characters until we find end of buffer or a 22045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // (probably ending) '/' character. 22057d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (CurPtr + 24 < BufferEnd && 22067d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis // If there is a code-completion point avoid the fast scan because it 22077d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis // doesn't check for '\0'. 22087d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis !(PP && PP->getCodeCompletionFileLoc() == FileLoc)) { 22095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // While not aligned to a 16-byte boundary. 22105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0) 22115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 22121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '/') goto FoundSlash; 22145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 22155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__ 22163f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer __m128i Slashes = _mm_set1_epi8('/'); 22173f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer while (CurPtr+16 <= BufferEnd) { 221831ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky int cmp = _mm_movemask_epi8(_mm_cmpeq_epi8(*(const __m128i*)CurPtr, 221931ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky Slashes)); 22203f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer if (cmp != 0) { 22216300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer // Adjust the pointer to point directly after the first slash. It's 22226300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer // not necessary to set C here, it will be overwritten at the end of 22236300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer // the outer loop. 22246300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer CurPtr += llvm::CountTrailingZeros_32(cmp) + 1; 22253f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer goto FoundSlash; 22263f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer } 22275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 16; 22283f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer } 22295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__ 22305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer __vector unsigned char Slashes = { 22311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump '/', '/', '/', '/', '/', '/', '/', '/', 22325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer '/', '/', '/', '/', '/', '/', '/', '/' 22335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer }; 22345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (CurPtr+16 <= BufferEnd && 22355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes)) 22365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 16; 22371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#else 22385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan for '/' quickly. Many block comments are very large. 22395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (CurPtr[0] != '/' && 22405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[1] != '/' && 22415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[2] != '/' && 22425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[3] != '/' && 22435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr+4 < BufferEnd) { 22445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 4; 22455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif 22471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It has to be one of the bytes scanned, increment to it and read one. 22495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 22505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Loop to scan the remainder. 22535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '/' && C != '\0') 22545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 22551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '/') { 22573f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer FoundSlash: 22585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[-2] == '*') // We found the final */. We're done! 22595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) { 22625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) { 22635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We found the final */, though it had an escaped newline between the 22645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // * and /. We're done! 22655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == '*' && CurPtr[1] != '/') { 22695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a /* inside of the comment, emit a warning. Don't do this 22705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // if this is a /*/, which will end the comment. This misses cases with 22715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // embedded escaped newlines, but oh well. 227274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 227374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::warn_nested_block_comment); 22745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == 0 && CurPtr == BufferEnd+1) { 22767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (!isLexingRawMode()) 227774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(BufferPtr, diag::err_unterminated_block_comment); 22785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Note: the user probably forgot a */. We could continue immediately 22795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // after the /*, but this would involve lexing a lot of what really is the 22805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // comment, which surely would confuse the parser. 228131f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner --CurPtr; 22821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 228331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // KeepWhitespaceMode should return this broken comment as a token. Since 228431f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // it isn't a well formed comment, just return it as an 'unknown' token. 228531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner if (isKeepWhitespaceMode()) { 22869e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 228731f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner return true; 228831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner } 22891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 229031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner BufferPtr = CurPtr; 22912d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 22927d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } else if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) { 22937d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 22947d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis cutOffLexing(); 22957d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return false; 22965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22977d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 22985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 22995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23013d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner // Notify comment handlers about the comment unless we're in a #if 0 block. 23023d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner if (PP && !isLexingRawMode() && 23033d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr), 23043d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner getSourceLocation(CurPtr)))) { 2305046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner BufferPtr = CurPtr; 2306046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return true; // A token has to be returned. 2307046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner } 23082e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 23095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are returning comments as tokens, return this comment as a token. 2310fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner if (inKeepCommentMode()) { 23119e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::comment); 23122d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return true; 23135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 23155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It is common for the tokens immediately after a /**/ comment to be 23165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // whitespace. Instead of going through the big switch, handle it 2317d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // efficiently now. This is safe even in KeepWhitespaceMode because we would 2318d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // have already returned above with the comment as a token. 23195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isHorizontalWhitespace(*CurPtr)) { 2320d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 23215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SkipWhitespace(Result, CurPtr+1); 23222d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 23235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 23255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, just return so that the next character will be lexed as a token. 23265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 2327d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 23282d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 23295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 23305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 23315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 23325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Lexing Entry Points 23335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 23345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 23355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ReadToEndOfLine - Read the rest of the current preprocessor line as an 23365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// uninterpreted string. This switches the lexer out of directive mode. 23373093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramervoid Lexer::ReadToEndOfLine(SmallVectorImpl<char> *Result) { 23385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(ParsingPreprocessorDirective && ParsingFilename == false && 23395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer "Must be in a preprocessing directive!"); 2340d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token Tmp; 23415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 23425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // CurPtr - Cache BufferPtr in an automatic variable. 23435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *CurPtr = BufferPtr; 23445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 23455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Char = getAndAdvanceChar(CurPtr, Tmp); 23465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Char) { 23475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 23483093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer if (Result) 23493093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer Result->push_back(Char); 23505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 23515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 0: // Null. 23525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Found end of file? 23535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr-1 != BufferEnd) { 23547d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (isCodeCompletionPoint(CurPtr-1)) { 23557d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 23567d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis cutOffLexing(); 23573093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer return; 23587d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 23597d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 23605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Nope, normal character, continue. 23613093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer if (Result) 23623093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer Result->push_back(Char); 23635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 23645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FALL THROUGH. 23665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\r': 23675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\n': 23685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Okay, we found the end of the line. First, back up past the \0, \r, \n. 23695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(CurPtr[-1] == Char && "Trigraphs for newline?"); 23705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr-1; 23711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 237284021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // Next, lex the character, which should handle the EOD transition. 23735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Lex(Tmp); 237455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor if (Tmp.is(tok::code_completion)) { 23757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (PP) 23767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 237755817afdf9d453a443262a733f6caf6692dca118Douglas Gregor Lex(Tmp); 237855817afdf9d453a443262a733f6caf6692dca118Douglas Gregor } 237984021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne assert(Tmp.is(tok::eod) && "Unexpected token!"); 23801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23813093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer // Finally, we're done; 23823093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer return; 23835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 23865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 23875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexEndOfFile - CurPtr points to the end of this file. Handle this 23885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// condition, reporting diagnostics and handling other edge cases as required. 23895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This returns true if Result contains a token, false if PP.Lex should be 23905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// called again. 2391d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) { 23925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we hit the end of the file while parsing a preprocessor directive, 23935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // end the preprocessor directive first. The next token returned will 23945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // then be the end of file. 23955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 23965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Done parsing the "line". 23975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParsingPreprocessorDirective = false; 23985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 239984021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne FormTokenWithChars(Result, CurPtr, tok::eod); 24001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore comment saving mode, in case it was disabled for directive. 2402f744d133e8d36eec65828d046ee695bda238125cChris Lattner SetCommentRetentionState(PP->getCommentRetentionState()); 24035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; // Have a token. 24041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 240586d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor 240686d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor // If we are in raw mode, return this event as an EOF token. Let the caller 240786d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor // that put us in raw mode handle the event. 240886d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor if (isLexingRawMode()) { 240986d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor Result.startToken(); 241086d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor BufferPtr = BufferEnd; 241186d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor FormTokenWithChars(Result, BufferEnd, tok::eof); 241286d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor return true; 241386d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor } 241481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor 2415f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor // Issue diagnostics for unterminated #if and missing newline. 2416f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor 24175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are in a #if directive, emit an error. 24185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (!ConditionalStack.empty()) { 24197d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (PP->getCodeCompletionFileLoc() != FileLoc) 24202d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor PP->Diag(ConditionalStack.back().IfLoc, 24212d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor diag::err_pp_unterminated_conditional); 24225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConditionalStack.pop_back(); 24235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 24241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2425b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue 2426b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner // a pedwarn. 24275e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) 24285e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell Diag(BufferEnd, LangOpts.CPlusPlus0x ? // C++11 [lex.phases] 2.2 p2 24295e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell diag::warn_cxx98_compat_no_newline_eof : diag::ext_no_newline_eof) 24305e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n"); 24311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 24335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 24345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Finally, let the preprocessor handle this. 24350cdd1fe3ec29b5cbff9a728966ace5c5b5d614f7Jordan Rose return PP->HandleEndOfFile(Result, isPragmaLexer()); 24365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 24375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 24385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from 24395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified lexer will return a tok::l_paren token, 0 if it is something 24405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// else and 2 if there are no more tokens in the buffer controlled by the 24415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexer. 24425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerunsigned Lexer::isNextPPTokenLParen() { 24435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?"); 24441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Switch to 'skipping' mode. This will ensure that we can lex a token 24465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // without emitting diagnostics, disables macro expansion, and will cause EOF 24475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // to return an EOF token instead of popping the include stack. 24485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexingRawMode = true; 24491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Save state that can be changed while lexing so that we can restore it. 24515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *TmpBufferPtr = BufferPtr; 2452a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner bool inPPDirectiveMode = ParsingPreprocessorDirective; 24531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2454d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token Tok; 24555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Tok.startToken(); 24565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexTokenInternal(Tok); 24571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore state that may have changed. 24595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = TmpBufferPtr; 2460a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner ParsingPreprocessorDirective = inPPDirectiveMode; 24611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore the lexer back to non-skipping mode. 24635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexingRawMode = false; 24641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 246522f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner if (Tok.is(tok::eof)) 24665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return 2; 246722f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner return Tok.is(tok::l_paren); 24685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 24695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2470ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// \brief Find the end of a version control conflict marker. 2471d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smithstatic const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd, 2472d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith ConflictMarkerKind CMK) { 2473d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith const char *Terminator = CMK == CMK_Perforce ? "<<<<\n" : ">>>>>>>"; 2474d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith size_t TermLen = CMK == CMK_Perforce ? 5 : 7; 2475d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith StringRef RestOfBuffer(CurPtr+TermLen, BufferEnd-CurPtr-TermLen); 2476d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith size_t Pos = RestOfBuffer.find(Terminator); 24775f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner while (Pos != StringRef::npos) { 247834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Must occur at start of line. 247934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (RestOfBuffer[Pos-1] != '\r' && 248034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner RestOfBuffer[Pos-1] != '\n') { 2481d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith RestOfBuffer = RestOfBuffer.substr(Pos+TermLen); 2482d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith Pos = RestOfBuffer.find(Terminator); 248334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner continue; 248434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 248534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return RestOfBuffer.data()+Pos; 248634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 248734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return 0; 248834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner} 248934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 249034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// IsStartOfConflictMarker - If the specified pointer is the start of a version 249134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// control conflict marker like '<<<<<<<', recognize it as such, emit an error 249234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// and recover nicely. This returns true if it is a conflict marker and false 249334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// if not. 249434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::IsStartOfConflictMarker(const char *CurPtr) { 249534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Only a conflict marker if it starts at the beginning of a line. 249634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr != BufferStart && 249734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr[-1] != '\n' && CurPtr[-1] != '\r') 249834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 249934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 2500d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // Check to see if we have <<<<<<< or >>>>. 2501d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if ((BufferEnd-CurPtr < 8 || StringRef(CurPtr, 7) != "<<<<<<<") && 2502d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith (BufferEnd-CurPtr < 6 || StringRef(CurPtr, 5) != ">>>> ")) 250334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 250434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 250534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If we have a situation where we don't care about conflict markers, ignore 250634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // it. 2507d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if (CurrentConflictMarkerState || isLexingRawMode()) 250834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 250934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 2510d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith ConflictMarkerKind Kind = *CurPtr == '<' ? CMK_Normal : CMK_Perforce; 2511d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith 2512d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // Check to see if there is an ending marker somewhere in the buffer at the 2513d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // start of a line to terminate this conflict marker. 2514d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if (FindConflictEnd(CurPtr, BufferEnd, Kind)) { 251534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // We found a match. We are really in a conflict marker. 251634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Diagnose this, and ignore to the end of line. 251734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Diag(CurPtr, diag::err_conflict_marker); 2518d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith CurrentConflictMarkerState = Kind; 251934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 252034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Skip ahead to the end of line. We know this exists because the 252134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // end-of-conflict marker starts with \r or \n. 252234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner while (*CurPtr != '\r' && *CurPtr != '\n') { 252334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner assert(CurPtr != BufferEnd && "Didn't find end of line"); 252434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner ++CurPtr; 252534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 252634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner BufferPtr = CurPtr; 252734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return true; 252834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 252934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 253034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // No end of conflict marker found. 253134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 253234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner} 253334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 253434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 2535d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// HandleEndOfConflictMarker - If this is a '====' or '||||' or '>>>>', or if 2536d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// it is '<<<<' and the conflict marker started with a '>>>>' marker, then it 2537d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// is the end of a conflict marker. Handle it by ignoring up until the end of 2538d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// the line. This returns true if it is a conflict marker and false if not. 253934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::HandleEndOfConflictMarker(const char *CurPtr) { 254034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Only a conflict marker if it starts at the beginning of a line. 254134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr != BufferStart && 254234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr[-1] != '\n' && CurPtr[-1] != '\r') 254334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 254434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 254534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If we have a situation where we don't care about conflict markers, ignore 254634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // it. 2547d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if (!CurrentConflictMarkerState || isLexingRawMode()) 254834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 254934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 2550d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // Check to see if we have the marker (4 characters in a row). 2551d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith for (unsigned i = 1; i != 4; ++i) 255234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr[i] != CurPtr[0]) 255334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 255434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 255534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If we do have it, search for the end of the conflict marker. This could 255634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // fail if it got skipped with a '#if 0' or something. Note that CurPtr might 255734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // be the end of conflict marker. 2558d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if (const char *End = FindConflictEnd(CurPtr, BufferEnd, 2559d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith CurrentConflictMarkerState)) { 256034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = End; 256134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 256234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Skip ahead to the end of line. 256334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n') 256434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner ++CurPtr; 256534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 256634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner BufferPtr = CurPtr; 256734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 256834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // No longer in the conflict marker. 2569d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith CurrentConflictMarkerState = CMK_None; 257034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return true; 257134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 257234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 257334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 257434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner} 257534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 25767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidisbool Lexer::isCodeCompletionPoint(const char *CurPtr) const { 25777d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (PP && PP->isCodeCompletionEnabled()) { 2578a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis SourceLocation Loc = FileLoc.getLocWithOffset(CurPtr-BufferStart); 25797d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return Loc == PP->getCodeCompletionLoc(); 25807d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 25817d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 25827d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return false; 25837d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis} 25847d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 25855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 25865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexTokenInternal - This implements a simple C family lexer. It is an 25875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// extremely performance critical piece of code. This assumes that the buffer 2588efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// has a null character at the end of the file. This returns a preprocessing 2589efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// token, not a normal token, as such, it is an internal interface. It assumes 2590efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// that the Flags of result have been cleared before calling this. 2591d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexTokenInternal(Token &Result) { 25925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerLexNextToken: 25935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // New token, can't need cleaning yet. 2594d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::NeedsCleaning); 25955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result.setIdentifierInfo(0); 25961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 25975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // CurPtr - Cache BufferPtr in an automatic variable. 25985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *CurPtr = BufferPtr; 25995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 26005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Small amounts of horizontal whitespace is very common between tokens. 26015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if ((*CurPtr == ' ') || (*CurPtr == '\t')) { 26025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 26035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while ((*CurPtr == ' ') || (*CurPtr == '\t')) 26045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 26051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2606d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // If we are keeping whitespace and other tokens, just return what we just 2607d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // skipped. The next lexer invocation will return the token after the 2608d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // whitespace. 2609d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (isKeepWhitespaceMode()) { 26109e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 2611d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; 2612d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner } 26131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 26145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 2615d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 26165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 26171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 26185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below. 26191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 26205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Read a character, advancing over it. 26215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Char = getAndAdvanceChar(CurPtr, Result); 26229e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner tok::TokenKind Kind; 26231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 26245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Char) { 26255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 0: // Null. 26265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Found end of file? 26275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr-1 == BufferEnd) { 26285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Read the PP instance variable into an automatic variable, because 26295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // LexEndOfFile will often delete 'this'. 2630168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner Preprocessor *PPCache = PP; 26315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. 26325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; // Got a token to return. 2633168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); 2634168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner return PPCache->Lex(Result); 26355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 26361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 26377d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis // Check if we are performing code completion. 26387d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (isCodeCompletionPoint(CurPtr-1)) { 26397d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis // Return the code-completion token. 26407d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis Result.startToken(); 26417d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis FormTokenWithChars(Result, CurPtr, tok::code_completion); 26427d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return; 26437d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 26447d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 264574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 264674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::null_in_file); 2647d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 2648d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 2649d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 26501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 26515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 2652a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner 2653a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner case 26: // DOS & CP/M EOF: "^Z". 2654a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // If we're in Microsoft extensions mode, treat this as end of file. 26554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.MicrosoftExt) { 2656a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // Read the PP instance variable into an automatic variable, because 2657a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // LexEndOfFile will often delete 'this'. 2658a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner Preprocessor *PPCache = PP; 2659a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. 2660a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner return; // Got a token to return. 2661a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); 2662a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner return PPCache->Lex(Result); 2663a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner } 2664a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // If Microsoft extensions are disabled, this is just random garbage. 2665a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner Kind = tok::unknown; 2666a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner break; 2667a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner 26685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\n': 26695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\r': 26705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are inside a preprocessor directive and we see the end of line, 267184021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // we know we are done with the directive, so return an EOD token. 26725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 26735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Done parsing the "line". 26745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParsingPreprocessorDirective = false; 26751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 26765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore comment saving mode, in case it was disabled for directive. 26771a8354659a6007bbae3b5d9161a56ecc8f61a219David Blaikie if (PP) 26788c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie SetCommentRetentionState(PP->getCommentRetentionState()); 26791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 26805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Since we consumed a newline, we are back at the start of a line. 26815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IsAtStartOfLine = true; 26821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 268384021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne Kind = tok::eod; 26845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 26855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 26865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The returned token is at the start of the line. 2687d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 26885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // No leading whitespace seen so far. 2689d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::LeadingSpace); 26901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2691d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 2692d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 26935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 26945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ' ': 26955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\t': 26965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\f': 26975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\v': 26988133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner SkipHorizontalWhitespace: 2699d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 2700d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 2701d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 27028133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner 27038133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner SkipIgnoredUnits: 27048133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner CurPtr = BufferPtr; 27051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27068133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner // If the next token is obviously a // or /* */ comment, skip it efficiently 27078133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner // too (without going through the big switch stmt). 27088402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() && 2709bb23628148f555a4cf71f98c27096a7a804c085cNico Weber LangOpts.LineComment && !LangOpts.TraditionalCPP) { 2710bb23628148f555a4cf71f98c27096a7a804c085cNico Weber if (SkipLineComment(Result, CurPtr+2)) 2711046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 27128133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipIgnoredUnits; 2713fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) { 2714046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner if (SkipBlockComment(Result, CurPtr+2)) 2715046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 27168133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipIgnoredUnits; 27178133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner } else if (isHorizontalWhitespace(*CurPtr)) { 27188133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipHorizontalWhitespace; 27198133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner } 27205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 2721a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner 27223a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // C99 6.4.4.1: Integer Constants. 27233a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // C99 6.4.4.2: Floating Constants. 27243a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '0': case '1': case '2': case '3': case '4': 27253a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '5': case '6': case '7': case '8': case '9': 27263a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Notify MIOpt that we read a non-whitespace/non-comment token. 27273a5707766850f9ee9daa35299794328b5caf96dcChris Lattner MIOpt.ReadToken(); 27283a5707766850f9ee9daa35299794328b5caf96dcChris Lattner return LexNumericConstant(Result, CurPtr); 27291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27305cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor case 'u': // Identifier (uber) or C++0x UTF-8 or UTF-16 string literal 27315cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // Notify MIOpt that we read a non-whitespace/non-comment token. 27325cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor MIOpt.ReadToken(); 27335cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 27344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.CPlusPlus0x) { 27355cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor Char = getCharAndSize(CurPtr, SizeTmp); 27365cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 27375cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // UTF-16 string literal 27385cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (Char == '"') 27395cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), 27405cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::utf16_string_literal); 27415cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 27425cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // UTF-16 character constant 27435cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (Char == '\'') 27445cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result), 27455cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::utf16_char_constant); 27465cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 27472fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // UTF-16 raw string literal 27482fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char == 'R' && getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"') 27492fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 27502fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 27512fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 27522fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::utf16_string_literal); 27532fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 27542fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char == '8') { 27552fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper char Char2 = getCharAndSize(CurPtr + SizeTmp, SizeTmp2); 27562fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 27572fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // UTF-8 string literal 27582fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char2 == '"') 27592fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexStringLiteral(Result, 27602fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 27612fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 27622fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::utf8_string_literal); 27632fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 27642fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char2 == 'R') { 27652fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper unsigned SizeTmp3; 27662fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper char Char3 = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3); 27672fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // UTF-8 raw string literal 27682fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char3 == '"') { 27692fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 27702fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 27712fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 27722fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp3, Result), 27732fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::utf8_string_literal); 27742fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 27752fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 27762fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 27775cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor } 27785cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 27795cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // treat u like the start of an identifier. 27805cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexIdentifier(Result, CurPtr); 27815cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 27825cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor case 'U': // Identifier (Uber) or C++0x UTF-32 string literal 27835cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // Notify MIOpt that we read a non-whitespace/non-comment token. 27845cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor MIOpt.ReadToken(); 27855cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 27864e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.CPlusPlus0x) { 27875cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor Char = getCharAndSize(CurPtr, SizeTmp); 27885cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 27895cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // UTF-32 string literal 27905cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (Char == '"') 27915cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), 27925cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::utf32_string_literal); 27935cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 27945cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // UTF-32 character constant 27955cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (Char == '\'') 27965cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result), 27975cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::utf32_char_constant); 27982fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 27992fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // UTF-32 raw string literal 28002fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char == 'R' && getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"') 28012fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 28022fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 28032fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 28042fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::utf32_string_literal); 28055cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor } 28065cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 28075cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // treat U like the start of an identifier. 28085cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexIdentifier(Result, CurPtr); 28095cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 28102fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper case 'R': // Identifier or C++0x raw string literal 28112fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Notify MIOpt that we read a non-whitespace/non-comment token. 28122fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper MIOpt.ReadToken(); 28132fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 28144e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.CPlusPlus0x) { 28152fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Char = getCharAndSize(CurPtr, SizeTmp); 28162fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 28172fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char == '"') 28182fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 28192fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(CurPtr, SizeTmp, Result), 28202fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::string_literal); 28212fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 28222fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 28232fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // treat R like the start of an identifier. 28242fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexIdentifier(Result, CurPtr); 28252fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 28263a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz"). 28275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 28285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 28295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 28305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 28315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Wide string literal. 28325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '"') 28335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), 28345cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::wide_string_literal); 28355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 28362fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Wide raw string literal. 28374e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.CPlusPlus0x && Char == 'R' && 28382fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"') 28392fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 28402fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 28412fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 28422fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::wide_string_literal); 28432fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 28445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Wide character constant. 28455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '\'') 28465cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result), 28475cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::wide_char_constant); 28485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FALL THROUGH, treating L like the start of an identifier. 28491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 28505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.2: Identifiers. 28515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': 28525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N': 28532fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper case 'O': case 'P': case 'Q': /*'R'*/case 'S': case 'T': /*'U'*/ 28545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'V': case 'W': case 'X': case 'Y': case 'Z': 28555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': 28565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': 28575cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor case 'o': case 'p': case 'q': case 'r': case 's': case 't': /*'u'*/ 28585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'v': case 'w': case 'x': case 'y': case 'z': 28595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '_': 28605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 28615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 28625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexIdentifier(Result, CurPtr); 28633a5707766850f9ee9daa35299794328b5caf96dcChris Lattner 28643a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '$': // $ in identifiers. 28654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.DollarIdents) { 286674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 286774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::ext_dollar_in_identifier); 28683a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Notify MIOpt that we read a non-whitespace/non-comment token. 28693a5707766850f9ee9daa35299794328b5caf96dcChris Lattner MIOpt.ReadToken(); 28703a5707766850f9ee9daa35299794328b5caf96dcChris Lattner return LexIdentifier(Result, CurPtr); 28713a5707766850f9ee9daa35299794328b5caf96dcChris Lattner } 28721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 28739e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 28743a5707766850f9ee9daa35299794328b5caf96dcChris Lattner break; 28751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 28765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.4: Character Constants. 28775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\'': 28785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 28795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 28805cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexCharConstant(Result, CurPtr, tok::char_constant); 28815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 28825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.5: String Literals. 28835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '"': 28845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 28855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 28865cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexStringLiteral(Result, CurPtr, tok::string_literal); 28875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 28885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.6: Punctuators. 28895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '?': 28909e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::question; 28915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 28925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '[': 28939e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_square; 28945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 28955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ']': 28969e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_square; 28975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 28985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '(': 28999e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_paren; 29005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 29015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ')': 29029e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_paren; 29035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 29045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '{': 29059e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_brace; 29065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 29075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '}': 29089e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_brace; 29095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 29105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '.': 29115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 29125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char >= '0' && Char <= '9') { 29135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 29145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 29155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 29165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); 29174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.CPlusPlus && Char == '*') { 29189e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::periodstar; 29195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += SizeTmp; 29205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '.' && 29215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') { 29229e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ellipsis; 29235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 29245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 29255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 29269e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::period; 29275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 29285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 29295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '&': 29305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 29315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '&') { 29329e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ampamp; 29335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 29345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 29359e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ampequal; 29365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 29375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 29389e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::amp; 29395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 29405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 29411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case '*': 29425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (getCharAndSize(CurPtr, SizeTmp) == '=') { 29439e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::starequal; 29445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 29455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 29469e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::star; 29475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 29485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 29495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '+': 29505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 29515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '+') { 29525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 29539e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plusplus; 29545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 29555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 29569e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plusequal; 29575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 29589e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plus; 29595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 29605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 29615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '-': 29625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 29639e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner if (Char == '-') { // -- 29645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 29659e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minusminus; 29664e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (Char == '>' && LangOpts.CPlusPlus && 29679e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->* 29685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 29695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 29709e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::arrowstar; 29719e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner } else if (Char == '>') { // -> 29725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 29739e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::arrow; 29749e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner } else if (Char == '=') { // -= 29755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 29769e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minusequal; 29775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 29789e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minus; 29795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 29805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 29815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '~': 29829e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::tilde; 29835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 29845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '!': 29855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (getCharAndSize(CurPtr, SizeTmp) == '=') { 29869e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::exclaimequal; 29875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 29885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 29899e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::exclaim; 29905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 29915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 29925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '/': 29935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // 6.4.9: Comments 29945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 2995bb23628148f555a4cf71f98c27096a7a804c085cNico Weber if (Char == '/') { // Line comment. 2996bb23628148f555a4cf71f98c27096a7a804c085cNico Weber // Even if Line comments are disabled (e.g. in C89 mode), we generally 29978402c73dd880e8af46c826d873681820aebe32ebChris Lattner // want to lex this as a comment. There is one problem with this though, 29988402c73dd880e8af46c826d873681820aebe32ebChris Lattner // that in one particular corner case, this can change the behavior of the 29998402c73dd880e8af46c826d873681820aebe32ebChris Lattner // resultant program. For example, In "foo //**/ bar", C89 would lex 3000bb23628148f555a4cf71f98c27096a7a804c085cNico Weber // this as "foo / bar" and langauges with Line comments would lex it as 30018402c73dd880e8af46c826d873681820aebe32ebChris Lattner // "foo". Check to see if the character after the second slash is a '*'. 30028402c73dd880e8af46c826d873681820aebe32ebChris Lattner // If so, we will lex that as a "/" instead of the start of a comment. 30032ed42287b9b88a0bb08ab10e95e306da3f294ba9Daniel Dunbar // However, we never do this in -traditional-cpp mode. 3004bb23628148f555a4cf71f98c27096a7a804c085cNico Weber if ((LangOpts.LineComment || 30052ed42287b9b88a0bb08ab10e95e306da3f294ba9Daniel Dunbar getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') && 30064e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie !LangOpts.TraditionalCPP) { 3007bb23628148f555a4cf71f98c27096a7a804c085cNico Weber if (SkipLineComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) 3008046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 30091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 30108402c73dd880e8af46c826d873681820aebe32ebChris Lattner // It is common for the tokens immediately after a // comment to be 30118402c73dd880e8af46c826d873681820aebe32ebChris Lattner // whitespace (indentation for the next line). Instead of going through 30128402c73dd880e8af46c826d873681820aebe32ebChris Lattner // the big switch, handle it efficiently now. 30138402c73dd880e8af46c826d873681820aebe32ebChris Lattner goto SkipIgnoredUnits; 30148402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 30158402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 30161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 30178402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (Char == '*') { // /**/ comment. 30185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) 3019046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 30202d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner goto LexNextToken; // GCC isn't tail call eliminating. 30218402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 30221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 30238402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (Char == '=') { 30245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30259e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::slashequal; 30265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 30279e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::slash; 30285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 30295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '%': 30315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 30325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 30339e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::percentequal; 30345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30354e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.Digraphs && Char == '>') { 30369e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_brace; // '%>' -> '}' 30375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30384e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.Digraphs && Char == ':') { 30395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 30415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') { 30429e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashhash; // '%:%:' -> '##' 30435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 30445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 30454e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (Char == '@' && LangOpts.MicrosoftExt) {// %:@ -> #@ -> Charize 30465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 304774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 304866d5ce11b9426f6a59f61a03cbd8dbf047cc9350Ted Kremenek Diag(BufferPtr, diag::ext_charize_microsoft); 30499e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashat; 3050e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner } else { // '%:' -> '#' 30515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We parsed a # character. If this occurs at the start of the line, 30525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it's actually the start of a preprocessing directive. Callback to 30535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the preprocessor to handle it. 30545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: -fpreprocessed mode?? 30553185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) 30563185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis goto HandleDirective; 30571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3058e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner Kind = tok::hash; 30595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 30605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 30619e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::percent; 30625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 30635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '<': 30655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 30665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingFilename) { 30679cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner return LexAngledStringLiteral(Result, CurPtr); 30685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '<') { 306934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2); 307034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (After == '=') { 307134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::lesslessequal; 307234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 307334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner SizeTmp2, Result); 307434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) { 307534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If this is actually a '<<<<<<<' version control conflict marker, 307634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // recognize it as such and recover nicely. 307734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 3078d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith } else if (After == '<' && HandleEndOfConflictMarker(CurPtr-1)) { 3079d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // If this is '<<<<' and we're in a Perforce-style conflict marker, 3080d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // ignore it. 3081d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith goto LexNextToken; 30824e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.CUDA && After == '<') { 30831b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne Kind = tok::lesslessless; 30841b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 30851b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne SizeTmp2, Result); 308634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else { 308734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 308834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::lessless; 308934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 30905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 30915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30929e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::lessequal; 30934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.Digraphs && Char == ':') { // '<:' -> '[' 30944e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.CPlusPlus0x && 309587a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == ':') { 309687a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // C++0x [lex.pptoken]p3: 309787a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // Otherwise, if the next three characters are <:: and the subsequent 309887a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // character is neither : nor >, the < is treated as a preprocessor 309987a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // token by itself and not as the first character of the alternative 310087a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // token <:. 310187a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith unsigned SizeTmp3; 310287a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith char After = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3); 310387a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith if (After != ':' && After != '>') { 310487a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith Kind = tok::less; 3105661a99690bc133bbaa029da925481d4a860dec90Richard Smith if (!isLexingRawMode()) 3106661a99690bc133bbaa029da925481d4a860dec90Richard Smith Diag(BufferPtr, diag::warn_cxx98_compat_less_colon_colon); 310787a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith break; 310887a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith } 310987a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith } 311087a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith 31115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31129e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_square; 31134e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.Digraphs && Char == '%') { // '<%' -> '{' 31145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31159e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_brace; 31165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 31179e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::less; 31185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 31195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 31205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '>': 31215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 31225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 31235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31249e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::greaterequal; 31255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '>') { 312634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2); 312734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (After == '=') { 312834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 312934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner SizeTmp2, Result); 313034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::greatergreaterequal; 3131d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith } else if (After == '>' && IsStartOfConflictMarker(CurPtr-1)) { 3132d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // If this is actually a '>>>>' conflict marker, recognize it as such 3133d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // and recover nicely. 3134d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith goto LexNextToken; 313534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) { 313634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If this is '>>>>>>>' and we're in a conflict marker, ignore it. 313734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 31384e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.CUDA && After == '>') { 31391b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne Kind = tok::greatergreatergreater; 31401b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 31411b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne SizeTmp2, Result); 314234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else { 314334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 314434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::greatergreater; 314534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 314634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 31475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 31489e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::greater; 31495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 31505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 31515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '^': 31525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 31535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 31545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31559e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::caretequal; 31565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 31579e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::caret; 31585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 31595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 31605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '|': 31615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 31625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 31639e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipeequal; 31645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '|') { 316634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If this is '|||||||' and we're in a conflict marker, ignore it. 316734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1)) 316834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 31699e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipepipe; 31705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 31729e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipe; 31735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 31745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 31755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ':': 31765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 31774e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.Digraphs && Char == '>') { 31789e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_square; // ':>' -> ']' 31795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.CPlusPlus && Char == ':') { 31819e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::coloncolon; 31825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else { 31849e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::colon; 31855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 31865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 31875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ';': 31889e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::semi; 31895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 31905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '=': 31915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 31925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 3193d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // If this is '====' and we're in a conflict marker, ignore it. 319434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1)) 319534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 319634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 31979e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::equalequal; 31985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else { 32009e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::equal; 32015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 32025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 32035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ',': 32049e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::comma; 32055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 32065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '#': 32075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 32085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '#') { 32099e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashhash; 32105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (Char == '@' && LangOpts.MicrosoftExt) { // #@ -> Charize 32129e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashat; 321374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 321466d5ce11b9426f6a59f61a03cbd8dbf047cc9350Ted Kremenek Diag(BufferPtr, diag::ext_charize_microsoft); 32155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 32175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We parsed a # character. If this occurs at the start of the line, 32185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it's actually the start of a preprocessing directive. Callback to 32195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the preprocessor to handle it. 32205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: -fpreprocessed mode?? 32213185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) 32223185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis goto HandleDirective; 32231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3224e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner Kind = tok::hash; 32255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 32265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 32275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 32283a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '@': 32293a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Objective C support. 32304e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CurPtr[-1] == '@' && LangOpts.ObjC1) 32319e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::at; 32323a5707766850f9ee9daa35299794328b5caf96dcChris Lattner else 32339e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 32343a5707766850f9ee9daa35299794328b5caf96dcChris Lattner break; 32351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 32365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\\': 32375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: UCN's. 32385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FALL THROUGH. 32395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 32409e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 32415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 32425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 32431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 32445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 32455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 32465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 32475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 32489e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, Kind); 32493185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis return; 32503185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis 32513185d4ac30378995ef70421e2848f77524c2b5d5Argyrios KyrtzidisHandleDirective: 32523185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // We parsed a # character and it's the start of a preprocessing directive. 32533185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis 32543185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis FormTokenWithChars(Result, CurPtr, tok::hash); 32553185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis PP->HandleDirective(Result); 32563185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis 32573185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // As an optimization, if the preprocessor didn't switch lexers, tail 32583185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // recurse. 32593185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis if (PP->isCurrentLexer(this)) { 32603185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // Start a new token. If this is a #include or something, the PP may 32613185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // want us starting at the beginning of the line again. If so, set 32623185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // the StartOfLine flag and clear LeadingSpace. 32633185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis if (IsAtStartOfLine) { 32643185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis Result.setFlag(Token::StartOfLine); 32653185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis Result.clearFlag(Token::LeadingSpace); 32663185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis IsAtStartOfLine = false; 32673185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis } 32683185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis goto LexNextToken; // GCC isn't tail call eliminating. 32693185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis } 32703185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis return PP->Lex(Result); 32715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3272