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