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" 289893902eceba7f01dd1521349d33866f77254d78Jordan Rose#include "clang/Basic/CharInfo.h" 299dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner#include "clang/Basic/SourceManager.h" 3055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/CodeCompletionHandler.h" 3155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/LexDiagnostic.h" 3255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/Preprocessor.h" 33d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis#include "llvm/ADT/STLExtras.h" 34c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose#include "llvm/ADT/StringExtras.h" 3555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/ADT/StringSwitch.h" 36409a03671224d4e5bdab1594c43baf070148f830Chris Lattner#include "llvm/Support/Compiler.h" 37cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko#include "llvm/Support/ConvertUTF.h" 385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Support/MemoryBuffer.h" 39ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose#include "UnicodeCharSets.h" 402fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper#include <cstring> 415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 43dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 44dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner// Token Class Implementation 45dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 46dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier. 48dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattnerbool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const { 49bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor if (IdentifierInfo *II = getIdentifierInfo()) 50bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor return II->getObjCKeywordID() == objcKey; 51bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor return false; 52dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner} 53dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 54dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner/// getObjCKeywordID - Return the ObjC keyword kind. 55dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattnertok::ObjCKeywordKind Token::getObjCKeywordID() const { 56dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner IdentifierInfo *specId = getIdentifierInfo(); 57dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword; 58dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner} 59dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 6053702cd401b8fdca985aede7732c2f6a82ad9b1cChris Lattner 61dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 62dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner// Lexer Class Implementation 63dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 64dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 6599ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid Lexer::anchor() { } 6699ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie 671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Lexer::InitLexer(const char *BufStart, const char *BufPtr, 6822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner const char *BufEnd) { 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 1256aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose resetExtendedTokenMode(); 1266aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose} 1276aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose 1286aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rosevoid Lexer::resetExtendedTokenMode() { 1296aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose assert(PP && "Cannot reset token mode without a preprocessor"); 1306aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose if (LangOpts.TraditionalCPP) 1316aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose SetKeepWhitespaceMode(true); 1326aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose else 1336aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose SetCommentRetentionState(PP->getCommentRetentionState()); 1340770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner} 135dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 136168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner/// Lexer constructor - Create a new raw lexer object. This object is only 137092bf67e5ca560d2fc6aa70be1f172b8b3a5ff96Dmitri Gribenko/// suitable for calls to 'LexFromRawLexer'. This lexer assumes that the text 138590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner/// range will outlive it, so it doesn't take ownership of it. 1394e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid BlaikieLexer::Lexer(SourceLocation fileloc, const LangOptions &langOpts, 140de96c0f29c4cacabe6ea577c61db87c2a85aea6cChris Lattner const char *BufStart, const char *BufPtr, const char *BufEnd) 1414e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie : FileLoc(fileloc), LangOpts(langOpts) { 14222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner 14322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner InitLexer(BufStart, BufPtr, BufEnd); 1441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 145168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner // We *are* in raw mode. 146168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner LexingRawMode = true; 1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 149025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// Lexer constructor - Create a new raw lexer object. This object is only 150092bf67e5ca560d2fc6aa70be1f172b8b3a5ff96Dmitri Gribenko/// suitable for calls to 'LexFromRawLexer'. This lexer assumes that the text 151025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// range will outlive it, so it doesn't take ownership of it. 1526e2901407bff59aeb4cc301cc58b034723d0eb49Chris LattnerLexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile, 1534e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const SourceManager &SM, const LangOptions &langOpts) 1544e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie : FileLoc(SM.getLocForStartOfFile(FID)), LangOpts(langOpts) { 155025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner 1561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(), 157025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner FromFile->getBufferEnd()); 1581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 159025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner // We *are* in raw mode. 160025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner LexingRawMode = true; 161025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner} 162025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner 16342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for 16442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// _Pragma expansion. This has a variety of magic semantics that this method 16542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// sets up. It returns a new'd Lexer that must be delete'd when done. 16642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 16742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// On entrance to this routine, TokStartLoc is a macro location which has a 16842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// spelling loc that indicates the bytes to be lexed for the token and an 169433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// expansion location that indicates where all lexed tokens should be 17042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// "expanded from". 17142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 17242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// FIXME: It would really be nice to make _Pragma just be a wrapper around a 17342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// normal lexer that remaps tokens as they fly by. This would require making 17442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer 17542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// interface that could handle this stuff. This would pull GetMappedTokenLoc 17642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// out of the critical path of the lexer! 17742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 1781eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpLexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc, 179433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation ExpansionLocStart, 180433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation ExpansionLocEnd, 181bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner unsigned TokLen, Preprocessor &PP) { 18242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner SourceManager &SM = PP.getSourceManager(); 18342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 18442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Create the lexer as if we were going to lex the file normally. 185a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner FileID SpellingFID = SM.getFileID(SpellingLoc); 1866e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID); 1876e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner Lexer *L = new Lexer(SpellingFID, InputFile, PP); 1881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Now that the lexer is created, change the start/end locations so that we 19042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // just lex the subsection of the file that we want. This is lexing from a 19142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // scratch buffer. 19242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner const char *StrData = SM.getCharacterData(SpellingLoc); 1931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->BufferPtr = StrData; 19542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->BufferEnd = StrData+TokLen; 1961fa495304c81e03f07f278a47b5efe9317104aabChris Lattner assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!"); 19742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 19842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Set the SourceLocation with the remapping information. This ensures that 19942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // GetMappedTokenLoc will remap the tokens as they are lexed. 200bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth L->FileLoc = SM.createExpansionLoc(SM.getLocForStartOfFile(SpellingFID), 201bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth ExpansionLocStart, 202bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth ExpansionLocEnd, TokLen); 2031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Ensure that the lexer thinks it is inside a directive, so that end \n will 20584021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // return an EOD token. 20642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->ParsingPreprocessorDirective = true; 2071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // This lexer really is for _Pragma. 20942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->Is_PragmaLexer = true; 21042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner return L; 21142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner} 21242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 213168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner 2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Stringify - Convert the specified string into a C string, with surrounding 2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ""'s, and with escaped \ and " characters. 2165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstd::string Lexer::Stringify(const std::string &Str, bool Charify) { 2175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer std::string Result = Str; 2185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Quote = Charify ? '\'' : '"'; 2195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = 0, e = Result.size(); i != e; ++i) { 2205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Result[i] == '\\' || Result[i] == Quote) { 2215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result.insert(Result.begin()+i, '\\'); 2225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++i; ++e; 2235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return Result; 2265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 228d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// Stringify - Convert the specified string into a C string by escaping '\' 229d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// and " characters. This does not add surrounding ""'s to the string. 2305f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid Lexer::Stringify(SmallVectorImpl<char> &Str) { 231d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner for (unsigned i = 0, e = Str.size(); i != e; ++i) { 232d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner if (Str[i] == '\\' || Str[i] == '"') { 233d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner Str.insert(Str.begin()+i, '\\'); 234d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner ++i; ++e; 235d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner } 236d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner } 237d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner} 238d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner 239b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner//===----------------------------------------------------------------------===// 240b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner// Token Spelling 241b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner//===----------------------------------------------------------------------===// 242b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 24330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith/// \brief Slow case of getSpelling. Extract the characters comprising the 24430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith/// spelling of this token from the provided input buffer. 24530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smithstatic size_t getSpellingSlow(const Token &Tok, const char *BufPtr, 24630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const LangOptions &LangOpts, char *Spelling) { 24730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith assert(Tok.needsCleaning() && "getSpellingSlow called on simple token"); 24830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 24930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith size_t Length = 0; 25030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const char *BufEnd = BufPtr + Tok.getLength(); 25130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 25230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith if (Tok.is(tok::string_literal)) { 25330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Munch the encoding-prefix and opening double-quote. 25430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith while (BufPtr < BufEnd) { 25530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith unsigned Size; 25630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Spelling[Length++] = Lexer::getCharAndSizeNoWarn(BufPtr, Size, LangOpts); 25730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith BufPtr += Size; 25830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 25930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith if (Spelling[Length - 1] == '"') 26030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith break; 26130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith } 26230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 26330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Raw string literals need special handling; trigraph expansion and line 26430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // splicing do not occur within their d-char-sequence nor within their 26530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // r-char-sequence. 26630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith if (Length >= 2 && 26730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Spelling[Length - 2] == 'R' && Spelling[Length - 1] == '"') { 26830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Search backwards from the end of the token to find the matching closing 26930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // quote. 27030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const char *RawEnd = BufEnd; 27130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith do --RawEnd; while (*RawEnd != '"'); 27230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith size_t RawLength = RawEnd - BufPtr + 1; 27330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 27430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Everything between the quotes is included verbatim in the spelling. 27530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith memcpy(Spelling + Length, BufPtr, RawLength); 27630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Length += RawLength; 27730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith BufPtr += RawLength; 27830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 27930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // The rest of the token is lexed normally. 28030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith } 28130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith } 28230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 28330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith while (BufPtr < BufEnd) { 28430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith unsigned Size; 28530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Spelling[Length++] = Lexer::getCharAndSizeNoWarn(BufPtr, Size, LangOpts); 28630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith BufPtr += Size; 28730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith } 28830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 28930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith assert(Length < Tok.getLength() && 29030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith "NeedsCleaning flag set on token that didn't need cleaning!"); 29130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith return Length; 29230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith} 29330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 294b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// getSpelling() - Return the 'spelling' of this token. The spelling of a 295b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// token are the characters used to represent the token in the source file 296b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// after trigraph expansion and escaped-newline folding. In particular, this 297b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// wants to get the true, uncanonicalized, spelling of things like digraphs 298b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// UCNs, etc. 2995f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerStringRef Lexer::getSpelling(SourceLocation loc, 30030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith SmallVectorImpl<char> &buffer, 30130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const SourceManager &SM, 30230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const LangOptions &options, 30330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith bool *invalid) { 304834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Break down the source location. 305834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc); 306834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 307834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Try to the load the file buffer. 308834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall bool invalidTemp = false; 3095f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); 310834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall if (invalidTemp) { 311834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall if (invalid) *invalid = true; 3125f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return StringRef(); 313834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall } 314834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 315834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall const char *tokenBegin = file.data() + locInfo.second; 316834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 317834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Lex from the start of the given location. 318834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall Lexer lexer(SM.getLocForStartOfFile(locInfo.first), options, 319834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall file.begin(), tokenBegin, file.end()); 320834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall Token token; 321834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall lexer.LexFromRawLexer(token); 322834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 323834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall unsigned length = token.getLength(); 324834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 325834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Common case: no need for cleaning. 326834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall if (!token.needsCleaning()) 3275f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return StringRef(tokenBegin, length); 328834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 32930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Hard case, we need to relex the characters into the string. 33030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith buffer.resize(length); 33130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith buffer.resize(getSpellingSlow(token, tokenBegin, options, buffer.data())); 3325f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return StringRef(buffer.data(), buffer.size()); 333834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall} 334834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 335834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// getSpelling() - Return the 'spelling' of this token. The spelling of a 336834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// token are the characters used to represent the token in the source file 337834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// after trigraph expansion and escaped-newline folding. In particular, this 338834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// wants to get the true, uncanonicalized, spelling of things like digraphs 339834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// UCNs, etc. 340b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattnerstd::string Lexer::getSpelling(const Token &Tok, const SourceManager &SourceMgr, 3414e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts, bool *Invalid) { 342b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); 34330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 344b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner bool CharDataInvalid = false; 34530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation(), 346b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner &CharDataInvalid); 347b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (Invalid) 348b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner *Invalid = CharDataInvalid; 349b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (CharDataInvalid) 350b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return std::string(); 35130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 35230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // If this token contains nothing interesting, return it directly. 353b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (!Tok.needsCleaning()) 35430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith return std::string(TokStart, TokStart + Tok.getLength()); 35530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 356b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner std::string Result; 35730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Result.resize(Tok.getLength()); 35830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Result.resize(getSpellingSlow(Tok, TokStart, LangOpts, &*Result.begin())); 359b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return Result; 360b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner} 361b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 362b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// getSpelling - This method is used to get the spelling of a token into a 363b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// preallocated buffer, instead of as an std::string. The caller is required 364b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// to allocate enough space for the token, which is guaranteed to be at least 365b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// Tok.getLength() bytes long. The actual length of the token is returned. 366b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// 367b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// Note that this method may do two possible things: it may either fill in 368b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// the buffer specified with characters, or it may *change the input pointer* 369b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// to point to a constant buffer with the data already in it (avoiding a 370b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// copy). The caller is not allowed to modify the returned buffer pointer 371b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// if an internal buffer is returned. 372b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattnerunsigned Lexer::getSpelling(const Token &Tok, const char *&Buffer, 373b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner const SourceManager &SourceMgr, 3744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts, bool *Invalid) { 375b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); 376c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 377c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara const char *TokStart = 0; 378c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // NOTE: this has to be checked *before* testing for an IdentifierInfo. 379c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara if (Tok.is(tok::raw_identifier)) 380c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara TokStart = Tok.getRawIdentifierData(); 381c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose else if (!Tok.hasUCN()) { 382c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { 383c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // Just return the string from the identifier table, which is very quick. 384c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Buffer = II->getNameStart(); 385c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return II->getLength(); 386c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 387b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 388c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 389c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // NOTE: this can be checked even after testing for an IdentifierInfo. 390b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (Tok.isLiteral()) 391b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner TokStart = Tok.getLiteralData(); 392c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 393b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (TokStart == 0) { 394c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // Compute the start of the token in the input lexer buffer. 395b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner bool CharDataInvalid = false; 396b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner TokStart = SourceMgr.getCharacterData(Tok.getLocation(), &CharDataInvalid); 397b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (Invalid) 398b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner *Invalid = CharDataInvalid; 399b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (CharDataInvalid) { 400b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Buffer = ""; 401b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return 0; 402b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 403b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 404c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 405b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner // If this token contains nothing interesting, return it directly. 406b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (!Tok.needsCleaning()) { 407b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Buffer = TokStart; 408b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return Tok.getLength(); 409b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 410c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 411b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner // Otherwise, hard case, relex the characters into the string. 41230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith return getSpellingSlow(Tok, TokStart, LangOpts, const_cast<char*>(Buffer)); 413b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner} 414b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 415b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 4169a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// MeasureTokenLength - Relex the token at the specified location and return 4179a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// its length in bytes in the input file. If the token needs cleaning (e.g. 4189a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// includes a trigraph or an escaped newline) then this count includes bytes 4199a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// that are part of that. 4209a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattnerunsigned Lexer::MeasureTokenLength(SourceLocation Loc, 4212c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner const SourceManager &SM, 4222c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner const LangOptions &LangOpts) { 423d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis Token TheTok; 424d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis if (getRawToken(Loc, TheTok, SM, LangOpts)) 425d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis return 0; 426d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis return TheTok.getLength(); 427d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis} 428d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis 429d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis/// \brief Relex the token at the specified location. 430d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis/// \returns true if there was a failure, false on success. 431d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidisbool Lexer::getRawToken(SourceLocation Loc, Token &Result, 432d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis const SourceManager &SM, 433d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis const LangOptions &LangOpts) { 4349a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // TODO: this could be special cased for common tokens like identifiers, ')', 4359a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // etc to make this faster, if it mattered. Just look at StrData[0] to handle 4361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // all obviously single-char tokens. This could use 4379a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // Lexer::isObviouslySimpleCharacter for example to handle identifiers or 4389a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // something. 439de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 440de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // If this comes from a macro expansion, we really do want the macro name, not 441de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // the token this macro expanded to. 442402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth Loc = SM.getExpansionLoc(Loc); 443363fdc29656cc03c1817268888f95e6343470aa8Chris Lattner std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 444f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor bool Invalid = false; 4455f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); 446f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor if (Invalid) 447d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis return true; 448f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer 449f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer const char *StrData = Buffer.data()+LocInfo.second; 4508350394c65b81bba3986dfe44ae17423873741deChris Lattner 45133e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor if (isWhitespace(StrData[0])) 452d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis return true; 45333e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor 4549a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // Create a lexer starting at the beginning of this token. 455c3526d89ef9c31639ec8b25180cfb22354344241Sebastian Redl Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, 456c3526d89ef9c31639ec8b25180cfb22354344241Sebastian Redl Buffer.begin(), StrData, Buffer.end()); 45739de7409bffb6b725a8aa64f0ba77ab51e8c9eb3Chris Lattner TheLexer.SetCommentRetentionState(true); 458d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis TheLexer.LexFromRawLexer(Result); 459d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis return false; 4609a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner} 4619a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner 4620e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidisstatic SourceLocation getBeginningOfFileToken(SourceLocation Loc, 4630e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis const SourceManager &SM, 4640e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis const LangOptions &LangOpts) { 4650e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis assert(Loc.isFileID()); 466a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 4673de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor if (LocInfo.first.isInvalid()) 4683de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor return Loc; 4693de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor 470a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor bool Invalid = false; 4715f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); 472a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (Invalid) 473a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return Loc; 474a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 475a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Back up from the current location until we hit the beginning of a line 476a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // (or the buffer). We'll relex from that point. 477a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor const char *BufStart = Buffer.data(); 4783de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor if (LocInfo.second >= Buffer.size()) 4793de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor return Loc; 4803de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor 481a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor const char *StrData = BufStart+LocInfo.second; 482a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (StrData[0] == '\n' || StrData[0] == '\r') 483a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return Loc; 484a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 485a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor const char *LexStart = StrData; 486a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor while (LexStart != BufStart) { 487a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (LexStart[0] == '\n' || LexStart[0] == '\r') { 488a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor ++LexStart; 489a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor break; 490a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } 491a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 492a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor --LexStart; 493a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } 494a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 495a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Create a lexer starting at the beginning of this token. 496a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis SourceLocation LexerStartLoc = Loc.getLocWithOffset(-LocInfo.second); 497a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor Lexer TheLexer(LexerStartLoc, LangOpts, BufStart, LexStart, Buffer.end()); 498a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor TheLexer.SetCommentRetentionState(true); 499a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 500a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Lex tokens until we find the token that contains the source location. 501a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor Token TheTok; 502a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor do { 503a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor TheLexer.LexFromRawLexer(TheTok); 504a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 505a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (TheLexer.getBufferLocation() > StrData) { 506a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Lexing this token has taken the lexer past the source location we're 507a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // looking for. If the current token encompasses our source location, 508a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // return the beginning of that token. 509a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (TheLexer.getBufferLocation() - TheTok.getLength() <= StrData) 510a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return TheTok.getLocation(); 511a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 512a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // We ended up skipping over the source location entirely, which means 513a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // that it points into whitespace. We're done here. 514a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor break; 515a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } 516a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } while (TheTok.getKind() != tok::eof); 517a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 518a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // We've passed our source location; just return the original source location. 519a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return Loc; 520a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor} 521a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 5220e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios KyrtzidisSourceLocation Lexer::GetBeginningOfToken(SourceLocation Loc, 5230e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis const SourceManager &SM, 5240e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis const LangOptions &LangOpts) { 5250e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis if (Loc.isFileID()) 5260e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis return getBeginningOfFileToken(Loc, SM, LangOpts); 5270e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis 5280e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis if (!SM.isMacroArgExpansion(Loc)) 5290e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis return Loc; 5300e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis 5310e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis SourceLocation FileLoc = SM.getSpellingLoc(Loc); 5320e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis SourceLocation BeginFileLoc = getBeginningOfFileToken(FileLoc, SM, LangOpts); 5330e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis std::pair<FileID, unsigned> FileLocInfo = SM.getDecomposedLoc(FileLoc); 534ae9f85b2c062ac20512a484cfa6e66239dd3d0d9Chandler Carruth std::pair<FileID, unsigned> BeginFileLocInfo 535ae9f85b2c062ac20512a484cfa6e66239dd3d0d9Chandler Carruth = SM.getDecomposedLoc(BeginFileLoc); 5360e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis assert(FileLocInfo.first == BeginFileLocInfo.first && 5370e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis FileLocInfo.second >= BeginFileLocInfo.second); 538ae9f85b2c062ac20512a484cfa6e66239dd3d0d9Chandler Carruth return Loc.getLocWithOffset(BeginFileLocInfo.second - FileLocInfo.second); 5390e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis} 5400e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis 541f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregornamespace { 542f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor enum PreambleDirectiveKind { 543f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_Skipped, 544f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_StartIf, 545f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_EndIf, 546f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_Unknown 547f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor }; 548f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor} 549f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 550f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregorstd::pair<unsigned, bool> 55103c107a42fae79e89d0016999a1a04c07d65591aArgyrios KyrtzidisLexer::ComputePreamble(const llvm::MemoryBuffer *Buffer, 5524e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts, unsigned MaxLines) { 553f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // Create a lexer starting at the beginning of the file. Note that we use a 554f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // "fake" file source location at offset 1 so that the lexer will track our 555f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // position within the file. 556f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor const unsigned StartOffset = 1; 5571cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis SourceLocation FileLoc = SourceLocation::getFromRawEncoding(StartOffset); 5581cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis Lexer TheLexer(FileLoc, LangOpts, Buffer->getBufferStart(), 559f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Buffer->getBufferStart(), Buffer->getBufferEnd()); 5601cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis 5611cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis // StartLoc will differ from FileLoc if there is a BOM that was skipped. 5621cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis SourceLocation StartLoc = TheLexer.getSourceLocation(); 5631cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis 564f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor bool InPreprocessorDirective = false; 565f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Token TheTok; 566f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Token IfStartTok; 567f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor unsigned IfCount = 0; 568c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis 569c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis unsigned MaxLineOffset = 0; 570c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (MaxLines) { 571c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis const char *CurPtr = Buffer->getBufferStart(); 572c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis unsigned CurLine = 0; 573c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis while (CurPtr != Buffer->getBufferEnd()) { 574c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis char ch = *CurPtr++; 575c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (ch == '\n') { 576c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis ++CurLine; 577c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (CurLine == MaxLines) 578c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis break; 579c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis } 580c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis } 581c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (CurPtr != Buffer->getBufferEnd()) 582c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis MaxLineOffset = CurPtr - Buffer->getBufferStart(); 583c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis } 584df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor 585f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor do { 586f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor TheLexer.LexFromRawLexer(TheTok); 587f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 588f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (InPreprocessorDirective) { 589f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // If we've hit the end of the file, we're done. 590f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (TheTok.getKind() == tok::eof) { 591f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 592f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 593f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 594f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // If we haven't hit the end of the preprocessor directive, skip this 595f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // token. 596f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (!TheTok.isAtStartOfLine()) 597f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 598f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 599f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // We've passed the end of the preprocessor directive, and will look 600f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // at this token again below. 601f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor InPreprocessorDirective = false; 602f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 603f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 604df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // Keep track of the # of lines in the preamble. 605df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor if (TheTok.isAtStartOfLine()) { 606c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis unsigned TokOffset = TheTok.getLocation().getRawEncoding() - StartOffset; 607df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor 608df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // If we were asked to limit the number of lines in the preamble, 609df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // and we're about to exceed that limit, we're done. 610c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (MaxLineOffset && TokOffset >= MaxLineOffset) 611df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor break; 612df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor } 613df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor 614f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // Comments are okay; skip over them. 615f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (TheTok.getKind() == tok::comment) 616f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 617f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 618f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) { 619f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // This is the start of a preprocessor directive. 620f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Token HashTok = TheTok; 621f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor InPreprocessorDirective = true; 622f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 62319207f1e5f51261a33492602501fb7ada50ea546Joerg Sonnenberger // Figure out which directive this is. Since we're lexing raw tokens, 624f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // we don't have an identifier table available. Instead, just look at 625f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // the raw identifier to recognize and categorize preprocessor directives. 626f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor TheLexer.LexFromRawLexer(TheTok); 627c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara if (TheTok.getKind() == tok::raw_identifier && !TheTok.needsCleaning()) { 6285f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Keyword(TheTok.getRawIdentifierData(), 629c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara TheTok.getLength()); 630f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PreambleDirectiveKind PDK 631f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor = llvm::StringSwitch<PreambleDirectiveKind>(Keyword) 632f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("include", PDK_Skipped) 633f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("__include_macros", PDK_Skipped) 634f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("define", PDK_Skipped) 635f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("undef", PDK_Skipped) 636f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("line", PDK_Skipped) 637f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("error", PDK_Skipped) 638f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("pragma", PDK_Skipped) 639f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("import", PDK_Skipped) 640f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("include_next", PDK_Skipped) 641f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("warning", PDK_Skipped) 642f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("ident", PDK_Skipped) 643f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("sccs", PDK_Skipped) 644f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("assert", PDK_Skipped) 645f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("unassert", PDK_Skipped) 646f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("if", PDK_StartIf) 647f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("ifdef", PDK_StartIf) 648f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("ifndef", PDK_StartIf) 649f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("elif", PDK_Skipped) 650f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("else", PDK_Skipped) 651f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("endif", PDK_EndIf) 652f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Default(PDK_Unknown); 653f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 654f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor switch (PDK) { 655f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_Skipped: 656f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 657f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 658f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_StartIf: 659f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (IfCount == 0) 660f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor IfStartTok = HashTok; 661f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 662f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor ++IfCount; 663f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 664f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 665f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_EndIf: 666f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // Mismatched #endif. The preamble ends here. 667f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (IfCount == 0) 668f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 669f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 670f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor --IfCount; 671f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 672f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 673f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_Unknown: 674f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // We don't know what this directive is; stop at the '#'. 675f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 676f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 677f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 678f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 679f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // We only end up here if we didn't recognize the preprocessor 680f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // directive or it was one that can't occur in the preamble at this 681f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // point. Roll back the current token to the location of the '#'. 682f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor InPreprocessorDirective = false; 683f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor TheTok = HashTok; 684f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 685f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 686df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // We hit a token that we don't recognize as being in the 687df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // "preprocessing only" part of the file, so we're no longer in 688df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // the preamble. 689f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 690f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } while (true); 691f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 692f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation(); 693f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(), 694f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor IfCount? IfStartTok.isAtStartOfLine() 695f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor : TheTok.isAtStartOfLine()); 696f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor} 697f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 6987ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 6997ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// AdvanceToTokenCharacter - Given a location that specifies the start of a 7007ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token, return a new location that specifies a character within the token. 7017ef5c27eb6e8ebe58b52013246c06753c3613263Chris LattnerSourceLocation Lexer::AdvanceToTokenCharacter(SourceLocation TokStart, 7027ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner unsigned CharNo, 7037ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner const SourceManager &SM, 7044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts) { 705433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth // Figure out how many physical characters away the specified expansion 7067ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // character is. This needs to take into consideration newlines and 7077ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // trigraphs. 7087ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner bool Invalid = false; 7097ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner const char *TokPtr = SM.getCharacterData(TokStart, &Invalid); 7107ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7117ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // If they request the first char of the token, we're trivially done. 7127ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (Invalid || (CharNo == 0 && Lexer::isObviouslySimpleCharacter(*TokPtr))) 7137ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner return TokStart; 7147ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7157ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner unsigned PhysOffset = 0; 7167ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7177ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // The usual case is that tokens don't contain anything interesting. Skip 7187ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // over the uninteresting characters. If a token only consists of simple 7197ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // chars, this method is extremely fast. 7207ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner while (Lexer::isObviouslySimpleCharacter(*TokPtr)) { 7217ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (CharNo == 0) 722a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return TokStart.getLocWithOffset(PhysOffset); 7237ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner ++TokPtr, --CharNo, ++PhysOffset; 7247ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner } 7257ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7267ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // If we have a character that may be a trigraph or escaped newline, use a 7277ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // lexer to parse it correctly. 7287ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner for (; CharNo; --CharNo) { 7297ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner unsigned Size; 7304e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie Lexer::getCharAndSizeNoWarn(TokPtr, Size, LangOpts); 7317ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner TokPtr += Size; 7327ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner PhysOffset += Size; 7337ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner } 7347ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7357ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // Final detail: if we end up on an escaped newline, we want to return the 7367ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // location of the actual byte of the token. For example foo\<newline>bar 7377ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // advanced by 3 should return the location of b, not of \\. One compounding 7387ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // detail of this is that the escape may be made by a trigraph. 7397ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (!Lexer::isObviouslySimpleCharacter(*TokPtr)) 7407ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner PhysOffset += Lexer::SkipEscapedNewLines(TokPtr)-TokPtr; 7417ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 742a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return TokStart.getLocWithOffset(PhysOffset); 7437ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner} 7447ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7457ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// \brief Computes the source location just past the end of the 7467ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token at this source location. 7477ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// 7487ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// This routine can be used to produce a source location that 7497ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// points just past the end of the token referenced by \p Loc, and 7507ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// is generally used when a diagnostic needs to point just after a 7517ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token where it expected something different that it received. If 7527ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// the returned source location would not be meaningful (e.g., if 7537ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// it points into a macro), this routine returns an invalid 7547ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// source location. 7557ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// 7567ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// \param Offset an offset from the end of the token, where the source 7577ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// location should refer to. The default offset (0) produces a source 7587ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// location pointing just past the end of the token; an offset of 1 produces 7597ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// a source location pointing to the last character in the token, etc. 7607ef5c27eb6e8ebe58b52013246c06753c3613263Chris LattnerSourceLocation Lexer::getLocForEndOfToken(SourceLocation Loc, unsigned Offset, 7617ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner const SourceManager &SM, 7624e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts) { 7637ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis if (Loc.isInvalid()) 7647ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner return SourceLocation(); 7657ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis 7667ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis if (Loc.isMacroID()) { 7674e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Offset > 0 || !isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc)) 768433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth return SourceLocation(); // Points inside the macro expansion. 7697ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis } 7707ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis 7714e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie unsigned Len = Lexer::MeasureTokenLength(Loc, SM, LangOpts); 7727ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (Len > Offset) 7737ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner Len = Len - Offset; 7747ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner else 7757ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner return Loc; 7767ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 777a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return Loc.getLocWithOffset(Len); 7787ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner} 7797ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7807a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis/// \brief Returns true if the given MacroID location points at the first 781433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// token of the macro expansion. 782433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruthbool Lexer::isAtStartOfMacroExpansion(SourceLocation loc, 783f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor const SourceManager &SM, 78469bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis const LangOptions &LangOpts, 78569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis SourceLocation *MacroBegin) { 7867a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); 7877a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 7887a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis std::pair<FileID, unsigned> infoLoc = SM.getDecomposedLoc(loc); 7897a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // FIXME: If the token comes from the macro token paste operator ('##') 7907a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // this function will always return false; 7917a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis if (infoLoc.second > 0) 7927a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis return false; // Does not point at the start of token. 7937a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 794433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation expansionLoc = 7951728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth SM.getSLocEntry(infoLoc.first).getExpansion().getExpansionLocStart(); 79669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (expansionLoc.isFileID()) { 79769bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis // No other macro expansions, this is the first. 79869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (MacroBegin) 79969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis *MacroBegin = expansionLoc; 80069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis return true; 80169bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis } 8027a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 80369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis return isAtStartOfMacroExpansion(expansionLoc, SM, LangOpts, MacroBegin); 8047a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis} 8057a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 8067a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis/// \brief Returns true if the given MacroID location points at the last 807433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// token of the macro expansion. 808433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruthbool Lexer::isAtEndOfMacroExpansion(SourceLocation loc, 80969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis const SourceManager &SM, 81069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis const LangOptions &LangOpts, 81169bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis SourceLocation *MacroEnd) { 8127a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); 8137a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 8147a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis SourceLocation spellLoc = SM.getSpellingLoc(loc); 8157a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis unsigned tokLen = MeasureTokenLength(spellLoc, SM, LangOpts); 8167a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis if (tokLen == 0) 8177a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis return false; 8187a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 8197a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis FileID FID = SM.getFileID(loc); 820a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis SourceLocation afterLoc = loc.getLocWithOffset(tokLen+1); 821f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis if (SM.isInFileID(afterLoc, FID)) 822f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis return false; // Still in the same FileID, does not point to the last token. 8237a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 8247a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // FIXME: If the token comes from the macro token paste operator ('##') 8257a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // or the stringify operator ('#') this function will always return false; 826f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis 827433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation expansionLoc = 8281728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth SM.getSLocEntry(FID).getExpansion().getExpansionLocEnd(); 82969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (expansionLoc.isFileID()) { 83069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis // No other macro expansions. 83169bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (MacroEnd) 83269bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis *MacroEnd = expansionLoc; 83369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis return true; 83469bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis } 8357a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 83669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis return isAtEndOfMacroExpansion(expansionLoc, SM, LangOpts, MacroEnd); 8377a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis} 8387a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 839a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidisstatic CharSourceRange makeRangeFromFileLocs(CharSourceRange Range, 840d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis const SourceManager &SM, 841d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis const LangOptions &LangOpts) { 842a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis SourceLocation Begin = Range.getBegin(); 843a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis SourceLocation End = Range.getEnd(); 844d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis assert(Begin.isFileID() && End.isFileID()); 845a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis if (Range.isTokenRange()) { 846a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis End = Lexer::getLocForEndOfToken(End, 0, SM,LangOpts); 847a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis if (End.isInvalid()) 848a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return CharSourceRange(); 849a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis } 850d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 851d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis // Break down the source locations. 852d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis FileID FID; 853d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis unsigned BeginOffs; 854d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis llvm::tie(FID, BeginOffs) = SM.getDecomposedLoc(Begin); 855d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (FID.isInvalid()) 856d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange(); 857d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 858d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis unsigned EndOffs; 859d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (!SM.isInFileID(End, FID, &EndOffs) || 860d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis BeginOffs > EndOffs) 861d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange(); 862d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 863d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange::getCharRange(Begin, End); 864d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis} 865d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 866a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios KyrtzidisCharSourceRange Lexer::makeFileCharRange(CharSourceRange Range, 86711b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis const SourceManager &SM, 86811b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis const LangOptions &LangOpts) { 869a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis SourceLocation Begin = Range.getBegin(); 870a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis SourceLocation End = Range.getEnd(); 871d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Begin.isInvalid() || End.isInvalid()) 87211b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis return CharSourceRange(); 87311b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 874d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Begin.isFileID() && End.isFileID()) 875a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 876d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 877d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Begin.isMacroID() && End.isFileID()) { 87811b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis if (!isAtStartOfMacroExpansion(Begin, SM, LangOpts, &Begin)) 87911b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis return CharSourceRange(); 880a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setBegin(Begin); 881a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 882d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis } 88311b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 884d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Begin.isFileID() && End.isMacroID()) { 885a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis if ((Range.isTokenRange() && !isAtEndOfMacroExpansion(End, SM, LangOpts, 886a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis &End)) || 887a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis (Range.isCharRange() && !isAtStartOfMacroExpansion(End, SM, LangOpts, 888a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis &End))) 889d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange(); 890a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setEnd(End); 891a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 892d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis } 89311b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 894d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis assert(Begin.isMacroID() && End.isMacroID()); 895d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis SourceLocation MacroBegin, MacroEnd; 896d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (isAtStartOfMacroExpansion(Begin, SM, LangOpts, &MacroBegin) && 897a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis ((Range.isTokenRange() && isAtEndOfMacroExpansion(End, SM, LangOpts, 898a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis &MacroEnd)) || 899a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis (Range.isCharRange() && isAtStartOfMacroExpansion(End, SM, LangOpts, 900a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis &MacroEnd)))) { 901a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setBegin(MacroBegin); 902a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setEnd(MacroEnd); 903a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 904a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis } 905d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 906d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis FileID FID; 907d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis unsigned BeginOffs; 908d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis llvm::tie(FID, BeginOffs) = SM.getDecomposedLoc(Begin); 909d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (FID.isInvalid()) 910e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return CharSourceRange(); 911e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 91211b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis unsigned EndOffs; 913d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (!SM.isInFileID(End, FID, &EndOffs) || 914d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis BeginOffs > EndOffs) 91511b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis return CharSourceRange(); 91611b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 917d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis const SrcMgr::SLocEntry *E = &SM.getSLocEntry(FID); 918d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis const SrcMgr::ExpansionInfo &Expansion = E->getExpansion(); 919d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Expansion.isMacroArgExpansion() && 920d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis Expansion.getSpellingLoc().isFileID()) { 921d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis SourceLocation SpellLoc = Expansion.getSpellingLoc(); 922a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setBegin(SpellLoc.getLocWithOffset(BeginOffs)); 923a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setEnd(SpellLoc.getLocWithOffset(EndOffs)); 924a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 925d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis } 926d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 927d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange(); 92811b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis} 92911b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 930e64d9037658a1b95c79ea275af6167a110b3c563Argyrios KyrtzidisStringRef Lexer::getSourceText(CharSourceRange Range, 931e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis const SourceManager &SM, 932e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis const LangOptions &LangOpts, 933e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis bool *Invalid) { 934a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range = makeFileCharRange(Range, SM, LangOpts); 935a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis if (Range.isInvalid()) { 936e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = true; 937e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return StringRef(); 938e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis } 939e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 940e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis // Break down the source location. 941e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis std::pair<FileID, unsigned> beginInfo = SM.getDecomposedLoc(Range.getBegin()); 942e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (beginInfo.first.isInvalid()) { 943e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = true; 944e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return StringRef(); 945e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis } 946e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 947e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis unsigned EndOffs; 948e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (!SM.isInFileID(Range.getEnd(), beginInfo.first, &EndOffs) || 949e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis beginInfo.second > EndOffs) { 950e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = true; 951e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return StringRef(); 952e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis } 953e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 954e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis // Try to the load the file buffer. 955e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis bool invalidTemp = false; 956e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis StringRef file = SM.getBufferData(beginInfo.first, &invalidTemp); 957e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (invalidTemp) { 958e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = true; 959e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return StringRef(); 960e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis } 961e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 962e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = false; 963e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return file.substr(beginInfo.second, EndOffs - beginInfo.second); 964e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis} 965e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 966c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna ZaksStringRef Lexer::getImmediateMacroName(SourceLocation Loc, 967c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks const SourceManager &SM, 968c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks const LangOptions &LangOpts) { 969c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks assert(Loc.isMacroID() && "Only reasonble to call this on macros"); 9707f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9717f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // Find the location of the immediate macro expansion. 9727f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis while (1) { 9737f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis FileID FID = SM.getFileID(Loc); 9747f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis const SrcMgr::SLocEntry *E = &SM.getSLocEntry(FID); 9757f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis const SrcMgr::ExpansionInfo &Expansion = E->getExpansion(); 9767f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis Loc = Expansion.getExpansionLocStart(); 9777f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis if (!Expansion.isMacroArgExpansion()) 9787f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis break; 9797f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9807f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // For macro arguments we need to check that the argument did not come 9817f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // from an inner macro, e.g: "MAC1( MAC2(foo) )" 9827f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9837f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // Loc points to the argument id of the macro definition, move to the 9847f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // macro expansion. 985c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks Loc = SM.getImmediateExpansionRange(Loc).first; 9867f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis SourceLocation SpellLoc = Expansion.getSpellingLoc(); 9877f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis if (SpellLoc.isFileID()) 9887f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis break; // No inner macro. 9897f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9907f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // If spelling location resides in the same FileID as macro expansion 9917f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // location, it means there is no inner macro. 9927f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis FileID MacroFID = SM.getFileID(Loc); 9937f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis if (SM.isInFileID(SpellLoc, MacroFID)) 9947f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis break; 9957f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9967f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // Argument came from inner macro. 9977f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis Loc = SpellLoc; 9987f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis } 999c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks 1000c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // Find the spelling location of the start of the non-argument expansion 1001c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // range. This is where the macro name was spelled in order to begin 1002c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // expanding this macro. 10037f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis Loc = SM.getSpellingLoc(Loc); 1004c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks 1005c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // Dig out the buffer where the macro name was spelled and the extents of the 1006c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // name so that we can render it into the expansion note. 1007c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks std::pair<FileID, unsigned> ExpansionInfo = SM.getDecomposedLoc(Loc); 1008c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks unsigned MacroTokenLength = Lexer::MeasureTokenLength(Loc, SM, LangOpts); 1009c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first); 1010c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks return ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength); 1011c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks} 1012c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks 1013d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rosebool Lexer::isIdentifierBodyChar(char c, const LangOptions &LangOpts) { 10149893902eceba7f01dd1521349d33866f77254d78Jordan Rose return isIdentifierBody(c, LangOpts.DollarIdents); 1015d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose} 1016d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose 10175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 10195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Diagnostics forwarding code. 10205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 10215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1022409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the 1023433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// lexer buffer was all expanded at a single point, perform the mapping. 1024409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// This is currently only used for _Pragma implementation, so it is the slow 1025409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// path of the hot getSourceLocation method. Do not allow it to be inlined. 102614bd96571ef6f0e97dc79ec4d01b547d60e8fa68Chandler Carruthstatic LLVM_ATTRIBUTE_NOINLINE SourceLocation GetMappedTokenLoc( 102714bd96571ef6f0e97dc79ec4d01b547d60e8fa68Chandler Carruth Preprocessor &PP, SourceLocation FileLoc, unsigned CharNo, unsigned TokLen); 1028409a03671224d4e5bdab1594c43baf070148f830Chris Lattnerstatic SourceLocation GetMappedTokenLoc(Preprocessor &PP, 1029409a03671224d4e5bdab1594c43baf070148f830Chris Lattner SourceLocation FileLoc, 1030de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned CharNo, unsigned TokLen) { 1031433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth assert(FileLoc.isMacroID() && "Must be a macro expansion"); 10321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1033409a03671224d4e5bdab1594c43baf070148f830Chris Lattner // Otherwise, we're lexing "mapped tokens". This is used for things like 1034433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth // _Pragma handling. Combine the expansion location of FileLoc with the 1035df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner // spelling location. 1036e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner SourceManager &SM = PP.getSourceManager(); 10371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1038433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth // Create a new SLoc which is expanded from Expansion(FileLoc) but whose 1039df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner // characters come from spelling(FileLoc)+Offset. 1040e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc); 1041a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis SpellingLoc = SpellingLoc.getLocWithOffset(CharNo); 10421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1043e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner // Figure out the expansion loc range, which is the range covered by the 1044e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner // original _Pragma(...) sequence. 1045e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner std::pair<SourceLocation,SourceLocation> II = 1046999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth SM.getImmediateExpansionRange(FileLoc); 10471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1048bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth return SM.createExpansionLoc(SpellingLoc, II.first, II.second, TokLen); 1049409a03671224d4e5bdab1594c43baf070148f830Chris Lattner} 1050409a03671224d4e5bdab1594c43baf070148f830Chris Lattner 10515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSourceLocation - Return a source location identifier for the specified 10525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// offset in the current file. 1053de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris LattnerSourceLocation Lexer::getSourceLocation(const char *Loc, 1054de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned TokLen) const { 1055448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner assert(Loc >= BufferStart && Loc <= BufferEnd && 10565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer "Location out of range for this buffer!"); 10579dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner 10589dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner // In the normal case, we're just lexing from a simple file buffer, return 10599dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner // the file id from FileLoc with the offset specified. 1060448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner unsigned CharNo = Loc-BufferStart; 10619dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner if (FileLoc.isFileID()) 1062a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return FileLoc.getLocWithOffset(CharNo); 10631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10642b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner // Otherwise, this is the _Pragma lexer case, which pretends that all of the 10652b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner // tokens are lexed from where the _Pragma was defined. 1066168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner assert(PP && "This doesn't work on raw lexers"); 1067de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen); 10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Diag - Forwarding function for diagnostics. This translate a source 10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// position in the current buffer into a SourceLocation object for rendering. 10723cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const { 10733692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner return PP->Diag(getSourceLocation(Loc), DiagID); 10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 10775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Trigraph and Escaped Newline Handling Code. 10785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair, 10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the decoded trigraph letter it corresponds to, or '\0' if nothing. 10825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char GetTrigraphCharForLetter(char Letter) { 10835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Letter) { 10845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: return 0; 10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '=': return '#'; 10865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ')': return ']'; 10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '(': return '['; 10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '!': return '|'; 10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\'': return '^'; 10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '>': return '}'; 10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '/': return '\\'; 10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '<': return '{'; 10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '-': return '~'; 10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// DecodeTrigraphChar - If the specified character is a legal trigraph when 10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled, 10995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the result character. Finally, emit a warning about trigraph use 11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whether trigraphs are enabled or not. 11015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char DecodeTrigraphChar(const char *CP, Lexer *L) { 11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Res = GetTrigraphCharForLetter(*CP); 11033692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner if (!Res || !L) return Res; 11041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11054e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!L->getLangOpts().Trigraphs) { 110674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 110774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CP-2, diag::trigraph_ignored); 11083692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner return 0; 11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 11101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 111174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 11125f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner L->Diag(CP-2, diag::trigraph_converted) << StringRef(&Res, 1); 11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return Res; 11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 111624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// getEscapedNewLineSize - Return the size of the specified escaped newline, 111724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a 11181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// trigraph equivalent on entry to this function. 111924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattnerunsigned Lexer::getEscapedNewLineSize(const char *Ptr) { 112024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner unsigned Size = 0; 112124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner while (isWhitespace(Ptr[Size])) { 112224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner ++Size; 11231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 112424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r') 112524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner continue; 112624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 112724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // If this is a \r\n or \n\r, skip the other half. 112824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') && 112924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr[Size-1] != Ptr[Size]) 113024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner ++Size; 11311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 113224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return Size; 11331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 11341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 113524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Not an escaped newline, must be a \t or something else. 113624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return 0; 113724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner} 113824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 1139033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// SkipEscapedNewLines - If P points to an escaped newline (or a series of 1140033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// them), skip over them and return the first non-escaped-newline found, 1141033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// otherwise return P. 1142033749571f8d4c804eeb357c70b06424aa24503bChris Lattnerconst char *Lexer::SkipEscapedNewLines(const char *P) { 1143033749571f8d4c804eeb357c70b06424aa24503bChris Lattner while (1) { 1144033749571f8d4c804eeb357c70b06424aa24503bChris Lattner const char *AfterEscape; 1145033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (*P == '\\') { 1146033749571f8d4c804eeb357c70b06424aa24503bChris Lattner AfterEscape = P+1; 1147033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } else if (*P == '?') { 1148033749571f8d4c804eeb357c70b06424aa24503bChris Lattner // If not a trigraph for escape, bail out. 1149033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (P[1] != '?' || P[2] != '/') 1150033749571f8d4c804eeb357c70b06424aa24503bChris Lattner return P; 1151033749571f8d4c804eeb357c70b06424aa24503bChris Lattner AfterEscape = P+3; 1152033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } else { 1153033749571f8d4c804eeb357c70b06424aa24503bChris Lattner return P; 1154033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } 11551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1156033749571f8d4c804eeb357c70b06424aa24503bChris Lattner unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape); 1157033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (NewLineSize == 0) return P; 1158033749571f8d4c804eeb357c70b06424aa24503bChris Lattner P = AfterEscape+NewLineSize; 1159033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } 1160033749571f8d4c804eeb357c70b06424aa24503bChris Lattner} 1161033749571f8d4c804eeb357c70b06424aa24503bChris Lattner 1162aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// \brief Checks that the given token is the first token that occurs after the 1163aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// given location (this excludes comments and whitespace). Returns the location 1164aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// immediately after the specified token. If the token is not found or the 1165aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// location is inside a macro, the returned source location will be invalid. 1166aca25bccefe56121b686706afc84c8cb5d46e65bAnna ZaksSourceLocation Lexer::findLocationAfterToken(SourceLocation Loc, 1167aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks tok::TokenKind TKind, 1168aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks const SourceManager &SM, 1169aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks const LangOptions &LangOpts, 1170aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks bool SkipTrailingWhitespaceAndNewLine) { 1171aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks if (Loc.isMacroID()) { 117269bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (!Lexer::isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc)) 1173aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks return SourceLocation(); 1174aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks } 1175aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, LangOpts); 1176aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1177aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Break down the source location. 1178aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 1179aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1180aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Try to load the file buffer. 1181aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks bool InvalidTemp = false; 1182cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp); 1183aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks if (InvalidTemp) 1184aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks return SourceLocation(); 1185aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1186aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks const char *TokenBegin = File.data() + LocInfo.second; 1187aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1188aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Lex from the start of the given location. 1189aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks Lexer lexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(), 1190aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks TokenBegin, File.end()); 1191aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Find the token. 1192aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks Token Tok; 1193aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks lexer.LexFromRawLexer(Tok); 1194aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks if (Tok.isNot(TKind)) 1195aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks return SourceLocation(); 1196aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks SourceLocation TokenLoc = Tok.getLocation(); 1197aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1198aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Calculate how much whitespace needs to be skipped if any. 1199aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks unsigned NumWhitespaceChars = 0; 1200aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks if (SkipTrailingWhitespaceAndNewLine) { 1201aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks const char *TokenEnd = SM.getCharacterData(TokenLoc) + 1202aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks Tok.getLength(); 1203aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks unsigned char C = *TokenEnd; 1204aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks while (isHorizontalWhitespace(C)) { 1205aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks C = *(++TokenEnd); 1206aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks NumWhitespaceChars++; 1207aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks } 120835a2b798efd61fec425553f387d76be9c522f184Eli Friedman 120935a2b798efd61fec425553f387d76be9c522f184Eli Friedman // Skip \r, \n, \r\n, or \n\r 121035a2b798efd61fec425553f387d76be9c522f184Eli Friedman if (C == '\n' || C == '\r') { 121135a2b798efd61fec425553f387d76be9c522f184Eli Friedman char PrevC = C; 121235a2b798efd61fec425553f387d76be9c522f184Eli Friedman C = *(++TokenEnd); 1213aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks NumWhitespaceChars++; 121435a2b798efd61fec425553f387d76be9c522f184Eli Friedman if ((C == '\n' || C == '\r') && C != PrevC) 121535a2b798efd61fec425553f387d76be9c522f184Eli Friedman NumWhitespaceChars++; 121635a2b798efd61fec425553f387d76be9c522f184Eli Friedman } 1217aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks } 1218aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1219a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return TokenLoc.getLocWithOffset(Tok.getLength() + NumWhitespaceChars); 1220aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks} 122124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer, 12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// get its size, and return it. This is tricky in several cases: 12245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 1. If currently at the start of a trigraph, we warn about the trigraph, 12255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// then either return the trigraph (skipping 3 chars) or the '?', 12265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// depending on whether trigraphs are enabled or not. 12275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 2. If this is an escaped newline (potentially with whitespace between 12285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the backslash and newline), implicitly skip the newline and return 12295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the char after it. 12305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 12315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This handles the slow/uncommon case of the getCharAndSize method. Here we 12325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// know that we can accumulate into Size, and that we have already incremented 12335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Ptr by Size bytes. 12345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 12355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should 12365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match. 12375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 12385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size, 1239d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token *Tok) { 12405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, look for an escaped newline. 12415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '\\') { 12425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 12435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Ptr; 12445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash: 12455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Common case, backslash-char where the char is not whitespace. 12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isWhitespace(Ptr[0])) return '\\'; 12471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12485636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner // See if we have optional whitespace characters between the slash and 12495636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner // newline. 125024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { 125124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Remember that this token needs to be cleaned. 125224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (Tok) Tok->setFlag(Token::NeedsCleaning); 125324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 125424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Warn if there was whitespace between the backslash and newline. 12555636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode()) 125624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Diag(Ptr, diag::backslash_newline_space); 12571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 125824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Found backslash<whitespace><newline>. Parse the char after it. 125924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Size += EscapedNewLineSize; 126024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr += EscapedNewLineSize; 1261f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis 126204a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // If the char that we finally got was a \n, then we must have had 126304a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // something like \<newline><newline>. We don't want to consume the 126404a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // second newline. 126504a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0') 126604a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis return ' '; 1267f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis 126824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Use slow version to accumulate a correct size field. 126924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return getCharAndSizeSlow(Ptr, Size, Tok); 127024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner } 12711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is not an escaped newline, just return the slash. 12735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return '\\'; 12745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a trigraph, process it. 12775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '?' && Ptr[1] == '?') { 12785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is actually a legal trigraph (not something like "??x"), emit 12795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // a trigraph warning. If so, and if trigraphs are enabled, return it. 12805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) { 12815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Remember that this token needs to be cleaned. 1282d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner if (Tok) Tok->setFlag(Token::NeedsCleaning); 12835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Ptr += 3; 12855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Size += 3; 12865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') goto Slash; 12875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return C; 12885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is neither, return a single character. 12925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 12935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return *Ptr; 12945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 12955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the 12985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size, 12995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// and that we have already incremented Ptr by Size bytes. 13005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 13015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlow (above) should 13025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match. 13035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size, 13044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts) { 13055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, look for an escaped newline. 13065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '\\') { 13075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 13085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Ptr; 13095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash: 13105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Common case, backslash-char where the char is not whitespace. 13115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isWhitespace(Ptr[0])) return '\\'; 13121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // See if we have optional whitespace characters followed by a newline. 131424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { 131524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Found backslash<whitespace><newline>. Parse the char after it. 131624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Size += EscapedNewLineSize; 131724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr += EscapedNewLineSize; 13181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 131904a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // If the char that we finally got was a \n, then we must have had 132004a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // something like \<newline><newline>. We don't want to consume the 132104a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // second newline. 132204a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0') 132304a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis return ' '; 1324f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis 132524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Use slow version to accumulate a correct size field. 13264e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie return getCharAndSizeSlowNoWarn(Ptr, Size, LangOpts); 132724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner } 13281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is not an escaped newline, just return the slash. 13305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return '\\'; 13315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 13321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a trigraph, process it. 13344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') { 13355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is actually a legal trigraph (not something like "??x"), return 13365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it. 13375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (char C = GetTrigraphCharForLetter(Ptr[2])) { 13385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Ptr += 3; 13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Size += 3; 13405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') goto Slash; 13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return C; 13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 13441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is neither, return a single character. 13465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 13475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return *Ptr; 13485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 13495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 13515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper methods for lexing. 13525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 13535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1354f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor/// \brief Routine that indiscriminately skips bytes in the source file. 1355f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregorvoid Lexer::SkipBytes(unsigned Bytes, bool StartOfLine) { 1356f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor BufferPtr += Bytes; 1357f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor if (BufferPtr > BufferEnd) 1358f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor BufferPtr = BufferEnd; 1359f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor IsAtStartOfLine = StartOfLine; 1360f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor} 1361f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor 1362ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rosestatic bool isAllowedIDChar(uint32_t C, const LangOptions &LangOpts) { 1363ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (LangOpts.CPlusPlus11 || LangOpts.C11) 1364ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose return isCharInSet(C, C11AllowedIDChars); 1365ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose else if (LangOpts.CPlusPlus) 1366ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose return isCharInSet(C, CXX03AllowedIDChars); 1367ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose else 1368ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose return isCharInSet(C, C99AllowedIDChars); 1369c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose} 1370c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 1371ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rosestatic bool isAllowedInitiallyIDChar(uint32_t C, const LangOptions &LangOpts) { 1372ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose assert(isAllowedIDChar(C, LangOpts)); 1373ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (LangOpts.CPlusPlus11 || LangOpts.C11) 1374ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose return !isCharInSet(C, C11DisallowedInitialIDChars); 1375ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose else if (LangOpts.CPlusPlus) 1376ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose return true; 1377ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose else 1378ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose return !isCharInSet(C, C99DisallowedInitialIDChars); 1379ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose} 1380ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose 1381ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rosestatic inline CharSourceRange makeCharRange(Lexer &L, const char *Begin, 1382ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose const char *End) { 1383ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose return CharSourceRange::getCharRange(L.getSourceLocation(Begin), 1384ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose L.getSourceLocation(End)); 1385ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose} 1386ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose 1387ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rosestatic void maybeDiagnoseIDCharCompat(DiagnosticsEngine &Diags, uint32_t C, 1388ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose CharSourceRange Range, bool IsFirst) { 1389ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose // Check C99 compatibility. 1390ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (Diags.getDiagnosticLevel(diag::warn_c99_compat_unicode_id, 1391ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose Range.getBegin()) > DiagnosticsEngine::Ignored) { 1392ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose enum { 1393ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose CannotAppearInIdentifier = 0, 1394ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose CannotStartIdentifier 1395ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose }; 1396ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose 1397ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (!isCharInSet(C, C99AllowedIDChars)) { 1398ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose Diags.Report(Range.getBegin(), diag::warn_c99_compat_unicode_id) 1399ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose << Range 1400ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose << CannotAppearInIdentifier; 1401ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } else if (IsFirst && isCharInSet(C, C99DisallowedInitialIDChars)) { 1402ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose Diags.Report(Range.getBegin(), diag::warn_c99_compat_unicode_id) 1403ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose << Range 1404ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose << CannotStartIdentifier; 1405ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 1406c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 1407c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 1408ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose // Check C++98 compatibility. 1409ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (Diags.getDiagnosticLevel(diag::warn_cxx98_compat_unicode_id, 1410ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose Range.getBegin()) > DiagnosticsEngine::Ignored) { 1411ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (!isCharInSet(C, CXX03AllowedIDChars)) { 1412ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose Diags.Report(Range.getBegin(), diag::warn_cxx98_compat_unicode_id) 1413ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose << Range; 1414ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 1415ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 1416ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 1417c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 1418d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexIdentifier(Token &Result, const char *CurPtr) { 14195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$] 14205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned Size; 14215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned char C = *CurPtr++; 1422cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner while (isIdentifierBody(C)) 14235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 1424cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner 14255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; // Back up over the skipped character. 14265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Fast path, no $,\,? in identifier found. '\' might be an escaped newline 14285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN. 1429cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner // 14309893902eceba7f01dd1521349d33866f77254d78Jordan Rose // TODO: Could merge these checks into an InfoTable flag to make the 14319893902eceba7f01dd1521349d33866f77254d78Jordan Rose // comparison cheaper 1432c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (isASCII(C) && C != '\\' && C != '?' && 1433c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose (C != '$' || !LangOpts.DollarIdents)) { 14345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerFinishIdentifier: 14355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *IdStart = BufferPtr; 1436c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara FormTokenWithChars(Result, CurPtr, tok::raw_identifier); 1437c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara Result.setRawIdentifierData(IdStart); 14381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are in raw mode, return this identifier raw. There is no need to 14405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // look up identifier information or attempt to macro expand it. 1441c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara if (LexingRawMode) 1442c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara return; 14431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1444c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // Fill in Result.IdentifierInfo and update the token kind, 1445c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // looking up the identifier in the identifier table. 1446c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara IdentifierInfo *II = PP->LookUpIdentifierInfo(Result); 14471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Finally, now that we know we have an identifier, pass this off to the 14495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // preprocessor, which may macro expand it or something. 1450d1186fa38166a581b51975f0382a45fc3a0733d0Chris Lattner if (II->isHandleIdentifierCase()) 14516a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner PP->HandleIdentifier(Result); 14526aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 14536a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner return; 14545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, $,\,? in identifier found. Enter slower path. 14571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 14595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 14605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '$') { 14615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we hit a $ and they are not supported in identifiers, we are done. 14624e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!LangOpts.DollarIdents) goto FinishIdentifier; 14631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, emit a diagnostic and continue. 146574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 146674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr, diag::ext_dollar_in_identifier); 14675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 14685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 14695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer continue; 1470c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 1471c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } else if (C == '\\') { 1472c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose const char *UCNPtr = CurPtr + Size; 1473c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose uint32_t CodePoint = tryReadUCN(UCNPtr, CurPtr, /*Token=*/0); 1474ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (CodePoint == 0 || !isAllowedIDChar(CodePoint, LangOpts)) 1475c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose goto FinishIdentifier; 1476c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 1477ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (!isLexingRawMode()) { 1478ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose maybeDiagnoseIDCharCompat(PP->getDiagnostics(), CodePoint, 1479ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose makeCharRange(*this, CurPtr, UCNPtr), 1480ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose /*IsFirst=*/false); 1481ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 1482ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose 1483c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Result.setFlag(Token::HasUCN); 1484c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if ((UCNPtr - CurPtr == 6 && CurPtr[1] == 'u') || 1485c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose (UCNPtr - CurPtr == 10 && CurPtr[1] == 'U')) 1486c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose CurPtr = UCNPtr; 1487c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose else 1488c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose while (CurPtr != UCNPtr) 1489c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose (void)getAndAdvanceChar(CurPtr, Result); 1490c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 1491c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose C = getCharAndSize(CurPtr, Size); 1492c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose continue; 1493c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } else if (!isASCII(C)) { 1494c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose const char *UnicodePtr = CurPtr; 1495c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose UTF32 CodePoint; 1496cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko ConversionResult Result = 1497cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko llvm::convertUTF8Sequence((const UTF8 **)&UnicodePtr, 1498cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko (const UTF8 *)BufferEnd, 1499cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko &CodePoint, 1500cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko strictConversion); 1501c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (Result != conversionOK || 1502ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose !isAllowedIDChar(static_cast<uint32_t>(CodePoint), LangOpts)) 1503c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose goto FinishIdentifier; 1504c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 1505ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (!isLexingRawMode()) { 1506ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose maybeDiagnoseIDCharCompat(PP->getDiagnostics(), CodePoint, 1507ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose makeCharRange(*this, CurPtr, UnicodePtr), 1508ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose /*IsFirst=*/false); 1509ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 1510ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose 1511c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose CurPtr = UnicodePtr; 1512c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose C = getCharAndSize(CurPtr, Size); 1513c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose continue; 1514c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } else if (!isIdentifierBody(C)) { 15155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto FinishIdentifier; 15165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this character is good, consume it. 15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 15205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 1522c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose while (isIdentifierBody(C)) { 15235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 15245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 15255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 15285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1529a75ec43d625753b4439b0d6f70bd988444c74617Douglas Gregor/// isHexaLiteral - Return true if Start points to a hex constant. 15304a551000bee716ac8b1bbe16134a53f0ad221a5aChris Lattner/// in microsoft mode (where this is supposed to be several different tokens). 1531e506f8a41063410c75945ebb804758bd0202947fEli Friedmanbool Lexer::isHexaLiteral(const char *Start, const LangOptions &LangOpts) { 15326ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner unsigned Size; 15334e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie char C1 = Lexer::getCharAndSizeNoWarn(Start, Size, LangOpts); 15346ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner if (C1 != '0') 15356ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner return false; 15364e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie char C2 = Lexer::getCharAndSizeNoWarn(Start + Size, Size, LangOpts); 15376ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner return (C2 == 'x' || C2 == 'X'); 1538a75ec43d625753b4439b0d6f70bd988444c74617Douglas Gregor} 15395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15405253c7ff266ae79308050c9f43d60dd1a67c5fb9Nate Begeman/// LexNumericConstant - Lex the remainder of a integer or floating point 15415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. From[-1] is the first character lexed. Return the end of the 15425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. 1543d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexNumericConstant(Token &Result, const char *CurPtr) { 15445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned Size; 15455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getCharAndSize(CurPtr, Size); 15465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char PrevCh = 0; 15479893902eceba7f01dd1521349d33866f77254d78Jordan Rose while (isPreprocessingNumberBody(C)) { // FIXME: UCNs in ud-suffix. 15485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 15495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer PrevCh = C; 15505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 15515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we fell out, check for a sign, due to 1e+12. If we have one, continue. 1554b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) { 1555b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner // If we are in Microsoft mode, don't continue if the constant is hex. 1556b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner // For example, MSVC will accept the following as 3 tokens: 0x1234567e+1 15574e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!LangOpts.MicrosoftExt || !isHexaLiteral(BufferPtr, LangOpts)) 1558b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); 1559b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner } 15605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a hex FP constant, continue. 1562d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p')) { 1563d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith // Outside C99, we accept hexadecimal floating point numbers as a 1564d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith // not-quite-conforming extension. Only do so if this looks like it's 1565d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith // actually meant to be a hexfloat, and not if it has a ud-suffix. 1566d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith bool IsHexFloat = true; 1567d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith if (!LangOpts.C99) { 1568d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith if (!isHexaLiteral(BufferPtr, LangOpts)) 1569d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith IsHexFloat = false; 1570d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith else if (std::find(BufferPtr, CurPtr, '_') != CurPtr) 1571d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith IsHexFloat = false; 1572d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith } 1573d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith if (IsHexFloat) 1574d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); 1575d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith } 15761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 157847246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 15799e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::numeric_constant); 158047246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 15815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 15825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15835cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith/// LexUDSuffix - Lex the ud-suffix production for user-defined literal suffixes 1584e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith/// in C++11, or warn on a ud-suffix in C++98. 15855cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smithconst char *Lexer::LexUDSuffix(Token &Result, const char *CurPtr) { 15864e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(getLangOpts().CPlusPlus); 15875cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 15885cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith // Maximally munch an identifier. FIXME: UCNs. 15895cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith unsigned Size; 15905cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith char C = getCharAndSize(CurPtr, Size); 15915cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith if (isIdentifierHead(C)) { 159280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (!getLangOpts().CPlusPlus11) { 1593e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith if (!isLexingRawMode()) 15942fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith Diag(CurPtr, 15952fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith C == '_' ? diag::warn_cxx11_compat_user_defined_literal 15962fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith : diag::warn_cxx11_compat_reserved_user_defined_literal) 15972fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith << FixItHint::CreateInsertion(getSourceLocation(CurPtr), " "); 15982fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith return CurPtr; 15992fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith } 16002fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith 16012fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith // C++11 [lex.ext]p10, [usrlit.suffix]p1: A program containing a ud-suffix 16022fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith // that does not start with an underscore is ill-formed. As a conforming 16032fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith // extension, we treat all such suffixes as if they had whitespace before 16042fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith // them. 16052fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith if (C != '_') { 16062fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith if (!isLexingRawMode()) 1607b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet Diag(CurPtr, getLangOpts().MicrosoftMode ? 1608b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet diag::ext_ms_reserved_user_defined_literal : 1609b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet diag::ext_reserved_user_defined_literal) 1610e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith << FixItHint::CreateInsertion(getSourceLocation(CurPtr), " "); 1611e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith return CurPtr; 1612e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith } 1613e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith 161499831e4677a7e2e051af636221694d60ba31fcdbRichard Smith Result.setFlag(Token::HasUDSuffix); 16155cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith do { 16165cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith CurPtr = ConsumeChar(CurPtr, Size, Result); 16175cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith C = getCharAndSize(CurPtr, Size); 16185cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith } while (isIdentifierBody(C)); 16195cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith } 16205cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith return CurPtr; 16215cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith} 16225cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 16235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexStringLiteral - Lex the remainder of a string literal, after having lexed 16245cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor/// either " or L" or u8" or u" or U". 16255cee1195584fa8672253139c86e922daeda69b9eDouglas Gregorvoid Lexer::LexStringLiteral(Token &Result, const char *CurPtr, 16265cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::TokenKind Kind) { 16275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this string contain the \0 character? 16281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1629661a99690bc133bbaa029da925481d4a860dec90Richard Smith if (!isLexingRawMode() && 1630661a99690bc133bbaa029da925481d4a860dec90Richard Smith (Kind == tok::utf8_string_literal || 1631661a99690bc133bbaa029da925481d4a860dec90Richard Smith Kind == tok::utf16_string_literal || 1632d4bf760d057f1e8724ab7be340a2d42adae0900aRichard Smith Kind == tok::utf32_string_literal)) 1633d4bf760d057f1e8724ab7be340a2d42adae0900aRichard Smith Diag(BufferPtr, getLangOpts().CPlusPlus 1634d4bf760d057f1e8724ab7be340a2d42adae0900aRichard Smith ? diag::warn_cxx98_compat_unicode_literal 1635d4bf760d057f1e8724ab7be340a2d42adae0900aRichard Smith : diag::warn_c99_compat_unicode_literal); 1636661a99690bc133bbaa029da925481d4a860dec90Richard Smith 16375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 16385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '"') { 1639571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner // Skip escaped characters. Escaped newlines will already be processed by 1640571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner // getAndAdvanceChar. 1641571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner if (C == '\\') 16425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 164333611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor 1644571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner if (C == '\n' || C == '\r' || // Newline. 164533611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. 16464e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!isLexingRawMode() && !LangOpts.AsmPreprocessor) 1647b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith Diag(BufferPtr, diag::ext_unterminated_string); 16489e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr-1, tok::unknown); 16495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 16505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1651571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner 16527d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (C == 0) { 16537d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (isCodeCompletionPoint(CurPtr-1)) { 16547d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 16557d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis FormTokenWithChars(Result, CurPtr-1, tok::unknown); 16567d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return cutOffLexing(); 16577d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 16587d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 1659571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner NulCharacter = CurPtr-1; 16607d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 16615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 16625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16645cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith // If we are in C++11, lex the optional ud-suffix. 16654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus) 16665cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith CurPtr = LexUDSuffix(Result, CurPtr); 16675cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 16685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If a nul character existed in the string, warn about it. 166974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 167074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_string); 16715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of the token as well as the BufferPtr instance var. 167347246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 16745cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor FormTokenWithChars(Result, CurPtr, Kind); 167547246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 16765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 16775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16782fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// LexRawStringLiteral - Lex the remainder of a raw string literal, after 16792fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// having lexed R", LR", u8R", uR", or UR". 16802fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Toppervoid Lexer::LexRawStringLiteral(Token &Result, const char *CurPtr, 16812fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::TokenKind Kind) { 16822fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // This function doesn't use getAndAdvanceChar because C++0x [lex.pptoken]p3: 16832fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Between the initial and final double quote characters of the raw string, 16842fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // any transformations performed in phases 1 and 2 (trigraphs, 16852fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // universal-character-names, and line splicing) are reverted. 16862fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 1687661a99690bc133bbaa029da925481d4a860dec90Richard Smith if (!isLexingRawMode()) 1688661a99690bc133bbaa029da925481d4a860dec90Richard Smith Diag(BufferPtr, diag::warn_cxx98_compat_raw_string_literal); 1689661a99690bc133bbaa029da925481d4a860dec90Richard Smith 16902fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper unsigned PrefixLen = 0; 16912fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 16922fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper while (PrefixLen != 16 && isRawStringDelimBody(CurPtr[PrefixLen])) 16932fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ++PrefixLen; 16942fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 16952fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // If the last character was not a '(', then we didn't lex a valid delimiter. 16962fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (CurPtr[PrefixLen] != '(') { 16972fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (!isLexingRawMode()) { 16982fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper const char *PrefixEnd = &CurPtr[PrefixLen]; 16992fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (PrefixLen == 16) { 17002fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Diag(PrefixEnd, diag::err_raw_delim_too_long); 17012fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } else { 17022fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Diag(PrefixEnd, diag::err_invalid_char_raw_delim) 17032fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper << StringRef(PrefixEnd, 1); 17042fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17052fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17062fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17072fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Search for the next '"' in hopes of salvaging the lexer. Unfortunately, 17082fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // it's possible the '"' was intended to be part of the raw string, but 17092fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // there's not much we can do about that. 17102fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper while (1) { 17112fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper char C = *CurPtr++; 17122fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17132fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (C == '"') 17142fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper break; 17152fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (C == 0 && CurPtr-1 == BufferEnd) { 17162fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper --CurPtr; 17172fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper break; 17182fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17192fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17202fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17212fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper FormTokenWithChars(Result, CurPtr, tok::unknown); 17222fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return; 17232fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17242fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17252fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Save prefix and move CurPtr past it 17262fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper const char *Prefix = CurPtr; 17272fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CurPtr += PrefixLen + 1; // skip over prefix and '(' 17282fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17292fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper while (1) { 17302fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper char C = *CurPtr++; 17312fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17322fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (C == ')') { 17332fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Check for prefix match and closing quote. 17342fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (strncmp(CurPtr, Prefix, PrefixLen) == 0 && CurPtr[PrefixLen] == '"') { 17352fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CurPtr += PrefixLen + 1; // skip over prefix and '"' 17362fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper break; 17372fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17382fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } else if (C == 0 && CurPtr-1 == BufferEnd) { // End of file. 17392fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (!isLexingRawMode()) 17402fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Diag(BufferPtr, diag::err_unterminated_raw_string) 17412fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper << StringRef(Prefix, PrefixLen); 17422fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper FormTokenWithChars(Result, CurPtr-1, tok::unknown); 17432fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return; 17442fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17452fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17462fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17475cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith // If we are in C++11, lex the optional ud-suffix. 17484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus) 17495cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith CurPtr = LexUDSuffix(Result, CurPtr); 17505cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 17512fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Update the location of token as well as BufferPtr. 17522fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper const char *TokStart = BufferPtr; 17532fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper FormTokenWithChars(Result, CurPtr, Kind); 17542fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Result.setLiteralData(TokStart); 17552fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper} 17562fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexAngledStringLiteral - Lex the remainder of an angled string literal, 17585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// after having lexed the '<' character. This is used for #include filenames. 1759d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) { 17605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this string contain the \0 character? 17619cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner const char *AfterLessPos = CurPtr; 17625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 17635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '>') { 17645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip escaped characters. 17655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') { 17665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip the escaped character. 176760b202c5eb6fb2d608bfef05523f40fdf5091d48Dmitri Gribenko getAndAdvanceChar(CurPtr, Result); 17685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == '\n' || C == '\r' || // Newline. 17697d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis (C == 0 && (CurPtr-1 == BufferEnd || // End of file. 17707d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis isCodeCompletionPoint(CurPtr-1)))) { 17719cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner // If the filename is unterminated, then it must just be a lone < 17729cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner // character. Return this as such. 17739cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner FormTokenWithChars(Result, AfterLessPos, tok::less); 17745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 17755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == 0) { 17765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer NulCharacter = CurPtr-1; 17775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 17795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If a nul character existed in the string, warn about it. 178274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 178374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_string); 17841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 178647246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 17879e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::angle_string_literal); 178847246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 17895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 17905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 17915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 17925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexCharConstant - Lex the remainder of a character constant, after having 17935cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor/// lexed either ' or L' or u' or U'. 17945cee1195584fa8672253139c86e922daeda69b9eDouglas Gregorvoid Lexer::LexCharConstant(Token &Result, const char *CurPtr, 17955cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::TokenKind Kind) { 17965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this character contain the \0 character? 17975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1798661a99690bc133bbaa029da925481d4a860dec90Richard Smith if (!isLexingRawMode() && 1799d4bf760d057f1e8724ab7be340a2d42adae0900aRichard Smith (Kind == tok::utf16_char_constant || Kind == tok::utf32_char_constant)) 1800d4bf760d057f1e8724ab7be340a2d42adae0900aRichard Smith Diag(BufferPtr, getLangOpts().CPlusPlus 1801d4bf760d057f1e8724ab7be340a2d42adae0900aRichard Smith ? diag::warn_cxx98_compat_unicode_literal 1802d4bf760d057f1e8724ab7be340a2d42adae0900aRichard Smith : diag::warn_c99_compat_unicode_literal); 1803661a99690bc133bbaa029da925481d4a860dec90Richard Smith 18045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 18055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\'') { 18064e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!isLexingRawMode() && !LangOpts.AsmPreprocessor) 1807b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith Diag(BufferPtr, diag::ext_empty_character); 18089e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 18095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 18105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1812d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner while (C != '\'') { 1813d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner // Skip escaped characters. 18146d926ae667cca926e77ddce1734514911706ed0aNico Weber if (C == '\\') 18156d926ae667cca926e77ddce1734514911706ed0aNico Weber C = getAndAdvanceChar(CurPtr, Result); 18166d926ae667cca926e77ddce1734514911706ed0aNico Weber 18176d926ae667cca926e77ddce1734514911706ed0aNico Weber if (C == '\n' || C == '\r' || // Newline. 18186d926ae667cca926e77ddce1734514911706ed0aNico Weber (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. 18194e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!isLexingRawMode() && !LangOpts.AsmPreprocessor) 1820b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith Diag(BufferPtr, diag::ext_unterminated_char); 1821d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner FormTokenWithChars(Result, CurPtr-1, tok::unknown); 1822d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner return; 18236d926ae667cca926e77ddce1734514911706ed0aNico Weber } 18246d926ae667cca926e77ddce1734514911706ed0aNico Weber 18256d926ae667cca926e77ddce1734514911706ed0aNico Weber if (C == 0) { 18267d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (isCodeCompletionPoint(CurPtr-1)) { 18277d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 18287d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis FormTokenWithChars(Result, CurPtr-1, tok::unknown); 18297d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return cutOffLexing(); 18307d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 18317d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 1832d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner NulCharacter = CurPtr-1; 1833d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner } 1834d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner C = getAndAdvanceChar(CurPtr, Result); 18355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18375cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith // If we are in C++11, lex the optional ud-suffix. 18384e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus) 18395cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith CurPtr = LexUDSuffix(Result, CurPtr); 18405cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 1841d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner // If a nul character existed in the character, warn about it. 184274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 184374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_char); 18445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 184647246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 18475cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor FormTokenWithChars(Result, CurPtr, Kind); 184847246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 18495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 18505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipWhitespace - Efficiently skip over a series of whitespace characters. 18525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Update BufferPtr to point to the next non-whitespace character and return. 1853d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// 1854d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// This method forms a token and returns true if KeepWhitespaceMode is enabled. 1855d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// 1856d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnerbool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) { 18575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Whitespace - Skip it, then return the token after the whitespace. 18586aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose bool SawNewline = isVerticalWhitespace(CurPtr[-1]); 18596aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose 18605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently. 18615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 18625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip horizontal whitespace very aggressively. 18635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isHorizontalWhitespace(Char)) 18645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = *++CurPtr; 18651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1866ddd3e8b90a92c39f620bda7a0945320c0b9e60dbDaniel Dunbar // Otherwise if we have something other than whitespace, we're done. 18676aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose if (!isVerticalWhitespace(Char)) 18685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 18691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 18715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // End of preprocessor directive line, let LexTokenInternal handle this. 18725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 1873d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return false; 18745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // ok, but handle newline. 18776aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose SawNewline = true; 18785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = *++CurPtr; 18795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1881d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // If the client wants us to return whitespace, return it now. 1882d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (isKeepWhitespaceMode()) { 18839e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 18846aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose if (SawNewline) 18856aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose IsAtStartOfLine = true; 18866aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose // FIXME: The next token will not have LeadingSpace set. 1887d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return true; 1888d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner } 18891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18906aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose // If this isn't immediately after a newline, there is leading space. 18916aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose char PrevChar = CurPtr[-1]; 18926aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose bool HasLeadingSpace = !isVerticalWhitespace(PrevChar); 18936aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose 18946aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose Result.setFlagValue(Token::LeadingSpace, HasLeadingSpace); 18956aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose if (SawNewline) 18966aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose Result.setFlag(Token::StartOfLine); 18976aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose 18985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 1899d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return false; 19005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 19015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1902bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// We have just read the // characters from input. Skip until we find the 1903bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// newline character thats terminate the comment. Then update BufferPtr and 1904bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// return. 1905046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// 1906046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted 1907046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true. 1908bb23628148f555a4cf71f98c27096a7a804c085cNico Weberbool Lexer::SkipLineComment(Token &Result, const char *CurPtr) { 1909bb23628148f555a4cf71f98c27096a7a804c085cNico Weber // If Line comments aren't explicitly enabled for this language, emit an 19105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // extension warning. 1911bb23628148f555a4cf71f98c27096a7a804c085cNico Weber if (!LangOpts.LineComment && !isLexingRawMode()) { 1912bb23628148f555a4cf71f98c27096a7a804c085cNico Weber Diag(BufferPtr, diag::ext_line_comment); 19131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Mark them enabled so we only emit one warning for this translation 19155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // unit. 1916bb23628148f555a4cf71f98c27096a7a804c085cNico Weber LangOpts.LineComment = true; 19175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 19181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan over the body of the comment. The common case, when scanning, is that 19205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the comment contains normal ascii characters with nothing interesting in 19215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // them. As such, optimize for this case with the inner loop. 19225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C; 19235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer do { 19245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr; 19255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip over characters in the fast loop. 19265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != 0 && // Potentially EOF. 19275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C != '\n' && C != '\r') // Newline or DOS-style newline. 19285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *++CurPtr; 19295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 19301daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer const char *NextLine = CurPtr; 19311daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer if (C != 0) { 19321daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer // We found a newline, see if it's escaped. 19331daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer const char *EscapePtr = CurPtr-1; 19341daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer while (isHorizontalWhitespace(*EscapePtr)) // Skip whitespace. 19351daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer --EscapePtr; 19361daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer 19371daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer if (*EscapePtr == '\\') // Escaped newline. 19381daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer CurPtr = EscapePtr; 19391daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer else if (EscapePtr[0] == '/' && EscapePtr[-1] == '?' && 19401daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer EscapePtr[-2] == '?') // Trigraph-escaped newline. 19411daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer CurPtr = EscapePtr-2; 19421daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer else 19431daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer break; // This is a newline, we're done. 19441daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer } 19451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to 1947bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // properly decode the character. Read it in raw mode to avoid emitting 1948bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // diagnostics about things like trigraphs. If we see an escaped newline, 1949bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // we'll handle it below. 19505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *OldPtr = CurPtr; 1951bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner bool OldRawMode = isLexingRawMode(); 1952bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner LexingRawMode = true; 19535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 1954bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner LexingRawMode = OldRawMode; 1955ead616c5d8681a41b11273327813e61bda01907aChris Lattner 19561daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer // If we only read only one character, then no special handling is needed. 19571daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer // We're done and can skip forward to the newline. 19581daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer if (C != 0 && CurPtr == OldPtr+1) { 19591daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer CurPtr = NextLine; 19601daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer break; 19611daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer } 19621daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer 19635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we read multiple characters, and one of those characters was a \r or 19645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n, then we had an escaped newline within the comment. Emit diagnostic 19655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // unless the next line is also a // comment. 19665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') { 19675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (; OldPtr != CurPtr; ++OldPtr) 19685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (OldPtr[0] == '\n' || OldPtr[0] == '\r') { 19695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Okay, we found a // comment that ends in a newline, if the next 19705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // line is also a // comment, but has spaces, don't emit a diagnostic. 19715d6ae288bc661572e52ea71fc14b249eb5e2d9e9Benjamin Kramer if (isWhitespace(C)) { 19725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *ForwardPtr = CurPtr; 19735d6ae288bc661572e52ea71fc14b249eb5e2d9e9Benjamin Kramer while (isWhitespace(*ForwardPtr)) // Skip whitespace. 19745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++ForwardPtr; 19755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/') 19765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 19775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 19781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 197974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 1980bb23628148f555a4cf71f98c27096a7a804c085cNico Weber Diag(OldPtr-1, diag::ext_multi_line_line_comment); 19815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 19825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 19835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 19841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 198555817afdf9d453a443262a733f6caf6692dca118Douglas Gregor if (CurPtr == BufferEnd+1) { 198655817afdf9d453a443262a733f6caf6692dca118Douglas Gregor --CurPtr; 198755817afdf9d453a443262a733f6caf6692dca118Douglas Gregor break; 198855817afdf9d453a443262a733f6caf6692dca118Douglas Gregor } 19897d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 19907d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) { 19917d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 19927d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis cutOffLexing(); 19937d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return false; 19947d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 19957d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 19965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } while (C != '\n' && C != '\r'); 19975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 19983d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner // Found but did not consume the newline. Notify comment handlers about the 19993d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner // comment unless we're in a #if 0 block. 20003d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner if (PP && !isLexingRawMode() && 20013d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr), 20023d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner getSourceLocation(CurPtr)))) { 2003046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner BufferPtr = CurPtr; 2004046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return true; // A token has to be returned. 2005046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner } 20061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are returning comments as tokens, return this comment as a token. 2008fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner if (inKeepCommentMode()) 2009bb23628148f555a4cf71f98c27096a7a804c085cNico Weber return SaveLineComment(Result, CurPtr); 20105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 20115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are inside a preprocessor directive and we see the end of line, 201284021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // return immediately, so that the lexer can return this as an EOD token. 20135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective || CurPtr == BufferEnd) { 20145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 20152d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 20165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 20171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, eat the \n character. We don't care if this is a \n\r or 20197a4f004a77421af876152281b92c4f8ea24afb2dChris Lattner // \r\n sequence. This is an efficiency hack (because we know the \n can't 2020d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // contribute to another token), it isn't needed for correctness. Note that 2021d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // this is ok even in KeepWhitespaceMode, because we would have returned the 2022d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner /// comment above in that mode. 20235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 20241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The next returned token is at the start of the line. 2026d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 20275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // No leading whitespace seen so far. 2028d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::LeadingSpace); 20295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 20302d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 20315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 20325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2033bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// If in save-comment mode, package up this Line comment in an appropriate 2034bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// way and return it. 2035bb23628148f555a4cf71f98c27096a7a804c085cNico Weberbool Lexer::SaveLineComment(Token &Result, const char *CurPtr) { 20369e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // If we're not in a preprocessor directive, just return the // comment 20379e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // directly. 20389e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::comment); 20391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20408c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie if (!ParsingPreprocessorDirective || LexingRawMode) 20419e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner return true; 20421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2043bb23628148f555a4cf71f98c27096a7a804c085cNico Weber // If this Line-style comment is in a macro definition, transmogrify it into 20449e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // a C-style block comment. 2045453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor bool Invalid = false; 2046453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor std::string Spelling = PP->getSpelling(Result, &Invalid); 2047453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor if (Invalid) 2048453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor return true; 2049453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor 2050bb23628148f555a4cf71f98c27096a7a804c085cNico Weber assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not line comment?"); 20519e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Spelling[1] = '*'; // Change prefix to "/*". 20529e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Spelling += "*/"; // add suffix. 20531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20549e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Result.setKind(tok::comment); 2055374b3837d676133fcc1eb70a25c8baf8ec4a5c4aDmitri Gribenko PP->CreateString(Spelling, Result, 2056a08529cc3f00e0b47a3c028823634129ac46847bAbramo Bagnara Result.getLocation(), Result.getLocation()); 20572d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return true; 20585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 20595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 20605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline 206180d7c52653800d3338cca2c4388348010d2b1921David Blaikie/// character (either \\n or \\r) is part of an escaped newline sequence. Issue 206280d7c52653800d3338cca2c4388348010d2b1921David Blaikie/// a diagnostic if so. We know that the newline is inside of a block comment. 20631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr, 20645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Lexer *L) { 20655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(CurPtr[0] == '\n' || CurPtr[0] == '\r'); 20661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Back up off the newline. 20685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 20691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a two-character newline sequence, skip the other character. 20715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == '\n' || CurPtr[0] == '\r') { 20725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n\n or \r\r -> not escaped newline. 20735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == CurPtr[1]) 20745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 20755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n\r or \r\n -> skip the newline. 20765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 20775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 20781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have horizontal whitespace, skip over it. We allow whitespace 20805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // between the slash and newline. 20815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool HasSpace = false; 20825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) { 20835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 20845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer HasSpace = true; 20855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 20861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, we know this is an escaped newline. 20885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (*CurPtr == '\\') { 20895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[-1] != '*') return false; 20905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 20915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It isn't a slash, is it the ?? / trigraph? 20925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' || 20935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[-3] != '*') 20945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 20951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // This is the trigraph ending the comment. Emit a stern warning! 20975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr -= 2; 20985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 20995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If no trigraphs are enabled, warn that we ignored this trigraph and 21005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // ignore this * character. 21014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!L->getLangOpts().Trigraphs) { 210274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 210374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::trigraph_ignored_block_comment); 21045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 21055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 210674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 210774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::trigraph_ends_block_comment); 21085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 21091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Warn about having an escaped newline between the */ characters. 211174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 211274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::escaped_newline_block_comment_end); 21131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If there was space between the backslash and newline, warn about it. 211574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (HasSpace && !L->isLexingRawMode()) 211674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::backslash_newline_space); 21171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 21195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 21205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 21215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__ 21225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <emmintrin.h> 21235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__ 21245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <altivec.h> 21255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#undef bool 21265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif 21275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2128ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// We have just read from input the / and * characters that started a comment. 2129ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// Read until we find the * and / characters that terminate the comment. 2130ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// Note that we don't bother decoding trigraphs or escaped newlines in block 2131ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// comments, because they cannot cause the comment to end. The only thing 2132ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// that can happen is the comment could end with an escaped newline between 2133ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// the terminating * and /. 21342d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner/// 2135046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted 2136046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true. 2137d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) { 21385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan one character past where we should, looking for a '/' character. Once 2139fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner // we find it, check to see if it was preceded by a *. This common 21405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // optimization helps people who like to put a lot of * characters in their 21415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // comments. 21428146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner 21438146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // The first character we get with newlines and trigraphs skipped to handle 21448146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // the degenerate /*/ case below correctly if the * has an escaped newline 21458146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // after it. 21468146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner unsigned CharSize; 21478146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner unsigned char C = getCharAndSize(CurPtr, CharSize); 21488146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner CurPtr += CharSize; 21495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == 0 && CurPtr == BufferEnd+1) { 21507d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (!isLexingRawMode()) 21510af574270d3be2b0e73a3379dfaa633746f8fc6fChris Lattner Diag(BufferPtr, diag::err_unterminated_block_comment); 215231f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner --CurPtr; 21531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 215431f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // KeepWhitespaceMode should return this broken comment as a token. Since 215531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // it isn't a well formed comment, just return it as an 'unknown' token. 215631f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner if (isKeepWhitespaceMode()) { 21579e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 215831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner return true; 215931f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner } 21601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 216131f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner BufferPtr = CurPtr; 21622d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 21635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 21641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21658146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // Check to see if the first character after the '/*' is another /. If so, 21668146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // then this slash does not end the block comment, it is part of it. 21678146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner if (C == '/') 21688146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner C = *CurPtr++; 21691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 21715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip over all non-interesting characters until we find end of buffer or a 21725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // (probably ending) '/' character. 21737d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (CurPtr + 24 < BufferEnd && 21747d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis // If there is a code-completion point avoid the fast scan because it 21757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis // doesn't check for '\0'. 21767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis !(PP && PP->getCodeCompletionFileLoc() == FileLoc)) { 21775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // While not aligned to a 16-byte boundary. 21785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0) 21795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 21801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '/') goto FoundSlash; 21825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 21835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__ 21843f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer __m128i Slashes = _mm_set1_epi8('/'); 21853f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer while (CurPtr+16 <= BufferEnd) { 218631ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky int cmp = _mm_movemask_epi8(_mm_cmpeq_epi8(*(const __m128i*)CurPtr, 218731ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky Slashes)); 21883f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer if (cmp != 0) { 21896300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer // Adjust the pointer to point directly after the first slash. It's 21906300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer // not necessary to set C here, it will be overwritten at the end of 21916300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer // the outer loop. 21926300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer CurPtr += llvm::CountTrailingZeros_32(cmp) + 1; 21933f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer goto FoundSlash; 21943f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer } 21955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 16; 21963f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer } 21975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__ 21985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer __vector unsigned char Slashes = { 21991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump '/', '/', '/', '/', '/', '/', '/', '/', 22005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer '/', '/', '/', '/', '/', '/', '/', '/' 22015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer }; 22025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (CurPtr+16 <= BufferEnd && 22035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes)) 22045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 16; 22051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#else 22065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan for '/' quickly. Many block comments are very large. 22075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (CurPtr[0] != '/' && 22085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[1] != '/' && 22095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[2] != '/' && 22105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[3] != '/' && 22115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr+4 < BufferEnd) { 22125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 4; 22135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif 22151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It has to be one of the bytes scanned, increment to it and read one. 22175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 22185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Loop to scan the remainder. 22215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '/' && C != '\0') 22225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 22231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '/') { 22253f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer FoundSlash: 22265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[-2] == '*') // We found the final */. We're done! 22275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) { 22305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) { 22315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We found the final */, though it had an escaped newline between the 22325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // * and /. We're done! 22335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == '*' && CurPtr[1] != '/') { 22375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a /* inside of the comment, emit a warning. Don't do this 22385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // if this is a /*/, which will end the comment. This misses cases with 22395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // embedded escaped newlines, but oh well. 224074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 224174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::warn_nested_block_comment); 22425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == 0 && CurPtr == BufferEnd+1) { 22447d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (!isLexingRawMode()) 224574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(BufferPtr, diag::err_unterminated_block_comment); 22465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Note: the user probably forgot a */. We could continue immediately 22475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // after the /*, but this would involve lexing a lot of what really is the 22485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // comment, which surely would confuse the parser. 224931f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner --CurPtr; 22501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 225131f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // KeepWhitespaceMode should return this broken comment as a token. Since 225231f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // it isn't a well formed comment, just return it as an 'unknown' token. 225331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner if (isKeepWhitespaceMode()) { 22549e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 225531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner return true; 225631f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner } 22571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 225831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner BufferPtr = CurPtr; 22592d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 22607d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } else if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) { 22617d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 22627d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis cutOffLexing(); 22637d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return false; 22645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22657d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 22665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 22675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22693d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner // Notify comment handlers about the comment unless we're in a #if 0 block. 22703d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner if (PP && !isLexingRawMode() && 22713d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr), 22723d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner getSourceLocation(CurPtr)))) { 2273046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner BufferPtr = CurPtr; 2274046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return true; // A token has to be returned. 2275046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner } 22762e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 22775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are returning comments as tokens, return this comment as a token. 2278fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner if (inKeepCommentMode()) { 22799e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::comment); 22802d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return true; 22815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 22835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It is common for the tokens immediately after a /**/ comment to be 22845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // whitespace. Instead of going through the big switch, handle it 2285d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // efficiently now. This is safe even in KeepWhitespaceMode because we would 2286d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // have already returned above with the comment as a token. 22875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isHorizontalWhitespace(*CurPtr)) { 22885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SkipWhitespace(Result, CurPtr+1); 22892d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 22905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 22925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, just return so that the next character will be lexed as a token. 22935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 2294d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 22952d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 22965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 22975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 22985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 22995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Lexing Entry Points 23005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 23015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 23025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ReadToEndOfLine - Read the rest of the current preprocessor line as an 23035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// uninterpreted string. This switches the lexer out of directive mode. 23043093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramervoid Lexer::ReadToEndOfLine(SmallVectorImpl<char> *Result) { 23055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(ParsingPreprocessorDirective && ParsingFilename == false && 23065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer "Must be in a preprocessing directive!"); 2307d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token Tmp; 23085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 23095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // CurPtr - Cache BufferPtr in an automatic variable. 23105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *CurPtr = BufferPtr; 23115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 23125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Char = getAndAdvanceChar(CurPtr, Tmp); 23135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Char) { 23145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 23153093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer if (Result) 23163093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer Result->push_back(Char); 23175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 23185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 0: // Null. 23195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Found end of file? 23205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr-1 != BufferEnd) { 23217d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (isCodeCompletionPoint(CurPtr-1)) { 23227d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 23237d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis cutOffLexing(); 23243093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer return; 23257d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 23267d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 23275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Nope, normal character, continue. 23283093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer if (Result) 23293093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer Result->push_back(Char); 23305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 23315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FALL THROUGH. 23335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\r': 23345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\n': 23355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Okay, we found the end of the line. First, back up past the \0, \r, \n. 23365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(CurPtr[-1] == Char && "Trigraphs for newline?"); 23375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr-1; 23381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 233984021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // Next, lex the character, which should handle the EOD transition. 23405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Lex(Tmp); 234155817afdf9d453a443262a733f6caf6692dca118Douglas Gregor if (Tmp.is(tok::code_completion)) { 23427d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (PP) 23437d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 234455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor Lex(Tmp); 234555817afdf9d453a443262a733f6caf6692dca118Douglas Gregor } 234684021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne assert(Tmp.is(tok::eod) && "Unexpected token!"); 23471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23483093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer // Finally, we're done; 23493093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer return; 23505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 23535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 23545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexEndOfFile - CurPtr points to the end of this file. Handle this 23555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// condition, reporting diagnostics and handling other edge cases as required. 23565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This returns true if Result contains a token, false if PP.Lex should be 23575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// called again. 2358d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) { 23595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we hit the end of the file while parsing a preprocessor directive, 23605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // end the preprocessor directive first. The next token returned will 23615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // then be the end of file. 23625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 23635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Done parsing the "line". 23645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParsingPreprocessorDirective = false; 23655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 236684021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne FormTokenWithChars(Result, CurPtr, tok::eod); 23671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore comment saving mode, in case it was disabled for directive. 23696aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose resetExtendedTokenMode(); 23705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; // Have a token. 23711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 237286d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor 237386d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor // If we are in raw mode, return this event as an EOF token. Let the caller 237486d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor // that put us in raw mode handle the event. 237586d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor if (isLexingRawMode()) { 237686d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor Result.startToken(); 237786d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor BufferPtr = BufferEnd; 237886d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor FormTokenWithChars(Result, BufferEnd, tok::eof); 237986d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor return true; 238086d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor } 238181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor 2382f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor // Issue diagnostics for unterminated #if and missing newline. 2383f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor 23845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are in a #if directive, emit an error. 23855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (!ConditionalStack.empty()) { 23867d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (PP->getCodeCompletionFileLoc() != FileLoc) 23872d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor PP->Diag(ConditionalStack.back().IfLoc, 23882d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor diag::err_pp_unterminated_conditional); 23895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConditionalStack.pop_back(); 23905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2392b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue 2393b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner // a pedwarn. 23945e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) 239580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith Diag(BufferEnd, LangOpts.CPlusPlus11 ? // C++11 [lex.phases] 2.2 p2 23965e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell diag::warn_cxx98_compat_no_newline_eof : diag::ext_no_newline_eof) 23975e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n"); 23981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 24005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 24015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Finally, let the preprocessor handle this. 24020cdd1fe3ec29b5cbff9a728966ace5c5b5d614f7Jordan Rose return PP->HandleEndOfFile(Result, isPragmaLexer()); 24035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 24045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 24055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from 24065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified lexer will return a tok::l_paren token, 0 if it is something 24075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// else and 2 if there are no more tokens in the buffer controlled by the 24085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexer. 24095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerunsigned Lexer::isNextPPTokenLParen() { 24105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?"); 24111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Switch to 'skipping' mode. This will ensure that we can lex a token 24135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // without emitting diagnostics, disables macro expansion, and will cause EOF 24145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // to return an EOF token instead of popping the include stack. 24155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexingRawMode = true; 24161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Save state that can be changed while lexing so that we can restore it. 24185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *TmpBufferPtr = BufferPtr; 2419a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner bool inPPDirectiveMode = ParsingPreprocessorDirective; 24201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2421d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token Tok; 24225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Tok.startToken(); 24235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexTokenInternal(Tok); 24241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore state that may have changed. 24265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = TmpBufferPtr; 2427a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner ParsingPreprocessorDirective = inPPDirectiveMode; 24281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore the lexer back to non-skipping mode. 24305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexingRawMode = false; 24311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 243222f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner if (Tok.is(tok::eof)) 24335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return 2; 243422f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner return Tok.is(tok::l_paren); 24355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 24365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2437ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// \brief Find the end of a version control conflict marker. 2438d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smithstatic const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd, 2439d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith ConflictMarkerKind CMK) { 2440d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith const char *Terminator = CMK == CMK_Perforce ? "<<<<\n" : ">>>>>>>"; 2441d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith size_t TermLen = CMK == CMK_Perforce ? 5 : 7; 2442d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith StringRef RestOfBuffer(CurPtr+TermLen, BufferEnd-CurPtr-TermLen); 2443d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith size_t Pos = RestOfBuffer.find(Terminator); 24445f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner while (Pos != StringRef::npos) { 244534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Must occur at start of line. 244634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (RestOfBuffer[Pos-1] != '\r' && 244734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner RestOfBuffer[Pos-1] != '\n') { 2448d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith RestOfBuffer = RestOfBuffer.substr(Pos+TermLen); 2449d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith Pos = RestOfBuffer.find(Terminator); 245034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner continue; 245134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 245234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return RestOfBuffer.data()+Pos; 245334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 245434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return 0; 245534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner} 245634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 245734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// IsStartOfConflictMarker - If the specified pointer is the start of a version 245834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// control conflict marker like '<<<<<<<', recognize it as such, emit an error 245934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// and recover nicely. This returns true if it is a conflict marker and false 246034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// if not. 246134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::IsStartOfConflictMarker(const char *CurPtr) { 246234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Only a conflict marker if it starts at the beginning of a line. 246334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr != BufferStart && 246434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr[-1] != '\n' && CurPtr[-1] != '\r') 246534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 246634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 2467d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // Check to see if we have <<<<<<< or >>>>. 2468d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if ((BufferEnd-CurPtr < 8 || StringRef(CurPtr, 7) != "<<<<<<<") && 2469d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith (BufferEnd-CurPtr < 6 || StringRef(CurPtr, 5) != ">>>> ")) 247034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 247134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 247234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If we have a situation where we don't care about conflict markers, ignore 247334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // it. 2474d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if (CurrentConflictMarkerState || isLexingRawMode()) 247534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 247634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 2477d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith ConflictMarkerKind Kind = *CurPtr == '<' ? CMK_Normal : CMK_Perforce; 2478d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith 2479d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // Check to see if there is an ending marker somewhere in the buffer at the 2480d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // start of a line to terminate this conflict marker. 2481d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if (FindConflictEnd(CurPtr, BufferEnd, Kind)) { 248234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // We found a match. We are really in a conflict marker. 248334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Diagnose this, and ignore to the end of line. 248434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Diag(CurPtr, diag::err_conflict_marker); 2485d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith CurrentConflictMarkerState = Kind; 248634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 248734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Skip ahead to the end of line. We know this exists because the 248834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // end-of-conflict marker starts with \r or \n. 248934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner while (*CurPtr != '\r' && *CurPtr != '\n') { 249034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner assert(CurPtr != BufferEnd && "Didn't find end of line"); 249134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner ++CurPtr; 249234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 249334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner BufferPtr = CurPtr; 249434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return true; 249534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 249634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 249734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // No end of conflict marker found. 249834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 249934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner} 250034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 250134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 2502d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// HandleEndOfConflictMarker - If this is a '====' or '||||' or '>>>>', or if 2503d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// it is '<<<<' and the conflict marker started with a '>>>>' marker, then it 2504d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// is the end of a conflict marker. Handle it by ignoring up until the end of 2505d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// the line. This returns true if it is a conflict marker and false if not. 250634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::HandleEndOfConflictMarker(const char *CurPtr) { 250734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Only a conflict marker if it starts at the beginning of a line. 250834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr != BufferStart && 250934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr[-1] != '\n' && CurPtr[-1] != '\r') 251034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 251134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 251234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If we have a situation where we don't care about conflict markers, ignore 251334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // it. 2514d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if (!CurrentConflictMarkerState || isLexingRawMode()) 251534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 251634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 2517d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // Check to see if we have the marker (4 characters in a row). 2518d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith for (unsigned i = 1; i != 4; ++i) 251934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr[i] != CurPtr[0]) 252034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 252134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 252234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If we do have it, search for the end of the conflict marker. This could 252334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // fail if it got skipped with a '#if 0' or something. Note that CurPtr might 252434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // be the end of conflict marker. 2525d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if (const char *End = FindConflictEnd(CurPtr, BufferEnd, 2526d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith CurrentConflictMarkerState)) { 252734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = End; 252834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 252934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Skip ahead to the end of line. 253034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n') 253134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner ++CurPtr; 253234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 253334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner BufferPtr = CurPtr; 253434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 253534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // No longer in the conflict marker. 2536d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith CurrentConflictMarkerState = CMK_None; 253734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return true; 253834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 253934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 254034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 254134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner} 254234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 25437d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidisbool Lexer::isCodeCompletionPoint(const char *CurPtr) const { 25447d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (PP && PP->isCodeCompletionEnabled()) { 2545a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis SourceLocation Loc = FileLoc.getLocWithOffset(CurPtr-BufferStart); 25467d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return Loc == PP->getCodeCompletionLoc(); 25477d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 25487d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 25497d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return false; 25507d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis} 25517d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 2552c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Roseuint32_t Lexer::tryReadUCN(const char *&StartPtr, const char *SlashLoc, 2553c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Token *Result) { 2554c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose unsigned CharSize; 2555c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose char Kind = getCharAndSize(StartPtr, CharSize); 2556c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2557c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose unsigned NumHexDigits; 2558c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (Kind == 'u') 2559c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose NumHexDigits = 4; 2560c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose else if (Kind == 'U') 2561c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose NumHexDigits = 8; 2562c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose else 2563c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return 0; 2564c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2565bfec916e5fc40f22ac11267e78a024cd8dcf3bbfJordan Rose if (!LangOpts.CPlusPlus && !LangOpts.C99) { 25668094bac4e987caf90e8fd719c24545add8dafcb6Jordan Rose if (Result && !isLexingRawMode()) 25678094bac4e987caf90e8fd719c24545add8dafcb6Jordan Rose Diag(SlashLoc, diag::warn_ucn_not_valid_in_c89); 2568bfec916e5fc40f22ac11267e78a024cd8dcf3bbfJordan Rose return 0; 2569bfec916e5fc40f22ac11267e78a024cd8dcf3bbfJordan Rose } 2570bfec916e5fc40f22ac11267e78a024cd8dcf3bbfJordan Rose 2571c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose const char *CurPtr = StartPtr + CharSize; 2572c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose const char *KindLoc = &CurPtr[-1]; 2573c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2574c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose uint32_t CodePoint = 0; 2575c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose for (unsigned i = 0; i < NumHexDigits; ++i) { 2576c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose char C = getCharAndSize(CurPtr, CharSize); 2577c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2578c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose unsigned Value = llvm::hexDigitValue(C); 2579c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (Value == -1U) { 2580c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (Result && !isLexingRawMode()) { 2581c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (i == 0) { 2582c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Diag(BufferPtr, diag::warn_ucn_escape_no_digits) 2583c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose << StringRef(KindLoc, 1); 2584c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } else { 2585c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Diag(BufferPtr, diag::warn_ucn_escape_incomplete); 2586b87672b124ab4fbe6f2cabc2ad71655fc71230eaJordan Rose 2587b87672b124ab4fbe6f2cabc2ad71655fc71230eaJordan Rose // If the user wrote \U1234, suggest a fixit to \u. 2588b87672b124ab4fbe6f2cabc2ad71655fc71230eaJordan Rose if (i == 4 && NumHexDigits == 8) { 2589ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose CharSourceRange URange = makeCharRange(*this, KindLoc, KindLoc + 1); 2590b87672b124ab4fbe6f2cabc2ad71655fc71230eaJordan Rose Diag(KindLoc, diag::note_ucn_four_not_eight) 2591b87672b124ab4fbe6f2cabc2ad71655fc71230eaJordan Rose << FixItHint::CreateReplacement(URange, "u"); 2592b87672b124ab4fbe6f2cabc2ad71655fc71230eaJordan Rose } 2593c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2594c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2595bfec916e5fc40f22ac11267e78a024cd8dcf3bbfJordan Rose 2596c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return 0; 2597c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2598c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2599c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose CodePoint <<= 4; 2600c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose CodePoint += Value; 2601c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2602c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose CurPtr += CharSize; 2603c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2604c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2605c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (Result) { 2606c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Result->setFlag(Token::HasUCN); 2607b6c08a64145485a7c233761220b8d82b74aa7546NAKAMURA Takumi if (CurPtr - StartPtr == (ptrdiff_t)NumHexDigits + 2) 2608c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose StartPtr = CurPtr; 2609c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose else 2610c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose while (StartPtr != CurPtr) 2611c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose (void)getAndAdvanceChar(StartPtr, *Result); 2612c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } else { 2613c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose StartPtr = CurPtr; 2614c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2615c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2616c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // C99 6.4.3p2: A universal character name shall not specify a character whose 2617c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // short identifier is less than 00A0 other than 0024 ($), 0040 (@), or 2618c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // 0060 (`), nor one in the range D800 through DFFF inclusive.) 2619c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // C++11 [lex.charset]p2: If the hexadecimal value for a 2620c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // universal-character-name corresponds to a surrogate code point (in the 2621c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // range 0xD800-0xDFFF, inclusive), the program is ill-formed. Additionally, 2622c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // if the hexadecimal value for a universal-character-name outside the 2623c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // c-char-sequence, s-char-sequence, or r-char-sequence of a character or 2624c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // string literal corresponds to a control character (in either of the 2625c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // ranges 0x00-0x1F or 0x7F-0x9F, both inclusive) or to a character in the 2626c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // basic source character set, the program is ill-formed. 2627c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (CodePoint < 0xA0) { 2628c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (CodePoint == 0x24 || CodePoint == 0x40 || CodePoint == 0x60) 2629c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return CodePoint; 2630c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2631c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // We don't use isLexingRawMode() here because we need to warn about bad 2632c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // UCNs even when skipping preprocessing tokens in a #if block. 2633c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (Result && PP) { 2634c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (CodePoint < 0x20 || CodePoint >= 0x7F) 2635c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Diag(BufferPtr, diag::err_ucn_control_character); 2636c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose else { 2637c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose char C = static_cast<char>(CodePoint); 2638c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Diag(BufferPtr, diag::err_ucn_escape_basic_scs) << StringRef(&C, 1); 2639c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2640c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2641c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2642c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return 0; 2643ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose 2644ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } else if (CodePoint >= 0xD800 && CodePoint <= 0xDFFF) { 2645c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // C++03 allows UCNs representing surrogate characters. C99 and C++11 don't. 2646ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose // We don't use isLexingRawMode() here because we need to diagnose bad 2647c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // UCNs even when skipping preprocessing tokens in a #if block. 2648ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (Result && PP) { 2649ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (LangOpts.CPlusPlus && !LangOpts.CPlusPlus11) 2650ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose Diag(BufferPtr, diag::warn_ucn_escape_surrogate); 2651ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose else 2652ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose Diag(BufferPtr, diag::err_ucn_escape_invalid); 2653ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 2654c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return 0; 2655c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2656c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2657c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return CodePoint; 2658c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose} 2659c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2660c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rosevoid Lexer::LexUnicode(Token &Result, uint32_t C, const char *CurPtr) { 266174c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose if (!isLexingRawMode() && !PP->isPreprocessedOutput() && 2662ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose isCharInSet(C, UnicodeWhitespaceChars)) { 266374c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose Diag(BufferPtr, diag::ext_unicode_whitespace) 2664ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose << makeCharRange(*this, BufferPtr, CurPtr); 2665fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose 2666fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose Result.setFlag(Token::LeadingSpace); 2667fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose if (SkipWhitespace(Result, CurPtr)) 2668fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose return; // KeepWhitespaceMode 2669fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose 2670fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose return LexTokenInternal(Result); 2671fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose } 2672fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose 2673ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (isAllowedIDChar(C, LangOpts) && isAllowedInitiallyIDChar(C, LangOpts)) { 2674ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (!isLexingRawMode() && !ParsingPreprocessorDirective && 2675ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose !PP->isPreprocessedOutput()) { 2676ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose maybeDiagnoseIDCharCompat(PP->getDiagnostics(), C, 2677ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose makeCharRange(*this, BufferPtr, CurPtr), 2678ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose /*IsFirst=*/true); 2679ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 2680ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose 2681c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose MIOpt.ReadToken(); 2682c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return LexIdentifier(Result, CurPtr); 2683c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2684c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 26850ed439487491e09faffdbabfacb1d050292c7723Jordan Rose if (!isLexingRawMode() && !ParsingPreprocessorDirective && 26860ed439487491e09faffdbabfacb1d050292c7723Jordan Rose !PP->isPreprocessedOutput() && 2687ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose !isASCII(*BufferPtr) && !isAllowedIDChar(C, LangOpts)) { 2688c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // Non-ASCII characters tend to creep into source code unintentionally. 2689c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // Instead of letting the parser complain about the unknown token, 2690c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // just drop the character. 2691c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // Note that we can /only/ do this when the non-ASCII character is actually 2692c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // spelled as Unicode, not written as a UCN. The standard requires that 2693c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // we not throw away any possible preprocessor tokens, but there's a 2694c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // loophole in the mapping of Unicode characters to basic character set 2695c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // characters that allows us to map these particular characters to, say, 2696c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // whitespace. 269774c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose Diag(BufferPtr, diag::err_non_ascii) 2698ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose << FixItHint::CreateRemoval(makeCharRange(*this, BufferPtr, CurPtr)); 2699c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2700c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose BufferPtr = CurPtr; 2701c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return LexTokenInternal(Result); 2702c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2703c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2704c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // Otherwise, we have an explicit UCN or a character that's unlikely to show 2705c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // up by accident. 2706c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose MIOpt.ReadToken(); 2707c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose FormTokenWithChars(Result, CurPtr, tok::unknown); 2708c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose} 2709c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 27105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 27115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexTokenInternal - This implements a simple C family lexer. It is an 27125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// extremely performance critical piece of code. This assumes that the buffer 2713efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// has a null character at the end of the file. This returns a preprocessing 2714efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// token, not a normal token, as such, it is an internal interface. It assumes 2715efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// that the Flags of result have been cleared before calling this. 2716d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexTokenInternal(Token &Result) { 27175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerLexNextToken: 27185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // New token, can't need cleaning yet. 2719d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::NeedsCleaning); 27205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result.setIdentifierInfo(0); 27211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // CurPtr - Cache BufferPtr in an automatic variable. 27235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *CurPtr = BufferPtr; 27245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 27255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Small amounts of horizontal whitespace is very common between tokens. 27265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if ((*CurPtr == ' ') || (*CurPtr == '\t')) { 27275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 27285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while ((*CurPtr == ' ') || (*CurPtr == '\t')) 27295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 27301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2731d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // If we are keeping whitespace and other tokens, just return what we just 2732d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // skipped. The next lexer invocation will return the token after the 2733d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // whitespace. 2734d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (isKeepWhitespaceMode()) { 27359e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 27366aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose // FIXME: The next token will not have LeadingSpace set. 2737d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; 2738d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner } 27391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 2741d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 27425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 27431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below. 27451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Read a character, advancing over it. 27475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Char = getAndAdvanceChar(CurPtr, Result); 27489e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner tok::TokenKind Kind; 27491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Char) { 27515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 0: // Null. 27525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Found end of file? 27535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr-1 == BufferEnd) { 27545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Read the PP instance variable into an automatic variable, because 27555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // LexEndOfFile will often delete 'this'. 2756168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner Preprocessor *PPCache = PP; 27575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. 27585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; // Got a token to return. 2759168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); 2760168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner return PPCache->Lex(Result); 27615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 27621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27637d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis // Check if we are performing code completion. 27647d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (isCodeCompletionPoint(CurPtr-1)) { 27657d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis // Return the code-completion token. 27667d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis Result.startToken(); 27677d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis FormTokenWithChars(Result, CurPtr, tok::code_completion); 27687d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return; 27697d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 27707d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 277174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 277274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::null_in_file); 2773d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 2774d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 2775d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 27761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 2778a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner 2779a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner case 26: // DOS & CP/M EOF: "^Z". 2780a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // If we're in Microsoft extensions mode, treat this as end of file. 27814e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.MicrosoftExt) { 2782a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // Read the PP instance variable into an automatic variable, because 2783a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // LexEndOfFile will often delete 'this'. 2784a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner Preprocessor *PPCache = PP; 2785a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. 2786a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner return; // Got a token to return. 2787a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); 2788a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner return PPCache->Lex(Result); 2789a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner } 2790a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // If Microsoft extensions are disabled, this is just random garbage. 2791a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner Kind = tok::unknown; 2792a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner break; 2793a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner 27945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\n': 27955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\r': 27965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are inside a preprocessor directive and we see the end of line, 279784021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // we know we are done with the directive, so return an EOD token. 27985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 27995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Done parsing the "line". 28005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParsingPreprocessorDirective = false; 28011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 28025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore comment saving mode, in case it was disabled for directive. 28031a8354659a6007bbae3b5d9161a56ecc8f61a219David Blaikie if (PP) 28046aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose resetExtendedTokenMode(); 28051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 28065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Since we consumed a newline, we are back at the start of a line. 28075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IsAtStartOfLine = true; 28081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 280984021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne Kind = tok::eod; 28105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 28115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 28126aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose 28135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // No leading whitespace seen so far. 2814d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::LeadingSpace); 28151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2816d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 2817d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 28185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 28195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ' ': 28205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\t': 28215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\f': 28225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\v': 28238133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner SkipHorizontalWhitespace: 2824d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 2825d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 2826d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 28278133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner 28288133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner SkipIgnoredUnits: 28298133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner CurPtr = BufferPtr; 28301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 28318133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner // If the next token is obviously a // or /* */ comment, skip it efficiently 28328133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner // too (without going through the big switch stmt). 28338402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() && 2834bb23628148f555a4cf71f98c27096a7a804c085cNico Weber LangOpts.LineComment && !LangOpts.TraditionalCPP) { 2835bb23628148f555a4cf71f98c27096a7a804c085cNico Weber if (SkipLineComment(Result, CurPtr+2)) 2836046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 28378133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipIgnoredUnits; 2838fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) { 2839046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner if (SkipBlockComment(Result, CurPtr+2)) 2840046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 28418133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipIgnoredUnits; 28428133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner } else if (isHorizontalWhitespace(*CurPtr)) { 28438133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipHorizontalWhitespace; 28448133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner } 28455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 2846a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner 28473a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // C99 6.4.4.1: Integer Constants. 28483a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // C99 6.4.4.2: Floating Constants. 28493a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '0': case '1': case '2': case '3': case '4': 28503a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '5': case '6': case '7': case '8': case '9': 28513a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Notify MIOpt that we read a non-whitespace/non-comment token. 28523a5707766850f9ee9daa35299794328b5caf96dcChris Lattner MIOpt.ReadToken(); 28533a5707766850f9ee9daa35299794328b5caf96dcChris Lattner return LexNumericConstant(Result, CurPtr); 28541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 28550093e12513c5c896434915d5e9126f51b780aa61Richard Smith case 'u': // Identifier (uber) or C11/C++11 UTF-8 or UTF-16 string literal 28565cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // Notify MIOpt that we read a non-whitespace/non-comment token. 28575cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor MIOpt.ReadToken(); 28585cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 28590093e12513c5c896434915d5e9126f51b780aa61Richard Smith if (LangOpts.CPlusPlus11 || LangOpts.C11) { 28605cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor Char = getCharAndSize(CurPtr, SizeTmp); 28615cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 28625cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // UTF-16 string literal 28635cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (Char == '"') 28645cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), 28655cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::utf16_string_literal); 28665cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 28675cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // UTF-16 character constant 28685cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (Char == '\'') 28695cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result), 28705cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::utf16_char_constant); 28715cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 28722fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // UTF-16 raw string literal 28730093e12513c5c896434915d5e9126f51b780aa61Richard Smith if (Char == 'R' && LangOpts.CPlusPlus11 && 28740093e12513c5c896434915d5e9126f51b780aa61Richard Smith getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"') 28752fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 28762fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 28772fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 28782fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::utf16_string_literal); 28792fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 28802fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char == '8') { 28812fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper char Char2 = getCharAndSize(CurPtr + SizeTmp, SizeTmp2); 28822fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 28832fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // UTF-8 string literal 28842fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char2 == '"') 28852fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexStringLiteral(Result, 28862fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 28872fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 28882fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::utf8_string_literal); 28892fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 28900093e12513c5c896434915d5e9126f51b780aa61Richard Smith if (Char2 == 'R' && LangOpts.CPlusPlus11) { 28912fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper unsigned SizeTmp3; 28922fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper char Char3 = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3); 28932fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // UTF-8 raw string literal 28942fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char3 == '"') { 28952fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 28962fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 28972fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 28982fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp3, Result), 28992fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::utf8_string_literal); 29002fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 29012fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 29022fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 29035cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor } 29045cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 29055cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // treat u like the start of an identifier. 29065cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexIdentifier(Result, CurPtr); 29075cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 29080093e12513c5c896434915d5e9126f51b780aa61Richard Smith case 'U': // Identifier (Uber) or C11/C++11 UTF-32 string literal 29095cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // Notify MIOpt that we read a non-whitespace/non-comment token. 29105cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor MIOpt.ReadToken(); 29115cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 29120093e12513c5c896434915d5e9126f51b780aa61Richard Smith if (LangOpts.CPlusPlus11 || LangOpts.C11) { 29135cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor Char = getCharAndSize(CurPtr, SizeTmp); 29145cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 29155cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // UTF-32 string literal 29165cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (Char == '"') 29175cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), 29185cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::utf32_string_literal); 29195cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 29205cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // UTF-32 character constant 29215cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (Char == '\'') 29225cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result), 29235cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::utf32_char_constant); 29242fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 29252fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // UTF-32 raw string literal 29260093e12513c5c896434915d5e9126f51b780aa61Richard Smith if (Char == 'R' && LangOpts.CPlusPlus11 && 29270093e12513c5c896434915d5e9126f51b780aa61Richard Smith getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"') 29282fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 29292fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 29302fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 29312fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::utf32_string_literal); 29325cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor } 29335cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 29345cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // treat U like the start of an identifier. 29355cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexIdentifier(Result, CurPtr); 29365cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 29372fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper case 'R': // Identifier or C++0x raw string literal 29382fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Notify MIOpt that we read a non-whitespace/non-comment token. 29392fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper MIOpt.ReadToken(); 29402fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 294180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (LangOpts.CPlusPlus11) { 29422fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Char = getCharAndSize(CurPtr, SizeTmp); 29432fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 29442fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char == '"') 29452fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 29462fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(CurPtr, SizeTmp, Result), 29472fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::string_literal); 29482fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 29492fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 29502fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // treat R like the start of an identifier. 29512fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexIdentifier(Result, CurPtr); 29522fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 29533a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz"). 29545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 29555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 29565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 29575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 29585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Wide string literal. 29595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '"') 29605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), 29615cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::wide_string_literal); 29625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 29632fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Wide raw string literal. 296480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (LangOpts.CPlusPlus11 && Char == 'R' && 29652fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"') 29662fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 29672fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 29682fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 29692fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::wide_string_literal); 29702fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 29715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Wide character constant. 29725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '\'') 29735cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result), 29745cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::wide_char_constant); 29755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FALL THROUGH, treating L like the start of an identifier. 29761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 29775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.2: Identifiers. 29785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': 29795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N': 29802fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper case 'O': case 'P': case 'Q': /*'R'*/case 'S': case 'T': /*'U'*/ 29815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'V': case 'W': case 'X': case 'Y': case 'Z': 29825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': 29835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': 29845cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor case 'o': case 'p': case 'q': case 'r': case 's': case 't': /*'u'*/ 29855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'v': case 'w': case 'x': case 'y': case 'z': 29865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '_': 29875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 29885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 29895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexIdentifier(Result, CurPtr); 29903a5707766850f9ee9daa35299794328b5caf96dcChris Lattner 29913a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '$': // $ in identifiers. 29924e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.DollarIdents) { 299374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 299474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::ext_dollar_in_identifier); 29953a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Notify MIOpt that we read a non-whitespace/non-comment token. 29963a5707766850f9ee9daa35299794328b5caf96dcChris Lattner MIOpt.ReadToken(); 29973a5707766850f9ee9daa35299794328b5caf96dcChris Lattner return LexIdentifier(Result, CurPtr); 29983a5707766850f9ee9daa35299794328b5caf96dcChris Lattner } 29991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 30009e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 30013a5707766850f9ee9daa35299794328b5caf96dcChris Lattner break; 30021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 30035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.4: Character Constants. 30045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\'': 30055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 30065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 30075cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexCharConstant(Result, CurPtr, tok::char_constant); 30085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 30095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.5: String Literals. 30105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '"': 30115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 30125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 30135cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexStringLiteral(Result, CurPtr, tok::string_literal); 30145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 30155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.6: Punctuators. 30165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '?': 30179e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::question; 30185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '[': 30209e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_square; 30215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ']': 30239e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_square; 30245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '(': 30269e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_paren; 30275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ')': 30299e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_paren; 30305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '{': 30329e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_brace; 30335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '}': 30359e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_brace; 30365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '.': 30385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 30395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char >= '0' && Char <= '9') { 30405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 30415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 30425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 30435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); 30444e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.CPlusPlus && Char == '*') { 30459e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::periodstar; 30465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += SizeTmp; 30475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '.' && 30485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') { 30499e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ellipsis; 30505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 30515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 30525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 30539e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::period; 30545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 30555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '&': 30575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 30585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '&') { 30599e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ampamp; 30605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 30629e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ampequal; 30635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 30659e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::amp; 30665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 30675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case '*': 30695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (getCharAndSize(CurPtr, SizeTmp) == '=') { 30709e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::starequal; 30715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 30739e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::star; 30745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 30755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '+': 30775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 30785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '+') { 30795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30809e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plusplus; 30815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 30825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30839e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plusequal; 30845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 30859e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plus; 30865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 30875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '-': 30895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 30909e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner if (Char == '-') { // -- 30915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30929e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minusminus; 30934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (Char == '>' && LangOpts.CPlusPlus && 30949e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->* 30955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 30965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 30979e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::arrowstar; 30989e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner } else if (Char == '>') { // -> 30995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31009e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::arrow; 31019e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner } else if (Char == '=') { // -= 31025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31039e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minusequal; 31045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 31059e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minus; 31065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 31075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 31085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '~': 31099e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::tilde; 31105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 31115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '!': 31125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (getCharAndSize(CurPtr, SizeTmp) == '=') { 31139e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::exclaimequal; 31145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 31169e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::exclaim; 31175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 31185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 31195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '/': 31205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // 6.4.9: Comments 31215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 3122bb23628148f555a4cf71f98c27096a7a804c085cNico Weber if (Char == '/') { // Line comment. 3123bb23628148f555a4cf71f98c27096a7a804c085cNico Weber // Even if Line comments are disabled (e.g. in C89 mode), we generally 31248402c73dd880e8af46c826d873681820aebe32ebChris Lattner // want to lex this as a comment. There is one problem with this though, 31258402c73dd880e8af46c826d873681820aebe32ebChris Lattner // that in one particular corner case, this can change the behavior of the 31268402c73dd880e8af46c826d873681820aebe32ebChris Lattner // resultant program. For example, In "foo //**/ bar", C89 would lex 3127bb23628148f555a4cf71f98c27096a7a804c085cNico Weber // this as "foo / bar" and langauges with Line comments would lex it as 31288402c73dd880e8af46c826d873681820aebe32ebChris Lattner // "foo". Check to see if the character after the second slash is a '*'. 31298402c73dd880e8af46c826d873681820aebe32ebChris Lattner // If so, we will lex that as a "/" instead of the start of a comment. 3130693fdfa069b74ffe1f79caa3c9ea8754555d29bdJordan Rose // However, we never do this if we are just preprocessing. 3131693fdfa069b74ffe1f79caa3c9ea8754555d29bdJordan Rose bool TreatAsComment = LangOpts.LineComment && !LangOpts.TraditionalCPP; 3132693fdfa069b74ffe1f79caa3c9ea8754555d29bdJordan Rose if (!TreatAsComment) 3133693fdfa069b74ffe1f79caa3c9ea8754555d29bdJordan Rose if (!(PP && PP->isPreprocessedOutput())) 3134693fdfa069b74ffe1f79caa3c9ea8754555d29bdJordan Rose TreatAsComment = getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*'; 3135693fdfa069b74ffe1f79caa3c9ea8754555d29bdJordan Rose 3136693fdfa069b74ffe1f79caa3c9ea8754555d29bdJordan Rose if (TreatAsComment) { 3137bb23628148f555a4cf71f98c27096a7a804c085cNico Weber if (SkipLineComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) 3138046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 31391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 31408402c73dd880e8af46c826d873681820aebe32ebChris Lattner // It is common for the tokens immediately after a // comment to be 31418402c73dd880e8af46c826d873681820aebe32ebChris Lattner // whitespace (indentation for the next line). Instead of going through 31428402c73dd880e8af46c826d873681820aebe32ebChris Lattner // the big switch, handle it efficiently now. 31438402c73dd880e8af46c826d873681820aebe32ebChris Lattner goto SkipIgnoredUnits; 31448402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 31458402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 31461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 31478402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (Char == '*') { // /**/ comment. 31485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) 3149046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 31502d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner goto LexNextToken; // GCC isn't tail call eliminating. 31518402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 31521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 31538402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (Char == '=') { 31545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31559e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::slashequal; 31565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 31579e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::slash; 31585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 31595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 31605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '%': 31615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 31625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 31639e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::percentequal; 31645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.Digraphs && Char == '>') { 31669e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_brace; // '%>' -> '}' 31675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.Digraphs && Char == ':') { 31695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 31715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') { 31729e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashhash; // '%:%:' -> '##' 31735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 31745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 31754e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (Char == '@' && LangOpts.MicrosoftExt) {// %:@ -> #@ -> Charize 31765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 317774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 317866d5ce11b9426f6a59f61a03cbd8dbf047cc9350Ted Kremenek Diag(BufferPtr, diag::ext_charize_microsoft); 31799e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashat; 3180e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner } else { // '%:' -> '#' 31815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We parsed a # character. If this occurs at the start of the line, 31825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it's actually the start of a preprocessing directive. Callback to 31835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the preprocessor to handle it. 31845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: -fpreprocessed mode?? 31853185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) 31863185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis goto HandleDirective; 31871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3188e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner Kind = tok::hash; 31895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 31905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 31919e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::percent; 31925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 31935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 31945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '<': 31955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 31965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingFilename) { 31979cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner return LexAngledStringLiteral(Result, CurPtr); 31985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '<') { 319934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2); 320034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (After == '=') { 320134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::lesslessequal; 320234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 320334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner SizeTmp2, Result); 320434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) { 320534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If this is actually a '<<<<<<<' version control conflict marker, 320634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // recognize it as such and recover nicely. 320734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 3208d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith } else if (After == '<' && HandleEndOfConflictMarker(CurPtr-1)) { 3209d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // If this is '<<<<' and we're in a Perforce-style conflict marker, 3210d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // ignore it. 3211d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith goto LexNextToken; 32124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.CUDA && After == '<') { 32131b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne Kind = tok::lesslessless; 32141b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 32151b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne SizeTmp2, Result); 321634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else { 321734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 321834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::lessless; 321934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 32205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 32215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32229e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::lessequal; 32234e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.Digraphs && Char == ':') { // '<:' -> '[' 322480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (LangOpts.CPlusPlus11 && 322587a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == ':') { 322687a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // C++0x [lex.pptoken]p3: 322787a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // Otherwise, if the next three characters are <:: and the subsequent 322887a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // character is neither : nor >, the < is treated as a preprocessor 322987a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // token by itself and not as the first character of the alternative 323087a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // token <:. 323187a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith unsigned SizeTmp3; 323287a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith char After = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3); 323387a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith if (After != ':' && After != '>') { 323487a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith Kind = tok::less; 3235661a99690bc133bbaa029da925481d4a860dec90Richard Smith if (!isLexingRawMode()) 3236661a99690bc133bbaa029da925481d4a860dec90Richard Smith Diag(BufferPtr, diag::warn_cxx98_compat_less_colon_colon); 323787a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith break; 323887a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith } 323987a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith } 324087a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith 32415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32429e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_square; 32434e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.Digraphs && Char == '%') { // '<%' -> '{' 32445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32459e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_brace; 32465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 32479e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::less; 32485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 32495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 32505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '>': 32515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 32525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 32535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32549e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::greaterequal; 32555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '>') { 325634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2); 325734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (After == '=') { 325834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 325934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner SizeTmp2, Result); 326034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::greatergreaterequal; 3261d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith } else if (After == '>' && IsStartOfConflictMarker(CurPtr-1)) { 3262d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // If this is actually a '>>>>' conflict marker, recognize it as such 3263d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // and recover nicely. 3264d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith goto LexNextToken; 326534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) { 326634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If this is '>>>>>>>' and we're in a conflict marker, ignore it. 326734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 32684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.CUDA && After == '>') { 32691b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne Kind = tok::greatergreatergreater; 32701b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 32711b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne SizeTmp2, Result); 327234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else { 327334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 327434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::greatergreater; 327534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 327634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 32775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 32789e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::greater; 32795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 32805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 32815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '^': 32825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 32835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 32845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32859e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::caretequal; 32865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 32879e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::caret; 32885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 32895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 32905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '|': 32915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 32925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 32939e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipeequal; 32945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '|') { 329634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If this is '|||||||' and we're in a conflict marker, ignore it. 329734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1)) 329834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 32999e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipepipe; 33005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 33015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 33029e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipe; 33035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 33045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 33055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ':': 33065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 33074e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.Digraphs && Char == '>') { 33089e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_square; // ':>' -> ']' 33095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 33104e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.CPlusPlus && Char == ':') { 33119e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::coloncolon; 33125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 33131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else { 33149e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::colon; 33155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 33165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 33175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ';': 33189e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::semi; 33195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 33205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '=': 33215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 33225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 3323d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // If this is '====' and we're in a conflict marker, ignore it. 332434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1)) 332534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 332634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 33279e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::equalequal; 33285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 33291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else { 33309e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::equal; 33315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 33325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 33335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ',': 33349e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::comma; 33355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 33365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '#': 33375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 33385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '#') { 33399e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashhash; 33405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 33414e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (Char == '@' && LangOpts.MicrosoftExt) { // #@ -> Charize 33429e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashat; 334374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 334466d5ce11b9426f6a59f61a03cbd8dbf047cc9350Ted Kremenek Diag(BufferPtr, diag::ext_charize_microsoft); 33455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 33465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 33475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We parsed a # character. If this occurs at the start of the line, 33485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it's actually the start of a preprocessing directive. Callback to 33495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the preprocessor to handle it. 33505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: -fpreprocessed mode?? 33513185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) 33523185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis goto HandleDirective; 33531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3354e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner Kind = tok::hash; 33555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 33565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 33575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 33583a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '@': 33593a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Objective C support. 33604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CurPtr[-1] == '@' && LangOpts.ObjC1) 33619e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::at; 33623a5707766850f9ee9daa35299794328b5caf96dcChris Lattner else 33639e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 33643a5707766850f9ee9daa35299794328b5caf96dcChris Lattner break; 33651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3366c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // UCNs (C99 6.4.3, C++11 [lex.charset]p2) 33675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\\': 3368c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (uint32_t CodePoint = tryReadUCN(CurPtr, BufferPtr, &Result)) 3369c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return LexUnicode(Result, CodePoint, CurPtr); 3370c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 33719e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 33725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 3373c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 3374c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose default: { 3375c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (isASCII(Char)) { 3376c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Kind = tok::unknown; 3377c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose break; 3378c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 3379c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 3380c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose UTF32 CodePoint; 3381c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 3382c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // We can't just reset CurPtr to BufferPtr because BufferPtr may point to 3383c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // an escaped newline. 3384c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose --CurPtr; 3385cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko ConversionResult Status = 3386cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko llvm::convertUTF8Sequence((const UTF8 **)&CurPtr, 3387cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko (const UTF8 *)BufferEnd, 3388cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko &CodePoint, 3389cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko strictConversion); 3390c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (Status == conversionOK) 3391c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return LexUnicode(Result, CodePoint, CurPtr); 3392c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 33930ed439487491e09faffdbabfacb1d050292c7723Jordan Rose if (isLexingRawMode() || ParsingPreprocessorDirective || 33940ed439487491e09faffdbabfacb1d050292c7723Jordan Rose PP->isPreprocessedOutput()) { 339520afc2977cd0a6bacbe6218a633cd59a24463e2fJordan Rose ++CurPtr; 339674c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose Kind = tok::unknown; 339774c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose break; 339874c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose } 339974c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose 3400c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // Non-ASCII characters tend to creep into source code unintentionally. 3401c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // Instead of letting the parser complain about the unknown token, 3402ae82c2b7b62d742f56638fa3cfb0f550ddcaf315Jordan Rose // just diagnose the invalid UTF-8, then drop the character. 340374c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose Diag(CurPtr, diag::err_invalid_utf8); 3404c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 3405c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose BufferPtr = CurPtr+1; 3406c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose goto LexNextToken; 3407c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 34085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 34091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 34105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 34115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 34125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 34135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 34149e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, Kind); 34153185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis return; 34163185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis 34173185d4ac30378995ef70421e2848f77524c2b5d5Argyrios KyrtzidisHandleDirective: 34183185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // We parsed a # character and it's the start of a preprocessing directive. 34193185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis 34203185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis FormTokenWithChars(Result, CurPtr, tok::hash); 34213185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis PP->HandleDirective(Result); 34223185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis 34233185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // As an optimization, if the preprocessor didn't switch lexers, tail 34243185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // recurse. 34253185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis if (PP->isCurrentLexer(this)) { 34263185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // Start a new token. If this is a #include or something, the PP may 34273185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // want us starting at the beginning of the line again. If so, set 34283185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // the StartOfLine flag and clear LeadingSpace. 34293185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis if (IsAtStartOfLine) { 34303185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis Result.setFlag(Token::StartOfLine); 34313185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis Result.clearFlag(Token::LeadingSpace); 34323185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis IsAtStartOfLine = false; 34333185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis } 34343185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis goto LexNextToken; // GCC isn't tail call eliminating. 34353185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis } 34363185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis return PP->Lex(Result); 34375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3438