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