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