Bug Summary

File:clang/lib/Lex/Pragma.cpp
Warning:line 1111, column 7
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -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 -mthread-model posix -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-11/lib/clang/11.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-11~++20200309111110+2c36c23f347/build-llvm/tools/clang/lib/Lex -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Lex -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/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-11/lib/clang/11.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-11~++20200309111110+2c36c23f347/build-llvm/tools/clang/lib/Lex -fdebug-prefix-map=/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-03-09-184146-41876-1 -x c++ /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/clang/lib/Lex/Pragma.cpp

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

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