Lexer.cpp revision 86d9a52c24d390631a888d4ff812e1b15445e0a0
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" 309dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner#include "clang/Basic/SourceManager.h" 31409a03671224d4e5bdab1594c43baf070148f830Chris Lattner#include "llvm/Support/Compiler.h" 325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Support/MemoryBuffer.h" 335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <cctype> 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic void InitCharacterInfo(); 375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 38dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 39dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner// Token Class Implementation 40dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 41dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier. 43dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattnerbool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const { 44bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor if (IdentifierInfo *II = getIdentifierInfo()) 45bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor return II->getObjCKeywordID() == objcKey; 46bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor return false; 47dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner} 48dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 49dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner/// getObjCKeywordID - Return the ObjC keyword kind. 50dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattnertok::ObjCKeywordKind Token::getObjCKeywordID() const { 51dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner IdentifierInfo *specId = getIdentifierInfo(); 52dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword; 53dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner} 54dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 5553702cd401b8fdca985aede7732c2f6a82ad9b1cChris Lattner 56dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 57dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner// Lexer Class Implementation 58dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===// 59dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Lexer::InitLexer(const char *BufStart, const char *BufPtr, 6122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner const char *BufEnd) { 6222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner InitCharacterInfo(); 631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner BufferStart = BufStart; 6522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner BufferPtr = BufPtr; 6622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner BufferEnd = BufEnd; 671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner assert(BufEnd[0] == 0 && 6922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner "We assume that the input buffer has a null character at the end" 7022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner " to simplify lexing!"); 711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner Is_PragmaLexer = false; 7381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor IsEofCodeCompletion = false; 7481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor 7522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // Start of the file is a start of line. 7622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner IsAtStartOfLine = true; 771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // We are not after parsing a #. 7922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner ParsingPreprocessorDirective = false; 801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // We are not after parsing #include. 8222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner ParsingFilename = false; 831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // We are not in raw mode. Raw mode disables diagnostics and interpretation 8522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // of tokens (e.g. identifiers, thus disabling macro expansion). It is used 8622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block 8722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // or otherwise skipping over tokens. 8822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner LexingRawMode = false; 891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner // Default to not keeping comments. 9122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner ExtendedTokenMode = 0; 9222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner} 9322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner 940770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// Lexer constructor - Create a new lexer object for the specified buffer 950770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// with the specified preprocessor managing the lexing process. This lexer 960770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// assumes that the associated file buffer and Preprocessor objects will 970770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// outlive it, so it doesn't take ownership of either of them. 9888d3ac1341aa016cabd966c5b113a95ac05ea43fChris LattnerLexer::Lexer(FileID FID, Preprocessor &PP) 9988d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner : PreprocessorLexer(&PP, FID), 10088d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)), 10188d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner Features(PP.getLangOptions()) { 1021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10388d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner const llvm::MemoryBuffer *InputFile = PP.getSourceManager().getBuffer(FID); 1041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1050770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(), 1060770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner InputFile->getBufferEnd()); 1071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1080770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner // Default to keeping comments if the preprocessor wants them. 1090770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner SetCommentRetentionState(PP.getCommentRetentionState()); 1100770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner} 111dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner 112168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner/// Lexer constructor - Create a new raw lexer object. This object is only 113590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner/// suitable for calls to 'LexRawToken'. This lexer assumes that the text 114590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner/// range will outlive it, so it doesn't take ownership of it. 115168ae2d44a443da75ea85db5f3b5081eb0bce113Chris LattnerLexer::Lexer(SourceLocation fileloc, const LangOptions &features, 116de96c0f29c4cacabe6ea577c61db87c2a85aea6cChris Lattner const char *BufStart, const char *BufPtr, const char *BufEnd) 117c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner : FileLoc(fileloc), Features(features) { 11822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner 11922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner InitLexer(BufStart, BufPtr, BufEnd); 1201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 121168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner // We *are* in raw mode. 122168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner LexingRawMode = true; 1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 125025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// Lexer constructor - Create a new raw lexer object. This object is only 126025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// suitable for calls to 'LexRawToken'. This lexer assumes that the text 127025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// range will outlive it, so it doesn't take ownership of it. 128025c3a66402fb713c2d9bf5dc174ff264765379aChris LattnerLexer::Lexer(FileID FID, const SourceManager &SM, const LangOptions &features) 129025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) { 130025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID); 131025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner 1321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(), 133025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner FromFile->getBufferEnd()); 1341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 135025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner // We *are* in raw mode. 136025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner LexingRawMode = true; 137025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner} 138025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner 13942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for 14042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// _Pragma expansion. This has a variety of magic semantics that this method 14142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// sets up. It returns a new'd Lexer that must be delete'd when done. 14242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 14342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// On entrance to this routine, TokStartLoc is a macro location which has a 14442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// spelling loc that indicates the bytes to be lexed for the token and an 14542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// instantiation location that indicates where all lexed tokens should be 14642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// "expanded from". 14742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 14842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// FIXME: It would really be nice to make _Pragma just be a wrapper around a 14942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// normal lexer that remaps tokens as they fly by. This would require making 15042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer 15142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// interface that could handle this stuff. This would pull GetMappedTokenLoc 15242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// out of the critical path of the lexer! 15342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// 1541eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpLexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc, 155e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner SourceLocation InstantiationLocStart, 156e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner SourceLocation InstantiationLocEnd, 157bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner unsigned TokLen, Preprocessor &PP) { 15842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner SourceManager &SM = PP.getSourceManager(); 15942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 16042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Create the lexer as if we were going to lex the file normally. 161a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner FileID SpellingFID = SM.getFileID(SpellingLoc); 162bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner Lexer *L = new Lexer(SpellingFID, PP); 1631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Now that the lexer is created, change the start/end locations so that we 16542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // just lex the subsection of the file that we want. This is lexing from a 16642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // scratch buffer. 16742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner const char *StrData = SM.getCharacterData(SpellingLoc); 1681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->BufferPtr = StrData; 17042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->BufferEnd = StrData+TokLen; 1711fa495304c81e03f07f278a47b5efe9317104aabChris Lattner assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!"); 17242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 17342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Set the SourceLocation with the remapping information. This ensures that 17442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // GetMappedTokenLoc will remap the tokens as they are lexed. 175de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID), 176e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner InstantiationLocStart, 177e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner InstantiationLocEnd, TokLen); 1781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // Ensure that the lexer thinks it is inside a directive, so that end \n will 18042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // return an EOM token. 18142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->ParsingPreprocessorDirective = true; 1821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner // This lexer really is for _Pragma. 18442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner L->Is_PragmaLexer = true; 18542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner return L; 18642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner} 18742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner 188168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner 1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Stringify - Convert the specified string into a C string, with surrounding 1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ""'s, and with escaped \ and " characters. 1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstd::string Lexer::Stringify(const std::string &Str, bool Charify) { 1925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer std::string Result = Str; 1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Quote = Charify ? '\'' : '"'; 1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = 0, e = Result.size(); i != e; ++i) { 1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Result[i] == '\\' || Result[i] == Quote) { 1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result.insert(Result.begin()+i, '\\'); 1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++i; ++e; 1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return Result; 2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 203d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// Stringify - Convert the specified string into a C string by escaping '\' 204d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// and " characters. This does not add surrounding ""'s to the string. 205d8e3083840fef752d11ca183f42786470ed061e3Chris Lattnervoid Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) { 206d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner for (unsigned i = 0, e = Str.size(); i != e; ++i) { 207d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner if (Str[i] == '\\' || Str[i] == '"') { 208d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner Str.insert(Str.begin()+i, '\\'); 209d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner ++i; ++e; 210d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner } 211d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner } 212d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner} 213d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner 2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2159a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// MeasureTokenLength - Relex the token at the specified location and return 2169a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// its length in bytes in the input file. If the token needs cleaning (e.g. 2179a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// includes a trigraph or an escaped newline) then this count includes bytes 2189a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// that are part of that. 2199a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattnerunsigned Lexer::MeasureTokenLength(SourceLocation Loc, 2202c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner const SourceManager &SM, 2212c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner const LangOptions &LangOpts) { 2229a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // TODO: this could be special cased for common tokens like identifiers, ')', 2239a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // etc to make this faster, if it mattered. Just look at StrData[0] to handle 2241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // all obviously single-char tokens. This could use 2259a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // Lexer::isObviouslySimpleCharacter for example to handle identifiers or 2269a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // something. 227de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 228de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // If this comes from a macro expansion, we really do want the macro name, not 229de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // the token this macro expanded to. 230363fdc29656cc03c1817268888f95e6343470aa8Chris Lattner Loc = SM.getInstantiationLoc(Loc); 231363fdc29656cc03c1817268888f95e6343470aa8Chris Lattner std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 2328350394c65b81bba3986dfe44ae17423873741deChris Lattner std::pair<const char *,const char *> Buffer = SM.getBufferData(LocInfo.first); 2338350394c65b81bba3986dfe44ae17423873741deChris Lattner const char *StrData = Buffer.first+LocInfo.second; 2348350394c65b81bba3986dfe44ae17423873741deChris Lattner 2359a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner // Create a lexer starting at the beginning of this token. 236de96c0f29c4cacabe6ea577c61db87c2a85aea6cChris Lattner Lexer TheLexer(Loc, LangOpts, Buffer.first, StrData, Buffer.second); 2379a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner Token TheTok; 238590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner TheLexer.LexFromRawLexer(TheTok); 2399a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner return TheTok.getLength(); 2409a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner} 2419a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner 2425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Character information. 2445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum { 2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0' 2485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_VERT_WS = 0x02, // '\r', '\n' 2495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_LETTER = 0x04, // a-z,A-Z 2505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_NUMBER = 0x08, // 0-9 2515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_UNDER = 0x10, // _ 2525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CHAR_PERIOD = 0x20 // . 2535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}; 2545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 25503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// Statically initialize CharInfo table based on ASCII character set 25603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// Reference: FreeBSD 7.2 /usr/share/misc/ascii 25703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattnerstatic const unsigned char CharInfo[256] = 25803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner{ 25903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 0 NUL 1 SOH 2 STX 3 ETX 26003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 4 EOT 5 ENQ 6 ACK 7 BEL 26103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 26203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 26303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 8 BS 9 HT 10 NL 11 VT 26403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//12 NP 13 CR 14 SO 15 SI 26503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS, 26603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 , 26703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//16 DLE 17 DC1 18 DC2 19 DC3 26803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//20 DC4 21 NAK 22 SYN 23 ETB 26903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 27003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 27103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//24 CAN 25 EM 26 SUB 27 ESC 27203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//28 FS 29 GS 30 RS 31 US 27303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 27403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 27503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//32 SP 33 ! 34 " 35 # 27603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//36 $ 37 % 38 & 39 ' 27703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_HORZ_WS, 0 , 0 , 0 , 27803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 27903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//40 ( 41 ) 42 * 43 + 28003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//44 , 45 - 46 . 47 / 28103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 28203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , CHAR_PERIOD , 0 , 28303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//48 0 49 1 50 2 51 3 28403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//52 4 53 5 54 6 55 7 28503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , 28603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , 28703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//56 8 57 9 58 : 59 ; 28803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//60 < 61 = 62 > 63 ? 28903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_NUMBER , CHAR_NUMBER , 0 , 0 , 29003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 , 29103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//64 @ 65 A 66 B 67 C 29203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//68 D 69 E 70 F 71 G 29303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 29403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 29503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//72 H 73 I 74 J 75 K 29603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//76 L 77 M 78 N 79 O 29703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 29803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 29903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//80 P 81 Q 82 R 83 S 30003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//84 T 85 U 86 V 87 W 30103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 30203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 30303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//88 X 89 Y 90 Z 91 [ 30403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//92 \ 93 ] 94 ^ 95 _ 30503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 , 30603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , CHAR_UNDER , 30703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//96 ` 97 a 98 b 99 c 30803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//100 d 101 e 102 f 103 g 30903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 31003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 31103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//104 h 105 i 106 j 107 k 31203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//108 l 109 m 110 n 111 o 31303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 31403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 31503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//112 p 113 q 114 r 115 s 31603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//116 t 117 u 118 v 119 w 31703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 31803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 31903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//120 x 121 y 122 z 123 { 32003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//124 | 125 } 126 ~ 127 DEL 32103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 , 32203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 0 , 0 , 0 , 0 32303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner}; 32403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 3255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic void InitCharacterInfo() { 3265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer static bool isInited = false; 3275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isInited) return; 32803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner // check the statically-initialized CharInfo table 32903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_HORZ_WS == CharInfo[(int)' ']); 33003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_HORZ_WS == CharInfo[(int)'\t']); 33103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_HORZ_WS == CharInfo[(int)'\f']); 33203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_HORZ_WS == CharInfo[(int)'\v']); 33303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_VERT_WS == CharInfo[(int)'\n']); 33403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_VERT_WS == CharInfo[(int)'\r']); 33503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_UNDER == CharInfo[(int)'_']); 33603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_PERIOD == CharInfo[(int)'.']); 33703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner for (unsigned i = 'a'; i <= 'z'; ++i) { 33803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_LETTER == CharInfo[i]); 33903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_LETTER == CharInfo[i+'A'-'a']); 34003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner } 3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = '0'; i <= '9'; ++i) 34203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner assert(CHAR_NUMBER == CharInfo[i]); 34303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner isInited = true; 3445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 34603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner 3475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIdentifierBody - Return true if this is the body character of an 3485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// identifier, which is [a-zA-Z0-9_]. 3495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isIdentifierBody(unsigned char c) { 35095c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false; 3515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isHorizontalWhitespace - Return true if this character is horizontal 3545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'. 3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isHorizontalWhitespace(unsigned char c) { 35695c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser return (CharInfo[c] & CHAR_HORZ_WS) ? true : false; 3575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isWhitespace - Return true if this character is horizontal or vertical 3605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false 3615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// for '\0'. 3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isWhitespace(unsigned char c) { 36395c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false; 3645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNumberBody - Return true if this is the body character of an 3675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// preprocessing number, which is [a-zA-Z0-9_.]. 3685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isNumberBody(unsigned char c) { 3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ? 37095c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser true : false; 3715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 3755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Diagnostics forwarding code. 3765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 378409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the 379409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// lexer buffer was all instantiated at a single point, perform the mapping. 380409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// This is currently only used for _Pragma implementation, so it is the slow 381409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// path of the hot getSourceLocation method. Do not allow it to be inlined. 382409a03671224d4e5bdab1594c43baf070148f830Chris Lattnerstatic SourceLocation GetMappedTokenLoc(Preprocessor &PP, 383409a03671224d4e5bdab1594c43baf070148f830Chris Lattner SourceLocation FileLoc, 384de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned CharNo, 385de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned TokLen) DISABLE_INLINE; 386409a03671224d4e5bdab1594c43baf070148f830Chris Lattnerstatic SourceLocation GetMappedTokenLoc(Preprocessor &PP, 387409a03671224d4e5bdab1594c43baf070148f830Chris Lattner SourceLocation FileLoc, 388de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned CharNo, unsigned TokLen) { 389e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner assert(FileLoc.isMacroID() && "Must be an instantiation"); 3901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 391409a03671224d4e5bdab1594c43baf070148f830Chris Lattner // Otherwise, we're lexing "mapped tokens". This is used for things like 392409a03671224d4e5bdab1594c43baf070148f830Chris Lattner // _Pragma handling. Combine the instantiation location of FileLoc with the 393df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner // spelling location. 394e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner SourceManager &SM = PP.getSourceManager(); 3951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 396f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose 397df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner // characters come from spelling(FileLoc)+Offset. 398e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc); 399bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo); 4001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 401e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner // Figure out the expansion loc range, which is the range covered by the 402e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner // original _Pragma(...) sequence. 403e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner std::pair<SourceLocation,SourceLocation> II = 404e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner SM.getImmediateInstantiationRange(FileLoc); 4051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 406e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen); 407409a03671224d4e5bdab1594c43baf070148f830Chris Lattner} 408409a03671224d4e5bdab1594c43baf070148f830Chris Lattner 4095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSourceLocation - Return a source location identifier for the specified 4105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// offset in the current file. 411de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris LattnerSourceLocation Lexer::getSourceLocation(const char *Loc, 412de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned TokLen) const { 413448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner assert(Loc >= BufferStart && Loc <= BufferEnd && 4145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer "Location out of range for this buffer!"); 4159dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner 4169dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner // In the normal case, we're just lexing from a simple file buffer, return 4179dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner // the file id from FileLoc with the offset specified. 418448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner unsigned CharNo = Loc-BufferStart; 4199dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner if (FileLoc.isFileID()) 420bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner return FileLoc.getFileLocWithOffset(CharNo); 4211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4222b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner // Otherwise, this is the _Pragma lexer case, which pretends that all of the 4232b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner // tokens are lexed from where the _Pragma was defined. 424168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner assert(PP && "This doesn't work on raw lexers"); 425de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen); 4265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Diag - Forwarding function for diagnostics. This translate a source 4295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// position in the current buffer into a SourceLocation object for rendering. 4303cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const { 4313692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner return PP->Diag(getSourceLocation(Loc), DiagID); 4325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 4355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Trigraph and Escaped Newline Handling Code. 4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair, 4395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the decoded trigraph letter it corresponds to, or '\0' if nothing. 4405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char GetTrigraphCharForLetter(char Letter) { 4415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Letter) { 4425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: return 0; 4435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '=': return '#'; 4445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ')': return ']'; 4455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '(': return '['; 4465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '!': return '|'; 4475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\'': return '^'; 4485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '>': return '}'; 4495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '/': return '\\'; 4505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '<': return '{'; 4515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '-': return '~'; 4525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 4535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// DecodeTrigraphChar - If the specified character is a legal trigraph when 4565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled, 4575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the result character. Finally, emit a warning about trigraph use 4585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whether trigraphs are enabled or not. 4595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char DecodeTrigraphChar(const char *CP, Lexer *L) { 4605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Res = GetTrigraphCharForLetter(*CP); 4613692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner if (!Res || !L) return Res; 4621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4633692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner if (!L->getFeatures().Trigraphs) { 46474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 46574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CP-2, diag::trigraph_ignored); 4663692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner return 0; 4675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 4681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 46974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 47074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res; 4715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return Res; 4725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 47424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// getEscapedNewLineSize - Return the size of the specified escaped newline, 47524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a 4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// trigraph equivalent on entry to this function. 47724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattnerunsigned Lexer::getEscapedNewLineSize(const char *Ptr) { 47824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner unsigned Size = 0; 47924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner while (isWhitespace(Ptr[Size])) { 48024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner ++Size; 4811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 48224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r') 48324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner continue; 48424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 48524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // If this is a \r\n or \n\r, skip the other half. 48624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') && 48724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr[Size-1] != Ptr[Size]) 48824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner ++Size; 4891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 49024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return Size; 4911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 4921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 49324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Not an escaped newline, must be a \t or something else. 49424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return 0; 49524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner} 49624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 497033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// SkipEscapedNewLines - If P points to an escaped newline (or a series of 498033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// them), skip over them and return the first non-escaped-newline found, 499033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// otherwise return P. 500033749571f8d4c804eeb357c70b06424aa24503bChris Lattnerconst char *Lexer::SkipEscapedNewLines(const char *P) { 501033749571f8d4c804eeb357c70b06424aa24503bChris Lattner while (1) { 502033749571f8d4c804eeb357c70b06424aa24503bChris Lattner const char *AfterEscape; 503033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (*P == '\\') { 504033749571f8d4c804eeb357c70b06424aa24503bChris Lattner AfterEscape = P+1; 505033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } else if (*P == '?') { 506033749571f8d4c804eeb357c70b06424aa24503bChris Lattner // If not a trigraph for escape, bail out. 507033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (P[1] != '?' || P[2] != '/') 508033749571f8d4c804eeb357c70b06424aa24503bChris Lattner return P; 509033749571f8d4c804eeb357c70b06424aa24503bChris Lattner AfterEscape = P+3; 510033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } else { 511033749571f8d4c804eeb357c70b06424aa24503bChris Lattner return P; 512033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } 5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 514033749571f8d4c804eeb357c70b06424aa24503bChris Lattner unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape); 515033749571f8d4c804eeb357c70b06424aa24503bChris Lattner if (NewLineSize == 0) return P; 516033749571f8d4c804eeb357c70b06424aa24503bChris Lattner P = AfterEscape+NewLineSize; 517033749571f8d4c804eeb357c70b06424aa24503bChris Lattner } 518033749571f8d4c804eeb357c70b06424aa24503bChris Lattner} 519033749571f8d4c804eeb357c70b06424aa24503bChris Lattner 52024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 5215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer, 5225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// get its size, and return it. This is tricky in several cases: 5235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 1. If currently at the start of a trigraph, we warn about the trigraph, 5245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// then either return the trigraph (skipping 3 chars) or the '?', 5255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// depending on whether trigraphs are enabled or not. 5265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 2. If this is an escaped newline (potentially with whitespace between 5275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the backslash and newline), implicitly skip the newline and return 5285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the char after it. 5295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 3. If this is a UCN, return it. FIXME: C++ UCN's? 5305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 5315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This handles the slow/uncommon case of the getCharAndSize method. Here we 5325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// know that we can accumulate into Size, and that we have already incremented 5335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Ptr by Size bytes. 5345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 5355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should 5365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match. 5375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 5385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size, 539d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token *Tok) { 5405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, look for an escaped newline. 5415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '\\') { 5425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 5435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Ptr; 5445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash: 5455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Common case, backslash-char where the char is not whitespace. 5465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isWhitespace(Ptr[0])) return '\\'; 5471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5485636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner // See if we have optional whitespace characters between the slash and 5495636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner // newline. 55024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { 55124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Remember that this token needs to be cleaned. 55224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (Tok) Tok->setFlag(Token::NeedsCleaning); 55324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner 55424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Warn if there was whitespace between the backslash and newline. 5555636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode()) 55624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Diag(Ptr, diag::backslash_newline_space); 5571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 55824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Found backslash<whitespace><newline>. Parse the char after it. 55924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Size += EscapedNewLineSize; 56024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr += EscapedNewLineSize; 56124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Use slow version to accumulate a correct size field. 56224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return getCharAndSizeSlow(Ptr, Size, Tok); 56324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner } 5641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is not an escaped newline, just return the slash. 5665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return '\\'; 5675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 5681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a trigraph, process it. 5705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '?' && Ptr[1] == '?') { 5715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is actually a legal trigraph (not something like "??x"), emit 5725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // a trigraph warning. If so, and if trigraphs are enabled, return it. 5735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) { 5745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Remember that this token needs to be cleaned. 575d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner if (Tok) Tok->setFlag(Token::NeedsCleaning); 5765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 5775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Ptr += 3; 5785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Size += 3; 5795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') goto Slash; 5805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return C; 5815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 5825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 5831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is neither, return a single character. 5855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 5865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return *Ptr; 5875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 5885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 5895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 5905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the 5915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size, 5925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// and that we have already incremented Ptr by Size bytes. 5935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 5945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlow (above) should 5955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match. 5965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size, 5975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const LangOptions &Features) { 5985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, look for an escaped newline. 5995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Ptr[0] == '\\') { 6005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 6015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Ptr; 6025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash: 6035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Common case, backslash-char where the char is not whitespace. 6045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isWhitespace(Ptr[0])) return '\\'; 6051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // See if we have optional whitespace characters followed by a newline. 60724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { 60824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Found backslash<whitespace><newline>. Parse the char after it. 60924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Size += EscapedNewLineSize; 61024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner Ptr += EscapedNewLineSize; 6111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 61224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner // Use slow version to accumulate a correct size field. 61324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner return getCharAndSizeSlowNoWarn(Ptr, Size, Features); 61424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner } 6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is not an escaped newline, just return the slash. 6175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return '\\'; 6185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 6191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a trigraph, process it. 6215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') { 6225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is actually a legal trigraph (not something like "??x"), return 6235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it. 6245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (char C = GetTrigraphCharForLetter(Ptr[2])) { 6255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Ptr += 3; 6265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Size += 3; 6275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') goto Slash; 6285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return C; 6295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 6305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 6311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is neither, return a single character. 6335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++Size; 6345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return *Ptr; 6355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 6365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 6385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper methods for lexing. 6395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 6405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 641d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexIdentifier(Token &Result, const char *CurPtr) { 6425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$] 6435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned Size; 6445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned char C = *CurPtr++; 6455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isIdentifierBody(C)) { 6465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 6475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 6485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; // Back up over the skipped character. 6495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Fast path, no $,\,? in identifier found. '\' might be an escaped newline 6515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN. 6525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: UCNs. 6535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) { 6545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerFinishIdentifier: 6555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *IdStart = BufferPtr; 6569e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::identifier); 6571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are in raw mode, return this identifier raw. There is no need to 6595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // look up identifier information or attempt to macro expand it. 6605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (LexingRawMode) return; 6611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Fill in Result.IdentifierInfo, looking up the identifier in the 6635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // identifier table. 664d1186fa38166a581b51975f0382a45fc3a0733d0Chris Lattner IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart); 6651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 666863c486fcb6162495a94fddf7ac8409de2638995Chris Lattner // Change the kind of this identifier to the appropriate token kind, e.g. 667863c486fcb6162495a94fddf7ac8409de2638995Chris Lattner // turning "for" into a keyword. 668863c486fcb6162495a94fddf7ac8409de2638995Chris Lattner Result.setKind(II->getTokenID()); 6691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Finally, now that we know we have an identifier, pass this off to the 6715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // preprocessor, which may macro expand it or something. 672d1186fa38166a581b51975f0382a45fc3a0733d0Chris Lattner if (II->isHandleIdentifierCase()) 6736a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner PP->HandleIdentifier(Result); 6746a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner return; 6755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, $,\,? in identifier found. Enter slower path. 6781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 6805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 6815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '$') { 6825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we hit a $ and they are not supported in identifiers, we are done. 6835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!Features.DollarIdents) goto FinishIdentifier; 6841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, emit a diagnostic and continue. 68674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 68774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr, diag::ext_dollar_in_identifier); 6885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 6895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 6905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer continue; 6915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (!isIdentifierBody(C)) { // FIXME: UCNs. 6925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Found end of identifier. 6935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto FinishIdentifier; 6945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 6955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this character is good, consume it. 6975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 6985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 7005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isIdentifierBody(C)) { // FIXME: UCNs. 7015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 7025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 7035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 7045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 7055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 7065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 7075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 7085253c7ff266ae79308050c9f43d60dd1a67c5fb9Nate Begeman/// LexNumericConstant - Lex the remainder of a integer or floating point 7095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. From[-1] is the first character lexed. Return the end of the 7105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. 711d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexNumericConstant(Token &Result, const char *CurPtr) { 7125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned Size; 7135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getCharAndSize(CurPtr, Size); 7145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char PrevCh = 0; 7155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isNumberBody(C)) { // FIXME: UCNs? 7165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, Size, Result); 7175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer PrevCh = C; 7185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getCharAndSize(CurPtr, Size); 7195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 7201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we fell out, check for a sign, due to 1e+12. If we have one, continue. 7225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) 7235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); 7245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 7255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a hex FP constant, continue. 726f01fdff97b245caac98100d232c760b4d0531411Eli Friedman if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p')) 7275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); 7281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 73047246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 7319e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::numeric_constant); 73247246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 7335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 7345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 7355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexStringLiteral - Lex the remainder of a string literal, after having lexed 7365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// either " or L". 737d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnervoid Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) { 7385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this string contain the \0 character? 7391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 7415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '"') { 7425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip escaped characters. 7435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') { 7445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip the escaped character. 7455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == '\n' || C == '\r' || // Newline. 7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. 74833ab3f6c7e3065550de202088f8f335ecfa16ae1Chris Lattner if (!isLexingRawMode() && !Features.AsmPreprocessor) 74974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(BufferPtr, diag::err_unterminated_string); 7509e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr-1, tok::unknown); 7515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 7525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == 0) { 7535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer NulCharacter = CurPtr-1; 7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 7555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 7565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 7571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If a nul character existed in the string, warn about it. 75974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 76074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_string); 7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 7625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of the token as well as the BufferPtr instance var. 76347246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 7649e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, 7659e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Wide ? tok::wide_string_literal : tok::string_literal); 76647246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 7675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 7685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 7695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexAngledStringLiteral - Lex the remainder of an angled string literal, 7705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// after having lexed the '<' character. This is used for #include filenames. 771d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) { 7725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this string contain the \0 character? 7739cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner const char *AfterLessPos = CurPtr; 7745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 7755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '>') { 7765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip escaped characters. 7775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') { 7785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip the escaped character. 7795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 7805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == '\n' || C == '\r' || // Newline. 7815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. 7829cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner // If the filename is unterminated, then it must just be a lone < 7839cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner // character. Return this as such. 7849cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner FormTokenWithChars(Result, AfterLessPos, tok::less); 7855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 7865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == 0) { 7875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer NulCharacter = CurPtr-1; 7885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 7895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 7905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 7911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If a nul character existed in the string, warn about it. 79374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 79474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_string); 7951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 79747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 7989e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::angle_string_literal); 79947246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 8005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 8015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexCharConstant - Lex the remainder of a character constant, after having 8045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexed either ' or L'. 805d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexCharConstant(Token &Result, const char *CurPtr) { 8065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *NulCharacter = 0; // Does this character contain the \0 character? 8075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Handle the common case of 'x' and '\y' efficiently. 8095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C = getAndAdvanceChar(CurPtr, Result); 8105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\'') { 81133ab3f6c7e3065550de202088f8f335ecfa16ae1Chris Lattner if (!isLexingRawMode() && !Features.AsmPreprocessor) 81274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(BufferPtr, diag::err_empty_character); 8139e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 8145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 8155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == '\\') { 8165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip the escaped character. 8175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: UCN's. 8185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 8195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') { 8225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 8235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 8245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Fall back on generic code for embedded nulls, newlines, wide chars. 8255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer do { 8265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip escaped characters. 8275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\\') { 8285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip the escaped character. 8295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 8305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == '\n' || C == '\r' || // Newline. 8315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. 83233ab3f6c7e3065550de202088f8f335ecfa16ae1Chris Lattner if (!isLexingRawMode() && !Features.AsmPreprocessor) 83374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(BufferPtr, diag::err_unterminated_char); 8349e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr-1, tok::unknown); 8355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; 8365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == 0) { 8375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer NulCharacter = CurPtr-1; 8385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 8405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } while (C != '\''); 8415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 84374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (NulCharacter && !isLexingRawMode()) 84474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(NulCharacter, diag::null_in_char); 8455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 84747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner const char *TokStart = BufferPtr; 8489e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::char_constant); 84947246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.setLiteralData(TokStart); 8505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 8515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipWhitespace - Efficiently skip over a series of whitespace characters. 8535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Update BufferPtr to point to the next non-whitespace character and return. 854d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// 855d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// This method forms a token and returns true if KeepWhitespaceMode is enabled. 856d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// 857d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnerbool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) { 8585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Whitespace - Skip it, then return the token after the whitespace. 8595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently. 8605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 8615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip horizontal whitespace very aggressively. 8625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isHorizontalWhitespace(Char)) 8635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = *++CurPtr; 8641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 865ddd3e8b90a92c39f620bda7a0945320c0b9e60dbDaniel Dunbar // Otherwise if we have something other than whitespace, we're done. 8665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char != '\n' && Char != '\r') 8675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 8681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 8705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // End of preprocessor directive line, let LexTokenInternal handle this. 8715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 872d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return false; 8735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // ok, but handle newline. 8765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The returned token is at the start of the line. 877d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 8785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // No leading whitespace seen so far. 879d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::LeadingSpace); 8805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = *++CurPtr; 8815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this isn't immediately after a newline, there is leading space. 8845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char PrevChar = CurPtr[-1]; 8855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (PrevChar != '\n' && PrevChar != '\r') 886d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 8875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 888d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // If the client wants us to return whitespace, return it now. 889d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (isKeepWhitespaceMode()) { 8909e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 891d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return true; 892d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner } 8931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 895d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return false; 8965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 8975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 8985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// SkipBCPLComment - We have just read the // characters from input. Skip until 8995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// we find the newline character thats terminate the comment. Then update 9002d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner/// BufferPtr and return. If we're in KeepCommentMode, this will form the token 9012d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner/// and return true. 902d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) { 9035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If BCPL comments aren't explicitly enabled for this language, emit an 9045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // extension warning. 90574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!Features.BCPLComment && !isLexingRawMode()) { 9065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Diag(BufferPtr, diag::ext_bcpl_comment); 9071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Mark them enabled so we only emit one warning for this translation 9095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // unit. 9105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Features.BCPLComment = true; 9115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan over the body of the comment. The common case, when scanning, is that 9145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the comment contains normal ascii characters with nothing interesting in 9155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // them. As such, optimize for this case with the inner loop. 9165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char C; 9175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer do { 9185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr; 9195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character. 9205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we find a \n character, scan backwards, checking to see if it's an 9215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // escaped newline, like we do for block comments. 9221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip over characters in the fast loop. 9245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != 0 && // Potentially EOF. 9255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C != '\\' && // Potentially escaped newline. 9265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C != '?' && // Potentially trigraph. 9275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C != '\n' && C != '\r') // Newline or DOS-style newline. 9285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *++CurPtr; 9295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a newline, we're done. 9315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '\n' || C == '\r') 9325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; // Found the newline? Break out! 9331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to 935bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // properly decode the character. Read it in raw mode to avoid emitting 936bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // diagnostics about things like trigraphs. If we see an escaped newline, 937bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner // we'll handle it below. 9385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *OldPtr = CurPtr; 939bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner bool OldRawMode = isLexingRawMode(); 940bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner LexingRawMode = true; 9415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = getAndAdvanceChar(CurPtr, Result); 942bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner LexingRawMode = OldRawMode; 943ead616c5d8681a41b11273327813e61bda01907aChris Lattner 944ead616c5d8681a41b11273327813e61bda01907aChris Lattner // If the char that we finally got was a \n, then we must have had something 945ead616c5d8681a41b11273327813e61bda01907aChris Lattner // like \<newline><newline>. We don't want to have consumed the second 946ead616c5d8681a41b11273327813e61bda01907aChris Lattner // newline, we want CurPtr, to end up pointing to it down below. 947ead616c5d8681a41b11273327813e61bda01907aChris Lattner if (C == '\n' || C == '\r') { 948ead616c5d8681a41b11273327813e61bda01907aChris Lattner --CurPtr; 949ead616c5d8681a41b11273327813e61bda01907aChris Lattner C = 'x'; // doesn't matter what this is. 950ead616c5d8681a41b11273327813e61bda01907aChris Lattner } 9511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we read multiple characters, and one of those characters was a \r or 9535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n, then we had an escaped newline within the comment. Emit diagnostic 9545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // unless the next line is also a // comment. 9555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') { 9565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (; OldPtr != CurPtr; ++OldPtr) 9575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (OldPtr[0] == '\n' || OldPtr[0] == '\r') { 9585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Okay, we found a // comment that ends in a newline, if the next 9595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // line is also a // comment, but has spaces, don't emit a diagnostic. 9605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isspace(C)) { 9615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *ForwardPtr = CurPtr; 9625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isspace(*ForwardPtr)) // Skip whitespace. 9635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++ForwardPtr; 9645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/') 9655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 9665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 96874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 96974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment); 9705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 9715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr == BufferEnd+1) { --CurPtr; break; } 9755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } while (C != '\n' && C != '\r'); 9765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Found but did not consume the newline. 9782e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (PP) 9791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump PP->HandleComment(SourceRange(getSourceLocation(BufferPtr), 9802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor getSourceLocation(CurPtr))); 9811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are returning comments as tokens, return this comment as a token. 983fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner if (inKeepCommentMode()) 9845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return SaveBCPLComment(Result, CurPtr); 9855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 9865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are inside a preprocessor directive and we see the end of line, 9875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // return immediately, so that the lexer can return this as an EOM token. 9885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective || CurPtr == BufferEnd) { 9895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 9902d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 9915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 9921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, eat the \n character. We don't care if this is a \n\r or 9947a4f004a77421af876152281b92c4f8ea24afb2dChris Lattner // \r\n sequence. This is an efficiency hack (because we know the \n can't 995d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // contribute to another token), it isn't needed for correctness. Note that 996d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // this is ok even in KeepWhitespaceMode, because we would have returned the 997d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner /// comment above in that mode. 9985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 9991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The next returned token is at the start of the line. 1001d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 10025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // No leading whitespace seen so far. 1003d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::LeadingSpace); 10045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 10052d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 10065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in 10095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// an appropriate way and return it. 1010d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) { 10119e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // If we're not in a preprocessor directive, just return the // comment 10129e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // directly. 10139e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::comment); 10141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10159e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner if (!ParsingPreprocessorDirective) 10169e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner return true; 10171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10189e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // If this BCPL-style comment is in a macro definition, transmogrify it into 10199e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner // a C-style block comment. 10209e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner std::string Spelling = PP->getSpelling(Result); 10219e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?"); 10229e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Spelling[1] = '*'; // Change prefix to "/*". 10239e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Spelling += "*/"; // add suffix. 10241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10259e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Result.setKind(tok::comment); 102647246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner PP->CreateString(&Spelling[0], Spelling.size(), Result, 102747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner Result.getLocation()); 10282d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return true; 10295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline 10325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// character (either \n or \r) is part of an escaped newline sequence. Issue a 103347a2b40b7a3d9c89d6c3d12e5a81e2498c207750Chris Lattner/// diagnostic if so. We know that the newline is inside of a block comment. 10341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr, 10355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Lexer *L) { 10365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(CurPtr[0] == '\n' || CurPtr[0] == '\r'); 10371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Back up off the newline. 10395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 10401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a two-character newline sequence, skip the other character. 10425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == '\n' || CurPtr[0] == '\r') { 10435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n\n or \r\r -> not escaped newline. 10445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == CurPtr[1]) 10455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 10465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // \n\r or \r\n -> skip the newline. 10475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 10485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have horizontal whitespace, skip over it. We allow whitespace 10515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // between the slash and newline. 10525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool HasSpace = false; 10535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) { 10545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --CurPtr; 10555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer HasSpace = true; 10565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we have a slash, we know this is an escaped newline. 10595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (*CurPtr == '\\') { 10605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[-1] != '*') return false; 10615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 10625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It isn't a slash, is it the ?? / trigraph? 10635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' || 10645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[-3] != '*') 10655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 10661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // This is the trigraph ending the comment. Emit a stern warning! 10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr -= 2; 10695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If no trigraphs are enabled, warn that we ignored this trigraph and 10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // ignore this * character. 10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!L->getFeatures().Trigraphs) { 107374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 107474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::trigraph_ignored_block_comment); 10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return false; 10765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 107774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 107874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::trigraph_ends_block_comment); 10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Warn about having an escaped newline between the */ characters. 108274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!L->isLexingRawMode()) 108374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::escaped_newline_block_comment_end); 10841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If there was space between the backslash and newline, warn about it. 108674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (HasSpace && !L->isLexingRawMode()) 108774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner L->Diag(CurPtr, diag::backslash_newline_space); 10881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; 10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__ 10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <emmintrin.h> 10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__ 10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <altivec.h> 10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#undef bool 10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif 10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipBlockComment - We have just read the /* characters from input. Read 11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// until we find the */ characters that terminate the comment. Note that we 11015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// don't bother decoding trigraphs or escaped newlines in block comments, 11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// because they cannot cause the comment to end. The only thing that can 11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// happen is the comment could end with an escaped newline between the */ end 11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// of comment. 11052d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner/// 11062d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner/// If KeepCommentMode is enabled, this forms a token from the comment and 11072d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner/// returns true. 1108d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) { 11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan one character past where we should, looking for a '/' character. Once 11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // we find it, check to see if it was preceeded by a *. This common 11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // optimization helps people who like to put a lot of * characters in their 11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // comments. 11138146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner 11148146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // The first character we get with newlines and trigraphs skipped to handle 11158146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // the degenerate /*/ case below correctly if the * has an escaped newline 11168146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // after it. 11178146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner unsigned CharSize; 11188146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner unsigned char C = getCharAndSize(CurPtr, CharSize); 11198146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner CurPtr += CharSize; 11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == 0 && CurPtr == BufferEnd+1) { 112174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 11220af574270d3be2b0e73a3379dfaa633746f8fc6fChris Lattner Diag(BufferPtr, diag::err_unterminated_block_comment); 112331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner --CurPtr; 11241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 112531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // KeepWhitespaceMode should return this broken comment as a token. Since 112631f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // it isn't a well formed comment, just return it as an 'unknown' token. 112731f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner if (isKeepWhitespaceMode()) { 11289e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 112931f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner return true; 113031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner } 11311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 113231f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner BufferPtr = CurPtr; 11332d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 11351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11368146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // Check to see if the first character after the '/*' is another /. If so, 11378146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner // then this slash does not end the block comment, it is part of it. 11388146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner if (C == '/') 11398146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner C = *CurPtr++; 11401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Skip over all non-interesting characters until we find end of buffer or a 11435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // (probably ending) '/' character. 11445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr + 24 < BufferEnd) { 11455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // While not aligned to a 16-byte boundary. 11465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0) 11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 11481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '/') goto FoundSlash; 11505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__ 11525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/', 11535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer '/', '/', '/', '/', '/', '/', '/', '/'); 11545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (CurPtr+16 <= BufferEnd && 11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0) 11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 16; 11575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__ 11585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer __vector unsigned char Slashes = { 11591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump '/', '/', '/', '/', '/', '/', '/', '/', 11605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer '/', '/', '/', '/', '/', '/', '/', '/' 11615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer }; 11625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (CurPtr+16 <= BufferEnd && 11635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes)) 11645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 16; 11651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#else 11665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Scan for '/' quickly. Many block comments are very large. 11675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (CurPtr[0] != '/' && 11685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[1] != '/' && 11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[2] != '/' && 11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr[3] != '/' && 11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr+4 < BufferEnd) { 11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += 4; 11735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif 11751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It has to be one of the bytes scanned, increment to it and read one. 11775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 11785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 11791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Loop to scan the remainder. 11815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (C != '/' && C != '\0') 11825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 11831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer FoundSlash: 11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C == '/') { 11865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[-2] == '*') // We found the final */. We're done! 11875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 11881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) { 11905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) { 11915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We found the final */, though it had an escaped newline between the 11925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // * and /. We're done! 11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr[0] == '*' && CurPtr[1] != '/') { 11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this is a /* inside of the comment, emit a warning. Don't do this 11985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // if this is a /*/, which will end the comment. This misses cases with 11995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // embedded escaped newlines, but oh well. 120074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 120174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::warn_nested_block_comment); 12025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (C == 0 && CurPtr == BufferEnd+1) { 120474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 120574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(BufferPtr, diag::err_unterminated_block_comment); 12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Note: the user probably forgot a */. We could continue immediately 12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // after the /*, but this would involve lexing a lot of what really is the 12085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // comment, which surely would confuse the parser. 120931f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner --CurPtr; 12101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 121131f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // KeepWhitespaceMode should return this broken comment as a token. Since 121231f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner // it isn't a well formed comment, just return it as an 'unknown' token. 121331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner if (isKeepWhitespaceMode()) { 12149e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 121531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner return true; 121631f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner } 12171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 121831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner BufferPtr = CurPtr; 12192d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 12205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer C = *CurPtr++; 12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (PP) 12251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump PP->HandleComment(SourceRange(getSourceLocation(BufferPtr), 12262e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor getSourceLocation(CurPtr))); 12272e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 12285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are returning comments as tokens, return this comment as a token. 1229fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner if (inKeepCommentMode()) { 12309e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::comment); 12312d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return true; 12325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // It is common for the tokens immediately after a /**/ comment to be 12355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // whitespace. Instead of going through the big switch, handle it 1236d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // efficiently now. This is safe even in KeepWhitespaceMode because we would 1237d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // have already returned above with the comment as a token. 12385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (isHorizontalWhitespace(*CurPtr)) { 1239d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 12405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SkipWhitespace(Result, CurPtr+1); 12412d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 12425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Otherwise, just return so that the next character will be lexed as a token. 12455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 1246d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 12472d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return false; 12485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 12495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 12515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Lexing Entry Points 12525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 12535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ReadToEndOfLine - Read the rest of the current preprocessor line as an 12555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// uninterpreted string. This switches the lexer out of directive mode. 12565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstd::string Lexer::ReadToEndOfLine() { 12575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(ParsingPreprocessorDirective && ParsingFilename == false && 12585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer "Must be in a preprocessing directive!"); 12595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer std::string Result; 1260d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token Tmp; 12615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // CurPtr - Cache BufferPtr in an automatic variable. 12635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *CurPtr = BufferPtr; 12645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (1) { 12655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Char = getAndAdvanceChar(CurPtr, Tmp); 12665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Char) { 12675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 12685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result += Char; 12695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 12705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 0: // Null. 12715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Found end of file? 12725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr-1 != BufferEnd) { 12735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Nope, normal character, continue. 12745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result += Char; 12755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 12765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FALL THROUGH. 12785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\r': 12795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\n': 12805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Okay, we found the end of the line. First, back up past the \0, \r, \n. 12815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(CurPtr[-1] == Char && "Trigraphs for newline?"); 12825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr-1; 12831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Next, lex the character, which should handle the EOM transition. 12855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Lex(Tmp); 128622f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner assert(Tmp.is(tok::eom) && "Unexpected token!"); 12871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Finally, we're done, return the string we found. 12895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return Result; 12905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 12935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexEndOfFile - CurPtr points to the end of this file. Handle this 12955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// condition, reporting diagnostics and handling other edge cases as required. 12965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This returns true if Result contains a token, false if PP.Lex should be 12975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// called again. 1298d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) { 12995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we hit the end of the file while parsing a preprocessor directive, 13005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // end the preprocessor directive first. The next token returned will 13015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // then be the end of file. 13025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 13035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Done parsing the "line". 13045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParsingPreprocessorDirective = false; 13055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 13069e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::eom); 13071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore comment saving mode, in case it was disabled for directive. 1309f744d133e8d36eec65828d046ee695bda238125cChris Lattner SetCommentRetentionState(PP->getCommentRetentionState()); 13105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return true; // Have a token. 13111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 131286d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor 131386d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor // If we are in raw mode, return this event as an EOF token. Let the caller 131486d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor // that put us in raw mode handle the event. 131586d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor if (isLexingRawMode()) { 131686d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor Result.startToken(); 131786d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor BufferPtr = BufferEnd; 131886d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor FormTokenWithChars(Result, BufferEnd, tok::eof); 131986d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor return true; 132086d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor } 132186d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor 132286d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor // Otherwise, check if we are code-completing, then issue diagnostics for 132386d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor // unterminated #if and missing newline. 13245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 132581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor if (IsEofCodeCompletion) { 132681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor // We're at the end of the file, but we've been asked to conside the 132781b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor // end of the file to be a code-completion token. Return the 132881b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor // code-completion token. 132981b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor Result.startToken(); 133081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor FormTokenWithChars(Result, CurPtr, tok::code_completion); 133186d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor 133281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor // Only do the eof -> code_completion translation once. 133381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor IsEofCodeCompletion = false; 133481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor return true; 133581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor } 133681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor 13375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are in a #if directive, emit an error. 13385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (!ConditionalStack.empty()) { 133930c6476e8ef00caeb3a10ea0228165013a4eb3f3Chris Lattner PP->Diag(ConditionalStack.back().IfLoc, 134030c6476e8ef00caeb3a10ea0228165013a4eb3f3Chris Lattner diag::err_pp_unterminated_conditional); 13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ConditionalStack.pop_back(); 13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 13431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1344b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue 1345b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner // a pedwarn. 1346b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) 134720d0ee5d23c0f0e879ce1711d11df3118b3fb033Mike Stump Diag(BufferEnd, diag::ext_no_newline_eof) 134820d0ee5d23c0f0e879ce1711d11df3118b3fb033Mike Stump << CodeModificationHint::CreateInsertion(getSourceLocation(BufferEnd), 134920d0ee5d23c0f0e879ce1711d11df3118b3fb033Mike Stump "\n"); 13501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 13525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Finally, let the preprocessor handle this. 1354168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner return PP->HandleEndOfFile(Result); 13555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 13565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from 13585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified lexer will return a tok::l_paren token, 0 if it is something 13595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// else and 2 if there are no more tokens in the buffer controlled by the 13605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexer. 13615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerunsigned Lexer::isNextPPTokenLParen() { 13625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?"); 13631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Switch to 'skipping' mode. This will ensure that we can lex a token 13655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // without emitting diagnostics, disables macro expansion, and will cause EOF 13665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // to return an EOF token instead of popping the include stack. 13675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexingRawMode = true; 13681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Save state that can be changed while lexing so that we can restore it. 13705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *TmpBufferPtr = BufferPtr; 1371a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner bool inPPDirectiveMode = ParsingPreprocessorDirective; 13721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1373d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Token Tok; 13745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Tok.startToken(); 13755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexTokenInternal(Tok); 13761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore state that may have changed. 13785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = TmpBufferPtr; 1379a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner ParsingPreprocessorDirective = inPPDirectiveMode; 13801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore the lexer back to non-skipping mode. 13825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LexingRawMode = false; 13831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 138422f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner if (Tok.is(tok::eof)) 13855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return 2; 138622f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner return Tok.is(tok::l_paren); 13875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 13885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexTokenInternal - This implements a simple C family lexer. It is an 13915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// extremely performance critical piece of code. This assumes that the buffer 1392efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// has a null character at the end of the file. This returns a preprocessing 1393efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// token, not a normal token, as such, it is an internal interface. It assumes 1394efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// that the Flags of result have been cleared before calling this. 1395d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexTokenInternal(Token &Result) { 13965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerLexNextToken: 13975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // New token, can't need cleaning yet. 1398d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::NeedsCleaning); 13995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Result.setIdentifierInfo(0); 14001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // CurPtr - Cache BufferPtr in an automatic variable. 14025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *CurPtr = BufferPtr; 14035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Small amounts of horizontal whitespace is very common between tokens. 14055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if ((*CurPtr == ' ') || (*CurPtr == '\t')) { 14065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 14075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while ((*CurPtr == ' ') || (*CurPtr == '\t')) 14085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++CurPtr; 14091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1410d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // If we are keeping whitespace and other tokens, just return what we just 1411d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // skipped. The next lexer invocation will return the token after the 1412d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner // whitespace. 1413d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (isKeepWhitespaceMode()) { 14149e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, tok::unknown); 1415d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; 1416d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner } 14171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BufferPtr = CurPtr; 1419d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 14205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below. 14231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Read a character, advancing over it. 14255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer char Char = getAndAdvanceChar(CurPtr, Result); 14269e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner tok::TokenKind Kind; 14271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (Char) { 14295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 0: // Null. 14305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Found end of file? 14315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CurPtr-1 == BufferEnd) { 14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Read the PP instance variable into an automatic variable, because 14335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // LexEndOfFile will often delete 'this'. 1434168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner Preprocessor *PPCache = PP; 14355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. 14365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return; // Got a token to return. 1437168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); 1438168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner return PPCache->Lex(Result); 14395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 144174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 144274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::null_in_file); 1443d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 1444d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 1445d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 14461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 14485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\n': 14495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\r': 14505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If we are inside a preprocessor directive and we see the end of line, 14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // we know we are done with the directive, so return an EOM token. 14525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingPreprocessorDirective) { 14535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Done parsing the "line". 14545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ParsingPreprocessorDirective = false; 14551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Restore comment saving mode, in case it was disabled for directive. 1457f744d133e8d36eec65828d046ee695bda238125cChris Lattner SetCommentRetentionState(PP->getCommentRetentionState()); 14581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Since we consumed a newline, we are back at the start of a line. 14605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IsAtStartOfLine = true; 14611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14629e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::eom; 14635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 14645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 14655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // The returned token is at the start of the line. 1466d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 14675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // No leading whitespace seen so far. 1468d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.clearFlag(Token::LeadingSpace); 14691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1470d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 1471d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 14725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 14735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ' ': 14745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\t': 14755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\f': 14765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\v': 14778133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner SkipHorizontalWhitespace: 1478d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::LeadingSpace); 1479d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner if (SkipWhitespace(Result, CurPtr)) 1480d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner return; // KeepWhitespaceMode 14818133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner 14828133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner SkipIgnoredUnits: 14838133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner CurPtr = BufferPtr; 14841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14858133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner // If the next token is obviously a // or /* */ comment, skip it efficiently 14868133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner // too (without going through the big switch stmt). 14878402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() && 14888402c73dd880e8af46c826d873681820aebe32ebChris Lattner Features.BCPLComment) { 14898133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner SkipBCPLComment(Result, CurPtr+2); 14908133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipIgnoredUnits; 1491fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) { 14928133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner SkipBlockComment(Result, CurPtr+2); 14938133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipIgnoredUnits; 14948133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner } else if (isHorizontalWhitespace(*CurPtr)) { 14958133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner goto SkipHorizontalWhitespace; 14968133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner } 14975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14993a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // C99 6.4.4.1: Integer Constants. 15003a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // C99 6.4.4.2: Floating Constants. 15013a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '0': case '1': case '2': case '3': case '4': 15023a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '5': case '6': case '7': case '8': case '9': 15033a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Notify MIOpt that we read a non-whitespace/non-comment token. 15043a5707766850f9ee9daa35299794328b5caf96dcChris Lattner MIOpt.ReadToken(); 15053a5707766850f9ee9daa35299794328b5caf96dcChris Lattner return LexNumericConstant(Result, CurPtr); 15061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15073a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz"). 15085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 15095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 15105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 15115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Wide string literal. 15135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '"') 15145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), 15155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer true); 15165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Wide character constant. 15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '\'') 15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); 15205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FALL THROUGH, treating L like the start of an identifier. 15211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.2: Identifiers. 15235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': 15245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N': 15255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': 15265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'V': case 'W': case 'X': case 'Y': case 'Z': 15275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': 15285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': 15295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': 15305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'v': case 'w': case 'x': case 'y': case 'z': 15315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '_': 15325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 15335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 15345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexIdentifier(Result, CurPtr); 15353a5707766850f9ee9daa35299794328b5caf96dcChris Lattner 15363a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '$': // $ in identifiers. 15373a5707766850f9ee9daa35299794328b5caf96dcChris Lattner if (Features.DollarIdents) { 153874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 153974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(CurPtr-1, diag::ext_dollar_in_identifier); 15403a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Notify MIOpt that we read a non-whitespace/non-comment token. 15413a5707766850f9ee9daa35299794328b5caf96dcChris Lattner MIOpt.ReadToken(); 15423a5707766850f9ee9daa35299794328b5caf96dcChris Lattner return LexIdentifier(Result, CurPtr); 15433a5707766850f9ee9daa35299794328b5caf96dcChris Lattner } 15441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15459e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 15463a5707766850f9ee9daa35299794328b5caf96dcChris Lattner break; 15471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.4: Character Constants. 15495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\'': 15505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 15515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 15525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexCharConstant(Result, CurPtr); 15535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.5: String Literals. 15555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '"': 15565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 15575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 15585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexStringLiteral(Result, CurPtr, false); 15595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.4.6: Punctuators. 15615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '?': 15629e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::question; 15635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 15645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '[': 15659e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_square; 15665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 15675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ']': 15689e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_square; 15695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 15705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '(': 15719e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_paren; 15725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 15735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ')': 15749e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_paren; 15755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 15765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '{': 15779e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_brace; 15785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 15795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '}': 15809e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_brace; 15815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 15825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '.': 15835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 15845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char >= '0' && Char <= '9') { 15855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 15865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 15875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); 15895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Features.CPlusPlus && Char == '*') { 15909e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::periodstar; 15915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr += SizeTmp; 15925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '.' && 15935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') { 15949e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ellipsis; 15955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 15965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 15975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 15989e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::period; 15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 16015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '&': 16025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 16035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '&') { 16049e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ampamp; 16055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 16065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 16079e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::ampequal; 16085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 16095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 16109e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::amp; 16115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 16131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case '*': 16145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (getCharAndSize(CurPtr, SizeTmp) == '=') { 16159e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::starequal; 16165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 16175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 16189e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::star; 16195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 16215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '+': 16225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 16235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '+') { 16245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 16259e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plusplus; 16265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 16275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 16289e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plusequal; 16295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 16309e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::plus; 16315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 16335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '-': 16345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 16359e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner if (Char == '-') { // -- 16365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 16379e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minusminus; 16381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else if (Char == '>' && Features.CPlusPlus && 16399e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->* 16405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 16415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 16429e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::arrowstar; 16439e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner } else if (Char == '>') { // -> 16445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 16459e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::arrow; 16469e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner } else if (Char == '=') { // -= 16475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 16489e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minusequal; 16495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 16509e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::minus; 16515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 16535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '~': 16549e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::tilde; 16555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 16565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '!': 16575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (getCharAndSize(CurPtr, SizeTmp) == '=') { 16589e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::exclaimequal; 16595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 16605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 16619e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::exclaim; 16625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 16645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '/': 16655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // 6.4.9: Comments 16665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 16675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '/') { // BCPL comment. 16688402c73dd880e8af46c826d873681820aebe32ebChris Lattner // Even if BCPL comments are disabled (e.g. in C89 mode), we generally 16698402c73dd880e8af46c826d873681820aebe32ebChris Lattner // want to lex this as a comment. There is one problem with this though, 16708402c73dd880e8af46c826d873681820aebe32ebChris Lattner // that in one particular corner case, this can change the behavior of the 16718402c73dd880e8af46c826d873681820aebe32ebChris Lattner // resultant program. For example, In "foo //**/ bar", C89 would lex 16728402c73dd880e8af46c826d873681820aebe32ebChris Lattner // this as "foo / bar" and langauges with BCPL comments would lex it as 16738402c73dd880e8af46c826d873681820aebe32ebChris Lattner // "foo". Check to see if the character after the second slash is a '*'. 16748402c73dd880e8af46c826d873681820aebe32ebChris Lattner // If so, we will lex that as a "/" instead of the start of a comment. 16758402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (Features.BCPLComment || 16768402c73dd880e8af46c826d873681820aebe32ebChris Lattner getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') { 16778402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) 16788402c73dd880e8af46c826d873681820aebe32ebChris Lattner return; // KeepCommentMode 16791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16808402c73dd880e8af46c826d873681820aebe32ebChris Lattner // It is common for the tokens immediately after a // comment to be 16818402c73dd880e8af46c826d873681820aebe32ebChris Lattner // whitespace (indentation for the next line). Instead of going through 16828402c73dd880e8af46c826d873681820aebe32ebChris Lattner // the big switch, handle it efficiently now. 16838402c73dd880e8af46c826d873681820aebe32ebChris Lattner goto SkipIgnoredUnits; 16848402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 16858402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 16861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16878402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (Char == '*') { // /**/ comment. 16885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) 16892d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner return; // KeepCommentMode 16902d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner goto LexNextToken; // GCC isn't tail call eliminating. 16918402c73dd880e8af46c826d873681820aebe32ebChris Lattner } 16921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16938402c73dd880e8af46c826d873681820aebe32ebChris Lattner if (Char == '=') { 16945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 16959e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::slashequal; 16965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 16979e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::slash; 16985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 17005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '%': 17015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 17025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 17039e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::percentequal; 17045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 17055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Features.Digraphs && Char == '>') { 17069e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_brace; // '%>' -> '}' 17075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 17085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Features.Digraphs && Char == ':') { 17095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 17105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 17115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') { 17129e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashhash; // '%:%:' -> '##' 17135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 17145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 17155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize 17165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 171774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 171874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(BufferPtr, diag::charize_microsoft_ext); 17199e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashat; 1720e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner } else { // '%:' -> '#' 17215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We parsed a # character. If this occurs at the start of the line, 17225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it's actually the start of a preprocessing directive. Callback to 17235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the preprocessor to handle it. 17245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: -fpreprocessed mode?? 1725766703b76dc1eb0c49d11cafc2a17c7407876ebcChris Lattner if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) { 1726e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner FormTokenWithChars(Result, CurPtr, tok::hash); 1727168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner PP->HandleDirective(Result); 17281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // As an optimization, if the preprocessor didn't switch lexers, tail 17305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // recurse. 1731168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner if (PP->isCurrentLexer(this)) { 17325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Start a new token. If this is a #include or something, the PP may 17335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // want us starting at the beginning of the line again. If so, set 17345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the StartOfLine flag. 17355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (IsAtStartOfLine) { 1736d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 17375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IsAtStartOfLine = false; 17385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 17405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1742168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner return PP->Lex(Result); 17435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1745e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner Kind = tok::hash; 17465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 17489e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::percent; 17495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 17515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '<': 17525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 17535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ParsingFilename) { 17549cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner return LexAngledStringLiteral(Result, CurPtr); 17555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '<' && 17565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') { 17579e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::lesslessequal; 17585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 17595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 17605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '<') { 17615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 17629e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::lessless; 17635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '=') { 17645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 17659e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::lessequal; 17669e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner } else if (Features.Digraphs && Char == ':') { // '<:' -> '[' 17675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 17689e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_square; 17699e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner } else if (Features.Digraphs && Char == '%') { // '<%' -> '{' 17705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 17719e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::l_brace; 17725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 17739e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::less; 17745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 17765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '>': 17775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 17785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 17795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 17809e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::greaterequal; 17811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else if (Char == '>' && 17825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') { 17835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 17845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SizeTmp2, Result); 17859e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::greatergreaterequal; 17865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '>') { 17875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 17889e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::greatergreater; 17895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 17909e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::greater; 17915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 17925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 17935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '^': 17945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 17955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 17965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 17979e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::caretequal; 17985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 17999e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::caret; 18005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 18025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '|': 18035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 18045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 18059e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipeequal; 18065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 18075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '|') { 18089e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipepipe; 18095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 18105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 18119e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::pipe; 18125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 18145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ':': 18155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 18165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Features.Digraphs && Char == '>') { 18179e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::r_square; // ':>' -> ']' 18185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 18195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Features.CPlusPlus && Char == ':') { 18209e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::coloncolon; 18215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 18221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else { 18239e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::colon; 18245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 18265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ';': 18279e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::semi; 18285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 18295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '=': 18305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 18315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '=') { 18329e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::equalequal; 18335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 18341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else { 18359e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::equal; 18365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 18385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case ',': 18399e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::comma; 18405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 18415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '#': 18425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Char = getCharAndSize(CurPtr, SizeTmp); 18435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Char == '#') { 18449e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashhash; 18455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 18465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize 18479e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::hashat; 184874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner if (!isLexingRawMode()) 184974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner Diag(BufferPtr, diag::charize_microsoft_ext); 18505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 18515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } else { 18525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We parsed a # character. If this occurs at the start of the line, 18535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // it's actually the start of a preprocessing directive. Callback to 18545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the preprocessor to handle it. 18555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: -fpreprocessed mode?? 1856766703b76dc1eb0c49d11cafc2a17c7407876ebcChris Lattner if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) { 1857e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner FormTokenWithChars(Result, CurPtr, tok::hash); 1858168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner PP->HandleDirective(Result); 18591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // As an optimization, if the preprocessor didn't switch lexers, tail 18615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // recurse. 1862168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner if (PP->isCurrentLexer(this)) { 18635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Start a new token. If this is a #include or something, the PP may 18645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // want us starting at the beginning of the line again. If so, set 18655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // the StartOfLine flag. 18665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (IsAtStartOfLine) { 1867d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner Result.setFlag(Token::StartOfLine); 18685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer IsAtStartOfLine = false; 18695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer goto LexNextToken; // GCC isn't tail call eliminating. 18715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1872168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner return PP->Lex(Result); 18735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1875e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner Kind = tok::hash; 18765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 18785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18793a5707766850f9ee9daa35299794328b5caf96dcChris Lattner case '@': 18803a5707766850f9ee9daa35299794328b5caf96dcChris Lattner // Objective C support. 18813a5707766850f9ee9daa35299794328b5caf96dcChris Lattner if (CurPtr[-1] == '@' && Features.ObjC1) 18829e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::at; 18833a5707766850f9ee9daa35299794328b5caf96dcChris Lattner else 18849e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 18853a5707766850f9ee9daa35299794328b5caf96dcChris Lattner break; 18861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\\': 18885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FIXME: UCN's. 18895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // FALL THROUGH. 18905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer default: 18919e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner Kind = tok::unknown; 18925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 18935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 18941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Notify MIOpt that we read a non-whitespace/non-comment token. 18965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer MIOpt.ReadToken(); 18975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 18985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Update the location of token as well as BufferPtr. 18999e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner FormTokenWithChars(Result, CurPtr, Kind); 19005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1901