Bug Summary

File:build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/clang/lib/Lex/Pragma.cpp
Warning:line 1127, column 7
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

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