Lexer.cpp revision ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1c
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 1250770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner // Default to keeping comments if the preprocessor wants them. 1260770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner SetCommentRetentionState(PP.getCommentRetentionState()); 1270770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner} 128dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 129168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner/// Lexer constructor - Create a new raw lexer object. This object is only 130092bf67e5ca560d2fc6aa70be1f172b8b3a5ff96Dmitri Gribenko/// suitable for calls to 'LexFromRawLexer'. This lexer assumes that the text 131590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner/// range will outlive it, so it doesn't take ownership of it. 1324e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid BlaikieLexer::Lexer(SourceLocation fileloc, const LangOptions &langOpts, 133de96c0f29c4cacabe6ea577c61db87c2a85aea6cChris Lattner const char *BufStart, const char *BufPtr, const char *BufEnd) 1344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie : FileLoc(fileloc), LangOpts(langOpts) { 13522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner 13622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner InitLexer(BufStart, BufPtr, BufEnd); 1371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 138168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner // We *are* in raw mode. 139168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner LexingRawMode = true; 1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 142025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// Lexer constructor - Create a new raw lexer object. This object is only 143092bf67e5ca560d2fc6aa70be1f172b8b3a5ff96Dmitri Gribenko/// suitable for calls to 'LexFromRawLexer'. This lexer assumes that the text 144025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// range will outlive it, so it doesn't take ownership of it. 1456e2901407bff59aeb4cc301cc58b034723d0eb49Chris LattnerLexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile, 1464e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const SourceManager &SM, const LangOptions &langOpts) 1474e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie : FileLoc(SM.getLocForStartOfFile(FID)), LangOpts(langOpts) { 148025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner 1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(), 150025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner FromFile->getBufferEnd()); 1511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 152025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner // We *are* in raw mode. 153025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner LexingRawMode = true; 154025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner} 155025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner 15642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for 15742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// _Pragma expansion. This has a variety of magic semantics that this method 15842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// sets up. It returns a new'd Lexer that must be delete'd when done. 15942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 16042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// On entrance to this routine, TokStartLoc is a macro location which has a 16142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// spelling loc that indicates the bytes to be lexed for the token and an 162433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// expansion location that indicates where all lexed tokens should be 16342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// "expanded from". 16442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 16542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// FIXME: It would really be nice to make _Pragma just be a wrapper around a 16642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// normal lexer that remaps tokens as they fly by. This would require making 16742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer 16842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// interface that could handle this stuff. This would pull GetMappedTokenLoc 16942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// out of the critical path of the lexer! 17042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 1711eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpLexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc, 172433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation ExpansionLocStart, 173433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation ExpansionLocEnd, 174bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner unsigned TokLen, Preprocessor &PP) { 17542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner SourceManager &SM = PP.getSourceManager(); 17642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 17742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Create the lexer as if we were going to lex the file normally. 178a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner FileID SpellingFID = SM.getFileID(SpellingLoc); 1796e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID); 1806e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner Lexer *L = new Lexer(SpellingFID, InputFile, PP); 1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Now that the lexer is created, change the start/end locations so that we 18342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // just lex the subsection of the file that we want. This is lexing from a 18442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // scratch buffer. 18542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner const char *StrData = SM.getCharacterData(SpellingLoc); 1861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->BufferPtr = StrData; 18842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->BufferEnd = StrData+TokLen; 1891fa495304c81e03f07f278a47b5efe9317104aabChris Lattner assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!"); 19042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 19142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Set the SourceLocation with the remapping information. This ensures that 19242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // GetMappedTokenLoc will remap the tokens as they are lexed. 193bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth L->FileLoc = SM.createExpansionLoc(SM.getLocForStartOfFile(SpellingFID), 194bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth ExpansionLocStart, 195bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth ExpansionLocEnd, TokLen); 1961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Ensure that the lexer thinks it is inside a directive, so that end \n will 19884021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // return an EOD token. 19942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->ParsingPreprocessorDirective = true; 2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // This lexer really is for _Pragma. 20242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->Is_PragmaLexer = true; 20342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner return L; 20442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner} 20542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 206168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner 2075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Stringify - Convert the specified string into a C string, with surrounding 2085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ""'s, and with escaped \ and " characters. 2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstd::string Lexer::Stringify(const std::string &Str, bool Charify) { 2105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer std::string Result = Str; 2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Quote = Charify ? '\'' : '"'; 2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = 0, e = Result.size(); i != e; ++i) { 2135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Result[i] == '\\' || Result[i] == Quote) { 2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result.insert(Result.begin()+i, '\\'); 2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++i; ++e; 2165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return Result; 2195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 221d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// Stringify - Convert the specified string into a C string by escaping '\' 222d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// and " characters. This does not add surrounding ""'s to the string. 2235f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid Lexer::Stringify(SmallVectorImpl<char> &Str) { 224d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner for (unsigned i = 0, e = Str.size(); i != e; ++i) { 225d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner if (Str[i] == '\\' || Str[i] == '"') { 226d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner Str.insert(Str.begin()+i, '\\'); 227d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner ++i; ++e; 228d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner } 229d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner } 230d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner} 231d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner 232b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner//===----------------------------------------------------------------------===// 233b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner// Token Spelling 234b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner//===----------------------------------------------------------------------===// 235b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 23630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith/// \brief Slow case of getSpelling. Extract the characters comprising the 23730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith/// spelling of this token from the provided input buffer. 23830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smithstatic size_t getSpellingSlow(const Token &Tok, const char *BufPtr, 23930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const LangOptions &LangOpts, char *Spelling) { 24030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith assert(Tok.needsCleaning() && "getSpellingSlow called on simple token"); 24130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 24230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith size_t Length = 0; 24330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const char *BufEnd = BufPtr + Tok.getLength(); 24430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 24530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith if (Tok.is(tok::string_literal)) { 24630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Munch the encoding-prefix and opening double-quote. 24730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith while (BufPtr < BufEnd) { 24830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith unsigned Size; 24930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Spelling[Length++] = Lexer::getCharAndSizeNoWarn(BufPtr, Size, LangOpts); 25030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith BufPtr += Size; 25130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 25230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith if (Spelling[Length - 1] == '"') 25330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith break; 25430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith } 25530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 25630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Raw string literals need special handling; trigraph expansion and line 25730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // splicing do not occur within their d-char-sequence nor within their 25830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // r-char-sequence. 25930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith if (Length >= 2 && 26030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Spelling[Length - 2] == 'R' && Spelling[Length - 1] == '"') { 26130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Search backwards from the end of the token to find the matching closing 26230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // quote. 26330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const char *RawEnd = BufEnd; 26430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith do --RawEnd; while (*RawEnd != '"'); 26530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith size_t RawLength = RawEnd - BufPtr + 1; 26630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 26730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Everything between the quotes is included verbatim in the spelling. 26830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith memcpy(Spelling + Length, BufPtr, RawLength); 26930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Length += RawLength; 27030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith BufPtr += RawLength; 27130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 27230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // The rest of the token is lexed normally. 27330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith } 27430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith } 27530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 27630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith while (BufPtr < BufEnd) { 27730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith unsigned Size; 27830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Spelling[Length++] = Lexer::getCharAndSizeNoWarn(BufPtr, Size, LangOpts); 27930cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith BufPtr += Size; 28030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith } 28130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 28230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith assert(Length < Tok.getLength() && 28330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith "NeedsCleaning flag set on token that didn't need cleaning!"); 28430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith return Length; 28530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith} 28630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 287b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// getSpelling() - Return the 'spelling' of this token. The spelling of a 288b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// token are the characters used to represent the token in the source file 289b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// after trigraph expansion and escaped-newline folding. In particular, this 290b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// wants to get the true, uncanonicalized, spelling of things like digraphs 291b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// UCNs, etc. 2925f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerStringRef Lexer::getSpelling(SourceLocation loc, 29330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith SmallVectorImpl<char> &buffer, 29430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const SourceManager &SM, 29530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const LangOptions &options, 29630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith bool *invalid) { 297834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Break down the source location. 298834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc); 299834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 300834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Try to the load the file buffer. 301834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall bool invalidTemp = false; 3025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); 303834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall if (invalidTemp) { 304834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall if (invalid) *invalid = true; 3055f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return StringRef(); 306834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall } 307834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 308834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall const char *tokenBegin = file.data() + locInfo.second; 309834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 310834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Lex from the start of the given location. 311834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall Lexer lexer(SM.getLocForStartOfFile(locInfo.first), options, 312834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall file.begin(), tokenBegin, file.end()); 313834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall Token token; 314834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall lexer.LexFromRawLexer(token); 315834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 316834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall unsigned length = token.getLength(); 317834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 318834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Common case: no need for cleaning. 319834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall if (!token.needsCleaning()) 3205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return StringRef(tokenBegin, length); 321834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 32230cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // Hard case, we need to relex the characters into the string. 32330cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith buffer.resize(length); 32430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith buffer.resize(getSpellingSlow(token, tokenBegin, options, buffer.data())); 3255f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return StringRef(buffer.data(), buffer.size()); 326834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall} 327834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 328834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// getSpelling() - Return the 'spelling' of this token. The spelling of a 329834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// token are the characters used to represent the token in the source file 330834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// after trigraph expansion and escaped-newline folding. In particular, this 331834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// wants to get the true, uncanonicalized, spelling of things like digraphs 332834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// UCNs, etc. 333b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattnerstd::string Lexer::getSpelling(const Token &Tok, const SourceManager &SourceMgr, 3344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts, bool *Invalid) { 335b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); 33630cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 337b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner bool CharDataInvalid = false; 33830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation(), 339b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner &CharDataInvalid); 340b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (Invalid) 341b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner *Invalid = CharDataInvalid; 342b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (CharDataInvalid) 343b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return std::string(); 34430cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 34530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith // If this token contains nothing interesting, return it directly. 346b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (!Tok.needsCleaning()) 34730cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith return std::string(TokStart, TokStart + Tok.getLength()); 34830cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith 349b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner std::string Result; 35030cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Result.resize(Tok.getLength()); 35130cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith Result.resize(getSpellingSlow(Tok, TokStart, LangOpts, &*Result.begin())); 352b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return Result; 353b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner} 354b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 355b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// getSpelling - This method is used to get the spelling of a token into a 356b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// preallocated buffer, instead of as an std::string. The caller is required 357b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// to allocate enough space for the token, which is guaranteed to be at least 358b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// Tok.getLength() bytes long. The actual length of the token is returned. 359b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// 360b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// Note that this method may do two possible things: it may either fill in 361b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// the buffer specified with characters, or it may *change the input pointer* 362b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// to point to a constant buffer with the data already in it (avoiding a 363b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// copy). The caller is not allowed to modify the returned buffer pointer 364b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// if an internal buffer is returned. 365b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattnerunsigned Lexer::getSpelling(const Token &Tok, const char *&Buffer, 366b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner const SourceManager &SourceMgr, 3674e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts, bool *Invalid) { 368b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); 369c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 370c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara const char *TokStart = 0; 371c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // NOTE: this has to be checked *before* testing for an IdentifierInfo. 372c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara if (Tok.is(tok::raw_identifier)) 373c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara TokStart = Tok.getRawIdentifierData(); 374c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose else if (!Tok.hasUCN()) { 375c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { 376c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // Just return the string from the identifier table, which is very quick. 377c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Buffer = II->getNameStart(); 378c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return II->getLength(); 379c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 380b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 381c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 382c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // NOTE: this can be checked even after testing for an IdentifierInfo. 383b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (Tok.isLiteral()) 384b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner TokStart = Tok.getLiteralData(); 385c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 386b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (TokStart == 0) { 387c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // Compute the start of the token in the input lexer buffer. 388b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner bool CharDataInvalid = false; 389b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner TokStart = SourceMgr.getCharacterData(Tok.getLocation(), &CharDataInvalid); 390b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (Invalid) 391b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner *Invalid = CharDataInvalid; 392b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (CharDataInvalid) { 393b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Buffer = ""; 394b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return 0; 395b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 396b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 397c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 398b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner // If this token contains nothing interesting, return it directly. 399b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (!Tok.needsCleaning()) { 400b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Buffer = TokStart; 401b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return Tok.getLength(); 402b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 403c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 404b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner // Otherwise, hard case, relex the characters into the string. 40530cddaec99fa6c3207613efdaedbb51dd8d70c77Richard Smith return getSpellingSlow(Tok, TokStart, LangOpts, const_cast<char*>(Buffer)); 406b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner} 407b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 408b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 4099a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// MeasureTokenLength - Relex the token at the specified location and return 4109a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// its length in bytes in the input file. If the token needs cleaning (e.g. 4119a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// includes a trigraph or an escaped newline) then this count includes bytes 4129a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// that are part of that. 4139a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattnerunsigned Lexer::MeasureTokenLength(SourceLocation Loc, 4142c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner const SourceManager &SM, 4152c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner const LangOptions &LangOpts) { 416d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis Token TheTok; 417d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis if (getRawToken(Loc, TheTok, SM, LangOpts)) 418d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis return 0; 419d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis return TheTok.getLength(); 420d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis} 421d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis 422d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis/// \brief Relex the token at the specified location. 423d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis/// \returns true if there was a failure, false on success. 424d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidisbool Lexer::getRawToken(SourceLocation Loc, Token &Result, 425d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis const SourceManager &SM, 426d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis const LangOptions &LangOpts) { 4279a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // TODO: this could be special cased for common tokens like identifiers, ')', 4289a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // etc to make this faster, if it mattered. Just look at StrData[0] to handle 4291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // all obviously single-char tokens. This could use 4309a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // Lexer::isObviouslySimpleCharacter for example to handle identifiers or 4319a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // something. 432de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 433de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // If this comes from a macro expansion, we really do want the macro name, not 434de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // the token this macro expanded to. 435402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth Loc = SM.getExpansionLoc(Loc); 436363fdc29656cc03c1817268888f95e6343470aa8Chris Lattner std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 437f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor bool Invalid = false; 4385f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); 439f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor if (Invalid) 440d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis return true; 441f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer 442f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer const char *StrData = Buffer.data()+LocInfo.second; 4438350394c65b81bba3986dfe44ae17423873741deChris Lattner 44433e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor if (isWhitespace(StrData[0])) 445d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis return true; 44633e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor 4479a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // Create a lexer starting at the beginning of this token. 448c3526d89ef9c31639ec8b25180cfb22354344241Sebastian Redl Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, 449c3526d89ef9c31639ec8b25180cfb22354344241Sebastian Redl Buffer.begin(), StrData, Buffer.end()); 45039de7409bffb6b725a8aa64f0ba77ab51e8c9eb3Chris Lattner TheLexer.SetCommentRetentionState(true); 451d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis TheLexer.LexFromRawLexer(Result); 452d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis return false; 4539a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner} 4549a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner 4550e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidisstatic SourceLocation getBeginningOfFileToken(SourceLocation Loc, 4560e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis const SourceManager &SM, 4570e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis const LangOptions &LangOpts) { 4580e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis assert(Loc.isFileID()); 459a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 4603de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor if (LocInfo.first.isInvalid()) 4613de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor return Loc; 4623de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor 463a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor bool Invalid = false; 4645f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); 465a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (Invalid) 466a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return Loc; 467a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 468a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Back up from the current location until we hit the beginning of a line 469a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // (or the buffer). We'll relex from that point. 470a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor const char *BufStart = Buffer.data(); 4713de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor if (LocInfo.second >= Buffer.size()) 4723de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor return Loc; 4733de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor 474a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor const char *StrData = BufStart+LocInfo.second; 475a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (StrData[0] == '\n' || StrData[0] == '\r') 476a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return Loc; 477a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 478a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor const char *LexStart = StrData; 479a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor while (LexStart != BufStart) { 480a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (LexStart[0] == '\n' || LexStart[0] == '\r') { 481a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor ++LexStart; 482a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor break; 483a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } 484a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 485a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor --LexStart; 486a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } 487a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 488a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Create a lexer starting at the beginning of this token. 489a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis SourceLocation LexerStartLoc = Loc.getLocWithOffset(-LocInfo.second); 490a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor Lexer TheLexer(LexerStartLoc, LangOpts, BufStart, LexStart, Buffer.end()); 491a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor TheLexer.SetCommentRetentionState(true); 492a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 493a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Lex tokens until we find the token that contains the source location. 494a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor Token TheTok; 495a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor do { 496a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor TheLexer.LexFromRawLexer(TheTok); 497a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 498a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (TheLexer.getBufferLocation() > StrData) { 499a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Lexing this token has taken the lexer past the source location we're 500a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // looking for. If the current token encompasses our source location, 501a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // return the beginning of that token. 502a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (TheLexer.getBufferLocation() - TheTok.getLength() <= StrData) 503a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return TheTok.getLocation(); 504a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 505a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // We ended up skipping over the source location entirely, which means 506a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // that it points into whitespace. We're done here. 507a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor break; 508a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } 509a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } while (TheTok.getKind() != tok::eof); 510a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 511a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // We've passed our source location; just return the original source location. 512a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return Loc; 513a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor} 514a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 5150e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios KyrtzidisSourceLocation Lexer::GetBeginningOfToken(SourceLocation Loc, 5160e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis const SourceManager &SM, 5170e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis const LangOptions &LangOpts) { 5180e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis if (Loc.isFileID()) 5190e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis return getBeginningOfFileToken(Loc, SM, LangOpts); 5200e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis 5210e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis if (!SM.isMacroArgExpansion(Loc)) 5220e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis return Loc; 5230e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis 5240e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis SourceLocation FileLoc = SM.getSpellingLoc(Loc); 5250e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis SourceLocation BeginFileLoc = getBeginningOfFileToken(FileLoc, SM, LangOpts); 5260e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis std::pair<FileID, unsigned> FileLocInfo = SM.getDecomposedLoc(FileLoc); 527ae9f85b2c062ac20512a484cfa6e66239dd3d0d9Chandler Carruth std::pair<FileID, unsigned> BeginFileLocInfo 528ae9f85b2c062ac20512a484cfa6e66239dd3d0d9Chandler Carruth = SM.getDecomposedLoc(BeginFileLoc); 5290e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis assert(FileLocInfo.first == BeginFileLocInfo.first && 5300e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis FileLocInfo.second >= BeginFileLocInfo.second); 531ae9f85b2c062ac20512a484cfa6e66239dd3d0d9Chandler Carruth return Loc.getLocWithOffset(BeginFileLocInfo.second - FileLocInfo.second); 5320e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis} 5330e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis 534f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregornamespace { 535f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor enum PreambleDirectiveKind { 536f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_Skipped, 537f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_StartIf, 538f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_EndIf, 539f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_Unknown 540f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor }; 541f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor} 542f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 543f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregorstd::pair<unsigned, bool> 54403c107a42fae79e89d0016999a1a04c07d65591aArgyrios KyrtzidisLexer::ComputePreamble(const llvm::MemoryBuffer *Buffer, 5454e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts, unsigned MaxLines) { 546f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // Create a lexer starting at the beginning of the file. Note that we use a 547f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // "fake" file source location at offset 1 so that the lexer will track our 548f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // position within the file. 549f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor const unsigned StartOffset = 1; 5501cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis SourceLocation FileLoc = SourceLocation::getFromRawEncoding(StartOffset); 5511cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis Lexer TheLexer(FileLoc, LangOpts, Buffer->getBufferStart(), 552f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Buffer->getBufferStart(), Buffer->getBufferEnd()); 5531cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis 5541cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis // StartLoc will differ from FileLoc if there is a BOM that was skipped. 5551cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis SourceLocation StartLoc = TheLexer.getSourceLocation(); 5561cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis 557f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor bool InPreprocessorDirective = false; 558f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Token TheTok; 559f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Token IfStartTok; 560f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor unsigned IfCount = 0; 561c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis 562c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis unsigned MaxLineOffset = 0; 563c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (MaxLines) { 564c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis const char *CurPtr = Buffer->getBufferStart(); 565c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis unsigned CurLine = 0; 566c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis while (CurPtr != Buffer->getBufferEnd()) { 567c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis char ch = *CurPtr++; 568c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (ch == '\n') { 569c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis ++CurLine; 570c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (CurLine == MaxLines) 571c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis break; 572c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis } 573c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis } 574c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (CurPtr != Buffer->getBufferEnd()) 575c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis MaxLineOffset = CurPtr - Buffer->getBufferStart(); 576c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis } 577df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor 578f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor do { 579f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor TheLexer.LexFromRawLexer(TheTok); 580f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 581f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (InPreprocessorDirective) { 582f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // If we've hit the end of the file, we're done. 583f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (TheTok.getKind() == tok::eof) { 584f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 585f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 586f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 587f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // If we haven't hit the end of the preprocessor directive, skip this 588f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // token. 589f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (!TheTok.isAtStartOfLine()) 590f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 591f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 592f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // We've passed the end of the preprocessor directive, and will look 593f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // at this token again below. 594f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor InPreprocessorDirective = false; 595f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 596f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 597df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // Keep track of the # of lines in the preamble. 598df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor if (TheTok.isAtStartOfLine()) { 599c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis unsigned TokOffset = TheTok.getLocation().getRawEncoding() - StartOffset; 600df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor 601df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // If we were asked to limit the number of lines in the preamble, 602df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // and we're about to exceed that limit, we're done. 603c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis if (MaxLineOffset && TokOffset >= MaxLineOffset) 604df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor break; 605df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor } 606df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor 607f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // Comments are okay; skip over them. 608f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (TheTok.getKind() == tok::comment) 609f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 610f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 611f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) { 612f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // This is the start of a preprocessor directive. 613f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Token HashTok = TheTok; 614f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor InPreprocessorDirective = true; 615f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 61619207f1e5f51261a33492602501fb7ada50ea546Joerg Sonnenberger // Figure out which directive this is. Since we're lexing raw tokens, 617f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // we don't have an identifier table available. Instead, just look at 618f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // the raw identifier to recognize and categorize preprocessor directives. 619f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor TheLexer.LexFromRawLexer(TheTok); 620c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara if (TheTok.getKind() == tok::raw_identifier && !TheTok.needsCleaning()) { 6215f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Keyword(TheTok.getRawIdentifierData(), 622c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara TheTok.getLength()); 623f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PreambleDirectiveKind PDK 624f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor = llvm::StringSwitch<PreambleDirectiveKind>(Keyword) 625f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("include", PDK_Skipped) 626f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("__include_macros", PDK_Skipped) 627f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("define", PDK_Skipped) 628f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("undef", PDK_Skipped) 629f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("line", PDK_Skipped) 630f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("error", PDK_Skipped) 631f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("pragma", PDK_Skipped) 632f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("import", PDK_Skipped) 633f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("include_next", PDK_Skipped) 634f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("warning", PDK_Skipped) 635f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("ident", PDK_Skipped) 636f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("sccs", PDK_Skipped) 637f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("assert", PDK_Skipped) 638f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("unassert", PDK_Skipped) 639f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("if", PDK_StartIf) 640f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("ifdef", PDK_StartIf) 641f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("ifndef", PDK_StartIf) 642f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("elif", PDK_Skipped) 643f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("else", PDK_Skipped) 644f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("endif", PDK_EndIf) 645f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Default(PDK_Unknown); 646f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 647f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor switch (PDK) { 648f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_Skipped: 649f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 650f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 651f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_StartIf: 652f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (IfCount == 0) 653f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor IfStartTok = HashTok; 654f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 655f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor ++IfCount; 656f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 657f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 658f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_EndIf: 659f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // Mismatched #endif. The preamble ends here. 660f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (IfCount == 0) 661f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 662f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 663f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor --IfCount; 664f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 665f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 666f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_Unknown: 667f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // We don't know what this directive is; stop at the '#'. 668f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 669f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 670f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 671f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 672f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // We only end up here if we didn't recognize the preprocessor 673f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // directive or it was one that can't occur in the preamble at this 674f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // point. Roll back the current token to the location of the '#'. 675f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor InPreprocessorDirective = false; 676f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor TheTok = HashTok; 677f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 678f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 679df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // We hit a token that we don't recognize as being in the 680df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // "preprocessing only" part of the file, so we're no longer in 681df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // the preamble. 682f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 683f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } while (true); 684f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 685f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation(); 686f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(), 687f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor IfCount? IfStartTok.isAtStartOfLine() 688f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor : TheTok.isAtStartOfLine()); 689f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor} 690f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 6917ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 6927ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// AdvanceToTokenCharacter - Given a location that specifies the start of a 6937ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token, return a new location that specifies a character within the token. 6947ef5c27eb6e8ebe58b52013246c06753c3613263Chris LattnerSourceLocation Lexer::AdvanceToTokenCharacter(SourceLocation TokStart, 6957ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner unsigned CharNo, 6967ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner const SourceManager &SM, 6974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts) { 698433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth // Figure out how many physical characters away the specified expansion 6997ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // character is. This needs to take into consideration newlines and 7007ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // trigraphs. 7017ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner bool Invalid = false; 7027ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner const char *TokPtr = SM.getCharacterData(TokStart, &Invalid); 7037ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7047ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // If they request the first char of the token, we're trivially done. 7057ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (Invalid || (CharNo == 0 && Lexer::isObviouslySimpleCharacter(*TokPtr))) 7067ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner return TokStart; 7077ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7087ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner unsigned PhysOffset = 0; 7097ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7107ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // The usual case is that tokens don't contain anything interesting. Skip 7117ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // over the uninteresting characters. If a token only consists of simple 7127ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // chars, this method is extremely fast. 7137ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner while (Lexer::isObviouslySimpleCharacter(*TokPtr)) { 7147ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (CharNo == 0) 715a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return TokStart.getLocWithOffset(PhysOffset); 7167ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner ++TokPtr, --CharNo, ++PhysOffset; 7177ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner } 7187ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7197ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // If we have a character that may be a trigraph or escaped newline, use a 7207ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // lexer to parse it correctly. 7217ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner for (; CharNo; --CharNo) { 7227ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner unsigned Size; 7234e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie Lexer::getCharAndSizeNoWarn(TokPtr, Size, LangOpts); 7247ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner TokPtr += Size; 7257ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner PhysOffset += Size; 7267ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner } 7277ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7287ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // Final detail: if we end up on an escaped newline, we want to return the 7297ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // location of the actual byte of the token. For example foo\<newline>bar 7307ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // advanced by 3 should return the location of b, not of \\. One compounding 7317ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // detail of this is that the escape may be made by a trigraph. 7327ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (!Lexer::isObviouslySimpleCharacter(*TokPtr)) 7337ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner PhysOffset += Lexer::SkipEscapedNewLines(TokPtr)-TokPtr; 7347ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 735a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return TokStart.getLocWithOffset(PhysOffset); 7367ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner} 7377ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7387ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// \brief Computes the source location just past the end of the 7397ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token at this source location. 7407ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// 7417ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// This routine can be used to produce a source location that 7427ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// points just past the end of the token referenced by \p Loc, and 7437ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// is generally used when a diagnostic needs to point just after a 7447ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token where it expected something different that it received. If 7457ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// the returned source location would not be meaningful (e.g., if 7467ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// it points into a macro), this routine returns an invalid 7477ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// source location. 7487ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// 7497ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// \param Offset an offset from the end of the token, where the source 7507ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// location should refer to. The default offset (0) produces a source 7517ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// location pointing just past the end of the token; an offset of 1 produces 7527ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// a source location pointing to the last character in the token, etc. 7537ef5c27eb6e8ebe58b52013246c06753c3613263Chris LattnerSourceLocation Lexer::getLocForEndOfToken(SourceLocation Loc, unsigned Offset, 7547ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner const SourceManager &SM, 7554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts) { 7567ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis if (Loc.isInvalid()) 7577ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner return SourceLocation(); 7587ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis 7597ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis if (Loc.isMacroID()) { 7604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Offset > 0 || !isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc)) 761433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth return SourceLocation(); // Points inside the macro expansion. 7627ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis } 7637ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis 7644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie unsigned Len = Lexer::MeasureTokenLength(Loc, SM, LangOpts); 7657ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (Len > Offset) 7667ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner Len = Len - Offset; 7677ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner else 7687ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner return Loc; 7697ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 770a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return Loc.getLocWithOffset(Len); 7717ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner} 7727ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7737a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis/// \brief Returns true if the given MacroID location points at the first 774433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// token of the macro expansion. 775433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruthbool Lexer::isAtStartOfMacroExpansion(SourceLocation loc, 776f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor const SourceManager &SM, 77769bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis const LangOptions &LangOpts, 77869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis SourceLocation *MacroBegin) { 7797a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); 7807a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 7817a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis std::pair<FileID, unsigned> infoLoc = SM.getDecomposedLoc(loc); 7827a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // FIXME: If the token comes from the macro token paste operator ('##') 7837a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // this function will always return false; 7847a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis if (infoLoc.second > 0) 7857a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis return false; // Does not point at the start of token. 7867a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 787433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation expansionLoc = 7881728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth SM.getSLocEntry(infoLoc.first).getExpansion().getExpansionLocStart(); 78969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (expansionLoc.isFileID()) { 79069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis // No other macro expansions, this is the first. 79169bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (MacroBegin) 79269bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis *MacroBegin = expansionLoc; 79369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis return true; 79469bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis } 7957a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 79669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis return isAtStartOfMacroExpansion(expansionLoc, SM, LangOpts, MacroBegin); 7977a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis} 7987a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 7997a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis/// \brief Returns true if the given MacroID location points at the last 800433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// token of the macro expansion. 801433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruthbool Lexer::isAtEndOfMacroExpansion(SourceLocation loc, 80269bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis const SourceManager &SM, 80369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis const LangOptions &LangOpts, 80469bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis SourceLocation *MacroEnd) { 8057a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); 8067a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 8077a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis SourceLocation spellLoc = SM.getSpellingLoc(loc); 8087a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis unsigned tokLen = MeasureTokenLength(spellLoc, SM, LangOpts); 8097a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis if (tokLen == 0) 8107a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis return false; 8117a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 8127a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis FileID FID = SM.getFileID(loc); 813a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis SourceLocation afterLoc = loc.getLocWithOffset(tokLen+1); 814f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis if (SM.isInFileID(afterLoc, FID)) 815f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis return false; // Still in the same FileID, does not point to the last token. 8167a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 8177a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // FIXME: If the token comes from the macro token paste operator ('##') 8187a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // or the stringify operator ('#') this function will always return false; 819f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis 820433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation expansionLoc = 8211728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth SM.getSLocEntry(FID).getExpansion().getExpansionLocEnd(); 82269bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (expansionLoc.isFileID()) { 82369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis // No other macro expansions. 82469bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (MacroEnd) 82569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis *MacroEnd = expansionLoc; 82669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis return true; 82769bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis } 8287a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 82969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis return isAtEndOfMacroExpansion(expansionLoc, SM, LangOpts, MacroEnd); 8307a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis} 8317a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 832a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidisstatic CharSourceRange makeRangeFromFileLocs(CharSourceRange Range, 833d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis const SourceManager &SM, 834d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis const LangOptions &LangOpts) { 835a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis SourceLocation Begin = Range.getBegin(); 836a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis SourceLocation End = Range.getEnd(); 837d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis assert(Begin.isFileID() && End.isFileID()); 838a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis if (Range.isTokenRange()) { 839a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis End = Lexer::getLocForEndOfToken(End, 0, SM,LangOpts); 840a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis if (End.isInvalid()) 841a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return CharSourceRange(); 842a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis } 843d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 844d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis // Break down the source locations. 845d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis FileID FID; 846d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis unsigned BeginOffs; 847d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis llvm::tie(FID, BeginOffs) = SM.getDecomposedLoc(Begin); 848d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (FID.isInvalid()) 849d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange(); 850d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 851d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis unsigned EndOffs; 852d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (!SM.isInFileID(End, FID, &EndOffs) || 853d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis BeginOffs > EndOffs) 854d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange(); 855d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 856d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange::getCharRange(Begin, End); 857d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis} 858d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 859a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios KyrtzidisCharSourceRange Lexer::makeFileCharRange(CharSourceRange Range, 86011b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis const SourceManager &SM, 86111b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis const LangOptions &LangOpts) { 862a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis SourceLocation Begin = Range.getBegin(); 863a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis SourceLocation End = Range.getEnd(); 864d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Begin.isInvalid() || End.isInvalid()) 86511b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis return CharSourceRange(); 86611b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 867d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Begin.isFileID() && End.isFileID()) 868a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 869d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 870d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Begin.isMacroID() && End.isFileID()) { 87111b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis if (!isAtStartOfMacroExpansion(Begin, SM, LangOpts, &Begin)) 87211b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis return CharSourceRange(); 873a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setBegin(Begin); 874a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 875d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis } 87611b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 877d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Begin.isFileID() && End.isMacroID()) { 878a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis if ((Range.isTokenRange() && !isAtEndOfMacroExpansion(End, SM, LangOpts, 879a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis &End)) || 880a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis (Range.isCharRange() && !isAtStartOfMacroExpansion(End, SM, LangOpts, 881a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis &End))) 882d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange(); 883a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setEnd(End); 884a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 885d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis } 88611b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 887d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis assert(Begin.isMacroID() && End.isMacroID()); 888d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis SourceLocation MacroBegin, MacroEnd; 889d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (isAtStartOfMacroExpansion(Begin, SM, LangOpts, &MacroBegin) && 890a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis ((Range.isTokenRange() && isAtEndOfMacroExpansion(End, SM, LangOpts, 891a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis &MacroEnd)) || 892a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis (Range.isCharRange() && isAtStartOfMacroExpansion(End, SM, LangOpts, 893a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis &MacroEnd)))) { 894a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setBegin(MacroBegin); 895a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setEnd(MacroEnd); 896a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 897a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis } 898d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 899d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis FileID FID; 900d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis unsigned BeginOffs; 901d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis llvm::tie(FID, BeginOffs) = SM.getDecomposedLoc(Begin); 902d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (FID.isInvalid()) 903e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return CharSourceRange(); 904e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 90511b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis unsigned EndOffs; 906d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (!SM.isInFileID(End, FID, &EndOffs) || 907d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis BeginOffs > EndOffs) 90811b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis return CharSourceRange(); 90911b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 910d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis const SrcMgr::SLocEntry *E = &SM.getSLocEntry(FID); 911d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis const SrcMgr::ExpansionInfo &Expansion = E->getExpansion(); 912d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis if (Expansion.isMacroArgExpansion() && 913d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis Expansion.getSpellingLoc().isFileID()) { 914d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis SourceLocation SpellLoc = Expansion.getSpellingLoc(); 915a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setBegin(SpellLoc.getLocWithOffset(BeginOffs)); 916a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range.setEnd(SpellLoc.getLocWithOffset(EndOffs)); 917a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis return makeRangeFromFileLocs(Range, SM, LangOpts); 918d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis } 919d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis 920d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis return CharSourceRange(); 92111b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis} 92211b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis 923e64d9037658a1b95c79ea275af6167a110b3c563Argyrios KyrtzidisStringRef Lexer::getSourceText(CharSourceRange Range, 924e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis const SourceManager &SM, 925e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis const LangOptions &LangOpts, 926e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis bool *Invalid) { 927a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis Range = makeFileCharRange(Range, SM, LangOpts); 928a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis if (Range.isInvalid()) { 929e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = true; 930e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return StringRef(); 931e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis } 932e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 933e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis // Break down the source location. 934e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis std::pair<FileID, unsigned> beginInfo = SM.getDecomposedLoc(Range.getBegin()); 935e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (beginInfo.first.isInvalid()) { 936e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = true; 937e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return StringRef(); 938e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis } 939e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 940e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis unsigned EndOffs; 941e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (!SM.isInFileID(Range.getEnd(), beginInfo.first, &EndOffs) || 942e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis beginInfo.second > EndOffs) { 943e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = true; 944e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return StringRef(); 945e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis } 946e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 947e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis // Try to the load the file buffer. 948e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis bool invalidTemp = false; 949e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis StringRef file = SM.getBufferData(beginInfo.first, &invalidTemp); 950e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (invalidTemp) { 951e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = true; 952e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return StringRef(); 953e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis } 954e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 955e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis if (Invalid) *Invalid = false; 956e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis return file.substr(beginInfo.second, EndOffs - beginInfo.second); 957e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis} 958e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis 959c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna ZaksStringRef Lexer::getImmediateMacroName(SourceLocation Loc, 960c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks const SourceManager &SM, 961c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks const LangOptions &LangOpts) { 962c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks assert(Loc.isMacroID() && "Only reasonble to call this on macros"); 9637f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9647f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // Find the location of the immediate macro expansion. 9657f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis while (1) { 9667f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis FileID FID = SM.getFileID(Loc); 9677f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis const SrcMgr::SLocEntry *E = &SM.getSLocEntry(FID); 9687f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis const SrcMgr::ExpansionInfo &Expansion = E->getExpansion(); 9697f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis Loc = Expansion.getExpansionLocStart(); 9707f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis if (!Expansion.isMacroArgExpansion()) 9717f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis break; 9727f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9737f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // For macro arguments we need to check that the argument did not come 9747f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // from an inner macro, e.g: "MAC1( MAC2(foo) )" 9757f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9767f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // Loc points to the argument id of the macro definition, move to the 9777f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // macro expansion. 978c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks Loc = SM.getImmediateExpansionRange(Loc).first; 9797f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis SourceLocation SpellLoc = Expansion.getSpellingLoc(); 9807f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis if (SpellLoc.isFileID()) 9817f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis break; // No inner macro. 9827f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9837f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // If spelling location resides in the same FileID as macro expansion 9847f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // location, it means there is no inner macro. 9857f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis FileID MacroFID = SM.getFileID(Loc); 9867f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis if (SM.isInFileID(SpellLoc, MacroFID)) 9877f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis break; 9887f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis 9897f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis // Argument came from inner macro. 9907f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis Loc = SpellLoc; 9917f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis } 992c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks 993c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // Find the spelling location of the start of the non-argument expansion 994c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // range. This is where the macro name was spelled in order to begin 995c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // expanding this macro. 9967f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis Loc = SM.getSpellingLoc(Loc); 997c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks 998c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // Dig out the buffer where the macro name was spelled and the extents of the 999c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks // name so that we can render it into the expansion note. 1000c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks std::pair<FileID, unsigned> ExpansionInfo = SM.getDecomposedLoc(Loc); 1001c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks unsigned MacroTokenLength = Lexer::MeasureTokenLength(Loc, SM, LangOpts); 1002c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first); 1003c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks return ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength); 1004c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks} 1005c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks 1006d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rosebool Lexer::isIdentifierBodyChar(char c, const LangOptions &LangOpts) { 10079893902eceba7f01dd1521349d33866f77254d78Jordan Rose return isIdentifierBody(c, LangOpts.DollarIdents); 1008d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose} 1009d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose 10105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 10125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Diagnostics forwarding code. 10135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 10145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1015409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the 1016433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// lexer buffer was all expanded at a single point, perform the mapping. 1017409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// This is currently only used for _Pragma implementation, so it is the slow 1018409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// path of the hot getSourceLocation method. Do not allow it to be inlined. 101914bd96571ef6f0e97dc79ec4d01b547d60e8fa68Chandler Carruthstatic LLVM_ATTRIBUTE_NOINLINE SourceLocation GetMappedTokenLoc( 102014bd96571ef6f0e97dc79ec4d01b547d60e8fa68Chandler Carruth Preprocessor &PP, SourceLocation FileLoc, unsigned CharNo, unsigned TokLen); 1021409a03671224d4e5bdab1594c43baf070148f830Chris Lattnerstatic SourceLocation GetMappedTokenLoc(Preprocessor &PP, 1022409a03671224d4e5bdab1594c43baf070148f830Chris Lattner SourceLocation FileLoc, 1023de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned CharNo, unsigned TokLen) { 1024433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth assert(FileLoc.isMacroID() && "Must be a macro expansion"); 10251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1026409a03671224d4e5bdab1594c43baf070148f830Chris Lattner // Otherwise, we're lexing "mapped tokens". This is used for things like 1027433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth // _Pragma handling. Combine the expansion location of FileLoc with the 1028df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner // spelling location. 1029e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner SourceManager &SM = PP.getSourceManager(); 10301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1031433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth // Create a new SLoc which is expanded from Expansion(FileLoc) but whose 1032df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner // characters come from spelling(FileLoc)+Offset. 1033e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc); 1034a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis SpellingLoc = SpellingLoc.getLocWithOffset(CharNo); 10351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1036e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner // Figure out the expansion loc range, which is the range covered by the 1037e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner // original _Pragma(...) sequence. 1038e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner std::pair<SourceLocation,SourceLocation> II = 1039999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth SM.getImmediateExpansionRange(FileLoc); 10401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1041bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth return SM.createExpansionLoc(SpellingLoc, II.first, II.second, TokLen); 1042409a03671224d4e5bdab1594c43baf070148f830Chris Lattner} 1043409a03671224d4e5bdab1594c43baf070148f830Chris Lattner 10445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSourceLocation - Return a source location identifier for the specified 10455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// offset in the current file. 1046de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris LattnerSourceLocation Lexer::getSourceLocation(const char *Loc, 1047de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned TokLen) const { 1048448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner assert(Loc >= BufferStart && Loc <= BufferEnd && 10495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer "Location out of range for this buffer!"); 10509dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner 10519dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner // In the normal case, we're just lexing from a simple file buffer, return 10529dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner // the file id from FileLoc with the offset specified. 1053448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner unsigned CharNo = Loc-BufferStart; 10549dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner if (FileLoc.isFileID()) 1055a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return FileLoc.getLocWithOffset(CharNo); 10561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10572b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner // Otherwise, this is the _Pragma lexer case, which pretends that all of the 10582b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner // tokens are lexed from where the _Pragma was defined. 1059168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner assert(PP && "This doesn't work on raw lexers"); 1060de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen); 10615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Diag - Forwarding function for diagnostics. This translate a source 10645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// position in the current buffer into a SourceLocation object for rendering. 10653cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const { 10663692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner return PP->Diag(getSourceLocation(Loc), DiagID); 10675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 10705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Trigraph and Escaped Newline Handling Code. 10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair, 10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the decoded trigraph letter it corresponds to, or '\0' if nothing. 10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char GetTrigraphCharForLetter(char Letter) { 10765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Letter) { 10775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: return 0; 10785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '=': return '#'; 10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ')': return ']'; 10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '(': return '['; 10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '!': return '|'; 10825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\'': return '^'; 10835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '>': return '}'; 10845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '/': return '\\'; 10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '<': return '{'; 10865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '-': return '~'; 10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// DecodeTrigraphChar - If the specified character is a legal trigraph when 10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled, 10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the result character. Finally, emit a warning about trigraph use 10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whether trigraphs are enabled or not. 10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char DecodeTrigraphChar(const char *CP, Lexer *L) { 10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Res = GetTrigraphCharForLetter(*CP); 10963692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner if (!Res || !L) return Res; 10971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10984e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!L->getLangOpts().Trigraphs) { 109974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 110074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CP-2, diag::trigraph_ignored); 11013692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner return 0; 11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 11031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 110474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 11055f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner L->Diag(CP-2, diag::trigraph_converted) << StringRef(&Res, 1); 11065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return Res; 11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 110924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// getEscapedNewLineSize - Return the size of the specified escaped newline, 111024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a 11111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// trigraph equivalent on entry to this function. 111224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattnerunsigned Lexer::getEscapedNewLineSize(const char *Ptr) { 111324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner unsigned Size = 0; 111424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner while (isWhitespace(Ptr[Size])) { 111524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner ++Size; 11161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 111724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r') 111824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner continue; 111924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 112024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // If this is a \r\n or \n\r, skip the other half. 112124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') && 112224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr[Size-1] != Ptr[Size]) 112324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner ++Size; 11241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 112524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return Size; 11261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 11271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 112824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Not an escaped newline, must be a \t or something else. 112924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return 0; 113024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner} 113124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 1132033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// SkipEscapedNewLines - If P points to an escaped newline (or a series of 1133033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// them), skip over them and return the first non-escaped-newline found, 1134033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// otherwise return P. 1135033749571f8d4c804eeb357c70b06424aa24503bChris Lattnerconst char *Lexer::SkipEscapedNewLines(const char *P) { 1136033749571f8d4c804eeb357c70b06424aa24503bChris Lattner while (1) { 1137033749571f8d4c804eeb357c70b06424aa24503bChris Lattner const char *AfterEscape; 1138033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (*P == '\\') { 1139033749571f8d4c804eeb357c70b06424aa24503bChris Lattner AfterEscape = P+1; 1140033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } else if (*P == '?') { 1141033749571f8d4c804eeb357c70b06424aa24503bChris Lattner // If not a trigraph for escape, bail out. 1142033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (P[1] != '?' || P[2] != '/') 1143033749571f8d4c804eeb357c70b06424aa24503bChris Lattner return P; 1144033749571f8d4c804eeb357c70b06424aa24503bChris Lattner AfterEscape = P+3; 1145033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } else { 1146033749571f8d4c804eeb357c70b06424aa24503bChris Lattner return P; 1147033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } 11481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1149033749571f8d4c804eeb357c70b06424aa24503bChris Lattner unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape); 1150033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (NewLineSize == 0) return P; 1151033749571f8d4c804eeb357c70b06424aa24503bChris Lattner P = AfterEscape+NewLineSize; 1152033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } 1153033749571f8d4c804eeb357c70b06424aa24503bChris Lattner} 1154033749571f8d4c804eeb357c70b06424aa24503bChris Lattner 1155aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// \brief Checks that the given token is the first token that occurs after the 1156aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// given location (this excludes comments and whitespace). Returns the location 1157aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// immediately after the specified token. If the token is not found or the 1158aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// location is inside a macro, the returned source location will be invalid. 1159aca25bccefe56121b686706afc84c8cb5d46e65bAnna ZaksSourceLocation Lexer::findLocationAfterToken(SourceLocation Loc, 1160aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks tok::TokenKind TKind, 1161aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks const SourceManager &SM, 1162aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks const LangOptions &LangOpts, 1163aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks bool SkipTrailingWhitespaceAndNewLine) { 1164aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks if (Loc.isMacroID()) { 116569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis if (!Lexer::isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc)) 1166aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks return SourceLocation(); 1167aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks } 1168aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, LangOpts); 1169aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1170aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Break down the source location. 1171aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 1172aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1173aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Try to load the file buffer. 1174aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks bool InvalidTemp = false; 1175cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp); 1176aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks if (InvalidTemp) 1177aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks return SourceLocation(); 1178aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1179aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks const char *TokenBegin = File.data() + LocInfo.second; 1180aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1181aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Lex from the start of the given location. 1182aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks Lexer lexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(), 1183aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks TokenBegin, File.end()); 1184aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Find the token. 1185aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks Token Tok; 1186aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks lexer.LexFromRawLexer(Tok); 1187aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks if (Tok.isNot(TKind)) 1188aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks return SourceLocation(); 1189aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks SourceLocation TokenLoc = Tok.getLocation(); 1190aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1191aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks // Calculate how much whitespace needs to be skipped if any. 1192aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks unsigned NumWhitespaceChars = 0; 1193aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks if (SkipTrailingWhitespaceAndNewLine) { 1194aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks const char *TokenEnd = SM.getCharacterData(TokenLoc) + 1195aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks Tok.getLength(); 1196aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks unsigned char C = *TokenEnd; 1197aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks while (isHorizontalWhitespace(C)) { 1198aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks C = *(++TokenEnd); 1199aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks NumWhitespaceChars++; 1200aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks } 120135a2b798efd61fec425553f387d76be9c522f184Eli Friedman 120235a2b798efd61fec425553f387d76be9c522f184Eli Friedman // Skip \r, \n, \r\n, or \n\r 120335a2b798efd61fec425553f387d76be9c522f184Eli Friedman if (C == '\n' || C == '\r') { 120435a2b798efd61fec425553f387d76be9c522f184Eli Friedman char PrevC = C; 120535a2b798efd61fec425553f387d76be9c522f184Eli Friedman C = *(++TokenEnd); 1206aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks NumWhitespaceChars++; 120735a2b798efd61fec425553f387d76be9c522f184Eli Friedman if ((C == '\n' || C == '\r') && C != PrevC) 120835a2b798efd61fec425553f387d76be9c522f184Eli Friedman NumWhitespaceChars++; 120935a2b798efd61fec425553f387d76be9c522f184Eli Friedman } 1210aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks } 1211aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks 1212a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis return TokenLoc.getLocWithOffset(Tok.getLength() + NumWhitespaceChars); 1213aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks} 121424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 12155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer, 12165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// get its size, and return it. This is tricky in several cases: 12175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 1. If currently at the start of a trigraph, we warn about the trigraph, 12185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// then either return the trigraph (skipping 3 chars) or the '?', 12195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// depending on whether trigraphs are enabled or not. 12205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 2. If this is an escaped newline (potentially with whitespace between 12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the backslash and newline), implicitly skip the newline and return 12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the char after it. 12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 12245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This handles the slow/uncommon case of the getCharAndSize method. Here we 12255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// know that we can accumulate into Size, and that we have already incremented 12265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Ptr by Size bytes. 12275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 12285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should 12295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match. 12305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 12315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size, 1232d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token *Tok) { 12335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, look for an escaped newline. 12345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '\\') { 12355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 12365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Ptr; 12375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash: 12385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Common case, backslash-char where the char is not whitespace. 12395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isWhitespace(Ptr[0])) return '\\'; 12401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12415636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner // See if we have optional whitespace characters between the slash and 12425636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner // newline. 124324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { 124424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Remember that this token needs to be cleaned. 124524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (Tok) Tok->setFlag(Token::NeedsCleaning); 124624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 124724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Warn if there was whitespace between the backslash and newline. 12485636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode()) 124924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Diag(Ptr, diag::backslash_newline_space); 12501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 125124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Found backslash<whitespace><newline>. Parse the char after it. 125224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Size += EscapedNewLineSize; 125324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr += EscapedNewLineSize; 1254f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis 125504a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // If the char that we finally got was a \n, then we must have had 125604a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // something like \<newline><newline>. We don't want to consume the 125704a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // second newline. 125804a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0') 125904a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis return ' '; 1260f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis 126124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Use slow version to accumulate a correct size field. 126224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return getCharAndSizeSlow(Ptr, Size, Tok); 126324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner } 12641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is not an escaped newline, just return the slash. 12665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return '\\'; 12675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a trigraph, process it. 12705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '?' && Ptr[1] == '?') { 12715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is actually a legal trigraph (not something like "??x"), emit 12725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // a trigraph warning. If so, and if trigraphs are enabled, return it. 12735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) { 12745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Remember that this token needs to be cleaned. 1275d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner if (Tok) Tok->setFlag(Token::NeedsCleaning); 12765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Ptr += 3; 12785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Size += 3; 12795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') goto Slash; 12805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return C; 12815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is neither, return a single character. 12855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 12865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return *Ptr; 12875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 12885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the 12915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size, 12925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// and that we have already incremented Ptr by Size bytes. 12935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 12945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlow (above) should 12955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match. 12965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size, 12974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &LangOpts) { 12985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, look for an escaped newline. 12995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '\\') { 13005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 13015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Ptr; 13025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash: 13035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Common case, backslash-char where the char is not whitespace. 13045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isWhitespace(Ptr[0])) return '\\'; 13051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // See if we have optional whitespace characters followed by a newline. 130724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { 130824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Found backslash<whitespace><newline>. Parse the char after it. 130924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Size += EscapedNewLineSize; 131024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr += EscapedNewLineSize; 13111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 131204a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // If the char that we finally got was a \n, then we must have had 131304a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // something like \<newline><newline>. We don't want to consume the 131404a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis // second newline. 131504a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0') 131604a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis return ' '; 1317f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis 131824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Use slow version to accumulate a correct size field. 13194e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie return getCharAndSizeSlowNoWarn(Ptr, Size, LangOpts); 132024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner } 13211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is not an escaped newline, just return the slash. 13235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return '\\'; 13245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 13251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a trigraph, process it. 13274e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') { 13285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is actually a legal trigraph (not something like "??x"), return 13295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it. 13305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (char C = GetTrigraphCharForLetter(Ptr[2])) { 13315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Ptr += 3; 13325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Size += 3; 13335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') goto Slash; 13345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return C; 13355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 13365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 13371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is neither, return a single character. 13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 13405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return *Ptr; 13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 13445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper methods for lexing. 13455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 13465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1347f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor/// \brief Routine that indiscriminately skips bytes in the source file. 1348f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregorvoid Lexer::SkipBytes(unsigned Bytes, bool StartOfLine) { 1349f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor BufferPtr += Bytes; 1350f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor if (BufferPtr > BufferEnd) 1351f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor BufferPtr = BufferEnd; 1352f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor IsAtStartOfLine = StartOfLine; 1353f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor} 1354f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor 1355ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rosestatic bool isAllowedIDChar(uint32_t C, const LangOptions &LangOpts) { 1356ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (LangOpts.CPlusPlus11 || LangOpts.C11) 1357ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose return isCharInSet(C, C11AllowedIDChars); 1358ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose else if (LangOpts.CPlusPlus) 1359ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose return isCharInSet(C, CXX03AllowedIDChars); 1360ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose else 1361ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose return isCharInSet(C, C99AllowedIDChars); 1362c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose} 1363c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 1364ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rosestatic bool isAllowedInitiallyIDChar(uint32_t C, const LangOptions &LangOpts) { 1365ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose assert(isAllowedIDChar(C, LangOpts)); 1366ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (LangOpts.CPlusPlus11 || LangOpts.C11) 1367ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose return !isCharInSet(C, C11DisallowedInitialIDChars); 1368ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose else if (LangOpts.CPlusPlus) 1369ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose return true; 1370ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose else 1371ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose return !isCharInSet(C, C99DisallowedInitialIDChars); 1372ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose} 1373ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose 1374ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rosestatic inline CharSourceRange makeCharRange(Lexer &L, const char *Begin, 1375ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose const char *End) { 1376ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose return CharSourceRange::getCharRange(L.getSourceLocation(Begin), 1377ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose L.getSourceLocation(End)); 1378ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose} 1379ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose 1380ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rosestatic void maybeDiagnoseIDCharCompat(DiagnosticsEngine &Diags, uint32_t C, 1381ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose CharSourceRange Range, bool IsFirst) { 1382ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose // Check C99 compatibility. 1383ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (Diags.getDiagnosticLevel(diag::warn_c99_compat_unicode_id, 1384ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose Range.getBegin()) > DiagnosticsEngine::Ignored) { 1385ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose enum { 1386ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose CannotAppearInIdentifier = 0, 1387ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose CannotStartIdentifier 1388ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose }; 1389ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose 1390ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (!isCharInSet(C, C99AllowedIDChars)) { 1391ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose Diags.Report(Range.getBegin(), diag::warn_c99_compat_unicode_id) 1392ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose << Range 1393ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose << CannotAppearInIdentifier; 1394ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } else if (IsFirst && isCharInSet(C, C99DisallowedInitialIDChars)) { 1395ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose Diags.Report(Range.getBegin(), diag::warn_c99_compat_unicode_id) 1396ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose << Range 1397ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose << CannotStartIdentifier; 1398ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 1399c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 1400c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 1401ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose // Check C++98 compatibility. 1402ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (Diags.getDiagnosticLevel(diag::warn_cxx98_compat_unicode_id, 1403ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose Range.getBegin()) > DiagnosticsEngine::Ignored) { 1404ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (!isCharInSet(C, CXX03AllowedIDChars)) { 1405ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose Diags.Report(Range.getBegin(), diag::warn_cxx98_compat_unicode_id) 1406ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose << Range; 1407ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 1408ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 1409ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 1410c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 1411d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexIdentifier(Token &Result, const char *CurPtr) { 14125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$] 14135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned Size; 14145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned char C = *CurPtr++; 1415cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner while (isIdentifierBody(C)) 14165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 1417cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner 14185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; // Back up over the skipped character. 14195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Fast path, no $,\,? in identifier found. '\' might be an escaped newline 14215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN. 1422cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner // 14239893902eceba7f01dd1521349d33866f77254d78Jordan Rose // TODO: Could merge these checks into an InfoTable flag to make the 14249893902eceba7f01dd1521349d33866f77254d78Jordan Rose // comparison cheaper 1425c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (isASCII(C) && C != '\\' && C != '?' && 1426c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose (C != '$' || !LangOpts.DollarIdents)) { 14275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerFinishIdentifier: 14285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *IdStart = BufferPtr; 1429c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara FormTokenWithChars(Result, CurPtr, tok::raw_identifier); 1430c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara Result.setRawIdentifierData(IdStart); 14311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are in raw mode, return this identifier raw. There is no need to 14335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // look up identifier information or attempt to macro expand it. 1434c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara if (LexingRawMode) 1435c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara return; 14361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1437c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // Fill in Result.IdentifierInfo and update the token kind, 1438c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // looking up the identifier in the identifier table. 1439c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara IdentifierInfo *II = PP->LookUpIdentifierInfo(Result); 14401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Finally, now that we know we have an identifier, pass this off to the 14425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // preprocessor, which may macro expand it or something. 1443d1186fa38166a581b51975f0382a45fc3a0733d0Chris Lattner if (II->isHandleIdentifierCase()) 14446a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner PP->HandleIdentifier(Result); 14456aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 14466a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner return; 14475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, $,\,? in identifier found. Enter slower path. 14501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 14525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 14535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '$') { 14545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we hit a $ and they are not supported in identifiers, we are done. 14554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!LangOpts.DollarIdents) goto FinishIdentifier; 14561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, emit a diagnostic and continue. 145874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 145974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr, diag::ext_dollar_in_identifier); 14605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 14615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 14625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer continue; 1463c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 1464c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } else if (C == '\\') { 1465c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose const char *UCNPtr = CurPtr + Size; 1466c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose uint32_t CodePoint = tryReadUCN(UCNPtr, CurPtr, /*Token=*/0); 1467ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (CodePoint == 0 || !isAllowedIDChar(CodePoint, LangOpts)) 1468c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose goto FinishIdentifier; 1469c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 1470ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (!isLexingRawMode()) { 1471ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose maybeDiagnoseIDCharCompat(PP->getDiagnostics(), CodePoint, 1472ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose makeCharRange(*this, CurPtr, UCNPtr), 1473ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose /*IsFirst=*/false); 1474ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 1475ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose 1476c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Result.setFlag(Token::HasUCN); 1477c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if ((UCNPtr - CurPtr == 6 && CurPtr[1] == 'u') || 1478c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose (UCNPtr - CurPtr == 10 && CurPtr[1] == 'U')) 1479c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose CurPtr = UCNPtr; 1480c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose else 1481c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose while (CurPtr != UCNPtr) 1482c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose (void)getAndAdvanceChar(CurPtr, Result); 1483c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 1484c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose C = getCharAndSize(CurPtr, Size); 1485c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose continue; 1486c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } else if (!isASCII(C)) { 1487c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose const char *UnicodePtr = CurPtr; 1488c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose UTF32 CodePoint; 1489cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko ConversionResult Result = 1490cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko llvm::convertUTF8Sequence((const UTF8 **)&UnicodePtr, 1491cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko (const UTF8 *)BufferEnd, 1492cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko &CodePoint, 1493cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko strictConversion); 1494c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (Result != conversionOK || 1495ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose !isAllowedIDChar(static_cast<uint32_t>(CodePoint), LangOpts)) 1496c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose goto FinishIdentifier; 1497c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 1498ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (!isLexingRawMode()) { 1499ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose maybeDiagnoseIDCharCompat(PP->getDiagnostics(), CodePoint, 1500ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose makeCharRange(*this, CurPtr, UnicodePtr), 1501ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose /*IsFirst=*/false); 1502ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 1503ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose 1504c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose CurPtr = UnicodePtr; 1505c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose C = getCharAndSize(CurPtr, Size); 1506c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose continue; 1507c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } else if (!isIdentifierBody(C)) { 15085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto FinishIdentifier; 15095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this character is good, consume it. 15125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 15135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 1515c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose while (isIdentifierBody(C)) { 15165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 15175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 15215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1522a75ec43d625753b4439b0d6f70bd988444c74617Douglas Gregor/// isHexaLiteral - Return true if Start points to a hex constant. 15234a551000bee716ac8b1bbe16134a53f0ad221a5aChris Lattner/// in microsoft mode (where this is supposed to be several different tokens). 1524e506f8a41063410c75945ebb804758bd0202947fEli Friedmanbool Lexer::isHexaLiteral(const char *Start, const LangOptions &LangOpts) { 15256ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner unsigned Size; 15264e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie char C1 = Lexer::getCharAndSizeNoWarn(Start, Size, LangOpts); 15276ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner if (C1 != '0') 15286ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner return false; 15294e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie char C2 = Lexer::getCharAndSizeNoWarn(Start + Size, Size, LangOpts); 15306ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner return (C2 == 'x' || C2 == 'X'); 1531a75ec43d625753b4439b0d6f70bd988444c74617Douglas Gregor} 15325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15335253c7ff266ae79308050c9f43d60dd1a67c5fb9Nate Begeman/// LexNumericConstant - Lex the remainder of a integer or floating point 15345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. From[-1] is the first character lexed. Return the end of the 15355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. 1536d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexNumericConstant(Token &Result, const char *CurPtr) { 15375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned Size; 15385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getCharAndSize(CurPtr, Size); 15395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char PrevCh = 0; 15409893902eceba7f01dd1521349d33866f77254d78Jordan Rose while (isPreprocessingNumberBody(C)) { // FIXME: UCNs in ud-suffix. 15415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 15425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer PrevCh = C; 15435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 15445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we fell out, check for a sign, due to 1e+12. If we have one, continue. 1547b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) { 1548b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner // If we are in Microsoft mode, don't continue if the constant is hex. 1549b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner // For example, MSVC will accept the following as 3 tokens: 0x1234567e+1 15504e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!LangOpts.MicrosoftExt || !isHexaLiteral(BufferPtr, LangOpts)) 1551b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); 1552b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner } 15535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a hex FP constant, continue. 1555d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p')) { 1556d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith // Outside C99, we accept hexadecimal floating point numbers as a 1557d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith // not-quite-conforming extension. Only do so if this looks like it's 1558d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith // actually meant to be a hexfloat, and not if it has a ud-suffix. 1559d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith bool IsHexFloat = true; 1560d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith if (!LangOpts.C99) { 1561d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith if (!isHexaLiteral(BufferPtr, LangOpts)) 1562d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith IsHexFloat = false; 1563d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith else if (std::find(BufferPtr, CurPtr, '_') != CurPtr) 1564d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith IsHexFloat = false; 1565d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith } 1566d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith if (IsHexFloat) 1567d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); 1568d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith } 15691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 157147246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 15729e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::numeric_constant); 157347246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 15745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 15755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15765cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith/// LexUDSuffix - Lex the ud-suffix production for user-defined literal suffixes 1577e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith/// in C++11, or warn on a ud-suffix in C++98. 15785cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smithconst char *Lexer::LexUDSuffix(Token &Result, const char *CurPtr) { 15794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(getLangOpts().CPlusPlus); 15805cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 15815cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith // Maximally munch an identifier. FIXME: UCNs. 15825cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith unsigned Size; 15835cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith char C = getCharAndSize(CurPtr, Size); 15845cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith if (isIdentifierHead(C)) { 158580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (!getLangOpts().CPlusPlus11) { 1586e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith if (!isLexingRawMode()) 15872fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith Diag(CurPtr, 15882fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith C == '_' ? diag::warn_cxx11_compat_user_defined_literal 15892fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith : diag::warn_cxx11_compat_reserved_user_defined_literal) 15902fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith << FixItHint::CreateInsertion(getSourceLocation(CurPtr), " "); 15912fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith return CurPtr; 15922fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith } 15932fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith 15942fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith // C++11 [lex.ext]p10, [usrlit.suffix]p1: A program containing a ud-suffix 15952fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith // that does not start with an underscore is ill-formed. As a conforming 15962fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith // extension, we treat all such suffixes as if they had whitespace before 15972fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith // them. 15982fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith if (C != '_') { 15992fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith if (!isLexingRawMode()) 1600b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet Diag(CurPtr, getLangOpts().MicrosoftMode ? 1601b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet diag::ext_ms_reserved_user_defined_literal : 1602b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet diag::ext_reserved_user_defined_literal) 1603e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith << FixItHint::CreateInsertion(getSourceLocation(CurPtr), " "); 1604e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith return CurPtr; 1605e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith } 1606e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith 160799831e4677a7e2e051af636221694d60ba31fcdbRichard Smith Result.setFlag(Token::HasUDSuffix); 16085cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith do { 16095cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith CurPtr = ConsumeChar(CurPtr, Size, Result); 16105cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith C = getCharAndSize(CurPtr, Size); 16115cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith } while (isIdentifierBody(C)); 16125cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith } 16135cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith return CurPtr; 16145cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith} 16155cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 16165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexStringLiteral - Lex the remainder of a string literal, after having lexed 16175cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor/// either " or L" or u8" or u" or U". 16185cee1195584fa8672253139c86e922daeda69b9eDouglas Gregorvoid Lexer::LexStringLiteral(Token &Result, const char *CurPtr, 16195cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::TokenKind Kind) { 16205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this string contain the \0 character? 16211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1622661a99690bc133bbaa029da925481d4a860dec90Richard Smith if (!isLexingRawMode() && 1623661a99690bc133bbaa029da925481d4a860dec90Richard Smith (Kind == tok::utf8_string_literal || 1624661a99690bc133bbaa029da925481d4a860dec90Richard Smith Kind == tok::utf16_string_literal || 1625661a99690bc133bbaa029da925481d4a860dec90Richard Smith Kind == tok::utf32_string_literal)) 1626661a99690bc133bbaa029da925481d4a860dec90Richard Smith Diag(BufferPtr, diag::warn_cxx98_compat_unicode_literal); 1627661a99690bc133bbaa029da925481d4a860dec90Richard Smith 16285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 16295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '"') { 1630571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner // Skip escaped characters. Escaped newlines will already be processed by 1631571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner // getAndAdvanceChar. 1632571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner if (C == '\\') 16335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 163433611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor 1635571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner if (C == '\n' || C == '\r' || // Newline. 163633611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. 16374e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!isLexingRawMode() && !LangOpts.AsmPreprocessor) 1638b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith Diag(BufferPtr, diag::ext_unterminated_string); 16399e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr-1, tok::unknown); 16405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 16415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1642571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner 16437d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (C == 0) { 16447d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (isCodeCompletionPoint(CurPtr-1)) { 16457d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 16467d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis FormTokenWithChars(Result, CurPtr-1, tok::unknown); 16477d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return cutOffLexing(); 16487d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 16497d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 1650571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner NulCharacter = CurPtr-1; 16517d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 16525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 16535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16555cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith // If we are in C++11, lex the optional ud-suffix. 16564e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus) 16575cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith CurPtr = LexUDSuffix(Result, CurPtr); 16585cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 16595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If a nul character existed in the string, warn about it. 166074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 166174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_string); 16625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of the token as well as the BufferPtr instance var. 166447246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 16655cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor FormTokenWithChars(Result, CurPtr, Kind); 166647246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 16675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 16685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16692fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// LexRawStringLiteral - Lex the remainder of a raw string literal, after 16702fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// having lexed R", LR", u8R", uR", or UR". 16712fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Toppervoid Lexer::LexRawStringLiteral(Token &Result, const char *CurPtr, 16722fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::TokenKind Kind) { 16732fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // This function doesn't use getAndAdvanceChar because C++0x [lex.pptoken]p3: 16742fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Between the initial and final double quote characters of the raw string, 16752fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // any transformations performed in phases 1 and 2 (trigraphs, 16762fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // universal-character-names, and line splicing) are reverted. 16772fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 1678661a99690bc133bbaa029da925481d4a860dec90Richard Smith if (!isLexingRawMode()) 1679661a99690bc133bbaa029da925481d4a860dec90Richard Smith Diag(BufferPtr, diag::warn_cxx98_compat_raw_string_literal); 1680661a99690bc133bbaa029da925481d4a860dec90Richard Smith 16812fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper unsigned PrefixLen = 0; 16822fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 16832fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper while (PrefixLen != 16 && isRawStringDelimBody(CurPtr[PrefixLen])) 16842fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ++PrefixLen; 16852fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 16862fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // If the last character was not a '(', then we didn't lex a valid delimiter. 16872fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (CurPtr[PrefixLen] != '(') { 16882fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (!isLexingRawMode()) { 16892fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper const char *PrefixEnd = &CurPtr[PrefixLen]; 16902fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (PrefixLen == 16) { 16912fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Diag(PrefixEnd, diag::err_raw_delim_too_long); 16922fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } else { 16932fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Diag(PrefixEnd, diag::err_invalid_char_raw_delim) 16942fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper << StringRef(PrefixEnd, 1); 16952fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 16962fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 16972fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 16982fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Search for the next '"' in hopes of salvaging the lexer. Unfortunately, 16992fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // it's possible the '"' was intended to be part of the raw string, but 17002fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // there's not much we can do about that. 17012fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper while (1) { 17022fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper char C = *CurPtr++; 17032fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17042fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (C == '"') 17052fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper break; 17062fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (C == 0 && CurPtr-1 == BufferEnd) { 17072fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper --CurPtr; 17082fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper break; 17092fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17102fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17112fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17122fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper FormTokenWithChars(Result, CurPtr, tok::unknown); 17132fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return; 17142fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17152fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17162fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Save prefix and move CurPtr past it 17172fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper const char *Prefix = CurPtr; 17182fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CurPtr += PrefixLen + 1; // skip over prefix and '(' 17192fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17202fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper while (1) { 17212fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper char C = *CurPtr++; 17222fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17232fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (C == ')') { 17242fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Check for prefix match and closing quote. 17252fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (strncmp(CurPtr, Prefix, PrefixLen) == 0 && CurPtr[PrefixLen] == '"') { 17262fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper CurPtr += PrefixLen + 1; // skip over prefix and '"' 17272fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper break; 17282fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17292fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } else if (C == 0 && CurPtr-1 == BufferEnd) { // End of file. 17302fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (!isLexingRawMode()) 17312fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Diag(BufferPtr, diag::err_unterminated_raw_string) 17322fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper << StringRef(Prefix, PrefixLen); 17332fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper FormTokenWithChars(Result, CurPtr-1, tok::unknown); 17342fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return; 17352fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17362fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 17372fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17385cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith // If we are in C++11, lex the optional ud-suffix. 17394e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus) 17405cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith CurPtr = LexUDSuffix(Result, CurPtr); 17415cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 17422fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Update the location of token as well as BufferPtr. 17432fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper const char *TokStart = BufferPtr; 17442fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper FormTokenWithChars(Result, CurPtr, Kind); 17452fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Result.setLiteralData(TokStart); 17462fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper} 17472fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 17485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexAngledStringLiteral - Lex the remainder of an angled string literal, 17495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// after having lexed the '<' character. This is used for #include filenames. 1750d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) { 17515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this string contain the \0 character? 17529cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner const char *AfterLessPos = CurPtr; 17535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 17545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '>') { 17555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip escaped characters. 17565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') { 17575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip the escaped character. 175860b202c5eb6fb2d608bfef05523f40fdf5091d48Dmitri Gribenko getAndAdvanceChar(CurPtr, Result); 17595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == '\n' || C == '\r' || // Newline. 17607d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis (C == 0 && (CurPtr-1 == BufferEnd || // End of file. 17617d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis isCodeCompletionPoint(CurPtr-1)))) { 17629cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner // If the filename is unterminated, then it must just be a lone < 17639cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner // character. Return this as such. 17649cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner FormTokenWithChars(Result, AfterLessPos, tok::less); 17655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 17665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == 0) { 17675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer NulCharacter = CurPtr-1; 17685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 17705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If a nul character existed in the string, warn about it. 177374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 177474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_string); 17751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 177747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 17789e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::angle_string_literal); 177947246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 17805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 17815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 17825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 17835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexCharConstant - Lex the remainder of a character constant, after having 17845cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor/// lexed either ' or L' or u' or U'. 17855cee1195584fa8672253139c86e922daeda69b9eDouglas Gregorvoid Lexer::LexCharConstant(Token &Result, const char *CurPtr, 17865cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::TokenKind Kind) { 17875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this character contain the \0 character? 17885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1789661a99690bc133bbaa029da925481d4a860dec90Richard Smith if (!isLexingRawMode() && 1790661a99690bc133bbaa029da925481d4a860dec90Richard Smith (Kind == tok::utf16_char_constant || Kind == tok::utf32_char_constant)) 1791661a99690bc133bbaa029da925481d4a860dec90Richard Smith Diag(BufferPtr, diag::warn_cxx98_compat_unicode_literal); 1792661a99690bc133bbaa029da925481d4a860dec90Richard Smith 17935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 17945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\'') { 17954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!isLexingRawMode() && !LangOpts.AsmPreprocessor) 1796b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith Diag(BufferPtr, diag::ext_empty_character); 17979e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 17985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 17995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1801d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner while (C != '\'') { 1802d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner // Skip escaped characters. 18036d926ae667cca926e77ddce1734514911706ed0aNico Weber if (C == '\\') 18046d926ae667cca926e77ddce1734514911706ed0aNico Weber C = getAndAdvanceChar(CurPtr, Result); 18056d926ae667cca926e77ddce1734514911706ed0aNico Weber 18066d926ae667cca926e77ddce1734514911706ed0aNico Weber if (C == '\n' || C == '\r' || // Newline. 18076d926ae667cca926e77ddce1734514911706ed0aNico Weber (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. 18084e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!isLexingRawMode() && !LangOpts.AsmPreprocessor) 1809b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith Diag(BufferPtr, diag::ext_unterminated_char); 1810d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner FormTokenWithChars(Result, CurPtr-1, tok::unknown); 1811d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner return; 18126d926ae667cca926e77ddce1734514911706ed0aNico Weber } 18136d926ae667cca926e77ddce1734514911706ed0aNico Weber 18146d926ae667cca926e77ddce1734514911706ed0aNico Weber if (C == 0) { 18157d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (isCodeCompletionPoint(CurPtr-1)) { 18167d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 18177d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis FormTokenWithChars(Result, CurPtr-1, tok::unknown); 18187d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return cutOffLexing(); 18197d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 18207d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 1821d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner NulCharacter = CurPtr-1; 1822d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner } 1823d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner C = getAndAdvanceChar(CurPtr, Result); 18245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18265cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith // If we are in C++11, lex the optional ud-suffix. 18274e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus) 18285cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith CurPtr = LexUDSuffix(Result, CurPtr); 18295cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith 1830d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner // If a nul character existed in the character, warn about it. 183174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 183274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_char); 18335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 183547246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 18365cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor FormTokenWithChars(Result, CurPtr, Kind); 183747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 18385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 18395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipWhitespace - Efficiently skip over a series of whitespace characters. 18415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Update BufferPtr to point to the next non-whitespace character and return. 1842d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// 1843d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// This method forms a token and returns true if KeepWhitespaceMode is enabled. 1844d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// 1845d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnerbool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) { 18465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Whitespace - Skip it, then return the token after the whitespace. 18475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently. 18485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 18495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip horizontal whitespace very aggressively. 18505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isHorizontalWhitespace(Char)) 18515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = *++CurPtr; 18521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1853ddd3e8b90a92c39f620bda7a0945320c0b9e60dbDaniel Dunbar // Otherwise if we have something other than whitespace, we're done. 18545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char != '\n' && Char != '\r') 18555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 18561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 18585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // End of preprocessor directive line, let LexTokenInternal handle this. 18595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 1860d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return false; 18615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // ok, but handle newline. 18645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The returned token is at the start of the line. 1865d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 18665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // No leading whitespace seen so far. 1867d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::LeadingSpace); 18685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = *++CurPtr; 18695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this isn't immediately after a newline, there is leading space. 18725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char PrevChar = CurPtr[-1]; 18735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (PrevChar != '\n' && PrevChar != '\r') 1874d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 18755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1876d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // If the client wants us to return whitespace, return it now. 1877d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (isKeepWhitespaceMode()) { 18789e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 1879d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return true; 1880d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner } 18811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 1883d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return false; 18845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 18855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1886bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// We have just read the // characters from input. Skip until we find the 1887bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// newline character thats terminate the comment. Then update BufferPtr and 1888bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// return. 1889046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// 1890046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted 1891046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true. 1892bb23628148f555a4cf71f98c27096a7a804c085cNico Weberbool Lexer::SkipLineComment(Token &Result, const char *CurPtr) { 1893bb23628148f555a4cf71f98c27096a7a804c085cNico Weber // If Line comments aren't explicitly enabled for this language, emit an 18945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // extension warning. 1895bb23628148f555a4cf71f98c27096a7a804c085cNico Weber if (!LangOpts.LineComment && !isLexingRawMode()) { 1896bb23628148f555a4cf71f98c27096a7a804c085cNico Weber Diag(BufferPtr, diag::ext_line_comment); 18971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Mark them enabled so we only emit one warning for this translation 18995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // unit. 1900bb23628148f555a4cf71f98c27096a7a804c085cNico Weber LangOpts.LineComment = true; 19015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 19021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan over the body of the comment. The common case, when scanning, is that 19045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the comment contains normal ascii characters with nothing interesting in 19055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // them. As such, optimize for this case with the inner loop. 19065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C; 19075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer do { 19085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr; 19095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip over characters in the fast loop. 19105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != 0 && // Potentially EOF. 19115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C != '\n' && C != '\r') // Newline or DOS-style newline. 19125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *++CurPtr; 19135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 19141daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer const char *NextLine = CurPtr; 19151daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer if (C != 0) { 19161daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer // We found a newline, see if it's escaped. 19171daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer const char *EscapePtr = CurPtr-1; 19181daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer while (isHorizontalWhitespace(*EscapePtr)) // Skip whitespace. 19191daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer --EscapePtr; 19201daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer 19211daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer if (*EscapePtr == '\\') // Escaped newline. 19221daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer CurPtr = EscapePtr; 19231daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer else if (EscapePtr[0] == '/' && EscapePtr[-1] == '?' && 19241daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer EscapePtr[-2] == '?') // Trigraph-escaped newline. 19251daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer CurPtr = EscapePtr-2; 19261daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer else 19271daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer break; // This is a newline, we're done. 19281daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer } 19291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to 1931bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // properly decode the character. Read it in raw mode to avoid emitting 1932bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // diagnostics about things like trigraphs. If we see an escaped newline, 1933bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // we'll handle it below. 19345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *OldPtr = CurPtr; 1935bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner bool OldRawMode = isLexingRawMode(); 1936bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner LexingRawMode = true; 19375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 1938bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner LexingRawMode = OldRawMode; 1939ead616c5d8681a41b11273327813e61bda01907aChris Lattner 19401daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer // If we only read only one character, then no special handling is needed. 19411daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer // We're done and can skip forward to the newline. 19421daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer if (C != 0 && CurPtr == OldPtr+1) { 19431daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer CurPtr = NextLine; 19441daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer break; 19451daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer } 19461daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer 19475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we read multiple characters, and one of those characters was a \r or 19485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n, then we had an escaped newline within the comment. Emit diagnostic 19495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // unless the next line is also a // comment. 19505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') { 19515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (; OldPtr != CurPtr; ++OldPtr) 19525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (OldPtr[0] == '\n' || OldPtr[0] == '\r') { 19535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Okay, we found a // comment that ends in a newline, if the next 19545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // line is also a // comment, but has spaces, don't emit a diagnostic. 19555d6ae288bc661572e52ea71fc14b249eb5e2d9e9Benjamin Kramer if (isWhitespace(C)) { 19565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *ForwardPtr = CurPtr; 19575d6ae288bc661572e52ea71fc14b249eb5e2d9e9Benjamin Kramer while (isWhitespace(*ForwardPtr)) // Skip whitespace. 19585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++ForwardPtr; 19595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/') 19605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 19615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 19621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 196374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 1964bb23628148f555a4cf71f98c27096a7a804c085cNico Weber Diag(OldPtr-1, diag::ext_multi_line_line_comment); 19655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 19665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 19675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 19681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 196955817afdf9d453a443262a733f6caf6692dca118Douglas Gregor if (CurPtr == BufferEnd+1) { 197055817afdf9d453a443262a733f6caf6692dca118Douglas Gregor --CurPtr; 197155817afdf9d453a443262a733f6caf6692dca118Douglas Gregor break; 197255817afdf9d453a443262a733f6caf6692dca118Douglas Gregor } 19737d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 19747d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) { 19757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 19767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis cutOffLexing(); 19777d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return false; 19787d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 19797d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 19805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } while (C != '\n' && C != '\r'); 19815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 19823d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner // Found but did not consume the newline. Notify comment handlers about the 19833d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner // comment unless we're in a #if 0 block. 19843d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner if (PP && !isLexingRawMode() && 19853d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr), 19863d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner getSourceLocation(CurPtr)))) { 1987046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner BufferPtr = CurPtr; 1988046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return true; // A token has to be returned. 1989046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner } 19901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are returning comments as tokens, return this comment as a token. 1992fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner if (inKeepCommentMode()) 1993bb23628148f555a4cf71f98c27096a7a804c085cNico Weber return SaveLineComment(Result, CurPtr); 19945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 19955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are inside a preprocessor directive and we see the end of line, 199684021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // return immediately, so that the lexer can return this as an EOD token. 19975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective || CurPtr == BufferEnd) { 19985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 19992d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 20005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 20011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, eat the \n character. We don't care if this is a \n\r or 20037a4f004a77421af876152281b92c4f8ea24afb2dChris Lattner // \r\n sequence. This is an efficiency hack (because we know the \n can't 2004d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // contribute to another token), it isn't needed for correctness. Note that 2005d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // this is ok even in KeepWhitespaceMode, because we would have returned the 2006d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner /// comment above in that mode. 20075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 20081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The next returned token is at the start of the line. 2010d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 20115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // No leading whitespace seen so far. 2012d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::LeadingSpace); 20135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 20142d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 20155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 20165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2017bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// If in save-comment mode, package up this Line comment in an appropriate 2018bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// way and return it. 2019bb23628148f555a4cf71f98c27096a7a804c085cNico Weberbool Lexer::SaveLineComment(Token &Result, const char *CurPtr) { 20209e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // If we're not in a preprocessor directive, just return the // comment 20219e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // directly. 20229e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::comment); 20231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20248c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie if (!ParsingPreprocessorDirective || LexingRawMode) 20259e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner return true; 20261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2027bb23628148f555a4cf71f98c27096a7a804c085cNico Weber // If this Line-style comment is in a macro definition, transmogrify it into 20289e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // a C-style block comment. 2029453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor bool Invalid = false; 2030453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor std::string Spelling = PP->getSpelling(Result, &Invalid); 2031453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor if (Invalid) 2032453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor return true; 2033453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor 2034bb23628148f555a4cf71f98c27096a7a804c085cNico Weber assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not line comment?"); 20359e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Spelling[1] = '*'; // Change prefix to "/*". 20369e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Spelling += "*/"; // add suffix. 20371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20389e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Result.setKind(tok::comment); 2039374b3837d676133fcc1eb70a25c8baf8ec4a5c4aDmitri Gribenko PP->CreateString(Spelling, Result, 2040a08529cc3f00e0b47a3c028823634129ac46847bAbramo Bagnara Result.getLocation(), Result.getLocation()); 20412d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return true; 20425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 20435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 20445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline 204580d7c52653800d3338cca2c4388348010d2b1921David Blaikie/// character (either \\n or \\r) is part of an escaped newline sequence. Issue 204680d7c52653800d3338cca2c4388348010d2b1921David Blaikie/// a diagnostic if so. We know that the newline is inside of a block comment. 20471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr, 20485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Lexer *L) { 20495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(CurPtr[0] == '\n' || CurPtr[0] == '\r'); 20501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Back up off the newline. 20525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 20531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a two-character newline sequence, skip the other character. 20555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == '\n' || CurPtr[0] == '\r') { 20565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n\n or \r\r -> not escaped newline. 20575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == CurPtr[1]) 20585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 20595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n\r or \r\n -> skip the newline. 20605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 20615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 20621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have horizontal whitespace, skip over it. We allow whitespace 20645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // between the slash and newline. 20655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool HasSpace = false; 20665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) { 20675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 20685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer HasSpace = true; 20695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 20701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, we know this is an escaped newline. 20725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (*CurPtr == '\\') { 20735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[-1] != '*') return false; 20745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 20755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It isn't a slash, is it the ?? / trigraph? 20765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' || 20775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[-3] != '*') 20785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 20791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // This is the trigraph ending the comment. Emit a stern warning! 20815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr -= 2; 20825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 20835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If no trigraphs are enabled, warn that we ignored this trigraph and 20845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // ignore this * character. 20854e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!L->getLangOpts().Trigraphs) { 208674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 208774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::trigraph_ignored_block_comment); 20885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 20895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 209074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 209174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::trigraph_ends_block_comment); 20925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 20931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Warn about having an escaped newline between the */ characters. 209574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 209674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::escaped_newline_block_comment_end); 20971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If there was space between the backslash and newline, warn about it. 209974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (HasSpace && !L->isLexingRawMode()) 210074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::backslash_newline_space); 21011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 21035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 21045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 21055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__ 21065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <emmintrin.h> 21075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__ 21085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <altivec.h> 21095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#undef bool 21105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif 21115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2112ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// We have just read from input the / and * characters that started a comment. 2113ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// Read until we find the * and / characters that terminate the comment. 2114ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// Note that we don't bother decoding trigraphs or escaped newlines in block 2115ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// comments, because they cannot cause the comment to end. The only thing 2116ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// that can happen is the comment could end with an escaped newline between 2117ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// the terminating * and /. 21182d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner/// 2119046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted 2120046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true. 2121d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) { 21225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan one character past where we should, looking for a '/' character. Once 2123fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner // we find it, check to see if it was preceded by a *. This common 21245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // optimization helps people who like to put a lot of * characters in their 21255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // comments. 21268146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner 21278146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // The first character we get with newlines and trigraphs skipped to handle 21288146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // the degenerate /*/ case below correctly if the * has an escaped newline 21298146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // after it. 21308146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner unsigned CharSize; 21318146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner unsigned char C = getCharAndSize(CurPtr, CharSize); 21328146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner CurPtr += CharSize; 21335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == 0 && CurPtr == BufferEnd+1) { 21347d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (!isLexingRawMode()) 21350af574270d3be2b0e73a3379dfaa633746f8fc6fChris Lattner Diag(BufferPtr, diag::err_unterminated_block_comment); 213631f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner --CurPtr; 21371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 213831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // KeepWhitespaceMode should return this broken comment as a token. Since 213931f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // it isn't a well formed comment, just return it as an 'unknown' token. 214031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner if (isKeepWhitespaceMode()) { 21419e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 214231f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner return true; 214331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner } 21441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 214531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner BufferPtr = CurPtr; 21462d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 21475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 21481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21498146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // Check to see if the first character after the '/*' is another /. If so, 21508146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // then this slash does not end the block comment, it is part of it. 21518146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner if (C == '/') 21528146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner C = *CurPtr++; 21531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 21555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip over all non-interesting characters until we find end of buffer or a 21565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // (probably ending) '/' character. 21577d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (CurPtr + 24 < BufferEnd && 21587d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis // If there is a code-completion point avoid the fast scan because it 21597d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis // doesn't check for '\0'. 21607d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis !(PP && PP->getCodeCompletionFileLoc() == FileLoc)) { 21615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // While not aligned to a 16-byte boundary. 21625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0) 21635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 21641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '/') goto FoundSlash; 21665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 21675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__ 21683f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer __m128i Slashes = _mm_set1_epi8('/'); 21693f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer while (CurPtr+16 <= BufferEnd) { 217031ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky int cmp = _mm_movemask_epi8(_mm_cmpeq_epi8(*(const __m128i*)CurPtr, 217131ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky Slashes)); 21723f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer if (cmp != 0) { 21736300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer // Adjust the pointer to point directly after the first slash. It's 21746300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer // not necessary to set C here, it will be overwritten at the end of 21756300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer // the outer loop. 21766300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer CurPtr += llvm::CountTrailingZeros_32(cmp) + 1; 21773f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer goto FoundSlash; 21783f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer } 21795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 16; 21803f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer } 21815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__ 21825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer __vector unsigned char Slashes = { 21831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump '/', '/', '/', '/', '/', '/', '/', '/', 21845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer '/', '/', '/', '/', '/', '/', '/', '/' 21855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer }; 21865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (CurPtr+16 <= BufferEnd && 21875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes)) 21885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 16; 21891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#else 21905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan for '/' quickly. Many block comments are very large. 21915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (CurPtr[0] != '/' && 21925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[1] != '/' && 21935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[2] != '/' && 21945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[3] != '/' && 21955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr+4 < BufferEnd) { 21965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 4; 21975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 21985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif 21991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It has to be one of the bytes scanned, increment to it and read one. 22015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 22025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Loop to scan the remainder. 22055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '/' && C != '\0') 22065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 22071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '/') { 22093f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer FoundSlash: 22105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[-2] == '*') // We found the final */. We're done! 22115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) { 22145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) { 22155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We found the final */, though it had an escaped newline between the 22165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // * and /. We're done! 22175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == '*' && CurPtr[1] != '/') { 22215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a /* inside of the comment, emit a warning. Don't do this 22225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // if this is a /*/, which will end the comment. This misses cases with 22235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // embedded escaped newlines, but oh well. 222474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 222574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::warn_nested_block_comment); 22265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == 0 && CurPtr == BufferEnd+1) { 22287d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (!isLexingRawMode()) 222974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(BufferPtr, diag::err_unterminated_block_comment); 22305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Note: the user probably forgot a */. We could continue immediately 22315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // after the /*, but this would involve lexing a lot of what really is the 22325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // comment, which surely would confuse the parser. 223331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner --CurPtr; 22341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 223531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // KeepWhitespaceMode should return this broken comment as a token. Since 223631f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // it isn't a well formed comment, just return it as an 'unknown' token. 223731f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner if (isKeepWhitespaceMode()) { 22389e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 223931f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner return true; 224031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner } 22411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 224231f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner BufferPtr = CurPtr; 22432d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 22447d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } else if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) { 22457d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 22467d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis cutOffLexing(); 22477d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return false; 22485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22497d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 22505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 22515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22533d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner // Notify comment handlers about the comment unless we're in a #if 0 block. 22543d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner if (PP && !isLexingRawMode() && 22553d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr), 22563d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner getSourceLocation(CurPtr)))) { 2257046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner BufferPtr = CurPtr; 2258046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return true; // A token has to be returned. 2259046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner } 22602e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 22615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are returning comments as tokens, return this comment as a token. 2262fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner if (inKeepCommentMode()) { 22639e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::comment); 22642d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return true; 22655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 22675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It is common for the tokens immediately after a /**/ comment to be 22685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // whitespace. Instead of going through the big switch, handle it 2269d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // efficiently now. This is safe even in KeepWhitespaceMode because we would 2270d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // have already returned above with the comment as a token. 22715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isHorizontalWhitespace(*CurPtr)) { 2272d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 22735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SkipWhitespace(Result, CurPtr+1); 22742d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 22755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 22775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, just return so that the next character will be lexed as a token. 22785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 2279d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 22802d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 22815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 22825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 22835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 22845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Lexing Entry Points 22855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 22865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 22875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ReadToEndOfLine - Read the rest of the current preprocessor line as an 22885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// uninterpreted string. This switches the lexer out of directive mode. 22893093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramervoid Lexer::ReadToEndOfLine(SmallVectorImpl<char> *Result) { 22905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(ParsingPreprocessorDirective && ParsingFilename == false && 22915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer "Must be in a preprocessing directive!"); 2292d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token Tmp; 22935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 22945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // CurPtr - Cache BufferPtr in an automatic variable. 22955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *CurPtr = BufferPtr; 22965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 22975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Char = getAndAdvanceChar(CurPtr, Tmp); 22985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Char) { 22995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 23003093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer if (Result) 23013093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer Result->push_back(Char); 23025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 23035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 0: // Null. 23045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Found end of file? 23055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr-1 != BufferEnd) { 23067d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (isCodeCompletionPoint(CurPtr-1)) { 23077d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 23087d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis cutOffLexing(); 23093093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer return; 23107d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 23117d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 23125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Nope, normal character, continue. 23133093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer if (Result) 23143093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer Result->push_back(Char); 23155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 23165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FALL THROUGH. 23185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\r': 23195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\n': 23205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Okay, we found the end of the line. First, back up past the \0, \r, \n. 23215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(CurPtr[-1] == Char && "Trigraphs for newline?"); 23225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr-1; 23231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 232484021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // Next, lex the character, which should handle the EOD transition. 23255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Lex(Tmp); 232655817afdf9d453a443262a733f6caf6692dca118Douglas Gregor if (Tmp.is(tok::code_completion)) { 23277d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (PP) 23287d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis PP->CodeCompleteNaturalLanguage(); 232955817afdf9d453a443262a733f6caf6692dca118Douglas Gregor Lex(Tmp); 233055817afdf9d453a443262a733f6caf6692dca118Douglas Gregor } 233184021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne assert(Tmp.is(tok::eod) && "Unexpected token!"); 23321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23333093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer // Finally, we're done; 23343093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer return; 23355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 23385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 23395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexEndOfFile - CurPtr points to the end of this file. Handle this 23405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// condition, reporting diagnostics and handling other edge cases as required. 23415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This returns true if Result contains a token, false if PP.Lex should be 23425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// called again. 2343d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) { 23445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we hit the end of the file while parsing a preprocessor directive, 23455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // end the preprocessor directive first. The next token returned will 23465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // then be the end of file. 23475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 23485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Done parsing the "line". 23495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParsingPreprocessorDirective = false; 23505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 235184021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne FormTokenWithChars(Result, CurPtr, tok::eod); 23521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore comment saving mode, in case it was disabled for directive. 2354f744d133e8d36eec65828d046ee695bda238125cChris Lattner SetCommentRetentionState(PP->getCommentRetentionState()); 23555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; // Have a token. 23561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 235786d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor 235886d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor // If we are in raw mode, return this event as an EOF token. Let the caller 235986d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor // that put us in raw mode handle the event. 236086d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor if (isLexingRawMode()) { 236186d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor Result.startToken(); 236286d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor BufferPtr = BufferEnd; 236386d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor FormTokenWithChars(Result, BufferEnd, tok::eof); 236486d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor return true; 236586d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor } 236681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor 2367f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor // Issue diagnostics for unterminated #if and missing newline. 2368f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor 23695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are in a #if directive, emit an error. 23705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (!ConditionalStack.empty()) { 23717d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (PP->getCodeCompletionFileLoc() != FileLoc) 23722d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor PP->Diag(ConditionalStack.back().IfLoc, 23732d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor diag::err_pp_unterminated_conditional); 23745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConditionalStack.pop_back(); 23755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2377b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue 2378b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner // a pedwarn. 23795e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) 238080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith Diag(BufferEnd, LangOpts.CPlusPlus11 ? // C++11 [lex.phases] 2.2 p2 23815e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell diag::warn_cxx98_compat_no_newline_eof : diag::ext_no_newline_eof) 23825e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n"); 23831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 23855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 23865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Finally, let the preprocessor handle this. 23870cdd1fe3ec29b5cbff9a728966ace5c5b5d614f7Jordan Rose return PP->HandleEndOfFile(Result, isPragmaLexer()); 23885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 23895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 23905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from 23915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified lexer will return a tok::l_paren token, 0 if it is something 23925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// else and 2 if there are no more tokens in the buffer controlled by the 23935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexer. 23945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerunsigned Lexer::isNextPPTokenLParen() { 23955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?"); 23961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Switch to 'skipping' mode. This will ensure that we can lex a token 23985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // without emitting diagnostics, disables macro expansion, and will cause EOF 23995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // to return an EOF token instead of popping the include stack. 24005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexingRawMode = true; 24011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Save state that can be changed while lexing so that we can restore it. 24035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *TmpBufferPtr = BufferPtr; 2404a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner bool inPPDirectiveMode = ParsingPreprocessorDirective; 24051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2406d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token Tok; 24075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Tok.startToken(); 24085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexTokenInternal(Tok); 24091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore state that may have changed. 24115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = TmpBufferPtr; 2412a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner ParsingPreprocessorDirective = inPPDirectiveMode; 24131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore the lexer back to non-skipping mode. 24155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexingRawMode = false; 24161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 241722f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner if (Tok.is(tok::eof)) 24185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return 2; 241922f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner return Tok.is(tok::l_paren); 24205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 24215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2422ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// \brief Find the end of a version control conflict marker. 2423d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smithstatic const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd, 2424d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith ConflictMarkerKind CMK) { 2425d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith const char *Terminator = CMK == CMK_Perforce ? "<<<<\n" : ">>>>>>>"; 2426d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith size_t TermLen = CMK == CMK_Perforce ? 5 : 7; 2427d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith StringRef RestOfBuffer(CurPtr+TermLen, BufferEnd-CurPtr-TermLen); 2428d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith size_t Pos = RestOfBuffer.find(Terminator); 24295f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner while (Pos != StringRef::npos) { 243034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Must occur at start of line. 243134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (RestOfBuffer[Pos-1] != '\r' && 243234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner RestOfBuffer[Pos-1] != '\n') { 2433d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith RestOfBuffer = RestOfBuffer.substr(Pos+TermLen); 2434d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith Pos = RestOfBuffer.find(Terminator); 243534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner continue; 243634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 243734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return RestOfBuffer.data()+Pos; 243834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 243934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return 0; 244034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner} 244134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 244234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// IsStartOfConflictMarker - If the specified pointer is the start of a version 244334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// control conflict marker like '<<<<<<<', recognize it as such, emit an error 244434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// and recover nicely. This returns true if it is a conflict marker and false 244534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// if not. 244634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::IsStartOfConflictMarker(const char *CurPtr) { 244734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Only a conflict marker if it starts at the beginning of a line. 244834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr != BufferStart && 244934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr[-1] != '\n' && CurPtr[-1] != '\r') 245034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 245134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 2452d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // Check to see if we have <<<<<<< or >>>>. 2453d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if ((BufferEnd-CurPtr < 8 || StringRef(CurPtr, 7) != "<<<<<<<") && 2454d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith (BufferEnd-CurPtr < 6 || StringRef(CurPtr, 5) != ">>>> ")) 245534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 245634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 245734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If we have a situation where we don't care about conflict markers, ignore 245834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // it. 2459d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if (CurrentConflictMarkerState || isLexingRawMode()) 246034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 246134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 2462d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith ConflictMarkerKind Kind = *CurPtr == '<' ? CMK_Normal : CMK_Perforce; 2463d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith 2464d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // Check to see if there is an ending marker somewhere in the buffer at the 2465d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // start of a line to terminate this conflict marker. 2466d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if (FindConflictEnd(CurPtr, BufferEnd, Kind)) { 246734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // We found a match. We are really in a conflict marker. 246834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Diagnose this, and ignore to the end of line. 246934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Diag(CurPtr, diag::err_conflict_marker); 2470d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith CurrentConflictMarkerState = Kind; 247134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 247234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Skip ahead to the end of line. We know this exists because the 247334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // end-of-conflict marker starts with \r or \n. 247434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner while (*CurPtr != '\r' && *CurPtr != '\n') { 247534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner assert(CurPtr != BufferEnd && "Didn't find end of line"); 247634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner ++CurPtr; 247734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 247834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner BufferPtr = CurPtr; 247934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return true; 248034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 248134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 248234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // No end of conflict marker found. 248334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 248434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner} 248534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 248634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 2487d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// HandleEndOfConflictMarker - If this is a '====' or '||||' or '>>>>', or if 2488d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// it is '<<<<' and the conflict marker started with a '>>>>' marker, then it 2489d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// is the end of a conflict marker. Handle it by ignoring up until the end of 2490d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// the line. This returns true if it is a conflict marker and false if not. 249134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::HandleEndOfConflictMarker(const char *CurPtr) { 249234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Only a conflict marker if it starts at the beginning of a line. 249334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr != BufferStart && 249434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr[-1] != '\n' && CurPtr[-1] != '\r') 249534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 249634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 249734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If we have a situation where we don't care about conflict markers, ignore 249834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // it. 2499d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if (!CurrentConflictMarkerState || isLexingRawMode()) 250034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 250134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 2502d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // Check to see if we have the marker (4 characters in a row). 2503d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith for (unsigned i = 1; i != 4; ++i) 250434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr[i] != CurPtr[0]) 250534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 250634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 250734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If we do have it, search for the end of the conflict marker. This could 250834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // fail if it got skipped with a '#if 0' or something. Note that CurPtr might 250934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // be the end of conflict marker. 2510d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith if (const char *End = FindConflictEnd(CurPtr, BufferEnd, 2511d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith CurrentConflictMarkerState)) { 251234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = End; 251334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 251434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Skip ahead to the end of line. 251534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n') 251634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner ++CurPtr; 251734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 251834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner BufferPtr = CurPtr; 251934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 252034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // No longer in the conflict marker. 2521d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith CurrentConflictMarkerState = CMK_None; 252234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return true; 252334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 252434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 252534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 252634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner} 252734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 25287d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidisbool Lexer::isCodeCompletionPoint(const char *CurPtr) const { 25297d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (PP && PP->isCodeCompletionEnabled()) { 2530a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis SourceLocation Loc = FileLoc.getLocWithOffset(CurPtr-BufferStart); 25317d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return Loc == PP->getCodeCompletionLoc(); 25327d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 25337d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 25347d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return false; 25357d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis} 25367d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 2537c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Roseuint32_t Lexer::tryReadUCN(const char *&StartPtr, const char *SlashLoc, 2538c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Token *Result) { 2539c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose unsigned CharSize; 2540c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose char Kind = getCharAndSize(StartPtr, CharSize); 2541c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2542c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose unsigned NumHexDigits; 2543c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (Kind == 'u') 2544c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose NumHexDigits = 4; 2545c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose else if (Kind == 'U') 2546c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose NumHexDigits = 8; 2547c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose else 2548c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return 0; 2549c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2550bfec916e5fc40f22ac11267e78a024cd8dcf3bbfJordan Rose if (!LangOpts.CPlusPlus && !LangOpts.C99) { 25518094bac4e987caf90e8fd719c24545add8dafcb6Jordan Rose if (Result && !isLexingRawMode()) 25528094bac4e987caf90e8fd719c24545add8dafcb6Jordan Rose Diag(SlashLoc, diag::warn_ucn_not_valid_in_c89); 2553bfec916e5fc40f22ac11267e78a024cd8dcf3bbfJordan Rose return 0; 2554bfec916e5fc40f22ac11267e78a024cd8dcf3bbfJordan Rose } 2555bfec916e5fc40f22ac11267e78a024cd8dcf3bbfJordan Rose 2556c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose const char *CurPtr = StartPtr + CharSize; 2557c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose const char *KindLoc = &CurPtr[-1]; 2558c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2559c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose uint32_t CodePoint = 0; 2560c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose for (unsigned i = 0; i < NumHexDigits; ++i) { 2561c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose char C = getCharAndSize(CurPtr, CharSize); 2562c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2563c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose unsigned Value = llvm::hexDigitValue(C); 2564c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (Value == -1U) { 2565c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (Result && !isLexingRawMode()) { 2566c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (i == 0) { 2567c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Diag(BufferPtr, diag::warn_ucn_escape_no_digits) 2568c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose << StringRef(KindLoc, 1); 2569c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } else { 2570c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Diag(BufferPtr, diag::warn_ucn_escape_incomplete); 2571b87672b124ab4fbe6f2cabc2ad71655fc71230eaJordan Rose 2572b87672b124ab4fbe6f2cabc2ad71655fc71230eaJordan Rose // If the user wrote \U1234, suggest a fixit to \u. 2573b87672b124ab4fbe6f2cabc2ad71655fc71230eaJordan Rose if (i == 4 && NumHexDigits == 8) { 2574ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose CharSourceRange URange = makeCharRange(*this, KindLoc, KindLoc + 1); 2575b87672b124ab4fbe6f2cabc2ad71655fc71230eaJordan Rose Diag(KindLoc, diag::note_ucn_four_not_eight) 2576b87672b124ab4fbe6f2cabc2ad71655fc71230eaJordan Rose << FixItHint::CreateReplacement(URange, "u"); 2577b87672b124ab4fbe6f2cabc2ad71655fc71230eaJordan Rose } 2578c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2579c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2580bfec916e5fc40f22ac11267e78a024cd8dcf3bbfJordan Rose 2581c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return 0; 2582c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2583c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2584c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose CodePoint <<= 4; 2585c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose CodePoint += Value; 2586c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2587c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose CurPtr += CharSize; 2588c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2589c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2590c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (Result) { 2591c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Result->setFlag(Token::HasUCN); 2592b6c08a64145485a7c233761220b8d82b74aa7546NAKAMURA Takumi if (CurPtr - StartPtr == (ptrdiff_t)NumHexDigits + 2) 2593c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose StartPtr = CurPtr; 2594c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose else 2595c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose while (StartPtr != CurPtr) 2596c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose (void)getAndAdvanceChar(StartPtr, *Result); 2597c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } else { 2598c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose StartPtr = CurPtr; 2599c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2600c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2601c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // C99 6.4.3p2: A universal character name shall not specify a character whose 2602c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // short identifier is less than 00A0 other than 0024 ($), 0040 (@), or 2603c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // 0060 (`), nor one in the range D800 through DFFF inclusive.) 2604c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // C++11 [lex.charset]p2: If the hexadecimal value for a 2605c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // universal-character-name corresponds to a surrogate code point (in the 2606c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // range 0xD800-0xDFFF, inclusive), the program is ill-formed. Additionally, 2607c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // if the hexadecimal value for a universal-character-name outside the 2608c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // c-char-sequence, s-char-sequence, or r-char-sequence of a character or 2609c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // string literal corresponds to a control character (in either of the 2610c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // ranges 0x00-0x1F or 0x7F-0x9F, both inclusive) or to a character in the 2611c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // basic source character set, the program is ill-formed. 2612c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (CodePoint < 0xA0) { 2613c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (CodePoint == 0x24 || CodePoint == 0x40 || CodePoint == 0x60) 2614c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return CodePoint; 2615c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2616c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // We don't use isLexingRawMode() here because we need to warn about bad 2617c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // UCNs even when skipping preprocessing tokens in a #if block. 2618c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (Result && PP) { 2619c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (CodePoint < 0x20 || CodePoint >= 0x7F) 2620c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Diag(BufferPtr, diag::err_ucn_control_character); 2621c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose else { 2622c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose char C = static_cast<char>(CodePoint); 2623c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Diag(BufferPtr, diag::err_ucn_escape_basic_scs) << StringRef(&C, 1); 2624c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2625c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2626c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2627c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return 0; 2628ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose 2629ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } else if (CodePoint >= 0xD800 && CodePoint <= 0xDFFF) { 2630c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // C++03 allows UCNs representing surrogate characters. C99 and C++11 don't. 2631ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose // We don't use isLexingRawMode() here because we need to diagnose bad 2632c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // UCNs even when skipping preprocessing tokens in a #if block. 2633ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (Result && PP) { 2634ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (LangOpts.CPlusPlus && !LangOpts.CPlusPlus11) 2635ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose Diag(BufferPtr, diag::warn_ucn_escape_surrogate); 2636ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose else 2637ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose Diag(BufferPtr, diag::err_ucn_escape_invalid); 2638ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 2639c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return 0; 2640c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2641c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2642c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return CodePoint; 2643c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose} 2644c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2645c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rosevoid Lexer::LexUnicode(Token &Result, uint32_t C, const char *CurPtr) { 264674c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose if (!isLexingRawMode() && !PP->isPreprocessedOutput() && 2647ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose isCharInSet(C, UnicodeWhitespaceChars)) { 264874c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose Diag(BufferPtr, diag::ext_unicode_whitespace) 2649ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose << makeCharRange(*this, BufferPtr, CurPtr); 2650fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose 2651fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose Result.setFlag(Token::LeadingSpace); 2652fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose if (SkipWhitespace(Result, CurPtr)) 2653fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose return; // KeepWhitespaceMode 2654fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose 2655fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose return LexTokenInternal(Result); 2656fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose } 2657fc12060ed595fd23d731b8a86adb21ddbb8c7bfbJordan Rose 2658ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (isAllowedIDChar(C, LangOpts) && isAllowedInitiallyIDChar(C, LangOpts)) { 2659ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose if (!isLexingRawMode() && !ParsingPreprocessorDirective && 2660ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose !PP->isPreprocessedOutput()) { 2661ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose maybeDiagnoseIDCharCompat(PP->getDiagnostics(), C, 2662ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose makeCharRange(*this, BufferPtr, CurPtr), 2663ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose /*IsFirst=*/true); 2664ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose } 2665ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose 2666c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose MIOpt.ReadToken(); 2667c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return LexIdentifier(Result, CurPtr); 2668c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2669c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 26700ed439487491e09faffdbabfacb1d050292c7723Jordan Rose if (!isLexingRawMode() && !ParsingPreprocessorDirective && 26710ed439487491e09faffdbabfacb1d050292c7723Jordan Rose !PP->isPreprocessedOutput() && 2672ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose !isASCII(*BufferPtr) && !isAllowedIDChar(C, LangOpts)) { 2673c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // Non-ASCII characters tend to creep into source code unintentionally. 2674c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // Instead of letting the parser complain about the unknown token, 2675c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // just drop the character. 2676c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // Note that we can /only/ do this when the non-ASCII character is actually 2677c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // spelled as Unicode, not written as a UCN. The standard requires that 2678c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // we not throw away any possible preprocessor tokens, but there's a 2679c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // loophole in the mapping of Unicode characters to basic character set 2680c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // characters that allows us to map these particular characters to, say, 2681c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // whitespace. 268274c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose Diag(BufferPtr, diag::err_non_ascii) 2683ed9c59f2ae338f6f70c1fed2ce7b0d8a5eb3ba1cJordan Rose << FixItHint::CreateRemoval(makeCharRange(*this, BufferPtr, CurPtr)); 2684c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2685c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose BufferPtr = CurPtr; 2686c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return LexTokenInternal(Result); 2687c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 2688c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 2689c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // Otherwise, we have an explicit UCN or a character that's unlikely to show 2690c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // up by accident. 2691c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose MIOpt.ReadToken(); 2692c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose FormTokenWithChars(Result, CurPtr, tok::unknown); 2693c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose} 2694c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 26955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 26965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexTokenInternal - This implements a simple C family lexer. It is an 26975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// extremely performance critical piece of code. This assumes that the buffer 2698efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// has a null character at the end of the file. This returns a preprocessing 2699efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// token, not a normal token, as such, it is an internal interface. It assumes 2700efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// that the Flags of result have been cleared before calling this. 2701d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexTokenInternal(Token &Result) { 27025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerLexNextToken: 27035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // New token, can't need cleaning yet. 2704d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::NeedsCleaning); 27055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result.setIdentifierInfo(0); 27061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // CurPtr - Cache BufferPtr in an automatic variable. 27085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *CurPtr = BufferPtr; 27095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 27105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Small amounts of horizontal whitespace is very common between tokens. 27115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if ((*CurPtr == ' ') || (*CurPtr == '\t')) { 27125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 27135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while ((*CurPtr == ' ') || (*CurPtr == '\t')) 27145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 27151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2716d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // If we are keeping whitespace and other tokens, just return what we just 2717d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // skipped. The next lexer invocation will return the token after the 2718d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // whitespace. 2719d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (isKeepWhitespaceMode()) { 27209e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 2721d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; 2722d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner } 27231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 2725d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 27265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 27271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below. 27291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Read a character, advancing over it. 27315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Char = getAndAdvanceChar(CurPtr, Result); 27329e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner tok::TokenKind Kind; 27331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Char) { 27355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 0: // Null. 27365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Found end of file? 27375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr-1 == BufferEnd) { 27385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Read the PP instance variable into an automatic variable, because 27395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // LexEndOfFile will often delete 'this'. 2740168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner Preprocessor *PPCache = PP; 27415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. 27425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; // Got a token to return. 2743168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); 2744168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner return PPCache->Lex(Result); 27455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 27461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27477d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis // Check if we are performing code completion. 27487d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis if (isCodeCompletionPoint(CurPtr-1)) { 27497d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis // Return the code-completion token. 27507d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis Result.startToken(); 27517d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis FormTokenWithChars(Result, CurPtr, tok::code_completion); 27527d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis return; 27537d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis } 27547d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis 275574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 275674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::null_in_file); 2757d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 2758d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 2759d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 27601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 2762a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner 2763a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner case 26: // DOS & CP/M EOF: "^Z". 2764a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // If we're in Microsoft extensions mode, treat this as end of file. 27654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.MicrosoftExt) { 2766a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // Read the PP instance variable into an automatic variable, because 2767a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // LexEndOfFile will often delete 'this'. 2768a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner Preprocessor *PPCache = PP; 2769a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. 2770a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner return; // Got a token to return. 2771a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); 2772a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner return PPCache->Lex(Result); 2773a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner } 2774a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // If Microsoft extensions are disabled, this is just random garbage. 2775a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner Kind = tok::unknown; 2776a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner break; 2777a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner 27785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\n': 27795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\r': 27805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are inside a preprocessor directive and we see the end of line, 278184021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // we know we are done with the directive, so return an EOD token. 27825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 27835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Done parsing the "line". 27845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParsingPreprocessorDirective = false; 27851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore comment saving mode, in case it was disabled for directive. 27871a8354659a6007bbae3b5d9161a56ecc8f61a219David Blaikie if (PP) 27888c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie SetCommentRetentionState(PP->getCommentRetentionState()); 27891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 27905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Since we consumed a newline, we are back at the start of a line. 27915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IsAtStartOfLine = true; 27921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 279384021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne Kind = tok::eod; 27945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 27955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 27965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The returned token is at the start of the line. 2797d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 27985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // No leading whitespace seen so far. 2799d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::LeadingSpace); 28001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2801d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 2802d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 28035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 28045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ' ': 28055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\t': 28065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\f': 28075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\v': 28088133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner SkipHorizontalWhitespace: 2809d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 2810d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 2811d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 28128133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner 28138133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner SkipIgnoredUnits: 28148133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner CurPtr = BufferPtr; 28151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 28168133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner // If the next token is obviously a // or /* */ comment, skip it efficiently 28178133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner // too (without going through the big switch stmt). 28188402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() && 2819bb23628148f555a4cf71f98c27096a7a804c085cNico Weber LangOpts.LineComment && !LangOpts.TraditionalCPP) { 2820bb23628148f555a4cf71f98c27096a7a804c085cNico Weber if (SkipLineComment(Result, CurPtr+2)) 2821046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 28228133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipIgnoredUnits; 2823fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) { 2824046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner if (SkipBlockComment(Result, CurPtr+2)) 2825046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 28268133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipIgnoredUnits; 28278133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner } else if (isHorizontalWhitespace(*CurPtr)) { 28288133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipHorizontalWhitespace; 28298133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner } 28305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 2831a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner 28323a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // C99 6.4.4.1: Integer Constants. 28333a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // C99 6.4.4.2: Floating Constants. 28343a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '0': case '1': case '2': case '3': case '4': 28353a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '5': case '6': case '7': case '8': case '9': 28363a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Notify MIOpt that we read a non-whitespace/non-comment token. 28373a5707766850f9ee9daa35299794328b5caf96dcChris Lattner MIOpt.ReadToken(); 28383a5707766850f9ee9daa35299794328b5caf96dcChris Lattner return LexNumericConstant(Result, CurPtr); 28391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 28405cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor case 'u': // Identifier (uber) or C++0x UTF-8 or UTF-16 string literal 28415cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // Notify MIOpt that we read a non-whitespace/non-comment token. 28425cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor MIOpt.ReadToken(); 28435cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 284480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (LangOpts.CPlusPlus11) { 28455cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor Char = getCharAndSize(CurPtr, SizeTmp); 28465cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 28475cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // UTF-16 string literal 28485cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (Char == '"') 28495cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), 28505cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::utf16_string_literal); 28515cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 28525cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // UTF-16 character constant 28535cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (Char == '\'') 28545cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result), 28555cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::utf16_char_constant); 28565cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 28572fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // UTF-16 raw string literal 28582fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char == 'R' && getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"') 28592fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 28602fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 28612fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 28622fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::utf16_string_literal); 28632fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 28642fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char == '8') { 28652fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper char Char2 = getCharAndSize(CurPtr + SizeTmp, SizeTmp2); 28662fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 28672fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // UTF-8 string literal 28682fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char2 == '"') 28692fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexStringLiteral(Result, 28702fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 28712fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 28722fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::utf8_string_literal); 28732fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 28742fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char2 == 'R') { 28752fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper unsigned SizeTmp3; 28762fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper char Char3 = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3); 28772fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // UTF-8 raw string literal 28782fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char3 == '"') { 28792fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 28802fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 28812fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 28822fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp3, Result), 28832fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::utf8_string_literal); 28842fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 28852fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 28862fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 28875cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor } 28885cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 28895cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // treat u like the start of an identifier. 28905cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexIdentifier(Result, CurPtr); 28915cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 28925cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor case 'U': // Identifier (Uber) or C++0x UTF-32 string literal 28935cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // Notify MIOpt that we read a non-whitespace/non-comment token. 28945cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor MIOpt.ReadToken(); 28955cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 289680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (LangOpts.CPlusPlus11) { 28975cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor Char = getCharAndSize(CurPtr, SizeTmp); 28985cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 28995cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // UTF-32 string literal 29005cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (Char == '"') 29015cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), 29025cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::utf32_string_literal); 29035cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 29045cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // UTF-32 character constant 29055cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (Char == '\'') 29065cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result), 29075cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::utf32_char_constant); 29082fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 29092fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // UTF-32 raw string literal 29102fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char == 'R' && getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"') 29112fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 29122fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 29132fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 29142fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::utf32_string_literal); 29155cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor } 29165cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 29175cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor // treat U like the start of an identifier. 29185cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexIdentifier(Result, CurPtr); 29195cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor 29202fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper case 'R': // Identifier or C++0x raw string literal 29212fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Notify MIOpt that we read a non-whitespace/non-comment token. 29222fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper MIOpt.ReadToken(); 29232fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 292480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (LangOpts.CPlusPlus11) { 29252fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper Char = getCharAndSize(CurPtr, SizeTmp); 29262fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 29272fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper if (Char == '"') 29282fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 29292fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(CurPtr, SizeTmp, Result), 29302fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::string_literal); 29312fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper } 29322fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 29332fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // treat R like the start of an identifier. 29342fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexIdentifier(Result, CurPtr); 29352fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 29363a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz"). 29375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 29385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 29395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 29405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 29415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Wide string literal. 29425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '"') 29435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), 29445cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::wide_string_literal); 29455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 29462fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper // Wide raw string literal. 294780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (LangOpts.CPlusPlus11 && Char == 'R' && 29482fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"') 29492fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper return LexRawStringLiteral(Result, 29502fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 29512fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper SizeTmp2, Result), 29522fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper tok::wide_string_literal); 29532fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper 29545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Wide character constant. 29555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '\'') 29565cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result), 29575cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor tok::wide_char_constant); 29585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FALL THROUGH, treating L like the start of an identifier. 29591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 29605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.2: Identifiers. 29615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': 29625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N': 29632fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper case 'O': case 'P': case 'Q': /*'R'*/case 'S': case 'T': /*'U'*/ 29645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'V': case 'W': case 'X': case 'Y': case 'Z': 29655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': 29665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': 29675cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor case 'o': case 'p': case 'q': case 'r': case 's': case 't': /*'u'*/ 29685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'v': case 'w': case 'x': case 'y': case 'z': 29695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '_': 29705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 29715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 29725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexIdentifier(Result, CurPtr); 29733a5707766850f9ee9daa35299794328b5caf96dcChris Lattner 29743a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '$': // $ in identifiers. 29754e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.DollarIdents) { 297674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 297774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::ext_dollar_in_identifier); 29783a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Notify MIOpt that we read a non-whitespace/non-comment token. 29793a5707766850f9ee9daa35299794328b5caf96dcChris Lattner MIOpt.ReadToken(); 29803a5707766850f9ee9daa35299794328b5caf96dcChris Lattner return LexIdentifier(Result, CurPtr); 29813a5707766850f9ee9daa35299794328b5caf96dcChris Lattner } 29821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 29839e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 29843a5707766850f9ee9daa35299794328b5caf96dcChris Lattner break; 29851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 29865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.4: Character Constants. 29875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\'': 29885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 29895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 29905cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexCharConstant(Result, CurPtr, tok::char_constant); 29915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 29925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.5: String Literals. 29935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '"': 29945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 29955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 29965cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor return LexStringLiteral(Result, CurPtr, tok::string_literal); 29975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 29985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.6: Punctuators. 29995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '?': 30009e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::question; 30015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '[': 30039e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_square; 30045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ']': 30069e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_square; 30075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '(': 30099e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_paren; 30105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ')': 30129e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_paren; 30135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '{': 30159e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_brace; 30165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '}': 30189e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_brace; 30195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '.': 30215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 30225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char >= '0' && Char <= '9') { 30235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 30245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 30255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 30265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); 30274e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.CPlusPlus && Char == '*') { 30289e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::periodstar; 30295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += SizeTmp; 30305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '.' && 30315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') { 30329e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ellipsis; 30335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 30345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 30355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 30369e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::period; 30375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 30385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '&': 30405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 30415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '&') { 30429e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ampamp; 30435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 30459e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ampequal; 30465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 30489e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::amp; 30495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 30505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case '*': 30525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (getCharAndSize(CurPtr, SizeTmp) == '=') { 30539e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::starequal; 30545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 30569e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::star; 30575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 30585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '+': 30605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 30615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '+') { 30625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30639e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plusplus; 30645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 30655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30669e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plusequal; 30675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 30689e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plus; 30695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 30705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '-': 30725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 30739e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner if (Char == '-') { // -- 30745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30759e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minusminus; 30764e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (Char == '>' && LangOpts.CPlusPlus && 30779e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->* 30785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 30795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 30809e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::arrowstar; 30819e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner } else if (Char == '>') { // -> 30825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30839e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::arrow; 30849e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner } else if (Char == '=') { // -= 30855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30869e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minusequal; 30875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 30889e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minus; 30895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 30905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '~': 30929e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::tilde; 30935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 30945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '!': 30955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (getCharAndSize(CurPtr, SizeTmp) == '=') { 30969e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::exclaimequal; 30975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 30985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 30999e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::exclaim; 31005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 31015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 31025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '/': 31035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // 6.4.9: Comments 31045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 3105bb23628148f555a4cf71f98c27096a7a804c085cNico Weber if (Char == '/') { // Line comment. 3106bb23628148f555a4cf71f98c27096a7a804c085cNico Weber // Even if Line comments are disabled (e.g. in C89 mode), we generally 31078402c73dd880e8af46c826d873681820aebe32ebChris Lattner // want to lex this as a comment. There is one problem with this though, 31088402c73dd880e8af46c826d873681820aebe32ebChris Lattner // that in one particular corner case, this can change the behavior of the 31098402c73dd880e8af46c826d873681820aebe32ebChris Lattner // resultant program. For example, In "foo //**/ bar", C89 would lex 3110bb23628148f555a4cf71f98c27096a7a804c085cNico Weber // this as "foo / bar" and langauges with Line comments would lex it as 31118402c73dd880e8af46c826d873681820aebe32ebChris Lattner // "foo". Check to see if the character after the second slash is a '*'. 31128402c73dd880e8af46c826d873681820aebe32ebChris Lattner // If so, we will lex that as a "/" instead of the start of a comment. 31132ed42287b9b88a0bb08ab10e95e306da3f294ba9Daniel Dunbar // However, we never do this in -traditional-cpp mode. 3114bb23628148f555a4cf71f98c27096a7a804c085cNico Weber if ((LangOpts.LineComment || 31152ed42287b9b88a0bb08ab10e95e306da3f294ba9Daniel Dunbar getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') && 31164e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie !LangOpts.TraditionalCPP) { 3117bb23628148f555a4cf71f98c27096a7a804c085cNico Weber if (SkipLineComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) 3118046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 31191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 31208402c73dd880e8af46c826d873681820aebe32ebChris Lattner // It is common for the tokens immediately after a // comment to be 31218402c73dd880e8af46c826d873681820aebe32ebChris Lattner // whitespace (indentation for the next line). Instead of going through 31228402c73dd880e8af46c826d873681820aebe32ebChris Lattner // the big switch, handle it efficiently now. 31238402c73dd880e8af46c826d873681820aebe32ebChris Lattner goto SkipIgnoredUnits; 31248402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 31258402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 31261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 31278402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (Char == '*') { // /**/ comment. 31285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) 3129046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 31302d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner goto LexNextToken; // GCC isn't tail call eliminating. 31318402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 31321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 31338402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (Char == '=') { 31345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31359e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::slashequal; 31365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 31379e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::slash; 31385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 31395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 31405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '%': 31415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 31425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 31439e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::percentequal; 31445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31454e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.Digraphs && Char == '>') { 31469e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_brace; // '%>' -> '}' 31475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.Digraphs && Char == ':') { 31495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 31505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 31515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') { 31529e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashhash; // '%:%:' -> '##' 31535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 31545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 31554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (Char == '@' && LangOpts.MicrosoftExt) {// %:@ -> #@ -> Charize 31565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 315774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 315866d5ce11b9426f6a59f61a03cbd8dbf047cc9350Ted Kremenek Diag(BufferPtr, diag::ext_charize_microsoft); 31599e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashat; 3160e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner } else { // '%:' -> '#' 31615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We parsed a # character. If this occurs at the start of the line, 31625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it's actually the start of a preprocessing directive. Callback to 31635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the preprocessor to handle it. 31645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: -fpreprocessed mode?? 31653185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) 31663185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis goto HandleDirective; 31671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3168e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner Kind = tok::hash; 31695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 31705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 31719e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::percent; 31725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 31735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 31745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '<': 31755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 31765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingFilename) { 31779cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner return LexAngledStringLiteral(Result, CurPtr); 31785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '<') { 317934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2); 318034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (After == '=') { 318134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::lesslessequal; 318234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 318334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner SizeTmp2, Result); 318434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) { 318534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If this is actually a '<<<<<<<' version control conflict marker, 318634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // recognize it as such and recover nicely. 318734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 3188d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith } else if (After == '<' && HandleEndOfConflictMarker(CurPtr-1)) { 3189d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // If this is '<<<<' and we're in a Perforce-style conflict marker, 3190d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // ignore it. 3191d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith goto LexNextToken; 31924e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.CUDA && After == '<') { 31931b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne Kind = tok::lesslessless; 31941b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 31951b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne SizeTmp2, Result); 319634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else { 319734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 319834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::lessless; 319934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 32005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 32015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32029e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::lessequal; 32034e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.Digraphs && Char == ':') { // '<:' -> '[' 320480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith if (LangOpts.CPlusPlus11 && 320587a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == ':') { 320687a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // C++0x [lex.pptoken]p3: 320787a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // Otherwise, if the next three characters are <:: and the subsequent 320887a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // character is neither : nor >, the < is treated as a preprocessor 320987a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // token by itself and not as the first character of the alternative 321087a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // token <:. 321187a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith unsigned SizeTmp3; 321287a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith char After = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3); 321387a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith if (After != ':' && After != '>') { 321487a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith Kind = tok::less; 3215661a99690bc133bbaa029da925481d4a860dec90Richard Smith if (!isLexingRawMode()) 3216661a99690bc133bbaa029da925481d4a860dec90Richard Smith Diag(BufferPtr, diag::warn_cxx98_compat_less_colon_colon); 321787a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith break; 321887a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith } 321987a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith } 322087a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith 32215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32229e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_square; 32234e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.Digraphs && Char == '%') { // '<%' -> '{' 32245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32259e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_brace; 32265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 32279e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::less; 32285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 32295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 32305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '>': 32315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 32325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 32335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32349e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::greaterequal; 32355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '>') { 323634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2); 323734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (After == '=') { 323834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 323934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner SizeTmp2, Result); 324034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::greatergreaterequal; 3241d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith } else if (After == '>' && IsStartOfConflictMarker(CurPtr-1)) { 3242d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // If this is actually a '>>>>' conflict marker, recognize it as such 3243d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // and recover nicely. 3244d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith goto LexNextToken; 324534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) { 324634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If this is '>>>>>>>' and we're in a conflict marker, ignore it. 324734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 32484e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.CUDA && After == '>') { 32491b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne Kind = tok::greatergreatergreater; 32501b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 32511b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne SizeTmp2, Result); 325234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else { 325334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 325434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::greatergreater; 325534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 325634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 32575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 32589e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::greater; 32595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 32605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 32615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '^': 32625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 32635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 32645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32659e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::caretequal; 32665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 32679e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::caret; 32685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 32695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 32705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '|': 32715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 32725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 32739e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipeequal; 32745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '|') { 327634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If this is '|||||||' and we're in a conflict marker, ignore it. 327734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1)) 327834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 32799e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipepipe; 32805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 32829e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipe; 32835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 32845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 32855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ':': 32865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 32874e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (LangOpts.Digraphs && Char == '>') { 32889e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_square; // ':>' -> ']' 32895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (LangOpts.CPlusPlus && Char == ':') { 32919e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::coloncolon; 32925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 32931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else { 32949e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::colon; 32955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 32965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 32975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ';': 32989e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::semi; 32995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 33005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '=': 33015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 33025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 3303d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith // If this is '====' and we're in a conflict marker, ignore it. 330434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1)) 330534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 330634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 33079e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::equalequal; 33085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 33091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else { 33109e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::equal; 33115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 33125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 33135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ',': 33149e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::comma; 33155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 33165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '#': 33175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 33185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '#') { 33199e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashhash; 33205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 33214e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (Char == '@' && LangOpts.MicrosoftExt) { // #@ -> Charize 33229e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashat; 332374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 332466d5ce11b9426f6a59f61a03cbd8dbf047cc9350Ted Kremenek Diag(BufferPtr, diag::ext_charize_microsoft); 33255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 33265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 33275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We parsed a # character. If this occurs at the start of the line, 33285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it's actually the start of a preprocessing directive. Callback to 33295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the preprocessor to handle it. 33305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: -fpreprocessed mode?? 33313185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) 33323185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis goto HandleDirective; 33331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3334e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner Kind = tok::hash; 33355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 33365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 33375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 33383a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '@': 33393a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Objective C support. 33404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CurPtr[-1] == '@' && LangOpts.ObjC1) 33419e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::at; 33423a5707766850f9ee9daa35299794328b5caf96dcChris Lattner else 33439e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 33443a5707766850f9ee9daa35299794328b5caf96dcChris Lattner break; 33451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3346c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // UCNs (C99 6.4.3, C++11 [lex.charset]p2) 33475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\\': 3348c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (uint32_t CodePoint = tryReadUCN(CurPtr, BufferPtr, &Result)) 3349c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return LexUnicode(Result, CodePoint, CurPtr); 3350c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 33519e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 33525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 3353c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 3354c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose default: { 3355c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (isASCII(Char)) { 3356c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose Kind = tok::unknown; 3357c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose break; 3358c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 3359c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 3360c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose UTF32 CodePoint; 3361c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 3362c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // We can't just reset CurPtr to BufferPtr because BufferPtr may point to 3363c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // an escaped newline. 3364c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose --CurPtr; 3365cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko ConversionResult Status = 3366cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko llvm::convertUTF8Sequence((const UTF8 **)&CurPtr, 3367cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko (const UTF8 *)BufferEnd, 3368cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko &CodePoint, 3369cb5620c9b213f4bd323912159fdddda35e258a14Dmitri Gribenko strictConversion); 3370c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose if (Status == conversionOK) 3371c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose return LexUnicode(Result, CodePoint, CurPtr); 3372c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 33730ed439487491e09faffdbabfacb1d050292c7723Jordan Rose if (isLexingRawMode() || ParsingPreprocessorDirective || 33740ed439487491e09faffdbabfacb1d050292c7723Jordan Rose PP->isPreprocessedOutput()) { 337520afc2977cd0a6bacbe6218a633cd59a24463e2fJordan Rose ++CurPtr; 337674c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose Kind = tok::unknown; 337774c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose break; 337874c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose } 337974c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose 3380c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // Non-ASCII characters tend to creep into source code unintentionally. 3381c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose // Instead of letting the parser complain about the unknown token, 3382ae82c2b7b62d742f56638fa3cfb0f550ddcaf315Jordan Rose // just diagnose the invalid UTF-8, then drop the character. 338374c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose Diag(CurPtr, diag::err_invalid_utf8); 3384c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose 3385c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose BufferPtr = CurPtr+1; 3386c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose goto LexNextToken; 3387c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose } 33885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 33891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 33905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 33915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 33925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 33935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 33949e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, Kind); 33953185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis return; 33963185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis 33973185d4ac30378995ef70421e2848f77524c2b5d5Argyrios KyrtzidisHandleDirective: 33983185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // We parsed a # character and it's the start of a preprocessing directive. 33993185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis 34003185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis FormTokenWithChars(Result, CurPtr, tok::hash); 34013185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis PP->HandleDirective(Result); 34023185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis 34033185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // As an optimization, if the preprocessor didn't switch lexers, tail 34043185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // recurse. 34053185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis if (PP->isCurrentLexer(this)) { 34063185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // Start a new token. If this is a #include or something, the PP may 34073185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // want us starting at the beginning of the line again. If so, set 34083185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis // the StartOfLine flag and clear LeadingSpace. 34093185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis if (IsAtStartOfLine) { 34103185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis Result.setFlag(Token::StartOfLine); 34113185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis Result.clearFlag(Token::LeadingSpace); 34123185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis IsAtStartOfLine = false; 34133185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis } 34143185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis goto LexNextToken; // GCC isn't tail call eliminating. 34153185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis } 34163185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis return PP->Lex(Result); 34175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3418