Lexer.cpp revision 3185d4ac30378995ef70421e2848f77524c2b5d5
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"
33d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis#include "llvm/ADT/STLExtras.h"
34409a03671224d4e5bdab1594c43baf070148f830Chris Lattner#include "llvm/Support/Compiler.h"
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Support/MemoryBuffer.h"
362fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper#include <cstring>
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
39a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic void InitCharacterInfo();
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
41dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===//
42dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner// Token Class Implementation
43dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===//
44dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
46dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattnerbool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
47bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor  if (IdentifierInfo *II = getIdentifierInfo())
48bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor    return II->getObjCKeywordID() == objcKey;
49bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor  return false;
50dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner}
51dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
52dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner/// getObjCKeywordID - Return the ObjC keyword kind.
53dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattnertok::ObjCKeywordKind Token::getObjCKeywordID() const {
54dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner  IdentifierInfo *specId = getIdentifierInfo();
55dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner  return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
56dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner}
57dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
5853702cd401b8fdca985aede7732c2f6a82ad9b1cChris Lattner
59dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===//
60dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner// Lexer Class Implementation
61dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===//
62dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
6399ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid Lexer::anchor() { }
6499ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Lexer::InitLexer(const char *BufStart, const char *BufPtr,
6622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner                      const char *BufEnd) {
67a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner  InitCharacterInfo();
681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  BufferStart = BufStart;
7022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  BufferPtr = BufPtr;
7122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  BufferEnd = BufEnd;
721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  assert(BufEnd[0] == 0 &&
7422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner         "We assume that the input buffer has a null character at the end"
7522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner         " to simplify lexing!");
761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
77156119df1d076b63609618976281961283f871dbEric Christopher  // Check whether we have a BOM in the beginning of the buffer. If yes - act
78156119df1d076b63609618976281961283f871dbEric Christopher  // accordingly. Right now we support only UTF-8 with and without BOM, so, just
79156119df1d076b63609618976281961283f871dbEric Christopher  // skip the UTF-8 BOM if it's present.
80156119df1d076b63609618976281961283f871dbEric Christopher  if (BufferStart == BufferPtr) {
81156119df1d076b63609618976281961283f871dbEric Christopher    // Determine the size of the BOM.
825f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    StringRef Buf(BufferStart, BufferEnd - BufferStart);
83969f9d47338fc36ebb6d24ad3a51e45eda07fd58Eli Friedman    size_t BOMLength = llvm::StringSwitch<size_t>(Buf)
84156119df1d076b63609618976281961283f871dbEric Christopher      .StartsWith("\xEF\xBB\xBF", 3) // UTF-8 BOM
85156119df1d076b63609618976281961283f871dbEric Christopher      .Default(0);
86156119df1d076b63609618976281961283f871dbEric Christopher
87156119df1d076b63609618976281961283f871dbEric Christopher    // Skip the BOM.
88156119df1d076b63609618976281961283f871dbEric Christopher    BufferPtr += BOMLength;
89156119df1d076b63609618976281961283f871dbEric Christopher  }
90156119df1d076b63609618976281961283f871dbEric Christopher
9122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  Is_PragmaLexer = false;
92d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  CurrentConflictMarkerState = CMK_None;
93156119df1d076b63609618976281961283f871dbEric Christopher
9422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // Start of the file is a start of line.
9522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  IsAtStartOfLine = true;
961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // We are not after parsing a #.
9822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  ParsingPreprocessorDirective = false;
991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // We are not after parsing #include.
10122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  ParsingFilename = false;
1021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // We are not in raw mode.  Raw mode disables diagnostics and interpretation
10422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // of tokens (e.g. identifiers, thus disabling macro expansion).  It is used
10522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
10622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // or otherwise skipping over tokens.
10722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  LexingRawMode = false;
1081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // Default to not keeping comments.
11022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  ExtendedTokenMode = 0;
11122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner}
11222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
1130770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// Lexer constructor - Create a new lexer object for the specified buffer
1140770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// with the specified preprocessor managing the lexing process.  This lexer
1150770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// assumes that the associated file buffer and Preprocessor objects will
1160770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// outlive it, so it doesn't take ownership of either of them.
1176e2901407bff59aeb4cc301cc58b034723d0eb49Chris LattnerLexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP)
11888d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner  : PreprocessorLexer(&PP, FID),
11988d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner    FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
1204e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    LangOpts(PP.getLangOpts()) {
1211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1220770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner  InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
1230770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner            InputFile->getBufferEnd());
1241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1250770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner  // Default to keeping comments if the preprocessor wants them.
1260770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner  SetCommentRetentionState(PP.getCommentRetentionState());
1270770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner}
128dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
129168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner/// Lexer constructor - Create a new raw lexer object.  This object is only
130092bf67e5ca560d2fc6aa70be1f172b8b3a5ff96Dmitri Gribenko/// suitable for calls to 'LexFromRawLexer'.  This lexer assumes that the text
131590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner/// range will outlive it, so it doesn't take ownership of it.
1324e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid BlaikieLexer::Lexer(SourceLocation fileloc, const LangOptions &langOpts,
133de96c0f29c4cacabe6ea577c61db87c2a85aea6cChris Lattner             const char *BufStart, const char *BufPtr, const char *BufEnd)
1344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  : FileLoc(fileloc), LangOpts(langOpts) {
13522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
13622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  InitLexer(BufStart, BufPtr, BufEnd);
1371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
138168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  // We *are* in raw mode.
139168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  LexingRawMode = true;
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
142025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// Lexer constructor - Create a new raw lexer object.  This object is only
143092bf67e5ca560d2fc6aa70be1f172b8b3a5ff96Dmitri Gribenko/// suitable for calls to 'LexFromRawLexer'.  This lexer assumes that the text
144025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// range will outlive it, so it doesn't take ownership of it.
1456e2901407bff59aeb4cc301cc58b034723d0eb49Chris LattnerLexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile,
1464e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie             const SourceManager &SM, const LangOptions &langOpts)
1474e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  : FileLoc(SM.getLocForStartOfFile(FID)), LangOpts(langOpts) {
148025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner
1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
150025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner            FromFile->getBufferEnd());
1511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
152025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner  // We *are* in raw mode.
153025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner  LexingRawMode = true;
154025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner}
155025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner
15642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
15742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// _Pragma expansion.  This has a variety of magic semantics that this method
15842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// sets up.  It returns a new'd Lexer that must be delete'd when done.
15942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner///
16042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// On entrance to this routine, TokStartLoc is a macro location which has a
16142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// spelling loc that indicates the bytes to be lexed for the token and an
162433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// expansion location that indicates where all lexed tokens should be
16342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// "expanded from".
16442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner///
16542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
16642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// normal lexer that remaps tokens as they fly by.  This would require making
16742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Preprocessor::Lex virtual.  Given that, we could just dump in a magic lexer
16842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// interface that could handle this stuff.  This would pull GetMappedTokenLoc
16942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// out of the critical path of the lexer!
17042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner///
1711eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpLexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
172433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth                                 SourceLocation ExpansionLocStart,
173433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth                                 SourceLocation ExpansionLocEnd,
174bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner                                 unsigned TokLen, Preprocessor &PP) {
17542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  SourceManager &SM = PP.getSourceManager();
17642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
17742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Create the lexer as if we were going to lex the file normally.
178a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner  FileID SpellingFID = SM.getFileID(SpellingLoc);
1796e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner  const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID);
1806e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner  Lexer *L = new Lexer(SpellingFID, InputFile, PP);
1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Now that the lexer is created, change the start/end locations so that we
18342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // just lex the subsection of the file that we want.  This is lexing from a
18442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // scratch buffer.
18542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  const char *StrData = SM.getCharacterData(SpellingLoc);
1861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->BufferPtr = StrData;
18842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->BufferEnd = StrData+TokLen;
1891fa495304c81e03f07f278a47b5efe9317104aabChris Lattner  assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
19042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
19142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Set the SourceLocation with the remapping information.  This ensures that
19242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // GetMappedTokenLoc will remap the tokens as they are lexed.
193bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  L->FileLoc = SM.createExpansionLoc(SM.getLocForStartOfFile(SpellingFID),
194bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                     ExpansionLocStart,
195bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                     ExpansionLocEnd, TokLen);
1961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Ensure that the lexer thinks it is inside a directive, so that end \n will
19884021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  // return an EOD token.
19942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->ParsingPreprocessorDirective = true;
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // This lexer really is for _Pragma.
20242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->Is_PragmaLexer = true;
20342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  return L;
20442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner}
20542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
206168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner
2075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Stringify - Convert the specified string into a C string, with surrounding
2085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ""'s, and with escaped \ and " characters.
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstd::string Lexer::Stringify(const std::string &Str, bool Charify) {
2105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  std::string Result = Str;
2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char Quote = Charify ? '\'' : '"';
2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Result.size(); i != e; ++i) {
2135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Result[i] == '\\' || Result[i] == Quote) {
2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Result.insert(Result.begin()+i, '\\');
2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++i; ++e;
2165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return Result;
2195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
221d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// Stringify - Convert the specified string into a C string by escaping '\'
222d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// and " characters.  This does not add surrounding ""'s to the string.
2235f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid Lexer::Stringify(SmallVectorImpl<char> &Str) {
224d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner  for (unsigned i = 0, e = Str.size(); i != e; ++i) {
225d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner    if (Str[i] == '\\' || Str[i] == '"') {
226d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner      Str.insert(Str.begin()+i, '\\');
227d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner      ++i; ++e;
228d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner    }
229d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner  }
230d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner}
231d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner
232b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner//===----------------------------------------------------------------------===//
233b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner// Token Spelling
234b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner//===----------------------------------------------------------------------===//
235b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
236b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// getSpelling() - Return the 'spelling' of this token.  The spelling of a
237b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// token are the characters used to represent the token in the source file
238b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// after trigraph expansion and escaped-newline folding.  In particular, this
239b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// wants to get the true, uncanonicalized, spelling of things like digraphs
240b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// UCNs, etc.
2415f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerStringRef Lexer::getSpelling(SourceLocation loc,
2425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                   SmallVectorImpl<char> &buffer,
243834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall                                   const SourceManager &SM,
244834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall                                   const LangOptions &options,
245834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall                                   bool *invalid) {
246834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // Break down the source location.
247834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
248834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
249834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // Try to the load the file buffer.
250834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  bool invalidTemp = false;
2515f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
252834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  if (invalidTemp) {
253834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    if (invalid) *invalid = true;
2545f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    return StringRef();
255834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  }
256834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
257834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  const char *tokenBegin = file.data() + locInfo.second;
258834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
259834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // Lex from the start of the given location.
260834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  Lexer lexer(SM.getLocForStartOfFile(locInfo.first), options,
261834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall              file.begin(), tokenBegin, file.end());
262834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  Token token;
263834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  lexer.LexFromRawLexer(token);
264834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
265834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  unsigned length = token.getLength();
266834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
267834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // Common case:  no need for cleaning.
268834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  if (!token.needsCleaning())
2695f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    return StringRef(tokenBegin, length);
270834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
271834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  // Hard case, we need to relex the characters into the string.
272834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  buffer.clear();
273834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  buffer.reserve(length);
274834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
275834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  for (const char *ti = tokenBegin, *te = ti + length; ti != te; ) {
276834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    unsigned charSize;
277834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    buffer.push_back(Lexer::getCharAndSizeNoWarn(ti, charSize, options));
278834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    ti += charSize;
279834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  }
280834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
2815f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  return StringRef(buffer.data(), buffer.size());
282834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall}
283834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
284834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// getSpelling() - Return the 'spelling' of this token.  The spelling of a
285834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// token are the characters used to represent the token in the source file
286834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// after trigraph expansion and escaped-newline folding.  In particular, this
287834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// wants to get the true, uncanonicalized, spelling of things like digraphs
288834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall/// UCNs, etc.
289b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattnerstd::string Lexer::getSpelling(const Token &Tok, const SourceManager &SourceMgr,
2904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                               const LangOptions &LangOpts, bool *Invalid) {
291b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
292b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
293b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  // If this token contains nothing interesting, return it directly.
294b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  bool CharDataInvalid = false;
295b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  const char* TokStart = SourceMgr.getCharacterData(Tok.getLocation(),
296b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner                                                    &CharDataInvalid);
297b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  if (Invalid)
298b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    *Invalid = CharDataInvalid;
299b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  if (CharDataInvalid)
300b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    return std::string();
301b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
302b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  if (!Tok.needsCleaning())
303b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    return std::string(TokStart, TokStart+Tok.getLength());
304b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
305b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  std::string Result;
306b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  Result.reserve(Tok.getLength());
307b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
308b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  // Otherwise, hard case, relex the characters into the string.
309b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
310b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner       Ptr != End; ) {
311b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    unsigned CharSize;
3124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, LangOpts));
313b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    Ptr += CharSize;
314b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  }
315b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  assert(Result.size() != unsigned(Tok.getLength()) &&
316b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner         "NeedsCleaning flag set on something that didn't need cleaning!");
317b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  return Result;
318b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner}
319b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
320b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// getSpelling - This method is used to get the spelling of a token into a
321b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// preallocated buffer, instead of as an std::string.  The caller is required
322b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// to allocate enough space for the token, which is guaranteed to be at least
323b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// Tok.getLength() bytes long.  The actual length of the token is returned.
324b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner///
325b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// Note that this method may do two possible things: it may either fill in
326b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// the buffer specified with characters, or it may *change the input pointer*
327b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// to point to a constant buffer with the data already in it (avoiding a
328b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// copy).  The caller is not allowed to modify the returned buffer pointer
329b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner/// if an internal buffer is returned.
330b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattnerunsigned Lexer::getSpelling(const Token &Tok, const char *&Buffer,
331b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner                            const SourceManager &SourceMgr,
3324e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                            const LangOptions &LangOpts, bool *Invalid) {
333b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
334c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara
335c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  const char *TokStart = 0;
336c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  // NOTE: this has to be checked *before* testing for an IdentifierInfo.
337c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  if (Tok.is(tok::raw_identifier))
338c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    TokStart = Tok.getRawIdentifierData();
339c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  else if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
340c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    // Just return the string from the identifier table, which is very quick.
341b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    Buffer = II->getNameStart();
342b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    return II->getLength();
343b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  }
344c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara
345c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  // NOTE: this can be checked even after testing for an IdentifierInfo.
346b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  if (Tok.isLiteral())
347b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    TokStart = Tok.getLiteralData();
348c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara
349b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  if (TokStart == 0) {
350c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    // Compute the start of the token in the input lexer buffer.
351b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    bool CharDataInvalid = false;
352b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    TokStart = SourceMgr.getCharacterData(Tok.getLocation(), &CharDataInvalid);
353b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    if (Invalid)
354b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner      *Invalid = CharDataInvalid;
355b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    if (CharDataInvalid) {
356b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner      Buffer = "";
357b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner      return 0;
358b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    }
359b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  }
360c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara
361b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  // If this token contains nothing interesting, return it directly.
362b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  if (!Tok.needsCleaning()) {
363b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    Buffer = TokStart;
364b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    return Tok.getLength();
365b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  }
366c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara
367b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  // Otherwise, hard case, relex the characters into the string.
368b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  char *OutBuf = const_cast<char*>(Buffer);
369b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
370b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner       Ptr != End; ) {
371b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    unsigned CharSize;
3724e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, LangOpts);
373b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    Ptr += CharSize;
374b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  }
375b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
376b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner         "NeedsCleaning flag set on something that didn't need cleaning!");
377c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara
378b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  return OutBuf-Buffer;
379b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner}
380b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
381b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
382b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
38333e9abd21083a0191a7676a04b497006d2da184dDouglas Gregorstatic bool isWhitespace(unsigned char c);
3845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3859a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// MeasureTokenLength - Relex the token at the specified location and return
3869a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// its length in bytes in the input file.  If the token needs cleaning (e.g.
3879a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// includes a trigraph or an escaped newline) then this count includes bytes
3889a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// that are part of that.
3899a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattnerunsigned Lexer::MeasureTokenLength(SourceLocation Loc,
3902c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner                                   const SourceManager &SM,
3912c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner                                   const LangOptions &LangOpts) {
3929a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // TODO: this could be special cased for common tokens like identifiers, ')',
3939a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // etc to make this faster, if it mattered.  Just look at StrData[0] to handle
3941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // all obviously single-char tokens.  This could use
3959a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
3969a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // something.
397de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
398de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // If this comes from a macro expansion, we really do want the macro name, not
399de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // the token this macro expanded to.
400402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  Loc = SM.getExpansionLoc(Loc);
401363fdc29656cc03c1817268888f95e6343470aa8Chris Lattner  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
402f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  bool Invalid = false;
4035f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
404f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  if (Invalid)
405aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregor    return 0;
406f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer
407f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer  const char *StrData = Buffer.data()+LocInfo.second;
4088350394c65b81bba3986dfe44ae17423873741deChris Lattner
40933e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  if (isWhitespace(StrData[0]))
41033e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    return 0;
41133e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor
4129a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // Create a lexer starting at the beginning of this token.
413c3526d89ef9c31639ec8b25180cfb22354344241Sebastian Redl  Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts,
414c3526d89ef9c31639ec8b25180cfb22354344241Sebastian Redl                 Buffer.begin(), StrData, Buffer.end());
41539de7409bffb6b725a8aa64f0ba77ab51e8c9eb3Chris Lattner  TheLexer.SetCommentRetentionState(true);
4169a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  Token TheTok;
417590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner  TheLexer.LexFromRawLexer(TheTok);
4189a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  return TheTok.getLength();
4199a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner}
4209a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner
4210e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidisstatic SourceLocation getBeginningOfFileToken(SourceLocation Loc,
4220e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis                                              const SourceManager &SM,
4230e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis                                              const LangOptions &LangOpts) {
4240e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis  assert(Loc.isFileID());
425a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
4263de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor  if (LocInfo.first.isInvalid())
4273de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor    return Loc;
4283de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor
429a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  bool Invalid = false;
4305f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
431a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  if (Invalid)
432a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    return Loc;
433a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
434a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  // Back up from the current location until we hit the beginning of a line
435a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  // (or the buffer). We'll relex from that point.
436a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  const char *BufStart = Buffer.data();
4373de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor  if (LocInfo.second >= Buffer.size())
4383de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor    return Loc;
4393de84241d90f3dd280126fdf2c4651667151c967Douglas Gregor
440a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  const char *StrData = BufStart+LocInfo.second;
441a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  if (StrData[0] == '\n' || StrData[0] == '\r')
442a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    return Loc;
443a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
444a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  const char *LexStart = StrData;
445a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  while (LexStart != BufStart) {
446a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    if (LexStart[0] == '\n' || LexStart[0] == '\r') {
447a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      ++LexStart;
448a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      break;
449a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    }
450a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
451a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    --LexStart;
452a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  }
453a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
454a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  // Create a lexer starting at the beginning of this token.
455a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  SourceLocation LexerStartLoc = Loc.getLocWithOffset(-LocInfo.second);
456a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  Lexer TheLexer(LexerStartLoc, LangOpts, BufStart, LexStart, Buffer.end());
457a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  TheLexer.SetCommentRetentionState(true);
458a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
459a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  // Lex tokens until we find the token that contains the source location.
460a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  Token TheTok;
461a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  do {
462a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    TheLexer.LexFromRawLexer(TheTok);
463a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
464a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    if (TheLexer.getBufferLocation() > StrData) {
465a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      // Lexing this token has taken the lexer past the source location we're
466a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      // looking for. If the current token encompasses our source location,
467a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      // return the beginning of that token.
468a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      if (TheLexer.getBufferLocation() - TheTok.getLength() <= StrData)
469a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor        return TheTok.getLocation();
470a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
471a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      // We ended up skipping over the source location entirely, which means
472a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      // that it points into whitespace. We're done here.
473a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor      break;
474a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor    }
475a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  } while (TheTok.getKind() != tok::eof);
476a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
477a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  // We've passed our source location; just return the original source location.
478a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  return Loc;
479a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor}
480a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
4810e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios KyrtzidisSourceLocation Lexer::GetBeginningOfToken(SourceLocation Loc,
4820e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis                                          const SourceManager &SM,
4830e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis                                          const LangOptions &LangOpts) {
4840e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis if (Loc.isFileID())
4850e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis   return getBeginningOfFileToken(Loc, SM, LangOpts);
4860e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis
4870e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis if (!SM.isMacroArgExpansion(Loc))
4880e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis   return Loc;
4890e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis
4900e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis SourceLocation FileLoc = SM.getSpellingLoc(Loc);
4910e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis SourceLocation BeginFileLoc = getBeginningOfFileToken(FileLoc, SM, LangOpts);
4920e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis std::pair<FileID, unsigned> FileLocInfo = SM.getDecomposedLoc(FileLoc);
493ae9f85b2c062ac20512a484cfa6e66239dd3d0d9Chandler Carruth std::pair<FileID, unsigned> BeginFileLocInfo
494ae9f85b2c062ac20512a484cfa6e66239dd3d0d9Chandler Carruth   = SM.getDecomposedLoc(BeginFileLoc);
4950e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis assert(FileLocInfo.first == BeginFileLocInfo.first &&
4960e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis        FileLocInfo.second >= BeginFileLocInfo.second);
497ae9f85b2c062ac20512a484cfa6e66239dd3d0d9Chandler Carruth return Loc.getLocWithOffset(BeginFileLocInfo.second - FileLocInfo.second);
4980e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis}
4990e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6Argyrios Kyrtzidis
500f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregornamespace {
501f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  enum PreambleDirectiveKind {
502f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    PDK_Skipped,
503f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    PDK_StartIf,
504f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    PDK_EndIf,
505f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    PDK_Unknown
506f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  };
507f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor}
508f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
509f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregorstd::pair<unsigned, bool>
51003c107a42fae79e89d0016999a1a04c07d65591aArgyrios KyrtzidisLexer::ComputePreamble(const llvm::MemoryBuffer *Buffer,
5114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                       const LangOptions &LangOpts, unsigned MaxLines) {
512f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  // Create a lexer starting at the beginning of the file. Note that we use a
513f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  // "fake" file source location at offset 1 so that the lexer will track our
514f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  // position within the file.
515f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  const unsigned StartOffset = 1;
5161cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis  SourceLocation FileLoc = SourceLocation::getFromRawEncoding(StartOffset);
5171cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis  Lexer TheLexer(FileLoc, LangOpts, Buffer->getBufferStart(),
518f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor                 Buffer->getBufferStart(), Buffer->getBufferEnd());
5191cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis
5201cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis  // StartLoc will differ from FileLoc if there is a BOM that was skipped.
5211cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis  SourceLocation StartLoc = TheLexer.getSourceLocation();
5221cb7142b66ac1844b91511314cce2b309a9a180dArgyrios Kyrtzidis
523f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  bool InPreprocessorDirective = false;
524f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  Token TheTok;
525f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  Token IfStartTok;
526f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  unsigned IfCount = 0;
527c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis
528c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis  unsigned MaxLineOffset = 0;
529c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis  if (MaxLines) {
530c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis    const char *CurPtr = Buffer->getBufferStart();
531c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis    unsigned CurLine = 0;
532c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis    while (CurPtr != Buffer->getBufferEnd()) {
533c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis      char ch = *CurPtr++;
534c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis      if (ch == '\n') {
535c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis        ++CurLine;
536c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis        if (CurLine == MaxLines)
537c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis          break;
538c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis      }
539c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis    }
540c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis    if (CurPtr != Buffer->getBufferEnd())
541c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis      MaxLineOffset = CurPtr - Buffer->getBufferStart();
542c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis  }
543df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
544f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  do {
545f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    TheLexer.LexFromRawLexer(TheTok);
546f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
547f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    if (InPreprocessorDirective) {
548f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // If we've hit the end of the file, we're done.
549f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      if (TheTok.getKind() == tok::eof) {
550f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        break;
551f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      }
552f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
553f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // If we haven't hit the end of the preprocessor directive, skip this
554f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // token.
555f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      if (!TheTok.isAtStartOfLine())
556f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        continue;
557f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
558f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // We've passed the end of the preprocessor directive, and will look
559f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // at this token again below.
560f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      InPreprocessorDirective = false;
561f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    }
562f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
563df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // Keep track of the # of lines in the preamble.
564df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    if (TheTok.isAtStartOfLine()) {
565c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis      unsigned TokOffset = TheTok.getLocation().getRawEncoding() - StartOffset;
566df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
567df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor      // If we were asked to limit the number of lines in the preamble,
568df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor      // and we're about to exceed that limit, we're done.
569c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis      if (MaxLineOffset && TokOffset >= MaxLineOffset)
570df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor        break;
571df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    }
572df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
573f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    // Comments are okay; skip over them.
574f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    if (TheTok.getKind() == tok::comment)
575f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      continue;
576f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
577f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) {
578f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // This is the start of a preprocessor directive.
579f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      Token HashTok = TheTok;
580f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      InPreprocessorDirective = true;
581f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
58219207f1e5f51261a33492602501fb7ada50ea546Joerg Sonnenberger      // Figure out which directive this is. Since we're lexing raw tokens,
583f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // we don't have an identifier table available. Instead, just look at
584f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // the raw identifier to recognize and categorize preprocessor directives.
585f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      TheLexer.LexFromRawLexer(TheTok);
586c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara      if (TheTok.getKind() == tok::raw_identifier && !TheTok.needsCleaning()) {
5875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        StringRef Keyword(TheTok.getRawIdentifierData(),
588c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara                                TheTok.getLength());
589f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        PreambleDirectiveKind PDK
590f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          = llvm::StringSwitch<PreambleDirectiveKind>(Keyword)
591f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("include", PDK_Skipped)
592f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("__include_macros", PDK_Skipped)
593f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("define", PDK_Skipped)
594f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("undef", PDK_Skipped)
595f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("line", PDK_Skipped)
596f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("error", PDK_Skipped)
597f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("pragma", PDK_Skipped)
598f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("import", PDK_Skipped)
599f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("include_next", PDK_Skipped)
600f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("warning", PDK_Skipped)
601f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("ident", PDK_Skipped)
602f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("sccs", PDK_Skipped)
603f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("assert", PDK_Skipped)
604f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("unassert", PDK_Skipped)
605f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("if", PDK_StartIf)
606f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("ifdef", PDK_StartIf)
607f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("ifndef", PDK_StartIf)
608f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("elif", PDK_Skipped)
609f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("else", PDK_Skipped)
610f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("endif", PDK_EndIf)
611f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Default(PDK_Unknown);
612f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
613f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        switch (PDK) {
614f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_Skipped:
615f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          continue;
616f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
617f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_StartIf:
618f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          if (IfCount == 0)
619f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor            IfStartTok = HashTok;
620f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
621f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          ++IfCount;
622f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          continue;
623f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
624f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_EndIf:
625f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          // Mismatched #endif. The preamble ends here.
626f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          if (IfCount == 0)
627f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor            break;
628f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
629f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          --IfCount;
630f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          continue;
631f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
632f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_Unknown:
633f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          // We don't know what this directive is; stop at the '#'.
634f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          break;
635f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        }
636f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      }
637f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
638f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // We only end up here if we didn't recognize the preprocessor
639f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // directive or it was one that can't occur in the preamble at this
640f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // point. Roll back the current token to the location of the '#'.
641f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      InPreprocessorDirective = false;
642f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      TheTok = HashTok;
643f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    }
644f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
645df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // We hit a token that we don't recognize as being in the
646df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // "preprocessing only" part of the file, so we're no longer in
647df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // the preamble.
648f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    break;
649f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  } while (true);
650f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
651f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation();
652f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(),
653f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                        IfCount? IfStartTok.isAtStartOfLine()
654f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                               : TheTok.isAtStartOfLine());
655f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor}
656f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
6577ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6587ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// AdvanceToTokenCharacter - Given a location that specifies the start of a
6597ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token, return a new location that specifies a character within the token.
6607ef5c27eb6e8ebe58b52013246c06753c3613263Chris LattnerSourceLocation Lexer::AdvanceToTokenCharacter(SourceLocation TokStart,
6617ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner                                              unsigned CharNo,
6627ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner                                              const SourceManager &SM,
6634e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                                              const LangOptions &LangOpts) {
664433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  // Figure out how many physical characters away the specified expansion
6657ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // character is.  This needs to take into consideration newlines and
6667ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // trigraphs.
6677ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  bool Invalid = false;
6687ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  const char *TokPtr = SM.getCharacterData(TokStart, &Invalid);
6697ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6707ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // If they request the first char of the token, we're trivially done.
6717ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  if (Invalid || (CharNo == 0 && Lexer::isObviouslySimpleCharacter(*TokPtr)))
6727ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    return TokStart;
6737ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6747ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  unsigned PhysOffset = 0;
6757ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6767ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // The usual case is that tokens don't contain anything interesting.  Skip
6777ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // over the uninteresting characters.  If a token only consists of simple
6787ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // chars, this method is extremely fast.
6797ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  while (Lexer::isObviouslySimpleCharacter(*TokPtr)) {
6807ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    if (CharNo == 0)
681a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis      return TokStart.getLocWithOffset(PhysOffset);
6827ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    ++TokPtr, --CharNo, ++PhysOffset;
6837ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  }
6847ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6857ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // If we have a character that may be a trigraph or escaped newline, use a
6867ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // lexer to parse it correctly.
6877ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  for (; CharNo; --CharNo) {
6887ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    unsigned Size;
6894e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    Lexer::getCharAndSizeNoWarn(TokPtr, Size, LangOpts);
6907ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    TokPtr += Size;
6917ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    PhysOffset += Size;
6927ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  }
6937ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6947ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // Final detail: if we end up on an escaped newline, we want to return the
6957ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // location of the actual byte of the token.  For example foo\<newline>bar
6967ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // advanced by 3 should return the location of b, not of \\.  One compounding
6977ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // detail of this is that the escape may be made by a trigraph.
6987ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  if (!Lexer::isObviouslySimpleCharacter(*TokPtr))
6997ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    PhysOffset += Lexer::SkipEscapedNewLines(TokPtr)-TokPtr;
7007ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
701a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  return TokStart.getLocWithOffset(PhysOffset);
7027ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner}
7037ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
7047ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// \brief Computes the source location just past the end of the
7057ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token at this source location.
7067ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner///
7077ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// This routine can be used to produce a source location that
7087ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// points just past the end of the token referenced by \p Loc, and
7097ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// is generally used when a diagnostic needs to point just after a
7107ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token where it expected something different that it received. If
7117ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// the returned source location would not be meaningful (e.g., if
7127ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// it points into a macro), this routine returns an invalid
7137ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// source location.
7147ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner///
7157ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// \param Offset an offset from the end of the token, where the source
7167ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// location should refer to. The default offset (0) produces a source
7177ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// location pointing just past the end of the token; an offset of 1 produces
7187ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// a source location pointing to the last character in the token, etc.
7197ef5c27eb6e8ebe58b52013246c06753c3613263Chris LattnerSourceLocation Lexer::getLocForEndOfToken(SourceLocation Loc, unsigned Offset,
7207ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner                                          const SourceManager &SM,
7214e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                                          const LangOptions &LangOpts) {
7227ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis  if (Loc.isInvalid())
7237ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    return SourceLocation();
7247ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis
7257ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis  if (Loc.isMacroID()) {
7264e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Offset > 0 || !isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc))
727433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth      return SourceLocation(); // Points inside the macro expansion.
7287ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis  }
7297ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis
7304e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  unsigned Len = Lexer::MeasureTokenLength(Loc, SM, LangOpts);
7317ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  if (Len > Offset)
7327ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    Len = Len - Offset;
7337ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  else
7347ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    return Loc;
7357ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
736a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  return Loc.getLocWithOffset(Len);
7377ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner}
7387ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
7397a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis/// \brief Returns true if the given MacroID location points at the first
740433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// token of the macro expansion.
741433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruthbool Lexer::isAtStartOfMacroExpansion(SourceLocation loc,
742f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                      const SourceManager &SM,
74369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                      const LangOptions &LangOpts,
74469bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                      SourceLocation *MacroBegin) {
7457a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc");
7467a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
7477a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  std::pair<FileID, unsigned> infoLoc = SM.getDecomposedLoc(loc);
7487a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  // FIXME: If the token comes from the macro token paste operator ('##')
7497a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  // this function will always return false;
7507a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  if (infoLoc.second > 0)
7517a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis    return false; // Does not point at the start of token.
7527a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
753433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  SourceLocation expansionLoc =
7541728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth    SM.getSLocEntry(infoLoc.first).getExpansion().getExpansionLocStart();
75569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  if (expansionLoc.isFileID()) {
75669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    // No other macro expansions, this is the first.
75769bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    if (MacroBegin)
75869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis      *MacroBegin = expansionLoc;
75969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    return true;
76069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  }
7617a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
76269bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  return isAtStartOfMacroExpansion(expansionLoc, SM, LangOpts, MacroBegin);
7637a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis}
7647a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
7657a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis/// \brief Returns true if the given MacroID location points at the last
766433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// token of the macro expansion.
767433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruthbool Lexer::isAtEndOfMacroExpansion(SourceLocation loc,
76869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                    const SourceManager &SM,
76969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                    const LangOptions &LangOpts,
77069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                    SourceLocation *MacroEnd) {
7717a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc");
7727a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
7737a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  SourceLocation spellLoc = SM.getSpellingLoc(loc);
7747a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  unsigned tokLen = MeasureTokenLength(spellLoc, SM, LangOpts);
7757a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  if (tokLen == 0)
7767a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis    return false;
7777a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
7787a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  FileID FID = SM.getFileID(loc);
779a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  SourceLocation afterLoc = loc.getLocWithOffset(tokLen+1);
780f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis  if (SM.isInFileID(afterLoc, FID))
781f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis    return false; // Still in the same FileID, does not point to the last token.
7827a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
7837a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  // FIXME: If the token comes from the macro token paste operator ('##')
7847a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  // or the stringify operator ('#') this function will always return false;
785f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis
786433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  SourceLocation expansionLoc =
7871728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth    SM.getSLocEntry(FID).getExpansion().getExpansionLocEnd();
78869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  if (expansionLoc.isFileID()) {
78969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    // No other macro expansions.
79069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    if (MacroEnd)
79169bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis      *MacroEnd = expansionLoc;
79269bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    return true;
79369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  }
7947a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
79569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  return isAtEndOfMacroExpansion(expansionLoc, SM, LangOpts, MacroEnd);
7967a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis}
7977a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
798a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidisstatic CharSourceRange makeRangeFromFileLocs(CharSourceRange Range,
799d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis                                             const SourceManager &SM,
800d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis                                             const LangOptions &LangOpts) {
801a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  SourceLocation Begin = Range.getBegin();
802a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  SourceLocation End = Range.getEnd();
803d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  assert(Begin.isFileID() && End.isFileID());
804a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  if (Range.isTokenRange()) {
805a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    End = Lexer::getLocForEndOfToken(End, 0, SM,LangOpts);
806a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    if (End.isInvalid())
807a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis      return CharSourceRange();
808a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  }
809d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis
810d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  // Break down the source locations.
811d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  FileID FID;
812d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  unsigned BeginOffs;
813d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  llvm::tie(FID, BeginOffs) = SM.getDecomposedLoc(Begin);
814d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (FID.isInvalid())
815d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis    return CharSourceRange();
816d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis
817d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  unsigned EndOffs;
818d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (!SM.isInFileID(End, FID, &EndOffs) ||
819d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis      BeginOffs > EndOffs)
820d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis    return CharSourceRange();
821d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis
822d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  return CharSourceRange::getCharRange(Begin, End);
823d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis}
824d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis
825a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios KyrtzidisCharSourceRange Lexer::makeFileCharRange(CharSourceRange Range,
82611b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis                                         const SourceManager &SM,
82711b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis                                         const LangOptions &LangOpts) {
828a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  SourceLocation Begin = Range.getBegin();
829a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  SourceLocation End = Range.getEnd();
830d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (Begin.isInvalid() || End.isInvalid())
83111b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis    return CharSourceRange();
83211b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis
833d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (Begin.isFileID() && End.isFileID())
834a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    return makeRangeFromFileLocs(Range, SM, LangOpts);
835d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis
836d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (Begin.isMacroID() && End.isFileID()) {
83711b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis    if (!isAtStartOfMacroExpansion(Begin, SM, LangOpts, &Begin))
83811b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis      return CharSourceRange();
839a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    Range.setBegin(Begin);
840a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    return makeRangeFromFileLocs(Range, SM, LangOpts);
841d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  }
84211b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis
843d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (Begin.isFileID() && End.isMacroID()) {
844a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    if ((Range.isTokenRange() && !isAtEndOfMacroExpansion(End, SM, LangOpts,
845a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis                                                          &End)) ||
846a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis        (Range.isCharRange() && !isAtStartOfMacroExpansion(End, SM, LangOpts,
847a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis                                                           &End)))
848d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis      return CharSourceRange();
849a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    Range.setEnd(End);
850a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    return makeRangeFromFileLocs(Range, SM, LangOpts);
851d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  }
85211b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis
853d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  assert(Begin.isMacroID() && End.isMacroID());
854d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  SourceLocation MacroBegin, MacroEnd;
855d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (isAtStartOfMacroExpansion(Begin, SM, LangOpts, &MacroBegin) &&
856a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis      ((Range.isTokenRange() && isAtEndOfMacroExpansion(End, SM, LangOpts,
857a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis                                                        &MacroEnd)) ||
858a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis       (Range.isCharRange() && isAtStartOfMacroExpansion(End, SM, LangOpts,
859a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis                                                         &MacroEnd)))) {
860a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    Range.setBegin(MacroBegin);
861a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    Range.setEnd(MacroEnd);
862a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    return makeRangeFromFileLocs(Range, SM, LangOpts);
863a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  }
864d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis
865d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  FileID FID;
866d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  unsigned BeginOffs;
867d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  llvm::tie(FID, BeginOffs) = SM.getDecomposedLoc(Begin);
868d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (FID.isInvalid())
869e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    return CharSourceRange();
870e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis
87111b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis  unsigned EndOffs;
872d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (!SM.isInFileID(End, FID, &EndOffs) ||
873d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis      BeginOffs > EndOffs)
87411b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis    return CharSourceRange();
87511b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis
876d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  const SrcMgr::SLocEntry *E = &SM.getSLocEntry(FID);
877d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();
878d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (Expansion.isMacroArgExpansion() &&
879d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis      Expansion.getSpellingLoc().isFileID()) {
880d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis    SourceLocation SpellLoc = Expansion.getSpellingLoc();
881a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    Range.setBegin(SpellLoc.getLocWithOffset(BeginOffs));
882a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    Range.setEnd(SpellLoc.getLocWithOffset(EndOffs));
883a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    return makeRangeFromFileLocs(Range, SM, LangOpts);
884d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  }
885d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis
886d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  return CharSourceRange();
88711b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis}
88811b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis
889e64d9037658a1b95c79ea275af6167a110b3c563Argyrios KyrtzidisStringRef Lexer::getSourceText(CharSourceRange Range,
890e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis                               const SourceManager &SM,
891e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis                               const LangOptions &LangOpts,
892e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis                               bool *Invalid) {
893a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  Range = makeFileCharRange(Range, SM, LangOpts);
894a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  if (Range.isInvalid()) {
895e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    if (Invalid) *Invalid = true;
896e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    return StringRef();
897e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  }
898e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis
899e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  // Break down the source location.
900e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  std::pair<FileID, unsigned> beginInfo = SM.getDecomposedLoc(Range.getBegin());
901e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  if (beginInfo.first.isInvalid()) {
902e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    if (Invalid) *Invalid = true;
903e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    return StringRef();
904e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  }
905e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis
906e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  unsigned EndOffs;
907e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  if (!SM.isInFileID(Range.getEnd(), beginInfo.first, &EndOffs) ||
908e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis      beginInfo.second > EndOffs) {
909e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    if (Invalid) *Invalid = true;
910e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    return StringRef();
911e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  }
912e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis
913e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  // Try to the load the file buffer.
914e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  bool invalidTemp = false;
915e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  StringRef file = SM.getBufferData(beginInfo.first, &invalidTemp);
916e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  if (invalidTemp) {
917e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    if (Invalid) *Invalid = true;
918e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    return StringRef();
919e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  }
920e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis
921e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  if (Invalid) *Invalid = false;
922e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  return file.substr(beginInfo.second, EndOffs - beginInfo.second);
923e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis}
924e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis
925c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna ZaksStringRef Lexer::getImmediateMacroName(SourceLocation Loc,
926c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks                                       const SourceManager &SM,
927c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks                                       const LangOptions &LangOpts) {
928c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  assert(Loc.isMacroID() && "Only reasonble to call this on macros");
9297f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis
9307f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis  // Find the location of the immediate macro expansion.
9317f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis  while (1) {
9327f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    FileID FID = SM.getFileID(Loc);
9337f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    const SrcMgr::SLocEntry *E = &SM.getSLocEntry(FID);
9347f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();
9357f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    Loc = Expansion.getExpansionLocStart();
9367f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    if (!Expansion.isMacroArgExpansion())
9377f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis      break;
9387f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis
9397f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    // For macro arguments we need to check that the argument did not come
9407f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    // from an inner macro, e.g: "MAC1( MAC2(foo) )"
9417f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis
9427f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    // Loc points to the argument id of the macro definition, move to the
9437f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    // macro expansion.
944c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks    Loc = SM.getImmediateExpansionRange(Loc).first;
9457f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    SourceLocation SpellLoc = Expansion.getSpellingLoc();
9467f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    if (SpellLoc.isFileID())
9477f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis      break; // No inner macro.
9487f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis
9497f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    // If spelling location resides in the same FileID as macro expansion
9507f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    // location, it means there is no inner macro.
9517f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    FileID MacroFID = SM.getFileID(Loc);
9527f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    if (SM.isInFileID(SpellLoc, MacroFID))
9537f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis      break;
9547f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis
9557f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    // Argument came from inner macro.
9567f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    Loc = SpellLoc;
9577f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis  }
958c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks
959c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  // Find the spelling location of the start of the non-argument expansion
960c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  // range. This is where the macro name was spelled in order to begin
961c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  // expanding this macro.
9627f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis  Loc = SM.getSpellingLoc(Loc);
963c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks
964c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  // Dig out the buffer where the macro name was spelled and the extents of the
965c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  // name so that we can render it into the expansion note.
966c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  std::pair<FileID, unsigned> ExpansionInfo = SM.getDecomposedLoc(Loc);
967c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  unsigned MacroTokenLength = Lexer::MeasureTokenLength(Loc, SM, LangOpts);
968c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first);
969c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  return ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength);
970c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks}
971c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks
9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
9735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Character information.
9745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
9755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum {
9775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_HORZ_WS  = 0x01,  // ' ', '\t', '\f', '\v'.  Note, no '\0'
9785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_VERT_WS  = 0x02,  // '\r', '\n'
9795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_LETTER   = 0x04,  // a-z,A-Z
9805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_NUMBER   = 0x08,  // 0-9
9815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_UNDER    = 0x10,  // _
9822fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  CHAR_PERIOD   = 0x20,  // .
9832fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  CHAR_RAWDEL   = 0x40   // {}[]#<>%:;?*+-/^&|~!=,"'
9845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
9855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
98603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// Statically initialize CharInfo table based on ASCII character set
98703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// Reference: FreeBSD 7.2 /usr/share/misc/ascii
988a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic const unsigned char CharInfo[256] =
98903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner{
99003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 0 NUL         1 SOH         2 STX         3 ETX
99103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 4 EOT         5 ENQ         6 ACK         7 BEL
99203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
99303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
99403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 8 BS          9 HT         10 NL         11 VT
99503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//12 NP         13 CR         14 SO         15 SI
99603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
99703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_HORZ_WS, CHAR_VERT_WS, 0           , 0           ,
99803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//16 DLE        17 DC1        18 DC2        19 DC3
99903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//20 DC4        21 NAK        22 SYN        23 ETB
100003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
100103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
100203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//24 CAN        25 EM         26 SUB        27 ESC
100303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//28 FS         29 GS         30 RS         31 US
100403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
100503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
100603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//32 SP         33  !         34  "         35  #
100703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//36  $         37  %         38  &         39  '
10082fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   CHAR_HORZ_WS, CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL ,
10092fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   0           , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL ,
101003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//40  (         41  )         42  *         43  +
101103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//44  ,         45  -         46  .         47  /
10122fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   0           , 0           , CHAR_RAWDEL , CHAR_RAWDEL ,
10132fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   CHAR_RAWDEL , CHAR_RAWDEL , CHAR_PERIOD , CHAR_RAWDEL ,
101403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//48  0         49  1         50  2         51  3
101503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//52  4         53  5         54  6         55  7
101603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
101703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
101803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//56  8         57  9         58  :         59  ;
101903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//60  <         61  =         62  >         63  ?
10202fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   CHAR_NUMBER , CHAR_NUMBER , CHAR_RAWDEL , CHAR_RAWDEL ,
10212fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL ,
102203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//64  @         65  A         66  B         67  C
102303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//68  D         69  E         70  F         71  G
102403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
102503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
102603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//72  H         73  I         74  J         75  K
102703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//76  L         77  M         78  N         79  O
102803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
102903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
103003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//80  P         81  Q         82  R         83  S
103103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//84  T         85  U         86  V         87  W
103203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
103303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
103403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//88  X         89  Y         90  Z         91  [
103503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//92  \         93  ]         94  ^         95  _
10362fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_RAWDEL ,
10372fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   0           , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_UNDER  ,
103803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//96  `         97  a         98  b         99  c
103903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//100  d       101  e        102  f        103  g
104003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
104103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
104203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//104  h       105  i        106  j        107  k
104303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//108  l       109  m        110  n        111  o
104403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
104503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
104603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//112  p       113  q        114  r        115  s
104703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//116  t       117  u        118  v        119  w
104803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
104903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
105003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//120  x       121  y        122  z        123  {
10512fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper//124  |       125  }        126  ~        127 DEL
10522fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_RAWDEL ,
10532fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , 0
105403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner};
105503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner
1056a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic void InitCharacterInfo() {
10575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static bool isInited = false;
10585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (isInited) return;
105903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  // check the statically-initialized CharInfo table
106003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
106103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
106203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
106303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
106403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
106503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
106603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_UNDER   == CharInfo[(int)'_']);
106703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_PERIOD  == CharInfo[(int)'.']);
106803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  for (unsigned i = 'a'; i <= 'z'; ++i) {
106903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner    assert(CHAR_LETTER == CharInfo[i]);
107003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner    assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
107103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  }
10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = '0'; i <= '9'; ++i)
107303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner    assert(CHAR_NUMBER == CharInfo[i]);
10747b682656310b0d7e94a98d982444bc7ddedc653fSteve Naroff
107503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  isInited = true;
10765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
107803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner
10795cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith/// isIdentifierHead - Return true if this is the first character of an
10805cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith/// identifier, which is [a-zA-Z_].
10815cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smithstatic inline bool isIdentifierHead(unsigned char c) {
10825cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  return (CharInfo[c] & (CHAR_LETTER|CHAR_UNDER)) ? true : false;
10835cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith}
10845cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith
10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIdentifierBody - Return true if this is the body character of an
10865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// identifier, which is [a-zA-Z0-9_].
10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isIdentifierBody(unsigned char c) {
108895c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isHorizontalWhitespace - Return true if this character is horizontal
1092a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// whitespace: ' ', '\\t', '\\f', '\\v'.  Note that this returns false for
1093a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// '\\0'.
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isHorizontalWhitespace(unsigned char c) {
109595c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1098aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// isVerticalWhitespace - Return true if this character is vertical
1099a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// whitespace: '\\n', '\\r'.  Note that this returns false for '\\0'.
1100aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaksstatic inline bool isVerticalWhitespace(unsigned char c) {
1101aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  return (CharInfo[c] & CHAR_VERT_WS) ? true : false;
1102aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks}
1103aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isWhitespace - Return true if this character is horizontal or vertical
1105a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// whitespace: ' ', '\\t', '\\f', '\\v', '\\n', '\\r'.  Note that this returns
1106a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// false for '\\0'.
11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isWhitespace(unsigned char c) {
110895c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNumberBody - Return true if this is the body character of an
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// preprocessing number, which is [a-zA-Z0-9_.].
11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isNumberBody(unsigned char c) {
11141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
111595c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser    true : false;
11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11182fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// isRawStringDelimBody - Return true if this is the body character of a
11192fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// raw string delimiter.
11202fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topperstatic inline bool isRawStringDelimBody(unsigned char c) {
11212fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  return (CharInfo[c] &
11222fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD|CHAR_RAWDEL)) ?
11232fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    true : false;
11242fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper}
11252fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
1126d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose// Allow external clients to make use of CharInfo.
1127d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rosebool Lexer::isIdentifierBodyChar(char c, const LangOptions &LangOpts) {
1128d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose  return isIdentifierBody(c) || (c == '$' && LangOpts.DollarIdents);
1129d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose}
1130d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
11335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Diagnostics forwarding code.
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1136409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
1137433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// lexer buffer was all expanded at a single point, perform the mapping.
1138409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// This is currently only used for _Pragma implementation, so it is the slow
1139409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// path of the hot getSourceLocation method.  Do not allow it to be inlined.
114014bd96571ef6f0e97dc79ec4d01b547d60e8fa68Chandler Carruthstatic LLVM_ATTRIBUTE_NOINLINE SourceLocation GetMappedTokenLoc(
114114bd96571ef6f0e97dc79ec4d01b547d60e8fa68Chandler Carruth    Preprocessor &PP, SourceLocation FileLoc, unsigned CharNo, unsigned TokLen);
1142409a03671224d4e5bdab1594c43baf070148f830Chris Lattnerstatic SourceLocation GetMappedTokenLoc(Preprocessor &PP,
1143409a03671224d4e5bdab1594c43baf070148f830Chris Lattner                                        SourceLocation FileLoc,
1144de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                        unsigned CharNo, unsigned TokLen) {
1145433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  assert(FileLoc.isMacroID() && "Must be a macro expansion");
11461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1147409a03671224d4e5bdab1594c43baf070148f830Chris Lattner  // Otherwise, we're lexing "mapped tokens".  This is used for things like
1148433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  // _Pragma handling.  Combine the expansion location of FileLoc with the
1149df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  // spelling location.
1150e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  SourceManager &SM = PP.getSourceManager();
11511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1152433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  // Create a new SLoc which is expanded from Expansion(FileLoc) but whose
1153df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  // characters come from spelling(FileLoc)+Offset.
1154e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
1155a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  SpellingLoc = SpellingLoc.getLocWithOffset(CharNo);
11561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1157e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  // Figure out the expansion loc range, which is the range covered by the
1158e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  // original _Pragma(...) sequence.
1159e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  std::pair<SourceLocation,SourceLocation> II =
1160999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth    SM.getImmediateExpansionRange(FileLoc);
11611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1162bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  return SM.createExpansionLoc(SpellingLoc, II.first, II.second, TokLen);
1163409a03671224d4e5bdab1594c43baf070148f830Chris Lattner}
1164409a03671224d4e5bdab1594c43baf070148f830Chris Lattner
11655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSourceLocation - Return a source location identifier for the specified
11665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// offset in the current file.
1167de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris LattnerSourceLocation Lexer::getSourceLocation(const char *Loc,
1168de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                        unsigned TokLen) const {
1169448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner  assert(Loc >= BufferStart && Loc <= BufferEnd &&
11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer         "Location out of range for this buffer!");
11719dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner
11729dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  // In the normal case, we're just lexing from a simple file buffer, return
11739dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  // the file id from FileLoc with the offset specified.
1174448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner  unsigned CharNo = Loc-BufferStart;
11759dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  if (FileLoc.isFileID())
1176a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis    return FileLoc.getLocWithOffset(CharNo);
11771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11782b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  // Otherwise, this is the _Pragma lexer case, which pretends that all of the
11792b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  // tokens are lexed from where the _Pragma was defined.
1180168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  assert(PP && "This doesn't work on raw lexers");
1181de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
11825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Diag - Forwarding function for diagnostics.  This translate a source
11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// position in the current buffer into a SourceLocation object for rendering.
11863cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
11873692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner  return PP->Diag(getSourceLocation(Loc), DiagID);
11885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
11915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Trigraph and Escaped Newline Handling Code.
11925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char GetTrigraphCharForLetter(char Letter) {
11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Letter) {
11985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:   return 0;
11995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '=':  return '#';
12005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ')':  return ']';
12015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '(':  return '[';
12025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '!':  return '|';
12035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\'': return '^';
12045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '>':  return '}';
12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '/':  return '\\';
12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '<':  return '{';
12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '-':  return '~';
12085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// DecodeTrigraphChar - If the specified character is a legal trigraph when
12125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// prefixed with ??, emit a trigraph warning.  If trigraphs are enabled,
12135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the result character.  Finally, emit a warning about trigraph use
12145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whether trigraphs are enabled or not.
12155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char DecodeTrigraphChar(const char *CP, Lexer *L) {
12165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char Res = GetTrigraphCharForLetter(*CP);
12173692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner  if (!Res || !L) return Res;
12181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12194e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!L->getLangOpts().Trigraphs) {
122074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!L->isLexingRawMode())
122174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      L->Diag(CP-2, diag::trigraph_ignored);
12223692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner    return 0;
12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
122574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (!L->isLexingRawMode())
12265f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    L->Diag(CP-2, diag::trigraph_converted) << StringRef(&Res, 1);
12275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return Res;
12285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
123024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// getEscapedNewLineSize - Return the size of the specified escaped newline,
123124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a
12321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// trigraph equivalent on entry to this function.
123324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattnerunsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
123424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  unsigned Size = 0;
123524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  while (isWhitespace(Ptr[Size])) {
123624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    ++Size;
12371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
123824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
123924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      continue;
124024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
124124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    // If this is a \r\n or \n\r, skip the other half.
124224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
124324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner        Ptr[Size-1] != Ptr[Size])
124424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      ++Size;
12451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
124624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    return Size;
12471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
12481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
124924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  // Not an escaped newline, must be a \t or something else.
125024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  return 0;
125124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner}
125224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
1253033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
1254033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// them), skip over them and return the first non-escaped-newline found,
1255033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// otherwise return P.
1256033749571f8d4c804eeb357c70b06424aa24503bChris Lattnerconst char *Lexer::SkipEscapedNewLines(const char *P) {
1257033749571f8d4c804eeb357c70b06424aa24503bChris Lattner  while (1) {
1258033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    const char *AfterEscape;
1259033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    if (*P == '\\') {
1260033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      AfterEscape = P+1;
1261033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    } else if (*P == '?') {
1262033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      // If not a trigraph for escape, bail out.
1263033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      if (P[1] != '?' || P[2] != '/')
1264033749571f8d4c804eeb357c70b06424aa24503bChris Lattner        return P;
1265033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      AfterEscape = P+3;
1266033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    } else {
1267033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      return P;
1268033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    }
12691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1270033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
1271033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    if (NewLineSize == 0) return P;
1272033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    P = AfterEscape+NewLineSize;
1273033749571f8d4c804eeb357c70b06424aa24503bChris Lattner  }
1274033749571f8d4c804eeb357c70b06424aa24503bChris Lattner}
1275033749571f8d4c804eeb357c70b06424aa24503bChris Lattner
1276aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// \brief Checks that the given token is the first token that occurs after the
1277aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// given location (this excludes comments and whitespace). Returns the location
1278aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// immediately after the specified token. If the token is not found or the
1279aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// location is inside a macro, the returned source location will be invalid.
1280aca25bccefe56121b686706afc84c8cb5d46e65bAnna ZaksSourceLocation Lexer::findLocationAfterToken(SourceLocation Loc,
1281aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks                                        tok::TokenKind TKind,
1282aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks                                        const SourceManager &SM,
1283aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks                                        const LangOptions &LangOpts,
1284aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks                                        bool SkipTrailingWhitespaceAndNewLine) {
1285aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  if (Loc.isMacroID()) {
128669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    if (!Lexer::isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc))
1287aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks      return SourceLocation();
1288aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  }
1289aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, LangOpts);
1290aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
1291aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  // Break down the source location.
1292aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
1293aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
1294aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  // Try to load the file buffer.
1295aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  bool InvalidTemp = false;
1296aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  llvm::StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp);
1297aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  if (InvalidTemp)
1298aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks    return SourceLocation();
1299aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
1300aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  const char *TokenBegin = File.data() + LocInfo.second;
1301aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
1302aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  // Lex from the start of the given location.
1303aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  Lexer lexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(),
1304aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks                                      TokenBegin, File.end());
1305aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  // Find the token.
1306aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  Token Tok;
1307aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  lexer.LexFromRawLexer(Tok);
1308aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  if (Tok.isNot(TKind))
1309aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks    return SourceLocation();
1310aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  SourceLocation TokenLoc = Tok.getLocation();
1311aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
1312aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  // Calculate how much whitespace needs to be skipped if any.
1313aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  unsigned NumWhitespaceChars = 0;
1314aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  if (SkipTrailingWhitespaceAndNewLine) {
1315aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks    const char *TokenEnd = SM.getCharacterData(TokenLoc) +
1316aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks                           Tok.getLength();
1317aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks    unsigned char C = *TokenEnd;
1318aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks    while (isHorizontalWhitespace(C)) {
1319aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks      C = *(++TokenEnd);
1320aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks      NumWhitespaceChars++;
1321aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks    }
1322aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks    if (isVerticalWhitespace(C))
1323aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks      NumWhitespaceChars++;
1324aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  }
1325aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
1326a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  return TokenLoc.getLocWithOffset(Tok.getLength() + NumWhitespaceChars);
1327aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks}
132824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
13295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
13305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// get its size, and return it.  This is tricky in several cases:
13315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   1. If currently at the start of a trigraph, we warn about the trigraph,
13325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      then either return the trigraph (skipping 3 chars) or the '?',
13335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      depending on whether trigraphs are enabled or not.
13345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   2. If this is an escaped newline (potentially with whitespace between
13355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      the backslash and newline), implicitly skip the newline and return
13365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      the char after it.
13375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   3. If this is a UCN, return it.  FIXME: C++ UCN's?
13385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This handles the slow/uncommon case of the getCharAndSize method.  Here we
13405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// know that we can accumulate into Size, and that we have already incremented
13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Ptr by Size bytes.
13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
13445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match.
13455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
13465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
1347d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner                               Token *Tok) {
13485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, look for an escaped newline.
13495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '\\') {
13505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Size;
13515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Ptr;
13525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash:
13535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Common case, backslash-char where the char is not whitespace.
13545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!isWhitespace(Ptr[0])) return '\\';
13551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13565636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner    // See if we have optional whitespace characters between the slash and
13575636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner    // newline.
135824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
135924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Remember that this token needs to be cleaned.
136024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      if (Tok) Tok->setFlag(Token::NeedsCleaning);
136124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
136224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Warn if there was whitespace between the backslash and newline.
13635636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner      if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
136424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner        Diag(Ptr, diag::backslash_newline_space);
13651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
136624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Found backslash<whitespace><newline>.  Parse the char after it.
136724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Size += EscapedNewLineSize;
136824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Ptr  += EscapedNewLineSize;
1369f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis
137004a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      // If the char that we finally got was a \n, then we must have had
137104a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      // something like \<newline><newline>.  We don't want to consume the
137204a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      // second newline.
137304a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0')
137404a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis        return ' ';
1375f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis
137624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Use slow version to accumulate a correct size field.
137724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      return getCharAndSizeSlow(Ptr, Size, Tok);
137824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    }
13791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is not an escaped newline, just return the slash.
13815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return '\\';
13825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a trigraph, process it.
13855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '?' && Ptr[1] == '?') {
13865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is actually a legal trigraph (not something like "??x"), emit
13875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // a trigraph warning.  If so, and if trigraphs are enabled, return it.
13885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
13895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Remember that this token needs to be cleaned.
1390d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner      if (Tok) Tok->setFlag(Token::NeedsCleaning);
13915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Ptr += 3;
13935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Size += 3;
13945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '\\') goto Slash;
13955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return C;
13965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
13975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is neither, return a single character.
14005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++Size;
14015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return *Ptr;
14025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
14065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeNoWarn method.  Here we know that we can accumulate into Size,
14075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// and that we have already incremented Ptr by Size bytes.
14085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
14095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
14105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match.
14115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
14124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                                     const LangOptions &LangOpts) {
14135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, look for an escaped newline.
14145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '\\') {
14155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Size;
14165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Ptr;
14175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash:
14185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Common case, backslash-char where the char is not whitespace.
14195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!isWhitespace(Ptr[0])) return '\\';
14201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // See if we have optional whitespace characters followed by a newline.
142224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
142324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Found backslash<whitespace><newline>.  Parse the char after it.
142424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Size += EscapedNewLineSize;
142524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Ptr  += EscapedNewLineSize;
14261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
142704a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      // If the char that we finally got was a \n, then we must have had
142804a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      // something like \<newline><newline>.  We don't want to consume the
142904a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      // second newline.
143004a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0')
143104a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis        return ' ';
1432f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis
143324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Use slow version to accumulate a correct size field.
14344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      return getCharAndSizeSlowNoWarn(Ptr, Size, LangOpts);
143524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    }
14361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is not an escaped newline, just return the slash.
14385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return '\\';
14395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a trigraph, process it.
14424e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (LangOpts.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
14435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is actually a legal trigraph (not something like "??x"), return
14445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // it.
14455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (char C = GetTrigraphCharForLetter(Ptr[2])) {
14465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Ptr += 3;
14475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Size += 3;
14485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '\\') goto Slash;
14495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return C;
14505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is neither, return a single character.
14545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++Size;
14555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return *Ptr;
14565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
14595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper methods for lexing.
14605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
14615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1462f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor/// \brief Routine that indiscriminately skips bytes in the source file.
1463f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregorvoid Lexer::SkipBytes(unsigned Bytes, bool StartOfLine) {
1464f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  BufferPtr += Bytes;
1465f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  if (BufferPtr > BufferEnd)
1466f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    BufferPtr = BufferEnd;
1467f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  IsAtStartOfLine = StartOfLine;
1468f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor}
1469f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
1470d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
14715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
14725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned Size;
14735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned char C = *CurPtr++;
1474cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  while (isIdentifierBody(C))
14755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr++;
1476cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner
14775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  --CurPtr;   // Back up over the skipped character.
14785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Fast path, no $,\,? in identifier found.  '\' might be an escaped newline
14805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
14815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: UCNs.
1482cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  //
1483cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  // TODO: Could merge these checks into a CharInfo flag to make the comparison
1484cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  // cheaper
14854e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (C != '\\' && C != '?' && (C != '$' || !LangOpts.DollarIdents)) {
14865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerFinishIdentifier:
14875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const char *IdStart = BufferPtr;
1488c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    FormTokenWithChars(Result, CurPtr, tok::raw_identifier);
1489c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    Result.setRawIdentifierData(IdStart);
14901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we are in raw mode, return this identifier raw.  There is no need to
14925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // look up identifier information or attempt to macro expand it.
1493c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    if (LexingRawMode)
1494c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara      return;
14951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1496c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    // Fill in Result.IdentifierInfo and update the token kind,
1497c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    // looking up the identifier in the identifier table.
1498c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    IdentifierInfo *II = PP->LookUpIdentifierInfo(Result);
14991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Finally, now that we know we have an identifier, pass this off to the
15015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // preprocessor, which may macro expand it or something.
1502d1186fa38166a581b51975f0382a45fc3a0733d0Chris Lattner    if (II->isHandleIdentifierCase())
15036a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner      PP->HandleIdentifier(Result);
15046aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
15056a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    return;
15065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, $,\,? in identifier found.  Enter slower path.
15091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  C = getCharAndSize(CurPtr, Size);
15115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
15125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '$') {
15135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If we hit a $ and they are not supported in identifiers, we are done.
15144e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!LangOpts.DollarIdents) goto FinishIdentifier;
15151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Otherwise, emit a diagnostic and continue.
151774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
151874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(CurPtr, diag::ext_dollar_in_identifier);
15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, Size, Result);
15205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getCharAndSize(CurPtr, Size);
15215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      continue;
15225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
15235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Found end of identifier.
15245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      goto FinishIdentifier;
15255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
15265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this character is good, consume it.
15285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr = ConsumeChar(CurPtr, Size, Result);
15295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getCharAndSize(CurPtr, Size);
15315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (isIdentifierBody(C)) { // FIXME: UCNs.
15325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, Size, Result);
15335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getCharAndSize(CurPtr, Size);
15345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
15355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1538a75ec43d625753b4439b0d6f70bd988444c74617Douglas Gregor/// isHexaLiteral - Return true if Start points to a hex constant.
15394a551000bee716ac8b1bbe16134a53f0ad221a5aChris Lattner/// in microsoft mode (where this is supposed to be several different tokens).
1540e506f8a41063410c75945ebb804758bd0202947fEli Friedmanbool Lexer::isHexaLiteral(const char *Start, const LangOptions &LangOpts) {
15416ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner  unsigned Size;
15424e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  char C1 = Lexer::getCharAndSizeNoWarn(Start, Size, LangOpts);
15436ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner  if (C1 != '0')
15446ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner    return false;
15454e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  char C2 = Lexer::getCharAndSizeNoWarn(Start + Size, Size, LangOpts);
15466ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner  return (C2 == 'x' || C2 == 'X');
1547a75ec43d625753b4439b0d6f70bd988444c74617Douglas Gregor}
15485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15495253c7ff266ae79308050c9f43d60dd1a67c5fb9Nate Begeman/// LexNumericConstant - Lex the remainder of a integer or floating point
15505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. From[-1] is the first character lexed.  Return the end of the
15515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant.
1552d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
15535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned Size;
15545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getCharAndSize(CurPtr, Size);
15555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char PrevCh = 0;
15565cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  while (isNumberBody(C)) { // FIXME: UCNs.
15575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr = ConsumeChar(CurPtr, Size, Result);
15585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PrevCh = C;
15595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getCharAndSize(CurPtr, Size);
15605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we fell out, check for a sign, due to 1e+12.  If we have one, continue.
1563b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner  if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) {
1564b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner    // If we are in Microsoft mode, don't continue if the constant is hex.
1565b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner    // For example, MSVC will accept the following as 3 tokens: 0x1234567e+1
15664e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!LangOpts.MicrosoftExt || !isHexaLiteral(BufferPtr, LangOpts))
1567b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner      return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
1568b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner  }
15695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a hex FP constant, continue.
1571d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith  if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p')) {
1572d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith    // Outside C99, we accept hexadecimal floating point numbers as a
1573d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith    // not-quite-conforming extension. Only do so if this looks like it's
1574d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith    // actually meant to be a hexfloat, and not if it has a ud-suffix.
1575d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith    bool IsHexFloat = true;
1576d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith    if (!LangOpts.C99) {
1577d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith      if (!isHexaLiteral(BufferPtr, LangOpts))
1578d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith        IsHexFloat = false;
1579d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith      else if (std::find(BufferPtr, CurPtr, '_') != CurPtr)
1580d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith        IsHexFloat = false;
1581d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith    }
1582d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith    if (IsHexFloat)
1583d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith      return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
1584d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith  }
15851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
158747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
15889e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
158947246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
15905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15925cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith/// LexUDSuffix - Lex the ud-suffix production for user-defined literal suffixes
1593e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith/// in C++11, or warn on a ud-suffix in C++98.
15945cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smithconst char *Lexer::LexUDSuffix(Token &Result, const char *CurPtr) {
15954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus);
15965cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith
15975cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  // Maximally munch an identifier. FIXME: UCNs.
15985cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  unsigned Size;
15995cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  char C = getCharAndSize(CurPtr, Size);
16005cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  if (isIdentifierHead(C)) {
16014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().CPlusPlus0x) {
1602e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith      if (!isLexingRawMode())
16032fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith        Diag(CurPtr,
16042fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith             C == '_' ? diag::warn_cxx11_compat_user_defined_literal
16052fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith                      : diag::warn_cxx11_compat_reserved_user_defined_literal)
16062fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith          << FixItHint::CreateInsertion(getSourceLocation(CurPtr), " ");
16072fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith      return CurPtr;
16082fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith    }
16092fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith
16102fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith    // C++11 [lex.ext]p10, [usrlit.suffix]p1: A program containing a ud-suffix
16112fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith    // that does not start with an underscore is ill-formed. As a conforming
16122fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith    // extension, we treat all such suffixes as if they had whitespace before
16132fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith    // them.
16142fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith    if (C != '_') {
16152fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith      if (!isLexingRawMode())
1616b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet        Diag(CurPtr, getLangOpts().MicrosoftMode ?
1617b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet            diag::ext_ms_reserved_user_defined_literal :
1618b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet            diag::ext_reserved_user_defined_literal)
1619e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith          << FixItHint::CreateInsertion(getSourceLocation(CurPtr), " ");
1620e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith      return CurPtr;
1621e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith    }
1622e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith
162399831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    Result.setFlag(Token::HasUDSuffix);
16245cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith    do {
16255cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith      CurPtr = ConsumeChar(CurPtr, Size, Result);
16265cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith      C = getCharAndSize(CurPtr, Size);
16275cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith    } while (isIdentifierBody(C));
16285cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  }
16295cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  return CurPtr;
16305cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith}
16315cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith
16325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
16335cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor/// either " or L" or u8" or u" or U".
16345cee1195584fa8672253139c86e922daeda69b9eDouglas Gregorvoid Lexer::LexStringLiteral(Token &Result, const char *CurPtr,
16355cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                             tok::TokenKind Kind) {
16365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this string contain the \0 character?
16371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1638661a99690bc133bbaa029da925481d4a860dec90Richard Smith  if (!isLexingRawMode() &&
1639661a99690bc133bbaa029da925481d4a860dec90Richard Smith      (Kind == tok::utf8_string_literal ||
1640661a99690bc133bbaa029da925481d4a860dec90Richard Smith       Kind == tok::utf16_string_literal ||
1641661a99690bc133bbaa029da925481d4a860dec90Richard Smith       Kind == tok::utf32_string_literal))
1642661a99690bc133bbaa029da925481d4a860dec90Richard Smith    Diag(BufferPtr, diag::warn_cxx98_compat_unicode_literal);
1643661a99690bc133bbaa029da925481d4a860dec90Richard Smith
16445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
16455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (C != '"') {
1646571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    // Skip escaped characters.  Escaped newlines will already be processed by
1647571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    // getAndAdvanceChar.
1648571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    if (C == '\\')
16495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getAndAdvanceChar(CurPtr, Result);
165033611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor
1651571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    if (C == '\n' || C == '\r' ||             // Newline.
165233611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor        (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
16534e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!isLexingRawMode() && !LangOpts.AsmPreprocessor)
1654b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith        Diag(BufferPtr, diag::ext_unterminated_string);
16559e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr-1, tok::unknown);
16565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return;
16575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1658571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner
16597d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    if (C == 0) {
16607d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      if (isCodeCompletionPoint(CurPtr-1)) {
16617d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        PP->CodeCompleteNaturalLanguage();
16627d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        FormTokenWithChars(Result, CurPtr-1, tok::unknown);
16637d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return cutOffLexing();
16647d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      }
16657d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
1666571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner      NulCharacter = CurPtr-1;
16677d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    }
16685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
16695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16715cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  // If we are in C++11, lex the optional ud-suffix.
16724e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
16735cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith    CurPtr = LexUDSuffix(Result, CurPtr);
16745cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith
16755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If a nul character existed in the string, warn about it.
167674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
167774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_string);
16785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of the token as well as the BufferPtr instance var.
168047246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
16815cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  FormTokenWithChars(Result, CurPtr, Kind);
168247246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
16835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16852fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// LexRawStringLiteral - Lex the remainder of a raw string literal, after
16862fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// having lexed R", LR", u8R", uR", or UR".
16872fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Toppervoid Lexer::LexRawStringLiteral(Token &Result, const char *CurPtr,
16882fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                tok::TokenKind Kind) {
16892fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  // This function doesn't use getAndAdvanceChar because C++0x [lex.pptoken]p3:
16902fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  //  Between the initial and final double quote characters of the raw string,
16912fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  //  any transformations performed in phases 1 and 2 (trigraphs,
16922fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  //  universal-character-names, and line splicing) are reverted.
16932fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
1694661a99690bc133bbaa029da925481d4a860dec90Richard Smith  if (!isLexingRawMode())
1695661a99690bc133bbaa029da925481d4a860dec90Richard Smith    Diag(BufferPtr, diag::warn_cxx98_compat_raw_string_literal);
1696661a99690bc133bbaa029da925481d4a860dec90Richard Smith
16972fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  unsigned PrefixLen = 0;
16982fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
16992fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  while (PrefixLen != 16 && isRawStringDelimBody(CurPtr[PrefixLen]))
17002fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    ++PrefixLen;
17012fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17022fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  // If the last character was not a '(', then we didn't lex a valid delimiter.
17032fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  if (CurPtr[PrefixLen] != '(') {
17042fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    if (!isLexingRawMode()) {
17052fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      const char *PrefixEnd = &CurPtr[PrefixLen];
17062fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (PrefixLen == 16) {
17072fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        Diag(PrefixEnd, diag::err_raw_delim_too_long);
17082fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      } else {
17092fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        Diag(PrefixEnd, diag::err_invalid_char_raw_delim)
17102fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          << StringRef(PrefixEnd, 1);
17112fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      }
17122fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    }
17132fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17142fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    // Search for the next '"' in hopes of salvaging the lexer. Unfortunately,
17152fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    // it's possible the '"' was intended to be part of the raw string, but
17162fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    // there's not much we can do about that.
17172fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    while (1) {
17182fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      char C = *CurPtr++;
17192fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17202fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (C == '"')
17212fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        break;
17222fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (C == 0 && CurPtr-1 == BufferEnd) {
17232fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        --CurPtr;
17242fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        break;
17252fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      }
17262fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    }
17272fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17282fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    FormTokenWithChars(Result, CurPtr, tok::unknown);
17292fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    return;
17302fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  }
17312fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17322fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  // Save prefix and move CurPtr past it
17332fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  const char *Prefix = CurPtr;
17342fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  CurPtr += PrefixLen + 1; // skip over prefix and '('
17352fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17362fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  while (1) {
17372fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    char C = *CurPtr++;
17382fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17392fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    if (C == ')') {
17402fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      // Check for prefix match and closing quote.
17412fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (strncmp(CurPtr, Prefix, PrefixLen) == 0 && CurPtr[PrefixLen] == '"') {
17422fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        CurPtr += PrefixLen + 1; // skip over prefix and '"'
17432fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        break;
17442fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      }
17452fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    } else if (C == 0 && CurPtr-1 == BufferEnd) { // End of file.
17462fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (!isLexingRawMode())
17472fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        Diag(BufferPtr, diag::err_unterminated_raw_string)
17482fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          << StringRef(Prefix, PrefixLen);
17492fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      FormTokenWithChars(Result, CurPtr-1, tok::unknown);
17502fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      return;
17512fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    }
17522fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  }
17532fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17545cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  // If we are in C++11, lex the optional ud-suffix.
17554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
17565cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith    CurPtr = LexUDSuffix(Result, CurPtr);
17575cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith
17582fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  // Update the location of token as well as BufferPtr.
17592fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  const char *TokStart = BufferPtr;
17602fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  FormTokenWithChars(Result, CurPtr, Kind);
17612fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  Result.setLiteralData(TokStart);
17622fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper}
17632fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
17655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// after having lexed the '<' character.  This is used for #include filenames.
1766d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
17675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this string contain the \0 character?
17689cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner  const char *AfterLessPos = CurPtr;
17695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
17705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (C != '>') {
17715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip escaped characters.
17725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '\\') {
17735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip the escaped character.
177460b202c5eb6fb2d608bfef05523f40fdf5091d48Dmitri Gribenko      getAndAdvanceChar(CurPtr, Result);
17755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == '\n' || C == '\r' ||             // Newline.
17767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis               (C == 0 && (CurPtr-1 == BufferEnd ||  // End of file.
17777d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis                           isCodeCompletionPoint(CurPtr-1)))) {
17789cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      // If the filename is unterminated, then it must just be a lone <
17799cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      // character.  Return this as such.
17809cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      FormTokenWithChars(Result, AfterLessPos, tok::less);
17815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return;
17825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == 0) {
17835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      NulCharacter = CurPtr-1;
17845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
17855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
17865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
17871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If a nul character existed in the string, warn about it.
178974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
179074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_string);
17911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
179347246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
17949e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
179547246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
17965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexCharConstant - Lex the remainder of a character constant, after having
18005cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor/// lexed either ' or L' or u' or U'.
18015cee1195584fa8672253139c86e922daeda69b9eDouglas Gregorvoid Lexer::LexCharConstant(Token &Result, const char *CurPtr,
18025cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                            tok::TokenKind Kind) {
18035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this character contain the \0 character?
18045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1805661a99690bc133bbaa029da925481d4a860dec90Richard Smith  if (!isLexingRawMode() &&
1806661a99690bc133bbaa029da925481d4a860dec90Richard Smith      (Kind == tok::utf16_char_constant || Kind == tok::utf32_char_constant))
1807661a99690bc133bbaa029da925481d4a860dec90Richard Smith    Diag(BufferPtr, diag::warn_cxx98_compat_unicode_literal);
1808661a99690bc133bbaa029da925481d4a860dec90Richard Smith
18095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
18105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (C == '\'') {
18114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!isLexingRawMode() && !LangOpts.AsmPreprocessor)
1812b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith      Diag(BufferPtr, diag::ext_empty_character);
18139e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::unknown);
18145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return;
18155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
18161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1817d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner  while (C != '\'') {
1818d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    // Skip escaped characters.
1819d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    if (C == '\\') {
1820d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      // Skip the escaped character.
1821d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      // FIXME: UCN's
182260b202c5eb6fb2d608bfef05523f40fdf5091d48Dmitri Gribenko      getAndAdvanceChar(CurPtr, Result);
1823d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    } else if (C == '\n' || C == '\r' ||             // Newline.
1824d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner               (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
18254e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!isLexingRawMode() && !LangOpts.AsmPreprocessor)
1826b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith        Diag(BufferPtr, diag::ext_unterminated_char);
1827d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      FormTokenWithChars(Result, CurPtr-1, tok::unknown);
1828d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      return;
1829d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    } else if (C == 0) {
18307d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      if (isCodeCompletionPoint(CurPtr-1)) {
18317d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        PP->CodeCompleteNaturalLanguage();
18327d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        FormTokenWithChars(Result, CurPtr-1, tok::unknown);
18337d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return cutOffLexing();
18347d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      }
18357d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
1836d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      NulCharacter = CurPtr-1;
1837d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    }
1838d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    C = getAndAdvanceChar(CurPtr, Result);
18395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
18401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18415cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  // If we are in C++11, lex the optional ud-suffix.
18424e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
18435cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith    CurPtr = LexUDSuffix(Result, CurPtr);
18445cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith
1845d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner  // If a nul character existed in the character, warn about it.
184674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
184774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_char);
18485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
185047246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
18515cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  FormTokenWithChars(Result, CurPtr, Kind);
185247246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
18535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
18565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Update BufferPtr to point to the next non-whitespace character and return.
1857d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner///
1858d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
1859d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner///
1860d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnerbool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
18615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Whitespace - Skip it, then return the token after the whitespace.
18625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned char Char = *CurPtr;  // Skip consequtive spaces efficiently.
18635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
18645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip horizontal whitespace very aggressively.
18655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (isHorizontalWhitespace(Char))
18665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Char = *++CurPtr;
18671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1868ddd3e8b90a92c39f620bda7a0945320c0b9e60dbDaniel Dunbar    // Otherwise if we have something other than whitespace, we're done.
18695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char != '\n' && Char != '\r')
18705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
18711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingPreprocessorDirective) {
18735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // End of preprocessor directive line, let LexTokenInternal handle this.
18745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      BufferPtr = CurPtr;
1875d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return false;
18765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
18771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // ok, but handle newline.
18795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // The returned token is at the start of the line.
1880d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::StartOfLine);
18815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // No leading whitespace seen so far.
1882d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.clearFlag(Token::LeadingSpace);
18835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = *++CurPtr;
18845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
18855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this isn't immediately after a newline, there is leading space.
18875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char PrevChar = CurPtr[-1];
18885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PrevChar != '\n' && PrevChar != '\r')
1889d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
18905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1891d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // If the client wants us to return whitespace, return it now.
1892d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  if (isKeepWhitespaceMode()) {
18939e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::unknown);
1894d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    return true;
1895d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  }
18961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
1898d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  return false;
18995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
19005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1901bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// We have just read the // characters from input.  Skip until we find the
1902bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// newline character thats terminate the comment.  Then update BufferPtr and
1903bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// return.
1904046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner///
1905046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted
1906046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true.
1907bb23628148f555a4cf71f98c27096a7a804c085cNico Weberbool Lexer::SkipLineComment(Token &Result, const char *CurPtr) {
1908bb23628148f555a4cf71f98c27096a7a804c085cNico Weber  // If Line comments aren't explicitly enabled for this language, emit an
19095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // extension warning.
1910bb23628148f555a4cf71f98c27096a7a804c085cNico Weber  if (!LangOpts.LineComment && !isLexingRawMode()) {
1911bb23628148f555a4cf71f98c27096a7a804c085cNico Weber    Diag(BufferPtr, diag::ext_line_comment);
19121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Mark them enabled so we only emit one warning for this translation
19145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // unit.
1915bb23628148f555a4cf71f98c27096a7a804c085cNico Weber    LangOpts.LineComment = true;
19165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
19171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Scan over the body of the comment.  The common case, when scanning, is that
19195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the comment contains normal ascii characters with nothing interesting in
19205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // them.  As such, optimize for this case with the inner loop.
19215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C;
19225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  do {
19235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr;
19245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over characters in the fast loop.
19255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (C != 0 &&                // Potentially EOF.
19265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           C != '\n' && C != '\r')  // Newline or DOS-style newline.
19275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *++CurPtr;
19285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19291daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer    const char *NextLine = CurPtr;
19301daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer    if (C != 0) {
19311daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      // We found a newline, see if it's escaped.
19321daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      const char *EscapePtr = CurPtr-1;
19331daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      while (isHorizontalWhitespace(*EscapePtr)) // Skip whitespace.
19341daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer        --EscapePtr;
19351daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer
19361daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      if (*EscapePtr == '\\') // Escaped newline.
19371daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer        CurPtr = EscapePtr;
19381daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      else if (EscapePtr[0] == '/' && EscapePtr[-1] == '?' &&
19391daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer               EscapePtr[-2] == '?') // Trigraph-escaped newline.
19401daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer        CurPtr = EscapePtr-2;
19411daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      else
19421daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer        break; // This is a newline, we're done.
19431daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer    }
19441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is a hard case.  Fall back on getAndAdvanceChar to
1946bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // properly decode the character.  Read it in raw mode to avoid emitting
1947bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // diagnostics about things like trigraphs.  If we see an escaped newline,
1948bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // we'll handle it below.
19495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const char *OldPtr = CurPtr;
1950bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    bool OldRawMode = isLexingRawMode();
1951bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    LexingRawMode = true;
19525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
1953bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    LexingRawMode = OldRawMode;
1954ead616c5d8681a41b11273327813e61bda01907aChris Lattner
19551daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer    // If we only read only one character, then no special handling is needed.
19561daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer    // We're done and can skip forward to the newline.
19571daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer    if (C != 0 && CurPtr == OldPtr+1) {
19581daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      CurPtr = NextLine;
19591daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      break;
19601daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer    }
19611daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer
19625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we read multiple characters, and one of those characters was a \r or
19635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n, then we had an escaped newline within the comment.  Emit diagnostic
19645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // unless the next line is also a // comment.
19655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
19665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      for (; OldPtr != CurPtr; ++OldPtr)
19675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
19685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Okay, we found a // comment that ends in a newline, if the next
19695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // line is also a // comment, but has spaces, don't emit a diagnostic.
19705d6ae288bc661572e52ea71fc14b249eb5e2d9e9Benjamin Kramer          if (isWhitespace(C)) {
19715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            const char *ForwardPtr = CurPtr;
19725d6ae288bc661572e52ea71fc14b249eb5e2d9e9Benjamin Kramer            while (isWhitespace(*ForwardPtr))  // Skip whitespace.
19735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              ++ForwardPtr;
19745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
19755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              break;
19765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
19771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
197874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          if (!isLexingRawMode())
1979bb23628148f555a4cf71f98c27096a7a804c085cNico Weber            Diag(OldPtr-1, diag::ext_multi_line_line_comment);
19805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          break;
19815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
19825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
19831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
198455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor    if (CurPtr == BufferEnd+1) {
198555817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      --CurPtr;
198655817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      break;
198755817afdf9d453a443262a733f6caf6692dca118Douglas Gregor    }
19887d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
19897d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) {
19907d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      PP->CodeCompleteNaturalLanguage();
19917d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffLexing();
19927d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return false;
19937d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    }
19947d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
19955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } while (C != '\n' && C != '\r');
19965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19973d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  // Found but did not consume the newline.  Notify comment handlers about the
19983d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  // comment unless we're in a #if 0 block.
19993d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  if (PP && !isLexingRawMode() &&
20003d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner      PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
20013d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner                                            getSourceLocation(CurPtr)))) {
2002046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    BufferPtr = CurPtr;
2003046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    return true; // A token has to be returned.
2004046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  }
20051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are returning comments as tokens, return this comment as a token.
2007fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner  if (inKeepCommentMode())
2008bb23628148f555a4cf71f98c27096a7a804c085cNico Weber    return SaveLineComment(Result, CurPtr);
20095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are inside a preprocessor directive and we see the end of line,
201184021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  // return immediately, so that the lexer can return this as an EOD token.
20125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
20135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    BufferPtr = CurPtr;
20142d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
20155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
20161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, eat the \n character.  We don't care if this is a \n\r or
20187a4f004a77421af876152281b92c4f8ea24afb2dChris Lattner  // \r\n sequence.  This is an efficiency hack (because we know the \n can't
2019d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // contribute to another token), it isn't needed for correctness.  Note that
2020d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // this is ok even in KeepWhitespaceMode, because we would have returned the
2021d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// comment above in that mode.
20225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++CurPtr;
20231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // The next returned token is at the start of the line.
2025d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.setFlag(Token::StartOfLine);
20265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // No leading whitespace seen so far.
2027d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.clearFlag(Token::LeadingSpace);
20285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
20292d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return false;
20305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2032bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// If in save-comment mode, package up this Line comment in an appropriate
2033bb23628148f555a4cf71f98c27096a7a804c085cNico Weber/// way and return it.
2034bb23628148f555a4cf71f98c27096a7a804c085cNico Weberbool Lexer::SaveLineComment(Token &Result, const char *CurPtr) {
20359e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // If we're not in a preprocessor directive, just return the // comment
20369e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // directly.
20379e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::comment);
20381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20398c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  if (!ParsingPreprocessorDirective || LexingRawMode)
20409e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    return true;
20411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2042bb23628148f555a4cf71f98c27096a7a804c085cNico Weber  // If this Line-style comment is in a macro definition, transmogrify it into
20439e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // a C-style block comment.
2044453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor  bool Invalid = false;
2045453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor  std::string Spelling = PP->getSpelling(Result, &Invalid);
2046453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor  if (Invalid)
2047453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor    return true;
2048453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor
2049bb23628148f555a4cf71f98c27096a7a804c085cNico Weber  assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not line comment?");
20509e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Spelling[1] = '*';   // Change prefix to "/*".
20519e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Spelling += "*/";    // add suffix.
20521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20539e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Result.setKind(tok::comment);
2054374b3837d676133fcc1eb70a25c8baf8ec4a5c4aDmitri Gribenko  PP->CreateString(Spelling, Result,
2055a08529cc3f00e0b47a3c028823634129ac46847bAbramo Bagnara                   Result.getLocation(), Result.getLocation());
20562d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return true;
20575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
206080d7c52653800d3338cca2c4388348010d2b1921David Blaikie/// character (either \\n or \\r) is part of an escaped newline sequence.  Issue
206180d7c52653800d3338cca2c4388348010d2b1921David Blaikie/// a diagnostic if so.  We know that the newline is inside of a block comment.
20621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
20635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                                  Lexer *L) {
20645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
20651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Back up off the newline.
20675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  --CurPtr;
20681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a two-character newline sequence, skip the other character.
20705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
20715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n\n or \r\r -> not escaped newline.
20725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[0] == CurPtr[1])
20735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
20745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n\r or \r\n -> skip the newline.
20755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    --CurPtr;
20765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
20771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have horizontal whitespace, skip over it.  We allow whitespace
20795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // between the slash and newline.
20805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool HasSpace = false;
20815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
20825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    --CurPtr;
20835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    HasSpace = true;
20845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
20851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, we know this is an escaped newline.
20875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (*CurPtr == '\\') {
20885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[-1] != '*') return false;
20895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
20905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // It isn't a slash, is it the ?? / trigraph?
20915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
20925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr[-3] != '*')
20935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
20941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // This is the trigraph ending the comment.  Emit a stern warning!
20965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr -= 2;
20975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If no trigraphs are enabled, warn that we ignored this trigraph and
20995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // ignore this * character.
21004e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!L->getLangOpts().Trigraphs) {
210174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!L->isLexingRawMode())
210274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
21035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
21045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
210574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!L->isLexingRawMode())
210674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      L->Diag(CurPtr, diag::trigraph_ends_block_comment);
21075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
21081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Warn about having an escaped newline between the */ characters.
211074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (!L->isLexingRawMode())
211174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
21121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If there was space between the backslash and newline, warn about it.
211474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (HasSpace && !L->isLexingRawMode())
211574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    L->Diag(CurPtr, diag::backslash_newline_space);
21161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return true;
21185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__
21215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <emmintrin.h>
21225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__
21235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <altivec.h>
21245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#undef bool
21255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
21265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2127ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// We have just read from input the / and * characters that started a comment.
2128ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// Read until we find the * and / characters that terminate the comment.
2129ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// Note that we don't bother decoding trigraphs or escaped newlines in block
2130ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// comments, because they cannot cause the comment to end.  The only thing
2131ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// that can happen is the comment could end with an escaped newline between
2132ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// the terminating * and /.
21332d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner///
2134046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted
2135046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true.
2136d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
21375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Scan one character past where we should, looking for a '/' character.  Once
2138fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner  // we find it, check to see if it was preceded by a *.  This common
21395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // optimization helps people who like to put a lot of * characters in their
21405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // comments.
21418146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner
21428146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // The first character we get with newlines and trigraphs skipped to handle
21438146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // the degenerate /*/ case below correctly if the * has an escaped newline
21448146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // after it.
21458146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  unsigned CharSize;
21468146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  unsigned char C = getCharAndSize(CurPtr, CharSize);
21478146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  CurPtr += CharSize;
21485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (C == 0 && CurPtr == BufferEnd+1) {
21497d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    if (!isLexingRawMode())
21500af574270d3be2b0e73a3379dfaa633746f8fc6fChris Lattner      Diag(BufferPtr, diag::err_unterminated_block_comment);
215131f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    --CurPtr;
21521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
215331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    // KeepWhitespaceMode should return this broken comment as a token.  Since
215431f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    // it isn't a well formed comment, just return it as an 'unknown' token.
215531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    if (isKeepWhitespaceMode()) {
21569e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr, tok::unknown);
215731f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      return true;
215831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    }
21591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
216031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    BufferPtr = CurPtr;
21612d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
21625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
21631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21648146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // Check to see if the first character after the '/*' is another /.  If so,
21658146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // then this slash does not end the block comment, it is part of it.
21668146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  if (C == '/')
21678146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner    C = *CurPtr++;
21681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
21705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over all non-interesting characters until we find end of buffer or a
21715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // (probably ending) '/' character.
21727d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    if (CurPtr + 24 < BufferEnd &&
21737d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        // If there is a code-completion point avoid the fast scan because it
21747d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        // doesn't check for '\0'.
21757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        !(PP && PP->getCodeCompletionFileLoc() == FileLoc)) {
21765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // While not aligned to a 16-byte boundary.
21775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
21785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        C = *CurPtr++;
21791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '/') goto FoundSlash;
21815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__
21833f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer      __m128i Slashes = _mm_set1_epi8('/');
21843f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer      while (CurPtr+16 <= BufferEnd) {
218531ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky        int cmp = _mm_movemask_epi8(_mm_cmpeq_epi8(*(const __m128i*)CurPtr,
218631ba6135375433b617a8587ea6cc836a014ebd86Roman Divacky                                    Slashes));
21873f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer        if (cmp != 0) {
21886300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer          // Adjust the pointer to point directly after the first slash. It's
21896300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer          // not necessary to set C here, it will be overwritten at the end of
21906300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer          // the outer loop.
21916300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer          CurPtr += llvm::CountTrailingZeros_32(cmp) + 1;
21923f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer          goto FoundSlash;
21933f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer        }
21945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 16;
21953f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer      }
21965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__
21975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      __vector unsigned char Slashes = {
21981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        '/', '/', '/', '/',  '/', '/', '/', '/',
21995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        '/', '/', '/', '/',  '/', '/', '/', '/'
22005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      };
22015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (CurPtr+16 <= BufferEnd &&
22025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
22035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 16;
22041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#else
22055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Scan for '/' quickly.  Many block comments are very large.
22065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (CurPtr[0] != '/' &&
22075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[1] != '/' &&
22085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[2] != '/' &&
22095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[3] != '/' &&
22105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr+4 < BufferEnd) {
22115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 4;
22125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
22135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
22141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // It has to be one of the bytes scanned, increment to it and read one.
22165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *CurPtr++;
22175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
22181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Loop to scan the remainder.
22205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (C != '/' && C != '\0')
22215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *CurPtr++;
22221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '/') {
22243f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer  FoundSlash:
22255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr[-2] == '*')  // We found the final */.  We're done!
22265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
22271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
22295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
22305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // We found the final */, though it had an escaped newline between the
22315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // * and /.  We're done!
22325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          break;
22335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
22345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
22355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr[0] == '*' && CurPtr[1] != '/') {
22365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // If this is a /* inside of the comment, emit a warning.  Don't do this
22375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // if this is a /*/, which will end the comment.  This misses cases with
22385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // embedded escaped newlines, but oh well.
223974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        if (!isLexingRawMode())
224074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          Diag(CurPtr-1, diag::warn_nested_block_comment);
22415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
22425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == 0 && CurPtr == BufferEnd+1) {
22437d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      if (!isLexingRawMode())
224474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(BufferPtr, diag::err_unterminated_block_comment);
22455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Note: the user probably forgot a */.  We could continue immediately
22465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // after the /*, but this would involve lexing a lot of what really is the
22475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // comment, which surely would confuse the parser.
224831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      --CurPtr;
22491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
225031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      // KeepWhitespaceMode should return this broken comment as a token.  Since
225131f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      // it isn't a well formed comment, just return it as an 'unknown' token.
225231f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      if (isKeepWhitespaceMode()) {
22539e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        FormTokenWithChars(Result, CurPtr, tok::unknown);
225431f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner        return true;
225531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      }
22561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
225731f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      BufferPtr = CurPtr;
22582d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner      return false;
22597d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    } else if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) {
22607d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      PP->CodeCompleteNaturalLanguage();
22617d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffLexing();
22627d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return false;
22635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
22647d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
22655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr++;
22665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
22671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22683d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  // Notify comment handlers about the comment unless we're in a #if 0 block.
22693d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  if (PP && !isLexingRawMode() &&
22703d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner      PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
22713d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner                                            getSourceLocation(CurPtr)))) {
2272046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    BufferPtr = CurPtr;
2273046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    return true; // A token has to be returned.
2274046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  }
22752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
22765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are returning comments as tokens, return this comment as a token.
2277fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner  if (inKeepCommentMode()) {
22789e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::comment);
22792d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return true;
22805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
22815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // It is common for the tokens immediately after a /**/ comment to be
22835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // whitespace.  Instead of going through the big switch, handle it
2284d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // efficiently now.  This is safe even in KeepWhitespaceMode because we would
2285d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // have already returned above with the comment as a token.
22865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (isHorizontalWhitespace(*CurPtr)) {
2287d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
22885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipWhitespace(Result, CurPtr+1);
22892d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
22905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
22915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, just return so that the next character will be lexed as a token.
22935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
2294d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.setFlag(Token::LeadingSpace);
22952d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return false;
22965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
22995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Lexing Entry Points
23005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
23015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
23035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// uninterpreted string.  This switches the lexer out of directive mode.
23043093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramervoid Lexer::ReadToEndOfLine(SmallVectorImpl<char> *Result) {
23055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParsingPreprocessorDirective && ParsingFilename == false &&
23065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer         "Must be in a preprocessing directive!");
2307d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Token Tmp;
23085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // CurPtr - Cache BufferPtr in an automatic variable.
23105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CurPtr = BufferPtr;
23115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
23125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    char Char = getAndAdvanceChar(CurPtr, Tmp);
23135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Char) {
23145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
23153093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer      if (Result)
23163093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer        Result->push_back(Char);
23175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
23185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case 0:  // Null.
23195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Found end of file?
23205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr-1 != BufferEnd) {
23217d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        if (isCodeCompletionPoint(CurPtr-1)) {
23227d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis          PP->CodeCompleteNaturalLanguage();
23237d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis          cutOffLexing();
23243093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer          return;
23257d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        }
23267d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
23275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Nope, normal character, continue.
23283093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer        if (Result)
23293093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer          Result->push_back(Char);
23305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
23315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
23325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FALL THROUGH.
23335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case '\r':
23345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case '\n':
23355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Okay, we found the end of the line. First, back up past the \0, \r, \n.
23365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(CurPtr[-1] == Char && "Trigraphs for newline?");
23375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      BufferPtr = CurPtr-1;
23381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
233984021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne      // Next, lex the character, which should handle the EOD transition.
23405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Lex(Tmp);
234155817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      if (Tmp.is(tok::code_completion)) {
23427d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        if (PP)
23437d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis          PP->CodeCompleteNaturalLanguage();
234455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor        Lex(Tmp);
234555817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      }
234684021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne      assert(Tmp.is(tok::eod) && "Unexpected token!");
23471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23483093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer      // Finally, we're done;
23493093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer      return;
23505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
23515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
23525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
23535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexEndOfFile - CurPtr points to the end of this file.  Handle this
23555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// condition, reporting diagnostics and handling other edge cases as required.
23565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This returns true if Result contains a token, false if PP.Lex should be
23575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// called again.
2358d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
23595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we hit the end of the file while parsing a preprocessor directive,
23605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // end the preprocessor directive first.  The next token returned will
23615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // then be the end of file.
23625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ParsingPreprocessorDirective) {
23635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Done parsing the "line".
23645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParsingPreprocessorDirective = false;
23655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Update the location of token as well as BufferPtr.
236684021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne    FormTokenWithChars(Result, CurPtr, tok::eod);
23671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Restore comment saving mode, in case it was disabled for directive.
2369f744d133e8d36eec65828d046ee695bda238125cChris Lattner    SetCommentRetentionState(PP->getCommentRetentionState());
23705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return true;  // Have a token.
23711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
237286d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor
237386d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  // If we are in raw mode, return this event as an EOF token.  Let the caller
237486d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  // that put us in raw mode handle the event.
237586d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  if (isLexingRawMode()) {
237686d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    Result.startToken();
237786d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    BufferPtr = BufferEnd;
237886d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    FormTokenWithChars(Result, BufferEnd, tok::eof);
237986d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    return true;
238086d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  }
238181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
2382f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  // Issue diagnostics for unterminated #if and missing newline.
2383f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
23845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are in a #if directive, emit an error.
23855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (!ConditionalStack.empty()) {
23867d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    if (PP->getCodeCompletionFileLoc() != FileLoc)
23872d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor      PP->Diag(ConditionalStack.back().IfLoc,
23882d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor               diag::err_pp_unterminated_conditional);
23895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConditionalStack.pop_back();
23905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
23911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2392b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner  // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
2393b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner  // a pedwarn.
23945e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell  if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
23955e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell    Diag(BufferEnd, LangOpts.CPlusPlus0x ? // C++11 [lex.phases] 2.2 p2
23965e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell         diag::warn_cxx98_compat_no_newline_eof : diag::ext_no_newline_eof)
23975e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell    << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n");
23981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
24005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Finally, let the preprocessor handle this.
24020cdd1fe3ec29b5cbff9a728966ace5c5b5d614f7Jordan Rose  return PP->HandleEndOfFile(Result, isPragmaLexer());
24035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
24045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
24065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified lexer will return a tok::l_paren token, 0 if it is something
24075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// else and 2 if there are no more tokens in the buffer controlled by the
24085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexer.
24095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerunsigned Lexer::isNextPPTokenLParen() {
24105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
24111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Switch to 'skipping' mode.  This will ensure that we can lex a token
24135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // without emitting diagnostics, disables macro expansion, and will cause EOF
24145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // to return an EOF token instead of popping the include stack.
24155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexingRawMode = true;
24161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Save state that can be changed while lexing so that we can restore it.
24185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *TmpBufferPtr = BufferPtr;
2419a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner  bool inPPDirectiveMode = ParsingPreprocessorDirective;
24201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2421d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Token Tok;
24225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Tok.startToken();
24235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexTokenInternal(Tok);
24241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Restore state that may have changed.
24265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = TmpBufferPtr;
2427a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner  ParsingPreprocessorDirective = inPPDirectiveMode;
24281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Restore the lexer back to non-skipping mode.
24305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexingRawMode = false;
24311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
243222f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner  if (Tok.is(tok::eof))
24335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return 2;
243422f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner  return Tok.is(tok::l_paren);
24355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
24365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2437ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// \brief Find the end of a version control conflict marker.
2438d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smithstatic const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd,
2439d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith                                   ConflictMarkerKind CMK) {
2440d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  const char *Terminator = CMK == CMK_Perforce ? "<<<<\n" : ">>>>>>>";
2441d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  size_t TermLen = CMK == CMK_Perforce ? 5 : 7;
2442d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  StringRef RestOfBuffer(CurPtr+TermLen, BufferEnd-CurPtr-TermLen);
2443d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  size_t Pos = RestOfBuffer.find(Terminator);
24445f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  while (Pos != StringRef::npos) {
244534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Must occur at start of line.
244634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    if (RestOfBuffer[Pos-1] != '\r' &&
244734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        RestOfBuffer[Pos-1] != '\n') {
2448d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith      RestOfBuffer = RestOfBuffer.substr(Pos+TermLen);
2449d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith      Pos = RestOfBuffer.find(Terminator);
245034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      continue;
245134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    }
245234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return RestOfBuffer.data()+Pos;
245334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  }
245434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  return 0;
245534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner}
245634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
245734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// IsStartOfConflictMarker - If the specified pointer is the start of a version
245834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// control conflict marker like '<<<<<<<', recognize it as such, emit an error
245934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// and recover nicely.  This returns true if it is a conflict marker and false
246034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// if not.
246134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
246234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // Only a conflict marker if it starts at the beginning of a line.
246334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (CurPtr != BufferStart &&
246434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
246534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
246634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
2467d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  // Check to see if we have <<<<<<< or >>>>.
2468d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  if ((BufferEnd-CurPtr < 8 || StringRef(CurPtr, 7) != "<<<<<<<") &&
2469d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith      (BufferEnd-CurPtr < 6 || StringRef(CurPtr, 5) != ">>>> "))
247034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
247134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
247234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // If we have a situation where we don't care about conflict markers, ignore
247334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // it.
2474d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  if (CurrentConflictMarkerState || isLexingRawMode())
247534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
247634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
2477d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  ConflictMarkerKind Kind = *CurPtr == '<' ? CMK_Normal : CMK_Perforce;
2478d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith
2479d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  // Check to see if there is an ending marker somewhere in the buffer at the
2480d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  // start of a line to terminate this conflict marker.
2481d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  if (FindConflictEnd(CurPtr, BufferEnd, Kind)) {
248234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // We found a match.  We are really in a conflict marker.
248334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Diagnose this, and ignore to the end of line.
248434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    Diag(CurPtr, diag::err_conflict_marker);
2485d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith    CurrentConflictMarkerState = Kind;
248634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
248734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Skip ahead to the end of line.  We know this exists because the
248834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // end-of-conflict marker starts with \r or \n.
248934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    while (*CurPtr != '\r' && *CurPtr != '\n') {
249034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      assert(CurPtr != BufferEnd && "Didn't find end of line");
249134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      ++CurPtr;
249234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    }
249334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    BufferPtr = CurPtr;
249434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return true;
249534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  }
249634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
249734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // No end of conflict marker found.
249834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  return false;
249934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner}
250034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
250134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
2502d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// HandleEndOfConflictMarker - If this is a '====' or '||||' or '>>>>', or if
2503d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// it is '<<<<' and the conflict marker started with a '>>>>' marker, then it
2504d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// is the end of a conflict marker.  Handle it by ignoring up until the end of
2505d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// the line.  This returns true if it is a conflict marker and false if not.
250634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
250734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // Only a conflict marker if it starts at the beginning of a line.
250834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (CurPtr != BufferStart &&
250934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
251034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
251134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
251234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // If we have a situation where we don't care about conflict markers, ignore
251334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // it.
2514d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  if (!CurrentConflictMarkerState || isLexingRawMode())
251534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
251634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
2517d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  // Check to see if we have the marker (4 characters in a row).
2518d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  for (unsigned i = 1; i != 4; ++i)
251934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    if (CurPtr[i] != CurPtr[0])
252034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      return false;
252134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
252234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // If we do have it, search for the end of the conflict marker.  This could
252334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // fail if it got skipped with a '#if 0' or something.  Note that CurPtr might
252434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // be the end of conflict marker.
2525d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  if (const char *End = FindConflictEnd(CurPtr, BufferEnd,
2526d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith                                        CurrentConflictMarkerState)) {
252734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    CurPtr = End;
252834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
252934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Skip ahead to the end of line.
253034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
253134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      ++CurPtr;
253234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
253334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    BufferPtr = CurPtr;
253434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
253534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // No longer in the conflict marker.
2536d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith    CurrentConflictMarkerState = CMK_None;
253734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return true;
253834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  }
253934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
254034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  return false;
254134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner}
254234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
25437d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidisbool Lexer::isCodeCompletionPoint(const char *CurPtr) const {
25447d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  if (PP && PP->isCodeCompletionEnabled()) {
2545a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis    SourceLocation Loc = FileLoc.getLocWithOffset(CurPtr-BufferStart);
25467d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return Loc == PP->getCodeCompletionLoc();
25477d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  }
25487d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
25497d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  return false;
25507d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis}
25517d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
25525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
25535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexTokenInternal - This implements a simple C family lexer.  It is an
25545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// extremely performance critical piece of code.  This assumes that the buffer
2555efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// has a null character at the end of the file.  This returns a preprocessing
2556efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// token, not a normal token, as such, it is an internal interface.  It assumes
2557efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// that the Flags of result have been cleared before calling this.
2558d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexTokenInternal(Token &Result) {
25595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerLexNextToken:
25605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // New token, can't need cleaning yet.
2561d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.clearFlag(Token::NeedsCleaning);
25625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Result.setIdentifierInfo(0);
25631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // CurPtr - Cache BufferPtr in an automatic variable.
25655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CurPtr = BufferPtr;
25665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
25675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Small amounts of horizontal whitespace is very common between tokens.
25685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
25695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++CurPtr;
25705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while ((*CurPtr == ' ') || (*CurPtr == '\t'))
25715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++CurPtr;
25721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2573d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // If we are keeping whitespace and other tokens, just return what we just
2574d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // skipped.  The next lexer invocation will return the token after the
2575d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // whitespace.
2576d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (isKeepWhitespaceMode()) {
25779e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr, tok::unknown);
2578d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return;
2579d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    }
25801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    BufferPtr = CurPtr;
2582d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
25835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
25841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned SizeTmp, SizeTmp2;   // Temporaries for use in cases below.
25861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Read a character, advancing over it.
25885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char Char = getAndAdvanceChar(CurPtr, Result);
25899e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  tok::TokenKind Kind;
25901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Char) {
25925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 0:  // Null.
25935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Found end of file?
25945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr-1 == BufferEnd) {
25955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Read the PP instance variable into an automatic variable, because
25965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // LexEndOfFile will often delete 'this'.
2597168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      Preprocessor *PPCache = PP;
25985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (LexEndOfFile(Result, CurPtr-1))  // Retreat back into the file.
25995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return;   // Got a token to return.
2600168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
2601168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      return PPCache->Lex(Result);
26025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
26031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26047d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    // Check if we are performing code completion.
26057d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    if (isCodeCompletionPoint(CurPtr-1)) {
26067d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      // Return the code-completion token.
26077d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      Result.startToken();
26087d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      FormTokenWithChars(Result, CurPtr, tok::code_completion);
26097d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return;
26107d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    }
26117d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
261274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!isLexingRawMode())
261374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      Diag(CurPtr-1, diag::null_in_file);
2614d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
2615d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
2616d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
26171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
2619a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner
2620a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner  case 26:  // DOS & CP/M EOF: "^Z".
2621a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    // If we're in Microsoft extensions mode, treat this as end of file.
26224e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (LangOpts.MicrosoftExt) {
2623a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      // Read the PP instance variable into an automatic variable, because
2624a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      // LexEndOfFile will often delete 'this'.
2625a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      Preprocessor *PPCache = PP;
2626a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      if (LexEndOfFile(Result, CurPtr-1))  // Retreat back into the file.
2627a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner        return;   // Got a token to return.
2628a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
2629a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      return PPCache->Lex(Result);
2630a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    }
2631a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    // If Microsoft extensions are disabled, this is just random garbage.
2632a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    Kind = tok::unknown;
2633a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    break;
2634a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner
26355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\n':
26365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\r':
26375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we are inside a preprocessor directive and we see the end of line,
263884021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne    // we know we are done with the directive, so return an EOD token.
26395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingPreprocessorDirective) {
26405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Done parsing the "line".
26415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParsingPreprocessorDirective = false;
26421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Restore comment saving mode, in case it was disabled for directive.
26441a8354659a6007bbae3b5d9161a56ecc8f61a219David Blaikie      if (PP)
26458c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie        SetCommentRetentionState(PP->getCommentRetentionState());
26461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Since we consumed a newline, we are back at the start of a line.
26485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      IsAtStartOfLine = true;
26491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
265084021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne      Kind = tok::eod;
26515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
26525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
26535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // The returned token is at the start of the line.
2654d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::StartOfLine);
26555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // No leading whitespace seen so far.
2656d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.clearFlag(Token::LeadingSpace);
26571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2658d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
2659d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
26605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
26615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ' ':
26625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\t':
26635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\f':
26645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\v':
26658133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner  SkipHorizontalWhitespace:
2666d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
2667d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
2668d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
26698133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner
26708133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner  SkipIgnoredUnits:
26718133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    CurPtr = BufferPtr;
26721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26738133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    // If the next token is obviously a // or /* */ comment, skip it efficiently
26748133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    // too (without going through the big switch stmt).
26758402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
2676bb23628148f555a4cf71f98c27096a7a804c085cNico Weber        LangOpts.LineComment && !LangOpts.TraditionalCPP) {
2677bb23628148f555a4cf71f98c27096a7a804c085cNico Weber      if (SkipLineComment(Result, CurPtr+2))
2678046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner        return; // There is a token to return.
26798133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipIgnoredUnits;
2680fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner    } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
2681046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner      if (SkipBlockComment(Result, CurPtr+2))
2682046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner        return; // There is a token to return.
26838133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipIgnoredUnits;
26848133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    } else if (isHorizontalWhitespace(*CurPtr)) {
26858133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipHorizontalWhitespace;
26868133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    }
26875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
2688a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner
26893a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  // C99 6.4.4.1: Integer Constants.
26903a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  // C99 6.4.4.2: Floating Constants.
26913a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '0': case '1': case '2': case '3': case '4':
26923a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '5': case '6': case '7': case '8': case '9':
26933a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    // Notify MIOpt that we read a non-whitespace/non-comment token.
26943a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    MIOpt.ReadToken();
26953a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    return LexNumericConstant(Result, CurPtr);
26961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26975cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  case 'u':   // Identifier (uber) or C++0x UTF-8 or UTF-16 string literal
26985cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    // Notify MIOpt that we read a non-whitespace/non-comment token.
26995cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    MIOpt.ReadToken();
27005cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (LangOpts.CPlusPlus0x) {
27025cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      Char = getCharAndSize(CurPtr, SizeTmp);
27035cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27045cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      // UTF-16 string literal
27055cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      if (Char == '"')
27065cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor        return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
27075cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                                tok::utf16_string_literal);
27085cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27095cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      // UTF-16 character constant
27105cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      if (Char == '\'')
27115cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor        return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result),
27125cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                               tok::utf16_char_constant);
27135cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27142fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      // UTF-16 raw string literal
27152fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (Char == 'R' && getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"')
27162fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        return LexRawStringLiteral(Result,
27172fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
27182fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                           SizeTmp2, Result),
27192fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               tok::utf16_string_literal);
27202fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27212fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (Char == '8') {
27222fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        char Char2 = getCharAndSize(CurPtr + SizeTmp, SizeTmp2);
27232fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27242fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        // UTF-8 string literal
27252fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        if (Char2 == '"')
27262fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          return LexStringLiteral(Result,
27272fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
27282fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                           SizeTmp2, Result),
27292fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               tok::utf8_string_literal);
27302fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27312fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        if (Char2 == 'R') {
27322fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          unsigned SizeTmp3;
27332fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          char Char3 = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3);
27342fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          // UTF-8 raw string literal
27352fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          if (Char3 == '"') {
27362fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper            return LexRawStringLiteral(Result,
27372fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                   ConsumeChar(ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
27382fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                           SizeTmp2, Result),
27392fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               SizeTmp3, Result),
27402fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                   tok::utf8_string_literal);
27412fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          }
27422fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        }
27432fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      }
27445cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    }
27455cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27465cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    // treat u like the start of an identifier.
27475cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return LexIdentifier(Result, CurPtr);
27485cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27495cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  case 'U':   // Identifier (Uber) or C++0x UTF-32 string literal
27505cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    // Notify MIOpt that we read a non-whitespace/non-comment token.
27515cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    MIOpt.ReadToken();
27525cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27534e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (LangOpts.CPlusPlus0x) {
27545cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      Char = getCharAndSize(CurPtr, SizeTmp);
27555cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27565cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      // UTF-32 string literal
27575cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      if (Char == '"')
27585cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor        return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
27595cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                                tok::utf32_string_literal);
27605cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27615cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      // UTF-32 character constant
27625cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      if (Char == '\'')
27635cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor        return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result),
27645cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                               tok::utf32_char_constant);
27652fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27662fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      // UTF-32 raw string literal
27672fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (Char == 'R' && getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"')
27682fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        return LexRawStringLiteral(Result,
27692fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
27702fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                           SizeTmp2, Result),
27712fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               tok::utf32_string_literal);
27725cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    }
27735cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27745cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    // treat U like the start of an identifier.
27755cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return LexIdentifier(Result, CurPtr);
27765cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27772fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  case 'R': // Identifier or C++0x raw string literal
27782fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    // Notify MIOpt that we read a non-whitespace/non-comment token.
27792fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    MIOpt.ReadToken();
27802fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27814e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (LangOpts.CPlusPlus0x) {
27822fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      Char = getCharAndSize(CurPtr, SizeTmp);
27832fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27842fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (Char == '"')
27852fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        return LexRawStringLiteral(Result,
27862fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                   ConsumeChar(CurPtr, SizeTmp, Result),
27872fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                   tok::string_literal);
27882fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    }
27892fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27902fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    // treat R like the start of an identifier.
27912fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    return LexIdentifier(Result, CurPtr);
27922fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27933a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case 'L':   // Identifier (Loony) or wide literal (L'x' or L"xyz").
27945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
27955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
27965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
27975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
27985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Wide string literal.
27995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '"')
28005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
28015cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                              tok::wide_string_literal);
28025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
28032fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    // Wide raw string literal.
28044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (LangOpts.CPlusPlus0x && Char == 'R' &&
28052fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"')
28062fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      return LexRawStringLiteral(Result,
28072fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
28082fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                           SizeTmp2, Result),
28092fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               tok::wide_string_literal);
28102fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
28115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Wide character constant.
28125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '\'')
28135cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result),
28145cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                             tok::wide_char_constant);
28155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FALL THROUGH, treating L like the start of an identifier.
28161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.2: Identifiers.
28185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
28195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'H': case 'I': case 'J': case 'K':    /*'L'*/case 'M': case 'N':
28202fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  case 'O': case 'P': case 'Q':    /*'R'*/case 'S': case 'T':    /*'U'*/
28215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'V': case 'W': case 'X': case 'Y': case 'Z':
28225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
28235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
28245cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  case 'o': case 'p': case 'q': case 'r': case 's': case 't':    /*'u'*/
28255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'v': case 'w': case 'x': case 'y': case 'z':
28265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '_':
28275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
28285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
28295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexIdentifier(Result, CurPtr);
28303a5707766850f9ee9daa35299794328b5caf96dcChris Lattner
28313a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '$':   // $ in identifiers.
28324e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (LangOpts.DollarIdents) {
283374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
283474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(CurPtr-1, diag::ext_dollar_in_identifier);
28353a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      // Notify MIOpt that we read a non-whitespace/non-comment token.
28363a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      MIOpt.ReadToken();
28373a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      return LexIdentifier(Result, CurPtr);
28383a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    }
28391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28409e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::unknown;
28413a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    break;
28421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.4: Character Constants.
28445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\'':
28455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
28465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
28475cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return LexCharConstant(Result, CurPtr, tok::char_constant);
28485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
28495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.5: String Literals.
28505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '"':
28515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
28525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
28535cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return LexStringLiteral(Result, CurPtr, tok::string_literal);
28545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
28555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.6: Punctuators.
28565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '?':
28579e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::question;
28585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '[':
28609e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_square;
28615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ']':
28639e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_square;
28645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '(':
28669e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_paren;
28675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ')':
28699e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_paren;
28705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '{':
28729e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_brace;
28735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '}':
28759e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_brace;
28765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '.':
28785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
28795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char >= '0' && Char <= '9') {
28805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Notify MIOpt that we read a non-whitespace/non-comment token.
28815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      MIOpt.ReadToken();
28825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
28835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
28844e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (LangOpts.CPlusPlus && Char == '*') {
28859e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::periodstar;
28865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr += SizeTmp;
28875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '.' &&
28885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer               getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
28899e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ellipsis;
28905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
28915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                           SizeTmp2, Result);
28925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
28939e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::period;
28945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
28955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '&':
28975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
28985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '&') {
28999e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ampamp;
29005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
29029e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ampequal;
29035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
29059e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::amp;
29065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
29075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
29081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case '*':
29095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (getCharAndSize(CurPtr, SizeTmp) == '=') {
29109e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::starequal;
29115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
29139e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::star;
29145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
29155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
29165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '+':
29175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
29185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '+') {
29195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29209e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plusplus;
29215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
29225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29239e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plusequal;
29245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
29259e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plus;
29265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
29275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
29285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '-':
29295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
29309e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    if (Char == '-') {      // --
29315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29329e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minusminus;
29334e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (Char == '>' && LangOpts.CPlusPlus &&
29349e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner               getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {  // C++ ->*
29355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
29365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                           SizeTmp2, Result);
29379e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::arrowstar;
29389e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Char == '>') {   // ->
29395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29409e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::arrow;
29419e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Char == '=') {   // -=
29425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29439e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minusequal;
29445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
29459e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minus;
29465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
29475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
29485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '~':
29499e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::tilde;
29505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
29515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '!':
29525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (getCharAndSize(CurPtr, SizeTmp) == '=') {
29539e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::exclaimequal;
29545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
29569e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::exclaim;
29575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
29585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
29595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '/':
29605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // 6.4.9: Comments
29615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
2962bb23628148f555a4cf71f98c27096a7a804c085cNico Weber    if (Char == '/') {         // Line comment.
2963bb23628148f555a4cf71f98c27096a7a804c085cNico Weber      // Even if Line comments are disabled (e.g. in C89 mode), we generally
29648402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // want to lex this as a comment.  There is one problem with this though,
29658402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // that in one particular corner case, this can change the behavior of the
29668402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // resultant program.  For example, In  "foo //**/ bar", C89 would lex
2967bb23628148f555a4cf71f98c27096a7a804c085cNico Weber      // this as "foo / bar" and langauges with Line comments would lex it as
29688402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // "foo".  Check to see if the character after the second slash is a '*'.
29698402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // If so, we will lex that as a "/" instead of the start of a comment.
29702ed42287b9b88a0bb08ab10e95e306da3f294ba9Daniel Dunbar      // However, we never do this in -traditional-cpp mode.
2971bb23628148f555a4cf71f98c27096a7a804c085cNico Weber      if ((LangOpts.LineComment ||
29722ed42287b9b88a0bb08ab10e95e306da3f294ba9Daniel Dunbar           getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') &&
29734e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          !LangOpts.TraditionalCPP) {
2974bb23628148f555a4cf71f98c27096a7a804c085cNico Weber        if (SkipLineComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
2975046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner          return; // There is a token to return.
29761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29778402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // It is common for the tokens immediately after a // comment to be
29788402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // whitespace (indentation for the next line).  Instead of going through
29798402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // the big switch, handle it efficiently now.
29808402c73dd880e8af46c826d873681820aebe32ebChris Lattner        goto SkipIgnoredUnits;
29818402c73dd880e8af46c826d873681820aebe32ebChris Lattner      }
29828402c73dd880e8af46c826d873681820aebe32ebChris Lattner    }
29831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29848402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (Char == '*') {  // /**/ comment.
29855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
2986046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner        return; // There is a token to return.
29872d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner      goto LexNextToken;   // GCC isn't tail call eliminating.
29888402c73dd880e8af46c826d873681820aebe32ebChris Lattner    }
29891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29908402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (Char == '=') {
29915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29929e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::slashequal;
29935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
29949e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::slash;
29955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
29965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
29975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '%':
29985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
29995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
30009e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::percentequal;
30015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
30024e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (LangOpts.Digraphs && Char == '>') {
30039e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::r_brace;                             // '%>' -> '}'
30045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
30054e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (LangOpts.Digraphs && Char == ':') {
30065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
30075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Char = getCharAndSize(CurPtr, SizeTmp);
30085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
30099e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        Kind = tok::hashhash;                          // '%:%:' -> '##'
30105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
30115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                             SizeTmp2, Result);
30124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      } else if (Char == '@' && LangOpts.MicrosoftExt) {// %:@ -> #@ -> Charize
30135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
301474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        if (!isLexingRawMode())
301566d5ce11b9426f6a59f61a03cbd8dbf047cc9350Ted Kremenek          Diag(BufferPtr, diag::ext_charize_microsoft);
30169e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        Kind = tok::hashat;
3017e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner      } else {                                         // '%:' -> '#'
30185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // We parsed a # character.  If this occurs at the start of the line,
30195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // it's actually the start of a preprocessing directive.  Callback to
30205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // the preprocessor to handle it.
30215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // FIXME: -fpreprocessed mode??
30223185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis        if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer)
30233185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis          goto HandleDirective;
30241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3025e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner        Kind = tok::hash;
30265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
30275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
30289e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::percent;
30295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
30305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
30315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '<':
30325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
30335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingFilename) {
30349cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      return LexAngledStringLiteral(Result, CurPtr);
30355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '<') {
303634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
303734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (After == '=') {
303834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::lesslessequal;
303934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
304034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner                             SizeTmp2, Result);
304134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
304234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        // If this is actually a '<<<<<<<' version control conflict marker,
304334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        // recognize it as such and recover nicely.
304434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
3045d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith      } else if (After == '<' && HandleEndOfConflictMarker(CurPtr-1)) {
3046d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith        // If this is '<<<<' and we're in a Perforce-style conflict marker,
3047d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith        // ignore it.
3048d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith        goto LexNextToken;
30494e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      } else if (LangOpts.CUDA && After == '<') {
30501b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne        Kind = tok::lesslessless;
30511b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
30521b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne                             SizeTmp2, Result);
305334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else {
305434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
305534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::lessless;
305634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      }
30575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
30585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
30599e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::lessequal;
30604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (LangOpts.Digraphs && Char == ':') {     // '<:' -> '['
30614e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (LangOpts.CPlusPlus0x &&
306287a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith          getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == ':') {
306387a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        // C++0x [lex.pptoken]p3:
306487a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        //  Otherwise, if the next three characters are <:: and the subsequent
306587a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        //  character is neither : nor >, the < is treated as a preprocessor
306687a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        //  token by itself and not as the first character of the alternative
306787a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        //  token <:.
306887a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        unsigned SizeTmp3;
306987a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        char After = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3);
307087a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        if (After != ':' && After != '>') {
307187a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith          Kind = tok::less;
3072661a99690bc133bbaa029da925481d4a860dec90Richard Smith          if (!isLexingRawMode())
3073661a99690bc133bbaa029da925481d4a860dec90Richard Smith            Diag(BufferPtr, diag::warn_cxx98_compat_less_colon_colon);
307487a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith          break;
307587a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        }
307687a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith      }
307787a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith
30785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
30799e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::l_square;
30804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (LangOpts.Digraphs && Char == '%') {     // '<%' -> '{'
30815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
30829e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::l_brace;
30835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
30849e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::less;
30855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
30865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
30875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '>':
30885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
30895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
30905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
30919e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::greaterequal;
30925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '>') {
309334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
309434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (After == '=') {
309534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
309634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner                             SizeTmp2, Result);
309734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::greatergreaterequal;
3098d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith      } else if (After == '>' && IsStartOfConflictMarker(CurPtr-1)) {
3099d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith        // If this is actually a '>>>>' conflict marker, recognize it as such
3100d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith        // and recover nicely.
3101d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith        goto LexNextToken;
310234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
310334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        // If this is '>>>>>>>' and we're in a conflict marker, ignore it.
310434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
31054e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      } else if (LangOpts.CUDA && After == '>') {
31061b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne        Kind = tok::greatergreatergreater;
31071b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
31081b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne                             SizeTmp2, Result);
310934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else {
311034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
311134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::greatergreater;
311234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      }
311334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
31145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
31159e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::greater;
31165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
31175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '^':
31195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
31205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
31215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31229e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::caretequal;
31235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
31249e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::caret;
31255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
31265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '|':
31285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
31295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
31309e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipeequal;
31315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '|') {
313334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      // If this is '|||||||' and we're in a conflict marker, ignore it.
313434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
313534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
31369e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipepipe;
31375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
31399e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipe;
31405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
31415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ':':
31435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
31444e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (LangOpts.Digraphs && Char == '>') {
31459e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::r_square; // ':>' -> ']'
31465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31474e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (LangOpts.CPlusPlus && Char == ':') {
31489e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::coloncolon;
31495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else {
31519e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::colon;
31525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
31535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ';':
31559e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::semi;
31565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '=':
31585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
31595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
3160d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith      // If this is '====' and we're in a conflict marker, ignore it.
316134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
316234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
316334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
31649e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::equalequal;
31655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else {
31679e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::equal;
31685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
31695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ',':
31719e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::comma;
31725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '#':
31745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
31755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '#') {
31769e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::hashhash;
31775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31784e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (Char == '@' && LangOpts.MicrosoftExt) {  // #@ -> Charize
31799e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::hashat;
318074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
318166d5ce11b9426f6a59f61a03cbd8dbf047cc9350Ted Kremenek        Diag(BufferPtr, diag::ext_charize_microsoft);
31825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
31845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // We parsed a # character.  If this occurs at the start of the line,
31855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // it's actually the start of a preprocessing directive.  Callback to
31865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // the preprocessor to handle it.
31875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FIXME: -fpreprocessed mode??
31883185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis      if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer)
31893185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis        goto HandleDirective;
31901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3191e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner      Kind = tok::hash;
31925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
31935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
31953a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '@':
31963a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    // Objective C support.
31974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (CurPtr[-1] == '@' && LangOpts.ObjC1)
31989e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::at;
31993a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    else
32009e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::unknown;
32013a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    break;
32021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\\':
32045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FIXME: UCN's.
32055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FALL THROUGH.
32065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
32079e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::unknown;
32085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
32095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
32101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Notify MIOpt that we read a non-whitespace/non-comment token.
32125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  MIOpt.ReadToken();
32135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
32145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
32159e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, Kind);
32163185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis  return;
32173185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis
32183185d4ac30378995ef70421e2848f77524c2b5d5Argyrios KyrtzidisHandleDirective:
32193185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis  // We parsed a # character and it's the start of a preprocessing directive.
32203185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis
32213185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis  FormTokenWithChars(Result, CurPtr, tok::hash);
32223185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis  PP->HandleDirective(Result);
32233185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis
32243185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis  // As an optimization, if the preprocessor didn't switch lexers, tail
32253185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis  // recurse.
32263185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis  if (PP->isCurrentLexer(this)) {
32273185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis    // Start a new token.  If this is a #include or something, the PP may
32283185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis    // want us starting at the beginning of the line again.  If so, set
32293185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis    // the StartOfLine flag and clear LeadingSpace.
32303185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis    if (IsAtStartOfLine) {
32313185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis      Result.setFlag(Token::StartOfLine);
32323185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis      Result.clearFlag(Token::LeadingSpace);
32333185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis      IsAtStartOfLine = false;
32343185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis    }
32353185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis    goto LexNextToken;   // GCC isn't tail call eliminating.
32363185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis  }
32373185d4ac30378995ef70421e2848f77524c2b5d5Argyrios Kyrtzidis  return PP->Lex(Result);
32385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3239