xref: /llvm-project-15.0.7/clang/lib/Lex/Lexer.cpp (revision 0cfd0e98)
1 //===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the Lexer and Token interfaces.
11 //
12 //===----------------------------------------------------------------------===//
13 //
14 // TODO: GCC Diagnostics emitted by the lexer:
15 // PEDWARN: (form feed|vertical tab) in preprocessing directive
16 //
17 // Universal characters, unicode, char mapping:
18 // WARNING: `%.*s' is not in NFKC
19 // WARNING: `%.*s' is not in NFC
20 //
21 // Other:
22 // TODO: Options to support:
23 //    -fexec-charset,-fwide-exec-charset
24 //
25 //===----------------------------------------------------------------------===//
26 
27 #include "clang/Lex/Lexer.h"
28 #include "UnicodeCharSets.h"
29 #include "clang/Basic/CharInfo.h"
30 #include "clang/Basic/SourceManager.h"
31 #include "clang/Lex/CodeCompletionHandler.h"
32 #include "clang/Lex/LexDiagnostic.h"
33 #include "clang/Lex/LiteralSupport.h"
34 #include "clang/Lex/Preprocessor.h"
35 #include "llvm/ADT/STLExtras.h"
36 #include "llvm/ADT/StringExtras.h"
37 #include "llvm/ADT/StringSwitch.h"
38 #include "llvm/Support/Compiler.h"
39 #include "llvm/Support/ConvertUTF.h"
40 #include "llvm/Support/MemoryBuffer.h"
41 #include <cstring>
42 using namespace clang;
43 
44 //===----------------------------------------------------------------------===//
45 // Token Class Implementation
46 //===----------------------------------------------------------------------===//
47 
48 /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
49 bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
50   if (IdentifierInfo *II = getIdentifierInfo())
51     return II->getObjCKeywordID() == objcKey;
52   return false;
53 }
54 
55 /// getObjCKeywordID - Return the ObjC keyword kind.
56 tok::ObjCKeywordKind Token::getObjCKeywordID() const {
57   IdentifierInfo *specId = getIdentifierInfo();
58   return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
59 }
60 
61 
62 //===----------------------------------------------------------------------===//
63 // Lexer Class Implementation
64 //===----------------------------------------------------------------------===//
65 
66 void Lexer::anchor() { }
67 
68 void Lexer::InitLexer(const char *BufStart, const char *BufPtr,
69                       const char *BufEnd) {
70   BufferStart = BufStart;
71   BufferPtr = BufPtr;
72   BufferEnd = BufEnd;
73 
74   assert(BufEnd[0] == 0 &&
75          "We assume that the input buffer has a null character at the end"
76          " to simplify lexing!");
77 
78   // Check whether we have a BOM in the beginning of the buffer. If yes - act
79   // accordingly. Right now we support only UTF-8 with and without BOM, so, just
80   // skip the UTF-8 BOM if it's present.
81   if (BufferStart == BufferPtr) {
82     // Determine the size of the BOM.
83     StringRef Buf(BufferStart, BufferEnd - BufferStart);
84     size_t BOMLength = llvm::StringSwitch<size_t>(Buf)
85       .StartsWith("\xEF\xBB\xBF", 3) // UTF-8 BOM
86       .Default(0);
87 
88     // Skip the BOM.
89     BufferPtr += BOMLength;
90   }
91 
92   Is_PragmaLexer = false;
93   CurrentConflictMarkerState = CMK_None;
94 
95   // Start of the file is a start of line.
96   IsAtStartOfLine = true;
97   IsAtPhysicalStartOfLine = true;
98 
99   HasLeadingSpace = false;
100   HasLeadingEmptyMacro = false;
101 
102   // We are not after parsing a #.
103   ParsingPreprocessorDirective = false;
104 
105   // We are not after parsing #include.
106   ParsingFilename = false;
107 
108   // We are not in raw mode.  Raw mode disables diagnostics and interpretation
109   // of tokens (e.g. identifiers, thus disabling macro expansion).  It is used
110   // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
111   // or otherwise skipping over tokens.
112   LexingRawMode = false;
113 
114   // Default to not keeping comments.
115   ExtendedTokenMode = 0;
116 }
117 
118 /// Lexer constructor - Create a new lexer object for the specified buffer
119 /// with the specified preprocessor managing the lexing process.  This lexer
120 /// assumes that the associated file buffer and Preprocessor objects will
121 /// outlive it, so it doesn't take ownership of either of them.
122 Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP)
123   : PreprocessorLexer(&PP, FID),
124     FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
125     LangOpts(PP.getLangOpts()) {
126 
127   InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
128             InputFile->getBufferEnd());
129 
130   resetExtendedTokenMode();
131 }
132 
133 void Lexer::resetExtendedTokenMode() {
134   assert(PP && "Cannot reset token mode without a preprocessor");
135   if (LangOpts.TraditionalCPP)
136     SetKeepWhitespaceMode(true);
137   else
138     SetCommentRetentionState(PP->getCommentRetentionState());
139 }
140 
141 /// Lexer constructor - Create a new raw lexer object.  This object is only
142 /// suitable for calls to 'LexFromRawLexer'.  This lexer assumes that the text
143 /// range will outlive it, so it doesn't take ownership of it.
144 Lexer::Lexer(SourceLocation fileloc, const LangOptions &langOpts,
145              const char *BufStart, const char *BufPtr, const char *BufEnd)
146   : FileLoc(fileloc), LangOpts(langOpts) {
147 
148   InitLexer(BufStart, BufPtr, BufEnd);
149 
150   // We *are* in raw mode.
151   LexingRawMode = true;
152 }
153 
154 /// Lexer constructor - Create a new raw lexer object.  This object is only
155 /// suitable for calls to 'LexFromRawLexer'.  This lexer assumes that the text
156 /// range will outlive it, so it doesn't take ownership of it.
157 Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile,
158              const SourceManager &SM, const LangOptions &langOpts)
159   : FileLoc(SM.getLocForStartOfFile(FID)), LangOpts(langOpts) {
160 
161   InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
162             FromFile->getBufferEnd());
163 
164   // We *are* in raw mode.
165   LexingRawMode = true;
166 }
167 
168 /// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
169 /// _Pragma expansion.  This has a variety of magic semantics that this method
170 /// sets up.  It returns a new'd Lexer that must be delete'd when done.
171 ///
172 /// On entrance to this routine, TokStartLoc is a macro location which has a
173 /// spelling loc that indicates the bytes to be lexed for the token and an
174 /// expansion location that indicates where all lexed tokens should be
175 /// "expanded from".
176 ///
177 /// FIXME: It would really be nice to make _Pragma just be a wrapper around a
178 /// normal lexer that remaps tokens as they fly by.  This would require making
179 /// Preprocessor::Lex virtual.  Given that, we could just dump in a magic lexer
180 /// interface that could handle this stuff.  This would pull GetMappedTokenLoc
181 /// out of the critical path of the lexer!
182 ///
183 Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
184                                  SourceLocation ExpansionLocStart,
185                                  SourceLocation ExpansionLocEnd,
186                                  unsigned TokLen, Preprocessor &PP) {
187   SourceManager &SM = PP.getSourceManager();
188 
189   // Create the lexer as if we were going to lex the file normally.
190   FileID SpellingFID = SM.getFileID(SpellingLoc);
191   const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID);
192   Lexer *L = new Lexer(SpellingFID, InputFile, PP);
193 
194   // Now that the lexer is created, change the start/end locations so that we
195   // just lex the subsection of the file that we want.  This is lexing from a
196   // scratch buffer.
197   const char *StrData = SM.getCharacterData(SpellingLoc);
198 
199   L->BufferPtr = StrData;
200   L->BufferEnd = StrData+TokLen;
201   assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
202 
203   // Set the SourceLocation with the remapping information.  This ensures that
204   // GetMappedTokenLoc will remap the tokens as they are lexed.
205   L->FileLoc = SM.createExpansionLoc(SM.getLocForStartOfFile(SpellingFID),
206                                      ExpansionLocStart,
207                                      ExpansionLocEnd, TokLen);
208 
209   // Ensure that the lexer thinks it is inside a directive, so that end \n will
210   // return an EOD token.
211   L->ParsingPreprocessorDirective = true;
212 
213   // This lexer really is for _Pragma.
214   L->Is_PragmaLexer = true;
215   return L;
216 }
217 
218 
219 /// Stringify - Convert the specified string into a C string, with surrounding
220 /// ""'s, and with escaped \ and " characters.
221 std::string Lexer::Stringify(const std::string &Str, bool Charify) {
222   std::string Result = Str;
223   char Quote = Charify ? '\'' : '"';
224   for (unsigned i = 0, e = Result.size(); i != e; ++i) {
225     if (Result[i] == '\\' || Result[i] == Quote) {
226       Result.insert(Result.begin()+i, '\\');
227       ++i; ++e;
228     }
229   }
230   return Result;
231 }
232 
233 /// Stringify - Convert the specified string into a C string by escaping '\'
234 /// and " characters.  This does not add surrounding ""'s to the string.
235 void Lexer::Stringify(SmallVectorImpl<char> &Str) {
236   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
237     if (Str[i] == '\\' || Str[i] == '"') {
238       Str.insert(Str.begin()+i, '\\');
239       ++i; ++e;
240     }
241   }
242 }
243 
244 //===----------------------------------------------------------------------===//
245 // Token Spelling
246 //===----------------------------------------------------------------------===//
247 
248 /// \brief Slow case of getSpelling. Extract the characters comprising the
249 /// spelling of this token from the provided input buffer.
250 static size_t getSpellingSlow(const Token &Tok, const char *BufPtr,
251                               const LangOptions &LangOpts, char *Spelling) {
252   assert(Tok.needsCleaning() && "getSpellingSlow called on simple token");
253 
254   size_t Length = 0;
255   const char *BufEnd = BufPtr + Tok.getLength();
256 
257   if (Tok.is(tok::string_literal)) {
258     // Munch the encoding-prefix and opening double-quote.
259     while (BufPtr < BufEnd) {
260       unsigned Size;
261       Spelling[Length++] = Lexer::getCharAndSizeNoWarn(BufPtr, Size, LangOpts);
262       BufPtr += Size;
263 
264       if (Spelling[Length - 1] == '"')
265         break;
266     }
267 
268     // Raw string literals need special handling; trigraph expansion and line
269     // splicing do not occur within their d-char-sequence nor within their
270     // r-char-sequence.
271     if (Length >= 2 &&
272         Spelling[Length - 2] == 'R' && Spelling[Length - 1] == '"') {
273       // Search backwards from the end of the token to find the matching closing
274       // quote.
275       const char *RawEnd = BufEnd;
276       do --RawEnd; while (*RawEnd != '"');
277       size_t RawLength = RawEnd - BufPtr + 1;
278 
279       // Everything between the quotes is included verbatim in the spelling.
280       memcpy(Spelling + Length, BufPtr, RawLength);
281       Length += RawLength;
282       BufPtr += RawLength;
283 
284       // The rest of the token is lexed normally.
285     }
286   }
287 
288   while (BufPtr < BufEnd) {
289     unsigned Size;
290     Spelling[Length++] = Lexer::getCharAndSizeNoWarn(BufPtr, Size, LangOpts);
291     BufPtr += Size;
292   }
293 
294   assert(Length < Tok.getLength() &&
295          "NeedsCleaning flag set on token that didn't need cleaning!");
296   return Length;
297 }
298 
299 /// getSpelling() - Return the 'spelling' of this token.  The spelling of a
300 /// token are the characters used to represent the token in the source file
301 /// after trigraph expansion and escaped-newline folding.  In particular, this
302 /// wants to get the true, uncanonicalized, spelling of things like digraphs
303 /// UCNs, etc.
304 StringRef Lexer::getSpelling(SourceLocation loc,
305                              SmallVectorImpl<char> &buffer,
306                              const SourceManager &SM,
307                              const LangOptions &options,
308                              bool *invalid) {
309   // Break down the source location.
310   std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
311 
312   // Try to the load the file buffer.
313   bool invalidTemp = false;
314   StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
315   if (invalidTemp) {
316     if (invalid) *invalid = true;
317     return StringRef();
318   }
319 
320   const char *tokenBegin = file.data() + locInfo.second;
321 
322   // Lex from the start of the given location.
323   Lexer lexer(SM.getLocForStartOfFile(locInfo.first), options,
324               file.begin(), tokenBegin, file.end());
325   Token token;
326   lexer.LexFromRawLexer(token);
327 
328   unsigned length = token.getLength();
329 
330   // Common case:  no need for cleaning.
331   if (!token.needsCleaning())
332     return StringRef(tokenBegin, length);
333 
334   // Hard case, we need to relex the characters into the string.
335   buffer.resize(length);
336   buffer.resize(getSpellingSlow(token, tokenBegin, options, buffer.data()));
337   return StringRef(buffer.data(), buffer.size());
338 }
339 
340 /// getSpelling() - Return the 'spelling' of this token.  The spelling of a
341 /// token are the characters used to represent the token in the source file
342 /// after trigraph expansion and escaped-newline folding.  In particular, this
343 /// wants to get the true, uncanonicalized, spelling of things like digraphs
344 /// UCNs, etc.
345 std::string Lexer::getSpelling(const Token &Tok, const SourceManager &SourceMgr,
346                                const LangOptions &LangOpts, bool *Invalid) {
347   assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
348 
349   bool CharDataInvalid = false;
350   const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation(),
351                                                     &CharDataInvalid);
352   if (Invalid)
353     *Invalid = CharDataInvalid;
354   if (CharDataInvalid)
355     return std::string();
356 
357   // If this token contains nothing interesting, return it directly.
358   if (!Tok.needsCleaning())
359     return std::string(TokStart, TokStart + Tok.getLength());
360 
361   std::string Result;
362   Result.resize(Tok.getLength());
363   Result.resize(getSpellingSlow(Tok, TokStart, LangOpts, &*Result.begin()));
364   return Result;
365 }
366 
367 /// getSpelling - This method is used to get the spelling of a token into a
368 /// preallocated buffer, instead of as an std::string.  The caller is required
369 /// to allocate enough space for the token, which is guaranteed to be at least
370 /// Tok.getLength() bytes long.  The actual length of the token is returned.
371 ///
372 /// Note that this method may do two possible things: it may either fill in
373 /// the buffer specified with characters, or it may *change the input pointer*
374 /// to point to a constant buffer with the data already in it (avoiding a
375 /// copy).  The caller is not allowed to modify the returned buffer pointer
376 /// if an internal buffer is returned.
377 unsigned Lexer::getSpelling(const Token &Tok, const char *&Buffer,
378                             const SourceManager &SourceMgr,
379                             const LangOptions &LangOpts, bool *Invalid) {
380   assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
381 
382   const char *TokStart = 0;
383   // NOTE: this has to be checked *before* testing for an IdentifierInfo.
384   if (Tok.is(tok::raw_identifier))
385     TokStart = Tok.getRawIdentifierData();
386   else if (!Tok.hasUCN()) {
387     if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
388       // Just return the string from the identifier table, which is very quick.
389       Buffer = II->getNameStart();
390       return II->getLength();
391     }
392   }
393 
394   // NOTE: this can be checked even after testing for an IdentifierInfo.
395   if (Tok.isLiteral())
396     TokStart = Tok.getLiteralData();
397 
398   if (TokStart == 0) {
399     // Compute the start of the token in the input lexer buffer.
400     bool CharDataInvalid = false;
401     TokStart = SourceMgr.getCharacterData(Tok.getLocation(), &CharDataInvalid);
402     if (Invalid)
403       *Invalid = CharDataInvalid;
404     if (CharDataInvalid) {
405       Buffer = "";
406       return 0;
407     }
408   }
409 
410   // If this token contains nothing interesting, return it directly.
411   if (!Tok.needsCleaning()) {
412     Buffer = TokStart;
413     return Tok.getLength();
414   }
415 
416   // Otherwise, hard case, relex the characters into the string.
417   return getSpellingSlow(Tok, TokStart, LangOpts, const_cast<char*>(Buffer));
418 }
419 
420 
421 /// MeasureTokenLength - Relex the token at the specified location and return
422 /// its length in bytes in the input file.  If the token needs cleaning (e.g.
423 /// includes a trigraph or an escaped newline) then this count includes bytes
424 /// that are part of that.
425 unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
426                                    const SourceManager &SM,
427                                    const LangOptions &LangOpts) {
428   Token TheTok;
429   if (getRawToken(Loc, TheTok, SM, LangOpts))
430     return 0;
431   return TheTok.getLength();
432 }
433 
434 /// \brief Relex the token at the specified location.
435 /// \returns true if there was a failure, false on success.
436 bool Lexer::getRawToken(SourceLocation Loc, Token &Result,
437                         const SourceManager &SM,
438                         const LangOptions &LangOpts,
439                         bool IgnoreWhiteSpace) {
440   // TODO: this could be special cased for common tokens like identifiers, ')',
441   // etc to make this faster, if it mattered.  Just look at StrData[0] to handle
442   // all obviously single-char tokens.  This could use
443   // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
444   // something.
445 
446   // If this comes from a macro expansion, we really do want the macro name, not
447   // the token this macro expanded to.
448   Loc = SM.getExpansionLoc(Loc);
449   std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
450   bool Invalid = false;
451   StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
452   if (Invalid)
453     return true;
454 
455   const char *StrData = Buffer.data()+LocInfo.second;
456 
457   if (!IgnoreWhiteSpace && isWhitespace(StrData[0]))
458     return true;
459 
460   // Create a lexer starting at the beginning of this token.
461   Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts,
462                  Buffer.begin(), StrData, Buffer.end());
463   TheLexer.SetCommentRetentionState(true);
464   TheLexer.LexFromRawLexer(Result);
465   return false;
466 }
467 
468 static SourceLocation getBeginningOfFileToken(SourceLocation Loc,
469                                               const SourceManager &SM,
470                                               const LangOptions &LangOpts) {
471   assert(Loc.isFileID());
472   std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
473   if (LocInfo.first.isInvalid())
474     return Loc;
475 
476   bool Invalid = false;
477   StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
478   if (Invalid)
479     return Loc;
480 
481   // Back up from the current location until we hit the beginning of a line
482   // (or the buffer). We'll relex from that point.
483   const char *BufStart = Buffer.data();
484   if (LocInfo.second >= Buffer.size())
485     return Loc;
486 
487   const char *StrData = BufStart+LocInfo.second;
488   if (StrData[0] == '\n' || StrData[0] == '\r')
489     return Loc;
490 
491   const char *LexStart = StrData;
492   while (LexStart != BufStart) {
493     if (LexStart[0] == '\n' || LexStart[0] == '\r') {
494       ++LexStart;
495       break;
496     }
497 
498     --LexStart;
499   }
500 
501   // Create a lexer starting at the beginning of this token.
502   SourceLocation LexerStartLoc = Loc.getLocWithOffset(-LocInfo.second);
503   Lexer TheLexer(LexerStartLoc, LangOpts, BufStart, LexStart, Buffer.end());
504   TheLexer.SetCommentRetentionState(true);
505 
506   // Lex tokens until we find the token that contains the source location.
507   Token TheTok;
508   do {
509     TheLexer.LexFromRawLexer(TheTok);
510 
511     if (TheLexer.getBufferLocation() > StrData) {
512       // Lexing this token has taken the lexer past the source location we're
513       // looking for. If the current token encompasses our source location,
514       // return the beginning of that token.
515       if (TheLexer.getBufferLocation() - TheTok.getLength() <= StrData)
516         return TheTok.getLocation();
517 
518       // We ended up skipping over the source location entirely, which means
519       // that it points into whitespace. We're done here.
520       break;
521     }
522   } while (TheTok.getKind() != tok::eof);
523 
524   // We've passed our source location; just return the original source location.
525   return Loc;
526 }
527 
528 SourceLocation Lexer::GetBeginningOfToken(SourceLocation Loc,
529                                           const SourceManager &SM,
530                                           const LangOptions &LangOpts) {
531  if (Loc.isFileID())
532    return getBeginningOfFileToken(Loc, SM, LangOpts);
533 
534  if (!SM.isMacroArgExpansion(Loc))
535    return Loc;
536 
537  SourceLocation FileLoc = SM.getSpellingLoc(Loc);
538  SourceLocation BeginFileLoc = getBeginningOfFileToken(FileLoc, SM, LangOpts);
539  std::pair<FileID, unsigned> FileLocInfo = SM.getDecomposedLoc(FileLoc);
540  std::pair<FileID, unsigned> BeginFileLocInfo
541    = SM.getDecomposedLoc(BeginFileLoc);
542  assert(FileLocInfo.first == BeginFileLocInfo.first &&
543         FileLocInfo.second >= BeginFileLocInfo.second);
544  return Loc.getLocWithOffset(BeginFileLocInfo.second - FileLocInfo.second);
545 }
546 
547 namespace {
548   enum PreambleDirectiveKind {
549     PDK_Skipped,
550     PDK_StartIf,
551     PDK_EndIf,
552     PDK_Unknown
553   };
554 }
555 
556 std::pair<unsigned, bool>
557 Lexer::ComputePreamble(const llvm::MemoryBuffer *Buffer,
558                        const LangOptions &LangOpts, unsigned MaxLines) {
559   // Create a lexer starting at the beginning of the file. Note that we use a
560   // "fake" file source location at offset 1 so that the lexer will track our
561   // position within the file.
562   const unsigned StartOffset = 1;
563   SourceLocation FileLoc = SourceLocation::getFromRawEncoding(StartOffset);
564   Lexer TheLexer(FileLoc, LangOpts, Buffer->getBufferStart(),
565                  Buffer->getBufferStart(), Buffer->getBufferEnd());
566   TheLexer.SetCommentRetentionState(true);
567 
568   // StartLoc will differ from FileLoc if there is a BOM that was skipped.
569   SourceLocation StartLoc = TheLexer.getSourceLocation();
570 
571   bool InPreprocessorDirective = false;
572   Token TheTok;
573   Token IfStartTok;
574   unsigned IfCount = 0;
575   SourceLocation ActiveCommentLoc;
576 
577   unsigned MaxLineOffset = 0;
578   if (MaxLines) {
579     const char *CurPtr = Buffer->getBufferStart();
580     unsigned CurLine = 0;
581     while (CurPtr != Buffer->getBufferEnd()) {
582       char ch = *CurPtr++;
583       if (ch == '\n') {
584         ++CurLine;
585         if (CurLine == MaxLines)
586           break;
587       }
588     }
589     if (CurPtr != Buffer->getBufferEnd())
590       MaxLineOffset = CurPtr - Buffer->getBufferStart();
591   }
592 
593   do {
594     TheLexer.LexFromRawLexer(TheTok);
595 
596     if (InPreprocessorDirective) {
597       // If we've hit the end of the file, we're done.
598       if (TheTok.getKind() == tok::eof) {
599         break;
600       }
601 
602       // If we haven't hit the end of the preprocessor directive, skip this
603       // token.
604       if (!TheTok.isAtStartOfLine())
605         continue;
606 
607       // We've passed the end of the preprocessor directive, and will look
608       // at this token again below.
609       InPreprocessorDirective = false;
610     }
611 
612     // Keep track of the # of lines in the preamble.
613     if (TheTok.isAtStartOfLine()) {
614       unsigned TokOffset = TheTok.getLocation().getRawEncoding() - StartOffset;
615 
616       // If we were asked to limit the number of lines in the preamble,
617       // and we're about to exceed that limit, we're done.
618       if (MaxLineOffset && TokOffset >= MaxLineOffset)
619         break;
620     }
621 
622     // Comments are okay; skip over them.
623     if (TheTok.getKind() == tok::comment) {
624       if (ActiveCommentLoc.isInvalid())
625         ActiveCommentLoc = TheTok.getLocation();
626       continue;
627     }
628 
629     if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) {
630       // This is the start of a preprocessor directive.
631       Token HashTok = TheTok;
632       InPreprocessorDirective = true;
633       ActiveCommentLoc = SourceLocation();
634 
635       // Figure out which directive this is. Since we're lexing raw tokens,
636       // we don't have an identifier table available. Instead, just look at
637       // the raw identifier to recognize and categorize preprocessor directives.
638       TheLexer.LexFromRawLexer(TheTok);
639       if (TheTok.getKind() == tok::raw_identifier && !TheTok.needsCleaning()) {
640         StringRef Keyword(TheTok.getRawIdentifierData(),
641                                 TheTok.getLength());
642         PreambleDirectiveKind PDK
643           = llvm::StringSwitch<PreambleDirectiveKind>(Keyword)
644               .Case("include", PDK_Skipped)
645               .Case("__include_macros", PDK_Skipped)
646               .Case("define", PDK_Skipped)
647               .Case("undef", PDK_Skipped)
648               .Case("line", PDK_Skipped)
649               .Case("error", PDK_Skipped)
650               .Case("pragma", PDK_Skipped)
651               .Case("import", PDK_Skipped)
652               .Case("include_next", PDK_Skipped)
653               .Case("warning", PDK_Skipped)
654               .Case("ident", PDK_Skipped)
655               .Case("sccs", PDK_Skipped)
656               .Case("assert", PDK_Skipped)
657               .Case("unassert", PDK_Skipped)
658               .Case("if", PDK_StartIf)
659               .Case("ifdef", PDK_StartIf)
660               .Case("ifndef", PDK_StartIf)
661               .Case("elif", PDK_Skipped)
662               .Case("else", PDK_Skipped)
663               .Case("endif", PDK_EndIf)
664               .Default(PDK_Unknown);
665 
666         switch (PDK) {
667         case PDK_Skipped:
668           continue;
669 
670         case PDK_StartIf:
671           if (IfCount == 0)
672             IfStartTok = HashTok;
673 
674           ++IfCount;
675           continue;
676 
677         case PDK_EndIf:
678           // Mismatched #endif. The preamble ends here.
679           if (IfCount == 0)
680             break;
681 
682           --IfCount;
683           continue;
684 
685         case PDK_Unknown:
686           // We don't know what this directive is; stop at the '#'.
687           break;
688         }
689       }
690 
691       // We only end up here if we didn't recognize the preprocessor
692       // directive or it was one that can't occur in the preamble at this
693       // point. Roll back the current token to the location of the '#'.
694       InPreprocessorDirective = false;
695       TheTok = HashTok;
696     }
697 
698     // We hit a token that we don't recognize as being in the
699     // "preprocessing only" part of the file, so we're no longer in
700     // the preamble.
701     break;
702   } while (true);
703 
704   SourceLocation End;
705   if (IfCount)
706     End = IfStartTok.getLocation();
707   else if (ActiveCommentLoc.isValid())
708     End = ActiveCommentLoc; // don't truncate a decl comment.
709   else
710     End = TheTok.getLocation();
711 
712   return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(),
713                         IfCount? IfStartTok.isAtStartOfLine()
714                                : TheTok.isAtStartOfLine());
715 }
716 
717 
718 /// AdvanceToTokenCharacter - Given a location that specifies the start of a
719 /// token, return a new location that specifies a character within the token.
720 SourceLocation Lexer::AdvanceToTokenCharacter(SourceLocation TokStart,
721                                               unsigned CharNo,
722                                               const SourceManager &SM,
723                                               const LangOptions &LangOpts) {
724   // Figure out how many physical characters away the specified expansion
725   // character is.  This needs to take into consideration newlines and
726   // trigraphs.
727   bool Invalid = false;
728   const char *TokPtr = SM.getCharacterData(TokStart, &Invalid);
729 
730   // If they request the first char of the token, we're trivially done.
731   if (Invalid || (CharNo == 0 && Lexer::isObviouslySimpleCharacter(*TokPtr)))
732     return TokStart;
733 
734   unsigned PhysOffset = 0;
735 
736   // The usual case is that tokens don't contain anything interesting.  Skip
737   // over the uninteresting characters.  If a token only consists of simple
738   // chars, this method is extremely fast.
739   while (Lexer::isObviouslySimpleCharacter(*TokPtr)) {
740     if (CharNo == 0)
741       return TokStart.getLocWithOffset(PhysOffset);
742     ++TokPtr, --CharNo, ++PhysOffset;
743   }
744 
745   // If we have a character that may be a trigraph or escaped newline, use a
746   // lexer to parse it correctly.
747   for (; CharNo; --CharNo) {
748     unsigned Size;
749     Lexer::getCharAndSizeNoWarn(TokPtr, Size, LangOpts);
750     TokPtr += Size;
751     PhysOffset += Size;
752   }
753 
754   // Final detail: if we end up on an escaped newline, we want to return the
755   // location of the actual byte of the token.  For example foo\<newline>bar
756   // advanced by 3 should return the location of b, not of \\.  One compounding
757   // detail of this is that the escape may be made by a trigraph.
758   if (!Lexer::isObviouslySimpleCharacter(*TokPtr))
759     PhysOffset += Lexer::SkipEscapedNewLines(TokPtr)-TokPtr;
760 
761   return TokStart.getLocWithOffset(PhysOffset);
762 }
763 
764 /// \brief Computes the source location just past the end of the
765 /// token at this source location.
766 ///
767 /// This routine can be used to produce a source location that
768 /// points just past the end of the token referenced by \p Loc, and
769 /// is generally used when a diagnostic needs to point just after a
770 /// token where it expected something different that it received. If
771 /// the returned source location would not be meaningful (e.g., if
772 /// it points into a macro), this routine returns an invalid
773 /// source location.
774 ///
775 /// \param Offset an offset from the end of the token, where the source
776 /// location should refer to. The default offset (0) produces a source
777 /// location pointing just past the end of the token; an offset of 1 produces
778 /// a source location pointing to the last character in the token, etc.
779 SourceLocation Lexer::getLocForEndOfToken(SourceLocation Loc, unsigned Offset,
780                                           const SourceManager &SM,
781                                           const LangOptions &LangOpts) {
782   if (Loc.isInvalid())
783     return SourceLocation();
784 
785   if (Loc.isMacroID()) {
786     if (Offset > 0 || !isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc))
787       return SourceLocation(); // Points inside the macro expansion.
788   }
789 
790   unsigned Len = Lexer::MeasureTokenLength(Loc, SM, LangOpts);
791   if (Len > Offset)
792     Len = Len - Offset;
793   else
794     return Loc;
795 
796   return Loc.getLocWithOffset(Len);
797 }
798 
799 /// \brief Returns true if the given MacroID location points at the first
800 /// token of the macro expansion.
801 bool Lexer::isAtStartOfMacroExpansion(SourceLocation loc,
802                                       const SourceManager &SM,
803                                       const LangOptions &LangOpts,
804                                       SourceLocation *MacroBegin) {
805   assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc");
806 
807   SourceLocation expansionLoc;
808   if (!SM.isAtStartOfImmediateMacroExpansion(loc, &expansionLoc))
809     return false;
810 
811   if (expansionLoc.isFileID()) {
812     // No other macro expansions, this is the first.
813     if (MacroBegin)
814       *MacroBegin = expansionLoc;
815     return true;
816   }
817 
818   return isAtStartOfMacroExpansion(expansionLoc, SM, LangOpts, MacroBegin);
819 }
820 
821 /// \brief Returns true if the given MacroID location points at the last
822 /// token of the macro expansion.
823 bool Lexer::isAtEndOfMacroExpansion(SourceLocation loc,
824                                     const SourceManager &SM,
825                                     const LangOptions &LangOpts,
826                                     SourceLocation *MacroEnd) {
827   assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc");
828 
829   SourceLocation spellLoc = SM.getSpellingLoc(loc);
830   unsigned tokLen = MeasureTokenLength(spellLoc, SM, LangOpts);
831   if (tokLen == 0)
832     return false;
833 
834   SourceLocation afterLoc = loc.getLocWithOffset(tokLen);
835   SourceLocation expansionLoc;
836   if (!SM.isAtEndOfImmediateMacroExpansion(afterLoc, &expansionLoc))
837     return false;
838 
839   if (expansionLoc.isFileID()) {
840     // No other macro expansions.
841     if (MacroEnd)
842       *MacroEnd = expansionLoc;
843     return true;
844   }
845 
846   return isAtEndOfMacroExpansion(expansionLoc, SM, LangOpts, MacroEnd);
847 }
848 
849 static CharSourceRange makeRangeFromFileLocs(CharSourceRange Range,
850                                              const SourceManager &SM,
851                                              const LangOptions &LangOpts) {
852   SourceLocation Begin = Range.getBegin();
853   SourceLocation End = Range.getEnd();
854   assert(Begin.isFileID() && End.isFileID());
855   if (Range.isTokenRange()) {
856     End = Lexer::getLocForEndOfToken(End, 0, SM,LangOpts);
857     if (End.isInvalid())
858       return CharSourceRange();
859   }
860 
861   // Break down the source locations.
862   FileID FID;
863   unsigned BeginOffs;
864   llvm::tie(FID, BeginOffs) = SM.getDecomposedLoc(Begin);
865   if (FID.isInvalid())
866     return CharSourceRange();
867 
868   unsigned EndOffs;
869   if (!SM.isInFileID(End, FID, &EndOffs) ||
870       BeginOffs > EndOffs)
871     return CharSourceRange();
872 
873   return CharSourceRange::getCharRange(Begin, End);
874 }
875 
876 CharSourceRange Lexer::makeFileCharRange(CharSourceRange Range,
877                                          const SourceManager &SM,
878                                          const LangOptions &LangOpts) {
879   SourceLocation Begin = Range.getBegin();
880   SourceLocation End = Range.getEnd();
881   if (Begin.isInvalid() || End.isInvalid())
882     return CharSourceRange();
883 
884   if (Begin.isFileID() && End.isFileID())
885     return makeRangeFromFileLocs(Range, SM, LangOpts);
886 
887   if (Begin.isMacroID() && End.isFileID()) {
888     if (!isAtStartOfMacroExpansion(Begin, SM, LangOpts, &Begin))
889       return CharSourceRange();
890     Range.setBegin(Begin);
891     return makeRangeFromFileLocs(Range, SM, LangOpts);
892   }
893 
894   if (Begin.isFileID() && End.isMacroID()) {
895     if ((Range.isTokenRange() && !isAtEndOfMacroExpansion(End, SM, LangOpts,
896                                                           &End)) ||
897         (Range.isCharRange() && !isAtStartOfMacroExpansion(End, SM, LangOpts,
898                                                            &End)))
899       return CharSourceRange();
900     Range.setEnd(End);
901     return makeRangeFromFileLocs(Range, SM, LangOpts);
902   }
903 
904   assert(Begin.isMacroID() && End.isMacroID());
905   SourceLocation MacroBegin, MacroEnd;
906   if (isAtStartOfMacroExpansion(Begin, SM, LangOpts, &MacroBegin) &&
907       ((Range.isTokenRange() && isAtEndOfMacroExpansion(End, SM, LangOpts,
908                                                         &MacroEnd)) ||
909        (Range.isCharRange() && isAtStartOfMacroExpansion(End, SM, LangOpts,
910                                                          &MacroEnd)))) {
911     Range.setBegin(MacroBegin);
912     Range.setEnd(MacroEnd);
913     return makeRangeFromFileLocs(Range, SM, LangOpts);
914   }
915 
916   bool Invalid = false;
917   const SrcMgr::SLocEntry &BeginEntry = SM.getSLocEntry(SM.getFileID(Begin),
918                                                         &Invalid);
919   if (Invalid)
920     return CharSourceRange();
921 
922   if (BeginEntry.getExpansion().isMacroArgExpansion()) {
923     const SrcMgr::SLocEntry &EndEntry = SM.getSLocEntry(SM.getFileID(End),
924                                                         &Invalid);
925     if (Invalid)
926       return CharSourceRange();
927 
928     if (EndEntry.getExpansion().isMacroArgExpansion() &&
929         BeginEntry.getExpansion().getExpansionLocStart() ==
930             EndEntry.getExpansion().getExpansionLocStart()) {
931       Range.setBegin(SM.getImmediateSpellingLoc(Begin));
932       Range.setEnd(SM.getImmediateSpellingLoc(End));
933       return makeFileCharRange(Range, SM, LangOpts);
934     }
935   }
936 
937   return CharSourceRange();
938 }
939 
940 StringRef Lexer::getSourceText(CharSourceRange Range,
941                                const SourceManager &SM,
942                                const LangOptions &LangOpts,
943                                bool *Invalid) {
944   Range = makeFileCharRange(Range, SM, LangOpts);
945   if (Range.isInvalid()) {
946     if (Invalid) *Invalid = true;
947     return StringRef();
948   }
949 
950   // Break down the source location.
951   std::pair<FileID, unsigned> beginInfo = SM.getDecomposedLoc(Range.getBegin());
952   if (beginInfo.first.isInvalid()) {
953     if (Invalid) *Invalid = true;
954     return StringRef();
955   }
956 
957   unsigned EndOffs;
958   if (!SM.isInFileID(Range.getEnd(), beginInfo.first, &EndOffs) ||
959       beginInfo.second > EndOffs) {
960     if (Invalid) *Invalid = true;
961     return StringRef();
962   }
963 
964   // Try to the load the file buffer.
965   bool invalidTemp = false;
966   StringRef file = SM.getBufferData(beginInfo.first, &invalidTemp);
967   if (invalidTemp) {
968     if (Invalid) *Invalid = true;
969     return StringRef();
970   }
971 
972   if (Invalid) *Invalid = false;
973   return file.substr(beginInfo.second, EndOffs - beginInfo.second);
974 }
975 
976 StringRef Lexer::getImmediateMacroName(SourceLocation Loc,
977                                        const SourceManager &SM,
978                                        const LangOptions &LangOpts) {
979   assert(Loc.isMacroID() && "Only reasonble to call this on macros");
980 
981   // Find the location of the immediate macro expansion.
982   while (1) {
983     FileID FID = SM.getFileID(Loc);
984     const SrcMgr::SLocEntry *E = &SM.getSLocEntry(FID);
985     const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();
986     Loc = Expansion.getExpansionLocStart();
987     if (!Expansion.isMacroArgExpansion())
988       break;
989 
990     // For macro arguments we need to check that the argument did not come
991     // from an inner macro, e.g: "MAC1( MAC2(foo) )"
992 
993     // Loc points to the argument id of the macro definition, move to the
994     // macro expansion.
995     Loc = SM.getImmediateExpansionRange(Loc).first;
996     SourceLocation SpellLoc = Expansion.getSpellingLoc();
997     if (SpellLoc.isFileID())
998       break; // No inner macro.
999 
1000     // If spelling location resides in the same FileID as macro expansion
1001     // location, it means there is no inner macro.
1002     FileID MacroFID = SM.getFileID(Loc);
1003     if (SM.isInFileID(SpellLoc, MacroFID))
1004       break;
1005 
1006     // Argument came from inner macro.
1007     Loc = SpellLoc;
1008   }
1009 
1010   // Find the spelling location of the start of the non-argument expansion
1011   // range. This is where the macro name was spelled in order to begin
1012   // expanding this macro.
1013   Loc = SM.getSpellingLoc(Loc);
1014 
1015   // Dig out the buffer where the macro name was spelled and the extents of the
1016   // name so that we can render it into the expansion note.
1017   std::pair<FileID, unsigned> ExpansionInfo = SM.getDecomposedLoc(Loc);
1018   unsigned MacroTokenLength = Lexer::MeasureTokenLength(Loc, SM, LangOpts);
1019   StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first);
1020   return ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength);
1021 }
1022 
1023 bool Lexer::isIdentifierBodyChar(char c, const LangOptions &LangOpts) {
1024   return isIdentifierBody(c, LangOpts.DollarIdents);
1025 }
1026 
1027 
1028 //===----------------------------------------------------------------------===//
1029 // Diagnostics forwarding code.
1030 //===----------------------------------------------------------------------===//
1031 
1032 /// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
1033 /// lexer buffer was all expanded at a single point, perform the mapping.
1034 /// This is currently only used for _Pragma implementation, so it is the slow
1035 /// path of the hot getSourceLocation method.  Do not allow it to be inlined.
1036 static LLVM_ATTRIBUTE_NOINLINE SourceLocation GetMappedTokenLoc(
1037     Preprocessor &PP, SourceLocation FileLoc, unsigned CharNo, unsigned TokLen);
1038 static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
1039                                         SourceLocation FileLoc,
1040                                         unsigned CharNo, unsigned TokLen) {
1041   assert(FileLoc.isMacroID() && "Must be a macro expansion");
1042 
1043   // Otherwise, we're lexing "mapped tokens".  This is used for things like
1044   // _Pragma handling.  Combine the expansion location of FileLoc with the
1045   // spelling location.
1046   SourceManager &SM = PP.getSourceManager();
1047 
1048   // Create a new SLoc which is expanded from Expansion(FileLoc) but whose
1049   // characters come from spelling(FileLoc)+Offset.
1050   SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
1051   SpellingLoc = SpellingLoc.getLocWithOffset(CharNo);
1052 
1053   // Figure out the expansion loc range, which is the range covered by the
1054   // original _Pragma(...) sequence.
1055   std::pair<SourceLocation,SourceLocation> II =
1056     SM.getImmediateExpansionRange(FileLoc);
1057 
1058   return SM.createExpansionLoc(SpellingLoc, II.first, II.second, TokLen);
1059 }
1060 
1061 /// getSourceLocation - Return a source location identifier for the specified
1062 /// offset in the current file.
1063 SourceLocation Lexer::getSourceLocation(const char *Loc,
1064                                         unsigned TokLen) const {
1065   assert(Loc >= BufferStart && Loc <= BufferEnd &&
1066          "Location out of range for this buffer!");
1067 
1068   // In the normal case, we're just lexing from a simple file buffer, return
1069   // the file id from FileLoc with the offset specified.
1070   unsigned CharNo = Loc-BufferStart;
1071   if (FileLoc.isFileID())
1072     return FileLoc.getLocWithOffset(CharNo);
1073 
1074   // Otherwise, this is the _Pragma lexer case, which pretends that all of the
1075   // tokens are lexed from where the _Pragma was defined.
1076   assert(PP && "This doesn't work on raw lexers");
1077   return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
1078 }
1079 
1080 /// Diag - Forwarding function for diagnostics.  This translate a source
1081 /// position in the current buffer into a SourceLocation object for rendering.
1082 DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
1083   return PP->Diag(getSourceLocation(Loc), DiagID);
1084 }
1085 
1086 //===----------------------------------------------------------------------===//
1087 // Trigraph and Escaped Newline Handling Code.
1088 //===----------------------------------------------------------------------===//
1089 
1090 /// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
1091 /// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
1092 static char GetTrigraphCharForLetter(char Letter) {
1093   switch (Letter) {
1094   default:   return 0;
1095   case '=':  return '#';
1096   case ')':  return ']';
1097   case '(':  return '[';
1098   case '!':  return '|';
1099   case '\'': return '^';
1100   case '>':  return '}';
1101   case '/':  return '\\';
1102   case '<':  return '{';
1103   case '-':  return '~';
1104   }
1105 }
1106 
1107 /// DecodeTrigraphChar - If the specified character is a legal trigraph when
1108 /// prefixed with ??, emit a trigraph warning.  If trigraphs are enabled,
1109 /// return the result character.  Finally, emit a warning about trigraph use
1110 /// whether trigraphs are enabled or not.
1111 static char DecodeTrigraphChar(const char *CP, Lexer *L) {
1112   char Res = GetTrigraphCharForLetter(*CP);
1113   if (!Res || !L) return Res;
1114 
1115   if (!L->getLangOpts().Trigraphs) {
1116     if (!L->isLexingRawMode())
1117       L->Diag(CP-2, diag::trigraph_ignored);
1118     return 0;
1119   }
1120 
1121   if (!L->isLexingRawMode())
1122     L->Diag(CP-2, diag::trigraph_converted) << StringRef(&Res, 1);
1123   return Res;
1124 }
1125 
1126 /// getEscapedNewLineSize - Return the size of the specified escaped newline,
1127 /// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a
1128 /// trigraph equivalent on entry to this function.
1129 unsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
1130   unsigned Size = 0;
1131   while (isWhitespace(Ptr[Size])) {
1132     ++Size;
1133 
1134     if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
1135       continue;
1136 
1137     // If this is a \r\n or \n\r, skip the other half.
1138     if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
1139         Ptr[Size-1] != Ptr[Size])
1140       ++Size;
1141 
1142     return Size;
1143   }
1144 
1145   // Not an escaped newline, must be a \t or something else.
1146   return 0;
1147 }
1148 
1149 /// SkipEscapedNewLines - If P points to an escaped newline (or a series of
1150 /// them), skip over them and return the first non-escaped-newline found,
1151 /// otherwise return P.
1152 const char *Lexer::SkipEscapedNewLines(const char *P) {
1153   while (1) {
1154     const char *AfterEscape;
1155     if (*P == '\\') {
1156       AfterEscape = P+1;
1157     } else if (*P == '?') {
1158       // If not a trigraph for escape, bail out.
1159       if (P[1] != '?' || P[2] != '/')
1160         return P;
1161       AfterEscape = P+3;
1162     } else {
1163       return P;
1164     }
1165 
1166     unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
1167     if (NewLineSize == 0) return P;
1168     P = AfterEscape+NewLineSize;
1169   }
1170 }
1171 
1172 /// \brief Checks that the given token is the first token that occurs after the
1173 /// given location (this excludes comments and whitespace). Returns the location
1174 /// immediately after the specified token. If the token is not found or the
1175 /// location is inside a macro, the returned source location will be invalid.
1176 SourceLocation Lexer::findLocationAfterToken(SourceLocation Loc,
1177                                         tok::TokenKind TKind,
1178                                         const SourceManager &SM,
1179                                         const LangOptions &LangOpts,
1180                                         bool SkipTrailingWhitespaceAndNewLine) {
1181   if (Loc.isMacroID()) {
1182     if (!Lexer::isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc))
1183       return SourceLocation();
1184   }
1185   Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, LangOpts);
1186 
1187   // Break down the source location.
1188   std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
1189 
1190   // Try to load the file buffer.
1191   bool InvalidTemp = false;
1192   StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp);
1193   if (InvalidTemp)
1194     return SourceLocation();
1195 
1196   const char *TokenBegin = File.data() + LocInfo.second;
1197 
1198   // Lex from the start of the given location.
1199   Lexer lexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(),
1200                                       TokenBegin, File.end());
1201   // Find the token.
1202   Token Tok;
1203   lexer.LexFromRawLexer(Tok);
1204   if (Tok.isNot(TKind))
1205     return SourceLocation();
1206   SourceLocation TokenLoc = Tok.getLocation();
1207 
1208   // Calculate how much whitespace needs to be skipped if any.
1209   unsigned NumWhitespaceChars = 0;
1210   if (SkipTrailingWhitespaceAndNewLine) {
1211     const char *TokenEnd = SM.getCharacterData(TokenLoc) +
1212                            Tok.getLength();
1213     unsigned char C = *TokenEnd;
1214     while (isHorizontalWhitespace(C)) {
1215       C = *(++TokenEnd);
1216       NumWhitespaceChars++;
1217     }
1218 
1219     // Skip \r, \n, \r\n, or \n\r
1220     if (C == '\n' || C == '\r') {
1221       char PrevC = C;
1222       C = *(++TokenEnd);
1223       NumWhitespaceChars++;
1224       if ((C == '\n' || C == '\r') && C != PrevC)
1225         NumWhitespaceChars++;
1226     }
1227   }
1228 
1229   return TokenLoc.getLocWithOffset(Tok.getLength() + NumWhitespaceChars);
1230 }
1231 
1232 /// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
1233 /// get its size, and return it.  This is tricky in several cases:
1234 ///   1. If currently at the start of a trigraph, we warn about the trigraph,
1235 ///      then either return the trigraph (skipping 3 chars) or the '?',
1236 ///      depending on whether trigraphs are enabled or not.
1237 ///   2. If this is an escaped newline (potentially with whitespace between
1238 ///      the backslash and newline), implicitly skip the newline and return
1239 ///      the char after it.
1240 ///
1241 /// This handles the slow/uncommon case of the getCharAndSize method.  Here we
1242 /// know that we can accumulate into Size, and that we have already incremented
1243 /// Ptr by Size bytes.
1244 ///
1245 /// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
1246 /// be updated to match.
1247 ///
1248 char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
1249                                Token *Tok) {
1250   // If we have a slash, look for an escaped newline.
1251   if (Ptr[0] == '\\') {
1252     ++Size;
1253     ++Ptr;
1254 Slash:
1255     // Common case, backslash-char where the char is not whitespace.
1256     if (!isWhitespace(Ptr[0])) return '\\';
1257 
1258     // See if we have optional whitespace characters between the slash and
1259     // newline.
1260     if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
1261       // Remember that this token needs to be cleaned.
1262       if (Tok) Tok->setFlag(Token::NeedsCleaning);
1263 
1264       // Warn if there was whitespace between the backslash and newline.
1265       if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
1266         Diag(Ptr, diag::backslash_newline_space);
1267 
1268       // Found backslash<whitespace><newline>.  Parse the char after it.
1269       Size += EscapedNewLineSize;
1270       Ptr  += EscapedNewLineSize;
1271 
1272       // If the char that we finally got was a \n, then we must have had
1273       // something like \<newline><newline>.  We don't want to consume the
1274       // second newline.
1275       if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0')
1276         return ' ';
1277 
1278       // Use slow version to accumulate a correct size field.
1279       return getCharAndSizeSlow(Ptr, Size, Tok);
1280     }
1281 
1282     // Otherwise, this is not an escaped newline, just return the slash.
1283     return '\\';
1284   }
1285 
1286   // If this is a trigraph, process it.
1287   if (Ptr[0] == '?' && Ptr[1] == '?') {
1288     // If this is actually a legal trigraph (not something like "??x"), emit
1289     // a trigraph warning.  If so, and if trigraphs are enabled, return it.
1290     if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
1291       // Remember that this token needs to be cleaned.
1292       if (Tok) Tok->setFlag(Token::NeedsCleaning);
1293 
1294       Ptr += 3;
1295       Size += 3;
1296       if (C == '\\') goto Slash;
1297       return C;
1298     }
1299   }
1300 
1301   // If this is neither, return a single character.
1302   ++Size;
1303   return *Ptr;
1304 }
1305 
1306 
1307 /// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
1308 /// getCharAndSizeNoWarn method.  Here we know that we can accumulate into Size,
1309 /// and that we have already incremented Ptr by Size bytes.
1310 ///
1311 /// NOTE: When this method is updated, getCharAndSizeSlow (above) should
1312 /// be updated to match.
1313 char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
1314                                      const LangOptions &LangOpts) {
1315   // If we have a slash, look for an escaped newline.
1316   if (Ptr[0] == '\\') {
1317     ++Size;
1318     ++Ptr;
1319 Slash:
1320     // Common case, backslash-char where the char is not whitespace.
1321     if (!isWhitespace(Ptr[0])) return '\\';
1322 
1323     // See if we have optional whitespace characters followed by a newline.
1324     if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
1325       // Found backslash<whitespace><newline>.  Parse the char after it.
1326       Size += EscapedNewLineSize;
1327       Ptr  += EscapedNewLineSize;
1328 
1329       // If the char that we finally got was a \n, then we must have had
1330       // something like \<newline><newline>.  We don't want to consume the
1331       // second newline.
1332       if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0')
1333         return ' ';
1334 
1335       // Use slow version to accumulate a correct size field.
1336       return getCharAndSizeSlowNoWarn(Ptr, Size, LangOpts);
1337     }
1338 
1339     // Otherwise, this is not an escaped newline, just return the slash.
1340     return '\\';
1341   }
1342 
1343   // If this is a trigraph, process it.
1344   if (LangOpts.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
1345     // If this is actually a legal trigraph (not something like "??x"), return
1346     // it.
1347     if (char C = GetTrigraphCharForLetter(Ptr[2])) {
1348       Ptr += 3;
1349       Size += 3;
1350       if (C == '\\') goto Slash;
1351       return C;
1352     }
1353   }
1354 
1355   // If this is neither, return a single character.
1356   ++Size;
1357   return *Ptr;
1358 }
1359 
1360 //===----------------------------------------------------------------------===//
1361 // Helper methods for lexing.
1362 //===----------------------------------------------------------------------===//
1363 
1364 /// \brief Routine that indiscriminately skips bytes in the source file.
1365 void Lexer::SkipBytes(unsigned Bytes, bool StartOfLine) {
1366   BufferPtr += Bytes;
1367   if (BufferPtr > BufferEnd)
1368     BufferPtr = BufferEnd;
1369   // FIXME: What exactly does the StartOfLine bit mean?  There are two
1370   // possible meanings for the "start" of the line: the first token on the
1371   // unexpanded line, or the first token on the expanded line.
1372   IsAtStartOfLine = StartOfLine;
1373   IsAtPhysicalStartOfLine = StartOfLine;
1374 }
1375 
1376 static bool isAllowedIDChar(uint32_t C, const LangOptions &LangOpts) {
1377   if (LangOpts.CPlusPlus11 || LangOpts.C11) {
1378     static const llvm::sys::UnicodeCharSet C11AllowedIDChars(
1379         C11AllowedIDCharRanges);
1380     return C11AllowedIDChars.contains(C);
1381   } else if (LangOpts.CPlusPlus) {
1382     static const llvm::sys::UnicodeCharSet CXX03AllowedIDChars(
1383         CXX03AllowedIDCharRanges);
1384     return CXX03AllowedIDChars.contains(C);
1385   } else {
1386     static const llvm::sys::UnicodeCharSet C99AllowedIDChars(
1387         C99AllowedIDCharRanges);
1388     return C99AllowedIDChars.contains(C);
1389   }
1390 }
1391 
1392 static bool isAllowedInitiallyIDChar(uint32_t C, const LangOptions &LangOpts) {
1393   assert(isAllowedIDChar(C, LangOpts));
1394   if (LangOpts.CPlusPlus11 || LangOpts.C11) {
1395     static const llvm::sys::UnicodeCharSet C11DisallowedInitialIDChars(
1396         C11DisallowedInitialIDCharRanges);
1397     return !C11DisallowedInitialIDChars.contains(C);
1398   } else if (LangOpts.CPlusPlus) {
1399     return true;
1400   } else {
1401     static const llvm::sys::UnicodeCharSet C99DisallowedInitialIDChars(
1402         C99DisallowedInitialIDCharRanges);
1403     return !C99DisallowedInitialIDChars.contains(C);
1404   }
1405 }
1406 
1407 static inline CharSourceRange makeCharRange(Lexer &L, const char *Begin,
1408                                             const char *End) {
1409   return CharSourceRange::getCharRange(L.getSourceLocation(Begin),
1410                                        L.getSourceLocation(End));
1411 }
1412 
1413 static void maybeDiagnoseIDCharCompat(DiagnosticsEngine &Diags, uint32_t C,
1414                                       CharSourceRange Range, bool IsFirst) {
1415   // Check C99 compatibility.
1416   if (Diags.getDiagnosticLevel(diag::warn_c99_compat_unicode_id,
1417                                Range.getBegin()) > DiagnosticsEngine::Ignored) {
1418     enum {
1419       CannotAppearInIdentifier = 0,
1420       CannotStartIdentifier
1421     };
1422 
1423     static const llvm::sys::UnicodeCharSet C99AllowedIDChars(
1424         C99AllowedIDCharRanges);
1425     static const llvm::sys::UnicodeCharSet C99DisallowedInitialIDChars(
1426         C99DisallowedInitialIDCharRanges);
1427     if (!C99AllowedIDChars.contains(C)) {
1428       Diags.Report(Range.getBegin(), diag::warn_c99_compat_unicode_id)
1429         << Range
1430         << CannotAppearInIdentifier;
1431     } else if (IsFirst && C99DisallowedInitialIDChars.contains(C)) {
1432       Diags.Report(Range.getBegin(), diag::warn_c99_compat_unicode_id)
1433         << Range
1434         << CannotStartIdentifier;
1435     }
1436   }
1437 
1438   // Check C++98 compatibility.
1439   if (Diags.getDiagnosticLevel(diag::warn_cxx98_compat_unicode_id,
1440                                Range.getBegin()) > DiagnosticsEngine::Ignored) {
1441     static const llvm::sys::UnicodeCharSet CXX03AllowedIDChars(
1442         CXX03AllowedIDCharRanges);
1443     if (!CXX03AllowedIDChars.contains(C)) {
1444       Diags.Report(Range.getBegin(), diag::warn_cxx98_compat_unicode_id)
1445         << Range;
1446     }
1447   }
1448  }
1449 
1450 bool Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
1451   // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
1452   unsigned Size;
1453   unsigned char C = *CurPtr++;
1454   while (isIdentifierBody(C))
1455     C = *CurPtr++;
1456 
1457   --CurPtr;   // Back up over the skipped character.
1458 
1459   // Fast path, no $,\,? in identifier found.  '\' might be an escaped newline
1460   // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
1461   //
1462   // TODO: Could merge these checks into an InfoTable flag to make the
1463   // comparison cheaper
1464   if (isASCII(C) && C != '\\' && C != '?' &&
1465       (C != '$' || !LangOpts.DollarIdents)) {
1466 FinishIdentifier:
1467     const char *IdStart = BufferPtr;
1468     FormTokenWithChars(Result, CurPtr, tok::raw_identifier);
1469     Result.setRawIdentifierData(IdStart);
1470 
1471     // If we are in raw mode, return this identifier raw.  There is no need to
1472     // look up identifier information or attempt to macro expand it.
1473     if (LexingRawMode)
1474       return true;
1475 
1476     // Fill in Result.IdentifierInfo and update the token kind,
1477     // looking up the identifier in the identifier table.
1478     IdentifierInfo *II = PP->LookUpIdentifierInfo(Result);
1479 
1480     // Finally, now that we know we have an identifier, pass this off to the
1481     // preprocessor, which may macro expand it or something.
1482     if (II->isHandleIdentifierCase())
1483       return PP->HandleIdentifier(Result);
1484 
1485     return true;
1486   }
1487 
1488   // Otherwise, $,\,? in identifier found.  Enter slower path.
1489 
1490   C = getCharAndSize(CurPtr, Size);
1491   while (1) {
1492     if (C == '$') {
1493       // If we hit a $ and they are not supported in identifiers, we are done.
1494       if (!LangOpts.DollarIdents) goto FinishIdentifier;
1495 
1496       // Otherwise, emit a diagnostic and continue.
1497       if (!isLexingRawMode())
1498         Diag(CurPtr, diag::ext_dollar_in_identifier);
1499       CurPtr = ConsumeChar(CurPtr, Size, Result);
1500       C = getCharAndSize(CurPtr, Size);
1501       continue;
1502 
1503     } else if (C == '\\') {
1504       const char *UCNPtr = CurPtr + Size;
1505       uint32_t CodePoint = tryReadUCN(UCNPtr, CurPtr, /*Token=*/0);
1506       if (CodePoint == 0 || !isAllowedIDChar(CodePoint, LangOpts))
1507         goto FinishIdentifier;
1508 
1509       if (!isLexingRawMode()) {
1510         maybeDiagnoseIDCharCompat(PP->getDiagnostics(), CodePoint,
1511                                   makeCharRange(*this, CurPtr, UCNPtr),
1512                                   /*IsFirst=*/false);
1513       }
1514 
1515       Result.setFlag(Token::HasUCN);
1516       if ((UCNPtr - CurPtr ==  6 && CurPtr[1] == 'u') ||
1517           (UCNPtr - CurPtr == 10 && CurPtr[1] == 'U'))
1518         CurPtr = UCNPtr;
1519       else
1520         while (CurPtr != UCNPtr)
1521           (void)getAndAdvanceChar(CurPtr, Result);
1522 
1523       C = getCharAndSize(CurPtr, Size);
1524       continue;
1525     } else if (!isASCII(C)) {
1526       const char *UnicodePtr = CurPtr;
1527       UTF32 CodePoint;
1528       ConversionResult Result =
1529           llvm::convertUTF8Sequence((const UTF8 **)&UnicodePtr,
1530                                     (const UTF8 *)BufferEnd,
1531                                     &CodePoint,
1532                                     strictConversion);
1533       if (Result != conversionOK ||
1534           !isAllowedIDChar(static_cast<uint32_t>(CodePoint), LangOpts))
1535         goto FinishIdentifier;
1536 
1537       if (!isLexingRawMode()) {
1538         maybeDiagnoseIDCharCompat(PP->getDiagnostics(), CodePoint,
1539                                   makeCharRange(*this, CurPtr, UnicodePtr),
1540                                   /*IsFirst=*/false);
1541       }
1542 
1543       CurPtr = UnicodePtr;
1544       C = getCharAndSize(CurPtr, Size);
1545       continue;
1546     } else if (!isIdentifierBody(C)) {
1547       goto FinishIdentifier;
1548     }
1549 
1550     // Otherwise, this character is good, consume it.
1551     CurPtr = ConsumeChar(CurPtr, Size, Result);
1552 
1553     C = getCharAndSize(CurPtr, Size);
1554     while (isIdentifierBody(C)) {
1555       CurPtr = ConsumeChar(CurPtr, Size, Result);
1556       C = getCharAndSize(CurPtr, Size);
1557     }
1558   }
1559 }
1560 
1561 /// isHexaLiteral - Return true if Start points to a hex constant.
1562 /// in microsoft mode (where this is supposed to be several different tokens).
1563 bool Lexer::isHexaLiteral(const char *Start, const LangOptions &LangOpts) {
1564   unsigned Size;
1565   char C1 = Lexer::getCharAndSizeNoWarn(Start, Size, LangOpts);
1566   if (C1 != '0')
1567     return false;
1568   char C2 = Lexer::getCharAndSizeNoWarn(Start + Size, Size, LangOpts);
1569   return (C2 == 'x' || C2 == 'X');
1570 }
1571 
1572 /// LexNumericConstant - Lex the remainder of a integer or floating point
1573 /// constant. From[-1] is the first character lexed.  Return the end of the
1574 /// constant.
1575 bool Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
1576   unsigned Size;
1577   char C = getCharAndSize(CurPtr, Size);
1578   char PrevCh = 0;
1579   while (isPreprocessingNumberBody(C)) { // FIXME: UCNs in ud-suffix.
1580     CurPtr = ConsumeChar(CurPtr, Size, Result);
1581     PrevCh = C;
1582     C = getCharAndSize(CurPtr, Size);
1583   }
1584 
1585   // If we fell out, check for a sign, due to 1e+12.  If we have one, continue.
1586   if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) {
1587     // If we are in Microsoft mode, don't continue if the constant is hex.
1588     // For example, MSVC will accept the following as 3 tokens: 0x1234567e+1
1589     if (!LangOpts.MicrosoftExt || !isHexaLiteral(BufferPtr, LangOpts))
1590       return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
1591   }
1592 
1593   // If we have a hex FP constant, continue.
1594   if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p')) {
1595     // Outside C99, we accept hexadecimal floating point numbers as a
1596     // not-quite-conforming extension. Only do so if this looks like it's
1597     // actually meant to be a hexfloat, and not if it has a ud-suffix.
1598     bool IsHexFloat = true;
1599     if (!LangOpts.C99) {
1600       if (!isHexaLiteral(BufferPtr, LangOpts))
1601         IsHexFloat = false;
1602       else if (std::find(BufferPtr, CurPtr, '_') != CurPtr)
1603         IsHexFloat = false;
1604     }
1605     if (IsHexFloat)
1606       return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
1607   }
1608 
1609   // If we have a digit separator, continue.
1610   if (C == '\'' && getLangOpts().CPlusPlus1y) {
1611     unsigned NextSize;
1612     char Next = getCharAndSizeNoWarn(CurPtr + Size, NextSize, getLangOpts());
1613     if (isIdentifierBody(Next)) {
1614       if (!isLexingRawMode())
1615         Diag(CurPtr, diag::warn_cxx11_compat_digit_separator);
1616       CurPtr = ConsumeChar(CurPtr, Size, Result);
1617       return LexNumericConstant(Result, CurPtr);
1618     }
1619   }
1620 
1621   // Update the location of token as well as BufferPtr.
1622   const char *TokStart = BufferPtr;
1623   FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
1624   Result.setLiteralData(TokStart);
1625   return true;
1626 }
1627 
1628 /// LexUDSuffix - Lex the ud-suffix production for user-defined literal suffixes
1629 /// in C++11, or warn on a ud-suffix in C++98.
1630 const char *Lexer::LexUDSuffix(Token &Result, const char *CurPtr,
1631                                bool IsStringLiteral) {
1632   assert(getLangOpts().CPlusPlus);
1633 
1634   // Maximally munch an identifier. FIXME: UCNs.
1635   unsigned Size;
1636   char C = getCharAndSize(CurPtr, Size);
1637   if (isIdentifierHead(C)) {
1638     if (!getLangOpts().CPlusPlus11) {
1639       if (!isLexingRawMode())
1640         Diag(CurPtr,
1641              C == '_' ? diag::warn_cxx11_compat_user_defined_literal
1642                       : diag::warn_cxx11_compat_reserved_user_defined_literal)
1643           << FixItHint::CreateInsertion(getSourceLocation(CurPtr), " ");
1644       return CurPtr;
1645     }
1646 
1647     // C++11 [lex.ext]p10, [usrlit.suffix]p1: A program containing a ud-suffix
1648     // that does not start with an underscore is ill-formed. As a conforming
1649     // extension, we treat all such suffixes as if they had whitespace before
1650     // them.
1651     bool IsUDSuffix = false;
1652     if (C == '_')
1653       IsUDSuffix = true;
1654     else if (IsStringLiteral && getLangOpts().CPlusPlus1y) {
1655       // In C++1y, we need to look ahead a few characters to see if this is a
1656       // valid suffix for a string literal or a numeric literal (this could be
1657       // the 'operator""if' defining a numeric literal operator).
1658       const unsigned MaxStandardSuffixLength = 3;
1659       char Buffer[MaxStandardSuffixLength] = { C };
1660       unsigned Consumed = Size;
1661       unsigned Chars = 1;
1662       while (true) {
1663         unsigned NextSize;
1664         char Next = getCharAndSizeNoWarn(CurPtr + Consumed, NextSize,
1665                                          getLangOpts());
1666         if (!isIdentifierBody(Next)) {
1667           // End of suffix. Check whether this is on the whitelist.
1668           IsUDSuffix = (Chars == 1 && Buffer[0] == 's') ||
1669                        NumericLiteralParser::isValidUDSuffix(
1670                            getLangOpts(), StringRef(Buffer, Chars));
1671           break;
1672         }
1673 
1674         if (Chars == MaxStandardSuffixLength)
1675           // Too long: can't be a standard suffix.
1676           break;
1677 
1678         Buffer[Chars++] = Next;
1679         Consumed += NextSize;
1680       }
1681     }
1682 
1683     if (!IsUDSuffix) {
1684       if (!isLexingRawMode())
1685         Diag(CurPtr, getLangOpts().MSVCCompat
1686                          ? diag::ext_ms_reserved_user_defined_literal
1687                          : diag::ext_reserved_user_defined_literal)
1688             << FixItHint::CreateInsertion(getSourceLocation(CurPtr), " ");
1689       return CurPtr;
1690     }
1691 
1692     Result.setFlag(Token::HasUDSuffix);
1693     do {
1694       CurPtr = ConsumeChar(CurPtr, Size, Result);
1695       C = getCharAndSize(CurPtr, Size);
1696     } while (isIdentifierBody(C));
1697   }
1698   return CurPtr;
1699 }
1700 
1701 /// LexStringLiteral - Lex the remainder of a string literal, after having lexed
1702 /// either " or L" or u8" or u" or U".
1703 bool Lexer::LexStringLiteral(Token &Result, const char *CurPtr,
1704                              tok::TokenKind Kind) {
1705   const char *NulCharacter = 0; // Does this string contain the \0 character?
1706 
1707   if (!isLexingRawMode() &&
1708       (Kind == tok::utf8_string_literal ||
1709        Kind == tok::utf16_string_literal ||
1710        Kind == tok::utf32_string_literal))
1711     Diag(BufferPtr, getLangOpts().CPlusPlus
1712            ? diag::warn_cxx98_compat_unicode_literal
1713            : diag::warn_c99_compat_unicode_literal);
1714 
1715   char C = getAndAdvanceChar(CurPtr, Result);
1716   while (C != '"') {
1717     // Skip escaped characters.  Escaped newlines will already be processed by
1718     // getAndAdvanceChar.
1719     if (C == '\\')
1720       C = getAndAdvanceChar(CurPtr, Result);
1721 
1722     if (C == '\n' || C == '\r' ||             // Newline.
1723         (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
1724       if (!isLexingRawMode() && !LangOpts.AsmPreprocessor)
1725         Diag(BufferPtr, diag::ext_unterminated_string);
1726       FormTokenWithChars(Result, CurPtr-1, tok::unknown);
1727       return true;
1728     }
1729 
1730     if (C == 0) {
1731       if (isCodeCompletionPoint(CurPtr-1)) {
1732         PP->CodeCompleteNaturalLanguage();
1733         FormTokenWithChars(Result, CurPtr-1, tok::unknown);
1734         cutOffLexing();
1735         return true;
1736       }
1737 
1738       NulCharacter = CurPtr-1;
1739     }
1740     C = getAndAdvanceChar(CurPtr, Result);
1741   }
1742 
1743   // If we are in C++11, lex the optional ud-suffix.
1744   if (getLangOpts().CPlusPlus)
1745     CurPtr = LexUDSuffix(Result, CurPtr, true);
1746 
1747   // If a nul character existed in the string, warn about it.
1748   if (NulCharacter && !isLexingRawMode())
1749     Diag(NulCharacter, diag::null_in_string);
1750 
1751   // Update the location of the token as well as the BufferPtr instance var.
1752   const char *TokStart = BufferPtr;
1753   FormTokenWithChars(Result, CurPtr, Kind);
1754   Result.setLiteralData(TokStart);
1755   return true;
1756 }
1757 
1758 /// LexRawStringLiteral - Lex the remainder of a raw string literal, after
1759 /// having lexed R", LR", u8R", uR", or UR".
1760 bool Lexer::LexRawStringLiteral(Token &Result, const char *CurPtr,
1761                                 tok::TokenKind Kind) {
1762   // This function doesn't use getAndAdvanceChar because C++0x [lex.pptoken]p3:
1763   //  Between the initial and final double quote characters of the raw string,
1764   //  any transformations performed in phases 1 and 2 (trigraphs,
1765   //  universal-character-names, and line splicing) are reverted.
1766 
1767   if (!isLexingRawMode())
1768     Diag(BufferPtr, diag::warn_cxx98_compat_raw_string_literal);
1769 
1770   unsigned PrefixLen = 0;
1771 
1772   while (PrefixLen != 16 && isRawStringDelimBody(CurPtr[PrefixLen]))
1773     ++PrefixLen;
1774 
1775   // If the last character was not a '(', then we didn't lex a valid delimiter.
1776   if (CurPtr[PrefixLen] != '(') {
1777     if (!isLexingRawMode()) {
1778       const char *PrefixEnd = &CurPtr[PrefixLen];
1779       if (PrefixLen == 16) {
1780         Diag(PrefixEnd, diag::err_raw_delim_too_long);
1781       } else {
1782         Diag(PrefixEnd, diag::err_invalid_char_raw_delim)
1783           << StringRef(PrefixEnd, 1);
1784       }
1785     }
1786 
1787     // Search for the next '"' in hopes of salvaging the lexer. Unfortunately,
1788     // it's possible the '"' was intended to be part of the raw string, but
1789     // there's not much we can do about that.
1790     while (1) {
1791       char C = *CurPtr++;
1792 
1793       if (C == '"')
1794         break;
1795       if (C == 0 && CurPtr-1 == BufferEnd) {
1796         --CurPtr;
1797         break;
1798       }
1799     }
1800 
1801     FormTokenWithChars(Result, CurPtr, tok::unknown);
1802     return true;
1803   }
1804 
1805   // Save prefix and move CurPtr past it
1806   const char *Prefix = CurPtr;
1807   CurPtr += PrefixLen + 1; // skip over prefix and '('
1808 
1809   while (1) {
1810     char C = *CurPtr++;
1811 
1812     if (C == ')') {
1813       // Check for prefix match and closing quote.
1814       if (strncmp(CurPtr, Prefix, PrefixLen) == 0 && CurPtr[PrefixLen] == '"') {
1815         CurPtr += PrefixLen + 1; // skip over prefix and '"'
1816         break;
1817       }
1818     } else if (C == 0 && CurPtr-1 == BufferEnd) { // End of file.
1819       if (!isLexingRawMode())
1820         Diag(BufferPtr, diag::err_unterminated_raw_string)
1821           << StringRef(Prefix, PrefixLen);
1822       FormTokenWithChars(Result, CurPtr-1, tok::unknown);
1823       return true;
1824     }
1825   }
1826 
1827   // If we are in C++11, lex the optional ud-suffix.
1828   if (getLangOpts().CPlusPlus)
1829     CurPtr = LexUDSuffix(Result, CurPtr, true);
1830 
1831   // Update the location of token as well as BufferPtr.
1832   const char *TokStart = BufferPtr;
1833   FormTokenWithChars(Result, CurPtr, Kind);
1834   Result.setLiteralData(TokStart);
1835   return true;
1836 }
1837 
1838 /// LexAngledStringLiteral - Lex the remainder of an angled string literal,
1839 /// after having lexed the '<' character.  This is used for #include filenames.
1840 bool Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
1841   const char *NulCharacter = 0; // Does this string contain the \0 character?
1842   const char *AfterLessPos = CurPtr;
1843   char C = getAndAdvanceChar(CurPtr, Result);
1844   while (C != '>') {
1845     // Skip escaped characters.
1846     if (C == '\\') {
1847       // Skip the escaped character.
1848       getAndAdvanceChar(CurPtr, Result);
1849     } else if (C == '\n' || C == '\r' ||             // Newline.
1850                (C == 0 && (CurPtr-1 == BufferEnd ||  // End of file.
1851                            isCodeCompletionPoint(CurPtr-1)))) {
1852       // If the filename is unterminated, then it must just be a lone <
1853       // character.  Return this as such.
1854       FormTokenWithChars(Result, AfterLessPos, tok::less);
1855       return true;
1856     } else if (C == 0) {
1857       NulCharacter = CurPtr-1;
1858     }
1859     C = getAndAdvanceChar(CurPtr, Result);
1860   }
1861 
1862   // If a nul character existed in the string, warn about it.
1863   if (NulCharacter && !isLexingRawMode())
1864     Diag(NulCharacter, diag::null_in_string);
1865 
1866   // Update the location of token as well as BufferPtr.
1867   const char *TokStart = BufferPtr;
1868   FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
1869   Result.setLiteralData(TokStart);
1870   return true;
1871 }
1872 
1873 
1874 /// LexCharConstant - Lex the remainder of a character constant, after having
1875 /// lexed either ' or L' or u' or U'.
1876 bool Lexer::LexCharConstant(Token &Result, const char *CurPtr,
1877                             tok::TokenKind Kind) {
1878   const char *NulCharacter = 0; // Does this character contain the \0 character?
1879 
1880   if (!isLexingRawMode() &&
1881       (Kind == tok::utf16_char_constant || Kind == tok::utf32_char_constant))
1882     Diag(BufferPtr, getLangOpts().CPlusPlus
1883            ? diag::warn_cxx98_compat_unicode_literal
1884            : diag::warn_c99_compat_unicode_literal);
1885 
1886   char C = getAndAdvanceChar(CurPtr, Result);
1887   if (C == '\'') {
1888     if (!isLexingRawMode() && !LangOpts.AsmPreprocessor)
1889       Diag(BufferPtr, diag::ext_empty_character);
1890     FormTokenWithChars(Result, CurPtr, tok::unknown);
1891     return true;
1892   }
1893 
1894   while (C != '\'') {
1895     // Skip escaped characters.
1896     if (C == '\\')
1897       C = getAndAdvanceChar(CurPtr, Result);
1898 
1899     if (C == '\n' || C == '\r' ||             // Newline.
1900         (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
1901       if (!isLexingRawMode() && !LangOpts.AsmPreprocessor)
1902         Diag(BufferPtr, diag::ext_unterminated_char);
1903       FormTokenWithChars(Result, CurPtr-1, tok::unknown);
1904       return true;
1905     }
1906 
1907     if (C == 0) {
1908       if (isCodeCompletionPoint(CurPtr-1)) {
1909         PP->CodeCompleteNaturalLanguage();
1910         FormTokenWithChars(Result, CurPtr-1, tok::unknown);
1911         cutOffLexing();
1912         return true;
1913       }
1914 
1915       NulCharacter = CurPtr-1;
1916     }
1917     C = getAndAdvanceChar(CurPtr, Result);
1918   }
1919 
1920   // If we are in C++11, lex the optional ud-suffix.
1921   if (getLangOpts().CPlusPlus)
1922     CurPtr = LexUDSuffix(Result, CurPtr, false);
1923 
1924   // If a nul character existed in the character, warn about it.
1925   if (NulCharacter && !isLexingRawMode())
1926     Diag(NulCharacter, diag::null_in_char);
1927 
1928   // Update the location of token as well as BufferPtr.
1929   const char *TokStart = BufferPtr;
1930   FormTokenWithChars(Result, CurPtr, Kind);
1931   Result.setLiteralData(TokStart);
1932   return true;
1933 }
1934 
1935 /// SkipWhitespace - Efficiently skip over a series of whitespace characters.
1936 /// Update BufferPtr to point to the next non-whitespace character and return.
1937 ///
1938 /// This method forms a token and returns true if KeepWhitespaceMode is enabled.
1939 ///
1940 bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr,
1941                            bool &TokAtPhysicalStartOfLine) {
1942   // Whitespace - Skip it, then return the token after the whitespace.
1943   bool SawNewline = isVerticalWhitespace(CurPtr[-1]);
1944 
1945   unsigned char Char = *CurPtr;
1946 
1947   // Skip consecutive spaces efficiently.
1948   while (1) {
1949     // Skip horizontal whitespace very aggressively.
1950     while (isHorizontalWhitespace(Char))
1951       Char = *++CurPtr;
1952 
1953     // Otherwise if we have something other than whitespace, we're done.
1954     if (!isVerticalWhitespace(Char))
1955       break;
1956 
1957     if (ParsingPreprocessorDirective) {
1958       // End of preprocessor directive line, let LexTokenInternal handle this.
1959       BufferPtr = CurPtr;
1960       return false;
1961     }
1962 
1963     // OK, but handle newline.
1964     SawNewline = true;
1965     Char = *++CurPtr;
1966   }
1967 
1968   // If the client wants us to return whitespace, return it now.
1969   if (isKeepWhitespaceMode()) {
1970     FormTokenWithChars(Result, CurPtr, tok::unknown);
1971     if (SawNewline) {
1972       IsAtStartOfLine = true;
1973       IsAtPhysicalStartOfLine = true;
1974     }
1975     // FIXME: The next token will not have LeadingSpace set.
1976     return true;
1977   }
1978 
1979   // If this isn't immediately after a newline, there is leading space.
1980   char PrevChar = CurPtr[-1];
1981   bool HasLeadingSpace = !isVerticalWhitespace(PrevChar);
1982 
1983   Result.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
1984   if (SawNewline) {
1985     Result.setFlag(Token::StartOfLine);
1986     TokAtPhysicalStartOfLine = true;
1987   }
1988 
1989   BufferPtr = CurPtr;
1990   return false;
1991 }
1992 
1993 /// We have just read the // characters from input.  Skip until we find the
1994 /// newline character thats terminate the comment.  Then update BufferPtr and
1995 /// return.
1996 ///
1997 /// If we're in KeepCommentMode or any CommentHandler has inserted
1998 /// some tokens, this will store the first token and return true.
1999 bool Lexer::SkipLineComment(Token &Result, const char *CurPtr,
2000                             bool &TokAtPhysicalStartOfLine) {
2001   // If Line comments aren't explicitly enabled for this language, emit an
2002   // extension warning.
2003   if (!LangOpts.LineComment && !isLexingRawMode()) {
2004     Diag(BufferPtr, diag::ext_line_comment);
2005 
2006     // Mark them enabled so we only emit one warning for this translation
2007     // unit.
2008     LangOpts.LineComment = true;
2009   }
2010 
2011   // Scan over the body of the comment.  The common case, when scanning, is that
2012   // the comment contains normal ascii characters with nothing interesting in
2013   // them.  As such, optimize for this case with the inner loop.
2014   char C;
2015   do {
2016     C = *CurPtr;
2017     // Skip over characters in the fast loop.
2018     while (C != 0 &&                // Potentially EOF.
2019            C != '\n' && C != '\r')  // Newline or DOS-style newline.
2020       C = *++CurPtr;
2021 
2022     const char *NextLine = CurPtr;
2023     if (C != 0) {
2024       // We found a newline, see if it's escaped.
2025       const char *EscapePtr = CurPtr-1;
2026       bool HasSpace = false;
2027       while (isHorizontalWhitespace(*EscapePtr)) { // Skip whitespace.
2028         --EscapePtr;
2029         HasSpace = true;
2030       }
2031 
2032       if (*EscapePtr == '\\') // Escaped newline.
2033         CurPtr = EscapePtr;
2034       else if (EscapePtr[0] == '/' && EscapePtr[-1] == '?' &&
2035                EscapePtr[-2] == '?') // Trigraph-escaped newline.
2036         CurPtr = EscapePtr-2;
2037       else
2038         break; // This is a newline, we're done.
2039 
2040       // If there was space between the backslash and newline, warn about it.
2041       if (HasSpace && !isLexingRawMode())
2042         Diag(EscapePtr, diag::backslash_newline_space);
2043     }
2044 
2045     // Otherwise, this is a hard case.  Fall back on getAndAdvanceChar to
2046     // properly decode the character.  Read it in raw mode to avoid emitting
2047     // diagnostics about things like trigraphs.  If we see an escaped newline,
2048     // we'll handle it below.
2049     const char *OldPtr = CurPtr;
2050     bool OldRawMode = isLexingRawMode();
2051     LexingRawMode = true;
2052     C = getAndAdvanceChar(CurPtr, Result);
2053     LexingRawMode = OldRawMode;
2054 
2055     // If we only read only one character, then no special handling is needed.
2056     // We're done and can skip forward to the newline.
2057     if (C != 0 && CurPtr == OldPtr+1) {
2058       CurPtr = NextLine;
2059       break;
2060     }
2061 
2062     // If we read multiple characters, and one of those characters was a \r or
2063     // \n, then we had an escaped newline within the comment.  Emit diagnostic
2064     // unless the next line is also a // comment.
2065     if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
2066       for (; OldPtr != CurPtr; ++OldPtr)
2067         if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
2068           // Okay, we found a // comment that ends in a newline, if the next
2069           // line is also a // comment, but has spaces, don't emit a diagnostic.
2070           if (isWhitespace(C)) {
2071             const char *ForwardPtr = CurPtr;
2072             while (isWhitespace(*ForwardPtr))  // Skip whitespace.
2073               ++ForwardPtr;
2074             if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
2075               break;
2076           }
2077 
2078           if (!isLexingRawMode())
2079             Diag(OldPtr-1, diag::ext_multi_line_line_comment);
2080           break;
2081         }
2082     }
2083 
2084     if (CurPtr == BufferEnd+1) {
2085       --CurPtr;
2086       break;
2087     }
2088 
2089     if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) {
2090       PP->CodeCompleteNaturalLanguage();
2091       cutOffLexing();
2092       return false;
2093     }
2094 
2095   } while (C != '\n' && C != '\r');
2096 
2097   // Found but did not consume the newline.  Notify comment handlers about the
2098   // comment unless we're in a #if 0 block.
2099   if (PP && !isLexingRawMode() &&
2100       PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
2101                                             getSourceLocation(CurPtr)))) {
2102     BufferPtr = CurPtr;
2103     return true; // A token has to be returned.
2104   }
2105 
2106   // If we are returning comments as tokens, return this comment as a token.
2107   if (inKeepCommentMode())
2108     return SaveLineComment(Result, CurPtr);
2109 
2110   // If we are inside a preprocessor directive and we see the end of line,
2111   // return immediately, so that the lexer can return this as an EOD token.
2112   if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
2113     BufferPtr = CurPtr;
2114     return false;
2115   }
2116 
2117   // Otherwise, eat the \n character.  We don't care if this is a \n\r or
2118   // \r\n sequence.  This is an efficiency hack (because we know the \n can't
2119   // contribute to another token), it isn't needed for correctness.  Note that
2120   // this is ok even in KeepWhitespaceMode, because we would have returned the
2121   /// comment above in that mode.
2122   ++CurPtr;
2123 
2124   // The next returned token is at the start of the line.
2125   Result.setFlag(Token::StartOfLine);
2126   TokAtPhysicalStartOfLine = true;
2127   // No leading whitespace seen so far.
2128   Result.clearFlag(Token::LeadingSpace);
2129   BufferPtr = CurPtr;
2130   return false;
2131 }
2132 
2133 /// If in save-comment mode, package up this Line comment in an appropriate
2134 /// way and return it.
2135 bool Lexer::SaveLineComment(Token &Result, const char *CurPtr) {
2136   // If we're not in a preprocessor directive, just return the // comment
2137   // directly.
2138   FormTokenWithChars(Result, CurPtr, tok::comment);
2139 
2140   if (!ParsingPreprocessorDirective || LexingRawMode)
2141     return true;
2142 
2143   // If this Line-style comment is in a macro definition, transmogrify it into
2144   // a C-style block comment.
2145   bool Invalid = false;
2146   std::string Spelling = PP->getSpelling(Result, &Invalid);
2147   if (Invalid)
2148     return true;
2149 
2150   assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not line comment?");
2151   Spelling[1] = '*';   // Change prefix to "/*".
2152   Spelling += "*/";    // add suffix.
2153 
2154   Result.setKind(tok::comment);
2155   PP->CreateString(Spelling, Result,
2156                    Result.getLocation(), Result.getLocation());
2157   return true;
2158 }
2159 
2160 /// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
2161 /// character (either \\n or \\r) is part of an escaped newline sequence.  Issue
2162 /// a diagnostic if so.  We know that the newline is inside of a block comment.
2163 static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
2164                                                   Lexer *L) {
2165   assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
2166 
2167   // Back up off the newline.
2168   --CurPtr;
2169 
2170   // If this is a two-character newline sequence, skip the other character.
2171   if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
2172     // \n\n or \r\r -> not escaped newline.
2173     if (CurPtr[0] == CurPtr[1])
2174       return false;
2175     // \n\r or \r\n -> skip the newline.
2176     --CurPtr;
2177   }
2178 
2179   // If we have horizontal whitespace, skip over it.  We allow whitespace
2180   // between the slash and newline.
2181   bool HasSpace = false;
2182   while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
2183     --CurPtr;
2184     HasSpace = true;
2185   }
2186 
2187   // If we have a slash, we know this is an escaped newline.
2188   if (*CurPtr == '\\') {
2189     if (CurPtr[-1] != '*') return false;
2190   } else {
2191     // It isn't a slash, is it the ?? / trigraph?
2192     if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
2193         CurPtr[-3] != '*')
2194       return false;
2195 
2196     // This is the trigraph ending the comment.  Emit a stern warning!
2197     CurPtr -= 2;
2198 
2199     // If no trigraphs are enabled, warn that we ignored this trigraph and
2200     // ignore this * character.
2201     if (!L->getLangOpts().Trigraphs) {
2202       if (!L->isLexingRawMode())
2203         L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
2204       return false;
2205     }
2206     if (!L->isLexingRawMode())
2207       L->Diag(CurPtr, diag::trigraph_ends_block_comment);
2208   }
2209 
2210   // Warn about having an escaped newline between the */ characters.
2211   if (!L->isLexingRawMode())
2212     L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
2213 
2214   // If there was space between the backslash and newline, warn about it.
2215   if (HasSpace && !L->isLexingRawMode())
2216     L->Diag(CurPtr, diag::backslash_newline_space);
2217 
2218   return true;
2219 }
2220 
2221 #ifdef __SSE2__
2222 #include <emmintrin.h>
2223 #elif __ALTIVEC__
2224 #include <altivec.h>
2225 #undef bool
2226 #endif
2227 
2228 /// We have just read from input the / and * characters that started a comment.
2229 /// Read until we find the * and / characters that terminate the comment.
2230 /// Note that we don't bother decoding trigraphs or escaped newlines in block
2231 /// comments, because they cannot cause the comment to end.  The only thing
2232 /// that can happen is the comment could end with an escaped newline between
2233 /// the terminating * and /.
2234 ///
2235 /// If we're in KeepCommentMode or any CommentHandler has inserted
2236 /// some tokens, this will store the first token and return true.
2237 bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr,
2238                              bool &TokAtPhysicalStartOfLine) {
2239   // Scan one character past where we should, looking for a '/' character.  Once
2240   // we find it, check to see if it was preceded by a *.  This common
2241   // optimization helps people who like to put a lot of * characters in their
2242   // comments.
2243 
2244   // The first character we get with newlines and trigraphs skipped to handle
2245   // the degenerate /*/ case below correctly if the * has an escaped newline
2246   // after it.
2247   unsigned CharSize;
2248   unsigned char C = getCharAndSize(CurPtr, CharSize);
2249   CurPtr += CharSize;
2250   if (C == 0 && CurPtr == BufferEnd+1) {
2251     if (!isLexingRawMode())
2252       Diag(BufferPtr, diag::err_unterminated_block_comment);
2253     --CurPtr;
2254 
2255     // KeepWhitespaceMode should return this broken comment as a token.  Since
2256     // it isn't a well formed comment, just return it as an 'unknown' token.
2257     if (isKeepWhitespaceMode()) {
2258       FormTokenWithChars(Result, CurPtr, tok::unknown);
2259       return true;
2260     }
2261 
2262     BufferPtr = CurPtr;
2263     return false;
2264   }
2265 
2266   // Check to see if the first character after the '/*' is another /.  If so,
2267   // then this slash does not end the block comment, it is part of it.
2268   if (C == '/')
2269     C = *CurPtr++;
2270 
2271   while (1) {
2272     // Skip over all non-interesting characters until we find end of buffer or a
2273     // (probably ending) '/' character.
2274     if (CurPtr + 24 < BufferEnd &&
2275         // If there is a code-completion point avoid the fast scan because it
2276         // doesn't check for '\0'.
2277         !(PP && PP->getCodeCompletionFileLoc() == FileLoc)) {
2278       // While not aligned to a 16-byte boundary.
2279       while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
2280         C = *CurPtr++;
2281 
2282       if (C == '/') goto FoundSlash;
2283 
2284 #ifdef __SSE2__
2285       __m128i Slashes = _mm_set1_epi8('/');
2286       while (CurPtr+16 <= BufferEnd) {
2287         int cmp = _mm_movemask_epi8(_mm_cmpeq_epi8(*(const __m128i*)CurPtr,
2288                                     Slashes));
2289         if (cmp != 0) {
2290           // Adjust the pointer to point directly after the first slash. It's
2291           // not necessary to set C here, it will be overwritten at the end of
2292           // the outer loop.
2293           CurPtr += llvm::countTrailingZeros<unsigned>(cmp) + 1;
2294           goto FoundSlash;
2295         }
2296         CurPtr += 16;
2297       }
2298 #elif __ALTIVEC__
2299       __vector unsigned char Slashes = {
2300         '/', '/', '/', '/',  '/', '/', '/', '/',
2301         '/', '/', '/', '/',  '/', '/', '/', '/'
2302       };
2303       while (CurPtr+16 <= BufferEnd &&
2304              !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
2305         CurPtr += 16;
2306 #else
2307       // Scan for '/' quickly.  Many block comments are very large.
2308       while (CurPtr[0] != '/' &&
2309              CurPtr[1] != '/' &&
2310              CurPtr[2] != '/' &&
2311              CurPtr[3] != '/' &&
2312              CurPtr+4 < BufferEnd) {
2313         CurPtr += 4;
2314       }
2315 #endif
2316 
2317       // It has to be one of the bytes scanned, increment to it and read one.
2318       C = *CurPtr++;
2319     }
2320 
2321     // Loop to scan the remainder.
2322     while (C != '/' && C != '\0')
2323       C = *CurPtr++;
2324 
2325     if (C == '/') {
2326   FoundSlash:
2327       if (CurPtr[-2] == '*')  // We found the final */.  We're done!
2328         break;
2329 
2330       if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
2331         if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
2332           // We found the final */, though it had an escaped newline between the
2333           // * and /.  We're done!
2334           break;
2335         }
2336       }
2337       if (CurPtr[0] == '*' && CurPtr[1] != '/') {
2338         // If this is a /* inside of the comment, emit a warning.  Don't do this
2339         // if this is a /*/, which will end the comment.  This misses cases with
2340         // embedded escaped newlines, but oh well.
2341         if (!isLexingRawMode())
2342           Diag(CurPtr-1, diag::warn_nested_block_comment);
2343       }
2344     } else if (C == 0 && CurPtr == BufferEnd+1) {
2345       if (!isLexingRawMode())
2346         Diag(BufferPtr, diag::err_unterminated_block_comment);
2347       // Note: the user probably forgot a */.  We could continue immediately
2348       // after the /*, but this would involve lexing a lot of what really is the
2349       // comment, which surely would confuse the parser.
2350       --CurPtr;
2351 
2352       // KeepWhitespaceMode should return this broken comment as a token.  Since
2353       // it isn't a well formed comment, just return it as an 'unknown' token.
2354       if (isKeepWhitespaceMode()) {
2355         FormTokenWithChars(Result, CurPtr, tok::unknown);
2356         return true;
2357       }
2358 
2359       BufferPtr = CurPtr;
2360       return false;
2361     } else if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) {
2362       PP->CodeCompleteNaturalLanguage();
2363       cutOffLexing();
2364       return false;
2365     }
2366 
2367     C = *CurPtr++;
2368   }
2369 
2370   // Notify comment handlers about the comment unless we're in a #if 0 block.
2371   if (PP && !isLexingRawMode() &&
2372       PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
2373                                             getSourceLocation(CurPtr)))) {
2374     BufferPtr = CurPtr;
2375     return true; // A token has to be returned.
2376   }
2377 
2378   // If we are returning comments as tokens, return this comment as a token.
2379   if (inKeepCommentMode()) {
2380     FormTokenWithChars(Result, CurPtr, tok::comment);
2381     return true;
2382   }
2383 
2384   // It is common for the tokens immediately after a /**/ comment to be
2385   // whitespace.  Instead of going through the big switch, handle it
2386   // efficiently now.  This is safe even in KeepWhitespaceMode because we would
2387   // have already returned above with the comment as a token.
2388   if (isHorizontalWhitespace(*CurPtr)) {
2389     SkipWhitespace(Result, CurPtr+1, TokAtPhysicalStartOfLine);
2390     return false;
2391   }
2392 
2393   // Otherwise, just return so that the next character will be lexed as a token.
2394   BufferPtr = CurPtr;
2395   Result.setFlag(Token::LeadingSpace);
2396   return false;
2397 }
2398 
2399 //===----------------------------------------------------------------------===//
2400 // Primary Lexing Entry Points
2401 //===----------------------------------------------------------------------===//
2402 
2403 /// ReadToEndOfLine - Read the rest of the current preprocessor line as an
2404 /// uninterpreted string.  This switches the lexer out of directive mode.
2405 void Lexer::ReadToEndOfLine(SmallVectorImpl<char> *Result) {
2406   assert(ParsingPreprocessorDirective && ParsingFilename == false &&
2407          "Must be in a preprocessing directive!");
2408   Token Tmp;
2409 
2410   // CurPtr - Cache BufferPtr in an automatic variable.
2411   const char *CurPtr = BufferPtr;
2412   while (1) {
2413     char Char = getAndAdvanceChar(CurPtr, Tmp);
2414     switch (Char) {
2415     default:
2416       if (Result)
2417         Result->push_back(Char);
2418       break;
2419     case 0:  // Null.
2420       // Found end of file?
2421       if (CurPtr-1 != BufferEnd) {
2422         if (isCodeCompletionPoint(CurPtr-1)) {
2423           PP->CodeCompleteNaturalLanguage();
2424           cutOffLexing();
2425           return;
2426         }
2427 
2428         // Nope, normal character, continue.
2429         if (Result)
2430           Result->push_back(Char);
2431         break;
2432       }
2433       // FALL THROUGH.
2434     case '\r':
2435     case '\n':
2436       // Okay, we found the end of the line. First, back up past the \0, \r, \n.
2437       assert(CurPtr[-1] == Char && "Trigraphs for newline?");
2438       BufferPtr = CurPtr-1;
2439 
2440       // Next, lex the character, which should handle the EOD transition.
2441       Lex(Tmp);
2442       if (Tmp.is(tok::code_completion)) {
2443         if (PP)
2444           PP->CodeCompleteNaturalLanguage();
2445         Lex(Tmp);
2446       }
2447       assert(Tmp.is(tok::eod) && "Unexpected token!");
2448 
2449       // Finally, we're done;
2450       return;
2451     }
2452   }
2453 }
2454 
2455 /// LexEndOfFile - CurPtr points to the end of this file.  Handle this
2456 /// condition, reporting diagnostics and handling other edge cases as required.
2457 /// This returns true if Result contains a token, false if PP.Lex should be
2458 /// called again.
2459 bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
2460   // If we hit the end of the file while parsing a preprocessor directive,
2461   // end the preprocessor directive first.  The next token returned will
2462   // then be the end of file.
2463   if (ParsingPreprocessorDirective) {
2464     // Done parsing the "line".
2465     ParsingPreprocessorDirective = false;
2466     // Update the location of token as well as BufferPtr.
2467     FormTokenWithChars(Result, CurPtr, tok::eod);
2468 
2469     // Restore comment saving mode, in case it was disabled for directive.
2470     if (PP)
2471       resetExtendedTokenMode();
2472     return true;  // Have a token.
2473   }
2474 
2475   // If we are in raw mode, return this event as an EOF token.  Let the caller
2476   // that put us in raw mode handle the event.
2477   if (isLexingRawMode()) {
2478     Result.startToken();
2479     BufferPtr = BufferEnd;
2480     FormTokenWithChars(Result, BufferEnd, tok::eof);
2481     return true;
2482   }
2483 
2484   // Issue diagnostics for unterminated #if and missing newline.
2485 
2486   // If we are in a #if directive, emit an error.
2487   while (!ConditionalStack.empty()) {
2488     if (PP->getCodeCompletionFileLoc() != FileLoc)
2489       PP->Diag(ConditionalStack.back().IfLoc,
2490                diag::err_pp_unterminated_conditional);
2491     ConditionalStack.pop_back();
2492   }
2493 
2494   // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
2495   // a pedwarn.
2496   if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) {
2497     DiagnosticsEngine &Diags = PP->getDiagnostics();
2498     SourceLocation EndLoc = getSourceLocation(BufferEnd);
2499     unsigned DiagID;
2500 
2501     if (LangOpts.CPlusPlus11) {
2502       // C++11 [lex.phases] 2.2 p2
2503       // Prefer the C++98 pedantic compatibility warning over the generic,
2504       // non-extension, user-requested "missing newline at EOF" warning.
2505       if (Diags.getDiagnosticLevel(diag::warn_cxx98_compat_no_newline_eof,
2506                                    EndLoc) != DiagnosticsEngine::Ignored) {
2507         DiagID = diag::warn_cxx98_compat_no_newline_eof;
2508       } else {
2509         DiagID = diag::warn_no_newline_eof;
2510       }
2511     } else {
2512       DiagID = diag::ext_no_newline_eof;
2513     }
2514 
2515     Diag(BufferEnd, DiagID)
2516       << FixItHint::CreateInsertion(EndLoc, "\n");
2517   }
2518 
2519   BufferPtr = CurPtr;
2520 
2521   // Finally, let the preprocessor handle this.
2522   return PP->HandleEndOfFile(Result, isPragmaLexer());
2523 }
2524 
2525 /// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
2526 /// the specified lexer will return a tok::l_paren token, 0 if it is something
2527 /// else and 2 if there are no more tokens in the buffer controlled by the
2528 /// lexer.
2529 unsigned Lexer::isNextPPTokenLParen() {
2530   assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
2531 
2532   // Switch to 'skipping' mode.  This will ensure that we can lex a token
2533   // without emitting diagnostics, disables macro expansion, and will cause EOF
2534   // to return an EOF token instead of popping the include stack.
2535   LexingRawMode = true;
2536 
2537   // Save state that can be changed while lexing so that we can restore it.
2538   const char *TmpBufferPtr = BufferPtr;
2539   bool inPPDirectiveMode = ParsingPreprocessorDirective;
2540   bool atStartOfLine = IsAtStartOfLine;
2541   bool atPhysicalStartOfLine = IsAtPhysicalStartOfLine;
2542   bool leadingSpace = HasLeadingSpace;
2543 
2544   Token Tok;
2545   Lex(Tok);
2546 
2547   // Restore state that may have changed.
2548   BufferPtr = TmpBufferPtr;
2549   ParsingPreprocessorDirective = inPPDirectiveMode;
2550   HasLeadingSpace = leadingSpace;
2551   IsAtStartOfLine = atStartOfLine;
2552   IsAtPhysicalStartOfLine = atPhysicalStartOfLine;
2553 
2554   // Restore the lexer back to non-skipping mode.
2555   LexingRawMode = false;
2556 
2557   if (Tok.is(tok::eof))
2558     return 2;
2559   return Tok.is(tok::l_paren);
2560 }
2561 
2562 /// \brief Find the end of a version control conflict marker.
2563 static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd,
2564                                    ConflictMarkerKind CMK) {
2565   const char *Terminator = CMK == CMK_Perforce ? "<<<<\n" : ">>>>>>>";
2566   size_t TermLen = CMK == CMK_Perforce ? 5 : 7;
2567   StringRef RestOfBuffer(CurPtr+TermLen, BufferEnd-CurPtr-TermLen);
2568   size_t Pos = RestOfBuffer.find(Terminator);
2569   while (Pos != StringRef::npos) {
2570     // Must occur at start of line.
2571     if (RestOfBuffer[Pos-1] != '\r' &&
2572         RestOfBuffer[Pos-1] != '\n') {
2573       RestOfBuffer = RestOfBuffer.substr(Pos+TermLen);
2574       Pos = RestOfBuffer.find(Terminator);
2575       continue;
2576     }
2577     return RestOfBuffer.data()+Pos;
2578   }
2579   return 0;
2580 }
2581 
2582 /// IsStartOfConflictMarker - If the specified pointer is the start of a version
2583 /// control conflict marker like '<<<<<<<', recognize it as such, emit an error
2584 /// and recover nicely.  This returns true if it is a conflict marker and false
2585 /// if not.
2586 bool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
2587   // Only a conflict marker if it starts at the beginning of a line.
2588   if (CurPtr != BufferStart &&
2589       CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
2590     return false;
2591 
2592   // Check to see if we have <<<<<<< or >>>>.
2593   if ((BufferEnd-CurPtr < 8 || StringRef(CurPtr, 7) != "<<<<<<<") &&
2594       (BufferEnd-CurPtr < 6 || StringRef(CurPtr, 5) != ">>>> "))
2595     return false;
2596 
2597   // If we have a situation where we don't care about conflict markers, ignore
2598   // it.
2599   if (CurrentConflictMarkerState || isLexingRawMode())
2600     return false;
2601 
2602   ConflictMarkerKind Kind = *CurPtr == '<' ? CMK_Normal : CMK_Perforce;
2603 
2604   // Check to see if there is an ending marker somewhere in the buffer at the
2605   // start of a line to terminate this conflict marker.
2606   if (FindConflictEnd(CurPtr, BufferEnd, Kind)) {
2607     // We found a match.  We are really in a conflict marker.
2608     // Diagnose this, and ignore to the end of line.
2609     Diag(CurPtr, diag::err_conflict_marker);
2610     CurrentConflictMarkerState = Kind;
2611 
2612     // Skip ahead to the end of line.  We know this exists because the
2613     // end-of-conflict marker starts with \r or \n.
2614     while (*CurPtr != '\r' && *CurPtr != '\n') {
2615       assert(CurPtr != BufferEnd && "Didn't find end of line");
2616       ++CurPtr;
2617     }
2618     BufferPtr = CurPtr;
2619     return true;
2620   }
2621 
2622   // No end of conflict marker found.
2623   return false;
2624 }
2625 
2626 
2627 /// HandleEndOfConflictMarker - If this is a '====' or '||||' or '>>>>', or if
2628 /// it is '<<<<' and the conflict marker started with a '>>>>' marker, then it
2629 /// is the end of a conflict marker.  Handle it by ignoring up until the end of
2630 /// the line.  This returns true if it is a conflict marker and false if not.
2631 bool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
2632   // Only a conflict marker if it starts at the beginning of a line.
2633   if (CurPtr != BufferStart &&
2634       CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
2635     return false;
2636 
2637   // If we have a situation where we don't care about conflict markers, ignore
2638   // it.
2639   if (!CurrentConflictMarkerState || isLexingRawMode())
2640     return false;
2641 
2642   // Check to see if we have the marker (4 characters in a row).
2643   for (unsigned i = 1; i != 4; ++i)
2644     if (CurPtr[i] != CurPtr[0])
2645       return false;
2646 
2647   // If we do have it, search for the end of the conflict marker.  This could
2648   // fail if it got skipped with a '#if 0' or something.  Note that CurPtr might
2649   // be the end of conflict marker.
2650   if (const char *End = FindConflictEnd(CurPtr, BufferEnd,
2651                                         CurrentConflictMarkerState)) {
2652     CurPtr = End;
2653 
2654     // Skip ahead to the end of line.
2655     while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
2656       ++CurPtr;
2657 
2658     BufferPtr = CurPtr;
2659 
2660     // No longer in the conflict marker.
2661     CurrentConflictMarkerState = CMK_None;
2662     return true;
2663   }
2664 
2665   return false;
2666 }
2667 
2668 bool Lexer::isCodeCompletionPoint(const char *CurPtr) const {
2669   if (PP && PP->isCodeCompletionEnabled()) {
2670     SourceLocation Loc = FileLoc.getLocWithOffset(CurPtr-BufferStart);
2671     return Loc == PP->getCodeCompletionLoc();
2672   }
2673 
2674   return false;
2675 }
2676 
2677 uint32_t Lexer::tryReadUCN(const char *&StartPtr, const char *SlashLoc,
2678                            Token *Result) {
2679   unsigned CharSize;
2680   char Kind = getCharAndSize(StartPtr, CharSize);
2681 
2682   unsigned NumHexDigits;
2683   if (Kind == 'u')
2684     NumHexDigits = 4;
2685   else if (Kind == 'U')
2686     NumHexDigits = 8;
2687   else
2688     return 0;
2689 
2690   if (!LangOpts.CPlusPlus && !LangOpts.C99) {
2691     if (Result && !isLexingRawMode())
2692       Diag(SlashLoc, diag::warn_ucn_not_valid_in_c89);
2693     return 0;
2694   }
2695 
2696   const char *CurPtr = StartPtr + CharSize;
2697   const char *KindLoc = &CurPtr[-1];
2698 
2699   uint32_t CodePoint = 0;
2700   for (unsigned i = 0; i < NumHexDigits; ++i) {
2701     char C = getCharAndSize(CurPtr, CharSize);
2702 
2703     unsigned Value = llvm::hexDigitValue(C);
2704     if (Value == -1U) {
2705       if (Result && !isLexingRawMode()) {
2706         if (i == 0) {
2707           Diag(BufferPtr, diag::warn_ucn_escape_no_digits)
2708             << StringRef(KindLoc, 1);
2709         } else {
2710           Diag(BufferPtr, diag::warn_ucn_escape_incomplete);
2711 
2712           // If the user wrote \U1234, suggest a fixit to \u.
2713           if (i == 4 && NumHexDigits == 8) {
2714             CharSourceRange URange = makeCharRange(*this, KindLoc, KindLoc + 1);
2715             Diag(KindLoc, diag::note_ucn_four_not_eight)
2716               << FixItHint::CreateReplacement(URange, "u");
2717           }
2718         }
2719       }
2720 
2721       return 0;
2722     }
2723 
2724     CodePoint <<= 4;
2725     CodePoint += Value;
2726 
2727     CurPtr += CharSize;
2728   }
2729 
2730   if (Result) {
2731     Result->setFlag(Token::HasUCN);
2732     if (CurPtr - StartPtr == (ptrdiff_t)NumHexDigits + 2)
2733       StartPtr = CurPtr;
2734     else
2735       while (StartPtr != CurPtr)
2736         (void)getAndAdvanceChar(StartPtr, *Result);
2737   } else {
2738     StartPtr = CurPtr;
2739   }
2740 
2741   // Don't apply C family restrictions to UCNs in assembly mode
2742   if (LangOpts.AsmPreprocessor)
2743     return CodePoint;
2744 
2745   // C99 6.4.3p2: A universal character name shall not specify a character whose
2746   //   short identifier is less than 00A0 other than 0024 ($), 0040 (@), or
2747   //   0060 (`), nor one in the range D800 through DFFF inclusive.)
2748   // C++11 [lex.charset]p2: If the hexadecimal value for a
2749   //   universal-character-name corresponds to a surrogate code point (in the
2750   //   range 0xD800-0xDFFF, inclusive), the program is ill-formed. Additionally,
2751   //   if the hexadecimal value for a universal-character-name outside the
2752   //   c-char-sequence, s-char-sequence, or r-char-sequence of a character or
2753   //   string literal corresponds to a control character (in either of the
2754   //   ranges 0x00-0x1F or 0x7F-0x9F, both inclusive) or to a character in the
2755   //   basic source character set, the program is ill-formed.
2756   if (CodePoint < 0xA0) {
2757     if (CodePoint == 0x24 || CodePoint == 0x40 || CodePoint == 0x60)
2758       return CodePoint;
2759 
2760     // We don't use isLexingRawMode() here because we need to warn about bad
2761     // UCNs even when skipping preprocessing tokens in a #if block.
2762     if (Result && PP) {
2763       if (CodePoint < 0x20 || CodePoint >= 0x7F)
2764         Diag(BufferPtr, diag::err_ucn_control_character);
2765       else {
2766         char C = static_cast<char>(CodePoint);
2767         Diag(BufferPtr, diag::err_ucn_escape_basic_scs) << StringRef(&C, 1);
2768       }
2769     }
2770 
2771     return 0;
2772 
2773   } else if (CodePoint >= 0xD800 && CodePoint <= 0xDFFF) {
2774     // C++03 allows UCNs representing surrogate characters. C99 and C++11 don't.
2775     // We don't use isLexingRawMode() here because we need to diagnose bad
2776     // UCNs even when skipping preprocessing tokens in a #if block.
2777     if (Result && PP) {
2778       if (LangOpts.CPlusPlus && !LangOpts.CPlusPlus11)
2779         Diag(BufferPtr, diag::warn_ucn_escape_surrogate);
2780       else
2781         Diag(BufferPtr, diag::err_ucn_escape_invalid);
2782     }
2783     return 0;
2784   }
2785 
2786   return CodePoint;
2787 }
2788 
2789 bool Lexer::CheckUnicodeWhitespace(Token &Result, uint32_t C,
2790                                    const char *CurPtr) {
2791   static const llvm::sys::UnicodeCharSet UnicodeWhitespaceChars(
2792       UnicodeWhitespaceCharRanges);
2793   if (!isLexingRawMode() && !PP->isPreprocessedOutput() &&
2794       UnicodeWhitespaceChars.contains(C)) {
2795     Diag(BufferPtr, diag::ext_unicode_whitespace)
2796       << makeCharRange(*this, BufferPtr, CurPtr);
2797 
2798     Result.setFlag(Token::LeadingSpace);
2799     return true;
2800   }
2801   return false;
2802 }
2803 
2804 bool Lexer::LexUnicode(Token &Result, uint32_t C, const char *CurPtr) {
2805   if (isAllowedIDChar(C, LangOpts) && isAllowedInitiallyIDChar(C, LangOpts)) {
2806     if (!isLexingRawMode() && !ParsingPreprocessorDirective &&
2807         !PP->isPreprocessedOutput()) {
2808       maybeDiagnoseIDCharCompat(PP->getDiagnostics(), C,
2809                                 makeCharRange(*this, BufferPtr, CurPtr),
2810                                 /*IsFirst=*/true);
2811     }
2812 
2813     MIOpt.ReadToken();
2814     return LexIdentifier(Result, CurPtr);
2815   }
2816 
2817   if (!isLexingRawMode() && !ParsingPreprocessorDirective &&
2818       !PP->isPreprocessedOutput() &&
2819       !isASCII(*BufferPtr) && !isAllowedIDChar(C, LangOpts)) {
2820     // Non-ASCII characters tend to creep into source code unintentionally.
2821     // Instead of letting the parser complain about the unknown token,
2822     // just drop the character.
2823     // Note that we can /only/ do this when the non-ASCII character is actually
2824     // spelled as Unicode, not written as a UCN. The standard requires that
2825     // we not throw away any possible preprocessor tokens, but there's a
2826     // loophole in the mapping of Unicode characters to basic character set
2827     // characters that allows us to map these particular characters to, say,
2828     // whitespace.
2829     Diag(BufferPtr, diag::err_non_ascii)
2830       << FixItHint::CreateRemoval(makeCharRange(*this, BufferPtr, CurPtr));
2831 
2832     BufferPtr = CurPtr;
2833     return false;
2834   }
2835 
2836   // Otherwise, we have an explicit UCN or a character that's unlikely to show
2837   // up by accident.
2838   MIOpt.ReadToken();
2839   FormTokenWithChars(Result, CurPtr, tok::unknown);
2840   return true;
2841 }
2842 
2843 void Lexer::PropagateLineStartLeadingSpaceInfo(Token &Result) {
2844   IsAtStartOfLine = Result.isAtStartOfLine();
2845   HasLeadingSpace = Result.hasLeadingSpace();
2846   HasLeadingEmptyMacro = Result.hasLeadingEmptyMacro();
2847   // Note that this doesn't affect IsAtPhysicalStartOfLine.
2848 }
2849 
2850 bool Lexer::Lex(Token &Result) {
2851   // Start a new token.
2852   Result.startToken();
2853 
2854   // Set up misc whitespace flags for LexTokenInternal.
2855   if (IsAtStartOfLine) {
2856     Result.setFlag(Token::StartOfLine);
2857     IsAtStartOfLine = false;
2858   }
2859 
2860   if (HasLeadingSpace) {
2861     Result.setFlag(Token::LeadingSpace);
2862     HasLeadingSpace = false;
2863   }
2864 
2865   if (HasLeadingEmptyMacro) {
2866     Result.setFlag(Token::LeadingEmptyMacro);
2867     HasLeadingEmptyMacro = false;
2868   }
2869 
2870   bool atPhysicalStartOfLine = IsAtPhysicalStartOfLine;
2871   IsAtPhysicalStartOfLine = false;
2872   bool isRawLex = isLexingRawMode();
2873   (void) isRawLex;
2874   bool returnedToken = LexTokenInternal(Result, atPhysicalStartOfLine);
2875   // (After the LexTokenInternal call, the lexer might be destroyed.)
2876   assert((returnedToken || !isRawLex) && "Raw lex must succeed");
2877   return returnedToken;
2878 }
2879 
2880 /// LexTokenInternal - This implements a simple C family lexer.  It is an
2881 /// extremely performance critical piece of code.  This assumes that the buffer
2882 /// has a null character at the end of the file.  This returns a preprocessing
2883 /// token, not a normal token, as such, it is an internal interface.  It assumes
2884 /// that the Flags of result have been cleared before calling this.
2885 bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
2886 LexNextToken:
2887   // New token, can't need cleaning yet.
2888   Result.clearFlag(Token::NeedsCleaning);
2889   Result.setIdentifierInfo(0);
2890 
2891   // CurPtr - Cache BufferPtr in an automatic variable.
2892   const char *CurPtr = BufferPtr;
2893 
2894   // Small amounts of horizontal whitespace is very common between tokens.
2895   if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
2896     ++CurPtr;
2897     while ((*CurPtr == ' ') || (*CurPtr == '\t'))
2898       ++CurPtr;
2899 
2900     // If we are keeping whitespace and other tokens, just return what we just
2901     // skipped.  The next lexer invocation will return the token after the
2902     // whitespace.
2903     if (isKeepWhitespaceMode()) {
2904       FormTokenWithChars(Result, CurPtr, tok::unknown);
2905       // FIXME: The next token will not have LeadingSpace set.
2906       return true;
2907     }
2908 
2909     BufferPtr = CurPtr;
2910     Result.setFlag(Token::LeadingSpace);
2911   }
2912 
2913   unsigned SizeTmp, SizeTmp2;   // Temporaries for use in cases below.
2914 
2915   // Read a character, advancing over it.
2916   char Char = getAndAdvanceChar(CurPtr, Result);
2917   tok::TokenKind Kind;
2918 
2919   switch (Char) {
2920   case 0:  // Null.
2921     // Found end of file?
2922     if (CurPtr-1 == BufferEnd)
2923       return LexEndOfFile(Result, CurPtr-1);
2924 
2925     // Check if we are performing code completion.
2926     if (isCodeCompletionPoint(CurPtr-1)) {
2927       // Return the code-completion token.
2928       Result.startToken();
2929       FormTokenWithChars(Result, CurPtr, tok::code_completion);
2930       return true;
2931     }
2932 
2933     if (!isLexingRawMode())
2934       Diag(CurPtr-1, diag::null_in_file);
2935     Result.setFlag(Token::LeadingSpace);
2936     if (SkipWhitespace(Result, CurPtr, TokAtPhysicalStartOfLine))
2937       return true; // KeepWhitespaceMode
2938 
2939     // We know the lexer hasn't changed, so just try again with this lexer.
2940     // (We manually eliminate the tail call to avoid recursion.)
2941     goto LexNextToken;
2942 
2943   case 26:  // DOS & CP/M EOF: "^Z".
2944     // If we're in Microsoft extensions mode, treat this as end of file.
2945     if (LangOpts.MicrosoftExt)
2946       return LexEndOfFile(Result, CurPtr-1);
2947 
2948     // If Microsoft extensions are disabled, this is just random garbage.
2949     Kind = tok::unknown;
2950     break;
2951 
2952   case '\n':
2953   case '\r':
2954     // If we are inside a preprocessor directive and we see the end of line,
2955     // we know we are done with the directive, so return an EOD token.
2956     if (ParsingPreprocessorDirective) {
2957       // Done parsing the "line".
2958       ParsingPreprocessorDirective = false;
2959 
2960       // Restore comment saving mode, in case it was disabled for directive.
2961       if (PP)
2962         resetExtendedTokenMode();
2963 
2964       // Since we consumed a newline, we are back at the start of a line.
2965       IsAtStartOfLine = true;
2966       IsAtPhysicalStartOfLine = true;
2967 
2968       Kind = tok::eod;
2969       break;
2970     }
2971 
2972     // No leading whitespace seen so far.
2973     Result.clearFlag(Token::LeadingSpace);
2974 
2975     if (SkipWhitespace(Result, CurPtr, TokAtPhysicalStartOfLine))
2976       return true; // KeepWhitespaceMode
2977 
2978     // We only saw whitespace, so just try again with this lexer.
2979     // (We manually eliminate the tail call to avoid recursion.)
2980     goto LexNextToken;
2981   case ' ':
2982   case '\t':
2983   case '\f':
2984   case '\v':
2985   SkipHorizontalWhitespace:
2986     Result.setFlag(Token::LeadingSpace);
2987     if (SkipWhitespace(Result, CurPtr, TokAtPhysicalStartOfLine))
2988       return true; // KeepWhitespaceMode
2989 
2990   SkipIgnoredUnits:
2991     CurPtr = BufferPtr;
2992 
2993     // If the next token is obviously a // or /* */ comment, skip it efficiently
2994     // too (without going through the big switch stmt).
2995     if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
2996         LangOpts.LineComment &&
2997         (LangOpts.CPlusPlus || !LangOpts.TraditionalCPP)) {
2998       if (SkipLineComment(Result, CurPtr+2, TokAtPhysicalStartOfLine))
2999         return true; // There is a token to return.
3000       goto SkipIgnoredUnits;
3001     } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
3002       if (SkipBlockComment(Result, CurPtr+2, TokAtPhysicalStartOfLine))
3003         return true; // There is a token to return.
3004       goto SkipIgnoredUnits;
3005     } else if (isHorizontalWhitespace(*CurPtr)) {
3006       goto SkipHorizontalWhitespace;
3007     }
3008     // We only saw whitespace, so just try again with this lexer.
3009     // (We manually eliminate the tail call to avoid recursion.)
3010     goto LexNextToken;
3011 
3012   // C99 6.4.4.1: Integer Constants.
3013   // C99 6.4.4.2: Floating Constants.
3014   case '0': case '1': case '2': case '3': case '4':
3015   case '5': case '6': case '7': case '8': case '9':
3016     // Notify MIOpt that we read a non-whitespace/non-comment token.
3017     MIOpt.ReadToken();
3018     return LexNumericConstant(Result, CurPtr);
3019 
3020   case 'u':   // Identifier (uber) or C11/C++11 UTF-8 or UTF-16 string literal
3021     // Notify MIOpt that we read a non-whitespace/non-comment token.
3022     MIOpt.ReadToken();
3023 
3024     if (LangOpts.CPlusPlus11 || LangOpts.C11) {
3025       Char = getCharAndSize(CurPtr, SizeTmp);
3026 
3027       // UTF-16 string literal
3028       if (Char == '"')
3029         return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
3030                                 tok::utf16_string_literal);
3031 
3032       // UTF-16 character constant
3033       if (Char == '\'')
3034         return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result),
3035                                tok::utf16_char_constant);
3036 
3037       // UTF-16 raw string literal
3038       if (Char == 'R' && LangOpts.CPlusPlus11 &&
3039           getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"')
3040         return LexRawStringLiteral(Result,
3041                                ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
3042                                            SizeTmp2, Result),
3043                                tok::utf16_string_literal);
3044 
3045       if (Char == '8') {
3046         char Char2 = getCharAndSize(CurPtr + SizeTmp, SizeTmp2);
3047 
3048         // UTF-8 string literal
3049         if (Char2 == '"')
3050           return LexStringLiteral(Result,
3051                                ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
3052                                            SizeTmp2, Result),
3053                                tok::utf8_string_literal);
3054 
3055         if (Char2 == 'R' && LangOpts.CPlusPlus11) {
3056           unsigned SizeTmp3;
3057           char Char3 = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3);
3058           // UTF-8 raw string literal
3059           if (Char3 == '"') {
3060             return LexRawStringLiteral(Result,
3061                    ConsumeChar(ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
3062                                            SizeTmp2, Result),
3063                                SizeTmp3, Result),
3064                    tok::utf8_string_literal);
3065           }
3066         }
3067       }
3068     }
3069 
3070     // treat u like the start of an identifier.
3071     return LexIdentifier(Result, CurPtr);
3072 
3073   case 'U':   // Identifier (Uber) or C11/C++11 UTF-32 string literal
3074     // Notify MIOpt that we read a non-whitespace/non-comment token.
3075     MIOpt.ReadToken();
3076 
3077     if (LangOpts.CPlusPlus11 || LangOpts.C11) {
3078       Char = getCharAndSize(CurPtr, SizeTmp);
3079 
3080       // UTF-32 string literal
3081       if (Char == '"')
3082         return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
3083                                 tok::utf32_string_literal);
3084 
3085       // UTF-32 character constant
3086       if (Char == '\'')
3087         return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result),
3088                                tok::utf32_char_constant);
3089 
3090       // UTF-32 raw string literal
3091       if (Char == 'R' && LangOpts.CPlusPlus11 &&
3092           getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"')
3093         return LexRawStringLiteral(Result,
3094                                ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
3095                                            SizeTmp2, Result),
3096                                tok::utf32_string_literal);
3097     }
3098 
3099     // treat U like the start of an identifier.
3100     return LexIdentifier(Result, CurPtr);
3101 
3102   case 'R': // Identifier or C++0x raw string literal
3103     // Notify MIOpt that we read a non-whitespace/non-comment token.
3104     MIOpt.ReadToken();
3105 
3106     if (LangOpts.CPlusPlus11) {
3107       Char = getCharAndSize(CurPtr, SizeTmp);
3108 
3109       if (Char == '"')
3110         return LexRawStringLiteral(Result,
3111                                    ConsumeChar(CurPtr, SizeTmp, Result),
3112                                    tok::string_literal);
3113     }
3114 
3115     // treat R like the start of an identifier.
3116     return LexIdentifier(Result, CurPtr);
3117 
3118   case 'L':   // Identifier (Loony) or wide literal (L'x' or L"xyz").
3119     // Notify MIOpt that we read a non-whitespace/non-comment token.
3120     MIOpt.ReadToken();
3121     Char = getCharAndSize(CurPtr, SizeTmp);
3122 
3123     // Wide string literal.
3124     if (Char == '"')
3125       return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
3126                               tok::wide_string_literal);
3127 
3128     // Wide raw string literal.
3129     if (LangOpts.CPlusPlus11 && Char == 'R' &&
3130         getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"')
3131       return LexRawStringLiteral(Result,
3132                                ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
3133                                            SizeTmp2, Result),
3134                                tok::wide_string_literal);
3135 
3136     // Wide character constant.
3137     if (Char == '\'')
3138       return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result),
3139                              tok::wide_char_constant);
3140     // FALL THROUGH, treating L like the start of an identifier.
3141 
3142   // C99 6.4.2: Identifiers.
3143   case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
3144   case 'H': case 'I': case 'J': case 'K':    /*'L'*/case 'M': case 'N':
3145   case 'O': case 'P': case 'Q':    /*'R'*/case 'S': case 'T':    /*'U'*/
3146   case 'V': case 'W': case 'X': case 'Y': case 'Z':
3147   case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
3148   case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
3149   case 'o': case 'p': case 'q': case 'r': case 's': case 't':    /*'u'*/
3150   case 'v': case 'w': case 'x': case 'y': case 'z':
3151   case '_':
3152     // Notify MIOpt that we read a non-whitespace/non-comment token.
3153     MIOpt.ReadToken();
3154     return LexIdentifier(Result, CurPtr);
3155 
3156   case '$':   // $ in identifiers.
3157     if (LangOpts.DollarIdents) {
3158       if (!isLexingRawMode())
3159         Diag(CurPtr-1, diag::ext_dollar_in_identifier);
3160       // Notify MIOpt that we read a non-whitespace/non-comment token.
3161       MIOpt.ReadToken();
3162       return LexIdentifier(Result, CurPtr);
3163     }
3164 
3165     Kind = tok::unknown;
3166     break;
3167 
3168   // C99 6.4.4: Character Constants.
3169   case '\'':
3170     // Notify MIOpt that we read a non-whitespace/non-comment token.
3171     MIOpt.ReadToken();
3172     return LexCharConstant(Result, CurPtr, tok::char_constant);
3173 
3174   // C99 6.4.5: String Literals.
3175   case '"':
3176     // Notify MIOpt that we read a non-whitespace/non-comment token.
3177     MIOpt.ReadToken();
3178     return LexStringLiteral(Result, CurPtr, tok::string_literal);
3179 
3180   // C99 6.4.6: Punctuators.
3181   case '?':
3182     Kind = tok::question;
3183     break;
3184   case '[':
3185     Kind = tok::l_square;
3186     break;
3187   case ']':
3188     Kind = tok::r_square;
3189     break;
3190   case '(':
3191     Kind = tok::l_paren;
3192     break;
3193   case ')':
3194     Kind = tok::r_paren;
3195     break;
3196   case '{':
3197     Kind = tok::l_brace;
3198     break;
3199   case '}':
3200     Kind = tok::r_brace;
3201     break;
3202   case '.':
3203     Char = getCharAndSize(CurPtr, SizeTmp);
3204     if (Char >= '0' && Char <= '9') {
3205       // Notify MIOpt that we read a non-whitespace/non-comment token.
3206       MIOpt.ReadToken();
3207 
3208       return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
3209     } else if (LangOpts.CPlusPlus && Char == '*') {
3210       Kind = tok::periodstar;
3211       CurPtr += SizeTmp;
3212     } else if (Char == '.' &&
3213                getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
3214       Kind = tok::ellipsis;
3215       CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
3216                            SizeTmp2, Result);
3217     } else {
3218       Kind = tok::period;
3219     }
3220     break;
3221   case '&':
3222     Char = getCharAndSize(CurPtr, SizeTmp);
3223     if (Char == '&') {
3224       Kind = tok::ampamp;
3225       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3226     } else if (Char == '=') {
3227       Kind = tok::ampequal;
3228       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3229     } else {
3230       Kind = tok::amp;
3231     }
3232     break;
3233   case '*':
3234     if (getCharAndSize(CurPtr, SizeTmp) == '=') {
3235       Kind = tok::starequal;
3236       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3237     } else {
3238       Kind = tok::star;
3239     }
3240     break;
3241   case '+':
3242     Char = getCharAndSize(CurPtr, SizeTmp);
3243     if (Char == '+') {
3244       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3245       Kind = tok::plusplus;
3246     } else if (Char == '=') {
3247       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3248       Kind = tok::plusequal;
3249     } else {
3250       Kind = tok::plus;
3251     }
3252     break;
3253   case '-':
3254     Char = getCharAndSize(CurPtr, SizeTmp);
3255     if (Char == '-') {      // --
3256       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3257       Kind = tok::minusminus;
3258     } else if (Char == '>' && LangOpts.CPlusPlus &&
3259                getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {  // C++ ->*
3260       CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
3261                            SizeTmp2, Result);
3262       Kind = tok::arrowstar;
3263     } else if (Char == '>') {   // ->
3264       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3265       Kind = tok::arrow;
3266     } else if (Char == '=') {   // -=
3267       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3268       Kind = tok::minusequal;
3269     } else {
3270       Kind = tok::minus;
3271     }
3272     break;
3273   case '~':
3274     Kind = tok::tilde;
3275     break;
3276   case '!':
3277     if (getCharAndSize(CurPtr, SizeTmp) == '=') {
3278       Kind = tok::exclaimequal;
3279       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3280     } else {
3281       Kind = tok::exclaim;
3282     }
3283     break;
3284   case '/':
3285     // 6.4.9: Comments
3286     Char = getCharAndSize(CurPtr, SizeTmp);
3287     if (Char == '/') {         // Line comment.
3288       // Even if Line comments are disabled (e.g. in C89 mode), we generally
3289       // want to lex this as a comment.  There is one problem with this though,
3290       // that in one particular corner case, this can change the behavior of the
3291       // resultant program.  For example, In  "foo //**/ bar", C89 would lex
3292       // this as "foo / bar" and langauges with Line comments would lex it as
3293       // "foo".  Check to see if the character after the second slash is a '*'.
3294       // If so, we will lex that as a "/" instead of the start of a comment.
3295       // However, we never do this if we are just preprocessing.
3296       bool TreatAsComment = LangOpts.LineComment &&
3297                             (LangOpts.CPlusPlus || !LangOpts.TraditionalCPP);
3298       if (!TreatAsComment)
3299         if (!(PP && PP->isPreprocessedOutput()))
3300           TreatAsComment = getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*';
3301 
3302       if (TreatAsComment) {
3303         if (SkipLineComment(Result, ConsumeChar(CurPtr, SizeTmp, Result),
3304                             TokAtPhysicalStartOfLine))
3305           return true; // There is a token to return.
3306 
3307         // It is common for the tokens immediately after a // comment to be
3308         // whitespace (indentation for the next line).  Instead of going through
3309         // the big switch, handle it efficiently now.
3310         goto SkipIgnoredUnits;
3311       }
3312     }
3313 
3314     if (Char == '*') {  // /**/ comment.
3315       if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result),
3316                            TokAtPhysicalStartOfLine))
3317         return true; // There is a token to return.
3318 
3319       // We only saw whitespace, so just try again with this lexer.
3320       // (We manually eliminate the tail call to avoid recursion.)
3321       goto LexNextToken;
3322     }
3323 
3324     if (Char == '=') {
3325       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3326       Kind = tok::slashequal;
3327     } else {
3328       Kind = tok::slash;
3329     }
3330     break;
3331   case '%':
3332     Char = getCharAndSize(CurPtr, SizeTmp);
3333     if (Char == '=') {
3334       Kind = tok::percentequal;
3335       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3336     } else if (LangOpts.Digraphs && Char == '>') {
3337       Kind = tok::r_brace;                             // '%>' -> '}'
3338       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3339     } else if (LangOpts.Digraphs && Char == ':') {
3340       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3341       Char = getCharAndSize(CurPtr, SizeTmp);
3342       if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
3343         Kind = tok::hashhash;                          // '%:%:' -> '##'
3344         CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
3345                              SizeTmp2, Result);
3346       } else if (Char == '@' && LangOpts.MicrosoftExt) {// %:@ -> #@ -> Charize
3347         CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3348         if (!isLexingRawMode())
3349           Diag(BufferPtr, diag::ext_charize_microsoft);
3350         Kind = tok::hashat;
3351       } else {                                         // '%:' -> '#'
3352         // We parsed a # character.  If this occurs at the start of the line,
3353         // it's actually the start of a preprocessing directive.  Callback to
3354         // the preprocessor to handle it.
3355         // FIXME: -fpreprocessed mode??
3356         if (TokAtPhysicalStartOfLine && !LexingRawMode && !Is_PragmaLexer)
3357           goto HandleDirective;
3358 
3359         Kind = tok::hash;
3360       }
3361     } else {
3362       Kind = tok::percent;
3363     }
3364     break;
3365   case '<':
3366     Char = getCharAndSize(CurPtr, SizeTmp);
3367     if (ParsingFilename) {
3368       return LexAngledStringLiteral(Result, CurPtr);
3369     } else if (Char == '<') {
3370       char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
3371       if (After == '=') {
3372         Kind = tok::lesslessequal;
3373         CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
3374                              SizeTmp2, Result);
3375       } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
3376         // If this is actually a '<<<<<<<' version control conflict marker,
3377         // recognize it as such and recover nicely.
3378         goto LexNextToken;
3379       } else if (After == '<' && HandleEndOfConflictMarker(CurPtr-1)) {
3380         // If this is '<<<<' and we're in a Perforce-style conflict marker,
3381         // ignore it.
3382         goto LexNextToken;
3383       } else if (LangOpts.CUDA && After == '<') {
3384         Kind = tok::lesslessless;
3385         CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
3386                              SizeTmp2, Result);
3387       } else {
3388         CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3389         Kind = tok::lessless;
3390       }
3391     } else if (Char == '=') {
3392       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3393       Kind = tok::lessequal;
3394     } else if (LangOpts.Digraphs && Char == ':') {     // '<:' -> '['
3395       if (LangOpts.CPlusPlus11 &&
3396           getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == ':') {
3397         // C++0x [lex.pptoken]p3:
3398         //  Otherwise, if the next three characters are <:: and the subsequent
3399         //  character is neither : nor >, the < is treated as a preprocessor
3400         //  token by itself and not as the first character of the alternative
3401         //  token <:.
3402         unsigned SizeTmp3;
3403         char After = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3);
3404         if (After != ':' && After != '>') {
3405           Kind = tok::less;
3406           if (!isLexingRawMode())
3407             Diag(BufferPtr, diag::warn_cxx98_compat_less_colon_colon);
3408           break;
3409         }
3410       }
3411 
3412       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3413       Kind = tok::l_square;
3414     } else if (LangOpts.Digraphs && Char == '%') {     // '<%' -> '{'
3415       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3416       Kind = tok::l_brace;
3417     } else {
3418       Kind = tok::less;
3419     }
3420     break;
3421   case '>':
3422     Char = getCharAndSize(CurPtr, SizeTmp);
3423     if (Char == '=') {
3424       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3425       Kind = tok::greaterequal;
3426     } else if (Char == '>') {
3427       char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
3428       if (After == '=') {
3429         CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
3430                              SizeTmp2, Result);
3431         Kind = tok::greatergreaterequal;
3432       } else if (After == '>' && IsStartOfConflictMarker(CurPtr-1)) {
3433         // If this is actually a '>>>>' conflict marker, recognize it as such
3434         // and recover nicely.
3435         goto LexNextToken;
3436       } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
3437         // If this is '>>>>>>>' and we're in a conflict marker, ignore it.
3438         goto LexNextToken;
3439       } else if (LangOpts.CUDA && After == '>') {
3440         Kind = tok::greatergreatergreater;
3441         CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
3442                              SizeTmp2, Result);
3443       } else {
3444         CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3445         Kind = tok::greatergreater;
3446       }
3447 
3448     } else {
3449       Kind = tok::greater;
3450     }
3451     break;
3452   case '^':
3453     Char = getCharAndSize(CurPtr, SizeTmp);
3454     if (Char == '=') {
3455       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3456       Kind = tok::caretequal;
3457     } else {
3458       Kind = tok::caret;
3459     }
3460     break;
3461   case '|':
3462     Char = getCharAndSize(CurPtr, SizeTmp);
3463     if (Char == '=') {
3464       Kind = tok::pipeequal;
3465       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3466     } else if (Char == '|') {
3467       // If this is '|||||||' and we're in a conflict marker, ignore it.
3468       if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
3469         goto LexNextToken;
3470       Kind = tok::pipepipe;
3471       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3472     } else {
3473       Kind = tok::pipe;
3474     }
3475     break;
3476   case ':':
3477     Char = getCharAndSize(CurPtr, SizeTmp);
3478     if (LangOpts.Digraphs && Char == '>') {
3479       Kind = tok::r_square; // ':>' -> ']'
3480       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3481     } else if (LangOpts.CPlusPlus && Char == ':') {
3482       Kind = tok::coloncolon;
3483       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3484     } else {
3485       Kind = tok::colon;
3486     }
3487     break;
3488   case ';':
3489     Kind = tok::semi;
3490     break;
3491   case '=':
3492     Char = getCharAndSize(CurPtr, SizeTmp);
3493     if (Char == '=') {
3494       // If this is '====' and we're in a conflict marker, ignore it.
3495       if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
3496         goto LexNextToken;
3497 
3498       Kind = tok::equalequal;
3499       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3500     } else {
3501       Kind = tok::equal;
3502     }
3503     break;
3504   case ',':
3505     Kind = tok::comma;
3506     break;
3507   case '#':
3508     Char = getCharAndSize(CurPtr, SizeTmp);
3509     if (Char == '#') {
3510       Kind = tok::hashhash;
3511       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3512     } else if (Char == '@' && LangOpts.MicrosoftExt) {  // #@ -> Charize
3513       Kind = tok::hashat;
3514       if (!isLexingRawMode())
3515         Diag(BufferPtr, diag::ext_charize_microsoft);
3516       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3517     } else {
3518       // We parsed a # character.  If this occurs at the start of the line,
3519       // it's actually the start of a preprocessing directive.  Callback to
3520       // the preprocessor to handle it.
3521       // FIXME: -fpreprocessed mode??
3522       if (TokAtPhysicalStartOfLine && !LexingRawMode && !Is_PragmaLexer)
3523         goto HandleDirective;
3524 
3525       Kind = tok::hash;
3526     }
3527     break;
3528 
3529   case '@':
3530     // Objective C support.
3531     if (CurPtr[-1] == '@' && LangOpts.ObjC1)
3532       Kind = tok::at;
3533     else
3534       Kind = tok::unknown;
3535     break;
3536 
3537   // UCNs (C99 6.4.3, C++11 [lex.charset]p2)
3538   case '\\':
3539     if (uint32_t CodePoint = tryReadUCN(CurPtr, BufferPtr, &Result)) {
3540       if (CheckUnicodeWhitespace(Result, CodePoint, CurPtr)) {
3541         if (SkipWhitespace(Result, CurPtr, TokAtPhysicalStartOfLine))
3542           return true; // KeepWhitespaceMode
3543 
3544         // We only saw whitespace, so just try again with this lexer.
3545         // (We manually eliminate the tail call to avoid recursion.)
3546         goto LexNextToken;
3547       }
3548 
3549       return LexUnicode(Result, CodePoint, CurPtr);
3550     }
3551 
3552     Kind = tok::unknown;
3553     break;
3554 
3555   default: {
3556     if (isASCII(Char)) {
3557       Kind = tok::unknown;
3558       break;
3559     }
3560 
3561     UTF32 CodePoint;
3562 
3563     // We can't just reset CurPtr to BufferPtr because BufferPtr may point to
3564     // an escaped newline.
3565     --CurPtr;
3566     ConversionResult Status =
3567         llvm::convertUTF8Sequence((const UTF8 **)&CurPtr,
3568                                   (const UTF8 *)BufferEnd,
3569                                   &CodePoint,
3570                                   strictConversion);
3571     if (Status == conversionOK) {
3572       if (CheckUnicodeWhitespace(Result, CodePoint, CurPtr)) {
3573         if (SkipWhitespace(Result, CurPtr, TokAtPhysicalStartOfLine))
3574           return true; // KeepWhitespaceMode
3575 
3576         // We only saw whitespace, so just try again with this lexer.
3577         // (We manually eliminate the tail call to avoid recursion.)
3578         goto LexNextToken;
3579       }
3580       return LexUnicode(Result, CodePoint, CurPtr);
3581     }
3582 
3583     if (isLexingRawMode() || ParsingPreprocessorDirective ||
3584         PP->isPreprocessedOutput()) {
3585       ++CurPtr;
3586       Kind = tok::unknown;
3587       break;
3588     }
3589 
3590     // Non-ASCII characters tend to creep into source code unintentionally.
3591     // Instead of letting the parser complain about the unknown token,
3592     // just diagnose the invalid UTF-8, then drop the character.
3593     Diag(CurPtr, diag::err_invalid_utf8);
3594 
3595     BufferPtr = CurPtr+1;
3596     // We're pretending the character didn't exist, so just try again with
3597     // this lexer.
3598     // (We manually eliminate the tail call to avoid recursion.)
3599     goto LexNextToken;
3600   }
3601   }
3602 
3603   // Notify MIOpt that we read a non-whitespace/non-comment token.
3604   MIOpt.ReadToken();
3605 
3606   // Update the location of token as well as BufferPtr.
3607   FormTokenWithChars(Result, CurPtr, Kind);
3608   return true;
3609 
3610 HandleDirective:
3611   // We parsed a # character and it's the start of a preprocessing directive.
3612 
3613   FormTokenWithChars(Result, CurPtr, tok::hash);
3614   PP->HandleDirective(Result);
3615 
3616   if (PP->hadModuleLoaderFatalFailure()) {
3617     // With a fatal failure in the module loader, we abort parsing.
3618     assert(Result.is(tok::eof) && "Preprocessor did not set tok:eof");
3619     return true;
3620   }
3621 
3622   // We parsed the directive; lex a token with the new state.
3623   return false;
3624 }
3625