Lexer.cpp revision e91e93225db2e66906878513c6ef4dd6a7ee2b6a
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
42dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner/// 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
6022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattnervoid Lexer::InitLexer(const char *BufStart, const char *BufPtr,
6122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner                      const char *BufEnd) {
6222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  InitCharacterInfo();
6322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
6422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  BufferStart = BufStart;
6522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  BufferPtr = BufPtr;
6622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  BufferEnd = BufEnd;
6722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
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!");
7122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
7222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  Is_PragmaLexer = false;
7322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
7422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // Start of the file is a start of line.
7522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  IsAtStartOfLine = true;
7622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
7722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // We are not after parsing a #.
7822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  ParsingPreprocessorDirective = false;
7922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
8022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // We are not after parsing #include.
8122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  ParsingFilename = false;
8222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
8322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // We are not in raw mode.  Raw mode disables diagnostics and interpretation
8422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // of tokens (e.g. identifiers, thus disabling macro expansion).  It is used
8522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
8622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // or otherwise skipping over tokens.
8722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  LexingRawMode = false;
8822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
8922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // Default to not keeping comments.
9022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  ExtendedTokenMode = 0;
9122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner}
9222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
930770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// Lexer constructor - Create a new lexer object for the specified buffer
940770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// with the specified preprocessor managing the lexing process.  This lexer
950770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// assumes that the associated file buffer and Preprocessor objects will
960770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// outlive it, so it doesn't take ownership of either of them.
9788d3ac1341aa016cabd966c5b113a95ac05ea43fChris LattnerLexer::Lexer(FileID FID, Preprocessor &PP)
9888d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner  : PreprocessorLexer(&PP, FID),
9988d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner    FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
10088d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner    Features(PP.getLangOptions()) {
1010770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner
10288d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner  const llvm::MemoryBuffer *InputFile = PP.getSourceManager().getBuffer(FID);
1030770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner
1040770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner  InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
1050770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner            InputFile->getBufferEnd());
1060770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner
1070770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner  // Default to keeping comments if the preprocessor wants them.
1080770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner  SetCommentRetentionState(PP.getCommentRetentionState());
1090770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner}
110dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
111168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner/// Lexer constructor - Create a new raw lexer object.  This object is only
112590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner/// suitable for calls to 'LexRawToken'.  This lexer assumes that the text
113590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner/// range will outlive it, so it doesn't take ownership of it.
114168ae2d44a443da75ea85db5f3b5081eb0bce113Chris LattnerLexer::Lexer(SourceLocation fileloc, const LangOptions &features,
115de96c0f29c4cacabe6ea577c61db87c2a85aea6cChris Lattner             const char *BufStart, const char *BufPtr, const char *BufEnd)
116c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  : FileLoc(fileloc), Features(features) {
11722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
11822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  InitLexer(BufStart, BufPtr, BufEnd);
119168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner
120168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  // We *are* in raw mode.
121168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  LexingRawMode = true;
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
124025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// Lexer constructor - Create a new raw lexer object.  This object is only
125025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// suitable for calls to 'LexRawToken'.  This lexer assumes that the text
126025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// range will outlive it, so it doesn't take ownership of it.
127025c3a66402fb713c2d9bf5dc174ff264765379aChris LattnerLexer::Lexer(FileID FID, const SourceManager &SM, const LangOptions &features)
128025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner  : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
129025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner  const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
130025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner
131025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner  InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
132025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner            FromFile->getBufferEnd());
133025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner
134025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner  // We *are* in raw mode.
135025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner  LexingRawMode = true;
136025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner}
137025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner
13842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
13942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// _Pragma expansion.  This has a variety of magic semantics that this method
14042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// sets up.  It returns a new'd Lexer that must be delete'd when done.
14142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner///
14242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// On entrance to this routine, TokStartLoc is a macro location which has a
14342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// spelling loc that indicates the bytes to be lexed for the token and an
14442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// instantiation location that indicates where all lexed tokens should be
14542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// "expanded from".
14642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner///
14742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
14842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// normal lexer that remaps tokens as they fly by.  This would require making
14942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Preprocessor::Lex virtual.  Given that, we could just dump in a magic lexer
15042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// interface that could handle this stuff.  This would pull GetMappedTokenLoc
15142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// out of the critical path of the lexer!
15242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner///
153bcc2a67e5180612417727cbdd8afd0f79fdf726dChris LattnerLexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
154e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                 SourceLocation InstantiationLocStart,
155e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                 SourceLocation InstantiationLocEnd,
156bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner                                 unsigned TokLen, Preprocessor &PP) {
15742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  SourceManager &SM = PP.getSourceManager();
15842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
15942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Create the lexer as if we were going to lex the file normally.
160a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner  FileID SpellingFID = SM.getFileID(SpellingLoc);
161bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner  Lexer *L = new Lexer(SpellingFID, PP);
16242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
16342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Now that the lexer is created, change the start/end locations so that we
16442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // just lex the subsection of the file that we want.  This is lexing from a
16542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // scratch buffer.
16642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  const char *StrData = SM.getCharacterData(SpellingLoc);
16742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
16842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->BufferPtr = StrData;
16942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->BufferEnd = StrData+TokLen;
1701fa495304c81e03f07f278a47b5efe9317104aabChris Lattner  assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
17142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
17242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Set the SourceLocation with the remapping information.  This ensures that
17342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // GetMappedTokenLoc will remap the tokens as they are lexed.
174de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
175e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                         InstantiationLocStart,
176e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                         InstantiationLocEnd, TokLen);
17742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
17842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Ensure that the lexer thinks it is inside a directive, so that end \n will
17942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // return an EOM token.
18042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->ParsingPreprocessorDirective = true;
18142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
18242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // This lexer really is for _Pragma.
18342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->Is_PragmaLexer = true;
18442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  return L;
18542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner}
18642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
187168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner
1885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Stringify - Convert the specified string into a C string, with surrounding
1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ""'s, and with escaped \ and " characters.
1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstd::string Lexer::Stringify(const std::string &Str, bool Charify) {
1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  std::string Result = Str;
1925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char Quote = Charify ? '\'' : '"';
1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Result.size(); i != e; ++i) {
1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Result[i] == '\\' || Result[i] == Quote) {
1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Result.insert(Result.begin()+i, '\\');
1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++i; ++e;
1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return Result;
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
202d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// Stringify - Convert the specified string into a C string by escaping '\'
203d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// and " characters.  This does not add surrounding ""'s to the string.
204d8e3083840fef752d11ca183f42786470ed061e3Chris Lattnervoid Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
205d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner  for (unsigned i = 0, e = Str.size(); i != e; ++i) {
206d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner    if (Str[i] == '\\' || Str[i] == '"') {
207d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner      Str.insert(Str.begin()+i, '\\');
208d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner      ++i; ++e;
209d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner    }
210d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner  }
211d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner}
212d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner
2135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2149a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// MeasureTokenLength - Relex the token at the specified location and return
2159a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// its length in bytes in the input file.  If the token needs cleaning (e.g.
2169a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// includes a trigraph or an escaped newline) then this count includes bytes
2179a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// that are part of that.
2189a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattnerunsigned Lexer::MeasureTokenLength(SourceLocation Loc,
2199a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner                                   const SourceManager &SM) {
2209a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // TODO: this could be special cased for common tokens like identifiers, ')',
2219a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // etc to make this faster, if it mattered.  Just look at StrData[0] to handle
2229a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // all obviously single-char tokens.  This could use
2239a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
2249a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // something.
225de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
226de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // If this comes from a macro expansion, we really do want the macro name, not
227de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // the token this macro expanded to.
228363fdc29656cc03c1817268888f95e6343470aa8Chris Lattner  Loc = SM.getInstantiationLoc(Loc);
229363fdc29656cc03c1817268888f95e6343470aa8Chris Lattner  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
2308350394c65b81bba3986dfe44ae17423873741deChris Lattner  std::pair<const char *,const char *> Buffer = SM.getBufferData(LocInfo.first);
2318350394c65b81bba3986dfe44ae17423873741deChris Lattner  const char *StrData = Buffer.first+LocInfo.second;
2328350394c65b81bba3986dfe44ae17423873741deChris Lattner
2339a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // Create a langops struct and enable trigraphs.  This is sufficient for
2349a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // measuring tokens.
2359a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  LangOptions LangOpts;
2369a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  LangOpts.Trigraphs = true;
2379a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner
2389a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // Create a lexer starting at the beginning of this token.
239de96c0f29c4cacabe6ea577c61db87c2a85aea6cChris Lattner  Lexer TheLexer(Loc, LangOpts, Buffer.first, StrData, Buffer.second);
2409a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  Token TheTok;
241590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner  TheLexer.LexFromRawLexer(TheTok);
2429a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  return TheTok.getLength();
2439a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner}
2449a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner
2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Character information.
2475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
2485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic unsigned char CharInfo[256];
2505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum {
2525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_HORZ_WS  = 0x01,  // ' ', '\t', '\f', '\v'.  Note, no '\0'
2535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_VERT_WS  = 0x02,  // '\r', '\n'
2545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_LETTER   = 0x04,  // a-z,A-Z
2555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_NUMBER   = 0x08,  // 0-9
2565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_UNDER    = 0x10,  // _
2575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_PERIOD   = 0x20   // .
2585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
2595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic void InitCharacterInfo() {
2615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static bool isInited = false;
2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (isInited) return;
2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  isInited = true;
2645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Intiialize the CharInfo table.
2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // TODO: statically initialize this.
2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CharInfo[(int)' '] = CharInfo[(int)'\t'] =
2685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
2695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
2705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CharInfo[(int)'_'] = CHAR_UNDER;
2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CharInfo[(int)'.'] = CHAR_PERIOD;
2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 'a'; i <= 'z'; ++i)
2745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
2755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = '0'; i <= '9'; ++i)
2765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CharInfo[i] = CHAR_NUMBER;
2775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIdentifierBody - Return true if this is the body character of an
2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// identifier, which is [a-zA-Z0-9_].
2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isIdentifierBody(unsigned char c) {
28295c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
2835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isHorizontalWhitespace - Return true if this character is horizontal
2865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whitespace: ' ', '\t', '\f', '\v'.  Note that this returns false for '\0'.
2875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isHorizontalWhitespace(unsigned char c) {
28895c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
2895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isWhitespace - Return true if this character is horizontal or vertical
2925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'.  Note that this returns false
2935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// for '\0'.
2945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isWhitespace(unsigned char c) {
29595c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
2965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNumberBody - Return true if this is the body character of an
2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// preprocessing number, which is [a-zA-Z0-9_.].
3005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isNumberBody(unsigned char c) {
30195c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
30295c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser    true : false;
3035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Diagnostics forwarding code.
3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
310409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
311409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// lexer buffer was all instantiated at a single point, perform the mapping.
312409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// This is currently only used for _Pragma implementation, so it is the slow
313409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// path of the hot getSourceLocation method.  Do not allow it to be inlined.
314409a03671224d4e5bdab1594c43baf070148f830Chris Lattnerstatic SourceLocation GetMappedTokenLoc(Preprocessor &PP,
315409a03671224d4e5bdab1594c43baf070148f830Chris Lattner                                        SourceLocation FileLoc,
316de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                        unsigned CharNo,
317de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                        unsigned TokLen) DISABLE_INLINE;
318409a03671224d4e5bdab1594c43baf070148f830Chris Lattnerstatic SourceLocation GetMappedTokenLoc(Preprocessor &PP,
319409a03671224d4e5bdab1594c43baf070148f830Chris Lattner                                        SourceLocation FileLoc,
320de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                        unsigned CharNo, unsigned TokLen) {
321e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  assert(FileLoc.isMacroID() && "Must be an instantiation");
322e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner
323409a03671224d4e5bdab1594c43baf070148f830Chris Lattner  // Otherwise, we're lexing "mapped tokens".  This is used for things like
324409a03671224d4e5bdab1594c43baf070148f830Chris Lattner  // _Pragma handling.  Combine the instantiation location of FileLoc with the
325df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  // spelling location.
326e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  SourceManager &SM = PP.getSourceManager();
327409a03671224d4e5bdab1594c43baf070148f830Chris Lattner
328f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
329df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  // characters come from spelling(FileLoc)+Offset.
330e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
331bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner  SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
332e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner
333e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  // Figure out the expansion loc range, which is the range covered by the
334e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  // original _Pragma(...) sequence.
335e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  std::pair<SourceLocation,SourceLocation> II =
336e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    SM.getImmediateInstantiationRange(FileLoc);
337e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner
338e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
339409a03671224d4e5bdab1594c43baf070148f830Chris Lattner}
340409a03671224d4e5bdab1594c43baf070148f830Chris Lattner
3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSourceLocation - Return a source location identifier for the specified
3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// offset in the current file.
343de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris LattnerSourceLocation Lexer::getSourceLocation(const char *Loc,
344de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                        unsigned TokLen) const {
345448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner  assert(Loc >= BufferStart && Loc <= BufferEnd &&
3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer         "Location out of range for this buffer!");
3479dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner
3489dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  // In the normal case, we're just lexing from a simple file buffer, return
3499dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  // the file id from FileLoc with the offset specified.
350448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner  unsigned CharNo = Loc-BufferStart;
3519dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  if (FileLoc.isFileID())
352bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner    return FileLoc.getFileLocWithOffset(CharNo);
3539dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner
3542b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  // Otherwise, this is the _Pragma lexer case, which pretends that all of the
3552b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  // tokens are lexed from where the _Pragma was defined.
356168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  assert(PP && "This doesn't work on raw lexers");
357de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
3585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Diag - Forwarding function for diagnostics.  This translate a source
3615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// position in the current buffer into a SourceLocation object for rendering.
3623cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
3633692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner  return PP->Diag(getSourceLocation(Loc), DiagID);
3645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Trigraph and Escaped Newline Handling Code.
3685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
3715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
3725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char GetTrigraphCharForLetter(char Letter) {
3735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Letter) {
3745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:   return 0;
3755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '=':  return '#';
3765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ')':  return ']';
3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '(':  return '[';
3785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '!':  return '|';
3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\'': return '^';
3805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '>':  return '}';
3815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '/':  return '\\';
3825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '<':  return '{';
3835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '-':  return '~';
3845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// DecodeTrigraphChar - If the specified character is a legal trigraph when
3885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// prefixed with ??, emit a trigraph warning.  If trigraphs are enabled,
3895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the result character.  Finally, emit a warning about trigraph use
3905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whether trigraphs are enabled or not.
3915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char DecodeTrigraphChar(const char *CP, Lexer *L) {
3925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char Res = GetTrigraphCharForLetter(*CP);
3933692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner  if (!Res || !L) return Res;
3943692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner
3953692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner  if (!L->getFeatures().Trigraphs) {
39674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!L->isLexingRawMode())
39774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      L->Diag(CP-2, diag::trigraph_ignored);
3983692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner    return 0;
3995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4003692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner
40174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (!L->isLexingRawMode())
40274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
4035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return Res;
4045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
4075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// get its size, and return it.  This is tricky in several cases:
4085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   1. If currently at the start of a trigraph, we warn about the trigraph,
4095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      then either return the trigraph (skipping 3 chars) or the '?',
4105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      depending on whether trigraphs are enabled or not.
4115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   2. If this is an escaped newline (potentially with whitespace between
4125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      the backslash and newline), implicitly skip the newline and return
4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      the char after it.
4145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   3. If this is a UCN, return it.  FIXME: C++ UCN's?
4155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This handles the slow/uncommon case of the getCharAndSize method.  Here we
4175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// know that we can accumulate into Size, and that we have already incremented
4185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Ptr by Size bytes.
4195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
4215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match.
4225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
424d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner                               Token *Tok) {
4255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, look for an escaped newline.
4265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '\\') {
4275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Size;
4285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Ptr;
4295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash:
4305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Common case, backslash-char where the char is not whitespace.
4315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!isWhitespace(Ptr[0])) return '\\';
4325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // See if we have optional whitespace characters followed by a newline.
4345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    {
4355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      unsigned SizeTmp = 0;
4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      do {
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ++SizeTmp;
4385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
4395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Remember that this token needs to be cleaned.
440d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner          if (Tok) Tok->setFlag(Token::NeedsCleaning);
4415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Warn if there was whitespace between the backslash and newline.
44374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          if (SizeTmp != 1 && Tok && !isLexingRawMode())
4445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            Diag(Ptr, diag::backslash_newline_space);
4455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // If this is a \r\n or \n\r, skip the newlines.
4475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
4485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Ptr[SizeTmp-1] != Ptr[SizeTmp])
4495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            ++SizeTmp;
4505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Found backslash<whitespace><newline>.  Parse the char after it.
4525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          Size += SizeTmp;
4535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          Ptr  += SizeTmp;
4545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Use slow version to accumulate a correct size field.
4555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          return getCharAndSizeSlow(Ptr, Size, Tok);
4565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
4575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } while (isWhitespace(Ptr[SizeTmp]));
4585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
4595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is not an escaped newline, just return the slash.
4615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return '\\';
4625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a trigraph, process it.
4655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '?' && Ptr[1] == '?') {
4665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is actually a legal trigraph (not something like "??x"), emit
4675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // a trigraph warning.  If so, and if trigraphs are enabled, return it.
4685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
4695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Remember that this token needs to be cleaned.
470d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner      if (Tok) Tok->setFlag(Token::NeedsCleaning);
4715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Ptr += 3;
4735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Size += 3;
4745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '\\') goto Slash;
4755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return C;
4765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
4775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is neither, return a single character.
4805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++Size;
4815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return *Ptr;
4825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
4865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeNoWarn method.  Here we know that we can accumulate into Size,
4875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// and that we have already incremented Ptr by Size bytes.
4885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
4905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match.
4915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
4925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                     const LangOptions &Features) {
4935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, look for an escaped newline.
4945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '\\') {
4955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Size;
4965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Ptr;
4975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash:
4985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Common case, backslash-char where the char is not whitespace.
4995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!isWhitespace(Ptr[0])) return '\\';
5005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // See if we have optional whitespace characters followed by a newline.
5025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    {
5035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      unsigned SizeTmp = 0;
5045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      do {
5055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        ++SizeTmp;
5065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
5075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // If this is a \r\n or \n\r, skip the newlines.
5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
5105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              Ptr[SizeTmp-1] != Ptr[SizeTmp])
5115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            ++SizeTmp;
5125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Found backslash<whitespace><newline>.  Parse the char after it.
5145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          Size += SizeTmp;
5155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          Ptr  += SizeTmp;
5165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Use slow version to accumulate a correct size field.
5185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
5195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
5205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } while (isWhitespace(Ptr[SizeTmp]));
5215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
5225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is not an escaped newline, just return the slash.
5245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return '\\';
5255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a trigraph, process it.
5285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
5295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is actually a legal trigraph (not something like "??x"), return
5305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // it.
5315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (char C = GetTrigraphCharForLetter(Ptr[2])) {
5325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Ptr += 3;
5335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Size += 3;
5345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '\\') goto Slash;
5355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return C;
5365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
5375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is neither, return a single character.
5405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++Size;
5415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return *Ptr;
5425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
5455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper methods for lexing.
5465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
5475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
548d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
5495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
5505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned Size;
5515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned char C = *CurPtr++;
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isIdentifierBody(C)) {
5535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr++;
5545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  --CurPtr;   // Back up over the skipped character.
5565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Fast path, no $,\,? in identifier found.  '\' might be an escaped newline
5585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
5595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: UCNs.
5605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
5615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerFinishIdentifier:
5625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const char *IdStart = BufferPtr;
5639e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::identifier);
5645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we are in raw mode, return this identifier raw.  There is no need to
5665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // look up identifier information or attempt to macro expand it.
5675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (LexingRawMode) return;
5685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Fill in Result.IdentifierInfo, looking up the identifier in the
5705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // identifier table.
571d1186fa38166a581b51975f0382a45fc3a0733d0Chris Lattner    IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
5725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
573863c486fcb6162495a94fddf7ac8409de2638995Chris Lattner    // Change the kind of this identifier to the appropriate token kind, e.g.
574863c486fcb6162495a94fddf7ac8409de2638995Chris Lattner    // turning "for" into a keyword.
575863c486fcb6162495a94fddf7ac8409de2638995Chris Lattner    Result.setKind(II->getTokenID());
576863c486fcb6162495a94fddf7ac8409de2638995Chris Lattner
5775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Finally, now that we know we have an identifier, pass this off to the
5785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // preprocessor, which may macro expand it or something.
579d1186fa38166a581b51975f0382a45fc3a0733d0Chris Lattner    if (II->isHandleIdentifierCase())
5806a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner      PP->HandleIdentifier(Result);
5816a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    return;
5825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, $,\,? in identifier found.  Enter slower path.
5855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  C = getCharAndSize(CurPtr, Size);
5875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
5885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '$') {
5895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If we hit a $ and they are not supported in identifiers, we are done.
5905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (!Features.DollarIdents) goto FinishIdentifier;
5915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Otherwise, emit a diagnostic and continue.
59374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
59474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(CurPtr, diag::ext_dollar_in_identifier);
5955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, Size, Result);
5965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getCharAndSize(CurPtr, Size);
5975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      continue;
5985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
5995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Found end of identifier.
6005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      goto FinishIdentifier;
6015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
6025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this character is good, consume it.
6045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr = ConsumeChar(CurPtr, Size, Result);
6055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getCharAndSize(CurPtr, Size);
6075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (isIdentifierBody(C)) { // FIXME: UCNs.
6085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, Size, Result);
6095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getCharAndSize(CurPtr, Size);
6105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
6115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6155253c7ff266ae79308050c9f43d60dd1a67c5fb9Nate Begeman/// LexNumericConstant - Lex the remainder of a integer or floating point
6165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. From[-1] is the first character lexed.  Return the end of the
6175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant.
618d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
6195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned Size;
6205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getCharAndSize(CurPtr, Size);
6215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char PrevCh = 0;
6225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isNumberBody(C)) { // FIXME: UCNs?
6235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr = ConsumeChar(CurPtr, Size, Result);
6245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PrevCh = C;
6255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getCharAndSize(CurPtr, Size);
6265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we fell out, check for a sign, due to 1e+12.  If we have one, continue.
6295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
6305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
6315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a hex FP constant, continue.
6334984212fced6d9c6538ebb7319839105e3880b45Chris Lattner  if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
6344984212fced6d9c6538ebb7319839105e3880b45Chris Lattner      (Features.HexFloats || !Features.NoExtensions))
6355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
6365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
63847246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
6399e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
64047246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
6415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
6445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// either " or L".
645d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnervoid Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
6465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this string contain the \0 character?
6475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
6495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (C != '"') {
6505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip escaped characters.
6515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '\\') {
6525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip the escaped character.
6535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getAndAdvanceChar(CurPtr, Result);
6545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == '\n' || C == '\r' ||             // Newline.
6555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer               (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
65674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
65774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(BufferPtr, diag::err_unterminated_string);
6589e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr-1, tok::unknown);
6595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return;
6605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == 0) {
6615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      NulCharacter = CurPtr-1;
6625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
6635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
6645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If a nul character existed in the string, warn about it.
66774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
66874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_string);
6695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of the token as well as the BufferPtr instance var.
67147246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
6729e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr,
6739e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner                     Wide ? tok::wide_string_literal : tok::string_literal);
67447246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
6755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
6785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// after having lexed the '<' character.  This is used for #include filenames.
679d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
6805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this string contain the \0 character?
6815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
6835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (C != '>') {
6845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip escaped characters.
6855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '\\') {
6865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip the escaped character.
6875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getAndAdvanceChar(CurPtr, Result);
6885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == '\n' || C == '\r' ||             // Newline.
6895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer               (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
69074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
691b66158c299786042cd6277b163a2c20fd3d11da5Chris Lattner        Diag(BufferPtr, diag::err_unterminated_angled_string);
6929e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr-1, tok::unknown);
6935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return;
6945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == 0) {
6955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      NulCharacter = CurPtr-1;
6965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
6975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
6985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If a nul character existed in the string, warn about it.
70174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
70274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_string);
7035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
70547246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
7069e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
70747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
7085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexCharConstant - Lex the remainder of a character constant, after having
7125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexed either ' or L'.
713d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
7145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this character contain the \0 character?
7155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Handle the common case of 'x' and '\y' efficiently.
7175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
7185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (C == '\'') {
71974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!isLexingRawMode())
72074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      Diag(BufferPtr, diag::err_empty_character);
7219e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::unknown);
7225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return;
7235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else if (C == '\\') {
7245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip the escaped character.
7255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FIXME: UCN's.
7265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
7275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
7305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++CurPtr;
7315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
7325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Fall back on generic code for embedded nulls, newlines, wide chars.
7335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    do {
7345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip escaped characters.
7355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '\\') {
7365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Skip the escaped character.
7375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        C = getAndAdvanceChar(CurPtr, Result);
7385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else if (C == '\n' || C == '\r' ||               // Newline.
7395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                 (C == 0 && CurPtr-1 == BufferEnd)) {    // End of file.
74074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        if (!isLexingRawMode())
74174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          Diag(BufferPtr, diag::err_unterminated_char);
7429e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        FormTokenWithChars(Result, CurPtr-1, tok::unknown);
7435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return;
7445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else if (C == 0) {
7455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        NulCharacter = CurPtr-1;
7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getAndAdvanceChar(CurPtr, Result);
7485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } while (C != '\'');
7495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
75174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
75274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_char);
7535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
75547246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
7569e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::char_constant);
75747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
7585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Update BufferPtr to point to the next non-whitespace character and return.
762d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner///
763d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
764d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner///
765d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnerbool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
7665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Whitespace - Skip it, then return the token after the whitespace.
7675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned char Char = *CurPtr;  // Skip consequtive spaces efficiently.
7685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
7695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip horizontal whitespace very aggressively.
7705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (isHorizontalWhitespace(Char))
7715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Char = *++CurPtr;
7725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
773ddd3e8b90a92c39f620bda7a0945320c0b9e60dbDaniel Dunbar    // Otherwise if we have something other than whitespace, we're done.
7745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char != '\n' && Char != '\r')
7755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
7765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingPreprocessorDirective) {
7785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // End of preprocessor directive line, let LexTokenInternal handle this.
7795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      BufferPtr = CurPtr;
780d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return false;
7815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
7825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // ok, but handle newline.
7845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // The returned token is at the start of the line.
785d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::StartOfLine);
7865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // No leading whitespace seen so far.
787d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.clearFlag(Token::LeadingSpace);
7885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = *++CurPtr;
7895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this isn't immediately after a newline, there is leading space.
7925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char PrevChar = CurPtr[-1];
7935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PrevChar != '\n' && PrevChar != '\r')
794d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
7955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
796d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // If the client wants us to return whitespace, return it now.
797d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  if (isKeepWhitespaceMode()) {
7989e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::unknown);
799d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    return true;
800d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  }
801d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner
8025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
803d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  return false;
8045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// SkipBCPLComment - We have just read the // characters from input.  Skip until
8075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// we find the newline character thats terminate the comment.  Then update
8082d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner/// BufferPtr and return.  If we're in KeepCommentMode, this will form the token
8092d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner/// and return true.
810d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
8115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If BCPL comments aren't explicitly enabled for this language, emit an
8125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // extension warning.
81374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (!Features.BCPLComment && !isLexingRawMode()) {
8145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(BufferPtr, diag::ext_bcpl_comment);
8155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Mark them enabled so we only emit one warning for this translation
8175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // unit.
8185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Features.BCPLComment = true;
8195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Scan over the body of the comment.  The common case, when scanning, is that
8225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the comment contains normal ascii characters with nothing interesting in
8235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // them.  As such, optimize for this case with the inner loop.
8245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C;
8255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  do {
8265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr;
8275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FIXME: Speedup BCPL comment lexing.  Just scan for a \n or \r character.
8285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we find a \n character, scan backwards, checking to see if it's an
8295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // escaped newline, like we do for block comments.
8305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over characters in the fast loop.
8325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (C != 0 &&                // Potentially EOF.
8335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           C != '\\' &&             // Potentially escaped newline.
8345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           C != '?' &&              // Potentially trigraph.
8355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           C != '\n' && C != '\r')  // Newline or DOS-style newline.
8365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *++CurPtr;
8375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is a newline, we're done.
8395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '\n' || C == '\r')
8405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;  // Found the newline? Break out!
8415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is a hard case.  Fall back on getAndAdvanceChar to
843bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // properly decode the character.  Read it in raw mode to avoid emitting
844bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // diagnostics about things like trigraphs.  If we see an escaped newline,
845bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // we'll handle it below.
8465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const char *OldPtr = CurPtr;
847bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    bool OldRawMode = isLexingRawMode();
848bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    LexingRawMode = true;
8495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
850bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    LexingRawMode = OldRawMode;
8515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we read multiple characters, and one of those characters was a \r or
8535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n, then we had an escaped newline within the comment.  Emit diagnostic
8545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // unless the next line is also a // comment.
8555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
8565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      for (; OldPtr != CurPtr; ++OldPtr)
8575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
8585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Okay, we found a // comment that ends in a newline, if the next
8595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // line is also a // comment, but has spaces, don't emit a diagnostic.
8605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (isspace(C)) {
8615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            const char *ForwardPtr = CurPtr;
8625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            while (isspace(*ForwardPtr))  // Skip whitespace.
8635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              ++ForwardPtr;
8645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
8655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              break;
8665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
8675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
86874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          if (!isLexingRawMode())
86974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner            Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
8705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          break;
8715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
8725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
8735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr == BufferEnd+1) { --CurPtr; break; }
8755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } while (C != '\n' && C != '\r');
8765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Found but did not consume the newline.
8785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are returning comments as tokens, return this comment as a token.
880fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner  if (inKeepCommentMode())
8815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return SaveBCPLComment(Result, CurPtr);
8825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are inside a preprocessor directive and we see the end of line,
8845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // return immediately, so that the lexer can return this as an EOM token.
8855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
8865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    BufferPtr = CurPtr;
8872d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
8885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, eat the \n character.  We don't care if this is a \n\r or
8917a4f004a77421af876152281b92c4f8ea24afb2dChris Lattner  // \r\n sequence.  This is an efficiency hack (because we know the \n can't
892d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // contribute to another token), it isn't needed for correctness.  Note that
893d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // this is ok even in KeepWhitespaceMode, because we would have returned the
894d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// comment above in that mode.
8955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++CurPtr;
8965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // The next returned token is at the start of the line.
898d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.setFlag(Token::StartOfLine);
8995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // No leading whitespace seen so far.
900d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.clearFlag(Token::LeadingSpace);
9015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
9022d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return false;
9035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
9065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// an appropriate way and return it.
907d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
9089e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // If we're not in a preprocessor directive, just return the // comment
9099e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // directly.
9109e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::comment);
9115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9129e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  if (!ParsingPreprocessorDirective)
9139e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    return true;
9149e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner
9159e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // If this BCPL-style comment is in a macro definition, transmogrify it into
9169e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // a C-style block comment.
9179e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  std::string Spelling = PP->getSpelling(Result);
9189e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
9199e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Spelling[1] = '*';   // Change prefix to "/*".
9209e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Spelling += "*/";    // add suffix.
9219e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner
9229e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Result.setKind(tok::comment);
92347246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  PP->CreateString(&Spelling[0], Spelling.size(), Result,
92447246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner                   Result.getLocation());
9252d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return true;
9265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
9295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// character (either \n or \r) is part of an escaped newline sequence.  Issue a
93047a2b40b7a3d9c89d6c3d12e5a81e2498c207750Chris Lattner/// diagnostic if so.  We know that the newline is inside of a block comment.
9315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
9325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                                  Lexer *L) {
9335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
9345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Back up off the newline.
9365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  --CurPtr;
9375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a two-character newline sequence, skip the other character.
9395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
9405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n\n or \r\r -> not escaped newline.
9415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[0] == CurPtr[1])
9425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
9435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n\r or \r\n -> skip the newline.
9445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    --CurPtr;
9455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have horizontal whitespace, skip over it.  We allow whitespace
9485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // between the slash and newline.
9495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool HasSpace = false;
9505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
9515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    --CurPtr;
9525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    HasSpace = true;
9535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, we know this is an escaped newline.
9565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (*CurPtr == '\\') {
9575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[-1] != '*') return false;
9585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
9595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // It isn't a slash, is it the ?? / trigraph?
9605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
9615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr[-3] != '*')
9625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
9635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // This is the trigraph ending the comment.  Emit a stern warning!
9655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr -= 2;
9665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If no trigraphs are enabled, warn that we ignored this trigraph and
9685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // ignore this * character.
9695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!L->getFeatures().Trigraphs) {
97074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!L->isLexingRawMode())
97174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
9735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
97474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!L->isLexingRawMode())
97574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      L->Diag(CurPtr, diag::trigraph_ends_block_comment);
9765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Warn about having an escaped newline between the */ characters.
97974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (!L->isLexingRawMode())
98074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
9815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If there was space between the backslash and newline, warn about it.
98374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (HasSpace && !L->isLexingRawMode())
98474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    L->Diag(CurPtr, diag::backslash_newline_space);
9855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return true;
9875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__
9905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <emmintrin.h>
9915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__
9925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <altivec.h>
9935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#undef bool
9945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
9955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipBlockComment - We have just read the /* characters from input.  Read
9975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// until we find the */ characters that terminate the comment.  Note that we
9985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// don't bother decoding trigraphs or escaped newlines in block comments,
9995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// because they cannot cause the comment to end.  The only thing that can
10005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// happen is the comment could end with an escaped newline between the */ end
10015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// of comment.
10022d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner///
10032d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner/// If KeepCommentMode is enabled, this forms a token from the comment and
10042d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner/// returns true.
1005d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
10065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Scan one character past where we should, looking for a '/' character.  Once
10075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // we find it, check to see if it was preceeded by a *.  This common
10085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // optimization helps people who like to put a lot of * characters in their
10095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // comments.
10108146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner
10118146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // The first character we get with newlines and trigraphs skipped to handle
10128146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // the degenerate /*/ case below correctly if the * has an escaped newline
10138146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // after it.
10148146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  unsigned CharSize;
10158146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  unsigned char C = getCharAndSize(CurPtr, CharSize);
10168146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  CurPtr += CharSize;
10175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (C == 0 && CurPtr == BufferEnd+1) {
101874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!isLexingRawMode())
10190af574270d3be2b0e73a3379dfaa633746f8fc6fChris Lattner      Diag(BufferPtr, diag::err_unterminated_block_comment);
102031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    --CurPtr;
102131f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner
102231f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    // KeepWhitespaceMode should return this broken comment as a token.  Since
102331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    // it isn't a well formed comment, just return it as an 'unknown' token.
102431f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    if (isKeepWhitespaceMode()) {
10259e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr, tok::unknown);
102631f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      return true;
102731f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    }
102831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner
102931f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    BufferPtr = CurPtr;
10302d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
10315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10338146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // Check to see if the first character after the '/*' is another /.  If so,
10348146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // then this slash does not end the block comment, it is part of it.
10358146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  if (C == '/')
10368146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner    C = *CurPtr++;
10378146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner
10385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
10395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over all non-interesting characters until we find end of buffer or a
10405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // (probably ending) '/' character.
10415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr + 24 < BufferEnd) {
10425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // While not aligned to a 16-byte boundary.
10435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
10445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        C = *CurPtr++;
10455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '/') goto FoundSlash;
10475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__
10495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
10505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                     '/', '/', '/', '/', '/', '/', '/', '/');
10515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (CurPtr+16 <= BufferEnd &&
10525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
10535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 16;
10545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__
10555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      __vector unsigned char Slashes = {
10565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        '/', '/', '/', '/',  '/', '/', '/', '/',
10575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        '/', '/', '/', '/',  '/', '/', '/', '/'
10585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      };
10595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (CurPtr+16 <= BufferEnd &&
10605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
10615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 16;
10625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#else
10635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Scan for '/' quickly.  Many block comments are very large.
10645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (CurPtr[0] != '/' &&
10655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[1] != '/' &&
10665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[2] != '/' &&
10675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[3] != '/' &&
10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr+4 < BufferEnd) {
10695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 4;
10705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // It has to be one of the bytes scanned, increment to it and read one.
10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *CurPtr++;
10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
10765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Loop to scan the remainder.
10785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (C != '/' && C != '\0')
10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *CurPtr++;
10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FoundSlash:
10825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '/') {
10835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr[-2] == '*')  // We found the final */.  We're done!
10845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // We found the final */, though it had an escaped newline between the
10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // * and /.  We're done!
10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          break;
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr[0] == '*' && CurPtr[1] != '/') {
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // If this is a /* inside of the comment, emit a warning.  Don't do this
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // if this is a /*/, which will end the comment.  This misses cases with
10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // embedded escaped newlines, but oh well.
109774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        if (!isLexingRawMode())
109874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          Diag(CurPtr-1, diag::warn_nested_block_comment);
10995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == 0 && CurPtr == BufferEnd+1) {
110174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
110274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(BufferPtr, diag::err_unterminated_block_comment);
11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Note: the user probably forgot a */.  We could continue immediately
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // after the /*, but this would involve lexing a lot of what really is the
11055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // comment, which surely would confuse the parser.
110631f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      --CurPtr;
110731f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner
110831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      // KeepWhitespaceMode should return this broken comment as a token.  Since
110931f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      // it isn't a well formed comment, just return it as an 'unknown' token.
111031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      if (isKeepWhitespaceMode()) {
11119e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        FormTokenWithChars(Result, CurPtr, tok::unknown);
111231f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner        return true;
111331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      }
111431f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner
111531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      BufferPtr = CurPtr;
11162d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner      return false;
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
11185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr++;
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are returning comments as tokens, return this comment as a token.
1122fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner  if (inKeepCommentMode()) {
11239e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::comment);
11242d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return true;
11255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // It is common for the tokens immediately after a /**/ comment to be
11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // whitespace.  Instead of going through the big switch, handle it
1129d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // efficiently now.  This is safe even in KeepWhitespaceMode because we would
1130d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // have already returned above with the comment as a token.
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (isHorizontalWhitespace(*CurPtr)) {
1132d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
11335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipWhitespace(Result, CurPtr+1);
11342d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, just return so that the next character will be lexed as a token.
11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
1139d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.setFlag(Token::LeadingSpace);
11402d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return false;
11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
11445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Lexing Entry Points
11455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
11465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
11485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// uninterpreted string.  This switches the lexer out of directive mode.
11495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstd::string Lexer::ReadToEndOfLine() {
11505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParsingPreprocessorDirective && ParsingFilename == false &&
11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer         "Must be in a preprocessing directive!");
11525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  std::string Result;
1153d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Token Tmp;
11545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // CurPtr - Cache BufferPtr in an automatic variable.
11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CurPtr = BufferPtr;
11575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
11585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    char Char = getAndAdvanceChar(CurPtr, Tmp);
11595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Char) {
11605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
11615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Result += Char;
11625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
11635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case 0:  // Null.
11645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Found end of file?
11655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr-1 != BufferEnd) {
11665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Nope, normal character, continue.
11675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        Result += Char;
11685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FALL THROUGH.
11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case '\r':
11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case '\n':
11735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Okay, we found the end of the line. First, back up past the \0, \r, \n.
11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(CurPtr[-1] == Char && "Trigraphs for newline?");
11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      BufferPtr = CurPtr-1;
11765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Next, lex the character, which should handle the EOM transition.
11785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Lex(Tmp);
117922f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner      assert(Tmp.is(tok::eom) && "Unexpected token!");
11805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Finally, we're done, return the string we found.
11825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return Result;
11835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
11845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexEndOfFile - CurPtr points to the end of this file.  Handle this
11885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// condition, reporting diagnostics and handling other edge cases as required.
11895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This returns true if Result contains a token, false if PP.Lex should be
11905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// called again.
1191d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
11925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we hit the end of the file while parsing a preprocessor directive,
11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // end the preprocessor directive first.  The next token returned will
11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // then be the end of file.
11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ParsingPreprocessorDirective) {
11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Done parsing the "line".
11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParsingPreprocessorDirective = false;
11985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Update the location of token as well as BufferPtr.
11999e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::eom);
12005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Restore comment saving mode, in case it was disabled for directive.
1202f744d133e8d36eec65828d046ee695bda238125cChris Lattner    SetCommentRetentionState(PP->getCommentRetentionState());
12035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return true;  // Have a token.
12045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are in raw mode, return this event as an EOF token.  Let the caller
12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // that put us in raw mode handle the event.
120874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (isLexingRawMode()) {
12095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Result.startToken();
12105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    BufferPtr = BufferEnd;
12119e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, BufferEnd, tok::eof);
12125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return true;
12135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, issue diagnostics for unterminated #if and missing newline.
12165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are in a #if directive, emit an error.
12185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (!ConditionalStack.empty()) {
121930c6476e8ef00caeb3a10ea0228165013a4eb3f3Chris Lattner    PP->Diag(ConditionalStack.back().IfLoc,
122030c6476e8ef00caeb3a10ea0228165013a4eb3f3Chris Lattner             diag::err_pp_unterminated_conditional);
12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConditionalStack.pop_back();
12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1224b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner  // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1225b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner  // a pedwarn.
1226b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner  if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
12275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(BufferEnd, diag::ext_no_newline_eof);
12285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
12305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Finally, let the preprocessor handle this.
1232168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  return PP->HandleEndOfFile(Result);
12335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
12365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified lexer will return a tok::l_paren token, 0 if it is something
12375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// else and 2 if there are no more tokens in the buffer controlled by the
12385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexer.
12395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerunsigned Lexer::isNextPPTokenLParen() {
12405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
12415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Switch to 'skipping' mode.  This will ensure that we can lex a token
12435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // without emitting diagnostics, disables macro expansion, and will cause EOF
12445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // to return an EOF token instead of popping the include stack.
12455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexingRawMode = true;
12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Save state that can be changed while lexing so that we can restore it.
12485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *TmpBufferPtr = BufferPtr;
12495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1250d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Token Tok;
12515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Tok.startToken();
12525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexTokenInternal(Tok);
12535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Restore state that may have changed.
12555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = TmpBufferPtr;
12565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Restore the lexer back to non-skipping mode.
12585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexingRawMode = false;
12595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
126022f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner  if (Tok.is(tok::eof))
12615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return 2;
126222f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner  return Tok.is(tok::l_paren);
12635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexTokenInternal - This implements a simple C family lexer.  It is an
12675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// extremely performance critical piece of code.  This assumes that the buffer
12685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// has a null character at the end of the file.  Return true if an error
12695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// occurred and compilation should terminate, false if normal.  This returns a
12705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// preprocessing token, not a normal token, as such, it is an internal
12715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// interface.  It assumes that the Flags of result have been cleared before
12725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// calling this.
1273d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexTokenInternal(Token &Result) {
12745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerLexNextToken:
12755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // New token, can't need cleaning yet.
1276d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.clearFlag(Token::NeedsCleaning);
12775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Result.setIdentifierInfo(0);
12785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // CurPtr - Cache BufferPtr in an automatic variable.
12805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CurPtr = BufferPtr;
12815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Small amounts of horizontal whitespace is very common between tokens.
12835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
12845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++CurPtr;
12855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while ((*CurPtr == ' ') || (*CurPtr == '\t'))
12865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++CurPtr;
1287d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner
1288d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // If we are keeping whitespace and other tokens, just return what we just
1289d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // skipped.  The next lexer invocation will return the token after the
1290d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // whitespace.
1291d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (isKeepWhitespaceMode()) {
12929e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr, tok::unknown);
1293d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return;
1294d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    }
1295d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner
12965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    BufferPtr = CurPtr;
1297d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
12985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned SizeTmp, SizeTmp2;   // Temporaries for use in cases below.
13015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Read a character, advancing over it.
13035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char Char = getAndAdvanceChar(CurPtr, Result);
13049e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  tok::TokenKind Kind;
13059e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner
13065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Char) {
13075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 0:  // Null.
13085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Found end of file?
13095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr-1 == BufferEnd) {
13105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Read the PP instance variable into an automatic variable, because
13115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // LexEndOfFile will often delete 'this'.
1312168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      Preprocessor *PPCache = PP;
13135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (LexEndOfFile(Result, CurPtr-1))  // Retreat back into the file.
13145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return;   // Got a token to return.
1315168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1316168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      return PPCache->Lex(Result);
13175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
13185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
131974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!isLexingRawMode())
132074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      Diag(CurPtr-1, diag::null_in_file);
1321d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
1322d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
1323d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
1324d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner
13255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
13265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\n':
13275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\r':
13285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we are inside a preprocessor directive and we see the end of line,
13295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // we know we are done with the directive, so return an EOM token.
13305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingPreprocessorDirective) {
13315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Done parsing the "line".
13325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParsingPreprocessorDirective = false;
13335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Restore comment saving mode, in case it was disabled for directive.
1335f744d133e8d36eec65828d046ee695bda238125cChris Lattner      SetCommentRetentionState(PP->getCommentRetentionState());
13365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Since we consumed a newline, we are back at the start of a line.
13385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      IsAtStartOfLine = true;
13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13409e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::eom;
13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // The returned token is at the start of the line.
1344d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::StartOfLine);
13455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // No leading whitespace seen so far.
1346d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.clearFlag(Token::LeadingSpace);
1347d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner
1348d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
1349d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
13505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
13515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ' ':
13525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\t':
13535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\f':
13545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\v':
13558133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner  SkipHorizontalWhitespace:
1356d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
1357d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
1358d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
13598133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner
13608133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner  SkipIgnoredUnits:
13618133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    CurPtr = BufferPtr;
13628133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner
13638133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    // If the next token is obviously a // or /* */ comment, skip it efficiently
13648133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    // too (without going through the big switch stmt).
13658402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
13668402c73dd880e8af46c826d873681820aebe32ebChris Lattner        Features.BCPLComment) {
13678133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      SkipBCPLComment(Result, CurPtr+2);
13688133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipIgnoredUnits;
1369fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner    } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
13708133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      SkipBlockComment(Result, CurPtr+2);
13718133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipIgnoredUnits;
13728133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    } else if (isHorizontalWhitespace(*CurPtr)) {
13738133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipHorizontalWhitespace;
13748133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    }
13755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
13765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13773a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  // C99 6.4.4.1: Integer Constants.
13783a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  // C99 6.4.4.2: Floating Constants.
13793a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '0': case '1': case '2': case '3': case '4':
13803a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '5': case '6': case '7': case '8': case '9':
13813a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    // Notify MIOpt that we read a non-whitespace/non-comment token.
13823a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    MIOpt.ReadToken();
13833a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    return LexNumericConstant(Result, CurPtr);
13843a5707766850f9ee9daa35299794328b5caf96dcChris Lattner
13853a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case 'L':   // Identifier (Loony) or wide literal (L'x' or L"xyz").
13865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
13875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
13885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
13895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Wide string literal.
13915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '"')
13925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
13935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              true);
13945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Wide character constant.
13965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '\'')
13975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
13985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FALL THROUGH, treating L like the start of an identifier.
13995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.2: Identifiers.
14015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
14025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'H': case 'I': case 'J': case 'K':    /*'L'*/case 'M': case 'N':
14035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
14045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'V': case 'W': case 'X': case 'Y': case 'Z':
14055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
14065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
14075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
14085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'v': case 'w': case 'x': case 'y': case 'z':
14095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '_':
14105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
14115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
14125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexIdentifier(Result, CurPtr);
14133a5707766850f9ee9daa35299794328b5caf96dcChris Lattner
14143a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '$':   // $ in identifiers.
14153a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    if (Features.DollarIdents) {
141674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
141774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(CurPtr-1, diag::ext_dollar_in_identifier);
14183a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      // Notify MIOpt that we read a non-whitespace/non-comment token.
14193a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      MIOpt.ReadToken();
14203a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      return LexIdentifier(Result, CurPtr);
14213a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    }
14225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14239e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::unknown;
14243a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    break;
14255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.4: Character Constants.
14275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\'':
14285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
14295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
14305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexCharConstant(Result, CurPtr);
14315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.5: String Literals.
14335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '"':
14345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
14355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
14365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexStringLiteral(Result, CurPtr, false);
14375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.6: Punctuators.
14395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '?':
14409e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::question;
14415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
14425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '[':
14439e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_square;
14445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
14455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ']':
14469e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_square;
14475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
14485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '(':
14499e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_paren;
14505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ')':
14529e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_paren;
14535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
14545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '{':
14559e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_brace;
14565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
14575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '}':
14589e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_brace;
14595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
14605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '.':
14615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
14625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char >= '0' && Char <= '9') {
14635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Notify MIOpt that we read a non-whitespace/non-comment token.
14645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      MIOpt.ReadToken();
14655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
14675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Features.CPlusPlus && Char == '*') {
14689e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::periodstar;
14695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr += SizeTmp;
14705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '.' &&
14715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer               getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
14729e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ellipsis;
14735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
14745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                           SizeTmp2, Result);
14755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
14769e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::period;
14775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
14785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
14795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '&':
14805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
14815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '&') {
14829e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ampamp;
14835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
14845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
14859e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ampequal;
14865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
14875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
14889e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::amp;
14895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
14905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
14915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '*':
14925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (getCharAndSize(CurPtr, SizeTmp) == '=') {
14939e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::starequal;
14945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
14955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
14969e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::star;
14975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
14995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '+':
15005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
15015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '+') {
15025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
15039e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plusplus;
15045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
15055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
15069e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plusequal;
15075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
15089e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plus;
15095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
15105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
15115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '-':
15125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
15139e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    if (Char == '-') {      // --
15145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
15159e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minusminus;
15165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '>' && Features.CPlusPlus &&
15179e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner               getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {  // C++ ->*
15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                           SizeTmp2, Result);
15209e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::arrowstar;
15219e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Char == '>') {   // ->
15225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
15239e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::arrow;
15249e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Char == '=') {   // -=
15255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
15269e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minusequal;
15275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
15289e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minus;
15295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
15305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
15315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '~':
15329e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::tilde;
15335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
15345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '!':
15355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (getCharAndSize(CurPtr, SizeTmp) == '=') {
15369e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::exclaimequal;
15375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
15385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
15399e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::exclaim;
15405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
15415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
15425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '/':
15435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // 6.4.9: Comments
15445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
15455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '/') {         // BCPL comment.
15468402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
15478402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // want to lex this as a comment.  There is one problem with this though,
15488402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // that in one particular corner case, this can change the behavior of the
15498402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // resultant program.  For example, In  "foo //**/ bar", C89 would lex
15508402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // this as "foo / bar" and langauges with BCPL comments would lex it as
15518402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // "foo".  Check to see if the character after the second slash is a '*'.
15528402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // If so, we will lex that as a "/" instead of the start of a comment.
15538402c73dd880e8af46c826d873681820aebe32ebChris Lattner      if (Features.BCPLComment ||
15548402c73dd880e8af46c826d873681820aebe32ebChris Lattner          getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
15558402c73dd880e8af46c826d873681820aebe32ebChris Lattner        if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
15568402c73dd880e8af46c826d873681820aebe32ebChris Lattner          return; // KeepCommentMode
15572d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner
15588402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // It is common for the tokens immediately after a // comment to be
15598402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // whitespace (indentation for the next line).  Instead of going through
15608402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // the big switch, handle it efficiently now.
15618402c73dd880e8af46c826d873681820aebe32ebChris Lattner        goto SkipIgnoredUnits;
15628402c73dd880e8af46c826d873681820aebe32ebChris Lattner      }
15638402c73dd880e8af46c826d873681820aebe32ebChris Lattner    }
15648402c73dd880e8af46c826d873681820aebe32ebChris Lattner
15658402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (Char == '*') {  // /**/ comment.
15665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
15672d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner        return; // KeepCommentMode
15682d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner      goto LexNextToken;   // GCC isn't tail call eliminating.
15698402c73dd880e8af46c826d873681820aebe32ebChris Lattner    }
15708402c73dd880e8af46c826d873681820aebe32ebChris Lattner
15718402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (Char == '=') {
15725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
15739e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::slashequal;
15745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
15759e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::slash;
15765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
15775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
15785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '%':
15795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
15805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
15819e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::percentequal;
15825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
15835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Features.Digraphs && Char == '>') {
15849e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::r_brace;                             // '%>' -> '}'
15855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
15865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Features.Digraphs && Char == ':') {
15875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
15885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Char = getCharAndSize(CurPtr, SizeTmp);
15895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
15909e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        Kind = tok::hashhash;                          // '%:%:' -> '##'
15915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
15925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                             SizeTmp2, Result);
15935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else if (Char == '@' && Features.Microsoft) {  // %:@ -> #@ -> Charize
15945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
159574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        if (!isLexingRawMode())
159674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          Diag(BufferPtr, diag::charize_microsoft_ext);
15979e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        Kind = tok::hashat;
1598e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner      } else {                                         // '%:' -> '#'
15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // We parsed a # character.  If this occurs at the start of the line,
16005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // it's actually the start of a preprocessing directive.  Callback to
16015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // the preprocessor to handle it.
16025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // FIXME: -fpreprocessed mode??
16035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (Result.isAtStartOfLine() && !LexingRawMode) {
1604e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner          FormTokenWithChars(Result, CurPtr, tok::hash);
1605168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner          PP->HandleDirective(Result);
16065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // As an optimization, if the preprocessor didn't switch lexers, tail
16085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // recurse.
1609168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner          if (PP->isCurrentLexer(this)) {
16105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // Start a new token. If this is a #include or something, the PP may
16115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // want us starting at the beginning of the line again.  If so, set
16125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // the StartOfLine flag.
16135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (IsAtStartOfLine) {
1614d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner              Result.setFlag(Token::StartOfLine);
16155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              IsAtStartOfLine = false;
16165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            }
16175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            goto LexNextToken;   // GCC isn't tail call eliminating.
16185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
16195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1620168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner          return PP->Lex(Result);
16215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
1622e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner
1623e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner        Kind = tok::hash;
16245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
16255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
16269e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::percent;
16275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
16285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
16295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '<':
16305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
16315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingFilename) {
16325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
16335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '<' &&
16345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer               getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
16359e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::lesslessequal;
16365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
16375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                           SizeTmp2, Result);
16385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '<') {
16395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
16409e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::lessless;
16415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
16425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
16439e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::lessequal;
16449e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Features.Digraphs && Char == ':') {     // '<:' -> '['
16455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
16469e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::l_square;
16479e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Features.Digraphs && Char == '%') {     // '<%' -> '{'
16485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
16499e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::l_brace;
16505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
16519e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::less;
16525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
16535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
16545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '>':
16555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
16565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
16575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
16589e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::greaterequal;
16595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '>' &&
16605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer               getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
16615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
16625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                           SizeTmp2, Result);
16639e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::greatergreaterequal;
16645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '>') {
16655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
16669e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::greatergreater;
16675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
16689e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::greater;
16695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
16705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
16715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '^':
16725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
16735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
16745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
16759e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::caretequal;
16765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
16779e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::caret;
16785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
16795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
16805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '|':
16815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
16825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
16839e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipeequal;
16845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
16855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '|') {
16869e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipepipe;
16875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
16885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
16899e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipe;
16905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
16915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
16925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ':':
16935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
16945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Features.Digraphs && Char == '>') {
16959e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::r_square; // ':>' -> ']'
16965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
16975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Features.CPlusPlus && Char == ':') {
16989e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::coloncolon;
16995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
17005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
17019e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::colon;
17025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
17035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
17045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ';':
17059e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::semi;
17065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
17075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '=':
17085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
17095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
17109e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::equalequal;
17115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
17125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
17139e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::equal;
17145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
17155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
17165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ',':
17179e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::comma;
17185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
17195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '#':
17205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
17215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '#') {
17229e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::hashhash;
17235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
17245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '@' && Features.Microsoft) {  // #@ -> Charize
17259e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::hashat;
172674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
172774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(BufferPtr, diag::charize_microsoft_ext);
17285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
17295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
17305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // We parsed a # character.  If this occurs at the start of the line,
17315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // it's actually the start of a preprocessing directive.  Callback to
17325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // the preprocessor to handle it.
17335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FIXME: -fpreprocessed mode??
17345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (Result.isAtStartOfLine() && !LexingRawMode) {
1735e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner        FormTokenWithChars(Result, CurPtr, tok::hash);
1736168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner        PP->HandleDirective(Result);
17375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // As an optimization, if the preprocessor didn't switch lexers, tail
17395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // recurse.
1740168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner        if (PP->isCurrentLexer(this)) {
17415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Start a new token.  If this is a #include or something, the PP may
17425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // want us starting at the beginning of the line again.  If so, set
17435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // the StartOfLine flag.
17445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (IsAtStartOfLine) {
1745d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner            Result.setFlag(Token::StartOfLine);
17465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            IsAtStartOfLine = false;
17475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
17485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          goto LexNextToken;   // GCC isn't tail call eliminating.
17495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
1750168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner        return PP->Lex(Result);
17515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
1752e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner
1753e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner      Kind = tok::hash;
17545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
17555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
17565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17573a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '@':
17583a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    // Objective C support.
17593a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    if (CurPtr[-1] == '@' && Features.ObjC1)
17609e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::at;
17613a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    else
17629e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::unknown;
17633a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    break;
17643a5707766850f9ee9daa35299794328b5caf96dcChris Lattner
17655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\\':
17665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FIXME: UCN's.
17675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FALL THROUGH.
17685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
17699e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::unknown;
17705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
17715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
17725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Notify MIOpt that we read a non-whitespace/non-comment token.
17745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  MIOpt.ReadToken();
17755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
17779e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, Kind);
17785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1779