Lexer.cpp revision bf340e452339e374ea6eef78c1f0a2abdd16c5a3
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" 285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Preprocessor.h" 29500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Lex/LexDiagnostic.h" 3055817afdf9d453a443262a733f6caf6692dca118Douglas Gregor#include "clang/Lex/CodeCompletionHandler.h" 319dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner#include "clang/Basic/SourceManager.h" 32f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor#include "llvm/ADT/StringSwitch.h" 33409a03671224d4e5bdab1594c43baf070148f830Chris Lattner#include "llvm/Support/Compiler.h" 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Support/MemoryBuffer.h" 355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <cctype> 365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 38a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic void InitCharacterInfo(); 395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 40dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 41dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner// Token Class Implementation 42dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 43dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier. 45dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattnerbool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const { 46bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor if (IdentifierInfo *II = getIdentifierInfo()) 47bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor return II->getObjCKeywordID() == objcKey; 48bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor return false; 49dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner} 50dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 51dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner/// getObjCKeywordID - Return the ObjC keyword kind. 52dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattnertok::ObjCKeywordKind Token::getObjCKeywordID() const { 53dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner IdentifierInfo *specId = getIdentifierInfo(); 54dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword; 55dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner} 56dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 5753702cd401b8fdca985aede7732c2f6a82ad9b1cChris Lattner 58dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 59dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner// Lexer Class Implementation 60dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 61dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Lexer::InitLexer(const char *BufStart, const char *BufPtr, 6322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner const char *BufEnd) { 64a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner InitCharacterInfo(); 651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner BufferStart = BufStart; 6722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner BufferPtr = BufPtr; 6822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner BufferEnd = BufEnd; 691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner assert(BufEnd[0] == 0 && 7122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner "We assume that the input buffer has a null character at the end" 7222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner " to simplify lexing!"); 731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 74156119df1d076b63609618976281961283f871dbEric Christopher // Check whether we have a BOM in the beginning of the buffer. If yes - act 75156119df1d076b63609618976281961283f871dbEric Christopher // accordingly. Right now we support only UTF-8 with and without BOM, so, just 76156119df1d076b63609618976281961283f871dbEric Christopher // skip the UTF-8 BOM if it's present. 77156119df1d076b63609618976281961283f871dbEric Christopher if (BufferStart == BufferPtr) { 78156119df1d076b63609618976281961283f871dbEric Christopher // Determine the size of the BOM. 795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Buf(BufferStart, BufferEnd - BufferStart); 80969f9d47338fc36ebb6d24ad3a51e45eda07fd58Eli Friedman size_t BOMLength = llvm::StringSwitch<size_t>(Buf) 81156119df1d076b63609618976281961283f871dbEric Christopher .StartsWith("\xEF\xBB\xBF", 3) // UTF-8 BOM 82156119df1d076b63609618976281961283f871dbEric Christopher .Default(0); 83156119df1d076b63609618976281961283f871dbEric Christopher 84156119df1d076b63609618976281961283f871dbEric Christopher // Skip the BOM. 85156119df1d076b63609618976281961283f871dbEric Christopher BufferPtr += BOMLength; 86156119df1d076b63609618976281961283f871dbEric Christopher } 87156119df1d076b63609618976281961283f871dbEric Christopher 8822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner Is_PragmaLexer = false; 8934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner IsInConflictMarker = false; 90156119df1d076b63609618976281961283f871dbEric Christopher 9122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // Start of the file is a start of line. 9222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner IsAtStartOfLine = true; 931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // We are not after parsing a #. 9522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner ParsingPreprocessorDirective = false; 961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // We are not after parsing #include. 9822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner ParsingFilename = false; 991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // We are not in raw mode. Raw mode disables diagnostics and interpretation 10122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // of tokens (e.g. identifiers, thus disabling macro expansion). It is used 10222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block 10322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // or otherwise skipping over tokens. 10422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner LexingRawMode = false; 1051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // Default to not keeping comments. 10722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner ExtendedTokenMode = 0; 10822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner} 10922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner 1100770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// Lexer constructor - Create a new lexer object for the specified buffer 1110770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// with the specified preprocessor managing the lexing process. This lexer 1120770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// assumes that the associated file buffer and Preprocessor objects will 1130770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// outlive it, so it doesn't take ownership of either of them. 1146e2901407bff59aeb4cc301cc58b034723d0eb49Chris LattnerLexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP) 11588d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner : PreprocessorLexer(&PP, FID), 11688d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)), 11788d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner Features(PP.getLangOptions()) { 1181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1190770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(), 1200770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner InputFile->getBufferEnd()); 1211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1220770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner // Default to keeping comments if the preprocessor wants them. 1230770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner SetCommentRetentionState(PP.getCommentRetentionState()); 1240770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner} 125dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 126168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner/// Lexer constructor - Create a new raw lexer object. This object is only 127590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner/// suitable for calls to 'LexRawToken'. This lexer assumes that the text 128590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner/// range will outlive it, so it doesn't take ownership of it. 129168ae2d44a443da75ea85db5f3b5081eb0bce113Chris LattnerLexer::Lexer(SourceLocation fileloc, const LangOptions &features, 130de96c0f29c4cacabe6ea577c61db87c2a85aea6cChris Lattner const char *BufStart, const char *BufPtr, const char *BufEnd) 131c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner : FileLoc(fileloc), Features(features) { 13222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner 13322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner InitLexer(BufStart, BufPtr, BufEnd); 1341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 135168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner // We *are* in raw mode. 136168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner LexingRawMode = true; 1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 139025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// Lexer constructor - Create a new raw lexer object. This object is only 140025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// suitable for calls to 'LexRawToken'. This lexer assumes that the text 141025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// range will outlive it, so it doesn't take ownership of it. 1426e2901407bff59aeb4cc301cc58b034723d0eb49Chris LattnerLexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile, 1436e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner const SourceManager &SM, const LangOptions &features) 144025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) { 145025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner 1461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(), 147025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner FromFile->getBufferEnd()); 1481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 149025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner // We *are* in raw mode. 150025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner LexingRawMode = true; 151025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner} 152025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner 15342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for 15442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// _Pragma expansion. This has a variety of magic semantics that this method 15542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// sets up. It returns a new'd Lexer that must be delete'd when done. 15642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 15742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// On entrance to this routine, TokStartLoc is a macro location which has a 15842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// spelling loc that indicates the bytes to be lexed for the token and an 159433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// expansion location that indicates where all lexed tokens should be 16042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// "expanded from". 16142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 16242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// FIXME: It would really be nice to make _Pragma just be a wrapper around a 16342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// normal lexer that remaps tokens as they fly by. This would require making 16442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer 16542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// interface that could handle this stuff. This would pull GetMappedTokenLoc 16642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// out of the critical path of the lexer! 16742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 1681eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpLexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc, 169433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation ExpansionLocStart, 170433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation ExpansionLocEnd, 171bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner unsigned TokLen, Preprocessor &PP) { 17242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner SourceManager &SM = PP.getSourceManager(); 17342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 17442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Create the lexer as if we were going to lex the file normally. 175a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner FileID SpellingFID = SM.getFileID(SpellingLoc); 1766e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID); 1776e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner Lexer *L = new Lexer(SpellingFID, InputFile, PP); 1781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Now that the lexer is created, change the start/end locations so that we 18042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // just lex the subsection of the file that we want. This is lexing from a 18142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // scratch buffer. 18242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner const char *StrData = SM.getCharacterData(SpellingLoc); 1831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->BufferPtr = StrData; 18542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->BufferEnd = StrData+TokLen; 1861fa495304c81e03f07f278a47b5efe9317104aabChris Lattner assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!"); 18742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 18842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Set the SourceLocation with the remapping information. This ensures that 18942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // GetMappedTokenLoc will remap the tokens as they are lexed. 190bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth L->FileLoc = SM.createExpansionLoc(SM.getLocForStartOfFile(SpellingFID), 191bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth ExpansionLocStart, 192bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth ExpansionLocEnd, TokLen); 1931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Ensure that the lexer thinks it is inside a directive, so that end \n will 19584021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // return an EOD token. 19642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->ParsingPreprocessorDirective = true; 1971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // This lexer really is for _Pragma. 19942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->Is_PragmaLexer = true; 20042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner return L; 20142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner} 20242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 203168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner 2045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Stringify - Convert the specified string into a C string, with surrounding 2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ""'s, and with escaped \ and " characters. 2065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstd::string Lexer::Stringify(const std::string &Str, bool Charify) { 2075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer std::string Result = Str; 2085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Quote = Charify ? '\'' : '"'; 2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = 0, e = Result.size(); i != e; ++i) { 2105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Result[i] == '\\' || Result[i] == Quote) { 2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result.insert(Result.begin()+i, '\\'); 2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++i; ++e; 2135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return Result; 2165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 218d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// Stringify - Convert the specified string into a C string by escaping '\' 219d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// and " characters. This does not add surrounding ""'s to the string. 2205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid Lexer::Stringify(SmallVectorImpl<char> &Str) { 221d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner for (unsigned i = 0, e = Str.size(); i != e; ++i) { 222d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner if (Str[i] == '\\' || Str[i] == '"') { 223d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner Str.insert(Str.begin()+i, '\\'); 224d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner ++i; ++e; 225d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner } 226d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner } 227d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner} 228d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner 229b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner//===----------------------------------------------------------------------===// 230b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner// Token Spelling 231b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner//===----------------------------------------------------------------------===// 232b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 233b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// getSpelling() - Return the 'spelling' of this token. The spelling of a 234b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// token are the characters used to represent the token in the source file 235b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// after trigraph expansion and escaped-newline folding. In particular, this 236b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// wants to get the true, uncanonicalized, spelling of things like digraphs 237b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// UCNs, etc. 2385f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerStringRef Lexer::getSpelling(SourceLocation loc, 2395f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<char> &buffer, 240834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall const SourceManager &SM, 241834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall const LangOptions &options, 242834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall bool *invalid) { 243834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Break down the source location. 244834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc); 245834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 246834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Try to the load the file buffer. 247834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall bool invalidTemp = false; 2485f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); 249834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall if (invalidTemp) { 250834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall if (invalid) *invalid = true; 2515f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return StringRef(); 252834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall } 253834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 254834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall const char *tokenBegin = file.data() + locInfo.second; 255834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 256834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Lex from the start of the given location. 257834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall Lexer lexer(SM.getLocForStartOfFile(locInfo.first), options, 258834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall file.begin(), tokenBegin, file.end()); 259834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall Token token; 260834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall lexer.LexFromRawLexer(token); 261834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 262834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall unsigned length = token.getLength(); 263834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 264834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Common case: no need for cleaning. 265834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall if (!token.needsCleaning()) 2665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return StringRef(tokenBegin, length); 267834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 268834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall // Hard case, we need to relex the characters into the string. 269834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall buffer.clear(); 270834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall buffer.reserve(length); 271834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 272834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall for (const char *ti = tokenBegin, *te = ti + length; ti != te; ) { 273834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall unsigned charSize; 274834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall buffer.push_back(Lexer::getCharAndSizeNoWarn(ti, charSize, options)); 275834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall ti += charSize; 276834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall } 277834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 2785f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return StringRef(buffer.data(), buffer.size()); 279834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall} 280834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall 281834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// getSpelling() - Return the 'spelling' of this token. The spelling of a 282834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// token are the characters used to represent the token in the source file 283834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// after trigraph expansion and escaped-newline folding. In particular, this 284834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// wants to get the true, uncanonicalized, spelling of things like digraphs 285834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// UCNs, etc. 286b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattnerstd::string Lexer::getSpelling(const Token &Tok, const SourceManager &SourceMgr, 287b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner const LangOptions &Features, bool *Invalid) { 288b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); 289b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 290b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner // If this token contains nothing interesting, return it directly. 291b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner bool CharDataInvalid = false; 292b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner const char* TokStart = SourceMgr.getCharacterData(Tok.getLocation(), 293b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner &CharDataInvalid); 294b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (Invalid) 295b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner *Invalid = CharDataInvalid; 296b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (CharDataInvalid) 297b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return std::string(); 298b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 299b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (!Tok.needsCleaning()) 300b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return std::string(TokStart, TokStart+Tok.getLength()); 301b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 302b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner std::string Result; 303b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Result.reserve(Tok.getLength()); 304b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 305b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner // Otherwise, hard case, relex the characters into the string. 306b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); 307b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Ptr != End; ) { 308b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner unsigned CharSize; 309b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features)); 310b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Ptr += CharSize; 311b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 312b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner assert(Result.size() != unsigned(Tok.getLength()) && 313b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner "NeedsCleaning flag set on something that didn't need cleaning!"); 314b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return Result; 315b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner} 316b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 317b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// getSpelling - This method is used to get the spelling of a token into a 318b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// preallocated buffer, instead of as an std::string. The caller is required 319b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// to allocate enough space for the token, which is guaranteed to be at least 320b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// Tok.getLength() bytes long. The actual length of the token is returned. 321b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// 322b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// Note that this method may do two possible things: it may either fill in 323b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// the buffer specified with characters, or it may *change the input pointer* 324b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// to point to a constant buffer with the data already in it (avoiding a 325b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// copy). The caller is not allowed to modify the returned buffer pointer 326b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// if an internal buffer is returned. 327b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattnerunsigned Lexer::getSpelling(const Token &Tok, const char *&Buffer, 328b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner const SourceManager &SourceMgr, 329b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner const LangOptions &Features, bool *Invalid) { 330b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); 331c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 332c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara const char *TokStart = 0; 333c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // NOTE: this has to be checked *before* testing for an IdentifierInfo. 334c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara if (Tok.is(tok::raw_identifier)) 335c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara TokStart = Tok.getRawIdentifierData(); 336c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara else if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { 337c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // Just return the string from the identifier table, which is very quick. 338b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Buffer = II->getNameStart(); 339b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return II->getLength(); 340b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 341c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 342c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // NOTE: this can be checked even after testing for an IdentifierInfo. 343b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (Tok.isLiteral()) 344b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner TokStart = Tok.getLiteralData(); 345c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 346b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (TokStart == 0) { 347c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // Compute the start of the token in the input lexer buffer. 348b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner bool CharDataInvalid = false; 349b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner TokStart = SourceMgr.getCharacterData(Tok.getLocation(), &CharDataInvalid); 350b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (Invalid) 351b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner *Invalid = CharDataInvalid; 352b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (CharDataInvalid) { 353b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Buffer = ""; 354b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return 0; 355b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 356b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 357c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 358b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner // If this token contains nothing interesting, return it directly. 359b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner if (!Tok.needsCleaning()) { 360b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Buffer = TokStart; 361b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return Tok.getLength(); 362b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 363c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 364b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner // Otherwise, hard case, relex the characters into the string. 365b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner char *OutBuf = const_cast<char*>(Buffer); 366b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); 367b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Ptr != End; ) { 368b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner unsigned CharSize; 369b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features); 370b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner Ptr += CharSize; 371b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner } 372b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner assert(unsigned(OutBuf-Buffer) != Tok.getLength() && 373b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner "NeedsCleaning flag set on something that didn't need cleaning!"); 374c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara 375b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner return OutBuf-Buffer; 376b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner} 377b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 378b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 379b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner 38033e9abd21083a0191a7676a04b497006d2da184dDouglas Gregorstatic bool isWhitespace(unsigned char c); 3815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3829a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// MeasureTokenLength - Relex the token at the specified location and return 3839a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// its length in bytes in the input file. If the token needs cleaning (e.g. 3849a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// includes a trigraph or an escaped newline) then this count includes bytes 3859a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// that are part of that. 3869a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattnerunsigned Lexer::MeasureTokenLength(SourceLocation Loc, 3872c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner const SourceManager &SM, 3882c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner const LangOptions &LangOpts) { 3899a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // TODO: this could be special cased for common tokens like identifiers, ')', 3909a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // etc to make this faster, if it mattered. Just look at StrData[0] to handle 3911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // all obviously single-char tokens. This could use 3929a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // Lexer::isObviouslySimpleCharacter for example to handle identifiers or 3939a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // something. 394de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 395de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // If this comes from a macro expansion, we really do want the macro name, not 396de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // the token this macro expanded to. 397402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth Loc = SM.getExpansionLoc(Loc); 398363fdc29656cc03c1817268888f95e6343470aa8Chris Lattner std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 399f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor bool Invalid = false; 4005f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); 401f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor if (Invalid) 402aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregor return 0; 403f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer 404f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer const char *StrData = Buffer.data()+LocInfo.second; 4058350394c65b81bba3986dfe44ae17423873741deChris Lattner 40633e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor if (isWhitespace(StrData[0])) 40733e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor return 0; 40833e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor 4099a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // Create a lexer starting at the beginning of this token. 410c3526d89ef9c31639ec8b25180cfb22354344241Sebastian Redl Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, 411c3526d89ef9c31639ec8b25180cfb22354344241Sebastian Redl Buffer.begin(), StrData, Buffer.end()); 41239de7409bffb6b725a8aa64f0ba77ab51e8c9eb3Chris Lattner TheLexer.SetCommentRetentionState(true); 4139a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner Token TheTok; 414590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner TheLexer.LexFromRawLexer(TheTok); 4159a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner return TheTok.getLength(); 4169a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner} 4179a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner 418a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas GregorSourceLocation Lexer::GetBeginningOfToken(SourceLocation Loc, 419a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor const SourceManager &SM, 420a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor const LangOptions &LangOpts) { 421a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 4223de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor if (LocInfo.first.isInvalid()) 4233de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor return Loc; 4243de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor 425a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor bool Invalid = false; 4265f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); 427a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (Invalid) 428a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return Loc; 429a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 430a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Back up from the current location until we hit the beginning of a line 431a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // (or the buffer). We'll relex from that point. 432a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor const char *BufStart = Buffer.data(); 4333de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor if (LocInfo.second >= Buffer.size()) 4343de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor return Loc; 4353de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor 436a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor const char *StrData = BufStart+LocInfo.second; 437a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (StrData[0] == '\n' || StrData[0] == '\r') 438a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return Loc; 439a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 440a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor const char *LexStart = StrData; 441a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor while (LexStart != BufStart) { 442a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (LexStart[0] == '\n' || LexStart[0] == '\r') { 443a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor ++LexStart; 444a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor break; 445a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } 446a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 447a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor --LexStart; 448a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } 449a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 450a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Create a lexer starting at the beginning of this token. 451a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor SourceLocation LexerStartLoc = Loc.getFileLocWithOffset(-LocInfo.second); 452a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor Lexer TheLexer(LexerStartLoc, LangOpts, BufStart, LexStart, Buffer.end()); 453a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor TheLexer.SetCommentRetentionState(true); 454a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 455a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Lex tokens until we find the token that contains the source location. 456a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor Token TheTok; 457a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor do { 458a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor TheLexer.LexFromRawLexer(TheTok); 459a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 460a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (TheLexer.getBufferLocation() > StrData) { 461a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // Lexing this token has taken the lexer past the source location we're 462a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // looking for. If the current token encompasses our source location, 463a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // return the beginning of that token. 464a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor if (TheLexer.getBufferLocation() - TheTok.getLength() <= StrData) 465a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return TheTok.getLocation(); 466a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 467a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // We ended up skipping over the source location entirely, which means 468a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // that it points into whitespace. We're done here. 469a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor break; 470a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } 471a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor } while (TheTok.getKind() != tok::eof); 472a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 473a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor // We've passed our source location; just return the original source location. 474a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor return Loc; 475a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor} 476a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor 477f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregornamespace { 478f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor enum PreambleDirectiveKind { 479f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_Skipped, 480f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_StartIf, 481f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_EndIf, 482f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PDK_Unknown 483f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor }; 484f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor} 485f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 486f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregorstd::pair<unsigned, bool> 487df95a13ec73d2cdaea79555cb412d767f4963120Douglas GregorLexer::ComputePreamble(const llvm::MemoryBuffer *Buffer, unsigned MaxLines) { 488f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // Create a lexer starting at the beginning of the file. Note that we use a 489f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // "fake" file source location at offset 1 so that the lexer will track our 490f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // position within the file. 491f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor const unsigned StartOffset = 1; 492f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor SourceLocation StartLoc = SourceLocation::getFromRawEncoding(StartOffset); 493f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor LangOptions LangOpts; 494f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Lexer TheLexer(StartLoc, LangOpts, Buffer->getBufferStart(), 495f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Buffer->getBufferStart(), Buffer->getBufferEnd()); 496f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 497f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor bool InPreprocessorDirective = false; 498f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Token TheTok; 499f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Token IfStartTok; 500f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor unsigned IfCount = 0; 501df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor unsigned Line = 0; 502df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor 503f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor do { 504f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor TheLexer.LexFromRawLexer(TheTok); 505f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 506f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (InPreprocessorDirective) { 507f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // If we've hit the end of the file, we're done. 508f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (TheTok.getKind() == tok::eof) { 509f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor InPreprocessorDirective = false; 510f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 511f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 512f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 513f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // If we haven't hit the end of the preprocessor directive, skip this 514f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // token. 515f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (!TheTok.isAtStartOfLine()) 516f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 517f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 518f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // We've passed the end of the preprocessor directive, and will look 519f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // at this token again below. 520f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor InPreprocessorDirective = false; 521f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 522f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 523df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // Keep track of the # of lines in the preamble. 524df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor if (TheTok.isAtStartOfLine()) { 525df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor ++Line; 526df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor 527df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // If we were asked to limit the number of lines in the preamble, 528df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // and we're about to exceed that limit, we're done. 529df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor if (MaxLines && Line >= MaxLines) 530df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor break; 531df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor } 532df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor 533f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // Comments are okay; skip over them. 534f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (TheTok.getKind() == tok::comment) 535f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 536f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 537f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) { 538f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // This is the start of a preprocessor directive. 539f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor Token HashTok = TheTok; 540f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor InPreprocessorDirective = true; 541f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 54219207f1e5f51261a33492602501fb7ada50ea546Joerg Sonnenberger // Figure out which directive this is. Since we're lexing raw tokens, 543f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // we don't have an identifier table available. Instead, just look at 544f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // the raw identifier to recognize and categorize preprocessor directives. 545f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor TheLexer.LexFromRawLexer(TheTok); 546c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara if (TheTok.getKind() == tok::raw_identifier && !TheTok.needsCleaning()) { 5475f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Keyword(TheTok.getRawIdentifierData(), 548c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara TheTok.getLength()); 549f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor PreambleDirectiveKind PDK 550f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor = llvm::StringSwitch<PreambleDirectiveKind>(Keyword) 551f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("include", PDK_Skipped) 552f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("__include_macros", PDK_Skipped) 553f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("define", PDK_Skipped) 554f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("undef", PDK_Skipped) 555f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("line", PDK_Skipped) 556f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("error", PDK_Skipped) 557f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("pragma", PDK_Skipped) 558f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("import", PDK_Skipped) 559f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("include_next", PDK_Skipped) 560f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("warning", PDK_Skipped) 561f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("ident", PDK_Skipped) 562f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("sccs", PDK_Skipped) 563f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("assert", PDK_Skipped) 564f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("unassert", PDK_Skipped) 565f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("if", PDK_StartIf) 566f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("ifdef", PDK_StartIf) 567f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("ifndef", PDK_StartIf) 568f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("elif", PDK_Skipped) 569f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("else", PDK_Skipped) 570f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Case("endif", PDK_EndIf) 571f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor .Default(PDK_Unknown); 572f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 573f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor switch (PDK) { 574f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_Skipped: 575f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 576f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 577f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_StartIf: 578f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (IfCount == 0) 579f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor IfStartTok = HashTok; 580f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 581f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor ++IfCount; 582f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 583f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 584f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_EndIf: 585f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // Mismatched #endif. The preamble ends here. 586f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor if (IfCount == 0) 587f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 588f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 589f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor --IfCount; 590f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor continue; 591f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 592f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor case PDK_Unknown: 593f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // We don't know what this directive is; stop at the '#'. 594f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 595f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 596f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 597f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 598f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // We only end up here if we didn't recognize the preprocessor 599f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // directive or it was one that can't occur in the preamble at this 600f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor // point. Roll back the current token to the location of the '#'. 601f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor InPreprocessorDirective = false; 602f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor TheTok = HashTok; 603f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } 604f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 605df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // We hit a token that we don't recognize as being in the 606df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // "preprocessing only" part of the file, so we're no longer in 607df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor // the preamble. 608f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor break; 609f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor } while (true); 610f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 611f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation(); 612f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(), 613f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor IfCount? IfStartTok.isAtStartOfLine() 614f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor : TheTok.isAtStartOfLine()); 615f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor} 616f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor 6177ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 6187ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// AdvanceToTokenCharacter - Given a location that specifies the start of a 6197ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token, return a new location that specifies a character within the token. 6207ef5c27eb6e8ebe58b52013246c06753c3613263Chris LattnerSourceLocation Lexer::AdvanceToTokenCharacter(SourceLocation TokStart, 6217ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner unsigned CharNo, 6227ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner const SourceManager &SM, 6237ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner const LangOptions &Features) { 624433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth // Figure out how many physical characters away the specified expansion 6257ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // character is. This needs to take into consideration newlines and 6267ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // trigraphs. 6277ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner bool Invalid = false; 6287ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner const char *TokPtr = SM.getCharacterData(TokStart, &Invalid); 6297ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 6307ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // If they request the first char of the token, we're trivially done. 6317ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (Invalid || (CharNo == 0 && Lexer::isObviouslySimpleCharacter(*TokPtr))) 6327ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner return TokStart; 6337ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 6347ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner unsigned PhysOffset = 0; 6357ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 6367ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // The usual case is that tokens don't contain anything interesting. Skip 6377ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // over the uninteresting characters. If a token only consists of simple 6387ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // chars, this method is extremely fast. 6397ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner while (Lexer::isObviouslySimpleCharacter(*TokPtr)) { 6407ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (CharNo == 0) 6417ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner return TokStart.getFileLocWithOffset(PhysOffset); 6427ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner ++TokPtr, --CharNo, ++PhysOffset; 6437ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner } 6447ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 6457ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // If we have a character that may be a trigraph or escaped newline, use a 6467ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // lexer to parse it correctly. 6477ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner for (; CharNo; --CharNo) { 6487ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner unsigned Size; 6497ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner Lexer::getCharAndSizeNoWarn(TokPtr, Size, Features); 6507ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner TokPtr += Size; 6517ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner PhysOffset += Size; 6527ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner } 6537ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 6547ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // Final detail: if we end up on an escaped newline, we want to return the 6557ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // location of the actual byte of the token. For example foo\<newline>bar 6567ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // advanced by 3 should return the location of b, not of \\. One compounding 6577ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner // detail of this is that the escape may be made by a trigraph. 6587ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (!Lexer::isObviouslySimpleCharacter(*TokPtr)) 6597ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner PhysOffset += Lexer::SkipEscapedNewLines(TokPtr)-TokPtr; 6607ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 6617ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner return TokStart.getFileLocWithOffset(PhysOffset); 6627ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner} 6637ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 6647ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// \brief Computes the source location just past the end of the 6657ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token at this source location. 6667ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// 6677ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// This routine can be used to produce a source location that 6687ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// points just past the end of the token referenced by \p Loc, and 6697ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// is generally used when a diagnostic needs to point just after a 6707ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token where it expected something different that it received. If 6717ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// the returned source location would not be meaningful (e.g., if 6727ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// it points into a macro), this routine returns an invalid 6737ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// source location. 6747ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// 6757ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// \param Offset an offset from the end of the token, where the source 6767ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// location should refer to. The default offset (0) produces a source 6777ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// location pointing just past the end of the token; an offset of 1 produces 6787ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// a source location pointing to the last character in the token, etc. 6797ef5c27eb6e8ebe58b52013246c06753c3613263Chris LattnerSourceLocation Lexer::getLocForEndOfToken(SourceLocation Loc, unsigned Offset, 6807ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner const SourceManager &SM, 6817ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner const LangOptions &Features) { 6827ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis if (Loc.isInvalid()) 6837ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner return SourceLocation(); 6847ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis 6857ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis if (Loc.isMacroID()) { 686433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth if (Offset > 0 || !isAtEndOfMacroExpansion(Loc, SM, Features)) 687433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth return SourceLocation(); // Points inside the macro expansion. 6887ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis 689433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth // Continue and find the location just after the macro expansion. 690edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth Loc = SM.getExpansionRange(Loc).second; 6917ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis } 6927ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis 6937ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner unsigned Len = Lexer::MeasureTokenLength(Loc, SM, Features); 6947ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner if (Len > Offset) 6957ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner Len = Len - Offset; 6967ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner else 6977ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner return Loc; 6987ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 69977ebb3829f2abb9be2d40927b793b72347626aadJohn McCall return Loc.getFileLocWithOffset(Len); 7007ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner} 7017ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner 7027a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis/// \brief Returns true if the given MacroID location points at the first 703433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// token of the macro expansion. 704433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruthbool Lexer::isAtStartOfMacroExpansion(SourceLocation loc, 705f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor const SourceManager &SM, 706f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor const LangOptions &LangOpts) { 7077a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); 7087a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 7097a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis std::pair<FileID, unsigned> infoLoc = SM.getDecomposedLoc(loc); 7107a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // FIXME: If the token comes from the macro token paste operator ('##') 7117a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // this function will always return false; 7127a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis if (infoLoc.second > 0) 7137a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis return false; // Does not point at the start of token. 7147a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 715433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation expansionLoc = 716433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SM.getSLocEntry(infoLoc.first) 717433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth .getInstantiation().getInstantiationLocStart(); 718433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth if (expansionLoc.isFileID()) 719433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth return true; // No other macro expansions, this is the first. 7207a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 721433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth return isAtStartOfMacroExpansion(expansionLoc, SM, LangOpts); 7227a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis} 7237a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 7247a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis/// \brief Returns true if the given MacroID location points at the last 725433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// token of the macro expansion. 726433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruthbool Lexer::isAtEndOfMacroExpansion(SourceLocation loc, 7277a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis const SourceManager &SM, 7287a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis const LangOptions &LangOpts) { 7297a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); 7307a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 7317a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis SourceLocation spellLoc = SM.getSpellingLoc(loc); 7327a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis unsigned tokLen = MeasureTokenLength(spellLoc, SM, LangOpts); 7337a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis if (tokLen == 0) 7347a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis return false; 7357a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 7367a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis FileID FID = SM.getFileID(loc); 7377a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis SourceLocation afterLoc = loc.getFileLocWithOffset(tokLen+1); 738f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor if (!SM.isBeforeInSourceLocationOffset(afterLoc, SM.getNextLocalOffset())) 7397a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis return true; // We got past the last FileID, this points to the last token. 7407a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 7417a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // FIXME: If the token comes from the macro token paste operator ('##') 7427a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis // or the stringify operator ('#') this function will always return false; 7437a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis if (FID == SM.getFileID(afterLoc)) 7447a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis return false; // Still in the same FileID, does not point to the last token. 7457a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 746433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth SourceLocation expansionLoc = 7477a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis SM.getSLocEntry(FID).getInstantiation().getInstantiationLocEnd(); 748433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth if (expansionLoc.isFileID()) 749433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth return true; // No other macro expansions. 7507a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 751433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth return isAtEndOfMacroExpansion(expansionLoc, SM, LangOpts); 7527a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis} 7537a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis 7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 7555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Character information. 7565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 7585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum { 7595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0' 7605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_VERT_WS = 0x02, // '\r', '\n' 7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_LETTER = 0x04, // a-z,A-Z 7625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_NUMBER = 0x08, // 0-9 7635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_UNDER = 0x10, // _ 7645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_PERIOD = 0x20 // . 7655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}; 7665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 76703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// Statically initialize CharInfo table based on ASCII character set 76803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// Reference: FreeBSD 7.2 /usr/share/misc/ascii 769a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic const unsigned char CharInfo[256] = 77003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner{ 77103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 0 NUL 1 SOH 2 STX 3 ETX 77203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 4 EOT 5 ENQ 6 ACK 7 BEL 77303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 77403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 77503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 8 BS 9 HT 10 NL 11 VT 77603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//12 NP 13 CR 14 SO 15 SI 77703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS, 77803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 , 77903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//16 DLE 17 DC1 18 DC2 19 DC3 78003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//20 DC4 21 NAK 22 SYN 23 ETB 78103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 78203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 78303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//24 CAN 25 EM 26 SUB 27 ESC 78403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//28 FS 29 GS 30 RS 31 US 78503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 78603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 78703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//32 SP 33 ! 34 " 35 # 78803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//36 $ 37 % 38 & 39 ' 78903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_HORZ_WS, 0 , 0 , 0 , 79003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 79103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//40 ( 41 ) 42 * 43 + 79203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//44 , 45 - 46 . 47 / 79303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 79403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , CHAR_PERIOD , 0 , 79503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//48 0 49 1 50 2 51 3 79603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//52 4 53 5 54 6 55 7 79703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , 79803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , 79903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//56 8 57 9 58 : 59 ; 80003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//60 < 61 = 62 > 63 ? 80103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_NUMBER , CHAR_NUMBER , 0 , 0 , 80203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 80303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//64 @ 65 A 66 B 67 C 80403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//68 D 69 E 70 F 71 G 80503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 80603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 80703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//72 H 73 I 74 J 75 K 80803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//76 L 77 M 78 N 79 O 80903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 81003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 81103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//80 P 81 Q 82 R 83 S 81203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//84 T 85 U 86 V 87 W 81303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 81403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 81503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//88 X 89 Y 90 Z 91 [ 81603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//92 \ 93 ] 94 ^ 95 _ 81703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 , 81803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , CHAR_UNDER , 81903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//96 ` 97 a 98 b 99 c 82003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//100 d 101 e 102 f 103 g 82103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 82203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 82303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//104 h 105 i 106 j 107 k 82403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//108 l 109 m 110 n 111 o 82503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 82603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 82703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//112 p 113 q 114 r 115 s 82803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//116 t 117 u 118 v 119 w 82903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 83003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 83103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//120 x 121 y 122 z 123 { 83203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//124 | 125 } 126 ~ 127 DEL 83303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 , 83403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 83503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner}; 83603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 837a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic void InitCharacterInfo() { 8385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer static bool isInited = false; 8395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isInited) return; 84003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner // check the statically-initialized CharInfo table 84103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_HORZ_WS == CharInfo[(int)' ']); 84203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_HORZ_WS == CharInfo[(int)'\t']); 84303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_HORZ_WS == CharInfo[(int)'\f']); 84403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_HORZ_WS == CharInfo[(int)'\v']); 84503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_VERT_WS == CharInfo[(int)'\n']); 84603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_VERT_WS == CharInfo[(int)'\r']); 84703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_UNDER == CharInfo[(int)'_']); 84803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_PERIOD == CharInfo[(int)'.']); 84903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner for (unsigned i = 'a'; i <= 'z'; ++i) { 85003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_LETTER == CharInfo[i]); 85103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_LETTER == CharInfo[i+'A'-'a']); 85203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner } 8535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = '0'; i <= '9'; ++i) 85403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_NUMBER == CharInfo[i]); 8557b682656310b0d7e94a98d982444bc7ddedc653fSteve Naroff 85603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner isInited = true; 8575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 8585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 85903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 8605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIdentifierBody - Return true if this is the body character of an 8615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// identifier, which is [a-zA-Z0-9_]. 8625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isIdentifierBody(unsigned char c) { 86395c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false; 8645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 8655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isHorizontalWhitespace - Return true if this character is horizontal 8675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'. 8685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isHorizontalWhitespace(unsigned char c) { 86995c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser return (CharInfo[c] & CHAR_HORZ_WS) ? true : false; 8705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 8715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isWhitespace - Return true if this character is horizontal or vertical 8735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false 8745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// for '\0'. 8755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isWhitespace(unsigned char c) { 87695c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false; 8775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 8785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNumberBody - Return true if this is the body character of an 8805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// preprocessing number, which is [a-zA-Z0-9_.]. 8815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isNumberBody(unsigned char c) { 8821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ? 88395c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser true : false; 8845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 8855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 8885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Diagnostics forwarding code. 8895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 8905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 891409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the 892433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// lexer buffer was all expanded at a single point, perform the mapping. 893409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// This is currently only used for _Pragma implementation, so it is the slow 894409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// path of the hot getSourceLocation method. Do not allow it to be inlined. 89514bd96571ef6f0e97dc79ec4d01b547d60e8fa68Chandler Carruthstatic LLVM_ATTRIBUTE_NOINLINE SourceLocation GetMappedTokenLoc( 89614bd96571ef6f0e97dc79ec4d01b547d60e8fa68Chandler Carruth Preprocessor &PP, SourceLocation FileLoc, unsigned CharNo, unsigned TokLen); 897409a03671224d4e5bdab1594c43baf070148f830Chris Lattnerstatic SourceLocation GetMappedTokenLoc(Preprocessor &PP, 898409a03671224d4e5bdab1594c43baf070148f830Chris Lattner SourceLocation FileLoc, 899de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned CharNo, unsigned TokLen) { 900433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth assert(FileLoc.isMacroID() && "Must be a macro expansion"); 9011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 902409a03671224d4e5bdab1594c43baf070148f830Chris Lattner // Otherwise, we're lexing "mapped tokens". This is used for things like 903433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth // _Pragma handling. Combine the expansion location of FileLoc with the 904df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner // spelling location. 905e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner SourceManager &SM = PP.getSourceManager(); 9061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 907433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth // Create a new SLoc which is expanded from Expansion(FileLoc) but whose 908df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner // characters come from spelling(FileLoc)+Offset. 909e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc); 910bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo); 9111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 912e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner // Figure out the expansion loc range, which is the range covered by the 913e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner // original _Pragma(...) sequence. 914e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner std::pair<SourceLocation,SourceLocation> II = 915999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth SM.getImmediateExpansionRange(FileLoc); 9161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 917bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth return SM.createExpansionLoc(SpellingLoc, II.first, II.second, TokLen); 918409a03671224d4e5bdab1594c43baf070148f830Chris Lattner} 919409a03671224d4e5bdab1594c43baf070148f830Chris Lattner 9205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSourceLocation - Return a source location identifier for the specified 9215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// offset in the current file. 922de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris LattnerSourceLocation Lexer::getSourceLocation(const char *Loc, 923de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned TokLen) const { 924448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner assert(Loc >= BufferStart && Loc <= BufferEnd && 9255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer "Location out of range for this buffer!"); 9269dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner 9279dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner // In the normal case, we're just lexing from a simple file buffer, return 9289dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner // the file id from FileLoc with the offset specified. 929448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner unsigned CharNo = Loc-BufferStart; 9309dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner if (FileLoc.isFileID()) 931bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner return FileLoc.getFileLocWithOffset(CharNo); 9321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9332b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner // Otherwise, this is the _Pragma lexer case, which pretends that all of the 9342b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner // tokens are lexed from where the _Pragma was defined. 935168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner assert(PP && "This doesn't work on raw lexers"); 936de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen); 9375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 9385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Diag - Forwarding function for diagnostics. This translate a source 9405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// position in the current buffer into a SourceLocation object for rendering. 9413cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const { 9423692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner return PP->Diag(getSourceLocation(Loc), DiagID); 9435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 9445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 9465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Trigraph and Escaped Newline Handling Code. 9475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 9485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair, 9505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the decoded trigraph letter it corresponds to, or '\0' if nothing. 9515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char GetTrigraphCharForLetter(char Letter) { 9525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Letter) { 9535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: return 0; 9545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '=': return '#'; 9555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ')': return ']'; 9565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '(': return '['; 9575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '!': return '|'; 9585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\'': return '^'; 9595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '>': return '}'; 9605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '/': return '\\'; 9615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '<': return '{'; 9625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '-': return '~'; 9635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 9655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// DecodeTrigraphChar - If the specified character is a legal trigraph when 9675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled, 9685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the result character. Finally, emit a warning about trigraph use 9695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whether trigraphs are enabled or not. 9705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char DecodeTrigraphChar(const char *CP, Lexer *L) { 9715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Res = GetTrigraphCharForLetter(*CP); 9723692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner if (!Res || !L) return Res; 9731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9743692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner if (!L->getFeatures().Trigraphs) { 97574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 97674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CP-2, diag::trigraph_ignored); 9773692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner return 0; 9785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 98074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 9815f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner L->Diag(CP-2, diag::trigraph_converted) << StringRef(&Res, 1); 9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return Res; 9835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 9845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 98524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// getEscapedNewLineSize - Return the size of the specified escaped newline, 98624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a 9871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// trigraph equivalent on entry to this function. 98824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattnerunsigned Lexer::getEscapedNewLineSize(const char *Ptr) { 98924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner unsigned Size = 0; 99024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner while (isWhitespace(Ptr[Size])) { 99124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner ++Size; 9921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 99324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r') 99424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner continue; 99524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 99624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // If this is a \r\n or \n\r, skip the other half. 99724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') && 99824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr[Size-1] != Ptr[Size]) 99924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner ++Size; 10001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 100124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return Size; 10021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 10031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 100424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Not an escaped newline, must be a \t or something else. 100524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return 0; 100624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner} 100724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 1008033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// SkipEscapedNewLines - If P points to an escaped newline (or a series of 1009033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// them), skip over them and return the first non-escaped-newline found, 1010033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// otherwise return P. 1011033749571f8d4c804eeb357c70b06424aa24503bChris Lattnerconst char *Lexer::SkipEscapedNewLines(const char *P) { 1012033749571f8d4c804eeb357c70b06424aa24503bChris Lattner while (1) { 1013033749571f8d4c804eeb357c70b06424aa24503bChris Lattner const char *AfterEscape; 1014033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (*P == '\\') { 1015033749571f8d4c804eeb357c70b06424aa24503bChris Lattner AfterEscape = P+1; 1016033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } else if (*P == '?') { 1017033749571f8d4c804eeb357c70b06424aa24503bChris Lattner // If not a trigraph for escape, bail out. 1018033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (P[1] != '?' || P[2] != '/') 1019033749571f8d4c804eeb357c70b06424aa24503bChris Lattner return P; 1020033749571f8d4c804eeb357c70b06424aa24503bChris Lattner AfterEscape = P+3; 1021033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } else { 1022033749571f8d4c804eeb357c70b06424aa24503bChris Lattner return P; 1023033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } 10241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1025033749571f8d4c804eeb357c70b06424aa24503bChris Lattner unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape); 1026033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (NewLineSize == 0) return P; 1027033749571f8d4c804eeb357c70b06424aa24503bChris Lattner P = AfterEscape+NewLineSize; 1028033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } 1029033749571f8d4c804eeb357c70b06424aa24503bChris Lattner} 1030033749571f8d4c804eeb357c70b06424aa24503bChris Lattner 103124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 10325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer, 10335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// get its size, and return it. This is tricky in several cases: 10345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 1. If currently at the start of a trigraph, we warn about the trigraph, 10355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// then either return the trigraph (skipping 3 chars) or the '?', 10365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// depending on whether trigraphs are enabled or not. 10375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 2. If this is an escaped newline (potentially with whitespace between 10385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the backslash and newline), implicitly skip the newline and return 10395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the char after it. 10405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 3. If this is a UCN, return it. FIXME: C++ UCN's? 10415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 10425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This handles the slow/uncommon case of the getCharAndSize method. Here we 10435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// know that we can accumulate into Size, and that we have already incremented 10445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Ptr by Size bytes. 10455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 10465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should 10475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match. 10485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 10495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size, 1050d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token *Tok) { 10515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, look for an escaped newline. 10525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '\\') { 10535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 10545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Ptr; 10555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash: 10565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Common case, backslash-char where the char is not whitespace. 10575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isWhitespace(Ptr[0])) return '\\'; 10581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10595636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner // See if we have optional whitespace characters between the slash and 10605636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner // newline. 106124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { 106224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Remember that this token needs to be cleaned. 106324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (Tok) Tok->setFlag(Token::NeedsCleaning); 106424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 106524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Warn if there was whitespace between the backslash and newline. 10665636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode()) 106724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Diag(Ptr, diag::backslash_newline_space); 10681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 106924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Found backslash<whitespace><newline>. Parse the char after it. 107024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Size += EscapedNewLineSize; 107124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr += EscapedNewLineSize; 107224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Use slow version to accumulate a correct size field. 107324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return getCharAndSizeSlow(Ptr, Size, Tok); 107424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner } 10751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is not an escaped newline, just return the slash. 10775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return '\\'; 10785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a trigraph, process it. 10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '?' && Ptr[1] == '?') { 10825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is actually a legal trigraph (not something like "??x"), emit 10835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // a trigraph warning. If so, and if trigraphs are enabled, return it. 10845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) { 10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Remember that this token needs to be cleaned. 1086d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner if (Tok) Tok->setFlag(Token::NeedsCleaning); 10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Ptr += 3; 10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Size += 3; 10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') goto Slash; 10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return C; 10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is neither, return a single character. 10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return *Ptr; 10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the 11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size, 11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// and that we have already incremented Ptr by Size bytes. 11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 11055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlow (above) should 11065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match. 11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size, 11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const LangOptions &Features) { 11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, look for an escaped newline. 11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '\\') { 11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Ptr; 11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash: 11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Common case, backslash-char where the char is not whitespace. 11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isWhitespace(Ptr[0])) return '\\'; 11161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // See if we have optional whitespace characters followed by a newline. 111824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { 111924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Found backslash<whitespace><newline>. Parse the char after it. 112024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Size += EscapedNewLineSize; 112124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr += EscapedNewLineSize; 11221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 112324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Use slow version to accumulate a correct size field. 112424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return getCharAndSizeSlowNoWarn(Ptr, Size, Features); 112524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner } 11261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is not an escaped newline, just return the slash. 11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return '\\'; 11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 11301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a trigraph, process it. 11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') { 11335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is actually a legal trigraph (not something like "??x"), return 11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it. 11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (char C = GetTrigraphCharForLetter(Ptr[2])) { 11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Ptr += 3; 11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Size += 3; 11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') goto Slash; 11395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return C; 11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 11421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is neither, return a single character. 11445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 11455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return *Ptr; 11465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 11495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper methods for lexing. 11505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1152f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor/// \brief Routine that indiscriminately skips bytes in the source file. 1153f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregorvoid Lexer::SkipBytes(unsigned Bytes, bool StartOfLine) { 1154f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor BufferPtr += Bytes; 1155f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor if (BufferPtr > BufferEnd) 1156f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor BufferPtr = BufferEnd; 1157f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor IsAtStartOfLine = StartOfLine; 1158f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor} 1159f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor 1160d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexIdentifier(Token &Result, const char *CurPtr) { 11615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$] 11625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned Size; 11635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned char C = *CurPtr++; 1164cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner while (isIdentifierBody(C)) 11655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 1166cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner 11675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; // Back up over the skipped character. 11685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Fast path, no $,\,? in identifier found. '\' might be an escaped newline 11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN. 11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: UCNs. 1172cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner // 1173cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner // TODO: Could merge these checks into a CharInfo flag to make the comparison 1174cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner // cheaper 11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) { 11765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerFinishIdentifier: 11775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *IdStart = BufferPtr; 1178c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara FormTokenWithChars(Result, CurPtr, tok::raw_identifier); 1179c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara Result.setRawIdentifierData(IdStart); 11801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are in raw mode, return this identifier raw. There is no need to 11825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // look up identifier information or attempt to macro expand it. 1183c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara if (LexingRawMode) 1184c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara return; 11851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1186c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // Fill in Result.IdentifierInfo and update the token kind, 1187c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara // looking up the identifier in the identifier table. 1188c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara IdentifierInfo *II = PP->LookUpIdentifierInfo(Result); 11891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Finally, now that we know we have an identifier, pass this off to the 11915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // preprocessor, which may macro expand it or something. 1192d1186fa38166a581b51975f0382a45fc3a0733d0Chris Lattner if (II->isHandleIdentifierCase()) 11936a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner PP->HandleIdentifier(Result); 11946a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner return; 11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 11961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, $,\,? in identifier found. Enter slower path. 11981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 12005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 12015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '$') { 12025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we hit a $ and they are not supported in identifiers, we are done. 12035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!Features.DollarIdents) goto FinishIdentifier; 12041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, emit a diagnostic and continue. 120674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 120774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr, diag::ext_dollar_in_identifier); 12085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 12095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 12105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer continue; 12115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (!isIdentifierBody(C)) { // FIXME: UCNs. 12125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Found end of identifier. 12135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto FinishIdentifier; 12145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this character is good, consume it. 12175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 12185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 12205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isIdentifierBody(C)) { // FIXME: UCNs. 12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 12265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1227a75ec43d625753b4439b0d6f70bd988444c74617Douglas Gregor/// isHexaLiteral - Return true if Start points to a hex constant. 12284a551000bee716ac8b1bbe16134a53f0ad221a5aChris Lattner/// in microsoft mode (where this is supposed to be several different tokens). 12296ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattnerstatic bool isHexaLiteral(const char *Start, const LangOptions &Features) { 12306ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner unsigned Size; 12316ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner char C1 = Lexer::getCharAndSizeNoWarn(Start, Size, Features); 12326ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner if (C1 != '0') 12336ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner return false; 12346ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner char C2 = Lexer::getCharAndSizeNoWarn(Start + Size, Size, Features); 12356ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner return (C2 == 'x' || C2 == 'X'); 1236a75ec43d625753b4439b0d6f70bd988444c74617Douglas Gregor} 12375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12385253c7ff266ae79308050c9f43d60dd1a67c5fb9Nate Begeman/// LexNumericConstant - Lex the remainder of a integer or floating point 12395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. From[-1] is the first character lexed. Return the end of the 12405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. 1241d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexNumericConstant(Token &Result, const char *CurPtr) { 12425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned Size; 12435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getCharAndSize(CurPtr, Size); 12445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char PrevCh = 0; 12455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isNumberBody(C)) { // FIXME: UCNs? 12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 12475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer PrevCh = C; 12485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 12495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we fell out, check for a sign, due to 1e+12. If we have one, continue. 1252b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) { 1253b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner // If we are in Microsoft mode, don't continue if the constant is hex. 1254b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner // For example, MSVC will accept the following as 3 tokens: 0x1234567e+1 12556ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner if (!Features.Microsoft || !isHexaLiteral(BufferPtr, Features)) 1256b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); 1257b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner } 12585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a hex FP constant, continue. 12608c723404c34e9347fae473489194e8c200c4ac83Sean Hunt if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') && 1261b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner !Features.CPlusPlus0x) 12625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); 12631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 126547246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 12669e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::numeric_constant); 126747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 12685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 12695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexStringLiteral - Lex the remainder of a string literal, after having lexed 12715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// either " or L". 1272d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnervoid Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) { 12735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this string contain the \0 character? 12741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 12765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '"') { 1277571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner // Skip escaped characters. Escaped newlines will already be processed by 1278571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner // getAndAdvanceChar. 1279571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner if (C == '\\') 12805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 128133611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor 1282571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner if (C == '\n' || C == '\r' || // Newline. 128333611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. 128455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor if (C == 0 && PP && PP->isCodeCompletionFile(FileLoc)) 128555817afdf9d453a443262a733f6caf6692dca118Douglas Gregor PP->CodeCompleteNaturalLanguage(); 128655817afdf9d453a443262a733f6caf6692dca118Douglas Gregor else if (!isLexingRawMode() && !Features.AsmPreprocessor) 1287ff1ed985ede24b9a45e52d726db95767e35fe21eArgyrios Kyrtzidis Diag(BufferPtr, diag::warn_unterminated_string); 12889e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr-1, tok::unknown); 12895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 12905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1291571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner 1292571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner if (C == 0) 1293571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner NulCharacter = CurPtr-1; 12945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 12955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If a nul character existed in the string, warn about it. 129874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 129974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_string); 13005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of the token as well as the BufferPtr instance var. 130247246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 13036cf750298d3621d8a10a6dd07fcee8e274b9d94dSean Hunt FormTokenWithChars(Result, CurPtr, 13046cf750298d3621d8a10a6dd07fcee8e274b9d94dSean Hunt Wide ? tok::wide_string_literal : tok::string_literal); 130547246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 13065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 13075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexAngledStringLiteral - Lex the remainder of an angled string literal, 13095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// after having lexed the '<' character. This is used for #include filenames. 1310d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) { 13115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this string contain the \0 character? 13129cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner const char *AfterLessPos = CurPtr; 13135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 13145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '>') { 13155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip escaped characters. 13165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') { 13175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip the escaped character. 13185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 13195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == '\n' || C == '\r' || // Newline. 13205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. 13219cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner // If the filename is unterminated, then it must just be a lone < 13229cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner // character. Return this as such. 13239cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner FormTokenWithChars(Result, AfterLessPos, tok::less); 13245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 13255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == 0) { 13265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer NulCharacter = CurPtr-1; 13275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 13285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 13295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 13301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If a nul character existed in the string, warn about it. 133274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 133374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_string); 13341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 133647246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 13379e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::angle_string_literal); 133847246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 13405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexCharConstant - Lex the remainder of a character constant, after having 13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexed either ' or L'. 1344d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexCharConstant(Token &Result, const char *CurPtr) { 13455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this character contain the \0 character? 13465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 13485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\'') { 134933ab3f6c7e3065550de202088f8f335ecfa16ae1Chris Lattner if (!isLexingRawMode() && !Features.AsmPreprocessor) 135074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(BufferPtr, diag::err_empty_character); 13519e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 13525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 13535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 13541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1355d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner while (C != '\'') { 1356d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner // Skip escaped characters. 1357d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner if (C == '\\') { 1358d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner // Skip the escaped character. 1359d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner // FIXME: UCN's 13605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 1361d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner } else if (C == '\n' || C == '\r' || // Newline. 1362d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. 136355817afdf9d453a443262a733f6caf6692dca118Douglas Gregor if (C == 0 && PP && PP->isCodeCompletionFile(FileLoc)) 136455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor PP->CodeCompleteNaturalLanguage(); 136555817afdf9d453a443262a733f6caf6692dca118Douglas Gregor else if (!isLexingRawMode() && !Features.AsmPreprocessor) 1366ff1ed985ede24b9a45e52d726db95767e35fe21eArgyrios Kyrtzidis Diag(BufferPtr, diag::warn_unterminated_char); 1367d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner FormTokenWithChars(Result, CurPtr-1, tok::unknown); 1368d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner return; 1369d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner } else if (C == 0) { 1370d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner NulCharacter = CurPtr-1; 1371d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner } 1372d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner C = getAndAdvanceChar(CurPtr, Result); 13735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 13741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1375d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner // If a nul character existed in the character, warn about it. 137674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 137774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_char); 13785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 138047246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 13819e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::char_constant); 138247246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 13835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 13845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipWhitespace - Efficiently skip over a series of whitespace characters. 13865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Update BufferPtr to point to the next non-whitespace character and return. 1387d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// 1388d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// This method forms a token and returns true if KeepWhitespaceMode is enabled. 1389d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// 1390d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnerbool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) { 13915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Whitespace - Skip it, then return the token after the whitespace. 13925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently. 13935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 13945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip horizontal whitespace very aggressively. 13955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isHorizontalWhitespace(Char)) 13965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = *++CurPtr; 13971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1398ddd3e8b90a92c39f620bda7a0945320c0b9e60dbDaniel Dunbar // Otherwise if we have something other than whitespace, we're done. 13995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char != '\n' && Char != '\r') 14005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 14011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 14035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // End of preprocessor directive line, let LexTokenInternal handle this. 14045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 1405d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return false; 14065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // ok, but handle newline. 14095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The returned token is at the start of the line. 1410d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 14115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // No leading whitespace seen so far. 1412d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::LeadingSpace); 14135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = *++CurPtr; 14145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this isn't immediately after a newline, there is leading space. 14175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char PrevChar = CurPtr[-1]; 14185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (PrevChar != '\n' && PrevChar != '\r') 1419d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 14205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1421d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // If the client wants us to return whitespace, return it now. 1422d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (isKeepWhitespaceMode()) { 14239e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 1424d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return true; 1425d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner } 14261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 1428d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return false; 14295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 14305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// SkipBCPLComment - We have just read the // characters from input. Skip until 14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// we find the newline character thats terminate the comment. Then update 1433046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// BufferPtr and return. 1434046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// 1435046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted 1436046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true. 1437d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) { 14385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If BCPL comments aren't explicitly enabled for this language, emit an 14395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // extension warning. 144074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!Features.BCPLComment && !isLexingRawMode()) { 14415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Diag(BufferPtr, diag::ext_bcpl_comment); 14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Mark them enabled so we only emit one warning for this translation 14445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // unit. 14455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Features.BCPLComment = true; 14465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan over the body of the comment. The common case, when scanning, is that 14495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the comment contains normal ascii characters with nothing interesting in 14505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // them. As such, optimize for this case with the inner loop. 14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C; 14525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer do { 14535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr; 14545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character. 14555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we find a \n character, scan backwards, checking to see if it's an 14565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // escaped newline, like we do for block comments. 14571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip over characters in the fast loop. 14595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != 0 && // Potentially EOF. 14605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C != '\\' && // Potentially escaped newline. 14615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C != '?' && // Potentially trigraph. 14625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C != '\n' && C != '\r') // Newline or DOS-style newline. 14635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *++CurPtr; 14645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a newline, we're done. 14665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\n' || C == '\r') 14675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; // Found the newline? Break out! 14681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to 1470bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // properly decode the character. Read it in raw mode to avoid emitting 1471bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // diagnostics about things like trigraphs. If we see an escaped newline, 1472bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // we'll handle it below. 14735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *OldPtr = CurPtr; 1474bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner bool OldRawMode = isLexingRawMode(); 1475bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner LexingRawMode = true; 14765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 1477bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner LexingRawMode = OldRawMode; 1478ead616c5d8681a41b11273327813e61bda01907aChris Lattner 1479ead616c5d8681a41b11273327813e61bda01907aChris Lattner // If the char that we finally got was a \n, then we must have had something 1480ead616c5d8681a41b11273327813e61bda01907aChris Lattner // like \<newline><newline>. We don't want to have consumed the second 1481ead616c5d8681a41b11273327813e61bda01907aChris Lattner // newline, we want CurPtr, to end up pointing to it down below. 1482ead616c5d8681a41b11273327813e61bda01907aChris Lattner if (C == '\n' || C == '\r') { 1483ead616c5d8681a41b11273327813e61bda01907aChris Lattner --CurPtr; 1484ead616c5d8681a41b11273327813e61bda01907aChris Lattner C = 'x'; // doesn't matter what this is. 1485ead616c5d8681a41b11273327813e61bda01907aChris Lattner } 14861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we read multiple characters, and one of those characters was a \r or 14885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n, then we had an escaped newline within the comment. Emit diagnostic 14895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // unless the next line is also a // comment. 14905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') { 14915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (; OldPtr != CurPtr; ++OldPtr) 14925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (OldPtr[0] == '\n' || OldPtr[0] == '\r') { 14935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Okay, we found a // comment that ends in a newline, if the next 14945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // line is also a // comment, but has spaces, don't emit a diagnostic. 14955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isspace(C)) { 14965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *ForwardPtr = CurPtr; 14975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isspace(*ForwardPtr)) // Skip whitespace. 14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++ForwardPtr; 14995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/') 15005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 15015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 150374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 150474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment); 15055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 15065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 150955817afdf9d453a443262a733f6caf6692dca118Douglas Gregor if (CurPtr == BufferEnd+1) { 151055817afdf9d453a443262a733f6caf6692dca118Douglas Gregor if (PP && PP->isCodeCompletionFile(FileLoc)) 151155817afdf9d453a443262a733f6caf6692dca118Douglas Gregor PP->CodeCompleteNaturalLanguage(); 151255817afdf9d453a443262a733f6caf6692dca118Douglas Gregor 151355817afdf9d453a443262a733f6caf6692dca118Douglas Gregor --CurPtr; 151455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor break; 151555817afdf9d453a443262a733f6caf6692dca118Douglas Gregor } 15165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } while (C != '\n' && C != '\r'); 15175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15183d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner // Found but did not consume the newline. Notify comment handlers about the 15193d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner // comment unless we're in a #if 0 block. 15203d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner if (PP && !isLexingRawMode() && 15213d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr), 15223d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner getSourceLocation(CurPtr)))) { 1523046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner BufferPtr = CurPtr; 1524046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return true; // A token has to be returned. 1525046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner } 15261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are returning comments as tokens, return this comment as a token. 1528fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner if (inKeepCommentMode()) 15295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return SaveBCPLComment(Result, CurPtr); 15305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are inside a preprocessor directive and we see the end of line, 153284021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // return immediately, so that the lexer can return this as an EOD token. 15335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective || CurPtr == BufferEnd) { 15345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 15352d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 15365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, eat the \n character. We don't care if this is a \n\r or 15397a4f004a77421af876152281b92c4f8ea24afb2dChris Lattner // \r\n sequence. This is an efficiency hack (because we know the \n can't 1540d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // contribute to another token), it isn't needed for correctness. Note that 1541d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // this is ok even in KeepWhitespaceMode, because we would have returned the 1542d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner /// comment above in that mode. 15435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 15441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The next returned token is at the start of the line. 1546d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 15475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // No leading whitespace seen so far. 1548d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::LeadingSpace); 15495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 15502d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 15515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 15525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in 15545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// an appropriate way and return it. 1555d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) { 15569e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // If we're not in a preprocessor directive, just return the // comment 15579e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // directly. 15589e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::comment); 15591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15609e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner if (!ParsingPreprocessorDirective) 15619e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner return true; 15621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15639e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // If this BCPL-style comment is in a macro definition, transmogrify it into 15649e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // a C-style block comment. 1565453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor bool Invalid = false; 1566453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor std::string Spelling = PP->getSpelling(Result, &Invalid); 1567453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor if (Invalid) 1568453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor return true; 1569453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor 15709e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?"); 15719e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Spelling[1] = '*'; // Change prefix to "/*". 15729e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Spelling += "*/"; // add suffix. 15731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15749e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Result.setKind(tok::comment); 157547246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner PP->CreateString(&Spelling[0], Spelling.size(), Result, 157647246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.getLocation()); 15772d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return true; 15785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 15795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline 15815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// character (either \n or \r) is part of an escaped newline sequence. Issue a 158247a2b40b7a3d9c89d6c3d12e5a81e2498c207750Chris Lattner/// diagnostic if so. We know that the newline is inside of a block comment. 15831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr, 15845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Lexer *L) { 15855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(CurPtr[0] == '\n' || CurPtr[0] == '\r'); 15861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Back up off the newline. 15885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 15891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a two-character newline sequence, skip the other character. 15915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == '\n' || CurPtr[0] == '\r') { 15925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n\n or \r\r -> not escaped newline. 15935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == CurPtr[1]) 15945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 15955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n\r or \r\n -> skip the newline. 15965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 15975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 15981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have horizontal whitespace, skip over it. We allow whitespace 16005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // between the slash and newline. 16015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool HasSpace = false; 16025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) { 16035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 16045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer HasSpace = true; 16055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, we know this is an escaped newline. 16085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (*CurPtr == '\\') { 16095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[-1] != '*') return false; 16105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 16115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It isn't a slash, is it the ?? / trigraph? 16125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' || 16135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[-3] != '*') 16145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 16151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // This is the trigraph ending the comment. Emit a stern warning! 16175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr -= 2; 16185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If no trigraphs are enabled, warn that we ignored this trigraph and 16205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // ignore this * character. 16215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!L->getFeatures().Trigraphs) { 162274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 162374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::trigraph_ignored_block_comment); 16245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 16255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 162674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 162774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::trigraph_ends_block_comment); 16285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Warn about having an escaped newline between the */ characters. 163174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 163274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::escaped_newline_block_comment_end); 16331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If there was space between the backslash and newline, warn about it. 163574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (HasSpace && !L->isLexingRawMode()) 163674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::backslash_newline_space); 16371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 16395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 16405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__ 16425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <emmintrin.h> 16435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__ 16445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <altivec.h> 16455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#undef bool 16465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif 16475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipBlockComment - We have just read the /* characters from input. Read 16495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// until we find the */ characters that terminate the comment. Note that we 16505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// don't bother decoding trigraphs or escaped newlines in block comments, 16515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// because they cannot cause the comment to end. The only thing that can 16525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// happen is the comment could end with an escaped newline between the */ end 16535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// of comment. 16542d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner/// 1655046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted 1656046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true. 1657d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) { 16585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan one character past where we should, looking for a '/' character. Once 1659fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner // we find it, check to see if it was preceded by a *. This common 16605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // optimization helps people who like to put a lot of * characters in their 16615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // comments. 16628146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner 16638146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // The first character we get with newlines and trigraphs skipped to handle 16648146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // the degenerate /*/ case below correctly if the * has an escaped newline 16658146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // after it. 16668146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner unsigned CharSize; 16678146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner unsigned char C = getCharAndSize(CurPtr, CharSize); 16688146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner CurPtr += CharSize; 16695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == 0 && CurPtr == BufferEnd+1) { 1670150fcd5dcb79ad0a9cc6824c5589000e3669c7a4Chris Lattner if (!isLexingRawMode() && 1671150fcd5dcb79ad0a9cc6824c5589000e3669c7a4Chris Lattner !PP->isCodeCompletionFile(FileLoc)) 16720af574270d3be2b0e73a3379dfaa633746f8fc6fChris Lattner Diag(BufferPtr, diag::err_unterminated_block_comment); 167331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner --CurPtr; 16741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 167531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // KeepWhitespaceMode should return this broken comment as a token. Since 167631f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // it isn't a well formed comment, just return it as an 'unknown' token. 167731f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner if (isKeepWhitespaceMode()) { 16789e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 167931f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner return true; 168031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner } 16811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 168231f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner BufferPtr = CurPtr; 16832d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 16845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16868146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // Check to see if the first character after the '/*' is another /. If so, 16878146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // then this slash does not end the block comment, it is part of it. 16888146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner if (C == '/') 16898146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner C = *CurPtr++; 16901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 16925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip over all non-interesting characters until we find end of buffer or a 16935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // (probably ending) '/' character. 16945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr + 24 < BufferEnd) { 16955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // While not aligned to a 16-byte boundary. 16965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0) 16975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 16981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '/') goto FoundSlash; 17005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 17015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__ 17025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/', 17035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer '/', '/', '/', '/', '/', '/', '/', '/'); 17045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (CurPtr+16 <= BufferEnd && 17055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0) 17065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 16; 17075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__ 17085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer __vector unsigned char Slashes = { 17091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump '/', '/', '/', '/', '/', '/', '/', '/', 17105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer '/', '/', '/', '/', '/', '/', '/', '/' 17115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer }; 17125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (CurPtr+16 <= BufferEnd && 17135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes)) 17145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 16; 17151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#else 17165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan for '/' quickly. Many block comments are very large. 17175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (CurPtr[0] != '/' && 17185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[1] != '/' && 17195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[2] != '/' && 17205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[3] != '/' && 17215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr+4 < BufferEnd) { 17225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 4; 17235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif 17251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It has to be one of the bytes scanned, increment to it and read one. 17275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 17285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Loop to scan the remainder. 17315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '/' && C != '\0') 17325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 17331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer FoundSlash: 17355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '/') { 17365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[-2] == '*') // We found the final */. We're done! 17375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 17381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) { 17405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) { 17415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We found the final */, though it had an escaped newline between the 17425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // * and /. We're done! 17435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 17445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == '*' && CurPtr[1] != '/') { 17475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a /* inside of the comment, emit a warning. Don't do this 17485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // if this is a /*/, which will end the comment. This misses cases with 17495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // embedded escaped newlines, but oh well. 175074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 175174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::warn_nested_block_comment); 17525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == 0 && CurPtr == BufferEnd+1) { 175455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor if (PP && PP->isCodeCompletionFile(FileLoc)) 175555817afdf9d453a443262a733f6caf6692dca118Douglas Gregor PP->CodeCompleteNaturalLanguage(); 175655817afdf9d453a443262a733f6caf6692dca118Douglas Gregor else if (!isLexingRawMode()) 175774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(BufferPtr, diag::err_unterminated_block_comment); 17585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Note: the user probably forgot a */. We could continue immediately 17595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // after the /*, but this would involve lexing a lot of what really is the 17605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // comment, which surely would confuse the parser. 176131f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner --CurPtr; 17621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 176331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // KeepWhitespaceMode should return this broken comment as a token. Since 176431f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // it isn't a well formed comment, just return it as an 'unknown' token. 176531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner if (isKeepWhitespaceMode()) { 17669e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 176731f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner return true; 176831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner } 17691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 177031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner BufferPtr = CurPtr; 17712d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 17725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 17745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17763d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner // Notify comment handlers about the comment unless we're in a #if 0 block. 17773d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner if (PP && !isLexingRawMode() && 17783d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr), 17793d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner getSourceLocation(CurPtr)))) { 1780046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner BufferPtr = CurPtr; 1781046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return true; // A token has to be returned. 1782046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner } 17832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 17845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are returning comments as tokens, return this comment as a token. 1785fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner if (inKeepCommentMode()) { 17869e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::comment); 17872d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return true; 17885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 17905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It is common for the tokens immediately after a /**/ comment to be 17915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // whitespace. Instead of going through the big switch, handle it 1792d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // efficiently now. This is safe even in KeepWhitespaceMode because we would 1793d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // have already returned above with the comment as a token. 17945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isHorizontalWhitespace(*CurPtr)) { 1795d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 17965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SkipWhitespace(Result, CurPtr+1); 17972d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 17985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, just return so that the next character will be lexed as a token. 18015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 1802d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 18032d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 18045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 18055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 18075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Lexing Entry Points 18085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 18095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ReadToEndOfLine - Read the rest of the current preprocessor line as an 18115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// uninterpreted string. This switches the lexer out of directive mode. 18125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstd::string Lexer::ReadToEndOfLine() { 18135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(ParsingPreprocessorDirective && ParsingFilename == false && 18145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer "Must be in a preprocessing directive!"); 18155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer std::string Result; 1816d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token Tmp; 18175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // CurPtr - Cache BufferPtr in an automatic variable. 18195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *CurPtr = BufferPtr; 18205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 18215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Char = getAndAdvanceChar(CurPtr, Tmp); 18225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Char) { 18235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 18245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result += Char; 18255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 18265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 0: // Null. 18275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Found end of file? 18285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr-1 != BufferEnd) { 18295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Nope, normal character, continue. 18305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result += Char; 18315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 18325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FALL THROUGH. 18345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\r': 18355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\n': 18365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Okay, we found the end of the line. First, back up past the \0, \r, \n. 18375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(CurPtr[-1] == Char && "Trigraphs for newline?"); 18385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr-1; 18391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 184084021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // Next, lex the character, which should handle the EOD transition. 18415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Lex(Tmp); 184255817afdf9d453a443262a733f6caf6692dca118Douglas Gregor if (Tmp.is(tok::code_completion)) { 184355817afdf9d453a443262a733f6caf6692dca118Douglas Gregor if (PP && PP->getCodeCompletionHandler()) 184455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor PP->getCodeCompletionHandler()->CodeCompleteNaturalLanguage(); 184555817afdf9d453a443262a733f6caf6692dca118Douglas Gregor Lex(Tmp); 184655817afdf9d453a443262a733f6caf6692dca118Douglas Gregor } 184784021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne assert(Tmp.is(tok::eod) && "Unexpected token!"); 18481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Finally, we're done, return the string we found. 18505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return Result; 18515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 18545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexEndOfFile - CurPtr points to the end of this file. Handle this 18565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// condition, reporting diagnostics and handling other edge cases as required. 18575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This returns true if Result contains a token, false if PP.Lex should be 18585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// called again. 1859d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) { 1860f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor // Check if we are performing code completion. 1861f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor if (PP && PP->isCodeCompletionFile(FileLoc)) { 1862f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor // We're at the end of the file, but we've been asked to consider the 1863f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor // end of the file to be a code-completion token. Return the 1864f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor // code-completion token. 1865f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor Result.startToken(); 1866f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor FormTokenWithChars(Result, CurPtr, tok::code_completion); 1867f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor 1868f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor // Only do the eof -> code_completion translation once. 1869f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor PP->SetCodeCompletionPoint(0, 0, 0); 1870f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor 1871f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor // Silence any diagnostics that occur once we hit the code-completion point. 1872f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor PP->getDiagnostics().setSuppressAllDiagnostics(true); 1873f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor return true; 1874f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor } 1875f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor 18765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we hit the end of the file while parsing a preprocessor directive, 18775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // end the preprocessor directive first. The next token returned will 18785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // then be the end of file. 18795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 18805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Done parsing the "line". 18815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParsingPreprocessorDirective = false; 18825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 188384021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne FormTokenWithChars(Result, CurPtr, tok::eod); 18841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore comment saving mode, in case it was disabled for directive. 1886f744d133e8d36eec65828d046ee695bda238125cChris Lattner SetCommentRetentionState(PP->getCommentRetentionState()); 18875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; // Have a token. 18881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 188986d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor 189086d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor // If we are in raw mode, return this event as an EOF token. Let the caller 189186d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor // that put us in raw mode handle the event. 189286d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor if (isLexingRawMode()) { 189386d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor Result.startToken(); 189486d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor BufferPtr = BufferEnd; 189586d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor FormTokenWithChars(Result, BufferEnd, tok::eof); 189686d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor return true; 189786d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor } 189881b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor 1899f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor // Issue diagnostics for unterminated #if and missing newline. 1900f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor 19015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are in a #if directive, emit an error. 19025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (!ConditionalStack.empty()) { 19032d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor if (!PP->isCodeCompletionFile(FileLoc)) 19042d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor PP->Diag(ConditionalStack.back().IfLoc, 19052d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor diag::err_pp_unterminated_conditional); 19065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConditionalStack.pop_back(); 19075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 19081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1909b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue 1910b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner // a pedwarn. 1911b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) 191220d0ee5d23c0f0e879ce1711d11df3118b3fb033Mike Stump Diag(BufferEnd, diag::ext_no_newline_eof) 1913849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n"); 19141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 19165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 19175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Finally, let the preprocessor handle this. 1918168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner return PP->HandleEndOfFile(Result); 19195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 19205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 19215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from 19225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified lexer will return a tok::l_paren token, 0 if it is something 19235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// else and 2 if there are no more tokens in the buffer controlled by the 19245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexer. 19255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerunsigned Lexer::isNextPPTokenLParen() { 19265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?"); 19271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Switch to 'skipping' mode. This will ensure that we can lex a token 19295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // without emitting diagnostics, disables macro expansion, and will cause EOF 19305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // to return an EOF token instead of popping the include stack. 19315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexingRawMode = true; 19321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Save state that can be changed while lexing so that we can restore it. 19345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *TmpBufferPtr = BufferPtr; 1935a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner bool inPPDirectiveMode = ParsingPreprocessorDirective; 19361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1937d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token Tok; 19385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Tok.startToken(); 19395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexTokenInternal(Tok); 19401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore state that may have changed. 19425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = TmpBufferPtr; 1943a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner ParsingPreprocessorDirective = inPPDirectiveMode; 19441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore the lexer back to non-skipping mode. 19465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexingRawMode = false; 19471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 194822f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner if (Tok.is(tok::eof)) 19495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return 2; 195022f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner return Tok.is(tok::l_paren); 19515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 19525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 195334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// FindConflictEnd - Find the end of a version control conflict marker. 195434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerstatic const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) { 19555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7); 195634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner size_t Pos = RestOfBuffer.find(">>>>>>>"); 19575f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner while (Pos != StringRef::npos) { 195834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Must occur at start of line. 195934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (RestOfBuffer[Pos-1] != '\r' && 196034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner RestOfBuffer[Pos-1] != '\n') { 196134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner RestOfBuffer = RestOfBuffer.substr(Pos+7); 19623d488990e13835cb35538b2f0cd53dbda92e36beChris Lattner Pos = RestOfBuffer.find(">>>>>>>"); 196334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner continue; 196434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 196534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return RestOfBuffer.data()+Pos; 196634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 196734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return 0; 196834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner} 196934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 197034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// IsStartOfConflictMarker - If the specified pointer is the start of a version 197134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// control conflict marker like '<<<<<<<', recognize it as such, emit an error 197234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// and recover nicely. This returns true if it is a conflict marker and false 197334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// if not. 197434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::IsStartOfConflictMarker(const char *CurPtr) { 197534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Only a conflict marker if it starts at the beginning of a line. 197634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr != BufferStart && 197734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr[-1] != '\n' && CurPtr[-1] != '\r') 197834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 197934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 198034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Check to see if we have <<<<<<<. 198134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (BufferEnd-CurPtr < 8 || 19825f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef(CurPtr, 7) != "<<<<<<<") 198334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 198434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 198534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If we have a situation where we don't care about conflict markers, ignore 198634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // it. 198734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (IsInConflictMarker || isLexingRawMode()) 198834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 198934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 199034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Check to see if there is a >>>>>>> somewhere in the buffer at the start of 199134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // a line to terminate this conflict marker. 19923d488990e13835cb35538b2f0cd53dbda92e36beChris Lattner if (FindConflictEnd(CurPtr, BufferEnd)) { 199334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // We found a match. We are really in a conflict marker. 199434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Diagnose this, and ignore to the end of line. 199534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Diag(CurPtr, diag::err_conflict_marker); 199634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner IsInConflictMarker = true; 199734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 199834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Skip ahead to the end of line. We know this exists because the 199934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // end-of-conflict marker starts with \r or \n. 200034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner while (*CurPtr != '\r' && *CurPtr != '\n') { 200134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner assert(CurPtr != BufferEnd && "Didn't find end of line"); 200234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner ++CurPtr; 200334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 200434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner BufferPtr = CurPtr; 200534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return true; 200634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 200734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 200834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // No end of conflict marker found. 200934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 201034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner} 201134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 201234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 201334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>' 201434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// marker, then it is the end of a conflict marker. Handle it by ignoring up 201534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// until the end of the line. This returns true if it is a conflict marker and 201634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// false if not. 201734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::HandleEndOfConflictMarker(const char *CurPtr) { 201834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Only a conflict marker if it starts at the beginning of a line. 201934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr != BufferStart && 202034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr[-1] != '\n' && CurPtr[-1] != '\r') 202134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 202234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 202334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If we have a situation where we don't care about conflict markers, ignore 202434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // it. 202534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (!IsInConflictMarker || isLexingRawMode()) 202634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 202734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 202834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Check to see if we have the marker (7 characters in a row). 202934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner for (unsigned i = 1; i != 7; ++i) 203034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr[i] != CurPtr[0]) 203134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 203234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 203334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If we do have it, search for the end of the conflict marker. This could 203434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // fail if it got skipped with a '#if 0' or something. Note that CurPtr might 203534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // be the end of conflict marker. 203634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) { 203734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = End; 203834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 203934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // Skip ahead to the end of line. 204034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n') 204134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner ++CurPtr; 204234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 204334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner BufferPtr = CurPtr; 204434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 204534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // No longer in the conflict marker. 204634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner IsInConflictMarker = false; 204734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return true; 204834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 204934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 205034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner return false; 205134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner} 205234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 20535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 20545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexTokenInternal - This implements a simple C family lexer. It is an 20555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// extremely performance critical piece of code. This assumes that the buffer 2056efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// has a null character at the end of the file. This returns a preprocessing 2057efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// token, not a normal token, as such, it is an internal interface. It assumes 2058efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// that the Flags of result have been cleared before calling this. 2059d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexTokenInternal(Token &Result) { 20605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerLexNextToken: 20615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // New token, can't need cleaning yet. 2062d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::NeedsCleaning); 20635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result.setIdentifierInfo(0); 20641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // CurPtr - Cache BufferPtr in an automatic variable. 20665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *CurPtr = BufferPtr; 20675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 20685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Small amounts of horizontal whitespace is very common between tokens. 20695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if ((*CurPtr == ' ') || (*CurPtr == '\t')) { 20705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 20715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while ((*CurPtr == ' ') || (*CurPtr == '\t')) 20725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 20731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2074d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // If we are keeping whitespace and other tokens, just return what we just 2075d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // skipped. The next lexer invocation will return the token after the 2076d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // whitespace. 2077d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (isKeepWhitespaceMode()) { 20789e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 2079d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; 2080d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner } 20811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 2083d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 20845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 20851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below. 20871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Read a character, advancing over it. 20895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Char = getAndAdvanceChar(CurPtr, Result); 20909e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner tok::TokenKind Kind; 20911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Char) { 20935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 0: // Null. 20945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Found end of file? 20955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr-1 == BufferEnd) { 20965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Read the PP instance variable into an automatic variable, because 20975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // LexEndOfFile will often delete 'this'. 2098168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner Preprocessor *PPCache = PP; 20995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. 21005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; // Got a token to return. 2101168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); 2102168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner return PPCache->Lex(Result); 21035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 21041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 210574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 210674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::null_in_file); 2107d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 2108d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 2109d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 21101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 2112a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner 2113a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner case 26: // DOS & CP/M EOF: "^Z". 2114a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // If we're in Microsoft extensions mode, treat this as end of file. 2115a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner if (Features.Microsoft) { 2116a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // Read the PP instance variable into an automatic variable, because 2117a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // LexEndOfFile will often delete 'this'. 2118a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner Preprocessor *PPCache = PP; 2119a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. 2120a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner return; // Got a token to return. 2121a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); 2122a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner return PPCache->Lex(Result); 2123a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner } 2124a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner // If Microsoft extensions are disabled, this is just random garbage. 2125a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner Kind = tok::unknown; 2126a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner break; 2127a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner 21285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\n': 21295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\r': 21305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are inside a preprocessor directive and we see the end of line, 213184021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne // we know we are done with the directive, so return an EOD token. 21325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 21335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Done parsing the "line". 21345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParsingPreprocessorDirective = false; 21351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore comment saving mode, in case it was disabled for directive. 2137f744d133e8d36eec65828d046ee695bda238125cChris Lattner SetCommentRetentionState(PP->getCommentRetentionState()); 21381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Since we consumed a newline, we are back at the start of a line. 21405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IsAtStartOfLine = true; 21411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 214284021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne Kind = tok::eod; 21435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 21445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 21455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The returned token is at the start of the line. 2146d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 21475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // No leading whitespace seen so far. 2148d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::LeadingSpace); 21491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2150d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 2151d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 21525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 21535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ' ': 21545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\t': 21555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\f': 21565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\v': 21578133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner SkipHorizontalWhitespace: 2158d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 2159d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 2160d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 21618133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner 21628133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner SkipIgnoredUnits: 21638133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner CurPtr = BufferPtr; 21641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21658133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner // If the next token is obviously a // or /* */ comment, skip it efficiently 21668133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner // too (without going through the big switch stmt). 21678402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() && 21682ed42287b9b88a0bb08ab10e95e306da3f294ba9Daniel Dunbar Features.BCPLComment && !Features.TraditionalCPP) { 2169046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner if (SkipBCPLComment(Result, CurPtr+2)) 2170046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 21718133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipIgnoredUnits; 2172fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) { 2173046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner if (SkipBlockComment(Result, CurPtr+2)) 2174046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 21758133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipIgnoredUnits; 21768133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner } else if (isHorizontalWhitespace(*CurPtr)) { 21778133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipHorizontalWhitespace; 21788133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner } 21795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 2180a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner 21813a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // C99 6.4.4.1: Integer Constants. 21823a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // C99 6.4.4.2: Floating Constants. 21833a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '0': case '1': case '2': case '3': case '4': 21843a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '5': case '6': case '7': case '8': case '9': 21853a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Notify MIOpt that we read a non-whitespace/non-comment token. 21863a5707766850f9ee9daa35299794328b5caf96dcChris Lattner MIOpt.ReadToken(); 21873a5707766850f9ee9daa35299794328b5caf96dcChris Lattner return LexNumericConstant(Result, CurPtr); 21881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21893a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz"). 21905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 21915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 21925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 21935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 21945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Wide string literal. 21955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '"') 21965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), 21975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer true); 21985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 21995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Wide character constant. 22005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '\'') 22015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); 22025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FALL THROUGH, treating L like the start of an identifier. 22031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.2: Identifiers. 22055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': 22065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N': 22075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': 22085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'V': case 'W': case 'X': case 'Y': case 'Z': 22095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': 22105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': 22115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': 22125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'v': case 'w': case 'x': case 'y': case 'z': 22135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '_': 22145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 22155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 22165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexIdentifier(Result, CurPtr); 22173a5707766850f9ee9daa35299794328b5caf96dcChris Lattner 22183a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '$': // $ in identifiers. 22193a5707766850f9ee9daa35299794328b5caf96dcChris Lattner if (Features.DollarIdents) { 222074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 222174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::ext_dollar_in_identifier); 22223a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Notify MIOpt that we read a non-whitespace/non-comment token. 22233a5707766850f9ee9daa35299794328b5caf96dcChris Lattner MIOpt.ReadToken(); 22243a5707766850f9ee9daa35299794328b5caf96dcChris Lattner return LexIdentifier(Result, CurPtr); 22253a5707766850f9ee9daa35299794328b5caf96dcChris Lattner } 22261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22279e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 22283a5707766850f9ee9daa35299794328b5caf96dcChris Lattner break; 22291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.4: Character Constants. 22315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\'': 22325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 22335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 22345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexCharConstant(Result, CurPtr); 22355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 22365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.5: String Literals. 22375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '"': 22385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 22395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 22405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexStringLiteral(Result, CurPtr, false); 22415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 22425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.6: Punctuators. 22435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '?': 22449e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::question; 22455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '[': 22479e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_square; 22485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ']': 22509e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_square; 22515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '(': 22539e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_paren; 22545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ')': 22569e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_paren; 22575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '{': 22599e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_brace; 22605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '}': 22629e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_brace; 22635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '.': 22655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 22665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char >= '0' && Char <= '9') { 22675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 22685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 22695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 22705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); 22715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Features.CPlusPlus && Char == '*') { 22729e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::periodstar; 22735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += SizeTmp; 22745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '.' && 22755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') { 22769e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ellipsis; 22775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 22785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 22795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 22809e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::period; 22815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '&': 22845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 22855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '&') { 22869e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ampamp; 22875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 22885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 22899e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ampequal; 22905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 22915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 22929e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::amp; 22935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 22945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 22951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case '*': 22965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (getCharAndSize(CurPtr, SizeTmp) == '=') { 22979e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::starequal; 22985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 22995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 23009e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::star; 23015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 23035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '+': 23045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 23055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '+') { 23065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 23079e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plusplus; 23085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 23095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 23109e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plusequal; 23115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 23129e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plus; 23135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 23155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '-': 23165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 23179e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner if (Char == '-') { // -- 23185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 23199e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minusminus; 23201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else if (Char == '>' && Features.CPlusPlus && 23219e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->* 23225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 23235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 23249e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::arrowstar; 23259e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner } else if (Char == '>') { // -> 23265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 23279e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::arrow; 23289e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner } else if (Char == '=') { // -= 23295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 23309e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minusequal; 23315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 23329e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minus; 23335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 23355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '~': 23369e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::tilde; 23375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 23385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '!': 23395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (getCharAndSize(CurPtr, SizeTmp) == '=') { 23409e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::exclaimequal; 23415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 23425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 23439e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::exclaim; 23445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 23465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '/': 23475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // 6.4.9: Comments 23485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 23495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '/') { // BCPL comment. 23508402c73dd880e8af46c826d873681820aebe32ebChris Lattner // Even if BCPL comments are disabled (e.g. in C89 mode), we generally 23518402c73dd880e8af46c826d873681820aebe32ebChris Lattner // want to lex this as a comment. There is one problem with this though, 23528402c73dd880e8af46c826d873681820aebe32ebChris Lattner // that in one particular corner case, this can change the behavior of the 23538402c73dd880e8af46c826d873681820aebe32ebChris Lattner // resultant program. For example, In "foo //**/ bar", C89 would lex 23548402c73dd880e8af46c826d873681820aebe32ebChris Lattner // this as "foo / bar" and langauges with BCPL comments would lex it as 23558402c73dd880e8af46c826d873681820aebe32ebChris Lattner // "foo". Check to see if the character after the second slash is a '*'. 23568402c73dd880e8af46c826d873681820aebe32ebChris Lattner // If so, we will lex that as a "/" instead of the start of a comment. 23572ed42287b9b88a0bb08ab10e95e306da3f294ba9Daniel Dunbar // However, we never do this in -traditional-cpp mode. 23582ed42287b9b88a0bb08ab10e95e306da3f294ba9Daniel Dunbar if ((Features.BCPLComment || 23592ed42287b9b88a0bb08ab10e95e306da3f294ba9Daniel Dunbar getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') && 23602ed42287b9b88a0bb08ab10e95e306da3f294ba9Daniel Dunbar !Features.TraditionalCPP) { 23618402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) 2362046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 23631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23648402c73dd880e8af46c826d873681820aebe32ebChris Lattner // It is common for the tokens immediately after a // comment to be 23658402c73dd880e8af46c826d873681820aebe32ebChris Lattner // whitespace (indentation for the next line). Instead of going through 23668402c73dd880e8af46c826d873681820aebe32ebChris Lattner // the big switch, handle it efficiently now. 23678402c73dd880e8af46c826d873681820aebe32ebChris Lattner goto SkipIgnoredUnits; 23688402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 23698402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 23701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23718402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (Char == '*') { // /**/ comment. 23725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) 2373046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner return; // There is a token to return. 23742d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner goto LexNextToken; // GCC isn't tail call eliminating. 23758402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 23761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23778402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (Char == '=') { 23785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 23799e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::slashequal; 23805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 23819e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::slash; 23825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 23835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 23845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '%': 23855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 23865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 23879e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::percentequal; 23885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 23895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Features.Digraphs && Char == '>') { 23909e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_brace; // '%>' -> '}' 23915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 23925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Features.Digraphs && Char == ':') { 23935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 23945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 23955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') { 23969e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashhash; // '%:%:' -> '##' 23975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 23985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 23995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize 24005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 240174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 240274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(BufferPtr, diag::charize_microsoft_ext); 24039e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashat; 2404e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner } else { // '%:' -> '#' 24055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We parsed a # character. If this occurs at the start of the line, 24065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it's actually the start of a preprocessing directive. Callback to 24075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the preprocessor to handle it. 24085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: -fpreprocessed mode?? 2409766703b76dc1eb0c49d11cafc2a17c7407876ebcChris Lattner if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) { 2410e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner FormTokenWithChars(Result, CurPtr, tok::hash); 2411168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner PP->HandleDirective(Result); 24121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 24135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // As an optimization, if the preprocessor didn't switch lexers, tail 24145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // recurse. 2415168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner if (PP->isCurrentLexer(this)) { 24165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Start a new token. If this is a #include or something, the PP may 24175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // want us starting at the beginning of the line again. If so, set 2418515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner // the StartOfLine flag and clear LeadingSpace. 24195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (IsAtStartOfLine) { 2420d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 2421515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner Result.clearFlag(Token::LeadingSpace); 24225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IsAtStartOfLine = false; 24235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 24245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 24255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 24261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2427168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner return PP->Lex(Result); 24285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 24291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2430e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner Kind = tok::hash; 24315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 24325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 24339e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::percent; 24345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 24355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 24365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '<': 24375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 24385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingFilename) { 24399cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner return LexAngledStringLiteral(Result, CurPtr); 24405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '<') { 244134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2); 244234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (After == '=') { 244334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::lesslessequal; 244434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 244534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner SizeTmp2, Result); 244634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) { 244734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If this is actually a '<<<<<<<' version control conflict marker, 244834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // recognize it as such and recover nicely. 244934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 24501b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne } else if (Features.CUDA && After == '<') { 24511b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne Kind = tok::lesslessless; 24521b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 24531b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne SizeTmp2, Result); 245434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else { 245534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 245634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::lessless; 245734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 24585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 24595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 24609e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::lessequal; 24619e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner } else if (Features.Digraphs && Char == ':') { // '<:' -> '[' 246287a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith if (Features.CPlusPlus0x && 246387a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == ':') { 246487a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // C++0x [lex.pptoken]p3: 246587a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // Otherwise, if the next three characters are <:: and the subsequent 246687a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // character is neither : nor >, the < is treated as a preprocessor 246787a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // token by itself and not as the first character of the alternative 246887a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith // token <:. 246987a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith unsigned SizeTmp3; 247087a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith char After = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3); 247187a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith if (After != ':' && After != '>') { 247287a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith Kind = tok::less; 247387a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith break; 247487a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith } 247587a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith } 247687a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith 24775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 24789e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_square; 24799e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner } else if (Features.Digraphs && Char == '%') { // '<%' -> '{' 24805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 24819e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_brace; 24825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 24839e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::less; 24845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 24855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 24865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '>': 24875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 24885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 24895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 24909e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::greaterequal; 24915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '>') { 249234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2); 249334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (After == '=') { 249434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 249534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner SizeTmp2, Result); 249634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::greatergreaterequal; 249734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) { 249834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If this is '>>>>>>>' and we're in a conflict marker, ignore it. 249934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 25001b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne } else if (Features.CUDA && After == '>') { 25011b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne Kind = tok::greatergreatergreater; 25021b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 25031b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne SizeTmp2, Result); 250434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } else { 250534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 250634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner Kind = tok::greatergreater; 250734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner } 250834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 25095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 25109e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::greater; 25115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 25125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 25135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '^': 25145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 25155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 25165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 25179e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::caretequal; 25185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 25199e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::caret; 25205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 25215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 25225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '|': 25235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 25245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 25259e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipeequal; 25265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 25275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '|') { 252834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If this is '|||||||' and we're in a conflict marker, ignore it. 252934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1)) 253034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 25319e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipepipe; 25325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 25335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 25349e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipe; 25355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 25365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 25375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ':': 25385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 25395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Features.Digraphs && Char == '>') { 25409e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_square; // ':>' -> ']' 25415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 25425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Features.CPlusPlus && Char == ':') { 25439e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::coloncolon; 25445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 25451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else { 25469e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::colon; 25475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 25485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 25495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ';': 25509e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::semi; 25515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 25525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '=': 25535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 25545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 255534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner // If this is '=======' and we're in a conflict marker, ignore it. 255634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1)) 255734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner goto LexNextToken; 255834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner 25599e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::equalequal; 25605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 25611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else { 25629e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::equal; 25635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 25645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 25655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ',': 25669e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::comma; 25675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 25685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '#': 25695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 25705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '#') { 25719e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashhash; 25725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 25735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize 25749e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashat; 257574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 257674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(BufferPtr, diag::charize_microsoft_ext); 25775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 25785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 25795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We parsed a # character. If this occurs at the start of the line, 25805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it's actually the start of a preprocessing directive. Callback to 25815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the preprocessor to handle it. 25825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: -fpreprocessed mode?? 2583766703b76dc1eb0c49d11cafc2a17c7407876ebcChris Lattner if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) { 2584e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner FormTokenWithChars(Result, CurPtr, tok::hash); 2585168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner PP->HandleDirective(Result); 25861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 25875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // As an optimization, if the preprocessor didn't switch lexers, tail 25885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // recurse. 2589168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner if (PP->isCurrentLexer(this)) { 25905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Start a new token. If this is a #include or something, the PP may 25915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // want us starting at the beginning of the line again. If so, set 2592515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner // the StartOfLine flag and clear LeadingSpace. 25935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (IsAtStartOfLine) { 2594d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 2595515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner Result.clearFlag(Token::LeadingSpace); 25965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IsAtStartOfLine = false; 25975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 25985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 25995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2600168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner return PP->Lex(Result); 26015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 26021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2603e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner Kind = tok::hash; 26045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 26055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 26065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 26073a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '@': 26083a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Objective C support. 26093a5707766850f9ee9daa35299794328b5caf96dcChris Lattner if (CurPtr[-1] == '@' && Features.ObjC1) 26109e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::at; 26113a5707766850f9ee9daa35299794328b5caf96dcChris Lattner else 26129e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 26133a5707766850f9ee9daa35299794328b5caf96dcChris Lattner break; 26141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 26155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\\': 26165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: UCN's. 26175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FALL THROUGH. 26185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 26199e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 26205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 26215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 26221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 26235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 26245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 26255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 26265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 26279e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, Kind); 26285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2629