Lexer.cpp revision e506f8a41063410c75945ebb804758bd0202947f
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;
516f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  SourceLocation StartLoc = SourceLocation::getFromRawEncoding(StartOffset);
5174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  Lexer TheLexer(StartLoc, LangOpts, Buffer->getBufferStart(),
518f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor                 Buffer->getBufferStart(), Buffer->getBufferEnd());
519f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
520f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  bool InPreprocessorDirective = false;
521f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  Token TheTok;
522f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  Token IfStartTok;
523f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  unsigned IfCount = 0;
524c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis
525c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis  unsigned MaxLineOffset = 0;
526c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis  if (MaxLines) {
527c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis    const char *CurPtr = Buffer->getBufferStart();
528c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis    unsigned CurLine = 0;
529c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis    while (CurPtr != Buffer->getBufferEnd()) {
530c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis      char ch = *CurPtr++;
531c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis      if (ch == '\n') {
532c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis        ++CurLine;
533c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis        if (CurLine == MaxLines)
534c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis          break;
535c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis      }
536c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis    }
537c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis    if (CurPtr != Buffer->getBufferEnd())
538c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis      MaxLineOffset = CurPtr - Buffer->getBufferStart();
539c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis  }
540df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
541f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  do {
542f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    TheLexer.LexFromRawLexer(TheTok);
543f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
544f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    if (InPreprocessorDirective) {
545f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // If we've hit the end of the file, we're done.
546f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      if (TheTok.getKind() == tok::eof) {
547f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        break;
548f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      }
549f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
550f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // If we haven't hit the end of the preprocessor directive, skip this
551f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // token.
552f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      if (!TheTok.isAtStartOfLine())
553f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        continue;
554f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
555f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // We've passed the end of the preprocessor directive, and will look
556f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // at this token again below.
557f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      InPreprocessorDirective = false;
558f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    }
559f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
560df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // Keep track of the # of lines in the preamble.
561df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    if (TheTok.isAtStartOfLine()) {
562c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis      unsigned TokOffset = TheTok.getLocation().getRawEncoding() - StartOffset;
563df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
564df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor      // If we were asked to limit the number of lines in the preamble,
565df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor      // and we're about to exceed that limit, we're done.
566c8c97a03eb0fdeb4f5fc9c4dea308ebbf46c2c93Argyrios Kyrtzidis      if (MaxLineOffset && TokOffset >= MaxLineOffset)
567df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor        break;
568df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    }
569df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
570f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    // Comments are okay; skip over them.
571f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    if (TheTok.getKind() == tok::comment)
572f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      continue;
573f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
574f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) {
575f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // This is the start of a preprocessor directive.
576f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      Token HashTok = TheTok;
577f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      InPreprocessorDirective = true;
578f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
57919207f1e5f51261a33492602501fb7ada50ea546Joerg Sonnenberger      // Figure out which directive this is. Since we're lexing raw tokens,
580f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // we don't have an identifier table available. Instead, just look at
581f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // the raw identifier to recognize and categorize preprocessor directives.
582f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      TheLexer.LexFromRawLexer(TheTok);
583c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara      if (TheTok.getKind() == tok::raw_identifier && !TheTok.needsCleaning()) {
5845f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        StringRef Keyword(TheTok.getRawIdentifierData(),
585c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara                                TheTok.getLength());
586f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        PreambleDirectiveKind PDK
587f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          = llvm::StringSwitch<PreambleDirectiveKind>(Keyword)
588f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("include", PDK_Skipped)
589f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("__include_macros", PDK_Skipped)
590f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("define", PDK_Skipped)
591f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("undef", PDK_Skipped)
592f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("line", PDK_Skipped)
593f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("error", PDK_Skipped)
594f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("pragma", PDK_Skipped)
595f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("import", PDK_Skipped)
596f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("include_next", PDK_Skipped)
597f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("warning", PDK_Skipped)
598f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("ident", PDK_Skipped)
599f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("sccs", PDK_Skipped)
600f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("assert", PDK_Skipped)
601f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("unassert", PDK_Skipped)
602f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("if", PDK_StartIf)
603f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("ifdef", PDK_StartIf)
604f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("ifndef", PDK_StartIf)
605f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("elif", PDK_Skipped)
606f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("else", PDK_Skipped)
607f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("endif", PDK_EndIf)
608f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Default(PDK_Unknown);
609f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
610f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        switch (PDK) {
611f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_Skipped:
612f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          continue;
613f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
614f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_StartIf:
615f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          if (IfCount == 0)
616f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor            IfStartTok = HashTok;
617f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
618f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          ++IfCount;
619f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          continue;
620f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
621f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_EndIf:
622f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          // Mismatched #endif. The preamble ends here.
623f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          if (IfCount == 0)
624f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor            break;
625f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
626f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          --IfCount;
627f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          continue;
628f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
629f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_Unknown:
630f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          // We don't know what this directive is; stop at the '#'.
631f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          break;
632f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        }
633f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      }
634f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
635f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // We only end up here if we didn't recognize the preprocessor
636f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // directive or it was one that can't occur in the preamble at this
637f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // point. Roll back the current token to the location of the '#'.
638f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      InPreprocessorDirective = false;
639f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      TheTok = HashTok;
640f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    }
641f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
642df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // We hit a token that we don't recognize as being in the
643df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // "preprocessing only" part of the file, so we're no longer in
644df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // the preamble.
645f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    break;
646f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  } while (true);
647f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
648f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation();
649f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(),
650f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                        IfCount? IfStartTok.isAtStartOfLine()
651f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                               : TheTok.isAtStartOfLine());
652f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor}
653f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
6547ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6557ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// AdvanceToTokenCharacter - Given a location that specifies the start of a
6567ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token, return a new location that specifies a character within the token.
6577ef5c27eb6e8ebe58b52013246c06753c3613263Chris LattnerSourceLocation Lexer::AdvanceToTokenCharacter(SourceLocation TokStart,
6587ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner                                              unsigned CharNo,
6597ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner                                              const SourceManager &SM,
6604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                                              const LangOptions &LangOpts) {
661433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  // Figure out how many physical characters away the specified expansion
6627ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // character is.  This needs to take into consideration newlines and
6637ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // trigraphs.
6647ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  bool Invalid = false;
6657ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  const char *TokPtr = SM.getCharacterData(TokStart, &Invalid);
6667ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6677ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // If they request the first char of the token, we're trivially done.
6687ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  if (Invalid || (CharNo == 0 && Lexer::isObviouslySimpleCharacter(*TokPtr)))
6697ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    return TokStart;
6707ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6717ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  unsigned PhysOffset = 0;
6727ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6737ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // The usual case is that tokens don't contain anything interesting.  Skip
6747ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // over the uninteresting characters.  If a token only consists of simple
6757ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // chars, this method is extremely fast.
6767ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  while (Lexer::isObviouslySimpleCharacter(*TokPtr)) {
6777ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    if (CharNo == 0)
678a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis      return TokStart.getLocWithOffset(PhysOffset);
6797ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    ++TokPtr, --CharNo, ++PhysOffset;
6807ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  }
6817ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6827ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // If we have a character that may be a trigraph or escaped newline, use a
6837ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // lexer to parse it correctly.
6847ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  for (; CharNo; --CharNo) {
6857ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    unsigned Size;
6864e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    Lexer::getCharAndSizeNoWarn(TokPtr, Size, LangOpts);
6877ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    TokPtr += Size;
6887ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    PhysOffset += Size;
6897ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  }
6907ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
6917ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // Final detail: if we end up on an escaped newline, we want to return the
6927ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // location of the actual byte of the token.  For example foo\<newline>bar
6937ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // advanced by 3 should return the location of b, not of \\.  One compounding
6947ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  // detail of this is that the escape may be made by a trigraph.
6957ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  if (!Lexer::isObviouslySimpleCharacter(*TokPtr))
6967ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    PhysOffset += Lexer::SkipEscapedNewLines(TokPtr)-TokPtr;
6977ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
698a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  return TokStart.getLocWithOffset(PhysOffset);
6997ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner}
7007ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
7017ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// \brief Computes the source location just past the end of the
7027ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token at this source location.
7037ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner///
7047ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// This routine can be used to produce a source location that
7057ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// points just past the end of the token referenced by \p Loc, and
7067ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// is generally used when a diagnostic needs to point just after a
7077ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// token where it expected something different that it received. If
7087ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// the returned source location would not be meaningful (e.g., if
7097ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// it points into a macro), this routine returns an invalid
7107ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// source location.
7117ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner///
7127ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// \param Offset an offset from the end of the token, where the source
7137ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// location should refer to. The default offset (0) produces a source
7147ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// location pointing just past the end of the token; an offset of 1 produces
7157ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner/// a source location pointing to the last character in the token, etc.
7167ef5c27eb6e8ebe58b52013246c06753c3613263Chris LattnerSourceLocation Lexer::getLocForEndOfToken(SourceLocation Loc, unsigned Offset,
7177ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner                                          const SourceManager &SM,
7184e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                                          const LangOptions &LangOpts) {
7197ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis  if (Loc.isInvalid())
7207ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    return SourceLocation();
7217ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis
7227ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis  if (Loc.isMacroID()) {
7234e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Offset > 0 || !isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc))
724433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth      return SourceLocation(); // Points inside the macro expansion.
7257ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis  }
7267ddf6b2d77ac4cb27f78d817d7884e6ce17afd0cArgyrios Kyrtzidis
7274e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  unsigned Len = Lexer::MeasureTokenLength(Loc, SM, LangOpts);
7287ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  if (Len > Offset)
7297ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    Len = Len - Offset;
7307ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  else
7317ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    return Loc;
7327ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
733a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  return Loc.getLocWithOffset(Len);
7347ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner}
7357ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
7367a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis/// \brief Returns true if the given MacroID location points at the first
737433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// token of the macro expansion.
738433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruthbool Lexer::isAtStartOfMacroExpansion(SourceLocation loc,
739f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                      const SourceManager &SM,
74069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                      const LangOptions &LangOpts,
74169bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                      SourceLocation *MacroBegin) {
7427a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc");
7437a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
7447a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  std::pair<FileID, unsigned> infoLoc = SM.getDecomposedLoc(loc);
7457a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  // FIXME: If the token comes from the macro token paste operator ('##')
7467a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  // this function will always return false;
7477a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  if (infoLoc.second > 0)
7487a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis    return false; // Does not point at the start of token.
7497a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
750433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  SourceLocation expansionLoc =
7511728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth    SM.getSLocEntry(infoLoc.first).getExpansion().getExpansionLocStart();
75269bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  if (expansionLoc.isFileID()) {
75369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    // No other macro expansions, this is the first.
75469bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    if (MacroBegin)
75569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis      *MacroBegin = expansionLoc;
75669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    return true;
75769bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  }
7587a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
75969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  return isAtStartOfMacroExpansion(expansionLoc, SM, LangOpts, MacroBegin);
7607a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis}
7617a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
7627a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis/// \brief Returns true if the given MacroID location points at the last
763433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// token of the macro expansion.
764433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruthbool Lexer::isAtEndOfMacroExpansion(SourceLocation loc,
76569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                    const SourceManager &SM,
76669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                    const LangOptions &LangOpts,
76769bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                    SourceLocation *MacroEnd) {
7687a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc");
7697a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
7707a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  SourceLocation spellLoc = SM.getSpellingLoc(loc);
7717a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  unsigned tokLen = MeasureTokenLength(spellLoc, SM, LangOpts);
7727a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  if (tokLen == 0)
7737a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis    return false;
7747a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
7757a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  FileID FID = SM.getFileID(loc);
776a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  SourceLocation afterLoc = loc.getLocWithOffset(tokLen+1);
777f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis  if (SM.isInFileID(afterLoc, FID))
778f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis    return false; // Still in the same FileID, does not point to the last token.
7797a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
7807a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  // FIXME: If the token comes from the macro token paste operator ('##')
7817a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  // or the stringify operator ('#') this function will always return false;
782f8c50652f7b224e66b0b6098d1fba07e036019b4Argyrios Kyrtzidis
783433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  SourceLocation expansionLoc =
7841728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth    SM.getSLocEntry(FID).getExpansion().getExpansionLocEnd();
78569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  if (expansionLoc.isFileID()) {
78669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    // No other macro expansions.
78769bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    if (MacroEnd)
78869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis      *MacroEnd = expansionLoc;
78969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    return true;
79069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  }
7917a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
79269bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  return isAtEndOfMacroExpansion(expansionLoc, SM, LangOpts, MacroEnd);
7937a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis}
7947a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
795a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidisstatic CharSourceRange makeRangeFromFileLocs(CharSourceRange Range,
796d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis                                             const SourceManager &SM,
797d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis                                             const LangOptions &LangOpts) {
798a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  SourceLocation Begin = Range.getBegin();
799a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  SourceLocation End = Range.getEnd();
800d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  assert(Begin.isFileID() && End.isFileID());
801a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  if (Range.isTokenRange()) {
802a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    End = Lexer::getLocForEndOfToken(End, 0, SM,LangOpts);
803a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    if (End.isInvalid())
804a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis      return CharSourceRange();
805a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  }
806d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis
807d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  // Break down the source locations.
808d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  FileID FID;
809d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  unsigned BeginOffs;
810d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  llvm::tie(FID, BeginOffs) = SM.getDecomposedLoc(Begin);
811d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (FID.isInvalid())
812d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis    return CharSourceRange();
813d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis
814d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  unsigned EndOffs;
815d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (!SM.isInFileID(End, FID, &EndOffs) ||
816d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis      BeginOffs > EndOffs)
817d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis    return CharSourceRange();
818d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis
819d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  return CharSourceRange::getCharRange(Begin, End);
820d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis}
821d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis
822a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios KyrtzidisCharSourceRange Lexer::makeFileCharRange(CharSourceRange Range,
82311b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis                                         const SourceManager &SM,
82411b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis                                         const LangOptions &LangOpts) {
825a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  SourceLocation Begin = Range.getBegin();
826a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  SourceLocation End = Range.getEnd();
827d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (Begin.isInvalid() || End.isInvalid())
82811b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis    return CharSourceRange();
82911b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis
830d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (Begin.isFileID() && End.isFileID())
831a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    return makeRangeFromFileLocs(Range, SM, LangOpts);
832d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis
833d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (Begin.isMacroID() && End.isFileID()) {
83411b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis    if (!isAtStartOfMacroExpansion(Begin, SM, LangOpts, &Begin))
83511b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis      return CharSourceRange();
836a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    Range.setBegin(Begin);
837a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    return makeRangeFromFileLocs(Range, SM, LangOpts);
838d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  }
83911b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis
840d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (Begin.isFileID() && End.isMacroID()) {
841a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    if ((Range.isTokenRange() && !isAtEndOfMacroExpansion(End, SM, LangOpts,
842a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis                                                          &End)) ||
843a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis        (Range.isCharRange() && !isAtStartOfMacroExpansion(End, SM, LangOpts,
844a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis                                                           &End)))
845d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis      return CharSourceRange();
846a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    Range.setEnd(End);
847a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    return makeRangeFromFileLocs(Range, SM, LangOpts);
848d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  }
84911b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis
850d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  assert(Begin.isMacroID() && End.isMacroID());
851d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  SourceLocation MacroBegin, MacroEnd;
852d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (isAtStartOfMacroExpansion(Begin, SM, LangOpts, &MacroBegin) &&
853a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis      ((Range.isTokenRange() && isAtEndOfMacroExpansion(End, SM, LangOpts,
854a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis                                                        &MacroEnd)) ||
855a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis       (Range.isCharRange() && isAtStartOfMacroExpansion(End, SM, LangOpts,
856a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis                                                         &MacroEnd)))) {
857a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    Range.setBegin(MacroBegin);
858a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    Range.setEnd(MacroEnd);
859a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    return makeRangeFromFileLocs(Range, SM, LangOpts);
860a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  }
861d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis
862d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  FileID FID;
863d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  unsigned BeginOffs;
864d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  llvm::tie(FID, BeginOffs) = SM.getDecomposedLoc(Begin);
865d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (FID.isInvalid())
866e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    return CharSourceRange();
867e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis
86811b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis  unsigned EndOffs;
869d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (!SM.isInFileID(End, FID, &EndOffs) ||
870d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis      BeginOffs > EndOffs)
87111b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis    return CharSourceRange();
87211b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis
873d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  const SrcMgr::SLocEntry *E = &SM.getSLocEntry(FID);
874d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();
875d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  if (Expansion.isMacroArgExpansion() &&
876d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis      Expansion.getSpellingLoc().isFileID()) {
877d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis    SourceLocation SpellLoc = Expansion.getSpellingLoc();
878a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    Range.setBegin(SpellLoc.getLocWithOffset(BeginOffs));
879a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    Range.setEnd(SpellLoc.getLocWithOffset(EndOffs));
880a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis    return makeRangeFromFileLocs(Range, SM, LangOpts);
881d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  }
882d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis
883d9806c912ae3e870a733acfd83c26e8a1f6a5ffcArgyrios Kyrtzidis  return CharSourceRange();
88411b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis}
88511b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis
886e64d9037658a1b95c79ea275af6167a110b3c563Argyrios KyrtzidisStringRef Lexer::getSourceText(CharSourceRange Range,
887e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis                               const SourceManager &SM,
888e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis                               const LangOptions &LangOpts,
889e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis                               bool *Invalid) {
890a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  Range = makeFileCharRange(Range, SM, LangOpts);
891a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  if (Range.isInvalid()) {
892e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    if (Invalid) *Invalid = true;
893e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    return StringRef();
894e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  }
895e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis
896e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  // Break down the source location.
897e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  std::pair<FileID, unsigned> beginInfo = SM.getDecomposedLoc(Range.getBegin());
898e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  if (beginInfo.first.isInvalid()) {
899e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    if (Invalid) *Invalid = true;
900e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    return StringRef();
901e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  }
902e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis
903e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  unsigned EndOffs;
904e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  if (!SM.isInFileID(Range.getEnd(), beginInfo.first, &EndOffs) ||
905e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis      beginInfo.second > EndOffs) {
906e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    if (Invalid) *Invalid = true;
907e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    return StringRef();
908e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  }
909e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis
910e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  // Try to the load the file buffer.
911e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  bool invalidTemp = false;
912e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  StringRef file = SM.getBufferData(beginInfo.first, &invalidTemp);
913e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  if (invalidTemp) {
914e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    if (Invalid) *Invalid = true;
915e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis    return StringRef();
916e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  }
917e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis
918e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  if (Invalid) *Invalid = false;
919e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  return file.substr(beginInfo.second, EndOffs - beginInfo.second);
920e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis}
921e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis
922c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna ZaksStringRef Lexer::getImmediateMacroName(SourceLocation Loc,
923c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks                                       const SourceManager &SM,
924c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks                                       const LangOptions &LangOpts) {
925c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  assert(Loc.isMacroID() && "Only reasonble to call this on macros");
9267f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis
9277f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis  // Find the location of the immediate macro expansion.
9287f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis  while (1) {
9297f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    FileID FID = SM.getFileID(Loc);
9307f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    const SrcMgr::SLocEntry *E = &SM.getSLocEntry(FID);
9317f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();
9327f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    Loc = Expansion.getExpansionLocStart();
9337f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    if (!Expansion.isMacroArgExpansion())
9347f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis      break;
9357f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis
9367f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    // For macro arguments we need to check that the argument did not come
9377f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    // from an inner macro, e.g: "MAC1( MAC2(foo) )"
9387f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis
9397f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    // Loc points to the argument id of the macro definition, move to the
9407f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    // macro expansion.
941c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks    Loc = SM.getImmediateExpansionRange(Loc).first;
9427f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    SourceLocation SpellLoc = Expansion.getSpellingLoc();
9437f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    if (SpellLoc.isFileID())
9447f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis      break; // No inner macro.
9457f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis
9467f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    // If spelling location resides in the same FileID as macro expansion
9477f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    // location, it means there is no inner macro.
9487f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    FileID MacroFID = SM.getFileID(Loc);
9497f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    if (SM.isInFileID(SpellLoc, MacroFID))
9507f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis      break;
9517f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis
9527f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    // Argument came from inner macro.
9537f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis    Loc = SpellLoc;
9547f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis  }
955c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks
956c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  // Find the spelling location of the start of the non-argument expansion
957c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  // range. This is where the macro name was spelled in order to begin
958c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  // expanding this macro.
9597f6cf9764b33381e03fcf7c44f7985a333212b06Argyrios Kyrtzidis  Loc = SM.getSpellingLoc(Loc);
960c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks
961c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  // Dig out the buffer where the macro name was spelled and the extents of the
962c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  // name so that we can render it into the expansion note.
963c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  std::pair<FileID, unsigned> ExpansionInfo = SM.getDecomposedLoc(Loc);
964c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  unsigned MacroTokenLength = Lexer::MeasureTokenLength(Loc, SM, LangOpts);
965c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first);
966c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  return ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength);
967c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks}
968c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks
9695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
9705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Character information.
9715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum {
9745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_HORZ_WS  = 0x01,  // ' ', '\t', '\f', '\v'.  Note, no '\0'
9755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_VERT_WS  = 0x02,  // '\r', '\n'
9765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_LETTER   = 0x04,  // a-z,A-Z
9775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_NUMBER   = 0x08,  // 0-9
9785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_UNDER    = 0x10,  // _
9792fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  CHAR_PERIOD   = 0x20,  // .
9802fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  CHAR_RAWDEL   = 0x40   // {}[]#<>%:;?*+-/^&|~!=,"'
9815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
98303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// Statically initialize CharInfo table based on ASCII character set
98403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// Reference: FreeBSD 7.2 /usr/share/misc/ascii
985a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic const unsigned char CharInfo[256] =
98603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner{
98703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 0 NUL         1 SOH         2 STX         3 ETX
98803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 4 EOT         5 ENQ         6 ACK         7 BEL
98903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
99003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
99103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 8 BS          9 HT         10 NL         11 VT
99203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//12 NP         13 CR         14 SO         15 SI
99303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
99403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_HORZ_WS, CHAR_VERT_WS, 0           , 0           ,
99503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//16 DLE        17 DC1        18 DC2        19 DC3
99603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//20 DC4        21 NAK        22 SYN        23 ETB
99703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
99803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
99903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//24 CAN        25 EM         26 SUB        27 ESC
100003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//28 FS         29 GS         30 RS         31 US
100103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
100203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
100303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//32 SP         33  !         34  "         35  #
100403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//36  $         37  %         38  &         39  '
10052fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   CHAR_HORZ_WS, CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL ,
10062fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   0           , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL ,
100703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//40  (         41  )         42  *         43  +
100803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//44  ,         45  -         46  .         47  /
10092fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   0           , 0           , CHAR_RAWDEL , CHAR_RAWDEL ,
10102fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   CHAR_RAWDEL , CHAR_RAWDEL , CHAR_PERIOD , CHAR_RAWDEL ,
101103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//48  0         49  1         50  2         51  3
101203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//52  4         53  5         54  6         55  7
101303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
101403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
101503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//56  8         57  9         58  :         59  ;
101603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//60  <         61  =         62  >         63  ?
10172fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   CHAR_NUMBER , CHAR_NUMBER , CHAR_RAWDEL , CHAR_RAWDEL ,
10182fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL ,
101903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//64  @         65  A         66  B         67  C
102003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//68  D         69  E         70  F         71  G
102103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
102203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
102303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//72  H         73  I         74  J         75  K
102403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//76  L         77  M         78  N         79  O
102503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
102603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
102703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//80  P         81  Q         82  R         83  S
102803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//84  T         85  U         86  V         87  W
102903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
103003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
103103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//88  X         89  Y         90  Z         91  [
103203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//92  \         93  ]         94  ^         95  _
10332fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_RAWDEL ,
10342fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   0           , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_UNDER  ,
103503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//96  `         97  a         98  b         99  c
103603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//100  d       101  e        102  f        103  g
103703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
103803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
103903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//104  h       105  i        106  j        107  k
104003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//108  l       109  m        110  n        111  o
104103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
104203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
104303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//112  p       113  q        114  r        115  s
104403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//116  t       117  u        118  v        119  w
104503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
104603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
104703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//120  x       121  y        122  z        123  {
10482fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper//124  |       125  }        126  ~        127 DEL
10492fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_RAWDEL ,
10502fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper   CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , 0
105103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner};
105203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner
1053a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic void InitCharacterInfo() {
10545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static bool isInited = false;
10555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (isInited) return;
105603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  // check the statically-initialized CharInfo table
105703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
105803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
105903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
106003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
106103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
106203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
106303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_UNDER   == CharInfo[(int)'_']);
106403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_PERIOD  == CharInfo[(int)'.']);
106503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  for (unsigned i = 'a'; i <= 'z'; ++i) {
106603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner    assert(CHAR_LETTER == CharInfo[i]);
106703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner    assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
106803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  }
10695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = '0'; i <= '9'; ++i)
107003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner    assert(CHAR_NUMBER == CharInfo[i]);
10717b682656310b0d7e94a98d982444bc7ddedc653fSteve Naroff
107203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  isInited = true;
10735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
107503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner
10765cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith/// isIdentifierHead - Return true if this is the first character of an
10775cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith/// identifier, which is [a-zA-Z_].
10785cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smithstatic inline bool isIdentifierHead(unsigned char c) {
10795cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  return (CharInfo[c] & (CHAR_LETTER|CHAR_UNDER)) ? true : false;
10805cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith}
10815cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith
10825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIdentifierBody - Return true if this is the body character of an
10835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// identifier, which is [a-zA-Z0-9_].
10845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isIdentifierBody(unsigned char c) {
108595c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
10865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isHorizontalWhitespace - Return true if this character is horizontal
1089a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// whitespace: ' ', '\\t', '\\f', '\\v'.  Note that this returns false for
1090a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// '\\0'.
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isHorizontalWhitespace(unsigned char c) {
109295c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1095aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// isVerticalWhitespace - Return true if this character is vertical
1096a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// whitespace: '\\n', '\\r'.  Note that this returns false for '\\0'.
1097aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaksstatic inline bool isVerticalWhitespace(unsigned char c) {
1098aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  return (CharInfo[c] & CHAR_VERT_WS) ? true : false;
1099aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks}
1100aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
11015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isWhitespace - Return true if this character is horizontal or vertical
1102a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// whitespace: ' ', '\\t', '\\f', '\\v', '\\n', '\\r'.  Note that this returns
1103a05369fbb7ec2840d6e4d53cbc48b70a8e9e37acJames Dennett/// false for '\\0'.
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isWhitespace(unsigned char c) {
110595c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
11065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNumberBody - Return true if this is the body character of an
11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// preprocessing number, which is [a-zA-Z0-9_.].
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isNumberBody(unsigned char c) {
11111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
111295c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser    true : false;
11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11152fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// isRawStringDelimBody - Return true if this is the body character of a
11162fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// raw string delimiter.
11172fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topperstatic inline bool isRawStringDelimBody(unsigned char c) {
11182fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  return (CharInfo[c] &
11192fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD|CHAR_RAWDEL)) ?
11202fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    true : false;
11212fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper}
11222fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
1123d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose// Allow external clients to make use of CharInfo.
1124d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rosebool Lexer::isIdentifierBodyChar(char c, const LangOptions &LangOpts) {
1125d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose  return isIdentifierBody(c) || (c == '$' && LangOpts.DollarIdents);
1126d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose}
1127d880b3aa6d594d1a7f2d307c29378c6f59b216ffJordan Rose
11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
11305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Diagnostics forwarding code.
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1133409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
1134433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth/// lexer buffer was all expanded at a single point, perform the mapping.
1135409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// This is currently only used for _Pragma implementation, so it is the slow
1136409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// path of the hot getSourceLocation method.  Do not allow it to be inlined.
113714bd96571ef6f0e97dc79ec4d01b547d60e8fa68Chandler Carruthstatic LLVM_ATTRIBUTE_NOINLINE SourceLocation GetMappedTokenLoc(
113814bd96571ef6f0e97dc79ec4d01b547d60e8fa68Chandler Carruth    Preprocessor &PP, SourceLocation FileLoc, unsigned CharNo, unsigned TokLen);
1139409a03671224d4e5bdab1594c43baf070148f830Chris Lattnerstatic SourceLocation GetMappedTokenLoc(Preprocessor &PP,
1140409a03671224d4e5bdab1594c43baf070148f830Chris Lattner                                        SourceLocation FileLoc,
1141de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                        unsigned CharNo, unsigned TokLen) {
1142433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  assert(FileLoc.isMacroID() && "Must be a macro expansion");
11431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1144409a03671224d4e5bdab1594c43baf070148f830Chris Lattner  // Otherwise, we're lexing "mapped tokens".  This is used for things like
1145433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  // _Pragma handling.  Combine the expansion location of FileLoc with the
1146df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  // spelling location.
1147e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  SourceManager &SM = PP.getSourceManager();
11481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1149433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  // Create a new SLoc which is expanded from Expansion(FileLoc) but whose
1150df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  // characters come from spelling(FileLoc)+Offset.
1151e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
1152a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  SpellingLoc = SpellingLoc.getLocWithOffset(CharNo);
11531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1154e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  // Figure out the expansion loc range, which is the range covered by the
1155e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  // original _Pragma(...) sequence.
1156e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  std::pair<SourceLocation,SourceLocation> II =
1157999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth    SM.getImmediateExpansionRange(FileLoc);
11581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1159bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  return SM.createExpansionLoc(SpellingLoc, II.first, II.second, TokLen);
1160409a03671224d4e5bdab1594c43baf070148f830Chris Lattner}
1161409a03671224d4e5bdab1594c43baf070148f830Chris Lattner
11625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSourceLocation - Return a source location identifier for the specified
11635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// offset in the current file.
1164de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris LattnerSourceLocation Lexer::getSourceLocation(const char *Loc,
1165de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                        unsigned TokLen) const {
1166448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner  assert(Loc >= BufferStart && Loc <= BufferEnd &&
11675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer         "Location out of range for this buffer!");
11689dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner
11699dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  // In the normal case, we're just lexing from a simple file buffer, return
11709dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  // the file id from FileLoc with the offset specified.
1171448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner  unsigned CharNo = Loc-BufferStart;
11729dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  if (FileLoc.isFileID())
1173a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis    return FileLoc.getLocWithOffset(CharNo);
11741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11752b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  // Otherwise, this is the _Pragma lexer case, which pretends that all of the
11762b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  // tokens are lexed from where the _Pragma was defined.
1177168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  assert(PP && "This doesn't work on raw lexers");
1178de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
11795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Diag - Forwarding function for diagnostics.  This translate a source
11825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// position in the current buffer into a SourceLocation object for rendering.
11833cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
11843692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner  return PP->Diag(getSourceLocation(Loc), DiagID);
11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
11885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Trigraph and Escaped Newline Handling Code.
11895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
11905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
11925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char GetTrigraphCharForLetter(char Letter) {
11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Letter) {
11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:   return 0;
11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '=':  return '#';
11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ')':  return ']';
11985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '(':  return '[';
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  }
12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// DecodeTrigraphChar - If the specified character is a legal trigraph when
12095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// prefixed with ??, emit a trigraph warning.  If trigraphs are enabled,
12105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the result character.  Finally, emit a warning about trigraph use
12115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whether trigraphs are enabled or not.
12125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char DecodeTrigraphChar(const char *CP, Lexer *L) {
12135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char Res = GetTrigraphCharForLetter(*CP);
12143692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner  if (!Res || !L) return Res;
12151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12164e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!L->getLangOpts().Trigraphs) {
121774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!L->isLexingRawMode())
121874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      L->Diag(CP-2, diag::trigraph_ignored);
12193692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner    return 0;
12205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
122274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (!L->isLexingRawMode())
12235f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    L->Diag(CP-2, diag::trigraph_converted) << StringRef(&Res, 1);
12245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return Res;
12255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
122724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// getEscapedNewLineSize - Return the size of the specified escaped newline,
122824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a
12291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// trigraph equivalent on entry to this function.
123024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattnerunsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
123124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  unsigned Size = 0;
123224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  while (isWhitespace(Ptr[Size])) {
123324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    ++Size;
12341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
123524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
123624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      continue;
123724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
123824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    // If this is a \r\n or \n\r, skip the other half.
123924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
124024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner        Ptr[Size-1] != Ptr[Size])
124124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      ++Size;
12421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
124324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    return Size;
12441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
12451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
124624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  // Not an escaped newline, must be a \t or something else.
124724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  return 0;
124824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner}
124924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
1250033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
1251033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// them), skip over them and return the first non-escaped-newline found,
1252033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// otherwise return P.
1253033749571f8d4c804eeb357c70b06424aa24503bChris Lattnerconst char *Lexer::SkipEscapedNewLines(const char *P) {
1254033749571f8d4c804eeb357c70b06424aa24503bChris Lattner  while (1) {
1255033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    const char *AfterEscape;
1256033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    if (*P == '\\') {
1257033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      AfterEscape = P+1;
1258033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    } else if (*P == '?') {
1259033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      // If not a trigraph for escape, bail out.
1260033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      if (P[1] != '?' || P[2] != '/')
1261033749571f8d4c804eeb357c70b06424aa24503bChris Lattner        return P;
1262033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      AfterEscape = P+3;
1263033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    } else {
1264033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      return P;
1265033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    }
12661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1267033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
1268033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    if (NewLineSize == 0) return P;
1269033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    P = AfterEscape+NewLineSize;
1270033749571f8d4c804eeb357c70b06424aa24503bChris Lattner  }
1271033749571f8d4c804eeb357c70b06424aa24503bChris Lattner}
1272033749571f8d4c804eeb357c70b06424aa24503bChris Lattner
1273aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// \brief Checks that the given token is the first token that occurs after the
1274aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// given location (this excludes comments and whitespace). Returns the location
1275aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// immediately after the specified token. If the token is not found or the
1276aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks/// location is inside a macro, the returned source location will be invalid.
1277aca25bccefe56121b686706afc84c8cb5d46e65bAnna ZaksSourceLocation Lexer::findLocationAfterToken(SourceLocation Loc,
1278aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks                                        tok::TokenKind TKind,
1279aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks                                        const SourceManager &SM,
1280aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks                                        const LangOptions &LangOpts,
1281aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks                                        bool SkipTrailingWhitespaceAndNewLine) {
1282aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  if (Loc.isMacroID()) {
128369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    if (!Lexer::isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc))
1284aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks      return SourceLocation();
1285aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  }
1286aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, LangOpts);
1287aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
1288aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  // Break down the source location.
1289aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
1290aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
1291aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  // Try to load the file buffer.
1292aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  bool InvalidTemp = false;
1293aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  llvm::StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp);
1294aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  if (InvalidTemp)
1295aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks    return SourceLocation();
1296aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
1297aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  const char *TokenBegin = File.data() + LocInfo.second;
1298aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
1299aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  // Lex from the start of the given location.
1300aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  Lexer lexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(),
1301aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks                                      TokenBegin, File.end());
1302aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  // Find the token.
1303aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  Token Tok;
1304aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  lexer.LexFromRawLexer(Tok);
1305aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  if (Tok.isNot(TKind))
1306aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks    return SourceLocation();
1307aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  SourceLocation TokenLoc = Tok.getLocation();
1308aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
1309aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  // Calculate how much whitespace needs to be skipped if any.
1310aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  unsigned NumWhitespaceChars = 0;
1311aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  if (SkipTrailingWhitespaceAndNewLine) {
1312aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks    const char *TokenEnd = SM.getCharacterData(TokenLoc) +
1313aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks                           Tok.getLength();
1314aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks    unsigned char C = *TokenEnd;
1315aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks    while (isHorizontalWhitespace(C)) {
1316aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks      C = *(++TokenEnd);
1317aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks      NumWhitespaceChars++;
1318aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks    }
1319aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks    if (isVerticalWhitespace(C))
1320aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks      NumWhitespaceChars++;
1321aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks  }
1322aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
1323a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis  return TokenLoc.getLocWithOffset(Tok.getLength() + NumWhitespaceChars);
1324aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks}
132524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
13265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
13275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// get its size, and return it.  This is tricky in several cases:
13285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   1. If currently at the start of a trigraph, we warn about the trigraph,
13295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      then either return the trigraph (skipping 3 chars) or the '?',
13305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      depending on whether trigraphs are enabled or not.
13315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   2. If this is an escaped newline (potentially with whitespace between
13325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      the backslash and newline), implicitly skip the newline and return
13335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      the char after it.
13345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   3. If this is a UCN, return it.  FIXME: C++ UCN's?
13355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
13365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This handles the slow/uncommon case of the getCharAndSize method.  Here we
13375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// know that we can accumulate into Size, and that we have already incremented
13385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Ptr by Size bytes.
13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
13405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match.
13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
1344d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner                               Token *Tok) {
13455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, look for an escaped newline.
13465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '\\') {
13475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Size;
13485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Ptr;
13495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash:
13505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Common case, backslash-char where the char is not whitespace.
13515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!isWhitespace(Ptr[0])) return '\\';
13521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13535636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner    // See if we have optional whitespace characters between the slash and
13545636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner    // newline.
135524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
135624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Remember that this token needs to be cleaned.
135724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      if (Tok) Tok->setFlag(Token::NeedsCleaning);
135824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
135924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Warn if there was whitespace between the backslash and newline.
13605636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner      if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
136124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner        Diag(Ptr, diag::backslash_newline_space);
13621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
136324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Found backslash<whitespace><newline>.  Parse the char after it.
136424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Size += EscapedNewLineSize;
136524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Ptr  += EscapedNewLineSize;
1366f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis
136704a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      // If the char that we finally got was a \n, then we must have had
136804a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      // something like \<newline><newline>.  We don't want to consume the
136904a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      // second newline.
137004a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0')
137104a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis        return ' ';
1372f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis
137324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Use slow version to accumulate a correct size field.
137424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      return getCharAndSizeSlow(Ptr, Size, Tok);
137524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    }
13761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is not an escaped newline, just return the slash.
13785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return '\\';
13795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a trigraph, process it.
13825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '?' && Ptr[1] == '?') {
13835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is actually a legal trigraph (not something like "??x"), emit
13845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // a trigraph warning.  If so, and if trigraphs are enabled, return it.
13855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
13865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Remember that this token needs to be cleaned.
1387d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner      if (Tok) Tok->setFlag(Token::NeedsCleaning);
13885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Ptr += 3;
13905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Size += 3;
13915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '\\') goto Slash;
13925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return C;
13935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
13945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is neither, return a single character.
13975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++Size;
13985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return *Ptr;
13995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
14035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeNoWarn method.  Here we know that we can accumulate into Size,
14045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// and that we have already incremented Ptr by Size bytes.
14055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
14065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
14075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match.
14085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
14094e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                                     const LangOptions &LangOpts) {
14105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, look for an escaped newline.
14115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '\\') {
14125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Size;
14135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Ptr;
14145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash:
14155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Common case, backslash-char where the char is not whitespace.
14165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!isWhitespace(Ptr[0])) return '\\';
14171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // See if we have optional whitespace characters followed by a newline.
141924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
142024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Found backslash<whitespace><newline>.  Parse the char after it.
142124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Size += EscapedNewLineSize;
142224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Ptr  += EscapedNewLineSize;
14231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
142404a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      // If the char that we finally got was a \n, then we must have had
142504a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      // something like \<newline><newline>.  We don't want to consume the
142604a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      // second newline.
142704a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis      if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0')
142804a94bcc56438b17e88db592708324041f75d48cArgyrios Kyrtzidis        return ' ';
1429f132dcaae82ebfc44c4fe0e84bf0b1f95e9d1251Argyrios Kyrtzidis
143024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Use slow version to accumulate a correct size field.
14314e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      return getCharAndSizeSlowNoWarn(Ptr, Size, LangOpts);
143224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    }
14331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is not an escaped newline, just return the slash.
14355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return '\\';
14365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a trigraph, process it.
14394e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (LangOpts.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
14405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is actually a legal trigraph (not something like "??x"), return
14415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // it.
14425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (char C = GetTrigraphCharForLetter(Ptr[2])) {
14435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Ptr += 3;
14445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Size += 3;
14455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '\\') goto Slash;
14465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return C;
14475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
14485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is neither, return a single character.
14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++Size;
14525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return *Ptr;
14535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
14565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper methods for lexing.
14575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
14585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1459f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor/// \brief Routine that indiscriminately skips bytes in the source file.
1460f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregorvoid Lexer::SkipBytes(unsigned Bytes, bool StartOfLine) {
1461f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  BufferPtr += Bytes;
1462f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  if (BufferPtr > BufferEnd)
1463f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    BufferPtr = BufferEnd;
1464f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  IsAtStartOfLine = StartOfLine;
1465f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor}
1466f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
1467d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
14685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
14695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned Size;
14705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned char C = *CurPtr++;
1471cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  while (isIdentifierBody(C))
14725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr++;
1473cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner
14745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  --CurPtr;   // Back up over the skipped character.
14755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Fast path, no $,\,? in identifier found.  '\' might be an escaped newline
14775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
14785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: UCNs.
1479cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  //
1480cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  // TODO: Could merge these checks into a CharInfo flag to make the comparison
1481cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  // cheaper
14824e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (C != '\\' && C != '?' && (C != '$' || !LangOpts.DollarIdents)) {
14835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerFinishIdentifier:
14845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const char *IdStart = BufferPtr;
1485c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    FormTokenWithChars(Result, CurPtr, tok::raw_identifier);
1486c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    Result.setRawIdentifierData(IdStart);
14871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we are in raw mode, return this identifier raw.  There is no need to
14895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // look up identifier information or attempt to macro expand it.
1490c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    if (LexingRawMode)
1491c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara      return;
14921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1493c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    // Fill in Result.IdentifierInfo and update the token kind,
1494c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    // looking up the identifier in the identifier table.
1495c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara    IdentifierInfo *II = PP->LookUpIdentifierInfo(Result);
14961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Finally, now that we know we have an identifier, pass this off to the
14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // preprocessor, which may macro expand it or something.
1499d1186fa38166a581b51975f0382a45fc3a0733d0Chris Lattner    if (II->isHandleIdentifierCase())
15006a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner      PP->HandleIdentifier(Result);
15016aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
15026a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    return;
15035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, $,\,? in identifier found.  Enter slower path.
15061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  C = getCharAndSize(CurPtr, Size);
15085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
15095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '$') {
15105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If we hit a $ and they are not supported in identifiers, we are done.
15114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!LangOpts.DollarIdents) goto FinishIdentifier;
15121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Otherwise, emit a diagnostic and continue.
151474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
151574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(CurPtr, diag::ext_dollar_in_identifier);
15165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, Size, Result);
15175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getCharAndSize(CurPtr, Size);
15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      continue;
15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
15205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Found end of identifier.
15215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      goto FinishIdentifier;
15225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
15235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this character is good, consume it.
15255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr = ConsumeChar(CurPtr, Size, Result);
15265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getCharAndSize(CurPtr, Size);
15285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (isIdentifierBody(C)) { // FIXME: UCNs.
15295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, Size, Result);
15305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getCharAndSize(CurPtr, Size);
15315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
15325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1535a75ec43d625753b4439b0d6f70bd988444c74617Douglas Gregor/// isHexaLiteral - Return true if Start points to a hex constant.
15364a551000bee716ac8b1bbe16134a53f0ad221a5aChris Lattner/// in microsoft mode (where this is supposed to be several different tokens).
1537e506f8a41063410c75945ebb804758bd0202947fEli Friedmanbool Lexer::isHexaLiteral(const char *Start, const LangOptions &LangOpts) {
15386ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner  unsigned Size;
15394e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  char C1 = Lexer::getCharAndSizeNoWarn(Start, Size, LangOpts);
15406ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner  if (C1 != '0')
15416ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner    return false;
15424e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  char C2 = Lexer::getCharAndSizeNoWarn(Start + Size, Size, LangOpts);
15436ab55ebab20086f725c4017b48ef8d7691ef870fChris Lattner  return (C2 == 'x' || C2 == 'X');
1544a75ec43d625753b4439b0d6f70bd988444c74617Douglas Gregor}
15455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15465253c7ff266ae79308050c9f43d60dd1a67c5fb9Nate Begeman/// LexNumericConstant - Lex the remainder of a integer or floating point
15475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. From[-1] is the first character lexed.  Return the end of the
15485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant.
1549d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
15505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned Size;
15515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getCharAndSize(CurPtr, Size);
15525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char PrevCh = 0;
15535cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  while (isNumberBody(C)) { // FIXME: UCNs.
15545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr = ConsumeChar(CurPtr, Size, Result);
15555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PrevCh = C;
15565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getCharAndSize(CurPtr, Size);
15575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we fell out, check for a sign, due to 1e+12.  If we have one, continue.
1560b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner  if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) {
1561b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner    // If we are in Microsoft mode, don't continue if the constant is hex.
1562b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner    // For example, MSVC will accept the following as 3 tokens: 0x1234567e+1
15634e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!LangOpts.MicrosoftExt || !isHexaLiteral(BufferPtr, LangOpts))
1564b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner      return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
1565b2f4a20ddc281b194caa00e850ad74759e0d50baChris Lattner  }
15665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a hex FP constant, continue.
1568d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith  if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p')) {
1569d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith    // Outside C99, we accept hexadecimal floating point numbers as a
1570d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith    // not-quite-conforming extension. Only do so if this looks like it's
1571d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith    // actually meant to be a hexfloat, and not if it has a ud-suffix.
1572d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith    bool IsHexFloat = true;
1573d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith    if (!LangOpts.C99) {
1574d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith      if (!isHexaLiteral(BufferPtr, LangOpts))
1575d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith        IsHexFloat = false;
1576d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith      else if (std::find(BufferPtr, CurPtr, '_') != CurPtr)
1577d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith        IsHexFloat = false;
1578d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith    }
1579d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith    if (IsHexFloat)
1580d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith      return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
1581d2e95d1538ff91fe902464f02f83429f96117af5Richard Smith  }
15821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
158447246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
15859e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
158647246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
15875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15895cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith/// LexUDSuffix - Lex the ud-suffix production for user-defined literal suffixes
1590e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith/// in C++11, or warn on a ud-suffix in C++98.
15915cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smithconst char *Lexer::LexUDSuffix(Token &Result, const char *CurPtr) {
15924e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().CPlusPlus);
15935cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith
15945cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  // Maximally munch an identifier. FIXME: UCNs.
15955cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  unsigned Size;
15965cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  char C = getCharAndSize(CurPtr, Size);
15975cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  if (isIdentifierHead(C)) {
15984e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!getLangOpts().CPlusPlus0x) {
1599e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith      if (!isLexingRawMode())
16002fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith        Diag(CurPtr,
16012fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith             C == '_' ? diag::warn_cxx11_compat_user_defined_literal
16022fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith                      : diag::warn_cxx11_compat_reserved_user_defined_literal)
16032fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith          << FixItHint::CreateInsertion(getSourceLocation(CurPtr), " ");
16042fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith      return CurPtr;
16052fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith    }
16062fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith
16072fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith    // C++11 [lex.ext]p10, [usrlit.suffix]p1: A program containing a ud-suffix
16082fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith    // that does not start with an underscore is ill-formed. As a conforming
16092fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith    // extension, we treat all such suffixes as if they had whitespace before
16102fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith    // them.
16112fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith    if (C != '_') {
16122fb4ae36825ca3a0cbe7e845c5747062870066beRichard Smith      if (!isLexingRawMode())
1613b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet        Diag(CurPtr, getLangOpts().MicrosoftMode ?
1614b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet            diag::ext_ms_reserved_user_defined_literal :
1615b0afd5df3c427c329f6c5e00fe264c5bada3bf36Francois Pichet            diag::ext_reserved_user_defined_literal)
1616e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith          << FixItHint::CreateInsertion(getSourceLocation(CurPtr), " ");
1617e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith      return CurPtr;
1618e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith    }
1619e816c717d4de1de6c67f1fd5ef4a927fe2bf2ea7Richard Smith
162099831e4677a7e2e051af636221694d60ba31fcdbRichard Smith    Result.setFlag(Token::HasUDSuffix);
16215cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith    do {
16225cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith      CurPtr = ConsumeChar(CurPtr, Size, Result);
16235cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith      C = getCharAndSize(CurPtr, Size);
16245cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith    } while (isIdentifierBody(C));
16255cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  }
16265cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  return CurPtr;
16275cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith}
16285cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith
16295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
16305cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor/// either " or L" or u8" or u" or U".
16315cee1195584fa8672253139c86e922daeda69b9eDouglas Gregorvoid Lexer::LexStringLiteral(Token &Result, const char *CurPtr,
16325cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                             tok::TokenKind Kind) {
16335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this string contain the \0 character?
16341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1635661a99690bc133bbaa029da925481d4a860dec90Richard Smith  if (!isLexingRawMode() &&
1636661a99690bc133bbaa029da925481d4a860dec90Richard Smith      (Kind == tok::utf8_string_literal ||
1637661a99690bc133bbaa029da925481d4a860dec90Richard Smith       Kind == tok::utf16_string_literal ||
1638661a99690bc133bbaa029da925481d4a860dec90Richard Smith       Kind == tok::utf32_string_literal))
1639661a99690bc133bbaa029da925481d4a860dec90Richard Smith    Diag(BufferPtr, diag::warn_cxx98_compat_unicode_literal);
1640661a99690bc133bbaa029da925481d4a860dec90Richard Smith
16415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
16425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (C != '"') {
1643571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    // Skip escaped characters.  Escaped newlines will already be processed by
1644571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    // getAndAdvanceChar.
1645571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    if (C == '\\')
16465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getAndAdvanceChar(CurPtr, Result);
164733611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor
1648571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    if (C == '\n' || C == '\r' ||             // Newline.
164933611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor        (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
16504e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!isLexingRawMode() && !LangOpts.AsmPreprocessor)
1651b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith        Diag(BufferPtr, diag::ext_unterminated_string);
16529e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr-1, tok::unknown);
16535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return;
16545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1655571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner
16567d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    if (C == 0) {
16577d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      if (isCodeCompletionPoint(CurPtr-1)) {
16587d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        PP->CodeCompleteNaturalLanguage();
16597d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        FormTokenWithChars(Result, CurPtr-1, tok::unknown);
16607d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return cutOffLexing();
16617d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      }
16627d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
1663571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner      NulCharacter = CurPtr-1;
16647d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    }
16655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
16665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16685cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  // If we are in C++11, lex the optional ud-suffix.
16694e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
16705cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith    CurPtr = LexUDSuffix(Result, CurPtr);
16715cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith
16725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If a nul character existed in the string, warn about it.
167374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
167474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_string);
16755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of the token as well as the BufferPtr instance var.
167747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
16785cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  FormTokenWithChars(Result, CurPtr, Kind);
167947246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
16805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16822fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// LexRawStringLiteral - Lex the remainder of a raw string literal, after
16832fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper/// having lexed R", LR", u8R", uR", or UR".
16842fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Toppervoid Lexer::LexRawStringLiteral(Token &Result, const char *CurPtr,
16852fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                tok::TokenKind Kind) {
16862fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  // This function doesn't use getAndAdvanceChar because C++0x [lex.pptoken]p3:
16872fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  //  Between the initial and final double quote characters of the raw string,
16882fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  //  any transformations performed in phases 1 and 2 (trigraphs,
16892fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  //  universal-character-names, and line splicing) are reverted.
16902fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
1691661a99690bc133bbaa029da925481d4a860dec90Richard Smith  if (!isLexingRawMode())
1692661a99690bc133bbaa029da925481d4a860dec90Richard Smith    Diag(BufferPtr, diag::warn_cxx98_compat_raw_string_literal);
1693661a99690bc133bbaa029da925481d4a860dec90Richard Smith
16942fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  unsigned PrefixLen = 0;
16952fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
16962fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  while (PrefixLen != 16 && isRawStringDelimBody(CurPtr[PrefixLen]))
16972fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    ++PrefixLen;
16982fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
16992fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  // If the last character was not a '(', then we didn't lex a valid delimiter.
17002fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  if (CurPtr[PrefixLen] != '(') {
17012fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    if (!isLexingRawMode()) {
17022fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      const char *PrefixEnd = &CurPtr[PrefixLen];
17032fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (PrefixLen == 16) {
17042fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        Diag(PrefixEnd, diag::err_raw_delim_too_long);
17052fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      } else {
17062fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        Diag(PrefixEnd, diag::err_invalid_char_raw_delim)
17072fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          << StringRef(PrefixEnd, 1);
17082fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      }
17092fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    }
17102fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17112fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    // Search for the next '"' in hopes of salvaging the lexer. Unfortunately,
17122fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    // it's possible the '"' was intended to be part of the raw string, but
17132fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    // there's not much we can do about that.
17142fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    while (1) {
17152fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      char C = *CurPtr++;
17162fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17172fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (C == '"')
17182fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        break;
17192fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (C == 0 && CurPtr-1 == BufferEnd) {
17202fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        --CurPtr;
17212fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        break;
17222fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      }
17232fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    }
17242fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17252fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    FormTokenWithChars(Result, CurPtr, tok::unknown);
17262fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    return;
17272fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  }
17282fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17292fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  // Save prefix and move CurPtr past it
17302fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  const char *Prefix = CurPtr;
17312fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  CurPtr += PrefixLen + 1; // skip over prefix and '('
17322fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17332fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  while (1) {
17342fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    char C = *CurPtr++;
17352fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17362fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    if (C == ')') {
17372fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      // Check for prefix match and closing quote.
17382fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (strncmp(CurPtr, Prefix, PrefixLen) == 0 && CurPtr[PrefixLen] == '"') {
17392fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        CurPtr += PrefixLen + 1; // skip over prefix and '"'
17402fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        break;
17412fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      }
17422fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    } else if (C == 0 && CurPtr-1 == BufferEnd) { // End of file.
17432fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (!isLexingRawMode())
17442fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        Diag(BufferPtr, diag::err_unterminated_raw_string)
17452fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          << StringRef(Prefix, PrefixLen);
17462fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      FormTokenWithChars(Result, CurPtr-1, tok::unknown);
17472fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      return;
17482fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    }
17492fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  }
17502fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17515cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  // If we are in C++11, lex the optional ud-suffix.
17524e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
17535cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith    CurPtr = LexUDSuffix(Result, CurPtr);
17545cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith
17552fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  // Update the location of token as well as BufferPtr.
17562fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  const char *TokStart = BufferPtr;
17572fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  FormTokenWithChars(Result, CurPtr, Kind);
17582fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  Result.setLiteralData(TokStart);
17592fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper}
17602fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
17615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
17625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// after having lexed the '<' character.  This is used for #include filenames.
1763d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
17645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this string contain the \0 character?
17659cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner  const char *AfterLessPos = CurPtr;
17665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
17675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (C != '>') {
17685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip escaped characters.
17695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '\\') {
17705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip the escaped character.
177160b202c5eb6fb2d608bfef05523f40fdf5091d48Dmitri Gribenko      getAndAdvanceChar(CurPtr, Result);
17725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == '\n' || C == '\r' ||             // Newline.
17737d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis               (C == 0 && (CurPtr-1 == BufferEnd ||  // End of file.
17747d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis                           isCodeCompletionPoint(CurPtr-1)))) {
17759cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      // If the filename is unterminated, then it must just be a lone <
17769cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      // character.  Return this as such.
17779cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      FormTokenWithChars(Result, AfterLessPos, tok::less);
17785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return;
17795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == 0) {
17805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      NulCharacter = CurPtr-1;
17815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
17825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
17835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
17841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If a nul character existed in the string, warn about it.
178674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
178774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_string);
17881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
179047246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
17919e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
179247246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
17935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexCharConstant - Lex the remainder of a character constant, after having
17975cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor/// lexed either ' or L' or u' or U'.
17985cee1195584fa8672253139c86e922daeda69b9eDouglas Gregorvoid Lexer::LexCharConstant(Token &Result, const char *CurPtr,
17995cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                            tok::TokenKind Kind) {
18005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this character contain the \0 character?
18015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1802661a99690bc133bbaa029da925481d4a860dec90Richard Smith  if (!isLexingRawMode() &&
1803661a99690bc133bbaa029da925481d4a860dec90Richard Smith      (Kind == tok::utf16_char_constant || Kind == tok::utf32_char_constant))
1804661a99690bc133bbaa029da925481d4a860dec90Richard Smith    Diag(BufferPtr, diag::warn_cxx98_compat_unicode_literal);
1805661a99690bc133bbaa029da925481d4a860dec90Richard Smith
18065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
18075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (C == '\'') {
18084e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!isLexingRawMode() && !LangOpts.AsmPreprocessor)
1809b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith      Diag(BufferPtr, diag::ext_empty_character);
18109e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::unknown);
18115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return;
18125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
18131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1814d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner  while (C != '\'') {
1815d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    // Skip escaped characters.
1816d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    if (C == '\\') {
1817d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      // Skip the escaped character.
1818d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      // FIXME: UCN's
181960b202c5eb6fb2d608bfef05523f40fdf5091d48Dmitri Gribenko      getAndAdvanceChar(CurPtr, Result);
1820d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    } else if (C == '\n' || C == '\r' ||             // Newline.
1821d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner               (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
18224e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!isLexingRawMode() && !LangOpts.AsmPreprocessor)
1823b6ebd4490235c9ea6016530d623c46d0b9ce565bRichard Smith        Diag(BufferPtr, diag::ext_unterminated_char);
1824d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      FormTokenWithChars(Result, CurPtr-1, tok::unknown);
1825d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      return;
1826d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    } else if (C == 0) {
18277d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      if (isCodeCompletionPoint(CurPtr-1)) {
18287d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        PP->CodeCompleteNaturalLanguage();
18297d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        FormTokenWithChars(Result, CurPtr-1, tok::unknown);
18307d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return cutOffLexing();
18317d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      }
18327d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
1833d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      NulCharacter = CurPtr-1;
1834d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    }
1835d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    C = getAndAdvanceChar(CurPtr, Result);
18365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
18371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18385cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith  // If we are in C++11, lex the optional ud-suffix.
18394e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
18405cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith    CurPtr = LexUDSuffix(Result, CurPtr);
18415cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith
1842d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner  // If a nul character existed in the character, warn about it.
184374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
184474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_char);
18455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
184747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
18485cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  FormTokenWithChars(Result, CurPtr, Kind);
184947246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
18505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
18535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Update BufferPtr to point to the next non-whitespace character and return.
1854d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner///
1855d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
1856d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner///
1857d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnerbool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
18585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Whitespace - Skip it, then return the token after the whitespace.
18595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned char Char = *CurPtr;  // Skip consequtive spaces efficiently.
18605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
18615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip horizontal whitespace very aggressively.
18625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (isHorizontalWhitespace(Char))
18635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Char = *++CurPtr;
18641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1865ddd3e8b90a92c39f620bda7a0945320c0b9e60dbDaniel Dunbar    // Otherwise if we have something other than whitespace, we're done.
18665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char != '\n' && Char != '\r')
18675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
18681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingPreprocessorDirective) {
18705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // End of preprocessor directive line, let LexTokenInternal handle this.
18715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      BufferPtr = CurPtr;
1872d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return false;
18735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
18741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // ok, but handle newline.
18765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // The returned token is at the start of the line.
1877d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::StartOfLine);
18785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // No leading whitespace seen so far.
1879d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.clearFlag(Token::LeadingSpace);
18805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = *++CurPtr;
18815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
18825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this isn't immediately after a newline, there is leading space.
18845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char PrevChar = CurPtr[-1];
18855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PrevChar != '\n' && PrevChar != '\r')
1886d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
18875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1888d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // If the client wants us to return whitespace, return it now.
1889d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  if (isKeepWhitespaceMode()) {
18909e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::unknown);
1891d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    return true;
1892d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  }
18931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
1895d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  return false;
18965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// SkipBCPLComment - We have just read the // characters from input.  Skip until
18995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// we find the newline character thats terminate the comment.  Then update
1900046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// BufferPtr and return.
1901046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner///
1902046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted
1903046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true.
1904d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
19055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If BCPL comments aren't explicitly enabled for this language, emit an
19065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // extension warning.
19074e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!LangOpts.BCPLComment && !isLexingRawMode()) {
19085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(BufferPtr, diag::ext_bcpl_comment);
19091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Mark them enabled so we only emit one warning for this translation
19115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // unit.
19124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    LangOpts.BCPLComment = true;
19135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
19141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Scan over the body of the comment.  The common case, when scanning, is that
19165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the comment contains normal ascii characters with nothing interesting in
19175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // them.  As such, optimize for this case with the inner loop.
19185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C;
19195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  do {
19205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr;
19215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over characters in the fast loop.
19225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (C != 0 &&                // Potentially EOF.
19235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           C != '\n' && C != '\r')  // Newline or DOS-style newline.
19245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *++CurPtr;
19255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19261daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer    const char *NextLine = CurPtr;
19271daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer    if (C != 0) {
19281daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      // We found a newline, see if it's escaped.
19291daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      const char *EscapePtr = CurPtr-1;
19301daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      while (isHorizontalWhitespace(*EscapePtr)) // Skip whitespace.
19311daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer        --EscapePtr;
19321daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer
19331daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      if (*EscapePtr == '\\') // Escaped newline.
19341daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer        CurPtr = EscapePtr;
19351daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      else if (EscapePtr[0] == '/' && EscapePtr[-1] == '?' &&
19361daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer               EscapePtr[-2] == '?') // Trigraph-escaped newline.
19371daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer        CurPtr = EscapePtr-2;
19381daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      else
19391daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer        break; // This is a newline, we're done.
19401daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer    }
19411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is a hard case.  Fall back on getAndAdvanceChar to
1943bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // properly decode the character.  Read it in raw mode to avoid emitting
1944bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // diagnostics about things like trigraphs.  If we see an escaped newline,
1945bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // we'll handle it below.
19465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const char *OldPtr = CurPtr;
1947bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    bool OldRawMode = isLexingRawMode();
1948bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    LexingRawMode = true;
19495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
1950bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    LexingRawMode = OldRawMode;
1951ead616c5d8681a41b11273327813e61bda01907aChris Lattner
19521daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer    // If we only read only one character, then no special handling is needed.
19531daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer    // We're done and can skip forward to the newline.
19541daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer    if (C != 0 && CurPtr == OldPtr+1) {
19551daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      CurPtr = NextLine;
19561daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer      break;
19571daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer    }
19581daa58ea8973854b62e79f0939fa37cb6fed00e6Benjamin Kramer
19595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we read multiple characters, and one of those characters was a \r or
19605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n, then we had an escaped newline within the comment.  Emit diagnostic
19615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // unless the next line is also a // comment.
19625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
19635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      for (; OldPtr != CurPtr; ++OldPtr)
19645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
19655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Okay, we found a // comment that ends in a newline, if the next
19665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // line is also a // comment, but has spaces, don't emit a diagnostic.
19675d6ae288bc661572e52ea71fc14b249eb5e2d9e9Benjamin Kramer          if (isWhitespace(C)) {
19685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            const char *ForwardPtr = CurPtr;
19695d6ae288bc661572e52ea71fc14b249eb5e2d9e9Benjamin Kramer            while (isWhitespace(*ForwardPtr))  // Skip whitespace.
19705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              ++ForwardPtr;
19715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
19725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              break;
19735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
19741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
197574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          if (!isLexingRawMode())
197674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner            Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
19775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          break;
19785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
19795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
19801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
198155817afdf9d453a443262a733f6caf6692dca118Douglas Gregor    if (CurPtr == BufferEnd+1) {
198255817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      --CurPtr;
198355817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      break;
198455817afdf9d453a443262a733f6caf6692dca118Douglas Gregor    }
19857d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
19867d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) {
19877d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      PP->CodeCompleteNaturalLanguage();
19887d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffLexing();
19897d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return false;
19907d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    }
19917d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
19925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } while (C != '\n' && C != '\r');
19935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19943d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  // Found but did not consume the newline.  Notify comment handlers about the
19953d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  // comment unless we're in a #if 0 block.
19963d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  if (PP && !isLexingRawMode() &&
19973d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner      PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
19983d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner                                            getSourceLocation(CurPtr)))) {
1999046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    BufferPtr = CurPtr;
2000046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    return true; // A token has to be returned.
2001046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  }
20021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are returning comments as tokens, return this comment as a token.
2004fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner  if (inKeepCommentMode())
20055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return SaveBCPLComment(Result, CurPtr);
20065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are inside a preprocessor directive and we see the end of line,
200884021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  // return immediately, so that the lexer can return this as an EOD token.
20095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
20105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    BufferPtr = CurPtr;
20112d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
20125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
20131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, eat the \n character.  We don't care if this is a \n\r or
20157a4f004a77421af876152281b92c4f8ea24afb2dChris Lattner  // \r\n sequence.  This is an efficiency hack (because we know the \n can't
2016d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // contribute to another token), it isn't needed for correctness.  Note that
2017d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // this is ok even in KeepWhitespaceMode, because we would have returned the
2018d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// comment above in that mode.
20195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++CurPtr;
20201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // The next returned token is at the start of the line.
2022d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.setFlag(Token::StartOfLine);
20235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // No leading whitespace seen so far.
2024d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.clearFlag(Token::LeadingSpace);
20255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
20262d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return false;
20275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
20305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// an appropriate way and return it.
2031d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
20329e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // If we're not in a preprocessor directive, just return the // comment
20339e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // directly.
20349e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::comment);
20351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20368c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  if (!ParsingPreprocessorDirective || LexingRawMode)
20379e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    return true;
20381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20399e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // If this BCPL-style comment is in a macro definition, transmogrify it into
20409e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // a C-style block comment.
2041453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor  bool Invalid = false;
2042453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor  std::string Spelling = PP->getSpelling(Result, &Invalid);
2043453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor  if (Invalid)
2044453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor    return true;
2045453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor
20469e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
20479e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Spelling[1] = '*';   // Change prefix to "/*".
20489e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Spelling += "*/";    // add suffix.
20491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20509e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Result.setKind(tok::comment);
205147246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  PP->CreateString(&Spelling[0], Spelling.size(), Result,
2052a08529cc3f00e0b47a3c028823634129ac46847bAbramo Bagnara                   Result.getLocation(), Result.getLocation());
20532d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return true;
20545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
205780d7c52653800d3338cca2c4388348010d2b1921David Blaikie/// character (either \\n or \\r) is part of an escaped newline sequence.  Issue
205880d7c52653800d3338cca2c4388348010d2b1921David Blaikie/// a diagnostic if so.  We know that the newline is inside of a block comment.
20591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
20605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                                  Lexer *L) {
20615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
20621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Back up off the newline.
20645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  --CurPtr;
20651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a two-character newline sequence, skip the other character.
20675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
20685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n\n or \r\r -> not escaped newline.
20695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[0] == CurPtr[1])
20705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
20715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n\r or \r\n -> skip the newline.
20725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    --CurPtr;
20735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
20741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have horizontal whitespace, skip over it.  We allow whitespace
20765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // between the slash and newline.
20775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool HasSpace = false;
20785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
20795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    --CurPtr;
20805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    HasSpace = true;
20815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
20821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, we know this is an escaped newline.
20845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (*CurPtr == '\\') {
20855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[-1] != '*') return false;
20865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
20875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // It isn't a slash, is it the ?? / trigraph?
20885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
20895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr[-3] != '*')
20905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
20911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // This is the trigraph ending the comment.  Emit a stern warning!
20935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr -= 2;
20945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If no trigraphs are enabled, warn that we ignored this trigraph and
20965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // ignore this * character.
20974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!L->getLangOpts().Trigraphs) {
209874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!L->isLexingRawMode())
209974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
21005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
21015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
210274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!L->isLexingRawMode())
210374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      L->Diag(CurPtr, diag::trigraph_ends_block_comment);
21045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
21051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Warn about having an escaped newline between the */ characters.
210774d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (!L->isLexingRawMode())
210874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
21091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If there was space between the backslash and newline, warn about it.
211174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (HasSpace && !L->isLexingRawMode())
211274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    L->Diag(CurPtr, diag::backslash_newline_space);
21131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return true;
21155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__
21185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <emmintrin.h>
21195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__
21205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <altivec.h>
21215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#undef bool
21225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
21235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2124ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// We have just read from input the / and * characters that started a comment.
2125ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// Read until we find the * and / characters that terminate the comment.
2126ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// Note that we don't bother decoding trigraphs or escaped newlines in block
2127ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// comments, because they cannot cause the comment to end.  The only thing
2128ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// that can happen is the comment could end with an escaped newline between
2129ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// the terminating * and /.
21302d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner///
2131046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted
2132046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true.
2133d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
21345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Scan one character past where we should, looking for a '/' character.  Once
2135fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner  // we find it, check to see if it was preceded by a *.  This common
21365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // optimization helps people who like to put a lot of * characters in their
21375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // comments.
21388146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner
21398146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // The first character we get with newlines and trigraphs skipped to handle
21408146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // the degenerate /*/ case below correctly if the * has an escaped newline
21418146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // after it.
21428146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  unsigned CharSize;
21438146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  unsigned char C = getCharAndSize(CurPtr, CharSize);
21448146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  CurPtr += CharSize;
21455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (C == 0 && CurPtr == BufferEnd+1) {
21467d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    if (!isLexingRawMode())
21470af574270d3be2b0e73a3379dfaa633746f8fc6fChris Lattner      Diag(BufferPtr, diag::err_unterminated_block_comment);
214831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    --CurPtr;
21491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
215031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    // KeepWhitespaceMode should return this broken comment as a token.  Since
215131f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    // it isn't a well formed comment, just return it as an 'unknown' token.
215231f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    if (isKeepWhitespaceMode()) {
21539e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr, tok::unknown);
215431f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      return true;
215531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    }
21561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
215731f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    BufferPtr = CurPtr;
21582d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
21595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
21601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21618146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // Check to see if the first character after the '/*' is another /.  If so,
21628146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // then this slash does not end the block comment, it is part of it.
21638146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  if (C == '/')
21648146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner    C = *CurPtr++;
21651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
21675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over all non-interesting characters until we find end of buffer or a
21685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // (probably ending) '/' character.
21697d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    if (CurPtr + 24 < BufferEnd &&
21707d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        // If there is a code-completion point avoid the fast scan because it
21717d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        // doesn't check for '\0'.
21727d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        !(PP && PP->getCodeCompletionFileLoc() == FileLoc)) {
21735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // While not aligned to a 16-byte boundary.
21745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
21755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        C = *CurPtr++;
21761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '/') goto FoundSlash;
21785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__
21803f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer      __m128i Slashes = _mm_set1_epi8('/');
21813f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer      while (CurPtr+16 <= BufferEnd) {
21823f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer        int cmp = _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes));
21833f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer        if (cmp != 0) {
21846300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer          // Adjust the pointer to point directly after the first slash. It's
21856300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer          // not necessary to set C here, it will be overwritten at the end of
21866300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer          // the outer loop.
21876300f5b4382d5128c9346a4d4f05e7bac2e3d771Benjamin Kramer          CurPtr += llvm::CountTrailingZeros_32(cmp) + 1;
21883f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer          goto FoundSlash;
21893f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer        }
21905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 16;
21913f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer      }
21925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__
21935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      __vector unsigned char Slashes = {
21941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        '/', '/', '/', '/',  '/', '/', '/', '/',
21955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        '/', '/', '/', '/',  '/', '/', '/', '/'
21965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      };
21975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (CurPtr+16 <= BufferEnd &&
21985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
21995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 16;
22001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#else
22015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Scan for '/' quickly.  Many block comments are very large.
22025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (CurPtr[0] != '/' &&
22035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[1] != '/' &&
22045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[2] != '/' &&
22055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[3] != '/' &&
22065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr+4 < BufferEnd) {
22075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 4;
22085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
22095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
22101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // It has to be one of the bytes scanned, increment to it and read one.
22125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *CurPtr++;
22135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
22141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Loop to scan the remainder.
22165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (C != '/' && C != '\0')
22175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *CurPtr++;
22181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '/') {
22203f6f4e65e7806fd49137cf3cf59a1432ed776b2aBenjamin Kramer  FoundSlash:
22215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr[-2] == '*')  // We found the final */.  We're done!
22225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
22231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
22255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
22265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // We found the final */, though it had an escaped newline between the
22275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // * and /.  We're done!
22285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          break;
22295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
22305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
22315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr[0] == '*' && CurPtr[1] != '/') {
22325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // If this is a /* inside of the comment, emit a warning.  Don't do this
22335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // if this is a /*/, which will end the comment.  This misses cases with
22345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // embedded escaped newlines, but oh well.
223574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        if (!isLexingRawMode())
223674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          Diag(CurPtr-1, diag::warn_nested_block_comment);
22375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
22385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == 0 && CurPtr == BufferEnd+1) {
22397d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      if (!isLexingRawMode())
224074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(BufferPtr, diag::err_unterminated_block_comment);
22415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Note: the user probably forgot a */.  We could continue immediately
22425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // after the /*, but this would involve lexing a lot of what really is the
22435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // comment, which surely would confuse the parser.
224431f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      --CurPtr;
22451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
224631f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      // KeepWhitespaceMode should return this broken comment as a token.  Since
224731f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      // it isn't a well formed comment, just return it as an 'unknown' token.
224831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      if (isKeepWhitespaceMode()) {
22499e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        FormTokenWithChars(Result, CurPtr, tok::unknown);
225031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner        return true;
225131f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      }
22521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
225331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      BufferPtr = CurPtr;
22542d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner      return false;
22557d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    } else if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) {
22567d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      PP->CodeCompleteNaturalLanguage();
22577d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffLexing();
22587d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return false;
22595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
22607d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
22615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr++;
22625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
22631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22643d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  // Notify comment handlers about the comment unless we're in a #if 0 block.
22653d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  if (PP && !isLexingRawMode() &&
22663d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner      PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
22673d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner                                            getSourceLocation(CurPtr)))) {
2268046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    BufferPtr = CurPtr;
2269046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    return true; // A token has to be returned.
2270046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  }
22712e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
22725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are returning comments as tokens, return this comment as a token.
2273fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner  if (inKeepCommentMode()) {
22749e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::comment);
22752d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return true;
22765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
22775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // It is common for the tokens immediately after a /**/ comment to be
22795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // whitespace.  Instead of going through the big switch, handle it
2280d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // efficiently now.  This is safe even in KeepWhitespaceMode because we would
2281d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // have already returned above with the comment as a token.
22825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (isHorizontalWhitespace(*CurPtr)) {
2283d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
22845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipWhitespace(Result, CurPtr+1);
22852d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
22865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
22875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, just return so that the next character will be lexed as a token.
22895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
2290d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.setFlag(Token::LeadingSpace);
22912d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return false;
22925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
22955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Lexing Entry Points
22965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
22975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
22995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// uninterpreted string.  This switches the lexer out of directive mode.
23003093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramervoid Lexer::ReadToEndOfLine(SmallVectorImpl<char> *Result) {
23015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParsingPreprocessorDirective && ParsingFilename == false &&
23025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer         "Must be in a preprocessing directive!");
2303d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Token Tmp;
23045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // CurPtr - Cache BufferPtr in an automatic variable.
23065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CurPtr = BufferPtr;
23075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
23085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    char Char = getAndAdvanceChar(CurPtr, Tmp);
23095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Char) {
23105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
23113093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer      if (Result)
23123093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer        Result->push_back(Char);
23135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
23145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case 0:  // Null.
23155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Found end of file?
23165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr-1 != BufferEnd) {
23177d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        if (isCodeCompletionPoint(CurPtr-1)) {
23187d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis          PP->CodeCompleteNaturalLanguage();
23197d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis          cutOffLexing();
23203093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer          return;
23217d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        }
23227d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
23235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Nope, normal character, continue.
23243093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer        if (Result)
23253093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer          Result->push_back(Char);
23265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
23275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
23285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FALL THROUGH.
23295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case '\r':
23305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case '\n':
23315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Okay, we found the end of the line. First, back up past the \0, \r, \n.
23325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(CurPtr[-1] == Char && "Trigraphs for newline?");
23335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      BufferPtr = CurPtr-1;
23341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
233584021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne      // Next, lex the character, which should handle the EOD transition.
23365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Lex(Tmp);
233755817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      if (Tmp.is(tok::code_completion)) {
23387d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        if (PP)
23397d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis          PP->CodeCompleteNaturalLanguage();
234055817afdf9d453a443262a733f6caf6692dca118Douglas Gregor        Lex(Tmp);
234155817afdf9d453a443262a733f6caf6692dca118Douglas Gregor      }
234284021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne      assert(Tmp.is(tok::eod) && "Unexpected token!");
23431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23443093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer      // Finally, we're done;
23453093b20d824a953d8bc7a786dd952414898f8d6dBenjamin Kramer      return;
23465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
23475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
23485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
23495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexEndOfFile - CurPtr points to the end of this file.  Handle this
23515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// condition, reporting diagnostics and handling other edge cases as required.
23525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This returns true if Result contains a token, false if PP.Lex should be
23535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// called again.
2354d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
23555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we hit the end of the file while parsing a preprocessor directive,
23565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // end the preprocessor directive first.  The next token returned will
23575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // then be the end of file.
23585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ParsingPreprocessorDirective) {
23595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Done parsing the "line".
23605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParsingPreprocessorDirective = false;
23615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Update the location of token as well as BufferPtr.
236284021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne    FormTokenWithChars(Result, CurPtr, tok::eod);
23631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Restore comment saving mode, in case it was disabled for directive.
2365f744d133e8d36eec65828d046ee695bda238125cChris Lattner    SetCommentRetentionState(PP->getCommentRetentionState());
23665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return true;  // Have a token.
23671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
236886d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor
236986d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  // If we are in raw mode, return this event as an EOF token.  Let the caller
237086d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  // that put us in raw mode handle the event.
237186d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  if (isLexingRawMode()) {
237286d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    Result.startToken();
237386d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    BufferPtr = BufferEnd;
237486d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    FormTokenWithChars(Result, BufferEnd, tok::eof);
237586d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    return true;
237686d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  }
237781b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
2378f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  // Issue diagnostics for unterminated #if and missing newline.
2379f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor
23805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are in a #if directive, emit an error.
23815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (!ConditionalStack.empty()) {
23827d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    if (PP->getCodeCompletionFileLoc() != FileLoc)
23832d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor      PP->Diag(ConditionalStack.back().IfLoc,
23842d474ba9e8ae43a1a5a9f72718c0d79092b9453fDouglas Gregor               diag::err_pp_unterminated_conditional);
23855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConditionalStack.pop_back();
23865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
23871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2388b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner  // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
2389b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner  // a pedwarn.
23905e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell  if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
23915e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell    Diag(BufferEnd, LangOpts.CPlusPlus0x ? // C++11 [lex.phases] 2.2 p2
23925e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell         diag::warn_cxx98_compat_no_newline_eof : diag::ext_no_newline_eof)
23935e6c3f0397254e2a1a9ef85cf13232f89e653640Seth Cantrell    << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n");
23941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
23965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Finally, let the preprocessor handle this.
23980cdd1fe3ec29b5cbff9a728966ace5c5b5d614f7Jordan Rose  return PP->HandleEndOfFile(Result, isPragmaLexer());
23995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
24005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
24025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified lexer will return a tok::l_paren token, 0 if it is something
24035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// else and 2 if there are no more tokens in the buffer controlled by the
24045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexer.
24055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerunsigned Lexer::isNextPPTokenLParen() {
24065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
24071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Switch to 'skipping' mode.  This will ensure that we can lex a token
24095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // without emitting diagnostics, disables macro expansion, and will cause EOF
24105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // to return an EOF token instead of popping the include stack.
24115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexingRawMode = true;
24121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Save state that can be changed while lexing so that we can restore it.
24145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *TmpBufferPtr = BufferPtr;
2415a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner  bool inPPDirectiveMode = ParsingPreprocessorDirective;
24161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2417d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Token Tok;
24185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Tok.startToken();
24195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexTokenInternal(Tok);
24201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Restore state that may have changed.
24225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = TmpBufferPtr;
2423a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner  ParsingPreprocessorDirective = inPPDirectiveMode;
24241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Restore the lexer back to non-skipping mode.
24265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexingRawMode = false;
24271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
242822f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner  if (Tok.is(tok::eof))
24295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return 2;
243022f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner  return Tok.is(tok::l_paren);
24315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
24325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2433ec7699398bad018e8ea8134ef760a259598fefc1James Dennett/// \brief Find the end of a version control conflict marker.
2434d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smithstatic const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd,
2435d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith                                   ConflictMarkerKind CMK) {
2436d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  const char *Terminator = CMK == CMK_Perforce ? "<<<<\n" : ">>>>>>>";
2437d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  size_t TermLen = CMK == CMK_Perforce ? 5 : 7;
2438d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  StringRef RestOfBuffer(CurPtr+TermLen, BufferEnd-CurPtr-TermLen);
2439d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  size_t Pos = RestOfBuffer.find(Terminator);
24405f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  while (Pos != StringRef::npos) {
244134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Must occur at start of line.
244234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    if (RestOfBuffer[Pos-1] != '\r' &&
244334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        RestOfBuffer[Pos-1] != '\n') {
2444d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith      RestOfBuffer = RestOfBuffer.substr(Pos+TermLen);
2445d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith      Pos = RestOfBuffer.find(Terminator);
244634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      continue;
244734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    }
244834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return RestOfBuffer.data()+Pos;
244934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  }
245034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  return 0;
245134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner}
245234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
245334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// IsStartOfConflictMarker - If the specified pointer is the start of a version
245434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// control conflict marker like '<<<<<<<', recognize it as such, emit an error
245534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// and recover nicely.  This returns true if it is a conflict marker and false
245634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// if not.
245734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
245834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // Only a conflict marker if it starts at the beginning of a line.
245934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (CurPtr != BufferStart &&
246034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
246134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
246234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
2463d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  // Check to see if we have <<<<<<< or >>>>.
2464d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  if ((BufferEnd-CurPtr < 8 || StringRef(CurPtr, 7) != "<<<<<<<") &&
2465d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith      (BufferEnd-CurPtr < 6 || StringRef(CurPtr, 5) != ">>>> "))
246634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
246734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
246834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // If we have a situation where we don't care about conflict markers, ignore
246934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // it.
2470d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  if (CurrentConflictMarkerState || isLexingRawMode())
247134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
247234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
2473d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  ConflictMarkerKind Kind = *CurPtr == '<' ? CMK_Normal : CMK_Perforce;
2474d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith
2475d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  // Check to see if there is an ending marker somewhere in the buffer at the
2476d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  // start of a line to terminate this conflict marker.
2477d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  if (FindConflictEnd(CurPtr, BufferEnd, Kind)) {
247834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // We found a match.  We are really in a conflict marker.
247934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Diagnose this, and ignore to the end of line.
248034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    Diag(CurPtr, diag::err_conflict_marker);
2481d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith    CurrentConflictMarkerState = Kind;
248234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
248334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Skip ahead to the end of line.  We know this exists because the
248434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // end-of-conflict marker starts with \r or \n.
248534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    while (*CurPtr != '\r' && *CurPtr != '\n') {
248634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      assert(CurPtr != BufferEnd && "Didn't find end of line");
248734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      ++CurPtr;
248834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    }
248934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    BufferPtr = CurPtr;
249034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return true;
249134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  }
249234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
249334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // No end of conflict marker found.
249434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  return false;
249534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner}
249634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
249734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
2498d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// HandleEndOfConflictMarker - If this is a '====' or '||||' or '>>>>', or if
2499d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// it is '<<<<' and the conflict marker started with a '>>>>' marker, then it
2500d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// is the end of a conflict marker.  Handle it by ignoring up until the end of
2501d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// the line.  This returns true if it is a conflict marker and false if not.
250234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
250334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // Only a conflict marker if it starts at the beginning of a line.
250434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (CurPtr != BufferStart &&
250534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
250634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
250734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
250834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // If we have a situation where we don't care about conflict markers, ignore
250934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // it.
2510d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  if (!CurrentConflictMarkerState || isLexingRawMode())
251134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
251234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
2513d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  // Check to see if we have the marker (4 characters in a row).
2514d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  for (unsigned i = 1; i != 4; ++i)
251534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    if (CurPtr[i] != CurPtr[0])
251634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      return false;
251734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
251834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // If we do have it, search for the end of the conflict marker.  This could
251934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // fail if it got skipped with a '#if 0' or something.  Note that CurPtr might
252034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // be the end of conflict marker.
2521d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  if (const char *End = FindConflictEnd(CurPtr, BufferEnd,
2522d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith                                        CurrentConflictMarkerState)) {
252334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    CurPtr = End;
252434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
252534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Skip ahead to the end of line.
252634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
252734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      ++CurPtr;
252834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
252934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    BufferPtr = CurPtr;
253034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
253134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // No longer in the conflict marker.
2532d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith    CurrentConflictMarkerState = CMK_None;
253334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return true;
253434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  }
253534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
253634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  return false;
253734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner}
253834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
25397d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidisbool Lexer::isCodeCompletionPoint(const char *CurPtr) const {
25407d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  if (PP && PP->isCodeCompletionEnabled()) {
2541a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis    SourceLocation Loc = FileLoc.getLocWithOffset(CurPtr-BufferStart);
25427d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return Loc == PP->getCodeCompletionLoc();
25437d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  }
25447d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
25457d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  return false;
25467d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis}
25477d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
25485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
25495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexTokenInternal - This implements a simple C family lexer.  It is an
25505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// extremely performance critical piece of code.  This assumes that the buffer
2551efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// has a null character at the end of the file.  This returns a preprocessing
2552efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// token, not a normal token, as such, it is an internal interface.  It assumes
2553efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// that the Flags of result have been cleared before calling this.
2554d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexTokenInternal(Token &Result) {
25555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerLexNextToken:
25565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // New token, can't need cleaning yet.
2557d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.clearFlag(Token::NeedsCleaning);
25585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Result.setIdentifierInfo(0);
25591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // CurPtr - Cache BufferPtr in an automatic variable.
25615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CurPtr = BufferPtr;
25625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
25635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Small amounts of horizontal whitespace is very common between tokens.
25645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
25655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++CurPtr;
25665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while ((*CurPtr == ' ') || (*CurPtr == '\t'))
25675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++CurPtr;
25681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2569d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // If we are keeping whitespace and other tokens, just return what we just
2570d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // skipped.  The next lexer invocation will return the token after the
2571d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // whitespace.
2572d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (isKeepWhitespaceMode()) {
25739e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr, tok::unknown);
2574d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return;
2575d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    }
25761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    BufferPtr = CurPtr;
2578d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
25795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
25801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned SizeTmp, SizeTmp2;   // Temporaries for use in cases below.
25821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Read a character, advancing over it.
25845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char Char = getAndAdvanceChar(CurPtr, Result);
25859e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  tok::TokenKind Kind;
25861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Char) {
25885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 0:  // Null.
25895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Found end of file?
25905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr-1 == BufferEnd) {
25915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Read the PP instance variable into an automatic variable, because
25925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // LexEndOfFile will often delete 'this'.
2593168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      Preprocessor *PPCache = PP;
25945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (LexEndOfFile(Result, CurPtr-1))  // Retreat back into the file.
25955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return;   // Got a token to return.
2596168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
2597168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      return PPCache->Lex(Result);
25985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
25991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26007d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    // Check if we are performing code completion.
26017d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    if (isCodeCompletionPoint(CurPtr-1)) {
26027d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      // Return the code-completion token.
26037d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      Result.startToken();
26047d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      FormTokenWithChars(Result, CurPtr, tok::code_completion);
26057d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return;
26067d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    }
26077d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
260874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!isLexingRawMode())
260974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      Diag(CurPtr-1, diag::null_in_file);
2610d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
2611d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
2612d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
26131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
2615a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner
2616a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner  case 26:  // DOS & CP/M EOF: "^Z".
2617a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    // If we're in Microsoft extensions mode, treat this as end of file.
26184e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (LangOpts.MicrosoftExt) {
2619a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      // Read the PP instance variable into an automatic variable, because
2620a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      // LexEndOfFile will often delete 'this'.
2621a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      Preprocessor *PPCache = PP;
2622a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      if (LexEndOfFile(Result, CurPtr-1))  // Retreat back into the file.
2623a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner        return;   // Got a token to return.
2624a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
2625a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      return PPCache->Lex(Result);
2626a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    }
2627a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    // If Microsoft extensions are disabled, this is just random garbage.
2628a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    Kind = tok::unknown;
2629a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    break;
2630a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner
26315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\n':
26325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\r':
26335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we are inside a preprocessor directive and we see the end of line,
263484021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne    // we know we are done with the directive, so return an EOD token.
26355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingPreprocessorDirective) {
26365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Done parsing the "line".
26375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParsingPreprocessorDirective = false;
26381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Restore comment saving mode, in case it was disabled for directive.
26401a8354659a6007bbae3b5d9161a56ecc8f61a219David Blaikie      if (PP)
26418c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie        SetCommentRetentionState(PP->getCommentRetentionState());
26421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Since we consumed a newline, we are back at the start of a line.
26445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      IsAtStartOfLine = true;
26451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
264684021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne      Kind = tok::eod;
26475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
26485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
26495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // The returned token is at the start of the line.
2650d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::StartOfLine);
26515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // No leading whitespace seen so far.
2652d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.clearFlag(Token::LeadingSpace);
26531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2654d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
2655d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
26565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
26575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ' ':
26585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\t':
26595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\f':
26605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\v':
26618133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner  SkipHorizontalWhitespace:
2662d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
2663d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
2664d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
26658133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner
26668133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner  SkipIgnoredUnits:
26678133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    CurPtr = BufferPtr;
26681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26698133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    // If the next token is obviously a // or /* */ comment, skip it efficiently
26708133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    // too (without going through the big switch stmt).
26718402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
26724e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        LangOpts.BCPLComment && !LangOpts.TraditionalCPP) {
2673046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner      if (SkipBCPLComment(Result, CurPtr+2))
2674046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner        return; // There is a token to return.
26758133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipIgnoredUnits;
2676fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner    } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
2677046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner      if (SkipBlockComment(Result, CurPtr+2))
2678046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner        return; // There is a token to return.
26798133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipIgnoredUnits;
26808133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    } else if (isHorizontalWhitespace(*CurPtr)) {
26818133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipHorizontalWhitespace;
26828133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    }
26835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
2684a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner
26853a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  // C99 6.4.4.1: Integer Constants.
26863a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  // C99 6.4.4.2: Floating Constants.
26873a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '0': case '1': case '2': case '3': case '4':
26883a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '5': case '6': case '7': case '8': case '9':
26893a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    // Notify MIOpt that we read a non-whitespace/non-comment token.
26903a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    MIOpt.ReadToken();
26913a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    return LexNumericConstant(Result, CurPtr);
26921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26935cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  case 'u':   // Identifier (uber) or C++0x UTF-8 or UTF-16 string literal
26945cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    // Notify MIOpt that we read a non-whitespace/non-comment token.
26955cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    MIOpt.ReadToken();
26965cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
26974e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (LangOpts.CPlusPlus0x) {
26985cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      Char = getCharAndSize(CurPtr, SizeTmp);
26995cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27005cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      // UTF-16 string literal
27015cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      if (Char == '"')
27025cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor        return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
27035cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                                tok::utf16_string_literal);
27045cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27055cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      // UTF-16 character constant
27065cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      if (Char == '\'')
27075cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor        return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result),
27085cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                               tok::utf16_char_constant);
27095cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27102fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      // UTF-16 raw string literal
27112fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (Char == 'R' && getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"')
27122fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        return LexRawStringLiteral(Result,
27132fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
27142fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                           SizeTmp2, Result),
27152fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               tok::utf16_string_literal);
27162fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27172fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (Char == '8') {
27182fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        char Char2 = getCharAndSize(CurPtr + SizeTmp, SizeTmp2);
27192fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27202fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        // UTF-8 string literal
27212fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        if (Char2 == '"')
27222fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          return LexStringLiteral(Result,
27232fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
27242fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                           SizeTmp2, Result),
27252fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               tok::utf8_string_literal);
27262fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27272fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        if (Char2 == 'R') {
27282fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          unsigned SizeTmp3;
27292fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          char Char3 = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3);
27302fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          // UTF-8 raw string literal
27312fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          if (Char3 == '"') {
27322fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper            return LexRawStringLiteral(Result,
27332fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                   ConsumeChar(ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
27342fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                           SizeTmp2, Result),
27352fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               SizeTmp3, Result),
27362fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                   tok::utf8_string_literal);
27372fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper          }
27382fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        }
27392fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      }
27405cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    }
27415cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27425cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    // treat u like the start of an identifier.
27435cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return LexIdentifier(Result, CurPtr);
27445cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27455cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  case 'U':   // Identifier (Uber) or C++0x UTF-32 string literal
27465cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    // Notify MIOpt that we read a non-whitespace/non-comment token.
27475cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    MIOpt.ReadToken();
27485cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27494e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (LangOpts.CPlusPlus0x) {
27505cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      Char = getCharAndSize(CurPtr, SizeTmp);
27515cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27525cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      // UTF-32 string literal
27535cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      if (Char == '"')
27545cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor        return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
27555cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                                tok::utf32_string_literal);
27565cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27575cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      // UTF-32 character constant
27585cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      if (Char == '\'')
27595cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor        return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result),
27605cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                               tok::utf32_char_constant);
27612fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27622fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      // UTF-32 raw string literal
27632fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (Char == 'R' && getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"')
27642fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        return LexRawStringLiteral(Result,
27652fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
27662fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                           SizeTmp2, Result),
27672fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               tok::utf32_string_literal);
27685cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    }
27695cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27705cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    // treat U like the start of an identifier.
27715cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return LexIdentifier(Result, CurPtr);
27725cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
27732fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  case 'R': // Identifier or C++0x raw string literal
27742fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    // Notify MIOpt that we read a non-whitespace/non-comment token.
27752fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    MIOpt.ReadToken();
27762fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27774e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (LangOpts.CPlusPlus0x) {
27782fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      Char = getCharAndSize(CurPtr, SizeTmp);
27792fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27802fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      if (Char == '"')
27812fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        return LexRawStringLiteral(Result,
27822fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                   ConsumeChar(CurPtr, SizeTmp, Result),
27832fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                   tok::string_literal);
27842fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    }
27852fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27862fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    // treat R like the start of an identifier.
27872fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    return LexIdentifier(Result, CurPtr);
27882fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
27893a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case 'L':   // Identifier (Loony) or wide literal (L'x' or L"xyz").
27905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
27915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
27925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
27935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
27945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Wide string literal.
27955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '"')
27965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
27975cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                              tok::wide_string_literal);
27985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
27992fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper    // Wide raw string literal.
28004e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (LangOpts.CPlusPlus0x && Char == 'R' &&
28012fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper        getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"')
28022fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper      return LexRawStringLiteral(Result,
28032fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
28042fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                                           SizeTmp2, Result),
28052fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                               tok::wide_string_literal);
28062fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper
28075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Wide character constant.
28085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '\'')
28095cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result),
28105cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                             tok::wide_char_constant);
28115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FALL THROUGH, treating L like the start of an identifier.
28121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.2: Identifiers.
28145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
28155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'H': case 'I': case 'J': case 'K':    /*'L'*/case 'M': case 'N':
28162fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper  case 'O': case 'P': case 'Q':    /*'R'*/case 'S': case 'T':    /*'U'*/
28175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'V': case 'W': case 'X': case 'Y': case 'Z':
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': case 'l': case 'm': case 'n':
28205cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  case 'o': case 'p': case 'q': case 'r': case 's': case 't':    /*'u'*/
28215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'v': case 'w': case 'x': case 'y': case 'z':
28225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '_':
28235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
28245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
28255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexIdentifier(Result, CurPtr);
28263a5707766850f9ee9daa35299794328b5caf96dcChris Lattner
28273a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '$':   // $ in identifiers.
28284e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (LangOpts.DollarIdents) {
282974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
283074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(CurPtr-1, diag::ext_dollar_in_identifier);
28313a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      // Notify MIOpt that we read a non-whitespace/non-comment token.
28323a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      MIOpt.ReadToken();
28333a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      return LexIdentifier(Result, CurPtr);
28343a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    }
28351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28369e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::unknown;
28373a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    break;
28381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.4: Character Constants.
28405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\'':
28415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
28425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
28435cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return LexCharConstant(Result, CurPtr, tok::char_constant);
28445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
28455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.5: String Literals.
28465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '"':
28475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
28485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
28495cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return LexStringLiteral(Result, CurPtr, tok::string_literal);
28505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
28515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.6: Punctuators.
28525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '?':
28539e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::question;
28545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '[':
28569e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_square;
28575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ']':
28599e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_square;
28605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '(':
28629e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_paren;
28635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ')':
28659e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_paren;
28665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '{':
28689e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_brace;
28695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '}':
28719e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_brace;
28725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '.':
28745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
28755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char >= '0' && Char <= '9') {
28765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Notify MIOpt that we read a non-whitespace/non-comment token.
28775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      MIOpt.ReadToken();
28785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
28795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
28804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (LangOpts.CPlusPlus && Char == '*') {
28819e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::periodstar;
28825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr += SizeTmp;
28835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '.' &&
28845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer               getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
28859e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ellipsis;
28865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
28875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                           SizeTmp2, Result);
28885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
28899e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::period;
28905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
28915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
28925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '&':
28935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
28945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '&') {
28959e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ampamp;
28965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
28975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
28989e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ampequal;
28995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
29019e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::amp;
29025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
29035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
29041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case '*':
29055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (getCharAndSize(CurPtr, SizeTmp) == '=') {
29069e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::starequal;
29075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
29099e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::star;
29105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
29115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
29125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '+':
29135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
29145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '+') {
29155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29169e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plusplus;
29175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
29185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29199e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plusequal;
29205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
29219e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plus;
29225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
29235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
29245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '-':
29255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
29269e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    if (Char == '-') {      // --
29275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29289e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minusminus;
29294e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (Char == '>' && LangOpts.CPlusPlus &&
29309e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner               getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {  // C++ ->*
29315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
29325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                           SizeTmp2, Result);
29339e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::arrowstar;
29349e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Char == '>') {   // ->
29355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29369e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::arrow;
29379e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Char == '=') {   // -=
29385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29399e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minusequal;
29405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
29419e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minus;
29425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
29435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
29445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '~':
29459e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::tilde;
29465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
29475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '!':
29485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (getCharAndSize(CurPtr, SizeTmp) == '=') {
29499e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::exclaimequal;
29505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
29529e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::exclaim;
29535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
29545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
29555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '/':
29565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // 6.4.9: Comments
29575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
29585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '/') {         // BCPL comment.
29598402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
29608402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // want to lex this as a comment.  There is one problem with this though,
29618402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // that in one particular corner case, this can change the behavior of the
29628402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // resultant program.  For example, In  "foo //**/ bar", C89 would lex
29638402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // this as "foo / bar" and langauges with BCPL comments would lex it as
29648402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // "foo".  Check to see if the character after the second slash is a '*'.
29658402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // If so, we will lex that as a "/" instead of the start of a comment.
29662ed42287b9b88a0bb08ab10e95e306da3f294ba9Daniel Dunbar      // However, we never do this in -traditional-cpp mode.
29674e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if ((LangOpts.BCPLComment ||
29682ed42287b9b88a0bb08ab10e95e306da3f294ba9Daniel Dunbar           getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') &&
29694e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          !LangOpts.TraditionalCPP) {
29708402c73dd880e8af46c826d873681820aebe32ebChris Lattner        if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
2971046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner          return; // There is a token to return.
29721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29738402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // It is common for the tokens immediately after a // comment to be
29748402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // whitespace (indentation for the next line).  Instead of going through
29758402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // the big switch, handle it efficiently now.
29768402c73dd880e8af46c826d873681820aebe32ebChris Lattner        goto SkipIgnoredUnits;
29778402c73dd880e8af46c826d873681820aebe32ebChris Lattner      }
29788402c73dd880e8af46c826d873681820aebe32ebChris Lattner    }
29791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29808402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (Char == '*') {  // /**/ comment.
29815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
2982046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner        return; // There is a token to return.
29832d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner      goto LexNextToken;   // GCC isn't tail call eliminating.
29848402c73dd880e8af46c826d873681820aebe32ebChris Lattner    }
29851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29868402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (Char == '=') {
29875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29889e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::slashequal;
29895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
29909e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::slash;
29915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
29925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
29935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '%':
29945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
29955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
29969e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::percentequal;
29975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
29984e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (LangOpts.Digraphs && Char == '>') {
29999e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::r_brace;                             // '%>' -> '}'
30005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
30014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (LangOpts.Digraphs && Char == ':') {
30025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
30035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Char = getCharAndSize(CurPtr, SizeTmp);
30045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
30059e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        Kind = tok::hashhash;                          // '%:%:' -> '##'
30065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
30075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                             SizeTmp2, Result);
30084e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      } else if (Char == '@' && LangOpts.MicrosoftExt) {// %:@ -> #@ -> Charize
30095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
301074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        if (!isLexingRawMode())
301166d5ce11b9426f6a59f61a03cbd8dbf047cc9350Ted Kremenek          Diag(BufferPtr, diag::ext_charize_microsoft);
30129e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        Kind = tok::hashat;
3013e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner      } else {                                         // '%:' -> '#'
30145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // We parsed a # character.  If this occurs at the start of the line,
30155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // it's actually the start of a preprocessing directive.  Callback to
30165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // the preprocessor to handle it.
30175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // FIXME: -fpreprocessed mode??
3018766703b76dc1eb0c49d11cafc2a17c7407876ebcChris Lattner        if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
3019e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner          FormTokenWithChars(Result, CurPtr, tok::hash);
3020168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner          PP->HandleDirective(Result);
30211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // As an optimization, if the preprocessor didn't switch lexers, tail
30235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // recurse.
3024168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner          if (PP->isCurrentLexer(this)) {
30255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // Start a new token. If this is a #include or something, the PP may
30265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // want us starting at the beginning of the line again.  If so, set
3027515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner            // the StartOfLine flag and clear LeadingSpace.
30285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (IsAtStartOfLine) {
3029d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner              Result.setFlag(Token::StartOfLine);
3030515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner              Result.clearFlag(Token::LeadingSpace);
30315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              IsAtStartOfLine = false;
30325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            }
30335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            goto LexNextToken;   // GCC isn't tail call eliminating.
30345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
30351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3036168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner          return PP->Lex(Result);
30375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
30381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3039e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner        Kind = tok::hash;
30405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
30415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
30429e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::percent;
30435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
30445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
30455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '<':
30465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
30475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingFilename) {
30489cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      return LexAngledStringLiteral(Result, CurPtr);
30495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '<') {
305034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
305134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (After == '=') {
305234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::lesslessequal;
305334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
305434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner                             SizeTmp2, Result);
305534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
305634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        // If this is actually a '<<<<<<<' version control conflict marker,
305734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        // recognize it as such and recover nicely.
305834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
3059d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith      } else if (After == '<' && HandleEndOfConflictMarker(CurPtr-1)) {
3060d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith        // If this is '<<<<' and we're in a Perforce-style conflict marker,
3061d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith        // ignore it.
3062d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith        goto LexNextToken;
30634e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      } else if (LangOpts.CUDA && After == '<') {
30641b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne        Kind = tok::lesslessless;
30651b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
30661b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne                             SizeTmp2, Result);
306734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else {
306834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
306934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::lessless;
307034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      }
30715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
30725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
30739e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::lessequal;
30744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (LangOpts.Digraphs && Char == ':') {     // '<:' -> '['
30754e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (LangOpts.CPlusPlus0x &&
307687a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith          getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == ':') {
307787a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        // C++0x [lex.pptoken]p3:
307887a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        //  Otherwise, if the next three characters are <:: and the subsequent
307987a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        //  character is neither : nor >, the < is treated as a preprocessor
308087a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        //  token by itself and not as the first character of the alternative
308187a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        //  token <:.
308287a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        unsigned SizeTmp3;
308387a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        char After = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3);
308487a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        if (After != ':' && After != '>') {
308587a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith          Kind = tok::less;
3086661a99690bc133bbaa029da925481d4a860dec90Richard Smith          if (!isLexingRawMode())
3087661a99690bc133bbaa029da925481d4a860dec90Richard Smith            Diag(BufferPtr, diag::warn_cxx98_compat_less_colon_colon);
308887a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith          break;
308987a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith        }
309087a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith      }
309187a1e19735a27d84edc2ac1331c040e2fb4c3b1aRichard Smith
30925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
30939e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::l_square;
30944e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (LangOpts.Digraphs && Char == '%') {     // '<%' -> '{'
30955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
30969e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::l_brace;
30975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
30989e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::less;
30995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
31005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '>':
31025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
31035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
31045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31059e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::greaterequal;
31065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '>') {
310734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
310834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (After == '=') {
310934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
311034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner                             SizeTmp2, Result);
311134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::greatergreaterequal;
3112d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith      } else if (After == '>' && IsStartOfConflictMarker(CurPtr-1)) {
3113d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith        // If this is actually a '>>>>' conflict marker, recognize it as such
3114d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith        // and recover nicely.
3115d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith        goto LexNextToken;
311634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
311734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        // If this is '>>>>>>>' and we're in a conflict marker, ignore it.
311834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
31194e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      } else if (LangOpts.CUDA && After == '>') {
31201b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne        Kind = tok::greatergreatergreater;
31211b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
31221b791d6465d42a9763927be1dd8af229efcbbf5ePeter Collingbourne                             SizeTmp2, Result);
312334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else {
312434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
312534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::greatergreater;
312634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      }
312734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
31285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
31299e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::greater;
31305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
31315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '^':
31335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
31345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
31355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31369e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::caretequal;
31375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
31389e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::caret;
31395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
31405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '|':
31425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
31435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
31449e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipeequal;
31455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '|') {
314734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      // If this is '|||||||' and we're in a conflict marker, ignore it.
314834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
314934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
31509e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipepipe;
31515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
31539e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipe;
31545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
31555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ':':
31575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
31584e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (LangOpts.Digraphs && Char == '>') {
31599e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::r_square; // ':>' -> ']'
31605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31614e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (LangOpts.CPlusPlus && Char == ':') {
31629e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::coloncolon;
31635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else {
31659e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::colon;
31665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
31675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ';':
31699e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::semi;
31705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '=':
31725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
31735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
3174d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith      // If this is '====' and we're in a conflict marker, ignore it.
317534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
317634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
317734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
31789e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::equalequal;
31795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else {
31819e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::equal;
31825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
31835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ',':
31859e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::comma;
31865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
31875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '#':
31885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
31895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '#') {
31909e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::hashhash;
31915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31924e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    } else if (Char == '@' && LangOpts.MicrosoftExt) {  // #@ -> Charize
31939e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::hashat;
319474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
319566d5ce11b9426f6a59f61a03cbd8dbf047cc9350Ted Kremenek        Diag(BufferPtr, diag::ext_charize_microsoft);
31965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
31975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
31985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // We parsed a # character.  If this occurs at the start of the line,
31995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // it's actually the start of a preprocessing directive.  Callback to
32005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // the preprocessor to handle it.
32015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FIXME: -fpreprocessed mode??
3202766703b76dc1eb0c49d11cafc2a17c7407876ebcChris Lattner      if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
3203e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner        FormTokenWithChars(Result, CurPtr, tok::hash);
3204168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner        PP->HandleDirective(Result);
32051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // As an optimization, if the preprocessor didn't switch lexers, tail
32075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // recurse.
3208168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner        if (PP->isCurrentLexer(this)) {
32095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Start a new token.  If this is a #include or something, the PP may
32105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // want us starting at the beginning of the line again.  If so, set
3211515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner          // the StartOfLine flag and clear LeadingSpace.
32125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (IsAtStartOfLine) {
3213d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner            Result.setFlag(Token::StartOfLine);
3214515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner            Result.clearFlag(Token::LeadingSpace);
32155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            IsAtStartOfLine = false;
32165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
32175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          goto LexNextToken;   // GCC isn't tail call eliminating.
32185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
3219168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner        return PP->Lex(Result);
32205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
32211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3222e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner      Kind = tok::hash;
32235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
32245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
32255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
32263a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '@':
32273a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    // Objective C support.
32284e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (CurPtr[-1] == '@' && LangOpts.ObjC1)
32299e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::at;
32303a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    else
32319e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::unknown;
32323a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    break;
32331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\\':
32355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FIXME: UCN's.
32365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FALL THROUGH.
32375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
32389e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::unknown;
32395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
32405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
32411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Notify MIOpt that we read a non-whitespace/non-comment token.
32435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  MIOpt.ReadToken();
32445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
32455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
32469e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, Kind);
32475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3248