Lexer.cpp revision f033f1da4a34f8df6e95e9929dc04ff54bb8fb01
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
10d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner//  This file implements the Lexer and Token interfaces.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// TODO: GCC Diagnostics emitted by the lexer:
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// PEDWARN: (form feed|vertical tab) in preprocessing directive
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Universal characters, unicode, char mapping:
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// WARNING: `%.*s' is not in NFKC
195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// WARNING: `%.*s' is not in NFC
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Other:
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// TODO: Options to support:
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//    -fexec-charset,-fwide-exec-charset
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Lexer.h"
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Preprocessor.h"
29500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Lex/LexDiagnostic.h"
309dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner#include "clang/Basic/SourceManager.h"
31f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor#include "llvm/ADT/StringSwitch.h"
32409a03671224d4e5bdab1594c43baf070148f830Chris Lattner#include "llvm/Support/Compiler.h"
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Support/MemoryBuffer.h"
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <cctype>
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
37a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic void InitCharacterInfo();
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
39dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===//
40dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner// Token Class Implementation
41dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===//
42dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
44dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattnerbool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
45bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor  if (IdentifierInfo *II = getIdentifierInfo())
46bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor    return II->getObjCKeywordID() == objcKey;
47bec1c9d6f3feeec112cc8eeae90c1be29c6aaf13Douglas Gregor  return false;
48dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner}
49dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
50dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner/// getObjCKeywordID - Return the ObjC keyword kind.
51dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattnertok::ObjCKeywordKind Token::getObjCKeywordID() const {
52dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner  IdentifierInfo *specId = getIdentifierInfo();
53dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner  return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
54dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner}
55dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
5653702cd401b8fdca985aede7732c2f6a82ad9b1cChris Lattner
57dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===//
58dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner// Lexer Class Implementation
59dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner//===----------------------------------------------------------------------===//
60dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid Lexer::InitLexer(const char *BufStart, const char *BufPtr,
6222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner                      const char *BufEnd) {
63a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner  InitCharacterInfo();
641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  BufferStart = BufStart;
6622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  BufferPtr = BufPtr;
6722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  BufferEnd = BufEnd;
681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  assert(BufEnd[0] == 0 &&
7022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner         "We assume that the input buffer has a null character at the end"
7122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner         " to simplify lexing!");
721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  Is_PragmaLexer = false;
7434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  IsInConflictMarker = false;
7581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
7622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // Start of the file is a start of line.
7722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  IsAtStartOfLine = true;
781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // We are not after parsing a #.
8022d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  ParsingPreprocessorDirective = false;
811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // We are not after parsing #include.
8322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  ParsingFilename = false;
841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8522d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // We are not in raw mode.  Raw mode disables diagnostics and interpretation
8622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // of tokens (e.g. identifiers, thus disabling macro expansion).  It is used
8722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
8822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // or otherwise skipping over tokens.
8922d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  LexingRawMode = false;
901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9122d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  // Default to not keeping comments.
9222d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  ExtendedTokenMode = 0;
9322d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner}
9422d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
950770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// Lexer constructor - Create a new lexer object for the specified buffer
960770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// with the specified preprocessor managing the lexing process.  This lexer
970770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// assumes that the associated file buffer and Preprocessor objects will
980770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner/// outlive it, so it doesn't take ownership of either of them.
996e2901407bff59aeb4cc301cc58b034723d0eb49Chris LattnerLexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP)
10088d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner  : PreprocessorLexer(&PP, FID),
10188d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner    FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
10288d3ac1341aa016cabd966c5b113a95ac05ea43fChris Lattner    Features(PP.getLangOptions()) {
1031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1040770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner  InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
1050770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner            InputFile->getBufferEnd());
1061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1070770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner  // Default to keeping comments if the preprocessor wants them.
1080770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner  SetCommentRetentionState(PP.getCommentRetentionState());
1090770dabb1ae81a2a9c2e7199262067103062a0b3Chris Lattner}
110dbf388b54bb1688ee4470c61235494cbc34b8f3cChris Lattner
111168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner/// Lexer constructor - Create a new raw lexer object.  This object is only
112590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner/// suitable for calls to 'LexRawToken'.  This lexer assumes that the text
113590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner/// range will outlive it, so it doesn't take ownership of it.
114168ae2d44a443da75ea85db5f3b5081eb0bce113Chris LattnerLexer::Lexer(SourceLocation fileloc, const LangOptions &features,
115de96c0f29c4cacabe6ea577c61db87c2a85aea6cChris Lattner             const char *BufStart, const char *BufPtr, const char *BufEnd)
116c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  : FileLoc(fileloc), Features(features) {
11722d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner
11822d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  InitLexer(BufStart, BufPtr, BufEnd);
1191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
120168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  // We *are* in raw mode.
121168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  LexingRawMode = true;
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
124025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// Lexer constructor - Create a new raw lexer object.  This object is only
125025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// suitable for calls to 'LexRawToken'.  This lexer assumes that the text
126025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner/// range will outlive it, so it doesn't take ownership of it.
1276e2901407bff59aeb4cc301cc58b034723d0eb49Chris LattnerLexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile,
1286e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner             const SourceManager &SM, const LangOptions &features)
129025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner  : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
130025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner
1311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
132025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner            FromFile->getBufferEnd());
1331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
134025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner  // We *are* in raw mode.
135025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner  LexingRawMode = true;
136025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner}
137025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner
13842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
13942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// _Pragma expansion.  This has a variety of magic semantics that this method
14042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// sets up.  It returns a new'd Lexer that must be delete'd when done.
14142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner///
14242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// On entrance to this routine, TokStartLoc is a macro location which has a
14342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// spelling loc that indicates the bytes to be lexed for the token and an
14442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// instantiation location that indicates where all lexed tokens should be
14542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// "expanded from".
14642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner///
14742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
14842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// normal lexer that remaps tokens as they fly by.  This would require making
14942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// Preprocessor::Lex virtual.  Given that, we could just dump in a magic lexer
15042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// interface that could handle this stuff.  This would pull GetMappedTokenLoc
15142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner/// out of the critical path of the lexer!
15242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner///
1531eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpLexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
154e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                 SourceLocation InstantiationLocStart,
155e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                 SourceLocation InstantiationLocEnd,
156bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner                                 unsigned TokLen, Preprocessor &PP) {
15742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  SourceManager &SM = PP.getSourceManager();
15842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
15942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Create the lexer as if we were going to lex the file normally.
160a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner  FileID SpellingFID = SM.getFileID(SpellingLoc);
1616e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner  const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID);
1626e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner  Lexer *L = new Lexer(SpellingFID, InputFile, PP);
1631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Now that the lexer is created, change the start/end locations so that we
16542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // just lex the subsection of the file that we want.  This is lexing from a
16642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // scratch buffer.
16742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  const char *StrData = SM.getCharacterData(SpellingLoc);
1681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->BufferPtr = StrData;
17042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->BufferEnd = StrData+TokLen;
1711fa495304c81e03f07f278a47b5efe9317104aabChris Lattner  assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
17242e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
17342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Set the SourceLocation with the remapping information.  This ensures that
17442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // GetMappedTokenLoc will remap the tokens as they are lexed.
175de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
176e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                         InstantiationLocStart,
177e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                         InstantiationLocEnd, TokLen);
1781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // Ensure that the lexer thinks it is inside a directive, so that end \n will
18042e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // return an EOM token.
18142e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->ParsingPreprocessorDirective = true;
1821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18342e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  // This lexer really is for _Pragma.
18442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  L->Is_PragmaLexer = true;
18542e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  return L;
18642e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner}
18742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
188168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner
1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Stringify - Convert the specified string into a C string, with surrounding
1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ""'s, and with escaped \ and " characters.
1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstd::string Lexer::Stringify(const std::string &Str, bool Charify) {
1925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  std::string Result = Str;
1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char Quote = Charify ? '\'' : '"';
1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Result.size(); i != e; ++i) {
1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Result[i] == '\\' || Result[i] == Quote) {
1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Result.insert(Result.begin()+i, '\\');
1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++i; ++e;
1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return Result;
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
203d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// Stringify - Convert the specified string into a C string by escaping '\'
204d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner/// and " characters.  This does not add surrounding ""'s to the string.
205d8e3083840fef752d11ca183f42786470ed061e3Chris Lattnervoid Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
206d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner  for (unsigned i = 0, e = Str.size(); i != e; ++i) {
207d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner    if (Str[i] == '\\' || Str[i] == '"') {
208d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner      Str.insert(Str.begin()+i, '\\');
209d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner      ++i; ++e;
210d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner    }
211d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner  }
212d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner}
213d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner
21433e9abd21083a0191a7676a04b497006d2da184dDouglas Gregorstatic bool isWhitespace(unsigned char c);
2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2169a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// MeasureTokenLength - Relex the token at the specified location and return
2179a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// its length in bytes in the input file.  If the token needs cleaning (e.g.
2189a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// includes a trigraph or an escaped newline) then this count includes bytes
2199a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner/// that are part of that.
2209a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattnerunsigned Lexer::MeasureTokenLength(SourceLocation Loc,
2212c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner                                   const SourceManager &SM,
2222c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner                                   const LangOptions &LangOpts) {
2239a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // TODO: this could be special cased for common tokens like identifiers, ')',
2249a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // etc to make this faster, if it mattered.  Just look at StrData[0] to handle
2251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // all obviously single-char tokens.  This could use
2269a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
2279a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // something.
228de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
229de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // If this comes from a macro expansion, we really do want the macro name, not
230de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // the token this macro expanded to.
231363fdc29656cc03c1817268888f95e6343470aa8Chris Lattner  Loc = SM.getInstantiationLoc(Loc);
232363fdc29656cc03c1817268888f95e6343470aa8Chris Lattner  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
233f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  bool Invalid = false;
234f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer  llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
235f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  if (Invalid)
236aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregor    return 0;
237f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer
238f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer  const char *StrData = Buffer.data()+LocInfo.second;
2398350394c65b81bba3986dfe44ae17423873741deChris Lattner
24033e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  if (isWhitespace(StrData[0]))
24133e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    return 0;
24233e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor
2439a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  // Create a lexer starting at the beginning of this token.
244f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer  Lexer TheLexer(Loc, LangOpts, Buffer.begin(), StrData, Buffer.end());
24539de7409bffb6b725a8aa64f0ba77ab51e8c9eb3Chris Lattner  TheLexer.SetCommentRetentionState(true);
2469a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  Token TheTok;
247590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner  TheLexer.LexFromRawLexer(TheTok);
2489a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  return TheTok.getLength();
2499a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner}
2509a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner
251f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregornamespace {
252f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  enum PreambleDirectiveKind {
253f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    PDK_Skipped,
254f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    PDK_StartIf,
255f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    PDK_EndIf,
256f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    PDK_Unknown
257f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  };
258f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor}
259f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
260f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregorunsigned Lexer::ComputePreamble(const llvm::MemoryBuffer *Buffer) {
261f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  // Create a lexer starting at the beginning of the file. Note that we use a
262f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  // "fake" file source location at offset 1 so that the lexer will track our
263f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  // position within the file.
264f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  const unsigned StartOffset = 1;
265f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  SourceLocation StartLoc = SourceLocation::getFromRawEncoding(StartOffset);
266f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  LangOptions LangOpts;
267f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  Lexer TheLexer(StartLoc, LangOpts, Buffer->getBufferStart(),
268f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor                 Buffer->getBufferStart(), Buffer->getBufferEnd());
269f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
270f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  bool InPreprocessorDirective = false;
271f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  Token TheTok;
272f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  Token IfStartTok;
273f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  unsigned IfCount = 0;
274f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  do {
275f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    TheLexer.LexFromRawLexer(TheTok);
276f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
277f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    if (InPreprocessorDirective) {
278f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // If we've hit the end of the file, we're done.
279f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      if (TheTok.getKind() == tok::eof) {
280f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        InPreprocessorDirective = false;
281f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        break;
282f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      }
283f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
284f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // If we haven't hit the end of the preprocessor directive, skip this
285f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // token.
286f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      if (!TheTok.isAtStartOfLine())
287f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        continue;
288f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
289f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // We've passed the end of the preprocessor directive, and will look
290f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // at this token again below.
291f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      InPreprocessorDirective = false;
292f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    }
293f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
294f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    // Comments are okay; skip over them.
295f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    if (TheTok.getKind() == tok::comment)
296f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      continue;
297f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
298f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) {
299f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // This is the start of a preprocessor directive.
300f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      Token HashTok = TheTok;
301f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      InPreprocessorDirective = true;
302f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
303f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // Figure out which direective this is. Since we're lexing raw tokens,
304f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // we don't have an identifier table available. Instead, just look at
305f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // the raw identifier to recognize and categorize preprocessor directives.
306f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      TheLexer.LexFromRawLexer(TheTok);
307f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      if (TheTok.getKind() == tok::identifier && !TheTok.needsCleaning()) {
308f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        const char *IdStart = Buffer->getBufferStart()
309f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor                            + TheTok.getLocation().getRawEncoding() - 1;
310f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        llvm::StringRef Keyword(IdStart, TheTok.getLength());
311f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        PreambleDirectiveKind PDK
312f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          = llvm::StringSwitch<PreambleDirectiveKind>(Keyword)
313f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("include", PDK_Skipped)
314f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("__include_macros", PDK_Skipped)
315f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("define", PDK_Skipped)
316f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("undef", PDK_Skipped)
317f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("line", PDK_Skipped)
318f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("error", PDK_Skipped)
319f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("pragma", PDK_Skipped)
320f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("import", PDK_Skipped)
321f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("include_next", PDK_Skipped)
322f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("warning", PDK_Skipped)
323f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("ident", PDK_Skipped)
324f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("sccs", PDK_Skipped)
325f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("assert", PDK_Skipped)
326f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("unassert", PDK_Skipped)
327f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("if", PDK_StartIf)
328f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("ifdef", PDK_StartIf)
329f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("ifndef", PDK_StartIf)
330f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("elif", PDK_Skipped)
331f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("else", PDK_Skipped)
332f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Case("endif", PDK_EndIf)
333f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor              .Default(PDK_Unknown);
334f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
335f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        switch (PDK) {
336f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_Skipped:
337f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          continue;
338f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
339f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_StartIf:
340f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          if (IfCount == 0)
341f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor            IfStartTok = HashTok;
342f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
343f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          ++IfCount;
344f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          continue;
345f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
346f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_EndIf:
347f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          // Mismatched #endif. The preamble ends here.
348f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          if (IfCount == 0)
349f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor            break;
350f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
351f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          --IfCount;
352f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          continue;
353f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
354f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        case PDK_Unknown:
355f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          // We don't know what this directive is; stop at the '#'.
356f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor          break;
357f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor        }
358f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      }
359f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
360f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // We only end up here if we didn't recognize the preprocessor
361f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // directive or it was one that can't occur in the preamble at this
362f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      // point. Roll back the current token to the location of the '#'.
363f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      InPreprocessorDirective = false;
364f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor      TheTok = HashTok;
365f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    }
366f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
367f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    // We hit a token
368f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor    break;
369f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  } while (true);
370f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
371f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation();
372f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  return End.getRawEncoding() - StartLoc.getRawEncoding();
373f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor}
374f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor
3755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Character information.
3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
3785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum {
3805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_HORZ_WS  = 0x01,  // ' ', '\t', '\f', '\v'.  Note, no '\0'
3815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_VERT_WS  = 0x02,  // '\r', '\n'
3825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_LETTER   = 0x04,  // a-z,A-Z
3835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_NUMBER   = 0x08,  // 0-9
3845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_UNDER    = 0x10,  // _
3855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CHAR_PERIOD   = 0x20   // .
3865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
38803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// Statically initialize CharInfo table based on ASCII character set
38903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// Reference: FreeBSD 7.2 /usr/share/misc/ascii
390a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic const unsigned char CharInfo[256] =
39103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner{
39203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 0 NUL         1 SOH         2 STX         3 ETX
39303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 4 EOT         5 ENQ         6 ACK         7 BEL
39403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
39503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
39603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner// 8 BS          9 HT         10 NL         11 VT
39703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//12 NP         13 CR         14 SO         15 SI
39803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
39903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_HORZ_WS, CHAR_VERT_WS, 0           , 0           ,
40003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//16 DLE        17 DC1        18 DC2        19 DC3
40103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//20 DC4        21 NAK        22 SYN        23 ETB
40203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
40303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
40403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//24 CAN        25 EM         26 SUB        27 ESC
40503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//28 FS         29 GS         30 RS         31 US
40603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
40703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
40803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//32 SP         33  !         34  "         35  #
40903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//36  $         37  %         38  &         39  '
41003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_HORZ_WS, 0           , 0           , 0           ,
41103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
41203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//40  (         41  )         42  *         43  +
41303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//44  ,         45  -         46  .         47  /
41403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
41503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , CHAR_PERIOD , 0           ,
41603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//48  0         49  1         50  2         51  3
41703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//52  4         53  5         54  6         55  7
41803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
41903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
42003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//56  8         57  9         58  :         59  ;
42103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//60  <         61  =         62  >         63  ?
42203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_NUMBER , CHAR_NUMBER , 0           , 0           ,
42303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0           ,
42403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//64  @         65  A         66  B         67  C
42503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//68  D         69  E         70  F         71  G
42603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
42703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
42803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//72  H         73  I         74  J         75  K
42903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//76  L         77  M         78  N         79  O
43003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
43103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
43203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//80  P         81  Q         82  R         83  S
43303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//84  T         85  U         86  V         87  W
43403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
43503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
43603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//88  X         89  Y         90  Z         91  [
43703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//92  \         93  ]         94  ^         95  _
43803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0           ,
43903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , CHAR_UNDER  ,
44003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//96  `         97  a         98  b         99  c
44103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//100  d       101  e        102  f        103  g
44203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
44303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
44403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//104  h       105  i        106  j        107  k
44503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//108  l       109  m        110  n        111  o
44603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
44703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
44803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//112  p       113  q        114  r        115  s
44903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//116  t       117  u        118  v        119  w
45003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
45103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
45203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//120  x       121  y        122  z        123  {
45303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner//124  |        125  }        126  ~        127 DEL
45403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0           ,
45503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner   0           , 0           , 0           , 0
45603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner};
45703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner
458a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattnerstatic void InitCharacterInfo() {
4595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static bool isInited = false;
4605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (isInited) return;
46103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  // check the statically-initialized CharInfo table
46203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
46303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
46403b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
46503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
46603b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
46703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
46803b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_UNDER   == CharInfo[(int)'_']);
46903b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  assert(CHAR_PERIOD  == CharInfo[(int)'.']);
47003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  for (unsigned i = 'a'; i <= 'z'; ++i) {
47103b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner    assert(CHAR_LETTER == CharInfo[i]);
47203b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner    assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
47303b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  }
4745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = '0'; i <= '9'; ++i)
47503b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner    assert(CHAR_NUMBER == CharInfo[i]);
4767b682656310b0d7e94a98d982444bc7ddedc653fSteve Naroff
47703b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner  isInited = true;
4785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
48003b986660cc9ae7a08588f99913f2d91e359a65fChris Lattner
4815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIdentifierBody - Return true if this is the body character of an
4825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// identifier, which is [a-zA-Z0-9_].
4835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isIdentifierBody(unsigned char c) {
48495c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
4855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isHorizontalWhitespace - Return true if this character is horizontal
4885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whitespace: ' ', '\t', '\f', '\v'.  Note that this returns false for '\0'.
4895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isHorizontalWhitespace(unsigned char c) {
49095c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
4915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isWhitespace - Return true if this character is horizontal or vertical
4945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'.  Note that this returns false
4955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// for '\0'.
4965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isWhitespace(unsigned char c) {
49795c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser  return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
4985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNumberBody - Return true if this is the body character of an
5015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// preprocessing number, which is [a-zA-Z0-9_.].
5025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic inline bool isNumberBody(unsigned char c) {
5031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
50495c062b94e2af5380617cf5dc115497ae7a5f817Hartmut Kaiser    true : false;
5055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Diagnostics forwarding code.
5105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
5115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
512409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
513409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// lexer buffer was all instantiated at a single point, perform the mapping.
514409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// This is currently only used for _Pragma implementation, so it is the slow
515409a03671224d4e5bdab1594c43baf070148f830Chris Lattner/// path of the hot getSourceLocation method.  Do not allow it to be inlined.
516c997eb4ac1b332406b1f16d4eddce7988d68c56bBenjamin Kramerstatic DISABLE_INLINE SourceLocation GetMappedTokenLoc(Preprocessor &PP,
517c997eb4ac1b332406b1f16d4eddce7988d68c56bBenjamin Kramer                                                       SourceLocation FileLoc,
518c997eb4ac1b332406b1f16d4eddce7988d68c56bBenjamin Kramer                                                       unsigned CharNo,
519c997eb4ac1b332406b1f16d4eddce7988d68c56bBenjamin Kramer                                                       unsigned TokLen);
520409a03671224d4e5bdab1594c43baf070148f830Chris Lattnerstatic SourceLocation GetMappedTokenLoc(Preprocessor &PP,
521409a03671224d4e5bdab1594c43baf070148f830Chris Lattner                                        SourceLocation FileLoc,
522de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                        unsigned CharNo, unsigned TokLen) {
523e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  assert(FileLoc.isMacroID() && "Must be an instantiation");
5241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
525409a03671224d4e5bdab1594c43baf070148f830Chris Lattner  // Otherwise, we're lexing "mapped tokens".  This is used for things like
526409a03671224d4e5bdab1594c43baf070148f830Chris Lattner  // _Pragma handling.  Combine the instantiation location of FileLoc with the
527df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  // spelling location.
528e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  SourceManager &SM = PP.getSourceManager();
5291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
530f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
531df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  // characters come from spelling(FileLoc)+Offset.
532e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
533bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner  SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
5341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
535e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  // Figure out the expansion loc range, which is the range covered by the
536e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  // original _Pragma(...) sequence.
537e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  std::pair<SourceLocation,SourceLocation> II =
538e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    SM.getImmediateInstantiationRange(FileLoc);
5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
540e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
541409a03671224d4e5bdab1594c43baf070148f830Chris Lattner}
542409a03671224d4e5bdab1594c43baf070148f830Chris Lattner
5435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSourceLocation - Return a source location identifier for the specified
5445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// offset in the current file.
545de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris LattnerSourceLocation Lexer::getSourceLocation(const char *Loc,
546de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                        unsigned TokLen) const {
547448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner  assert(Loc >= BufferStart && Loc <= BufferEnd &&
5485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer         "Location out of range for this buffer!");
5499dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner
5509dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  // In the normal case, we're just lexing from a simple file buffer, return
5519dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  // the file id from FileLoc with the offset specified.
552448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner  unsigned CharNo = Loc-BufferStart;
5539dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  if (FileLoc.isFileID())
554bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner    return FileLoc.getFileLocWithOffset(CharNo);
5551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5562b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  // Otherwise, this is the _Pragma lexer case, which pretends that all of the
5572b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  // tokens are lexed from where the _Pragma was defined.
558168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  assert(PP && "This doesn't work on raw lexers");
559de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
5605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Diag - Forwarding function for diagnostics.  This translate a source
5635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// position in the current buffer into a SourceLocation object for rendering.
5643cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris LattnerDiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
5653692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner  return PP->Diag(getSourceLocation(Loc), DiagID);
5665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
5695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Trigraph and Escaped Newline Handling Code.
5705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
5715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
5735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
5745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char GetTrigraphCharForLetter(char Letter) {
5755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Letter) {
5765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:   return 0;
5775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '=':  return '#';
5785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ')':  return ']';
5795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '(':  return '[';
5805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '!':  return '|';
5815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\'': return '^';
5825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '>':  return '}';
5835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '/':  return '\\';
5845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '<':  return '{';
5855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '-':  return '~';
5865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// DecodeTrigraphChar - If the specified character is a legal trigraph when
5905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// prefixed with ??, emit a trigraph warning.  If trigraphs are enabled,
5915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// return the result character.  Finally, emit a warning about trigraph use
5925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// whether trigraphs are enabled or not.
5935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic char DecodeTrigraphChar(const char *CP, Lexer *L) {
5945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char Res = GetTrigraphCharForLetter(*CP);
5953692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner  if (!Res || !L) return Res;
5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5973692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner  if (!L->getFeatures().Trigraphs) {
59874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!L->isLexingRawMode())
59974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      L->Diag(CP-2, diag::trigraph_ignored);
6003692b09faa9fe346f39bc922db6dce48cdcc3f63Chris Lattner    return 0;
6015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (!L->isLexingRawMode())
60474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
6055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return Res;
6065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
60824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// getEscapedNewLineSize - Return the size of the specified escaped newline,
60924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a
6101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// trigraph equivalent on entry to this function.
61124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattnerunsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
61224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  unsigned Size = 0;
61324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  while (isWhitespace(Ptr[Size])) {
61424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    ++Size;
6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
61724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      continue;
61824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
61924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    // If this is a \r\n or \n\r, skip the other half.
62024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
62124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner        Ptr[Size-1] != Ptr[Size])
62224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      ++Size;
6231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
62424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    return Size;
6251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
6261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
62724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  // Not an escaped newline, must be a \t or something else.
62824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  return 0;
62924f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner}
63024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
631033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
632033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// them), skip over them and return the first non-escaped-newline found,
633033749571f8d4c804eeb357c70b06424aa24503bChris Lattner/// otherwise return P.
634033749571f8d4c804eeb357c70b06424aa24503bChris Lattnerconst char *Lexer::SkipEscapedNewLines(const char *P) {
635033749571f8d4c804eeb357c70b06424aa24503bChris Lattner  while (1) {
636033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    const char *AfterEscape;
637033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    if (*P == '\\') {
638033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      AfterEscape = P+1;
639033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    } else if (*P == '?') {
640033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      // If not a trigraph for escape, bail out.
641033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      if (P[1] != '?' || P[2] != '/')
642033749571f8d4c804eeb357c70b06424aa24503bChris Lattner        return P;
643033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      AfterEscape = P+3;
644033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    } else {
645033749571f8d4c804eeb357c70b06424aa24503bChris Lattner      return P;
646033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    }
6471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
648033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
649033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    if (NewLineSize == 0) return P;
650033749571f8d4c804eeb357c70b06424aa24503bChris Lattner    P = AfterEscape+NewLineSize;
651033749571f8d4c804eeb357c70b06424aa24503bChris Lattner  }
652033749571f8d4c804eeb357c70b06424aa24503bChris Lattner}
653033749571f8d4c804eeb357c70b06424aa24503bChris Lattner
65424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
6555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
6565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// get its size, and return it.  This is tricky in several cases:
6575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   1. If currently at the start of a trigraph, we warn about the trigraph,
6585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      then either return the trigraph (skipping 3 chars) or the '?',
6595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      depending on whether trigraphs are enabled or not.
6605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   2. If this is an escaped newline (potentially with whitespace between
6615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      the backslash and newline), implicitly skip the newline and return
6625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///      the char after it.
6635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   3. If this is a UCN, return it.  FIXME: C++ UCN's?
6645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
6655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This handles the slow/uncommon case of the getCharAndSize method.  Here we
6665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// know that we can accumulate into Size, and that we have already incremented
6675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Ptr by Size bytes.
6685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
6695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
6705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match.
6715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
6725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
673d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner                               Token *Tok) {
6745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, look for an escaped newline.
6755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '\\') {
6765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Size;
6775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Ptr;
6785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash:
6795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Common case, backslash-char where the char is not whitespace.
6805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!isWhitespace(Ptr[0])) return '\\';
6811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6825636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner    // See if we have optional whitespace characters between the slash and
6835636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner    // newline.
68424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
68524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Remember that this token needs to be cleaned.
68624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      if (Tok) Tok->setFlag(Token::NeedsCleaning);
68724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner
68824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Warn if there was whitespace between the backslash and newline.
6895636a3b6ece2c1f413464b72545e08eb0b7f06e4Chris Lattner      if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
69024f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner        Diag(Ptr, diag::backslash_newline_space);
6911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
69224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Found backslash<whitespace><newline>.  Parse the char after it.
69324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Size += EscapedNewLineSize;
69424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Ptr  += EscapedNewLineSize;
69524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Use slow version to accumulate a correct size field.
69624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      return getCharAndSizeSlow(Ptr, Size, Tok);
69724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    }
6981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is not an escaped newline, just return the slash.
7005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return '\\';
7015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a trigraph, process it.
7045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '?' && Ptr[1] == '?') {
7055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is actually a legal trigraph (not something like "??x"), emit
7065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // a trigraph warning.  If so, and if trigraphs are enabled, return it.
7075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
7085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Remember that this token needs to be cleaned.
709d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner      if (Tok) Tok->setFlag(Token::NeedsCleaning);
7105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Ptr += 3;
7125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Size += 3;
7135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '\\') goto Slash;
7145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return C;
7155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
7165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is neither, return a single character.
7195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++Size;
7205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return *Ptr;
7215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
7255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getCharAndSizeNoWarn method.  Here we know that we can accumulate into Size,
7265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// and that we have already incremented Ptr by Size bytes.
7275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
7285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
7295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// be updated to match.
7305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerchar Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
7315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                     const LangOptions &Features) {
7325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, look for an escaped newline.
7335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Ptr[0] == '\\') {
7345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Size;
7355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++Ptr;
7365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerSlash:
7375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Common case, backslash-char where the char is not whitespace.
7385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!isWhitespace(Ptr[0])) return '\\';
7391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // See if we have optional whitespace characters followed by a newline.
74124f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
74224f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Found backslash<whitespace><newline>.  Parse the char after it.
74324f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Size += EscapedNewLineSize;
74424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      Ptr  += EscapedNewLineSize;
7451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
74624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      // Use slow version to accumulate a correct size field.
74724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner      return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
74824f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner    }
7491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is not an escaped newline, just return the slash.
7515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return '\\';
7525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a trigraph, process it.
7555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
7565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is actually a legal trigraph (not something like "??x"), return
7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // it.
7585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (char C = GetTrigraphCharForLetter(Ptr[2])) {
7595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Ptr += 3;
7605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Size += 3;
7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '\\') goto Slash;
7625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return C;
7635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
7645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is neither, return a single character.
7675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++Size;
7685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return *Ptr;
7695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
7725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Helper methods for lexing.
7735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
7745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
775d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
7765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
7775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned Size;
7785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned char C = *CurPtr++;
779cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  while (isIdentifierBody(C))
7805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr++;
781cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner
7825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  --CurPtr;   // Back up over the skipped character.
7835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Fast path, no $,\,? in identifier found.  '\' might be an escaped newline
7855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
7865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: UCNs.
787cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  //
788cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  // TODO: Could merge these checks into a CharInfo flag to make the comparison
789cd991dbb12f24368753cef086c7ad3ec203c9ea6Chris Lattner  // cheaper
7905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
7915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerFinishIdentifier:
7925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const char *IdStart = BufferPtr;
7939e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::identifier);
7941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we are in raw mode, return this identifier raw.  There is no need to
7965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // look up identifier information or attempt to macro expand it.
7975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (LexingRawMode) return;
7981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Fill in Result.IdentifierInfo, looking up the identifier in the
8005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // identifier table.
801d1186fa38166a581b51975f0382a45fc3a0733d0Chris Lattner    IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
8021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
803863c486fcb6162495a94fddf7ac8409de2638995Chris Lattner    // Change the kind of this identifier to the appropriate token kind, e.g.
804863c486fcb6162495a94fddf7ac8409de2638995Chris Lattner    // turning "for" into a keyword.
805863c486fcb6162495a94fddf7ac8409de2638995Chris Lattner    Result.setKind(II->getTokenID());
8061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Finally, now that we know we have an identifier, pass this off to the
8085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // preprocessor, which may macro expand it or something.
809d1186fa38166a581b51975f0382a45fc3a0733d0Chris Lattner    if (II->isHandleIdentifierCase())
8106a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner      PP->HandleIdentifier(Result);
8116a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    return;
8125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, $,\,? in identifier found.  Enter slower path.
8151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  C = getCharAndSize(CurPtr, Size);
8175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
8185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '$') {
8195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // If we hit a $ and they are not supported in identifiers, we are done.
8205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (!Features.DollarIdents) goto FinishIdentifier;
8211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Otherwise, emit a diagnostic and continue.
82374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
82474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(CurPtr, diag::ext_dollar_in_identifier);
8255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, Size, Result);
8265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getCharAndSize(CurPtr, Size);
8275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      continue;
8285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
8295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Found end of identifier.
8305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      goto FinishIdentifier;
8315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
8325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this character is good, consume it.
8345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr = ConsumeChar(CurPtr, Size, Result);
8355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getCharAndSize(CurPtr, Size);
8375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (isIdentifierBody(C)) { // FIXME: UCNs.
8385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, Size, Result);
8395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getCharAndSize(CurPtr, Size);
8405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
8415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8455253c7ff266ae79308050c9f43d60dd1a67c5fb9Nate Begeman/// LexNumericConstant - Lex the remainder of a integer or floating point
8465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant. From[-1] is the first character lexed.  Return the end of the
8475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// constant.
848d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
8495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned Size;
8505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getCharAndSize(CurPtr, Size);
8515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char PrevCh = 0;
8525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isNumberBody(C)) { // FIXME: UCNs?
8535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr = ConsumeChar(CurPtr, Size, Result);
8545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PrevCh = C;
8555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getCharAndSize(CurPtr, Size);
8565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we fell out, check for a sign, due to 1e+12.  If we have one, continue.
8595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
8605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
8615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a hex FP constant, continue.
8638c723404c34e9347fae473489194e8c200c4ac83Sean Hunt  if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
8648c723404c34e9347fae473489194e8c200c4ac83Sean Hunt      (!PP || !PP->getLangOptions().CPlusPlus0x))
8655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
8661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
86847246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
8699e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
87047246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
8715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
8745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// either " or L".
875d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnervoid Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
8765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this string contain the \0 character?
8771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
8795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (C != '"') {
880571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    // Skip escaped characters.  Escaped newlines will already be processed by
881571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    // getAndAdvanceChar.
882571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    if (C == '\\')
8835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getAndAdvanceChar(CurPtr, Result);
88433611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor
885571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    if (C == '\n' || C == '\r' ||             // Newline.
88633611e0d5ab1372608a7649b1877cd4300621c71Douglas Gregor        (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
88733ab3f6c7e3065550de202088f8f335ecfa16ae1Chris Lattner      if (!isLexingRawMode() && !Features.AsmPreprocessor)
88874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(BufferPtr, diag::err_unterminated_string);
8899e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr-1, tok::unknown);
8905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return;
8915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
892571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner
893571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner    if (C == 0)
894571339c215dc3fa44acccb89c7c6b1d5b962b7f2Chris Lattner      NulCharacter = CurPtr-1;
8955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
8965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If a nul character existed in the string, warn about it.
89974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
90074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_string);
9015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of the token as well as the BufferPtr instance var.
90347246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
9049e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr,
9059e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner                     Wide ? tok::wide_string_literal : tok::string_literal);
90647246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
9075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
9105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// after having lexed the '<' character.  This is used for #include filenames.
911d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
9125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this string contain the \0 character?
9139cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner  const char *AfterLessPos = CurPtr;
9145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
9155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (C != '>') {
9165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip escaped characters.
9175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '\\') {
9185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Skip the escaped character.
9195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getAndAdvanceChar(CurPtr, Result);
9205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == '\n' || C == '\r' ||             // Newline.
9215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer               (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
9229cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      // If the filename is unterminated, then it must just be a lone <
9239cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      // character.  Return this as such.
9249cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      FormTokenWithChars(Result, AfterLessPos, tok::less);
9255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return;
9265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == 0) {
9275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      NulCharacter = CurPtr-1;
9285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
9295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
9305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If a nul character existed in the string, warn about it.
93374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
93474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_string);
9351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
93747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
9389e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
93947246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
9405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexCharConstant - Lex the remainder of a character constant, after having
9445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexed either ' or L'.
945d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
9465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *NulCharacter = 0; // Does this character contain the \0 character?
9475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C = getAndAdvanceChar(CurPtr, Result);
9495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (C == '\'') {
95033ab3f6c7e3065550de202088f8f335ecfa16ae1Chris Lattner    if (!isLexingRawMode() && !Features.AsmPreprocessor)
95174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      Diag(BufferPtr, diag::err_empty_character);
9529e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::unknown);
9535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return;
9545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
956d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner  while (C != '\'') {
957d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    // Skip escaped characters.
958d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    if (C == '\\') {
959d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      // Skip the escaped character.
960d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      // FIXME: UCN's
9615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = getAndAdvanceChar(CurPtr, Result);
962d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    } else if (C == '\n' || C == '\r' ||             // Newline.
963d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner               (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
964d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      if (!isLexingRawMode() && !Features.AsmPreprocessor)
965d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner        Diag(BufferPtr, diag::err_unterminated_char);
966d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      FormTokenWithChars(Result, CurPtr-1, tok::unknown);
967d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      return;
968d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    } else if (C == 0) {
969d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner      NulCharacter = CurPtr-1;
970d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    }
971d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner    C = getAndAdvanceChar(CurPtr, Result);
9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
974d80f786689d608e5c22d6e1045884de7aff76c40Chris Lattner  // If a nul character existed in the character, warn about it.
97574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (NulCharacter && !isLexingRawMode())
97674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    Diag(NulCharacter, diag::null_in_char);
9775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
97947246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  const char *TokStart = BufferPtr;
9809e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::char_constant);
98147246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  Result.setLiteralData(TokStart);
9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
9855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Update BufferPtr to point to the next non-whitespace character and return.
986d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner///
987d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
988d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner///
989d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattnerbool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
9905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Whitespace - Skip it, then return the token after the whitespace.
9915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned char Char = *CurPtr;  // Skip consequtive spaces efficiently.
9925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
9935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip horizontal whitespace very aggressively.
9945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (isHorizontalWhitespace(Char))
9955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Char = *++CurPtr;
9961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
997ddd3e8b90a92c39f620bda7a0945320c0b9e60dbDaniel Dunbar    // Otherwise if we have something other than whitespace, we're done.
9985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char != '\n' && Char != '\r')
9995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
10001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingPreprocessorDirective) {
10025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // End of preprocessor directive line, let LexTokenInternal handle this.
10035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      BufferPtr = CurPtr;
1004d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return false;
10055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
10061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // ok, but handle newline.
10085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // The returned token is at the start of the line.
1009d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::StartOfLine);
10105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // No leading whitespace seen so far.
1011d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.clearFlag(Token::LeadingSpace);
10125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = *++CurPtr;
10135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this isn't immediately after a newline, there is leading space.
10165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char PrevChar = CurPtr[-1];
10175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PrevChar != '\n' && PrevChar != '\r')
1018d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
10195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1020d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // If the client wants us to return whitespace, return it now.
1021d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  if (isKeepWhitespaceMode()) {
10229e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::unknown);
1023d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    return true;
1024d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  }
10251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
1027d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  return false;
10285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// SkipBCPLComment - We have just read the // characters from input.  Skip until
10315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// we find the newline character thats terminate the comment.  Then update
1032046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// BufferPtr and return.
1033046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner///
1034046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted
1035046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true.
1036d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
10375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If BCPL comments aren't explicitly enabled for this language, emit an
10385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // extension warning.
103974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (!Features.BCPLComment && !isLexingRawMode()) {
10405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(BufferPtr, diag::ext_bcpl_comment);
10411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Mark them enabled so we only emit one warning for this translation
10435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // unit.
10445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Features.BCPLComment = true;
10455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Scan over the body of the comment.  The common case, when scanning, is that
10485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // the comment contains normal ascii characters with nothing interesting in
10495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // them.  As such, optimize for this case with the inner loop.
10505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char C;
10515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  do {
10525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr;
10535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FIXME: Speedup BCPL comment lexing.  Just scan for a \n or \r character.
10545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we find a \n character, scan backwards, checking to see if it's an
10555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // escaped newline, like we do for block comments.
10561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over characters in the fast loop.
10585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (C != 0 &&                // Potentially EOF.
10595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           C != '\\' &&             // Potentially escaped newline.
10605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           C != '?' &&              // Potentially trigraph.
10615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           C != '\n' && C != '\r')  // Newline or DOS-style newline.
10625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *++CurPtr;
10635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is a newline, we're done.
10655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '\n' || C == '\r')
10665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;  // Found the newline? Break out!
10671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, this is a hard case.  Fall back on getAndAdvanceChar to
1069bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // properly decode the character.  Read it in raw mode to avoid emitting
1070bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // diagnostics about things like trigraphs.  If we see an escaped newline,
1071bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    // we'll handle it below.
10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const char *OldPtr = CurPtr;
1073bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    bool OldRawMode = isLexingRawMode();
1074bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    LexingRawMode = true;
10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = getAndAdvanceChar(CurPtr, Result);
1076bc3e9842212b78bcf49682641307e4f3419ecc5eChris Lattner    LexingRawMode = OldRawMode;
1077ead616c5d8681a41b11273327813e61bda01907aChris Lattner
1078ead616c5d8681a41b11273327813e61bda01907aChris Lattner    // If the char that we finally got was a \n, then we must have had something
1079ead616c5d8681a41b11273327813e61bda01907aChris Lattner    // like \<newline><newline>.  We don't want to have consumed the second
1080ead616c5d8681a41b11273327813e61bda01907aChris Lattner    // newline, we want CurPtr, to end up pointing to it down below.
1081ead616c5d8681a41b11273327813e61bda01907aChris Lattner    if (C == '\n' || C == '\r') {
1082ead616c5d8681a41b11273327813e61bda01907aChris Lattner      --CurPtr;
1083ead616c5d8681a41b11273327813e61bda01907aChris Lattner      C = 'x'; // doesn't matter what this is.
1084ead616c5d8681a41b11273327813e61bda01907aChris Lattner    }
10851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we read multiple characters, and one of those characters was a \r or
10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n, then we had an escaped newline within the comment.  Emit diagnostic
10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // unless the next line is also a // comment.
10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      for (; OldPtr != CurPtr; ++OldPtr)
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Okay, we found a // comment that ends in a newline, if the next
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // line is also a // comment, but has spaces, don't emit a diagnostic.
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (isspace(C)) {
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            const char *ForwardPtr = CurPtr;
10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            while (isspace(*ForwardPtr))  // Skip whitespace.
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              ++ForwardPtr;
10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
10995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              break;
11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
11011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
110274d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          if (!isLexingRawMode())
110374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner            Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          break;
11055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
11065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
11071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr == BufferEnd+1) { --CurPtr; break; }
11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } while (C != '\n' && C != '\r');
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11113d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  // Found but did not consume the newline.  Notify comment handlers about the
11123d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  // comment unless we're in a #if 0 block.
11133d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  if (PP && !isLexingRawMode() &&
11143d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner      PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
11153d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner                                            getSourceLocation(CurPtr)))) {
1116046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    BufferPtr = CurPtr;
1117046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    return true; // A token has to be returned.
1118046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  }
11191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are returning comments as tokens, return this comment as a token.
1121fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner  if (inKeepCommentMode())
11225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return SaveBCPLComment(Result, CurPtr);
11235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are inside a preprocessor directive and we see the end of line,
11255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // return immediately, so that the lexer can return this as an EOM token.
11265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
11275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    BufferPtr = CurPtr;
11282d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, eat the \n character.  We don't care if this is a \n\r or
11327a4f004a77421af876152281b92c4f8ea24afb2dChris Lattner  // \r\n sequence.  This is an efficiency hack (because we know the \n can't
1133d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // contribute to another token), it isn't needed for correctness.  Note that
1134d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // this is ok even in KeepWhitespaceMode, because we would have returned the
1135d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// comment above in that mode.
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ++CurPtr;
11371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // The next returned token is at the start of the line.
1139d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.setFlag(Token::StartOfLine);
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // No leading whitespace seen so far.
1141d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.clearFlag(Token::LeadingSpace);
11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
11432d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return false;
11445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// an appropriate way and return it.
1148d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
11499e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // If we're not in a preprocessor directive, just return the // comment
11509e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // directly.
11519e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, tok::comment);
11521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11539e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  if (!ParsingPreprocessorDirective)
11549e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    return true;
11551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11569e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // If this BCPL-style comment is in a macro definition, transmogrify it into
11579e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  // a C-style block comment.
1158453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor  bool Invalid = false;
1159453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor  std::string Spelling = PP->getSpelling(Result, &Invalid);
1160453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor  if (Invalid)
1161453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor    return true;
1162453091cc2082e207ea2c2dda645a9bc01b37fb0cDouglas Gregor
11639e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
11649e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Spelling[1] = '*';   // Change prefix to "/*".
11659e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Spelling += "*/";    // add suffix.
11661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11679e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  Result.setKind(tok::comment);
116847246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  PP->CreateString(&Spelling[0], Spelling.size(), Result,
116947246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner                   Result.getLocation());
11702d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return true;
11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// character (either \n or \r) is part of an escaped newline sequence.  Issue a
117547a2b40b7a3d9c89d6c3d12e5a81e2498c207750Chris Lattner/// diagnostic if so.  We know that the newline is inside of a block comment.
11761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
11775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                                  Lexer *L) {
11785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
11791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Back up off the newline.
11815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  --CurPtr;
11821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this is a two-character newline sequence, skip the other character.
11845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n\n or \r\r -> not escaped newline.
11865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[0] == CurPtr[1])
11875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
11885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // \n\r or \r\n -> skip the newline.
11895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    --CurPtr;
11905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have horizontal whitespace, skip over it.  We allow whitespace
11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // between the slash and newline.
11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool HasSpace = false;
11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    --CurPtr;
11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    HasSpace = true;
11985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we have a slash, we know this is an escaped newline.
12015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (*CurPtr == '\\') {
12025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[-1] != '*') return false;
12035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  } else {
12045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // It isn't a slash, is it the ?? / trigraph?
12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr[-3] != '*')
12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
12081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // This is the trigraph ending the comment.  Emit a stern warning!
12105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CurPtr -= 2;
12115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If no trigraphs are enabled, warn that we ignored this trigraph and
12135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // ignore this * character.
12145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!L->getFeatures().Trigraphs) {
121574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!L->isLexingRawMode())
121674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
12175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return false;
12185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
121974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!L->isLexingRawMode())
122074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      L->Diag(CurPtr, diag::trigraph_ends_block_comment);
12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Warn about having an escaped newline between the */ characters.
122474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (!L->isLexingRawMode())
122574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
12261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If there was space between the backslash and newline, warn about it.
122874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner  if (HasSpace && !L->isLexingRawMode())
122974d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    L->Diag(CurPtr, diag::backslash_newline_space);
12301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return true;
12325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__
12355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <emmintrin.h>
12365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__
12375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <altivec.h>
12385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#undef bool
12395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
12405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SkipBlockComment - We have just read the /* characters from input.  Read
12425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// until we find the */ characters that terminate the comment.  Note that we
12435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// don't bother decoding trigraphs or escaped newlines in block comments,
12445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// because they cannot cause the comment to end.  The only thing that can
12455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// happen is the comment could end with an escaped newline between the */ end
12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// of comment.
12472d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner///
1248046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// If we're in KeepCommentMode or any CommentHandler has inserted
1249046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner/// some tokens, this will store the first token and return true.
1250d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
12515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Scan one character past where we should, looking for a '/' character.  Once
12525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // we find it, check to see if it was preceeded by a *.  This common
12535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // optimization helps people who like to put a lot of * characters in their
12545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // comments.
12558146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner
12568146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // The first character we get with newlines and trigraphs skipped to handle
12578146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // the degenerate /*/ case below correctly if the * has an escaped newline
12588146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // after it.
12598146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  unsigned CharSize;
12608146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  unsigned char C = getCharAndSize(CurPtr, CharSize);
12618146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  CurPtr += CharSize;
12625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (C == 0 && CurPtr == BufferEnd+1) {
1263150fcd5dcb79ad0a9cc6824c5589000e3669c7a4Chris Lattner    if (!isLexingRawMode() &&
1264150fcd5dcb79ad0a9cc6824c5589000e3669c7a4Chris Lattner        !PP->isCodeCompletionFile(FileLoc))
12650af574270d3be2b0e73a3379dfaa633746f8fc6fChris Lattner      Diag(BufferPtr, diag::err_unterminated_block_comment);
126631f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    --CurPtr;
12671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
126831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    // KeepWhitespaceMode should return this broken comment as a token.  Since
126931f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    // it isn't a well formed comment, just return it as an 'unknown' token.
127031f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    if (isKeepWhitespaceMode()) {
12719e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr, tok::unknown);
127231f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      return true;
127331f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    }
12741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
127531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner    BufferPtr = CurPtr;
12762d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
12775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12798146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // Check to see if the first character after the '/*' is another /.  If so,
12808146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  // then this slash does not end the block comment, it is part of it.
12818146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner  if (C == '/')
12828146b6851e873eab71341f1b1d3198894bc4c0acChris Lattner    C = *CurPtr++;
12831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
12855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Skip over all non-interesting characters until we find end of buffer or a
12865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // (probably ending) '/' character.
12875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr + 24 < BufferEnd) {
12885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // While not aligned to a 16-byte boundary.
12895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
12905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        C = *CurPtr++;
12911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (C == '/') goto FoundSlash;
12935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifdef __SSE2__
12955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
12965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                     '/', '/', '/', '/', '/', '/', '/', '/');
12975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (CurPtr+16 <= BufferEnd &&
12985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
12995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 16;
13005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#elif __ALTIVEC__
13015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      __vector unsigned char Slashes = {
13021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        '/', '/', '/', '/',  '/', '/', '/', '/',
13035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        '/', '/', '/', '/',  '/', '/', '/', '/'
13045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      };
13055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (CurPtr+16 <= BufferEnd &&
13065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
13075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 16;
13081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#else
13095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Scan for '/' quickly.  Many block comments are very large.
13105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      while (CurPtr[0] != '/' &&
13115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[1] != '/' &&
13125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[2] != '/' &&
13135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr[3] != '/' &&
13145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer             CurPtr+4 < BufferEnd) {
13155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr += 4;
13165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
13175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
13181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // It has to be one of the bytes scanned, increment to it and read one.
13205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *CurPtr++;
13215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
13221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Loop to scan the remainder.
13245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (C != '/' && C != '\0')
13255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      C = *CurPtr++;
13261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FoundSlash:
13285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (C == '/') {
13295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr[-2] == '*')  // We found the final */.  We're done!
13305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
13311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
13335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
13345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // We found the final */, though it had an escaped newline between the
13355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // * and /.  We're done!
13365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          break;
13375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
13385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr[0] == '*' && CurPtr[1] != '/') {
13405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // If this is a /* inside of the comment, emit a warning.  Don't do this
13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // if this is a /*/, which will end the comment.  This misses cases with
13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // embedded escaped newlines, but oh well.
134374d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        if (!isLexingRawMode())
134474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          Diag(CurPtr-1, diag::warn_nested_block_comment);
13455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
13465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (C == 0 && CurPtr == BufferEnd+1) {
1347150fcd5dcb79ad0a9cc6824c5589000e3669c7a4Chris Lattner      if (!isLexingRawMode() && !PP->isCodeCompletionFile(FileLoc))
134874d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(BufferPtr, diag::err_unterminated_block_comment);
13495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Note: the user probably forgot a */.  We could continue immediately
13505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // after the /*, but this would involve lexing a lot of what really is the
13515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // comment, which surely would confuse the parser.
135231f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      --CurPtr;
13531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
135431f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      // KeepWhitespaceMode should return this broken comment as a token.  Since
135531f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      // it isn't a well formed comment, just return it as an 'unknown' token.
135631f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      if (isKeepWhitespaceMode()) {
13579e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        FormTokenWithChars(Result, CurPtr, tok::unknown);
135831f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner        return true;
135931f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      }
13601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
136131f0ecae4b2d4d268d04c562a43d9fbfee96f189Chris Lattner      BufferPtr = CurPtr;
13622d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner      return false;
13635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
13645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    C = *CurPtr++;
13655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13673d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  // Notify comment handlers about the comment unless we're in a #if 0 block.
13683d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner  if (PP && !isLexingRawMode() &&
13693d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner      PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
13703d0ad58b28e0d50fca7f21c6a078b05370510288Chris Lattner                                            getSourceLocation(CurPtr)))) {
1371046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    BufferPtr = CurPtr;
1372046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner    return true; // A token has to be returned.
1373046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  }
13742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
13755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are returning comments as tokens, return this comment as a token.
1376fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner  if (inKeepCommentMode()) {
13779e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::comment);
13782d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return true;
13795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // It is common for the tokens immediately after a /**/ comment to be
13825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // whitespace.  Instead of going through the big switch, handle it
1383d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // efficiently now.  This is safe even in KeepWhitespaceMode because we would
1384d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  // have already returned above with the comment as a token.
13855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (isHorizontalWhitespace(*CurPtr)) {
1386d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
13875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SkipWhitespace(Result, CurPtr+1);
13882d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner    return false;
13895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Otherwise, just return so that the next character will be lexed as a token.
13925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
1393d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.setFlag(Token::LeadingSpace);
13942d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner  return false;
13955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
13985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Primary Lexing Entry Points
13995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
14005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
14025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// uninterpreted string.  This switches the lexer out of directive mode.
14035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstd::string Lexer::ReadToEndOfLine() {
14045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParsingPreprocessorDirective && ParsingFilename == false &&
14055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer         "Must be in a preprocessing directive!");
14065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  std::string Result;
1407d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Token Tmp;
14085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // CurPtr - Cache BufferPtr in an automatic variable.
14105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CurPtr = BufferPtr;
14115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
14125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    char Char = getAndAdvanceChar(CurPtr, Tmp);
14135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    switch (Char) {
14145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    default:
14155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Result += Char;
14165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
14175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case 0:  // Null.
14185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Found end of file?
14195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (CurPtr-1 != BufferEnd) {
14205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // Nope, normal character, continue.
14215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        Result += Char;
14225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        break;
14235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
14245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FALL THROUGH.
14255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case '\r':
14265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case '\n':
14275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Okay, we found the end of the line. First, back up past the \0, \r, \n.
14285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(CurPtr[-1] == Char && "Trigraphs for newline?");
14295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      BufferPtr = CurPtr-1;
14301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Next, lex the character, which should handle the EOM transition.
14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Lex(Tmp);
143322f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner      assert(Tmp.is(tok::eom) && "Unexpected token!");
14341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Finally, we're done, return the string we found.
14365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return Result;
14375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
14385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexEndOfFile - CurPtr points to the end of this file.  Handle this
14425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// condition, reporting diagnostics and handling other edge cases as required.
14435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This returns true if Result contains a token, false if PP.Lex should be
14445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// called again.
1445d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnerbool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
14465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we hit the end of the file while parsing a preprocessor directive,
14475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // end the preprocessor directive first.  The next token returned will
14485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // then be the end of file.
14495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ParsingPreprocessorDirective) {
14505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Done parsing the "line".
14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ParsingPreprocessorDirective = false;
14525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Update the location of token as well as BufferPtr.
14539e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    FormTokenWithChars(Result, CurPtr, tok::eom);
14541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Restore comment saving mode, in case it was disabled for directive.
1456f744d133e8d36eec65828d046ee695bda238125cChris Lattner    SetCommentRetentionState(PP->getCommentRetentionState());
14575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return true;  // Have a token.
14581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
145986d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor
146086d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  // If we are in raw mode, return this event as an EOF token.  Let the caller
146186d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  // that put us in raw mode handle the event.
146286d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  if (isLexingRawMode()) {
146386d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    Result.startToken();
146486d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    BufferPtr = BufferEnd;
146586d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    FormTokenWithChars(Result, BufferEnd, tok::eof);
146686d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor    return true;
146786d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  }
146886d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor
146986d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  // Otherwise, check if we are code-completing, then issue diagnostics for
147086d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor  // unterminated #if and missing newline.
14715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14722968442603b029949246467253eeac8139a5b6d8Douglas Gregor  if (PP && PP->isCodeCompletionFile(FileLoc)) {
14732968442603b029949246467253eeac8139a5b6d8Douglas Gregor    // We're at the end of the file, but we've been asked to consider the
14742968442603b029949246467253eeac8139a5b6d8Douglas Gregor    // end of the file to be a code-completion token. Return the
14752968442603b029949246467253eeac8139a5b6d8Douglas Gregor    // code-completion token.
14762968442603b029949246467253eeac8139a5b6d8Douglas Gregor    Result.startToken();
14772968442603b029949246467253eeac8139a5b6d8Douglas Gregor    FormTokenWithChars(Result, CurPtr, tok::code_completion);
147886d9a52c24d390631a888d4ff812e1b15445e0a0Douglas Gregor
14792968442603b029949246467253eeac8139a5b6d8Douglas Gregor    // Only do the eof -> code_completion translation once.
14802968442603b029949246467253eeac8139a5b6d8Douglas Gregor    PP->SetCodeCompletionPoint(0, 0, 0);
1481dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor
1482dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    // Silence any diagnostics that occur once we hit the code-completion point.
1483dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    PP->getDiagnostics().setSuppressAllDiagnostics(true);
14842968442603b029949246467253eeac8139a5b6d8Douglas Gregor    return true;
148581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  }
148681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
14875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If we are in a #if directive, emit an error.
14885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (!ConditionalStack.empty()) {
148930c6476e8ef00caeb3a10ea0228165013a4eb3f3Chris Lattner    PP->Diag(ConditionalStack.back().IfLoc,
149030c6476e8ef00caeb3a10ea0228165013a4eb3f3Chris Lattner             diag::err_pp_unterminated_conditional);
14915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConditionalStack.pop_back();
14925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1494b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner  // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1495b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner  // a pedwarn.
1496b25e5d79d9d1967df058a242e96a62d0d0ace074Chris Lattner  if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
149720d0ee5d23c0f0e879ce1711d11df3118b3fb033Mike Stump    Diag(BufferEnd, diag::ext_no_newline_eof)
1498849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n");
14991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = CurPtr;
15015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Finally, let the preprocessor handle this.
1503168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  return PP->HandleEndOfFile(Result);
15045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
15075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified lexer will return a tok::l_paren token, 0 if it is something
15085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// else and 2 if there are no more tokens in the buffer controlled by the
15095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// lexer.
15105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerunsigned Lexer::isNextPPTokenLParen() {
15115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
15121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Switch to 'skipping' mode.  This will ensure that we can lex a token
15145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // without emitting diagnostics, disables macro expansion, and will cause EOF
15155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // to return an EOF token instead of popping the include stack.
15165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexingRawMode = true;
15171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Save state that can be changed while lexing so that we can restore it.
15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *TmpBufferPtr = BufferPtr;
1520a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner  bool inPPDirectiveMode = ParsingPreprocessorDirective;
15211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1522d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Token Tok;
15235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Tok.startToken();
15245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexTokenInternal(Tok);
15251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Restore state that may have changed.
15275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BufferPtr = TmpBufferPtr;
1528a864cf7c1d774a0f790bfc46befc87d9dbf1f65cChris Lattner  ParsingPreprocessorDirective = inPPDirectiveMode;
15291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Restore the lexer back to non-skipping mode.
15315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LexingRawMode = false;
15321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
153322f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner  if (Tok.is(tok::eof))
15345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return 2;
153522f6bbcafa8871f4f20c4402d9cbc5c024fee99aChris Lattner  return Tok.is(tok::l_paren);
15365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
153834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// FindConflictEnd - Find the end of a version control conflict marker.
153934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerstatic const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) {
154034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  llvm::StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7);
154134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  size_t Pos = RestOfBuffer.find(">>>>>>>");
154234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  while (Pos != llvm::StringRef::npos) {
154334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Must occur at start of line.
154434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    if (RestOfBuffer[Pos-1] != '\r' &&
154534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        RestOfBuffer[Pos-1] != '\n') {
154634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      RestOfBuffer = RestOfBuffer.substr(Pos+7);
15473d488990e13835cb35538b2f0cd53dbda92e36beChris Lattner      Pos = RestOfBuffer.find(">>>>>>>");
154834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      continue;
154934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    }
155034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return RestOfBuffer.data()+Pos;
155134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  }
155234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  return 0;
155334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner}
155434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
155534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// IsStartOfConflictMarker - If the specified pointer is the start of a version
155634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// control conflict marker like '<<<<<<<', recognize it as such, emit an error
155734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// and recover nicely.  This returns true if it is a conflict marker and false
155834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// if not.
155934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
156034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // Only a conflict marker if it starts at the beginning of a line.
156134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (CurPtr != BufferStart &&
156234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
156334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
156434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
156534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // Check to see if we have <<<<<<<.
156634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (BufferEnd-CurPtr < 8 ||
156734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      llvm::StringRef(CurPtr, 7) != "<<<<<<<")
156834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
156934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
157034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // If we have a situation where we don't care about conflict markers, ignore
157134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // it.
157234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (IsInConflictMarker || isLexingRawMode())
157334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
157434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
157534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // Check to see if there is a >>>>>>> somewhere in the buffer at the start of
157634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // a line to terminate this conflict marker.
15773d488990e13835cb35538b2f0cd53dbda92e36beChris Lattner  if (FindConflictEnd(CurPtr, BufferEnd)) {
157834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // We found a match.  We are really in a conflict marker.
157934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Diagnose this, and ignore to the end of line.
158034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    Diag(CurPtr, diag::err_conflict_marker);
158134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    IsInConflictMarker = true;
158234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
158334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Skip ahead to the end of line.  We know this exists because the
158434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // end-of-conflict marker starts with \r or \n.
158534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    while (*CurPtr != '\r' && *CurPtr != '\n') {
158634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      assert(CurPtr != BufferEnd && "Didn't find end of line");
158734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      ++CurPtr;
158834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    }
158934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    BufferPtr = CurPtr;
159034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return true;
159134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  }
159234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
159334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // No end of conflict marker found.
159434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  return false;
159534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner}
159634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
159734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
159834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>'
159934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// marker, then it is the end of a conflict marker.  Handle it by ignoring up
160034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// until the end of the line.  This returns true if it is a conflict marker and
160134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner/// false if not.
160234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattnerbool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
160334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // Only a conflict marker if it starts at the beginning of a line.
160434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (CurPtr != BufferStart &&
160534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
160634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
160734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
160834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // If we have a situation where we don't care about conflict markers, ignore
160934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // it.
161034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (!IsInConflictMarker || isLexingRawMode())
161134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return false;
161234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
161334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // Check to see if we have the marker (7 characters in a row).
161434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  for (unsigned i = 1; i != 7; ++i)
161534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    if (CurPtr[i] != CurPtr[0])
161634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      return false;
161734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
161834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // If we do have it, search for the end of the conflict marker.  This could
161934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // fail if it got skipped with a '#if 0' or something.  Note that CurPtr might
162034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  // be the end of conflict marker.
162134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) {
162234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    CurPtr = End;
162334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
162434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // Skip ahead to the end of line.
162534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
162634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      ++CurPtr;
162734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
162834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    BufferPtr = CurPtr;
162934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
163034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    // No longer in the conflict marker.
163134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    IsInConflictMarker = false;
163234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner    return true;
163334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  }
163434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
163534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  return false;
163634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner}
163734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
16385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// LexTokenInternal - This implements a simple C family lexer.  It is an
16405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// extremely performance critical piece of code.  This assumes that the buffer
1641efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// has a null character at the end of the file.  This returns a preprocessing
1642efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// token, not a normal token, as such, it is an internal interface.  It assumes
1643efb173ddd95325c7fd3c15070392b27c07a49a85Chris Lattner/// that the Flags of result have been cleared before calling this.
1644d217773f106856a11879ec79dc468efefaf2ee75Chris Lattnervoid Lexer::LexTokenInternal(Token &Result) {
16455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerLexNextToken:
16465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // New token, can't need cleaning yet.
1647d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  Result.clearFlag(Token::NeedsCleaning);
16485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Result.setIdentifierInfo(0);
16491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // CurPtr - Cache BufferPtr in an automatic variable.
16515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *CurPtr = BufferPtr;
16525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Small amounts of horizontal whitespace is very common between tokens.
16545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
16555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ++CurPtr;
16565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while ((*CurPtr == ' ') || (*CurPtr == '\t'))
16575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++CurPtr;
16581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1659d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // If we are keeping whitespace and other tokens, just return what we just
1660d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // skipped.  The next lexer invocation will return the token after the
1661d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    // whitespace.
1662d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (isKeepWhitespaceMode()) {
16639e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      FormTokenWithChars(Result, CurPtr, tok::unknown);
1664d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return;
1665d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    }
16661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    BufferPtr = CurPtr;
1668d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
16695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned SizeTmp, SizeTmp2;   // Temporaries for use in cases below.
16721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Read a character, advancing over it.
16745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  char Char = getAndAdvanceChar(CurPtr, Result);
16759e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  tok::TokenKind Kind;
16761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (Char) {
16785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 0:  // Null.
16795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Found end of file?
16805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurPtr-1 == BufferEnd) {
16815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Read the PP instance variable into an automatic variable, because
16825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // LexEndOfFile will often delete 'this'.
1683168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      Preprocessor *PPCache = PP;
16845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (LexEndOfFile(Result, CurPtr-1))  // Retreat back into the file.
16855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        return;   // Got a token to return.
1686168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1687168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner      return PPCache->Lex(Result);
16885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
16891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
169074d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner    if (!isLexingRawMode())
169174d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      Diag(CurPtr-1, diag::null_in_file);
1692d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
1693d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
1694d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
16951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
1697a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner
1698a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner  case 26:  // DOS & CP/M EOF: "^Z".
1699a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    // If we're in Microsoft extensions mode, treat this as end of file.
1700a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    if (Features.Microsoft) {
1701a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      // Read the PP instance variable into an automatic variable, because
1702a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      // LexEndOfFile will often delete 'this'.
1703a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      Preprocessor *PPCache = PP;
1704a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      if (LexEndOfFile(Result, CurPtr-1))  // Retreat back into the file.
1705a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner        return;   // Got a token to return.
1706a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1707a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner      return PPCache->Lex(Result);
1708a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    }
1709a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    // If Microsoft extensions are disabled, this is just random garbage.
1710a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    Kind = tok::unknown;
1711a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner    break;
1712a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner
17135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\n':
17145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\r':
17155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If we are inside a preprocessor directive and we see the end of line,
17165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // we know we are done with the directive, so return an EOM token.
17175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingPreprocessorDirective) {
17185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Done parsing the "line".
17195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ParsingPreprocessorDirective = false;
17201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Restore comment saving mode, in case it was disabled for directive.
1722f744d133e8d36eec65828d046ee695bda238125cChris Lattner      SetCommentRetentionState(PP->getCommentRetentionState());
17231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Since we consumed a newline, we are back at the start of a line.
17255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      IsAtStartOfLine = true;
17261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17279e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::eom;
17285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
17295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
17305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // The returned token is at the start of the line.
1731d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::StartOfLine);
17325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // No leading whitespace seen so far.
1733d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.clearFlag(Token::LeadingSpace);
17341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1735d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
1736d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
17375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
17385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ' ':
17395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\t':
17405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\f':
17415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\v':
17428133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner  SkipHorizontalWhitespace:
1743d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner    Result.setFlag(Token::LeadingSpace);
1744d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    if (SkipWhitespace(Result, CurPtr))
1745d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner      return; // KeepWhitespaceMode
17468133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner
17478133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner  SkipIgnoredUnits:
17488133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    CurPtr = BufferPtr;
17491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17508133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    // If the next token is obviously a // or /* */ comment, skip it efficiently
17518133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    // too (without going through the big switch stmt).
17528402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
17538402c73dd880e8af46c826d873681820aebe32ebChris Lattner        Features.BCPLComment) {
1754046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner      if (SkipBCPLComment(Result, CurPtr+2))
1755046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner        return; // There is a token to return.
17568133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipIgnoredUnits;
1757fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner    } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
1758046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner      if (SkipBlockComment(Result, CurPtr+2))
1759046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner        return; // There is a token to return.
17608133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipIgnoredUnits;
17618133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    } else if (isHorizontalWhitespace(*CurPtr)) {
17628133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner      goto SkipHorizontalWhitespace;
17638133cfcc0b672cc86d1ef73ce0c61eccecdb3690Chris Lattner    }
17645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    goto LexNextToken;   // GCC isn't tail call eliminating.
1765a2bf105739cef7aea8e4d219629c627fcd11742eChris Lattner
17663a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  // C99 6.4.4.1: Integer Constants.
17673a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  // C99 6.4.4.2: Floating Constants.
17683a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '0': case '1': case '2': case '3': case '4':
17693a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '5': case '6': case '7': case '8': case '9':
17703a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    // Notify MIOpt that we read a non-whitespace/non-comment token.
17713a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    MIOpt.ReadToken();
17723a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    return LexNumericConstant(Result, CurPtr);
17731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17743a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case 'L':   // Identifier (Loony) or wide literal (L'x' or L"xyz").
17755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
17765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
17775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
17785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Wide string literal.
17805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '"')
17815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
17825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              true);
17835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Wide character constant.
17855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '\'')
17865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
17875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FALL THROUGH, treating L like the start of an identifier.
17881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.2: Identifiers.
17905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
17915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'H': case 'I': case 'J': case 'K':    /*'L'*/case 'M': case 'N':
17925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
17935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'V': case 'W': case 'X': case 'Y': case 'Z':
17945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
17955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
17965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
17975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case 'v': case 'w': case 'x': case 'y': case 'z':
17985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '_':
17995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
18005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
18015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexIdentifier(Result, CurPtr);
18023a5707766850f9ee9daa35299794328b5caf96dcChris Lattner
18033a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '$':   // $ in identifiers.
18043a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    if (Features.DollarIdents) {
180574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
180674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(CurPtr-1, diag::ext_dollar_in_identifier);
18073a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      // Notify MIOpt that we read a non-whitespace/non-comment token.
18083a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      MIOpt.ReadToken();
18093a5707766850f9ee9daa35299794328b5caf96dcChris Lattner      return LexIdentifier(Result, CurPtr);
18103a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    }
18111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18129e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::unknown;
18133a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    break;
18141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.4: Character Constants.
18165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\'':
18175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
18185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
18195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexCharConstant(Result, CurPtr);
18205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.5: String Literals.
18225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '"':
18235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Notify MIOpt that we read a non-whitespace/non-comment token.
18245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MIOpt.ReadToken();
18255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return LexStringLiteral(Result, CurPtr, false);
18265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.4.6: Punctuators.
18285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '?':
18299e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::question;
18305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
18315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '[':
18329e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_square;
18335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
18345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ']':
18359e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_square;
18365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
18375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '(':
18389e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_paren;
18395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
18405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ')':
18419e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_paren;
18425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
18435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '{':
18449e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::l_brace;
18455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
18465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '}':
18479e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::r_brace;
18485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
18495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '.':
18505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
18515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char >= '0' && Char <= '9') {
18525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Notify MIOpt that we read a non-whitespace/non-comment token.
18535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      MIOpt.ReadToken();
18545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
18565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Features.CPlusPlus && Char == '*') {
18579e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::periodstar;
18585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr += SizeTmp;
18595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '.' &&
18605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer               getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
18619e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ellipsis;
18625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
18635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                           SizeTmp2, Result);
18645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
18659e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::period;
18665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
18675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
18685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '&':
18695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
18705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '&') {
18719e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ampamp;
18725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
18735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
18749e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::ampequal;
18755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
18765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
18779e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::amp;
18785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
18795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
18801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case '*':
18815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (getCharAndSize(CurPtr, SizeTmp) == '=') {
18829e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::starequal;
18835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
18845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
18859e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::star;
18865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
18875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
18885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '+':
18895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
18905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '+') {
18915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
18929e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plusplus;
18935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
18945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
18959e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plusequal;
18965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
18979e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::plus;
18985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
18995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
19005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '-':
19015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
19029e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    if (Char == '-') {      // --
19035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
19049e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minusminus;
19051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (Char == '>' && Features.CPlusPlus &&
19069e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner               getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {  // C++ ->*
19075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
19085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                           SizeTmp2, Result);
19099e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::arrowstar;
19109e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Char == '>') {   // ->
19115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
19129e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::arrow;
19139e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Char == '=') {   // -=
19145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
19159e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minusequal;
19165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
19179e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::minus;
19185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
19195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
19205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '~':
19219e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::tilde;
19225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
19235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '!':
19245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (getCharAndSize(CurPtr, SizeTmp) == '=') {
19259e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::exclaimequal;
19265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
19275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
19289e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::exclaim;
19295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
19305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
19315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '/':
19325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // 6.4.9: Comments
19335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
19345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '/') {         // BCPL comment.
19358402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
19368402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // want to lex this as a comment.  There is one problem with this though,
19378402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // that in one particular corner case, this can change the behavior of the
19388402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // resultant program.  For example, In  "foo //**/ bar", C89 would lex
19398402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // this as "foo / bar" and langauges with BCPL comments would lex it as
19408402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // "foo".  Check to see if the character after the second slash is a '*'.
19418402c73dd880e8af46c826d873681820aebe32ebChris Lattner      // If so, we will lex that as a "/" instead of the start of a comment.
19428402c73dd880e8af46c826d873681820aebe32ebChris Lattner      if (Features.BCPLComment ||
19438402c73dd880e8af46c826d873681820aebe32ebChris Lattner          getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
19448402c73dd880e8af46c826d873681820aebe32ebChris Lattner        if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1945046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner          return; // There is a token to return.
19461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19478402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // It is common for the tokens immediately after a // comment to be
19488402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // whitespace (indentation for the next line).  Instead of going through
19498402c73dd880e8af46c826d873681820aebe32ebChris Lattner        // the big switch, handle it efficiently now.
19508402c73dd880e8af46c826d873681820aebe32ebChris Lattner        goto SkipIgnoredUnits;
19518402c73dd880e8af46c826d873681820aebe32ebChris Lattner      }
19528402c73dd880e8af46c826d873681820aebe32ebChris Lattner    }
19531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19548402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (Char == '*') {  // /**/ comment.
19555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1956046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner        return; // There is a token to return.
19572d3818959df7b71bfcf8c2f2b573856bfc4c3a21Chris Lattner      goto LexNextToken;   // GCC isn't tail call eliminating.
19588402c73dd880e8af46c826d873681820aebe32ebChris Lattner    }
19591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19608402c73dd880e8af46c826d873681820aebe32ebChris Lattner    if (Char == '=') {
19615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
19629e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::slashequal;
19635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
19649e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::slash;
19655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
19665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
19675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '%':
19685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
19695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
19709e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::percentequal;
19715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
19725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Features.Digraphs && Char == '>') {
19739e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::r_brace;                             // '%>' -> '}'
19745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
19755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Features.Digraphs && Char == ':') {
19765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
19775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Char = getCharAndSize(CurPtr, SizeTmp);
19785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
19799e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        Kind = tok::hashhash;                          // '%:%:' -> '##'
19805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
19815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                             SizeTmp2, Result);
19825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      } else if (Char == '@' && Features.Microsoft) {  // %:@ -> #@ -> Charize
19835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
198474d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        if (!isLexingRawMode())
198574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner          Diag(BufferPtr, diag::charize_microsoft_ext);
19869e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner        Kind = tok::hashat;
1987e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner      } else {                                         // '%:' -> '#'
19885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // We parsed a # character.  If this occurs at the start of the line,
19895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // it's actually the start of a preprocessing directive.  Callback to
19905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // the preprocessor to handle it.
19915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // FIXME: -fpreprocessed mode??
1992766703b76dc1eb0c49d11cafc2a17c7407876ebcChris Lattner        if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
1993e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner          FormTokenWithChars(Result, CurPtr, tok::hash);
1994168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner          PP->HandleDirective(Result);
19951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // As an optimization, if the preprocessor didn't switch lexers, tail
19975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // recurse.
1998168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner          if (PP->isCurrentLexer(this)) {
19995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // Start a new token. If this is a #include or something, the PP may
20005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            // want us starting at the beginning of the line again.  If so, set
2001515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner            // the StartOfLine flag and clear LeadingSpace.
20025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            if (IsAtStartOfLine) {
2003d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner              Result.setFlag(Token::StartOfLine);
2004515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner              Result.clearFlag(Token::LeadingSpace);
20055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer              IsAtStartOfLine = false;
20065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            }
20075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            goto LexNextToken;   // GCC isn't tail call eliminating.
20085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
20091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2010168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner          return PP->Lex(Result);
20115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
20121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2013e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner        Kind = tok::hash;
20145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
20155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
20169e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::percent;
20175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
20185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
20195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '<':
20205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
20215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (ParsingFilename) {
20229cb51ce03caf0755759f7e67aea7d4be684ff738Chris Lattner      return LexAngledStringLiteral(Result, CurPtr);
20235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '<') {
202434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
202534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (After == '=') {
202634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::lesslessequal;
202734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
202834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner                             SizeTmp2, Result);
202934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
203034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        // If this is actually a '<<<<<<<' version control conflict marker,
203134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        // recognize it as such and recover nicely.
203234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
203334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else {
203434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
203534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::lessless;
203634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      }
20375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '=') {
20385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
20399e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::lessequal;
20409e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Features.Digraphs && Char == ':') {     // '<:' -> '['
20415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
20429e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::l_square;
20439e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    } else if (Features.Digraphs && Char == '%') {     // '<%' -> '{'
20445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
20459e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::l_brace;
20465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
20479e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::less;
20485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
20495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
20505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '>':
20515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
20525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
20535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
20549e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::greaterequal;
20555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '>') {
205634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
205734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (After == '=') {
205834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
205934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner                             SizeTmp2, Result);
206034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::greatergreaterequal;
206134f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
206234f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        // If this is '>>>>>>>' and we're in a conflict marker, ignore it.
206334f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
206434f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      } else {
206534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
206634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        Kind = tok::greatergreater;
206734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      }
206834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
20695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
20709e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::greater;
20715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
20725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
20735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '^':
20745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
20755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
20765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
20779e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::caretequal;
20785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
20799e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::caret;
20805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
20815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
20825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '|':
20835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
20845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
20859e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipeequal;
20865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
20875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '|') {
208834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      // If this is '|||||||' and we're in a conflict marker, ignore it.
208934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
209034f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
20919e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipepipe;
20925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
20935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
20949e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::pipe;
20955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
20965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
20975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ':':
20985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
20995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Features.Digraphs && Char == '>') {
21009e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::r_square; // ':>' -> ']'
21015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
21025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Features.CPlusPlus && Char == ':') {
21039e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::coloncolon;
21045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
21051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else {
21069e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::colon;
21075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
21085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ';':
21109e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::semi;
21115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '=':
21135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
21145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '=') {
211534f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      // If this is '=======' and we're in a conflict marker, ignore it.
211634f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner      if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
211734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner        goto LexNextToken;
211834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
21199e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::equalequal;
21205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
21211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else {
21229e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::equal;
21235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
21245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ',':
21269e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::comma;
21275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '#':
21295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Char = getCharAndSize(CurPtr, SizeTmp);
21305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Char == '#') {
21319e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::hashhash;
21325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
21335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else if (Char == '@' && Features.Microsoft) {  // #@ -> Charize
21349e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::hashat;
213574d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner      if (!isLexingRawMode())
213674d15dfd183b2082e8a5d4dfbf66bd861b220901Chris Lattner        Diag(BufferPtr, diag::charize_microsoft_ext);
21375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
21385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
21395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // We parsed a # character.  If this occurs at the start of the line,
21405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // it's actually the start of a preprocessing directive.  Callback to
21415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // the preprocessor to handle it.
21425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // FIXME: -fpreprocessed mode??
2143766703b76dc1eb0c49d11cafc2a17c7407876ebcChris Lattner      if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
2144e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner        FormTokenWithChars(Result, CurPtr, tok::hash);
2145168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner        PP->HandleDirective(Result);
21461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // As an optimization, if the preprocessor didn't switch lexers, tail
21485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        // recurse.
2149168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner        if (PP->isCurrentLexer(this)) {
21505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // Start a new token.  If this is a #include or something, the PP may
21515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          // want us starting at the beginning of the line again.  If so, set
2152515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner          // the StartOfLine flag and clear LeadingSpace.
21535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          if (IsAtStartOfLine) {
2154d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner            Result.setFlag(Token::StartOfLine);
2155515f43f9f23de50d155b481b8774ec40bdfd7ff2Chris Lattner            Result.clearFlag(Token::LeadingSpace);
21565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer            IsAtStartOfLine = false;
21575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          }
21585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer          goto LexNextToken;   // GCC isn't tail call eliminating.
21595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        }
2160168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner        return PP->Lex(Result);
21615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
21621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2163e91e93225db2e66906878513c6ef4dd6a7ee2b6aChris Lattner      Kind = tok::hash;
21645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
21655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21673a5707766850f9ee9daa35299794328b5caf96dcChris Lattner  case '@':
21683a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    // Objective C support.
21693a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    if (CurPtr[-1] == '@' && Features.ObjC1)
21709e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::at;
21713a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    else
21729e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner      Kind = tok::unknown;
21733a5707766850f9ee9daa35299794328b5caf96dcChris Lattner    break;
21741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case '\\':
21765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FIXME: UCN's.
21775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // FALL THROUGH.
21785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
21799e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Kind = tok::unknown;
21805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
21815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
21821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Notify MIOpt that we read a non-whitespace/non-comment token.
21845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  MIOpt.ReadToken();
21855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Update the location of token as well as BufferPtr.
21879e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner  FormTokenWithChars(Result, CurPtr, Kind);
21885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2189