blob: 61e424d49618ee9e3a1019a08b7b95b794cb546b [file] [log] [blame]
Eugene Zelenkocb96ac62017-12-08 22:39:26 +00001//===- Pragma.cpp - Pragma registration and handling ----------------------===//
Chris Lattnerb8761832006-06-24 21:31:03 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattnerb8761832006-06-24 21:31:03 +00006//
7//===----------------------------------------------------------------------===//
8//
Chris Lattnerb694ba72006-07-02 22:41:36 +00009// This file implements the PragmaHandler/PragmaTable interfaces and implements
10// pragma related methods of the Preprocessor class.
Chris Lattnerb8761832006-06-24 21:31:03 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/Pragma.h"
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattnerb694ba72006-07-02 22:41:36 +000016#include "clang/Basic/FileManager.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000017#include "clang/Basic/IdentifierTable.h"
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000018#include "clang/Basic/LLVM.h"
19#include "clang/Basic/LangOptions.h"
20#include "clang/Basic/Module.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000021#include "clang/Basic/SourceLocation.h"
Chris Lattnerb694ba72006-07-02 22:41:36 +000022#include "clang/Basic/SourceManager.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000023#include "clang/Basic/TokenKinds.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Lex/HeaderSearch.h"
25#include "clang/Lex/LexDiagnostic.h"
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000026#include "clang/Lex/Lexer.h"
Richard Smith9565c75b2017-06-19 23:09:36 +000027#include "clang/Lex/LiteralSupport.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000028#include "clang/Lex/MacroInfo.h"
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000029#include "clang/Lex/ModuleLoader.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000030#include "clang/Lex/PPCallbacks.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000031#include "clang/Lex/Preprocessor.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000032#include "clang/Lex/PreprocessorLexer.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000033#include "clang/Lex/Token.h"
34#include "clang/Lex/TokenLexer.h"
35#include "llvm/ADT/ArrayRef.h"
36#include "llvm/ADT/DenseMap.h"
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000037#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000038#include "llvm/ADT/SmallString.h"
39#include "llvm/ADT/SmallVector.h"
Andrew V. Tischenkoc88deb12018-04-10 10:34:13 +000040#include "llvm/ADT/StringSwitch.h"
Nico Weberade321e2018-04-10 18:53:28 +000041#include "llvm/ADT/StringRef.h"
Andrew V. Tischenkoc88deb12018-04-10 10:34:13 +000042#include "llvm/Support/CrashRecoveryContext.h"
Nico Weberade321e2018-04-10 18:53:28 +000043#include "llvm/Support/Compiler.h"
Daniel Dunbarf2cf3292010-08-17 22:32:48 +000044#include "llvm/Support/ErrorHandling.h"
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000045#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000046#include <cassert>
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000047#include <cstddef>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000048#include <cstdint>
49#include <limits>
50#include <string>
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000051#include <utility>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000052#include <vector>
53
Chris Lattnerb8761832006-06-24 21:31:03 +000054using namespace clang;
55
56// Out-of-line destructor to provide a home for the class.
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000057PragmaHandler::~PragmaHandler() = default;
Chris Lattnerb8761832006-06-24 21:31:03 +000058
Chris Lattner2e155302006-07-03 05:34:41 +000059//===----------------------------------------------------------------------===//
Daniel Dunbard839e772010-06-11 20:10:12 +000060// EmptyPragmaHandler Implementation.
61//===----------------------------------------------------------------------===//
62
Hans Wennborg7357bbc2015-10-12 20:47:58 +000063EmptyPragmaHandler::EmptyPragmaHandler(StringRef Name) : PragmaHandler(Name) {}
Daniel Dunbard839e772010-06-11 20:10:12 +000064
Fangrui Song6907ce22018-07-30 19:24:48 +000065void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +000066 PragmaIntroducer Introducer,
Douglas Gregorc7d65762010-09-09 22:45:38 +000067 Token &FirstToken) {}
Daniel Dunbard839e772010-06-11 20:10:12 +000068
69//===----------------------------------------------------------------------===//
Chris Lattner2e155302006-07-03 05:34:41 +000070// PragmaNamespace Implementation.
71//===----------------------------------------------------------------------===//
72
Chris Lattner2e155302006-07-03 05:34:41 +000073PragmaNamespace::~PragmaNamespace() {
Reid Kleckner588c9372014-02-19 23:44:52 +000074 llvm::DeleteContainerSeconds(Handlers);
Chris Lattner2e155302006-07-03 05:34:41 +000075}
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.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000081PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
Chris Lattner2e155302006-07-03 05:34:41 +000082 bool IgnoreNull) const {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000083 if (PragmaHandler *Handler = Handlers.lookup(Name))
84 return Handler;
Craig Topperd2d442c2014-05-17 23:10:59 +000085 return IgnoreNull ? nullptr : Handlers.lookup(StringRef());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000086}
Mike Stump11289f42009-09-09 15:08:12 +000087
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000088void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
89 assert(!Handlers.lookup(Handler->getName()) &&
90 "A handler with this name is already registered in this namespace");
David Blaikie13156b62014-11-19 03:06:06 +000091 Handlers[Handler->getName()] = Handler;
Chris Lattner2e155302006-07-03 05:34:41 +000092}
93
Daniel Dunbar40596532008-10-04 19:17:46 +000094void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000095 assert(Handlers.lookup(Handler->getName()) &&
96 "Handler not registered in this namespace");
97 Handlers.erase(Handler->getName());
Daniel Dunbar40596532008-10-04 19:17:46 +000098}
99
Fangrui Song6907ce22018-07-30 19:24:48 +0000100void PragmaNamespace::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000101 PragmaIntroducer Introducer, Token &Tok) {
Chris Lattnerb8761832006-06-24 21:31:03 +0000102 // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
103 // expand it, the user can have a STDC #define, that should not affect this.
104 PP.LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000105
Chris Lattnerb8761832006-06-24 21:31:03 +0000106 // Get the handler for this token. If there is no handler, ignore the pragma.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000107 PragmaHandler *Handler
108 = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000109 : StringRef(),
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000110 /*IgnoreNull=*/false);
Craig Topperd2d442c2014-05-17 23:10:59 +0000111 if (!Handler) {
Chris Lattner21656f22009-04-19 21:10:26 +0000112 PP.Diag(Tok, diag::warn_pragma_ignored);
113 return;
114 }
Mike Stump11289f42009-09-09 15:08:12 +0000115
Chris Lattnerb8761832006-06-24 21:31:03 +0000116 // Otherwise, pass it down.
Douglas Gregorc7d65762010-09-09 22:45:38 +0000117 Handler->HandlePragma(PP, Introducer, Tok);
Chris Lattnerb8761832006-06-24 21:31:03 +0000118}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000119
Chris Lattnerb694ba72006-07-02 22:41:36 +0000120//===----------------------------------------------------------------------===//
121// Preprocessor Pragma Directive Handling.
122//===----------------------------------------------------------------------===//
123
James Dennett18a6d792012-06-17 03:26:26 +0000124/// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the
Chris Lattnerb694ba72006-07-02 22:41:36 +0000125/// rest of the pragma, passing it to the registered pragma handlers.
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000126void Preprocessor::HandlePragmaDirective(PragmaIntroducer Introducer) {
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000127 if (Callbacks)
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000128 Callbacks->PragmaDirective(Introducer.Loc, Introducer.Kind);
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000129
Jordan Rosede1a2922012-06-08 18:06:21 +0000130 if (!PragmasEnabled)
131 return;
132
Chris Lattnerb694ba72006-07-02 22:41:36 +0000133 ++NumPragma;
Mike Stump11289f42009-09-09 15:08:12 +0000134
Chris Lattnerb694ba72006-07-02 22:41:36 +0000135 // Invoke the first level of pragma handlers which reads the namespace id.
Chris Lattner146762e2007-07-20 16:59:19 +0000136 Token Tok;
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000137 PragmaHandlers->HandlePragma(*this, Introducer, Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000138
Chris Lattnerb694ba72006-07-02 22:41:36 +0000139 // If the pragma handler didn't read the rest of the line, consume it now.
Fangrui Song6907ce22018-07-30 19:24:48 +0000140 if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000141 || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000142 DiscardUntilEndOfDirective();
143}
144
145/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
146/// return the first token after the directive. The _Pragma token has just
147/// been read into 'Tok'.
Chris Lattner146762e2007-07-20 16:59:19 +0000148void Preprocessor::Handle_Pragma(Token &Tok) {
Richard Smith75f96812019-04-11 21:18:22 +0000149 // C11 6.10.3.4/3:
150 // all pragma unary operator expressions within [a completely
151 // macro-replaced preprocessing token sequence] are [...] processed [after
152 // rescanning is complete]
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000153 //
Richard Smith75f96812019-04-11 21:18:22 +0000154 // This means that we execute _Pragma operators in two cases:
155 //
156 // 1) on token sequences that would otherwise be produced as the output of
157 // phase 4 of preprocessing, and
158 // 2) on token sequences formed as the macro-replaced token sequence of a
159 // macro argument
160 //
161 // Case #2 appears to be a wording bug: only _Pragmas that would survive to
162 // the end of phase 4 should actually be executed. Discussion on the WG14
163 // mailing list suggests that a _Pragma operator is notionally checked early,
164 // but only pragmas that survive to the end of phase 4 should be executed.
165 //
166 // In Case #2, we check the syntax now, but then put the tokens back into the
167 // token stream for later consumption.
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000168
Richard Smith75f96812019-04-11 21:18:22 +0000169 struct TokenCollector {
170 Preprocessor &Self;
171 bool Collect;
172 SmallVector<Token, 3> Tokens;
173 Token &Tok;
174
175 void lex() {
176 if (Collect)
177 Tokens.push_back(Tok);
178 Self.Lex(Tok);
179 }
180
181 void revert() {
182 assert(Collect && "did not collect tokens");
183 assert(!Tokens.empty() && "collected unexpected number of tokens");
184
185 // Push the ( "string" ) tokens into the token stream.
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +0000186 auto Toks = std::make_unique<Token[]>(Tokens.size());
Richard Smith75f96812019-04-11 21:18:22 +0000187 std::copy(Tokens.begin() + 1, Tokens.end(), Toks.get());
188 Toks[Tokens.size() - 1] = Tok;
189 Self.EnterTokenStream(std::move(Toks), Tokens.size(),
Ilya Biryukov929af672019-05-17 09:32:05 +0000190 /*DisableMacroExpansion*/ true,
191 /*IsReinject*/ true);
Richard Smith75f96812019-04-11 21:18:22 +0000192
193 // ... and return the _Pragma token unchanged.
194 Tok = *Tokens.begin();
195 }
196 };
197
198 TokenCollector Toks = {*this, InMacroArgPreExpansion, {}, Tok};
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000199
Chris Lattnerb694ba72006-07-02 22:41:36 +0000200 // Remember the pragma token location.
201 SourceLocation PragmaLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000202
Chris Lattnerb694ba72006-07-02 22:41:36 +0000203 // Read the '('.
Richard Smith75f96812019-04-11 21:18:22 +0000204 Toks.lex();
Chris Lattner907dfe92008-11-18 07:59:24 +0000205 if (Tok.isNot(tok::l_paren)) {
206 Diag(PragmaLoc, diag::err__Pragma_malformed);
Richard Smith75f96812019-04-11 21:18:22 +0000207 return;
Chris Lattner907dfe92008-11-18 07:59:24 +0000208 }
Chris Lattnerb694ba72006-07-02 22:41:36 +0000209
210 // Read the '"..."'.
Richard Smith75f96812019-04-11 21:18:22 +0000211 Toks.lex();
Richard Smithc98bb4e2013-03-09 23:30:15 +0000212 if (!tok::isStringLiteral(Tok.getKind())) {
Chris Lattner907dfe92008-11-18 07:59:24 +0000213 Diag(PragmaLoc, diag::err__Pragma_malformed);
Hubert Tong0deb6942015-07-30 21:30:00 +0000214 // Skip bad tokens, and the ')', if present.
Reid Kleckner53e6a5d2014-08-14 19:47:06 +0000215 if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof))
Richard Smithd67aea22012-03-06 03:21:47 +0000216 Lex(Tok);
Hubert Tong0deb6942015-07-30 21:30:00 +0000217 while (Tok.isNot(tok::r_paren) &&
218 !Tok.isAtStartOfLine() &&
219 Tok.isNot(tok::eof))
220 Lex(Tok);
Richard Smithd67aea22012-03-06 03:21:47 +0000221 if (Tok.is(tok::r_paren))
222 Lex(Tok);
Richard Smith75f96812019-04-11 21:18:22 +0000223 return;
Richard Smithd67aea22012-03-06 03:21:47 +0000224 }
225
226 if (Tok.hasUDSuffix()) {
227 Diag(Tok, diag::err_invalid_string_udl);
228 // Skip this token, and the ')', if present.
229 Lex(Tok);
230 if (Tok.is(tok::r_paren))
231 Lex(Tok);
Richard Smith75f96812019-04-11 21:18:22 +0000232 return;
Chris Lattner907dfe92008-11-18 07:59:24 +0000233 }
Mike Stump11289f42009-09-09 15:08:12 +0000234
Chris Lattnerb694ba72006-07-02 22:41:36 +0000235 // Remember the string.
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000236 Token StrTok = Tok;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000237
238 // Read the ')'.
Richard Smith75f96812019-04-11 21:18:22 +0000239 Toks.lex();
Chris Lattner907dfe92008-11-18 07:59:24 +0000240 if (Tok.isNot(tok::r_paren)) {
241 Diag(PragmaLoc, diag::err__Pragma_malformed);
Richard Smith75f96812019-04-11 21:18:22 +0000242 return;
Chris Lattner907dfe92008-11-18 07:59:24 +0000243 }
Mike Stump11289f42009-09-09 15:08:12 +0000244
Richard Smith75f96812019-04-11 21:18:22 +0000245 // If we're expanding a macro argument, put the tokens back.
246 if (InMacroArgPreExpansion) {
247 Toks.revert();
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000248 return;
Richard Smith75f96812019-04-11 21:18:22 +0000249 }
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000250
Chris Lattner9dc9c202009-02-15 20:52:18 +0000251 SourceLocation RParenLoc = Tok.getLocation();
Argyrios Kyrtzidisf1b64c62012-04-03 16:47:40 +0000252 std::string StrVal = getSpelling(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +0000253
Richard Smithc98bb4e2013-03-09 23:30:15 +0000254 // The _Pragma is lexically sound. Destringize according to C11 6.10.9.1:
255 // "The string literal is destringized by deleting any encoding prefix,
Chris Lattner262d4e32009-01-16 18:59:23 +0000256 // deleting the leading and trailing double-quotes, replacing each escape
257 // sequence \" by a double-quote, and replacing each escape sequence \\ by a
258 // single backslash."
Richard Smithc98bb4e2013-03-09 23:30:15 +0000259 if (StrVal[0] == 'L' || StrVal[0] == 'U' ||
260 (StrVal[0] == 'u' && StrVal[1] != '8'))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000261 StrVal.erase(StrVal.begin());
Richard Smithc98bb4e2013-03-09 23:30:15 +0000262 else if (StrVal[0] == 'u')
263 StrVal.erase(StrVal.begin(), StrVal.begin() + 2);
264
265 if (StrVal[0] == 'R') {
266 // FIXME: C++11 does not specify how to handle raw-string-literals here.
267 // We strip off the 'R', the quotes, the d-char-sequences, and the parens.
268 assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' &&
269 "Invalid raw string token!");
270
271 // Measure the length of the d-char-sequence.
272 unsigned NumDChars = 0;
273 while (StrVal[2 + NumDChars] != '(') {
274 assert(NumDChars < (StrVal.size() - 5) / 2 &&
275 "Invalid raw string token!");
276 ++NumDChars;
277 }
278 assert(StrVal[StrVal.size() - 2 - NumDChars] == ')');
279
280 // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the
281 // parens below.
282 StrVal.erase(0, 2 + NumDChars);
283 StrVal.erase(StrVal.size() - 1 - NumDChars);
284 } else {
285 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
286 "Invalid string token!");
287
288 // Remove escaped quotes and escapes.
Benjamin Kramerc2f5f292013-05-04 10:37:20 +0000289 unsigned ResultPos = 1;
Erik Verbruggene4fd6522016-10-26 13:06:13 +0000290 for (size_t i = 1, e = StrVal.size() - 1; i != e; ++i) {
Reid Kleckner95e036c2013-09-25 16:42:48 +0000291 // Skip escapes. \\ -> '\' and \" -> '"'.
292 if (StrVal[i] == '\\' && i + 1 < e &&
293 (StrVal[i + 1] == '\\' || StrVal[i + 1] == '"'))
294 ++i;
295 StrVal[ResultPos++] = StrVal[i];
Richard Smithc98bb4e2013-03-09 23:30:15 +0000296 }
Reid Kleckner95e036c2013-09-25 16:42:48 +0000297 StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1);
Richard Smithc98bb4e2013-03-09 23:30:15 +0000298 }
Mike Stump11289f42009-09-09 15:08:12 +0000299
Chris Lattnerb694ba72006-07-02 22:41:36 +0000300 // Remove the front quote, replacing it with a space, so that the pragma
301 // contents appear to have a space before them.
302 StrVal[0] = ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000303
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000304 // Replace the terminating quote with a \n.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000305 StrVal[StrVal.size()-1] = '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000306
Peter Collingbournef29ce972011-02-22 13:49:06 +0000307 // Plop the string (including the newline and trailing null) into a buffer
308 // where we can lex it.
309 Token TmpTok;
310 TmpTok.startToken();
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000311 CreateString(StrVal, TmpTok);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000312 SourceLocation TokLoc = TmpTok.getLocation();
313
314 // Make and enter a lexer object so that we lex and expand the tokens just
315 // like any others.
316 Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
317 StrVal.size(), *this);
318
Craig Topperd2d442c2014-05-17 23:10:59 +0000319 EnterSourceFileWithLexer(TL, nullptr);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000320
321 // With everything set up, lex this as a #pragma directive.
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000322 HandlePragmaDirective({PIK__Pragma, PragmaLoc});
John McCall89e925d2010-08-28 22:34:47 +0000323
324 // Finally, return whatever came after the pragma directive.
325 return Lex(Tok);
326}
327
328/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
329/// is not enclosed within a string literal.
330void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
331 // Remember the pragma token location.
332 SourceLocation PragmaLoc = Tok.getLocation();
333
334 // Read the '('.
335 Lex(Tok);
336 if (Tok.isNot(tok::l_paren)) {
337 Diag(PragmaLoc, diag::err__Pragma_malformed);
338 return;
339 }
340
Peter Collingbournef29ce972011-02-22 13:49:06 +0000341 // Get the tokens enclosed within the __pragma(), as well as the final ')'.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000342 SmallVector<Token, 32> PragmaToks;
John McCall89e925d2010-08-28 22:34:47 +0000343 int NumParens = 0;
344 Lex(Tok);
345 while (Tok.isNot(tok::eof)) {
Peter Collingbournef29ce972011-02-22 13:49:06 +0000346 PragmaToks.push_back(Tok);
John McCall89e925d2010-08-28 22:34:47 +0000347 if (Tok.is(tok::l_paren))
348 NumParens++;
349 else if (Tok.is(tok::r_paren) && NumParens-- == 0)
350 break;
John McCall89e925d2010-08-28 22:34:47 +0000351 Lex(Tok);
352 }
353
John McCall49039d42010-08-29 01:09:54 +0000354 if (Tok.is(tok::eof)) {
355 Diag(PragmaLoc, diag::err_unterminated___pragma);
356 return;
357 }
358
Peter Collingbournef29ce972011-02-22 13:49:06 +0000359 PragmaToks.front().setFlag(Token::LeadingSpace);
John McCall89e925d2010-08-28 22:34:47 +0000360
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000361 // Replace the ')' with an EOD to mark the end of the pragma.
362 PragmaToks.back().setKind(tok::eod);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000363
364 Token *TokArray = new Token[PragmaToks.size()];
365 std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
366
367 // Push the tokens onto the stack.
Ilya Biryukov929af672019-05-17 09:32:05 +0000368 EnterTokenStream(TokArray, PragmaToks.size(), true, true,
369 /*IsReinject*/ false);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000370
371 // With everything set up, lex this as a #pragma directive.
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000372 HandlePragmaDirective({PIK___pragma, PragmaLoc});
John McCall89e925d2010-08-28 22:34:47 +0000373
374 // Finally, return whatever came after the pragma directive.
375 return Lex(Tok);
376}
377
James Dennett18a6d792012-06-17 03:26:26 +0000378/// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'.
Chris Lattner146762e2007-07-20 16:59:19 +0000379void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
Sunil Srivastavafe583272016-07-25 17:17:06 +0000380 // Don't honor the 'once' when handling the primary source file, unless
Erik Verbruggene0bde752016-10-27 14:17:10 +0000381 // this is a prefix to a TU, which indicates we're generating a PCH file, or
382 // when the main file is a header (e.g. when -xc-header is provided on the
383 // commandline).
384 if (isInPrimaryFile() && TUKind != TU_Prefix && !getLangOpts().IsHeaderFile) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000385 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
386 return;
387 }
Mike Stump11289f42009-09-09 15:08:12 +0000388
Chris Lattnerb694ba72006-07-02 22:41:36 +0000389 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000390 // Mark the file as a once-only file now.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000391 HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
Chris Lattnerb694ba72006-07-02 22:41:36 +0000392}
393
Chris Lattnerc2383312007-12-19 19:38:36 +0000394void Preprocessor::HandlePragmaMark() {
Ted Kremenek76c34412008-11-19 22:21:33 +0000395 assert(CurPPLexer && "No current lexer?");
Erich Keane0a6b5b62018-12-04 14:34:09 +0000396 CurLexer->ReadToEndOfLine();
Chris Lattnerc2383312007-12-19 19:38:36 +0000397}
398
James Dennett18a6d792012-06-17 03:26:26 +0000399/// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
Erik Verbruggen851ce0e2016-10-26 11:46:10 +0000400void Preprocessor::HandlePragmaPoison() {
Chris Lattner146762e2007-07-20 16:59:19 +0000401 Token Tok;
Chris Lattner538d7f32006-07-20 04:31:52 +0000402
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000403 while (true) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000404 // Read the next token to poison. While doing this, pretend that we are
405 // skipping while reading the identifier to poison.
406 // This avoids errors on code like:
407 // #pragma GCC poison X
408 // #pragma GCC poison X
Ted Kremenek551c82a2008-11-18 01:12:54 +0000409 if (CurPPLexer) CurPPLexer->LexingRawMode = true;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000410 LexUnexpandedToken(Tok);
Ted Kremenek551c82a2008-11-18 01:12:54 +0000411 if (CurPPLexer) CurPPLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000412
Chris Lattnerb694ba72006-07-02 22:41:36 +0000413 // If we reached the end of line, we're done.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000414 if (Tok.is(tok::eod)) return;
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattnerb694ba72006-07-02 22:41:36 +0000416 // Can only poison identifiers.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000417 if (Tok.isNot(tok::raw_identifier)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000418 Diag(Tok, diag::err_pp_invalid_poison);
419 return;
420 }
Mike Stump11289f42009-09-09 15:08:12 +0000421
Chris Lattnercefc7682006-07-08 08:28:12 +0000422 // Look up the identifier info for the token. We disabled identifier lookup
423 // by saying we're skipping contents, so we need to do this manually.
424 IdentifierInfo *II = LookUpIdentifierInfo(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000425
Chris Lattnerb694ba72006-07-02 22:41:36 +0000426 // Already poisoned.
427 if (II->isPoisoned()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000428
Chris Lattnerb694ba72006-07-02 22:41:36 +0000429 // If this is a macro identifier, emit a warning.
Richard Smith20e883e2015-04-29 23:20:19 +0000430 if (isMacroDefined(II))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000431 Diag(Tok, diag::pp_poisoning_existing_macro);
Mike Stump11289f42009-09-09 15:08:12 +0000432
Chris Lattnerb694ba72006-07-02 22:41:36 +0000433 // Finally, poison it!
434 II->setIsPoisoned();
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000435 if (II->isFromAST())
436 II->setChangedSinceDeserialization();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000437 }
438}
439
James Dennett18a6d792012-06-17 03:26:26 +0000440/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
Chris Lattnerb694ba72006-07-02 22:41:36 +0000441/// that the whole directive has been parsed.
Chris Lattner146762e2007-07-20 16:59:19 +0000442void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000443 if (isInPrimaryFile()) {
444 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
445 return;
446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Chris Lattnerb694ba72006-07-02 22:41:36 +0000448 // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
Ted Kremenek300590b2008-11-20 01:45:11 +0000449 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000450
Chris Lattnerb694ba72006-07-02 22:41:36 +0000451 // Mark the file as a system header.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000452 HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000453
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000454 PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000455 if (PLoc.isInvalid())
456 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000457
Jay Foad9a6b0982011-06-21 15:13:30 +0000458 unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
Mike Stump11289f42009-09-09 15:08:12 +0000459
Chris Lattner3bdc7672011-05-22 22:10:16 +0000460 // Notify the client, if desired, that we are in a new source file.
461 if (Callbacks)
462 Callbacks->FileChanged(SysHeaderTok.getLocation(),
463 PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
464
Chris Lattnerd9efb6e2009-06-15 05:02:34 +0000465 // Emit a line marker. This will change any source locations from this point
466 // forward to realize they are in a system header.
467 // Create a line note with this information.
Reid Klecknereb00ee02017-05-22 21:42:58 +0000468 SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine() + 1,
Jordan Rose111c4a62013-04-17 19:09:18 +0000469 FilenameID, /*IsEntry=*/false, /*IsExit=*/false,
Reid Klecknereb00ee02017-05-22 21:42:58 +0000470 SrcMgr::C_System);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000471}
472
James Dennett18a6d792012-06-17 03:26:26 +0000473/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
Chris Lattner146762e2007-07-20 16:59:19 +0000474void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
475 Token FilenameTok;
Richard Smithb9b05102019-03-19 01:51:19 +0000476 if (LexHeaderName(FilenameTok, /*AllowConcatenation*/false))
Chris Lattnerb694ba72006-07-02 22:41:36 +0000477 return;
Mike Stump11289f42009-09-09 15:08:12 +0000478
Richard Smithb9b05102019-03-19 01:51:19 +0000479 // If the next token wasn't a header-name, diagnose the error.
Richard Smith91e150d2019-03-19 22:09:55 +0000480 if (FilenameTok.isNot(tok::header_name)) {
Richard Smithb9b05102019-03-19 01:51:19 +0000481 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
482 return;
483 }
484
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000485 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000486 SmallString<128> FilenameBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000487 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000488 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000489 if (Invalid)
490 return;
Mike Stump11289f42009-09-09 15:08:12 +0000491
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000492 bool isAngled =
493 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000494 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
495 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000496 if (Filename.empty())
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000497 return;
Mike Stump11289f42009-09-09 15:08:12 +0000498
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000499 // Search include directories for this file.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000500 const DirectoryLookup *CurDir;
Alex Lorenz4dc55732019-08-22 18:15:50 +0000501 Optional<FileEntryRef> File =
Richard Smith25d50752014-10-20 00:15:49 +0000502 LookupFile(FilenameTok.getLocation(), Filename, isAngled, nullptr,
Volodymyr Sapsai421380a2019-02-05 22:34:55 +0000503 nullptr, CurDir, nullptr, nullptr, nullptr, nullptr, nullptr);
Craig Topperd2d442c2014-05-17 23:10:59 +0000504 if (!File) {
Eli Friedman3781a362011-08-30 23:07:51 +0000505 if (!SuppressIncludeNotFoundError)
506 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
Chris Lattner97b8e842008-11-18 08:02:48 +0000507 return;
508 }
Mike Stump11289f42009-09-09 15:08:12 +0000509
Chris Lattnerd32480d2009-01-17 06:22:33 +0000510 const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000511
512 // If this file is older than the file it depends on, emit a diagnostic.
513 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
514 // Lex tokens at the end of the message and include them in the message.
515 std::string Message;
516 Lex(DependencyTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000517 while (DependencyTok.isNot(tok::eod)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000518 Message += getSpelling(DependencyTok) + " ";
519 Lex(DependencyTok);
520 }
Mike Stump11289f42009-09-09 15:08:12 +0000521
Chris Lattnerf0b04972010-09-05 23:16:09 +0000522 // Remove the trailing ' ' if present.
523 if (!Message.empty())
524 Message.erase(Message.end()-1);
Chris Lattner97b8e842008-11-18 08:02:48 +0000525 Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
Chris Lattnerb694ba72006-07-02 22:41:36 +0000526 }
527}
528
Reid Kleckner002562a2013-05-06 21:02:12 +0000529/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000530/// Return the IdentifierInfo* associated with the macro to push or pop.
531IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
532 // Remember the pragma token location.
533 Token PragmaTok = Tok;
534
535 // Read the '('.
536 Lex(Tok);
537 if (Tok.isNot(tok::l_paren)) {
538 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
539 << getSpelling(PragmaTok);
Craig Topperd2d442c2014-05-17 23:10:59 +0000540 return nullptr;
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000541 }
542
543 // Read the macro name string.
544 Lex(Tok);
545 if (Tok.isNot(tok::string_literal)) {
546 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
547 << getSpelling(PragmaTok);
Craig Topperd2d442c2014-05-17 23:10:59 +0000548 return nullptr;
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000549 }
550
Richard Smithd67aea22012-03-06 03:21:47 +0000551 if (Tok.hasUDSuffix()) {
552 Diag(Tok, diag::err_invalid_string_udl);
Craig Topperd2d442c2014-05-17 23:10:59 +0000553 return nullptr;
Richard Smithd67aea22012-03-06 03:21:47 +0000554 }
555
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000556 // Remember the macro string.
557 std::string StrVal = getSpelling(Tok);
558
559 // Read the ')'.
560 Lex(Tok);
561 if (Tok.isNot(tok::r_paren)) {
562 Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
563 << getSpelling(PragmaTok);
Craig Topperd2d442c2014-05-17 23:10:59 +0000564 return nullptr;
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000565 }
566
567 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
568 "Invalid string token!");
569
570 // Create a Token from the string.
571 Token MacroTok;
572 MacroTok.startToken();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000573 MacroTok.setKind(tok::raw_identifier);
Dmitri Gribenkob8e9e752012-09-24 21:07:17 +0000574 CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000575
576 // Get the IdentifierInfo of MacroToPushTok.
577 return LookUpIdentifierInfo(MacroTok);
578}
579
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000580/// Handle \#pragma push_macro.
James Dennett18a6d792012-06-17 03:26:26 +0000581///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000582/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000583/// \code
Dmitri Gribenko9ebd1612012-11-30 20:04:39 +0000584/// #pragma push_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000585/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000586void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
587 // Parse the pragma directive and get the macro IdentifierInfo*.
588 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
589 if (!IdentInfo) return;
590
591 // Get the MacroInfo associated with IdentInfo.
592 MacroInfo *MI = getMacroInfo(IdentInfo);
Fangrui Song6907ce22018-07-30 19:24:48 +0000593
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000594 if (MI) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000595 // Allow the original MacroInfo to be redefined later.
596 MI->setIsAllowRedefinitionsWithoutWarning(true);
597 }
598
599 // Push the cloned MacroInfo so we can retrieve it later.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000600 PragmaPushMacroInfo[IdentInfo].push_back(MI);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000601}
602
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000603/// Handle \#pragma pop_macro.
James Dennett18a6d792012-06-17 03:26:26 +0000604///
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000605/// The syntax is:
James Dennett18a6d792012-06-17 03:26:26 +0000606/// \code
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000607/// #pragma pop_macro("macro")
James Dennett18a6d792012-06-17 03:26:26 +0000608/// \endcode
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000609void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
610 SourceLocation MessageLoc = PopMacroTok.getLocation();
611
612 // Parse the pragma directive and get the macro IdentifierInfo*.
613 IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
614 if (!IdentInfo) return;
615
616 // Find the vector<MacroInfo*> associated with the macro.
Eugene Zelenkocb96ac62017-12-08 22:39:26 +0000617 llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>>::iterator iter =
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000618 PragmaPushMacroInfo.find(IdentInfo);
619 if (iter != PragmaPushMacroInfo.end()) {
Alexander Kornienko8b3f6232012-08-29 00:20:03 +0000620 // Forget the MacroInfo currently associated with IdentInfo.
Richard Smith20e883e2015-04-29 23:20:19 +0000621 if (MacroInfo *MI = getMacroInfo(IdentInfo)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000622 if (MI->isWarnIfUnused())
623 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
624 appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000625 }
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000626
627 // Get the MacroInfo we want to reinstall.
628 MacroInfo *MacroToReInstall = iter->second.back();
629
Richard Smith713369b2015-04-23 20:40:50 +0000630 if (MacroToReInstall)
Alexander Kornienkoc0b49282012-08-29 16:56:24 +0000631 // Reinstall the previously pushed macro.
Richard Smith713369b2015-04-23 20:40:50 +0000632 appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc);
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000633
634 // Pop PragmaPushMacroInfo stack.
635 iter->second.pop_back();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000636 if (iter->second.empty())
Chris Lattnerc0a585d2010-08-17 15:55:45 +0000637 PragmaPushMacroInfo.erase(iter);
638 } else {
639 Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
640 << IdentInfo->getName();
641 }
642}
Chris Lattnerb694ba72006-07-02 22:41:36 +0000643
Aaron Ballman611306e2012-03-02 22:51:54 +0000644void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000645 // We will either get a quoted filename or a bracketed filename, and we
Aaron Ballman611306e2012-03-02 22:51:54 +0000646 // have to track which we got. The first filename is the source name,
647 // and the second name is the mapped filename. If the first is quoted,
648 // the second must be as well (cannot mix and match quotes and brackets).
Aaron Ballman611306e2012-03-02 22:51:54 +0000649
650 // Get the open paren
651 Lex(Tok);
652 if (Tok.isNot(tok::l_paren)) {
653 Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
654 return;
655 }
656
657 // We expect either a quoted string literal, or a bracketed name
658 Token SourceFilenameTok;
Richard Smithb9b05102019-03-19 01:51:19 +0000659 if (LexHeaderName(SourceFilenameTok))
Aaron Ballman611306e2012-03-02 22:51:54 +0000660 return;
Aaron Ballman611306e2012-03-02 22:51:54 +0000661
662 StringRef SourceFileName;
663 SmallString<128> FileNameBuffer;
Richard Smith91e150d2019-03-19 22:09:55 +0000664 if (SourceFilenameTok.is(tok::header_name)) {
Aaron Ballman611306e2012-03-02 22:51:54 +0000665 SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
Aaron Ballman611306e2012-03-02 22:51:54 +0000666 } else {
667 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
668 return;
669 }
670 FileNameBuffer.clear();
671
672 // Now we expect a comma, followed by another include name
673 Lex(Tok);
674 if (Tok.isNot(tok::comma)) {
675 Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
676 return;
677 }
678
679 Token ReplaceFilenameTok;
Richard Smithb9b05102019-03-19 01:51:19 +0000680 if (LexHeaderName(ReplaceFilenameTok))
Aaron Ballman611306e2012-03-02 22:51:54 +0000681 return;
Aaron Ballman611306e2012-03-02 22:51:54 +0000682
683 StringRef ReplaceFileName;
Richard Smith91e150d2019-03-19 22:09:55 +0000684 if (ReplaceFilenameTok.is(tok::header_name)) {
Aaron Ballman611306e2012-03-02 22:51:54 +0000685 ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
Aaron Ballman611306e2012-03-02 22:51:54 +0000686 } else {
687 Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
688 return;
689 }
690
691 // Finally, we expect the closing paren
692 Lex(Tok);
693 if (Tok.isNot(tok::r_paren)) {
694 Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
695 return;
696 }
697
698 // Now that we have the source and target filenames, we need to make sure
699 // they're both of the same type (angled vs non-angled)
700 StringRef OriginalSource = SourceFileName;
701
Fangrui Song6907ce22018-07-30 19:24:48 +0000702 bool SourceIsAngled =
703 GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
Aaron Ballman611306e2012-03-02 22:51:54 +0000704 SourceFileName);
705 bool ReplaceIsAngled =
706 GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
707 ReplaceFileName);
708 if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
709 (SourceIsAngled != ReplaceIsAngled)) {
710 unsigned int DiagID;
711 if (SourceIsAngled)
712 DiagID = diag::warn_pragma_include_alias_mismatch_angle;
713 else
714 DiagID = diag::warn_pragma_include_alias_mismatch_quote;
715
716 Diag(SourceFilenameTok.getLocation(), DiagID)
Fangrui Song6907ce22018-07-30 19:24:48 +0000717 << SourceFileName
Aaron Ballman611306e2012-03-02 22:51:54 +0000718 << ReplaceFileName;
719
720 return;
721 }
722
723 // Now we can let the include handler know about this mapping
724 getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
725}
726
Richard Smith9565c75b2017-06-19 23:09:36 +0000727// Lex a component of a module name: either an identifier or a string literal;
728// for components that can be expressed both ways, the two forms are equivalent.
729static bool LexModuleNameComponent(
730 Preprocessor &PP, Token &Tok,
731 std::pair<IdentifierInfo *, SourceLocation> &ModuleNameComponent,
732 bool First) {
733 PP.LexUnexpandedToken(Tok);
734 if (Tok.is(tok::string_literal) && !Tok.hasUDSuffix()) {
735 StringLiteralParser Literal(Tok, PP);
736 if (Literal.hadError)
737 return true;
738 ModuleNameComponent = std::make_pair(
739 PP.getIdentifierInfo(Literal.GetString()), Tok.getLocation());
740 } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) {
741 ModuleNameComponent =
742 std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation());
743 } else {
744 PP.Diag(Tok.getLocation(), diag::err_pp_expected_module_name) << First;
745 return true;
746 }
747 return false;
748}
749
750static bool LexModuleName(
751 Preprocessor &PP, Token &Tok,
752 llvm::SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>>
753 &ModuleName) {
754 while (true) {
755 std::pair<IdentifierInfo*, SourceLocation> NameComponent;
756 if (LexModuleNameComponent(PP, Tok, NameComponent, ModuleName.empty()))
757 return true;
758 ModuleName.push_back(NameComponent);
759
760 PP.LexUnexpandedToken(Tok);
761 if (Tok.isNot(tok::period))
762 return false;
763 }
764}
765
Richard Smith5d2ed482017-06-09 19:22:32 +0000766void Preprocessor::HandlePragmaModuleBuild(Token &Tok) {
767 SourceLocation Loc = Tok.getLocation();
768
Richard Smith9565c75b2017-06-19 23:09:36 +0000769 std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc;
770 if (LexModuleNameComponent(*this, Tok, ModuleNameLoc, true))
Richard Smith5d2ed482017-06-09 19:22:32 +0000771 return;
Richard Smith9565c75b2017-06-19 23:09:36 +0000772 IdentifierInfo *ModuleName = ModuleNameLoc.first;
Richard Smith5d2ed482017-06-09 19:22:32 +0000773
774 LexUnexpandedToken(Tok);
775 if (Tok.isNot(tok::eod)) {
776 Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
777 DiscardUntilEndOfDirective();
778 }
779
Richard Smith5d2ed482017-06-09 19:22:32 +0000780 CurLexer->LexingRawMode = true;
781
782 auto TryConsumeIdentifier = [&](StringRef Ident) -> bool {
783 if (Tok.getKind() != tok::raw_identifier ||
784 Tok.getRawIdentifier() != Ident)
785 return false;
786 CurLexer->Lex(Tok);
787 return true;
788 };
789
790 // Scan forward looking for the end of the module.
791 const char *Start = CurLexer->getBufferLocation();
792 const char *End = nullptr;
793 unsigned NestingLevel = 1;
794 while (true) {
795 End = CurLexer->getBufferLocation();
796 CurLexer->Lex(Tok);
797
798 if (Tok.is(tok::eof)) {
799 Diag(Loc, diag::err_pp_module_build_missing_end);
800 break;
801 }
802
803 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) {
804 // Token was part of module; keep going.
805 continue;
806 }
807
808 // We hit something directive-shaped; check to see if this is the end
809 // of the module build.
810 CurLexer->ParsingPreprocessorDirective = true;
811 CurLexer->Lex(Tok);
812 if (TryConsumeIdentifier("pragma") && TryConsumeIdentifier("clang") &&
813 TryConsumeIdentifier("module")) {
814 if (TryConsumeIdentifier("build"))
815 // #pragma clang module build -> entering a nested module build.
816 ++NestingLevel;
817 else if (TryConsumeIdentifier("endbuild")) {
818 // #pragma clang module endbuild -> leaving a module build.
819 if (--NestingLevel == 0)
820 break;
821 }
822 // We should either be looking at the EOD or more of the current directive
823 // preceding the EOD. Either way we can ignore this token and keep going.
824 assert(Tok.getKind() != tok::eof && "missing EOD before EOF");
825 }
826 }
827
828 CurLexer->LexingRawMode = false;
829
830 // Load the extracted text as a preprocessed module.
831 assert(CurLexer->getBuffer().begin() <= Start &&
832 Start <= CurLexer->getBuffer().end() &&
833 CurLexer->getBuffer().begin() <= End &&
834 End <= CurLexer->getBuffer().end() &&
835 "module source range not contained within same file buffer");
836 TheModuleLoader.loadModuleFromSource(Loc, ModuleName->getName(),
837 StringRef(Start, End - Start));
838}
839
Mike Rice58df1af2018-09-11 17:10:44 +0000840void Preprocessor::HandlePragmaHdrstop(Token &Tok) {
841 Lex(Tok);
842 if (Tok.is(tok::l_paren)) {
843 Diag(Tok.getLocation(), diag::warn_pp_hdrstop_filename_ignored);
844
845 std::string FileName;
846 if (!LexStringLiteral(Tok, FileName, "pragma hdrstop", false))
847 return;
848
849 if (Tok.isNot(tok::r_paren)) {
850 Diag(Tok, diag::err_expected) << tok::r_paren;
851 return;
852 }
853 Lex(Tok);
854 }
855 if (Tok.isNot(tok::eod))
856 Diag(Tok.getLocation(), diag::ext_pp_extra_tokens_at_eol)
857 << "pragma hdrstop";
858
859 if (creatingPCHWithPragmaHdrStop() &&
860 SourceMgr.isInMainFile(Tok.getLocation())) {
861 assert(CurLexer && "no lexer for #pragma hdrstop processing");
862 Token &Result = Tok;
863 Result.startToken();
864 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
865 CurLexer->cutOffLexing();
866 }
867 if (usingPCHWithPragmaHdrStop())
868 SkippingUntilPragmaHdrStop = false;
869}
870
Chris Lattnerb694ba72006-07-02 22:41:36 +0000871/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
872/// If 'Namespace' is non-null, then it is a token required to exist on the
873/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000874void Preprocessor::AddPragmaHandler(StringRef Namespace,
Chris Lattnerb694ba72006-07-02 22:41:36 +0000875 PragmaHandler *Handler) {
Craig Topperbe250302014-09-12 05:19:24 +0000876 PragmaNamespace *InsertNS = PragmaHandlers.get();
Mike Stump11289f42009-09-09 15:08:12 +0000877
Chris Lattnerb694ba72006-07-02 22:41:36 +0000878 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000879 if (!Namespace.empty()) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000880 // If there is already a pragma handler with the name of this namespace,
881 // we either have an error (directive with the same name as a namespace) or
882 // we already have the namespace to insert into.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000883 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000884 InsertNS = Existing->getIfNamespace();
Craig Topperd2d442c2014-05-17 23:10:59 +0000885 assert(InsertNS != nullptr && "Cannot have a pragma namespace and pragma"
Chris Lattnerb694ba72006-07-02 22:41:36 +0000886 " handler with the same name!");
887 } else {
888 // Otherwise, this namespace doesn't exist yet, create and insert the
889 // handler for it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000890 InsertNS = new PragmaNamespace(Namespace);
Chris Lattnerb694ba72006-07-02 22:41:36 +0000891 PragmaHandlers->AddPragma(InsertNS);
892 }
893 }
Mike Stump11289f42009-09-09 15:08:12 +0000894
Chris Lattnerb694ba72006-07-02 22:41:36 +0000895 // Check to make sure we don't already have a pragma for this identifier.
896 assert(!InsertNS->FindHandler(Handler->getName()) &&
897 "Pragma handler already exists for this identifier!");
898 InsertNS->AddPragma(Handler);
899}
900
Daniel Dunbar40596532008-10-04 19:17:46 +0000901/// RemovePragmaHandler - Remove the specific pragma handler from the
902/// preprocessor. If \arg Namespace is non-null, then it should be the
903/// namespace that \arg Handler was added to. It is an error to remove
904/// a handler that has not been registered.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000905void Preprocessor::RemovePragmaHandler(StringRef Namespace,
Daniel Dunbar40596532008-10-04 19:17:46 +0000906 PragmaHandler *Handler) {
Craig Topperbe250302014-09-12 05:19:24 +0000907 PragmaNamespace *NS = PragmaHandlers.get();
Mike Stump11289f42009-09-09 15:08:12 +0000908
Daniel Dunbar40596532008-10-04 19:17:46 +0000909 // If this is specified to be in a namespace, step down into it.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000910 if (!Namespace.empty()) {
911 PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
Daniel Dunbar40596532008-10-04 19:17:46 +0000912 assert(Existing && "Namespace containing handler does not exist!");
913
914 NS = Existing->getIfNamespace();
915 assert(NS && "Invalid namespace, registered as a regular pragma handler!");
916 }
917
918 NS->RemovePragmaHandler(Handler);
Mike Stump11289f42009-09-09 15:08:12 +0000919
Craig Topperbe250302014-09-12 05:19:24 +0000920 // If this is a non-default namespace and it is now empty, remove it.
921 if (NS != PragmaHandlers.get() && NS->IsEmpty()) {
Daniel Dunbar40596532008-10-04 19:17:46 +0000922 PragmaHandlers->RemovePragmaHandler(NS);
Argyrios Kyrtzidis7ce75262012-01-06 00:22:09 +0000923 delete NS;
924 }
Daniel Dunbar40596532008-10-04 19:17:46 +0000925}
926
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000927bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
928 Token Tok;
929 LexUnexpandedToken(Tok);
930
931 if (Tok.isNot(tok::identifier)) {
932 Diag(Tok, diag::ext_on_off_switch_syntax);
933 return true;
934 }
935 IdentifierInfo *II = Tok.getIdentifierInfo();
936 if (II->isStr("ON"))
937 Result = tok::OOS_ON;
938 else if (II->isStr("OFF"))
939 Result = tok::OOS_OFF;
940 else if (II->isStr("DEFAULT"))
941 Result = tok::OOS_DEFAULT;
942 else {
943 Diag(Tok, diag::ext_on_off_switch_syntax);
944 return true;
945 }
946
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000947 // Verify that this is followed by EOD.
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000948 LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000949 if (Tok.isNot(tok::eod))
950 Diag(Tok, diag::ext_pragma_syntax_eod);
Peter Collingbourne3bffa522011-02-14 01:42:24 +0000951 return false;
952}
953
Chris Lattnerb694ba72006-07-02 22:41:36 +0000954namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000955
James Dennett18a6d792012-06-17 03:26:26 +0000956/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000957struct PragmaOnceHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000958 PragmaOnceHandler() : PragmaHandler("once") {}
Eugene Zelenkocb96ac62017-12-08 22:39:26 +0000959
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000960 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper9140dd22014-03-11 06:50:42 +0000961 Token &OnceTok) override {
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000962 PP.CheckEndOfDirective("pragma once");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000963 PP.HandlePragmaOnce(OnceTok);
964 }
965};
966
James Dennett18a6d792012-06-17 03:26:26 +0000967/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
Chris Lattnerc2383312007-12-19 19:38:36 +0000968/// rest of the line is not lexed.
969struct PragmaMarkHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000970 PragmaMarkHandler() : PragmaHandler("mark") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000971
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000972 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper9140dd22014-03-11 06:50:42 +0000973 Token &MarkTok) override {
Chris Lattnerc2383312007-12-19 19:38:36 +0000974 PP.HandlePragmaMark();
975 }
976};
977
James Dennett18a6d792012-06-17 03:26:26 +0000978/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000979struct PragmaPoisonHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000980 PragmaPoisonHandler() : PragmaHandler("poison") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000981
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000982 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper9140dd22014-03-11 06:50:42 +0000983 Token &PoisonTok) override {
Erik Verbruggen851ce0e2016-10-26 11:46:10 +0000984 PP.HandlePragmaPoison();
Chris Lattnerb694ba72006-07-02 22:41:36 +0000985 }
986};
987
James Dennett18a6d792012-06-17 03:26:26 +0000988/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
Chris Lattnerc2383312007-12-19 19:38:36 +0000989/// as a system header, which silences warnings in it.
Chris Lattnerb694ba72006-07-02 22:41:36 +0000990struct PragmaSystemHeaderHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000991 PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000992
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000993 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper9140dd22014-03-11 06:50:42 +0000994 Token &SHToken) override {
Chris Lattnerb694ba72006-07-02 22:41:36 +0000995 PP.HandlePragmaSystemHeader(SHToken);
Chris Lattnerce2ab6f2009-04-14 05:07:49 +0000996 PP.CheckEndOfDirective("pragma");
Chris Lattnerb694ba72006-07-02 22:41:36 +0000997 }
998};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000999
Chris Lattnerb694ba72006-07-02 22:41:36 +00001000struct PragmaDependencyHandler : public PragmaHandler {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001001 PragmaDependencyHandler() : PragmaHandler("dependency") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001002
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001003 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper9140dd22014-03-11 06:50:42 +00001004 Token &DepToken) override {
Chris Lattnerb694ba72006-07-02 22:41:36 +00001005 PP.HandlePragmaDependency(DepToken);
1006 }
1007};
Mike Stump11289f42009-09-09 15:08:12 +00001008
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001009struct PragmaDebugHandler : public PragmaHandler {
1010 PragmaDebugHandler() : PragmaHandler("__debug") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001011
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001012 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Richard Smith77e53cbe2019-04-18 00:57:01 +00001013 Token &DebugToken) override {
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001014 Token Tok;
1015 PP.LexUnexpandedToken(Tok);
1016 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001017 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001018 return;
1019 }
1020 IdentifierInfo *II = Tok.getIdentifierInfo();
1021
Daniel Dunbarf2cf3292010-08-17 22:32:48 +00001022 if (II->isStr("assert")) {
David Blaikie83d382b2011-09-23 05:06:16 +00001023 llvm_unreachable("This is an assertion!");
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001024 } else if (II->isStr("crash")) {
David Blaikie5bd4c2a2012-08-21 18:56:49 +00001025 LLVM_BUILTIN_TRAP;
David Blaikie5d577a22012-06-29 22:03:56 +00001026 } else if (II->isStr("parser_crash")) {
1027 Token Crasher;
Benjamin Kramer3162f292015-03-08 19:28:24 +00001028 Crasher.startToken();
David Blaikie5d577a22012-06-29 22:03:56 +00001029 Crasher.setKind(tok::annot_pragma_parser_crash);
Benjamin Kramer3162f292015-03-08 19:28:24 +00001030 Crasher.setAnnotationRange(SourceRange(Tok.getLocation()));
Ilya Biryukov929af672019-05-17 09:32:05 +00001031 PP.EnterToken(Crasher, /*IsReinject*/false);
Richard Smithba3a4f92016-01-12 21:59:26 +00001032 } else if (II->isStr("dump")) {
1033 Token Identifier;
1034 PP.LexUnexpandedToken(Identifier);
1035 if (auto *DumpII = Identifier.getIdentifierInfo()) {
1036 Token DumpAnnot;
1037 DumpAnnot.startToken();
1038 DumpAnnot.setKind(tok::annot_pragma_dump);
1039 DumpAnnot.setAnnotationRange(
1040 SourceRange(Tok.getLocation(), Identifier.getLocation()));
1041 DumpAnnot.setAnnotationValue(DumpII);
1042 PP.DiscardUntilEndOfDirective();
Ilya Biryukov929af672019-05-17 09:32:05 +00001043 PP.EnterToken(DumpAnnot, /*IsReinject*/false);
Richard Smithba3a4f92016-01-12 21:59:26 +00001044 } else {
1045 PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument)
1046 << II->getName();
1047 }
Richard Smith6c2b5a82018-02-09 01:15:13 +00001048 } else if (II->isStr("diag_mapping")) {
1049 Token DiagName;
1050 PP.LexUnexpandedToken(DiagName);
1051 if (DiagName.is(tok::eod))
1052 PP.getDiagnostics().dump();
1053 else if (DiagName.is(tok::string_literal) && !DiagName.hasUDSuffix()) {
1054 StringLiteralParser Literal(DiagName, PP);
1055 if (Literal.hadError)
1056 return;
1057 PP.getDiagnostics().dump(Literal.GetString());
1058 } else {
1059 PP.Diag(DiagName, diag::warn_pragma_debug_missing_argument)
1060 << II->getName();
1061 }
Daniel Dunbarf2cf3292010-08-17 22:32:48 +00001062 } else if (II->isStr("llvm_fatal_error")) {
1063 llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
1064 } else if (II->isStr("llvm_unreachable")) {
1065 llvm_unreachable("#pragma clang __debug llvm_unreachable");
Richard Smith3ffa61d2015-04-30 23:10:40 +00001066 } else if (II->isStr("macro")) {
1067 Token MacroName;
1068 PP.LexUnexpandedToken(MacroName);
1069 auto *MacroII = MacroName.getIdentifierInfo();
1070 if (MacroII)
1071 PP.dumpMacroInfo(MacroII);
1072 else
Richard Smithba3a4f92016-01-12 21:59:26 +00001073 PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument)
1074 << II->getName();
Richard Smith77e53cbe2019-04-18 00:57:01 +00001075 } else if (II->isStr("module_map")) {
1076 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1077 ModuleName;
1078 if (LexModuleName(PP, Tok, ModuleName))
1079 return;
1080 ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap();
1081 Module *M = nullptr;
1082 for (auto IIAndLoc : ModuleName) {
1083 M = MM.lookupModuleQualified(IIAndLoc.first->getName(), M);
1084 if (!M) {
1085 PP.Diag(IIAndLoc.second, diag::warn_pragma_debug_unknown_module)
1086 << IIAndLoc.first;
1087 return;
1088 }
1089 }
1090 M->dump();
Daniel Dunbarf2cf3292010-08-17 22:32:48 +00001091 } else if (II->isStr("overflow_stack")) {
1092 DebugOverflowStack();
Daniel Dunbar211a7872010-08-18 23:09:23 +00001093 } else if (II->isStr("handle_crash")) {
1094 llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
1095 if (CRC)
1096 CRC->HandleCrash();
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +00001097 } else if (II->isStr("captured")) {
1098 HandleCaptured(PP);
Daniel Dunbarf2cf3292010-08-17 22:32:48 +00001099 } else {
1100 PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
1101 << II->getName();
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001102 }
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +00001103
1104 PPCallbacks *Callbacks = PP.getPPCallbacks();
1105 if (Callbacks)
1106 Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
1107 }
1108
1109 void HandleCaptured(Preprocessor &PP) {
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +00001110 Token Tok;
1111 PP.LexUnexpandedToken(Tok);
1112
1113 if (Tok.isNot(tok::eod)) {
1114 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
1115 << "pragma clang __debug captured";
1116 return;
1117 }
1118
1119 SourceLocation NameLoc = Tok.getLocation();
David Blaikie2eabcc92016-02-09 18:52:09 +00001120 MutableArrayRef<Token> Toks(
1121 PP.getPreprocessorAllocator().Allocate<Token>(1), 1);
1122 Toks[0].startToken();
1123 Toks[0].setKind(tok::annot_pragma_captured);
1124 Toks[0].setLocation(NameLoc);
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +00001125
Ilya Biryukov929af672019-05-17 09:32:05 +00001126 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
1127 /*IsReinject=*/false);
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001128 }
1129
Francois Pichet2e11f5d2011-05-25 16:15:03 +00001130// Disable MSVC warning about runtime stack overflow.
1131#ifdef _MSC_VER
1132 #pragma warning(disable : 4717)
1133#endif
Matthias Braun285f88d2017-04-24 18:41:00 +00001134 static void DebugOverflowStack(void (*P)() = nullptr) {
1135 void (*volatile Self)(void(*P)()) = DebugOverflowStack;
1136 Self(reinterpret_cast<void(*)()>(Self));
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001137 }
Francois Pichet2e11f5d2011-05-25 16:15:03 +00001138#ifdef _MSC_VER
1139 #pragma warning(default : 4717)
1140#endif
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001141};
1142
James Dennett18a6d792012-06-17 03:26:26 +00001143/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
Chris Lattner504af112009-04-19 23:16:58 +00001144struct PragmaDiagnosticHandler : public PragmaHandler {
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001145private:
1146 const char *Namespace;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001147
Chris Lattnerfb42a182009-07-12 21:18:45 +00001148public:
Eugene Zelenkocb96ac62017-12-08 22:39:26 +00001149 explicit PragmaDiagnosticHandler(const char *NS)
1150 : PragmaHandler("diagnostic"), Namespace(NS) {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001151
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001152 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper9140dd22014-03-11 06:50:42 +00001153 Token &DiagToken) override {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001154 SourceLocation DiagLoc = DiagToken.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001155 Token Tok;
1156 PP.LexUnexpandedToken(Tok);
1157 if (Tok.isNot(tok::identifier)) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001158 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001159 return;
1160 }
1161 IdentifierInfo *II = Tok.getIdentifierInfo();
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001162 PPCallbacks *Callbacks = PP.getPPCallbacks();
Mike Stump11289f42009-09-09 15:08:12 +00001163
Alp Toker46df1c02014-06-12 10:15:20 +00001164 if (II->isStr("pop")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001165 if (!PP.getDiagnostics().popMappings(DiagLoc))
Douglas Gregor3cc26482010-08-30 15:15:34 +00001166 PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001167 else if (Callbacks)
1168 Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
Douglas Gregor3cc26482010-08-30 15:15:34 +00001169 return;
1170 } else if (II->isStr("push")) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001171 PP.getDiagnostics().pushMappings(DiagLoc);
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001172 if (Callbacks)
1173 Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
Chris Lattnerfb42a182009-07-12 21:18:45 +00001174 return;
Alp Toker46df1c02014-06-12 10:15:20 +00001175 }
1176
1177 diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName())
1178 .Case("ignored", diag::Severity::Ignored)
1179 .Case("warning", diag::Severity::Warning)
1180 .Case("error", diag::Severity::Error)
1181 .Case("fatal", diag::Severity::Fatal)
1182 .Default(diag::Severity());
1183
1184 if (SV == diag::Severity()) {
Douglas Gregor3cc26482010-08-30 15:15:34 +00001185 PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
Chris Lattner504af112009-04-19 23:16:58 +00001186 return;
1187 }
Mike Stump11289f42009-09-09 15:08:12 +00001188
Chris Lattner504af112009-04-19 23:16:58 +00001189 PP.LexUnexpandedToken(Tok);
Andy Gibbs58905d22012-11-17 19:15:38 +00001190 SourceLocation StringLoc = Tok.getLocation();
Chris Lattner504af112009-04-19 23:16:58 +00001191
Andy Gibbs58905d22012-11-17 19:15:38 +00001192 std::string WarningName;
Andy Gibbsa8df57a2012-11-17 19:16:52 +00001193 if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001194 /*AllowMacroExpansion=*/false))
Chris Lattner504af112009-04-19 23:16:58 +00001195 return;
Mike Stump11289f42009-09-09 15:08:12 +00001196
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001197 if (Tok.isNot(tok::eod)) {
Chris Lattner504af112009-04-19 23:16:58 +00001198 PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1199 return;
1200 }
Mike Stump11289f42009-09-09 15:08:12 +00001201
Chris Lattner504af112009-04-19 23:16:58 +00001202 if (WarningName.size() < 3 || WarningName[0] != '-' ||
Richard Smith3be1cb22014-08-07 00:24:21 +00001203 (WarningName[1] != 'W' && WarningName[1] != 'R')) {
Andy Gibbs58905d22012-11-17 19:15:38 +00001204 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
Chris Lattner504af112009-04-19 23:16:58 +00001205 return;
1206 }
Mike Stump11289f42009-09-09 15:08:12 +00001207
Sunil Srivastava5239de72016-02-13 01:44:05 +00001208 diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError
1209 : diag::Flavor::Remark;
Benjamin Kramer2193e232016-02-13 13:42:41 +00001210 StringRef Group = StringRef(WarningName).substr(2);
Sunil Srivastava5239de72016-02-13 01:44:05 +00001211 bool unknownDiag = false;
1212 if (Group == "everything") {
1213 // Special handling for pragma clang diagnostic ... "-Weverything".
1214 // There is no formal group named "everything", so there has to be a
1215 // special case for it.
1216 PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc);
1217 } else
1218 unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV,
1219 DiagLoc);
1220 if (unknownDiag)
Andy Gibbs58905d22012-11-17 19:15:38 +00001221 PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1222 << WarningName;
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001223 else if (Callbacks)
Alp Toker46df1c02014-06-12 10:15:20 +00001224 Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName);
Chris Lattner504af112009-04-19 23:16:58 +00001225 }
1226};
Mike Stump11289f42009-09-09 15:08:12 +00001227
Mike Rice58df1af2018-09-11 17:10:44 +00001228/// "\#pragma hdrstop [<header-name-string>]"
1229struct PragmaHdrstopHandler : public PragmaHandler {
1230 PragmaHdrstopHandler() : PragmaHandler("hdrstop") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001231 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Mike Rice58df1af2018-09-11 17:10:44 +00001232 Token &DepToken) override {
1233 PP.HandlePragmaHdrstop(DepToken);
1234 }
1235};
1236
Reid Kleckner881dff32013-09-13 22:00:30 +00001237/// "\#pragma warning(...)". MSVC's diagnostics do not map cleanly to clang's
1238/// diagnostics, so we don't really implement this pragma. We parse it and
1239/// ignore it to avoid -Wunknown-pragma warnings.
1240struct PragmaWarningHandler : public PragmaHandler {
1241 PragmaWarningHandler() : PragmaHandler("warning") {}
1242
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001243 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper9140dd22014-03-11 06:50:42 +00001244 Token &Tok) override {
Reid Kleckner881dff32013-09-13 22:00:30 +00001245 // Parse things like:
1246 // warning(push, 1)
1247 // warning(pop)
John Thompson4762b232013-11-16 00:16:03 +00001248 // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9)
Reid Kleckner881dff32013-09-13 22:00:30 +00001249 SourceLocation DiagLoc = Tok.getLocation();
1250 PPCallbacks *Callbacks = PP.getPPCallbacks();
1251
1252 PP.Lex(Tok);
1253 if (Tok.isNot(tok::l_paren)) {
1254 PP.Diag(Tok, diag::warn_pragma_warning_expected) << "(";
1255 return;
1256 }
1257
1258 PP.Lex(Tok);
1259 IdentifierInfo *II = Tok.getIdentifierInfo();
Reid Kleckner881dff32013-09-13 22:00:30 +00001260
Alexander Musman6b080fc2015-05-25 11:21:20 +00001261 if (II && II->isStr("push")) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001262 // #pragma warning( push[ ,n ] )
Reid Kleckner4d185102013-10-02 15:19:23 +00001263 int Level = -1;
Reid Kleckner881dff32013-09-13 22:00:30 +00001264 PP.Lex(Tok);
1265 if (Tok.is(tok::comma)) {
1266 PP.Lex(Tok);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001267 uint64_t Value;
1268 if (Tok.is(tok::numeric_constant) &&
1269 PP.parseSimpleIntegerLiteral(Tok, Value))
1270 Level = int(Value);
Reid Kleckner4d185102013-10-02 15:19:23 +00001271 if (Level < 0 || Level > 4) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001272 PP.Diag(Tok, diag::warn_pragma_warning_push_level);
1273 return;
1274 }
1275 }
1276 if (Callbacks)
1277 Callbacks->PragmaWarningPush(DiagLoc, Level);
Alexander Musman6b080fc2015-05-25 11:21:20 +00001278 } else if (II && II->isStr("pop")) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001279 // #pragma warning( pop )
1280 PP.Lex(Tok);
1281 if (Callbacks)
1282 Callbacks->PragmaWarningPop(DiagLoc);
1283 } else {
1284 // #pragma warning( warning-specifier : warning-number-list
1285 // [; warning-specifier : warning-number-list...] )
1286 while (true) {
1287 II = Tok.getIdentifierInfo();
Alexander Musman6b080fc2015-05-25 11:21:20 +00001288 if (!II && !Tok.is(tok::numeric_constant)) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001289 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1290 return;
1291 }
1292
1293 // Figure out which warning specifier this is.
Alexander Musman6b080fc2015-05-25 11:21:20 +00001294 bool SpecifierValid;
1295 StringRef Specifier;
1296 llvm::SmallString<1> SpecifierBuf;
1297 if (II) {
1298 Specifier = II->getName();
1299 SpecifierValid = llvm::StringSwitch<bool>(Specifier)
1300 .Cases("default", "disable", "error", "once",
1301 "suppress", true)
1302 .Default(false);
1303 // If we read a correct specifier, snatch next token (that should be
1304 // ":", checked later).
1305 if (SpecifierValid)
1306 PP.Lex(Tok);
1307 } else {
1308 // Token is a numeric constant. It should be either 1, 2, 3 or 4.
1309 uint64_t Value;
1310 Specifier = PP.getSpelling(Tok, SpecifierBuf);
1311 if (PP.parseSimpleIntegerLiteral(Tok, Value)) {
1312 SpecifierValid = (Value >= 1) && (Value <= 4);
1313 } else
1314 SpecifierValid = false;
1315 // Next token already snatched by parseSimpleIntegerLiteral.
1316 }
1317
Reid Kleckner881dff32013-09-13 22:00:30 +00001318 if (!SpecifierValid) {
1319 PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1320 return;
1321 }
Reid Kleckner881dff32013-09-13 22:00:30 +00001322 if (Tok.isNot(tok::colon)) {
1323 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":";
1324 return;
1325 }
1326
1327 // Collect the warning ids.
1328 SmallVector<int, 4> Ids;
1329 PP.Lex(Tok);
1330 while (Tok.is(tok::numeric_constant)) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001331 uint64_t Value;
1332 if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 ||
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001333 Value > std::numeric_limits<int>::max()) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001334 PP.Diag(Tok, diag::warn_pragma_warning_expected_number);
1335 return;
1336 }
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001337 Ids.push_back(int(Value));
Reid Kleckner881dff32013-09-13 22:00:30 +00001338 }
1339 if (Callbacks)
1340 Callbacks->PragmaWarning(DiagLoc, Specifier, Ids);
1341
1342 // Parse the next specifier if there is a semicolon.
1343 if (Tok.isNot(tok::semi))
1344 break;
1345 PP.Lex(Tok);
1346 }
1347 }
1348
1349 if (Tok.isNot(tok::r_paren)) {
1350 PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")";
1351 return;
1352 }
1353
1354 PP.Lex(Tok);
1355 if (Tok.isNot(tok::eod))
1356 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning";
1357 }
1358};
1359
Reid Kleckner0f56b222019-03-14 18:12:17 +00001360/// "\#pragma execution_character_set(...)". MSVC supports this pragma only
1361/// for "UTF-8". We parse it and ignore it if UTF-8 is provided and warn
1362/// otherwise to avoid -Wunknown-pragma warnings.
1363struct PragmaExecCharsetHandler : public PragmaHandler {
1364 PragmaExecCharsetHandler() : PragmaHandler("execution_character_set") {}
1365
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001366 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Reid Kleckner0f56b222019-03-14 18:12:17 +00001367 Token &Tok) override {
1368 // Parse things like:
1369 // execution_character_set(push, "UTF-8")
1370 // execution_character_set(pop)
1371 SourceLocation DiagLoc = Tok.getLocation();
1372 PPCallbacks *Callbacks = PP.getPPCallbacks();
1373
1374 PP.Lex(Tok);
1375 if (Tok.isNot(tok::l_paren)) {
1376 PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << "(";
1377 return;
1378 }
1379
1380 PP.Lex(Tok);
1381 IdentifierInfo *II = Tok.getIdentifierInfo();
1382
1383 if (II && II->isStr("push")) {
1384 // #pragma execution_character_set( push[ , string ] )
1385 PP.Lex(Tok);
1386 if (Tok.is(tok::comma)) {
1387 PP.Lex(Tok);
1388
1389 std::string ExecCharset;
1390 if (!PP.FinishLexStringLiteral(Tok, ExecCharset,
1391 "pragma execution_character_set",
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001392 /*AllowMacroExpansion=*/false))
Reid Kleckner0f56b222019-03-14 18:12:17 +00001393 return;
1394
1395 // MSVC supports either of these, but nothing else.
1396 if (ExecCharset != "UTF-8" && ExecCharset != "utf-8") {
1397 PP.Diag(Tok, diag::warn_pragma_exec_charset_push_invalid) << ExecCharset;
1398 return;
1399 }
1400 }
1401 if (Callbacks)
1402 Callbacks->PragmaExecCharsetPush(DiagLoc, "UTF-8");
1403 } else if (II && II->isStr("pop")) {
1404 // #pragma execution_character_set( pop )
1405 PP.Lex(Tok);
1406 if (Callbacks)
1407 Callbacks->PragmaExecCharsetPop(DiagLoc);
1408 } else {
1409 PP.Diag(Tok, diag::warn_pragma_exec_charset_spec_invalid);
1410 return;
1411 }
1412
1413 if (Tok.isNot(tok::r_paren)) {
1414 PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << ")";
1415 return;
1416 }
1417
1418 PP.Lex(Tok);
1419 if (Tok.isNot(tok::eod))
1420 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma execution_character_set";
1421 }
1422};
1423
James Dennett18a6d792012-06-17 03:26:26 +00001424/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
Aaron Ballman611306e2012-03-02 22:51:54 +00001425struct PragmaIncludeAliasHandler : public PragmaHandler {
1426 PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
Eugene Zelenkocb96ac62017-12-08 22:39:26 +00001427
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001428 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper9140dd22014-03-11 06:50:42 +00001429 Token &IncludeAliasTok) override {
Reid Kleckner881dff32013-09-13 22:00:30 +00001430 PP.HandlePragmaIncludeAlias(IncludeAliasTok);
Aaron Ballman611306e2012-03-02 22:51:54 +00001431 }
1432};
1433
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001434/// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1435/// extension. The syntax is:
1436/// \code
1437/// #pragma message(string)
1438/// \endcode
1439/// OR, in GCC mode:
1440/// \code
1441/// #pragma message string
1442/// \endcode
1443/// string is a string, which is fully macro expanded, and permits string
1444/// concatenation, embedded escape characters, etc... See MSDN for more details.
1445/// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1446/// form as \#pragma message.
Chris Lattner30c924b2010-06-26 17:11:39 +00001447struct PragmaMessageHandler : public PragmaHandler {
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001448private:
1449 const PPCallbacks::PragmaMessageKind Kind;
1450 const StringRef Namespace;
1451
1452 static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1453 bool PragmaNameOnly = false) {
1454 switch (Kind) {
1455 case PPCallbacks::PMK_Message:
1456 return PragmaNameOnly ? "message" : "pragma message";
1457 case PPCallbacks::PMK_Warning:
1458 return PragmaNameOnly ? "warning" : "pragma warning";
1459 case PPCallbacks::PMK_Error:
1460 return PragmaNameOnly ? "error" : "pragma error";
1461 }
1462 llvm_unreachable("Unknown PragmaMessageKind!");
1463 }
1464
1465public:
1466 PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1467 StringRef Namespace = StringRef())
Eugene Zelenkocb96ac62017-12-08 22:39:26 +00001468 : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind),
1469 Namespace(Namespace) {}
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001470
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001471 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper9140dd22014-03-11 06:50:42 +00001472 Token &Tok) override {
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001473 SourceLocation MessageLoc = Tok.getLocation();
1474 PP.Lex(Tok);
1475 bool ExpectClosingParen = false;
1476 switch (Tok.getKind()) {
1477 case tok::l_paren:
1478 // We have a MSVC style pragma message.
1479 ExpectClosingParen = true;
1480 // Read the string.
1481 PP.Lex(Tok);
1482 break;
1483 case tok::string_literal:
1484 // We have a GCC style pragma message, and we just read the string.
1485 break;
1486 default:
1487 PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1488 return;
1489 }
1490
1491 std::string MessageString;
1492 if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001493 /*AllowMacroExpansion=*/true))
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001494 return;
1495
1496 if (ExpectClosingParen) {
1497 if (Tok.isNot(tok::r_paren)) {
1498 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1499 return;
1500 }
1501 PP.Lex(Tok); // eat the r_paren.
1502 }
1503
1504 if (Tok.isNot(tok::eod)) {
1505 PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1506 return;
1507 }
1508
1509 // Output the message.
1510 PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1511 ? diag::err_pragma_message
1512 : diag::warn_pragma_message) << MessageString;
1513
1514 // If the pragma is lexically sound, notify any interested PPCallbacks.
1515 if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1516 Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
Chris Lattner30c924b2010-06-26 17:11:39 +00001517 }
1518};
1519
Richard Smithc51c38b2017-04-29 00:34:47 +00001520/// Handle the clang \#pragma module import extension. The syntax is:
1521/// \code
1522/// #pragma clang module import some.module.name
1523/// \endcode
1524struct PragmaModuleImportHandler : public PragmaHandler {
1525 PragmaModuleImportHandler() : PragmaHandler("import") {}
1526
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001527 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Richard Smithd1386302017-05-04 00:29:54 +00001528 Token &Tok) override {
1529 SourceLocation ImportLoc = Tok.getLocation();
1530
1531 // Read the module name.
1532 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1533 ModuleName;
1534 if (LexModuleName(PP, Tok, ModuleName))
1535 return;
1536
1537 if (Tok.isNot(tok::eod))
1538 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1539
1540 // If we have a non-empty module path, load the named module.
1541 Module *Imported =
1542 PP.getModuleLoader().loadModule(ImportLoc, ModuleName, Module::Hidden,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001543 /*IsInclusionDirective=*/false);
Richard Smithd1386302017-05-04 00:29:54 +00001544 if (!Imported)
1545 return;
1546
1547 PP.makeModuleVisible(Imported, ImportLoc);
1548 PP.EnterAnnotationToken(SourceRange(ImportLoc, ModuleName.back().second),
1549 tok::annot_module_include, Imported);
1550 if (auto *CB = PP.getPPCallbacks())
1551 CB->moduleImport(ImportLoc, ModuleName, Imported);
1552 }
1553};
1554
1555/// Handle the clang \#pragma module begin extension. The syntax is:
1556/// \code
1557/// #pragma clang module begin some.module.name
1558/// ...
1559/// #pragma clang module end
1560/// \endcode
1561struct PragmaModuleBeginHandler : public PragmaHandler {
1562 PragmaModuleBeginHandler() : PragmaHandler("begin") {}
1563
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001564 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Richard Smithd1386302017-05-04 00:29:54 +00001565 Token &Tok) override {
1566 SourceLocation BeginLoc = Tok.getLocation();
1567
1568 // Read the module name.
1569 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1570 ModuleName;
1571 if (LexModuleName(PP, Tok, ModuleName))
1572 return;
1573
1574 if (Tok.isNot(tok::eod))
1575 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1576
1577 // We can only enter submodules of the current module.
1578 StringRef Current = PP.getLangOpts().CurrentModule;
1579 if (ModuleName.front().first->getName() != Current) {
1580 PP.Diag(ModuleName.front().second, diag::err_pp_module_begin_wrong_module)
1581 << ModuleName.front().first << (ModuleName.size() > 1)
1582 << Current.empty() << Current;
1583 return;
1584 }
1585
1586 // Find the module we're entering. We require that a module map for it
1587 // be loaded or implicitly loadable.
David Blaikie89e58dd2019-05-07 21:38:51 +00001588 auto &HSI = PP.getHeaderSearchInfo();
1589 Module *M = HSI.lookupModule(Current);
Richard Smithd1386302017-05-04 00:29:54 +00001590 if (!M) {
1591 PP.Diag(ModuleName.front().second,
1592 diag::err_pp_module_begin_no_module_map) << Current;
1593 return;
1594 }
1595 for (unsigned I = 1; I != ModuleName.size(); ++I) {
David Blaikie89e58dd2019-05-07 21:38:51 +00001596 auto *NewM = M->findOrInferSubmodule(ModuleName[I].first->getName());
Richard Smithd1386302017-05-04 00:29:54 +00001597 if (!NewM) {
1598 PP.Diag(ModuleName[I].second, diag::err_pp_module_begin_no_submodule)
1599 << M->getFullModuleName() << ModuleName[I].first;
1600 return;
1601 }
1602 M = NewM;
1603 }
1604
Richard Smith51d09c52017-05-30 05:22:59 +00001605 // If the module isn't available, it doesn't make sense to enter it.
Richard Smith27e5aa02017-06-05 18:57:56 +00001606 if (Preprocessor::checkModuleIsAvailable(
1607 PP.getLangOpts(), PP.getTargetInfo(), PP.getDiagnostics(), M)) {
Richard Smith51d09c52017-05-30 05:22:59 +00001608 PP.Diag(BeginLoc, diag::note_pp_module_begin_here)
1609 << M->getTopLevelModuleName();
1610 return;
1611 }
1612
Richard Smithd1386302017-05-04 00:29:54 +00001613 // Enter the scope of the submodule.
1614 PP.EnterSubmodule(M, BeginLoc, /*ForPragma*/true);
1615 PP.EnterAnnotationToken(SourceRange(BeginLoc, ModuleName.back().second),
1616 tok::annot_module_begin, M);
1617 }
1618};
1619
1620/// Handle the clang \#pragma module end extension.
1621struct PragmaModuleEndHandler : public PragmaHandler {
1622 PragmaModuleEndHandler() : PragmaHandler("end") {}
1623
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001624 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Richard Smithd1386302017-05-04 00:29:54 +00001625 Token &Tok) override {
1626 SourceLocation Loc = Tok.getLocation();
1627
1628 PP.LexUnexpandedToken(Tok);
1629 if (Tok.isNot(tok::eod))
1630 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1631
1632 Module *M = PP.LeaveSubmodule(/*ForPragma*/true);
1633 if (M)
1634 PP.EnterAnnotationToken(SourceRange(Loc), tok::annot_module_end, M);
1635 else
1636 PP.Diag(Loc, diag::err_pp_module_end_without_module_begin);
Richard Smithc51c38b2017-04-29 00:34:47 +00001637 }
1638};
1639
Richard Smith5d2ed482017-06-09 19:22:32 +00001640/// Handle the clang \#pragma module build extension.
1641struct PragmaModuleBuildHandler : public PragmaHandler {
1642 PragmaModuleBuildHandler() : PragmaHandler("build") {}
1643
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001644 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Richard Smith5d2ed482017-06-09 19:22:32 +00001645 Token &Tok) override {
1646 PP.HandlePragmaModuleBuild(Tok);
1647 }
1648};
1649
1650/// Handle the clang \#pragma module load extension.
1651struct PragmaModuleLoadHandler : public PragmaHandler {
1652 PragmaModuleLoadHandler() : PragmaHandler("load") {}
1653
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001654 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Richard Smith5d2ed482017-06-09 19:22:32 +00001655 Token &Tok) override {
1656 SourceLocation Loc = Tok.getLocation();
1657
1658 // Read the module name.
1659 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1660 ModuleName;
1661 if (LexModuleName(PP, Tok, ModuleName))
1662 return;
1663
1664 if (Tok.isNot(tok::eod))
1665 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1666
1667 // Load the module, don't make it visible.
1668 PP.getModuleLoader().loadModule(Loc, ModuleName, Module::Hidden,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001669 /*IsInclusionDirective=*/false);
Richard Smith5d2ed482017-06-09 19:22:32 +00001670 }
1671};
1672
James Dennett18a6d792012-06-17 03:26:26 +00001673/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001674/// macro on the top of the stack.
1675struct PragmaPushMacroHandler : public PragmaHandler {
1676 PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001677
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001678 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper9140dd22014-03-11 06:50:42 +00001679 Token &PushMacroTok) override {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001680 PP.HandlePragmaPushMacro(PushMacroTok);
1681 }
1682};
1683
James Dennett18a6d792012-06-17 03:26:26 +00001684/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001685/// macro to the value on the top of the stack.
1686struct PragmaPopMacroHandler : public PragmaHandler {
1687 PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001688
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001689 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper9140dd22014-03-11 06:50:42 +00001690 Token &PopMacroTok) override {
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001691 PP.HandlePragmaPopMacro(PopMacroTok);
1692 }
1693};
1694
Fangrui Song6907ce22018-07-30 19:24:48 +00001695/// PragmaARCCFCodeAuditedHandler -
James Dennett18a6d792012-06-17 03:26:26 +00001696/// \#pragma clang arc_cf_code_audited begin/end
John McCall32f5fe12011-09-30 05:12:12 +00001697struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1698 PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001699
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001700 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper9140dd22014-03-11 06:50:42 +00001701 Token &NameTok) override {
John McCall32f5fe12011-09-30 05:12:12 +00001702 SourceLocation Loc = NameTok.getLocation();
1703 bool IsBegin;
1704
1705 Token Tok;
1706
1707 // Lex the 'begin' or 'end'.
1708 PP.LexUnexpandedToken(Tok);
1709 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1710 if (BeginEnd && BeginEnd->isStr("begin")) {
1711 IsBegin = true;
1712 } else if (BeginEnd && BeginEnd->isStr("end")) {
1713 IsBegin = false;
1714 } else {
1715 PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1716 return;
1717 }
1718
1719 // Verify that this is followed by EOD.
1720 PP.LexUnexpandedToken(Tok);
1721 if (Tok.isNot(tok::eod))
1722 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1723
1724 // The start location of the active audit.
Erich Keane6a24e802019-09-13 17:39:31 +00001725 SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedInfo().second;
John McCall32f5fe12011-09-30 05:12:12 +00001726
1727 // The start location we want after processing this.
1728 SourceLocation NewLoc;
1729
1730 if (IsBegin) {
1731 // Complain about attempts to re-enter an audit.
1732 if (BeginLoc.isValid()) {
1733 PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1734 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1735 }
1736 NewLoc = Loc;
1737 } else {
1738 // Complain about attempts to leave an audit that doesn't exist.
1739 if (!BeginLoc.isValid()) {
1740 PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1741 return;
1742 }
1743 NewLoc = SourceLocation();
1744 }
1745
Erich Keane6a24e802019-09-13 17:39:31 +00001746 PP.setPragmaARCCFCodeAuditedInfo(NameTok.getIdentifierInfo(), NewLoc);
John McCall32f5fe12011-09-30 05:12:12 +00001747 }
1748};
1749
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001750/// PragmaAssumeNonNullHandler -
1751/// \#pragma clang assume_nonnull begin/end
1752struct PragmaAssumeNonNullHandler : public PragmaHandler {
1753 PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001754
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001755 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001756 Token &NameTok) override {
1757 SourceLocation Loc = NameTok.getLocation();
1758 bool IsBegin;
1759
1760 Token Tok;
1761
1762 // Lex the 'begin' or 'end'.
1763 PP.LexUnexpandedToken(Tok);
1764 const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1765 if (BeginEnd && BeginEnd->isStr("begin")) {
1766 IsBegin = true;
1767 } else if (BeginEnd && BeginEnd->isStr("end")) {
1768 IsBegin = false;
1769 } else {
1770 PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax);
1771 return;
1772 }
1773
1774 // Verify that this is followed by EOD.
1775 PP.LexUnexpandedToken(Tok);
1776 if (Tok.isNot(tok::eod))
1777 PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1778
1779 // The start location of the active audit.
1780 SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc();
1781
1782 // The start location we want after processing this.
1783 SourceLocation NewLoc;
Eli Friedman16fee082017-09-27 23:29:37 +00001784 PPCallbacks *Callbacks = PP.getPPCallbacks();
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001785
1786 if (IsBegin) {
1787 // Complain about attempts to re-enter an audit.
1788 if (BeginLoc.isValid()) {
1789 PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull);
1790 PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1791 }
1792 NewLoc = Loc;
Eli Friedman16fee082017-09-27 23:29:37 +00001793 if (Callbacks)
1794 Callbacks->PragmaAssumeNonNullBegin(NewLoc);
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001795 } else {
1796 // Complain about attempts to leave an audit that doesn't exist.
1797 if (!BeginLoc.isValid()) {
1798 PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull);
1799 return;
1800 }
1801 NewLoc = SourceLocation();
Eli Friedman16fee082017-09-27 23:29:37 +00001802 if (Callbacks)
1803 Callbacks->PragmaAssumeNonNullEnd(NewLoc);
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001804 }
1805
1806 PP.setPragmaAssumeNonNullLoc(NewLoc);
1807 }
1808};
1809
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001810/// Handle "\#pragma region [...]"
David Majnemer7aa8c2f2013-06-30 08:18:16 +00001811///
1812/// The syntax is
1813/// \code
1814/// #pragma region [optional name]
1815/// #pragma endregion [optional comment]
1816/// \endcode
1817///
1818/// \note This is
1819/// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1820/// pragma, just skipped by compiler.
1821struct PragmaRegionHandler : public PragmaHandler {
Eugene Zelenkocb96ac62017-12-08 22:39:26 +00001822 PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {}
Aaron Ballman406ea512012-11-30 19:52:30 +00001823
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001824 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper9140dd22014-03-11 06:50:42 +00001825 Token &NameTok) override {
David Majnemer7aa8c2f2013-06-30 08:18:16 +00001826 // #pragma region: endregion matches can be verified
1827 // __pragma(region): no sense, but ignored by msvc
1828 // _Pragma is not valid for MSVC, but there isn't any point
1829 // to handle a _Pragma differently.
1830 }
1831};
Aaron Ballman406ea512012-11-30 19:52:30 +00001832
Eugene Zelenkocb96ac62017-12-08 22:39:26 +00001833} // namespace
Chris Lattnerb694ba72006-07-02 22:41:36 +00001834
1835/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
James Dennett18a6d792012-06-17 03:26:26 +00001836/// \#pragma GCC poison/system_header/dependency and \#pragma once.
Chris Lattnerb694ba72006-07-02 22:41:36 +00001837void Preprocessor::RegisterBuiltinPragmas() {
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001838 AddPragmaHandler(new PragmaOnceHandler());
1839 AddPragmaHandler(new PragmaMarkHandler());
Chris Lattnerc0a585d2010-08-17 15:55:45 +00001840 AddPragmaHandler(new PragmaPushMacroHandler());
1841 AddPragmaHandler(new PragmaPopMacroHandler());
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001842 AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
Mike Stump11289f42009-09-09 15:08:12 +00001843
Chris Lattnerb61448d2009-05-12 18:21:11 +00001844 // #pragma GCC ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001845 AddPragmaHandler("GCC", new PragmaPoisonHandler());
1846 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1847 AddPragmaHandler("GCC", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001848 AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
Andy Gibbs9c2ccd62013-04-17 16:16:16 +00001849 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1850 "GCC"));
1851 AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1852 "GCC"));
Chris Lattnerb61448d2009-05-12 18:21:11 +00001853 // #pragma clang ...
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001854 AddPragmaHandler("clang", new PragmaPoisonHandler());
1855 AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
Daniel Dunbarb8068c32010-07-28 15:40:33 +00001856 AddPragmaHandler("clang", new PragmaDebugHandler());
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +00001857 AddPragmaHandler("clang", new PragmaDependencyHandler());
Douglas Gregor3bde9b12011-06-22 19:41:48 +00001858 AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
John McCall32f5fe12011-09-30 05:12:12 +00001859 AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001860 AddPragmaHandler("clang", new PragmaAssumeNonNullHandler());
Chris Lattnerb61448d2009-05-12 18:21:11 +00001861
Richard Smithc51c38b2017-04-29 00:34:47 +00001862 // #pragma clang module ...
1863 auto *ModuleHandler = new PragmaNamespace("module");
1864 AddPragmaHandler("clang", ModuleHandler);
1865 ModuleHandler->AddPragma(new PragmaModuleImportHandler());
Richard Smithd1386302017-05-04 00:29:54 +00001866 ModuleHandler->AddPragma(new PragmaModuleBeginHandler());
1867 ModuleHandler->AddPragma(new PragmaModuleEndHandler());
Richard Smith5d2ed482017-06-09 19:22:32 +00001868 ModuleHandler->AddPragma(new PragmaModuleBuildHandler());
1869 ModuleHandler->AddPragma(new PragmaModuleLoadHandler());
Fangrui Song6907ce22018-07-30 19:24:48 +00001870
Matt Davis1edb9052018-01-27 00:25:29 +00001871 // Add region pragmas.
1872 AddPragmaHandler(new PragmaRegionHandler("region"));
1873 AddPragmaHandler(new PragmaRegionHandler("endregion"));
Richard Smithc51c38b2017-04-29 00:34:47 +00001874
Chris Lattner2ff698d2009-01-16 08:21:25 +00001875 // MS extensions.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001876 if (LangOpts.MicrosoftExt) {
Reid Kleckner881dff32013-09-13 22:00:30 +00001877 AddPragmaHandler(new PragmaWarningHandler());
Reid Kleckner0f56b222019-03-14 18:12:17 +00001878 AddPragmaHandler(new PragmaExecCharsetHandler());
Aaron Ballman611306e2012-03-02 22:51:54 +00001879 AddPragmaHandler(new PragmaIncludeAliasHandler());
Mike Rice58df1af2018-09-11 17:10:44 +00001880 AddPragmaHandler(new PragmaHdrstopHandler());
Chris Lattner30c924b2010-06-26 17:11:39 +00001881 }
John Brawn8e62db32016-04-04 14:22:58 +00001882
1883 // Pragmas added by plugins
1884 for (PragmaHandlerRegistry::iterator it = PragmaHandlerRegistry::begin(),
1885 ie = PragmaHandlerRegistry::end();
1886 it != ie; ++it) {
1887 AddPragmaHandler(it->instantiate().release());
1888 }
Chris Lattnerb694ba72006-07-02 22:41:36 +00001889}
Lubos Lunak576a0412014-05-01 12:54:03 +00001890
1891/// Ignore all pragmas, useful for modes such as -Eonly which would otherwise
1892/// warn about those pragmas being unknown.
1893void Preprocessor::IgnorePragmas() {
1894 AddPragmaHandler(new EmptyPragmaHandler());
1895 // Also ignore all pragmas in all namespaces created
1896 // in Preprocessor::RegisterBuiltinPragmas().
1897 AddPragmaHandler("GCC", new EmptyPragmaHandler());
1898 AddPragmaHandler("clang", new EmptyPragmaHandler());
Lubos Lunak576a0412014-05-01 12:54:03 +00001899}