Bug Summary

File:build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/clang/lib/Lex/Lexer.cpp
Warning:line 1169, column 10
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name Lexer.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm -resource-dir /usr/lib/llvm-15/lib/clang/15.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/clang/lib/Lex -I /build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/clang/lib/Lex -I /build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/clang/include -I tools/clang/include -I include -I /build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-15/lib/clang/15.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-04-20-140412-16051-1 -x c++ /build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/clang/lib/Lex/Lexer.cpp

/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/clang/lib/Lex/Lexer.cpp

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

/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/clang/include/clang/Lex/Lexer.h

1//===- Lexer.h - C Language Family Lexer ------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the Lexer interface.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LEX_LEXER_H
14#define LLVM_CLANG_LEX_LEXER_H
15
16#include "clang/Basic/SourceLocation.h"
17#include "clang/Basic/TokenKinds.h"
18#include "clang/Lex/PreprocessorLexer.h"
19#include "clang/Lex/Token.h"
20#include "llvm/ADT/Optional.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include <cassert>
24#include <cstdint>
25#include <string>
26
27namespace llvm {
28
29class MemoryBufferRef;
30
31} // namespace llvm
32
33namespace clang {
34
35class DiagnosticBuilder;
36class Preprocessor;
37class SourceManager;
38class LangOptions;
39
40/// ConflictMarkerKind - Kinds of conflict marker which the lexer might be
41/// recovering from.
42enum ConflictMarkerKind {
43 /// Not within a conflict marker.
44 CMK_None,
45
46 /// A normal or diff3 conflict marker, initiated by at least 7 "<"s,
47 /// separated by at least 7 "="s or "|"s, and terminated by at least 7 ">"s.
48 CMK_Normal,
49
50 /// A Perforce-style conflict marker, initiated by 4 ">"s,
51 /// separated by 4 "="s, and terminated by 4 "<"s.
52 CMK_Perforce
53};
54
55/// Describes the bounds (start, size) of the preamble and a flag required by
56/// PreprocessorOptions::PrecompiledPreambleBytes.
57/// The preamble includes the BOM, if any.
58struct PreambleBounds {
59 /// Size of the preamble in bytes.
60 unsigned Size;
61
62 /// Whether the preamble ends at the start of a new line.
63 ///
64 /// Used to inform the lexer as to whether it's starting at the beginning of
65 /// a line after skipping the preamble.
66 bool PreambleEndsAtStartOfLine;
67
68 PreambleBounds(unsigned Size, bool PreambleEndsAtStartOfLine)
69 : Size(Size), PreambleEndsAtStartOfLine(PreambleEndsAtStartOfLine) {}
70};
71
72/// Lexer - This provides a simple interface that turns a text buffer into a
73/// stream of tokens. This provides no support for file reading or buffering,
74/// or buffering/seeking of tokens, only forward lexing is supported. It relies
75/// on the specified Preprocessor object to handle preprocessor directives, etc.
76class Lexer : public PreprocessorLexer {
77 friend class Preprocessor;
78
79 void anchor() override;
80
81 //===--------------------------------------------------------------------===//
82 // Constant configuration values for this lexer.
83
84 // Start of the buffer.
85 const char *BufferStart;
86
87 // End of the buffer.
88 const char *BufferEnd;
89
90 // Location for start of file.
91 SourceLocation FileLoc;
92
93 // LangOpts enabled by this language.
94 // Storing LangOptions as reference here is important from performance point
95 // of view. Lack of reference means that LangOptions copy constructor would be
96 // called by Lexer(..., const LangOptions &LangOpts,...). Given that local
97 // Lexer objects are created thousands times (in Lexer::getRawToken,
98 // Preprocessor::EnterSourceFile and other places) during single module
99 // processing in frontend it would make std::vector<std::string> copy
100 // constructors surprisingly hot.
101 const LangOptions &LangOpts;
102
103 // True if '//' line comments are enabled.
104 bool LineComment;
105
106 // True if lexer for _Pragma handling.
107 bool Is_PragmaLexer;
108
109 //===--------------------------------------------------------------------===//
110 // Context-specific lexing flags set by the preprocessor.
111 //
112
113 /// ExtendedTokenMode - The lexer can optionally keep comments and whitespace
114 /// and return them as tokens. This is used for -C and -CC modes, and
115 /// whitespace preservation can be useful for some clients that want to lex
116 /// the file in raw mode and get every character from the file.
117 ///
118 /// When this is set to 2 it returns comments and whitespace. When set to 1
119 /// it returns comments, when it is set to 0 it returns normal tokens only.
120 unsigned char ExtendedTokenMode;
121
122 //===--------------------------------------------------------------------===//
123 // Context that changes as the file is lexed.
124 // NOTE: any state that mutates when in raw mode must have save/restore code
125 // in Lexer::isNextPPTokenLParen.
126
127 // BufferPtr - Current pointer into the buffer. This is the next character
128 // to be lexed.
129 const char *BufferPtr;
130
131 // IsAtStartOfLine - True if the next lexed token should get the "start of
132 // line" flag set on it.
133 bool IsAtStartOfLine;
134
135 bool IsAtPhysicalStartOfLine;
136
137 bool HasLeadingSpace;
138
139 bool HasLeadingEmptyMacro;
140
141 /// True if this is the first time we're lexing the input file.
142 bool IsFirstTimeLexingFile;
143
144 // NewLinePtr - A pointer to new line character '\n' being lexed. For '\r\n',
145 // it also points to '\n.'
146 const char *NewLinePtr;
147
148 // CurrentConflictMarkerState - The kind of conflict marker we are handling.
149 ConflictMarkerKind CurrentConflictMarkerState;
150
151 void InitLexer(const char *BufStart, const char *BufPtr, const char *BufEnd);
152
153public:
154 /// Lexer constructor - Create a new lexer object for the specified buffer
155 /// with the specified preprocessor managing the lexing process. This lexer
156 /// assumes that the associated file buffer and Preprocessor objects will
157 /// outlive it, so it doesn't take ownership of either of them.
158 Lexer(FileID FID, const llvm::MemoryBufferRef &InputFile, Preprocessor &PP,
159 bool IsFirstIncludeOfFile = true);
160
161 /// Lexer constructor - Create a new raw lexer object. This object is only
162 /// suitable for calls to 'LexFromRawLexer'. This lexer assumes that the
163 /// text range will outlive it, so it doesn't take ownership of it.
164 Lexer(SourceLocation FileLoc, const LangOptions &LangOpts,
165 const char *BufStart, const char *BufPtr, const char *BufEnd,
166 bool IsFirstIncludeOfFile = true);
167
168 /// Lexer constructor - Create a new raw lexer object. This object is only
169 /// suitable for calls to 'LexFromRawLexer'. This lexer assumes that the
170 /// text range will outlive it, so it doesn't take ownership of it.
171 Lexer(FileID FID, const llvm::MemoryBufferRef &FromFile,
172 const SourceManager &SM, const LangOptions &LangOpts,
173 bool IsFirstIncludeOfFile = true);
174
175 Lexer(const Lexer &) = delete;
176 Lexer &operator=(const Lexer &) = delete;
177
178 /// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
179 /// _Pragma expansion. This has a variety of magic semantics that this method
180 /// sets up. It returns a new'd Lexer that must be delete'd when done.
181 static Lexer *Create_PragmaLexer(SourceLocation SpellingLoc,
182 SourceLocation ExpansionLocStart,
183 SourceLocation ExpansionLocEnd,
184 unsigned TokLen, Preprocessor &PP);
185
186 /// getFileLoc - Return the File Location for the file we are lexing out of.
187 /// The physical location encodes the location where the characters come from,
188 /// the virtual location encodes where we should *claim* the characters came
189 /// from. Currently this is only used by _Pragma handling.
190 SourceLocation getFileLoc() const { return FileLoc; }
191
192private:
193 /// Lex - Return the next token in the file. If this is the end of file, it
194 /// return the tok::eof token. This implicitly involves the preprocessor.
195 bool Lex(Token &Result);
196
197public:
198 /// isPragmaLexer - Returns true if this Lexer is being used to lex a pragma.
199 bool isPragmaLexer() const { return Is_PragmaLexer; }
200
201private:
202 /// IndirectLex - An indirect call to 'Lex' that can be invoked via
203 /// the PreprocessorLexer interface.
204 void IndirectLex(Token &Result) override { Lex(Result); }
205
206public:
207 /// LexFromRawLexer - Lex a token from a designated raw lexer (one with no
208 /// associated preprocessor object. Return true if the 'next character to
209 /// read' pointer points at the end of the lexer buffer, false otherwise.
210 bool LexFromRawLexer(Token &Result) {
211 assert(LexingRawMode && "Not already in raw mode!")(static_cast <bool> (LexingRawMode && "Not already in raw mode!"
) ? void (0) : __assert_fail ("LexingRawMode && \"Not already in raw mode!\""
, "clang/include/clang/Lex/Lexer.h", 211, __extension__ __PRETTY_FUNCTION__
))
;
212 Lex(Result);
213 // Note that lexing to the end of the buffer doesn't implicitly delete the
214 // lexer when in raw mode.
215 return BufferPtr == BufferEnd;
216 }
217
218 /// isKeepWhitespaceMode - Return true if the lexer should return tokens for
219 /// every character in the file, including whitespace and comments. This
220 /// should only be used in raw mode, as the preprocessor is not prepared to
221 /// deal with the excess tokens.
222 bool isKeepWhitespaceMode() const {
223 return ExtendedTokenMode > 1;
224 }
225
226 /// SetKeepWhitespaceMode - This method lets clients enable or disable
227 /// whitespace retention mode.
228 void SetKeepWhitespaceMode(bool Val) {
229 assert((!Val || LexingRawMode || LangOpts.TraditionalCPP) &&(static_cast <bool> ((!Val || LexingRawMode || LangOpts
.TraditionalCPP) && "Can only retain whitespace in raw mode or -traditional-cpp"
) ? void (0) : __assert_fail ("(!Val || LexingRawMode || LangOpts.TraditionalCPP) && \"Can only retain whitespace in raw mode or -traditional-cpp\""
, "clang/include/clang/Lex/Lexer.h", 230, __extension__ __PRETTY_FUNCTION__
))
230 "Can only retain whitespace in raw mode or -traditional-cpp")(static_cast <bool> ((!Val || LexingRawMode || LangOpts
.TraditionalCPP) && "Can only retain whitespace in raw mode or -traditional-cpp"
) ? void (0) : __assert_fail ("(!Val || LexingRawMode || LangOpts.TraditionalCPP) && \"Can only retain whitespace in raw mode or -traditional-cpp\""
, "clang/include/clang/Lex/Lexer.h", 230, __extension__ __PRETTY_FUNCTION__
))
;
231 ExtendedTokenMode = Val ? 2 : 0;
232 }
233
234 /// inKeepCommentMode - Return true if the lexer should return comments as
235 /// tokens.
236 bool inKeepCommentMode() const {
237 return ExtendedTokenMode > 0;
238 }
239
240 /// SetCommentRetentionMode - Change the comment retention mode of the lexer
241 /// to the specified mode. This is really only useful when lexing in raw
242 /// mode, because otherwise the lexer needs to manage this.
243 void SetCommentRetentionState(bool Mode) {
244 assert(!isKeepWhitespaceMode() &&(static_cast <bool> (!isKeepWhitespaceMode() &&
"Can't play with comment retention state when retaining whitespace"
) ? void (0) : __assert_fail ("!isKeepWhitespaceMode() && \"Can't play with comment retention state when retaining whitespace\""
, "clang/include/clang/Lex/Lexer.h", 245, __extension__ __PRETTY_FUNCTION__
))
245 "Can't play with comment retention state when retaining whitespace")(static_cast <bool> (!isKeepWhitespaceMode() &&
"Can't play with comment retention state when retaining whitespace"
) ? void (0) : __assert_fail ("!isKeepWhitespaceMode() && \"Can't play with comment retention state when retaining whitespace\""
, "clang/include/clang/Lex/Lexer.h", 245, __extension__ __PRETTY_FUNCTION__
))
;
246 ExtendedTokenMode = Mode ? 1 : 0;
247 }
248
249 /// Sets the extended token mode back to its initial value, according to the
250 /// language options and preprocessor. This controls whether the lexer
251 /// produces comment and whitespace tokens.
252 ///
253 /// This requires the lexer to have an associated preprocessor. A standalone
254 /// lexer has nothing to reset to.
255 void resetExtendedTokenMode();
256
257 /// Gets source code buffer.
258 StringRef getBuffer() const {
259 return StringRef(BufferStart, BufferEnd - BufferStart);
260 }
261
262 /// ReadToEndOfLine - Read the rest of the current preprocessor line as an
263 /// uninterpreted string. This switches the lexer out of directive mode.
264 void ReadToEndOfLine(SmallVectorImpl<char> *Result = nullptr);
265
266
267 /// Diag - Forwarding function for diagnostics. This translate a source
268 /// position in the current buffer into a SourceLocation object for rendering.
269 DiagnosticBuilder Diag(const char *Loc, unsigned DiagID) const;
270
271 /// getSourceLocation - Return a source location identifier for the specified
272 /// offset in the current file.
273 SourceLocation getSourceLocation(const char *Loc, unsigned TokLen = 1) const;
274
275 /// getSourceLocation - Return a source location for the next character in
276 /// the current file.
277 SourceLocation getSourceLocation() override {
278 return getSourceLocation(BufferPtr);
279 }
280
281 /// Return the current location in the buffer.
282 const char *getBufferLocation() const { return BufferPtr; }
283
284 /// Returns the current lexing offset.
285 unsigned getCurrentBufferOffset() {
286 assert(BufferPtr >= BufferStart && "Invalid buffer state")(static_cast <bool> (BufferPtr >= BufferStart &&
"Invalid buffer state") ? void (0) : __assert_fail ("BufferPtr >= BufferStart && \"Invalid buffer state\""
, "clang/include/clang/Lex/Lexer.h", 286, __extension__ __PRETTY_FUNCTION__
))
;
287 return BufferPtr - BufferStart;
288 }
289
290 /// Skip over \p NumBytes bytes.
291 ///
292 /// If the skip is successful, the next token will be lexed from the new
293 /// offset. The lexer also assumes that we skipped to the start of the line.
294 ///
295 /// \returns true if the skip failed (new offset would have been past the
296 /// end of the buffer), false otherwise.
297 bool skipOver(unsigned NumBytes);
298
299 /// Stringify - Convert the specified string into a C string by i) escaping
300 /// '\\' and " characters and ii) replacing newline character(s) with "\\n".
301 /// If Charify is true, this escapes the ' character instead of ".
302 static std::string Stringify(StringRef Str, bool Charify = false);
303
304 /// Stringify - Convert the specified string into a C string by i) escaping
305 /// '\\' and " characters and ii) replacing newline character(s) with "\\n".
306 static void Stringify(SmallVectorImpl<char> &Str);
307
308 /// getSpelling - This method is used to get the spelling of a token into a
309 /// preallocated buffer, instead of as an std::string. The caller is required
310 /// to allocate enough space for the token, which is guaranteed to be at least
311 /// Tok.getLength() bytes long. The length of the actual result is returned.
312 ///
313 /// Note that this method may do two possible things: it may either fill in
314 /// the buffer specified with characters, or it may *change the input pointer*
315 /// to point to a constant buffer with the data already in it (avoiding a
316 /// copy). The caller is not allowed to modify the returned buffer pointer
317 /// if an internal buffer is returned.
318 static unsigned getSpelling(const Token &Tok, const char *&Buffer,
319 const SourceManager &SourceMgr,
320 const LangOptions &LangOpts,
321 bool *Invalid = nullptr);
322
323 /// getSpelling() - Return the 'spelling' of the Tok token. The spelling of a
324 /// token is the characters used to represent the token in the source file
325 /// after trigraph expansion and escaped-newline folding. In particular, this
326 /// wants to get the true, uncanonicalized, spelling of things like digraphs
327 /// UCNs, etc.
328 static std::string getSpelling(const Token &Tok,
329 const SourceManager &SourceMgr,
330 const LangOptions &LangOpts,
331 bool *Invalid = nullptr);
332
333 /// getSpelling - This method is used to get the spelling of the
334 /// token at the given source location. If, as is usually true, it
335 /// is not necessary to copy any data, then the returned string may
336 /// not point into the provided buffer.
337 ///
338 /// This method lexes at the expansion depth of the given
339 /// location and does not jump to the expansion or spelling
340 /// location.
341 static StringRef getSpelling(SourceLocation loc,
342 SmallVectorImpl<char> &buffer,
343 const SourceManager &SM,
344 const LangOptions &options,
345 bool *invalid = nullptr);
346
347 /// MeasureTokenLength - Relex the token at the specified location and return
348 /// its length in bytes in the input file. If the token needs cleaning (e.g.
349 /// includes a trigraph or an escaped newline) then this count includes bytes
350 /// that are part of that.
351 static unsigned MeasureTokenLength(SourceLocation Loc,
352 const SourceManager &SM,
353 const LangOptions &LangOpts);
354
355 /// Relex the token at the specified location.
356 /// \returns true if there was a failure, false on success.
357 static bool getRawToken(SourceLocation Loc, Token &Result,
358 const SourceManager &SM,
359 const LangOptions &LangOpts,
360 bool IgnoreWhiteSpace = false);
361
362 /// Given a location any where in a source buffer, find the location
363 /// that corresponds to the beginning of the token in which the original
364 /// source location lands.
365 static SourceLocation GetBeginningOfToken(SourceLocation Loc,
366 const SourceManager &SM,
367 const LangOptions &LangOpts);
368
369 /// Get the physical length (including trigraphs and escaped newlines) of the
370 /// first \p Characters characters of the token starting at TokStart.
371 static unsigned getTokenPrefixLength(SourceLocation TokStart,
372 unsigned CharNo,
373 const SourceManager &SM,
374 const LangOptions &LangOpts);
375
376 /// AdvanceToTokenCharacter - If the current SourceLocation specifies a
377 /// location at the start of a token, return a new location that specifies a
378 /// character within the token. This handles trigraphs and escaped newlines.
379 static SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,
380 unsigned Characters,
381 const SourceManager &SM,
382 const LangOptions &LangOpts) {
383 return TokStart.getLocWithOffset(
384 getTokenPrefixLength(TokStart, Characters, SM, LangOpts));
385 }
386
387 /// Computes the source location just past the end of the
388 /// token at this source location.
389 ///
390 /// This routine can be used to produce a source location that
391 /// points just past the end of the token referenced by \p Loc, and
392 /// is generally used when a diagnostic needs to point just after a
393 /// token where it expected something different that it received. If
394 /// the returned source location would not be meaningful (e.g., if
395 /// it points into a macro), this routine returns an invalid
396 /// source location.
397 ///
398 /// \param Offset an offset from the end of the token, where the source
399 /// location should refer to. The default offset (0) produces a source
400 /// location pointing just past the end of the token; an offset of 1 produces
401 /// a source location pointing to the last character in the token, etc.
402 static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset,
403 const SourceManager &SM,
404 const LangOptions &LangOpts);
405
406 /// Given a token range, produce a corresponding CharSourceRange that
407 /// is not a token range. This allows the source range to be used by
408 /// components that don't have access to the lexer and thus can't find the
409 /// end of the range for themselves.
410 static CharSourceRange getAsCharRange(SourceRange Range,
411 const SourceManager &SM,
412 const LangOptions &LangOpts) {
413 SourceLocation End = getLocForEndOfToken(Range.getEnd(), 0, SM, LangOpts);
414 return End.isInvalid() ? CharSourceRange()
415 : CharSourceRange::getCharRange(
416 Range.getBegin(), End);
417 }
418 static CharSourceRange getAsCharRange(CharSourceRange Range,
419 const SourceManager &SM,
420 const LangOptions &LangOpts) {
421 return Range.isTokenRange()
422 ? getAsCharRange(Range.getAsRange(), SM, LangOpts)
423 : Range;
424 }
425
426 /// Returns true if the given MacroID location points at the first
427 /// token of the macro expansion.
428 ///
429 /// \param MacroBegin If non-null and function returns true, it is set to
430 /// begin location of the macro.
431 static bool isAtStartOfMacroExpansion(SourceLocation loc,
432 const SourceManager &SM,
433 const LangOptions &LangOpts,
434 SourceLocation *MacroBegin = nullptr);
435
436 /// Returns true if the given MacroID location points at the last
437 /// token of the macro expansion.
438 ///
439 /// \param MacroEnd If non-null and function returns true, it is set to
440 /// end location of the macro.
441 static bool isAtEndOfMacroExpansion(SourceLocation loc,
442 const SourceManager &SM,
443 const LangOptions &LangOpts,
444 SourceLocation *MacroEnd = nullptr);
445
446 /// Accepts a range and returns a character range with file locations.
447 ///
448 /// Returns a null range if a part of the range resides inside a macro
449 /// expansion or the range does not reside on the same FileID.
450 ///
451 /// This function is trying to deal with macros and return a range based on
452 /// file locations. The cases where it can successfully handle macros are:
453 ///
454 /// -begin or end range lies at the start or end of a macro expansion, in
455 /// which case the location will be set to the expansion point, e.g:
456 /// \#define M 1 2
457 /// a M
458 /// If you have a range [a, 2] (where 2 came from the macro), the function
459 /// will return a range for "a M"
460 /// if you have range [a, 1], the function will fail because the range
461 /// overlaps with only a part of the macro
462 ///
463 /// -The macro is a function macro and the range can be mapped to the macro
464 /// arguments, e.g:
465 /// \#define M 1 2
466 /// \#define FM(x) x
467 /// FM(a b M)
468 /// if you have range [b, 2], the function will return the file range "b M"
469 /// inside the macro arguments.
470 /// if you have range [a, 2], the function will return the file range
471 /// "FM(a b M)" since the range includes all of the macro expansion.
472 static CharSourceRange makeFileCharRange(CharSourceRange Range,
473 const SourceManager &SM,
474 const LangOptions &LangOpts);
475
476 /// Returns a string for the source that the range encompasses.
477 static StringRef getSourceText(CharSourceRange Range,
478 const SourceManager &SM,
479 const LangOptions &LangOpts,
480 bool *Invalid = nullptr);
481
482 /// Retrieve the name of the immediate macro expansion.
483 ///
484 /// This routine starts from a source location, and finds the name of the macro
485 /// responsible for its immediate expansion. It looks through any intervening
486 /// macro argument expansions to compute this. It returns a StringRef which
487 /// refers to the SourceManager-owned buffer of the source where that macro
488 /// name is spelled. Thus, the result shouldn't out-live that SourceManager.
489 static StringRef getImmediateMacroName(SourceLocation Loc,
490 const SourceManager &SM,
491 const LangOptions &LangOpts);
492
493 /// Retrieve the name of the immediate macro expansion.
494 ///
495 /// This routine starts from a source location, and finds the name of the
496 /// macro responsible for its immediate expansion. It looks through any
497 /// intervening macro argument expansions to compute this. It returns a
498 /// StringRef which refers to the SourceManager-owned buffer of the source
499 /// where that macro name is spelled. Thus, the result shouldn't out-live
500 /// that SourceManager.
501 ///
502 /// This differs from Lexer::getImmediateMacroName in that any macro argument
503 /// location will result in the topmost function macro that accepted it.
504 /// e.g.
505 /// \code
506 /// MAC1( MAC2(foo) )
507 /// \endcode
508 /// for location of 'foo' token, this function will return "MAC1" while
509 /// Lexer::getImmediateMacroName will return "MAC2".
510 static StringRef getImmediateMacroNameForDiagnostics(
511 SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts);
512
513 /// Compute the preamble of the given file.
514 ///
515 /// The preamble of a file contains the initial comments, include directives,
516 /// and other preprocessor directives that occur before the code in this
517 /// particular file actually begins. The preamble of the main source file is
518 /// a potential prefix header.
519 ///
520 /// \param Buffer The memory buffer containing the file's contents.
521 ///
522 /// \param MaxLines If non-zero, restrict the length of the preamble
523 /// to fewer than this number of lines.
524 ///
525 /// \returns The offset into the file where the preamble ends and the rest
526 /// of the file begins along with a boolean value indicating whether
527 /// the preamble ends at the beginning of a new line.
528 static PreambleBounds ComputePreamble(StringRef Buffer,
529 const LangOptions &LangOpts,
530 unsigned MaxLines = 0);
531
532 /// Finds the token that comes right after the given location.
533 ///
534 /// Returns the next token, or none if the location is inside a macro.
535 static Optional<Token> findNextToken(SourceLocation Loc,
536 const SourceManager &SM,
537 const LangOptions &LangOpts);
538
539 /// Checks that the given token is the first token that occurs after
540 /// the given location (this excludes comments and whitespace). Returns the
541 /// location immediately after the specified token. If the token is not found
542 /// or the location is inside a macro, the returned source location will be
543 /// invalid.
544 static SourceLocation findLocationAfterToken(SourceLocation loc,
545 tok::TokenKind TKind,
546 const SourceManager &SM,
547 const LangOptions &LangOpts,
548 bool SkipTrailingWhitespaceAndNewLine);
549
550 /// Returns true if the given character could appear in an identifier.
551 static bool isAsciiIdentifierContinueChar(char c,
552 const LangOptions &LangOpts);
553
554 /// Checks whether new line pointed by Str is preceded by escape
555 /// sequence.
556 static bool isNewLineEscaped(const char *BufferStart, const char *Str);
557
558 /// getCharAndSizeNoWarn - Like the getCharAndSize method, but does not ever
559 /// emit a warning.
560 static inline char getCharAndSizeNoWarn(const char *Ptr, unsigned &Size,
561 const LangOptions &LangOpts) {
562 // If this is not a trigraph and not a UCN or escaped newline, return
563 // quickly.
564 if (isObviouslySimpleCharacter(Ptr[0])) {
565 Size = 1;
566 return *Ptr;
567 }
568
569 Size = 0;
570 return getCharAndSizeSlowNoWarn(Ptr, Size, LangOpts);
571 }
572
573 /// Returns the leading whitespace for line that corresponds to the given
574 /// location \p Loc.
575 static StringRef getIndentationForLine(SourceLocation Loc,
576 const SourceManager &SM);
577
578 /// Check if this is the first time we're lexing the input file.
579 bool isFirstTimeLexingFile() const { return IsFirstTimeLexingFile; }
580
581private:
582 //===--------------------------------------------------------------------===//
583 // Internal implementation interfaces.
584
585 /// LexTokenInternal - Internal interface to lex a preprocessing token. Called
586 /// by Lex.
587 ///
588 bool LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine);
589
590 bool CheckUnicodeWhitespace(Token &Result, uint32_t C, const char *CurPtr);
591
592 bool LexUnicodeIdentifierStart(Token &Result, uint32_t C, const char *CurPtr);
593
594 /// FormTokenWithChars - When we lex a token, we have identified a span
595 /// starting at BufferPtr, going to TokEnd that forms the token. This method
596 /// takes that range and assigns it to the token as its location and size. In
597 /// addition, since tokens cannot overlap, this also updates BufferPtr to be
598 /// TokEnd.
599 void FormTokenWithChars(Token &Result, const char *TokEnd,
600 tok::TokenKind Kind) {
601 unsigned TokLen = TokEnd-BufferPtr;
602 Result.setLength(TokLen);
603 Result.setLocation(getSourceLocation(BufferPtr, TokLen));
604 Result.setKind(Kind);
605 BufferPtr = TokEnd;
606 }
607
608 /// isNextPPTokenLParen - Return 1 if the next unexpanded token will return a
609 /// tok::l_paren token, 0 if it is something else and 2 if there are no more
610 /// tokens in the buffer controlled by this lexer.
611 unsigned isNextPPTokenLParen();
612
613 //===--------------------------------------------------------------------===//
614 // Lexer character reading interfaces.
615
616 // This lexer is built on two interfaces for reading characters, both of which
617 // automatically provide phase 1/2 translation. getAndAdvanceChar is used
618 // when we know that we will be reading a character from the input buffer and
619 // that this character will be part of the result token. This occurs in (f.e.)
620 // string processing, because we know we need to read until we find the
621 // closing '"' character.
622 //
623 // The second interface is the combination of getCharAndSize with
624 // ConsumeChar. getCharAndSize reads a phase 1/2 translated character,
625 // returning it and its size. If the lexer decides that this character is
626 // part of the current token, it calls ConsumeChar on it. This two stage
627 // approach allows us to emit diagnostics for characters (e.g. warnings about
628 // trigraphs), knowing that they only are emitted if the character is
629 // consumed.
630
631 /// isObviouslySimpleCharacter - Return true if the specified character is
632 /// obviously the same in translation phase 1 and translation phase 3. This
633 /// can return false for characters that end up being the same, but it will
634 /// never return true for something that needs to be mapped.
635 static bool isObviouslySimpleCharacter(char C) {
636 return C != '?' && C != '\\';
637 }
638
639 /// getAndAdvanceChar - Read a single 'character' from the specified buffer,
640 /// advance over it, and return it. This is tricky in several cases. Here we
641 /// just handle the trivial case and fall-back to the non-inlined
642 /// getCharAndSizeSlow method to handle the hard case.
643 inline char getAndAdvanceChar(const char *&Ptr, Token &Tok) {
644 // If this is not a trigraph and not a UCN or escaped newline, return
645 // quickly.
646 if (isObviouslySimpleCharacter(Ptr[0])) return *Ptr++;
647
648 unsigned Size = 0;
649 char C = getCharAndSizeSlow(Ptr, Size, &Tok);
650 Ptr += Size;
651 return C;
652 }
653
654 /// ConsumeChar - When a character (identified by getCharAndSize) is consumed
655 /// and added to a given token, check to see if there are diagnostics that
656 /// need to be emitted or flags that need to be set on the token. If so, do
657 /// it.
658 const char *ConsumeChar(const char *Ptr, unsigned Size, Token &Tok) {
659 // Normal case, we consumed exactly one token. Just return it.
660 if (Size == 1)
661 return Ptr+Size;
662
663 // Otherwise, re-lex the character with a current token, allowing
664 // diagnostics to be emitted and flags to be set.
665 Size = 0;
666 getCharAndSizeSlow(Ptr, Size, &Tok);
667 return Ptr+Size;
668 }
669
670 /// getCharAndSize - Peek a single 'character' from the specified buffer,
671 /// get its size, and return it. This is tricky in several cases. Here we
672 /// just handle the trivial case and fall-back to the non-inlined
673 /// getCharAndSizeSlow method to handle the hard case.
674 inline char getCharAndSize(const char *Ptr, unsigned &Size) {
675 // If this is not a trigraph and not a UCN or escaped newline, return
676 // quickly.
677 if (isObviouslySimpleCharacter(Ptr[0])) {
12
Taking false branch
678 Size = 1;
679 return *Ptr;
680 }
681
682 Size = 0;
683 return getCharAndSizeSlow(Ptr, Size);
13
Value assigned to field 'PP'
684 }
685
686 /// getCharAndSizeSlow - Handle the slow/uncommon case of the getCharAndSize
687 /// method.
688 char getCharAndSizeSlow(const char *Ptr, unsigned &Size,
689 Token *Tok = nullptr);
690
691 /// getEscapedNewLineSize - Return the size of the specified escaped newline,
692 /// or 0 if it is not an escaped newline. P[-1] is known to be a "\" on entry
693 /// to this function.
694 static unsigned getEscapedNewLineSize(const char *P);
695
696 /// SkipEscapedNewLines - If P points to an escaped newline (or a series of
697 /// them), skip over them and return the first non-escaped-newline found,
698 /// otherwise return P.
699 static const char *SkipEscapedNewLines(const char *P);
700
701 /// getCharAndSizeSlowNoWarn - Same as getCharAndSizeSlow, but never emits a
702 /// diagnostic.
703 static char getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
704 const LangOptions &LangOpts);
705
706 //===--------------------------------------------------------------------===//
707 // Other lexer functions.
708
709 void SetByteOffset(unsigned Offset, bool StartOfLine);
710
711 void PropagateLineStartLeadingSpaceInfo(Token &Result);
712
713 const char *LexUDSuffix(Token &Result, const char *CurPtr,
714 bool IsStringLiteral);
715
716 // Helper functions to lex the remainder of a token of the specific type.
717
718 // This function handles both ASCII and Unicode identifiers after
719 // the first codepoint of the identifyier has been parsed.
720 bool LexIdentifierContinue(Token &Result, const char *CurPtr);
721
722 bool LexNumericConstant (Token &Result, const char *CurPtr);
723 bool LexStringLiteral (Token &Result, const char *CurPtr,
724 tok::TokenKind Kind);
725 bool LexRawStringLiteral (Token &Result, const char *CurPtr,
726 tok::TokenKind Kind);
727 bool LexAngledStringLiteral(Token &Result, const char *CurPtr);
728 bool LexCharConstant (Token &Result, const char *CurPtr,
729 tok::TokenKind Kind);
730 bool LexEndOfFile (Token &Result, const char *CurPtr);
731 bool SkipWhitespace (Token &Result, const char *CurPtr,
732 bool &TokAtPhysicalStartOfLine);
733 bool SkipLineComment (Token &Result, const char *CurPtr,
734 bool &TokAtPhysicalStartOfLine);
735 bool SkipBlockComment (Token &Result, const char *CurPtr,
736 bool &TokAtPhysicalStartOfLine);
737 bool SaveLineComment (Token &Result, const char *CurPtr);
738
739 bool IsStartOfConflictMarker(const char *CurPtr);
740 bool HandleEndOfConflictMarker(const char *CurPtr);
741
742 bool lexEditorPlaceholder(Token &Result, const char *CurPtr);
743
744 bool isCodeCompletionPoint(const char *CurPtr) const;
745 void cutOffLexing() { BufferPtr = BufferEnd; }
746
747 bool isHexaLiteral(const char *Start, const LangOptions &LangOpts);
748
749 void codeCompleteIncludedFile(const char *PathStart,
750 const char *CompletionPoint, bool IsAngled);
751
752 /// Read a universal character name.
753 ///
754 /// \param StartPtr The position in the source buffer after the initial '\'.
755 /// If the UCN is syntactically well-formed (but not
756 /// necessarily valid), this parameter will be updated to
757 /// point to the character after the UCN.
758 /// \param SlashLoc The position in the source buffer of the '\'.
759 /// \param Result The token being formed. Pass \c nullptr to suppress
760 /// diagnostics and handle token formation in the caller.
761 ///
762 /// \return The Unicode codepoint specified by the UCN, or 0 if the UCN is
763 /// invalid.
764 uint32_t tryReadUCN(const char *&StartPtr, const char *SlashLoc, Token *Result);
765
766 /// Try to consume a UCN as part of an identifier at the current
767 /// location.
768 /// \param CurPtr Initially points to the range of characters in the source
769 /// buffer containing the '\'. Updated to point past the end of
770 /// the UCN on success.
771 /// \param Size The number of characters occupied by the '\' (including
772 /// trigraphs and escaped newlines).
773 /// \param Result The token being produced. Marked as containing a UCN on
774 /// success.
775 /// \return \c true if a UCN was lexed and it produced an acceptable
776 /// identifier character, \c false otherwise.
777 bool tryConsumeIdentifierUCN(const char *&CurPtr, unsigned Size,
778 Token &Result);
779
780 /// Try to consume an identifier character encoded in UTF-8.
781 /// \param CurPtr Points to the start of the (potential) UTF-8 code unit
782 /// sequence. On success, updated to point past the end of it.
783 /// \return \c true if a UTF-8 sequence mapping to an acceptable identifier
784 /// character was lexed, \c false otherwise.
785 bool tryConsumeIdentifierUTF8Char(const char *&CurPtr);
786};
787
788} // namespace clang
789
790#endif // LLVM_CLANG_LEX_LEXER_H