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