Lexer.cpp revision 1b791d6465d42a9763927be1dd8af229efcbbf5e
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
10d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner//  This file implements the Lexer and Token interfaces.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// TODO: GCC Diagnostics emitted by the lexer:
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// PEDWARN: (form feed|vertical tab) in preprocessing directive
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Universal characters, unicode, char mapping:
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// WARNING: `%.*s' is not in NFKC
195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// WARNING: `%.*s' is not in NFC
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Other:
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// TODO: Options to support:
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//    -fexec-charset,-fwide-exec-charset
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Lexer.h"
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Preprocessor.h"
29500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Lex/LexDiagnostic.h"
3055817afdf9d453a443262a733f6caf6692dca118Douglas Gregor#include "clang/Lex/CodeCompletionHandler.h"
319dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner#include "clang/Basic/SourceManager.h"
32f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor#include "llvm/ADT/StringSwitch.h"
33409a03671224d4e5bdab1594c43baf070148f830Chris Lattner#include "llvm/Support/Compiler.h"
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Support/MemoryBuffer.h"
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <cctype>
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
38a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic void InitCharacterInfo();
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
40dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===//
41dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner// Token Class Implementation
42dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===//
43dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
45dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattnerbool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
46bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor  if (IdentifierInfo *II = getIdentifierInfo())
47bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor    return II->getObjCKeywordID() == objcKey;
48bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor  return false;
49dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner}
50dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
51dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner/// getObjCKeywordID - Return the ObjC keyword kind.
52dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattnertok::ObjCKeywordKind Token::getObjCKeywordID() const {
53dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner  IdentifierInfo *specId = getIdentifierInfo();
54dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner  return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
55dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner}
56dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
5753702cd401b8fdca985aede7732c2f6a82ad9b1cChris Lattner
58dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===//
59dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner// Lexer Class Implementation
60dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===//
61dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Lexer::InitLexer(const char *BufStart, const char *BufPtr,
6322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner                      const char *BufEnd) {
64a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner  InitCharacterInfo();
651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  BufferStart = BufStart;
6722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  BufferPtr = BufPtr;
6822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  BufferEnd = BufEnd;
691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  assert(BufEnd[0] == 0 &&
7122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner         "We assume that the input buffer has a null character at the end"
7222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner         " to simplify lexing!");
731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  Is_PragmaLexer = false;
7534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  IsInConflictMarker = false;
7681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
7722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // Start of the file is a start of line.
7822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  IsAtStartOfLine = true;
791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // We are not after parsing a #.
8122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  ParsingPreprocessorDirective = false;
821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // We are not after parsing #include.
8422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  ParsingFilename = false;
851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // We are not in raw mode.  Raw mode disables diagnostics and interpretation
8722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // of tokens (e.g. identifiers, thus disabling macro expansion).  It is used
8822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
8922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // or otherwise skipping over tokens.
9022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  LexingRawMode = false;
911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // Default to not keeping comments.
9322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  ExtendedTokenMode = 0;
9422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner}
9522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
960770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// Lexer constructor - Create a new lexer object for the specified buffer
970770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// with the specified preprocessor managing the lexing process.  This lexer
980770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// assumes that the associated file buffer and Preprocessor objects will
990770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// outlive it, so it doesn't take ownership of either of them.
1006e2901407bff59aeb4cc301cc58b034723d0eb49Chris LattnerLexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP)
10188d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner  : PreprocessorLexer(&PP, FID),
10288d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner    FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
10388d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner    Features(PP.getLangOptions()) {
1041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1050770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner  InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
1060770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner            InputFile->getBufferEnd());
1071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1080770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner  // Default to keeping comments if the preprocessor wants them.
1090770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner  SetCommentRetentionState(PP.getCommentRetentionState());
1100770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner}
111dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
112168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner/// Lexer constructor - Create a new raw lexer object.  This object is only
113590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner/// suitable for calls to 'LexRawToken'.  This lexer assumes that the text
114590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner/// range will outlive it, so it doesn't take ownership of it.
115168ae2d44a443da75ea85db5f3b5081eb0bce113Chris LattnerLexer::Lexer(SourceLocation fileloc, const LangOptions &features,
116de96c0f29c4cacabe6ea577c61db87c2a85aea6cChris Lattner             const char *BufStart, const char *BufPtr, const char *BufEnd)
117c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  : FileLoc(fileloc), Features(features) {
11822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
11922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  InitLexer(BufStart, BufPtr, BufEnd);
1201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
121168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  // We *are* in raw mode.
122168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  LexingRawMode = true;
1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
125025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// Lexer constructor - Create a new raw lexer object.  This object is only
126025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// suitable for calls to 'LexRawToken'.  This lexer assumes that the text
127025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// range will outlive it, so it doesn't take ownership of it.
1286e2901407bff59aeb4cc301cc58b034723d0eb49Chris LattnerLexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile,
1296e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner             const SourceManager &SM, const LangOptions &features)
130025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner  : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
131025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner
1321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
133025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner            FromFile->getBufferEnd());
1341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
135025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner  // We *are* in raw mode.
136025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner  LexingRawMode = true;
137025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner}
138025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner
13942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
14042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// _Pragma expansion.  This has a variety of magic semantics that this method
14142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// sets up.  It returns a new'd Lexer that must be delete'd when done.
14242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner///
14342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// On entrance to this routine, TokStartLoc is a macro location which has a
14442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// spelling loc that indicates the bytes to be lexed for the token and an
14542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// instantiation location that indicates where all lexed tokens should be
14642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// "expanded from".
14742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner///
14842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
14942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// normal lexer that remaps tokens as they fly by.  This would require making
15042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Preprocessor::Lex virtual.  Given that, we could just dump in a magic lexer
15142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// interface that could handle this stuff.  This would pull GetMappedTokenLoc
15242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// out of the critical path of the lexer!
15342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner///
1541eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpLexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
155e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                 SourceLocation InstantiationLocStart,
156e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                 SourceLocation InstantiationLocEnd,
157bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner                                 unsigned TokLen, Preprocessor &PP) {
15842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  SourceManager &SM = PP.getSourceManager();
15942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
16042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Create the lexer as if we were going to lex the file normally.
161a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner  FileID SpellingFID = SM.getFileID(SpellingLoc);
1626e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner  const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID);
1636e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner  Lexer *L = new Lexer(SpellingFID, InputFile, PP);
1641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Now that the lexer is created, change the start/end locations so that we
16642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // just lex the subsection of the file that we want.  This is lexing from a
16742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // scratch buffer.
16842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  const char *StrData = SM.getCharacterData(SpellingLoc);
1691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->BufferPtr = StrData;
17142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->BufferEnd = StrData+TokLen;
1721fa495304c81e03f07f278a47b5efe9317104aabChris Lattner  assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
17342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
17442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Set the SourceLocation with the remapping information.  This ensures that
17542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // GetMappedTokenLoc will remap the tokens as they are lexed.
176de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
177e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                         InstantiationLocStart,
178e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                         InstantiationLocEnd, TokLen);
1791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Ensure that the lexer thinks it is inside a directive, so that end \n will
18142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // return an EOM token.
18242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->ParsingPreprocessorDirective = true;
1831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // This lexer really is for _Pragma.
18542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->Is_PragmaLexer = true;
18642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  return L;
18742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner}
18842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
189168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner
1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Stringify - Convert the specified string into a C string, with surrounding
1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ""'s, and with escaped \ and " characters.
1925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstd::string Lexer::Stringify(const std::string &Str, bool Charify) {
1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  std::string Result = Str;
1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char Quote = Charify ? '\'' : '"';
1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Result.size(); i != e; ++i) {
1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Result[i] == '\\' || Result[i] == Quote) {
1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Result.insert(Result.begin()+i, '\\');
1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++i; ++e;
1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return Result;
2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
204d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// Stringify - Convert the specified string into a C string by escaping '\'
205d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// and " characters.  This does not add surrounding ""'s to the string.
206d8e3083840fef752d11ca183f42786470ed061e3Chris Lattnervoid Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
207d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner  for (unsigned i = 0, e = Str.size(); i != e; ++i) {
208d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner    if (Str[i] == '\\' || Str[i] == '"') {
209d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner      Str.insert(Str.begin()+i, '\\');
210d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner      ++i; ++e;
211d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner    }
212d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner  }
213d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner}
214d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner
215b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner//===----------------------------------------------------------------------===//
216b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner// Token Spelling
217b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner//===----------------------------------------------------------------------===//
218b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
219b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// getSpelling() - Return the 'spelling' of this token.  The spelling of a
220b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// token are the characters used to represent the token in the source file
221b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// after trigraph expansion and escaped-newline folding.  In particular, this
222b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// wants to get the true, uncanonicalized, spelling of things like digraphs
223b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// UCNs, etc.
224b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattnerstd::string Lexer::getSpelling(const Token &Tok, const SourceManager &SourceMgr,
225b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner                               const LangOptions &Features, bool *Invalid) {
226b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
227b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
228b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  // If this token contains nothing interesting, return it directly.
229b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  bool CharDataInvalid = false;
230b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  const char* TokStart = SourceMgr.getCharacterData(Tok.getLocation(),
231b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner                                                    &CharDataInvalid);
232b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  if (Invalid)
233b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    *Invalid = CharDataInvalid;
234b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  if (CharDataInvalid)
235b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    return std::string();
236b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
237b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  if (!Tok.needsCleaning())
238b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    return std::string(TokStart, TokStart+Tok.getLength());
239b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
240b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  std::string Result;
241b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  Result.reserve(Tok.getLength());
242b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
243b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  // Otherwise, hard case, relex the characters into the string.
244b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
245b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner       Ptr != End; ) {
246b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    unsigned CharSize;
247b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
248b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    Ptr += CharSize;
249b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  }
250b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  assert(Result.size() != unsigned(Tok.getLength()) &&
251b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner         "NeedsCleaning flag set on something that didn't need cleaning!");
252b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  return Result;
253b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner}
254b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
255b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// getSpelling - This method is used to get the spelling of a token into a
256b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// preallocated buffer, instead of as an std::string.  The caller is required
257b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// to allocate enough space for the token, which is guaranteed to be at least
258b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// Tok.getLength() bytes long.  The actual length of the token is returned.
259b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner///
260b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// Note that this method may do two possible things: it may either fill in
261b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// the buffer specified with characters, or it may *change the input pointer*
262b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// to point to a constant buffer with the data already in it (avoiding a
263b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// copy).  The caller is not allowed to modify the returned buffer pointer
264b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// if an internal buffer is returned.
265b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattnerunsigned Lexer::getSpelling(const Token &Tok, const char *&Buffer,
266b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner                            const SourceManager &SourceMgr,
267b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner                            const LangOptions &Features, bool *Invalid) {
268b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
269c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara
270c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  const char *TokStart = 0;
271c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  // NOTE: this has to be checked *before* testing for an IdentifierInfo.
272c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  if (Tok.is(tok::raw_identifier))
273c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    TokStart = Tok.getRawIdentifierData();
274c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  else if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
275c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    // Just return the string from the identifier table, which is very quick.
276b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    Buffer = II->getNameStart();
277b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    return II->getLength();
278b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  }
279c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara
280c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  // NOTE: this can be checked even after testing for an IdentifierInfo.
281b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  if (Tok.isLiteral())
282b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    TokStart = Tok.getLiteralData();
283c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara
284b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  if (TokStart == 0) {
285c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    // Compute the start of the token in the input lexer buffer.
286b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    bool CharDataInvalid = false;
287b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    TokStart = SourceMgr.getCharacterData(Tok.getLocation(), &CharDataInvalid);
288b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    if (Invalid)
289b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner      *Invalid = CharDataInvalid;
290b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    if (CharDataInvalid) {
291b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner      Buffer = "";
292b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner      return 0;
293b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    }
294b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  }
295c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara
296b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  // If this token contains nothing interesting, return it directly.
297b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  if (!Tok.needsCleaning()) {
298b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    Buffer = TokStart;
299b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    return Tok.getLength();
300b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  }
301c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara
302b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  // Otherwise, hard case, relex the characters into the string.
303b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  char *OutBuf = const_cast<char*>(Buffer);
304b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
305b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner       Ptr != End; ) {
306b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    unsigned CharSize;
307b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
308b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    Ptr += CharSize;
309b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  }
310b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
311b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner         "NeedsCleaning flag set on something that didn't need cleaning!");
312c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara
313b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  return OutBuf-Buffer;
314b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner}
315b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
316b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
317b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
31833e9abd21083a0191a7676a04b497006d2da184dDouglas Gregorstatic bool isWhitespace(unsigned char c);
3195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3209a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// MeasureTokenLength - Relex the token at the specified location and return
3219a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// its length in bytes in the input file.  If the token needs cleaning (e.g.
3229a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// includes a trigraph or an escaped newline) then this count includes bytes
3239a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// that are part of that.
3249a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattnerunsigned Lexer::MeasureTokenLength(SourceLocation Loc,
3252c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner                                   const SourceManager &SM,
3262c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner                                   const LangOptions &LangOpts) {
3279a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // TODO: this could be special cased for common tokens like identifiers, ')',
3289a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // etc to make this faster, if it mattered.  Just look at StrData[0] to handle
3291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // all obviously single-char tokens.  This could use
3309a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
3319a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // something.
332de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
333de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // If this comes from a macro expansion, we really do want the macro name, not
334de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // the token this macro expanded to.
335363fdc29656cc03c1817268888f95e6343470aa8Chris Lattner  Loc = SM.getInstantiationLoc(Loc);
336363fdc29656cc03c1817268888f95e6343470aa8Chris Lattner  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
337f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  bool Invalid = false;
338f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer  llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
339f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  if (Invalid)
340aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregor    return 0;
341f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer
342f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer  const char *StrData = Buffer.data()+LocInfo.second;
3438350394c65b81bba3986dfe44ae17423873741deChris Lattner
34433e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  if (isWhitespace(StrData[0]))
34533e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    return 0;
34633e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor
3479a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // Create a lexer starting at the beginning of this token.
348c3526d89ef9c31639ec8b25180cfb22354344241Sebastian Redl  Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts,
349c3526d89ef9c31639ec8b25180cfb22354344241Sebastian Redl                 Buffer.begin(), StrData, Buffer.end());
35039de7409bffb6b725a8aa64f0ba77ab51e8c9eb3Chris Lattner  TheLexer.SetCommentRetentionState(true);
3519a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  Token TheTok;
352590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner  TheLexer.LexFromRawLexer(TheTok);
3539a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  return TheTok.getLength();
3549a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner}
3559a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner
356a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas GregorSourceLocation Lexer::GetBeginningOfToken(SourceLocation Loc,
357a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor                                          const SourceManager &SM,
358a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor                                          const LangOptions &LangOpts) {
359a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
3603de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor  if (LocInfo.first.isInvalid())
3613de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor    return Loc;
3623de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor
363a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  bool Invalid = false;
364a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
365a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  if (Invalid)
366a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    return Loc;
367a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
368a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  // Back up from the current location until we hit the beginning of a line
369a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  // (or the buffer). We'll relex from that point.
370a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  const char *BufStart = Buffer.data();
3713de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor  if (LocInfo.second >= Buffer.size())
3723de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor    return Loc;
3733de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor
374a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  const char *StrData = BufStart+LocInfo.second;
375a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  if (StrData[0] == '\n' || StrData[0] == '\r')
376a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    return Loc;
377a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
378a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  const char *LexStart = StrData;
379a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  while (LexStart != BufStart) {
380a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    if (LexStart[0] == '\n' || LexStart[0] == '\r') {
381a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      ++LexStart;
382a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      break;
383a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    }
384a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
385a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    --LexStart;
386a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  }
387a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
388a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  // Create a lexer starting at the beginning of this token.
389a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  SourceLocation LexerStartLoc = Loc.getFileLocWithOffset(-LocInfo.second);
390a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  Lexer TheLexer(LexerStartLoc, LangOpts, BufStart, LexStart, Buffer.end());
391a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  TheLexer.SetCommentRetentionState(true);
392a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
393a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  // Lex tokens until we find the token that contains the source location.
394a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  Token TheTok;
395a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  do {
396a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    TheLexer.LexFromRawLexer(TheTok);
397a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
398a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    if (TheLexer.getBufferLocation() > StrData) {
399a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      // Lexing this token has taken the lexer past the source location we're
400a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      // looking for. If the current token encompasses our source location,
401a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      // return the beginning of that token.
402a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      if (TheLexer.getBufferLocation() - TheTok.getLength() <= StrData)
403a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor        return TheTok.getLocation();
404a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
405a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      // We ended up skipping over the source location entirely, which means
406a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      // that it points into whitespace. We're done here.
407a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      break;
408a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    }
409a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  } while (TheTok.getKind() != tok::eof);
410a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
411a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  // We've passed our source location; just return the original source location.
412a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  return Loc;
413a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor}
414a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
415f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregornamespace {
416f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  enum PreambleDirectiveKind {
417f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    PDK_Skipped,
418f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    PDK_StartIf,
419f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    PDK_EndIf,
420f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    PDK_Unknown
421f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  };
422f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor}
423f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
424f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregorstd::pair<unsigned, bool>
425df95a13ec73d2cdaea79555cb412d767f4963120Douglas GregorLexer::ComputePreamble(const llvm::MemoryBuffer *Buffer, unsigned MaxLines) {
426f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  // Create a lexer starting at the beginning of the file. Note that we use a
427f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  // "fake" file source location at offset 1 so that the lexer will track our
428f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  // position within the file.
429f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  const unsigned StartOffset = 1;
430f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  SourceLocation StartLoc = SourceLocation::getFromRawEncoding(StartOffset);
431f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  LangOptions LangOpts;
432f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  Lexer TheLexer(StartLoc, LangOpts, Buffer->getBufferStart(),
433f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor                 Buffer->getBufferStart(), Buffer->getBufferEnd());
434f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
435f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  bool InPreprocessorDirective = false;
436f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  Token TheTok;
437f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  Token IfStartTok;
438f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  unsigned IfCount = 0;
439df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  unsigned Line = 0;
440df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
441f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  do {
442f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    TheLexer.LexFromRawLexer(TheTok);
443f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
444f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    if (InPreprocessorDirective) {
445f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // If we've hit the end of the file, we're done.
446f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      if (TheTok.getKind() == tok::eof) {
447f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        InPreprocessorDirective = false;
448f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        break;
449f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      }
450f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
451f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // If we haven't hit the end of the preprocessor directive, skip this
452f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // token.
453f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      if (!TheTok.isAtStartOfLine())
454f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        continue;
455f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
456f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // We've passed the end of the preprocessor directive, and will look
457f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // at this token again below.
458f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      InPreprocessorDirective = false;
459f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    }
460f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
461df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // Keep track of the # of lines in the preamble.
462df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    if (TheTok.isAtStartOfLine()) {
463df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor      ++Line;
464df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
465df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor      // If we were asked to limit the number of lines in the preamble,
466df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor      // and we're about to exceed that limit, we're done.
467df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor      if (MaxLines && Line >= MaxLines)
468df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor        break;
469df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    }
470df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
471f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    // Comments are okay; skip over them.
472f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    if (TheTok.getKind() == tok::comment)
473f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      continue;
474f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
475f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) {
476f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // This is the start of a preprocessor directive.
477f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      Token HashTok = TheTok;
478f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      InPreprocessorDirective = true;
479f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
480f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // Figure out which direective this is. Since we're lexing raw tokens,
481f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // we don't have an identifier table available. Instead, just look at
482f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // the raw identifier to recognize and categorize preprocessor directives.
483f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      TheLexer.LexFromRawLexer(TheTok);
484c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara      if (TheTok.getKind() == tok::raw_identifier && !TheTok.needsCleaning()) {
485c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara        llvm::StringRef Keyword(TheTok.getRawIdentifierData(),
486c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara                                TheTok.getLength());
487f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        PreambleDirectiveKind PDK
488f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          = llvm::StringSwitch<PreambleDirectiveKind>(Keyword)
489f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("include", PDK_Skipped)
490f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("__include_macros", PDK_Skipped)
491f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("define", PDK_Skipped)
492f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("undef", PDK_Skipped)
493f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("line", PDK_Skipped)
494f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("error", PDK_Skipped)
495f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("pragma", PDK_Skipped)
496f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("import", PDK_Skipped)
497f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("include_next", PDK_Skipped)
498f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("warning", PDK_Skipped)
499f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("ident", PDK_Skipped)
500f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("sccs", PDK_Skipped)
501f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("assert", PDK_Skipped)
502f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("unassert", PDK_Skipped)
503f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("if", PDK_StartIf)
504f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("ifdef", PDK_StartIf)
505f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("ifndef", PDK_StartIf)
506f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("elif", PDK_Skipped)
507f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("else", PDK_Skipped)
508f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("endif", PDK_EndIf)
509f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Default(PDK_Unknown);
510f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
511f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        switch (PDK) {
512f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_Skipped:
513f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          continue;
514f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
515f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_StartIf:
516f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          if (IfCount == 0)
517f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor            IfStartTok = HashTok;
518f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
519f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          ++IfCount;
520f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          continue;
521f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
522f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_EndIf:
523f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          // Mismatched #endif. The preamble ends here.
524f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          if (IfCount == 0)
525f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor            break;
526f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
527f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          --IfCount;
528f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          continue;
529f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
530f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_Unknown:
531f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          // We don't know what this directive is; stop at the '#'.
532f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          break;
533f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        }
534f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      }
535f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
536f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // We only end up here if we didn't recognize the preprocessor
537f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // directive or it was one that can't occur in the preamble at this
538f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // point. Roll back the current token to the location of the '#'.
539f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      InPreprocessorDirective = false;
540f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      TheTok = HashTok;
541f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    }
542f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
543df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // We hit a token that we don't recognize as being in the
544df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // "preprocessing only" part of the file, so we're no longer in
545df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // the preamble.
546f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    break;
547f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  } while (true);
548f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
549f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation();
550f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(),
551f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                        IfCount? IfStartTok.isAtStartOfLine()
552f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                               : TheTok.isAtStartOfLine());
553f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor}
554f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
5557ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
5567ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// AdvanceToTokenCharacter - Given a location that specifies the start of a
5577ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token, return a new location that specifies a character within the token.
5587ef5c27eb6e8ebe58b52013246c06753c3613263Chris LattnerSourceLocation Lexer::AdvanceToTokenCharacter(SourceLocation TokStart,
5597ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner                                              unsigned CharNo,
5607ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner                                              const SourceManager &SM,
5617ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner                                              const LangOptions &Features) {
5627ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // Figure out how many physical characters away the specified instantiation
5637ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // character is.  This needs to take into consideration newlines and
5647ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // trigraphs.
5657ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  bool Invalid = false;
5667ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  const char *TokPtr = SM.getCharacterData(TokStart, &Invalid);
5677ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
5687ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // If they request the first char of the token, we're trivially done.
5697ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  if (Invalid || (CharNo == 0 && Lexer::isObviouslySimpleCharacter(*TokPtr)))
5707ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    return TokStart;
5717ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
5727ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  unsigned PhysOffset = 0;
5737ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
5747ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // The usual case is that tokens don't contain anything interesting.  Skip
5757ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // over the uninteresting characters.  If a token only consists of simple
5767ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // chars, this method is extremely fast.
5777ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  while (Lexer::isObviouslySimpleCharacter(*TokPtr)) {
5787ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    if (CharNo == 0)
5797ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner      return TokStart.getFileLocWithOffset(PhysOffset);
5807ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    ++TokPtr, --CharNo, ++PhysOffset;
5817ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  }
5827ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
5837ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // If we have a character that may be a trigraph or escaped newline, use a
5847ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // lexer to parse it correctly.
5857ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  for (; CharNo; --CharNo) {
5867ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    unsigned Size;
5877ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    Lexer::getCharAndSizeNoWarn(TokPtr, Size, Features);
5887ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    TokPtr += Size;
5897ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    PhysOffset += Size;
5907ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  }
5917ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
5927ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // Final detail: if we end up on an escaped newline, we want to return the
5937ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // location of the actual byte of the token.  For example foo\<newline>bar
5947ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // advanced by 3 should return the location of b, not of \\.  One compounding
5957ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // detail of this is that the escape may be made by a trigraph.
5967ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  if (!Lexer::isObviouslySimpleCharacter(*TokPtr))
5977ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    PhysOffset += Lexer::SkipEscapedNewLines(TokPtr)-TokPtr;
5987ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
5997ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  return TokStart.getFileLocWithOffset(PhysOffset);
6007ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner}
6017ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6027ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// \brief Computes the source location just past the end of the
6037ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token at this source location.
6047ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner///
6057ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// This routine can be used to produce a source location that
6067ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// points just past the end of the token referenced by \p Loc, and
6077ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// is generally used when a diagnostic needs to point just after a
6087ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token where it expected something different that it received. If
6097ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// the returned source location would not be meaningful (e.g., if
6107ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// it points into a macro), this routine returns an invalid
6117ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// source location.
6127ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner///
6137ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// \param Offset an offset from the end of the token, where the source
6147ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// location should refer to. The default offset (0) produces a source
6157ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// location pointing just past the end of the token; an offset of 1 produces
6167ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// a source location pointing to the last character in the token, etc.
6177ef5c27eb6e8ebe58b52013246c06753c3613263Chris LattnerSourceLocation Lexer::getLocForEndOfToken(SourceLocation Loc, unsigned Offset,
6187ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner                                          const SourceManager &SM,
6197ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner                                          const LangOptions &Features) {
6207ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  if (Loc.isInvalid() || !Loc.isFileID())
6217ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    return SourceLocation();
6227ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6237ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  unsigned Len = Lexer::MeasureTokenLength(Loc, SM, Features);
6247ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  if (Len > Offset)
6257ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    Len = Len - Offset;
6267ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  else
6277ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    return Loc;
6287ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6297ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  return AdvanceToTokenCharacter(Loc, Len, SM, Features);
6307ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner}
6317ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
6335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Character information.
6345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
6355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum {
6375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_HORZ_WS  = 0x01,  // ' ', '\t', '\f', '\v'.  Note, no '\0'
6385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_VERT_WS  = 0x02,  // '\r', '\n'
6395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_LETTER   = 0x04,  // a-z,A-Z
6405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_NUMBER   = 0x08,  // 0-9
6415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_UNDER    = 0x10,  // _
6425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_PERIOD   = 0x20   // .
6435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
6445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
64503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// Statically initialize CharInfo table based on ASCII character set
64603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// Reference: FreeBSD 7.2 /usr/share/misc/ascii
647a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic const unsigned char CharInfo[256] =
64803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner{
64903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 0 NUL         1 SOH         2 STX         3 ETX
65003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 4 EOT         5 ENQ         6 ACK         7 BEL
65103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
65203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
65303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 8 BS          9 HT         10 NL         11 VT
65403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//12 NP         13 CR         14 SO         15 SI
65503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
65603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_HORZ_WS, CHAR_VERT_WS, 0           , 0           ,
65703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//16 DLE        17 DC1        18 DC2        19 DC3
65803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//20 DC4        21 NAK        22 SYN        23 ETB
65903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
66003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
66103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//24 CAN        25 EM         26 SUB        27 ESC
66203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//28 FS         29 GS         30 RS         31 US
66303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
66403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
66503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//32 SP         33  !         34  "         35  #
66603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//36  $         37  %         38  &         39  '
66703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_HORZ_WS, 0           , 0           , 0           ,
66803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
66903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//40  (         41  )         42  *         43  +
67003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//44  ,         45  -         46  .         47  /
67103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
67203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , CHAR_PERIOD , 0           ,
67303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//48  0         49  1         50  2         51  3
67403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//52  4         53  5         54  6         55  7
67503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
67603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
67703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//56  8         57  9         58  :         59  ;
67803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//60  <         61  =         62  >         63  ?
67903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_NUMBER , CHAR_NUMBER , 0           , 0           ,
68003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
68103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//64  @         65  A         66  B         67  C
68203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//68  D         69  E         70  F         71  G
68303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
68403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
68503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//72  H         73  I         74  J         75  K
68603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//76  L         77  M         78  N         79  O
68703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
68803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
68903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//80  P         81  Q         82  R         83  S
69003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//84  T         85  U         86  V         87  W
69103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
69203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
69303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//88  X         89  Y         90  Z         91  [
69403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//92  \         93  ]         94  ^         95  _
69503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0           ,
69603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , CHAR_UNDER  ,
69703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//96  `         97  a         98  b         99  c
69803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//100  d       101  e        102  f        103  g
69903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
70003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
70103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//104  h       105  i        106  j        107  k
70203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//108  l       109  m        110  n        111  o
70303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
70403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
70503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//112  p       113  q        114  r        115  s
70603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//116  t       117  u        118  v        119  w
70703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
70803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
70903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//120  x       121  y        122  z        123  {
71003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//124  |        125  }        126  ~        127 DEL
71103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0           ,
71203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0
71303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner};
71403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner
715a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic void InitCharacterInfo() {
7165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static bool isInited = false;
7175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (isInited) return;
71803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  // check the statically-initialized CharInfo table
71903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
72003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
72103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
72203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
72303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
72403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
72503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_UNDER   == CharInfo[(int)'_']);
72603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_PERIOD  == CharInfo[(int)'.']);
72703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  for (unsigned i = 'a'; i <= 'z'; ++i) {
72803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner    assert(CHAR_LETTER == CharInfo[i]);
72903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner    assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
73003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  }
7315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = '0'; i <= '9'; ++i)
73203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner    assert(CHAR_NUMBER == CharInfo[i]);
7337b682656310b0d7e94a98d982444bc7ddedc653fSteve Naroff
73403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  isInited = true;
7355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
73703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner
7385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIdentifierBody - Return true if this is the body character of an
7395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// identifier, which is [a-zA-Z0-9_].
7405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isIdentifierBody(unsigned char c) {
74195c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
7425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isHorizontalWhitespace - Return true if this character is horizontal
7455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whitespace: ' ', '\t', '\f', '\v'.  Note that this returns false for '\0'.
7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isHorizontalWhitespace(unsigned char c) {
74795c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
7485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isWhitespace - Return true if this character is horizontal or vertical
7515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'.  Note that this returns false
7525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// for '\0'.
7535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isWhitespace(unsigned char c) {
75495c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
7555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNumberBody - Return true if this is the body character of an
7585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// preprocessing number, which is [a-zA-Z0-9_.].
7595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isNumberBody(unsigned char c) {
7601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
76195c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser    true : false;
7625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
7665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Diagnostics forwarding code.
7675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
7685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
769409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
770409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// lexer buffer was all instantiated at a single point, perform the mapping.
771409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// This is currently only used for _Pragma implementation, so it is the slow
772409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// path of the hot getSourceLocation method.  Do not allow it to be inlined.
77314bd96571ef6f0e97dc79ec4d01b547d60e8fa68Chandler Carruthstatic LLVM_ATTRIBUTE_NOINLINE SourceLocation GetMappedTokenLoc(
77414bd96571ef6f0e97dc79ec4d01b547d60e8fa68Chandler Carruth    Preprocessor &PP, SourceLocation FileLoc, unsigned CharNo, unsigned TokLen);
775409a03671224d4e5bdab1594c43baf070148f830Chris Lattnerstatic SourceLocation GetMappedTokenLoc(Preprocessor &PP,
776409a03671224d4e5bdab1594c43baf070148f830Chris Lattner                                        SourceLocation FileLoc,
777de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                        unsigned CharNo, unsigned TokLen) {
778e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  assert(FileLoc.isMacroID() && "Must be an instantiation");
7791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
780409a03671224d4e5bdab1594c43baf070148f830Chris Lattner  // Otherwise, we're lexing "mapped tokens".  This is used for things like
781409a03671224d4e5bdab1594c43baf070148f830Chris Lattner  // _Pragma handling.  Combine the instantiation location of FileLoc with the
782df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  // spelling location.
783e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  SourceManager &SM = PP.getSourceManager();
7841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
785f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
786df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  // characters come from spelling(FileLoc)+Offset.
787e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
788bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner  SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
7891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
790e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  // Figure out the expansion loc range, which is the range covered by the
791e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  // original _Pragma(...) sequence.
792e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  std::pair<SourceLocation,SourceLocation> II =
793e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    SM.getImmediateInstantiationRange(FileLoc);
7941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
795e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
796409a03671224d4e5bdab1594c43baf070148f830Chris Lattner}
797409a03671224d4e5bdab1594c43baf070148f830Chris Lattner
7985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSourceLocation - Return a source location identifier for the specified
7995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// offset in the current file.
800de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris LattnerSourceLocation Lexer::getSourceLocation(const char *Loc,
801de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                        unsigned TokLen) const {
802448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner  assert(Loc >= BufferStart && Loc <= BufferEnd &&
8035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer         "Location out of range for this buffer!");
8049dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner
8059dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  // In the normal case, we're just lexing from a simple file buffer, return
8069dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  // the file id from FileLoc with the offset specified.
807448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner  unsigned CharNo = Loc-BufferStart;
8089dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  if (FileLoc.isFileID())
809bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner    return FileLoc.getFileLocWithOffset(CharNo);
8101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8112b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  // Otherwise, this is the _Pragma lexer case, which pretends that all of the
8122b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  // tokens are lexed from where the _Pragma was defined.
813168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  assert(PP && "This doesn't work on raw lexers");
814de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
8155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Diag - Forwarding function for diagnostics.  This translate a source
8185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// position in the current buffer into a SourceLocation object for rendering.
8193cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
8203692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner  return PP->Diag(getSourceLocation(Loc), DiagID);
8215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
8245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Trigraph and Escaped Newline Handling Code.
8255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
8265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
8285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
8295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char GetTrigraphCharForLetter(char Letter) {
8305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Letter) {
8315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:   return 0;
8325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '=':  return '#';
8335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ')':  return ']';
8345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '(':  return '[';
8355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '!':  return '|';
8365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\'': return '^';
8375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '>':  return '}';
8385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '/':  return '\\';
8395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '<':  return '{';
8405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '-':  return '~';
8415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// DecodeTrigraphChar - If the specified character is a legal trigraph when
8455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// prefixed with ??, emit a trigraph warning.  If trigraphs are enabled,
8465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the result character.  Finally, emit a warning about trigraph use
8475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whether trigraphs are enabled or not.
8485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char DecodeTrigraphChar(const char *CP, Lexer *L) {
8495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char Res = GetTrigraphCharForLetter(*CP);
8503692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner  if (!Res || !L) return Res;
8511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8523692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner  if (!L->getFeatures().Trigraphs) {
85374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!L->isLexingRawMode())
85474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      L->Diag(CP-2, diag::trigraph_ignored);
8553692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner    return 0;
8565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (!L->isLexingRawMode())
859476d8b863cb65b2b5833235d97315cdb46e6f5aaBenjamin Kramer    L->Diag(CP-2, diag::trigraph_converted) << llvm::StringRef(&Res, 1);
8605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return Res;
8615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
86324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// getEscapedNewLineSize - Return the size of the specified escaped newline,
86424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a
8651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// trigraph equivalent on entry to this function.
86624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattnerunsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
86724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  unsigned Size = 0;
86824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  while (isWhitespace(Ptr[Size])) {
86924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    ++Size;
8701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
87224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      continue;
87324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
87424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    // If this is a \r\n or \n\r, skip the other half.
87524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
87624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner        Ptr[Size-1] != Ptr[Size])
87724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      ++Size;
8781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    return Size;
8801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
8811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
88224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  // Not an escaped newline, must be a \t or something else.
88324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  return 0;
88424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner}
88524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
886033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
887033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// them), skip over them and return the first non-escaped-newline found,
888033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// otherwise return P.
889033749571f8d4c804eeb357c70b06424aa24503bChris Lattnerconst char *Lexer::SkipEscapedNewLines(const char *P) {
890033749571f8d4c804eeb357c70b06424aa24503bChris Lattner  while (1) {
891033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    const char *AfterEscape;
892033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    if (*P == '\\') {
893033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      AfterEscape = P+1;
894033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    } else if (*P == '?') {
895033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      // If not a trigraph for escape, bail out.
896033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      if (P[1] != '?' || P[2] != '/')
897033749571f8d4c804eeb357c70b06424aa24503bChris Lattner        return P;
898033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      AfterEscape = P+3;
899033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    } else {
900033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      return P;
901033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    }
9021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
903033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
904033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    if (NewLineSize == 0) return P;
905033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    P = AfterEscape+NewLineSize;
906033749571f8d4c804eeb357c70b06424aa24503bChris Lattner  }
907033749571f8d4c804eeb357c70b06424aa24503bChris Lattner}
908033749571f8d4c804eeb357c70b06424aa24503bChris Lattner
90924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
9105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
9115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// get its size, and return it.  This is tricky in several cases:
9125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   1. If currently at the start of a trigraph, we warn about the trigraph,
9135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      then either return the trigraph (skipping 3 chars) or the '?',
9145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      depending on whether trigraphs are enabled or not.
9155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   2. If this is an escaped newline (potentially with whitespace between
9165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      the backslash and newline), implicitly skip the newline and return
9175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      the char after it.
9185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   3. If this is a UCN, return it.  FIXME: C++ UCN's?
9195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
9205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This handles the slow/uncommon case of the getCharAndSize method.  Here we
9215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// know that we can accumulate into Size, and that we have already incremented
9225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Ptr by Size bytes.
9235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
9245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
9255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match.
9265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
9275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
928d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner                               Token *Tok) {
9295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, look for an escaped newline.
9305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '\\') {
9315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Size;
9325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Ptr;
9335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash:
9345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Common case, backslash-char where the char is not whitespace.
9355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!isWhitespace(Ptr[0])) return '\\';
9361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9375636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner    // See if we have optional whitespace characters between the slash and
9385636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner    // newline.
93924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
94024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Remember that this token needs to be cleaned.
94124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      if (Tok) Tok->setFlag(Token::NeedsCleaning);
94224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
94324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Warn if there was whitespace between the backslash and newline.
9445636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner      if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
94524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner        Diag(Ptr, diag::backslash_newline_space);
9461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
94724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Found backslash<whitespace><newline>.  Parse the char after it.
94824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Size += EscapedNewLineSize;
94924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Ptr  += EscapedNewLineSize;
95024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Use slow version to accumulate a correct size field.
95124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      return getCharAndSizeSlow(Ptr, Size, Tok);
95224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    }
9531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is not an escaped newline, just return the slash.
9555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return '\\';
9565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a trigraph, process it.
9595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '?' && Ptr[1] == '?') {
9605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is actually a legal trigraph (not something like "??x"), emit
9615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // a trigraph warning.  If so, and if trigraphs are enabled, return it.
9625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
9635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Remember that this token needs to be cleaned.
964d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner      if (Tok) Tok->setFlag(Token::NeedsCleaning);
9655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Ptr += 3;
9675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Size += 3;
9685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '\\') goto Slash;
9695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return C;
9705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
9715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is neither, return a single character.
9745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++Size;
9755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return *Ptr;
9765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
9805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeNoWarn method.  Here we know that we can accumulate into Size,
9815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// and that we have already incremented Ptr by Size bytes.
9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
9835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
9845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match.
9855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
9865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                     const LangOptions &Features) {
9875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, look for an escaped newline.
9885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '\\') {
9895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Size;
9905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Ptr;
9915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash:
9925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Common case, backslash-char where the char is not whitespace.
9935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!isWhitespace(Ptr[0])) return '\\';
9941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // See if we have optional whitespace characters followed by a newline.
99624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
99724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Found backslash<whitespace><newline>.  Parse the char after it.
99824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Size += EscapedNewLineSize;
99924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Ptr  += EscapedNewLineSize;
10001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
100124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Use slow version to accumulate a correct size field.
100224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
100324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    }
10041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is not an escaped newline, just return the slash.
10065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return '\\';
10075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a trigraph, process it.
10105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
10115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is actually a legal trigraph (not something like "??x"), return
10125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // it.
10135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (char C = GetTrigraphCharForLetter(Ptr[2])) {
10145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Ptr += 3;
10155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Size += 3;
10165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '\\') goto Slash;
10175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return C;
10185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
10195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is neither, return a single character.
10225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++Size;
10235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return *Ptr;
10245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
10275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper methods for lexing.
10285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
10295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1030f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor/// \brief Routine that indiscriminately skips bytes in the source file.
1031f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregorvoid Lexer::SkipBytes(unsigned Bytes, bool StartOfLine) {
1032f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  BufferPtr += Bytes;
1033f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  if (BufferPtr > BufferEnd)
1034f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    BufferPtr = BufferEnd;
1035f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  IsAtStartOfLine = StartOfLine;
1036f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor}
1037f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
1038d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
10395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
10405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned Size;
10415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned char C = *CurPtr++;
1042cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  while (isIdentifierBody(C))
10435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr++;
1044cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner
10455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  --CurPtr;   // Back up over the skipped character.
10465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Fast path, no $,\,? in identifier found.  '\' might be an escaped newline
10485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
10495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: UCNs.
1050cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  //
1051cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  // TODO: Could merge these checks into a CharInfo flag to make the comparison
1052cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  // cheaper
10535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
10545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerFinishIdentifier:
10555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const char *IdStart = BufferPtr;
1056c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    FormTokenWithChars(Result, CurPtr, tok::raw_identifier);
1057c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    Result.setRawIdentifierData(IdStart);
10581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we are in raw mode, return this identifier raw.  There is no need to
10605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // look up identifier information or attempt to macro expand it.
1061c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    if (LexingRawMode)
1062c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara      return;
10631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1064c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    // Fill in Result.IdentifierInfo and update the token kind,
1065c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    // looking up the identifier in the identifier table.
1066c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    IdentifierInfo *II = PP->LookUpIdentifierInfo(Result);
10671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Finally, now that we know we have an identifier, pass this off to the
10695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // preprocessor, which may macro expand it or something.
1070d1186fa38166a581b51975f0382a45fc3a0733d0Chris Lattner    if (II->isHandleIdentifierCase())
10716a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner      PP->HandleIdentifier(Result);
10726a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    return;
10735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, $,\,? in identifier found.  Enter slower path.
10761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  C = getCharAndSize(CurPtr, Size);
10785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '$') {
10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If we hit a $ and they are not supported in identifiers, we are done.
10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (!Features.DollarIdents) goto FinishIdentifier;
10821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Otherwise, emit a diagnostic and continue.
108474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
108574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(CurPtr, diag::ext_dollar_in_identifier);
10865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, Size, Result);
10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getCharAndSize(CurPtr, Size);
10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      continue;
10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Found end of identifier.
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      goto FinishIdentifier;
10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this character is good, consume it.
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr = ConsumeChar(CurPtr, Size, Result);
10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getCharAndSize(CurPtr, Size);
10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (isIdentifierBody(C)) { // FIXME: UCNs.
10995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, Size, Result);
11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getCharAndSize(CurPtr, Size);
11015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1105a75ec43d625753b4439b0d6f70bd988444c74617Douglas Gregor/// isHexaLiteral - Return true if Start points to a hex constant.
11064a551000bee716ac8b1bbe16134a53f0ad221a5aChris Lattner/// in microsoft mode (where this is supposed to be several different tokens).
11076ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattnerstatic bool isHexaLiteral(const char *Start, const LangOptions &Features) {
11086ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner  unsigned Size;
11096ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner  char C1 = Lexer::getCharAndSizeNoWarn(Start, Size, Features);
11106ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner  if (C1 != '0')
11116ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner    return false;
11126ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner  char C2 = Lexer::getCharAndSizeNoWarn(Start + Size, Size, Features);
11136ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner  return (C2 == 'x' || C2 == 'X');
1114a75ec43d625753b4439b0d6f70bd988444c74617Douglas Gregor}
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11165253c7ff266ae79308050c9f43d60dd1a67c5fb9Nate Begeman/// LexNumericConstant - Lex the remainder of a integer or floating point
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. From[-1] is the first character lexed.  Return the end of the
11185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant.
1119d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned Size;
11215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getCharAndSize(CurPtr, Size);
11225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char PrevCh = 0;
11235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isNumberBody(C)) { // FIXME: UCNs?
11245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr = ConsumeChar(CurPtr, Size, Result);
11255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PrevCh = C;
11265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getCharAndSize(CurPtr, Size);
11275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we fell out, check for a sign, due to 1e+12.  If we have one, continue.
1130b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner  if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) {
1131b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner    // If we are in Microsoft mode, don't continue if the constant is hex.
1132b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner    // For example, MSVC will accept the following as 3 tokens: 0x1234567e+1
11336ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner    if (!Features.Microsoft || !isHexaLiteral(BufferPtr, Features))
1134b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner      return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
1135b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner  }
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a hex FP constant, continue.
11388c723404c34e9347fae473489194e8c200c4ac83Sean Hunt  if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
1139b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner      !Features.CPlusPlus0x)
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
11411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
114347246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
11449e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
114547246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
11465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
11495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// either " or L".
1150d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnervoid Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this string contain the \0 character?
11521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
11545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (C != '"') {
1155571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    // Skip escaped characters.  Escaped newlines will already be processed by
1156571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    // getAndAdvanceChar.
1157571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    if (C == '\\')
11585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getAndAdvanceChar(CurPtr, Result);
115933611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor
1160571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    if (C == '\n' || C == '\r' ||             // Newline.
116133611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor        (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
116255817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      if (C == 0 && PP && PP->isCodeCompletionFile(FileLoc))
116355817afdf9d453a443262a733f6caf6692dca118Douglas Gregor        PP->CodeCompleteNaturalLanguage();
116455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      else if (!isLexingRawMode() && !Features.AsmPreprocessor)
116574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(BufferPtr, diag::err_unterminated_string);
11669e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr-1, tok::unknown);
11675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return;
11685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1169571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner
1170571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    if (C == 0)
1171571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner      NulCharacter = CurPtr-1;
11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
11735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If a nul character existed in the string, warn about it.
117674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
117774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_string);
11785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of the token as well as the BufferPtr instance var.
118047246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
11816cf750298d3621d8a10a6dd07fcee8e274b9d94dSean Hunt  FormTokenWithChars(Result, CurPtr,
11826cf750298d3621d8a10a6dd07fcee8e274b9d94dSean Hunt                     Wide ? tok::wide_string_literal : tok::string_literal);
118347246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
11845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
11875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// after having lexed the '<' character.  This is used for #include filenames.
1188d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
11895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this string contain the \0 character?
11909cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner  const char *AfterLessPos = CurPtr;
11915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
11925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (C != '>') {
11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip escaped characters.
11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '\\') {
11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip the escaped character.
11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getAndAdvanceChar(CurPtr, Result);
11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == '\n' || C == '\r' ||             // Newline.
11985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer               (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
11999cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      // If the filename is unterminated, then it must just be a lone <
12009cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      // character.  Return this as such.
12019cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      FormTokenWithChars(Result, AfterLessPos, tok::less);
12025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return;
12035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == 0) {
12045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      NulCharacter = CurPtr-1;
12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If a nul character existed in the string, warn about it.
121074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
121174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_string);
12121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
121447246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
12159e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
121647246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
12175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexCharConstant - Lex the remainder of a character constant, after having
12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexed either ' or L'.
1222d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this character contain the \0 character?
12245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
12265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (C == '\'') {
122733ab3f6c7e3065550de202088f8f335ecfa16ae1Chris Lattner    if (!isLexingRawMode() && !Features.AsmPreprocessor)
122874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      Diag(BufferPtr, diag::err_empty_character);
12299e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::unknown);
12305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return;
12315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1233d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner  while (C != '\'') {
1234d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    // Skip escaped characters.
1235d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    if (C == '\\') {
1236d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      // Skip the escaped character.
1237d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      // FIXME: UCN's
12385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getAndAdvanceChar(CurPtr, Result);
1239d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    } else if (C == '\n' || C == '\r' ||             // Newline.
1240d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner               (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
124155817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      if (C == 0 && PP && PP->isCodeCompletionFile(FileLoc))
124255817afdf9d453a443262a733f6caf6692dca118Douglas Gregor        PP->CodeCompleteNaturalLanguage();
124355817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      else if (!isLexingRawMode() && !Features.AsmPreprocessor)
1244d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner        Diag(BufferPtr, diag::err_unterminated_char);
1245d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      FormTokenWithChars(Result, CurPtr-1, tok::unknown);
1246d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      return;
1247d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    } else if (C == 0) {
1248d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      NulCharacter = CurPtr-1;
1249d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    }
1250d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    C = getAndAdvanceChar(CurPtr, Result);
12515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1253d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner  // If a nul character existed in the character, warn about it.
125474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
125574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_char);
12565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
125847246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
12599e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::char_constant);
126047246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
12615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
12645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Update BufferPtr to point to the next non-whitespace character and return.
1265d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner///
1266d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
1267d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner///
1268d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnerbool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
12695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Whitespace - Skip it, then return the token after the whitespace.
12705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned char Char = *CurPtr;  // Skip consequtive spaces efficiently.
12715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
12725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip horizontal whitespace very aggressively.
12735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (isHorizontalWhitespace(Char))
12745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Char = *++CurPtr;
12751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1276ddd3e8b90a92c39f620bda7a0945320c0b9e60dbDaniel Dunbar    // Otherwise if we have something other than whitespace, we're done.
12775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char != '\n' && Char != '\r')
12785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
12791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingPreprocessorDirective) {
12815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // End of preprocessor directive line, let LexTokenInternal handle this.
12825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      BufferPtr = CurPtr;
1283d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return false;
12845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
12851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // ok, but handle newline.
12875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // The returned token is at the start of the line.
1288d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::StartOfLine);
12895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // No leading whitespace seen so far.
1290d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.clearFlag(Token::LeadingSpace);
12915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = *++CurPtr;
12925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this isn't immediately after a newline, there is leading space.
12955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char PrevChar = CurPtr[-1];
12965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PrevChar != '\n' && PrevChar != '\r')
1297d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
12985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1299d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // If the client wants us to return whitespace, return it now.
1300d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  if (isKeepWhitespaceMode()) {
13019e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::unknown);
1302d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    return true;
1303d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  }
13041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
1306d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  return false;
13075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// SkipBCPLComment - We have just read the // characters from input.  Skip until
13105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// we find the newline character thats terminate the comment.  Then update
1311046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// BufferPtr and return.
1312046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner///
1313046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted
1314046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true.
1315d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
13165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If BCPL comments aren't explicitly enabled for this language, emit an
13175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // extension warning.
131874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (!Features.BCPLComment && !isLexingRawMode()) {
13195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(BufferPtr, diag::ext_bcpl_comment);
13201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Mark them enabled so we only emit one warning for this translation
13225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // unit.
13235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Features.BCPLComment = true;
13245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Scan over the body of the comment.  The common case, when scanning, is that
13275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the comment contains normal ascii characters with nothing interesting in
13285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // them.  As such, optimize for this case with the inner loop.
13295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C;
13305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  do {
13315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr;
13325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FIXME: Speedup BCPL comment lexing.  Just scan for a \n or \r character.
13335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we find a \n character, scan backwards, checking to see if it's an
13345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // escaped newline, like we do for block comments.
13351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over characters in the fast loop.
13375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (C != 0 &&                // Potentially EOF.
13385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           C != '\\' &&             // Potentially escaped newline.
13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           C != '?' &&              // Potentially trigraph.
13405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           C != '\n' && C != '\r')  // Newline or DOS-style newline.
13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *++CurPtr;
13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is a newline, we're done.
13445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '\n' || C == '\r')
13455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;  // Found the newline? Break out!
13461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is a hard case.  Fall back on getAndAdvanceChar to
1348bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // properly decode the character.  Read it in raw mode to avoid emitting
1349bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // diagnostics about things like trigraphs.  If we see an escaped newline,
1350bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // we'll handle it below.
13515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const char *OldPtr = CurPtr;
1352bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    bool OldRawMode = isLexingRawMode();
1353bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    LexingRawMode = true;
13545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
1355bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    LexingRawMode = OldRawMode;
1356ead616c5d8681a41b11273327813e61bda01907aChris Lattner
1357ead616c5d8681a41b11273327813e61bda01907aChris Lattner    // If the char that we finally got was a \n, then we must have had something
1358ead616c5d8681a41b11273327813e61bda01907aChris Lattner    // like \<newline><newline>.  We don't want to have consumed the second
1359ead616c5d8681a41b11273327813e61bda01907aChris Lattner    // newline, we want CurPtr, to end up pointing to it down below.
1360ead616c5d8681a41b11273327813e61bda01907aChris Lattner    if (C == '\n' || C == '\r') {
1361ead616c5d8681a41b11273327813e61bda01907aChris Lattner      --CurPtr;
1362ead616c5d8681a41b11273327813e61bda01907aChris Lattner      C = 'x'; // doesn't matter what this is.
1363ead616c5d8681a41b11273327813e61bda01907aChris Lattner    }
13641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we read multiple characters, and one of those characters was a \r or
13665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n, then we had an escaped newline within the comment.  Emit diagnostic
13675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // unless the next line is also a // comment.
13685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
13695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      for (; OldPtr != CurPtr; ++OldPtr)
13705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
13715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Okay, we found a // comment that ends in a newline, if the next
13725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // line is also a // comment, but has spaces, don't emit a diagnostic.
13735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (isspace(C)) {
13745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            const char *ForwardPtr = CurPtr;
13755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            while (isspace(*ForwardPtr))  // Skip whitespace.
13765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              ++ForwardPtr;
13775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
13785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              break;
13795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
13801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
138174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          if (!isLexingRawMode())
138274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner            Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
13835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          break;
13845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
13855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
13861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
138755817afdf9d453a443262a733f6caf6692dca118Douglas Gregor    if (CurPtr == BufferEnd+1) {
138855817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      if (PP && PP->isCodeCompletionFile(FileLoc))
138955817afdf9d453a443262a733f6caf6692dca118Douglas Gregor        PP->CodeCompleteNaturalLanguage();
139055817afdf9d453a443262a733f6caf6692dca118Douglas Gregor
139155817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      --CurPtr;
139255817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      break;
139355817afdf9d453a443262a733f6caf6692dca118Douglas Gregor    }
13945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } while (C != '\n' && C != '\r');
13955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13963d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  // Found but did not consume the newline.  Notify comment handlers about the
13973d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  // comment unless we're in a #if 0 block.
13983d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  if (PP && !isLexingRawMode() &&
13993d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner      PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
14003d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner                                            getSourceLocation(CurPtr)))) {
1401046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    BufferPtr = CurPtr;
1402046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    return true; // A token has to be returned.
1403046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  }
14041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are returning comments as tokens, return this comment as a token.
1406fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner  if (inKeepCommentMode())
14075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return SaveBCPLComment(Result, CurPtr);
14085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are inside a preprocessor directive and we see the end of line,
14105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // return immediately, so that the lexer can return this as an EOM token.
14115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
14125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    BufferPtr = CurPtr;
14132d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
14145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, eat the \n character.  We don't care if this is a \n\r or
14177a4f004a77421af876152281b92c4f8ea24afb2dChris Lattner  // \r\n sequence.  This is an efficiency hack (because we know the \n can't
1418d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // contribute to another token), it isn't needed for correctness.  Note that
1419d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // this is ok even in KeepWhitespaceMode, because we would have returned the
1420d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// comment above in that mode.
14215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++CurPtr;
14221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // The next returned token is at the start of the line.
1424d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.setFlag(Token::StartOfLine);
14255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // No leading whitespace seen so far.
1426d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.clearFlag(Token::LeadingSpace);
14275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
14282d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return false;
14295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// an appropriate way and return it.
1433d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
14349e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // If we're not in a preprocessor directive, just return the // comment
14359e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // directly.
14369e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::comment);
14371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14389e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  if (!ParsingPreprocessorDirective)
14399e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    return true;
14401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14419e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // If this BCPL-style comment is in a macro definition, transmogrify it into
14429e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // a C-style block comment.
1443453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor  bool Invalid = false;
1444453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor  std::string Spelling = PP->getSpelling(Result, &Invalid);
1445453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor  if (Invalid)
1446453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor    return true;
1447453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor
14489e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
14499e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Spelling[1] = '*';   // Change prefix to "/*".
14509e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Spelling += "*/";    // add suffix.
14511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14529e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Result.setKind(tok::comment);
145347246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  PP->CreateString(&Spelling[0], Spelling.size(), Result,
145447246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner                   Result.getLocation());
14552d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return true;
14565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
14595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// character (either \n or \r) is part of an escaped newline sequence.  Issue a
146047a2b40b7a3d9c89d6c3d12e5a81e2498c207750Chris Lattner/// diagnostic if so.  We know that the newline is inside of a block comment.
14611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
14625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                                  Lexer *L) {
14635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
14641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Back up off the newline.
14665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  --CurPtr;
14671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a two-character newline sequence, skip the other character.
14695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
14705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n\n or \r\r -> not escaped newline.
14715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[0] == CurPtr[1])
14725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
14735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n\r or \r\n -> skip the newline.
14745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    --CurPtr;
14755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have horizontal whitespace, skip over it.  We allow whitespace
14785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // between the slash and newline.
14795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool HasSpace = false;
14805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
14815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    --CurPtr;
14825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    HasSpace = true;
14835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, we know this is an escaped newline.
14865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (*CurPtr == '\\') {
14875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[-1] != '*') return false;
14885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
14895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // It isn't a slash, is it the ?? / trigraph?
14905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
14915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr[-3] != '*')
14925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
14931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // This is the trigraph ending the comment.  Emit a stern warning!
14955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr -= 2;
14965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If no trigraphs are enabled, warn that we ignored this trigraph and
14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // ignore this * character.
14995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!L->getFeatures().Trigraphs) {
150074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!L->isLexingRawMode())
150174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
15025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
15035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
150474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!L->isLexingRawMode())
150574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      L->Diag(CurPtr, diag::trigraph_ends_block_comment);
15065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Warn about having an escaped newline between the */ characters.
150974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (!L->isLexingRawMode())
151074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
15111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If there was space between the backslash and newline, warn about it.
151374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (HasSpace && !L->isLexingRawMode())
151474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    L->Diag(CurPtr, diag::backslash_newline_space);
15151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return true;
15175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__
15205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <emmintrin.h>
15215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__
15225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <altivec.h>
15235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#undef bool
15245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
15255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipBlockComment - We have just read the /* characters from input.  Read
15275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// until we find the */ characters that terminate the comment.  Note that we
15285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// don't bother decoding trigraphs or escaped newlines in block comments,
15295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// because they cannot cause the comment to end.  The only thing that can
15305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// happen is the comment could end with an escaped newline between the */ end
15315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// of comment.
15322d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner///
1533046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted
1534046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true.
1535d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
15365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Scan one character past where we should, looking for a '/' character.  Once
15375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // we find it, check to see if it was preceeded by a *.  This common
15385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // optimization helps people who like to put a lot of * characters in their
15395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // comments.
15408146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner
15418146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // The first character we get with newlines and trigraphs skipped to handle
15428146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // the degenerate /*/ case below correctly if the * has an escaped newline
15438146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // after it.
15448146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  unsigned CharSize;
15458146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  unsigned char C = getCharAndSize(CurPtr, CharSize);
15468146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  CurPtr += CharSize;
15475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (C == 0 && CurPtr == BufferEnd+1) {
1548150fcd5dcb79ad0a9cc6824c5589000e3669c7a4Chris Lattner    if (!isLexingRawMode() &&
1549150fcd5dcb79ad0a9cc6824c5589000e3669c7a4Chris Lattner        !PP->isCodeCompletionFile(FileLoc))
15500af574270d3be2b0e73a3379dfaa633746f8fc6fChris Lattner      Diag(BufferPtr, diag::err_unterminated_block_comment);
155131f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    --CurPtr;
15521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
155331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    // KeepWhitespaceMode should return this broken comment as a token.  Since
155431f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    // it isn't a well formed comment, just return it as an 'unknown' token.
155531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    if (isKeepWhitespaceMode()) {
15569e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr, tok::unknown);
155731f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      return true;
155831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    }
15591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
156031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    BufferPtr = CurPtr;
15612d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
15625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15648146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // Check to see if the first character after the '/*' is another /.  If so,
15658146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // then this slash does not end the block comment, it is part of it.
15668146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  if (C == '/')
15678146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner    C = *CurPtr++;
15681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
15705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over all non-interesting characters until we find end of buffer or a
15715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // (probably ending) '/' character.
15725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr + 24 < BufferEnd) {
15735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // While not aligned to a 16-byte boundary.
15745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
15755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        C = *CurPtr++;
15761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '/') goto FoundSlash;
15785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__
15805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
15815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                     '/', '/', '/', '/', '/', '/', '/', '/');
15825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (CurPtr+16 <= BufferEnd &&
15835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
15845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 16;
15855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__
15865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      __vector unsigned char Slashes = {
15871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        '/', '/', '/', '/',  '/', '/', '/', '/',
15885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        '/', '/', '/', '/',  '/', '/', '/', '/'
15895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      };
15905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (CurPtr+16 <= BufferEnd &&
15915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
15925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 16;
15931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#else
15945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Scan for '/' quickly.  Many block comments are very large.
15955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (CurPtr[0] != '/' &&
15965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[1] != '/' &&
15975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[2] != '/' &&
15985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[3] != '/' &&
15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr+4 < BufferEnd) {
16005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 4;
16015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
16025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
16031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // It has to be one of the bytes scanned, increment to it and read one.
16055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *CurPtr++;
16065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
16071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Loop to scan the remainder.
16095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (C != '/' && C != '\0')
16105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *CurPtr++;
16111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FoundSlash:
16135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '/') {
16145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr[-2] == '*')  // We found the final */.  We're done!
16155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
16161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
16185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
16195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // We found the final */, though it had an escaped newline between the
16205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // * and /.  We're done!
16215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          break;
16225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
16235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
16245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr[0] == '*' && CurPtr[1] != '/') {
16255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // If this is a /* inside of the comment, emit a warning.  Don't do this
16265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // if this is a /*/, which will end the comment.  This misses cases with
16275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // embedded escaped newlines, but oh well.
162874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        if (!isLexingRawMode())
162974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          Diag(CurPtr-1, diag::warn_nested_block_comment);
16305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
16315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == 0 && CurPtr == BufferEnd+1) {
163255817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      if (PP && PP->isCodeCompletionFile(FileLoc))
163355817afdf9d453a443262a733f6caf6692dca118Douglas Gregor        PP->CodeCompleteNaturalLanguage();
163455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      else if (!isLexingRawMode())
163574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(BufferPtr, diag::err_unterminated_block_comment);
16365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Note: the user probably forgot a */.  We could continue immediately
16375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // after the /*, but this would involve lexing a lot of what really is the
16385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // comment, which surely would confuse the parser.
163931f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      --CurPtr;
16401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
164131f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      // KeepWhitespaceMode should return this broken comment as a token.  Since
164231f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      // it isn't a well formed comment, just return it as an 'unknown' token.
164331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      if (isKeepWhitespaceMode()) {
16449e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        FormTokenWithChars(Result, CurPtr, tok::unknown);
164531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner        return true;
164631f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      }
16471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
164831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      BufferPtr = CurPtr;
16492d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner      return false;
16505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
16515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr++;
16525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16543d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  // Notify comment handlers about the comment unless we're in a #if 0 block.
16553d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  if (PP && !isLexingRawMode() &&
16563d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner      PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
16573d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner                                            getSourceLocation(CurPtr)))) {
1658046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    BufferPtr = CurPtr;
1659046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    return true; // A token has to be returned.
1660046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  }
16612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
16625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are returning comments as tokens, return this comment as a token.
1663fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner  if (inKeepCommentMode()) {
16649e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::comment);
16652d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return true;
16665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // It is common for the tokens immediately after a /**/ comment to be
16695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // whitespace.  Instead of going through the big switch, handle it
1670d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // efficiently now.  This is safe even in KeepWhitespaceMode because we would
1671d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // have already returned above with the comment as a token.
16725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (isHorizontalWhitespace(*CurPtr)) {
1673d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
16745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipWhitespace(Result, CurPtr+1);
16752d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
16765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, just return so that the next character will be lexed as a token.
16795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
1680d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.setFlag(Token::LeadingSpace);
16812d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return false;
16825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
16855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Lexing Entry Points
16865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
16875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
16895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// uninterpreted string.  This switches the lexer out of directive mode.
16905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstd::string Lexer::ReadToEndOfLine() {
16915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParsingPreprocessorDirective && ParsingFilename == false &&
16925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer         "Must be in a preprocessing directive!");
16935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  std::string Result;
1694d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Token Tmp;
16955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // CurPtr - Cache BufferPtr in an automatic variable.
16975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CurPtr = BufferPtr;
16985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
16995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    char Char = getAndAdvanceChar(CurPtr, Tmp);
17005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Char) {
17015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
17025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Result += Char;
17035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
17045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case 0:  // Null.
17055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Found end of file?
17065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr-1 != BufferEnd) {
17075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Nope, normal character, continue.
17085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        Result += Char;
17095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
17105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
17115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FALL THROUGH.
17125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case '\r':
17135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case '\n':
17145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Okay, we found the end of the line. First, back up past the \0, \r, \n.
17155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(CurPtr[-1] == Char && "Trigraphs for newline?");
17165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      BufferPtr = CurPtr-1;
17171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Next, lex the character, which should handle the EOM transition.
17195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Lex(Tmp);
172055817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      if (Tmp.is(tok::code_completion)) {
172155817afdf9d453a443262a733f6caf6692dca118Douglas Gregor        if (PP && PP->getCodeCompletionHandler())
172255817afdf9d453a443262a733f6caf6692dca118Douglas Gregor          PP->getCodeCompletionHandler()->CodeCompleteNaturalLanguage();
172355817afdf9d453a443262a733f6caf6692dca118Douglas Gregor        Lex(Tmp);
172455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      }
172522f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner      assert(Tmp.is(tok::eom) && "Unexpected token!");
17261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Finally, we're done, return the string we found.
17285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return Result;
17295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
17305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
17315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexEndOfFile - CurPtr points to the end of this file.  Handle this
17345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// condition, reporting diagnostics and handling other edge cases as required.
17355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This returns true if Result contains a token, false if PP.Lex should be
17365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// called again.
1737d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
1738f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  // Check if we are performing code completion.
1739f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  if (PP && PP->isCodeCompletionFile(FileLoc)) {
1740f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    // We're at the end of the file, but we've been asked to consider the
1741f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    // end of the file to be a code-completion token. Return the
1742f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    // code-completion token.
1743f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    Result.startToken();
1744f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    FormTokenWithChars(Result, CurPtr, tok::code_completion);
1745f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1746f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    // Only do the eof -> code_completion translation once.
1747f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    PP->SetCodeCompletionPoint(0, 0, 0);
1748f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
1749f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    // Silence any diagnostics that occur once we hit the code-completion point.
1750f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    PP->getDiagnostics().setSuppressAllDiagnostics(true);
1751f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    return true;
1752f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  }
1753f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
17545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we hit the end of the file while parsing a preprocessor directive,
17555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // end the preprocessor directive first.  The next token returned will
17565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // then be the end of file.
17575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ParsingPreprocessorDirective) {
17585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Done parsing the "line".
17595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParsingPreprocessorDirective = false;
17605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Update the location of token as well as BufferPtr.
17619e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::eom);
17621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Restore comment saving mode, in case it was disabled for directive.
1764f744d133e8d36eec65828d046ee695bda238125cChris Lattner    SetCommentRetentionState(PP->getCommentRetentionState());
17655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return true;  // Have a token.
17661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
176786d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor
176886d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  // If we are in raw mode, return this event as an EOF token.  Let the caller
176986d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  // that put us in raw mode handle the event.
177086d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  if (isLexingRawMode()) {
177186d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    Result.startToken();
177286d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    BufferPtr = BufferEnd;
177386d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    FormTokenWithChars(Result, BufferEnd, tok::eof);
177486d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    return true;
177586d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  }
177681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
1777f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  // Issue diagnostics for unterminated #if and missing newline.
1778f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
17795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are in a #if directive, emit an error.
17805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (!ConditionalStack.empty()) {
17812d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor    if (!PP->isCodeCompletionFile(FileLoc))
17822d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor      PP->Diag(ConditionalStack.back().IfLoc,
17832d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor               diag::err_pp_unterminated_conditional);
17845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConditionalStack.pop_back();
17855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
17861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1787b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner  // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1788b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner  // a pedwarn.
1789b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner  if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
179020d0ee5d23c0f0e879ce1711d11df3118b3fb033Mike Stump    Diag(BufferEnd, diag::ext_no_newline_eof)
1791849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n");
17921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
17945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Finally, let the preprocessor handle this.
1796168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  return PP->HandleEndOfFile(Result);
17975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
18005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified lexer will return a tok::l_paren token, 0 if it is something
18015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// else and 2 if there are no more tokens in the buffer controlled by the
18025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexer.
18035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerunsigned Lexer::isNextPPTokenLParen() {
18045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
18051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Switch to 'skipping' mode.  This will ensure that we can lex a token
18075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // without emitting diagnostics, disables macro expansion, and will cause EOF
18085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // to return an EOF token instead of popping the include stack.
18095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexingRawMode = true;
18101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Save state that can be changed while lexing so that we can restore it.
18125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *TmpBufferPtr = BufferPtr;
1813a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner  bool inPPDirectiveMode = ParsingPreprocessorDirective;
18141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1815d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Token Tok;
18165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Tok.startToken();
18175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexTokenInternal(Tok);
18181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Restore state that may have changed.
18205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = TmpBufferPtr;
1821a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner  ParsingPreprocessorDirective = inPPDirectiveMode;
18221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Restore the lexer back to non-skipping mode.
18245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexingRawMode = false;
18251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
182622f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner  if (Tok.is(tok::eof))
18275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return 2;
182822f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner  return Tok.is(tok::l_paren);
18295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
183134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// FindConflictEnd - Find the end of a version control conflict marker.
183234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerstatic const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) {
183334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  llvm::StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7);
183434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  size_t Pos = RestOfBuffer.find(">>>>>>>");
183534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  while (Pos != llvm::StringRef::npos) {
183634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Must occur at start of line.
183734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    if (RestOfBuffer[Pos-1] != '\r' &&
183834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        RestOfBuffer[Pos-1] != '\n') {
183934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      RestOfBuffer = RestOfBuffer.substr(Pos+7);
18403d488990e13835cb35538b2f0cd53dbda92e36beChris Lattner      Pos = RestOfBuffer.find(">>>>>>>");
184134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      continue;
184234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    }
184334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return RestOfBuffer.data()+Pos;
184434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  }
184534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  return 0;
184634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner}
184734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
184834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// IsStartOfConflictMarker - If the specified pointer is the start of a version
184934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// control conflict marker like '<<<<<<<', recognize it as such, emit an error
185034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// and recover nicely.  This returns true if it is a conflict marker and false
185134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// if not.
185234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
185334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // Only a conflict marker if it starts at the beginning of a line.
185434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (CurPtr != BufferStart &&
185534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
185634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
185734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
185834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // Check to see if we have <<<<<<<.
185934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (BufferEnd-CurPtr < 8 ||
186034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      llvm::StringRef(CurPtr, 7) != "<<<<<<<")
186134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
186234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
186334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // If we have a situation where we don't care about conflict markers, ignore
186434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // it.
186534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (IsInConflictMarker || isLexingRawMode())
186634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
186734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
186834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // Check to see if there is a >>>>>>> somewhere in the buffer at the start of
186934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // a line to terminate this conflict marker.
18703d488990e13835cb35538b2f0cd53dbda92e36beChris Lattner  if (FindConflictEnd(CurPtr, BufferEnd)) {
187134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // We found a match.  We are really in a conflict marker.
187234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Diagnose this, and ignore to the end of line.
187334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    Diag(CurPtr, diag::err_conflict_marker);
187434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    IsInConflictMarker = true;
187534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
187634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Skip ahead to the end of line.  We know this exists because the
187734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // end-of-conflict marker starts with \r or \n.
187834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    while (*CurPtr != '\r' && *CurPtr != '\n') {
187934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      assert(CurPtr != BufferEnd && "Didn't find end of line");
188034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      ++CurPtr;
188134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    }
188234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    BufferPtr = CurPtr;
188334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return true;
188434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  }
188534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
188634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // No end of conflict marker found.
188734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  return false;
188834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner}
188934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
189034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
189134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>'
189234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// marker, then it is the end of a conflict marker.  Handle it by ignoring up
189334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// until the end of the line.  This returns true if it is a conflict marker and
189434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// false if not.
189534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
189634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // Only a conflict marker if it starts at the beginning of a line.
189734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (CurPtr != BufferStart &&
189834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
189934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
190034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
190134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // If we have a situation where we don't care about conflict markers, ignore
190234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // it.
190334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (!IsInConflictMarker || isLexingRawMode())
190434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
190534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
190634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // Check to see if we have the marker (7 characters in a row).
190734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  for (unsigned i = 1; i != 7; ++i)
190834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    if (CurPtr[i] != CurPtr[0])
190934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      return false;
191034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
191134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // If we do have it, search for the end of the conflict marker.  This could
191234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // fail if it got skipped with a '#if 0' or something.  Note that CurPtr might
191334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // be the end of conflict marker.
191434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) {
191534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    CurPtr = End;
191634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
191734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Skip ahead to the end of line.
191834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
191934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      ++CurPtr;
192034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
192134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    BufferPtr = CurPtr;
192234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
192334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // No longer in the conflict marker.
192434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    IsInConflictMarker = false;
192534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return true;
192634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  }
192734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
192834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  return false;
192934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner}
193034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
19315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexTokenInternal - This implements a simple C family lexer.  It is an
19335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// extremely performance critical piece of code.  This assumes that the buffer
1934efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// has a null character at the end of the file.  This returns a preprocessing
1935efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// token, not a normal token, as such, it is an internal interface.  It assumes
1936efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// that the Flags of result have been cleared before calling this.
1937d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexTokenInternal(Token &Result) {
19385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerLexNextToken:
19395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // New token, can't need cleaning yet.
1940d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.clearFlag(Token::NeedsCleaning);
19415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Result.setIdentifierInfo(0);
19421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // CurPtr - Cache BufferPtr in an automatic variable.
19445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CurPtr = BufferPtr;
19455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Small amounts of horizontal whitespace is very common between tokens.
19475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
19485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++CurPtr;
19495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while ((*CurPtr == ' ') || (*CurPtr == '\t'))
19505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++CurPtr;
19511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1952d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // If we are keeping whitespace and other tokens, just return what we just
1953d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // skipped.  The next lexer invocation will return the token after the
1954d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // whitespace.
1955d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (isKeepWhitespaceMode()) {
19569e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr, tok::unknown);
1957d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return;
1958d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    }
19591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    BufferPtr = CurPtr;
1961d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
19625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
19631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned SizeTmp, SizeTmp2;   // Temporaries for use in cases below.
19651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Read a character, advancing over it.
19675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char Char = getAndAdvanceChar(CurPtr, Result);
19689e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  tok::TokenKind Kind;
19691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Char) {
19715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 0:  // Null.
19725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Found end of file?
19735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr-1 == BufferEnd) {
19745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Read the PP instance variable into an automatic variable, because
19755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // LexEndOfFile will often delete 'this'.
1976168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      Preprocessor *PPCache = PP;
19775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (LexEndOfFile(Result, CurPtr-1))  // Retreat back into the file.
19785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return;   // Got a token to return.
1979168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1980168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      return PPCache->Lex(Result);
19815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
19821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
198374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!isLexingRawMode())
198474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      Diag(CurPtr-1, diag::null_in_file);
1985d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
1986d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
1987d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
19881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
1990a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner
1991a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner  case 26:  // DOS & CP/M EOF: "^Z".
1992a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    // If we're in Microsoft extensions mode, treat this as end of file.
1993a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    if (Features.Microsoft) {
1994a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      // Read the PP instance variable into an automatic variable, because
1995a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      // LexEndOfFile will often delete 'this'.
1996a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      Preprocessor *PPCache = PP;
1997a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      if (LexEndOfFile(Result, CurPtr-1))  // Retreat back into the file.
1998a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner        return;   // Got a token to return.
1999a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
2000a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      return PPCache->Lex(Result);
2001a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    }
2002a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    // If Microsoft extensions are disabled, this is just random garbage.
2003a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    Kind = tok::unknown;
2004a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    break;
2005a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner
20065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\n':
20075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\r':
20085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we are inside a preprocessor directive and we see the end of line,
20095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // we know we are done with the directive, so return an EOM token.
20105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingPreprocessorDirective) {
20115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Done parsing the "line".
20125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParsingPreprocessorDirective = false;
20131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Restore comment saving mode, in case it was disabled for directive.
2015f744d133e8d36eec65828d046ee695bda238125cChris Lattner      SetCommentRetentionState(PP->getCommentRetentionState());
20161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Since we consumed a newline, we are back at the start of a line.
20185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      IsAtStartOfLine = true;
20191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20209e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::eom;
20215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
20225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
20235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // The returned token is at the start of the line.
2024d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::StartOfLine);
20255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // No leading whitespace seen so far.
2026d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.clearFlag(Token::LeadingSpace);
20271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2028d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
2029d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
20305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
20315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ' ':
20325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\t':
20335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\f':
20345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\v':
20358133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner  SkipHorizontalWhitespace:
2036d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
2037d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
2038d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
20398133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner
20408133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner  SkipIgnoredUnits:
20418133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    CurPtr = BufferPtr;
20421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20438133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    // If the next token is obviously a // or /* */ comment, skip it efficiently
20448133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    // too (without going through the big switch stmt).
20458402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
20468402c73dd880e8af46c826d873681820aebe32ebChris Lattner        Features.BCPLComment) {
2047046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner      if (SkipBCPLComment(Result, CurPtr+2))
2048046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner        return; // There is a token to return.
20498133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipIgnoredUnits;
2050fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner    } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
2051046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner      if (SkipBlockComment(Result, CurPtr+2))
2052046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner        return; // There is a token to return.
20538133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipIgnoredUnits;
20548133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    } else if (isHorizontalWhitespace(*CurPtr)) {
20558133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipHorizontalWhitespace;
20568133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    }
20575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
2058a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner
20593a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  // C99 6.4.4.1: Integer Constants.
20603a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  // C99 6.4.4.2: Floating Constants.
20613a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '0': case '1': case '2': case '3': case '4':
20623a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '5': case '6': case '7': case '8': case '9':
20633a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    // Notify MIOpt that we read a non-whitespace/non-comment token.
20643a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    MIOpt.ReadToken();
20653a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    return LexNumericConstant(Result, CurPtr);
20661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20673a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case 'L':   // Identifier (Loony) or wide literal (L'x' or L"xyz").
20685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
20695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
20705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
20715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Wide string literal.
20735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '"')
20745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
20755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              true);
20765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Wide character constant.
20785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '\'')
20795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
20805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FALL THROUGH, treating L like the start of an identifier.
20811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.2: Identifiers.
20835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
20845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'H': case 'I': case 'J': case 'K':    /*'L'*/case 'M': case 'N':
20855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
20865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'V': case 'W': case 'X': case 'Y': case 'Z':
20875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
20885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
20895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
20905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'v': case 'w': case 'x': case 'y': case 'z':
20915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '_':
20925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
20935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
20945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexIdentifier(Result, CurPtr);
20953a5707766850f9ee9daa35299794328b5caf96dcChris Lattner
20963a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '$':   // $ in identifiers.
20973a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    if (Features.DollarIdents) {
209874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
209974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(CurPtr-1, diag::ext_dollar_in_identifier);
21003a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      // Notify MIOpt that we read a non-whitespace/non-comment token.
21013a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      MIOpt.ReadToken();
21023a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      return LexIdentifier(Result, CurPtr);
21033a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    }
21041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21059e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::unknown;
21063a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    break;
21071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.4: Character Constants.
21095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\'':
21105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
21115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
21125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexCharConstant(Result, CurPtr);
21135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.5: String Literals.
21155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '"':
21165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
21175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
21185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexStringLiteral(Result, CurPtr, false);
21195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.6: Punctuators.
21215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '?':
21229e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::question;
21235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '[':
21259e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_square;
21265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ']':
21289e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_square;
21295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '(':
21319e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_paren;
21325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ')':
21349e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_paren;
21355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '{':
21379e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_brace;
21385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '}':
21409e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_brace;
21415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '.':
21435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
21445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char >= '0' && Char <= '9') {
21455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Notify MIOpt that we read a non-whitespace/non-comment token.
21465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      MIOpt.ReadToken();
21475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
21495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Features.CPlusPlus && Char == '*') {
21509e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::periodstar;
21515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr += SizeTmp;
21525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '.' &&
21535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer               getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
21549e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ellipsis;
21555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
21565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                           SizeTmp2, Result);
21575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
21589e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::period;
21595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
21605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '&':
21625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
21635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '&') {
21649e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ampamp;
21655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
21665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
21679e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ampequal;
21685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
21695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
21709e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::amp;
21715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
21725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case '*':
21745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (getCharAndSize(CurPtr, SizeTmp) == '=') {
21759e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::starequal;
21765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
21775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
21789e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::star;
21795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
21805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '+':
21825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
21835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '+') {
21845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
21859e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plusplus;
21865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
21875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
21889e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plusequal;
21895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
21909e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plus;
21915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
21925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '-':
21945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
21959e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    if (Char == '-') {      // --
21965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
21979e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minusminus;
21981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (Char == '>' && Features.CPlusPlus &&
21999e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner               getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {  // C++ ->*
22005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
22015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                           SizeTmp2, Result);
22029e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::arrowstar;
22039e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Char == '>') {   // ->
22045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
22059e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::arrow;
22069e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Char == '=') {   // -=
22075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
22089e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minusequal;
22095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
22109e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minus;
22115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
22125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
22135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '~':
22149e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::tilde;
22155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
22165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '!':
22175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (getCharAndSize(CurPtr, SizeTmp) == '=') {
22189e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::exclaimequal;
22195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
22205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
22219e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::exclaim;
22225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
22235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
22245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '/':
22255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // 6.4.9: Comments
22265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
22275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '/') {         // BCPL comment.
22288402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
22298402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // want to lex this as a comment.  There is one problem with this though,
22308402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // that in one particular corner case, this can change the behavior of the
22318402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // resultant program.  For example, In  "foo //**/ bar", C89 would lex
22328402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // this as "foo / bar" and langauges with BCPL comments would lex it as
22338402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // "foo".  Check to see if the character after the second slash is a '*'.
22348402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // If so, we will lex that as a "/" instead of the start of a comment.
22358402c73dd880e8af46c826d873681820aebe32ebChris Lattner      if (Features.BCPLComment ||
22368402c73dd880e8af46c826d873681820aebe32ebChris Lattner          getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
22378402c73dd880e8af46c826d873681820aebe32ebChris Lattner        if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
2238046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner          return; // There is a token to return.
22391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22408402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // It is common for the tokens immediately after a // comment to be
22418402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // whitespace (indentation for the next line).  Instead of going through
22428402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // the big switch, handle it efficiently now.
22438402c73dd880e8af46c826d873681820aebe32ebChris Lattner        goto SkipIgnoredUnits;
22448402c73dd880e8af46c826d873681820aebe32ebChris Lattner      }
22458402c73dd880e8af46c826d873681820aebe32ebChris Lattner    }
22461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22478402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (Char == '*') {  // /**/ comment.
22485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
2249046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner        return; // There is a token to return.
22502d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner      goto LexNextToken;   // GCC isn't tail call eliminating.
22518402c73dd880e8af46c826d873681820aebe32ebChris Lattner    }
22521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22538402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (Char == '=') {
22545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
22559e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::slashequal;
22565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
22579e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::slash;
22585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
22595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
22605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '%':
22615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
22625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
22639e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::percentequal;
22645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
22655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Features.Digraphs && Char == '>') {
22669e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::r_brace;                             // '%>' -> '}'
22675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
22685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Features.Digraphs && Char == ':') {
22695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
22705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Char = getCharAndSize(CurPtr, SizeTmp);
22715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
22729e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        Kind = tok::hashhash;                          // '%:%:' -> '##'
22735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
22745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                             SizeTmp2, Result);
22755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else if (Char == '@' && Features.Microsoft) {  // %:@ -> #@ -> Charize
22765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
227774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        if (!isLexingRawMode())
227874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          Diag(BufferPtr, diag::charize_microsoft_ext);
22799e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        Kind = tok::hashat;
2280e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner      } else {                                         // '%:' -> '#'
22815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // We parsed a # character.  If this occurs at the start of the line,
22825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // it's actually the start of a preprocessing directive.  Callback to
22835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // the preprocessor to handle it.
22845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // FIXME: -fpreprocessed mode??
2285766703b76dc1eb0c49d11cafc2a17c7407876ebcChris Lattner        if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
2286e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner          FormTokenWithChars(Result, CurPtr, tok::hash);
2287168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner          PP->HandleDirective(Result);
22881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // As an optimization, if the preprocessor didn't switch lexers, tail
22905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // recurse.
2291168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner          if (PP->isCurrentLexer(this)) {
22925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // Start a new token. If this is a #include or something, the PP may
22935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // want us starting at the beginning of the line again.  If so, set
2294515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner            // the StartOfLine flag and clear LeadingSpace.
22955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (IsAtStartOfLine) {
2296d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner              Result.setFlag(Token::StartOfLine);
2297515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner              Result.clearFlag(Token::LeadingSpace);
22985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              IsAtStartOfLine = false;
22995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            }
23005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            goto LexNextToken;   // GCC isn't tail call eliminating.
23015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
23021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2303168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner          return PP->Lex(Result);
23045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
23051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2306e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner        Kind = tok::hash;
23075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
23085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
23099e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::percent;
23105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
23115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
23125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '<':
23135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
23145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingFilename) {
23159cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      return LexAngledStringLiteral(Result, CurPtr);
23165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '<') {
231734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
231834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (After == '=') {
231934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::lesslessequal;
232034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
232134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner                             SizeTmp2, Result);
232234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
232334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        // If this is actually a '<<<<<<<' version control conflict marker,
232434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        // recognize it as such and recover nicely.
232534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
23261b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne      } else if (Features.CUDA && After == '<') {
23271b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne        Kind = tok::lesslessless;
23281b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
23291b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne                             SizeTmp2, Result);
233034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else {
233134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
233234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::lessless;
233334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      }
23345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
23355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
23369e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::lessequal;
23379e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Features.Digraphs && Char == ':') {     // '<:' -> '['
23385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
23399e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::l_square;
23409e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Features.Digraphs && Char == '%') {     // '<%' -> '{'
23415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
23429e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::l_brace;
23435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
23449e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::less;
23455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
23465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
23475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '>':
23485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
23495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
23505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
23519e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::greaterequal;
23525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '>') {
235334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
235434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (After == '=') {
235534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
235634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner                             SizeTmp2, Result);
235734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::greatergreaterequal;
235834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
235934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        // If this is '>>>>>>>' and we're in a conflict marker, ignore it.
236034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
23611b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne      } else if (Features.CUDA && After == '>') {
23621b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne        Kind = tok::greatergreatergreater;
23631b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
23641b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne                             SizeTmp2, Result);
236534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else {
236634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
236734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::greatergreater;
236834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      }
236934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
23705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
23719e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::greater;
23725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
23735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
23745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '^':
23755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
23765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
23775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
23789e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::caretequal;
23795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
23809e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::caret;
23815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
23825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
23835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '|':
23845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
23855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
23869e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipeequal;
23875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
23885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '|') {
238934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      // If this is '|||||||' and we're in a conflict marker, ignore it.
239034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
239134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
23929e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipepipe;
23935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
23945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
23959e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipe;
23965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
23975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
23985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ':':
23995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
24005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Features.Digraphs && Char == '>') {
24019e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::r_square; // ':>' -> ']'
24025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
24035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Features.CPlusPlus && Char == ':') {
24049e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::coloncolon;
24055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
24061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else {
24079e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::colon;
24085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
24095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
24105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ';':
24119e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::semi;
24125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
24135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '=':
24145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
24155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
241634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      // If this is '=======' and we're in a conflict marker, ignore it.
241734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
241834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
241934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
24209e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::equalequal;
24215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
24221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else {
24239e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::equal;
24245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
24255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
24265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ',':
24279e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::comma;
24285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
24295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '#':
24305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
24315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '#') {
24329e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::hashhash;
24335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
24345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '@' && Features.Microsoft) {  // #@ -> Charize
24359e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::hashat;
243674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
243774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(BufferPtr, diag::charize_microsoft_ext);
24385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
24395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
24405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // We parsed a # character.  If this occurs at the start of the line,
24415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // it's actually the start of a preprocessing directive.  Callback to
24425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // the preprocessor to handle it.
24435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FIXME: -fpreprocessed mode??
2444766703b76dc1eb0c49d11cafc2a17c7407876ebcChris Lattner      if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
2445e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner        FormTokenWithChars(Result, CurPtr, tok::hash);
2446168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner        PP->HandleDirective(Result);
24471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // As an optimization, if the preprocessor didn't switch lexers, tail
24495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // recurse.
2450168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner        if (PP->isCurrentLexer(this)) {
24515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Start a new token.  If this is a #include or something, the PP may
24525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // want us starting at the beginning of the line again.  If so, set
2453515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner          // the StartOfLine flag and clear LeadingSpace.
24545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (IsAtStartOfLine) {
2455d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner            Result.setFlag(Token::StartOfLine);
2456515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner            Result.clearFlag(Token::LeadingSpace);
24575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            IsAtStartOfLine = false;
24585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
24595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          goto LexNextToken;   // GCC isn't tail call eliminating.
24605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
2461168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner        return PP->Lex(Result);
24625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
24631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2464e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner      Kind = tok::hash;
24655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
24665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
24675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24683a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '@':
24693a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    // Objective C support.
24703a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    if (CurPtr[-1] == '@' && Features.ObjC1)
24719e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::at;
24723a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    else
24739e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::unknown;
24743a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    break;
24751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\\':
24775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FIXME: UCN's.
24785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FALL THROUGH.
24795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
24809e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::unknown;
24815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
24825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
24831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Notify MIOpt that we read a non-whitespace/non-comment token.
24855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  MIOpt.ReadToken();
24865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
24889e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, Kind);
24895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2490