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