Bug Summary

File:clang/lib/Lex/Pragma.cpp
Warning:line 1119, column 7
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 -disable-llvm-verifier -discard-value-names -main-file-name Pragma.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 -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-12/lib/clang/12.0.0 -D CLANG_VENDOR="Debian " -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/build-llvm/tools/clang/lib/Lex -I /build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex -I /build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include -I /build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/build-llvm/include -I /build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-12/lib/clang/12.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/build-llvm/tools/clang/lib/Lex -fdebug-prefix-map=/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-09-17-195756-12974-1 -x c++ /build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp

/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp

1//===- Pragma.cpp - Pragma registration and handling ----------------------===//
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 PragmaHandler/PragmaTable interfaces and implements
10// pragma related methods of the Preprocessor class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/Pragma.h"
15#include "clang/Basic/Diagnostic.h"
16#include "clang/Basic/FileManager.h"
17#include "clang/Basic/IdentifierTable.h"
18#include "clang/Basic/LLVM.h"
19#include "clang/Basic/LangOptions.h"
20#include "clang/Basic/Module.h"
21#include "clang/Basic/SourceLocation.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/TokenKinds.h"
24#include "clang/Lex/HeaderSearch.h"
25#include "clang/Lex/LexDiagnostic.h"
26#include "clang/Lex/Lexer.h"
27#include "clang/Lex/LiteralSupport.h"
28#include "clang/Lex/MacroInfo.h"
29#include "clang/Lex/ModuleLoader.h"
30#include "clang/Lex/PPCallbacks.h"
31#include "clang/Lex/Preprocessor.h"
32#include "clang/Lex/PreprocessorLexer.h"
33#include "clang/Lex/PreprocessorOptions.h"
34#include "clang/Lex/Token.h"
35#include "clang/Lex/TokenLexer.h"
36#include "llvm/ADT/ArrayRef.h"
37#include "llvm/ADT/DenseMap.h"
38#include "llvm/ADT/STLExtras.h"
39#include "llvm/ADT/SmallString.h"
40#include "llvm/ADT/SmallVector.h"
41#include "llvm/ADT/StringSwitch.h"
42#include "llvm/ADT/StringRef.h"
43#include "llvm/Support/Compiler.h"
44#include "llvm/Support/ErrorHandling.h"
45#include "llvm/Support/Timer.h"
46#include <algorithm>
47#include <cassert>
48#include <cstddef>
49#include <cstdint>
50#include <limits>
51#include <string>
52#include <utility>
53#include <vector>
54
55using namespace clang;
56
57// Out-of-line destructor to provide a home for the class.
58PragmaHandler::~PragmaHandler() = default;
59
60//===----------------------------------------------------------------------===//
61// EmptyPragmaHandler Implementation.
62//===----------------------------------------------------------------------===//
63
64EmptyPragmaHandler::EmptyPragmaHandler(StringRef Name) : PragmaHandler(Name) {}
65
66void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
67 PragmaIntroducer Introducer,
68 Token &FirstToken) {}
69
70//===----------------------------------------------------------------------===//
71// PragmaNamespace Implementation.
72//===----------------------------------------------------------------------===//
73
74/// FindHandler - Check to see if there is already a handler for the
75/// specified name. If not, return the handler for the null identifier if it
76/// exists, otherwise return null. If IgnoreNull is true (the default) then
77/// the null handler isn't returned on failure to match.
78PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
79 bool IgnoreNull) const {
80 auto I = Handlers.find(Name);
81 if (I != Handlers.end())
82 return I->getValue().get();
83 if (IgnoreNull)
84 return nullptr;
85 I = Handlers.find(StringRef());
86 if (I != Handlers.end())
87 return I->getValue().get();
88 return nullptr;
89}
90
91void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
92 assert(!Handlers.count(Handler->getName()) &&((!Handlers.count(Handler->getName()) && "A handler with this name is already registered in this namespace"
) ? static_cast<void> (0) : __assert_fail ("!Handlers.count(Handler->getName()) && \"A handler with this name is already registered in this namespace\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 93, __PRETTY_FUNCTION__))
93 "A handler with this name is already registered in this namespace")((!Handlers.count(Handler->getName()) && "A handler with this name is already registered in this namespace"
) ? static_cast<void> (0) : __assert_fail ("!Handlers.count(Handler->getName()) && \"A handler with this name is already registered in this namespace\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 93, __PRETTY_FUNCTION__))
;
94 Handlers[Handler->getName()].reset(Handler);
95}
96
97void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
98 auto I = Handlers.find(Handler->getName());
99 assert(I != Handlers.end() &&((I != Handlers.end() && "Handler not registered in this namespace"
) ? static_cast<void> (0) : __assert_fail ("I != Handlers.end() && \"Handler not registered in this namespace\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 100, __PRETTY_FUNCTION__))
100 "Handler not registered in this namespace")((I != Handlers.end() && "Handler not registered in this namespace"
) ? static_cast<void> (0) : __assert_fail ("I != Handlers.end() && \"Handler not registered in this namespace\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 100, __PRETTY_FUNCTION__))
;
101 // Release ownership back to the caller.
102 I->getValue().release();
103 Handlers.erase(I);
104}
105
106void PragmaNamespace::HandlePragma(Preprocessor &PP,
107 PragmaIntroducer Introducer, Token &Tok) {
108 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
109 // expand it, the user can have a STDC #define, that should not affect this.
110 PP.LexUnexpandedToken(Tok);
111
112 // Get the handler for this token. If there is no handler, ignore the pragma.
113 PragmaHandler *Handler
114 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
115 : StringRef(),
116 /*IgnoreNull=*/false);
117 if (!Handler) {
118 PP.Diag(Tok, diag::warn_pragma_ignored);
119 return;
120 }
121
122 // Otherwise, pass it down.
123 Handler->HandlePragma(PP, Introducer, Tok);
124}
125
126//===----------------------------------------------------------------------===//
127// Preprocessor Pragma Directive Handling.
128//===----------------------------------------------------------------------===//
129
130namespace {
131// TokenCollector provides the option to collect tokens that were "read"
132// and return them to the stream to be read later.
133// Currently used when reading _Pragma/__pragma directives.
134struct TokenCollector {
135 Preprocessor &Self;
136 bool Collect;
137 SmallVector<Token, 3> Tokens;
138 Token &Tok;
139
140 void lex() {
141 if (Collect)
142 Tokens.push_back(Tok);
143 Self.Lex(Tok);
144 }
145
146 void revert() {
147 assert(Collect && "did not collect tokens")((Collect && "did not collect tokens") ? static_cast<
void> (0) : __assert_fail ("Collect && \"did not collect tokens\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 147, __PRETTY_FUNCTION__))
;
148 assert(!Tokens.empty() && "collected unexpected number of tokens")((!Tokens.empty() && "collected unexpected number of tokens"
) ? static_cast<void> (0) : __assert_fail ("!Tokens.empty() && \"collected unexpected number of tokens\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 148, __PRETTY_FUNCTION__))
;
149
150 // Push the ( "string" ) tokens into the token stream.
151 auto Toks = std::make_unique<Token[]>(Tokens.size());
152 std::copy(Tokens.begin() + 1, Tokens.end(), Toks.get());
153 Toks[Tokens.size() - 1] = Tok;
154 Self.EnterTokenStream(std::move(Toks), Tokens.size(),
155 /*DisableMacroExpansion*/ true,
156 /*IsReinject*/ true);
157
158 // ... and return the pragma token unchanged.
159 Tok = *Tokens.begin();
160 }
161};
162} // namespace
163
164/// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the
165/// rest of the pragma, passing it to the registered pragma handlers.
166void Preprocessor::HandlePragmaDirective(PragmaIntroducer Introducer) {
167 if (Callbacks)
168 Callbacks->PragmaDirective(Introducer.Loc, Introducer.Kind);
169
170 if (!PragmasEnabled)
171 return;
172
173 ++NumPragma;
174
175 // Invoke the first level of pragma handlers which reads the namespace id.
176 Token Tok;
177 PragmaHandlers->HandlePragma(*this, Introducer, Tok);
178
179 // If the pragma handler didn't read the rest of the line, consume it now.
180 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
181 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
182 DiscardUntilEndOfDirective();
183}
184
185/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
186/// return the first token after the directive. The _Pragma token has just
187/// been read into 'Tok'.
188void Preprocessor::Handle_Pragma(Token &Tok) {
189 // C11 6.10.3.4/3:
190 // all pragma unary operator expressions within [a completely
191 // macro-replaced preprocessing token sequence] are [...] processed [after
192 // rescanning is complete]
193 //
194 // This means that we execute _Pragma operators in two cases:
195 //
196 // 1) on token sequences that would otherwise be produced as the output of
197 // phase 4 of preprocessing, and
198 // 2) on token sequences formed as the macro-replaced token sequence of a
199 // macro argument
200 //
201 // Case #2 appears to be a wording bug: only _Pragmas that would survive to
202 // the end of phase 4 should actually be executed. Discussion on the WG14
203 // mailing list suggests that a _Pragma operator is notionally checked early,
204 // but only pragmas that survive to the end of phase 4 should be executed.
205 //
206 // In Case #2, we check the syntax now, but then put the tokens back into the
207 // token stream for later consumption.
208
209 TokenCollector Toks = {*this, InMacroArgPreExpansion, {}, Tok};
210
211 // Remember the pragma token location.
212 SourceLocation PragmaLoc = Tok.getLocation();
213
214 // Read the '('.
215 Toks.lex();
216 if (Tok.isNot(tok::l_paren)) {
217 Diag(PragmaLoc, diag::err__Pragma_malformed);
218 return;
219 }
220
221 // Read the '"..."'.
222 Toks.lex();
223 if (!tok::isStringLiteral(Tok.getKind())) {
224 Diag(PragmaLoc, diag::err__Pragma_malformed);
225 // Skip bad tokens, and the ')', if present.
226 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof))
227 Lex(Tok);
228 while (Tok.isNot(tok::r_paren) &&
229 !Tok.isAtStartOfLine() &&
230 Tok.isNot(tok::eof))
231 Lex(Tok);
232 if (Tok.is(tok::r_paren))
233 Lex(Tok);
234 return;
235 }
236
237 if (Tok.hasUDSuffix()) {
238 Diag(Tok, diag::err_invalid_string_udl);
239 // Skip this token, and the ')', if present.
240 Lex(Tok);
241 if (Tok.is(tok::r_paren))
242 Lex(Tok);
243 return;
244 }
245
246 // Remember the string.
247 Token StrTok = Tok;
248
249 // Read the ')'.
250 Toks.lex();
251 if (Tok.isNot(tok::r_paren)) {
252 Diag(PragmaLoc, diag::err__Pragma_malformed);
253 return;
254 }
255
256 // If we're expanding a macro argument, put the tokens back.
257 if (InMacroArgPreExpansion) {
258 Toks.revert();
259 return;
260 }
261
262 SourceLocation RParenLoc = Tok.getLocation();
263 std::string StrVal = getSpelling(StrTok);
264
265 // The _Pragma is lexically sound. Destringize according to C11 6.10.9.1:
266 // "The string literal is destringized by deleting any encoding prefix,
267 // deleting the leading and trailing double-quotes, replacing each escape
268 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
269 // single backslash."
270 if (StrVal[0] == 'L' || StrVal[0] == 'U' ||
271 (StrVal[0] == 'u' && StrVal[1] != '8'))
272 StrVal.erase(StrVal.begin());
273 else if (StrVal[0] == 'u')
274 StrVal.erase(StrVal.begin(), StrVal.begin() + 2);
275
276 if (StrVal[0] == 'R') {
277 // FIXME: C++11 does not specify how to handle raw-string-literals here.
278 // We strip off the 'R', the quotes, the d-char-sequences, and the parens.
279 assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' &&((StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"'
&& "Invalid raw string token!") ? static_cast<void
> (0) : __assert_fail ("StrVal[1] == '\"' && StrVal[StrVal.size() - 1] == '\"' && \"Invalid raw string token!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 280, __PRETTY_FUNCTION__))
280 "Invalid raw string token!")((StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"'
&& "Invalid raw string token!") ? static_cast<void
> (0) : __assert_fail ("StrVal[1] == '\"' && StrVal[StrVal.size() - 1] == '\"' && \"Invalid raw string token!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 280, __PRETTY_FUNCTION__))
;
281
282 // Measure the length of the d-char-sequence.
283 unsigned NumDChars = 0;
284 while (StrVal[2 + NumDChars] != '(') {
285 assert(NumDChars < (StrVal.size() - 5) / 2 &&((NumDChars < (StrVal.size() - 5) / 2 && "Invalid raw string token!"
) ? static_cast<void> (0) : __assert_fail ("NumDChars < (StrVal.size() - 5) / 2 && \"Invalid raw string token!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 286, __PRETTY_FUNCTION__))
286 "Invalid raw string token!")((NumDChars < (StrVal.size() - 5) / 2 && "Invalid raw string token!"
) ? static_cast<void> (0) : __assert_fail ("NumDChars < (StrVal.size() - 5) / 2 && \"Invalid raw string token!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 286, __PRETTY_FUNCTION__))
;
287 ++NumDChars;
288 }
289 assert(StrVal[StrVal.size() - 2 - NumDChars] == ')')((StrVal[StrVal.size() - 2 - NumDChars] == ')') ? static_cast
<void> (0) : __assert_fail ("StrVal[StrVal.size() - 2 - NumDChars] == ')'"
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 289, __PRETTY_FUNCTION__))
;
290
291 // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the
292 // parens below.
293 StrVal.erase(0, 2 + NumDChars);
294 StrVal.erase(StrVal.size() - 1 - NumDChars);
295 } else {
296 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&((StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
"Invalid string token!") ? static_cast<void> (0) : __assert_fail
("StrVal[0] == '\"' && StrVal[StrVal.size()-1] == '\"' && \"Invalid string token!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 297, __PRETTY_FUNCTION__))
297 "Invalid string token!")((StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
"Invalid string token!") ? static_cast<void> (0) : __assert_fail
("StrVal[0] == '\"' && StrVal[StrVal.size()-1] == '\"' && \"Invalid string token!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 297, __PRETTY_FUNCTION__))
;
298
299 // Remove escaped quotes and escapes.
300 unsigned ResultPos = 1;
301 for (size_t i = 1, e = StrVal.size() - 1; i != e; ++i) {
302 // Skip escapes. \\ -> '\' and \" -> '"'.
303 if (StrVal[i] == '\\' && i + 1 < e &&
304 (StrVal[i + 1] == '\\' || StrVal[i + 1] == '"'))
305 ++i;
306 StrVal[ResultPos++] = StrVal[i];
307 }
308 StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1);
309 }
310
311 // Remove the front quote, replacing it with a space, so that the pragma
312 // contents appear to have a space before them.
313 StrVal[0] = ' ';
314
315 // Replace the terminating quote with a \n.
316 StrVal[StrVal.size()-1] = '\n';
317
318 // Plop the string (including the newline and trailing null) into a buffer
319 // where we can lex it.
320 Token TmpTok;
321 TmpTok.startToken();
322 CreateString(StrVal, TmpTok);
323 SourceLocation TokLoc = TmpTok.getLocation();
324
325 // Make and enter a lexer object so that we lex and expand the tokens just
326 // like any others.
327 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
328 StrVal.size(), *this);
329
330 EnterSourceFileWithLexer(TL, nullptr);
331
332 // With everything set up, lex this as a #pragma directive.
333 HandlePragmaDirective({PIK__Pragma, PragmaLoc});
334
335 // Finally, return whatever came after the pragma directive.
336 return Lex(Tok);
337}
338
339/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
340/// is not enclosed within a string literal.
341void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
342 // During macro pre-expansion, check the syntax now but put the tokens back
343 // into the token stream for later consumption. Same as Handle_Pragma.
344 TokenCollector Toks = {*this, InMacroArgPreExpansion, {}, Tok};
345
346 // Remember the pragma token location.
347 SourceLocation PragmaLoc = Tok.getLocation();
348
349 // Read the '('.
350 Toks.lex();
351 if (Tok.isNot(tok::l_paren)) {
352 Diag(PragmaLoc, diag::err__Pragma_malformed);
353 return;
354 }
355
356 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
357 SmallVector<Token, 32> PragmaToks;
358 int NumParens = 0;
359 Toks.lex();
360 while (Tok.isNot(tok::eof)) {
361 PragmaToks.push_back(Tok);
362 if (Tok.is(tok::l_paren))
363 NumParens++;
364 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
365 break;
366 Toks.lex();
367 }
368
369 if (Tok.is(tok::eof)) {
370 Diag(PragmaLoc, diag::err_unterminated___pragma);
371 return;
372 }
373
374 // If we're expanding a macro argument, put the tokens back.
375 if (InMacroArgPreExpansion) {
376 Toks.revert();
377 return;
378 }
379
380 PragmaToks.front().setFlag(Token::LeadingSpace);
381
382 // Replace the ')' with an EOD to mark the end of the pragma.
383 PragmaToks.back().setKind(tok::eod);
384
385 Token *TokArray = new Token[PragmaToks.size()];
386 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
387
388 // Push the tokens onto the stack.
389 EnterTokenStream(TokArray, PragmaToks.size(), true, true,
390 /*IsReinject*/ false);
391
392 // With everything set up, lex this as a #pragma directive.
393 HandlePragmaDirective({PIK___pragma, PragmaLoc});
394
395 // Finally, return whatever came after the pragma directive.
396 return Lex(Tok);
397}
398
399/// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'.
400void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
401 // Don't honor the 'once' when handling the primary source file, unless
402 // this is a prefix to a TU, which indicates we're generating a PCH file, or
403 // when the main file is a header (e.g. when -xc-header is provided on the
404 // commandline).
405 if (isInPrimaryFile() && TUKind != TU_Prefix && !getLangOpts().IsHeaderFile) {
406 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
407 return;
408 }
409
410 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
411 // Mark the file as a once-only file now.
412 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
413}
414
415void Preprocessor::HandlePragmaMark() {
416 assert(CurPPLexer && "No current lexer?")((CurPPLexer && "No current lexer?") ? static_cast<
void> (0) : __assert_fail ("CurPPLexer && \"No current lexer?\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 416, __PRETTY_FUNCTION__))
;
417 CurLexer->ReadToEndOfLine();
418}
419
420/// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
421void Preprocessor::HandlePragmaPoison() {
422 Token Tok;
423
424 while (true) {
425 // Read the next token to poison. While doing this, pretend that we are
426 // skipping while reading the identifier to poison.
427 // This avoids errors on code like:
428 // #pragma GCC poison X
429 // #pragma GCC poison X
430 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
431 LexUnexpandedToken(Tok);
432 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
433
434 // If we reached the end of line, we're done.
435 if (Tok.is(tok::eod)) return;
436
437 // Can only poison identifiers.
438 if (Tok.isNot(tok::raw_identifier)) {
439 Diag(Tok, diag::err_pp_invalid_poison);
440 return;
441 }
442
443 // Look up the identifier info for the token. We disabled identifier lookup
444 // by saying we're skipping contents, so we need to do this manually.
445 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
446
447 // Already poisoned.
448 if (II->isPoisoned()) continue;
449
450 // If this is a macro identifier, emit a warning.
451 if (isMacroDefined(II))
452 Diag(Tok, diag::pp_poisoning_existing_macro);
453
454 // Finally, poison it!
455 II->setIsPoisoned();
456 if (II->isFromAST())
457 II->setChangedSinceDeserialization();
458 }
459}
460
461/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
462/// that the whole directive has been parsed.
463void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
464 if (isInPrimaryFile()) {
465 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
466 return;
467 }
468
469 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
470 PreprocessorLexer *TheLexer = getCurrentFileLexer();
471
472 // Mark the file as a system header.
473 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
474
475 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
476 if (PLoc.isInvalid())
477 return;
478
479 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
480
481 // Notify the client, if desired, that we are in a new source file.
482 if (Callbacks)
483 Callbacks->FileChanged(SysHeaderTok.getLocation(),
484 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
485
486 // Emit a line marker. This will change any source locations from this point
487 // forward to realize they are in a system header.
488 // Create a line note with this information.
489 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine() + 1,
490 FilenameID, /*IsEntry=*/false, /*IsExit=*/false,
491 SrcMgr::C_System);
492}
493
494/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
495void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
496 Token FilenameTok;
497 if (LexHeaderName(FilenameTok, /*AllowConcatenation*/false))
498 return;
499
500 // If the next token wasn't a header-name, diagnose the error.
501 if (FilenameTok.isNot(tok::header_name)) {
502 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
503 return;
504 }
505
506 // Reserve a buffer to get the spelling.
507 SmallString<128> FilenameBuffer;
508 bool Invalid = false;
509 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
510 if (Invalid)
511 return;
512
513 bool isAngled =
514 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
515 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
516 // error.
517 if (Filename.empty())
518 return;
519
520 // Search include directories for this file.
521 const DirectoryLookup *CurDir;
522 Optional<FileEntryRef> File =
523 LookupFile(FilenameTok.getLocation(), Filename, isAngled, nullptr,
524 nullptr, CurDir, nullptr, nullptr, nullptr, nullptr, nullptr);
525 if (!File) {
526 if (!SuppressIncludeNotFoundError)
527 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
528 return;
529 }
530
531 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
532
533 // If this file is older than the file it depends on, emit a diagnostic.
534 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
535 // Lex tokens at the end of the message and include them in the message.
536 std::string Message;
537 Lex(DependencyTok);
538 while (DependencyTok.isNot(tok::eod)) {
539 Message += getSpelling(DependencyTok) + " ";
540 Lex(DependencyTok);
541 }
542
543 // Remove the trailing ' ' if present.
544 if (!Message.empty())
545 Message.erase(Message.end()-1);
546 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
547 }
548}
549
550/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
551/// Return the IdentifierInfo* associated with the macro to push or pop.
552IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
553 // Remember the pragma token location.
554 Token PragmaTok = Tok;
555
556 // Read the '('.
557 Lex(Tok);
558 if (Tok.isNot(tok::l_paren)) {
559 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
560 << getSpelling(PragmaTok);
561 return nullptr;
562 }
563
564 // Read the macro name string.
565 Lex(Tok);
566 if (Tok.isNot(tok::string_literal)) {
567 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
568 << getSpelling(PragmaTok);
569 return nullptr;
570 }
571
572 if (Tok.hasUDSuffix()) {
573 Diag(Tok, diag::err_invalid_string_udl);
574 return nullptr;
575 }
576
577 // Remember the macro string.
578 std::string StrVal = getSpelling(Tok);
579
580 // Read the ')'.
581 Lex(Tok);
582 if (Tok.isNot(tok::r_paren)) {
583 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
584 << getSpelling(PragmaTok);
585 return nullptr;
586 }
587
588 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&((StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
"Invalid string token!") ? static_cast<void> (0) : __assert_fail
("StrVal[0] == '\"' && StrVal[StrVal.size()-1] == '\"' && \"Invalid string token!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 589, __PRETTY_FUNCTION__))
589 "Invalid string token!")((StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
"Invalid string token!") ? static_cast<void> (0) : __assert_fail
("StrVal[0] == '\"' && StrVal[StrVal.size()-1] == '\"' && \"Invalid string token!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 589, __PRETTY_FUNCTION__))
;
590
591 // Create a Token from the string.
592 Token MacroTok;
593 MacroTok.startToken();
594 MacroTok.setKind(tok::raw_identifier);
595 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
596
597 // Get the IdentifierInfo of MacroToPushTok.
598 return LookUpIdentifierInfo(MacroTok);
599}
600
601/// Handle \#pragma push_macro.
602///
603/// The syntax is:
604/// \code
605/// #pragma push_macro("macro")
606/// \endcode
607void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
608 // Parse the pragma directive and get the macro IdentifierInfo*.
609 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
610 if (!IdentInfo) return;
611
612 // Get the MacroInfo associated with IdentInfo.
613 MacroInfo *MI = getMacroInfo(IdentInfo);
614
615 if (MI) {
616 // Allow the original MacroInfo to be redefined later.
617 MI->setIsAllowRedefinitionsWithoutWarning(true);
618 }
619
620 // Push the cloned MacroInfo so we can retrieve it later.
621 PragmaPushMacroInfo[IdentInfo].push_back(MI);
622}
623
624/// Handle \#pragma pop_macro.
625///
626/// The syntax is:
627/// \code
628/// #pragma pop_macro("macro")
629/// \endcode
630void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
631 SourceLocation MessageLoc = PopMacroTok.getLocation();
632
633 // Parse the pragma directive and get the macro IdentifierInfo*.
634 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
635 if (!IdentInfo) return;
636
637 // Find the vector<MacroInfo*> associated with the macro.
638 llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>>::iterator iter =
639 PragmaPushMacroInfo.find(IdentInfo);
640 if (iter != PragmaPushMacroInfo.end()) {
641 // Forget the MacroInfo currently associated with IdentInfo.
642 if (MacroInfo *MI = getMacroInfo(IdentInfo)) {
643 if (MI->isWarnIfUnused())
644 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
645 appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
646 }
647
648 // Get the MacroInfo we want to reinstall.
649 MacroInfo *MacroToReInstall = iter->second.back();
650
651 if (MacroToReInstall)
652 // Reinstall the previously pushed macro.
653 appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc);
654
655 // Pop PragmaPushMacroInfo stack.
656 iter->second.pop_back();
657 if (iter->second.empty())
658 PragmaPushMacroInfo.erase(iter);
659 } else {
660 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
661 << IdentInfo->getName();
662 }
663}
664
665void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
666 // We will either get a quoted filename or a bracketed filename, and we
667 // have to track which we got. The first filename is the source name,
668 // and the second name is the mapped filename. If the first is quoted,
669 // the second must be as well (cannot mix and match quotes and brackets).
670
671 // Get the open paren
672 Lex(Tok);
673 if (Tok.isNot(tok::l_paren)) {
674 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
675 return;
676 }
677
678 // We expect either a quoted string literal, or a bracketed name
679 Token SourceFilenameTok;
680 if (LexHeaderName(SourceFilenameTok))
681 return;
682
683 StringRef SourceFileName;
684 SmallString<128> FileNameBuffer;
685 if (SourceFilenameTok.is(tok::header_name)) {
686 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
687 } else {
688 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
689 return;
690 }
691 FileNameBuffer.clear();
692
693 // Now we expect a comma, followed by another include name
694 Lex(Tok);
695 if (Tok.isNot(tok::comma)) {
696 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
697 return;
698 }
699
700 Token ReplaceFilenameTok;
701 if (LexHeaderName(ReplaceFilenameTok))
702 return;
703
704 StringRef ReplaceFileName;
705 if (ReplaceFilenameTok.is(tok::header_name)) {
706 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
707 } else {
708 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
709 return;
710 }
711
712 // Finally, we expect the closing paren
713 Lex(Tok);
714 if (Tok.isNot(tok::r_paren)) {
715 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
716 return;
717 }
718
719 // Now that we have the source and target filenames, we need to make sure
720 // they're both of the same type (angled vs non-angled)
721 StringRef OriginalSource = SourceFileName;
722
723 bool SourceIsAngled =
724 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
725 SourceFileName);
726 bool ReplaceIsAngled =
727 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
728 ReplaceFileName);
729 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
730 (SourceIsAngled != ReplaceIsAngled)) {
731 unsigned int DiagID;
732 if (SourceIsAngled)
733 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
734 else
735 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
736
737 Diag(SourceFilenameTok.getLocation(), DiagID)
738 << SourceFileName
739 << ReplaceFileName;
740
741 return;
742 }
743
744 // Now we can let the include handler know about this mapping
745 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
746}
747
748// Lex a component of a module name: either an identifier or a string literal;
749// for components that can be expressed both ways, the two forms are equivalent.
750static bool LexModuleNameComponent(
751 Preprocessor &PP, Token &Tok,
752 std::pair<IdentifierInfo *, SourceLocation> &ModuleNameComponent,
753 bool First) {
754 PP.LexUnexpandedToken(Tok);
755 if (Tok.is(tok::string_literal) && !Tok.hasUDSuffix()) {
756 StringLiteralParser Literal(Tok, PP);
757 if (Literal.hadError)
758 return true;
759 ModuleNameComponent = std::make_pair(
760 PP.getIdentifierInfo(Literal.GetString()), Tok.getLocation());
761 } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) {
762 ModuleNameComponent =
763 std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation());
764 } else {
765 PP.Diag(Tok.getLocation(), diag::err_pp_expected_module_name) << First;
766 return true;
767 }
768 return false;
769}
770
771static bool LexModuleName(
772 Preprocessor &PP, Token &Tok,
773 llvm::SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>>
774 &ModuleName) {
775 while (true) {
776 std::pair<IdentifierInfo*, SourceLocation> NameComponent;
777 if (LexModuleNameComponent(PP, Tok, NameComponent, ModuleName.empty()))
778 return true;
779 ModuleName.push_back(NameComponent);
780
781 PP.LexUnexpandedToken(Tok);
782 if (Tok.isNot(tok::period))
783 return false;
784 }
785}
786
787void Preprocessor::HandlePragmaModuleBuild(Token &Tok) {
788 SourceLocation Loc = Tok.getLocation();
789
790 std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc;
791 if (LexModuleNameComponent(*this, Tok, ModuleNameLoc, true))
792 return;
793 IdentifierInfo *ModuleName = ModuleNameLoc.first;
794
795 LexUnexpandedToken(Tok);
796 if (Tok.isNot(tok::eod)) {
797 Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
798 DiscardUntilEndOfDirective();
799 }
800
801 CurLexer->LexingRawMode = true;
802
803 auto TryConsumeIdentifier = [&](StringRef Ident) -> bool {
804 if (Tok.getKind() != tok::raw_identifier ||
805 Tok.getRawIdentifier() != Ident)
806 return false;
807 CurLexer->Lex(Tok);
808 return true;
809 };
810
811 // Scan forward looking for the end of the module.
812 const char *Start = CurLexer->getBufferLocation();
813 const char *End = nullptr;
814 unsigned NestingLevel = 1;
815 while (true) {
816 End = CurLexer->getBufferLocation();
817 CurLexer->Lex(Tok);
818
819 if (Tok.is(tok::eof)) {
820 Diag(Loc, diag::err_pp_module_build_missing_end);
821 break;
822 }
823
824 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) {
825 // Token was part of module; keep going.
826 continue;
827 }
828
829 // We hit something directive-shaped; check to see if this is the end
830 // of the module build.
831 CurLexer->ParsingPreprocessorDirective = true;
832 CurLexer->Lex(Tok);
833 if (TryConsumeIdentifier("pragma") && TryConsumeIdentifier("clang") &&
834 TryConsumeIdentifier("module")) {
835 if (TryConsumeIdentifier("build"))
836 // #pragma clang module build -> entering a nested module build.
837 ++NestingLevel;
838 else if (TryConsumeIdentifier("endbuild")) {
839 // #pragma clang module endbuild -> leaving a module build.
840 if (--NestingLevel == 0)
841 break;
842 }
843 // We should either be looking at the EOD or more of the current directive
844 // preceding the EOD. Either way we can ignore this token and keep going.
845 assert(Tok.getKind() != tok::eof && "missing EOD before EOF")((Tok.getKind() != tok::eof && "missing EOD before EOF"
) ? static_cast<void> (0) : __assert_fail ("Tok.getKind() != tok::eof && \"missing EOD before EOF\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 845, __PRETTY_FUNCTION__))
;
846 }
847 }
848
849 CurLexer->LexingRawMode = false;
850
851 // Load the extracted text as a preprocessed module.
852 assert(CurLexer->getBuffer().begin() <= Start &&((CurLexer->getBuffer().begin() <= Start && Start
<= CurLexer->getBuffer().end() && CurLexer->
getBuffer().begin() <= End && End <= CurLexer->
getBuffer().end() && "module source range not contained within same file buffer"
) ? static_cast<void> (0) : __assert_fail ("CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && \"module source range not contained within same file buffer\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 856, __PRETTY_FUNCTION__))
853 Start <= CurLexer->getBuffer().end() &&((CurLexer->getBuffer().begin() <= Start && Start
<= CurLexer->getBuffer().end() && CurLexer->
getBuffer().begin() <= End && End <= CurLexer->
getBuffer().end() && "module source range not contained within same file buffer"
) ? static_cast<void> (0) : __assert_fail ("CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && \"module source range not contained within same file buffer\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 856, __PRETTY_FUNCTION__))
854 CurLexer->getBuffer().begin() <= End &&((CurLexer->getBuffer().begin() <= Start && Start
<= CurLexer->getBuffer().end() && CurLexer->
getBuffer().begin() <= End && End <= CurLexer->
getBuffer().end() && "module source range not contained within same file buffer"
) ? static_cast<void> (0) : __assert_fail ("CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && \"module source range not contained within same file buffer\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 856, __PRETTY_FUNCTION__))
855 End <= CurLexer->getBuffer().end() &&((CurLexer->getBuffer().begin() <= Start && Start
<= CurLexer->getBuffer().end() && CurLexer->
getBuffer().begin() <= End && End <= CurLexer->
getBuffer().end() && "module source range not contained within same file buffer"
) ? static_cast<void> (0) : __assert_fail ("CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && \"module source range not contained within same file buffer\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 856, __PRETTY_FUNCTION__))
856 "module source range not contained within same file buffer")((CurLexer->getBuffer().begin() <= Start && Start
<= CurLexer->getBuffer().end() && CurLexer->
getBuffer().begin() <= End && End <= CurLexer->
getBuffer().end() && "module source range not contained within same file buffer"
) ? static_cast<void> (0) : __assert_fail ("CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && \"module source range not contained within same file buffer\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 856, __PRETTY_FUNCTION__))
;
857 TheModuleLoader.createModuleFromSource(Loc, ModuleName->getName(),
858 StringRef(Start, End - Start));
859}
860
861void Preprocessor::HandlePragmaHdrstop(Token &Tok) {
862 Lex(Tok);
863 if (Tok.is(tok::l_paren)) {
864 Diag(Tok.getLocation(), diag::warn_pp_hdrstop_filename_ignored);
865
866 std::string FileName;
867 if (!LexStringLiteral(Tok, FileName, "pragma hdrstop", false))
868 return;
869
870 if (Tok.isNot(tok::r_paren)) {
871 Diag(Tok, diag::err_expected) << tok::r_paren;
872 return;
873 }
874 Lex(Tok);
875 }
876 if (Tok.isNot(tok::eod))
877 Diag(Tok.getLocation(), diag::ext_pp_extra_tokens_at_eol)
878 << "pragma hdrstop";
879
880 if (creatingPCHWithPragmaHdrStop() &&
881 SourceMgr.isInMainFile(Tok.getLocation())) {
882 assert(CurLexer && "no lexer for #pragma hdrstop processing")((CurLexer && "no lexer for #pragma hdrstop processing"
) ? static_cast<void> (0) : __assert_fail ("CurLexer && \"no lexer for #pragma hdrstop processing\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 882, __PRETTY_FUNCTION__))
;
883 Token &Result = Tok;
884 Result.startToken();
885 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
886 CurLexer->cutOffLexing();
887 }
888 if (usingPCHWithPragmaHdrStop())
889 SkippingUntilPragmaHdrStop = false;
890}
891
892/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
893/// If 'Namespace' is non-null, then it is a token required to exist on the
894/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
895void Preprocessor::AddPragmaHandler(StringRef Namespace,
896 PragmaHandler *Handler) {
897 PragmaNamespace *InsertNS = PragmaHandlers.get();
898
899 // If this is specified to be in a namespace, step down into it.
900 if (!Namespace.empty()) {
901 // If there is already a pragma handler with the name of this namespace,
902 // we either have an error (directive with the same name as a namespace) or
903 // we already have the namespace to insert into.
904 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
905 InsertNS = Existing->getIfNamespace();
906 assert(InsertNS != nullptr && "Cannot have a pragma namespace and pragma"((InsertNS != nullptr && "Cannot have a pragma namespace and pragma"
" handler with the same name!") ? static_cast<void> (0
) : __assert_fail ("InsertNS != nullptr && \"Cannot have a pragma namespace and pragma\" \" handler with the same name!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 907, __PRETTY_FUNCTION__))
907 " handler with the same name!")((InsertNS != nullptr && "Cannot have a pragma namespace and pragma"
" handler with the same name!") ? static_cast<void> (0
) : __assert_fail ("InsertNS != nullptr && \"Cannot have a pragma namespace and pragma\" \" handler with the same name!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 907, __PRETTY_FUNCTION__))
;
908 } else {
909 // Otherwise, this namespace doesn't exist yet, create and insert the
910 // handler for it.
911 InsertNS = new PragmaNamespace(Namespace);
912 PragmaHandlers->AddPragma(InsertNS);
913 }
914 }
915
916 // Check to make sure we don't already have a pragma for this identifier.
917 assert(!InsertNS->FindHandler(Handler->getName()) &&((!InsertNS->FindHandler(Handler->getName()) &&
"Pragma handler already exists for this identifier!") ? static_cast
<void> (0) : __assert_fail ("!InsertNS->FindHandler(Handler->getName()) && \"Pragma handler already exists for this identifier!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 918, __PRETTY_FUNCTION__))
918 "Pragma handler already exists for this identifier!")((!InsertNS->FindHandler(Handler->getName()) &&
"Pragma handler already exists for this identifier!") ? static_cast
<void> (0) : __assert_fail ("!InsertNS->FindHandler(Handler->getName()) && \"Pragma handler already exists for this identifier!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 918, __PRETTY_FUNCTION__))
;
919 InsertNS->AddPragma(Handler);
920}
921
922/// RemovePragmaHandler - Remove the specific pragma handler from the
923/// preprocessor. If \arg Namespace is non-null, then it should be the
924/// namespace that \arg Handler was added to. It is an error to remove
925/// a handler that has not been registered.
926void Preprocessor::RemovePragmaHandler(StringRef Namespace,
927 PragmaHandler *Handler) {
928 PragmaNamespace *NS = PragmaHandlers.get();
929
930 // If this is specified to be in a namespace, step down into it.
931 if (!Namespace.empty()) {
932 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
933 assert(Existing && "Namespace containing handler does not exist!")((Existing && "Namespace containing handler does not exist!"
) ? static_cast<void> (0) : __assert_fail ("Existing && \"Namespace containing handler does not exist!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 933, __PRETTY_FUNCTION__))
;
934
935 NS = Existing->getIfNamespace();
936 assert(NS && "Invalid namespace, registered as a regular pragma handler!")((NS && "Invalid namespace, registered as a regular pragma handler!"
) ? static_cast<void> (0) : __assert_fail ("NS && \"Invalid namespace, registered as a regular pragma handler!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 936, __PRETTY_FUNCTION__))
;
937 }
938
939 NS->RemovePragmaHandler(Handler);
940
941 // If this is a non-default namespace and it is now empty, remove it.
942 if (NS != PragmaHandlers.get() && NS->IsEmpty()) {
943 PragmaHandlers->RemovePragmaHandler(NS);
944 delete NS;
945 }
946}
947
948bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
949 Token Tok;
950 LexUnexpandedToken(Tok);
951
952 if (Tok.isNot(tok::identifier)) {
953 Diag(Tok, diag::ext_on_off_switch_syntax);
954 return true;
955 }
956 IdentifierInfo *II = Tok.getIdentifierInfo();
957 if (II->isStr("ON"))
958 Result = tok::OOS_ON;
959 else if (II->isStr("OFF"))
960 Result = tok::OOS_OFF;
961 else if (II->isStr("DEFAULT"))
962 Result = tok::OOS_DEFAULT;
963 else {
964 Diag(Tok, diag::ext_on_off_switch_syntax);
965 return true;
966 }
967
968 // Verify that this is followed by EOD.
969 LexUnexpandedToken(Tok);
970 if (Tok.isNot(tok::eod))
971 Diag(Tok, diag::ext_pragma_syntax_eod);
972 return false;
973}
974
975namespace {
976
977/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
978struct PragmaOnceHandler : public PragmaHandler {
979 PragmaOnceHandler() : PragmaHandler("once") {}
980
981 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
982 Token &OnceTok) override {
983 PP.CheckEndOfDirective("pragma once");
984 PP.HandlePragmaOnce(OnceTok);
985 }
986};
987
988/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
989/// rest of the line is not lexed.
990struct PragmaMarkHandler : public PragmaHandler {
991 PragmaMarkHandler() : PragmaHandler("mark") {}
992
993 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
994 Token &MarkTok) override {
995 PP.HandlePragmaMark();
996 }
997};
998
999/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
1000struct PragmaPoisonHandler : public PragmaHandler {
1001 PragmaPoisonHandler() : PragmaHandler("poison") {}
1002
1003 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1004 Token &PoisonTok) override {
1005 PP.HandlePragmaPoison();
1006 }
1007};
1008
1009/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
1010/// as a system header, which silences warnings in it.
1011struct PragmaSystemHeaderHandler : public PragmaHandler {
1012 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
1013
1014 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1015 Token &SHToken) override {
1016 PP.HandlePragmaSystemHeader(SHToken);
1017 PP.CheckEndOfDirective("pragma");
1018 }
1019};
1020
1021struct PragmaDependencyHandler : public PragmaHandler {
1022 PragmaDependencyHandler() : PragmaHandler("dependency") {}
1023
1024 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1025 Token &DepToken) override {
1026 PP.HandlePragmaDependency(DepToken);
1027 }
1028};
1029
1030struct PragmaDebugHandler : public PragmaHandler {
1031 PragmaDebugHandler() : PragmaHandler("__debug") {}
1032
1033 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1034 Token &DebugToken) override {
1035 Token Tok;
1036 PP.LexUnexpandedToken(Tok);
1037 if (Tok.isNot(tok::identifier)) {
1
Calling 'Token::isNot'
4
Returning from 'Token::isNot'
5
Taking false branch
1038 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1039 return;
1040 }
1041 IdentifierInfo *II = Tok.getIdentifierInfo();
1042
1043 if (II->isStr("assert")) {
6
Calling 'IdentifierInfo::isStr'
9
Returning from 'IdentifierInfo::isStr'
10
Taking false branch
1044 if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash)
1045 llvm_unreachable("This is an assertion!")::llvm::llvm_unreachable_internal("This is an assertion!", "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 1045)
;
1046 } else if (II->isStr("crash")) {
11
Calling 'IdentifierInfo::isStr'
14
Returning from 'IdentifierInfo::isStr'
15
Taking false branch
1047 llvm::Timer T("crash", "pragma crash");
1048 llvm::TimeRegion R(&T);
1049 if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash)
1050 LLVM_BUILTIN_TRAP__builtin_trap();
1051 } else if (II->isStr("parser_crash")) {
16
Calling 'IdentifierInfo::isStr'
19
Returning from 'IdentifierInfo::isStr'
20
Taking false branch
1052 if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) {
1053 Token Crasher;
1054 Crasher.startToken();
1055 Crasher.setKind(tok::annot_pragma_parser_crash);
1056 Crasher.setAnnotationRange(SourceRange(Tok.getLocation()));
1057 PP.EnterToken(Crasher, /*IsReinject*/ false);
1058 }
1059 } else if (II->isStr("dump")) {
21
Calling 'IdentifierInfo::isStr'
24
Returning from 'IdentifierInfo::isStr'
25
Taking false branch
1060 Token Identifier;
1061 PP.LexUnexpandedToken(Identifier);
1062 if (auto *DumpII = Identifier.getIdentifierInfo()) {
1063 Token DumpAnnot;
1064 DumpAnnot.startToken();
1065 DumpAnnot.setKind(tok::annot_pragma_dump);
1066 DumpAnnot.setAnnotationRange(
1067 SourceRange(Tok.getLocation(), Identifier.getLocation()));
1068 DumpAnnot.setAnnotationValue(DumpII);
1069 PP.DiscardUntilEndOfDirective();
1070 PP.EnterToken(DumpAnnot, /*IsReinject*/false);
1071 } else {
1072 PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument)
1073 << II->getName();
1074 }
1075 } else if (II->isStr("diag_mapping")) {
26
Calling 'IdentifierInfo::isStr'
28
Returning from 'IdentifierInfo::isStr'
29
Taking false branch
1076 Token DiagName;
1077 PP.LexUnexpandedToken(DiagName);
1078 if (DiagName.is(tok::eod))
1079 PP.getDiagnostics().dump();
1080 else if (DiagName.is(tok::string_literal) && !DiagName.hasUDSuffix()) {
1081 StringLiteralParser Literal(DiagName, PP);
1082 if (Literal.hadError)
1083 return;
1084 PP.getDiagnostics().dump(Literal.GetString());
1085 } else {
1086 PP.Diag(DiagName, diag::warn_pragma_debug_missing_argument)
1087 << II->getName();
1088 }
1089 } else if (II->isStr("llvm_fatal_error")) {
30
Calling 'IdentifierInfo::isStr'
33
Returning from 'IdentifierInfo::isStr'
34
Taking false branch
1090 if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash)
1091 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
1092 } else if (II->isStr("llvm_unreachable")) {
35
Calling 'IdentifierInfo::isStr'
37
Returning from 'IdentifierInfo::isStr'
38
Taking false branch
1093 if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash)
1094 llvm_unreachable("#pragma clang __debug llvm_unreachable")::llvm::llvm_unreachable_internal("#pragma clang __debug llvm_unreachable"
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 1094)
;
1095 } else if (II->isStr("macro")) {
39
Calling 'IdentifierInfo::isStr'
41
Returning from 'IdentifierInfo::isStr'
42
Taking false branch
1096 Token MacroName;
1097 PP.LexUnexpandedToken(MacroName);
1098 auto *MacroII = MacroName.getIdentifierInfo();
1099 if (MacroII)
1100 PP.dumpMacroInfo(MacroII);
1101 else
1102 PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument)
1103 << II->getName();
1104 } else if (II->isStr("module_map")) {
43
Calling 'IdentifierInfo::isStr'
47
Returning from 'IdentifierInfo::isStr'
48
Taking true branch
1105 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1106 ModuleName;
1107 if (LexModuleName(PP, Tok, ModuleName))
49
Assuming the condition is false
50
Taking false branch
1108 return;
1109 ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap();
1110 Module *M = nullptr;
51
'M' initialized to a null pointer value
1111 for (auto IIAndLoc : ModuleName) {
52
Assuming '__begin11' is equal to '__end11'
1112 M = MM.lookupModuleQualified(IIAndLoc.first->getName(), M);
1113 if (!M) {
1114 PP.Diag(IIAndLoc.second, diag::warn_pragma_debug_unknown_module)
1115 << IIAndLoc.first;
1116 return;
1117 }
1118 }
1119 M->dump();
53
Called C++ object pointer is null
1120 } else if (II->isStr("overflow_stack")) {
1121 if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash)
1122 DebugOverflowStack();
1123 } else if (II->isStr("captured")) {
1124 HandleCaptured(PP);
1125 } else {
1126 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
1127 << II->getName();
1128 }
1129
1130 PPCallbacks *Callbacks = PP.getPPCallbacks();
1131 if (Callbacks)
1132 Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
1133 }
1134
1135 void HandleCaptured(Preprocessor &PP) {
1136 Token Tok;
1137 PP.LexUnexpandedToken(Tok);
1138
1139 if (Tok.isNot(tok::eod)) {
1140 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
1141 << "pragma clang __debug captured";
1142 return;
1143 }
1144
1145 SourceLocation NameLoc = Tok.getLocation();
1146 MutableArrayRef<Token> Toks(
1147 PP.getPreprocessorAllocator().Allocate<Token>(1), 1);
1148 Toks[0].startToken();
1149 Toks[0].setKind(tok::annot_pragma_captured);
1150 Toks[0].setLocation(NameLoc);
1151
1152 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
1153 /*IsReinject=*/false);
1154 }
1155
1156// Disable MSVC warning about runtime stack overflow.
1157#ifdef _MSC_VER
1158 #pragma warning(disable : 4717)
1159#endif
1160 static void DebugOverflowStack(void (*P)() = nullptr) {
1161 void (*volatile Self)(void(*P)()) = DebugOverflowStack;
1162 Self(reinterpret_cast<void(*)()>(Self));
1163 }
1164#ifdef _MSC_VER
1165 #pragma warning(default : 4717)
1166#endif
1167};
1168
1169/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
1170struct PragmaDiagnosticHandler : public PragmaHandler {
1171private:
1172 const char *Namespace;
1173
1174public:
1175 explicit PragmaDiagnosticHandler(const char *NS)
1176 : PragmaHandler("diagnostic"), Namespace(NS) {}
1177
1178 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1179 Token &DiagToken) override {
1180 SourceLocation DiagLoc = DiagToken.getLocation();
1181 Token Tok;
1182 PP.LexUnexpandedToken(Tok);
1183 if (Tok.isNot(tok::identifier)) {
1184 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1185 return;
1186 }
1187 IdentifierInfo *II = Tok.getIdentifierInfo();
1188 PPCallbacks *Callbacks = PP.getPPCallbacks();
1189
1190 if (II->isStr("pop")) {
1191 if (!PP.getDiagnostics().popMappings(DiagLoc))
1192 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
1193 else if (Callbacks)
1194 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
1195 return;
1196 } else if (II->isStr("push")) {
1197 PP.getDiagnostics().pushMappings(DiagLoc);
1198 if (Callbacks)
1199 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
1200 return;
1201 }
1202
1203 diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName())
1204 .Case("ignored", diag::Severity::Ignored)
1205 .Case("warning", diag::Severity::Warning)
1206 .Case("error", diag::Severity::Error)
1207 .Case("fatal", diag::Severity::Fatal)
1208 .Default(diag::Severity());
1209
1210 if (SV == diag::Severity()) {
1211 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1212 return;
1213 }
1214
1215 PP.LexUnexpandedToken(Tok);
1216 SourceLocation StringLoc = Tok.getLocation();
1217
1218 std::string WarningName;
1219 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
1220 /*AllowMacroExpansion=*/false))
1221 return;
1222
1223 if (Tok.isNot(tok::eod)) {
1224 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1225 return;
1226 }
1227
1228 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1229 (WarningName[1] != 'W' && WarningName[1] != 'R')) {
1230 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
1231 return;
1232 }
1233
1234 diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError
1235 : diag::Flavor::Remark;
1236 StringRef Group = StringRef(WarningName).substr(2);
1237 bool unknownDiag = false;
1238 if (Group == "everything") {
1239 // Special handling for pragma clang diagnostic ... "-Weverything".
1240 // There is no formal group named "everything", so there has to be a
1241 // special case for it.
1242 PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc);
1243 } else
1244 unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV,
1245 DiagLoc);
1246 if (unknownDiag)
1247 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1248 << WarningName;
1249 else if (Callbacks)
1250 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName);
1251 }
1252};
1253
1254/// "\#pragma hdrstop [<header-name-string>]"
1255struct PragmaHdrstopHandler : public PragmaHandler {
1256 PragmaHdrstopHandler() : PragmaHandler("hdrstop") {}
1257 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1258 Token &DepToken) override {
1259 PP.HandlePragmaHdrstop(DepToken);
1260 }
1261};
1262
1263/// "\#pragma warning(...)". MSVC's diagnostics do not map cleanly to clang's
1264/// diagnostics, so we don't really implement this pragma. We parse it and
1265/// ignore it to avoid -Wunknown-pragma warnings.
1266struct PragmaWarningHandler : public PragmaHandler {
1267 PragmaWarningHandler() : PragmaHandler("warning") {}
1268
1269 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1270 Token &Tok) override {
1271 // Parse things like:
1272 // warning(push, 1)
1273 // warning(pop)
1274 // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9)
1275 SourceLocation DiagLoc = Tok.getLocation();
1276 PPCallbacks *Callbacks = PP.getPPCallbacks();
1277
1278 PP.Lex(Tok);
1279 if (Tok.isNot(tok::l_paren)) {
1280 PP.Diag(Tok, diag::warn_pragma_warning_expected) << "(";
1281 return;
1282 }
1283
1284 PP.Lex(Tok);
1285 IdentifierInfo *II = Tok.getIdentifierInfo();
1286
1287 if (II && II->isStr("push")) {
1288 // #pragma warning( push[ ,n ] )
1289 int Level = -1;
1290 PP.Lex(Tok);
1291 if (Tok.is(tok::comma)) {
1292 PP.Lex(Tok);
1293 uint64_t Value;
1294 if (Tok.is(tok::numeric_constant) &&
1295 PP.parseSimpleIntegerLiteral(Tok, Value))
1296 Level = int(Value);
1297 if (Level < 0 || Level > 4) {
1298 PP.Diag(Tok, diag::warn_pragma_warning_push_level);
1299 return;
1300 }
1301 }
1302 if (Callbacks)
1303 Callbacks->PragmaWarningPush(DiagLoc, Level);
1304 } else if (II && II->isStr("pop")) {
1305 // #pragma warning( pop )
1306 PP.Lex(Tok);
1307 if (Callbacks)
1308 Callbacks->PragmaWarningPop(DiagLoc);
1309 } else {
1310 // #pragma warning( warning-specifier : warning-number-list
1311 // [; warning-specifier : warning-number-list...] )
1312 while (true) {
1313 II = Tok.getIdentifierInfo();
1314 if (!II && !Tok.is(tok::numeric_constant)) {
1315 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1316 return;
1317 }
1318
1319 // Figure out which warning specifier this is.
1320 bool SpecifierValid;
1321 StringRef Specifier;
1322 llvm::SmallString<1> SpecifierBuf;
1323 if (II) {
1324 Specifier = II->getName();
1325 SpecifierValid = llvm::StringSwitch<bool>(Specifier)
1326 .Cases("default", "disable", "error", "once",
1327 "suppress", true)
1328 .Default(false);
1329 // If we read a correct specifier, snatch next token (that should be
1330 // ":", checked later).
1331 if (SpecifierValid)
1332 PP.Lex(Tok);
1333 } else {
1334 // Token is a numeric constant. It should be either 1, 2, 3 or 4.
1335 uint64_t Value;
1336 Specifier = PP.getSpelling(Tok, SpecifierBuf);
1337 if (PP.parseSimpleIntegerLiteral(Tok, Value)) {
1338 SpecifierValid = (Value >= 1) && (Value <= 4);
1339 } else
1340 SpecifierValid = false;
1341 // Next token already snatched by parseSimpleIntegerLiteral.
1342 }
1343
1344 if (!SpecifierValid) {
1345 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1346 return;
1347 }
1348 if (Tok.isNot(tok::colon)) {
1349 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":";
1350 return;
1351 }
1352
1353 // Collect the warning ids.
1354 SmallVector<int, 4> Ids;
1355 PP.Lex(Tok);
1356 while (Tok.is(tok::numeric_constant)) {
1357 uint64_t Value;
1358 if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 ||
1359 Value > INT_MAX2147483647) {
1360 PP.Diag(Tok, diag::warn_pragma_warning_expected_number);
1361 return;
1362 }
1363 Ids.push_back(int(Value));
1364 }
1365 if (Callbacks)
1366 Callbacks->PragmaWarning(DiagLoc, Specifier, Ids);
1367
1368 // Parse the next specifier if there is a semicolon.
1369 if (Tok.isNot(tok::semi))
1370 break;
1371 PP.Lex(Tok);
1372 }
1373 }
1374
1375 if (Tok.isNot(tok::r_paren)) {
1376 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")";
1377 return;
1378 }
1379
1380 PP.Lex(Tok);
1381 if (Tok.isNot(tok::eod))
1382 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning";
1383 }
1384};
1385
1386/// "\#pragma execution_character_set(...)". MSVC supports this pragma only
1387/// for "UTF-8". We parse it and ignore it if UTF-8 is provided and warn
1388/// otherwise to avoid -Wunknown-pragma warnings.
1389struct PragmaExecCharsetHandler : public PragmaHandler {
1390 PragmaExecCharsetHandler() : PragmaHandler("execution_character_set") {}
1391
1392 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1393 Token &Tok) override {
1394 // Parse things like:
1395 // execution_character_set(push, "UTF-8")
1396 // execution_character_set(pop)
1397 SourceLocation DiagLoc = Tok.getLocation();
1398 PPCallbacks *Callbacks = PP.getPPCallbacks();
1399
1400 PP.Lex(Tok);
1401 if (Tok.isNot(tok::l_paren)) {
1402 PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << "(";
1403 return;
1404 }
1405
1406 PP.Lex(Tok);
1407 IdentifierInfo *II = Tok.getIdentifierInfo();
1408
1409 if (II && II->isStr("push")) {
1410 // #pragma execution_character_set( push[ , string ] )
1411 PP.Lex(Tok);
1412 if (Tok.is(tok::comma)) {
1413 PP.Lex(Tok);
1414
1415 std::string ExecCharset;
1416 if (!PP.FinishLexStringLiteral(Tok, ExecCharset,
1417 "pragma execution_character_set",
1418 /*AllowMacroExpansion=*/false))
1419 return;
1420
1421 // MSVC supports either of these, but nothing else.
1422 if (ExecCharset != "UTF-8" && ExecCharset != "utf-8") {
1423 PP.Diag(Tok, diag::warn_pragma_exec_charset_push_invalid) << ExecCharset;
1424 return;
1425 }
1426 }
1427 if (Callbacks)
1428 Callbacks->PragmaExecCharsetPush(DiagLoc, "UTF-8");
1429 } else if (II && II->isStr("pop")) {
1430 // #pragma execution_character_set( pop )
1431 PP.Lex(Tok);
1432 if (Callbacks)
1433 Callbacks->PragmaExecCharsetPop(DiagLoc);
1434 } else {
1435 PP.Diag(Tok, diag::warn_pragma_exec_charset_spec_invalid);
1436 return;
1437 }
1438
1439 if (Tok.isNot(tok::r_paren)) {
1440 PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << ")";
1441 return;
1442 }
1443
1444 PP.Lex(Tok);
1445 if (Tok.isNot(tok::eod))
1446 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma execution_character_set";
1447 }
1448};
1449
1450/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
1451struct PragmaIncludeAliasHandler : public PragmaHandler {
1452 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1453
1454 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1455 Token &IncludeAliasTok) override {
1456 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1457 }
1458};
1459
1460/// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1461/// extension. The syntax is:
1462/// \code
1463/// #pragma message(string)
1464/// \endcode
1465/// OR, in GCC mode:
1466/// \code
1467/// #pragma message string
1468/// \endcode
1469/// string is a string, which is fully macro expanded, and permits string
1470/// concatenation, embedded escape characters, etc... See MSDN for more details.
1471/// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1472/// form as \#pragma message.
1473struct PragmaMessageHandler : public PragmaHandler {
1474private:
1475 const PPCallbacks::PragmaMessageKind Kind;
1476 const StringRef Namespace;
1477
1478 static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1479 bool PragmaNameOnly = false) {
1480 switch (Kind) {
1481 case PPCallbacks::PMK_Message:
1482 return PragmaNameOnly ? "message" : "pragma message";
1483 case PPCallbacks::PMK_Warning:
1484 return PragmaNameOnly ? "warning" : "pragma warning";
1485 case PPCallbacks::PMK_Error:
1486 return PragmaNameOnly ? "error" : "pragma error";
1487 }
1488 llvm_unreachable("Unknown PragmaMessageKind!")::llvm::llvm_unreachable_internal("Unknown PragmaMessageKind!"
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/lib/Lex/Pragma.cpp"
, 1488)
;
1489 }
1490
1491public:
1492 PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1493 StringRef Namespace = StringRef())
1494 : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind),
1495 Namespace(Namespace) {}
1496
1497 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1498 Token &Tok) override {
1499 SourceLocation MessageLoc = Tok.getLocation();
1500 PP.Lex(Tok);
1501 bool ExpectClosingParen = false;
1502 switch (Tok.getKind()) {
1503 case tok::l_paren:
1504 // We have a MSVC style pragma message.
1505 ExpectClosingParen = true;
1506 // Read the string.
1507 PP.Lex(Tok);
1508 break;
1509 case tok::string_literal:
1510 // We have a GCC style pragma message, and we just read the string.
1511 break;
1512 default:
1513 PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1514 return;
1515 }
1516
1517 std::string MessageString;
1518 if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1519 /*AllowMacroExpansion=*/true))
1520 return;
1521
1522 if (ExpectClosingParen) {
1523 if (Tok.isNot(tok::r_paren)) {
1524 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1525 return;
1526 }
1527 PP.Lex(Tok); // eat the r_paren.
1528 }
1529
1530 if (Tok.isNot(tok::eod)) {
1531 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1532 return;
1533 }
1534
1535 // Output the message.
1536 PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1537 ? diag::err_pragma_message
1538 : diag::warn_pragma_message) << MessageString;
1539
1540 // If the pragma is lexically sound, notify any interested PPCallbacks.
1541 if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1542 Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
1543 }
1544};
1545
1546/// Handle the clang \#pragma module import extension. The syntax is:
1547/// \code
1548/// #pragma clang module import some.module.name
1549/// \endcode
1550struct PragmaModuleImportHandler : public PragmaHandler {
1551 PragmaModuleImportHandler() : PragmaHandler("import") {}
1552
1553 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1554 Token &Tok) override {
1555 SourceLocation ImportLoc = Tok.getLocation();
1556
1557 // Read the module name.
1558 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1559 ModuleName;
1560 if (LexModuleName(PP, Tok, ModuleName))
1561 return;
1562
1563 if (Tok.isNot(tok::eod))
1564 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1565
1566 // If we have a non-empty module path, load the named module.
1567 Module *Imported =
1568 PP.getModuleLoader().loadModule(ImportLoc, ModuleName, Module::Hidden,
1569 /*IsInclusionDirective=*/false);
1570 if (!Imported)
1571 return;
1572
1573 PP.makeModuleVisible(Imported, ImportLoc);
1574 PP.EnterAnnotationToken(SourceRange(ImportLoc, ModuleName.back().second),
1575 tok::annot_module_include, Imported);
1576 if (auto *CB = PP.getPPCallbacks())
1577 CB->moduleImport(ImportLoc, ModuleName, Imported);
1578 }
1579};
1580
1581/// Handle the clang \#pragma module begin extension. The syntax is:
1582/// \code
1583/// #pragma clang module begin some.module.name
1584/// ...
1585/// #pragma clang module end
1586/// \endcode
1587struct PragmaModuleBeginHandler : public PragmaHandler {
1588 PragmaModuleBeginHandler() : PragmaHandler("begin") {}
1589
1590 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1591 Token &Tok) override {
1592 SourceLocation BeginLoc = Tok.getLocation();
1593
1594 // Read the module name.
1595 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1596 ModuleName;
1597 if (LexModuleName(PP, Tok, ModuleName))
1598 return;
1599
1600 if (Tok.isNot(tok::eod))
1601 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1602
1603 // We can only enter submodules of the current module.
1604 StringRef Current = PP.getLangOpts().CurrentModule;
1605 if (ModuleName.front().first->getName() != Current) {
1606 PP.Diag(ModuleName.front().second, diag::err_pp_module_begin_wrong_module)
1607 << ModuleName.front().first << (ModuleName.size() > 1)
1608 << Current.empty() << Current;
1609 return;
1610 }
1611
1612 // Find the module we're entering. We require that a module map for it
1613 // be loaded or implicitly loadable.
1614 auto &HSI = PP.getHeaderSearchInfo();
1615 Module *M = HSI.lookupModule(Current);
1616 if (!M) {
1617 PP.Diag(ModuleName.front().second,
1618 diag::err_pp_module_begin_no_module_map) << Current;
1619 return;
1620 }
1621 for (unsigned I = 1; I != ModuleName.size(); ++I) {
1622 auto *NewM = M->findOrInferSubmodule(ModuleName[I].first->getName());
1623 if (!NewM) {
1624 PP.Diag(ModuleName[I].second, diag::err_pp_module_begin_no_submodule)
1625 << M->getFullModuleName() << ModuleName[I].first;
1626 return;
1627 }
1628 M = NewM;
1629 }
1630
1631 // If the module isn't available, it doesn't make sense to enter it.
1632 if (Preprocessor::checkModuleIsAvailable(
1633 PP.getLangOpts(), PP.getTargetInfo(), PP.getDiagnostics(), M)) {
1634 PP.Diag(BeginLoc, diag::note_pp_module_begin_here)
1635 << M->getTopLevelModuleName();
1636 return;
1637 }
1638
1639 // Enter the scope of the submodule.
1640 PP.EnterSubmodule(M, BeginLoc, /*ForPragma*/true);
1641 PP.EnterAnnotationToken(SourceRange(BeginLoc, ModuleName.back().second),
1642 tok::annot_module_begin, M);
1643 }
1644};
1645
1646/// Handle the clang \#pragma module end extension.
1647struct PragmaModuleEndHandler : public PragmaHandler {
1648 PragmaModuleEndHandler() : PragmaHandler("end") {}
1649
1650 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1651 Token &Tok) override {
1652 SourceLocation Loc = Tok.getLocation();
1653
1654 PP.LexUnexpandedToken(Tok);
1655 if (Tok.isNot(tok::eod))
1656 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1657
1658 Module *M = PP.LeaveSubmodule(/*ForPragma*/true);
1659 if (M)
1660 PP.EnterAnnotationToken(SourceRange(Loc), tok::annot_module_end, M);
1661 else
1662 PP.Diag(Loc, diag::err_pp_module_end_without_module_begin);
1663 }
1664};
1665
1666/// Handle the clang \#pragma module build extension.
1667struct PragmaModuleBuildHandler : public PragmaHandler {
1668 PragmaModuleBuildHandler() : PragmaHandler("build") {}
1669
1670 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1671 Token &Tok) override {
1672 PP.HandlePragmaModuleBuild(Tok);
1673 }
1674};
1675
1676/// Handle the clang \#pragma module load extension.
1677struct PragmaModuleLoadHandler : public PragmaHandler {
1678 PragmaModuleLoadHandler() : PragmaHandler("load") {}
1679
1680 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1681 Token &Tok) override {
1682 SourceLocation Loc = Tok.getLocation();
1683
1684 // Read the module name.
1685 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1686 ModuleName;
1687 if (LexModuleName(PP, Tok, ModuleName))
1688 return;
1689
1690 if (Tok.isNot(tok::eod))
1691 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1692
1693 // Load the module, don't make it visible.
1694 PP.getModuleLoader().loadModule(Loc, ModuleName, Module::Hidden,
1695 /*IsInclusionDirective=*/false);
1696 }
1697};
1698
1699/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
1700/// macro on the top of the stack.
1701struct PragmaPushMacroHandler : public PragmaHandler {
1702 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
1703
1704 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1705 Token &PushMacroTok) override {
1706 PP.HandlePragmaPushMacro(PushMacroTok);
1707 }
1708};
1709
1710/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
1711/// macro to the value on the top of the stack.
1712struct PragmaPopMacroHandler : public PragmaHandler {
1713 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
1714
1715 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1716 Token &PopMacroTok) override {
1717 PP.HandlePragmaPopMacro(PopMacroTok);
1718 }
1719};
1720
1721/// PragmaARCCFCodeAuditedHandler -
1722/// \#pragma clang arc_cf_code_audited begin/end
1723struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1724 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1725
1726 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1727 Token &NameTok) override {
1728 SourceLocation Loc = NameTok.getLocation();
1729 bool IsBegin;
1730
1731 Token Tok;
1732
1733 // Lex the 'begin' or 'end'.
1734 PP.LexUnexpandedToken(Tok);
1735 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1736 if (BeginEnd && BeginEnd->isStr("begin")) {
1737 IsBegin = true;
1738 } else if (BeginEnd && BeginEnd->isStr("end")) {
1739 IsBegin = false;
1740 } else {
1741 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1742 return;
1743 }
1744
1745 // Verify that this is followed by EOD.
1746 PP.LexUnexpandedToken(Tok);
1747 if (Tok.isNot(tok::eod))
1748 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1749
1750 // The start location of the active audit.
1751 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedInfo().second;
1752
1753 // The start location we want after processing this.
1754 SourceLocation NewLoc;
1755
1756 if (IsBegin) {
1757 // Complain about attempts to re-enter an audit.
1758 if (BeginLoc.isValid()) {
1759 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1760 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1761 }
1762 NewLoc = Loc;
1763 } else {
1764 // Complain about attempts to leave an audit that doesn't exist.
1765 if (!BeginLoc.isValid()) {
1766 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1767 return;
1768 }
1769 NewLoc = SourceLocation();
1770 }
1771
1772 PP.setPragmaARCCFCodeAuditedInfo(NameTok.getIdentifierInfo(), NewLoc);
1773 }
1774};
1775
1776/// PragmaAssumeNonNullHandler -
1777/// \#pragma clang assume_nonnull begin/end
1778struct PragmaAssumeNonNullHandler : public PragmaHandler {
1779 PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {}
1780
1781 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1782 Token &NameTok) override {
1783 SourceLocation Loc = NameTok.getLocation();
1784 bool IsBegin;
1785
1786 Token Tok;
1787
1788 // Lex the 'begin' or 'end'.
1789 PP.LexUnexpandedToken(Tok);
1790 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1791 if (BeginEnd && BeginEnd->isStr("begin")) {
1792 IsBegin = true;
1793 } else if (BeginEnd && BeginEnd->isStr("end")) {
1794 IsBegin = false;
1795 } else {
1796 PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax);
1797 return;
1798 }
1799
1800 // Verify that this is followed by EOD.
1801 PP.LexUnexpandedToken(Tok);
1802 if (Tok.isNot(tok::eod))
1803 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1804
1805 // The start location of the active audit.
1806 SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc();
1807
1808 // The start location we want after processing this.
1809 SourceLocation NewLoc;
1810 PPCallbacks *Callbacks = PP.getPPCallbacks();
1811
1812 if (IsBegin) {
1813 // Complain about attempts to re-enter an audit.
1814 if (BeginLoc.isValid()) {
1815 PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull);
1816 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1817 }
1818 NewLoc = Loc;
1819 if (Callbacks)
1820 Callbacks->PragmaAssumeNonNullBegin(NewLoc);
1821 } else {
1822 // Complain about attempts to leave an audit that doesn't exist.
1823 if (!BeginLoc.isValid()) {
1824 PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull);
1825 return;
1826 }
1827 NewLoc = SourceLocation();
1828 if (Callbacks)
1829 Callbacks->PragmaAssumeNonNullEnd(NewLoc);
1830 }
1831
1832 PP.setPragmaAssumeNonNullLoc(NewLoc);
1833 }
1834};
1835
1836/// Handle "\#pragma region [...]"
1837///
1838/// The syntax is
1839/// \code
1840/// #pragma region [optional name]
1841/// #pragma endregion [optional comment]
1842/// \endcode
1843///
1844/// \note This is
1845/// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1846/// pragma, just skipped by compiler.
1847struct PragmaRegionHandler : public PragmaHandler {
1848 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {}
1849
1850 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1851 Token &NameTok) override {
1852 // #pragma region: endregion matches can be verified
1853 // __pragma(region): no sense, but ignored by msvc
1854 // _Pragma is not valid for MSVC, but there isn't any point
1855 // to handle a _Pragma differently.
1856 }
1857};
1858
1859} // namespace
1860
1861/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1862/// \#pragma GCC poison/system_header/dependency and \#pragma once.
1863void Preprocessor::RegisterBuiltinPragmas() {
1864 AddPragmaHandler(new PragmaOnceHandler());
1865 AddPragmaHandler(new PragmaMarkHandler());
1866 AddPragmaHandler(new PragmaPushMacroHandler());
1867 AddPragmaHandler(new PragmaPopMacroHandler());
1868 AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
1869
1870 // #pragma GCC ...
1871 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1872 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1873 AddPragmaHandler("GCC", new PragmaDependencyHandler());
1874 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
1875 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1876 "GCC"));
1877 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1878 "GCC"));
1879 // #pragma clang ...
1880 AddPragmaHandler("clang", new PragmaPoisonHandler());
1881 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
1882 AddPragmaHandler("clang", new PragmaDebugHandler());
1883 AddPragmaHandler("clang", new PragmaDependencyHandler());
1884 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
1885 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
1886 AddPragmaHandler("clang", new PragmaAssumeNonNullHandler());
1887
1888 // #pragma clang module ...
1889 auto *ModuleHandler = new PragmaNamespace("module");
1890 AddPragmaHandler("clang", ModuleHandler);
1891 ModuleHandler->AddPragma(new PragmaModuleImportHandler());
1892 ModuleHandler->AddPragma(new PragmaModuleBeginHandler());
1893 ModuleHandler->AddPragma(new PragmaModuleEndHandler());
1894 ModuleHandler->AddPragma(new PragmaModuleBuildHandler());
1895 ModuleHandler->AddPragma(new PragmaModuleLoadHandler());
1896
1897 // Add region pragmas.
1898 AddPragmaHandler(new PragmaRegionHandler("region"));
1899 AddPragmaHandler(new PragmaRegionHandler("endregion"));
1900
1901 // MS extensions.
1902 if (LangOpts.MicrosoftExt) {
1903 AddPragmaHandler(new PragmaWarningHandler());
1904 AddPragmaHandler(new PragmaExecCharsetHandler());
1905 AddPragmaHandler(new PragmaIncludeAliasHandler());
1906 AddPragmaHandler(new PragmaHdrstopHandler());
1907 }
1908
1909 // Pragmas added by plugins
1910 for (const PragmaHandlerRegistry::entry &handler :
1911 PragmaHandlerRegistry::entries()) {
1912 AddPragmaHandler(handler.instantiate().release());
1913 }
1914}
1915
1916/// Ignore all pragmas, useful for modes such as -Eonly which would otherwise
1917/// warn about those pragmas being unknown.
1918void Preprocessor::IgnorePragmas() {
1919 AddPragmaHandler(new EmptyPragmaHandler());
1920 // Also ignore all pragmas in all namespaces created
1921 // in Preprocessor::RegisterBuiltinPragmas().
1922 AddPragmaHandler("GCC", new EmptyPragmaHandler());
1923 AddPragmaHandler("clang", new EmptyPragmaHandler());
1924}

/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h

1//===--- Token.h - Token interface ------------------------------*- 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 Token interface.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LEX_TOKEN_H
14#define LLVM_CLANG_LEX_TOKEN_H
15
16#include "clang/Basic/SourceLocation.h"
17#include "clang/Basic/TokenKinds.h"
18#include "llvm/ADT/StringRef.h"
19#include <cassert>
20
21namespace clang {
22
23class IdentifierInfo;
24
25/// Token - This structure provides full information about a lexed token.
26/// It is not intended to be space efficient, it is intended to return as much
27/// information as possible about each returned token. This is expected to be
28/// compressed into a smaller form if memory footprint is important.
29///
30/// The parser can create a special "annotation token" representing a stream of
31/// tokens that were parsed and semantically resolved, e.g.: "foo::MyClass<int>"
32/// can be represented by a single typename annotation token that carries
33/// information about the SourceRange of the tokens and the type object.
34class Token {
35 /// The location of the token. This is actually a SourceLocation.
36 unsigned Loc;
37
38 // Conceptually these next two fields could be in a union. However, this
39 // causes gcc 4.2 to pessimize LexTokenInternal, a very performance critical
40 // routine. Keeping as separate members with casts until a more beautiful fix
41 // presents itself.
42
43 /// UintData - This holds either the length of the token text, when
44 /// a normal token, or the end of the SourceRange when an annotation
45 /// token.
46 unsigned UintData;
47
48 /// PtrData - This is a union of four different pointer types, which depends
49 /// on what type of token this is:
50 /// Identifiers, keywords, etc:
51 /// This is an IdentifierInfo*, which contains the uniqued identifier
52 /// spelling.
53 /// Literals: isLiteral() returns true.
54 /// This is a pointer to the start of the token in a text buffer, which
55 /// may be dirty (have trigraphs / escaped newlines).
56 /// Annotations (resolved type names, C++ scopes, etc): isAnnotation().
57 /// This is a pointer to sema-specific data for the annotation token.
58 /// Eof:
59 // This is a pointer to a Decl.
60 /// Other:
61 /// This is null.
62 void *PtrData;
63
64 /// Kind - The actual flavor of token this is.
65 tok::TokenKind Kind;
66
67 /// Flags - Bits we track about this token, members of the TokenFlags enum.
68 unsigned short Flags;
69
70public:
71 // Various flags set per token:
72 enum TokenFlags {
73 StartOfLine = 0x01, // At start of line or only after whitespace
74 // (considering the line after macro expansion).
75 LeadingSpace = 0x02, // Whitespace exists before this token (considering
76 // whitespace after macro expansion).
77 DisableExpand = 0x04, // This identifier may never be macro expanded.
78 NeedsCleaning = 0x08, // Contained an escaped newline or trigraph.
79 LeadingEmptyMacro = 0x10, // Empty macro exists before this token.
80 HasUDSuffix = 0x20, // This string or character literal has a ud-suffix.
81 HasUCN = 0x40, // This identifier contains a UCN.
82 IgnoredComma = 0x80, // This comma is not a macro argument separator (MS).
83 StringifiedInMacro = 0x100, // This string or character literal is formed by
84 // macro stringizing or charizing operator.
85 CommaAfterElided = 0x200, // The comma following this token was elided (MS).
86 IsEditorPlaceholder = 0x400, // This identifier is a placeholder.
87 IsReinjected = 0x800, // A phase 4 token that was produced before and
88 // re-added, e.g. via EnterTokenStream. Annotation
89 // tokens are *not* reinjected.
90 };
91
92 tok::TokenKind getKind() const { return Kind; }
93 void setKind(tok::TokenKind K) { Kind = K; }
94
95 /// is/isNot - Predicates to check if this token is a specific kind, as in
96 /// "if (Tok.is(tok::l_brace)) {...}".
97 bool is(tok::TokenKind K) const { return Kind == K; }
98 bool isNot(tok::TokenKind K) const { return Kind != K; }
2
Assuming 'K' is equal to field 'Kind'
3
Returning zero, which participates in a condition later
99 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const {
100 return is(K1) || is(K2);
101 }
102 template <typename... Ts>
103 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, Ts... Ks) const {
104 return is(K1) || isOneOf(K2, Ks...);
105 }
106
107 /// Return true if this is a raw identifier (when lexing
108 /// in raw mode) or a non-keyword identifier (when lexing in non-raw mode).
109 bool isAnyIdentifier() const {
110 return tok::isAnyIdentifier(getKind());
111 }
112
113 /// Return true if this is a "literal", like a numeric
114 /// constant, string, etc.
115 bool isLiteral() const {
116 return tok::isLiteral(getKind());
117 }
118
119 /// Return true if this is any of tok::annot_* kind tokens.
120 bool isAnnotation() const {
121 return tok::isAnnotation(getKind());
122 }
123
124 /// Return a source location identifier for the specified
125 /// offset in the current file.
126 SourceLocation getLocation() const {
127 return SourceLocation::getFromRawEncoding(Loc);
128 }
129 unsigned getLength() const {
130 assert(!isAnnotation() && "Annotation tokens have no length field")((!isAnnotation() && "Annotation tokens have no length field"
) ? static_cast<void> (0) : __assert_fail ("!isAnnotation() && \"Annotation tokens have no length field\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 130, __PRETTY_FUNCTION__))
;
131 return UintData;
132 }
133
134 void setLocation(SourceLocation L) { Loc = L.getRawEncoding(); }
135 void setLength(unsigned Len) {
136 assert(!isAnnotation() && "Annotation tokens have no length field")((!isAnnotation() && "Annotation tokens have no length field"
) ? static_cast<void> (0) : __assert_fail ("!isAnnotation() && \"Annotation tokens have no length field\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 136, __PRETTY_FUNCTION__))
;
137 UintData = Len;
138 }
139
140 SourceLocation getAnnotationEndLoc() const {
141 assert(isAnnotation() && "Used AnnotEndLocID on non-annotation token")((isAnnotation() && "Used AnnotEndLocID on non-annotation token"
) ? static_cast<void> (0) : __assert_fail ("isAnnotation() && \"Used AnnotEndLocID on non-annotation token\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 141, __PRETTY_FUNCTION__))
;
142 return SourceLocation::getFromRawEncoding(UintData ? UintData : Loc);
143 }
144 void setAnnotationEndLoc(SourceLocation L) {
145 assert(isAnnotation() && "Used AnnotEndLocID on non-annotation token")((isAnnotation() && "Used AnnotEndLocID on non-annotation token"
) ? static_cast<void> (0) : __assert_fail ("isAnnotation() && \"Used AnnotEndLocID on non-annotation token\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 145, __PRETTY_FUNCTION__))
;
146 UintData = L.getRawEncoding();
147 }
148
149 SourceLocation getLastLoc() const {
150 return isAnnotation() ? getAnnotationEndLoc() : getLocation();
151 }
152
153 SourceLocation getEndLoc() const {
154 return isAnnotation() ? getAnnotationEndLoc()
155 : getLocation().getLocWithOffset(getLength());
156 }
157
158 /// SourceRange of the group of tokens that this annotation token
159 /// represents.
160 SourceRange getAnnotationRange() const {
161 return SourceRange(getLocation(), getAnnotationEndLoc());
162 }
163 void setAnnotationRange(SourceRange R) {
164 setLocation(R.getBegin());
165 setAnnotationEndLoc(R.getEnd());
166 }
167
168 const char *getName() const { return tok::getTokenName(Kind); }
169
170 /// Reset all flags to cleared.
171 void startToken() {
172 Kind = tok::unknown;
173 Flags = 0;
174 PtrData = nullptr;
175 UintData = 0;
176 Loc = SourceLocation().getRawEncoding();
177 }
178
179 IdentifierInfo *getIdentifierInfo() const {
180 assert(isNot(tok::raw_identifier) &&((isNot(tok::raw_identifier) && "getIdentifierInfo() on a tok::raw_identifier token!"
) ? static_cast<void> (0) : __assert_fail ("isNot(tok::raw_identifier) && \"getIdentifierInfo() on a tok::raw_identifier token!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 181, __PRETTY_FUNCTION__))
181 "getIdentifierInfo() on a tok::raw_identifier token!")((isNot(tok::raw_identifier) && "getIdentifierInfo() on a tok::raw_identifier token!"
) ? static_cast<void> (0) : __assert_fail ("isNot(tok::raw_identifier) && \"getIdentifierInfo() on a tok::raw_identifier token!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 181, __PRETTY_FUNCTION__))
;
182 assert(!isAnnotation() &&((!isAnnotation() && "getIdentifierInfo() on an annotation token!"
) ? static_cast<void> (0) : __assert_fail ("!isAnnotation() && \"getIdentifierInfo() on an annotation token!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 183, __PRETTY_FUNCTION__))
183 "getIdentifierInfo() on an annotation token!")((!isAnnotation() && "getIdentifierInfo() on an annotation token!"
) ? static_cast<void> (0) : __assert_fail ("!isAnnotation() && \"getIdentifierInfo() on an annotation token!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 183, __PRETTY_FUNCTION__))
;
184 if (isLiteral()) return nullptr;
185 if (is(tok::eof)) return nullptr;
186 return (IdentifierInfo*) PtrData;
187 }
188 void setIdentifierInfo(IdentifierInfo *II) {
189 PtrData = (void*) II;
190 }
191
192 const void *getEofData() const {
193 assert(is(tok::eof))((is(tok::eof)) ? static_cast<void> (0) : __assert_fail
("is(tok::eof)", "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 193, __PRETTY_FUNCTION__))
;
194 return reinterpret_cast<const void *>(PtrData);
195 }
196 void setEofData(const void *D) {
197 assert(is(tok::eof))((is(tok::eof)) ? static_cast<void> (0) : __assert_fail
("is(tok::eof)", "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 197, __PRETTY_FUNCTION__))
;
198 assert(!PtrData)((!PtrData) ? static_cast<void> (0) : __assert_fail ("!PtrData"
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 198, __PRETTY_FUNCTION__))
;
199 PtrData = const_cast<void *>(D);
200 }
201
202 /// getRawIdentifier - For a raw identifier token (i.e., an identifier
203 /// lexed in raw mode), returns a reference to the text substring in the
204 /// buffer if known.
205 StringRef getRawIdentifier() const {
206 assert(is(tok::raw_identifier))((is(tok::raw_identifier)) ? static_cast<void> (0) : __assert_fail
("is(tok::raw_identifier)", "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 206, __PRETTY_FUNCTION__))
;
207 return StringRef(reinterpret_cast<const char *>(PtrData), getLength());
208 }
209 void setRawIdentifierData(const char *Ptr) {
210 assert(is(tok::raw_identifier))((is(tok::raw_identifier)) ? static_cast<void> (0) : __assert_fail
("is(tok::raw_identifier)", "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 210, __PRETTY_FUNCTION__))
;
211 PtrData = const_cast<char*>(Ptr);
212 }
213
214 /// getLiteralData - For a literal token (numeric constant, string, etc), this
215 /// returns a pointer to the start of it in the text buffer if known, null
216 /// otherwise.
217 const char *getLiteralData() const {
218 assert(isLiteral() && "Cannot get literal data of non-literal")((isLiteral() && "Cannot get literal data of non-literal"
) ? static_cast<void> (0) : __assert_fail ("isLiteral() && \"Cannot get literal data of non-literal\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 218, __PRETTY_FUNCTION__))
;
219 return reinterpret_cast<const char*>(PtrData);
220 }
221 void setLiteralData(const char *Ptr) {
222 assert(isLiteral() && "Cannot set literal data of non-literal")((isLiteral() && "Cannot set literal data of non-literal"
) ? static_cast<void> (0) : __assert_fail ("isLiteral() && \"Cannot set literal data of non-literal\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 222, __PRETTY_FUNCTION__))
;
223 PtrData = const_cast<char*>(Ptr);
224 }
225
226 void *getAnnotationValue() const {
227 assert(isAnnotation() && "Used AnnotVal on non-annotation token")((isAnnotation() && "Used AnnotVal on non-annotation token"
) ? static_cast<void> (0) : __assert_fail ("isAnnotation() && \"Used AnnotVal on non-annotation token\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 227, __PRETTY_FUNCTION__))
;
228 return PtrData;
229 }
230 void setAnnotationValue(void *val) {
231 assert(isAnnotation() && "Used AnnotVal on non-annotation token")((isAnnotation() && "Used AnnotVal on non-annotation token"
) ? static_cast<void> (0) : __assert_fail ("isAnnotation() && \"Used AnnotVal on non-annotation token\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Lex/Token.h"
, 231, __PRETTY_FUNCTION__))
;
232 PtrData = val;
233 }
234
235 /// Set the specified flag.
236 void setFlag(TokenFlags Flag) {
237 Flags |= Flag;
238 }
239
240 /// Get the specified flag.
241 bool getFlag(TokenFlags Flag) const {
242 return (Flags & Flag) != 0;
243 }
244
245 /// Unset the specified flag.
246 void clearFlag(TokenFlags Flag) {
247 Flags &= ~Flag;
248 }
249
250 /// Return the internal represtation of the flags.
251 ///
252 /// This is only intended for low-level operations such as writing tokens to
253 /// disk.
254 unsigned getFlags() const {
255 return Flags;
256 }
257
258 /// Set a flag to either true or false.
259 void setFlagValue(TokenFlags Flag, bool Val) {
260 if (Val)
261 setFlag(Flag);
262 else
263 clearFlag(Flag);
264 }
265
266 /// isAtStartOfLine - Return true if this token is at the start of a line.
267 ///
268 bool isAtStartOfLine() const { return getFlag(StartOfLine); }
269
270 /// Return true if this token has whitespace before it.
271 ///
272 bool hasLeadingSpace() const { return getFlag(LeadingSpace); }
273
274 /// Return true if this identifier token should never
275 /// be expanded in the future, due to C99 6.10.3.4p2.
276 bool isExpandDisabled() const { return getFlag(DisableExpand); }
277
278 /// Return true if we have an ObjC keyword identifier.
279 bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const;
280
281 /// Return the ObjC keyword kind.
282 tok::ObjCKeywordKind getObjCKeywordID() const;
283
284 /// Return true if this token has trigraphs or escaped newlines in it.
285 bool needsCleaning() const { return getFlag(NeedsCleaning); }
286
287 /// Return true if this token has an empty macro before it.
288 ///
289 bool hasLeadingEmptyMacro() const { return getFlag(LeadingEmptyMacro); }
290
291 /// Return true if this token is a string or character literal which
292 /// has a ud-suffix.
293 bool hasUDSuffix() const { return getFlag(HasUDSuffix); }
294
295 /// Returns true if this token contains a universal character name.
296 bool hasUCN() const { return getFlag(HasUCN); }
297
298 /// Returns true if this token is formed by macro by stringizing or charizing
299 /// operator.
300 bool stringifiedInMacro() const { return getFlag(StringifiedInMacro); }
301
302 /// Returns true if the comma after this token was elided.
303 bool commaAfterElided() const { return getFlag(CommaAfterElided); }
304
305 /// Returns true if this token is an editor placeholder.
306 ///
307 /// Editor placeholders are produced by the code-completion engine and are
308 /// represented as characters between '<#' and '#>' in the source code. The
309 /// lexer uses identifier tokens to represent placeholders.
310 bool isEditorPlaceholder() const { return getFlag(IsEditorPlaceholder); }
311};
312
313/// Information about the conditional stack (\#if directives)
314/// currently active.
315struct PPConditionalInfo {
316 /// Location where the conditional started.
317 SourceLocation IfLoc;
318
319 /// True if this was contained in a skipping directive, e.g.,
320 /// in a "\#if 0" block.
321 bool WasSkipping;
322
323 /// True if we have emitted tokens already, and now we're in
324 /// an \#else block or something. Only useful in Skipping blocks.
325 bool FoundNonSkip;
326
327 /// True if we've seen a \#else in this block. If so,
328 /// \#elif/\#else directives are not allowed.
329 bool FoundElse;
330};
331
332} // end namespace clang
333
334#endif // LLVM_CLANG_LEX_TOKEN_H

/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Basic/IdentifierTable.h

1//===- IdentifierTable.h - Hash table for identifier lookup -----*- 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/// \file
10/// Defines the clang::IdentifierInfo, clang::IdentifierTable, and
11/// clang::Selector interfaces.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_BASIC_IDENTIFIERTABLE_H
16#define LLVM_CLANG_BASIC_IDENTIFIERTABLE_H
17
18#include "clang/Basic/LLVM.h"
19#include "clang/Basic/TokenKinds.h"
20#include "llvm/ADT/DenseMapInfo.h"
21#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/StringMap.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/Support/Allocator.h"
25#include "llvm/Support/PointerLikeTypeTraits.h"
26#include "llvm/Support/type_traits.h"
27#include <cassert>
28#include <cstddef>
29#include <cstdint>
30#include <cstring>
31#include <string>
32#include <utility>
33
34namespace clang {
35
36class DeclarationName;
37class DeclarationNameTable;
38class IdentifierInfo;
39class LangOptions;
40class MultiKeywordSelector;
41class SourceLocation;
42
43/// A simple pair of identifier info and location.
44using IdentifierLocPair = std::pair<IdentifierInfo *, SourceLocation>;
45
46/// IdentifierInfo and other related classes are aligned to
47/// 8 bytes so that DeclarationName can use the lower 3 bits
48/// of a pointer to one of these classes.
49enum { IdentifierInfoAlignment = 8 };
50
51static constexpr int ObjCOrBuiltinIDBits = 15;
52
53/// One of these records is kept for each identifier that
54/// is lexed. This contains information about whether the token was \#define'd,
55/// is a language keyword, or if it is a front-end token of some sort (e.g. a
56/// variable or function name). The preprocessor keeps this information in a
57/// set, and all tok::identifier tokens have a pointer to one of these.
58/// It is aligned to 8 bytes because DeclarationName needs the lower 3 bits.
59class alignas(IdentifierInfoAlignment) IdentifierInfo {
60 friend class IdentifierTable;
61
62 // Front-end token ID or tok::identifier.
63 unsigned TokenID : 9;
64
65 // ObjC keyword ('protocol' in '@protocol') or builtin (__builtin_inf).
66 // First NUM_OBJC_KEYWORDS values are for Objective-C,
67 // the remaining values are for builtins.
68 unsigned ObjCOrBuiltinID : ObjCOrBuiltinIDBits;
69
70 // True if there is a #define for this.
71 unsigned HasMacro : 1;
72
73 // True if there was a #define for this.
74 unsigned HadMacro : 1;
75
76 // True if the identifier is a language extension.
77 unsigned IsExtension : 1;
78
79 // True if the identifier is a keyword in a newer or proposed Standard.
80 unsigned IsFutureCompatKeyword : 1;
81
82 // True if the identifier is poisoned.
83 unsigned IsPoisoned : 1;
84
85 // True if the identifier is a C++ operator keyword.
86 unsigned IsCPPOperatorKeyword : 1;
87
88 // Internal bit set by the member function RecomputeNeedsHandleIdentifier.
89 // See comment about RecomputeNeedsHandleIdentifier for more info.
90 unsigned NeedsHandleIdentifier : 1;
91
92 // True if the identifier was loaded (at least partially) from an AST file.
93 unsigned IsFromAST : 1;
94
95 // True if the identifier has changed from the definition
96 // loaded from an AST file.
97 unsigned ChangedAfterLoad : 1;
98
99 // True if the identifier's frontend information has changed from the
100 // definition loaded from an AST file.
101 unsigned FEChangedAfterLoad : 1;
102
103 // True if revertTokenIDToIdentifier was called.
104 unsigned RevertedTokenID : 1;
105
106 // True if there may be additional information about
107 // this identifier stored externally.
108 unsigned OutOfDate : 1;
109
110 // True if this is the 'import' contextual keyword.
111 unsigned IsModulesImport : 1;
112
113 // True if this is a mangled OpenMP variant name.
114 unsigned IsMangledOpenMPVariantName : 1;
115
116 // 28 bits left in a 64-bit word.
117
118 // Managed by the language front-end.
119 void *FETokenInfo = nullptr;
120
121 llvm::StringMapEntry<IdentifierInfo *> *Entry = nullptr;
122
123 IdentifierInfo()
124 : TokenID(tok::identifier), ObjCOrBuiltinID(0), HasMacro(false),
125 HadMacro(false), IsExtension(false), IsFutureCompatKeyword(false),
126 IsPoisoned(false), IsCPPOperatorKeyword(false),
127 NeedsHandleIdentifier(false), IsFromAST(false), ChangedAfterLoad(false),
128 FEChangedAfterLoad(false), RevertedTokenID(false), OutOfDate(false),
129 IsModulesImport(false), IsMangledOpenMPVariantName(false) {}
130
131public:
132 IdentifierInfo(const IdentifierInfo &) = delete;
133 IdentifierInfo &operator=(const IdentifierInfo &) = delete;
134 IdentifierInfo(IdentifierInfo &&) = delete;
135 IdentifierInfo &operator=(IdentifierInfo &&) = delete;
136
137 /// Return true if this is the identifier for the specified string.
138 ///
139 /// This is intended to be used for string literals only: II->isStr("foo").
140 template <std::size_t StrLen>
141 bool isStr(const char (&Str)[StrLen]) const {
142 return getLength() == StrLen-1 &&
7
Assuming the condition is false
8
Returning zero, which participates in a condition later
12
Assuming the condition is false
13
Returning zero, which participates in a condition later
17
Assuming the condition is false
18
Returning zero, which participates in a condition later
22
Assuming the condition is false
23
Returning zero, which participates in a condition later
27
Returning zero, which participates in a condition later
31
Assuming the condition is false
32
Returning zero, which participates in a condition later
36
Returning zero, which participates in a condition later
40
Returning zero, which participates in a condition later
44
Assuming the condition is true
46
Returning the value 1, which participates in a condition later
143 memcmp(getNameStart(), Str, StrLen-1) == 0;
45
Assuming the condition is true
144 }
145
146 /// Return true if this is the identifier for the specified StringRef.
147 bool isStr(llvm::StringRef Str) const {
148 llvm::StringRef ThisStr(getNameStart(), getLength());
149 return ThisStr == Str;
150 }
151
152 /// Return the beginning of the actual null-terminated string for this
153 /// identifier.
154 const char *getNameStart() const { return Entry->getKeyData(); }
155
156 /// Efficiently return the length of this identifier info.
157 unsigned getLength() const { return Entry->getKeyLength(); }
158
159 /// Return the actual identifier string.
160 StringRef getName() const {
161 return StringRef(getNameStart(), getLength());
162 }
163
164 /// Return true if this identifier is \#defined to some other value.
165 /// \note The current definition may be in a module and not currently visible.
166 bool hasMacroDefinition() const {
167 return HasMacro;
168 }
169 void setHasMacroDefinition(bool Val) {
170 if (HasMacro == Val) return;
171
172 HasMacro = Val;
173 if (Val) {
174 NeedsHandleIdentifier = true;
175 HadMacro = true;
176 } else {
177 RecomputeNeedsHandleIdentifier();
178 }
179 }
180 /// Returns true if this identifier was \#defined to some value at any
181 /// moment. In this case there should be an entry for the identifier in the
182 /// macro history table in Preprocessor.
183 bool hadMacroDefinition() const {
184 return HadMacro;
185 }
186
187 /// If this is a source-language token (e.g. 'for'), this API
188 /// can be used to cause the lexer to map identifiers to source-language
189 /// tokens.
190 tok::TokenKind getTokenID() const { return (tok::TokenKind)TokenID; }
191
192 /// True if revertTokenIDToIdentifier() was called.
193 bool hasRevertedTokenIDToIdentifier() const { return RevertedTokenID; }
194
195 /// Revert TokenID to tok::identifier; used for GNU libstdc++ 4.2
196 /// compatibility.
197 ///
198 /// TokenID is normally read-only but there are 2 instances where we revert it
199 /// to tok::identifier for libstdc++ 4.2. Keep track of when this happens
200 /// using this method so we can inform serialization about it.
201 void revertTokenIDToIdentifier() {
202 assert(TokenID != tok::identifier && "Already at tok::identifier")((TokenID != tok::identifier && "Already at tok::identifier"
) ? static_cast<void> (0) : __assert_fail ("TokenID != tok::identifier && \"Already at tok::identifier\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Basic/IdentifierTable.h"
, 202, __PRETTY_FUNCTION__))
;
203 TokenID = tok::identifier;
204 RevertedTokenID = true;
205 }
206 void revertIdentifierToTokenID(tok::TokenKind TK) {
207 assert(TokenID == tok::identifier && "Should be at tok::identifier")((TokenID == tok::identifier && "Should be at tok::identifier"
) ? static_cast<void> (0) : __assert_fail ("TokenID == tok::identifier && \"Should be at tok::identifier\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Basic/IdentifierTable.h"
, 207, __PRETTY_FUNCTION__))
;
208 TokenID = TK;
209 RevertedTokenID = false;
210 }
211
212 /// Return the preprocessor keyword ID for this identifier.
213 ///
214 /// For example, "define" will return tok::pp_define.
215 tok::PPKeywordKind getPPKeywordID() const;
216
217 /// Return the Objective-C keyword ID for the this identifier.
218 ///
219 /// For example, 'class' will return tok::objc_class if ObjC is enabled.
220 tok::ObjCKeywordKind getObjCKeywordID() const {
221 if (ObjCOrBuiltinID < tok::NUM_OBJC_KEYWORDS)
222 return tok::ObjCKeywordKind(ObjCOrBuiltinID);
223 else
224 return tok::objc_not_keyword;
225 }
226 void setObjCKeywordID(tok::ObjCKeywordKind ID) { ObjCOrBuiltinID = ID; }
227
228 /// True if setNotBuiltin() was called.
229 bool hasRevertedBuiltin() const {
230 return ObjCOrBuiltinID == tok::NUM_OBJC_KEYWORDS;
231 }
232
233 /// Revert the identifier to a non-builtin identifier. We do this if
234 /// the name of a known builtin library function is used to declare that
235 /// function, but an unexpected type is specified.
236 void revertBuiltin() {
237 setBuiltinID(0);
238 }
239
240 /// Return a value indicating whether this is a builtin function.
241 ///
242 /// 0 is not-built-in. 1+ are specific builtin functions.
243 unsigned getBuiltinID() const {
244 if (ObjCOrBuiltinID >= tok::NUM_OBJC_KEYWORDS)
245 return ObjCOrBuiltinID - tok::NUM_OBJC_KEYWORDS;
246 else
247 return 0;
248 }
249 void setBuiltinID(unsigned ID) {
250 ObjCOrBuiltinID = ID + tok::NUM_OBJC_KEYWORDS;
251 assert(ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID((ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID &&
"ID too large for field!") ? static_cast<void> (0) : __assert_fail
("ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID && \"ID too large for field!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Basic/IdentifierTable.h"
, 252, __PRETTY_FUNCTION__))
252 && "ID too large for field!")((ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID &&
"ID too large for field!") ? static_cast<void> (0) : __assert_fail
("ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID && \"ID too large for field!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Basic/IdentifierTable.h"
, 252, __PRETTY_FUNCTION__))
;
253 }
254
255 unsigned getObjCOrBuiltinID() const { return ObjCOrBuiltinID; }
256 void setObjCOrBuiltinID(unsigned ID) { ObjCOrBuiltinID = ID; }
257
258 /// get/setExtension - Initialize information about whether or not this
259 /// language token is an extension. This controls extension warnings, and is
260 /// only valid if a custom token ID is set.
261 bool isExtensionToken() const { return IsExtension; }
262 void setIsExtensionToken(bool Val) {
263 IsExtension = Val;
264 if (Val)
265 NeedsHandleIdentifier = true;
266 else
267 RecomputeNeedsHandleIdentifier();
268 }
269
270 /// is/setIsFutureCompatKeyword - Initialize information about whether or not
271 /// this language token is a keyword in a newer or proposed Standard. This
272 /// controls compatibility warnings, and is only true when not parsing the
273 /// corresponding Standard. Once a compatibility problem has been diagnosed
274 /// with this keyword, the flag will be cleared.
275 bool isFutureCompatKeyword() const { return IsFutureCompatKeyword; }
276 void setIsFutureCompatKeyword(bool Val) {
277 IsFutureCompatKeyword = Val;
278 if (Val)
279 NeedsHandleIdentifier = true;
280 else
281 RecomputeNeedsHandleIdentifier();
282 }
283
284 /// setIsPoisoned - Mark this identifier as poisoned. After poisoning, the
285 /// Preprocessor will emit an error every time this token is used.
286 void setIsPoisoned(bool Value = true) {
287 IsPoisoned = Value;
288 if (Value)
289 NeedsHandleIdentifier = true;
290 else
291 RecomputeNeedsHandleIdentifier();
292 }
293
294 /// Return true if this token has been poisoned.
295 bool isPoisoned() const { return IsPoisoned; }
296
297 /// isCPlusPlusOperatorKeyword/setIsCPlusPlusOperatorKeyword controls whether
298 /// this identifier is a C++ alternate representation of an operator.
299 void setIsCPlusPlusOperatorKeyword(bool Val = true) {
300 IsCPPOperatorKeyword = Val;
301 }
302 bool isCPlusPlusOperatorKeyword() const { return IsCPPOperatorKeyword; }
303
304 /// Return true if this token is a keyword in the specified language.
305 bool isKeyword(const LangOptions &LangOpts) const;
306
307 /// Return true if this token is a C++ keyword in the specified
308 /// language.
309 bool isCPlusPlusKeyword(const LangOptions &LangOpts) const;
310
311 /// Get and set FETokenInfo. The language front-end is allowed to associate
312 /// arbitrary metadata with this token.
313 void *getFETokenInfo() const { return FETokenInfo; }
314 void setFETokenInfo(void *T) { FETokenInfo = T; }
315
316 /// Return true if the Preprocessor::HandleIdentifier must be called
317 /// on a token of this identifier.
318 ///
319 /// If this returns false, we know that HandleIdentifier will not affect
320 /// the token.
321 bool isHandleIdentifierCase() const { return NeedsHandleIdentifier; }
322
323 /// Return true if the identifier in its current state was loaded
324 /// from an AST file.
325 bool isFromAST() const { return IsFromAST; }
326
327 void setIsFromAST() { IsFromAST = true; }
328
329 /// Determine whether this identifier has changed since it was loaded
330 /// from an AST file.
331 bool hasChangedSinceDeserialization() const {
332 return ChangedAfterLoad;
333 }
334
335 /// Note that this identifier has changed since it was loaded from
336 /// an AST file.
337 void setChangedSinceDeserialization() {
338 ChangedAfterLoad = true;
339 }
340
341 /// Determine whether the frontend token information for this
342 /// identifier has changed since it was loaded from an AST file.
343 bool hasFETokenInfoChangedSinceDeserialization() const {
344 return FEChangedAfterLoad;
345 }
346
347 /// Note that the frontend token information for this identifier has
348 /// changed since it was loaded from an AST file.
349 void setFETokenInfoChangedSinceDeserialization() {
350 FEChangedAfterLoad = true;
351 }
352
353 /// Determine whether the information for this identifier is out of
354 /// date with respect to the external source.
355 bool isOutOfDate() const { return OutOfDate; }
356
357 /// Set whether the information for this identifier is out of
358 /// date with respect to the external source.
359 void setOutOfDate(bool OOD) {
360 OutOfDate = OOD;
361 if (OOD)
362 NeedsHandleIdentifier = true;
363 else
364 RecomputeNeedsHandleIdentifier();
365 }
366
367 /// Determine whether this is the contextual keyword \c import.
368 bool isModulesImport() const { return IsModulesImport; }
369
370 /// Set whether this identifier is the contextual keyword \c import.
371 void setModulesImport(bool I) {
372 IsModulesImport = I;
373 if (I)
374 NeedsHandleIdentifier = true;
375 else
376 RecomputeNeedsHandleIdentifier();
377 }
378
379 /// Determine whether this is the mangled name of an OpenMP variant.
380 bool isMangledOpenMPVariantName() const { return IsMangledOpenMPVariantName; }
381
382 /// Set whether this is the mangled name of an OpenMP variant.
383 void setMangledOpenMPVariantName(bool I) { IsMangledOpenMPVariantName = I; }
384
385 /// Return true if this identifier is an editor placeholder.
386 ///
387 /// Editor placeholders are produced by the code-completion engine and are
388 /// represented as characters between '<#' and '#>' in the source code. An
389 /// example of auto-completed call with a placeholder parameter is shown
390 /// below:
391 /// \code
392 /// function(<#int x#>);
393 /// \endcode
394 bool isEditorPlaceholder() const {
395 return getName().startswith("<#") && getName().endswith("#>");
396 }
397
398 /// Determine whether \p this is a name reserved for the implementation (C99
399 /// 7.1.3, C++ [lib.global.names]).
400 bool isReservedName(bool doubleUnderscoreOnly = false) const {
401 if (getLength() < 2)
402 return false;
403 const char *Name = getNameStart();
404 return Name[0] == '_' &&
405 (Name[1] == '_' ||
406 (Name[1] >= 'A' && Name[1] <= 'Z' && !doubleUnderscoreOnly));
407 }
408
409 /// Provide less than operator for lexicographical sorting.
410 bool operator<(const IdentifierInfo &RHS) const {
411 return getName() < RHS.getName();
412 }
413
414private:
415 /// The Preprocessor::HandleIdentifier does several special (but rare)
416 /// things to identifiers of various sorts. For example, it changes the
417 /// \c for keyword token from tok::identifier to tok::for.
418 ///
419 /// This method is very tied to the definition of HandleIdentifier. Any
420 /// change to it should be reflected here.
421 void RecomputeNeedsHandleIdentifier() {
422 NeedsHandleIdentifier = isPoisoned() || hasMacroDefinition() ||
423 isExtensionToken() || isFutureCompatKeyword() ||
424 isOutOfDate() || isModulesImport();
425 }
426};
427
428/// An RAII object for [un]poisoning an identifier within a scope.
429///
430/// \p II is allowed to be null, in which case objects of this type have
431/// no effect.
432class PoisonIdentifierRAIIObject {
433 IdentifierInfo *const II;
434 const bool OldValue;
435
436public:
437 PoisonIdentifierRAIIObject(IdentifierInfo *II, bool NewValue)
438 : II(II), OldValue(II ? II->isPoisoned() : false) {
439 if(II)
440 II->setIsPoisoned(NewValue);
441 }
442
443 ~PoisonIdentifierRAIIObject() {
444 if(II)
445 II->setIsPoisoned(OldValue);
446 }
447};
448
449/// An iterator that walks over all of the known identifiers
450/// in the lookup table.
451///
452/// Since this iterator uses an abstract interface via virtual
453/// functions, it uses an object-oriented interface rather than the
454/// more standard C++ STL iterator interface. In this OO-style
455/// iteration, the single function \c Next() provides dereference,
456/// advance, and end-of-sequence checking in a single
457/// operation. Subclasses of this iterator type will provide the
458/// actual functionality.
459class IdentifierIterator {
460protected:
461 IdentifierIterator() = default;
462
463public:
464 IdentifierIterator(const IdentifierIterator &) = delete;
465 IdentifierIterator &operator=(const IdentifierIterator &) = delete;
466
467 virtual ~IdentifierIterator();
468
469 /// Retrieve the next string in the identifier table and
470 /// advances the iterator for the following string.
471 ///
472 /// \returns The next string in the identifier table. If there is
473 /// no such string, returns an empty \c StringRef.
474 virtual StringRef Next() = 0;
475};
476
477/// Provides lookups to, and iteration over, IdentiferInfo objects.
478class IdentifierInfoLookup {
479public:
480 virtual ~IdentifierInfoLookup();
481
482 /// Return the IdentifierInfo for the specified named identifier.
483 ///
484 /// Unlike the version in IdentifierTable, this returns a pointer instead
485 /// of a reference. If the pointer is null then the IdentifierInfo cannot
486 /// be found.
487 virtual IdentifierInfo* get(StringRef Name) = 0;
488
489 /// Retrieve an iterator into the set of all identifiers
490 /// known to this identifier lookup source.
491 ///
492 /// This routine provides access to all of the identifiers known to
493 /// the identifier lookup, allowing access to the contents of the
494 /// identifiers without introducing the overhead of constructing
495 /// IdentifierInfo objects for each.
496 ///
497 /// \returns A new iterator into the set of known identifiers. The
498 /// caller is responsible for deleting this iterator.
499 virtual IdentifierIterator *getIdentifiers();
500};
501
502/// Implements an efficient mapping from strings to IdentifierInfo nodes.
503///
504/// This has no other purpose, but this is an extremely performance-critical
505/// piece of the code, as each occurrence of every identifier goes through
506/// here when lexed.
507class IdentifierTable {
508 // Shark shows that using MallocAllocator is *much* slower than using this
509 // BumpPtrAllocator!
510 using HashTableTy = llvm::StringMap<IdentifierInfo *, llvm::BumpPtrAllocator>;
511 HashTableTy HashTable;
512
513 IdentifierInfoLookup* ExternalLookup;
514
515public:
516 /// Create the identifier table.
517 explicit IdentifierTable(IdentifierInfoLookup *ExternalLookup = nullptr);
518
519 /// Create the identifier table, populating it with info about the
520 /// language keywords for the language specified by \p LangOpts.
521 explicit IdentifierTable(const LangOptions &LangOpts,
522 IdentifierInfoLookup *ExternalLookup = nullptr);
523
524 /// Set the external identifier lookup mechanism.
525 void setExternalIdentifierLookup(IdentifierInfoLookup *IILookup) {
526 ExternalLookup = IILookup;
527 }
528
529 /// Retrieve the external identifier lookup object, if any.
530 IdentifierInfoLookup *getExternalIdentifierLookup() const {
531 return ExternalLookup;
532 }
533
534 llvm::BumpPtrAllocator& getAllocator() {
535 return HashTable.getAllocator();
536 }
537
538 /// Return the identifier token info for the specified named
539 /// identifier.
540 IdentifierInfo &get(StringRef Name) {
541 auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
542
543 IdentifierInfo *&II = Entry.second;
544 if (II) return *II;
545
546 // No entry; if we have an external lookup, look there first.
547 if (ExternalLookup) {
548 II = ExternalLookup->get(Name);
549 if (II)
550 return *II;
551 }
552
553 // Lookups failed, make a new IdentifierInfo.
554 void *Mem = getAllocator().Allocate<IdentifierInfo>();
555 II = new (Mem) IdentifierInfo();
556
557 // Make sure getName() knows how to find the IdentifierInfo
558 // contents.
559 II->Entry = &Entry;
560
561 return *II;
562 }
563
564 IdentifierInfo &get(StringRef Name, tok::TokenKind TokenCode) {
565 IdentifierInfo &II = get(Name);
566 II.TokenID = TokenCode;
567 assert(II.TokenID == (unsigned) TokenCode && "TokenCode too large")((II.TokenID == (unsigned) TokenCode && "TokenCode too large"
) ? static_cast<void> (0) : __assert_fail ("II.TokenID == (unsigned) TokenCode && \"TokenCode too large\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Basic/IdentifierTable.h"
, 567, __PRETTY_FUNCTION__))
;
568 return II;
569 }
570
571 /// Gets an IdentifierInfo for the given name without consulting
572 /// external sources.
573 ///
574 /// This is a version of get() meant for external sources that want to
575 /// introduce or modify an identifier. If they called get(), they would
576 /// likely end up in a recursion.
577 IdentifierInfo &getOwn(StringRef Name) {
578 auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
579
580 IdentifierInfo *&II = Entry.second;
581 if (II)
582 return *II;
583
584 // Lookups failed, make a new IdentifierInfo.
585 void *Mem = getAllocator().Allocate<IdentifierInfo>();
586 II = new (Mem) IdentifierInfo();
587
588 // Make sure getName() knows how to find the IdentifierInfo
589 // contents.
590 II->Entry = &Entry;
591
592 // If this is the 'import' contextual keyword, mark it as such.
593 if (Name.equals("import"))
594 II->setModulesImport(true);
595
596 return *II;
597 }
598
599 using iterator = HashTableTy::const_iterator;
600 using const_iterator = HashTableTy::const_iterator;
601
602 iterator begin() const { return HashTable.begin(); }
603 iterator end() const { return HashTable.end(); }
604 unsigned size() const { return HashTable.size(); }
605
606 iterator find(StringRef Name) const { return HashTable.find(Name); }
607
608 /// Print some statistics to stderr that indicate how well the
609 /// hashing is doing.
610 void PrintStats() const;
611
612 /// Populate the identifier table with info about the language keywords
613 /// for the language specified by \p LangOpts.
614 void AddKeywords(const LangOptions &LangOpts);
615};
616
617/// A family of Objective-C methods.
618///
619/// These families have no inherent meaning in the language, but are
620/// nonetheless central enough in the existing implementations to
621/// merit direct AST support. While, in theory, arbitrary methods can
622/// be considered to form families, we focus here on the methods
623/// involving allocation and retain-count management, as these are the
624/// most "core" and the most likely to be useful to diverse clients
625/// without extra information.
626///
627/// Both selectors and actual method declarations may be classified
628/// into families. Method families may impose additional restrictions
629/// beyond their selector name; for example, a method called '_init'
630/// that returns void is not considered to be in the 'init' family
631/// (but would be if it returned 'id'). It is also possible to
632/// explicitly change or remove a method's family. Therefore the
633/// method's family should be considered the single source of truth.
634enum ObjCMethodFamily {
635 /// No particular method family.
636 OMF_None,
637
638 // Selectors in these families may have arbitrary arity, may be
639 // written with arbitrary leading underscores, and may have
640 // additional CamelCase "words" in their first selector chunk
641 // following the family name.
642 OMF_alloc,
643 OMF_copy,
644 OMF_init,
645 OMF_mutableCopy,
646 OMF_new,
647
648 // These families are singletons consisting only of the nullary
649 // selector with the given name.
650 OMF_autorelease,
651 OMF_dealloc,
652 OMF_finalize,
653 OMF_release,
654 OMF_retain,
655 OMF_retainCount,
656 OMF_self,
657 OMF_initialize,
658
659 // performSelector families
660 OMF_performSelector
661};
662
663/// Enough bits to store any enumerator in ObjCMethodFamily or
664/// InvalidObjCMethodFamily.
665enum { ObjCMethodFamilyBitWidth = 4 };
666
667/// An invalid value of ObjCMethodFamily.
668enum { InvalidObjCMethodFamily = (1 << ObjCMethodFamilyBitWidth) - 1 };
669
670/// A family of Objective-C methods.
671///
672/// These are family of methods whose result type is initially 'id', but
673/// but are candidate for the result type to be changed to 'instancetype'.
674enum ObjCInstanceTypeFamily {
675 OIT_None,
676 OIT_Array,
677 OIT_Dictionary,
678 OIT_Singleton,
679 OIT_Init,
680 OIT_ReturnsSelf
681};
682
683enum ObjCStringFormatFamily {
684 SFF_None,
685 SFF_NSString,
686 SFF_CFString
687};
688
689/// Smart pointer class that efficiently represents Objective-C method
690/// names.
691///
692/// This class will either point to an IdentifierInfo or a
693/// MultiKeywordSelector (which is private). This enables us to optimize
694/// selectors that take no arguments and selectors that take 1 argument, which
695/// accounts for 78% of all selectors in Cocoa.h.
696class Selector {
697 friend class Diagnostic;
698 friend class SelectorTable; // only the SelectorTable can create these
699 friend class DeclarationName; // and the AST's DeclarationName.
700
701 enum IdentifierInfoFlag {
702 // Empty selector = 0. Note that these enumeration values must
703 // correspond to the enumeration values of DeclarationName::StoredNameKind
704 ZeroArg = 0x01,
705 OneArg = 0x02,
706 MultiArg = 0x07,
707 ArgFlags = 0x07
708 };
709
710 /// A pointer to the MultiKeywordSelector or IdentifierInfo. We use the low
711 /// three bits of InfoPtr to store an IdentifierInfoFlag. Note that in any
712 /// case IdentifierInfo and MultiKeywordSelector are already aligned to
713 /// 8 bytes even on 32 bits archs because of DeclarationName.
714 uintptr_t InfoPtr = 0;
715
716 Selector(IdentifierInfo *II, unsigned nArgs) {
717 InfoPtr = reinterpret_cast<uintptr_t>(II);
718 assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo")(((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo"
) ? static_cast<void> (0) : __assert_fail ("(InfoPtr & ArgFlags) == 0 &&\"Insufficiently aligned IdentifierInfo\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Basic/IdentifierTable.h"
, 718, __PRETTY_FUNCTION__))
;
719 assert(nArgs < 2 && "nArgs not equal to 0/1")((nArgs < 2 && "nArgs not equal to 0/1") ? static_cast
<void> (0) : __assert_fail ("nArgs < 2 && \"nArgs not equal to 0/1\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Basic/IdentifierTable.h"
, 719, __PRETTY_FUNCTION__))
;
720 InfoPtr |= nArgs+1;
721 }
722
723 Selector(MultiKeywordSelector *SI) {
724 InfoPtr = reinterpret_cast<uintptr_t>(SI);
725 assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo")(((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo"
) ? static_cast<void> (0) : __assert_fail ("(InfoPtr & ArgFlags) == 0 &&\"Insufficiently aligned IdentifierInfo\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Basic/IdentifierTable.h"
, 725, __PRETTY_FUNCTION__))
;
726 InfoPtr |= MultiArg;
727 }
728
729 IdentifierInfo *getAsIdentifierInfo() const {
730 if (getIdentifierInfoFlag() < MultiArg)
731 return reinterpret_cast<IdentifierInfo *>(InfoPtr & ~ArgFlags);
732 return nullptr;
733 }
734
735 MultiKeywordSelector *getMultiKeywordSelector() const {
736 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr & ~ArgFlags);
737 }
738
739 unsigned getIdentifierInfoFlag() const {
740 return InfoPtr & ArgFlags;
741 }
742
743 static ObjCMethodFamily getMethodFamilyImpl(Selector sel);
744
745 static ObjCStringFormatFamily getStringFormatFamilyImpl(Selector sel);
746
747public:
748 /// The default ctor should only be used when creating data structures that
749 /// will contain selectors.
750 Selector() = default;
751 explicit Selector(uintptr_t V) : InfoPtr(V) {}
752
753 /// operator==/!= - Indicate whether the specified selectors are identical.
754 bool operator==(Selector RHS) const {
755 return InfoPtr == RHS.InfoPtr;
756 }
757 bool operator!=(Selector RHS) const {
758 return InfoPtr != RHS.InfoPtr;
759 }
760
761 void *getAsOpaquePtr() const {
762 return reinterpret_cast<void*>(InfoPtr);
763 }
764
765 /// Determine whether this is the empty selector.
766 bool isNull() const { return InfoPtr == 0; }
767
768 // Predicates to identify the selector type.
769 bool isKeywordSelector() const {
770 return getIdentifierInfoFlag() != ZeroArg;
771 }
772
773 bool isUnarySelector() const {
774 return getIdentifierInfoFlag() == ZeroArg;
775 }
776
777 /// If this selector is the specific keyword selector described by Names.
778 bool isKeywordSelector(ArrayRef<StringRef> Names) const;
779
780 /// If this selector is the specific unary selector described by Name.
781 bool isUnarySelector(StringRef Name) const;
782
783 unsigned getNumArgs() const;
784
785 /// Retrieve the identifier at a given position in the selector.
786 ///
787 /// Note that the identifier pointer returned may be NULL. Clients that only
788 /// care about the text of the identifier string, and not the specific,
789 /// uniqued identifier pointer, should use \c getNameForSlot(), which returns
790 /// an empty string when the identifier pointer would be NULL.
791 ///
792 /// \param argIndex The index for which we want to retrieve the identifier.
793 /// This index shall be less than \c getNumArgs() unless this is a keyword
794 /// selector, in which case 0 is the only permissible value.
795 ///
796 /// \returns the uniqued identifier for this slot, or NULL if this slot has
797 /// no corresponding identifier.
798 IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const;
799
800 /// Retrieve the name at a given position in the selector.
801 ///
802 /// \param argIndex The index for which we want to retrieve the name.
803 /// This index shall be less than \c getNumArgs() unless this is a keyword
804 /// selector, in which case 0 is the only permissible value.
805 ///
806 /// \returns the name for this slot, which may be the empty string if no
807 /// name was supplied.
808 StringRef getNameForSlot(unsigned argIndex) const;
809
810 /// Derive the full selector name (e.g. "foo:bar:") and return
811 /// it as an std::string.
812 std::string getAsString() const;
813
814 /// Prints the full selector name (e.g. "foo:bar:").
815 void print(llvm::raw_ostream &OS) const;
816
817 void dump() const;
818
819 /// Derive the conventional family of this method.
820 ObjCMethodFamily getMethodFamily() const {
821 return getMethodFamilyImpl(*this);
822 }
823
824 ObjCStringFormatFamily getStringFormatFamily() const {
825 return getStringFormatFamilyImpl(*this);
826 }
827
828 static Selector getEmptyMarker() {
829 return Selector(uintptr_t(-1));
830 }
831
832 static Selector getTombstoneMarker() {
833 return Selector(uintptr_t(-2));
834 }
835
836 static ObjCInstanceTypeFamily getInstTypeMethodFamily(Selector sel);
837};
838
839/// This table allows us to fully hide how we implement
840/// multi-keyword caching.
841class SelectorTable {
842 // Actually a SelectorTableImpl
843 void *Impl;
844
845public:
846 SelectorTable();
847 SelectorTable(const SelectorTable &) = delete;
848 SelectorTable &operator=(const SelectorTable &) = delete;
849 ~SelectorTable();
850
851 /// Can create any sort of selector.
852 ///
853 /// \p NumArgs indicates whether this is a no argument selector "foo", a
854 /// single argument selector "foo:" or multi-argument "foo:bar:".
855 Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV);
856
857 Selector getUnarySelector(IdentifierInfo *ID) {
858 return Selector(ID, 1);
859 }
860
861 Selector getNullarySelector(IdentifierInfo *ID) {
862 return Selector(ID, 0);
863 }
864
865 /// Return the total amount of memory allocated for managing selectors.
866 size_t getTotalMemory() const;
867
868 /// Return the default setter name for the given identifier.
869 ///
870 /// This is "set" + \p Name where the initial character of \p Name
871 /// has been capitalized.
872 static SmallString<64> constructSetterName(StringRef Name);
873
874 /// Return the default setter selector for the given identifier.
875 ///
876 /// This is "set" + \p Name where the initial character of \p Name
877 /// has been capitalized.
878 static Selector constructSetterSelector(IdentifierTable &Idents,
879 SelectorTable &SelTable,
880 const IdentifierInfo *Name);
881
882 /// Return the property name for the given setter selector.
883 static std::string getPropertyNameFromSetterSelector(Selector Sel);
884};
885
886namespace detail {
887
888/// DeclarationNameExtra is used as a base of various uncommon special names.
889/// This class is needed since DeclarationName has not enough space to store
890/// the kind of every possible names. Therefore the kind of common names is
891/// stored directly in DeclarationName, and the kind of uncommon names is
892/// stored in DeclarationNameExtra. It is aligned to 8 bytes because
893/// DeclarationName needs the lower 3 bits to store the kind of common names.
894/// DeclarationNameExtra is tightly coupled to DeclarationName and any change
895/// here is very likely to require changes in DeclarationName(Table).
896class alignas(IdentifierInfoAlignment) DeclarationNameExtra {
897 friend class clang::DeclarationName;
898 friend class clang::DeclarationNameTable;
899
900protected:
901 /// The kind of "extra" information stored in the DeclarationName. See
902 /// @c ExtraKindOrNumArgs for an explanation of how these enumerator values
903 /// are used. Note that DeclarationName depends on the numerical values
904 /// of the enumerators in this enum. See DeclarationName::StoredNameKind
905 /// for more info.
906 enum ExtraKind {
907 CXXDeductionGuideName,
908 CXXLiteralOperatorName,
909 CXXUsingDirective,
910 ObjCMultiArgSelector
911 };
912
913 /// ExtraKindOrNumArgs has one of the following meaning:
914 /// * The kind of an uncommon C++ special name. This DeclarationNameExtra
915 /// is in this case in fact either a CXXDeductionGuideNameExtra or
916 /// a CXXLiteralOperatorIdName.
917 ///
918 /// * It may be also name common to C++ using-directives (CXXUsingDirective),
919 ///
920 /// * Otherwise it is ObjCMultiArgSelector+NumArgs, where NumArgs is
921 /// the number of arguments in the Objective-C selector, in which
922 /// case the DeclarationNameExtra is also a MultiKeywordSelector.
923 unsigned ExtraKindOrNumArgs;
924
925 DeclarationNameExtra(ExtraKind Kind) : ExtraKindOrNumArgs(Kind) {}
926 DeclarationNameExtra(unsigned NumArgs)
927 : ExtraKindOrNumArgs(ObjCMultiArgSelector + NumArgs) {}
928
929 /// Return the corresponding ExtraKind.
930 ExtraKind getKind() const {
931 return static_cast<ExtraKind>(ExtraKindOrNumArgs >
932 (unsigned)ObjCMultiArgSelector
933 ? (unsigned)ObjCMultiArgSelector
934 : ExtraKindOrNumArgs);
935 }
936
937 /// Return the number of arguments in an ObjC selector. Only valid when this
938 /// is indeed an ObjCMultiArgSelector.
939 unsigned getNumArgs() const {
940 assert(ExtraKindOrNumArgs >= (unsigned)ObjCMultiArgSelector &&((ExtraKindOrNumArgs >= (unsigned)ObjCMultiArgSelector &&
"getNumArgs called but this is not an ObjC selector!") ? static_cast
<void> (0) : __assert_fail ("ExtraKindOrNumArgs >= (unsigned)ObjCMultiArgSelector && \"getNumArgs called but this is not an ObjC selector!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Basic/IdentifierTable.h"
, 941, __PRETTY_FUNCTION__))
941 "getNumArgs called but this is not an ObjC selector!")((ExtraKindOrNumArgs >= (unsigned)ObjCMultiArgSelector &&
"getNumArgs called but this is not an ObjC selector!") ? static_cast
<void> (0) : __assert_fail ("ExtraKindOrNumArgs >= (unsigned)ObjCMultiArgSelector && \"getNumArgs called but this is not an ObjC selector!\""
, "/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/clang/include/clang/Basic/IdentifierTable.h"
, 941, __PRETTY_FUNCTION__))
;
942 return ExtraKindOrNumArgs - (unsigned)ObjCMultiArgSelector;
943 }
944};
945
946} // namespace detail
947
948} // namespace clang
949
950namespace llvm {
951
952/// Define DenseMapInfo so that Selectors can be used as keys in DenseMap and
953/// DenseSets.
954template <>
955struct DenseMapInfo<clang::Selector> {
956 static clang::Selector getEmptyKey() {
957 return clang::Selector::getEmptyMarker();
958 }
959
960 static clang::Selector getTombstoneKey() {
961 return clang::Selector::getTombstoneMarker();
962 }
963
964 static unsigned getHashValue(clang::Selector S);
965
966 static bool isEqual(clang::Selector LHS, clang::Selector RHS) {
967 return LHS == RHS;
968 }
969};
970
971template<>
972struct PointerLikeTypeTraits<clang::Selector> {
973 static const void *getAsVoidPointer(clang::Selector P) {
974 return P.getAsOpaquePtr();
975 }
976
977 static clang::Selector getFromVoidPointer(const void *P) {
978 return clang::Selector(reinterpret_cast<uintptr_t>(P));
979 }
980
981 static constexpr int NumLowBitsAvailable = 0;
982};
983
984// Provide PointerLikeTypeTraits for IdentifierInfo pointers, which
985// are not guaranteed to be 8-byte aligned.
986template<>
987struct PointerLikeTypeTraits<clang::IdentifierInfo*> {
988 static void *getAsVoidPointer(clang::IdentifierInfo* P) {
989 return P;
990 }
991
992 static clang::IdentifierInfo *getFromVoidPointer(void *P) {
993 return static_cast<clang::IdentifierInfo*>(P);
994 }
995
996 static constexpr int NumLowBitsAvailable = 1;
997};
998
999template<>
1000struct PointerLikeTypeTraits<const clang::IdentifierInfo*> {
1001 static const void *getAsVoidPointer(const clang::IdentifierInfo* P) {
1002 return P;
1003 }
1004
1005 static const clang::IdentifierInfo *getFromVoidPointer(const void *P) {
1006 return static_cast<const clang::IdentifierInfo*>(P);
1007 }
1008
1009 static constexpr int NumLowBitsAvailable = 1;
1010};
1011
1012} // namespace llvm
1013
1014#endif // LLVM_CLANG_BASIC_IDENTIFIERTABLE_H