File: | clang/lib/Lex/Pragma.cpp |
Warning: | line 1182, column 7 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- Pragma.cpp - Pragma registration and handling ----------------------===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | // This file implements the PragmaHandler/PragmaTable interfaces and implements | |||
10 | // pragma related methods of the Preprocessor class. | |||
11 | // | |||
12 | //===----------------------------------------------------------------------===// | |||
13 | ||||
14 | #include "clang/Lex/Pragma.h" | |||
15 | #include "clang/Basic/Diagnostic.h" | |||
16 | #include "clang/Basic/DiagnosticLex.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/Optional.h" | |||
40 | #include "llvm/ADT/STLExtras.h" | |||
41 | #include "llvm/ADT/SmallString.h" | |||
42 | #include "llvm/ADT/SmallVector.h" | |||
43 | #include "llvm/ADT/StringRef.h" | |||
44 | #include "llvm/ADT/StringSwitch.h" | |||
45 | #include "llvm/Support/Compiler.h" | |||
46 | #include "llvm/Support/ErrorHandling.h" | |||
47 | #include "llvm/Support/Timer.h" | |||
48 | #include <algorithm> | |||
49 | #include <cassert> | |||
50 | #include <cstddef> | |||
51 | #include <cstdint> | |||
52 | #include <limits> | |||
53 | #include <string> | |||
54 | #include <utility> | |||
55 | #include <vector> | |||
56 | ||||
57 | using namespace clang; | |||
58 | ||||
59 | // Out-of-line destructor to provide a home for the class. | |||
60 | PragmaHandler::~PragmaHandler() = default; | |||
61 | ||||
62 | //===----------------------------------------------------------------------===// | |||
63 | // EmptyPragmaHandler Implementation. | |||
64 | //===----------------------------------------------------------------------===// | |||
65 | ||||
66 | EmptyPragmaHandler::EmptyPragmaHandler(StringRef Name) : PragmaHandler(Name) {} | |||
67 | ||||
68 | void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, | |||
69 | PragmaIntroducer Introducer, | |||
70 | Token &FirstToken) {} | |||
71 | ||||
72 | //===----------------------------------------------------------------------===// | |||
73 | // PragmaNamespace Implementation. | |||
74 | //===----------------------------------------------------------------------===// | |||
75 | ||||
76 | /// FindHandler - Check to see if there is already a handler for the | |||
77 | /// specified name. If not, return the handler for the null identifier if it | |||
78 | /// exists, otherwise return null. If IgnoreNull is true (the default) then | |||
79 | /// the null handler isn't returned on failure to match. | |||
80 | PragmaHandler *PragmaNamespace::FindHandler(StringRef Name, | |||
81 | bool IgnoreNull) const { | |||
82 | auto I = Handlers.find(Name); | |||
83 | if (I != Handlers.end()) | |||
84 | return I->getValue().get(); | |||
85 | if (IgnoreNull) | |||
86 | return nullptr; | |||
87 | I = Handlers.find(StringRef()); | |||
88 | if (I != Handlers.end()) | |||
89 | return I->getValue().get(); | |||
90 | return nullptr; | |||
91 | } | |||
92 | ||||
93 | void PragmaNamespace::AddPragma(PragmaHandler *Handler) { | |||
94 | assert(!Handlers.count(Handler->getName()) &&(static_cast <bool> (!Handlers.count(Handler->getName ()) && "A handler with this name is already registered in this namespace" ) ? void (0) : __assert_fail ("!Handlers.count(Handler->getName()) && \"A handler with this name is already registered in this namespace\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 95, __extension__ __PRETTY_FUNCTION__)) | |||
95 | "A handler with this name is already registered in this namespace")(static_cast <bool> (!Handlers.count(Handler->getName ()) && "A handler with this name is already registered in this namespace" ) ? void (0) : __assert_fail ("!Handlers.count(Handler->getName()) && \"A handler with this name is already registered in this namespace\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 95, __extension__ __PRETTY_FUNCTION__)); | |||
96 | Handlers[Handler->getName()].reset(Handler); | |||
97 | } | |||
98 | ||||
99 | void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) { | |||
100 | auto I = Handlers.find(Handler->getName()); | |||
101 | assert(I != Handlers.end() &&(static_cast <bool> (I != Handlers.end() && "Handler not registered in this namespace" ) ? void (0) : __assert_fail ("I != Handlers.end() && \"Handler not registered in this namespace\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 102, __extension__ __PRETTY_FUNCTION__)) | |||
102 | "Handler not registered in this namespace")(static_cast <bool> (I != Handlers.end() && "Handler not registered in this namespace" ) ? void (0) : __assert_fail ("I != Handlers.end() && \"Handler not registered in this namespace\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 102, __extension__ __PRETTY_FUNCTION__)); | |||
103 | // Release ownership back to the caller. | |||
104 | I->getValue().release(); | |||
105 | Handlers.erase(I); | |||
106 | } | |||
107 | ||||
108 | void PragmaNamespace::HandlePragma(Preprocessor &PP, | |||
109 | PragmaIntroducer Introducer, Token &Tok) { | |||
110 | // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro | |||
111 | // expand it, the user can have a STDC #define, that should not affect this. | |||
112 | PP.LexUnexpandedToken(Tok); | |||
113 | ||||
114 | // Get the handler for this token. If there is no handler, ignore the pragma. | |||
115 | PragmaHandler *Handler | |||
116 | = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName() | |||
117 | : StringRef(), | |||
118 | /*IgnoreNull=*/false); | |||
119 | if (!Handler) { | |||
120 | PP.Diag(Tok, diag::warn_pragma_ignored); | |||
121 | return; | |||
122 | } | |||
123 | ||||
124 | // Otherwise, pass it down. | |||
125 | Handler->HandlePragma(PP, Introducer, Tok); | |||
126 | } | |||
127 | ||||
128 | //===----------------------------------------------------------------------===// | |||
129 | // Preprocessor Pragma Directive Handling. | |||
130 | //===----------------------------------------------------------------------===// | |||
131 | ||||
132 | namespace { | |||
133 | // TokenCollector provides the option to collect tokens that were "read" | |||
134 | // and return them to the stream to be read later. | |||
135 | // Currently used when reading _Pragma/__pragma directives. | |||
136 | struct TokenCollector { | |||
137 | Preprocessor &Self; | |||
138 | bool Collect; | |||
139 | SmallVector<Token, 3> Tokens; | |||
140 | Token &Tok; | |||
141 | ||||
142 | void lex() { | |||
143 | if (Collect) | |||
144 | Tokens.push_back(Tok); | |||
145 | Self.Lex(Tok); | |||
146 | } | |||
147 | ||||
148 | void revert() { | |||
149 | assert(Collect && "did not collect tokens")(static_cast <bool> (Collect && "did not collect tokens" ) ? void (0) : __assert_fail ("Collect && \"did not collect tokens\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 149, __extension__ __PRETTY_FUNCTION__)); | |||
150 | assert(!Tokens.empty() && "collected unexpected number of tokens")(static_cast <bool> (!Tokens.empty() && "collected unexpected number of tokens" ) ? void (0) : __assert_fail ("!Tokens.empty() && \"collected unexpected number of tokens\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 150, __extension__ __PRETTY_FUNCTION__)); | |||
151 | ||||
152 | // Push the ( "string" ) tokens into the token stream. | |||
153 | auto Toks = std::make_unique<Token[]>(Tokens.size()); | |||
154 | std::copy(Tokens.begin() + 1, Tokens.end(), Toks.get()); | |||
155 | Toks[Tokens.size() - 1] = Tok; | |||
156 | Self.EnterTokenStream(std::move(Toks), Tokens.size(), | |||
157 | /*DisableMacroExpansion*/ true, | |||
158 | /*IsReinject*/ true); | |||
159 | ||||
160 | // ... and return the pragma token unchanged. | |||
161 | Tok = *Tokens.begin(); | |||
162 | } | |||
163 | }; | |||
164 | } // namespace | |||
165 | ||||
166 | /// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the | |||
167 | /// rest of the pragma, passing it to the registered pragma handlers. | |||
168 | void Preprocessor::HandlePragmaDirective(PragmaIntroducer Introducer) { | |||
169 | if (Callbacks) | |||
170 | Callbacks->PragmaDirective(Introducer.Loc, Introducer.Kind); | |||
171 | ||||
172 | if (!PragmasEnabled) | |||
173 | return; | |||
174 | ||||
175 | ++NumPragma; | |||
176 | ||||
177 | // Invoke the first level of pragma handlers which reads the namespace id. | |||
178 | Token Tok; | |||
179 | PragmaHandlers->HandlePragma(*this, Introducer, Tok); | |||
180 | ||||
181 | // If the pragma handler didn't read the rest of the line, consume it now. | |||
182 | if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective()) | |||
183 | || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)) | |||
184 | DiscardUntilEndOfDirective(); | |||
185 | } | |||
186 | ||||
187 | /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then | |||
188 | /// return the first token after the directive. The _Pragma token has just | |||
189 | /// been read into 'Tok'. | |||
190 | void Preprocessor::Handle_Pragma(Token &Tok) { | |||
191 | // C11 6.10.3.4/3: | |||
192 | // all pragma unary operator expressions within [a completely | |||
193 | // macro-replaced preprocessing token sequence] are [...] processed [after | |||
194 | // rescanning is complete] | |||
195 | // | |||
196 | // This means that we execute _Pragma operators in two cases: | |||
197 | // | |||
198 | // 1) on token sequences that would otherwise be produced as the output of | |||
199 | // phase 4 of preprocessing, and | |||
200 | // 2) on token sequences formed as the macro-replaced token sequence of a | |||
201 | // macro argument | |||
202 | // | |||
203 | // Case #2 appears to be a wording bug: only _Pragmas that would survive to | |||
204 | // the end of phase 4 should actually be executed. Discussion on the WG14 | |||
205 | // mailing list suggests that a _Pragma operator is notionally checked early, | |||
206 | // but only pragmas that survive to the end of phase 4 should be executed. | |||
207 | // | |||
208 | // In Case #2, we check the syntax now, but then put the tokens back into the | |||
209 | // token stream for later consumption. | |||
210 | ||||
211 | TokenCollector Toks = {*this, InMacroArgPreExpansion, {}, Tok}; | |||
212 | ||||
213 | // Remember the pragma token location. | |||
214 | SourceLocation PragmaLoc = Tok.getLocation(); | |||
215 | ||||
216 | // Read the '('. | |||
217 | Toks.lex(); | |||
218 | if (Tok.isNot(tok::l_paren)) { | |||
219 | Diag(PragmaLoc, diag::err__Pragma_malformed); | |||
220 | return; | |||
221 | } | |||
222 | ||||
223 | // Read the '"..."'. | |||
224 | Toks.lex(); | |||
225 | if (!tok::isStringLiteral(Tok.getKind())) { | |||
226 | Diag(PragmaLoc, diag::err__Pragma_malformed); | |||
227 | // Skip bad tokens, and the ')', if present. | |||
228 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof)) | |||
229 | Lex(Tok); | |||
230 | while (Tok.isNot(tok::r_paren) && | |||
231 | !Tok.isAtStartOfLine() && | |||
232 | Tok.isNot(tok::eof)) | |||
233 | Lex(Tok); | |||
234 | if (Tok.is(tok::r_paren)) | |||
235 | Lex(Tok); | |||
236 | return; | |||
237 | } | |||
238 | ||||
239 | if (Tok.hasUDSuffix()) { | |||
240 | Diag(Tok, diag::err_invalid_string_udl); | |||
241 | // Skip this token, and the ')', if present. | |||
242 | Lex(Tok); | |||
243 | if (Tok.is(tok::r_paren)) | |||
244 | Lex(Tok); | |||
245 | return; | |||
246 | } | |||
247 | ||||
248 | // Remember the string. | |||
249 | Token StrTok = Tok; | |||
250 | ||||
251 | // Read the ')'. | |||
252 | Toks.lex(); | |||
253 | if (Tok.isNot(tok::r_paren)) { | |||
254 | Diag(PragmaLoc, diag::err__Pragma_malformed); | |||
255 | return; | |||
256 | } | |||
257 | ||||
258 | // If we're expanding a macro argument, put the tokens back. | |||
259 | if (InMacroArgPreExpansion) { | |||
260 | Toks.revert(); | |||
261 | return; | |||
262 | } | |||
263 | ||||
264 | SourceLocation RParenLoc = Tok.getLocation(); | |||
265 | std::string StrVal = getSpelling(StrTok); | |||
266 | ||||
267 | // The _Pragma is lexically sound. Destringize according to C11 6.10.9.1: | |||
268 | // "The string literal is destringized by deleting any encoding prefix, | |||
269 | // deleting the leading and trailing double-quotes, replacing each escape | |||
270 | // sequence \" by a double-quote, and replacing each escape sequence \\ by a | |||
271 | // single backslash." | |||
272 | if (StrVal[0] == 'L' || StrVal[0] == 'U' || | |||
273 | (StrVal[0] == 'u' && StrVal[1] != '8')) | |||
274 | StrVal.erase(StrVal.begin()); | |||
275 | else if (StrVal[0] == 'u') | |||
276 | StrVal.erase(StrVal.begin(), StrVal.begin() + 2); | |||
277 | ||||
278 | if (StrVal[0] == 'R') { | |||
279 | // FIXME: C++11 does not specify how to handle raw-string-literals here. | |||
280 | // We strip off the 'R', the quotes, the d-char-sequences, and the parens. | |||
281 | assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' &&(static_cast <bool> (StrVal[1] == '"' && StrVal [StrVal.size() - 1] == '"' && "Invalid raw string token!" ) ? void (0) : __assert_fail ("StrVal[1] == '\"' && StrVal[StrVal.size() - 1] == '\"' && \"Invalid raw string token!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 282, __extension__ __PRETTY_FUNCTION__)) | |||
282 | "Invalid raw string token!")(static_cast <bool> (StrVal[1] == '"' && StrVal [StrVal.size() - 1] == '"' && "Invalid raw string token!" ) ? void (0) : __assert_fail ("StrVal[1] == '\"' && StrVal[StrVal.size() - 1] == '\"' && \"Invalid raw string token!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 282, __extension__ __PRETTY_FUNCTION__)); | |||
283 | ||||
284 | // Measure the length of the d-char-sequence. | |||
285 | unsigned NumDChars = 0; | |||
286 | while (StrVal[2 + NumDChars] != '(') { | |||
287 | assert(NumDChars < (StrVal.size() - 5) / 2 &&(static_cast <bool> (NumDChars < (StrVal.size() - 5) / 2 && "Invalid raw string token!") ? void (0) : __assert_fail ("NumDChars < (StrVal.size() - 5) / 2 && \"Invalid raw string token!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 288, __extension__ __PRETTY_FUNCTION__)) | |||
288 | "Invalid raw string token!")(static_cast <bool> (NumDChars < (StrVal.size() - 5) / 2 && "Invalid raw string token!") ? void (0) : __assert_fail ("NumDChars < (StrVal.size() - 5) / 2 && \"Invalid raw string token!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 288, __extension__ __PRETTY_FUNCTION__)); | |||
289 | ++NumDChars; | |||
290 | } | |||
291 | assert(StrVal[StrVal.size() - 2 - NumDChars] == ')')(static_cast <bool> (StrVal[StrVal.size() - 2 - NumDChars ] == ')') ? void (0) : __assert_fail ("StrVal[StrVal.size() - 2 - NumDChars] == ')'" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 291, __extension__ __PRETTY_FUNCTION__)); | |||
292 | ||||
293 | // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the | |||
294 | // parens below. | |||
295 | StrVal.erase(0, 2 + NumDChars); | |||
296 | StrVal.erase(StrVal.size() - 1 - NumDChars); | |||
297 | } else { | |||
298 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&(static_cast <bool> (StrVal[0] == '"' && StrVal [StrVal.size()-1] == '"' && "Invalid string token!") ? void (0) : __assert_fail ("StrVal[0] == '\"' && StrVal[StrVal.size()-1] == '\"' && \"Invalid string token!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 299, __extension__ __PRETTY_FUNCTION__)) | |||
299 | "Invalid string token!")(static_cast <bool> (StrVal[0] == '"' && StrVal [StrVal.size()-1] == '"' && "Invalid string token!") ? void (0) : __assert_fail ("StrVal[0] == '\"' && StrVal[StrVal.size()-1] == '\"' && \"Invalid string token!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 299, __extension__ __PRETTY_FUNCTION__)); | |||
300 | ||||
301 | // Remove escaped quotes and escapes. | |||
302 | unsigned ResultPos = 1; | |||
303 | for (size_t i = 1, e = StrVal.size() - 1; i != e; ++i) { | |||
304 | // Skip escapes. \\ -> '\' and \" -> '"'. | |||
305 | if (StrVal[i] == '\\' && i + 1 < e && | |||
306 | (StrVal[i + 1] == '\\' || StrVal[i + 1] == '"')) | |||
307 | ++i; | |||
308 | StrVal[ResultPos++] = StrVal[i]; | |||
309 | } | |||
310 | StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1); | |||
311 | } | |||
312 | ||||
313 | // Remove the front quote, replacing it with a space, so that the pragma | |||
314 | // contents appear to have a space before them. | |||
315 | StrVal[0] = ' '; | |||
316 | ||||
317 | // Replace the terminating quote with a \n. | |||
318 | StrVal[StrVal.size()-1] = '\n'; | |||
319 | ||||
320 | // Plop the string (including the newline and trailing null) into a buffer | |||
321 | // where we can lex it. | |||
322 | Token TmpTok; | |||
323 | TmpTok.startToken(); | |||
324 | CreateString(StrVal, TmpTok); | |||
325 | SourceLocation TokLoc = TmpTok.getLocation(); | |||
326 | ||||
327 | // Make and enter a lexer object so that we lex and expand the tokens just | |||
328 | // like any others. | |||
329 | Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc, | |||
330 | StrVal.size(), *this); | |||
331 | ||||
332 | EnterSourceFileWithLexer(TL, nullptr); | |||
333 | ||||
334 | // With everything set up, lex this as a #pragma directive. | |||
335 | HandlePragmaDirective({PIK__Pragma, PragmaLoc}); | |||
336 | ||||
337 | // Finally, return whatever came after the pragma directive. | |||
338 | return Lex(Tok); | |||
339 | } | |||
340 | ||||
341 | /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text | |||
342 | /// is not enclosed within a string literal. | |||
343 | void Preprocessor::HandleMicrosoft__pragma(Token &Tok) { | |||
344 | // During macro pre-expansion, check the syntax now but put the tokens back | |||
345 | // into the token stream for later consumption. Same as Handle_Pragma. | |||
346 | TokenCollector Toks = {*this, InMacroArgPreExpansion, {}, Tok}; | |||
347 | ||||
348 | // Remember the pragma token location. | |||
349 | SourceLocation PragmaLoc = Tok.getLocation(); | |||
350 | ||||
351 | // Read the '('. | |||
352 | Toks.lex(); | |||
353 | if (Tok.isNot(tok::l_paren)) { | |||
354 | Diag(PragmaLoc, diag::err__Pragma_malformed); | |||
355 | return; | |||
356 | } | |||
357 | ||||
358 | // Get the tokens enclosed within the __pragma(), as well as the final ')'. | |||
359 | SmallVector<Token, 32> PragmaToks; | |||
360 | int NumParens = 0; | |||
361 | Toks.lex(); | |||
362 | while (Tok.isNot(tok::eof)) { | |||
363 | PragmaToks.push_back(Tok); | |||
364 | if (Tok.is(tok::l_paren)) | |||
365 | NumParens++; | |||
366 | else if (Tok.is(tok::r_paren) && NumParens-- == 0) | |||
367 | break; | |||
368 | Toks.lex(); | |||
369 | } | |||
370 | ||||
371 | if (Tok.is(tok::eof)) { | |||
372 | Diag(PragmaLoc, diag::err_unterminated___pragma); | |||
373 | return; | |||
374 | } | |||
375 | ||||
376 | // If we're expanding a macro argument, put the tokens back. | |||
377 | if (InMacroArgPreExpansion) { | |||
378 | Toks.revert(); | |||
379 | return; | |||
380 | } | |||
381 | ||||
382 | PragmaToks.front().setFlag(Token::LeadingSpace); | |||
383 | ||||
384 | // Replace the ')' with an EOD to mark the end of the pragma. | |||
385 | PragmaToks.back().setKind(tok::eod); | |||
386 | ||||
387 | Token *TokArray = new Token[PragmaToks.size()]; | |||
388 | std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray); | |||
389 | ||||
390 | // Push the tokens onto the stack. | |||
391 | EnterTokenStream(TokArray, PragmaToks.size(), true, true, | |||
392 | /*IsReinject*/ false); | |||
393 | ||||
394 | // With everything set up, lex this as a #pragma directive. | |||
395 | HandlePragmaDirective({PIK___pragma, PragmaLoc}); | |||
396 | ||||
397 | // Finally, return whatever came after the pragma directive. | |||
398 | return Lex(Tok); | |||
399 | } | |||
400 | ||||
401 | /// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'. | |||
402 | void Preprocessor::HandlePragmaOnce(Token &OnceTok) { | |||
403 | // Don't honor the 'once' when handling the primary source file, unless | |||
404 | // this is a prefix to a TU, which indicates we're generating a PCH file, or | |||
405 | // when the main file is a header (e.g. when -xc-header is provided on the | |||
406 | // commandline). | |||
407 | if (isInPrimaryFile() && TUKind != TU_Prefix && !getLangOpts().IsHeaderFile) { | |||
408 | Diag(OnceTok, diag::pp_pragma_once_in_main_file); | |||
409 | return; | |||
410 | } | |||
411 | ||||
412 | // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. | |||
413 | // Mark the file as a once-only file now. | |||
414 | HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry()); | |||
415 | } | |||
416 | ||||
417 | void Preprocessor::HandlePragmaMark(Token &MarkTok) { | |||
418 | assert(CurPPLexer && "No current lexer?")(static_cast <bool> (CurPPLexer && "No current lexer?" ) ? void (0) : __assert_fail ("CurPPLexer && \"No current lexer?\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 418, __extension__ __PRETTY_FUNCTION__)); | |||
419 | ||||
420 | SmallString<64> Buffer; | |||
421 | CurLexer->ReadToEndOfLine(&Buffer); | |||
422 | if (Callbacks) | |||
423 | Callbacks->PragmaMark(MarkTok.getLocation(), Buffer); | |||
424 | } | |||
425 | ||||
426 | /// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'. | |||
427 | void Preprocessor::HandlePragmaPoison() { | |||
428 | Token Tok; | |||
429 | ||||
430 | while (true) { | |||
431 | // Read the next token to poison. While doing this, pretend that we are | |||
432 | // skipping while reading the identifier to poison. | |||
433 | // This avoids errors on code like: | |||
434 | // #pragma GCC poison X | |||
435 | // #pragma GCC poison X | |||
436 | if (CurPPLexer) CurPPLexer->LexingRawMode = true; | |||
437 | LexUnexpandedToken(Tok); | |||
438 | if (CurPPLexer) CurPPLexer->LexingRawMode = false; | |||
439 | ||||
440 | // If we reached the end of line, we're done. | |||
441 | if (Tok.is(tok::eod)) return; | |||
442 | ||||
443 | // Can only poison identifiers. | |||
444 | if (Tok.isNot(tok::raw_identifier)) { | |||
445 | Diag(Tok, diag::err_pp_invalid_poison); | |||
446 | return; | |||
447 | } | |||
448 | ||||
449 | // Look up the identifier info for the token. We disabled identifier lookup | |||
450 | // by saying we're skipping contents, so we need to do this manually. | |||
451 | IdentifierInfo *II = LookUpIdentifierInfo(Tok); | |||
452 | ||||
453 | // Already poisoned. | |||
454 | if (II->isPoisoned()) continue; | |||
455 | ||||
456 | // If this is a macro identifier, emit a warning. | |||
457 | if (isMacroDefined(II)) | |||
458 | Diag(Tok, diag::pp_poisoning_existing_macro); | |||
459 | ||||
460 | // Finally, poison it! | |||
461 | II->setIsPoisoned(); | |||
462 | if (II->isFromAST()) | |||
463 | II->setChangedSinceDeserialization(); | |||
464 | } | |||
465 | } | |||
466 | ||||
467 | /// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know | |||
468 | /// that the whole directive has been parsed. | |||
469 | void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) { | |||
470 | if (isInPrimaryFile()) { | |||
471 | Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file); | |||
472 | return; | |||
473 | } | |||
474 | ||||
475 | // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. | |||
476 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); | |||
477 | ||||
478 | // Mark the file as a system header. | |||
479 | HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry()); | |||
480 | ||||
481 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation()); | |||
482 | if (PLoc.isInvalid()) | |||
483 | return; | |||
484 | ||||
485 | unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename()); | |||
486 | ||||
487 | // Notify the client, if desired, that we are in a new source file. | |||
488 | if (Callbacks) | |||
489 | Callbacks->FileChanged(SysHeaderTok.getLocation(), | |||
490 | PPCallbacks::SystemHeaderPragma, SrcMgr::C_System); | |||
491 | ||||
492 | // Emit a line marker. This will change any source locations from this point | |||
493 | // forward to realize they are in a system header. | |||
494 | // Create a line note with this information. | |||
495 | SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine() + 1, | |||
496 | FilenameID, /*IsEntry=*/false, /*IsExit=*/false, | |||
497 | SrcMgr::C_System); | |||
498 | } | |||
499 | ||||
500 | static llvm::Optional<Token> LexHeader(Preprocessor &PP, | |||
501 | Optional<FileEntryRef> &File, | |||
502 | bool SuppressIncludeNotFoundError) { | |||
503 | Token FilenameTok; | |||
504 | if (PP.LexHeaderName(FilenameTok, /*AllowConcatenation*/ false)) | |||
505 | return llvm::None; | |||
506 | ||||
507 | // If the next token wasn't a header-name, diagnose the error. | |||
508 | if (FilenameTok.isNot(tok::header_name)) { | |||
509 | PP.Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); | |||
510 | return llvm::None; | |||
511 | } | |||
512 | ||||
513 | // Reserve a buffer to get the spelling. | |||
514 | SmallString<128> FilenameBuffer; | |||
515 | bool Invalid = false; | |||
516 | StringRef Filename = PP.getSpelling(FilenameTok, FilenameBuffer, &Invalid); | |||
517 | if (Invalid) | |||
518 | return llvm::None; | |||
519 | ||||
520 | bool isAngled = | |||
521 | PP.GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); | |||
522 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an | |||
523 | // error. | |||
524 | if (Filename.empty()) | |||
525 | return llvm::None; | |||
526 | ||||
527 | // Search include directories for this file. | |||
528 | const DirectoryLookup *CurDir; | |||
529 | File = PP.LookupFile(FilenameTok.getLocation(), Filename, isAngled, nullptr, | |||
530 | nullptr, CurDir, nullptr, nullptr, nullptr, nullptr, | |||
531 | nullptr); | |||
532 | if (!File) { | |||
533 | if (!SuppressIncludeNotFoundError) | |||
534 | PP.Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; | |||
535 | return llvm::None; | |||
536 | } | |||
537 | ||||
538 | return FilenameTok; | |||
539 | } | |||
540 | ||||
541 | /// HandlePragmaIncludeInstead - Handle \#pragma clang include_instead(header). | |||
542 | void Preprocessor::HandlePragmaIncludeInstead(Token &Tok) { | |||
543 | // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. | |||
544 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); | |||
545 | ||||
546 | if (!SourceMgr.isInSystemHeader(Tok.getLocation())) { | |||
547 | Diag(Tok, diag::err_pragma_include_instead_not_sysheader); | |||
548 | return; | |||
549 | } | |||
550 | ||||
551 | Lex(Tok); | |||
552 | if (Tok.isNot(tok::l_paren)) { | |||
553 | Diag(Tok, diag::err_expected) << "("; | |||
554 | return; | |||
555 | } | |||
556 | ||||
557 | Optional<FileEntryRef> File; | |||
558 | llvm::Optional<Token> FilenameTok = | |||
559 | LexHeader(*this, File, SuppressIncludeNotFoundError); | |||
560 | if (!FilenameTok) | |||
561 | return; | |||
562 | ||||
563 | Lex(Tok); | |||
564 | if (Tok.isNot(tok::r_paren)) { | |||
565 | Diag(Tok, diag::err_expected) << ")"; | |||
566 | return; | |||
567 | } | |||
568 | ||||
569 | SmallString<128> FilenameBuffer; | |||
570 | StringRef Filename = getSpelling(*FilenameTok, FilenameBuffer); | |||
571 | HeaderInfo.AddFileAlias(TheLexer->getFileEntry(), Filename); | |||
572 | } | |||
573 | ||||
574 | /// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah. | |||
575 | void Preprocessor::HandlePragmaDependency(Token &DependencyTok) { | |||
576 | Optional<FileEntryRef> File; | |||
577 | llvm::Optional<Token> FilenameTok = | |||
578 | LexHeader(*this, File, SuppressIncludeNotFoundError); | |||
579 | if (!FilenameTok) | |||
580 | return; | |||
581 | ||||
582 | const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry(); | |||
583 | ||||
584 | // If this file is older than the file it depends on, emit a diagnostic. | |||
585 | if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { | |||
586 | // Lex tokens at the end of the message and include them in the message. | |||
587 | std::string Message; | |||
588 | Lex(DependencyTok); | |||
589 | while (DependencyTok.isNot(tok::eod)) { | |||
590 | Message += getSpelling(DependencyTok) + " "; | |||
591 | Lex(DependencyTok); | |||
592 | } | |||
593 | ||||
594 | // Remove the trailing ' ' if present. | |||
595 | if (!Message.empty()) | |||
596 | Message.erase(Message.end()-1); | |||
597 | Diag(*FilenameTok, diag::pp_out_of_date_dependency) << Message; | |||
598 | } | |||
599 | } | |||
600 | ||||
601 | /// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro. | |||
602 | /// Return the IdentifierInfo* associated with the macro to push or pop. | |||
603 | IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) { | |||
604 | // Remember the pragma token location. | |||
605 | Token PragmaTok = Tok; | |||
606 | ||||
607 | // Read the '('. | |||
608 | Lex(Tok); | |||
609 | if (Tok.isNot(tok::l_paren)) { | |||
610 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) | |||
611 | << getSpelling(PragmaTok); | |||
612 | return nullptr; | |||
613 | } | |||
614 | ||||
615 | // Read the macro name string. | |||
616 | Lex(Tok); | |||
617 | if (Tok.isNot(tok::string_literal)) { | |||
618 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) | |||
619 | << getSpelling(PragmaTok); | |||
620 | return nullptr; | |||
621 | } | |||
622 | ||||
623 | if (Tok.hasUDSuffix()) { | |||
624 | Diag(Tok, diag::err_invalid_string_udl); | |||
625 | return nullptr; | |||
626 | } | |||
627 | ||||
628 | // Remember the macro string. | |||
629 | std::string StrVal = getSpelling(Tok); | |||
630 | ||||
631 | // Read the ')'. | |||
632 | Lex(Tok); | |||
633 | if (Tok.isNot(tok::r_paren)) { | |||
634 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) | |||
635 | << getSpelling(PragmaTok); | |||
636 | return nullptr; | |||
637 | } | |||
638 | ||||
639 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&(static_cast <bool> (StrVal[0] == '"' && StrVal [StrVal.size()-1] == '"' && "Invalid string token!") ? void (0) : __assert_fail ("StrVal[0] == '\"' && StrVal[StrVal.size()-1] == '\"' && \"Invalid string token!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 640, __extension__ __PRETTY_FUNCTION__)) | |||
640 | "Invalid string token!")(static_cast <bool> (StrVal[0] == '"' && StrVal [StrVal.size()-1] == '"' && "Invalid string token!") ? void (0) : __assert_fail ("StrVal[0] == '\"' && StrVal[StrVal.size()-1] == '\"' && \"Invalid string token!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 640, __extension__ __PRETTY_FUNCTION__)); | |||
641 | ||||
642 | // Create a Token from the string. | |||
643 | Token MacroTok; | |||
644 | MacroTok.startToken(); | |||
645 | MacroTok.setKind(tok::raw_identifier); | |||
646 | CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok); | |||
647 | ||||
648 | // Get the IdentifierInfo of MacroToPushTok. | |||
649 | return LookUpIdentifierInfo(MacroTok); | |||
650 | } | |||
651 | ||||
652 | /// Handle \#pragma push_macro. | |||
653 | /// | |||
654 | /// The syntax is: | |||
655 | /// \code | |||
656 | /// #pragma push_macro("macro") | |||
657 | /// \endcode | |||
658 | void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) { | |||
659 | // Parse the pragma directive and get the macro IdentifierInfo*. | |||
660 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok); | |||
661 | if (!IdentInfo) return; | |||
662 | ||||
663 | // Get the MacroInfo associated with IdentInfo. | |||
664 | MacroInfo *MI = getMacroInfo(IdentInfo); | |||
665 | ||||
666 | if (MI) { | |||
667 | // Allow the original MacroInfo to be redefined later. | |||
668 | MI->setIsAllowRedefinitionsWithoutWarning(true); | |||
669 | } | |||
670 | ||||
671 | // Push the cloned MacroInfo so we can retrieve it later. | |||
672 | PragmaPushMacroInfo[IdentInfo].push_back(MI); | |||
673 | } | |||
674 | ||||
675 | /// Handle \#pragma pop_macro. | |||
676 | /// | |||
677 | /// The syntax is: | |||
678 | /// \code | |||
679 | /// #pragma pop_macro("macro") | |||
680 | /// \endcode | |||
681 | void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { | |||
682 | SourceLocation MessageLoc = PopMacroTok.getLocation(); | |||
683 | ||||
684 | // Parse the pragma directive and get the macro IdentifierInfo*. | |||
685 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok); | |||
686 | if (!IdentInfo) return; | |||
687 | ||||
688 | // Find the vector<MacroInfo*> associated with the macro. | |||
689 | llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>>::iterator iter = | |||
690 | PragmaPushMacroInfo.find(IdentInfo); | |||
691 | if (iter != PragmaPushMacroInfo.end()) { | |||
692 | // Forget the MacroInfo currently associated with IdentInfo. | |||
693 | if (MacroInfo *MI = getMacroInfo(IdentInfo)) { | |||
694 | if (MI->isWarnIfUnused()) | |||
695 | WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); | |||
696 | appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc)); | |||
697 | } | |||
698 | ||||
699 | // Get the MacroInfo we want to reinstall. | |||
700 | MacroInfo *MacroToReInstall = iter->second.back(); | |||
701 | ||||
702 | if (MacroToReInstall) | |||
703 | // Reinstall the previously pushed macro. | |||
704 | appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc); | |||
705 | ||||
706 | // Pop PragmaPushMacroInfo stack. | |||
707 | iter->second.pop_back(); | |||
708 | if (iter->second.empty()) | |||
709 | PragmaPushMacroInfo.erase(iter); | |||
710 | } else { | |||
711 | Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push) | |||
712 | << IdentInfo->getName(); | |||
713 | } | |||
714 | } | |||
715 | ||||
716 | void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) { | |||
717 | // We will either get a quoted filename or a bracketed filename, and we | |||
718 | // have to track which we got. The first filename is the source name, | |||
719 | // and the second name is the mapped filename. If the first is quoted, | |||
720 | // the second must be as well (cannot mix and match quotes and brackets). | |||
721 | ||||
722 | // Get the open paren | |||
723 | Lex(Tok); | |||
724 | if (Tok.isNot(tok::l_paren)) { | |||
725 | Diag(Tok, diag::warn_pragma_include_alias_expected) << "("; | |||
726 | return; | |||
727 | } | |||
728 | ||||
729 | // We expect either a quoted string literal, or a bracketed name | |||
730 | Token SourceFilenameTok; | |||
731 | if (LexHeaderName(SourceFilenameTok)) | |||
732 | return; | |||
733 | ||||
734 | StringRef SourceFileName; | |||
735 | SmallString<128> FileNameBuffer; | |||
736 | if (SourceFilenameTok.is(tok::header_name)) { | |||
737 | SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer); | |||
738 | } else { | |||
739 | Diag(Tok, diag::warn_pragma_include_alias_expected_filename); | |||
740 | return; | |||
741 | } | |||
742 | FileNameBuffer.clear(); | |||
743 | ||||
744 | // Now we expect a comma, followed by another include name | |||
745 | Lex(Tok); | |||
746 | if (Tok.isNot(tok::comma)) { | |||
747 | Diag(Tok, diag::warn_pragma_include_alias_expected) << ","; | |||
748 | return; | |||
749 | } | |||
750 | ||||
751 | Token ReplaceFilenameTok; | |||
752 | if (LexHeaderName(ReplaceFilenameTok)) | |||
753 | return; | |||
754 | ||||
755 | StringRef ReplaceFileName; | |||
756 | if (ReplaceFilenameTok.is(tok::header_name)) { | |||
757 | ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer); | |||
758 | } else { | |||
759 | Diag(Tok, diag::warn_pragma_include_alias_expected_filename); | |||
760 | return; | |||
761 | } | |||
762 | ||||
763 | // Finally, we expect the closing paren | |||
764 | Lex(Tok); | |||
765 | if (Tok.isNot(tok::r_paren)) { | |||
766 | Diag(Tok, diag::warn_pragma_include_alias_expected) << ")"; | |||
767 | return; | |||
768 | } | |||
769 | ||||
770 | // Now that we have the source and target filenames, we need to make sure | |||
771 | // they're both of the same type (angled vs non-angled) | |||
772 | StringRef OriginalSource = SourceFileName; | |||
773 | ||||
774 | bool SourceIsAngled = | |||
775 | GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(), | |||
776 | SourceFileName); | |||
777 | bool ReplaceIsAngled = | |||
778 | GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(), | |||
779 | ReplaceFileName); | |||
780 | if (!SourceFileName.empty() && !ReplaceFileName.empty() && | |||
781 | (SourceIsAngled != ReplaceIsAngled)) { | |||
782 | unsigned int DiagID; | |||
783 | if (SourceIsAngled) | |||
784 | DiagID = diag::warn_pragma_include_alias_mismatch_angle; | |||
785 | else | |||
786 | DiagID = diag::warn_pragma_include_alias_mismatch_quote; | |||
787 | ||||
788 | Diag(SourceFilenameTok.getLocation(), DiagID) | |||
789 | << SourceFileName | |||
790 | << ReplaceFileName; | |||
791 | ||||
792 | return; | |||
793 | } | |||
794 | ||||
795 | // Now we can let the include handler know about this mapping | |||
796 | getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName); | |||
797 | } | |||
798 | ||||
799 | // Lex a component of a module name: either an identifier or a string literal; | |||
800 | // for components that can be expressed both ways, the two forms are equivalent. | |||
801 | static bool LexModuleNameComponent( | |||
802 | Preprocessor &PP, Token &Tok, | |||
803 | std::pair<IdentifierInfo *, SourceLocation> &ModuleNameComponent, | |||
804 | bool First) { | |||
805 | PP.LexUnexpandedToken(Tok); | |||
806 | if (Tok.is(tok::string_literal) && !Tok.hasUDSuffix()) { | |||
807 | StringLiteralParser Literal(Tok, PP); | |||
808 | if (Literal.hadError) | |||
809 | return true; | |||
810 | ModuleNameComponent = std::make_pair( | |||
811 | PP.getIdentifierInfo(Literal.GetString()), Tok.getLocation()); | |||
812 | } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) { | |||
813 | ModuleNameComponent = | |||
814 | std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()); | |||
815 | } else { | |||
816 | PP.Diag(Tok.getLocation(), diag::err_pp_expected_module_name) << First; | |||
817 | return true; | |||
818 | } | |||
819 | return false; | |||
820 | } | |||
821 | ||||
822 | static bool LexModuleName( | |||
823 | Preprocessor &PP, Token &Tok, | |||
824 | llvm::SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>> | |||
825 | &ModuleName) { | |||
826 | while (true) { | |||
827 | std::pair<IdentifierInfo*, SourceLocation> NameComponent; | |||
828 | if (LexModuleNameComponent(PP, Tok, NameComponent, ModuleName.empty())) | |||
829 | return true; | |||
830 | ModuleName.push_back(NameComponent); | |||
831 | ||||
832 | PP.LexUnexpandedToken(Tok); | |||
833 | if (Tok.isNot(tok::period)) | |||
834 | return false; | |||
835 | } | |||
836 | } | |||
837 | ||||
838 | void Preprocessor::HandlePragmaModuleBuild(Token &Tok) { | |||
839 | SourceLocation Loc = Tok.getLocation(); | |||
840 | ||||
841 | std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc; | |||
842 | if (LexModuleNameComponent(*this, Tok, ModuleNameLoc, true)) | |||
843 | return; | |||
844 | IdentifierInfo *ModuleName = ModuleNameLoc.first; | |||
845 | ||||
846 | LexUnexpandedToken(Tok); | |||
847 | if (Tok.isNot(tok::eod)) { | |||
848 | Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; | |||
849 | DiscardUntilEndOfDirective(); | |||
850 | } | |||
851 | ||||
852 | CurLexer->LexingRawMode = true; | |||
853 | ||||
854 | auto TryConsumeIdentifier = [&](StringRef Ident) -> bool { | |||
855 | if (Tok.getKind() != tok::raw_identifier || | |||
856 | Tok.getRawIdentifier() != Ident) | |||
857 | return false; | |||
858 | CurLexer->Lex(Tok); | |||
859 | return true; | |||
860 | }; | |||
861 | ||||
862 | // Scan forward looking for the end of the module. | |||
863 | const char *Start = CurLexer->getBufferLocation(); | |||
864 | const char *End = nullptr; | |||
865 | unsigned NestingLevel = 1; | |||
866 | while (true) { | |||
867 | End = CurLexer->getBufferLocation(); | |||
868 | CurLexer->Lex(Tok); | |||
869 | ||||
870 | if (Tok.is(tok::eof)) { | |||
871 | Diag(Loc, diag::err_pp_module_build_missing_end); | |||
872 | break; | |||
873 | } | |||
874 | ||||
875 | if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) { | |||
876 | // Token was part of module; keep going. | |||
877 | continue; | |||
878 | } | |||
879 | ||||
880 | // We hit something directive-shaped; check to see if this is the end | |||
881 | // of the module build. | |||
882 | CurLexer->ParsingPreprocessorDirective = true; | |||
883 | CurLexer->Lex(Tok); | |||
884 | if (TryConsumeIdentifier("pragma") && TryConsumeIdentifier("clang") && | |||
885 | TryConsumeIdentifier("module")) { | |||
886 | if (TryConsumeIdentifier("build")) | |||
887 | // #pragma clang module build -> entering a nested module build. | |||
888 | ++NestingLevel; | |||
889 | else if (TryConsumeIdentifier("endbuild")) { | |||
890 | // #pragma clang module endbuild -> leaving a module build. | |||
891 | if (--NestingLevel == 0) | |||
892 | break; | |||
893 | } | |||
894 | // We should either be looking at the EOD or more of the current directive | |||
895 | // preceding the EOD. Either way we can ignore this token and keep going. | |||
896 | assert(Tok.getKind() != tok::eof && "missing EOD before EOF")(static_cast <bool> (Tok.getKind() != tok::eof && "missing EOD before EOF") ? void (0) : __assert_fail ("Tok.getKind() != tok::eof && \"missing EOD before EOF\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 896, __extension__ __PRETTY_FUNCTION__)); | |||
897 | } | |||
898 | } | |||
899 | ||||
900 | CurLexer->LexingRawMode = false; | |||
901 | ||||
902 | // Load the extracted text as a preprocessed module. | |||
903 | assert(CurLexer->getBuffer().begin() <= Start &&(static_cast <bool> (CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && "module source range not contained within same file buffer" ) ? void (0) : __assert_fail ("CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && \"module source range not contained within same file buffer\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 907, __extension__ __PRETTY_FUNCTION__)) | |||
904 | Start <= CurLexer->getBuffer().end() &&(static_cast <bool> (CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && "module source range not contained within same file buffer" ) ? void (0) : __assert_fail ("CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && \"module source range not contained within same file buffer\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 907, __extension__ __PRETTY_FUNCTION__)) | |||
905 | CurLexer->getBuffer().begin() <= End &&(static_cast <bool> (CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && "module source range not contained within same file buffer" ) ? void (0) : __assert_fail ("CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && \"module source range not contained within same file buffer\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 907, __extension__ __PRETTY_FUNCTION__)) | |||
906 | End <= CurLexer->getBuffer().end() &&(static_cast <bool> (CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && "module source range not contained within same file buffer" ) ? void (0) : __assert_fail ("CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && \"module source range not contained within same file buffer\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 907, __extension__ __PRETTY_FUNCTION__)) | |||
907 | "module source range not contained within same file buffer")(static_cast <bool> (CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && "module source range not contained within same file buffer" ) ? void (0) : __assert_fail ("CurLexer->getBuffer().begin() <= Start && Start <= CurLexer->getBuffer().end() && CurLexer->getBuffer().begin() <= End && End <= CurLexer->getBuffer().end() && \"module source range not contained within same file buffer\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 907, __extension__ __PRETTY_FUNCTION__)); | |||
908 | TheModuleLoader.createModuleFromSource(Loc, ModuleName->getName(), | |||
909 | StringRef(Start, End - Start)); | |||
910 | } | |||
911 | ||||
912 | void Preprocessor::HandlePragmaHdrstop(Token &Tok) { | |||
913 | Lex(Tok); | |||
914 | if (Tok.is(tok::l_paren)) { | |||
915 | Diag(Tok.getLocation(), diag::warn_pp_hdrstop_filename_ignored); | |||
916 | ||||
917 | std::string FileName; | |||
918 | if (!LexStringLiteral(Tok, FileName, "pragma hdrstop", false)) | |||
919 | return; | |||
920 | ||||
921 | if (Tok.isNot(tok::r_paren)) { | |||
922 | Diag(Tok, diag::err_expected) << tok::r_paren; | |||
923 | return; | |||
924 | } | |||
925 | Lex(Tok); | |||
926 | } | |||
927 | if (Tok.isNot(tok::eod)) | |||
928 | Diag(Tok.getLocation(), diag::ext_pp_extra_tokens_at_eol) | |||
929 | << "pragma hdrstop"; | |||
930 | ||||
931 | if (creatingPCHWithPragmaHdrStop() && | |||
932 | SourceMgr.isInMainFile(Tok.getLocation())) { | |||
933 | assert(CurLexer && "no lexer for #pragma hdrstop processing")(static_cast <bool> (CurLexer && "no lexer for #pragma hdrstop processing" ) ? void (0) : __assert_fail ("CurLexer && \"no lexer for #pragma hdrstop processing\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 933, __extension__ __PRETTY_FUNCTION__)); | |||
934 | Token &Result = Tok; | |||
935 | Result.startToken(); | |||
936 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof); | |||
937 | CurLexer->cutOffLexing(); | |||
938 | } | |||
939 | if (usingPCHWithPragmaHdrStop()) | |||
940 | SkippingUntilPragmaHdrStop = false; | |||
941 | } | |||
942 | ||||
943 | /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. | |||
944 | /// If 'Namespace' is non-null, then it is a token required to exist on the | |||
945 | /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". | |||
946 | void Preprocessor::AddPragmaHandler(StringRef Namespace, | |||
947 | PragmaHandler *Handler) { | |||
948 | PragmaNamespace *InsertNS = PragmaHandlers.get(); | |||
949 | ||||
950 | // If this is specified to be in a namespace, step down into it. | |||
951 | if (!Namespace.empty()) { | |||
952 | // If there is already a pragma handler with the name of this namespace, | |||
953 | // we either have an error (directive with the same name as a namespace) or | |||
954 | // we already have the namespace to insert into. | |||
955 | if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) { | |||
956 | InsertNS = Existing->getIfNamespace(); | |||
957 | assert(InsertNS != nullptr && "Cannot have a pragma namespace and pragma"(static_cast <bool> (InsertNS != nullptr && "Cannot have a pragma namespace and pragma" " handler with the same name!") ? void (0) : __assert_fail ( "InsertNS != nullptr && \"Cannot have a pragma namespace and pragma\" \" handler with the same name!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 958, __extension__ __PRETTY_FUNCTION__)) | |||
958 | " handler with the same name!")(static_cast <bool> (InsertNS != nullptr && "Cannot have a pragma namespace and pragma" " handler with the same name!") ? void (0) : __assert_fail ( "InsertNS != nullptr && \"Cannot have a pragma namespace and pragma\" \" handler with the same name!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 958, __extension__ __PRETTY_FUNCTION__)); | |||
959 | } else { | |||
960 | // Otherwise, this namespace doesn't exist yet, create and insert the | |||
961 | // handler for it. | |||
962 | InsertNS = new PragmaNamespace(Namespace); | |||
963 | PragmaHandlers->AddPragma(InsertNS); | |||
964 | } | |||
965 | } | |||
966 | ||||
967 | // Check to make sure we don't already have a pragma for this identifier. | |||
968 | assert(!InsertNS->FindHandler(Handler->getName()) &&(static_cast <bool> (!InsertNS->FindHandler(Handler-> getName()) && "Pragma handler already exists for this identifier!" ) ? void (0) : __assert_fail ("!InsertNS->FindHandler(Handler->getName()) && \"Pragma handler already exists for this identifier!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 969, __extension__ __PRETTY_FUNCTION__)) | |||
969 | "Pragma handler already exists for this identifier!")(static_cast <bool> (!InsertNS->FindHandler(Handler-> getName()) && "Pragma handler already exists for this identifier!" ) ? void (0) : __assert_fail ("!InsertNS->FindHandler(Handler->getName()) && \"Pragma handler already exists for this identifier!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 969, __extension__ __PRETTY_FUNCTION__)); | |||
970 | InsertNS->AddPragma(Handler); | |||
971 | } | |||
972 | ||||
973 | /// RemovePragmaHandler - Remove the specific pragma handler from the | |||
974 | /// preprocessor. If \arg Namespace is non-null, then it should be the | |||
975 | /// namespace that \arg Handler was added to. It is an error to remove | |||
976 | /// a handler that has not been registered. | |||
977 | void Preprocessor::RemovePragmaHandler(StringRef Namespace, | |||
978 | PragmaHandler *Handler) { | |||
979 | PragmaNamespace *NS = PragmaHandlers.get(); | |||
980 | ||||
981 | // If this is specified to be in a namespace, step down into it. | |||
982 | if (!Namespace.empty()) { | |||
983 | PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace); | |||
984 | assert(Existing && "Namespace containing handler does not exist!")(static_cast <bool> (Existing && "Namespace containing handler does not exist!" ) ? void (0) : __assert_fail ("Existing && \"Namespace containing handler does not exist!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 984, __extension__ __PRETTY_FUNCTION__)); | |||
985 | ||||
986 | NS = Existing->getIfNamespace(); | |||
987 | assert(NS && "Invalid namespace, registered as a regular pragma handler!")(static_cast <bool> (NS && "Invalid namespace, registered as a regular pragma handler!" ) ? void (0) : __assert_fail ("NS && \"Invalid namespace, registered as a regular pragma handler!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 987, __extension__ __PRETTY_FUNCTION__)); | |||
988 | } | |||
989 | ||||
990 | NS->RemovePragmaHandler(Handler); | |||
991 | ||||
992 | // If this is a non-default namespace and it is now empty, remove it. | |||
993 | if (NS != PragmaHandlers.get() && NS->IsEmpty()) { | |||
994 | PragmaHandlers->RemovePragmaHandler(NS); | |||
995 | delete NS; | |||
996 | } | |||
997 | } | |||
998 | ||||
999 | bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) { | |||
1000 | Token Tok; | |||
1001 | LexUnexpandedToken(Tok); | |||
1002 | ||||
1003 | if (Tok.isNot(tok::identifier)) { | |||
1004 | Diag(Tok, diag::ext_on_off_switch_syntax); | |||
1005 | return true; | |||
1006 | } | |||
1007 | IdentifierInfo *II = Tok.getIdentifierInfo(); | |||
1008 | if (II->isStr("ON")) | |||
1009 | Result = tok::OOS_ON; | |||
1010 | else if (II->isStr("OFF")) | |||
1011 | Result = tok::OOS_OFF; | |||
1012 | else if (II->isStr("DEFAULT")) | |||
1013 | Result = tok::OOS_DEFAULT; | |||
1014 | else { | |||
1015 | Diag(Tok, diag::ext_on_off_switch_syntax); | |||
1016 | return true; | |||
1017 | } | |||
1018 | ||||
1019 | // Verify that this is followed by EOD. | |||
1020 | LexUnexpandedToken(Tok); | |||
1021 | if (Tok.isNot(tok::eod)) | |||
1022 | Diag(Tok, diag::ext_pragma_syntax_eod); | |||
1023 | return false; | |||
1024 | } | |||
1025 | ||||
1026 | namespace { | |||
1027 | ||||
1028 | /// PragmaOnceHandler - "\#pragma once" marks the file as atomically included. | |||
1029 | struct PragmaOnceHandler : public PragmaHandler { | |||
1030 | PragmaOnceHandler() : PragmaHandler("once") {} | |||
1031 | ||||
1032 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1033 | Token &OnceTok) override { | |||
1034 | PP.CheckEndOfDirective("pragma once"); | |||
1035 | PP.HandlePragmaOnce(OnceTok); | |||
1036 | } | |||
1037 | }; | |||
1038 | ||||
1039 | /// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the | |||
1040 | /// rest of the line is not lexed. | |||
1041 | struct PragmaMarkHandler : public PragmaHandler { | |||
1042 | PragmaMarkHandler() : PragmaHandler("mark") {} | |||
1043 | ||||
1044 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1045 | Token &MarkTok) override { | |||
1046 | PP.HandlePragmaMark(MarkTok); | |||
1047 | } | |||
1048 | }; | |||
1049 | ||||
1050 | /// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable. | |||
1051 | struct PragmaPoisonHandler : public PragmaHandler { | |||
1052 | PragmaPoisonHandler() : PragmaHandler("poison") {} | |||
1053 | ||||
1054 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1055 | Token &PoisonTok) override { | |||
1056 | PP.HandlePragmaPoison(); | |||
1057 | } | |||
1058 | }; | |||
1059 | ||||
1060 | /// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file | |||
1061 | /// as a system header, which silences warnings in it. | |||
1062 | struct PragmaSystemHeaderHandler : public PragmaHandler { | |||
1063 | PragmaSystemHeaderHandler() : PragmaHandler("system_header") {} | |||
1064 | ||||
1065 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1066 | Token &SHToken) override { | |||
1067 | PP.HandlePragmaSystemHeader(SHToken); | |||
1068 | PP.CheckEndOfDirective("pragma"); | |||
1069 | } | |||
1070 | }; | |||
1071 | ||||
1072 | /// PragmaIncludeInsteadHandler - "\#pragma clang include_instead(header)" marks | |||
1073 | /// the current file as non-includable if the including header is not a system | |||
1074 | /// header. | |||
1075 | struct PragmaIncludeInsteadHandler : public PragmaHandler { | |||
1076 | PragmaIncludeInsteadHandler() : PragmaHandler("include_instead") {} | |||
1077 | ||||
1078 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1079 | Token &IIToken) override { | |||
1080 | PP.HandlePragmaIncludeInstead(IIToken); | |||
1081 | } | |||
1082 | }; | |||
1083 | ||||
1084 | struct PragmaDependencyHandler : public PragmaHandler { | |||
1085 | PragmaDependencyHandler() : PragmaHandler("dependency") {} | |||
1086 | ||||
1087 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1088 | Token &DepToken) override { | |||
1089 | PP.HandlePragmaDependency(DepToken); | |||
1090 | } | |||
1091 | }; | |||
1092 | ||||
1093 | struct PragmaDebugHandler : public PragmaHandler { | |||
1094 | PragmaDebugHandler() : PragmaHandler("__debug") {} | |||
1095 | ||||
1096 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1097 | Token &DebugToken) override { | |||
1098 | Token Tok; | |||
1099 | PP.LexUnexpandedToken(Tok); | |||
1100 | if (Tok.isNot(tok::identifier)) { | |||
| ||||
1101 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); | |||
1102 | return; | |||
1103 | } | |||
1104 | IdentifierInfo *II = Tok.getIdentifierInfo(); | |||
1105 | ||||
1106 | if (II->isStr("assert")) { | |||
1107 | if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) | |||
1108 | llvm_unreachable("This is an assertion!")::llvm::llvm_unreachable_internal("This is an assertion!", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 1108); | |||
1109 | } else if (II->isStr("crash")) { | |||
1110 | llvm::Timer T("crash", "pragma crash"); | |||
1111 | llvm::TimeRegion R(&T); | |||
1112 | if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) | |||
1113 | LLVM_BUILTIN_TRAP__builtin_trap(); | |||
1114 | } else if (II->isStr("parser_crash")) { | |||
1115 | if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) { | |||
1116 | Token Crasher; | |||
1117 | Crasher.startToken(); | |||
1118 | Crasher.setKind(tok::annot_pragma_parser_crash); | |||
1119 | Crasher.setAnnotationRange(SourceRange(Tok.getLocation())); | |||
1120 | PP.EnterToken(Crasher, /*IsReinject*/ false); | |||
1121 | } | |||
1122 | } else if (II->isStr("dump")) { | |||
1123 | Token Identifier; | |||
1124 | PP.LexUnexpandedToken(Identifier); | |||
1125 | if (auto *DumpII = Identifier.getIdentifierInfo()) { | |||
1126 | Token DumpAnnot; | |||
1127 | DumpAnnot.startToken(); | |||
1128 | DumpAnnot.setKind(tok::annot_pragma_dump); | |||
1129 | DumpAnnot.setAnnotationRange( | |||
1130 | SourceRange(Tok.getLocation(), Identifier.getLocation())); | |||
1131 | DumpAnnot.setAnnotationValue(DumpII); | |||
1132 | PP.DiscardUntilEndOfDirective(); | |||
1133 | PP.EnterToken(DumpAnnot, /*IsReinject*/false); | |||
1134 | } else { | |||
1135 | PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument) | |||
1136 | << II->getName(); | |||
1137 | } | |||
1138 | } else if (II->isStr("diag_mapping")) { | |||
1139 | Token DiagName; | |||
1140 | PP.LexUnexpandedToken(DiagName); | |||
1141 | if (DiagName.is(tok::eod)) | |||
1142 | PP.getDiagnostics().dump(); | |||
1143 | else if (DiagName.is(tok::string_literal) && !DiagName.hasUDSuffix()) { | |||
1144 | StringLiteralParser Literal(DiagName, PP); | |||
1145 | if (Literal.hadError) | |||
1146 | return; | |||
1147 | PP.getDiagnostics().dump(Literal.GetString()); | |||
1148 | } else { | |||
1149 | PP.Diag(DiagName, diag::warn_pragma_debug_missing_argument) | |||
1150 | << II->getName(); | |||
1151 | } | |||
1152 | } else if (II->isStr("llvm_fatal_error")) { | |||
1153 | if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) | |||
1154 | llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error"); | |||
1155 | } else if (II->isStr("llvm_unreachable")) { | |||
1156 | if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) | |||
1157 | llvm_unreachable("#pragma clang __debug llvm_unreachable")::llvm::llvm_unreachable_internal("#pragma clang __debug llvm_unreachable" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 1157); | |||
1158 | } else if (II->isStr("macro")) { | |||
1159 | Token MacroName; | |||
1160 | PP.LexUnexpandedToken(MacroName); | |||
1161 | auto *MacroII = MacroName.getIdentifierInfo(); | |||
1162 | if (MacroII) | |||
1163 | PP.dumpMacroInfo(MacroII); | |||
1164 | else | |||
1165 | PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument) | |||
1166 | << II->getName(); | |||
1167 | } else if (II->isStr("module_map")) { | |||
1168 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> | |||
1169 | ModuleName; | |||
1170 | if (LexModuleName(PP, Tok, ModuleName)) | |||
1171 | return; | |||
1172 | ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap(); | |||
1173 | Module *M = nullptr; | |||
1174 | for (auto IIAndLoc : ModuleName) { | |||
1175 | M = MM.lookupModuleQualified(IIAndLoc.first->getName(), M); | |||
1176 | if (!M) { | |||
1177 | PP.Diag(IIAndLoc.second, diag::warn_pragma_debug_unknown_module) | |||
1178 | << IIAndLoc.first; | |||
1179 | return; | |||
1180 | } | |||
1181 | } | |||
1182 | M->dump(); | |||
| ||||
1183 | } else if (II->isStr("overflow_stack")) { | |||
1184 | if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) | |||
1185 | DebugOverflowStack(); | |||
1186 | } else if (II->isStr("captured")) { | |||
1187 | HandleCaptured(PP); | |||
1188 | } else if (II->isStr("modules")) { | |||
1189 | struct ModuleVisitor { | |||
1190 | Preprocessor &PP; | |||
1191 | void visit(Module *M, bool VisibleOnly) { | |||
1192 | SourceLocation ImportLoc = PP.getModuleImportLoc(M); | |||
1193 | if (!VisibleOnly || ImportLoc.isValid()) { | |||
1194 | llvm::errs() << M->getFullModuleName() << " "; | |||
1195 | if (ImportLoc.isValid()) { | |||
1196 | llvm::errs() << M << " visible "; | |||
1197 | ImportLoc.print(llvm::errs(), PP.getSourceManager()); | |||
1198 | } | |||
1199 | llvm::errs() << "\n"; | |||
1200 | } | |||
1201 | for (Module *Sub : M->submodules()) { | |||
1202 | if (!VisibleOnly || ImportLoc.isInvalid() || Sub->IsExplicit) | |||
1203 | visit(Sub, VisibleOnly); | |||
1204 | } | |||
1205 | } | |||
1206 | void visitAll(bool VisibleOnly) { | |||
1207 | for (auto &NameAndMod : | |||
1208 | PP.getHeaderSearchInfo().getModuleMap().modules()) | |||
1209 | visit(NameAndMod.second, VisibleOnly); | |||
1210 | } | |||
1211 | } Visitor{PP}; | |||
1212 | ||||
1213 | Token Kind; | |||
1214 | PP.LexUnexpandedToken(Kind); | |||
1215 | auto *DumpII = Kind.getIdentifierInfo(); | |||
1216 | if (!DumpII) { | |||
1217 | PP.Diag(Kind, diag::warn_pragma_debug_missing_argument) | |||
1218 | << II->getName(); | |||
1219 | } else if (DumpII->isStr("all")) { | |||
1220 | Visitor.visitAll(false); | |||
1221 | } else if (DumpII->isStr("visible")) { | |||
1222 | Visitor.visitAll(true); | |||
1223 | } else if (DumpII->isStr("building")) { | |||
1224 | for (auto &Building : PP.getBuildingSubmodules()) { | |||
1225 | llvm::errs() << "in " << Building.M->getFullModuleName(); | |||
1226 | if (Building.ImportLoc.isValid()) { | |||
1227 | llvm::errs() << " imported "; | |||
1228 | if (Building.IsPragma) | |||
1229 | llvm::errs() << "via pragma "; | |||
1230 | llvm::errs() << "at "; | |||
1231 | Building.ImportLoc.print(llvm::errs(), PP.getSourceManager()); | |||
1232 | llvm::errs() << "\n"; | |||
1233 | } | |||
1234 | } | |||
1235 | } else { | |||
1236 | PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command) | |||
1237 | << DumpII->getName(); | |||
1238 | } | |||
1239 | } else { | |||
1240 | PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command) | |||
1241 | << II->getName(); | |||
1242 | } | |||
1243 | ||||
1244 | PPCallbacks *Callbacks = PP.getPPCallbacks(); | |||
1245 | if (Callbacks) | |||
1246 | Callbacks->PragmaDebug(Tok.getLocation(), II->getName()); | |||
1247 | } | |||
1248 | ||||
1249 | void HandleCaptured(Preprocessor &PP) { | |||
1250 | Token Tok; | |||
1251 | PP.LexUnexpandedToken(Tok); | |||
1252 | ||||
1253 | if (Tok.isNot(tok::eod)) { | |||
1254 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) | |||
1255 | << "pragma clang __debug captured"; | |||
1256 | return; | |||
1257 | } | |||
1258 | ||||
1259 | SourceLocation NameLoc = Tok.getLocation(); | |||
1260 | MutableArrayRef<Token> Toks( | |||
1261 | PP.getPreprocessorAllocator().Allocate<Token>(1), 1); | |||
1262 | Toks[0].startToken(); | |||
1263 | Toks[0].setKind(tok::annot_pragma_captured); | |||
1264 | Toks[0].setLocation(NameLoc); | |||
1265 | ||||
1266 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true, | |||
1267 | /*IsReinject=*/false); | |||
1268 | } | |||
1269 | ||||
1270 | // Disable MSVC warning about runtime stack overflow. | |||
1271 | #ifdef _MSC_VER | |||
1272 | #pragma warning(disable : 4717) | |||
1273 | #endif | |||
1274 | static void DebugOverflowStack(void (*P)() = nullptr) { | |||
1275 | void (*volatile Self)(void(*P)()) = DebugOverflowStack; | |||
1276 | Self(reinterpret_cast<void(*)()>(Self)); | |||
1277 | } | |||
1278 | #ifdef _MSC_VER | |||
1279 | #pragma warning(default : 4717) | |||
1280 | #endif | |||
1281 | }; | |||
1282 | ||||
1283 | /// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"' | |||
1284 | struct PragmaDiagnosticHandler : public PragmaHandler { | |||
1285 | private: | |||
1286 | const char *Namespace; | |||
1287 | ||||
1288 | public: | |||
1289 | explicit PragmaDiagnosticHandler(const char *NS) | |||
1290 | : PragmaHandler("diagnostic"), Namespace(NS) {} | |||
1291 | ||||
1292 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1293 | Token &DiagToken) override { | |||
1294 | SourceLocation DiagLoc = DiagToken.getLocation(); | |||
1295 | Token Tok; | |||
1296 | PP.LexUnexpandedToken(Tok); | |||
1297 | if (Tok.isNot(tok::identifier)) { | |||
1298 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); | |||
1299 | return; | |||
1300 | } | |||
1301 | IdentifierInfo *II = Tok.getIdentifierInfo(); | |||
1302 | PPCallbacks *Callbacks = PP.getPPCallbacks(); | |||
1303 | ||||
1304 | if (II->isStr("pop")) { | |||
1305 | if (!PP.getDiagnostics().popMappings(DiagLoc)) | |||
1306 | PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop); | |||
1307 | else if (Callbacks) | |||
1308 | Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace); | |||
1309 | return; | |||
1310 | } else if (II->isStr("push")) { | |||
1311 | PP.getDiagnostics().pushMappings(DiagLoc); | |||
1312 | if (Callbacks) | |||
1313 | Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace); | |||
1314 | return; | |||
1315 | } | |||
1316 | ||||
1317 | diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName()) | |||
1318 | .Case("ignored", diag::Severity::Ignored) | |||
1319 | .Case("warning", diag::Severity::Warning) | |||
1320 | .Case("error", diag::Severity::Error) | |||
1321 | .Case("fatal", diag::Severity::Fatal) | |||
1322 | .Default(diag::Severity()); | |||
1323 | ||||
1324 | if (SV == diag::Severity()) { | |||
1325 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); | |||
1326 | return; | |||
1327 | } | |||
1328 | ||||
1329 | PP.LexUnexpandedToken(Tok); | |||
1330 | SourceLocation StringLoc = Tok.getLocation(); | |||
1331 | ||||
1332 | std::string WarningName; | |||
1333 | if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic", | |||
1334 | /*AllowMacroExpansion=*/false)) | |||
1335 | return; | |||
1336 | ||||
1337 | if (Tok.isNot(tok::eod)) { | |||
1338 | PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); | |||
1339 | return; | |||
1340 | } | |||
1341 | ||||
1342 | if (WarningName.size() < 3 || WarningName[0] != '-' || | |||
1343 | (WarningName[1] != 'W' && WarningName[1] != 'R')) { | |||
1344 | PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option); | |||
1345 | return; | |||
1346 | } | |||
1347 | ||||
1348 | diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError | |||
1349 | : diag::Flavor::Remark; | |||
1350 | StringRef Group = StringRef(WarningName).substr(2); | |||
1351 | bool unknownDiag = false; | |||
1352 | if (Group == "everything") { | |||
1353 | // Special handling for pragma clang diagnostic ... "-Weverything". | |||
1354 | // There is no formal group named "everything", so there has to be a | |||
1355 | // special case for it. | |||
1356 | PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc); | |||
1357 | } else | |||
1358 | unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV, | |||
1359 | DiagLoc); | |||
1360 | if (unknownDiag) | |||
1361 | PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning) | |||
1362 | << WarningName; | |||
1363 | else if (Callbacks) | |||
1364 | Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName); | |||
1365 | } | |||
1366 | }; | |||
1367 | ||||
1368 | /// "\#pragma hdrstop [<header-name-string>]" | |||
1369 | struct PragmaHdrstopHandler : public PragmaHandler { | |||
1370 | PragmaHdrstopHandler() : PragmaHandler("hdrstop") {} | |||
1371 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1372 | Token &DepToken) override { | |||
1373 | PP.HandlePragmaHdrstop(DepToken); | |||
1374 | } | |||
1375 | }; | |||
1376 | ||||
1377 | /// "\#pragma warning(...)". MSVC's diagnostics do not map cleanly to clang's | |||
1378 | /// diagnostics, so we don't really implement this pragma. We parse it and | |||
1379 | /// ignore it to avoid -Wunknown-pragma warnings. | |||
1380 | struct PragmaWarningHandler : public PragmaHandler { | |||
1381 | PragmaWarningHandler() : PragmaHandler("warning") {} | |||
1382 | ||||
1383 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1384 | Token &Tok) override { | |||
1385 | // Parse things like: | |||
1386 | // warning(push, 1) | |||
1387 | // warning(pop) | |||
1388 | // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9) | |||
1389 | SourceLocation DiagLoc = Tok.getLocation(); | |||
1390 | PPCallbacks *Callbacks = PP.getPPCallbacks(); | |||
1391 | ||||
1392 | PP.Lex(Tok); | |||
1393 | if (Tok.isNot(tok::l_paren)) { | |||
1394 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << "("; | |||
1395 | return; | |||
1396 | } | |||
1397 | ||||
1398 | PP.Lex(Tok); | |||
1399 | IdentifierInfo *II = Tok.getIdentifierInfo(); | |||
1400 | ||||
1401 | if (II && II->isStr("push")) { | |||
1402 | // #pragma warning( push[ ,n ] ) | |||
1403 | int Level = -1; | |||
1404 | PP.Lex(Tok); | |||
1405 | if (Tok.is(tok::comma)) { | |||
1406 | PP.Lex(Tok); | |||
1407 | uint64_t Value; | |||
1408 | if (Tok.is(tok::numeric_constant) && | |||
1409 | PP.parseSimpleIntegerLiteral(Tok, Value)) | |||
1410 | Level = int(Value); | |||
1411 | if (Level < 0 || Level > 4) { | |||
1412 | PP.Diag(Tok, diag::warn_pragma_warning_push_level); | |||
1413 | return; | |||
1414 | } | |||
1415 | } | |||
1416 | if (Callbacks) | |||
1417 | Callbacks->PragmaWarningPush(DiagLoc, Level); | |||
1418 | } else if (II && II->isStr("pop")) { | |||
1419 | // #pragma warning( pop ) | |||
1420 | PP.Lex(Tok); | |||
1421 | if (Callbacks) | |||
1422 | Callbacks->PragmaWarningPop(DiagLoc); | |||
1423 | } else { | |||
1424 | // #pragma warning( warning-specifier : warning-number-list | |||
1425 | // [; warning-specifier : warning-number-list...] ) | |||
1426 | while (true) { | |||
1427 | II = Tok.getIdentifierInfo(); | |||
1428 | if (!II && !Tok.is(tok::numeric_constant)) { | |||
1429 | PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid); | |||
1430 | return; | |||
1431 | } | |||
1432 | ||||
1433 | // Figure out which warning specifier this is. | |||
1434 | bool SpecifierValid; | |||
1435 | StringRef Specifier; | |||
1436 | llvm::SmallString<1> SpecifierBuf; | |||
1437 | if (II) { | |||
1438 | Specifier = II->getName(); | |||
1439 | SpecifierValid = llvm::StringSwitch<bool>(Specifier) | |||
1440 | .Cases("default", "disable", "error", "once", | |||
1441 | "suppress", true) | |||
1442 | .Default(false); | |||
1443 | // If we read a correct specifier, snatch next token (that should be | |||
1444 | // ":", checked later). | |||
1445 | if (SpecifierValid) | |||
1446 | PP.Lex(Tok); | |||
1447 | } else { | |||
1448 | // Token is a numeric constant. It should be either 1, 2, 3 or 4. | |||
1449 | uint64_t Value; | |||
1450 | Specifier = PP.getSpelling(Tok, SpecifierBuf); | |||
1451 | if (PP.parseSimpleIntegerLiteral(Tok, Value)) { | |||
1452 | SpecifierValid = (Value >= 1) && (Value <= 4); | |||
1453 | } else | |||
1454 | SpecifierValid = false; | |||
1455 | // Next token already snatched by parseSimpleIntegerLiteral. | |||
1456 | } | |||
1457 | ||||
1458 | if (!SpecifierValid) { | |||
1459 | PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid); | |||
1460 | return; | |||
1461 | } | |||
1462 | if (Tok.isNot(tok::colon)) { | |||
1463 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":"; | |||
1464 | return; | |||
1465 | } | |||
1466 | ||||
1467 | // Collect the warning ids. | |||
1468 | SmallVector<int, 4> Ids; | |||
1469 | PP.Lex(Tok); | |||
1470 | while (Tok.is(tok::numeric_constant)) { | |||
1471 | uint64_t Value; | |||
1472 | if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 || | |||
1473 | Value > INT_MAX2147483647) { | |||
1474 | PP.Diag(Tok, diag::warn_pragma_warning_expected_number); | |||
1475 | return; | |||
1476 | } | |||
1477 | Ids.push_back(int(Value)); | |||
1478 | } | |||
1479 | if (Callbacks) | |||
1480 | Callbacks->PragmaWarning(DiagLoc, Specifier, Ids); | |||
1481 | ||||
1482 | // Parse the next specifier if there is a semicolon. | |||
1483 | if (Tok.isNot(tok::semi)) | |||
1484 | break; | |||
1485 | PP.Lex(Tok); | |||
1486 | } | |||
1487 | } | |||
1488 | ||||
1489 | if (Tok.isNot(tok::r_paren)) { | |||
1490 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")"; | |||
1491 | return; | |||
1492 | } | |||
1493 | ||||
1494 | PP.Lex(Tok); | |||
1495 | if (Tok.isNot(tok::eod)) | |||
1496 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning"; | |||
1497 | } | |||
1498 | }; | |||
1499 | ||||
1500 | /// "\#pragma execution_character_set(...)". MSVC supports this pragma only | |||
1501 | /// for "UTF-8". We parse it and ignore it if UTF-8 is provided and warn | |||
1502 | /// otherwise to avoid -Wunknown-pragma warnings. | |||
1503 | struct PragmaExecCharsetHandler : public PragmaHandler { | |||
1504 | PragmaExecCharsetHandler() : PragmaHandler("execution_character_set") {} | |||
1505 | ||||
1506 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1507 | Token &Tok) override { | |||
1508 | // Parse things like: | |||
1509 | // execution_character_set(push, "UTF-8") | |||
1510 | // execution_character_set(pop) | |||
1511 | SourceLocation DiagLoc = Tok.getLocation(); | |||
1512 | PPCallbacks *Callbacks = PP.getPPCallbacks(); | |||
1513 | ||||
1514 | PP.Lex(Tok); | |||
1515 | if (Tok.isNot(tok::l_paren)) { | |||
1516 | PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << "("; | |||
1517 | return; | |||
1518 | } | |||
1519 | ||||
1520 | PP.Lex(Tok); | |||
1521 | IdentifierInfo *II = Tok.getIdentifierInfo(); | |||
1522 | ||||
1523 | if (II && II->isStr("push")) { | |||
1524 | // #pragma execution_character_set( push[ , string ] ) | |||
1525 | PP.Lex(Tok); | |||
1526 | if (Tok.is(tok::comma)) { | |||
1527 | PP.Lex(Tok); | |||
1528 | ||||
1529 | std::string ExecCharset; | |||
1530 | if (!PP.FinishLexStringLiteral(Tok, ExecCharset, | |||
1531 | "pragma execution_character_set", | |||
1532 | /*AllowMacroExpansion=*/false)) | |||
1533 | return; | |||
1534 | ||||
1535 | // MSVC supports either of these, but nothing else. | |||
1536 | if (ExecCharset != "UTF-8" && ExecCharset != "utf-8") { | |||
1537 | PP.Diag(Tok, diag::warn_pragma_exec_charset_push_invalid) << ExecCharset; | |||
1538 | return; | |||
1539 | } | |||
1540 | } | |||
1541 | if (Callbacks) | |||
1542 | Callbacks->PragmaExecCharsetPush(DiagLoc, "UTF-8"); | |||
1543 | } else if (II && II->isStr("pop")) { | |||
1544 | // #pragma execution_character_set( pop ) | |||
1545 | PP.Lex(Tok); | |||
1546 | if (Callbacks) | |||
1547 | Callbacks->PragmaExecCharsetPop(DiagLoc); | |||
1548 | } else { | |||
1549 | PP.Diag(Tok, diag::warn_pragma_exec_charset_spec_invalid); | |||
1550 | return; | |||
1551 | } | |||
1552 | ||||
1553 | if (Tok.isNot(tok::r_paren)) { | |||
1554 | PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << ")"; | |||
1555 | return; | |||
1556 | } | |||
1557 | ||||
1558 | PP.Lex(Tok); | |||
1559 | if (Tok.isNot(tok::eod)) | |||
1560 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma execution_character_set"; | |||
1561 | } | |||
1562 | }; | |||
1563 | ||||
1564 | /// PragmaIncludeAliasHandler - "\#pragma include_alias("...")". | |||
1565 | struct PragmaIncludeAliasHandler : public PragmaHandler { | |||
1566 | PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {} | |||
1567 | ||||
1568 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1569 | Token &IncludeAliasTok) override { | |||
1570 | PP.HandlePragmaIncludeAlias(IncludeAliasTok); | |||
1571 | } | |||
1572 | }; | |||
1573 | ||||
1574 | /// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message | |||
1575 | /// extension. The syntax is: | |||
1576 | /// \code | |||
1577 | /// #pragma message(string) | |||
1578 | /// \endcode | |||
1579 | /// OR, in GCC mode: | |||
1580 | /// \code | |||
1581 | /// #pragma message string | |||
1582 | /// \endcode | |||
1583 | /// string is a string, which is fully macro expanded, and permits string | |||
1584 | /// concatenation, embedded escape characters, etc... See MSDN for more details. | |||
1585 | /// Also handles \#pragma GCC warning and \#pragma GCC error which take the same | |||
1586 | /// form as \#pragma message. | |||
1587 | struct PragmaMessageHandler : public PragmaHandler { | |||
1588 | private: | |||
1589 | const PPCallbacks::PragmaMessageKind Kind; | |||
1590 | const StringRef Namespace; | |||
1591 | ||||
1592 | static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind, | |||
1593 | bool PragmaNameOnly = false) { | |||
1594 | switch (Kind) { | |||
1595 | case PPCallbacks::PMK_Message: | |||
1596 | return PragmaNameOnly ? "message" : "pragma message"; | |||
1597 | case PPCallbacks::PMK_Warning: | |||
1598 | return PragmaNameOnly ? "warning" : "pragma warning"; | |||
1599 | case PPCallbacks::PMK_Error: | |||
1600 | return PragmaNameOnly ? "error" : "pragma error"; | |||
1601 | } | |||
1602 | llvm_unreachable("Unknown PragmaMessageKind!")::llvm::llvm_unreachable_internal("Unknown PragmaMessageKind!" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/lib/Lex/Pragma.cpp" , 1602); | |||
1603 | } | |||
1604 | ||||
1605 | public: | |||
1606 | PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind, | |||
1607 | StringRef Namespace = StringRef()) | |||
1608 | : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), | |||
1609 | Namespace(Namespace) {} | |||
1610 | ||||
1611 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1612 | Token &Tok) override { | |||
1613 | SourceLocation MessageLoc = Tok.getLocation(); | |||
1614 | PP.Lex(Tok); | |||
1615 | bool ExpectClosingParen = false; | |||
1616 | switch (Tok.getKind()) { | |||
1617 | case tok::l_paren: | |||
1618 | // We have a MSVC style pragma message. | |||
1619 | ExpectClosingParen = true; | |||
1620 | // Read the string. | |||
1621 | PP.Lex(Tok); | |||
1622 | break; | |||
1623 | case tok::string_literal: | |||
1624 | // We have a GCC style pragma message, and we just read the string. | |||
1625 | break; | |||
1626 | default: | |||
1627 | PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind; | |||
1628 | return; | |||
1629 | } | |||
1630 | ||||
1631 | std::string MessageString; | |||
1632 | if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind), | |||
1633 | /*AllowMacroExpansion=*/true)) | |||
1634 | return; | |||
1635 | ||||
1636 | if (ExpectClosingParen) { | |||
1637 | if (Tok.isNot(tok::r_paren)) { | |||
1638 | PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind; | |||
1639 | return; | |||
1640 | } | |||
1641 | PP.Lex(Tok); // eat the r_paren. | |||
1642 | } | |||
1643 | ||||
1644 | if (Tok.isNot(tok::eod)) { | |||
1645 | PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind; | |||
1646 | return; | |||
1647 | } | |||
1648 | ||||
1649 | // Output the message. | |||
1650 | PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error) | |||
1651 | ? diag::err_pragma_message | |||
1652 | : diag::warn_pragma_message) << MessageString; | |||
1653 | ||||
1654 | // If the pragma is lexically sound, notify any interested PPCallbacks. | |||
1655 | if (PPCallbacks *Callbacks = PP.getPPCallbacks()) | |||
1656 | Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString); | |||
1657 | } | |||
1658 | }; | |||
1659 | ||||
1660 | /// Handle the clang \#pragma module import extension. The syntax is: | |||
1661 | /// \code | |||
1662 | /// #pragma clang module import some.module.name | |||
1663 | /// \endcode | |||
1664 | struct PragmaModuleImportHandler : public PragmaHandler { | |||
1665 | PragmaModuleImportHandler() : PragmaHandler("import") {} | |||
1666 | ||||
1667 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1668 | Token &Tok) override { | |||
1669 | SourceLocation ImportLoc = Tok.getLocation(); | |||
1670 | ||||
1671 | // Read the module name. | |||
1672 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> | |||
1673 | ModuleName; | |||
1674 | if (LexModuleName(PP, Tok, ModuleName)) | |||
1675 | return; | |||
1676 | ||||
1677 | if (Tok.isNot(tok::eod)) | |||
1678 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; | |||
1679 | ||||
1680 | // If we have a non-empty module path, load the named module. | |||
1681 | Module *Imported = | |||
1682 | PP.getModuleLoader().loadModule(ImportLoc, ModuleName, Module::Hidden, | |||
1683 | /*IsInclusionDirective=*/false); | |||
1684 | if (!Imported) | |||
1685 | return; | |||
1686 | ||||
1687 | PP.makeModuleVisible(Imported, ImportLoc); | |||
1688 | PP.EnterAnnotationToken(SourceRange(ImportLoc, ModuleName.back().second), | |||
1689 | tok::annot_module_include, Imported); | |||
1690 | if (auto *CB = PP.getPPCallbacks()) | |||
1691 | CB->moduleImport(ImportLoc, ModuleName, Imported); | |||
1692 | } | |||
1693 | }; | |||
1694 | ||||
1695 | /// Handle the clang \#pragma module begin extension. The syntax is: | |||
1696 | /// \code | |||
1697 | /// #pragma clang module begin some.module.name | |||
1698 | /// ... | |||
1699 | /// #pragma clang module end | |||
1700 | /// \endcode | |||
1701 | struct PragmaModuleBeginHandler : public PragmaHandler { | |||
1702 | PragmaModuleBeginHandler() : PragmaHandler("begin") {} | |||
1703 | ||||
1704 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1705 | Token &Tok) override { | |||
1706 | SourceLocation BeginLoc = Tok.getLocation(); | |||
1707 | ||||
1708 | // Read the module name. | |||
1709 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> | |||
1710 | ModuleName; | |||
1711 | if (LexModuleName(PP, Tok, ModuleName)) | |||
1712 | return; | |||
1713 | ||||
1714 | if (Tok.isNot(tok::eod)) | |||
1715 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; | |||
1716 | ||||
1717 | // We can only enter submodules of the current module. | |||
1718 | StringRef Current = PP.getLangOpts().CurrentModule; | |||
1719 | if (ModuleName.front().first->getName() != Current) { | |||
1720 | PP.Diag(ModuleName.front().second, diag::err_pp_module_begin_wrong_module) | |||
1721 | << ModuleName.front().first << (ModuleName.size() > 1) | |||
1722 | << Current.empty() << Current; | |||
1723 | return; | |||
1724 | } | |||
1725 | ||||
1726 | // Find the module we're entering. We require that a module map for it | |||
1727 | // be loaded or implicitly loadable. | |||
1728 | auto &HSI = PP.getHeaderSearchInfo(); | |||
1729 | Module *M = HSI.lookupModule(Current); | |||
1730 | if (!M) { | |||
1731 | PP.Diag(ModuleName.front().second, | |||
1732 | diag::err_pp_module_begin_no_module_map) << Current; | |||
1733 | return; | |||
1734 | } | |||
1735 | for (unsigned I = 1; I != ModuleName.size(); ++I) { | |||
1736 | auto *NewM = M->findOrInferSubmodule(ModuleName[I].first->getName()); | |||
1737 | if (!NewM) { | |||
1738 | PP.Diag(ModuleName[I].second, diag::err_pp_module_begin_no_submodule) | |||
1739 | << M->getFullModuleName() << ModuleName[I].first; | |||
1740 | return; | |||
1741 | } | |||
1742 | M = NewM; | |||
1743 | } | |||
1744 | ||||
1745 | // If the module isn't available, it doesn't make sense to enter it. | |||
1746 | if (Preprocessor::checkModuleIsAvailable( | |||
1747 | PP.getLangOpts(), PP.getTargetInfo(), PP.getDiagnostics(), M)) { | |||
1748 | PP.Diag(BeginLoc, diag::note_pp_module_begin_here) | |||
1749 | << M->getTopLevelModuleName(); | |||
1750 | return; | |||
1751 | } | |||
1752 | ||||
1753 | // Enter the scope of the submodule. | |||
1754 | PP.EnterSubmodule(M, BeginLoc, /*ForPragma*/true); | |||
1755 | PP.EnterAnnotationToken(SourceRange(BeginLoc, ModuleName.back().second), | |||
1756 | tok::annot_module_begin, M); | |||
1757 | } | |||
1758 | }; | |||
1759 | ||||
1760 | /// Handle the clang \#pragma module end extension. | |||
1761 | struct PragmaModuleEndHandler : public PragmaHandler { | |||
1762 | PragmaModuleEndHandler() : PragmaHandler("end") {} | |||
1763 | ||||
1764 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1765 | Token &Tok) override { | |||
1766 | SourceLocation Loc = Tok.getLocation(); | |||
1767 | ||||
1768 | PP.LexUnexpandedToken(Tok); | |||
1769 | if (Tok.isNot(tok::eod)) | |||
1770 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; | |||
1771 | ||||
1772 | Module *M = PP.LeaveSubmodule(/*ForPragma*/true); | |||
1773 | if (M) | |||
1774 | PP.EnterAnnotationToken(SourceRange(Loc), tok::annot_module_end, M); | |||
1775 | else | |||
1776 | PP.Diag(Loc, diag::err_pp_module_end_without_module_begin); | |||
1777 | } | |||
1778 | }; | |||
1779 | ||||
1780 | /// Handle the clang \#pragma module build extension. | |||
1781 | struct PragmaModuleBuildHandler : public PragmaHandler { | |||
1782 | PragmaModuleBuildHandler() : PragmaHandler("build") {} | |||
1783 | ||||
1784 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1785 | Token &Tok) override { | |||
1786 | PP.HandlePragmaModuleBuild(Tok); | |||
1787 | } | |||
1788 | }; | |||
1789 | ||||
1790 | /// Handle the clang \#pragma module load extension. | |||
1791 | struct PragmaModuleLoadHandler : public PragmaHandler { | |||
1792 | PragmaModuleLoadHandler() : PragmaHandler("load") {} | |||
1793 | ||||
1794 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1795 | Token &Tok) override { | |||
1796 | SourceLocation Loc = Tok.getLocation(); | |||
1797 | ||||
1798 | // Read the module name. | |||
1799 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> | |||
1800 | ModuleName; | |||
1801 | if (LexModuleName(PP, Tok, ModuleName)) | |||
1802 | return; | |||
1803 | ||||
1804 | if (Tok.isNot(tok::eod)) | |||
1805 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; | |||
1806 | ||||
1807 | // Load the module, don't make it visible. | |||
1808 | PP.getModuleLoader().loadModule(Loc, ModuleName, Module::Hidden, | |||
1809 | /*IsInclusionDirective=*/false); | |||
1810 | } | |||
1811 | }; | |||
1812 | ||||
1813 | /// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the | |||
1814 | /// macro on the top of the stack. | |||
1815 | struct PragmaPushMacroHandler : public PragmaHandler { | |||
1816 | PragmaPushMacroHandler() : PragmaHandler("push_macro") {} | |||
1817 | ||||
1818 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1819 | Token &PushMacroTok) override { | |||
1820 | PP.HandlePragmaPushMacro(PushMacroTok); | |||
1821 | } | |||
1822 | }; | |||
1823 | ||||
1824 | /// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the | |||
1825 | /// macro to the value on the top of the stack. | |||
1826 | struct PragmaPopMacroHandler : public PragmaHandler { | |||
1827 | PragmaPopMacroHandler() : PragmaHandler("pop_macro") {} | |||
1828 | ||||
1829 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1830 | Token &PopMacroTok) override { | |||
1831 | PP.HandlePragmaPopMacro(PopMacroTok); | |||
1832 | } | |||
1833 | }; | |||
1834 | ||||
1835 | /// PragmaARCCFCodeAuditedHandler - | |||
1836 | /// \#pragma clang arc_cf_code_audited begin/end | |||
1837 | struct PragmaARCCFCodeAuditedHandler : public PragmaHandler { | |||
1838 | PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {} | |||
1839 | ||||
1840 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1841 | Token &NameTok) override { | |||
1842 | SourceLocation Loc = NameTok.getLocation(); | |||
1843 | bool IsBegin; | |||
1844 | ||||
1845 | Token Tok; | |||
1846 | ||||
1847 | // Lex the 'begin' or 'end'. | |||
1848 | PP.LexUnexpandedToken(Tok); | |||
1849 | const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); | |||
1850 | if (BeginEnd && BeginEnd->isStr("begin")) { | |||
1851 | IsBegin = true; | |||
1852 | } else if (BeginEnd && BeginEnd->isStr("end")) { | |||
1853 | IsBegin = false; | |||
1854 | } else { | |||
1855 | PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax); | |||
1856 | return; | |||
1857 | } | |||
1858 | ||||
1859 | // Verify that this is followed by EOD. | |||
1860 | PP.LexUnexpandedToken(Tok); | |||
1861 | if (Tok.isNot(tok::eod)) | |||
1862 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; | |||
1863 | ||||
1864 | // The start location of the active audit. | |||
1865 | SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedInfo().second; | |||
1866 | ||||
1867 | // The start location we want after processing this. | |||
1868 | SourceLocation NewLoc; | |||
1869 | ||||
1870 | if (IsBegin) { | |||
1871 | // Complain about attempts to re-enter an audit. | |||
1872 | if (BeginLoc.isValid()) { | |||
1873 | PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited); | |||
1874 | PP.Diag(BeginLoc, diag::note_pragma_entered_here); | |||
1875 | } | |||
1876 | NewLoc = Loc; | |||
1877 | } else { | |||
1878 | // Complain about attempts to leave an audit that doesn't exist. | |||
1879 | if (!BeginLoc.isValid()) { | |||
1880 | PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited); | |||
1881 | return; | |||
1882 | } | |||
1883 | NewLoc = SourceLocation(); | |||
1884 | } | |||
1885 | ||||
1886 | PP.setPragmaARCCFCodeAuditedInfo(NameTok.getIdentifierInfo(), NewLoc); | |||
1887 | } | |||
1888 | }; | |||
1889 | ||||
1890 | /// PragmaAssumeNonNullHandler - | |||
1891 | /// \#pragma clang assume_nonnull begin/end | |||
1892 | struct PragmaAssumeNonNullHandler : public PragmaHandler { | |||
1893 | PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {} | |||
1894 | ||||
1895 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1896 | Token &NameTok) override { | |||
1897 | SourceLocation Loc = NameTok.getLocation(); | |||
1898 | bool IsBegin; | |||
1899 | ||||
1900 | Token Tok; | |||
1901 | ||||
1902 | // Lex the 'begin' or 'end'. | |||
1903 | PP.LexUnexpandedToken(Tok); | |||
1904 | const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); | |||
1905 | if (BeginEnd && BeginEnd->isStr("begin")) { | |||
1906 | IsBegin = true; | |||
1907 | } else if (BeginEnd && BeginEnd->isStr("end")) { | |||
1908 | IsBegin = false; | |||
1909 | } else { | |||
1910 | PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax); | |||
1911 | return; | |||
1912 | } | |||
1913 | ||||
1914 | // Verify that this is followed by EOD. | |||
1915 | PP.LexUnexpandedToken(Tok); | |||
1916 | if (Tok.isNot(tok::eod)) | |||
1917 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; | |||
1918 | ||||
1919 | // The start location of the active audit. | |||
1920 | SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc(); | |||
1921 | ||||
1922 | // The start location we want after processing this. | |||
1923 | SourceLocation NewLoc; | |||
1924 | PPCallbacks *Callbacks = PP.getPPCallbacks(); | |||
1925 | ||||
1926 | if (IsBegin) { | |||
1927 | // Complain about attempts to re-enter an audit. | |||
1928 | if (BeginLoc.isValid()) { | |||
1929 | PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull); | |||
1930 | PP.Diag(BeginLoc, diag::note_pragma_entered_here); | |||
1931 | } | |||
1932 | NewLoc = Loc; | |||
1933 | if (Callbacks) | |||
1934 | Callbacks->PragmaAssumeNonNullBegin(NewLoc); | |||
1935 | } else { | |||
1936 | // Complain about attempts to leave an audit that doesn't exist. | |||
1937 | if (!BeginLoc.isValid()) { | |||
1938 | PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull); | |||
1939 | return; | |||
1940 | } | |||
1941 | NewLoc = SourceLocation(); | |||
1942 | if (Callbacks) | |||
1943 | Callbacks->PragmaAssumeNonNullEnd(NewLoc); | |||
1944 | } | |||
1945 | ||||
1946 | PP.setPragmaAssumeNonNullLoc(NewLoc); | |||
1947 | } | |||
1948 | }; | |||
1949 | ||||
1950 | /// Handle "\#pragma region [...]" | |||
1951 | /// | |||
1952 | /// The syntax is | |||
1953 | /// \code | |||
1954 | /// #pragma region [optional name] | |||
1955 | /// #pragma endregion [optional comment] | |||
1956 | /// \endcode | |||
1957 | /// | |||
1958 | /// \note This is | |||
1959 | /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a> | |||
1960 | /// pragma, just skipped by compiler. | |||
1961 | struct PragmaRegionHandler : public PragmaHandler { | |||
1962 | PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {} | |||
1963 | ||||
1964 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
1965 | Token &NameTok) override { | |||
1966 | // #pragma region: endregion matches can be verified | |||
1967 | // __pragma(region): no sense, but ignored by msvc | |||
1968 | // _Pragma is not valid for MSVC, but there isn't any point | |||
1969 | // to handle a _Pragma differently. | |||
1970 | } | |||
1971 | }; | |||
1972 | ||||
1973 | /// This handles parsing pragmas that take a macro name and optional message | |||
1974 | static IdentifierInfo *HandleMacroAnnotationPragma(Preprocessor &PP, Token &Tok, | |||
1975 | const char *Pragma, | |||
1976 | std::string &MessageString) { | |||
1977 | std::string Macro; | |||
1978 | ||||
1979 | PP.Lex(Tok); | |||
1980 | if (Tok.isNot(tok::l_paren)) { | |||
1981 | PP.Diag(Tok, diag::err_expected) << "("; | |||
1982 | return nullptr; | |||
1983 | } | |||
1984 | ||||
1985 | PP.LexUnexpandedToken(Tok); | |||
1986 | if (!Tok.is(tok::identifier)) { | |||
1987 | PP.Diag(Tok, diag::err_expected) << tok::identifier; | |||
1988 | return nullptr; | |||
1989 | } | |||
1990 | IdentifierInfo *II = Tok.getIdentifierInfo(); | |||
1991 | ||||
1992 | if (!II->hasMacroDefinition()) { | |||
1993 | PP.Diag(Tok, diag::err_pp_visibility_non_macro) << II->getName(); | |||
1994 | return nullptr; | |||
1995 | } | |||
1996 | ||||
1997 | PP.Lex(Tok); | |||
1998 | if (Tok.is(tok::comma)) { | |||
1999 | PP.Lex(Tok); | |||
2000 | if (!PP.FinishLexStringLiteral(Tok, MessageString, Pragma, | |||
2001 | /*AllowMacroExpansion=*/true)) | |||
2002 | return nullptr; | |||
2003 | } | |||
2004 | ||||
2005 | if (Tok.isNot(tok::r_paren)) { | |||
2006 | PP.Diag(Tok, diag::err_expected) << ")"; | |||
2007 | return nullptr; | |||
2008 | } | |||
2009 | return II; | |||
2010 | } | |||
2011 | ||||
2012 | /// "\#pragma clang deprecated(...)" | |||
2013 | /// | |||
2014 | /// The syntax is | |||
2015 | /// \code | |||
2016 | /// #pragma clang deprecate(MACRO_NAME [, Message]) | |||
2017 | /// \endcode | |||
2018 | struct PragmaDeprecatedHandler : public PragmaHandler { | |||
2019 | PragmaDeprecatedHandler() : PragmaHandler("deprecated") {} | |||
2020 | ||||
2021 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
2022 | Token &Tok) override { | |||
2023 | std::string MessageString; | |||
2024 | ||||
2025 | if (IdentifierInfo *II = HandleMacroAnnotationPragma( | |||
2026 | PP, Tok, "#pragma clang deprecated", MessageString)) { | |||
2027 | II->setIsDeprecatedMacro(true); | |||
2028 | if (!MessageString.empty()) | |||
2029 | PP.addMacroDeprecationMsg(II, std::move(MessageString)); | |||
2030 | } | |||
2031 | } | |||
2032 | }; | |||
2033 | ||||
2034 | /// "\#pragma clang restrict_expansion(...)" | |||
2035 | /// | |||
2036 | /// The syntax is | |||
2037 | /// \code | |||
2038 | /// #pragma clang restrict_expansion(MACRO_NAME [, Message]) | |||
2039 | /// \endcode | |||
2040 | struct PragmaRestrictExpansionHandler : public PragmaHandler { | |||
2041 | PragmaRestrictExpansionHandler() : PragmaHandler("restrict_expansion") {} | |||
2042 | ||||
2043 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, | |||
2044 | Token &Tok) override { | |||
2045 | std::string MessageString; | |||
2046 | ||||
2047 | if (IdentifierInfo *II = HandleMacroAnnotationPragma( | |||
2048 | PP, Tok, "#pragma clang restrict_expansion", MessageString)) { | |||
2049 | II->setIsRestrictExpansion(true); | |||
2050 | PP.addRestrictExpansionMsg(II, std::move(MessageString), | |||
2051 | Tok.getLocation()); | |||
2052 | } | |||
2053 | } | |||
2054 | }; | |||
2055 | ||||
2056 | } // namespace | |||
2057 | ||||
2058 | /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: | |||
2059 | /// \#pragma GCC poison/system_header/dependency and \#pragma once. | |||
2060 | void Preprocessor::RegisterBuiltinPragmas() { | |||
2061 | AddPragmaHandler(new PragmaOnceHandler()); | |||
2062 | AddPragmaHandler(new PragmaMarkHandler()); | |||
2063 | AddPragmaHandler(new PragmaPushMacroHandler()); | |||
2064 | AddPragmaHandler(new PragmaPopMacroHandler()); | |||
2065 | AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message)); | |||
2066 | ||||
2067 | // #pragma GCC ... | |||
2068 | AddPragmaHandler("GCC", new PragmaPoisonHandler()); | |||
2069 | AddPragmaHandler("GCC", new PragmaSystemHeaderHandler()); | |||
2070 | AddPragmaHandler("GCC", new PragmaDependencyHandler()); | |||
2071 | AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC")); | |||
2072 | AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning, | |||
2073 | "GCC")); | |||
2074 | AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error, | |||
2075 | "GCC")); | |||
2076 | // #pragma clang ... | |||
2077 | AddPragmaHandler("clang", new PragmaPoisonHandler()); | |||
2078 | AddPragmaHandler("clang", new PragmaSystemHeaderHandler()); | |||
2079 | AddPragmaHandler("clang", new PragmaIncludeInsteadHandler()); | |||
2080 | AddPragmaHandler("clang", new PragmaDebugHandler()); | |||
2081 | AddPragmaHandler("clang", new PragmaDependencyHandler()); | |||
2082 | AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang")); | |||
2083 | AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler()); | |||
2084 | AddPragmaHandler("clang", new PragmaAssumeNonNullHandler()); | |||
2085 | AddPragmaHandler("clang", new PragmaDeprecatedHandler()); | |||
2086 | AddPragmaHandler("clang", new PragmaRestrictExpansionHandler()); | |||
2087 | ||||
2088 | // #pragma clang module ... | |||
2089 | auto *ModuleHandler = new PragmaNamespace("module"); | |||
2090 | AddPragmaHandler("clang", ModuleHandler); | |||
2091 | ModuleHandler->AddPragma(new PragmaModuleImportHandler()); | |||
2092 | ModuleHandler->AddPragma(new PragmaModuleBeginHandler()); | |||
2093 | ModuleHandler->AddPragma(new PragmaModuleEndHandler()); | |||
2094 | ModuleHandler->AddPragma(new PragmaModuleBuildHandler()); | |||
2095 | ModuleHandler->AddPragma(new PragmaModuleLoadHandler()); | |||
2096 | ||||
2097 | // Add region pragmas. | |||
2098 | AddPragmaHandler(new PragmaRegionHandler("region")); | |||
2099 | AddPragmaHandler(new PragmaRegionHandler("endregion")); | |||
2100 | ||||
2101 | // MS extensions. | |||
2102 | if (LangOpts.MicrosoftExt) { | |||
2103 | AddPragmaHandler(new PragmaWarningHandler()); | |||
2104 | AddPragmaHandler(new PragmaExecCharsetHandler()); | |||
2105 | AddPragmaHandler(new PragmaIncludeAliasHandler()); | |||
2106 | AddPragmaHandler(new PragmaHdrstopHandler()); | |||
2107 | AddPragmaHandler(new PragmaSystemHeaderHandler()); | |||
2108 | } | |||
2109 | ||||
2110 | // Pragmas added by plugins | |||
2111 | for (const PragmaHandlerRegistry::entry &handler : | |||
2112 | PragmaHandlerRegistry::entries()) { | |||
2113 | AddPragmaHandler(handler.instantiate().release()); | |||
2114 | } | |||
2115 | } | |||
2116 | ||||
2117 | /// Ignore all pragmas, useful for modes such as -Eonly which would otherwise | |||
2118 | /// warn about those pragmas being unknown. | |||
2119 | void Preprocessor::IgnorePragmas() { | |||
2120 | AddPragmaHandler(new EmptyPragmaHandler()); | |||
2121 | // Also ignore all pragmas in all namespaces created | |||
2122 | // in Preprocessor::RegisterBuiltinPragmas(). | |||
2123 | AddPragmaHandler("GCC", new EmptyPragmaHandler()); | |||
2124 | AddPragmaHandler("clang", new EmptyPragmaHandler()); | |||
2125 | } |
1 | //===--- Token.h - Token interface ------------------------------*- C++ -*-===// |
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 defines the Token interface. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_CLANG_LEX_TOKEN_H |
14 | #define LLVM_CLANG_LEX_TOKEN_H |
15 | |
16 | #include "clang/Basic/SourceLocation.h" |
17 | #include "clang/Basic/TokenKinds.h" |
18 | #include "llvm/ADT/StringRef.h" |
19 | #include <cassert> |
20 | |
21 | namespace clang { |
22 | |
23 | class IdentifierInfo; |
24 | |
25 | /// Token - This structure provides full information about a lexed token. |
26 | /// It is not intended to be space efficient, it is intended to return as much |
27 | /// information as possible about each returned token. This is expected to be |
28 | /// compressed into a smaller form if memory footprint is important. |
29 | /// |
30 | /// The parser can create a special "annotation token" representing a stream of |
31 | /// tokens that were parsed and semantically resolved, e.g.: "foo::MyClass<int>" |
32 | /// can be represented by a single typename annotation token that carries |
33 | /// information about the SourceRange of the tokens and the type object. |
34 | class Token { |
35 | /// The location of the token. This is actually a SourceLocation. |
36 | SourceLocation::UIntTy Loc; |
37 | |
38 | // Conceptually these next two fields could be in a union. However, this |
39 | // causes gcc 4.2 to pessimize LexTokenInternal, a very performance critical |
40 | // routine. Keeping as separate members with casts until a more beautiful fix |
41 | // presents itself. |
42 | |
43 | /// UintData - This holds either the length of the token text, when |
44 | /// a normal token, or the end of the SourceRange when an annotation |
45 | /// token. |
46 | SourceLocation::UIntTy UintData; |
47 | |
48 | /// PtrData - This is a union of four different pointer types, which depends |
49 | /// on what type of token this is: |
50 | /// Identifiers, keywords, etc: |
51 | /// This is an IdentifierInfo*, which contains the uniqued identifier |
52 | /// spelling. |
53 | /// Literals: isLiteral() returns true. |
54 | /// This is a pointer to the start of the token in a text buffer, which |
55 | /// may be dirty (have trigraphs / escaped newlines). |
56 | /// Annotations (resolved type names, C++ scopes, etc): isAnnotation(). |
57 | /// This is a pointer to sema-specific data for the annotation token. |
58 | /// Eof: |
59 | // This is a pointer to a Decl. |
60 | /// Other: |
61 | /// This is null. |
62 | void *PtrData; |
63 | |
64 | /// Kind - The actual flavor of token this is. |
65 | tok::TokenKind Kind; |
66 | |
67 | /// Flags - Bits we track about this token, members of the TokenFlags enum. |
68 | unsigned short Flags; |
69 | |
70 | public: |
71 | // Various flags set per token: |
72 | enum TokenFlags { |
73 | StartOfLine = 0x01, // At start of line or only after whitespace |
74 | // (considering the line after macro expansion). |
75 | LeadingSpace = 0x02, // Whitespace exists before this token (considering |
76 | // whitespace after macro expansion). |
77 | DisableExpand = 0x04, // This identifier may never be macro expanded. |
78 | NeedsCleaning = 0x08, // Contained an escaped newline or trigraph. |
79 | LeadingEmptyMacro = 0x10, // Empty macro exists before this token. |
80 | HasUDSuffix = 0x20, // This string or character literal has a ud-suffix. |
81 | HasUCN = 0x40, // This identifier contains a UCN. |
82 | IgnoredComma = 0x80, // This comma is not a macro argument separator (MS). |
83 | StringifiedInMacro = 0x100, // This string or character literal is formed by |
84 | // macro stringizing or charizing operator. |
85 | CommaAfterElided = 0x200, // The comma following this token was elided (MS). |
86 | IsEditorPlaceholder = 0x400, // This identifier is a placeholder. |
87 | IsReinjected = 0x800, // A phase 4 token that was produced before and |
88 | // re-added, e.g. via EnterTokenStream. Annotation |
89 | // tokens are *not* reinjected. |
90 | }; |
91 | |
92 | tok::TokenKind getKind() const { return Kind; } |
93 | void setKind(tok::TokenKind K) { Kind = K; } |
94 | |
95 | /// is/isNot - Predicates to check if this token is a specific kind, as in |
96 | /// "if (Tok.is(tok::l_brace)) {...}". |
97 | bool is(tok::TokenKind K) const { return Kind == K; } |
98 | bool isNot(tok::TokenKind K) const { return Kind != K; } |
99 | bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const { |
100 | return is(K1) || is(K2); |
101 | } |
102 | template <typename... Ts> |
103 | bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, Ts... Ks) const { |
104 | return is(K1) || isOneOf(K2, Ks...); |
105 | } |
106 | |
107 | /// Return true if this is a raw identifier (when lexing |
108 | /// in raw mode) or a non-keyword identifier (when lexing in non-raw mode). |
109 | bool isAnyIdentifier() const { |
110 | return tok::isAnyIdentifier(getKind()); |
111 | } |
112 | |
113 | /// Return true if this is a "literal", like a numeric |
114 | /// constant, string, etc. |
115 | bool isLiteral() const { |
116 | return tok::isLiteral(getKind()); |
117 | } |
118 | |
119 | /// Return true if this is any of tok::annot_* kind tokens. |
120 | bool isAnnotation() const { |
121 | return tok::isAnnotation(getKind()); |
122 | } |
123 | |
124 | /// Return a source location identifier for the specified |
125 | /// offset in the current file. |
126 | SourceLocation getLocation() const { |
127 | return SourceLocation::getFromRawEncoding(Loc); |
128 | } |
129 | unsigned getLength() const { |
130 | assert(!isAnnotation() && "Annotation tokens have no length field")(static_cast <bool> (!isAnnotation() && "Annotation tokens have no length field" ) ? void (0) : __assert_fail ("!isAnnotation() && \"Annotation tokens have no length field\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 130, __extension__ __PRETTY_FUNCTION__)); |
131 | return UintData; |
132 | } |
133 | |
134 | void setLocation(SourceLocation L) { Loc = L.getRawEncoding(); } |
135 | void setLength(unsigned Len) { |
136 | assert(!isAnnotation() && "Annotation tokens have no length field")(static_cast <bool> (!isAnnotation() && "Annotation tokens have no length field" ) ? void (0) : __assert_fail ("!isAnnotation() && \"Annotation tokens have no length field\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 136, __extension__ __PRETTY_FUNCTION__)); |
137 | UintData = Len; |
138 | } |
139 | |
140 | SourceLocation getAnnotationEndLoc() const { |
141 | assert(isAnnotation() && "Used AnnotEndLocID on non-annotation token")(static_cast <bool> (isAnnotation() && "Used AnnotEndLocID on non-annotation token" ) ? void (0) : __assert_fail ("isAnnotation() && \"Used AnnotEndLocID on non-annotation token\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 141, __extension__ __PRETTY_FUNCTION__)); |
142 | return SourceLocation::getFromRawEncoding(UintData ? UintData : Loc); |
143 | } |
144 | void setAnnotationEndLoc(SourceLocation L) { |
145 | assert(isAnnotation() && "Used AnnotEndLocID on non-annotation token")(static_cast <bool> (isAnnotation() && "Used AnnotEndLocID on non-annotation token" ) ? void (0) : __assert_fail ("isAnnotation() && \"Used AnnotEndLocID on non-annotation token\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 145, __extension__ __PRETTY_FUNCTION__)); |
146 | UintData = L.getRawEncoding(); |
147 | } |
148 | |
149 | SourceLocation getLastLoc() const { |
150 | return isAnnotation() ? getAnnotationEndLoc() : getLocation(); |
151 | } |
152 | |
153 | SourceLocation getEndLoc() const { |
154 | return isAnnotation() ? getAnnotationEndLoc() |
155 | : getLocation().getLocWithOffset(getLength()); |
156 | } |
157 | |
158 | /// SourceRange of the group of tokens that this annotation token |
159 | /// represents. |
160 | SourceRange getAnnotationRange() const { |
161 | return SourceRange(getLocation(), getAnnotationEndLoc()); |
162 | } |
163 | void setAnnotationRange(SourceRange R) { |
164 | setLocation(R.getBegin()); |
165 | setAnnotationEndLoc(R.getEnd()); |
166 | } |
167 | |
168 | const char *getName() const { return tok::getTokenName(Kind); } |
169 | |
170 | /// Reset all flags to cleared. |
171 | void startToken() { |
172 | Kind = tok::unknown; |
173 | Flags = 0; |
174 | PtrData = nullptr; |
175 | UintData = 0; |
176 | Loc = SourceLocation().getRawEncoding(); |
177 | } |
178 | |
179 | IdentifierInfo *getIdentifierInfo() const { |
180 | assert(isNot(tok::raw_identifier) &&(static_cast <bool> (isNot(tok::raw_identifier) && "getIdentifierInfo() on a tok::raw_identifier token!") ? void (0) : __assert_fail ("isNot(tok::raw_identifier) && \"getIdentifierInfo() on a tok::raw_identifier token!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 181, __extension__ __PRETTY_FUNCTION__)) |
181 | "getIdentifierInfo() on a tok::raw_identifier token!")(static_cast <bool> (isNot(tok::raw_identifier) && "getIdentifierInfo() on a tok::raw_identifier token!") ? void (0) : __assert_fail ("isNot(tok::raw_identifier) && \"getIdentifierInfo() on a tok::raw_identifier token!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 181, __extension__ __PRETTY_FUNCTION__)); |
182 | assert(!isAnnotation() &&(static_cast <bool> (!isAnnotation() && "getIdentifierInfo() on an annotation token!" ) ? void (0) : __assert_fail ("!isAnnotation() && \"getIdentifierInfo() on an annotation token!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 183, __extension__ __PRETTY_FUNCTION__)) |
183 | "getIdentifierInfo() on an annotation token!")(static_cast <bool> (!isAnnotation() && "getIdentifierInfo() on an annotation token!" ) ? void (0) : __assert_fail ("!isAnnotation() && \"getIdentifierInfo() on an annotation token!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 183, __extension__ __PRETTY_FUNCTION__)); |
184 | if (isLiteral()) return nullptr; |
185 | if (is(tok::eof)) return nullptr; |
186 | return (IdentifierInfo*) PtrData; |
187 | } |
188 | void setIdentifierInfo(IdentifierInfo *II) { |
189 | PtrData = (void*) II; |
190 | } |
191 | |
192 | const void *getEofData() const { |
193 | assert(is(tok::eof))(static_cast <bool> (is(tok::eof)) ? void (0) : __assert_fail ("is(tok::eof)", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 193, __extension__ __PRETTY_FUNCTION__)); |
194 | return reinterpret_cast<const void *>(PtrData); |
195 | } |
196 | void setEofData(const void *D) { |
197 | assert(is(tok::eof))(static_cast <bool> (is(tok::eof)) ? void (0) : __assert_fail ("is(tok::eof)", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 197, __extension__ __PRETTY_FUNCTION__)); |
198 | assert(!PtrData)(static_cast <bool> (!PtrData) ? void (0) : __assert_fail ("!PtrData", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 198, __extension__ __PRETTY_FUNCTION__)); |
199 | PtrData = const_cast<void *>(D); |
200 | } |
201 | |
202 | /// getRawIdentifier - For a raw identifier token (i.e., an identifier |
203 | /// lexed in raw mode), returns a reference to the text substring in the |
204 | /// buffer if known. |
205 | StringRef getRawIdentifier() const { |
206 | assert(is(tok::raw_identifier))(static_cast <bool> (is(tok::raw_identifier)) ? void (0 ) : __assert_fail ("is(tok::raw_identifier)", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 206, __extension__ __PRETTY_FUNCTION__)); |
207 | return StringRef(reinterpret_cast<const char *>(PtrData), getLength()); |
208 | } |
209 | void setRawIdentifierData(const char *Ptr) { |
210 | assert(is(tok::raw_identifier))(static_cast <bool> (is(tok::raw_identifier)) ? void (0 ) : __assert_fail ("is(tok::raw_identifier)", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 210, __extension__ __PRETTY_FUNCTION__)); |
211 | PtrData = const_cast<char*>(Ptr); |
212 | } |
213 | |
214 | /// getLiteralData - For a literal token (numeric constant, string, etc), this |
215 | /// returns a pointer to the start of it in the text buffer if known, null |
216 | /// otherwise. |
217 | const char *getLiteralData() const { |
218 | assert(isLiteral() && "Cannot get literal data of non-literal")(static_cast <bool> (isLiteral() && "Cannot get literal data of non-literal" ) ? void (0) : __assert_fail ("isLiteral() && \"Cannot get literal data of non-literal\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 218, __extension__ __PRETTY_FUNCTION__)); |
219 | return reinterpret_cast<const char*>(PtrData); |
220 | } |
221 | void setLiteralData(const char *Ptr) { |
222 | assert(isLiteral() && "Cannot set literal data of non-literal")(static_cast <bool> (isLiteral() && "Cannot set literal data of non-literal" ) ? void (0) : __assert_fail ("isLiteral() && \"Cannot set literal data of non-literal\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 222, __extension__ __PRETTY_FUNCTION__)); |
223 | PtrData = const_cast<char*>(Ptr); |
224 | } |
225 | |
226 | void *getAnnotationValue() const { |
227 | assert(isAnnotation() && "Used AnnotVal on non-annotation token")(static_cast <bool> (isAnnotation() && "Used AnnotVal on non-annotation token" ) ? void (0) : __assert_fail ("isAnnotation() && \"Used AnnotVal on non-annotation token\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 227, __extension__ __PRETTY_FUNCTION__)); |
228 | return PtrData; |
229 | } |
230 | void setAnnotationValue(void *val) { |
231 | assert(isAnnotation() && "Used AnnotVal on non-annotation token")(static_cast <bool> (isAnnotation() && "Used AnnotVal on non-annotation token" ) ? void (0) : __assert_fail ("isAnnotation() && \"Used AnnotVal on non-annotation token\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Lex/Token.h" , 231, __extension__ __PRETTY_FUNCTION__)); |
232 | PtrData = val; |
233 | } |
234 | |
235 | /// Set the specified flag. |
236 | void setFlag(TokenFlags Flag) { |
237 | Flags |= Flag; |
238 | } |
239 | |
240 | /// Get the specified flag. |
241 | bool getFlag(TokenFlags Flag) const { |
242 | return (Flags & Flag) != 0; |
243 | } |
244 | |
245 | /// Unset the specified flag. |
246 | void clearFlag(TokenFlags Flag) { |
247 | Flags &= ~Flag; |
248 | } |
249 | |
250 | /// Return the internal represtation of the flags. |
251 | /// |
252 | /// This is only intended for low-level operations such as writing tokens to |
253 | /// disk. |
254 | unsigned getFlags() const { |
255 | return Flags; |
256 | } |
257 | |
258 | /// Set a flag to either true or false. |
259 | void setFlagValue(TokenFlags Flag, bool Val) { |
260 | if (Val) |
261 | setFlag(Flag); |
262 | else |
263 | clearFlag(Flag); |
264 | } |
265 | |
266 | /// isAtStartOfLine - Return true if this token is at the start of a line. |
267 | /// |
268 | bool isAtStartOfLine() const { return getFlag(StartOfLine); } |
269 | |
270 | /// Return true if this token has whitespace before it. |
271 | /// |
272 | bool hasLeadingSpace() const { return getFlag(LeadingSpace); } |
273 | |
274 | /// Return true if this identifier token should never |
275 | /// be expanded in the future, due to C99 6.10.3.4p2. |
276 | bool isExpandDisabled() const { return getFlag(DisableExpand); } |
277 | |
278 | /// Return true if we have an ObjC keyword identifier. |
279 | bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const; |
280 | |
281 | /// Return the ObjC keyword kind. |
282 | tok::ObjCKeywordKind getObjCKeywordID() const; |
283 | |
284 | /// Return true if this token has trigraphs or escaped newlines in it. |
285 | bool needsCleaning() const { return getFlag(NeedsCleaning); } |
286 | |
287 | /// Return true if this token has an empty macro before it. |
288 | /// |
289 | bool hasLeadingEmptyMacro() const { return getFlag(LeadingEmptyMacro); } |
290 | |
291 | /// Return true if this token is a string or character literal which |
292 | /// has a ud-suffix. |
293 | bool hasUDSuffix() const { return getFlag(HasUDSuffix); } |
294 | |
295 | /// Returns true if this token contains a universal character name. |
296 | bool hasUCN() const { return getFlag(HasUCN); } |
297 | |
298 | /// Returns true if this token is formed by macro by stringizing or charizing |
299 | /// operator. |
300 | bool stringifiedInMacro() const { return getFlag(StringifiedInMacro); } |
301 | |
302 | /// Returns true if the comma after this token was elided. |
303 | bool commaAfterElided() const { return getFlag(CommaAfterElided); } |
304 | |
305 | /// Returns true if this token is an editor placeholder. |
306 | /// |
307 | /// Editor placeholders are produced by the code-completion engine and are |
308 | /// represented as characters between '<#' and '#>' in the source code. The |
309 | /// lexer uses identifier tokens to represent placeholders. |
310 | bool isEditorPlaceholder() const { return getFlag(IsEditorPlaceholder); } |
311 | }; |
312 | |
313 | /// Information about the conditional stack (\#if directives) |
314 | /// currently active. |
315 | struct PPConditionalInfo { |
316 | /// Location where the conditional started. |
317 | SourceLocation IfLoc; |
318 | |
319 | /// True if this was contained in a skipping directive, e.g., |
320 | /// in a "\#if 0" block. |
321 | bool WasSkipping; |
322 | |
323 | /// True if we have emitted tokens already, and now we're in |
324 | /// an \#else block or something. Only useful in Skipping blocks. |
325 | bool FoundNonSkip; |
326 | |
327 | /// True if we've seen a \#else in this block. If so, |
328 | /// \#elif/\#else directives are not allowed. |
329 | bool FoundElse; |
330 | }; |
331 | |
332 | } // end namespace clang |
333 | |
334 | #endif // LLVM_CLANG_LEX_TOKEN_H |
1 | //===- IdentifierTable.h - Hash table for identifier lookup -----*- C++ -*-===// |
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 | /// \file |
10 | /// Defines the clang::IdentifierInfo, clang::IdentifierTable, and |
11 | /// clang::Selector interfaces. |
12 | // |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #ifndef LLVM_CLANG_BASIC_IDENTIFIERTABLE_H |
16 | #define LLVM_CLANG_BASIC_IDENTIFIERTABLE_H |
17 | |
18 | #include "clang/Basic/LLVM.h" |
19 | #include "clang/Basic/TokenKinds.h" |
20 | #include "llvm/ADT/DenseMapInfo.h" |
21 | #include "llvm/ADT/SmallString.h" |
22 | #include "llvm/ADT/StringMap.h" |
23 | #include "llvm/ADT/StringRef.h" |
24 | #include "llvm/Support/Allocator.h" |
25 | #include "llvm/Support/PointerLikeTypeTraits.h" |
26 | #include "llvm/Support/type_traits.h" |
27 | #include <cassert> |
28 | #include <cstddef> |
29 | #include <cstdint> |
30 | #include <cstring> |
31 | #include <string> |
32 | #include <utility> |
33 | |
34 | namespace clang { |
35 | |
36 | class DeclarationName; |
37 | class DeclarationNameTable; |
38 | class IdentifierInfo; |
39 | class LangOptions; |
40 | class MultiKeywordSelector; |
41 | class SourceLocation; |
42 | |
43 | enum class ReservedIdentifierStatus { |
44 | NotReserved = 0, |
45 | StartsWithUnderscoreAtGlobalScope, |
46 | StartsWithDoubleUnderscore, |
47 | StartsWithUnderscoreFollowedByCapitalLetter, |
48 | ContainsDoubleUnderscore, |
49 | }; |
50 | |
51 | /// A simple pair of identifier info and location. |
52 | using IdentifierLocPair = std::pair<IdentifierInfo *, SourceLocation>; |
53 | |
54 | /// IdentifierInfo and other related classes are aligned to |
55 | /// 8 bytes so that DeclarationName can use the lower 3 bits |
56 | /// of a pointer to one of these classes. |
57 | enum { IdentifierInfoAlignment = 8 }; |
58 | |
59 | static constexpr int ObjCOrBuiltinIDBits = 16; |
60 | |
61 | /// One of these records is kept for each identifier that |
62 | /// is lexed. This contains information about whether the token was \#define'd, |
63 | /// is a language keyword, or if it is a front-end token of some sort (e.g. a |
64 | /// variable or function name). The preprocessor keeps this information in a |
65 | /// set, and all tok::identifier tokens have a pointer to one of these. |
66 | /// It is aligned to 8 bytes because DeclarationName needs the lower 3 bits. |
67 | class alignas(IdentifierInfoAlignment) IdentifierInfo { |
68 | friend class IdentifierTable; |
69 | |
70 | // Front-end token ID or tok::identifier. |
71 | unsigned TokenID : 9; |
72 | |
73 | // ObjC keyword ('protocol' in '@protocol') or builtin (__builtin_inf). |
74 | // First NUM_OBJC_KEYWORDS values are for Objective-C, |
75 | // the remaining values are for builtins. |
76 | unsigned ObjCOrBuiltinID : ObjCOrBuiltinIDBits; |
77 | |
78 | // True if there is a #define for this. |
79 | unsigned HasMacro : 1; |
80 | |
81 | // True if there was a #define for this. |
82 | unsigned HadMacro : 1; |
83 | |
84 | // True if the identifier is a language extension. |
85 | unsigned IsExtension : 1; |
86 | |
87 | // True if the identifier is a keyword in a newer or proposed Standard. |
88 | unsigned IsFutureCompatKeyword : 1; |
89 | |
90 | // True if the identifier is poisoned. |
91 | unsigned IsPoisoned : 1; |
92 | |
93 | // True if the identifier is a C++ operator keyword. |
94 | unsigned IsCPPOperatorKeyword : 1; |
95 | |
96 | // Internal bit set by the member function RecomputeNeedsHandleIdentifier. |
97 | // See comment about RecomputeNeedsHandleIdentifier for more info. |
98 | unsigned NeedsHandleIdentifier : 1; |
99 | |
100 | // True if the identifier was loaded (at least partially) from an AST file. |
101 | unsigned IsFromAST : 1; |
102 | |
103 | // True if the identifier has changed from the definition |
104 | // loaded from an AST file. |
105 | unsigned ChangedAfterLoad : 1; |
106 | |
107 | // True if the identifier's frontend information has changed from the |
108 | // definition loaded from an AST file. |
109 | unsigned FEChangedAfterLoad : 1; |
110 | |
111 | // True if revertTokenIDToIdentifier was called. |
112 | unsigned RevertedTokenID : 1; |
113 | |
114 | // True if there may be additional information about |
115 | // this identifier stored externally. |
116 | unsigned OutOfDate : 1; |
117 | |
118 | // True if this is the 'import' contextual keyword. |
119 | unsigned IsModulesImport : 1; |
120 | |
121 | // True if this is a mangled OpenMP variant name. |
122 | unsigned IsMangledOpenMPVariantName : 1; |
123 | |
124 | // True if this is a deprecated macro. |
125 | unsigned IsDeprecatedMacro : 1; |
126 | |
127 | // True if this macro is unsafe in headers. |
128 | unsigned IsRestrictExpansion : 1; |
129 | |
130 | // 23 bits left in a 64-bit word. |
131 | |
132 | // Managed by the language front-end. |
133 | void *FETokenInfo = nullptr; |
134 | |
135 | llvm::StringMapEntry<IdentifierInfo *> *Entry = nullptr; |
136 | |
137 | IdentifierInfo() |
138 | : TokenID(tok::identifier), ObjCOrBuiltinID(0), HasMacro(false), |
139 | HadMacro(false), IsExtension(false), IsFutureCompatKeyword(false), |
140 | IsPoisoned(false), IsCPPOperatorKeyword(false), |
141 | NeedsHandleIdentifier(false), IsFromAST(false), ChangedAfterLoad(false), |
142 | FEChangedAfterLoad(false), RevertedTokenID(false), OutOfDate(false), |
143 | IsModulesImport(false), IsMangledOpenMPVariantName(false), |
144 | IsDeprecatedMacro(false), IsRestrictExpansion(false) {} |
145 | |
146 | public: |
147 | IdentifierInfo(const IdentifierInfo &) = delete; |
148 | IdentifierInfo &operator=(const IdentifierInfo &) = delete; |
149 | IdentifierInfo(IdentifierInfo &&) = delete; |
150 | IdentifierInfo &operator=(IdentifierInfo &&) = delete; |
151 | |
152 | /// Return true if this is the identifier for the specified string. |
153 | /// |
154 | /// This is intended to be used for string literals only: II->isStr("foo"). |
155 | template <std::size_t StrLen> |
156 | bool isStr(const char (&Str)[StrLen]) const { |
157 | return getLength() == StrLen-1 && |
158 | memcmp(getNameStart(), Str, StrLen-1) == 0; |
159 | } |
160 | |
161 | /// Return true if this is the identifier for the specified StringRef. |
162 | bool isStr(llvm::StringRef Str) const { |
163 | llvm::StringRef ThisStr(getNameStart(), getLength()); |
164 | return ThisStr == Str; |
165 | } |
166 | |
167 | /// Return the beginning of the actual null-terminated string for this |
168 | /// identifier. |
169 | const char *getNameStart() const { return Entry->getKeyData(); } |
170 | |
171 | /// Efficiently return the length of this identifier info. |
172 | unsigned getLength() const { return Entry->getKeyLength(); } |
173 | |
174 | /// Return the actual identifier string. |
175 | StringRef getName() const { |
176 | return StringRef(getNameStart(), getLength()); |
177 | } |
178 | |
179 | /// Return true if this identifier is \#defined to some other value. |
180 | /// \note The current definition may be in a module and not currently visible. |
181 | bool hasMacroDefinition() const { |
182 | return HasMacro; |
183 | } |
184 | void setHasMacroDefinition(bool Val) { |
185 | if (HasMacro == Val) return; |
186 | |
187 | HasMacro = Val; |
188 | if (Val) { |
189 | NeedsHandleIdentifier = true; |
190 | HadMacro = true; |
191 | } else { |
192 | // Because calling the setters of these calls recomputes, just set them |
193 | // manually to avoid recomputing a bunch of times. |
194 | IsDeprecatedMacro = false; |
195 | IsRestrictExpansion = false; |
196 | RecomputeNeedsHandleIdentifier(); |
197 | } |
198 | } |
199 | /// Returns true if this identifier was \#defined to some value at any |
200 | /// moment. In this case there should be an entry for the identifier in the |
201 | /// macro history table in Preprocessor. |
202 | bool hadMacroDefinition() const { |
203 | return HadMacro; |
204 | } |
205 | |
206 | bool isDeprecatedMacro() const { return IsDeprecatedMacro; } |
207 | |
208 | void setIsDeprecatedMacro(bool Val) { |
209 | if (IsDeprecatedMacro == Val) |
210 | return; |
211 | IsDeprecatedMacro = Val; |
212 | if (Val) |
213 | NeedsHandleIdentifier = true; |
214 | else |
215 | RecomputeNeedsHandleIdentifier(); |
216 | } |
217 | |
218 | bool isRestrictExpansion() const { return IsRestrictExpansion; } |
219 | |
220 | void setIsRestrictExpansion(bool Val) { |
221 | if (IsRestrictExpansion == Val) |
222 | return; |
223 | IsRestrictExpansion = Val; |
224 | if (Val) |
225 | NeedsHandleIdentifier = true; |
226 | else |
227 | RecomputeNeedsHandleIdentifier(); |
228 | } |
229 | |
230 | /// If this is a source-language token (e.g. 'for'), this API |
231 | /// can be used to cause the lexer to map identifiers to source-language |
232 | /// tokens. |
233 | tok::TokenKind getTokenID() const { return (tok::TokenKind)TokenID; } |
234 | |
235 | /// True if revertTokenIDToIdentifier() was called. |
236 | bool hasRevertedTokenIDToIdentifier() const { return RevertedTokenID; } |
237 | |
238 | /// Revert TokenID to tok::identifier; used for GNU libstdc++ 4.2 |
239 | /// compatibility. |
240 | /// |
241 | /// TokenID is normally read-only but there are 2 instances where we revert it |
242 | /// to tok::identifier for libstdc++ 4.2. Keep track of when this happens |
243 | /// using this method so we can inform serialization about it. |
244 | void revertTokenIDToIdentifier() { |
245 | assert(TokenID != tok::identifier && "Already at tok::identifier")(static_cast <bool> (TokenID != tok::identifier && "Already at tok::identifier") ? void (0) : __assert_fail ("TokenID != tok::identifier && \"Already at tok::identifier\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Basic/IdentifierTable.h" , 245, __extension__ __PRETTY_FUNCTION__)); |
246 | TokenID = tok::identifier; |
247 | RevertedTokenID = true; |
248 | } |
249 | void revertIdentifierToTokenID(tok::TokenKind TK) { |
250 | assert(TokenID == tok::identifier && "Should be at tok::identifier")(static_cast <bool> (TokenID == tok::identifier && "Should be at tok::identifier") ? void (0) : __assert_fail ( "TokenID == tok::identifier && \"Should be at tok::identifier\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Basic/IdentifierTable.h" , 250, __extension__ __PRETTY_FUNCTION__)); |
251 | TokenID = TK; |
252 | RevertedTokenID = false; |
253 | } |
254 | |
255 | /// Return the preprocessor keyword ID for this identifier. |
256 | /// |
257 | /// For example, "define" will return tok::pp_define. |
258 | tok::PPKeywordKind getPPKeywordID() const; |
259 | |
260 | /// Return the Objective-C keyword ID for the this identifier. |
261 | /// |
262 | /// For example, 'class' will return tok::objc_class if ObjC is enabled. |
263 | tok::ObjCKeywordKind getObjCKeywordID() const { |
264 | if (ObjCOrBuiltinID < tok::NUM_OBJC_KEYWORDS) |
265 | return tok::ObjCKeywordKind(ObjCOrBuiltinID); |
266 | else |
267 | return tok::objc_not_keyword; |
268 | } |
269 | void setObjCKeywordID(tok::ObjCKeywordKind ID) { ObjCOrBuiltinID = ID; } |
270 | |
271 | /// Return a value indicating whether this is a builtin function. |
272 | /// |
273 | /// 0 is not-built-in. 1+ are specific builtin functions. |
274 | unsigned getBuiltinID() const { |
275 | if (ObjCOrBuiltinID >= tok::NUM_OBJC_KEYWORDS) |
276 | return ObjCOrBuiltinID - tok::NUM_OBJC_KEYWORDS; |
277 | else |
278 | return 0; |
279 | } |
280 | void setBuiltinID(unsigned ID) { |
281 | ObjCOrBuiltinID = ID + tok::NUM_OBJC_KEYWORDS; |
282 | assert(ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID(static_cast <bool> (ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS ) == ID && "ID too large for field!") ? void (0) : __assert_fail ("ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID && \"ID too large for field!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Basic/IdentifierTable.h" , 283, __extension__ __PRETTY_FUNCTION__)) |
283 | && "ID too large for field!")(static_cast <bool> (ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS ) == ID && "ID too large for field!") ? void (0) : __assert_fail ("ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID && \"ID too large for field!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Basic/IdentifierTable.h" , 283, __extension__ __PRETTY_FUNCTION__)); |
284 | } |
285 | |
286 | unsigned getObjCOrBuiltinID() const { return ObjCOrBuiltinID; } |
287 | void setObjCOrBuiltinID(unsigned ID) { ObjCOrBuiltinID = ID; } |
288 | |
289 | /// get/setExtension - Initialize information about whether or not this |
290 | /// language token is an extension. This controls extension warnings, and is |
291 | /// only valid if a custom token ID is set. |
292 | bool isExtensionToken() const { return IsExtension; } |
293 | void setIsExtensionToken(bool Val) { |
294 | IsExtension = Val; |
295 | if (Val) |
296 | NeedsHandleIdentifier = true; |
297 | else |
298 | RecomputeNeedsHandleIdentifier(); |
299 | } |
300 | |
301 | /// is/setIsFutureCompatKeyword - Initialize information about whether or not |
302 | /// this language token is a keyword in a newer or proposed Standard. This |
303 | /// controls compatibility warnings, and is only true when not parsing the |
304 | /// corresponding Standard. Once a compatibility problem has been diagnosed |
305 | /// with this keyword, the flag will be cleared. |
306 | bool isFutureCompatKeyword() const { return IsFutureCompatKeyword; } |
307 | void setIsFutureCompatKeyword(bool Val) { |
308 | IsFutureCompatKeyword = Val; |
309 | if (Val) |
310 | NeedsHandleIdentifier = true; |
311 | else |
312 | RecomputeNeedsHandleIdentifier(); |
313 | } |
314 | |
315 | /// setIsPoisoned - Mark this identifier as poisoned. After poisoning, the |
316 | /// Preprocessor will emit an error every time this token is used. |
317 | void setIsPoisoned(bool Value = true) { |
318 | IsPoisoned = Value; |
319 | if (Value) |
320 | NeedsHandleIdentifier = true; |
321 | else |
322 | RecomputeNeedsHandleIdentifier(); |
323 | } |
324 | |
325 | /// Return true if this token has been poisoned. |
326 | bool isPoisoned() const { return IsPoisoned; } |
327 | |
328 | /// isCPlusPlusOperatorKeyword/setIsCPlusPlusOperatorKeyword controls whether |
329 | /// this identifier is a C++ alternate representation of an operator. |
330 | void setIsCPlusPlusOperatorKeyword(bool Val = true) { |
331 | IsCPPOperatorKeyword = Val; |
332 | } |
333 | bool isCPlusPlusOperatorKeyword() const { return IsCPPOperatorKeyword; } |
334 | |
335 | /// Return true if this token is a keyword in the specified language. |
336 | bool isKeyword(const LangOptions &LangOpts) const; |
337 | |
338 | /// Return true if this token is a C++ keyword in the specified |
339 | /// language. |
340 | bool isCPlusPlusKeyword(const LangOptions &LangOpts) const; |
341 | |
342 | /// Get and set FETokenInfo. The language front-end is allowed to associate |
343 | /// arbitrary metadata with this token. |
344 | void *getFETokenInfo() const { return FETokenInfo; } |
345 | void setFETokenInfo(void *T) { FETokenInfo = T; } |
346 | |
347 | /// Return true if the Preprocessor::HandleIdentifier must be called |
348 | /// on a token of this identifier. |
349 | /// |
350 | /// If this returns false, we know that HandleIdentifier will not affect |
351 | /// the token. |
352 | bool isHandleIdentifierCase() const { return NeedsHandleIdentifier; } |
353 | |
354 | /// Return true if the identifier in its current state was loaded |
355 | /// from an AST file. |
356 | bool isFromAST() const { return IsFromAST; } |
357 | |
358 | void setIsFromAST() { IsFromAST = true; } |
359 | |
360 | /// Determine whether this identifier has changed since it was loaded |
361 | /// from an AST file. |
362 | bool hasChangedSinceDeserialization() const { |
363 | return ChangedAfterLoad; |
364 | } |
365 | |
366 | /// Note that this identifier has changed since it was loaded from |
367 | /// an AST file. |
368 | void setChangedSinceDeserialization() { |
369 | ChangedAfterLoad = true; |
370 | } |
371 | |
372 | /// Determine whether the frontend token information for this |
373 | /// identifier has changed since it was loaded from an AST file. |
374 | bool hasFETokenInfoChangedSinceDeserialization() const { |
375 | return FEChangedAfterLoad; |
376 | } |
377 | |
378 | /// Note that the frontend token information for this identifier has |
379 | /// changed since it was loaded from an AST file. |
380 | void setFETokenInfoChangedSinceDeserialization() { |
381 | FEChangedAfterLoad = true; |
382 | } |
383 | |
384 | /// Determine whether the information for this identifier is out of |
385 | /// date with respect to the external source. |
386 | bool isOutOfDate() const { return OutOfDate; } |
387 | |
388 | /// Set whether the information for this identifier is out of |
389 | /// date with respect to the external source. |
390 | void setOutOfDate(bool OOD) { |
391 | OutOfDate = OOD; |
392 | if (OOD) |
393 | NeedsHandleIdentifier = true; |
394 | else |
395 | RecomputeNeedsHandleIdentifier(); |
396 | } |
397 | |
398 | /// Determine whether this is the contextual keyword \c import. |
399 | bool isModulesImport() const { return IsModulesImport; } |
400 | |
401 | /// Set whether this identifier is the contextual keyword \c import. |
402 | void setModulesImport(bool I) { |
403 | IsModulesImport = I; |
404 | if (I) |
405 | NeedsHandleIdentifier = true; |
406 | else |
407 | RecomputeNeedsHandleIdentifier(); |
408 | } |
409 | |
410 | /// Determine whether this is the mangled name of an OpenMP variant. |
411 | bool isMangledOpenMPVariantName() const { return IsMangledOpenMPVariantName; } |
412 | |
413 | /// Set whether this is the mangled name of an OpenMP variant. |
414 | void setMangledOpenMPVariantName(bool I) { IsMangledOpenMPVariantName = I; } |
415 | |
416 | /// Return true if this identifier is an editor placeholder. |
417 | /// |
418 | /// Editor placeholders are produced by the code-completion engine and are |
419 | /// represented as characters between '<#' and '#>' in the source code. An |
420 | /// example of auto-completed call with a placeholder parameter is shown |
421 | /// below: |
422 | /// \code |
423 | /// function(<#int x#>); |
424 | /// \endcode |
425 | bool isEditorPlaceholder() const { |
426 | return getName().startswith("<#") && getName().endswith("#>"); |
427 | } |
428 | |
429 | /// Determine whether \p this is a name reserved for the implementation (C99 |
430 | /// 7.1.3, C++ [lib.global.names]). |
431 | ReservedIdentifierStatus isReserved(const LangOptions &LangOpts) const; |
432 | |
433 | /// Provide less than operator for lexicographical sorting. |
434 | bool operator<(const IdentifierInfo &RHS) const { |
435 | return getName() < RHS.getName(); |
436 | } |
437 | |
438 | private: |
439 | /// The Preprocessor::HandleIdentifier does several special (but rare) |
440 | /// things to identifiers of various sorts. For example, it changes the |
441 | /// \c for keyword token from tok::identifier to tok::for. |
442 | /// |
443 | /// This method is very tied to the definition of HandleIdentifier. Any |
444 | /// change to it should be reflected here. |
445 | void RecomputeNeedsHandleIdentifier() { |
446 | NeedsHandleIdentifier = isPoisoned() || hasMacroDefinition() || |
447 | isExtensionToken() || isFutureCompatKeyword() || |
448 | isOutOfDate() || isModulesImport(); |
449 | } |
450 | }; |
451 | |
452 | /// An RAII object for [un]poisoning an identifier within a scope. |
453 | /// |
454 | /// \p II is allowed to be null, in which case objects of this type have |
455 | /// no effect. |
456 | class PoisonIdentifierRAIIObject { |
457 | IdentifierInfo *const II; |
458 | const bool OldValue; |
459 | |
460 | public: |
461 | PoisonIdentifierRAIIObject(IdentifierInfo *II, bool NewValue) |
462 | : II(II), OldValue(II ? II->isPoisoned() : false) { |
463 | if(II) |
464 | II->setIsPoisoned(NewValue); |
465 | } |
466 | |
467 | ~PoisonIdentifierRAIIObject() { |
468 | if(II) |
469 | II->setIsPoisoned(OldValue); |
470 | } |
471 | }; |
472 | |
473 | /// An iterator that walks over all of the known identifiers |
474 | /// in the lookup table. |
475 | /// |
476 | /// Since this iterator uses an abstract interface via virtual |
477 | /// functions, it uses an object-oriented interface rather than the |
478 | /// more standard C++ STL iterator interface. In this OO-style |
479 | /// iteration, the single function \c Next() provides dereference, |
480 | /// advance, and end-of-sequence checking in a single |
481 | /// operation. Subclasses of this iterator type will provide the |
482 | /// actual functionality. |
483 | class IdentifierIterator { |
484 | protected: |
485 | IdentifierIterator() = default; |
486 | |
487 | public: |
488 | IdentifierIterator(const IdentifierIterator &) = delete; |
489 | IdentifierIterator &operator=(const IdentifierIterator &) = delete; |
490 | |
491 | virtual ~IdentifierIterator(); |
492 | |
493 | /// Retrieve the next string in the identifier table and |
494 | /// advances the iterator for the following string. |
495 | /// |
496 | /// \returns The next string in the identifier table. If there is |
497 | /// no such string, returns an empty \c StringRef. |
498 | virtual StringRef Next() = 0; |
499 | }; |
500 | |
501 | /// Provides lookups to, and iteration over, IdentiferInfo objects. |
502 | class IdentifierInfoLookup { |
503 | public: |
504 | virtual ~IdentifierInfoLookup(); |
505 | |
506 | /// Return the IdentifierInfo for the specified named identifier. |
507 | /// |
508 | /// Unlike the version in IdentifierTable, this returns a pointer instead |
509 | /// of a reference. If the pointer is null then the IdentifierInfo cannot |
510 | /// be found. |
511 | virtual IdentifierInfo* get(StringRef Name) = 0; |
512 | |
513 | /// Retrieve an iterator into the set of all identifiers |
514 | /// known to this identifier lookup source. |
515 | /// |
516 | /// This routine provides access to all of the identifiers known to |
517 | /// the identifier lookup, allowing access to the contents of the |
518 | /// identifiers without introducing the overhead of constructing |
519 | /// IdentifierInfo objects for each. |
520 | /// |
521 | /// \returns A new iterator into the set of known identifiers. The |
522 | /// caller is responsible for deleting this iterator. |
523 | virtual IdentifierIterator *getIdentifiers(); |
524 | }; |
525 | |
526 | /// Implements an efficient mapping from strings to IdentifierInfo nodes. |
527 | /// |
528 | /// This has no other purpose, but this is an extremely performance-critical |
529 | /// piece of the code, as each occurrence of every identifier goes through |
530 | /// here when lexed. |
531 | class IdentifierTable { |
532 | // Shark shows that using MallocAllocator is *much* slower than using this |
533 | // BumpPtrAllocator! |
534 | using HashTableTy = llvm::StringMap<IdentifierInfo *, llvm::BumpPtrAllocator>; |
535 | HashTableTy HashTable; |
536 | |
537 | IdentifierInfoLookup* ExternalLookup; |
538 | |
539 | public: |
540 | /// Create the identifier table. |
541 | explicit IdentifierTable(IdentifierInfoLookup *ExternalLookup = nullptr); |
542 | |
543 | /// Create the identifier table, populating it with info about the |
544 | /// language keywords for the language specified by \p LangOpts. |
545 | explicit IdentifierTable(const LangOptions &LangOpts, |
546 | IdentifierInfoLookup *ExternalLookup = nullptr); |
547 | |
548 | /// Set the external identifier lookup mechanism. |
549 | void setExternalIdentifierLookup(IdentifierInfoLookup *IILookup) { |
550 | ExternalLookup = IILookup; |
551 | } |
552 | |
553 | /// Retrieve the external identifier lookup object, if any. |
554 | IdentifierInfoLookup *getExternalIdentifierLookup() const { |
555 | return ExternalLookup; |
556 | } |
557 | |
558 | llvm::BumpPtrAllocator& getAllocator() { |
559 | return HashTable.getAllocator(); |
560 | } |
561 | |
562 | /// Return the identifier token info for the specified named |
563 | /// identifier. |
564 | IdentifierInfo &get(StringRef Name) { |
565 | auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first; |
566 | |
567 | IdentifierInfo *&II = Entry.second; |
568 | if (II) return *II; |
569 | |
570 | // No entry; if we have an external lookup, look there first. |
571 | if (ExternalLookup) { |
572 | II = ExternalLookup->get(Name); |
573 | if (II) |
574 | return *II; |
575 | } |
576 | |
577 | // Lookups failed, make a new IdentifierInfo. |
578 | void *Mem = getAllocator().Allocate<IdentifierInfo>(); |
579 | II = new (Mem) IdentifierInfo(); |
580 | |
581 | // Make sure getName() knows how to find the IdentifierInfo |
582 | // contents. |
583 | II->Entry = &Entry; |
584 | |
585 | return *II; |
586 | } |
587 | |
588 | IdentifierInfo &get(StringRef Name, tok::TokenKind TokenCode) { |
589 | IdentifierInfo &II = get(Name); |
590 | II.TokenID = TokenCode; |
591 | assert(II.TokenID == (unsigned) TokenCode && "TokenCode too large")(static_cast <bool> (II.TokenID == (unsigned) TokenCode && "TokenCode too large") ? void (0) : __assert_fail ("II.TokenID == (unsigned) TokenCode && \"TokenCode too large\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Basic/IdentifierTable.h" , 591, __extension__ __PRETTY_FUNCTION__)); |
592 | return II; |
593 | } |
594 | |
595 | /// Gets an IdentifierInfo for the given name without consulting |
596 | /// external sources. |
597 | /// |
598 | /// This is a version of get() meant for external sources that want to |
599 | /// introduce or modify an identifier. If they called get(), they would |
600 | /// likely end up in a recursion. |
601 | IdentifierInfo &getOwn(StringRef Name) { |
602 | auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first; |
603 | |
604 | IdentifierInfo *&II = Entry.second; |
605 | if (II) |
606 | return *II; |
607 | |
608 | // Lookups failed, make a new IdentifierInfo. |
609 | void *Mem = getAllocator().Allocate<IdentifierInfo>(); |
610 | II = new (Mem) IdentifierInfo(); |
611 | |
612 | // Make sure getName() knows how to find the IdentifierInfo |
613 | // contents. |
614 | II->Entry = &Entry; |
615 | |
616 | // If this is the 'import' contextual keyword, mark it as such. |
617 | if (Name.equals("import")) |
618 | II->setModulesImport(true); |
619 | |
620 | return *II; |
621 | } |
622 | |
623 | using iterator = HashTableTy::const_iterator; |
624 | using const_iterator = HashTableTy::const_iterator; |
625 | |
626 | iterator begin() const { return HashTable.begin(); } |
627 | iterator end() const { return HashTable.end(); } |
628 | unsigned size() const { return HashTable.size(); } |
629 | |
630 | iterator find(StringRef Name) const { return HashTable.find(Name); } |
631 | |
632 | /// Print some statistics to stderr that indicate how well the |
633 | /// hashing is doing. |
634 | void PrintStats() const; |
635 | |
636 | /// Populate the identifier table with info about the language keywords |
637 | /// for the language specified by \p LangOpts. |
638 | void AddKeywords(const LangOptions &LangOpts); |
639 | }; |
640 | |
641 | /// A family of Objective-C methods. |
642 | /// |
643 | /// These families have no inherent meaning in the language, but are |
644 | /// nonetheless central enough in the existing implementations to |
645 | /// merit direct AST support. While, in theory, arbitrary methods can |
646 | /// be considered to form families, we focus here on the methods |
647 | /// involving allocation and retain-count management, as these are the |
648 | /// most "core" and the most likely to be useful to diverse clients |
649 | /// without extra information. |
650 | /// |
651 | /// Both selectors and actual method declarations may be classified |
652 | /// into families. Method families may impose additional restrictions |
653 | /// beyond their selector name; for example, a method called '_init' |
654 | /// that returns void is not considered to be in the 'init' family |
655 | /// (but would be if it returned 'id'). It is also possible to |
656 | /// explicitly change or remove a method's family. Therefore the |
657 | /// method's family should be considered the single source of truth. |
658 | enum ObjCMethodFamily { |
659 | /// No particular method family. |
660 | OMF_None, |
661 | |
662 | // Selectors in these families may have arbitrary arity, may be |
663 | // written with arbitrary leading underscores, and may have |
664 | // additional CamelCase "words" in their first selector chunk |
665 | // following the family name. |
666 | OMF_alloc, |
667 | OMF_copy, |
668 | OMF_init, |
669 | OMF_mutableCopy, |
670 | OMF_new, |
671 | |
672 | // These families are singletons consisting only of the nullary |
673 | // selector with the given name. |
674 | OMF_autorelease, |
675 | OMF_dealloc, |
676 | OMF_finalize, |
677 | OMF_release, |
678 | OMF_retain, |
679 | OMF_retainCount, |
680 | OMF_self, |
681 | OMF_initialize, |
682 | |
683 | // performSelector families |
684 | OMF_performSelector |
685 | }; |
686 | |
687 | /// Enough bits to store any enumerator in ObjCMethodFamily or |
688 | /// InvalidObjCMethodFamily. |
689 | enum { ObjCMethodFamilyBitWidth = 4 }; |
690 | |
691 | /// An invalid value of ObjCMethodFamily. |
692 | enum { InvalidObjCMethodFamily = (1 << ObjCMethodFamilyBitWidth) - 1 }; |
693 | |
694 | /// A family of Objective-C methods. |
695 | /// |
696 | /// These are family of methods whose result type is initially 'id', but |
697 | /// but are candidate for the result type to be changed to 'instancetype'. |
698 | enum ObjCInstanceTypeFamily { |
699 | OIT_None, |
700 | OIT_Array, |
701 | OIT_Dictionary, |
702 | OIT_Singleton, |
703 | OIT_Init, |
704 | OIT_ReturnsSelf |
705 | }; |
706 | |
707 | enum ObjCStringFormatFamily { |
708 | SFF_None, |
709 | SFF_NSString, |
710 | SFF_CFString |
711 | }; |
712 | |
713 | /// Smart pointer class that efficiently represents Objective-C method |
714 | /// names. |
715 | /// |
716 | /// This class will either point to an IdentifierInfo or a |
717 | /// MultiKeywordSelector (which is private). This enables us to optimize |
718 | /// selectors that take no arguments and selectors that take 1 argument, which |
719 | /// accounts for 78% of all selectors in Cocoa.h. |
720 | class Selector { |
721 | friend class Diagnostic; |
722 | friend class SelectorTable; // only the SelectorTable can create these |
723 | friend class DeclarationName; // and the AST's DeclarationName. |
724 | |
725 | enum IdentifierInfoFlag { |
726 | // Empty selector = 0. Note that these enumeration values must |
727 | // correspond to the enumeration values of DeclarationName::StoredNameKind |
728 | ZeroArg = 0x01, |
729 | OneArg = 0x02, |
730 | MultiArg = 0x07, |
731 | ArgFlags = 0x07 |
732 | }; |
733 | |
734 | /// A pointer to the MultiKeywordSelector or IdentifierInfo. We use the low |
735 | /// three bits of InfoPtr to store an IdentifierInfoFlag. Note that in any |
736 | /// case IdentifierInfo and MultiKeywordSelector are already aligned to |
737 | /// 8 bytes even on 32 bits archs because of DeclarationName. |
738 | uintptr_t InfoPtr = 0; |
739 | |
740 | Selector(IdentifierInfo *II, unsigned nArgs) { |
741 | InfoPtr = reinterpret_cast<uintptr_t>(II); |
742 | assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo")(static_cast <bool> ((InfoPtr & ArgFlags) == 0 && "Insufficiently aligned IdentifierInfo") ? void (0) : __assert_fail ("(InfoPtr & ArgFlags) == 0 &&\"Insufficiently aligned IdentifierInfo\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Basic/IdentifierTable.h" , 742, __extension__ __PRETTY_FUNCTION__)); |
743 | assert(nArgs < 2 && "nArgs not equal to 0/1")(static_cast <bool> (nArgs < 2 && "nArgs not equal to 0/1" ) ? void (0) : __assert_fail ("nArgs < 2 && \"nArgs not equal to 0/1\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Basic/IdentifierTable.h" , 743, __extension__ __PRETTY_FUNCTION__)); |
744 | InfoPtr |= nArgs+1; |
745 | } |
746 | |
747 | Selector(MultiKeywordSelector *SI) { |
748 | InfoPtr = reinterpret_cast<uintptr_t>(SI); |
749 | assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo")(static_cast <bool> ((InfoPtr & ArgFlags) == 0 && "Insufficiently aligned IdentifierInfo") ? void (0) : __assert_fail ("(InfoPtr & ArgFlags) == 0 &&\"Insufficiently aligned IdentifierInfo\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Basic/IdentifierTable.h" , 749, __extension__ __PRETTY_FUNCTION__)); |
750 | InfoPtr |= MultiArg; |
751 | } |
752 | |
753 | IdentifierInfo *getAsIdentifierInfo() const { |
754 | if (getIdentifierInfoFlag() < MultiArg) |
755 | return reinterpret_cast<IdentifierInfo *>(InfoPtr & ~ArgFlags); |
756 | return nullptr; |
757 | } |
758 | |
759 | MultiKeywordSelector *getMultiKeywordSelector() const { |
760 | return reinterpret_cast<MultiKeywordSelector *>(InfoPtr & ~ArgFlags); |
761 | } |
762 | |
763 | unsigned getIdentifierInfoFlag() const { |
764 | return InfoPtr & ArgFlags; |
765 | } |
766 | |
767 | static ObjCMethodFamily getMethodFamilyImpl(Selector sel); |
768 | |
769 | static ObjCStringFormatFamily getStringFormatFamilyImpl(Selector sel); |
770 | |
771 | public: |
772 | /// The default ctor should only be used when creating data structures that |
773 | /// will contain selectors. |
774 | Selector() = default; |
775 | explicit Selector(uintptr_t V) : InfoPtr(V) {} |
776 | |
777 | /// operator==/!= - Indicate whether the specified selectors are identical. |
778 | bool operator==(Selector RHS) const { |
779 | return InfoPtr == RHS.InfoPtr; |
780 | } |
781 | bool operator!=(Selector RHS) const { |
782 | return InfoPtr != RHS.InfoPtr; |
783 | } |
784 | |
785 | void *getAsOpaquePtr() const { |
786 | return reinterpret_cast<void*>(InfoPtr); |
787 | } |
788 | |
789 | /// Determine whether this is the empty selector. |
790 | bool isNull() const { return InfoPtr == 0; } |
791 | |
792 | // Predicates to identify the selector type. |
793 | bool isKeywordSelector() const { |
794 | return getIdentifierInfoFlag() != ZeroArg; |
795 | } |
796 | |
797 | bool isUnarySelector() const { |
798 | return getIdentifierInfoFlag() == ZeroArg; |
799 | } |
800 | |
801 | /// If this selector is the specific keyword selector described by Names. |
802 | bool isKeywordSelector(ArrayRef<StringRef> Names) const; |
803 | |
804 | /// If this selector is the specific unary selector described by Name. |
805 | bool isUnarySelector(StringRef Name) const; |
806 | |
807 | unsigned getNumArgs() const; |
808 | |
809 | /// Retrieve the identifier at a given position in the selector. |
810 | /// |
811 | /// Note that the identifier pointer returned may be NULL. Clients that only |
812 | /// care about the text of the identifier string, and not the specific, |
813 | /// uniqued identifier pointer, should use \c getNameForSlot(), which returns |
814 | /// an empty string when the identifier pointer would be NULL. |
815 | /// |
816 | /// \param argIndex The index for which we want to retrieve the identifier. |
817 | /// This index shall be less than \c getNumArgs() unless this is a keyword |
818 | /// selector, in which case 0 is the only permissible value. |
819 | /// |
820 | /// \returns the uniqued identifier for this slot, or NULL if this slot has |
821 | /// no corresponding identifier. |
822 | IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const; |
823 | |
824 | /// Retrieve the name at a given position in the selector. |
825 | /// |
826 | /// \param argIndex The index for which we want to retrieve the name. |
827 | /// This index shall be less than \c getNumArgs() unless this is a keyword |
828 | /// selector, in which case 0 is the only permissible value. |
829 | /// |
830 | /// \returns the name for this slot, which may be the empty string if no |
831 | /// name was supplied. |
832 | StringRef getNameForSlot(unsigned argIndex) const; |
833 | |
834 | /// Derive the full selector name (e.g. "foo:bar:") and return |
835 | /// it as an std::string. |
836 | std::string getAsString() const; |
837 | |
838 | /// Prints the full selector name (e.g. "foo:bar:"). |
839 | void print(llvm::raw_ostream &OS) const; |
840 | |
841 | void dump() const; |
842 | |
843 | /// Derive the conventional family of this method. |
844 | ObjCMethodFamily getMethodFamily() const { |
845 | return getMethodFamilyImpl(*this); |
846 | } |
847 | |
848 | ObjCStringFormatFamily getStringFormatFamily() const { |
849 | return getStringFormatFamilyImpl(*this); |
850 | } |
851 | |
852 | static Selector getEmptyMarker() { |
853 | return Selector(uintptr_t(-1)); |
854 | } |
855 | |
856 | static Selector getTombstoneMarker() { |
857 | return Selector(uintptr_t(-2)); |
858 | } |
859 | |
860 | static ObjCInstanceTypeFamily getInstTypeMethodFamily(Selector sel); |
861 | }; |
862 | |
863 | /// This table allows us to fully hide how we implement |
864 | /// multi-keyword caching. |
865 | class SelectorTable { |
866 | // Actually a SelectorTableImpl |
867 | void *Impl; |
868 | |
869 | public: |
870 | SelectorTable(); |
871 | SelectorTable(const SelectorTable &) = delete; |
872 | SelectorTable &operator=(const SelectorTable &) = delete; |
873 | ~SelectorTable(); |
874 | |
875 | /// Can create any sort of selector. |
876 | /// |
877 | /// \p NumArgs indicates whether this is a no argument selector "foo", a |
878 | /// single argument selector "foo:" or multi-argument "foo:bar:". |
879 | Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV); |
880 | |
881 | Selector getUnarySelector(IdentifierInfo *ID) { |
882 | return Selector(ID, 1); |
883 | } |
884 | |
885 | Selector getNullarySelector(IdentifierInfo *ID) { |
886 | return Selector(ID, 0); |
887 | } |
888 | |
889 | /// Return the total amount of memory allocated for managing selectors. |
890 | size_t getTotalMemory() const; |
891 | |
892 | /// Return the default setter name for the given identifier. |
893 | /// |
894 | /// This is "set" + \p Name where the initial character of \p Name |
895 | /// has been capitalized. |
896 | static SmallString<64> constructSetterName(StringRef Name); |
897 | |
898 | /// Return the default setter selector for the given identifier. |
899 | /// |
900 | /// This is "set" + \p Name where the initial character of \p Name |
901 | /// has been capitalized. |
902 | static Selector constructSetterSelector(IdentifierTable &Idents, |
903 | SelectorTable &SelTable, |
904 | const IdentifierInfo *Name); |
905 | |
906 | /// Return the property name for the given setter selector. |
907 | static std::string getPropertyNameFromSetterSelector(Selector Sel); |
908 | }; |
909 | |
910 | namespace detail { |
911 | |
912 | /// DeclarationNameExtra is used as a base of various uncommon special names. |
913 | /// This class is needed since DeclarationName has not enough space to store |
914 | /// the kind of every possible names. Therefore the kind of common names is |
915 | /// stored directly in DeclarationName, and the kind of uncommon names is |
916 | /// stored in DeclarationNameExtra. It is aligned to 8 bytes because |
917 | /// DeclarationName needs the lower 3 bits to store the kind of common names. |
918 | /// DeclarationNameExtra is tightly coupled to DeclarationName and any change |
919 | /// here is very likely to require changes in DeclarationName(Table). |
920 | class alignas(IdentifierInfoAlignment) DeclarationNameExtra { |
921 | friend class clang::DeclarationName; |
922 | friend class clang::DeclarationNameTable; |
923 | |
924 | protected: |
925 | /// The kind of "extra" information stored in the DeclarationName. See |
926 | /// @c ExtraKindOrNumArgs for an explanation of how these enumerator values |
927 | /// are used. Note that DeclarationName depends on the numerical values |
928 | /// of the enumerators in this enum. See DeclarationName::StoredNameKind |
929 | /// for more info. |
930 | enum ExtraKind { |
931 | CXXDeductionGuideName, |
932 | CXXLiteralOperatorName, |
933 | CXXUsingDirective, |
934 | ObjCMultiArgSelector |
935 | }; |
936 | |
937 | /// ExtraKindOrNumArgs has one of the following meaning: |
938 | /// * The kind of an uncommon C++ special name. This DeclarationNameExtra |
939 | /// is in this case in fact either a CXXDeductionGuideNameExtra or |
940 | /// a CXXLiteralOperatorIdName. |
941 | /// |
942 | /// * It may be also name common to C++ using-directives (CXXUsingDirective), |
943 | /// |
944 | /// * Otherwise it is ObjCMultiArgSelector+NumArgs, where NumArgs is |
945 | /// the number of arguments in the Objective-C selector, in which |
946 | /// case the DeclarationNameExtra is also a MultiKeywordSelector. |
947 | unsigned ExtraKindOrNumArgs; |
948 | |
949 | DeclarationNameExtra(ExtraKind Kind) : ExtraKindOrNumArgs(Kind) {} |
950 | DeclarationNameExtra(unsigned NumArgs) |
951 | : ExtraKindOrNumArgs(ObjCMultiArgSelector + NumArgs) {} |
952 | |
953 | /// Return the corresponding ExtraKind. |
954 | ExtraKind getKind() const { |
955 | return static_cast<ExtraKind>(ExtraKindOrNumArgs > |
956 | (unsigned)ObjCMultiArgSelector |
957 | ? (unsigned)ObjCMultiArgSelector |
958 | : ExtraKindOrNumArgs); |
959 | } |
960 | |
961 | /// Return the number of arguments in an ObjC selector. Only valid when this |
962 | /// is indeed an ObjCMultiArgSelector. |
963 | unsigned getNumArgs() const { |
964 | assert(ExtraKindOrNumArgs >= (unsigned)ObjCMultiArgSelector &&(static_cast <bool> (ExtraKindOrNumArgs >= (unsigned )ObjCMultiArgSelector && "getNumArgs called but this is not an ObjC selector!" ) ? void (0) : __assert_fail ("ExtraKindOrNumArgs >= (unsigned)ObjCMultiArgSelector && \"getNumArgs called but this is not an ObjC selector!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Basic/IdentifierTable.h" , 965, __extension__ __PRETTY_FUNCTION__)) |
965 | "getNumArgs called but this is not an ObjC selector!")(static_cast <bool> (ExtraKindOrNumArgs >= (unsigned )ObjCMultiArgSelector && "getNumArgs called but this is not an ObjC selector!" ) ? void (0) : __assert_fail ("ExtraKindOrNumArgs >= (unsigned)ObjCMultiArgSelector && \"getNumArgs called but this is not an ObjC selector!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/include/clang/Basic/IdentifierTable.h" , 965, __extension__ __PRETTY_FUNCTION__)); |
966 | return ExtraKindOrNumArgs - (unsigned)ObjCMultiArgSelector; |
967 | } |
968 | }; |
969 | |
970 | } // namespace detail |
971 | |
972 | } // namespace clang |
973 | |
974 | namespace llvm { |
975 | |
976 | /// Define DenseMapInfo so that Selectors can be used as keys in DenseMap and |
977 | /// DenseSets. |
978 | template <> |
979 | struct DenseMapInfo<clang::Selector> { |
980 | static clang::Selector getEmptyKey() { |
981 | return clang::Selector::getEmptyMarker(); |
982 | } |
983 | |
984 | static clang::Selector getTombstoneKey() { |
985 | return clang::Selector::getTombstoneMarker(); |
986 | } |
987 | |
988 | static unsigned getHashValue(clang::Selector S); |
989 | |
990 | static bool isEqual(clang::Selector LHS, clang::Selector RHS) { |
991 | return LHS == RHS; |
992 | } |
993 | }; |
994 | |
995 | template<> |
996 | struct PointerLikeTypeTraits<clang::Selector> { |
997 | static const void *getAsVoidPointer(clang::Selector P) { |
998 | return P.getAsOpaquePtr(); |
999 | } |
1000 | |
1001 | static clang::Selector getFromVoidPointer(const void *P) { |
1002 | return clang::Selector(reinterpret_cast<uintptr_t>(P)); |
1003 | } |
1004 | |
1005 | static constexpr int NumLowBitsAvailable = 0; |
1006 | }; |
1007 | |
1008 | // Provide PointerLikeTypeTraits for IdentifierInfo pointers, which |
1009 | // are not guaranteed to be 8-byte aligned. |
1010 | template<> |
1011 | struct PointerLikeTypeTraits<clang::IdentifierInfo*> { |
1012 | static void *getAsVoidPointer(clang::IdentifierInfo* P) { |
1013 | return P; |
1014 | } |
1015 | |
1016 | static clang::IdentifierInfo *getFromVoidPointer(void *P) { |
1017 | return static_cast<clang::IdentifierInfo*>(P); |
1018 | } |
1019 | |
1020 | static constexpr int NumLowBitsAvailable = 1; |
1021 | }; |
1022 | |
1023 | template<> |
1024 | struct PointerLikeTypeTraits<const clang::IdentifierInfo*> { |
1025 | static const void *getAsVoidPointer(const clang::IdentifierInfo* P) { |
1026 | return P; |
1027 | } |
1028 | |
1029 | static const clang::IdentifierInfo *getFromVoidPointer(const void *P) { |
1030 | return static_cast<const clang::IdentifierInfo*>(P); |
1031 | } |
1032 | |
1033 | static constexpr int NumLowBitsAvailable = 1; |
1034 | }; |
1035 | |
1036 | } // namespace llvm |
1037 | |
1038 | #endif // LLVM_CLANG_BASIC_IDENTIFIERTABLE_H |