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