Bug Summary

File:clang/lib/Lex/Pragma.cpp
Warning:line 1105, 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 -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.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-10~+201911111502510600c19528f1809/build-llvm/tools/clang/lib/Lex -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Lex -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/include -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/include -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/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-10/lib/clang/10.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-10~+201911111502510600c19528f1809/build-llvm/tools/clang/lib/Lex -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809=. -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-2019-12-09-002921-48462-1 -x c++ /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Lex/Pragma.cpp

/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/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/Token.h"
34#include "clang/Lex/TokenLexer.h"
35#include "llvm/ADT/ArrayRef.h"
36#include "llvm/ADT/DenseMap.h"
37#include "llvm/ADT/STLExtras.h"
38#include "llvm/ADT/SmallString.h"
39#include "llvm/ADT/SmallVector.h"
40#include "llvm/ADT/StringSwitch.h"
41#include "llvm/ADT/StringRef.h"
42#include "llvm/Support/CrashRecoveryContext.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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/clang/lib/Lex/Pragma.cpp"
, 850, __PRETTY_FUNCTION__))
;
851 TheModuleLoader.loadModuleFromSource(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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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
Taking false branch
1038 llvm_unreachable("This is an assertion!")::llvm::llvm_unreachable_internal("This is an assertion!", "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Lex/Pragma.cpp"
, 1038)
;
1039 } else if (II->isStr("crash")) {
7
Taking false branch
1040 LLVM_BUILTIN_TRAP__builtin_trap();
1041 } else if (II->isStr("parser_crash")) {
8
Calling 'IdentifierInfo::isStr'
11
Returning from 'IdentifierInfo::isStr'
12
Taking false branch
1042 Token Crasher;
1043 Crasher.startToken();
1044 Crasher.setKind(tok::annot_pragma_parser_crash);
1045 Crasher.setAnnotationRange(SourceRange(Tok.getLocation()));
1046 PP.EnterToken(Crasher, /*IsReinject*/false);
1047 } else if (II->isStr("dump")) {
13
Calling 'IdentifierInfo::isStr'
16
Returning from 'IdentifierInfo::isStr'
17
Taking false branch
1048 Token Identifier;
1049 PP.LexUnexpandedToken(Identifier);
1050 if (auto *DumpII = Identifier.getIdentifierInfo()) {
1051 Token DumpAnnot;
1052 DumpAnnot.startToken();
1053 DumpAnnot.setKind(tok::annot_pragma_dump);
1054 DumpAnnot.setAnnotationRange(
1055 SourceRange(Tok.getLocation(), Identifier.getLocation()));
1056 DumpAnnot.setAnnotationValue(DumpII);
1057 PP.DiscardUntilEndOfDirective();
1058 PP.EnterToken(DumpAnnot, /*IsReinject*/false);
1059 } else {
1060 PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument)
1061 << II->getName();
1062 }
1063 } else if (II->isStr("diag_mapping")) {
18
Calling 'IdentifierInfo::isStr'
20
Returning from 'IdentifierInfo::isStr'
21
Taking false branch
1064 Token DiagName;
1065 PP.LexUnexpandedToken(DiagName);
1066 if (DiagName.is(tok::eod))
1067 PP.getDiagnostics().dump();
1068 else if (DiagName.is(tok::string_literal) && !DiagName.hasUDSuffix()) {
1069 StringLiteralParser Literal(DiagName, PP);
1070 if (Literal.hadError)
1071 return;
1072 PP.getDiagnostics().dump(Literal.GetString());
1073 } else {
1074 PP.Diag(DiagName, diag::warn_pragma_debug_missing_argument)
1075 << II->getName();
1076 }
1077 } else if (II->isStr("llvm_fatal_error")) {
22
Taking false branch
1078 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
1079 } else if (II->isStr("llvm_unreachable")) {
23
Taking false branch
1080 llvm_unreachable("#pragma clang __debug llvm_unreachable")::llvm::llvm_unreachable_internal("#pragma clang __debug llvm_unreachable"
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Lex/Pragma.cpp"
, 1080)
;
1081 } else if (II->isStr("macro")) {
24
Calling 'IdentifierInfo::isStr'
26
Returning from 'IdentifierInfo::isStr'
27
Taking false branch
1082 Token MacroName;
1083 PP.LexUnexpandedToken(MacroName);
1084 auto *MacroII = MacroName.getIdentifierInfo();
1085 if (MacroII)
1086 PP.dumpMacroInfo(MacroII);
1087 else
1088 PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument)
1089 << II->getName();
1090 } else if (II->isStr("module_map")) {
28
Calling 'IdentifierInfo::isStr'
31
Returning from 'IdentifierInfo::isStr'
32
Taking true branch
1091 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1092 ModuleName;
1093 if (LexModuleName(PP, Tok, ModuleName))
33
Assuming the condition is false
34
Taking false branch
1094 return;
1095 ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap();
1096 Module *M = nullptr;
35
'M' initialized to a null pointer value
1097 for (auto IIAndLoc : ModuleName) {
36
Assuming '__begin11' is equal to '__end11'
1098 M = MM.lookupModuleQualified(IIAndLoc.first->getName(), M);
1099 if (!M) {
1100 PP.Diag(IIAndLoc.second, diag::warn_pragma_debug_unknown_module)
1101 << IIAndLoc.first;
1102 return;
1103 }
1104 }
1105 M->dump();
37
Called C++ object pointer is null
1106 } else if (II->isStr("overflow_stack")) {
1107 DebugOverflowStack();
1108 } else if (II->isStr("handle_crash")) {
1109 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
1110 if (CRC)
1111 CRC->HandleCrash();
1112 } else if (II->isStr("captured")) {
1113 HandleCaptured(PP);
1114 } else {
1115 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
1116 << II->getName();
1117 }
1118
1119 PPCallbacks *Callbacks = PP.getPPCallbacks();
1120 if (Callbacks)
1121 Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
1122 }
1123
1124 void HandleCaptured(Preprocessor &PP) {
1125 Token Tok;
1126 PP.LexUnexpandedToken(Tok);
1127
1128 if (Tok.isNot(tok::eod)) {
1129 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
1130 << "pragma clang __debug captured";
1131 return;
1132 }
1133
1134 SourceLocation NameLoc = Tok.getLocation();
1135 MutableArrayRef<Token> Toks(
1136 PP.getPreprocessorAllocator().Allocate<Token>(1), 1);
1137 Toks[0].startToken();
1138 Toks[0].setKind(tok::annot_pragma_captured);
1139 Toks[0].setLocation(NameLoc);
1140
1141 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
1142 /*IsReinject=*/false);
1143 }
1144
1145// Disable MSVC warning about runtime stack overflow.
1146#ifdef _MSC_VER
1147 #pragma warning(disable : 4717)
1148#endif
1149 static void DebugOverflowStack(void (*P)() = nullptr) {
1150 void (*volatile Self)(void(*P)()) = DebugOverflowStack;
1151 Self(reinterpret_cast<void(*)()>(Self));
1152 }
1153#ifdef _MSC_VER
1154 #pragma warning(default : 4717)
1155#endif
1156};
1157
1158/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
1159struct PragmaDiagnosticHandler : public PragmaHandler {
1160private:
1161 const char *Namespace;
1162
1163public:
1164 explicit PragmaDiagnosticHandler(const char *NS)
1165 : PragmaHandler("diagnostic"), Namespace(NS) {}
1166
1167 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1168 Token &DiagToken) override {
1169 SourceLocation DiagLoc = DiagToken.getLocation();
1170 Token Tok;
1171 PP.LexUnexpandedToken(Tok);
1172 if (Tok.isNot(tok::identifier)) {
1173 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1174 return;
1175 }
1176 IdentifierInfo *II = Tok.getIdentifierInfo();
1177 PPCallbacks *Callbacks = PP.getPPCallbacks();
1178
1179 if (II->isStr("pop")) {
1180 if (!PP.getDiagnostics().popMappings(DiagLoc))
1181 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
1182 else if (Callbacks)
1183 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
1184 return;
1185 } else if (II->isStr("push")) {
1186 PP.getDiagnostics().pushMappings(DiagLoc);
1187 if (Callbacks)
1188 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
1189 return;
1190 }
1191
1192 diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName())
1193 .Case("ignored", diag::Severity::Ignored)
1194 .Case("warning", diag::Severity::Warning)
1195 .Case("error", diag::Severity::Error)
1196 .Case("fatal", diag::Severity::Fatal)
1197 .Default(diag::Severity());
1198
1199 if (SV == diag::Severity()) {
1200 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1201 return;
1202 }
1203
1204 PP.LexUnexpandedToken(Tok);
1205 SourceLocation StringLoc = Tok.getLocation();
1206
1207 std::string WarningName;
1208 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
1209 /*AllowMacroExpansion=*/false))
1210 return;
1211
1212 if (Tok.isNot(tok::eod)) {
1213 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1214 return;
1215 }
1216
1217 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1218 (WarningName[1] != 'W' && WarningName[1] != 'R')) {
1219 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
1220 return;
1221 }
1222
1223 diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError
1224 : diag::Flavor::Remark;
1225 StringRef Group = StringRef(WarningName).substr(2);
1226 bool unknownDiag = false;
1227 if (Group == "everything") {
1228 // Special handling for pragma clang diagnostic ... "-Weverything".
1229 // There is no formal group named "everything", so there has to be a
1230 // special case for it.
1231 PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc);
1232 } else
1233 unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV,
1234 DiagLoc);
1235 if (unknownDiag)
1236 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1237 << WarningName;
1238 else if (Callbacks)
1239 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName);
1240 }
1241};
1242
1243/// "\#pragma hdrstop [<header-name-string>]"
1244struct PragmaHdrstopHandler : public PragmaHandler {
1245 PragmaHdrstopHandler() : PragmaHandler("hdrstop") {}
1246 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1247 Token &DepToken) override {
1248 PP.HandlePragmaHdrstop(DepToken);
1249 }
1250};
1251
1252/// "\#pragma warning(...)". MSVC's diagnostics do not map cleanly to clang's
1253/// diagnostics, so we don't really implement this pragma. We parse it and
1254/// ignore it to avoid -Wunknown-pragma warnings.
1255struct PragmaWarningHandler : public PragmaHandler {
1256 PragmaWarningHandler() : PragmaHandler("warning") {}
1257
1258 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1259 Token &Tok) override {
1260 // Parse things like:
1261 // warning(push, 1)
1262 // warning(pop)
1263 // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9)
1264 SourceLocation DiagLoc = Tok.getLocation();
1265 PPCallbacks *Callbacks = PP.getPPCallbacks();
1266
1267 PP.Lex(Tok);
1268 if (Tok.isNot(tok::l_paren)) {
1269 PP.Diag(Tok, diag::warn_pragma_warning_expected) << "(";
1270 return;
1271 }
1272
1273 PP.Lex(Tok);
1274 IdentifierInfo *II = Tok.getIdentifierInfo();
1275
1276 if (II && II->isStr("push")) {
1277 // #pragma warning( push[ ,n ] )
1278 int Level = -1;
1279 PP.Lex(Tok);
1280 if (Tok.is(tok::comma)) {
1281 PP.Lex(Tok);
1282 uint64_t Value;
1283 if (Tok.is(tok::numeric_constant) &&
1284 PP.parseSimpleIntegerLiteral(Tok, Value))
1285 Level = int(Value);
1286 if (Level < 0 || Level > 4) {
1287 PP.Diag(Tok, diag::warn_pragma_warning_push_level);
1288 return;
1289 }
1290 }
1291 if (Callbacks)
1292 Callbacks->PragmaWarningPush(DiagLoc, Level);
1293 } else if (II && II->isStr("pop")) {
1294 // #pragma warning( pop )
1295 PP.Lex(Tok);
1296 if (Callbacks)
1297 Callbacks->PragmaWarningPop(DiagLoc);
1298 } else {
1299 // #pragma warning( warning-specifier : warning-number-list
1300 // [; warning-specifier : warning-number-list...] )
1301 while (true) {
1302 II = Tok.getIdentifierInfo();
1303 if (!II && !Tok.is(tok::numeric_constant)) {
1304 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1305 return;
1306 }
1307
1308 // Figure out which warning specifier this is.
1309 bool SpecifierValid;
1310 StringRef Specifier;
1311 llvm::SmallString<1> SpecifierBuf;
1312 if (II) {
1313 Specifier = II->getName();
1314 SpecifierValid = llvm::StringSwitch<bool>(Specifier)
1315 .Cases("default", "disable", "error", "once",
1316 "suppress", true)
1317 .Default(false);
1318 // If we read a correct specifier, snatch next token (that should be
1319 // ":", checked later).
1320 if (SpecifierValid)
1321 PP.Lex(Tok);
1322 } else {
1323 // Token is a numeric constant. It should be either 1, 2, 3 or 4.
1324 uint64_t Value;
1325 Specifier = PP.getSpelling(Tok, SpecifierBuf);
1326 if (PP.parseSimpleIntegerLiteral(Tok, Value)) {
1327 SpecifierValid = (Value >= 1) && (Value <= 4);
1328 } else
1329 SpecifierValid = false;
1330 // Next token already snatched by parseSimpleIntegerLiteral.
1331 }
1332
1333 if (!SpecifierValid) {
1334 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1335 return;
1336 }
1337 if (Tok.isNot(tok::colon)) {
1338 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":";
1339 return;
1340 }
1341
1342 // Collect the warning ids.
1343 SmallVector<int, 4> Ids;
1344 PP.Lex(Tok);
1345 while (Tok.is(tok::numeric_constant)) {
1346 uint64_t Value;
1347 if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 ||
1348 Value > std::numeric_limits<int>::max()) {
1349 PP.Diag(Tok, diag::warn_pragma_warning_expected_number);
1350 return;
1351 }
1352 Ids.push_back(int(Value));
1353 }
1354 if (Callbacks)
1355 Callbacks->PragmaWarning(DiagLoc, Specifier, Ids);
1356
1357 // Parse the next specifier if there is a semicolon.
1358 if (Tok.isNot(tok::semi))
1359 break;
1360 PP.Lex(Tok);
1361 }
1362 }
1363
1364 if (Tok.isNot(tok::r_paren)) {
1365 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")";
1366 return;
1367 }
1368
1369 PP.Lex(Tok);
1370 if (Tok.isNot(tok::eod))
1371 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning";
1372 }
1373};
1374
1375/// "\#pragma execution_character_set(...)". MSVC supports this pragma only
1376/// for "UTF-8". We parse it and ignore it if UTF-8 is provided and warn
1377/// otherwise to avoid -Wunknown-pragma warnings.
1378struct PragmaExecCharsetHandler : public PragmaHandler {
1379 PragmaExecCharsetHandler() : PragmaHandler("execution_character_set") {}
1380
1381 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1382 Token &Tok) override {
1383 // Parse things like:
1384 // execution_character_set(push, "UTF-8")
1385 // execution_character_set(pop)
1386 SourceLocation DiagLoc = Tok.getLocation();
1387 PPCallbacks *Callbacks = PP.getPPCallbacks();
1388
1389 PP.Lex(Tok);
1390 if (Tok.isNot(tok::l_paren)) {
1391 PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << "(";
1392 return;
1393 }
1394
1395 PP.Lex(Tok);
1396 IdentifierInfo *II = Tok.getIdentifierInfo();
1397
1398 if (II && II->isStr("push")) {
1399 // #pragma execution_character_set( push[ , string ] )
1400 PP.Lex(Tok);
1401 if (Tok.is(tok::comma)) {
1402 PP.Lex(Tok);
1403
1404 std::string ExecCharset;
1405 if (!PP.FinishLexStringLiteral(Tok, ExecCharset,
1406 "pragma execution_character_set",
1407 /*AllowMacroExpansion=*/false))
1408 return;
1409
1410 // MSVC supports either of these, but nothing else.
1411 if (ExecCharset != "UTF-8" && ExecCharset != "utf-8") {
1412 PP.Diag(Tok, diag::warn_pragma_exec_charset_push_invalid) << ExecCharset;
1413 return;
1414 }
1415 }
1416 if (Callbacks)
1417 Callbacks->PragmaExecCharsetPush(DiagLoc, "UTF-8");
1418 } else if (II && II->isStr("pop")) {
1419 // #pragma execution_character_set( pop )
1420 PP.Lex(Tok);
1421 if (Callbacks)
1422 Callbacks->PragmaExecCharsetPop(DiagLoc);
1423 } else {
1424 PP.Diag(Tok, diag::warn_pragma_exec_charset_spec_invalid);
1425 return;
1426 }
1427
1428 if (Tok.isNot(tok::r_paren)) {
1429 PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << ")";
1430 return;
1431 }
1432
1433 PP.Lex(Tok);
1434 if (Tok.isNot(tok::eod))
1435 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma execution_character_set";
1436 }
1437};
1438
1439/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
1440struct PragmaIncludeAliasHandler : public PragmaHandler {
1441 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1442
1443 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1444 Token &IncludeAliasTok) override {
1445 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1446 }
1447};
1448
1449/// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1450/// extension. The syntax is:
1451/// \code
1452/// #pragma message(string)
1453/// \endcode
1454/// OR, in GCC mode:
1455/// \code
1456/// #pragma message string
1457/// \endcode
1458/// string is a string, which is fully macro expanded, and permits string
1459/// concatenation, embedded escape characters, etc... See MSDN for more details.
1460/// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1461/// form as \#pragma message.
1462struct PragmaMessageHandler : public PragmaHandler {
1463private:
1464 const PPCallbacks::PragmaMessageKind Kind;
1465 const StringRef Namespace;
1466
1467 static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1468 bool PragmaNameOnly = false) {
1469 switch (Kind) {
1470 case PPCallbacks::PMK_Message:
1471 return PragmaNameOnly ? "message" : "pragma message";
1472 case PPCallbacks::PMK_Warning:
1473 return PragmaNameOnly ? "warning" : "pragma warning";
1474 case PPCallbacks::PMK_Error:
1475 return PragmaNameOnly ? "error" : "pragma error";
1476 }
1477 llvm_unreachable("Unknown PragmaMessageKind!")::llvm::llvm_unreachable_internal("Unknown PragmaMessageKind!"
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/clang/lib/Lex/Pragma.cpp"
, 1477)
;
1478 }
1479
1480public:
1481 PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1482 StringRef Namespace = StringRef())
1483 : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind),
1484 Namespace(Namespace) {}
1485
1486 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1487 Token &Tok) override {
1488 SourceLocation MessageLoc = Tok.getLocation();
1489 PP.Lex(Tok);
1490 bool ExpectClosingParen = false;
1491 switch (Tok.getKind()) {
1492 case tok::l_paren:
1493 // We have a MSVC style pragma message.
1494 ExpectClosingParen = true;
1495 // Read the string.
1496 PP.Lex(Tok);
1497 break;
1498 case tok::string_literal:
1499 // We have a GCC style pragma message, and we just read the string.
1500 break;
1501 default:
1502 PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1503 return;
1504 }
1505
1506 std::string MessageString;
1507 if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1508 /*AllowMacroExpansion=*/true))
1509 return;
1510
1511 if (ExpectClosingParen) {
1512 if (Tok.isNot(tok::r_paren)) {
1513 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1514 return;
1515 }
1516 PP.Lex(Tok); // eat the r_paren.
1517 }
1518
1519 if (Tok.isNot(tok::eod)) {
1520 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1521 return;
1522 }
1523
1524 // Output the message.
1525 PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1526 ? diag::err_pragma_message
1527 : diag::warn_pragma_message) << MessageString;
1528
1529 // If the pragma is lexically sound, notify any interested PPCallbacks.
1530 if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1531 Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
1532 }
1533};
1534
1535/// Handle the clang \#pragma module import extension. The syntax is:
1536/// \code
1537/// #pragma clang module import some.module.name
1538/// \endcode
1539struct PragmaModuleImportHandler : public PragmaHandler {
1540 PragmaModuleImportHandler() : PragmaHandler("import") {}
1541
1542 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1543 Token &Tok) override {
1544 SourceLocation ImportLoc = Tok.getLocation();
1545
1546 // Read the module name.
1547 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1548 ModuleName;
1549 if (LexModuleName(PP, Tok, ModuleName))
1550 return;
1551
1552 if (Tok.isNot(tok::eod))
1553 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1554
1555 // If we have a non-empty module path, load the named module.
1556 Module *Imported =
1557 PP.getModuleLoader().loadModule(ImportLoc, ModuleName, Module::Hidden,
1558 /*IsInclusionDirective=*/false);
1559 if (!Imported)
1560 return;
1561
1562 PP.makeModuleVisible(Imported, ImportLoc);
1563 PP.EnterAnnotationToken(SourceRange(ImportLoc, ModuleName.back().second),
1564 tok::annot_module_include, Imported);
1565 if (auto *CB = PP.getPPCallbacks())
1566 CB->moduleImport(ImportLoc, ModuleName, Imported);
1567 }
1568};
1569
1570/// Handle the clang \#pragma module begin extension. The syntax is:
1571/// \code
1572/// #pragma clang module begin some.module.name
1573/// ...
1574/// #pragma clang module end
1575/// \endcode
1576struct PragmaModuleBeginHandler : public PragmaHandler {
1577 PragmaModuleBeginHandler() : PragmaHandler("begin") {}
1578
1579 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1580 Token &Tok) override {
1581 SourceLocation BeginLoc = Tok.getLocation();
1582
1583 // Read the module name.
1584 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1585 ModuleName;
1586 if (LexModuleName(PP, Tok, ModuleName))
1587 return;
1588
1589 if (Tok.isNot(tok::eod))
1590 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1591
1592 // We can only enter submodules of the current module.
1593 StringRef Current = PP.getLangOpts().CurrentModule;
1594 if (ModuleName.front().first->getName() != Current) {
1595 PP.Diag(ModuleName.front().second, diag::err_pp_module_begin_wrong_module)
1596 << ModuleName.front().first << (ModuleName.size() > 1)
1597 << Current.empty() << Current;
1598 return;
1599 }
1600
1601 // Find the module we're entering. We require that a module map for it
1602 // be loaded or implicitly loadable.
1603 auto &HSI = PP.getHeaderSearchInfo();
1604 Module *M = HSI.lookupModule(Current);
1605 if (!M) {
1606 PP.Diag(ModuleName.front().second,
1607 diag::err_pp_module_begin_no_module_map) << Current;
1608 return;
1609 }
1610 for (unsigned I = 1; I != ModuleName.size(); ++I) {
1611 auto *NewM = M->findOrInferSubmodule(ModuleName[I].first->getName());
1612 if (!NewM) {
1613 PP.Diag(ModuleName[I].second, diag::err_pp_module_begin_no_submodule)
1614 << M->getFullModuleName() << ModuleName[I].first;
1615 return;
1616 }
1617 M = NewM;
1618 }
1619
1620 // If the module isn't available, it doesn't make sense to enter it.
1621 if (Preprocessor::checkModuleIsAvailable(
1622 PP.getLangOpts(), PP.getTargetInfo(), PP.getDiagnostics(), M)) {
1623 PP.Diag(BeginLoc, diag::note_pp_module_begin_here)
1624 << M->getTopLevelModuleName();
1625 return;
1626 }
1627
1628 // Enter the scope of the submodule.
1629 PP.EnterSubmodule(M, BeginLoc, /*ForPragma*/true);
1630 PP.EnterAnnotationToken(SourceRange(BeginLoc, ModuleName.back().second),
1631 tok::annot_module_begin, M);
1632 }
1633};
1634
1635/// Handle the clang \#pragma module end extension.
1636struct PragmaModuleEndHandler : public PragmaHandler {
1637 PragmaModuleEndHandler() : PragmaHandler("end") {}
1638
1639 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1640 Token &Tok) override {
1641 SourceLocation Loc = Tok.getLocation();
1642
1643 PP.LexUnexpandedToken(Tok);
1644 if (Tok.isNot(tok::eod))
1645 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1646
1647 Module *M = PP.LeaveSubmodule(/*ForPragma*/true);
1648 if (M)
1649 PP.EnterAnnotationToken(SourceRange(Loc), tok::annot_module_end, M);
1650 else
1651 PP.Diag(Loc, diag::err_pp_module_end_without_module_begin);
1652 }
1653};
1654
1655/// Handle the clang \#pragma module build extension.
1656struct PragmaModuleBuildHandler : public PragmaHandler {
1657 PragmaModuleBuildHandler() : PragmaHandler("build") {}
1658
1659 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1660 Token &Tok) override {
1661 PP.HandlePragmaModuleBuild(Tok);
1662 }
1663};
1664
1665/// Handle the clang \#pragma module load extension.
1666struct PragmaModuleLoadHandler : public PragmaHandler {
1667 PragmaModuleLoadHandler() : PragmaHandler("load") {}
1668
1669 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1670 Token &Tok) override {
1671 SourceLocation Loc = Tok.getLocation();
1672
1673 // Read the module name.
1674 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1675 ModuleName;
1676 if (LexModuleName(PP, Tok, ModuleName))
1677 return;
1678
1679 if (Tok.isNot(tok::eod))
1680 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1681
1682 // Load the module, don't make it visible.
1683 PP.getModuleLoader().loadModule(Loc, ModuleName, Module::Hidden,
1684 /*IsInclusionDirective=*/false);
1685 }
1686};
1687
1688/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
1689/// macro on the top of the stack.
1690struct PragmaPushMacroHandler : public PragmaHandler {
1691 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
1692
1693 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1694 Token &PushMacroTok) override {
1695 PP.HandlePragmaPushMacro(PushMacroTok);
1696 }
1697};
1698
1699/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
1700/// macro to the value on the top of the stack.
1701struct PragmaPopMacroHandler : public PragmaHandler {
1702 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
1703
1704 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1705 Token &PopMacroTok) override {
1706 PP.HandlePragmaPopMacro(PopMacroTok);
1707 }
1708};
1709
1710/// PragmaARCCFCodeAuditedHandler -
1711/// \#pragma clang arc_cf_code_audited begin/end
1712struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1713 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1714
1715 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1716 Token &NameTok) override {
1717 SourceLocation Loc = NameTok.getLocation();
1718 bool IsBegin;
1719
1720 Token Tok;
1721
1722 // Lex the 'begin' or 'end'.
1723 PP.LexUnexpandedToken(Tok);
1724 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1725 if (BeginEnd && BeginEnd->isStr("begin")) {
1726 IsBegin = true;
1727 } else if (BeginEnd && BeginEnd->isStr("end")) {
1728 IsBegin = false;
1729 } else {
1730 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1731 return;
1732 }
1733
1734 // Verify that this is followed by EOD.
1735 PP.LexUnexpandedToken(Tok);
1736 if (Tok.isNot(tok::eod))
1737 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1738
1739 // The start location of the active audit.
1740 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedInfo().second;
1741
1742 // The start location we want after processing this.
1743 SourceLocation NewLoc;
1744
1745 if (IsBegin) {
1746 // Complain about attempts to re-enter an audit.
1747 if (BeginLoc.isValid()) {
1748 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1749 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1750 }
1751 NewLoc = Loc;
1752 } else {
1753 // Complain about attempts to leave an audit that doesn't exist.
1754 if (!BeginLoc.isValid()) {
1755 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1756 return;
1757 }
1758 NewLoc = SourceLocation();
1759 }
1760
1761 PP.setPragmaARCCFCodeAuditedInfo(NameTok.getIdentifierInfo(), NewLoc);
1762 }
1763};
1764
1765/// PragmaAssumeNonNullHandler -
1766/// \#pragma clang assume_nonnull begin/end
1767struct PragmaAssumeNonNullHandler : public PragmaHandler {
1768 PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {}
1769
1770 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1771 Token &NameTok) override {
1772 SourceLocation Loc = NameTok.getLocation();
1773 bool IsBegin;
1774
1775 Token Tok;
1776
1777 // Lex the 'begin' or 'end'.
1778 PP.LexUnexpandedToken(Tok);
1779 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1780 if (BeginEnd && BeginEnd->isStr("begin")) {
1781 IsBegin = true;
1782 } else if (BeginEnd && BeginEnd->isStr("end")) {
1783 IsBegin = false;
1784 } else {
1785 PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax);
1786 return;
1787 }
1788
1789 // Verify that this is followed by EOD.
1790 PP.LexUnexpandedToken(Tok);
1791 if (Tok.isNot(tok::eod))
1792 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1793
1794 // The start location of the active audit.
1795 SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc();
1796
1797 // The start location we want after processing this.
1798 SourceLocation NewLoc;
1799 PPCallbacks *Callbacks = PP.getPPCallbacks();
1800
1801 if (IsBegin) {
1802 // Complain about attempts to re-enter an audit.
1803 if (BeginLoc.isValid()) {
1804 PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull);
1805 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1806 }
1807 NewLoc = Loc;
1808 if (Callbacks)
1809 Callbacks->PragmaAssumeNonNullBegin(NewLoc);
1810 } else {
1811 // Complain about attempts to leave an audit that doesn't exist.
1812 if (!BeginLoc.isValid()) {
1813 PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull);
1814 return;
1815 }
1816 NewLoc = SourceLocation();
1817 if (Callbacks)
1818 Callbacks->PragmaAssumeNonNullEnd(NewLoc);
1819 }
1820
1821 PP.setPragmaAssumeNonNullLoc(NewLoc);
1822 }
1823};
1824
1825/// Handle "\#pragma region [...]"
1826///
1827/// The syntax is
1828/// \code
1829/// #pragma region [optional name]
1830/// #pragma endregion [optional comment]
1831/// \endcode
1832///
1833/// \note This is
1834/// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1835/// pragma, just skipped by compiler.
1836struct PragmaRegionHandler : public PragmaHandler {
1837 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {}
1838
1839 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1840 Token &NameTok) override {
1841 // #pragma region: endregion matches can be verified
1842 // __pragma(region): no sense, but ignored by msvc
1843 // _Pragma is not valid for MSVC, but there isn't any point
1844 // to handle a _Pragma differently.
1845 }
1846};
1847
1848} // namespace
1849
1850/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1851/// \#pragma GCC poison/system_header/dependency and \#pragma once.
1852void Preprocessor::RegisterBuiltinPragmas() {
1853 AddPragmaHandler(new PragmaOnceHandler());
1854 AddPragmaHandler(new PragmaMarkHandler());
1855 AddPragmaHandler(new PragmaPushMacroHandler());
1856 AddPragmaHandler(new PragmaPopMacroHandler());
1857 AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
1858
1859 // #pragma GCC ...
1860 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1861 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1862 AddPragmaHandler("GCC", new PragmaDependencyHandler());
1863 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
1864 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1865 "GCC"));
1866 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1867 "GCC"));
1868 // #pragma clang ...
1869 AddPragmaHandler("clang", new PragmaPoisonHandler());
1870 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
1871 AddPragmaHandler("clang", new PragmaDebugHandler());
1872 AddPragmaHandler("clang", new PragmaDependencyHandler());
1873 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
1874 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
1875 AddPragmaHandler("clang", new PragmaAssumeNonNullHandler());
1876
1877 // #pragma clang module ...
1878 auto *ModuleHandler = new PragmaNamespace("module");
1879 AddPragmaHandler("clang", ModuleHandler);
1880 ModuleHandler->AddPragma(new PragmaModuleImportHandler());
1881 ModuleHandler->AddPragma(new PragmaModuleBeginHandler());
1882 ModuleHandler->AddPragma(new PragmaModuleEndHandler());
1883 ModuleHandler->AddPragma(new PragmaModuleBuildHandler());
1884 ModuleHandler->AddPragma(new PragmaModuleLoadHandler());
1885
1886 // Add region pragmas.
1887 AddPragmaHandler(new PragmaRegionHandler("region"));
1888 AddPragmaHandler(new PragmaRegionHandler("endregion"));
1889
1890 // MS extensions.
1891 if (LangOpts.MicrosoftExt) {
1892 AddPragmaHandler(new PragmaWarningHandler());
1893 AddPragmaHandler(new PragmaExecCharsetHandler());
1894 AddPragmaHandler(new PragmaIncludeAliasHandler());
1895 AddPragmaHandler(new PragmaHdrstopHandler());
1896 }
1897
1898 // Pragmas added by plugins
1899 for (PragmaHandlerRegistry::iterator it = PragmaHandlerRegistry::begin(),
1900 ie = PragmaHandlerRegistry::end();
1901 it != ie; ++it) {
1902 AddPragmaHandler(it->instantiate().release());
1903 }
1904}
1905
1906/// Ignore all pragmas, useful for modes such as -Eonly which would otherwise
1907/// warn about those pragmas being unknown.
1908void Preprocessor::IgnorePragmas() {
1909 AddPragmaHandler(new EmptyPragmaHandler());
1910 // Also ignore all pragmas in all namespaces created
1911 // in Preprocessor::RegisterBuiltinPragmas().
1912 AddPragmaHandler("GCC", new EmptyPragmaHandler());
1913 AddPragmaHandler("clang", new EmptyPragmaHandler());
1914}

/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/clang/include/clang/Lex/Token.h"
, 197, __PRETTY_FUNCTION__))
;
198 assert(!PtrData)((!PtrData) ? static_cast<void> (0) : __assert_fail ("!PtrData"
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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 &&
9
Assuming the condition is false
10
Returning zero, which participates in a condition later
14
Assuming the condition is false
15
Returning zero, which participates in a condition later
19
Returning zero, which participates in a condition later
25
Returning zero, which participates in a condition later
29
Assuming the condition is true
30
Returning the value 1, which participates in a condition later
138 memcmp(getNameStart(), Str, StrLen-1) == 0;
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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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-10~+201911111502510600c19528f1809/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 /// Provide less than operator for lexicographical sorting.
388 bool operator<(const IdentifierInfo &RHS) const {
389 return getName() < RHS.getName();
390 }
391
392private:
393 /// The Preprocessor::HandleIdentifier does several special (but rare)
394 /// things to identifiers of various sorts. For example, it changes the
395 /// \c for keyword token from tok::identifier to tok::for.
396 ///
397 /// This method is very tied to the definition of HandleIdentifier. Any
398 /// change to it should be reflected here.
399 void RecomputeNeedsHandleIdentifier() {
400 NeedsHandleIdentifier = isPoisoned() || hasMacroDefinition() ||
401 isExtensionToken() || isFutureCompatKeyword() ||
402 isOutOfDate() || isModulesImport();
403 }
404};
405
406/// An RAII object for [un]poisoning an identifier within a scope.
407///
408/// \p II is allowed to be null, in which case objects of this type have
409/// no effect.
410class PoisonIdentifierRAIIObject {
411 IdentifierInfo *const II;
412 const bool OldValue;
413
414public:
415 PoisonIdentifierRAIIObject(IdentifierInfo *II, bool NewValue)
416 : II(II), OldValue(II ? II->isPoisoned() : false) {
417 if(II)
418 II->setIsPoisoned(NewValue);
419 }
420
421 ~PoisonIdentifierRAIIObject() {
422 if(II)
423 II->setIsPoisoned(OldValue);
424 }
425};
426
427/// An iterator that walks over all of the known identifiers
428/// in the lookup table.
429///
430/// Since this iterator uses an abstract interface via virtual
431/// functions, it uses an object-oriented interface rather than the
432/// more standard C++ STL iterator interface. In this OO-style
433/// iteration, the single function \c Next() provides dereference,
434/// advance, and end-of-sequence checking in a single
435/// operation. Subclasses of this iterator type will provide the
436/// actual functionality.
437class IdentifierIterator {
438protected:
439 IdentifierIterator() = default;
440
441public:
442 IdentifierIterator(const IdentifierIterator &) = delete;
443 IdentifierIterator &operator=(const IdentifierIterator &) = delete;
444
445 virtual ~IdentifierIterator();
446
447 /// Retrieve the next string in the identifier table and
448 /// advances the iterator for the following string.
449 ///
450 /// \returns The next string in the identifier table. If there is
451 /// no such string, returns an empty \c StringRef.
452 virtual StringRef Next() = 0;
453};
454
455/// Provides lookups to, and iteration over, IdentiferInfo objects.
456class IdentifierInfoLookup {
457public:
458 virtual ~IdentifierInfoLookup();
459
460 /// Return the IdentifierInfo for the specified named identifier.
461 ///
462 /// Unlike the version in IdentifierTable, this returns a pointer instead
463 /// of a reference. If the pointer is null then the IdentifierInfo cannot
464 /// be found.
465 virtual IdentifierInfo* get(StringRef Name) = 0;
466
467 /// Retrieve an iterator into the set of all identifiers
468 /// known to this identifier lookup source.
469 ///
470 /// This routine provides access to all of the identifiers known to
471 /// the identifier lookup, allowing access to the contents of the
472 /// identifiers without introducing the overhead of constructing
473 /// IdentifierInfo objects for each.
474 ///
475 /// \returns A new iterator into the set of known identifiers. The
476 /// caller is responsible for deleting this iterator.
477 virtual IdentifierIterator *getIdentifiers();
478};
479
480/// Implements an efficient mapping from strings to IdentifierInfo nodes.
481///
482/// This has no other purpose, but this is an extremely performance-critical
483/// piece of the code, as each occurrence of every identifier goes through
484/// here when lexed.
485class IdentifierTable {
486 // Shark shows that using MallocAllocator is *much* slower than using this
487 // BumpPtrAllocator!
488 using HashTableTy = llvm::StringMap<IdentifierInfo *, llvm::BumpPtrAllocator>;
489 HashTableTy HashTable;
490
491 IdentifierInfoLookup* ExternalLookup;
492
493public:
494 /// Create the identifier table.
495 explicit IdentifierTable(IdentifierInfoLookup *ExternalLookup = nullptr);
496
497 /// Create the identifier table, populating it with info about the
498 /// language keywords for the language specified by \p LangOpts.
499 explicit IdentifierTable(const LangOptions &LangOpts,
500 IdentifierInfoLookup *ExternalLookup = nullptr);
501
502 /// Set the external identifier lookup mechanism.
503 void setExternalIdentifierLookup(IdentifierInfoLookup *IILookup) {
504 ExternalLookup = IILookup;
505 }
506
507 /// Retrieve the external identifier lookup object, if any.
508 IdentifierInfoLookup *getExternalIdentifierLookup() const {
509 return ExternalLookup;
510 }
511
512 llvm::BumpPtrAllocator& getAllocator() {
513 return HashTable.getAllocator();
514 }
515
516 /// Return the identifier token info for the specified named
517 /// identifier.
518 IdentifierInfo &get(StringRef Name) {
519 auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
520
521 IdentifierInfo *&II = Entry.second;
522 if (II) return *II;
523
524 // No entry; if we have an external lookup, look there first.
525 if (ExternalLookup) {
526 II = ExternalLookup->get(Name);
527 if (II)
528 return *II;
529 }
530
531 // Lookups failed, make a new IdentifierInfo.
532 void *Mem = getAllocator().Allocate<IdentifierInfo>();
533 II = new (Mem) IdentifierInfo();
534
535 // Make sure getName() knows how to find the IdentifierInfo
536 // contents.
537 II->Entry = &Entry;
538
539 return *II;
540 }
541
542 IdentifierInfo &get(StringRef Name, tok::TokenKind TokenCode) {
543 IdentifierInfo &II = get(Name);
544 II.TokenID = TokenCode;
545 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-10~+201911111502510600c19528f1809/clang/include/clang/Basic/IdentifierTable.h"
, 545, __PRETTY_FUNCTION__))
;
546 return II;
547 }
548
549 /// Gets an IdentifierInfo for the given name without consulting
550 /// external sources.
551 ///
552 /// This is a version of get() meant for external sources that want to
553 /// introduce or modify an identifier. If they called get(), they would
554 /// likely end up in a recursion.
555 IdentifierInfo &getOwn(StringRef Name) {
556 auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
557
558 IdentifierInfo *&II = Entry.second;
559 if (II)
560 return *II;
561
562 // Lookups failed, make a new IdentifierInfo.
563 void *Mem = getAllocator().Allocate<IdentifierInfo>();
564 II = new (Mem) IdentifierInfo();
565
566 // Make sure getName() knows how to find the IdentifierInfo
567 // contents.
568 II->Entry = &Entry;
569
570 // If this is the 'import' contextual keyword, mark it as such.
571 if (Name.equals("import"))
572 II->setModulesImport(true);
573
574 return *II;
575 }
576
577 using iterator = HashTableTy::const_iterator;
578 using const_iterator = HashTableTy::const_iterator;
579
580 iterator begin() const { return HashTable.begin(); }
581 iterator end() const { return HashTable.end(); }
582 unsigned size() const { return HashTable.size(); }
583
584 iterator find(StringRef Name) const { return HashTable.find(Name); }
585
586 /// Print some statistics to stderr that indicate how well the
587 /// hashing is doing.
588 void PrintStats() const;
589
590 /// Populate the identifier table with info about the language keywords
591 /// for the language specified by \p LangOpts.
592 void AddKeywords(const LangOptions &LangOpts);
593};
594
595/// A family of Objective-C methods.
596///
597/// These families have no inherent meaning in the language, but are
598/// nonetheless central enough in the existing implementations to
599/// merit direct AST support. While, in theory, arbitrary methods can
600/// be considered to form families, we focus here on the methods
601/// involving allocation and retain-count management, as these are the
602/// most "core" and the most likely to be useful to diverse clients
603/// without extra information.
604///
605/// Both selectors and actual method declarations may be classified
606/// into families. Method families may impose additional restrictions
607/// beyond their selector name; for example, a method called '_init'
608/// that returns void is not considered to be in the 'init' family
609/// (but would be if it returned 'id'). It is also possible to
610/// explicitly change or remove a method's family. Therefore the
611/// method's family should be considered the single source of truth.
612enum ObjCMethodFamily {
613 /// No particular method family.
614 OMF_None,
615
616 // Selectors in these families may have arbitrary arity, may be
617 // written with arbitrary leading underscores, and may have
618 // additional CamelCase "words" in their first selector chunk
619 // following the family name.
620 OMF_alloc,
621 OMF_copy,
622 OMF_init,
623 OMF_mutableCopy,
624 OMF_new,
625
626 // These families are singletons consisting only of the nullary
627 // selector with the given name.
628 OMF_autorelease,
629 OMF_dealloc,
630 OMF_finalize,
631 OMF_release,
632 OMF_retain,
633 OMF_retainCount,
634 OMF_self,
635 OMF_initialize,
636
637 // performSelector families
638 OMF_performSelector
639};
640
641/// Enough bits to store any enumerator in ObjCMethodFamily or
642/// InvalidObjCMethodFamily.
643enum { ObjCMethodFamilyBitWidth = 4 };
644
645/// An invalid value of ObjCMethodFamily.
646enum { InvalidObjCMethodFamily = (1 << ObjCMethodFamilyBitWidth) - 1 };
647
648/// A family of Objective-C methods.
649///
650/// These are family of methods whose result type is initially 'id', but
651/// but are candidate for the result type to be changed to 'instancetype'.
652enum ObjCInstanceTypeFamily {
653 OIT_None,
654 OIT_Array,
655 OIT_Dictionary,
656 OIT_Singleton,
657 OIT_Init,
658 OIT_ReturnsSelf
659};
660
661enum ObjCStringFormatFamily {
662 SFF_None,
663 SFF_NSString,
664 SFF_CFString
665};
666
667/// Smart pointer class that efficiently represents Objective-C method
668/// names.
669///
670/// This class will either point to an IdentifierInfo or a
671/// MultiKeywordSelector (which is private). This enables us to optimize
672/// selectors that take no arguments and selectors that take 1 argument, which
673/// accounts for 78% of all selectors in Cocoa.h.
674class Selector {
675 friend class Diagnostic;
676 friend class SelectorTable; // only the SelectorTable can create these
677 friend class DeclarationName; // and the AST's DeclarationName.
678
679 enum IdentifierInfoFlag {
680 // Empty selector = 0. Note that these enumeration values must
681 // correspond to the enumeration values of DeclarationName::StoredNameKind
682 ZeroArg = 0x01,
683 OneArg = 0x02,
684 MultiArg = 0x07,
685 ArgFlags = 0x07
686 };
687
688 /// A pointer to the MultiKeywordSelector or IdentifierInfo. We use the low
689 /// three bits of InfoPtr to store an IdentifierInfoFlag. Note that in any
690 /// case IdentifierInfo and MultiKeywordSelector are already aligned to
691 /// 8 bytes even on 32 bits archs because of DeclarationName.
692 uintptr_t InfoPtr = 0;
693
694 Selector(IdentifierInfo *II, unsigned nArgs) {
695 InfoPtr = reinterpret_cast<uintptr_t>(II);
696 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-10~+201911111502510600c19528f1809/clang/include/clang/Basic/IdentifierTable.h"
, 696, __PRETTY_FUNCTION__))
;
697 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-10~+201911111502510600c19528f1809/clang/include/clang/Basic/IdentifierTable.h"
, 697, __PRETTY_FUNCTION__))
;
698 InfoPtr |= nArgs+1;
699 }
700
701 Selector(MultiKeywordSelector *SI) {
702 InfoPtr = reinterpret_cast<uintptr_t>(SI);
703 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-10~+201911111502510600c19528f1809/clang/include/clang/Basic/IdentifierTable.h"
, 703, __PRETTY_FUNCTION__))
;
704 InfoPtr |= MultiArg;
705 }
706
707 IdentifierInfo *getAsIdentifierInfo() const {
708 if (getIdentifierInfoFlag() < MultiArg)
709 return reinterpret_cast<IdentifierInfo *>(InfoPtr & ~ArgFlags);
710 return nullptr;
711 }
712
713 MultiKeywordSelector *getMultiKeywordSelector() const {
714 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr & ~ArgFlags);
715 }
716
717 unsigned getIdentifierInfoFlag() const {
718 return InfoPtr & ArgFlags;
719 }
720
721 static ObjCMethodFamily getMethodFamilyImpl(Selector sel);
722
723 static ObjCStringFormatFamily getStringFormatFamilyImpl(Selector sel);
724
725public:
726 /// The default ctor should only be used when creating data structures that
727 /// will contain selectors.
728 Selector() = default;
729 explicit Selector(uintptr_t V) : InfoPtr(V) {}
730
731 /// operator==/!= - Indicate whether the specified selectors are identical.
732 bool operator==(Selector RHS) const {
733 return InfoPtr == RHS.InfoPtr;
734 }
735 bool operator!=(Selector RHS) const {
736 return InfoPtr != RHS.InfoPtr;
737 }
738
739 void *getAsOpaquePtr() const {
740 return reinterpret_cast<void*>(InfoPtr);
741 }
742
743 /// Determine whether this is the empty selector.
744 bool isNull() const { return InfoPtr == 0; }
745
746 // Predicates to identify the selector type.
747 bool isKeywordSelector() const {
748 return getIdentifierInfoFlag() != ZeroArg;
749 }
750
751 bool isUnarySelector() const {
752 return getIdentifierInfoFlag() == ZeroArg;
753 }
754
755 /// If this selector is the specific keyword selector described by Names.
756 bool isKeywordSelector(ArrayRef<StringRef> Names) const;
757
758 /// If this selector is the specific unary selector described by Name.
759 bool isUnarySelector(StringRef Name) const;
760
761 unsigned getNumArgs() const;
762
763 /// Retrieve the identifier at a given position in the selector.
764 ///
765 /// Note that the identifier pointer returned may be NULL. Clients that only
766 /// care about the text of the identifier string, and not the specific,
767 /// uniqued identifier pointer, should use \c getNameForSlot(), which returns
768 /// an empty string when the identifier pointer would be NULL.
769 ///
770 /// \param argIndex The index for which we want to retrieve the identifier.
771 /// This index shall be less than \c getNumArgs() unless this is a keyword
772 /// selector, in which case 0 is the only permissible value.
773 ///
774 /// \returns the uniqued identifier for this slot, or NULL if this slot has
775 /// no corresponding identifier.
776 IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const;
777
778 /// Retrieve the name at a given position in the selector.
779 ///
780 /// \param argIndex The index for which we want to retrieve the name.
781 /// This index shall be less than \c getNumArgs() unless this is a keyword
782 /// selector, in which case 0 is the only permissible value.
783 ///
784 /// \returns the name for this slot, which may be the empty string if no
785 /// name was supplied.
786 StringRef getNameForSlot(unsigned argIndex) const;
787
788 /// Derive the full selector name (e.g. "foo:bar:") and return
789 /// it as an std::string.
790 std::string getAsString() const;
791
792 /// Prints the full selector name (e.g. "foo:bar:").
793 void print(llvm::raw_ostream &OS) const;
794
795 void dump() const;
796
797 /// Derive the conventional family of this method.
798 ObjCMethodFamily getMethodFamily() const {
799 return getMethodFamilyImpl(*this);
800 }
801
802 ObjCStringFormatFamily getStringFormatFamily() const {
803 return getStringFormatFamilyImpl(*this);
804 }
805
806 static Selector getEmptyMarker() {
807 return Selector(uintptr_t(-1));
808 }
809
810 static Selector getTombstoneMarker() {
811 return Selector(uintptr_t(-2));
812 }
813
814 static ObjCInstanceTypeFamily getInstTypeMethodFamily(Selector sel);
815};
816
817/// This table allows us to fully hide how we implement
818/// multi-keyword caching.
819class SelectorTable {
820 // Actually a SelectorTableImpl
821 void *Impl;
822
823public:
824 SelectorTable();
825 SelectorTable(const SelectorTable &) = delete;
826 SelectorTable &operator=(const SelectorTable &) = delete;
827 ~SelectorTable();
828
829 /// Can create any sort of selector.
830 ///
831 /// \p NumArgs indicates whether this is a no argument selector "foo", a
832 /// single argument selector "foo:" or multi-argument "foo:bar:".
833 Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV);
834
835 Selector getUnarySelector(IdentifierInfo *ID) {
836 return Selector(ID, 1);
837 }
838
839 Selector getNullarySelector(IdentifierInfo *ID) {
840 return Selector(ID, 0);
841 }
842
843 /// Return the total amount of memory allocated for managing selectors.
844 size_t getTotalMemory() const;
845
846 /// Return the default setter name for the given identifier.
847 ///
848 /// This is "set" + \p Name where the initial character of \p Name
849 /// has been capitalized.
850 static SmallString<64> constructSetterName(StringRef Name);
851
852 /// Return the default setter selector for the given identifier.
853 ///
854 /// This is "set" + \p Name where the initial character of \p Name
855 /// has been capitalized.
856 static Selector constructSetterSelector(IdentifierTable &Idents,
857 SelectorTable &SelTable,
858 const IdentifierInfo *Name);
859
860 /// Return the property name for the given setter selector.
861 static std::string getPropertyNameFromSetterSelector(Selector Sel);
862};
863
864namespace detail {
865
866/// DeclarationNameExtra is used as a base of various uncommon special names.
867/// This class is needed since DeclarationName has not enough space to store
868/// the kind of every possible names. Therefore the kind of common names is
869/// stored directly in DeclarationName, and the kind of uncommon names is
870/// stored in DeclarationNameExtra. It is aligned to 8 bytes because
871/// DeclarationName needs the lower 3 bits to store the kind of common names.
872/// DeclarationNameExtra is tightly coupled to DeclarationName and any change
873/// here is very likely to require changes in DeclarationName(Table).
874class alignas(IdentifierInfoAlignment) DeclarationNameExtra {
875 friend class clang::DeclarationName;
876 friend class clang::DeclarationNameTable;
877
878protected:
879 /// The kind of "extra" information stored in the DeclarationName. See
880 /// @c ExtraKindOrNumArgs for an explanation of how these enumerator values
881 /// are used. Note that DeclarationName depends on the numerical values
882 /// of the enumerators in this enum. See DeclarationName::StoredNameKind
883 /// for more info.
884 enum ExtraKind {
885 CXXDeductionGuideName,
886 CXXLiteralOperatorName,
887 CXXUsingDirective,
888 ObjCMultiArgSelector
889 };
890
891 /// ExtraKindOrNumArgs has one of the following meaning:
892 /// * The kind of an uncommon C++ special name. This DeclarationNameExtra
893 /// is in this case in fact either a CXXDeductionGuideNameExtra or
894 /// a CXXLiteralOperatorIdName.
895 ///
896 /// * It may be also name common to C++ using-directives (CXXUsingDirective),
897 ///
898 /// * Otherwise it is ObjCMultiArgSelector+NumArgs, where NumArgs is
899 /// the number of arguments in the Objective-C selector, in which
900 /// case the DeclarationNameExtra is also a MultiKeywordSelector.
901 unsigned ExtraKindOrNumArgs;
902
903 DeclarationNameExtra(ExtraKind Kind) : ExtraKindOrNumArgs(Kind) {}
904 DeclarationNameExtra(unsigned NumArgs)
905 : ExtraKindOrNumArgs(ObjCMultiArgSelector + NumArgs) {}
906
907 /// Return the corresponding ExtraKind.
908 ExtraKind getKind() const {
909 return static_cast<ExtraKind>(ExtraKindOrNumArgs >
910 (unsigned)ObjCMultiArgSelector
911 ? (unsigned)ObjCMultiArgSelector
912 : ExtraKindOrNumArgs);
913 }
914
915 /// Return the number of arguments in an ObjC selector. Only valid when this
916 /// is indeed an ObjCMultiArgSelector.
917 unsigned getNumArgs() const {
918 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-10~+201911111502510600c19528f1809/clang/include/clang/Basic/IdentifierTable.h"
, 919, __PRETTY_FUNCTION__))
919 "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-10~+201911111502510600c19528f1809/clang/include/clang/Basic/IdentifierTable.h"
, 919, __PRETTY_FUNCTION__))
;
920 return ExtraKindOrNumArgs - (unsigned)ObjCMultiArgSelector;
921 }
922};
923
924} // namespace detail
925
926} // namespace clang
927
928namespace llvm {
929
930/// Define DenseMapInfo so that Selectors can be used as keys in DenseMap and
931/// DenseSets.
932template <>
933struct DenseMapInfo<clang::Selector> {
934 static clang::Selector getEmptyKey() {
935 return clang::Selector::getEmptyMarker();
936 }
937
938 static clang::Selector getTombstoneKey() {
939 return clang::Selector::getTombstoneMarker();
940 }
941
942 static unsigned getHashValue(clang::Selector S);
943
944 static bool isEqual(clang::Selector LHS, clang::Selector RHS) {
945 return LHS == RHS;
946 }
947};
948
949template<>
950struct PointerLikeTypeTraits<clang::Selector> {
951 static const void *getAsVoidPointer(clang::Selector P) {
952 return P.getAsOpaquePtr();
953 }
954
955 static clang::Selector getFromVoidPointer(const void *P) {
956 return clang::Selector(reinterpret_cast<uintptr_t>(P));
957 }
958
959 enum { NumLowBitsAvailable = 0 };
960};
961
962// Provide PointerLikeTypeTraits for IdentifierInfo pointers, which
963// are not guaranteed to be 8-byte aligned.
964template<>
965struct PointerLikeTypeTraits<clang::IdentifierInfo*> {
966 static void *getAsVoidPointer(clang::IdentifierInfo* P) {
967 return P;
968 }
969
970 static clang::IdentifierInfo *getFromVoidPointer(void *P) {
971 return static_cast<clang::IdentifierInfo*>(P);
972 }
973
974 enum { NumLowBitsAvailable = 1 };
975};
976
977template<>
978struct PointerLikeTypeTraits<const clang::IdentifierInfo*> {
979 static const void *getAsVoidPointer(const clang::IdentifierInfo* P) {
980 return P;
981 }
982
983 static const clang::IdentifierInfo *getFromVoidPointer(const void *P) {
984 return static_cast<const clang::IdentifierInfo*>(P);
985 }
986
987 enum { NumLowBitsAvailable = 1 };
988};
989
990} // namespace llvm
991
992#endif // LLVM_CLANG_BASIC_IDENTIFIERTABLE_H