/src/llvm-project/clang/lib/Lex/PPDirectives.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===// |
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 | | /// Implements # directive processing for the Preprocessor. |
11 | | /// |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "clang/Basic/CharInfo.h" |
15 | | #include "clang/Basic/DirectoryEntry.h" |
16 | | #include "clang/Basic/FileManager.h" |
17 | | #include "clang/Basic/IdentifierTable.h" |
18 | | #include "clang/Basic/LangOptions.h" |
19 | | #include "clang/Basic/Module.h" |
20 | | #include "clang/Basic/SourceLocation.h" |
21 | | #include "clang/Basic/SourceManager.h" |
22 | | #include "clang/Basic/TokenKinds.h" |
23 | | #include "clang/Lex/CodeCompletionHandler.h" |
24 | | #include "clang/Lex/HeaderSearch.h" |
25 | | #include "clang/Lex/HeaderSearchOptions.h" |
26 | | #include "clang/Lex/LexDiagnostic.h" |
27 | | #include "clang/Lex/LiteralSupport.h" |
28 | | #include "clang/Lex/MacroInfo.h" |
29 | | #include "clang/Lex/ModuleLoader.h" |
30 | | #include "clang/Lex/ModuleMap.h" |
31 | | #include "clang/Lex/PPCallbacks.h" |
32 | | #include "clang/Lex/Pragma.h" |
33 | | #include "clang/Lex/Preprocessor.h" |
34 | | #include "clang/Lex/PreprocessorOptions.h" |
35 | | #include "clang/Lex/Token.h" |
36 | | #include "clang/Lex/VariadicMacroSupport.h" |
37 | | #include "llvm/ADT/ArrayRef.h" |
38 | | #include "llvm/ADT/STLExtras.h" |
39 | | #include "llvm/ADT/ScopeExit.h" |
40 | | #include "llvm/ADT/SmallString.h" |
41 | | #include "llvm/ADT/SmallVector.h" |
42 | | #include "llvm/ADT/StringRef.h" |
43 | | #include "llvm/ADT/StringSwitch.h" |
44 | | #include "llvm/Support/AlignOf.h" |
45 | | #include "llvm/Support/ErrorHandling.h" |
46 | | #include "llvm/Support/Path.h" |
47 | | #include "llvm/Support/SaveAndRestore.h" |
48 | | #include <algorithm> |
49 | | #include <cassert> |
50 | | #include <cstring> |
51 | | #include <new> |
52 | | #include <optional> |
53 | | #include <string> |
54 | | #include <utility> |
55 | | |
56 | | using namespace clang; |
57 | | |
58 | | //===----------------------------------------------------------------------===// |
59 | | // Utility Methods for Preprocessor Directive Handling. |
60 | | //===----------------------------------------------------------------------===// |
61 | | |
62 | 19.7k | MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) { |
63 | 19.7k | static_assert(std::is_trivially_destructible_v<MacroInfo>, ""); |
64 | 19.7k | return new (BP) MacroInfo(L); |
65 | 19.7k | } |
66 | | |
67 | | DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI, |
68 | 19.7k | SourceLocation Loc) { |
69 | 19.7k | return new (BP) DefMacroDirective(MI, Loc); |
70 | 19.7k | } |
71 | | |
72 | | UndefMacroDirective * |
73 | 0 | Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) { |
74 | 0 | return new (BP) UndefMacroDirective(UndefLoc); |
75 | 0 | } |
76 | | |
77 | | VisibilityMacroDirective * |
78 | | Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc, |
79 | 0 | bool isPublic) { |
80 | 0 | return new (BP) VisibilityMacroDirective(Loc, isPublic); |
81 | 0 | } |
82 | | |
83 | | /// Read and discard all tokens remaining on the current line until |
84 | | /// the tok::eod token is found. |
85 | 5.31k | SourceRange Preprocessor::DiscardUntilEndOfDirective() { |
86 | 5.31k | Token Tmp; |
87 | 5.31k | SourceRange Res; |
88 | | |
89 | 5.31k | LexUnexpandedToken(Tmp); |
90 | 5.31k | Res.setBegin(Tmp.getLocation()); |
91 | 259k | while (Tmp.isNot(tok::eod)) { |
92 | 253k | assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens"); |
93 | 0 | LexUnexpandedToken(Tmp); |
94 | 253k | } |
95 | 5.31k | Res.setEnd(Tmp.getLocation()); |
96 | 5.31k | return Res; |
97 | 5.31k | } |
98 | | |
99 | | /// Enumerates possible cases of #define/#undef a reserved identifier. |
100 | | enum MacroDiag { |
101 | | MD_NoWarn, //> Not a reserved identifier |
102 | | MD_KeywordDef, //> Macro hides keyword, enabled by default |
103 | | MD_ReservedMacro //> #define of #undef reserved id, disabled by default |
104 | | }; |
105 | | |
106 | | /// Enumerates possible %select values for the pp_err_elif_after_else and |
107 | | /// pp_err_elif_without_if diagnostics. |
108 | | enum PPElifDiag { |
109 | | PED_Elif, |
110 | | PED_Elifdef, |
111 | | PED_Elifndef |
112 | | }; |
113 | | |
114 | 0 | static bool isFeatureTestMacro(StringRef MacroName) { |
115 | | // list from: |
116 | | // * https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html |
117 | | // * https://docs.microsoft.com/en-us/cpp/c-runtime-library/security-features-in-the-crt?view=msvc-160 |
118 | | // * man 7 feature_test_macros |
119 | | // The list must be sorted for correct binary search. |
120 | 0 | static constexpr StringRef ReservedMacro[] = { |
121 | 0 | "_ATFILE_SOURCE", |
122 | 0 | "_BSD_SOURCE", |
123 | 0 | "_CRT_NONSTDC_NO_WARNINGS", |
124 | 0 | "_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES", |
125 | 0 | "_CRT_SECURE_NO_WARNINGS", |
126 | 0 | "_FILE_OFFSET_BITS", |
127 | 0 | "_FORTIFY_SOURCE", |
128 | 0 | "_GLIBCXX_ASSERTIONS", |
129 | 0 | "_GLIBCXX_CONCEPT_CHECKS", |
130 | 0 | "_GLIBCXX_DEBUG", |
131 | 0 | "_GLIBCXX_DEBUG_PEDANTIC", |
132 | 0 | "_GLIBCXX_PARALLEL", |
133 | 0 | "_GLIBCXX_PARALLEL_ASSERTIONS", |
134 | 0 | "_GLIBCXX_SANITIZE_VECTOR", |
135 | 0 | "_GLIBCXX_USE_CXX11_ABI", |
136 | 0 | "_GLIBCXX_USE_DEPRECATED", |
137 | 0 | "_GNU_SOURCE", |
138 | 0 | "_ISOC11_SOURCE", |
139 | 0 | "_ISOC95_SOURCE", |
140 | 0 | "_ISOC99_SOURCE", |
141 | 0 | "_LARGEFILE64_SOURCE", |
142 | 0 | "_POSIX_C_SOURCE", |
143 | 0 | "_REENTRANT", |
144 | 0 | "_SVID_SOURCE", |
145 | 0 | "_THREAD_SAFE", |
146 | 0 | "_XOPEN_SOURCE", |
147 | 0 | "_XOPEN_SOURCE_EXTENDED", |
148 | 0 | "__STDCPP_WANT_MATH_SPEC_FUNCS__", |
149 | 0 | "__STDC_FORMAT_MACROS", |
150 | 0 | }; |
151 | 0 | return std::binary_search(std::begin(ReservedMacro), std::end(ReservedMacro), |
152 | 0 | MacroName); |
153 | 0 | } |
154 | | |
155 | | static bool isLanguageDefinedBuiltin(const SourceManager &SourceMgr, |
156 | | const MacroInfo *MI, |
157 | 0 | const StringRef MacroName) { |
158 | | // If this is a macro with special handling (like __LINE__) then it's language |
159 | | // defined. |
160 | 0 | if (MI->isBuiltinMacro()) |
161 | 0 | return true; |
162 | | // Builtin macros are defined in the builtin file |
163 | 0 | if (!SourceMgr.isWrittenInBuiltinFile(MI->getDefinitionLoc())) |
164 | 0 | return false; |
165 | | // C defines macros starting with __STDC, and C++ defines macros starting with |
166 | | // __STDCPP |
167 | 0 | if (MacroName.starts_with("__STDC")) |
168 | 0 | return true; |
169 | | // C++ defines the __cplusplus macro |
170 | 0 | if (MacroName == "__cplusplus") |
171 | 0 | return true; |
172 | | // C++ defines various feature-test macros starting with __cpp |
173 | 0 | if (MacroName.starts_with("__cpp")) |
174 | 0 | return true; |
175 | | // Anything else isn't language-defined |
176 | 0 | return false; |
177 | 0 | } |
178 | | |
179 | 0 | static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) { |
180 | 0 | const LangOptions &Lang = PP.getLangOpts(); |
181 | 0 | StringRef Text = II->getName(); |
182 | 0 | if (isReservedInAllContexts(II->isReserved(Lang))) |
183 | 0 | return isFeatureTestMacro(Text) ? MD_NoWarn : MD_ReservedMacro; |
184 | 0 | if (II->isKeyword(Lang)) |
185 | 0 | return MD_KeywordDef; |
186 | 0 | if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final"))) |
187 | 0 | return MD_KeywordDef; |
188 | 0 | return MD_NoWarn; |
189 | 0 | } |
190 | | |
191 | 0 | static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) { |
192 | 0 | const LangOptions &Lang = PP.getLangOpts(); |
193 | | // Do not warn on keyword undef. It is generally harmless and widely used. |
194 | 0 | if (isReservedInAllContexts(II->isReserved(Lang))) |
195 | 0 | return MD_ReservedMacro; |
196 | 0 | return MD_NoWarn; |
197 | 0 | } |
198 | | |
199 | | // Return true if we want to issue a diagnostic by default if we |
200 | | // encounter this name in a #include with the wrong case. For now, |
201 | | // this includes the standard C and C++ headers, Posix headers, |
202 | | // and Boost headers. Improper case for these #includes is a |
203 | | // potential portability issue. |
204 | 0 | static bool warnByDefaultOnWrongCase(StringRef Include) { |
205 | | // If the first component of the path is "boost", treat this like a standard header |
206 | | // for the purposes of diagnostics. |
207 | 0 | if (::llvm::sys::path::begin(Include)->equals_insensitive("boost")) |
208 | 0 | return true; |
209 | | |
210 | | // "condition_variable" is the longest standard header name at 18 characters. |
211 | | // If the include file name is longer than that, it can't be a standard header. |
212 | 0 | static const size_t MaxStdHeaderNameLen = 18u; |
213 | 0 | if (Include.size() > MaxStdHeaderNameLen) |
214 | 0 | return false; |
215 | | |
216 | | // Lowercase and normalize the search string. |
217 | 0 | SmallString<32> LowerInclude{Include}; |
218 | 0 | for (char &Ch : LowerInclude) { |
219 | | // In the ASCII range? |
220 | 0 | if (static_cast<unsigned char>(Ch) > 0x7f) |
221 | 0 | return false; // Can't be a standard header |
222 | | // ASCII lowercase: |
223 | 0 | if (Ch >= 'A' && Ch <= 'Z') |
224 | 0 | Ch += 'a' - 'A'; |
225 | | // Normalize path separators for comparison purposes. |
226 | 0 | else if (::llvm::sys::path::is_separator(Ch)) |
227 | 0 | Ch = '/'; |
228 | 0 | } |
229 | | |
230 | | // The standard C/C++ and Posix headers |
231 | 0 | return llvm::StringSwitch<bool>(LowerInclude) |
232 | | // C library headers |
233 | 0 | .Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true) |
234 | 0 | .Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true) |
235 | 0 | .Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true) |
236 | 0 | .Cases("stdatomic.h", "stdbool.h", "stdckdint.h", "stddef.h", true) |
237 | 0 | .Cases("stdint.h", "stdio.h", "stdlib.h", "stdnoreturn.h", true) |
238 | 0 | .Cases("string.h", "tgmath.h", "threads.h", "time.h", "uchar.h", true) |
239 | 0 | .Cases("wchar.h", "wctype.h", true) |
240 | | |
241 | | // C++ headers for C library facilities |
242 | 0 | .Cases("cassert", "ccomplex", "cctype", "cerrno", "cfenv", true) |
243 | 0 | .Cases("cfloat", "cinttypes", "ciso646", "climits", "clocale", true) |
244 | 0 | .Cases("cmath", "csetjmp", "csignal", "cstdalign", "cstdarg", true) |
245 | 0 | .Cases("cstdbool", "cstddef", "cstdint", "cstdio", "cstdlib", true) |
246 | 0 | .Cases("cstring", "ctgmath", "ctime", "cuchar", "cwchar", true) |
247 | 0 | .Case("cwctype", true) |
248 | | |
249 | | // C++ library headers |
250 | 0 | .Cases("algorithm", "fstream", "list", "regex", "thread", true) |
251 | 0 | .Cases("array", "functional", "locale", "scoped_allocator", "tuple", true) |
252 | 0 | .Cases("atomic", "future", "map", "set", "type_traits", true) |
253 | 0 | .Cases("bitset", "initializer_list", "memory", "shared_mutex", "typeindex", true) |
254 | 0 | .Cases("chrono", "iomanip", "mutex", "sstream", "typeinfo", true) |
255 | 0 | .Cases("codecvt", "ios", "new", "stack", "unordered_map", true) |
256 | 0 | .Cases("complex", "iosfwd", "numeric", "stdexcept", "unordered_set", true) |
257 | 0 | .Cases("condition_variable", "iostream", "ostream", "streambuf", "utility", true) |
258 | 0 | .Cases("deque", "istream", "queue", "string", "valarray", true) |
259 | 0 | .Cases("exception", "iterator", "random", "strstream", "vector", true) |
260 | 0 | .Cases("forward_list", "limits", "ratio", "system_error", true) |
261 | | |
262 | | // POSIX headers (which aren't also C headers) |
263 | 0 | .Cases("aio.h", "arpa/inet.h", "cpio.h", "dirent.h", "dlfcn.h", true) |
264 | 0 | .Cases("fcntl.h", "fmtmsg.h", "fnmatch.h", "ftw.h", "glob.h", true) |
265 | 0 | .Cases("grp.h", "iconv.h", "langinfo.h", "libgen.h", "monetary.h", true) |
266 | 0 | .Cases("mqueue.h", "ndbm.h", "net/if.h", "netdb.h", "netinet/in.h", true) |
267 | 0 | .Cases("netinet/tcp.h", "nl_types.h", "poll.h", "pthread.h", "pwd.h", true) |
268 | 0 | .Cases("regex.h", "sched.h", "search.h", "semaphore.h", "spawn.h", true) |
269 | 0 | .Cases("strings.h", "stropts.h", "sys/ipc.h", "sys/mman.h", "sys/msg.h", true) |
270 | 0 | .Cases("sys/resource.h", "sys/select.h", "sys/sem.h", "sys/shm.h", "sys/socket.h", true) |
271 | 0 | .Cases("sys/stat.h", "sys/statvfs.h", "sys/time.h", "sys/times.h", "sys/types.h", true) |
272 | 0 | .Cases("sys/uio.h", "sys/un.h", "sys/utsname.h", "sys/wait.h", "syslog.h", true) |
273 | 0 | .Cases("tar.h", "termios.h", "trace.h", "ulimit.h", true) |
274 | 0 | .Cases("unistd.h", "utime.h", "utmpx.h", "wordexp.h", true) |
275 | 0 | .Default(false); |
276 | 0 | } |
277 | | |
278 | | /// Find a similar string in `Candidates`. |
279 | | /// |
280 | | /// \param LHS a string for a similar string in `Candidates` |
281 | | /// |
282 | | /// \param Candidates the candidates to find a similar string. |
283 | | /// |
284 | | /// \returns a similar string if exists. If no similar string exists, |
285 | | /// returns std::nullopt. |
286 | | static std::optional<StringRef> |
287 | 0 | findSimilarStr(StringRef LHS, const std::vector<StringRef> &Candidates) { |
288 | | // We need to check if `Candidates` has the exact case-insensitive string |
289 | | // because the Levenshtein distance match does not care about it. |
290 | 0 | for (StringRef C : Candidates) { |
291 | 0 | if (LHS.equals_insensitive(C)) { |
292 | 0 | return C; |
293 | 0 | } |
294 | 0 | } |
295 | | |
296 | | // Keep going with the Levenshtein distance match. |
297 | | // If the LHS size is less than 3, use the LHS size minus 1 and if not, |
298 | | // use the LHS size divided by 3. |
299 | 0 | size_t Length = LHS.size(); |
300 | 0 | size_t MaxDist = Length < 3 ? Length - 1 : Length / 3; |
301 | |
|
302 | 0 | std::optional<std::pair<StringRef, size_t>> SimilarStr; |
303 | 0 | for (StringRef C : Candidates) { |
304 | 0 | size_t CurDist = LHS.edit_distance(C, true); |
305 | 0 | if (CurDist <= MaxDist) { |
306 | 0 | if (!SimilarStr) { |
307 | | // The first similar string found. |
308 | 0 | SimilarStr = {C, CurDist}; |
309 | 0 | } else if (CurDist < SimilarStr->second) { |
310 | | // More similar string found. |
311 | 0 | SimilarStr = {C, CurDist}; |
312 | 0 | } |
313 | 0 | } |
314 | 0 | } |
315 | |
|
316 | 0 | if (SimilarStr) { |
317 | 0 | return SimilarStr->first; |
318 | 0 | } else { |
319 | 0 | return std::nullopt; |
320 | 0 | } |
321 | 0 | } |
322 | | |
323 | | bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef, |
324 | 18.4k | bool *ShadowFlag) { |
325 | | // Missing macro name? |
326 | 18.4k | if (MacroNameTok.is(tok::eod)) |
327 | 0 | return Diag(MacroNameTok, diag::err_pp_missing_macro_name); |
328 | | |
329 | 18.4k | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
330 | 18.4k | if (!II) |
331 | 0 | return Diag(MacroNameTok, diag::err_pp_macro_not_identifier); |
332 | | |
333 | 18.4k | if (II->isCPlusPlusOperatorKeyword()) { |
334 | | // C++ 2.5p2: Alternative tokens behave the same as its primary token |
335 | | // except for their spellings. |
336 | 0 | Diag(MacroNameTok, getLangOpts().MicrosoftExt |
337 | 0 | ? diag::ext_pp_operator_used_as_macro_name |
338 | 0 | : diag::err_pp_operator_used_as_macro_name) |
339 | 0 | << II << MacroNameTok.getKind(); |
340 | | // Allow #defining |and| and friends for Microsoft compatibility or |
341 | | // recovery when legacy C headers are included in C++. |
342 | 0 | } |
343 | | |
344 | 18.4k | if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) { |
345 | | // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4. |
346 | 0 | return Diag(MacroNameTok, diag::err_defined_macro_name); |
347 | 0 | } |
348 | | |
349 | | // If defining/undefining reserved identifier or a keyword, we need to issue |
350 | | // a warning. |
351 | 18.4k | SourceLocation MacroNameLoc = MacroNameTok.getLocation(); |
352 | 18.4k | if (ShadowFlag) |
353 | 18.4k | *ShadowFlag = false; |
354 | 18.4k | if (!SourceMgr.isInSystemHeader(MacroNameLoc) && |
355 | 18.4k | (SourceMgr.getBufferName(MacroNameLoc) != "<built-in>")) { |
356 | 0 | MacroDiag D = MD_NoWarn; |
357 | 0 | if (isDefineUndef == MU_Define) { |
358 | 0 | D = shouldWarnOnMacroDef(*this, II); |
359 | 0 | } |
360 | 0 | else if (isDefineUndef == MU_Undef) |
361 | 0 | D = shouldWarnOnMacroUndef(*this, II); |
362 | 0 | if (D == MD_KeywordDef) { |
363 | | // We do not want to warn on some patterns widely used in configuration |
364 | | // scripts. This requires analyzing next tokens, so do not issue warnings |
365 | | // now, only inform caller. |
366 | 0 | if (ShadowFlag) |
367 | 0 | *ShadowFlag = true; |
368 | 0 | } |
369 | 0 | if (D == MD_ReservedMacro) |
370 | 0 | Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id); |
371 | 0 | } |
372 | | |
373 | | // Okay, we got a good identifier. |
374 | 18.4k | return false; |
375 | 18.4k | } |
376 | | |
377 | | /// Lex and validate a macro name, which occurs after a |
378 | | /// \#define or \#undef. |
379 | | /// |
380 | | /// This sets the token kind to eod and discards the rest of the macro line if |
381 | | /// the macro name is invalid. |
382 | | /// |
383 | | /// \param MacroNameTok Token that is expected to be a macro name. |
384 | | /// \param isDefineUndef Context in which macro is used. |
385 | | /// \param ShadowFlag Points to a flag that is set if macro shadows a keyword. |
386 | | void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef, |
387 | 18.4k | bool *ShadowFlag) { |
388 | | // Read the token, don't allow macro expansion on it. |
389 | 18.4k | LexUnexpandedToken(MacroNameTok); |
390 | | |
391 | 18.4k | if (MacroNameTok.is(tok::code_completion)) { |
392 | 0 | if (CodeComplete) |
393 | 0 | CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define); |
394 | 0 | setCodeCompletionReached(); |
395 | 0 | LexUnexpandedToken(MacroNameTok); |
396 | 0 | } |
397 | | |
398 | 18.4k | if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag)) |
399 | 18.4k | return; |
400 | | |
401 | | // Invalid macro name, read and discard the rest of the line and set the |
402 | | // token kind to tok::eod if necessary. |
403 | 0 | if (MacroNameTok.isNot(tok::eod)) { |
404 | 0 | MacroNameTok.setKind(tok::eod); |
405 | 0 | DiscardUntilEndOfDirective(); |
406 | 0 | } |
407 | 0 | } |
408 | | |
409 | | /// Ensure that the next token is a tok::eod token. |
410 | | /// |
411 | | /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is |
412 | | /// true, then we consider macros that expand to zero tokens as being ok. |
413 | | /// |
414 | | /// Returns the location of the end of the directive. |
415 | | SourceLocation Preprocessor::CheckEndOfDirective(const char *DirType, |
416 | 0 | bool EnableMacros) { |
417 | 0 | Token Tmp; |
418 | | // Lex unexpanded tokens for most directives: macros might expand to zero |
419 | | // tokens, causing us to miss diagnosing invalid lines. Some directives (like |
420 | | // #line) allow empty macros. |
421 | 0 | if (EnableMacros) |
422 | 0 | Lex(Tmp); |
423 | 0 | else |
424 | 0 | LexUnexpandedToken(Tmp); |
425 | | |
426 | | // There should be no tokens after the directive, but we allow them as an |
427 | | // extension. |
428 | 0 | while (Tmp.is(tok::comment)) // Skip comments in -C mode. |
429 | 0 | LexUnexpandedToken(Tmp); |
430 | |
|
431 | 0 | if (Tmp.is(tok::eod)) |
432 | 0 | return Tmp.getLocation(); |
433 | | |
434 | | // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89, |
435 | | // or if this is a macro-style preprocessing directive, because it is more |
436 | | // trouble than it is worth to insert /**/ and check that there is no /**/ |
437 | | // in the range also. |
438 | 0 | FixItHint Hint; |
439 | 0 | if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) && |
440 | 0 | !CurTokenLexer) |
441 | 0 | Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//"); |
442 | 0 | Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint; |
443 | 0 | return DiscardUntilEndOfDirective().getEnd(); |
444 | 0 | } |
445 | | |
446 | | void Preprocessor::SuggestTypoedDirective(const Token &Tok, |
447 | 0 | StringRef Directive) const { |
448 | | // If this is a `.S` file, treat unknown # directives as non-preprocessor |
449 | | // directives. |
450 | 0 | if (getLangOpts().AsmPreprocessor) return; |
451 | | |
452 | 0 | std::vector<StringRef> Candidates = { |
453 | 0 | "if", "ifdef", "ifndef", "elif", "else", "endif" |
454 | 0 | }; |
455 | 0 | if (LangOpts.C23 || LangOpts.CPlusPlus23) |
456 | 0 | Candidates.insert(Candidates.end(), {"elifdef", "elifndef"}); |
457 | |
|
458 | 0 | if (std::optional<StringRef> Sugg = findSimilarStr(Directive, Candidates)) { |
459 | | // Directive cannot be coming from macro. |
460 | 0 | assert(Tok.getLocation().isFileID()); |
461 | 0 | CharSourceRange DirectiveRange = CharSourceRange::getCharRange( |
462 | 0 | Tok.getLocation(), |
463 | 0 | Tok.getLocation().getLocWithOffset(Directive.size())); |
464 | 0 | StringRef SuggValue = *Sugg; |
465 | |
|
466 | 0 | auto Hint = FixItHint::CreateReplacement(DirectiveRange, SuggValue); |
467 | 0 | Diag(Tok, diag::warn_pp_invalid_directive) << 1 << SuggValue << Hint; |
468 | 0 | } |
469 | 0 | } |
470 | | |
471 | | /// SkipExcludedConditionalBlock - We just read a \#if or related directive and |
472 | | /// decided that the subsequent tokens are in the \#if'd out portion of the |
473 | | /// file. Lex the rest of the file, until we see an \#endif. If |
474 | | /// FoundNonSkipPortion is true, then we have already emitted code for part of |
475 | | /// this \#if directive, so \#else/\#elif blocks should never be entered. |
476 | | /// If ElseOk is true, then \#else directives are ok, if not, then we have |
477 | | /// already seen one so a \#else directive is a duplicate. When this returns, |
478 | | /// the caller can lex the first valid token. |
479 | | void Preprocessor::SkipExcludedConditionalBlock(SourceLocation HashTokenLoc, |
480 | | SourceLocation IfTokenLoc, |
481 | | bool FoundNonSkipPortion, |
482 | | bool FoundElse, |
483 | 0 | SourceLocation ElseLoc) { |
484 | | // In SkippingRangeStateTy we are depending on SkipExcludedConditionalBlock() |
485 | | // not getting called recursively by storing the RecordedSkippedRanges |
486 | | // DenseMap lookup pointer (field SkipRangePtr). SkippingRangeStateTy expects |
487 | | // that RecordedSkippedRanges won't get modified and SkipRangePtr won't be |
488 | | // invalidated. If this changes and there is a need to call |
489 | | // SkipExcludedConditionalBlock() recursively, SkippingRangeStateTy should |
490 | | // change to do a second lookup in endLexPass function instead of reusing the |
491 | | // lookup pointer. |
492 | 0 | assert(!SkippingExcludedConditionalBlock && |
493 | 0 | "calling SkipExcludedConditionalBlock recursively"); |
494 | 0 | llvm::SaveAndRestore SARSkipping(SkippingExcludedConditionalBlock, true); |
495 | |
|
496 | 0 | ++NumSkipped; |
497 | 0 | assert(!CurTokenLexer && "Conditional PP block cannot appear in a macro!"); |
498 | 0 | assert(CurPPLexer && "Conditional PP block must be in a file!"); |
499 | 0 | assert(CurLexer && "Conditional PP block but no current lexer set!"); |
500 | | |
501 | 0 | if (PreambleConditionalStack.reachedEOFWhileSkipping()) |
502 | 0 | PreambleConditionalStack.clearSkipInfo(); |
503 | 0 | else |
504 | 0 | CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/ false, |
505 | 0 | FoundNonSkipPortion, FoundElse); |
506 | | |
507 | | // Enter raw mode to disable identifier lookup (and thus macro expansion), |
508 | | // disabling warnings, etc. |
509 | 0 | CurPPLexer->LexingRawMode = true; |
510 | 0 | Token Tok; |
511 | 0 | SourceLocation endLoc; |
512 | | |
513 | | /// Keeps track and caches skipped ranges and also retrieves a prior skipped |
514 | | /// range if the same block is re-visited. |
515 | 0 | struct SkippingRangeStateTy { |
516 | 0 | Preprocessor &PP; |
517 | |
|
518 | 0 | const char *BeginPtr = nullptr; |
519 | 0 | unsigned *SkipRangePtr = nullptr; |
520 | |
|
521 | 0 | SkippingRangeStateTy(Preprocessor &PP) : PP(PP) {} |
522 | |
|
523 | 0 | void beginLexPass() { |
524 | 0 | if (BeginPtr) |
525 | 0 | return; // continue skipping a block. |
526 | | |
527 | | // Initiate a skipping block and adjust the lexer if we already skipped it |
528 | | // before. |
529 | 0 | BeginPtr = PP.CurLexer->getBufferLocation(); |
530 | 0 | SkipRangePtr = &PP.RecordedSkippedRanges[BeginPtr]; |
531 | 0 | if (*SkipRangePtr) { |
532 | 0 | PP.CurLexer->seek(PP.CurLexer->getCurrentBufferOffset() + *SkipRangePtr, |
533 | 0 | /*IsAtStartOfLine*/ true); |
534 | 0 | } |
535 | 0 | } |
536 | |
|
537 | 0 | void endLexPass(const char *Hashptr) { |
538 | 0 | if (!BeginPtr) { |
539 | | // Not doing normal lexing. |
540 | 0 | assert(PP.CurLexer->isDependencyDirectivesLexer()); |
541 | 0 | return; |
542 | 0 | } |
543 | | |
544 | | // Finished skipping a block, record the range if it's first time visited. |
545 | 0 | if (!*SkipRangePtr) { |
546 | 0 | *SkipRangePtr = Hashptr - BeginPtr; |
547 | 0 | } |
548 | 0 | assert(*SkipRangePtr == Hashptr - BeginPtr); |
549 | 0 | BeginPtr = nullptr; |
550 | 0 | SkipRangePtr = nullptr; |
551 | 0 | } |
552 | 0 | } SkippingRangeState(*this); |
553 | |
|
554 | 0 | while (true) { |
555 | 0 | if (CurLexer->isDependencyDirectivesLexer()) { |
556 | 0 | CurLexer->LexDependencyDirectiveTokenWhileSkipping(Tok); |
557 | 0 | } else { |
558 | 0 | SkippingRangeState.beginLexPass(); |
559 | 0 | while (true) { |
560 | 0 | CurLexer->Lex(Tok); |
561 | |
|
562 | 0 | if (Tok.is(tok::code_completion)) { |
563 | 0 | setCodeCompletionReached(); |
564 | 0 | if (CodeComplete) |
565 | 0 | CodeComplete->CodeCompleteInConditionalExclusion(); |
566 | 0 | continue; |
567 | 0 | } |
568 | | |
569 | | // If this is the end of the buffer, we have an error. |
570 | 0 | if (Tok.is(tok::eof)) { |
571 | | // We don't emit errors for unterminated conditionals here, |
572 | | // Lexer::LexEndOfFile can do that properly. |
573 | | // Just return and let the caller lex after this #include. |
574 | 0 | if (PreambleConditionalStack.isRecording()) |
575 | 0 | PreambleConditionalStack.SkipInfo.emplace(HashTokenLoc, IfTokenLoc, |
576 | 0 | FoundNonSkipPortion, |
577 | 0 | FoundElse, ElseLoc); |
578 | 0 | break; |
579 | 0 | } |
580 | | |
581 | | // If this token is not a preprocessor directive, just skip it. |
582 | 0 | if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) |
583 | 0 | continue; |
584 | | |
585 | 0 | break; |
586 | 0 | } |
587 | 0 | } |
588 | 0 | if (Tok.is(tok::eof)) |
589 | 0 | break; |
590 | | |
591 | | // We just parsed a # character at the start of a line, so we're in |
592 | | // directive mode. Tell the lexer this so any newlines we see will be |
593 | | // converted into an EOD token (this terminates the macro). |
594 | 0 | CurPPLexer->ParsingPreprocessorDirective = true; |
595 | 0 | if (CurLexer) CurLexer->SetKeepWhitespaceMode(false); |
596 | |
|
597 | 0 | assert(Tok.is(tok::hash)); |
598 | 0 | const char *Hashptr = CurLexer->getBufferLocation() - Tok.getLength(); |
599 | 0 | assert(CurLexer->getSourceLocation(Hashptr) == Tok.getLocation()); |
600 | | |
601 | | // Read the next token, the directive flavor. |
602 | 0 | LexUnexpandedToken(Tok); |
603 | | |
604 | | // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or |
605 | | // something bogus), skip it. |
606 | 0 | if (Tok.isNot(tok::raw_identifier)) { |
607 | 0 | CurPPLexer->ParsingPreprocessorDirective = false; |
608 | | // Restore comment saving mode. |
609 | 0 | if (CurLexer) CurLexer->resetExtendedTokenMode(); |
610 | 0 | continue; |
611 | 0 | } |
612 | | |
613 | | // If the first letter isn't i or e, it isn't intesting to us. We know that |
614 | | // this is safe in the face of spelling differences, because there is no way |
615 | | // to spell an i/e in a strange way that is another letter. Skipping this |
616 | | // allows us to avoid looking up the identifier info for #define/#undef and |
617 | | // other common directives. |
618 | 0 | StringRef RI = Tok.getRawIdentifier(); |
619 | |
|
620 | 0 | char FirstChar = RI[0]; |
621 | 0 | if (FirstChar >= 'a' && FirstChar <= 'z' && |
622 | 0 | FirstChar != 'i' && FirstChar != 'e') { |
623 | 0 | CurPPLexer->ParsingPreprocessorDirective = false; |
624 | | // Restore comment saving mode. |
625 | 0 | if (CurLexer) CurLexer->resetExtendedTokenMode(); |
626 | 0 | continue; |
627 | 0 | } |
628 | | |
629 | | // Get the identifier name without trigraphs or embedded newlines. Note |
630 | | // that we can't use Tok.getIdentifierInfo() because its lookup is disabled |
631 | | // when skipping. |
632 | 0 | char DirectiveBuf[20]; |
633 | 0 | StringRef Directive; |
634 | 0 | if (!Tok.needsCleaning() && RI.size() < 20) { |
635 | 0 | Directive = RI; |
636 | 0 | } else { |
637 | 0 | std::string DirectiveStr = getSpelling(Tok); |
638 | 0 | size_t IdLen = DirectiveStr.size(); |
639 | 0 | if (IdLen >= 20) { |
640 | 0 | CurPPLexer->ParsingPreprocessorDirective = false; |
641 | | // Restore comment saving mode. |
642 | 0 | if (CurLexer) CurLexer->resetExtendedTokenMode(); |
643 | 0 | continue; |
644 | 0 | } |
645 | 0 | memcpy(DirectiveBuf, &DirectiveStr[0], IdLen); |
646 | 0 | Directive = StringRef(DirectiveBuf, IdLen); |
647 | 0 | } |
648 | | |
649 | 0 | if (Directive.starts_with("if")) { |
650 | 0 | StringRef Sub = Directive.substr(2); |
651 | 0 | if (Sub.empty() || // "if" |
652 | 0 | Sub == "def" || // "ifdef" |
653 | 0 | Sub == "ndef") { // "ifndef" |
654 | | // We know the entire #if/#ifdef/#ifndef block will be skipped, don't |
655 | | // bother parsing the condition. |
656 | 0 | DiscardUntilEndOfDirective(); |
657 | 0 | CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, |
658 | 0 | /*foundnonskip*/false, |
659 | 0 | /*foundelse*/false); |
660 | 0 | } else { |
661 | 0 | SuggestTypoedDirective(Tok, Directive); |
662 | 0 | } |
663 | 0 | } else if (Directive[0] == 'e') { |
664 | 0 | StringRef Sub = Directive.substr(1); |
665 | 0 | if (Sub == "ndif") { // "endif" |
666 | 0 | PPConditionalInfo CondInfo; |
667 | 0 | CondInfo.WasSkipping = true; // Silence bogus warning. |
668 | 0 | bool InCond = CurPPLexer->popConditionalLevel(CondInfo); |
669 | 0 | (void)InCond; // Silence warning in no-asserts mode. |
670 | 0 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
671 | | |
672 | | // If we popped the outermost skipping block, we're done skipping! |
673 | 0 | if (!CondInfo.WasSkipping) { |
674 | 0 | SkippingRangeState.endLexPass(Hashptr); |
675 | | // Restore the value of LexingRawMode so that trailing comments |
676 | | // are handled correctly, if we've reached the outermost block. |
677 | 0 | CurPPLexer->LexingRawMode = false; |
678 | 0 | endLoc = CheckEndOfDirective("endif"); |
679 | 0 | CurPPLexer->LexingRawMode = true; |
680 | 0 | if (Callbacks) |
681 | 0 | Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc); |
682 | 0 | break; |
683 | 0 | } else { |
684 | 0 | DiscardUntilEndOfDirective(); |
685 | 0 | } |
686 | 0 | } else if (Sub == "lse") { // "else". |
687 | | // #else directive in a skipping conditional. If not in some other |
688 | | // skipping conditional, and if #else hasn't already been seen, enter it |
689 | | // as a non-skipping conditional. |
690 | 0 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
691 | |
|
692 | 0 | if (!CondInfo.WasSkipping) |
693 | 0 | SkippingRangeState.endLexPass(Hashptr); |
694 | | |
695 | | // If this is a #else with a #else before it, report the error. |
696 | 0 | if (CondInfo.FoundElse) |
697 | 0 | Diag(Tok, diag::pp_err_else_after_else); |
698 | | |
699 | | // Note that we've seen a #else in this conditional. |
700 | 0 | CondInfo.FoundElse = true; |
701 | | |
702 | | // If the conditional is at the top level, and the #if block wasn't |
703 | | // entered, enter the #else block now. |
704 | 0 | if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { |
705 | 0 | CondInfo.FoundNonSkip = true; |
706 | | // Restore the value of LexingRawMode so that trailing comments |
707 | | // are handled correctly. |
708 | 0 | CurPPLexer->LexingRawMode = false; |
709 | 0 | endLoc = CheckEndOfDirective("else"); |
710 | 0 | CurPPLexer->LexingRawMode = true; |
711 | 0 | if (Callbacks) |
712 | 0 | Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc); |
713 | 0 | break; |
714 | 0 | } else { |
715 | 0 | DiscardUntilEndOfDirective(); // C99 6.10p4. |
716 | 0 | } |
717 | 0 | } else if (Sub == "lif") { // "elif". |
718 | 0 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
719 | |
|
720 | 0 | if (!CondInfo.WasSkipping) |
721 | 0 | SkippingRangeState.endLexPass(Hashptr); |
722 | | |
723 | | // If this is a #elif with a #else before it, report the error. |
724 | 0 | if (CondInfo.FoundElse) |
725 | 0 | Diag(Tok, diag::pp_err_elif_after_else) << PED_Elif; |
726 | | |
727 | | // If this is in a skipping block or if we're already handled this #if |
728 | | // block, don't bother parsing the condition. |
729 | 0 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
730 | | // FIXME: We should probably do at least some minimal parsing of the |
731 | | // condition to verify that it is well-formed. The current state |
732 | | // allows #elif* directives with completely malformed (or missing) |
733 | | // conditions. |
734 | 0 | DiscardUntilEndOfDirective(); |
735 | 0 | } else { |
736 | | // Restore the value of LexingRawMode so that identifiers are |
737 | | // looked up, etc, inside the #elif expression. |
738 | 0 | assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); |
739 | 0 | CurPPLexer->LexingRawMode = false; |
740 | 0 | IdentifierInfo *IfNDefMacro = nullptr; |
741 | 0 | DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro); |
742 | | // Stop if Lexer became invalid after hitting code completion token. |
743 | 0 | if (!CurPPLexer) |
744 | 0 | return; |
745 | 0 | const bool CondValue = DER.Conditional; |
746 | 0 | CurPPLexer->LexingRawMode = true; |
747 | 0 | if (Callbacks) { |
748 | 0 | Callbacks->Elif( |
749 | 0 | Tok.getLocation(), DER.ExprRange, |
750 | 0 | (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), |
751 | 0 | CondInfo.IfLoc); |
752 | 0 | } |
753 | | // If this condition is true, enter it! |
754 | 0 | if (CondValue) { |
755 | 0 | CondInfo.FoundNonSkip = true; |
756 | 0 | break; |
757 | 0 | } |
758 | 0 | } |
759 | 0 | } else if (Sub == "lifdef" || // "elifdef" |
760 | 0 | Sub == "lifndef") { // "elifndef" |
761 | 0 | bool IsElifDef = Sub == "lifdef"; |
762 | 0 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
763 | 0 | Token DirectiveToken = Tok; |
764 | |
|
765 | 0 | if (!CondInfo.WasSkipping) |
766 | 0 | SkippingRangeState.endLexPass(Hashptr); |
767 | | |
768 | | // Warn if using `#elifdef` & `#elifndef` in not C23 & C++23 mode even |
769 | | // if this branch is in a skipping block. |
770 | 0 | unsigned DiagID; |
771 | 0 | if (LangOpts.CPlusPlus) |
772 | 0 | DiagID = LangOpts.CPlusPlus23 ? diag::warn_cxx23_compat_pp_directive |
773 | 0 | : diag::ext_cxx23_pp_directive; |
774 | 0 | else |
775 | 0 | DiagID = LangOpts.C23 ? diag::warn_c23_compat_pp_directive |
776 | 0 | : diag::ext_c23_pp_directive; |
777 | 0 | Diag(Tok, DiagID) << (IsElifDef ? PED_Elifdef : PED_Elifndef); |
778 | | |
779 | | // If this is a #elif with a #else before it, report the error. |
780 | 0 | if (CondInfo.FoundElse) |
781 | 0 | Diag(Tok, diag::pp_err_elif_after_else) |
782 | 0 | << (IsElifDef ? PED_Elifdef : PED_Elifndef); |
783 | | |
784 | | // If this is in a skipping block or if we're already handled this #if |
785 | | // block, don't bother parsing the condition. |
786 | 0 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
787 | | // FIXME: We should probably do at least some minimal parsing of the |
788 | | // condition to verify that it is well-formed. The current state |
789 | | // allows #elif* directives with completely malformed (or missing) |
790 | | // conditions. |
791 | 0 | DiscardUntilEndOfDirective(); |
792 | 0 | } else { |
793 | | // Restore the value of LexingRawMode so that identifiers are |
794 | | // looked up, etc, inside the #elif[n]def expression. |
795 | 0 | assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); |
796 | 0 | CurPPLexer->LexingRawMode = false; |
797 | 0 | Token MacroNameTok; |
798 | 0 | ReadMacroName(MacroNameTok); |
799 | 0 | CurPPLexer->LexingRawMode = true; |
800 | | |
801 | | // If the macro name token is tok::eod, there was an error that was |
802 | | // already reported. |
803 | 0 | if (MacroNameTok.is(tok::eod)) { |
804 | | // Skip code until we get to #endif. This helps with recovery by |
805 | | // not emitting an error when the #endif is reached. |
806 | 0 | continue; |
807 | 0 | } |
808 | | |
809 | 0 | emitMacroExpansionWarnings(MacroNameTok); |
810 | |
|
811 | 0 | CheckEndOfDirective(IsElifDef ? "elifdef" : "elifndef"); |
812 | |
|
813 | 0 | IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); |
814 | 0 | auto MD = getMacroDefinition(MII); |
815 | 0 | MacroInfo *MI = MD.getMacroInfo(); |
816 | |
|
817 | 0 | if (Callbacks) { |
818 | 0 | if (IsElifDef) { |
819 | 0 | Callbacks->Elifdef(DirectiveToken.getLocation(), MacroNameTok, |
820 | 0 | MD); |
821 | 0 | } else { |
822 | 0 | Callbacks->Elifndef(DirectiveToken.getLocation(), MacroNameTok, |
823 | 0 | MD); |
824 | 0 | } |
825 | 0 | } |
826 | | // If this condition is true, enter it! |
827 | 0 | if (static_cast<bool>(MI) == IsElifDef) { |
828 | 0 | CondInfo.FoundNonSkip = true; |
829 | 0 | break; |
830 | 0 | } |
831 | 0 | } |
832 | 0 | } else { |
833 | 0 | SuggestTypoedDirective(Tok, Directive); |
834 | 0 | } |
835 | 0 | } else { |
836 | 0 | SuggestTypoedDirective(Tok, Directive); |
837 | 0 | } |
838 | | |
839 | 0 | CurPPLexer->ParsingPreprocessorDirective = false; |
840 | | // Restore comment saving mode. |
841 | 0 | if (CurLexer) CurLexer->resetExtendedTokenMode(); |
842 | 0 | } |
843 | | |
844 | | // Finally, if we are out of the conditional (saw an #endif or ran off the end |
845 | | // of the file, just stop skipping and return to lexing whatever came after |
846 | | // the #if block. |
847 | 0 | CurPPLexer->LexingRawMode = false; |
848 | | |
849 | | // The last skipped range isn't actually skipped yet if it's truncated |
850 | | // by the end of the preamble; we'll resume parsing after the preamble. |
851 | 0 | if (Callbacks && (Tok.isNot(tok::eof) || !isRecordingPreamble())) |
852 | 0 | Callbacks->SourceRangeSkipped( |
853 | 0 | SourceRange(HashTokenLoc, endLoc.isValid() |
854 | 0 | ? endLoc |
855 | 0 | : CurPPLexer->getSourceLocation()), |
856 | 0 | Tok.getLocation()); |
857 | 0 | } |
858 | | |
859 | | Module *Preprocessor::getModuleForLocation(SourceLocation Loc, |
860 | 0 | bool AllowTextual) { |
861 | 0 | if (!SourceMgr.isInMainFile(Loc)) { |
862 | | // Try to determine the module of the include directive. |
863 | | // FIXME: Look into directly passing the FileEntry from LookupFile instead. |
864 | 0 | FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc)); |
865 | 0 | if (auto EntryOfIncl = SourceMgr.getFileEntryRefForID(IDOfIncl)) { |
866 | | // The include comes from an included file. |
867 | 0 | return HeaderInfo.getModuleMap() |
868 | 0 | .findModuleForHeader(*EntryOfIncl, AllowTextual) |
869 | 0 | .getModule(); |
870 | 0 | } |
871 | 0 | } |
872 | | |
873 | | // This is either in the main file or not in a file at all. It belongs |
874 | | // to the current module, if there is one. |
875 | 0 | return getLangOpts().CurrentModule.empty() |
876 | 0 | ? nullptr |
877 | 0 | : HeaderInfo.lookupModule(getLangOpts().CurrentModule, Loc); |
878 | 0 | } |
879 | | |
880 | | OptionalFileEntryRef |
881 | | Preprocessor::getHeaderToIncludeForDiagnostics(SourceLocation IncLoc, |
882 | 0 | SourceLocation Loc) { |
883 | 0 | Module *IncM = getModuleForLocation( |
884 | 0 | IncLoc, LangOpts.ModulesValidateTextualHeaderIncludes); |
885 | | |
886 | | // Walk up through the include stack, looking through textual headers of M |
887 | | // until we hit a non-textual header that we can #include. (We assume textual |
888 | | // headers of a module with non-textual headers aren't meant to be used to |
889 | | // import entities from the module.) |
890 | 0 | auto &SM = getSourceManager(); |
891 | 0 | while (!Loc.isInvalid() && !SM.isInMainFile(Loc)) { |
892 | 0 | auto ID = SM.getFileID(SM.getExpansionLoc(Loc)); |
893 | 0 | auto FE = SM.getFileEntryRefForID(ID); |
894 | 0 | if (!FE) |
895 | 0 | break; |
896 | | |
897 | | // We want to find all possible modules that might contain this header, so |
898 | | // search all enclosing directories for module maps and load them. |
899 | 0 | HeaderInfo.hasModuleMap(FE->getName(), /*Root*/ nullptr, |
900 | 0 | SourceMgr.isInSystemHeader(Loc)); |
901 | |
|
902 | 0 | bool InPrivateHeader = false; |
903 | 0 | for (auto Header : HeaderInfo.findAllModulesForHeader(*FE)) { |
904 | 0 | if (!Header.isAccessibleFrom(IncM)) { |
905 | | // It's in a private header; we can't #include it. |
906 | | // FIXME: If there's a public header in some module that re-exports it, |
907 | | // then we could suggest including that, but it's not clear that's the |
908 | | // expected way to make this entity visible. |
909 | 0 | InPrivateHeader = true; |
910 | 0 | continue; |
911 | 0 | } |
912 | | |
913 | | // Don't suggest explicitly excluded headers. |
914 | 0 | if (Header.getRole() == ModuleMap::ExcludedHeader) |
915 | 0 | continue; |
916 | | |
917 | | // We'll suggest including textual headers below if they're |
918 | | // include-guarded. |
919 | 0 | if (Header.getRole() & ModuleMap::TextualHeader) |
920 | 0 | continue; |
921 | | |
922 | | // If we have a module import syntax, we shouldn't include a header to |
923 | | // make a particular module visible. Let the caller know they should |
924 | | // suggest an import instead. |
925 | 0 | if (getLangOpts().ObjC || getLangOpts().CPlusPlusModules) |
926 | 0 | return std::nullopt; |
927 | | |
928 | | // If this is an accessible, non-textual header of M's top-level module |
929 | | // that transitively includes the given location and makes the |
930 | | // corresponding module visible, this is the thing to #include. |
931 | 0 | return *FE; |
932 | 0 | } |
933 | | |
934 | | // FIXME: If we're bailing out due to a private header, we shouldn't suggest |
935 | | // an import either. |
936 | 0 | if (InPrivateHeader) |
937 | 0 | return std::nullopt; |
938 | | |
939 | | // If the header is includable and has an include guard, assume the |
940 | | // intended way to expose its contents is by #include, not by importing a |
941 | | // module that transitively includes it. |
942 | 0 | if (getHeaderSearchInfo().isFileMultipleIncludeGuarded(*FE)) |
943 | 0 | return *FE; |
944 | | |
945 | 0 | Loc = SM.getIncludeLoc(ID); |
946 | 0 | } |
947 | | |
948 | 0 | return std::nullopt; |
949 | 0 | } |
950 | | |
951 | | OptionalFileEntryRef Preprocessor::LookupFile( |
952 | | SourceLocation FilenameLoc, StringRef Filename, bool isAngled, |
953 | | ConstSearchDirIterator FromDir, const FileEntry *FromFile, |
954 | | ConstSearchDirIterator *CurDirArg, SmallVectorImpl<char> *SearchPath, |
955 | | SmallVectorImpl<char> *RelativePath, |
956 | | ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, |
957 | 0 | bool *IsFrameworkFound, bool SkipCache, bool OpenFile, bool CacheFailures) { |
958 | 0 | ConstSearchDirIterator CurDirLocal = nullptr; |
959 | 0 | ConstSearchDirIterator &CurDir = CurDirArg ? *CurDirArg : CurDirLocal; |
960 | |
|
961 | 0 | Module *RequestingModule = getModuleForLocation( |
962 | 0 | FilenameLoc, LangOpts.ModulesValidateTextualHeaderIncludes); |
963 | | |
964 | | // If the header lookup mechanism may be relative to the current inclusion |
965 | | // stack, record the parent #includes. |
966 | 0 | SmallVector<std::pair<OptionalFileEntryRef, DirectoryEntryRef>, 16> Includers; |
967 | 0 | bool BuildSystemModule = false; |
968 | 0 | if (!FromDir && !FromFile) { |
969 | 0 | FileID FID = getCurrentFileLexer()->getFileID(); |
970 | 0 | OptionalFileEntryRef FileEnt = SourceMgr.getFileEntryRefForID(FID); |
971 | | |
972 | | // If there is no file entry associated with this file, it must be the |
973 | | // predefines buffer or the module includes buffer. Any other file is not |
974 | | // lexed with a normal lexer, so it won't be scanned for preprocessor |
975 | | // directives. |
976 | | // |
977 | | // If we have the predefines buffer, resolve #include references (which come |
978 | | // from the -include command line argument) from the current working |
979 | | // directory instead of relative to the main file. |
980 | | // |
981 | | // If we have the module includes buffer, resolve #include references (which |
982 | | // come from header declarations in the module map) relative to the module |
983 | | // map file. |
984 | 0 | if (!FileEnt) { |
985 | 0 | if (FID == SourceMgr.getMainFileID() && MainFileDir) { |
986 | 0 | auto IncludeDir = |
987 | 0 | HeaderInfo.getModuleMap().shouldImportRelativeToBuiltinIncludeDir( |
988 | 0 | Filename, getCurrentModule()) |
989 | 0 | ? HeaderInfo.getModuleMap().getBuiltinDir() |
990 | 0 | : MainFileDir; |
991 | 0 | Includers.push_back(std::make_pair(std::nullopt, *IncludeDir)); |
992 | 0 | BuildSystemModule = getCurrentModule()->IsSystem; |
993 | 0 | } else if ((FileEnt = SourceMgr.getFileEntryRefForID( |
994 | 0 | SourceMgr.getMainFileID()))) { |
995 | 0 | auto CWD = FileMgr.getOptionalDirectoryRef("."); |
996 | 0 | Includers.push_back(std::make_pair(*FileEnt, *CWD)); |
997 | 0 | } |
998 | 0 | } else { |
999 | 0 | Includers.push_back(std::make_pair(*FileEnt, FileEnt->getDir())); |
1000 | 0 | } |
1001 | | |
1002 | | // MSVC searches the current include stack from top to bottom for |
1003 | | // headers included by quoted include directives. |
1004 | | // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx |
1005 | 0 | if (LangOpts.MSVCCompat && !isAngled) { |
1006 | 0 | for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) { |
1007 | 0 | if (IsFileLexer(ISEntry)) |
1008 | 0 | if ((FileEnt = ISEntry.ThePPLexer->getFileEntry())) |
1009 | 0 | Includers.push_back(std::make_pair(*FileEnt, FileEnt->getDir())); |
1010 | 0 | } |
1011 | 0 | } |
1012 | 0 | } |
1013 | |
|
1014 | 0 | CurDir = CurDirLookup; |
1015 | |
|
1016 | 0 | if (FromFile) { |
1017 | | // We're supposed to start looking from after a particular file. Search |
1018 | | // the include path until we find that file or run out of files. |
1019 | 0 | ConstSearchDirIterator TmpCurDir = CurDir; |
1020 | 0 | ConstSearchDirIterator TmpFromDir = nullptr; |
1021 | 0 | while (OptionalFileEntryRef FE = HeaderInfo.LookupFile( |
1022 | 0 | Filename, FilenameLoc, isAngled, TmpFromDir, &TmpCurDir, |
1023 | 0 | Includers, SearchPath, RelativePath, RequestingModule, |
1024 | 0 | SuggestedModule, /*IsMapped=*/nullptr, |
1025 | 0 | /*IsFrameworkFound=*/nullptr, SkipCache)) { |
1026 | | // Keep looking as if this file did a #include_next. |
1027 | 0 | TmpFromDir = TmpCurDir; |
1028 | 0 | ++TmpFromDir; |
1029 | 0 | if (&FE->getFileEntry() == FromFile) { |
1030 | | // Found it. |
1031 | 0 | FromDir = TmpFromDir; |
1032 | 0 | CurDir = TmpCurDir; |
1033 | 0 | break; |
1034 | 0 | } |
1035 | 0 | } |
1036 | 0 | } |
1037 | | |
1038 | | // Do a standard file entry lookup. |
1039 | 0 | OptionalFileEntryRef FE = HeaderInfo.LookupFile( |
1040 | 0 | Filename, FilenameLoc, isAngled, FromDir, &CurDir, Includers, SearchPath, |
1041 | 0 | RelativePath, RequestingModule, SuggestedModule, IsMapped, |
1042 | 0 | IsFrameworkFound, SkipCache, BuildSystemModule, OpenFile, CacheFailures); |
1043 | 0 | if (FE) |
1044 | 0 | return FE; |
1045 | | |
1046 | 0 | OptionalFileEntryRef CurFileEnt; |
1047 | | // Otherwise, see if this is a subframework header. If so, this is relative |
1048 | | // to one of the headers on the #include stack. Walk the list of the current |
1049 | | // headers on the #include stack and pass them to HeaderInfo. |
1050 | 0 | if (IsFileLexer()) { |
1051 | 0 | if ((CurFileEnt = CurPPLexer->getFileEntry())) { |
1052 | 0 | if (OptionalFileEntryRef FE = HeaderInfo.LookupSubframeworkHeader( |
1053 | 0 | Filename, *CurFileEnt, SearchPath, RelativePath, RequestingModule, |
1054 | 0 | SuggestedModule)) { |
1055 | 0 | return FE; |
1056 | 0 | } |
1057 | 0 | } |
1058 | 0 | } |
1059 | | |
1060 | 0 | for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) { |
1061 | 0 | if (IsFileLexer(ISEntry)) { |
1062 | 0 | if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) { |
1063 | 0 | if (OptionalFileEntryRef FE = HeaderInfo.LookupSubframeworkHeader( |
1064 | 0 | Filename, *CurFileEnt, SearchPath, RelativePath, |
1065 | 0 | RequestingModule, SuggestedModule)) { |
1066 | 0 | return FE; |
1067 | 0 | } |
1068 | 0 | } |
1069 | 0 | } |
1070 | 0 | } |
1071 | | |
1072 | | // Otherwise, we really couldn't find the file. |
1073 | 0 | return std::nullopt; |
1074 | 0 | } |
1075 | | |
1076 | | //===----------------------------------------------------------------------===// |
1077 | | // Preprocessor Directive Handling. |
1078 | | //===----------------------------------------------------------------------===// |
1079 | | |
1080 | | class Preprocessor::ResetMacroExpansionHelper { |
1081 | | public: |
1082 | | ResetMacroExpansionHelper(Preprocessor *pp) |
1083 | 24.0k | : PP(pp), save(pp->DisableMacroExpansion) { |
1084 | 24.0k | if (pp->MacroExpansionInDirectivesOverride) |
1085 | 0 | pp->DisableMacroExpansion = false; |
1086 | 24.0k | } |
1087 | | |
1088 | 24.0k | ~ResetMacroExpansionHelper() { |
1089 | 24.0k | PP->DisableMacroExpansion = save; |
1090 | 24.0k | } |
1091 | | |
1092 | | private: |
1093 | | Preprocessor *PP; |
1094 | | bool save; |
1095 | | }; |
1096 | | |
1097 | | /// Process a directive while looking for the through header or a #pragma |
1098 | | /// hdrstop. The following directives are handled: |
1099 | | /// #include (to check if it is the through header) |
1100 | | /// #define (to warn about macros that don't match the PCH) |
1101 | | /// #pragma (to check for pragma hdrstop). |
1102 | | /// All other directives are completely discarded. |
1103 | | void Preprocessor::HandleSkippedDirectiveWhileUsingPCH(Token &Result, |
1104 | 0 | SourceLocation HashLoc) { |
1105 | 0 | if (const IdentifierInfo *II = Result.getIdentifierInfo()) { |
1106 | 0 | if (II->getPPKeywordID() == tok::pp_define) { |
1107 | 0 | return HandleDefineDirective(Result, |
1108 | 0 | /*ImmediatelyAfterHeaderGuard=*/false); |
1109 | 0 | } |
1110 | 0 | if (SkippingUntilPCHThroughHeader && |
1111 | 0 | II->getPPKeywordID() == tok::pp_include) { |
1112 | 0 | return HandleIncludeDirective(HashLoc, Result); |
1113 | 0 | } |
1114 | 0 | if (SkippingUntilPragmaHdrStop && II->getPPKeywordID() == tok::pp_pragma) { |
1115 | 0 | Lex(Result); |
1116 | 0 | auto *II = Result.getIdentifierInfo(); |
1117 | 0 | if (II && II->getName() == "hdrstop") |
1118 | 0 | return HandlePragmaHdrstop(Result); |
1119 | 0 | } |
1120 | 0 | } |
1121 | 0 | DiscardUntilEndOfDirective(); |
1122 | 0 | } |
1123 | | |
1124 | | /// HandleDirective - This callback is invoked when the lexer sees a # token |
1125 | | /// at the start of a line. This consumes the directive, modifies the |
1126 | | /// lexer/preprocessor state, and advances the lexer(s) so that the next token |
1127 | | /// read is the correct one. |
1128 | 24.0k | void Preprocessor::HandleDirective(Token &Result) { |
1129 | | // FIXME: Traditional: # with whitespace before it not recognized by K&R? |
1130 | | |
1131 | | // We just parsed a # character at the start of a line, so we're in directive |
1132 | | // mode. Tell the lexer this so any newlines we see will be converted into an |
1133 | | // EOD token (which terminates the directive). |
1134 | 24.0k | CurPPLexer->ParsingPreprocessorDirective = true; |
1135 | 24.0k | if (CurLexer) CurLexer->SetKeepWhitespaceMode(false); |
1136 | | |
1137 | 24.0k | bool ImmediatelyAfterTopLevelIfndef = |
1138 | 24.0k | CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef(); |
1139 | 24.0k | CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef(); |
1140 | | |
1141 | 24.0k | ++NumDirectives; |
1142 | | |
1143 | | // We are about to read a token. For the multiple-include optimization FA to |
1144 | | // work, we have to remember if we had read any tokens *before* this |
1145 | | // pp-directive. |
1146 | 24.0k | bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal(); |
1147 | | |
1148 | | // Save the '#' token in case we need to return it later. |
1149 | 24.0k | Token SavedHash = Result; |
1150 | | |
1151 | | // Read the next token, the directive flavor. This isn't expanded due to |
1152 | | // C99 6.10.3p8. |
1153 | 24.0k | LexUnexpandedToken(Result); |
1154 | | |
1155 | | // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: |
1156 | | // #define A(x) #x |
1157 | | // A(abc |
1158 | | // #warning blah |
1159 | | // def) |
1160 | | // If so, the user is relying on undefined behavior, emit a diagnostic. Do |
1161 | | // not support this for #include-like directives, since that can result in |
1162 | | // terrible diagnostics, and does not work in GCC. |
1163 | 24.0k | if (InMacroArgs) { |
1164 | 0 | if (IdentifierInfo *II = Result.getIdentifierInfo()) { |
1165 | 0 | switch (II->getPPKeywordID()) { |
1166 | 0 | case tok::pp_include: |
1167 | 0 | case tok::pp_import: |
1168 | 0 | case tok::pp_include_next: |
1169 | 0 | case tok::pp___include_macros: |
1170 | 0 | case tok::pp_pragma: |
1171 | 0 | Diag(Result, diag::err_embedded_directive) << II->getName(); |
1172 | 0 | Diag(*ArgMacro, diag::note_macro_expansion_here) |
1173 | 0 | << ArgMacro->getIdentifierInfo(); |
1174 | 0 | DiscardUntilEndOfDirective(); |
1175 | 0 | return; |
1176 | 0 | default: |
1177 | 0 | break; |
1178 | 0 | } |
1179 | 0 | } |
1180 | 0 | Diag(Result, diag::ext_embedded_directive); |
1181 | 0 | } |
1182 | | |
1183 | | // Temporarily enable macro expansion if set so |
1184 | | // and reset to previous state when returning from this function. |
1185 | 24.0k | ResetMacroExpansionHelper helper(this); |
1186 | | |
1187 | 24.0k | if (SkippingUntilPCHThroughHeader || SkippingUntilPragmaHdrStop) |
1188 | 0 | return HandleSkippedDirectiveWhileUsingPCH(Result, SavedHash.getLocation()); |
1189 | | |
1190 | 24.0k | switch (Result.getKind()) { |
1191 | 208 | case tok::eod: |
1192 | | // Ignore the null directive with regards to the multiple-include |
1193 | | // optimization, i.e. allow the null directive to appear outside of the |
1194 | | // include guard and still enable the multiple-include optimization. |
1195 | 208 | CurPPLexer->MIOpt.SetReadToken(ReadAnyTokensBeforeDirective); |
1196 | 208 | return; // null directive. |
1197 | 0 | case tok::code_completion: |
1198 | 0 | setCodeCompletionReached(); |
1199 | 0 | if (CodeComplete) |
1200 | 0 | CodeComplete->CodeCompleteDirective( |
1201 | 0 | CurPPLexer->getConditionalStackDepth() > 0); |
1202 | 0 | return; |
1203 | 172 | case tok::numeric_constant: // # 7 GNU line marker directive. |
1204 | | // In a .S file "# 4" may be a comment so don't treat it as a preprocessor |
1205 | | // directive. However do permit it in the predefines file, as we use line |
1206 | | // markers to mark the builtin macros as being in a system header. |
1207 | 172 | if (getLangOpts().AsmPreprocessor && |
1208 | 172 | SourceMgr.getFileID(SavedHash.getLocation()) != getPredefinesFileID()) |
1209 | 0 | break; |
1210 | 172 | return HandleDigitDirective(Result); |
1211 | 23.6k | default: |
1212 | 23.6k | IdentifierInfo *II = Result.getIdentifierInfo(); |
1213 | 23.6k | if (!II) break; // Not an identifier. |
1214 | | |
1215 | | // Ask what the preprocessor keyword ID is. |
1216 | 20.6k | switch (II->getPPKeywordID()) { |
1217 | 2.22k | default: break; |
1218 | | // C99 6.10.1 - Conditional Inclusion. |
1219 | 2.22k | case tok::pp_if: |
1220 | 0 | return HandleIfDirective(Result, SavedHash, ReadAnyTokensBeforeDirective); |
1221 | 0 | case tok::pp_ifdef: |
1222 | 0 | return HandleIfdefDirective(Result, SavedHash, false, |
1223 | 0 | true /*not valid for miopt*/); |
1224 | 0 | case tok::pp_ifndef: |
1225 | 0 | return HandleIfdefDirective(Result, SavedHash, true, |
1226 | 0 | ReadAnyTokensBeforeDirective); |
1227 | 0 | case tok::pp_elif: |
1228 | 0 | case tok::pp_elifdef: |
1229 | 0 | case tok::pp_elifndef: |
1230 | 0 | return HandleElifFamilyDirective(Result, SavedHash, II->getPPKeywordID()); |
1231 | | |
1232 | 0 | case tok::pp_else: |
1233 | 0 | return HandleElseDirective(Result, SavedHash); |
1234 | 0 | case tok::pp_endif: |
1235 | 0 | return HandleEndifDirective(Result); |
1236 | | |
1237 | | // C99 6.10.2 - Source File Inclusion. |
1238 | 0 | case tok::pp_include: |
1239 | | // Handle #include. |
1240 | 0 | return HandleIncludeDirective(SavedHash.getLocation(), Result); |
1241 | 0 | case tok::pp___include_macros: |
1242 | | // Handle -imacros. |
1243 | 0 | return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result); |
1244 | | |
1245 | | // C99 6.10.3 - Macro Replacement. |
1246 | 18.4k | case tok::pp_define: |
1247 | 18.4k | return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef); |
1248 | 0 | case tok::pp_undef: |
1249 | 0 | return HandleUndefDirective(); |
1250 | | |
1251 | | // C99 6.10.4 - Line Control. |
1252 | 0 | case tok::pp_line: |
1253 | 0 | return HandleLineDirective(); |
1254 | | |
1255 | | // C99 6.10.5 - Error Directive. |
1256 | 0 | case tok::pp_error: |
1257 | 0 | return HandleUserDiagnosticDirective(Result, false); |
1258 | | |
1259 | | // C99 6.10.6 - Pragma Directive. |
1260 | 0 | case tok::pp_pragma: |
1261 | 0 | return HandlePragmaDirective({PIK_HashPragma, SavedHash.getLocation()}); |
1262 | | |
1263 | | // GNU Extensions. |
1264 | 0 | case tok::pp_import: |
1265 | 0 | return HandleImportDirective(SavedHash.getLocation(), Result); |
1266 | 0 | case tok::pp_include_next: |
1267 | 0 | return HandleIncludeNextDirective(SavedHash.getLocation(), Result); |
1268 | | |
1269 | 0 | case tok::pp_warning: |
1270 | 0 | if (LangOpts.CPlusPlus) |
1271 | 0 | Diag(Result, LangOpts.CPlusPlus23 |
1272 | 0 | ? diag::warn_cxx23_compat_warning_directive |
1273 | 0 | : diag::ext_pp_warning_directive) |
1274 | 0 | << /*C++23*/ 1; |
1275 | 0 | else |
1276 | 0 | Diag(Result, LangOpts.C23 ? diag::warn_c23_compat_warning_directive |
1277 | 0 | : diag::ext_pp_warning_directive) |
1278 | 0 | << /*C23*/ 0; |
1279 | |
|
1280 | 0 | return HandleUserDiagnosticDirective(Result, true); |
1281 | 0 | case tok::pp_ident: |
1282 | 0 | return HandleIdentSCCSDirective(Result); |
1283 | 0 | case tok::pp_sccs: |
1284 | 0 | return HandleIdentSCCSDirective(Result); |
1285 | 0 | case tok::pp_assert: |
1286 | | //isExtension = true; // FIXME: implement #assert |
1287 | 0 | break; |
1288 | 0 | case tok::pp_unassert: |
1289 | | //isExtension = true; // FIXME: implement #unassert |
1290 | 0 | break; |
1291 | | |
1292 | 0 | case tok::pp___public_macro: |
1293 | 0 | if (getLangOpts().Modules || getLangOpts().ModulesLocalVisibility) |
1294 | 0 | return HandleMacroPublicDirective(Result); |
1295 | 0 | break; |
1296 | | |
1297 | 0 | case tok::pp___private_macro: |
1298 | 0 | if (getLangOpts().Modules || getLangOpts().ModulesLocalVisibility) |
1299 | 0 | return HandleMacroPrivateDirective(); |
1300 | 0 | break; |
1301 | 20.6k | } |
1302 | 2.22k | break; |
1303 | 24.0k | } |
1304 | | |
1305 | | // If this is a .S file, treat unknown # directives as non-preprocessor |
1306 | | // directives. This is important because # may be a comment or introduce |
1307 | | // various pseudo-ops. Just return the # token and push back the following |
1308 | | // token to be lexed next time. |
1309 | 5.28k | if (getLangOpts().AsmPreprocessor) { |
1310 | 0 | auto Toks = std::make_unique<Token[]>(2); |
1311 | | // Return the # and the token after it. |
1312 | 0 | Toks[0] = SavedHash; |
1313 | 0 | Toks[1] = Result; |
1314 | | |
1315 | | // If the second token is a hashhash token, then we need to translate it to |
1316 | | // unknown so the token lexer doesn't try to perform token pasting. |
1317 | 0 | if (Result.is(tok::hashhash)) |
1318 | 0 | Toks[1].setKind(tok::unknown); |
1319 | | |
1320 | | // Enter this token stream so that we re-lex the tokens. Make sure to |
1321 | | // enable macro expansion, in case the token after the # is an identifier |
1322 | | // that is expanded. |
1323 | 0 | EnterTokenStream(std::move(Toks), 2, false, /*IsReinject*/false); |
1324 | 0 | return; |
1325 | 0 | } |
1326 | | |
1327 | | // If we reached here, the preprocessing token is not valid! |
1328 | | // Start suggesting if a similar directive found. |
1329 | 5.28k | Diag(Result, diag::err_pp_invalid_directive) << 0; |
1330 | | |
1331 | | // Read the rest of the PP line. |
1332 | 5.28k | DiscardUntilEndOfDirective(); |
1333 | | |
1334 | | // Okay, we're done parsing the directive. |
1335 | 5.28k | } |
1336 | | |
1337 | | /// GetLineValue - Convert a numeric token into an unsigned value, emitting |
1338 | | /// Diagnostic DiagID if it is invalid, and returning the value in Val. |
1339 | | static bool GetLineValue(Token &DigitTok, unsigned &Val, |
1340 | | unsigned DiagID, Preprocessor &PP, |
1341 | 310 | bool IsGNULineDirective=false) { |
1342 | 310 | if (DigitTok.isNot(tok::numeric_constant)) { |
1343 | 0 | PP.Diag(DigitTok, DiagID); |
1344 | |
|
1345 | 0 | if (DigitTok.isNot(tok::eod)) |
1346 | 0 | PP.DiscardUntilEndOfDirective(); |
1347 | 0 | return true; |
1348 | 0 | } |
1349 | | |
1350 | 310 | SmallString<64> IntegerBuffer; |
1351 | 310 | IntegerBuffer.resize(DigitTok.getLength()); |
1352 | 310 | const char *DigitTokBegin = &IntegerBuffer[0]; |
1353 | 310 | bool Invalid = false; |
1354 | 310 | unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid); |
1355 | 310 | if (Invalid) |
1356 | 0 | return true; |
1357 | | |
1358 | | // Verify that we have a simple digit-sequence, and compute the value. This |
1359 | | // is always a simple digit string computed in decimal, so we do this manually |
1360 | | // here. |
1361 | 310 | Val = 0; |
1362 | 623 | for (unsigned i = 0; i != ActualLength; ++i) { |
1363 | | // C++1y [lex.fcon]p1: |
1364 | | // Optional separating single quotes in a digit-sequence are ignored |
1365 | 325 | if (DigitTokBegin[i] == '\'') |
1366 | 0 | continue; |
1367 | | |
1368 | 325 | if (!isDigit(DigitTokBegin[i])) { |
1369 | 12 | PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i), |
1370 | 12 | diag::err_pp_line_digit_sequence) << IsGNULineDirective; |
1371 | 12 | PP.DiscardUntilEndOfDirective(); |
1372 | 12 | return true; |
1373 | 12 | } |
1374 | | |
1375 | 313 | unsigned NextVal = Val*10+(DigitTokBegin[i]-'0'); |
1376 | 313 | if (NextVal < Val) { // overflow. |
1377 | 0 | PP.Diag(DigitTok, DiagID); |
1378 | 0 | PP.DiscardUntilEndOfDirective(); |
1379 | 0 | return true; |
1380 | 0 | } |
1381 | 313 | Val = NextVal; |
1382 | 313 | } |
1383 | | |
1384 | 298 | if (DigitTokBegin[0] == '0' && Val) |
1385 | 0 | PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal) |
1386 | 0 | << IsGNULineDirective; |
1387 | | |
1388 | 298 | return false; |
1389 | 310 | } |
1390 | | |
1391 | | /// Handle a \#line directive: C99 6.10.4. |
1392 | | /// |
1393 | | /// The two acceptable forms are: |
1394 | | /// \verbatim |
1395 | | /// # line digit-sequence |
1396 | | /// # line digit-sequence "s-char-sequence" |
1397 | | /// \endverbatim |
1398 | 0 | void Preprocessor::HandleLineDirective() { |
1399 | | // Read the line # and string argument. Per C99 6.10.4p5, these tokens are |
1400 | | // expanded. |
1401 | 0 | Token DigitTok; |
1402 | 0 | Lex(DigitTok); |
1403 | | |
1404 | | // Validate the number and convert it to an unsigned. |
1405 | 0 | unsigned LineNo; |
1406 | 0 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this)) |
1407 | 0 | return; |
1408 | | |
1409 | 0 | if (LineNo == 0) |
1410 | 0 | Diag(DigitTok, diag::ext_pp_line_zero); |
1411 | | |
1412 | | // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a |
1413 | | // number greater than 2147483647". C90 requires that the line # be <= 32767. |
1414 | 0 | unsigned LineLimit = 32768U; |
1415 | 0 | if (LangOpts.C99 || LangOpts.CPlusPlus11) |
1416 | 0 | LineLimit = 2147483648U; |
1417 | 0 | if (LineNo >= LineLimit) |
1418 | 0 | Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit; |
1419 | 0 | else if (LangOpts.CPlusPlus11 && LineNo >= 32768U) |
1420 | 0 | Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big); |
1421 | |
|
1422 | 0 | int FilenameID = -1; |
1423 | 0 | Token StrTok; |
1424 | 0 | Lex(StrTok); |
1425 | | |
1426 | | // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a |
1427 | | // string followed by eod. |
1428 | 0 | if (StrTok.is(tok::eod)) |
1429 | 0 | ; // ok |
1430 | 0 | else if (StrTok.isNot(tok::string_literal)) { |
1431 | 0 | Diag(StrTok, diag::err_pp_line_invalid_filename); |
1432 | 0 | DiscardUntilEndOfDirective(); |
1433 | 0 | return; |
1434 | 0 | } else if (StrTok.hasUDSuffix()) { |
1435 | 0 | Diag(StrTok, diag::err_invalid_string_udl); |
1436 | 0 | DiscardUntilEndOfDirective(); |
1437 | 0 | return; |
1438 | 0 | } else { |
1439 | | // Parse and validate the string, converting it into a unique ID. |
1440 | 0 | StringLiteralParser Literal(StrTok, *this); |
1441 | 0 | assert(Literal.isOrdinary() && "Didn't allow wide strings in"); |
1442 | 0 | if (Literal.hadError) { |
1443 | 0 | DiscardUntilEndOfDirective(); |
1444 | 0 | return; |
1445 | 0 | } |
1446 | 0 | if (Literal.Pascal) { |
1447 | 0 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
1448 | 0 | DiscardUntilEndOfDirective(); |
1449 | 0 | return; |
1450 | 0 | } |
1451 | 0 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString()); |
1452 | | |
1453 | | // Verify that there is nothing after the string, other than EOD. Because |
1454 | | // of C99 6.10.4p5, macros that expand to empty tokens are ok. |
1455 | 0 | CheckEndOfDirective("line", true); |
1456 | 0 | } |
1457 | | |
1458 | | // Take the file kind of the file containing the #line directive. #line |
1459 | | // directives are often used for generated sources from the same codebase, so |
1460 | | // the new file should generally be classified the same way as the current |
1461 | | // file. This is visible in GCC's pre-processed output, which rewrites #line |
1462 | | // to GNU line markers. |
1463 | 0 | SrcMgr::CharacteristicKind FileKind = |
1464 | 0 | SourceMgr.getFileCharacteristic(DigitTok.getLocation()); |
1465 | |
|
1466 | 0 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, false, |
1467 | 0 | false, FileKind); |
1468 | |
|
1469 | 0 | if (Callbacks) |
1470 | 0 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), |
1471 | 0 | PPCallbacks::RenameFile, FileKind); |
1472 | 0 | } |
1473 | | |
1474 | | /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line |
1475 | | /// marker directive. |
1476 | | static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, |
1477 | | SrcMgr::CharacteristicKind &FileKind, |
1478 | 138 | Preprocessor &PP) { |
1479 | 138 | unsigned FlagVal; |
1480 | 138 | Token FlagTok; |
1481 | 138 | PP.Lex(FlagTok); |
1482 | 138 | if (FlagTok.is(tok::eod)) return false; |
1483 | 138 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) |
1484 | 0 | return true; |
1485 | | |
1486 | 138 | if (FlagVal == 1) { |
1487 | 46 | IsFileEntry = true; |
1488 | | |
1489 | 46 | PP.Lex(FlagTok); |
1490 | 46 | if (FlagTok.is(tok::eod)) return false; |
1491 | 0 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) |
1492 | 0 | return true; |
1493 | 92 | } else if (FlagVal == 2) { |
1494 | 46 | IsFileExit = true; |
1495 | | |
1496 | 46 | SourceManager &SM = PP.getSourceManager(); |
1497 | | // If we are leaving the current presumed file, check to make sure the |
1498 | | // presumed include stack isn't empty! |
1499 | 46 | FileID CurFileID = |
1500 | 46 | SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first; |
1501 | 46 | PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation()); |
1502 | 46 | if (PLoc.isInvalid()) |
1503 | 0 | return true; |
1504 | | |
1505 | | // If there is no include loc (main file) or if the include loc is in a |
1506 | | // different physical file, then we aren't in a "1" line marker flag region. |
1507 | 46 | SourceLocation IncLoc = PLoc.getIncludeLoc(); |
1508 | 46 | if (IncLoc.isInvalid() || |
1509 | 46 | SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) { |
1510 | 0 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop); |
1511 | 0 | PP.DiscardUntilEndOfDirective(); |
1512 | 0 | return true; |
1513 | 0 | } |
1514 | | |
1515 | 46 | PP.Lex(FlagTok); |
1516 | 46 | if (FlagTok.is(tok::eod)) return false; |
1517 | 0 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) |
1518 | 0 | return true; |
1519 | 0 | } |
1520 | | |
1521 | | // We must have 3 if there are still flags. |
1522 | 46 | if (FlagVal != 3) { |
1523 | 0 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
1524 | 0 | PP.DiscardUntilEndOfDirective(); |
1525 | 0 | return true; |
1526 | 0 | } |
1527 | | |
1528 | 46 | FileKind = SrcMgr::C_System; |
1529 | | |
1530 | 46 | PP.Lex(FlagTok); |
1531 | 46 | if (FlagTok.is(tok::eod)) return false; |
1532 | 0 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) |
1533 | 0 | return true; |
1534 | | |
1535 | | // We must have 4 if there is yet another flag. |
1536 | 0 | if (FlagVal != 4) { |
1537 | 0 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
1538 | 0 | PP.DiscardUntilEndOfDirective(); |
1539 | 0 | return true; |
1540 | 0 | } |
1541 | | |
1542 | 0 | FileKind = SrcMgr::C_ExternCSystem; |
1543 | |
|
1544 | 0 | PP.Lex(FlagTok); |
1545 | 0 | if (FlagTok.is(tok::eod)) return false; |
1546 | | |
1547 | | // There are no more valid flags here. |
1548 | 0 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
1549 | 0 | PP.DiscardUntilEndOfDirective(); |
1550 | 0 | return true; |
1551 | 0 | } |
1552 | | |
1553 | | /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is |
1554 | | /// one of the following forms: |
1555 | | /// |
1556 | | /// # 42 |
1557 | | /// # 42 "file" ('1' | '2')? |
1558 | | /// # 42 "file" ('1' | '2')? '3' '4'? |
1559 | | /// |
1560 | 172 | void Preprocessor::HandleDigitDirective(Token &DigitTok) { |
1561 | | // Validate the number and convert it to an unsigned. GNU does not have a |
1562 | | // line # limit other than it fit in 32-bits. |
1563 | 172 | unsigned LineNo; |
1564 | 172 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer, |
1565 | 172 | *this, true)) |
1566 | 12 | return; |
1567 | | |
1568 | 160 | Token StrTok; |
1569 | 160 | Lex(StrTok); |
1570 | | |
1571 | 160 | bool IsFileEntry = false, IsFileExit = false; |
1572 | 160 | int FilenameID = -1; |
1573 | 160 | SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; |
1574 | | |
1575 | | // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a |
1576 | | // string followed by eod. |
1577 | 160 | if (StrTok.is(tok::eod)) { |
1578 | 2 | Diag(StrTok, diag::ext_pp_gnu_line_directive); |
1579 | | // Treat this like "#line NN", which doesn't change file characteristics. |
1580 | 2 | FileKind = SourceMgr.getFileCharacteristic(DigitTok.getLocation()); |
1581 | 158 | } else if (StrTok.isNot(tok::string_literal)) { |
1582 | 20 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
1583 | 20 | DiscardUntilEndOfDirective(); |
1584 | 20 | return; |
1585 | 138 | } else if (StrTok.hasUDSuffix()) { |
1586 | 0 | Diag(StrTok, diag::err_invalid_string_udl); |
1587 | 0 | DiscardUntilEndOfDirective(); |
1588 | 0 | return; |
1589 | 138 | } else { |
1590 | | // Parse and validate the string, converting it into a unique ID. |
1591 | 138 | StringLiteralParser Literal(StrTok, *this); |
1592 | 138 | assert(Literal.isOrdinary() && "Didn't allow wide strings in"); |
1593 | 138 | if (Literal.hadError) { |
1594 | 0 | DiscardUntilEndOfDirective(); |
1595 | 0 | return; |
1596 | 0 | } |
1597 | 138 | if (Literal.Pascal) { |
1598 | 0 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
1599 | 0 | DiscardUntilEndOfDirective(); |
1600 | 0 | return; |
1601 | 0 | } |
1602 | | |
1603 | | // If a filename was present, read any flags that are present. |
1604 | 138 | if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, FileKind, *this)) |
1605 | 0 | return; |
1606 | 138 | if (!SourceMgr.isWrittenInBuiltinFile(DigitTok.getLocation()) && |
1607 | 138 | !SourceMgr.isWrittenInCommandLineFile(DigitTok.getLocation())) |
1608 | 0 | Diag(StrTok, diag::ext_pp_gnu_line_directive); |
1609 | | |
1610 | | // Exiting to an empty string means pop to the including file, so leave |
1611 | | // FilenameID as -1 in that case. |
1612 | 138 | if (!(IsFileExit && Literal.GetString().empty())) |
1613 | 138 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString()); |
1614 | 138 | } |
1615 | | |
1616 | | // Create a line note with this information. |
1617 | 140 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, IsFileEntry, |
1618 | 140 | IsFileExit, FileKind); |
1619 | | |
1620 | | // If the preprocessor has callbacks installed, notify them of the #line |
1621 | | // change. This is used so that the line marker comes out in -E mode for |
1622 | | // example. |
1623 | 140 | if (Callbacks) { |
1624 | 140 | PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile; |
1625 | 140 | if (IsFileEntry) |
1626 | 46 | Reason = PPCallbacks::EnterFile; |
1627 | 94 | else if (IsFileExit) |
1628 | 46 | Reason = PPCallbacks::ExitFile; |
1629 | | |
1630 | 140 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind); |
1631 | 140 | } |
1632 | 140 | } |
1633 | | |
1634 | | /// HandleUserDiagnosticDirective - Handle a #warning or #error directive. |
1635 | | /// |
1636 | | void Preprocessor::HandleUserDiagnosticDirective(Token &Tok, |
1637 | 0 | bool isWarning) { |
1638 | | // Read the rest of the line raw. We do this because we don't want macros |
1639 | | // to be expanded and we don't require that the tokens be valid preprocessing |
1640 | | // tokens. For example, this is allowed: "#warning ` 'foo". GCC does |
1641 | | // collapse multiple consecutive white space between tokens, but this isn't |
1642 | | // specified by the standard. |
1643 | 0 | SmallString<128> Message; |
1644 | 0 | CurLexer->ReadToEndOfLine(&Message); |
1645 | | |
1646 | | // Find the first non-whitespace character, so that we can make the |
1647 | | // diagnostic more succinct. |
1648 | 0 | StringRef Msg = Message.str().ltrim(' '); |
1649 | |
|
1650 | 0 | if (isWarning) |
1651 | 0 | Diag(Tok, diag::pp_hash_warning) << Msg; |
1652 | 0 | else |
1653 | 0 | Diag(Tok, diag::err_pp_hash_error) << Msg; |
1654 | 0 | } |
1655 | | |
1656 | | /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. |
1657 | | /// |
1658 | 0 | void Preprocessor::HandleIdentSCCSDirective(Token &Tok) { |
1659 | | // Yes, this directive is an extension. |
1660 | 0 | Diag(Tok, diag::ext_pp_ident_directive); |
1661 | | |
1662 | | // Read the string argument. |
1663 | 0 | Token StrTok; |
1664 | 0 | Lex(StrTok); |
1665 | | |
1666 | | // If the token kind isn't a string, it's a malformed directive. |
1667 | 0 | if (StrTok.isNot(tok::string_literal) && |
1668 | 0 | StrTok.isNot(tok::wide_string_literal)) { |
1669 | 0 | Diag(StrTok, diag::err_pp_malformed_ident); |
1670 | 0 | if (StrTok.isNot(tok::eod)) |
1671 | 0 | DiscardUntilEndOfDirective(); |
1672 | 0 | return; |
1673 | 0 | } |
1674 | | |
1675 | 0 | if (StrTok.hasUDSuffix()) { |
1676 | 0 | Diag(StrTok, diag::err_invalid_string_udl); |
1677 | 0 | DiscardUntilEndOfDirective(); |
1678 | 0 | return; |
1679 | 0 | } |
1680 | | |
1681 | | // Verify that there is nothing after the string, other than EOD. |
1682 | 0 | CheckEndOfDirective("ident"); |
1683 | |
|
1684 | 0 | if (Callbacks) { |
1685 | 0 | bool Invalid = false; |
1686 | 0 | std::string Str = getSpelling(StrTok, &Invalid); |
1687 | 0 | if (!Invalid) |
1688 | 0 | Callbacks->Ident(Tok.getLocation(), Str); |
1689 | 0 | } |
1690 | 0 | } |
1691 | | |
1692 | | /// Handle a #public directive. |
1693 | 0 | void Preprocessor::HandleMacroPublicDirective(Token &Tok) { |
1694 | 0 | Token MacroNameTok; |
1695 | 0 | ReadMacroName(MacroNameTok, MU_Undef); |
1696 | | |
1697 | | // Error reading macro name? If so, diagnostic already issued. |
1698 | 0 | if (MacroNameTok.is(tok::eod)) |
1699 | 0 | return; |
1700 | | |
1701 | | // Check to see if this is the last token on the #__public_macro line. |
1702 | 0 | CheckEndOfDirective("__public_macro"); |
1703 | |
|
1704 | 0 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
1705 | | // Okay, we finally have a valid identifier to undef. |
1706 | 0 | MacroDirective *MD = getLocalMacroDirective(II); |
1707 | | |
1708 | | // If the macro is not defined, this is an error. |
1709 | 0 | if (!MD) { |
1710 | 0 | Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II; |
1711 | 0 | return; |
1712 | 0 | } |
1713 | | |
1714 | | // Note that this macro has now been exported. |
1715 | 0 | appendMacroDirective(II, AllocateVisibilityMacroDirective( |
1716 | 0 | MacroNameTok.getLocation(), /*isPublic=*/true)); |
1717 | 0 | } |
1718 | | |
1719 | | /// Handle a #private directive. |
1720 | 0 | void Preprocessor::HandleMacroPrivateDirective() { |
1721 | 0 | Token MacroNameTok; |
1722 | 0 | ReadMacroName(MacroNameTok, MU_Undef); |
1723 | | |
1724 | | // Error reading macro name? If so, diagnostic already issued. |
1725 | 0 | if (MacroNameTok.is(tok::eod)) |
1726 | 0 | return; |
1727 | | |
1728 | | // Check to see if this is the last token on the #__private_macro line. |
1729 | 0 | CheckEndOfDirective("__private_macro"); |
1730 | |
|
1731 | 0 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
1732 | | // Okay, we finally have a valid identifier to undef. |
1733 | 0 | MacroDirective *MD = getLocalMacroDirective(II); |
1734 | | |
1735 | | // If the macro is not defined, this is an error. |
1736 | 0 | if (!MD) { |
1737 | 0 | Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II; |
1738 | 0 | return; |
1739 | 0 | } |
1740 | | |
1741 | | // Note that this macro has now been marked private. |
1742 | 0 | appendMacroDirective(II, AllocateVisibilityMacroDirective( |
1743 | 0 | MacroNameTok.getLocation(), /*isPublic=*/false)); |
1744 | 0 | } |
1745 | | |
1746 | | //===----------------------------------------------------------------------===// |
1747 | | // Preprocessor Include Directive Handling. |
1748 | | //===----------------------------------------------------------------------===// |
1749 | | |
1750 | | /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully |
1751 | | /// checked and spelled filename, e.g. as an operand of \#include. This returns |
1752 | | /// true if the input filename was in <>'s or false if it were in ""'s. The |
1753 | | /// caller is expected to provide a buffer that is large enough to hold the |
1754 | | /// spelling of the filename, but is also expected to handle the case when |
1755 | | /// this method decides to use a different buffer. |
1756 | | bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, |
1757 | 0 | StringRef &Buffer) { |
1758 | | // Get the text form of the filename. |
1759 | 0 | assert(!Buffer.empty() && "Can't have tokens with empty spellings!"); |
1760 | | |
1761 | | // FIXME: Consider warning on some of the cases described in C11 6.4.7/3 and |
1762 | | // C++20 [lex.header]/2: |
1763 | | // |
1764 | | // If `"`, `'`, `\`, `/*`, or `//` appears in a header-name, then |
1765 | | // in C: behavior is undefined |
1766 | | // in C++: program is conditionally-supported with implementation-defined |
1767 | | // semantics |
1768 | | |
1769 | | // Make sure the filename is <x> or "x". |
1770 | 0 | bool isAngled; |
1771 | 0 | if (Buffer[0] == '<') { |
1772 | 0 | if (Buffer.back() != '>') { |
1773 | 0 | Diag(Loc, diag::err_pp_expects_filename); |
1774 | 0 | Buffer = StringRef(); |
1775 | 0 | return true; |
1776 | 0 | } |
1777 | 0 | isAngled = true; |
1778 | 0 | } else if (Buffer[0] == '"') { |
1779 | 0 | if (Buffer.back() != '"') { |
1780 | 0 | Diag(Loc, diag::err_pp_expects_filename); |
1781 | 0 | Buffer = StringRef(); |
1782 | 0 | return true; |
1783 | 0 | } |
1784 | 0 | isAngled = false; |
1785 | 0 | } else { |
1786 | 0 | Diag(Loc, diag::err_pp_expects_filename); |
1787 | 0 | Buffer = StringRef(); |
1788 | 0 | return true; |
1789 | 0 | } |
1790 | | |
1791 | | // Diagnose #include "" as invalid. |
1792 | 0 | if (Buffer.size() <= 2) { |
1793 | 0 | Diag(Loc, diag::err_pp_empty_filename); |
1794 | 0 | Buffer = StringRef(); |
1795 | 0 | return true; |
1796 | 0 | } |
1797 | | |
1798 | | // Skip the brackets. |
1799 | 0 | Buffer = Buffer.substr(1, Buffer.size()-2); |
1800 | 0 | return isAngled; |
1801 | 0 | } |
1802 | | |
1803 | | /// Push a token onto the token stream containing an annotation. |
1804 | | void Preprocessor::EnterAnnotationToken(SourceRange Range, |
1805 | | tok::TokenKind Kind, |
1806 | 0 | void *AnnotationVal) { |
1807 | | // FIXME: Produce this as the current token directly, rather than |
1808 | | // allocating a new token for it. |
1809 | 0 | auto Tok = std::make_unique<Token[]>(1); |
1810 | 0 | Tok[0].startToken(); |
1811 | 0 | Tok[0].setKind(Kind); |
1812 | 0 | Tok[0].setLocation(Range.getBegin()); |
1813 | 0 | Tok[0].setAnnotationEndLoc(Range.getEnd()); |
1814 | 0 | Tok[0].setAnnotationValue(AnnotationVal); |
1815 | 0 | EnterTokenStream(std::move(Tok), 1, true, /*IsReinject*/ false); |
1816 | 0 | } |
1817 | | |
1818 | | /// Produce a diagnostic informing the user that a #include or similar |
1819 | | /// was implicitly treated as a module import. |
1820 | | static void diagnoseAutoModuleImport( |
1821 | | Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok, |
1822 | | ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path, |
1823 | 0 | SourceLocation PathEnd) { |
1824 | 0 | SmallString<128> PathString; |
1825 | 0 | for (size_t I = 0, N = Path.size(); I != N; ++I) { |
1826 | 0 | if (I) |
1827 | 0 | PathString += '.'; |
1828 | 0 | PathString += Path[I].first->getName(); |
1829 | 0 | } |
1830 | |
|
1831 | 0 | int IncludeKind = 0; |
1832 | 0 | switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { |
1833 | 0 | case tok::pp_include: |
1834 | 0 | IncludeKind = 0; |
1835 | 0 | break; |
1836 | | |
1837 | 0 | case tok::pp_import: |
1838 | 0 | IncludeKind = 1; |
1839 | 0 | break; |
1840 | | |
1841 | 0 | case tok::pp_include_next: |
1842 | 0 | IncludeKind = 2; |
1843 | 0 | break; |
1844 | | |
1845 | 0 | case tok::pp___include_macros: |
1846 | 0 | IncludeKind = 3; |
1847 | 0 | break; |
1848 | | |
1849 | 0 | default: |
1850 | 0 | llvm_unreachable("unknown include directive kind"); |
1851 | 0 | } |
1852 | | |
1853 | 0 | PP.Diag(HashLoc, diag::remark_pp_include_directive_modular_translation) |
1854 | 0 | << IncludeKind << PathString; |
1855 | 0 | } |
1856 | | |
1857 | | // Given a vector of path components and a string containing the real |
1858 | | // path to the file, build a properly-cased replacement in the vector, |
1859 | | // and return true if the replacement should be suggested. |
1860 | | static bool trySimplifyPath(SmallVectorImpl<StringRef> &Components, |
1861 | | StringRef RealPathName, |
1862 | 0 | llvm::sys::path::Style Separator) { |
1863 | 0 | auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName); |
1864 | 0 | auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName); |
1865 | 0 | int Cnt = 0; |
1866 | 0 | bool SuggestReplacement = false; |
1867 | |
|
1868 | 0 | auto IsSep = [Separator](StringRef Component) { |
1869 | 0 | return Component.size() == 1 && |
1870 | 0 | llvm::sys::path::is_separator(Component[0], Separator); |
1871 | 0 | }; |
1872 | | |
1873 | | // Below is a best-effort to handle ".." in paths. It is admittedly |
1874 | | // not 100% correct in the presence of symlinks. |
1875 | 0 | for (auto &Component : llvm::reverse(Components)) { |
1876 | 0 | if ("." == Component) { |
1877 | 0 | } else if (".." == Component) { |
1878 | 0 | ++Cnt; |
1879 | 0 | } else if (Cnt) { |
1880 | 0 | --Cnt; |
1881 | 0 | } else if (RealPathComponentIter != RealPathComponentEnd) { |
1882 | 0 | if (!IsSep(Component) && !IsSep(*RealPathComponentIter) && |
1883 | 0 | Component != *RealPathComponentIter) { |
1884 | | // If these non-separator path components differ by more than just case, |
1885 | | // then we may be looking at symlinked paths. Bail on this diagnostic to |
1886 | | // avoid noisy false positives. |
1887 | 0 | SuggestReplacement = |
1888 | 0 | RealPathComponentIter->equals_insensitive(Component); |
1889 | 0 | if (!SuggestReplacement) |
1890 | 0 | break; |
1891 | 0 | Component = *RealPathComponentIter; |
1892 | 0 | } |
1893 | 0 | ++RealPathComponentIter; |
1894 | 0 | } |
1895 | 0 | } |
1896 | 0 | return SuggestReplacement; |
1897 | 0 | } |
1898 | | |
1899 | | bool Preprocessor::checkModuleIsAvailable(const LangOptions &LangOpts, |
1900 | | const TargetInfo &TargetInfo, |
1901 | | const Module &M, |
1902 | 0 | DiagnosticsEngine &Diags) { |
1903 | 0 | Module::Requirement Requirement; |
1904 | 0 | Module::UnresolvedHeaderDirective MissingHeader; |
1905 | 0 | Module *ShadowingModule = nullptr; |
1906 | 0 | if (M.isAvailable(LangOpts, TargetInfo, Requirement, MissingHeader, |
1907 | 0 | ShadowingModule)) |
1908 | 0 | return false; |
1909 | | |
1910 | 0 | if (MissingHeader.FileNameLoc.isValid()) { |
1911 | 0 | Diags.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing) |
1912 | 0 | << MissingHeader.IsUmbrella << MissingHeader.FileName; |
1913 | 0 | } else if (ShadowingModule) { |
1914 | 0 | Diags.Report(M.DefinitionLoc, diag::err_module_shadowed) << M.Name; |
1915 | 0 | Diags.Report(ShadowingModule->DefinitionLoc, |
1916 | 0 | diag::note_previous_definition); |
1917 | 0 | } else { |
1918 | | // FIXME: Track the location at which the requirement was specified, and |
1919 | | // use it here. |
1920 | 0 | Diags.Report(M.DefinitionLoc, diag::err_module_unavailable) |
1921 | 0 | << M.getFullModuleName() << Requirement.second << Requirement.first; |
1922 | 0 | } |
1923 | 0 | return true; |
1924 | 0 | } |
1925 | | |
1926 | | std::pair<ConstSearchDirIterator, const FileEntry *> |
1927 | 0 | Preprocessor::getIncludeNextStart(const Token &IncludeNextTok) const { |
1928 | | // #include_next is like #include, except that we start searching after |
1929 | | // the current found directory. If we can't do this, issue a |
1930 | | // diagnostic. |
1931 | 0 | ConstSearchDirIterator Lookup = CurDirLookup; |
1932 | 0 | const FileEntry *LookupFromFile = nullptr; |
1933 | |
|
1934 | 0 | if (isInPrimaryFile() && LangOpts.IsHeaderFile) { |
1935 | | // If the main file is a header, then it's either for PCH/AST generation, |
1936 | | // or libclang opened it. Either way, handle it as a normal include below |
1937 | | // and do not complain about include_next. |
1938 | 0 | } else if (isInPrimaryFile()) { |
1939 | 0 | Lookup = nullptr; |
1940 | 0 | Diag(IncludeNextTok, diag::pp_include_next_in_primary); |
1941 | 0 | } else if (CurLexerSubmodule) { |
1942 | | // Start looking up in the directory *after* the one in which the current |
1943 | | // file would be found, if any. |
1944 | 0 | assert(CurPPLexer && "#include_next directive in macro?"); |
1945 | 0 | if (auto FE = CurPPLexer->getFileEntry()) |
1946 | 0 | LookupFromFile = *FE; |
1947 | 0 | Lookup = nullptr; |
1948 | 0 | } else if (!Lookup) { |
1949 | | // The current file was not found by walking the include path. Either it |
1950 | | // is the primary file (handled above), or it was found by absolute path, |
1951 | | // or it was found relative to such a file. |
1952 | | // FIXME: Track enough information so we know which case we're in. |
1953 | 0 | Diag(IncludeNextTok, diag::pp_include_next_absolute_path); |
1954 | 0 | } else { |
1955 | | // Start looking up in the next directory. |
1956 | 0 | ++Lookup; |
1957 | 0 | } |
1958 | | |
1959 | 0 | return {Lookup, LookupFromFile}; |
1960 | 0 | } |
1961 | | |
1962 | | /// HandleIncludeDirective - The "\#include" tokens have just been read, read |
1963 | | /// the file to be included from the lexer, then include it! This is a common |
1964 | | /// routine with functionality shared between \#include, \#include_next and |
1965 | | /// \#import. LookupFrom is set when this is a \#include_next directive, it |
1966 | | /// specifies the file to start searching from. |
1967 | | void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, |
1968 | | Token &IncludeTok, |
1969 | | ConstSearchDirIterator LookupFrom, |
1970 | 0 | const FileEntry *LookupFromFile) { |
1971 | 0 | Token FilenameTok; |
1972 | 0 | if (LexHeaderName(FilenameTok)) |
1973 | 0 | return; |
1974 | | |
1975 | 0 | if (FilenameTok.isNot(tok::header_name)) { |
1976 | 0 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
1977 | 0 | if (FilenameTok.isNot(tok::eod)) |
1978 | 0 | DiscardUntilEndOfDirective(); |
1979 | 0 | return; |
1980 | 0 | } |
1981 | | |
1982 | | // Verify that there is nothing after the filename, other than EOD. Note |
1983 | | // that we allow macros that expand to nothing after the filename, because |
1984 | | // this falls into the category of "#include pp-tokens new-line" specified |
1985 | | // in C99 6.10.2p4. |
1986 | 0 | SourceLocation EndLoc = |
1987 | 0 | CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true); |
1988 | |
|
1989 | 0 | auto Action = HandleHeaderIncludeOrImport(HashLoc, IncludeTok, FilenameTok, |
1990 | 0 | EndLoc, LookupFrom, LookupFromFile); |
1991 | 0 | switch (Action.Kind) { |
1992 | 0 | case ImportAction::None: |
1993 | 0 | case ImportAction::SkippedModuleImport: |
1994 | 0 | break; |
1995 | 0 | case ImportAction::ModuleBegin: |
1996 | 0 | EnterAnnotationToken(SourceRange(HashLoc, EndLoc), |
1997 | 0 | tok::annot_module_begin, Action.ModuleForHeader); |
1998 | 0 | break; |
1999 | 0 | case ImportAction::HeaderUnitImport: |
2000 | 0 | EnterAnnotationToken(SourceRange(HashLoc, EndLoc), tok::annot_header_unit, |
2001 | 0 | Action.ModuleForHeader); |
2002 | 0 | break; |
2003 | 0 | case ImportAction::ModuleImport: |
2004 | 0 | EnterAnnotationToken(SourceRange(HashLoc, EndLoc), |
2005 | 0 | tok::annot_module_include, Action.ModuleForHeader); |
2006 | 0 | break; |
2007 | 0 | case ImportAction::Failure: |
2008 | 0 | assert(TheModuleLoader.HadFatalFailure && |
2009 | 0 | "This should be an early exit only to a fatal error"); |
2010 | 0 | TheModuleLoader.HadFatalFailure = true; |
2011 | 0 | IncludeTok.setKind(tok::eof); |
2012 | 0 | CurLexer->cutOffLexing(); |
2013 | 0 | return; |
2014 | 0 | } |
2015 | 0 | } |
2016 | | |
2017 | | OptionalFileEntryRef Preprocessor::LookupHeaderIncludeOrImport( |
2018 | | ConstSearchDirIterator *CurDir, StringRef &Filename, |
2019 | | SourceLocation FilenameLoc, CharSourceRange FilenameRange, |
2020 | | const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl, |
2021 | | bool &IsMapped, ConstSearchDirIterator LookupFrom, |
2022 | | const FileEntry *LookupFromFile, StringRef &LookupFilename, |
2023 | | SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath, |
2024 | 0 | ModuleMap::KnownHeader &SuggestedModule, bool isAngled) { |
2025 | 0 | auto DiagnoseHeaderInclusion = [&](FileEntryRef FE) { |
2026 | 0 | if (LangOpts.AsmPreprocessor) |
2027 | 0 | return; |
2028 | | |
2029 | 0 | Module *RequestingModule = getModuleForLocation( |
2030 | 0 | FilenameLoc, LangOpts.ModulesValidateTextualHeaderIncludes); |
2031 | 0 | bool RequestingModuleIsModuleInterface = |
2032 | 0 | !SourceMgr.isInMainFile(FilenameLoc); |
2033 | |
|
2034 | 0 | HeaderInfo.getModuleMap().diagnoseHeaderInclusion( |
2035 | 0 | RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc, |
2036 | 0 | Filename, FE); |
2037 | 0 | }; |
2038 | |
|
2039 | 0 | OptionalFileEntryRef File = LookupFile( |
2040 | 0 | FilenameLoc, LookupFilename, isAngled, LookupFrom, LookupFromFile, CurDir, |
2041 | 0 | Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr, |
2042 | 0 | &SuggestedModule, &IsMapped, &IsFrameworkFound); |
2043 | 0 | if (File) { |
2044 | 0 | DiagnoseHeaderInclusion(*File); |
2045 | 0 | return File; |
2046 | 0 | } |
2047 | | |
2048 | | // Give the clients a chance to silently skip this include. |
2049 | 0 | if (Callbacks && Callbacks->FileNotFound(Filename)) |
2050 | 0 | return std::nullopt; |
2051 | | |
2052 | 0 | if (SuppressIncludeNotFoundError) |
2053 | 0 | return std::nullopt; |
2054 | | |
2055 | | // If the file could not be located and it was included via angle |
2056 | | // brackets, we can attempt a lookup as though it were a quoted path to |
2057 | | // provide the user with a possible fixit. |
2058 | 0 | if (isAngled) { |
2059 | 0 | OptionalFileEntryRef File = LookupFile( |
2060 | 0 | FilenameLoc, LookupFilename, false, LookupFrom, LookupFromFile, CurDir, |
2061 | 0 | Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr, |
2062 | 0 | &SuggestedModule, &IsMapped, |
2063 | 0 | /*IsFrameworkFound=*/nullptr); |
2064 | 0 | if (File) { |
2065 | 0 | DiagnoseHeaderInclusion(*File); |
2066 | 0 | Diag(FilenameTok, diag::err_pp_file_not_found_angled_include_not_fatal) |
2067 | 0 | << Filename << IsImportDecl |
2068 | 0 | << FixItHint::CreateReplacement(FilenameRange, |
2069 | 0 | "\"" + Filename.str() + "\""); |
2070 | 0 | return File; |
2071 | 0 | } |
2072 | 0 | } |
2073 | | |
2074 | | // Check for likely typos due to leading or trailing non-isAlphanumeric |
2075 | | // characters |
2076 | 0 | StringRef OriginalFilename = Filename; |
2077 | 0 | if (LangOpts.SpellChecking) { |
2078 | | // A heuristic to correct a typo file name by removing leading and |
2079 | | // trailing non-isAlphanumeric characters. |
2080 | 0 | auto CorrectTypoFilename = [](llvm::StringRef Filename) { |
2081 | 0 | Filename = Filename.drop_until(isAlphanumeric); |
2082 | 0 | while (!Filename.empty() && !isAlphanumeric(Filename.back())) { |
2083 | 0 | Filename = Filename.drop_back(); |
2084 | 0 | } |
2085 | 0 | return Filename; |
2086 | 0 | }; |
2087 | 0 | StringRef TypoCorrectionName = CorrectTypoFilename(Filename); |
2088 | 0 | StringRef TypoCorrectionLookupName = CorrectTypoFilename(LookupFilename); |
2089 | |
|
2090 | 0 | OptionalFileEntryRef File = LookupFile( |
2091 | 0 | FilenameLoc, TypoCorrectionLookupName, isAngled, LookupFrom, |
2092 | 0 | LookupFromFile, CurDir, Callbacks ? &SearchPath : nullptr, |
2093 | 0 | Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped, |
2094 | 0 | /*IsFrameworkFound=*/nullptr); |
2095 | 0 | if (File) { |
2096 | 0 | DiagnoseHeaderInclusion(*File); |
2097 | 0 | auto Hint = |
2098 | 0 | isAngled ? FixItHint::CreateReplacement( |
2099 | 0 | FilenameRange, "<" + TypoCorrectionName.str() + ">") |
2100 | 0 | : FixItHint::CreateReplacement( |
2101 | 0 | FilenameRange, "\"" + TypoCorrectionName.str() + "\""); |
2102 | 0 | Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal) |
2103 | 0 | << OriginalFilename << TypoCorrectionName << Hint; |
2104 | | // We found the file, so set the Filename to the name after typo |
2105 | | // correction. |
2106 | 0 | Filename = TypoCorrectionName; |
2107 | 0 | LookupFilename = TypoCorrectionLookupName; |
2108 | 0 | return File; |
2109 | 0 | } |
2110 | 0 | } |
2111 | | |
2112 | | // If the file is still not found, just go with the vanilla diagnostic |
2113 | 0 | assert(!File && "expected missing file"); |
2114 | 0 | Diag(FilenameTok, diag::err_pp_file_not_found) |
2115 | 0 | << OriginalFilename << FilenameRange; |
2116 | 0 | if (IsFrameworkFound) { |
2117 | 0 | size_t SlashPos = OriginalFilename.find('/'); |
2118 | 0 | assert(SlashPos != StringRef::npos && |
2119 | 0 | "Include with framework name should have '/' in the filename"); |
2120 | 0 | StringRef FrameworkName = OriginalFilename.substr(0, SlashPos); |
2121 | 0 | FrameworkCacheEntry &CacheEntry = |
2122 | 0 | HeaderInfo.LookupFrameworkCache(FrameworkName); |
2123 | 0 | assert(CacheEntry.Directory && "Found framework should be in cache"); |
2124 | 0 | Diag(FilenameTok, diag::note_pp_framework_without_header) |
2125 | 0 | << OriginalFilename.substr(SlashPos + 1) << FrameworkName |
2126 | 0 | << CacheEntry.Directory->getName(); |
2127 | 0 | } |
2128 | | |
2129 | 0 | return std::nullopt; |
2130 | 0 | } |
2131 | | |
2132 | | /// Handle either a #include-like directive or an import declaration that names |
2133 | | /// a header file. |
2134 | | /// |
2135 | | /// \param HashLoc The location of the '#' token for an include, or |
2136 | | /// SourceLocation() for an import declaration. |
2137 | | /// \param IncludeTok The include / include_next / import token. |
2138 | | /// \param FilenameTok The header-name token. |
2139 | | /// \param EndLoc The location at which any imported macros become visible. |
2140 | | /// \param LookupFrom For #include_next, the starting directory for the |
2141 | | /// directory lookup. |
2142 | | /// \param LookupFromFile For #include_next, the starting file for the directory |
2143 | | /// lookup. |
2144 | | Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport( |
2145 | | SourceLocation HashLoc, Token &IncludeTok, Token &FilenameTok, |
2146 | | SourceLocation EndLoc, ConstSearchDirIterator LookupFrom, |
2147 | 0 | const FileEntry *LookupFromFile) { |
2148 | 0 | SmallString<128> FilenameBuffer; |
2149 | 0 | StringRef Filename = getSpelling(FilenameTok, FilenameBuffer); |
2150 | 0 | SourceLocation CharEnd = FilenameTok.getEndLoc(); |
2151 | |
|
2152 | 0 | CharSourceRange FilenameRange |
2153 | 0 | = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd); |
2154 | 0 | StringRef OriginalFilename = Filename; |
2155 | 0 | bool isAngled = |
2156 | 0 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
2157 | | |
2158 | | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
2159 | | // error. |
2160 | 0 | if (Filename.empty()) |
2161 | 0 | return {ImportAction::None}; |
2162 | | |
2163 | 0 | bool IsImportDecl = HashLoc.isInvalid(); |
2164 | 0 | SourceLocation StartLoc = IsImportDecl ? IncludeTok.getLocation() : HashLoc; |
2165 | | |
2166 | | // Complain about attempts to #include files in an audit pragma. |
2167 | 0 | if (PragmaARCCFCodeAuditedInfo.second.isValid()) { |
2168 | 0 | Diag(StartLoc, diag::err_pp_include_in_arc_cf_code_audited) << IsImportDecl; |
2169 | 0 | Diag(PragmaARCCFCodeAuditedInfo.second, diag::note_pragma_entered_here); |
2170 | | |
2171 | | // Immediately leave the pragma. |
2172 | 0 | PragmaARCCFCodeAuditedInfo = {nullptr, SourceLocation()}; |
2173 | 0 | } |
2174 | | |
2175 | | // Complain about attempts to #include files in an assume-nonnull pragma. |
2176 | 0 | if (PragmaAssumeNonNullLoc.isValid()) { |
2177 | 0 | Diag(StartLoc, diag::err_pp_include_in_assume_nonnull) << IsImportDecl; |
2178 | 0 | Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here); |
2179 | | |
2180 | | // Immediately leave the pragma. |
2181 | 0 | PragmaAssumeNonNullLoc = SourceLocation(); |
2182 | 0 | } |
2183 | |
|
2184 | 0 | if (HeaderInfo.HasIncludeAliasMap()) { |
2185 | | // Map the filename with the brackets still attached. If the name doesn't |
2186 | | // map to anything, fall back on the filename we've already gotten the |
2187 | | // spelling for. |
2188 | 0 | StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename); |
2189 | 0 | if (!NewName.empty()) |
2190 | 0 | Filename = NewName; |
2191 | 0 | } |
2192 | | |
2193 | | // Search include directories. |
2194 | 0 | bool IsMapped = false; |
2195 | 0 | bool IsFrameworkFound = false; |
2196 | 0 | ConstSearchDirIterator CurDir = nullptr; |
2197 | 0 | SmallString<1024> SearchPath; |
2198 | 0 | SmallString<1024> RelativePath; |
2199 | | // We get the raw path only if we have 'Callbacks' to which we later pass |
2200 | | // the path. |
2201 | 0 | ModuleMap::KnownHeader SuggestedModule; |
2202 | 0 | SourceLocation FilenameLoc = FilenameTok.getLocation(); |
2203 | 0 | StringRef LookupFilename = Filename; |
2204 | | |
2205 | | // Normalize slashes when compiling with -fms-extensions on non-Windows. This |
2206 | | // is unnecessary on Windows since the filesystem there handles backslashes. |
2207 | 0 | SmallString<128> NormalizedPath; |
2208 | 0 | llvm::sys::path::Style BackslashStyle = llvm::sys::path::Style::native; |
2209 | 0 | if (is_style_posix(BackslashStyle) && LangOpts.MicrosoftExt) { |
2210 | 0 | NormalizedPath = Filename.str(); |
2211 | 0 | llvm::sys::path::native(NormalizedPath); |
2212 | 0 | LookupFilename = NormalizedPath; |
2213 | 0 | BackslashStyle = llvm::sys::path::Style::windows; |
2214 | 0 | } |
2215 | |
|
2216 | 0 | OptionalFileEntryRef File = LookupHeaderIncludeOrImport( |
2217 | 0 | &CurDir, Filename, FilenameLoc, FilenameRange, FilenameTok, |
2218 | 0 | IsFrameworkFound, IsImportDecl, IsMapped, LookupFrom, LookupFromFile, |
2219 | 0 | LookupFilename, RelativePath, SearchPath, SuggestedModule, isAngled); |
2220 | |
|
2221 | 0 | if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) { |
2222 | 0 | if (File && isPCHThroughHeader(&File->getFileEntry())) |
2223 | 0 | SkippingUntilPCHThroughHeader = false; |
2224 | 0 | return {ImportAction::None}; |
2225 | 0 | } |
2226 | | |
2227 | | // Should we enter the source file? Set to Skip if either the source file is |
2228 | | // known to have no effect beyond its effect on module visibility -- that is, |
2229 | | // if it's got an include guard that is already defined, set to Import if it |
2230 | | // is a modular header we've already built and should import. |
2231 | | |
2232 | | // For C++20 Modules |
2233 | | // [cpp.include]/7 If the header identified by the header-name denotes an |
2234 | | // importable header, it is implementation-defined whether the #include |
2235 | | // preprocessing directive is instead replaced by an import directive. |
2236 | | // For this implementation, the translation is permitted when we are parsing |
2237 | | // the Global Module Fragment, and not otherwise (the cases where it would be |
2238 | | // valid to replace an include with an import are highly constrained once in |
2239 | | // named module purview; this choice avoids considerable complexity in |
2240 | | // determining valid cases). |
2241 | | |
2242 | 0 | enum { Enter, Import, Skip, IncludeLimitReached } Action = Enter; |
2243 | |
|
2244 | 0 | if (PPOpts->SingleFileParseMode) |
2245 | 0 | Action = IncludeLimitReached; |
2246 | | |
2247 | | // If we've reached the max allowed include depth, it is usually due to an |
2248 | | // include cycle. Don't enter already processed files again as it can lead to |
2249 | | // reaching the max allowed include depth again. |
2250 | 0 | if (Action == Enter && HasReachedMaxIncludeDepth && File && |
2251 | 0 | alreadyIncluded(*File)) |
2252 | 0 | Action = IncludeLimitReached; |
2253 | | |
2254 | | // FIXME: We do not have a good way to disambiguate C++ clang modules from |
2255 | | // C++ standard modules (other than use/non-use of Header Units). |
2256 | 0 | Module *SM = SuggestedModule.getModule(); |
2257 | |
|
2258 | 0 | bool MaybeTranslateInclude = |
2259 | 0 | Action == Enter && File && SM && !SM->isForBuilding(getLangOpts()); |
2260 | | |
2261 | | // Maybe a usable Header Unit |
2262 | 0 | bool UsableHeaderUnit = false; |
2263 | 0 | if (getLangOpts().CPlusPlusModules && SM && SM->isHeaderUnit()) { |
2264 | 0 | if (TrackGMFState.inGMF() || IsImportDecl) |
2265 | 0 | UsableHeaderUnit = true; |
2266 | 0 | else if (!IsImportDecl) { |
2267 | | // This is a Header Unit that we do not include-translate |
2268 | 0 | SuggestedModule = ModuleMap::KnownHeader(); |
2269 | 0 | SM = nullptr; |
2270 | 0 | } |
2271 | 0 | } |
2272 | | // Maybe a usable clang header module. |
2273 | 0 | bool UsableClangHeaderModule = |
2274 | 0 | (getLangOpts().CPlusPlusModules || getLangOpts().Modules) && SM && |
2275 | 0 | !SM->isHeaderUnit(); |
2276 | | |
2277 | | // Determine whether we should try to import the module for this #include, if |
2278 | | // there is one. Don't do so if precompiled module support is disabled or we |
2279 | | // are processing this module textually (because we're building the module). |
2280 | 0 | if (MaybeTranslateInclude && (UsableHeaderUnit || UsableClangHeaderModule)) { |
2281 | | // If this include corresponds to a module but that module is |
2282 | | // unavailable, diagnose the situation and bail out. |
2283 | | // FIXME: Remove this; loadModule does the same check (but produces |
2284 | | // slightly worse diagnostics). |
2285 | 0 | if (checkModuleIsAvailable(getLangOpts(), getTargetInfo(), |
2286 | 0 | *SuggestedModule.getModule(), |
2287 | 0 | getDiagnostics())) { |
2288 | 0 | Diag(FilenameTok.getLocation(), |
2289 | 0 | diag::note_implicit_top_level_module_import_here) |
2290 | 0 | << SuggestedModule.getModule()->getTopLevelModuleName(); |
2291 | 0 | return {ImportAction::None}; |
2292 | 0 | } |
2293 | | |
2294 | | // Compute the module access path corresponding to this module. |
2295 | | // FIXME: Should we have a second loadModule() overload to avoid this |
2296 | | // extra lookup step? |
2297 | 0 | SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path; |
2298 | 0 | for (Module *Mod = SM; Mod; Mod = Mod->Parent) |
2299 | 0 | Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name), |
2300 | 0 | FilenameTok.getLocation())); |
2301 | 0 | std::reverse(Path.begin(), Path.end()); |
2302 | | |
2303 | | // Warn that we're replacing the include/import with a module import. |
2304 | 0 | if (!IsImportDecl) |
2305 | 0 | diagnoseAutoModuleImport(*this, StartLoc, IncludeTok, Path, CharEnd); |
2306 | | |
2307 | | // Load the module to import its macros. We'll make the declarations |
2308 | | // visible when the parser gets here. |
2309 | | // FIXME: Pass SuggestedModule in here rather than converting it to a path |
2310 | | // and making the module loader convert it back again. |
2311 | 0 | ModuleLoadResult Imported = TheModuleLoader.loadModule( |
2312 | 0 | IncludeTok.getLocation(), Path, Module::Hidden, |
2313 | 0 | /*IsInclusionDirective=*/true); |
2314 | 0 | assert((Imported == nullptr || Imported == SuggestedModule.getModule()) && |
2315 | 0 | "the imported module is different than the suggested one"); |
2316 | | |
2317 | 0 | if (Imported) { |
2318 | 0 | Action = Import; |
2319 | 0 | } else if (Imported.isMissingExpected()) { |
2320 | 0 | markClangModuleAsAffecting( |
2321 | 0 | static_cast<Module *>(Imported)->getTopLevelModule()); |
2322 | | // We failed to find a submodule that we assumed would exist (because it |
2323 | | // was in the directory of an umbrella header, for instance), but no |
2324 | | // actual module containing it exists (because the umbrella header is |
2325 | | // incomplete). Treat this as a textual inclusion. |
2326 | 0 | SuggestedModule = ModuleMap::KnownHeader(); |
2327 | 0 | SM = nullptr; |
2328 | 0 | } else if (Imported.isConfigMismatch()) { |
2329 | | // On a configuration mismatch, enter the header textually. We still know |
2330 | | // that it's part of the corresponding module. |
2331 | 0 | } else { |
2332 | | // We hit an error processing the import. Bail out. |
2333 | 0 | if (hadModuleLoaderFatalFailure()) { |
2334 | | // With a fatal failure in the module loader, we abort parsing. |
2335 | 0 | Token &Result = IncludeTok; |
2336 | 0 | assert(CurLexer && "#include but no current lexer set!"); |
2337 | 0 | Result.startToken(); |
2338 | 0 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof); |
2339 | 0 | CurLexer->cutOffLexing(); |
2340 | 0 | } |
2341 | 0 | return {ImportAction::None}; |
2342 | 0 | } |
2343 | 0 | } |
2344 | | |
2345 | | // The #included file will be considered to be a system header if either it is |
2346 | | // in a system include directory, or if the #includer is a system include |
2347 | | // header. |
2348 | 0 | SrcMgr::CharacteristicKind FileCharacter = |
2349 | 0 | SourceMgr.getFileCharacteristic(FilenameTok.getLocation()); |
2350 | 0 | if (File) |
2351 | 0 | FileCharacter = std::max(HeaderInfo.getFileDirFlavor(*File), FileCharacter); |
2352 | | |
2353 | | // If this is a '#import' or an import-declaration, don't re-enter the file. |
2354 | | // |
2355 | | // FIXME: If we have a suggested module for a '#include', and we've already |
2356 | | // visited this file, don't bother entering it again. We know it has no |
2357 | | // further effect. |
2358 | 0 | bool EnterOnce = |
2359 | 0 | IsImportDecl || |
2360 | 0 | IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import; |
2361 | |
|
2362 | 0 | bool IsFirstIncludeOfFile = false; |
2363 | | |
2364 | | // Ask HeaderInfo if we should enter this #include file. If not, #including |
2365 | | // this file will have no effect. |
2366 | 0 | if (Action == Enter && File && |
2367 | 0 | !HeaderInfo.ShouldEnterIncludeFile(*this, *File, EnterOnce, |
2368 | 0 | getLangOpts().Modules, SM, |
2369 | 0 | IsFirstIncludeOfFile)) { |
2370 | | // C++ standard modules: |
2371 | | // If we are not in the GMF, then we textually include only |
2372 | | // clang modules: |
2373 | | // Even if we've already preprocessed this header once and know that we |
2374 | | // don't need to see its contents again, we still need to import it if it's |
2375 | | // modular because we might not have imported it from this submodule before. |
2376 | | // |
2377 | | // FIXME: We don't do this when compiling a PCH because the AST |
2378 | | // serialization layer can't cope with it. This means we get local |
2379 | | // submodule visibility semantics wrong in that case. |
2380 | 0 | if (UsableHeaderUnit && !getLangOpts().CompilingPCH) |
2381 | 0 | Action = TrackGMFState.inGMF() ? Import : Skip; |
2382 | 0 | else |
2383 | 0 | Action = (SuggestedModule && !getLangOpts().CompilingPCH) ? Import : Skip; |
2384 | 0 | } |
2385 | | |
2386 | | // Check for circular inclusion of the main file. |
2387 | | // We can't generate a consistent preamble with regard to the conditional |
2388 | | // stack if the main file is included again as due to the preamble bounds |
2389 | | // some directives (e.g. #endif of a header guard) will never be seen. |
2390 | | // Since this will lead to confusing errors, avoid the inclusion. |
2391 | 0 | if (Action == Enter && File && PreambleConditionalStack.isRecording() && |
2392 | 0 | SourceMgr.isMainFile(File->getFileEntry())) { |
2393 | 0 | Diag(FilenameTok.getLocation(), |
2394 | 0 | diag::err_pp_including_mainfile_in_preamble); |
2395 | 0 | return {ImportAction::None}; |
2396 | 0 | } |
2397 | | |
2398 | 0 | if (Callbacks && !IsImportDecl) { |
2399 | | // Notify the callback object that we've seen an inclusion directive. |
2400 | | // FIXME: Use a different callback for a pp-import? |
2401 | 0 | Callbacks->InclusionDirective(HashLoc, IncludeTok, LookupFilename, isAngled, |
2402 | 0 | FilenameRange, File, SearchPath, RelativePath, |
2403 | 0 | Action == Import ? SuggestedModule.getModule() |
2404 | 0 | : nullptr, |
2405 | 0 | FileCharacter); |
2406 | 0 | if (Action == Skip && File) |
2407 | 0 | Callbacks->FileSkipped(*File, FilenameTok, FileCharacter); |
2408 | 0 | } |
2409 | |
|
2410 | 0 | if (!File) |
2411 | 0 | return {ImportAction::None}; |
2412 | | |
2413 | | // If this is a C++20 pp-import declaration, diagnose if we didn't find any |
2414 | | // module corresponding to the named header. |
2415 | 0 | if (IsImportDecl && !SuggestedModule) { |
2416 | 0 | Diag(FilenameTok, diag::err_header_import_not_header_unit) |
2417 | 0 | << OriginalFilename << File->getName(); |
2418 | 0 | return {ImportAction::None}; |
2419 | 0 | } |
2420 | | |
2421 | | // Issue a diagnostic if the name of the file on disk has a different case |
2422 | | // than the one we're about to open. |
2423 | 0 | const bool CheckIncludePathPortability = |
2424 | 0 | !IsMapped && !File->getFileEntry().tryGetRealPathName().empty(); |
2425 | |
|
2426 | 0 | if (CheckIncludePathPortability) { |
2427 | 0 | StringRef Name = LookupFilename; |
2428 | 0 | StringRef NameWithoriginalSlashes = Filename; |
2429 | | #if defined(_WIN32) |
2430 | | // Skip UNC prefix if present. (tryGetRealPathName() always |
2431 | | // returns a path with the prefix skipped.) |
2432 | | bool NameWasUNC = Name.consume_front("\\\\?\\"); |
2433 | | NameWithoriginalSlashes.consume_front("\\\\?\\"); |
2434 | | #endif |
2435 | 0 | StringRef RealPathName = File->getFileEntry().tryGetRealPathName(); |
2436 | 0 | SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name), |
2437 | 0 | llvm::sys::path::end(Name)); |
2438 | | #if defined(_WIN32) |
2439 | | // -Wnonportable-include-path is designed to diagnose includes using |
2440 | | // case even on systems with a case-insensitive file system. |
2441 | | // On Windows, RealPathName always starts with an upper-case drive |
2442 | | // letter for absolute paths, but Name might start with either |
2443 | | // case depending on if `cd c:\foo` or `cd C:\foo` was used in the shell. |
2444 | | // ("foo" will always have on-disk case, no matter which case was |
2445 | | // used in the cd command). To not emit this warning solely for |
2446 | | // the drive letter, whose case is dependent on if `cd` is used |
2447 | | // with upper- or lower-case drive letters, always consider the |
2448 | | // given drive letter case as correct for the purpose of this warning. |
2449 | | SmallString<128> FixedDriveRealPath; |
2450 | | if (llvm::sys::path::is_absolute(Name) && |
2451 | | llvm::sys::path::is_absolute(RealPathName) && |
2452 | | toLowercase(Name[0]) == toLowercase(RealPathName[0]) && |
2453 | | isLowercase(Name[0]) != isLowercase(RealPathName[0])) { |
2454 | | assert(Components.size() >= 3 && "should have drive, backslash, name"); |
2455 | | assert(Components[0].size() == 2 && "should start with drive"); |
2456 | | assert(Components[0][1] == ':' && "should have colon"); |
2457 | | FixedDriveRealPath = (Name.substr(0, 1) + RealPathName.substr(1)).str(); |
2458 | | RealPathName = FixedDriveRealPath; |
2459 | | } |
2460 | | #endif |
2461 | |
|
2462 | 0 | if (trySimplifyPath(Components, RealPathName, BackslashStyle)) { |
2463 | 0 | SmallString<128> Path; |
2464 | 0 | Path.reserve(Name.size()+2); |
2465 | 0 | Path.push_back(isAngled ? '<' : '"'); |
2466 | |
|
2467 | 0 | const auto IsSep = [BackslashStyle](char c) { |
2468 | 0 | return llvm::sys::path::is_separator(c, BackslashStyle); |
2469 | 0 | }; |
2470 | |
|
2471 | 0 | for (auto Component : Components) { |
2472 | | // On POSIX, Components will contain a single '/' as first element |
2473 | | // exactly if Name is an absolute path. |
2474 | | // On Windows, it will contain "C:" followed by '\' for absolute paths. |
2475 | | // The drive letter is optional for absolute paths on Windows, but |
2476 | | // clang currently cannot process absolute paths in #include lines that |
2477 | | // don't have a drive. |
2478 | | // If the first entry in Components is a directory separator, |
2479 | | // then the code at the bottom of this loop that keeps the original |
2480 | | // directory separator style copies it. If the second entry is |
2481 | | // a directory separator (the C:\ case), then that separator already |
2482 | | // got copied when the C: was processed and we want to skip that entry. |
2483 | 0 | if (!(Component.size() == 1 && IsSep(Component[0]))) |
2484 | 0 | Path.append(Component); |
2485 | 0 | else if (Path.size() != 1) |
2486 | 0 | continue; |
2487 | | |
2488 | | // Append the separator(s) the user used, or the close quote |
2489 | 0 | if (Path.size() > NameWithoriginalSlashes.size()) { |
2490 | 0 | Path.push_back(isAngled ? '>' : '"'); |
2491 | 0 | continue; |
2492 | 0 | } |
2493 | 0 | assert(IsSep(NameWithoriginalSlashes[Path.size()-1])); |
2494 | 0 | do |
2495 | 0 | Path.push_back(NameWithoriginalSlashes[Path.size()-1]); |
2496 | 0 | while (Path.size() <= NameWithoriginalSlashes.size() && |
2497 | 0 | IsSep(NameWithoriginalSlashes[Path.size()-1])); |
2498 | 0 | } |
2499 | |
|
2500 | | #if defined(_WIN32) |
2501 | | // Restore UNC prefix if it was there. |
2502 | | if (NameWasUNC) |
2503 | | Path = (Path.substr(0, 1) + "\\\\?\\" + Path.substr(1)).str(); |
2504 | | #endif |
2505 | | |
2506 | | // For user files and known standard headers, issue a diagnostic. |
2507 | | // For other system headers, don't. They can be controlled separately. |
2508 | 0 | auto DiagId = |
2509 | 0 | (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name)) |
2510 | 0 | ? diag::pp_nonportable_path |
2511 | 0 | : diag::pp_nonportable_system_path; |
2512 | 0 | Diag(FilenameTok, DiagId) << Path << |
2513 | 0 | FixItHint::CreateReplacement(FilenameRange, Path); |
2514 | 0 | } |
2515 | 0 | } |
2516 | |
|
2517 | 0 | switch (Action) { |
2518 | 0 | case Skip: |
2519 | | // If we don't need to enter the file, stop now. |
2520 | 0 | if (SM) |
2521 | 0 | return {ImportAction::SkippedModuleImport, SM}; |
2522 | 0 | return {ImportAction::None}; |
2523 | | |
2524 | 0 | case IncludeLimitReached: |
2525 | | // If we reached our include limit and don't want to enter any more files, |
2526 | | // don't go any further. |
2527 | 0 | return {ImportAction::None}; |
2528 | | |
2529 | 0 | case Import: { |
2530 | | // If this is a module import, make it visible if needed. |
2531 | 0 | assert(SM && "no module to import"); |
2532 | | |
2533 | 0 | makeModuleVisible(SM, EndLoc); |
2534 | |
|
2535 | 0 | if (IncludeTok.getIdentifierInfo()->getPPKeywordID() == |
2536 | 0 | tok::pp___include_macros) |
2537 | 0 | return {ImportAction::None}; |
2538 | | |
2539 | 0 | return {ImportAction::ModuleImport, SM}; |
2540 | 0 | } |
2541 | | |
2542 | 0 | case Enter: |
2543 | 0 | break; |
2544 | 0 | } |
2545 | | |
2546 | | // Check that we don't have infinite #include recursion. |
2547 | 0 | if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) { |
2548 | 0 | Diag(FilenameTok, diag::err_pp_include_too_deep); |
2549 | 0 | HasReachedMaxIncludeDepth = true; |
2550 | 0 | return {ImportAction::None}; |
2551 | 0 | } |
2552 | | |
2553 | 0 | if (isAngled && isInNamedModule()) |
2554 | 0 | Diag(FilenameTok, diag::warn_pp_include_angled_in_module_purview) |
2555 | 0 | << getNamedModuleName(); |
2556 | | |
2557 | | // Look up the file, create a File ID for it. |
2558 | 0 | SourceLocation IncludePos = FilenameTok.getLocation(); |
2559 | | // If the filename string was the result of macro expansions, set the include |
2560 | | // position on the file where it will be included and after the expansions. |
2561 | 0 | if (IncludePos.isMacroID()) |
2562 | 0 | IncludePos = SourceMgr.getExpansionRange(IncludePos).getEnd(); |
2563 | 0 | FileID FID = SourceMgr.createFileID(*File, IncludePos, FileCharacter); |
2564 | 0 | if (!FID.isValid()) { |
2565 | 0 | TheModuleLoader.HadFatalFailure = true; |
2566 | 0 | return ImportAction::Failure; |
2567 | 0 | } |
2568 | | |
2569 | | // If all is good, enter the new file! |
2570 | 0 | if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation(), |
2571 | 0 | IsFirstIncludeOfFile)) |
2572 | 0 | return {ImportAction::None}; |
2573 | | |
2574 | | // Determine if we're switching to building a new submodule, and which one. |
2575 | | // This does not apply for C++20 modules header units. |
2576 | 0 | if (SM && !SM->isHeaderUnit()) { |
2577 | 0 | if (SM->getTopLevelModule()->ShadowingModule) { |
2578 | | // We are building a submodule that belongs to a shadowed module. This |
2579 | | // means we find header files in the shadowed module. |
2580 | 0 | Diag(SM->DefinitionLoc, diag::err_module_build_shadowed_submodule) |
2581 | 0 | << SM->getFullModuleName(); |
2582 | 0 | Diag(SM->getTopLevelModule()->ShadowingModule->DefinitionLoc, |
2583 | 0 | diag::note_previous_definition); |
2584 | 0 | return {ImportAction::None}; |
2585 | 0 | } |
2586 | | // When building a pch, -fmodule-name tells the compiler to textually |
2587 | | // include headers in the specified module. We are not building the |
2588 | | // specified module. |
2589 | | // |
2590 | | // FIXME: This is the wrong way to handle this. We should produce a PCH |
2591 | | // that behaves the same as the header would behave in a compilation using |
2592 | | // that PCH, which means we should enter the submodule. We need to teach |
2593 | | // the AST serialization layer to deal with the resulting AST. |
2594 | 0 | if (getLangOpts().CompilingPCH && SM->isForBuilding(getLangOpts())) |
2595 | 0 | return {ImportAction::None}; |
2596 | | |
2597 | 0 | assert(!CurLexerSubmodule && "should not have marked this as a module yet"); |
2598 | 0 | CurLexerSubmodule = SM; |
2599 | | |
2600 | | // Let the macro handling code know that any future macros are within |
2601 | | // the new submodule. |
2602 | 0 | EnterSubmodule(SM, EndLoc, /*ForPragma*/ false); |
2603 | | |
2604 | | // Let the parser know that any future declarations are within the new |
2605 | | // submodule. |
2606 | | // FIXME: There's no point doing this if we're handling a #__include_macros |
2607 | | // directive. |
2608 | 0 | return {ImportAction::ModuleBegin, SM}; |
2609 | 0 | } |
2610 | | |
2611 | 0 | assert(!IsImportDecl && "failed to diagnose missing module for import decl"); |
2612 | 0 | return {ImportAction::None}; |
2613 | 0 | } |
2614 | | |
2615 | | /// HandleIncludeNextDirective - Implements \#include_next. |
2616 | | /// |
2617 | | void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc, |
2618 | 0 | Token &IncludeNextTok) { |
2619 | 0 | Diag(IncludeNextTok, diag::ext_pp_include_next_directive); |
2620 | |
|
2621 | 0 | ConstSearchDirIterator Lookup = nullptr; |
2622 | 0 | const FileEntry *LookupFromFile; |
2623 | 0 | std::tie(Lookup, LookupFromFile) = getIncludeNextStart(IncludeNextTok); |
2624 | |
|
2625 | 0 | return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup, |
2626 | 0 | LookupFromFile); |
2627 | 0 | } |
2628 | | |
2629 | | /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode |
2630 | 0 | void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) { |
2631 | | // The Microsoft #import directive takes a type library and generates header |
2632 | | // files from it, and includes those. This is beyond the scope of what clang |
2633 | | // does, so we ignore it and error out. However, #import can optionally have |
2634 | | // trailing attributes that span multiple lines. We're going to eat those |
2635 | | // so we can continue processing from there. |
2636 | 0 | Diag(Tok, diag::err_pp_import_directive_ms ); |
2637 | | |
2638 | | // Read tokens until we get to the end of the directive. Note that the |
2639 | | // directive can be split over multiple lines using the backslash character. |
2640 | 0 | DiscardUntilEndOfDirective(); |
2641 | 0 | } |
2642 | | |
2643 | | /// HandleImportDirective - Implements \#import. |
2644 | | /// |
2645 | | void Preprocessor::HandleImportDirective(SourceLocation HashLoc, |
2646 | 0 | Token &ImportTok) { |
2647 | 0 | if (!LangOpts.ObjC) { // #import is standard for ObjC. |
2648 | 0 | if (LangOpts.MSVCCompat) |
2649 | 0 | return HandleMicrosoftImportDirective(ImportTok); |
2650 | 0 | Diag(ImportTok, diag::ext_pp_import_directive); |
2651 | 0 | } |
2652 | 0 | return HandleIncludeDirective(HashLoc, ImportTok); |
2653 | 0 | } |
2654 | | |
2655 | | /// HandleIncludeMacrosDirective - The -imacros command line option turns into a |
2656 | | /// pseudo directive in the predefines buffer. This handles it by sucking all |
2657 | | /// tokens through the preprocessor and discarding them (only keeping the side |
2658 | | /// effects on the preprocessor). |
2659 | | void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc, |
2660 | 0 | Token &IncludeMacrosTok) { |
2661 | | // This directive should only occur in the predefines buffer. If not, emit an |
2662 | | // error and reject it. |
2663 | 0 | SourceLocation Loc = IncludeMacrosTok.getLocation(); |
2664 | 0 | if (SourceMgr.getBufferName(Loc) != "<built-in>") { |
2665 | 0 | Diag(IncludeMacrosTok.getLocation(), |
2666 | 0 | diag::pp_include_macros_out_of_predefines); |
2667 | 0 | DiscardUntilEndOfDirective(); |
2668 | 0 | return; |
2669 | 0 | } |
2670 | | |
2671 | | // Treat this as a normal #include for checking purposes. If this is |
2672 | | // successful, it will push a new lexer onto the include stack. |
2673 | 0 | HandleIncludeDirective(HashLoc, IncludeMacrosTok); |
2674 | |
|
2675 | 0 | Token TmpTok; |
2676 | 0 | do { |
2677 | 0 | Lex(TmpTok); |
2678 | 0 | assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!"); |
2679 | 0 | } while (TmpTok.isNot(tok::hashhash)); |
2680 | 0 | } |
2681 | | |
2682 | | //===----------------------------------------------------------------------===// |
2683 | | // Preprocessor Macro Directive Handling. |
2684 | | //===----------------------------------------------------------------------===// |
2685 | | |
2686 | | /// ReadMacroParameterList - The ( starting a parameter list of a macro |
2687 | | /// definition has just been read. Lex the rest of the parameters and the |
2688 | | /// closing ), updating MI with what we learn. Return true if an error occurs |
2689 | | /// parsing the param list. |
2690 | 23 | bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) { |
2691 | 23 | SmallVector<IdentifierInfo*, 32> Parameters; |
2692 | | |
2693 | 23 | while (true) { |
2694 | 23 | LexUnexpandedNonComment(Tok); |
2695 | 23 | switch (Tok.getKind()) { |
2696 | 0 | case tok::r_paren: |
2697 | | // Found the end of the parameter list. |
2698 | 0 | if (Parameters.empty()) // #define FOO() |
2699 | 0 | return false; |
2700 | | // Otherwise we have #define FOO(A,) |
2701 | 0 | Diag(Tok, diag::err_pp_expected_ident_in_arg_list); |
2702 | 0 | return true; |
2703 | 0 | case tok::ellipsis: // #define X(... -> C99 varargs |
2704 | 0 | if (!LangOpts.C99) |
2705 | 0 | Diag(Tok, LangOpts.CPlusPlus11 ? |
2706 | 0 | diag::warn_cxx98_compat_variadic_macro : |
2707 | 0 | diag::ext_variadic_macro); |
2708 | | |
2709 | | // OpenCL v1.2 s6.9.e: variadic macros are not supported. |
2710 | 0 | if (LangOpts.OpenCL && !LangOpts.OpenCLCPlusPlus) { |
2711 | 0 | Diag(Tok, diag::ext_pp_opencl_variadic_macros); |
2712 | 0 | } |
2713 | | |
2714 | | // Lex the token after the identifier. |
2715 | 0 | LexUnexpandedNonComment(Tok); |
2716 | 0 | if (Tok.isNot(tok::r_paren)) { |
2717 | 0 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
2718 | 0 | return true; |
2719 | 0 | } |
2720 | | // Add the __VA_ARGS__ identifier as a parameter. |
2721 | 0 | Parameters.push_back(Ident__VA_ARGS__); |
2722 | 0 | MI->setIsC99Varargs(); |
2723 | 0 | MI->setParameterList(Parameters, BP); |
2724 | 0 | return false; |
2725 | 0 | case tok::eod: // #define X( |
2726 | 0 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
2727 | 0 | return true; |
2728 | 23 | default: |
2729 | | // Handle keywords and identifiers here to accept things like |
2730 | | // #define Foo(for) for. |
2731 | 23 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
2732 | 23 | if (!II) { |
2733 | | // #define X(1 |
2734 | 0 | Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); |
2735 | 0 | return true; |
2736 | 0 | } |
2737 | | |
2738 | | // If this is already used as a parameter, it is used multiple times (e.g. |
2739 | | // #define X(A,A. |
2740 | 23 | if (llvm::is_contained(Parameters, II)) { // C99 6.10.3p6 |
2741 | 0 | Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II; |
2742 | 0 | return true; |
2743 | 0 | } |
2744 | | |
2745 | | // Add the parameter to the macro info. |
2746 | 23 | Parameters.push_back(II); |
2747 | | |
2748 | | // Lex the token after the identifier. |
2749 | 23 | LexUnexpandedNonComment(Tok); |
2750 | | |
2751 | 23 | switch (Tok.getKind()) { |
2752 | 0 | default: // #define X(A B |
2753 | 0 | Diag(Tok, diag::err_pp_expected_comma_in_arg_list); |
2754 | 0 | return true; |
2755 | 23 | case tok::r_paren: // #define X(A) |
2756 | 23 | MI->setParameterList(Parameters, BP); |
2757 | 23 | return false; |
2758 | 0 | case tok::comma: // #define X(A, |
2759 | 0 | break; |
2760 | 0 | case tok::ellipsis: // #define X(A... -> GCC extension |
2761 | | // Diagnose extension. |
2762 | 0 | Diag(Tok, diag::ext_named_variadic_macro); |
2763 | | |
2764 | | // Lex the token after the identifier. |
2765 | 0 | LexUnexpandedNonComment(Tok); |
2766 | 0 | if (Tok.isNot(tok::r_paren)) { |
2767 | 0 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
2768 | 0 | return true; |
2769 | 0 | } |
2770 | | |
2771 | 0 | MI->setIsGNUVarargs(); |
2772 | 0 | MI->setParameterList(Parameters, BP); |
2773 | 0 | return false; |
2774 | 23 | } |
2775 | 23 | } |
2776 | 23 | } |
2777 | 23 | } |
2778 | | |
2779 | | static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI, |
2780 | 0 | const LangOptions &LOptions) { |
2781 | 0 | if (MI->getNumTokens() == 1) { |
2782 | 0 | const Token &Value = MI->getReplacementToken(0); |
2783 | | |
2784 | | // Macro that is identity, like '#define inline inline' is a valid pattern. |
2785 | 0 | if (MacroName.getKind() == Value.getKind()) |
2786 | 0 | return true; |
2787 | | |
2788 | | // Macro that maps a keyword to the same keyword decorated with leading/ |
2789 | | // trailing underscores is a valid pattern: |
2790 | | // #define inline __inline |
2791 | | // #define inline __inline__ |
2792 | | // #define inline _inline (in MS compatibility mode) |
2793 | 0 | StringRef MacroText = MacroName.getIdentifierInfo()->getName(); |
2794 | 0 | if (IdentifierInfo *II = Value.getIdentifierInfo()) { |
2795 | 0 | if (!II->isKeyword(LOptions)) |
2796 | 0 | return false; |
2797 | 0 | StringRef ValueText = II->getName(); |
2798 | 0 | StringRef TrimmedValue = ValueText; |
2799 | 0 | if (!ValueText.starts_with("__")) { |
2800 | 0 | if (ValueText.starts_with("_")) |
2801 | 0 | TrimmedValue = TrimmedValue.drop_front(1); |
2802 | 0 | else |
2803 | 0 | return false; |
2804 | 0 | } else { |
2805 | 0 | TrimmedValue = TrimmedValue.drop_front(2); |
2806 | 0 | if (TrimmedValue.ends_with("__")) |
2807 | 0 | TrimmedValue = TrimmedValue.drop_back(2); |
2808 | 0 | } |
2809 | 0 | return TrimmedValue.equals(MacroText); |
2810 | 0 | } else { |
2811 | 0 | return false; |
2812 | 0 | } |
2813 | 0 | } |
2814 | | |
2815 | | // #define inline |
2816 | 0 | return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static, |
2817 | 0 | tok::kw_const) && |
2818 | 0 | MI->getNumTokens() == 0; |
2819 | 0 | } |
2820 | | |
2821 | | // ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the |
2822 | | // entire line) of the macro's tokens and adds them to MacroInfo, and while |
2823 | | // doing so performs certain validity checks including (but not limited to): |
2824 | | // - # (stringization) is followed by a macro parameter |
2825 | | // |
2826 | | // Returns a nullptr if an invalid sequence of tokens is encountered or returns |
2827 | | // a pointer to a MacroInfo object. |
2828 | | |
2829 | | MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody( |
2830 | 18.4k | const Token &MacroNameTok, const bool ImmediatelyAfterHeaderGuard) { |
2831 | | |
2832 | 18.4k | Token LastTok = MacroNameTok; |
2833 | | // Create the new macro. |
2834 | 18.4k | MacroInfo *const MI = AllocateMacroInfo(MacroNameTok.getLocation()); |
2835 | | |
2836 | 18.4k | Token Tok; |
2837 | 18.4k | LexUnexpandedToken(Tok); |
2838 | | |
2839 | | // Ensure we consume the rest of the macro body if errors occur. |
2840 | 18.4k | auto _ = llvm::make_scope_exit([&]() { |
2841 | | // The flag indicates if we are still waiting for 'eod'. |
2842 | 18.4k | if (CurLexer->ParsingPreprocessorDirective) |
2843 | 0 | DiscardUntilEndOfDirective(); |
2844 | 18.4k | }); |
2845 | | |
2846 | | // Used to un-poison and then re-poison identifiers of the __VA_ARGS__ ilk |
2847 | | // within their appropriate context. |
2848 | 18.4k | VariadicMacroScopeGuard VariadicMacroScopeGuard(*this); |
2849 | | |
2850 | | // If this is a function-like macro definition, parse the argument list, |
2851 | | // marking each of the identifiers as being used as macro arguments. Also, |
2852 | | // check other constraints on the first token of the macro body. |
2853 | 18.4k | if (Tok.is(tok::eod)) { |
2854 | 368 | if (ImmediatelyAfterHeaderGuard) { |
2855 | | // Save this macro information since it may part of a header guard. |
2856 | 0 | CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(), |
2857 | 0 | MacroNameTok.getLocation()); |
2858 | 0 | } |
2859 | | // If there is no body to this macro, we have no special handling here. |
2860 | 18.0k | } else if (Tok.hasLeadingSpace()) { |
2861 | | // This is a normal token with leading space. Clear the leading space |
2862 | | // marker on the first token to get proper expansion. |
2863 | 18.0k | Tok.clearFlag(Token::LeadingSpace); |
2864 | 18.0k | } else if (Tok.is(tok::l_paren)) { |
2865 | | // This is a function-like macro definition. Read the argument list. |
2866 | 23 | MI->setIsFunctionLike(); |
2867 | 23 | if (ReadMacroParameterList(MI, LastTok)) |
2868 | 0 | return nullptr; |
2869 | | |
2870 | | // If this is a definition of an ISO C/C++ variadic function-like macro (not |
2871 | | // using the GNU named varargs extension) inform our variadic scope guard |
2872 | | // which un-poisons and re-poisons certain identifiers (e.g. __VA_ARGS__) |
2873 | | // allowed only within the definition of a variadic macro. |
2874 | | |
2875 | 23 | if (MI->isC99Varargs()) { |
2876 | 0 | VariadicMacroScopeGuard.enterScope(); |
2877 | 0 | } |
2878 | | |
2879 | | // Read the first token after the arg list for down below. |
2880 | 23 | LexUnexpandedToken(Tok); |
2881 | 23 | } else if (LangOpts.C99 || LangOpts.CPlusPlus11) { |
2882 | | // C99 requires whitespace between the macro definition and the body. Emit |
2883 | | // a diagnostic for something like "#define X+". |
2884 | 0 | Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); |
2885 | 0 | } else { |
2886 | | // C90 6.8 TC1 says: "In the definition of an object-like macro, if the |
2887 | | // first character of a replacement list is not a character required by |
2888 | | // subclause 5.2.1, then there shall be white-space separation between the |
2889 | | // identifier and the replacement list.". 5.2.1 lists this set: |
2890 | | // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which |
2891 | | // is irrelevant here. |
2892 | 0 | bool isInvalid = false; |
2893 | 0 | if (Tok.is(tok::at)) // @ is not in the list above. |
2894 | 0 | isInvalid = true; |
2895 | 0 | else if (Tok.is(tok::unknown)) { |
2896 | | // If we have an unknown token, it is something strange like "`". Since |
2897 | | // all of valid characters would have lexed into a single character |
2898 | | // token of some sort, we know this is not a valid case. |
2899 | 0 | isInvalid = true; |
2900 | 0 | } |
2901 | 0 | if (isInvalid) |
2902 | 0 | Diag(Tok, diag::ext_missing_whitespace_after_macro_name); |
2903 | 0 | else |
2904 | 0 | Diag(Tok, diag::warn_missing_whitespace_after_macro_name); |
2905 | 0 | } |
2906 | | |
2907 | 18.4k | if (!Tok.is(tok::eod)) |
2908 | 18.0k | LastTok = Tok; |
2909 | | |
2910 | 18.4k | SmallVector<Token, 16> Tokens; |
2911 | | |
2912 | | // Read the rest of the macro body. |
2913 | 18.4k | if (MI->isObjectLike()) { |
2914 | | // Object-like macros are very simple, just read their body. |
2915 | 40.7k | while (Tok.isNot(tok::eod)) { |
2916 | 22.3k | LastTok = Tok; |
2917 | 22.3k | Tokens.push_back(Tok); |
2918 | | // Get the next token of the macro. |
2919 | 22.3k | LexUnexpandedToken(Tok); |
2920 | 22.3k | } |
2921 | 18.3k | } else { |
2922 | | // Otherwise, read the body of a function-like macro. While we are at it, |
2923 | | // check C99 6.10.3.2p1: ensure that # operators are followed by macro |
2924 | | // parameters in function-like macro expansions. |
2925 | | |
2926 | 23 | VAOptDefinitionContext VAOCtx(*this); |
2927 | | |
2928 | 230 | while (Tok.isNot(tok::eod)) { |
2929 | 207 | LastTok = Tok; |
2930 | | |
2931 | 207 | if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) { |
2932 | 207 | Tokens.push_back(Tok); |
2933 | | |
2934 | 207 | if (VAOCtx.isVAOptToken(Tok)) { |
2935 | | // If we're already within a VAOPT, emit an error. |
2936 | 0 | if (VAOCtx.isInVAOpt()) { |
2937 | 0 | Diag(Tok, diag::err_pp_vaopt_nested_use); |
2938 | 0 | return nullptr; |
2939 | 0 | } |
2940 | | // Ensure VAOPT is followed by a '(' . |
2941 | 0 | LexUnexpandedToken(Tok); |
2942 | 0 | if (Tok.isNot(tok::l_paren)) { |
2943 | 0 | Diag(Tok, diag::err_pp_missing_lparen_in_vaopt_use); |
2944 | 0 | return nullptr; |
2945 | 0 | } |
2946 | 0 | Tokens.push_back(Tok); |
2947 | 0 | VAOCtx.sawVAOptFollowedByOpeningParens(Tok.getLocation()); |
2948 | 0 | LexUnexpandedToken(Tok); |
2949 | 0 | if (Tok.is(tok::hashhash)) { |
2950 | 0 | Diag(Tok, diag::err_vaopt_paste_at_start); |
2951 | 0 | return nullptr; |
2952 | 0 | } |
2953 | 0 | continue; |
2954 | 207 | } else if (VAOCtx.isInVAOpt()) { |
2955 | 0 | if (Tok.is(tok::r_paren)) { |
2956 | 0 | if (VAOCtx.sawClosingParen()) { |
2957 | 0 | assert(Tokens.size() >= 3 && |
2958 | 0 | "Must have seen at least __VA_OPT__( " |
2959 | 0 | "and a subsequent tok::r_paren"); |
2960 | 0 | if (Tokens[Tokens.size() - 2].is(tok::hashhash)) { |
2961 | 0 | Diag(Tok, diag::err_vaopt_paste_at_end); |
2962 | 0 | return nullptr; |
2963 | 0 | } |
2964 | 0 | } |
2965 | 0 | } else if (Tok.is(tok::l_paren)) { |
2966 | 0 | VAOCtx.sawOpeningParen(Tok.getLocation()); |
2967 | 0 | } |
2968 | 0 | } |
2969 | | // Get the next token of the macro. |
2970 | 207 | LexUnexpandedToken(Tok); |
2971 | 207 | continue; |
2972 | 207 | } |
2973 | | |
2974 | | // If we're in -traditional mode, then we should ignore stringification |
2975 | | // and token pasting. Mark the tokens as unknown so as not to confuse |
2976 | | // things. |
2977 | 0 | if (getLangOpts().TraditionalCPP) { |
2978 | 0 | Tok.setKind(tok::unknown); |
2979 | 0 | Tokens.push_back(Tok); |
2980 | | |
2981 | | // Get the next token of the macro. |
2982 | 0 | LexUnexpandedToken(Tok); |
2983 | 0 | continue; |
2984 | 0 | } |
2985 | | |
2986 | 0 | if (Tok.is(tok::hashhash)) { |
2987 | | // If we see token pasting, check if it looks like the gcc comma |
2988 | | // pasting extension. We'll use this information to suppress |
2989 | | // diagnostics later on. |
2990 | | |
2991 | | // Get the next token of the macro. |
2992 | 0 | LexUnexpandedToken(Tok); |
2993 | |
|
2994 | 0 | if (Tok.is(tok::eod)) { |
2995 | 0 | Tokens.push_back(LastTok); |
2996 | 0 | break; |
2997 | 0 | } |
2998 | | |
2999 | 0 | if (!Tokens.empty() && Tok.getIdentifierInfo() == Ident__VA_ARGS__ && |
3000 | 0 | Tokens[Tokens.size() - 1].is(tok::comma)) |
3001 | 0 | MI->setHasCommaPasting(); |
3002 | | |
3003 | | // Things look ok, add the '##' token to the macro. |
3004 | 0 | Tokens.push_back(LastTok); |
3005 | 0 | continue; |
3006 | 0 | } |
3007 | | |
3008 | | // Our Token is a stringization operator. |
3009 | | // Get the next token of the macro. |
3010 | 0 | LexUnexpandedToken(Tok); |
3011 | | |
3012 | | // Check for a valid macro arg identifier or __VA_OPT__. |
3013 | 0 | if (!VAOCtx.isVAOptToken(Tok) && |
3014 | 0 | (Tok.getIdentifierInfo() == nullptr || |
3015 | 0 | MI->getParameterNum(Tok.getIdentifierInfo()) == -1)) { |
3016 | | |
3017 | | // If this is assembler-with-cpp mode, we accept random gibberish after |
3018 | | // the '#' because '#' is often a comment character. However, change |
3019 | | // the kind of the token to tok::unknown so that the preprocessor isn't |
3020 | | // confused. |
3021 | 0 | if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) { |
3022 | 0 | LastTok.setKind(tok::unknown); |
3023 | 0 | Tokens.push_back(LastTok); |
3024 | 0 | continue; |
3025 | 0 | } else { |
3026 | 0 | Diag(Tok, diag::err_pp_stringize_not_parameter) |
3027 | 0 | << LastTok.is(tok::hashat); |
3028 | 0 | return nullptr; |
3029 | 0 | } |
3030 | 0 | } |
3031 | | |
3032 | | // Things look ok, add the '#' and param name tokens to the macro. |
3033 | 0 | Tokens.push_back(LastTok); |
3034 | | |
3035 | | // If the token following '#' is VAOPT, let the next iteration handle it |
3036 | | // and check it for correctness, otherwise add the token and prime the |
3037 | | // loop with the next one. |
3038 | 0 | if (!VAOCtx.isVAOptToken(Tok)) { |
3039 | 0 | Tokens.push_back(Tok); |
3040 | 0 | LastTok = Tok; |
3041 | | |
3042 | | // Get the next token of the macro. |
3043 | 0 | LexUnexpandedToken(Tok); |
3044 | 0 | } |
3045 | 0 | } |
3046 | 23 | if (VAOCtx.isInVAOpt()) { |
3047 | 0 | assert(Tok.is(tok::eod) && "Must be at End Of preprocessing Directive"); |
3048 | 0 | Diag(Tok, diag::err_pp_expected_after) |
3049 | 0 | << LastTok.getKind() << tok::r_paren; |
3050 | 0 | Diag(VAOCtx.getUnmatchedOpeningParenLoc(), diag::note_matching) << tok::l_paren; |
3051 | 0 | return nullptr; |
3052 | 0 | } |
3053 | 23 | } |
3054 | 18.4k | MI->setDefinitionEndLoc(LastTok.getLocation()); |
3055 | | |
3056 | 18.4k | MI->setTokens(Tokens, BP); |
3057 | 18.4k | return MI; |
3058 | 18.4k | } |
3059 | | |
3060 | 0 | static bool isObjCProtectedMacro(const IdentifierInfo *II) { |
3061 | 0 | return II->isStr("__strong") || II->isStr("__weak") || |
3062 | 0 | II->isStr("__unsafe_unretained") || II->isStr("__autoreleasing"); |
3063 | 0 | } |
3064 | | |
3065 | | /// HandleDefineDirective - Implements \#define. This consumes the entire macro |
3066 | | /// line then lets the caller lex the next real token. |
3067 | | void Preprocessor::HandleDefineDirective( |
3068 | 18.4k | Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) { |
3069 | 18.4k | ++NumDefined; |
3070 | | |
3071 | 18.4k | Token MacroNameTok; |
3072 | 18.4k | bool MacroShadowsKeyword; |
3073 | 18.4k | ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword); |
3074 | | |
3075 | | // Error reading macro name? If so, diagnostic already issued. |
3076 | 18.4k | if (MacroNameTok.is(tok::eod)) |
3077 | 0 | return; |
3078 | | |
3079 | 18.4k | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
3080 | | // Issue a final pragma warning if we're defining a macro that was has been |
3081 | | // undefined and is being redefined. |
3082 | 18.4k | if (!II->hasMacroDefinition() && II->hadMacroDefinition() && II->isFinal()) |
3083 | 0 | emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/false); |
3084 | | |
3085 | | // If we are supposed to keep comments in #defines, reenable comment saving |
3086 | | // mode. |
3087 | 18.4k | if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments); |
3088 | | |
3089 | 18.4k | MacroInfo *const MI = ReadOptionalMacroParameterListAndBody( |
3090 | 18.4k | MacroNameTok, ImmediatelyAfterHeaderGuard); |
3091 | | |
3092 | 18.4k | if (!MI) return; |
3093 | | |
3094 | 18.4k | if (MacroShadowsKeyword && |
3095 | 18.4k | !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) { |
3096 | 0 | Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword); |
3097 | 0 | } |
3098 | | // Check that there is no paste (##) operator at the beginning or end of the |
3099 | | // replacement list. |
3100 | 18.4k | unsigned NumTokens = MI->getNumTokens(); |
3101 | 18.4k | if (NumTokens != 0) { |
3102 | 18.0k | if (MI->getReplacementToken(0).is(tok::hashhash)) { |
3103 | 0 | Diag(MI->getReplacementToken(0), diag::err_paste_at_start); |
3104 | 0 | return; |
3105 | 0 | } |
3106 | 18.0k | if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) { |
3107 | 0 | Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); |
3108 | 0 | return; |
3109 | 0 | } |
3110 | 18.0k | } |
3111 | | |
3112 | | // When skipping just warn about macros that do not match. |
3113 | 18.4k | if (SkippingUntilPCHThroughHeader) { |
3114 | 0 | const MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo()); |
3115 | 0 | if (!OtherMI || !MI->isIdenticalTo(*OtherMI, *this, |
3116 | 0 | /*Syntactic=*/LangOpts.MicrosoftExt)) |
3117 | 0 | Diag(MI->getDefinitionLoc(), diag::warn_pp_macro_def_mismatch_with_pch) |
3118 | 0 | << MacroNameTok.getIdentifierInfo(); |
3119 | | // Issue the diagnostic but allow the change if msvc extensions are enabled |
3120 | 0 | if (!LangOpts.MicrosoftExt) |
3121 | 0 | return; |
3122 | 0 | } |
3123 | | |
3124 | | // Finally, if this identifier already had a macro defined for it, verify that |
3125 | | // the macro bodies are identical, and issue diagnostics if they are not. |
3126 | 18.4k | if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) { |
3127 | | // Final macros are hard-mode: they always warn. Even if the bodies are |
3128 | | // identical. Even if they are in system headers. Even if they are things we |
3129 | | // would silently allow in the past. |
3130 | 0 | if (MacroNameTok.getIdentifierInfo()->isFinal()) |
3131 | 0 | emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/false); |
3132 | | |
3133 | | // In Objective-C, ignore attempts to directly redefine the builtin |
3134 | | // definitions of the ownership qualifiers. It's still possible to |
3135 | | // #undef them. |
3136 | 0 | if (getLangOpts().ObjC && |
3137 | 0 | SourceMgr.getFileID(OtherMI->getDefinitionLoc()) == |
3138 | 0 | getPredefinesFileID() && |
3139 | 0 | isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) { |
3140 | | // Warn if it changes the tokens. |
3141 | 0 | if ((!getDiagnostics().getSuppressSystemWarnings() || |
3142 | 0 | !SourceMgr.isInSystemHeader(DefineTok.getLocation())) && |
3143 | 0 | !MI->isIdenticalTo(*OtherMI, *this, |
3144 | 0 | /*Syntactic=*/LangOpts.MicrosoftExt)) { |
3145 | 0 | Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored); |
3146 | 0 | } |
3147 | 0 | assert(!OtherMI->isWarnIfUnused()); |
3148 | 0 | return; |
3149 | 0 | } |
3150 | | |
3151 | | // It is very common for system headers to have tons of macro redefinitions |
3152 | | // and for warnings to be disabled in system headers. If this is the case, |
3153 | | // then don't bother calling MacroInfo::isIdenticalTo. |
3154 | 0 | if (!getDiagnostics().getSuppressSystemWarnings() || |
3155 | 0 | !SourceMgr.isInSystemHeader(DefineTok.getLocation())) { |
3156 | |
|
3157 | 0 | if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused()) |
3158 | 0 | Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); |
3159 | | |
3160 | | // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and |
3161 | | // C++ [cpp.predefined]p4, but allow it as an extension. |
3162 | 0 | if (isLanguageDefinedBuiltin(SourceMgr, OtherMI, II->getName())) |
3163 | 0 | Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro); |
3164 | | // Macros must be identical. This means all tokens and whitespace |
3165 | | // separation must be the same. C99 6.10.3p2. |
3166 | 0 | else if (!OtherMI->isAllowRedefinitionsWithoutWarning() && |
3167 | 0 | !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) { |
3168 | 0 | Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef) |
3169 | 0 | << MacroNameTok.getIdentifierInfo(); |
3170 | 0 | Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition); |
3171 | 0 | } |
3172 | 0 | } |
3173 | 0 | if (OtherMI->isWarnIfUnused()) |
3174 | 0 | WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc()); |
3175 | 0 | } |
3176 | | |
3177 | 18.4k | DefMacroDirective *MD = |
3178 | 18.4k | appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI); |
3179 | | |
3180 | 18.4k | assert(!MI->isUsed()); |
3181 | | // If we need warning for not using the macro, add its location in the |
3182 | | // warn-because-unused-macro set. If it gets used it will be removed from set. |
3183 | 18.4k | if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) && |
3184 | 18.4k | !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc()) && |
3185 | 18.4k | !MacroExpansionInDirectivesOverride && |
3186 | 18.4k | getSourceManager().getFileID(MI->getDefinitionLoc()) != |
3187 | 0 | getPredefinesFileID()) { |
3188 | 0 | MI->setIsWarnIfUnused(true); |
3189 | 0 | WarnUnusedMacroLocs.insert(MI->getDefinitionLoc()); |
3190 | 0 | } |
3191 | | |
3192 | | // If the callbacks want to know, tell them about the macro definition. |
3193 | 18.4k | if (Callbacks) |
3194 | 18.4k | Callbacks->MacroDefined(MacroNameTok, MD); |
3195 | | |
3196 | | // If we're in MS compatibility mode and the macro being defined is the |
3197 | | // assert macro, implicitly add a macro definition for static_assert to work |
3198 | | // around their broken assert.h header file in C. Only do so if there isn't |
3199 | | // already a static_assert macro defined. |
3200 | 18.4k | if (!getLangOpts().CPlusPlus && getLangOpts().MSVCCompat && |
3201 | 18.4k | MacroNameTok.getIdentifierInfo()->isStr("assert") && |
3202 | 18.4k | !isMacroDefined("static_assert")) { |
3203 | 0 | MacroInfo *MI = AllocateMacroInfo(SourceLocation()); |
3204 | |
|
3205 | 0 | Token Tok; |
3206 | 0 | Tok.startToken(); |
3207 | 0 | Tok.setKind(tok::kw__Static_assert); |
3208 | 0 | Tok.setIdentifierInfo(getIdentifierInfo("_Static_assert")); |
3209 | 0 | MI->setTokens({Tok}, BP); |
3210 | 0 | (void)appendDefMacroDirective(getIdentifierInfo("static_assert"), MI); |
3211 | 0 | } |
3212 | 18.4k | } |
3213 | | |
3214 | | /// HandleUndefDirective - Implements \#undef. |
3215 | | /// |
3216 | 0 | void Preprocessor::HandleUndefDirective() { |
3217 | 0 | ++NumUndefined; |
3218 | |
|
3219 | 0 | Token MacroNameTok; |
3220 | 0 | ReadMacroName(MacroNameTok, MU_Undef); |
3221 | | |
3222 | | // Error reading macro name? If so, diagnostic already issued. |
3223 | 0 | if (MacroNameTok.is(tok::eod)) |
3224 | 0 | return; |
3225 | | |
3226 | | // Check to see if this is the last token on the #undef line. |
3227 | 0 | CheckEndOfDirective("undef"); |
3228 | | |
3229 | | // Okay, we have a valid identifier to undef. |
3230 | 0 | auto *II = MacroNameTok.getIdentifierInfo(); |
3231 | 0 | auto MD = getMacroDefinition(II); |
3232 | 0 | UndefMacroDirective *Undef = nullptr; |
3233 | |
|
3234 | 0 | if (II->isFinal()) |
3235 | 0 | emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/true); |
3236 | | |
3237 | | // If the macro is not defined, this is a noop undef. |
3238 | 0 | if (const MacroInfo *MI = MD.getMacroInfo()) { |
3239 | 0 | if (!MI->isUsed() && MI->isWarnIfUnused()) |
3240 | 0 | Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); |
3241 | | |
3242 | | // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 and |
3243 | | // C++ [cpp.predefined]p4, but allow it as an extension. |
3244 | 0 | if (isLanguageDefinedBuiltin(SourceMgr, MI, II->getName())) |
3245 | 0 | Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro); |
3246 | |
|
3247 | 0 | if (MI->isWarnIfUnused()) |
3248 | 0 | WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); |
3249 | |
|
3250 | 0 | Undef = AllocateUndefMacroDirective(MacroNameTok.getLocation()); |
3251 | 0 | } |
3252 | | |
3253 | | // If the callbacks want to know, tell them about the macro #undef. |
3254 | | // Note: no matter if the macro was defined or not. |
3255 | 0 | if (Callbacks) |
3256 | 0 | Callbacks->MacroUndefined(MacroNameTok, MD, Undef); |
3257 | |
|
3258 | 0 | if (Undef) |
3259 | 0 | appendMacroDirective(II, Undef); |
3260 | 0 | } |
3261 | | |
3262 | | //===----------------------------------------------------------------------===// |
3263 | | // Preprocessor Conditional Directive Handling. |
3264 | | //===----------------------------------------------------------------------===// |
3265 | | |
3266 | | /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef |
3267 | | /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is |
3268 | | /// true if any tokens have been returned or pp-directives activated before this |
3269 | | /// \#ifndef has been lexed. |
3270 | | /// |
3271 | | void Preprocessor::HandleIfdefDirective(Token &Result, |
3272 | | const Token &HashToken, |
3273 | | bool isIfndef, |
3274 | 0 | bool ReadAnyTokensBeforeDirective) { |
3275 | 0 | ++NumIf; |
3276 | 0 | Token DirectiveTok = Result; |
3277 | |
|
3278 | 0 | Token MacroNameTok; |
3279 | 0 | ReadMacroName(MacroNameTok); |
3280 | | |
3281 | | // Error reading macro name? If so, diagnostic already issued. |
3282 | 0 | if (MacroNameTok.is(tok::eod)) { |
3283 | | // Skip code until we get to #endif. This helps with recovery by not |
3284 | | // emitting an error when the #endif is reached. |
3285 | 0 | SkipExcludedConditionalBlock(HashToken.getLocation(), |
3286 | 0 | DirectiveTok.getLocation(), |
3287 | 0 | /*Foundnonskip*/ false, /*FoundElse*/ false); |
3288 | 0 | return; |
3289 | 0 | } |
3290 | | |
3291 | 0 | emitMacroExpansionWarnings(MacroNameTok); |
3292 | | |
3293 | | // Check to see if this is the last token on the #if[n]def line. |
3294 | 0 | CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); |
3295 | |
|
3296 | 0 | IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); |
3297 | 0 | auto MD = getMacroDefinition(MII); |
3298 | 0 | MacroInfo *MI = MD.getMacroInfo(); |
3299 | |
|
3300 | 0 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
3301 | | // If the start of a top-level #ifdef and if the macro is not defined, |
3302 | | // inform MIOpt that this might be the start of a proper include guard. |
3303 | | // Otherwise it is some other form of unknown conditional which we can't |
3304 | | // handle. |
3305 | 0 | if (!ReadAnyTokensBeforeDirective && !MI) { |
3306 | 0 | assert(isIfndef && "#ifdef shouldn't reach here"); |
3307 | 0 | CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation()); |
3308 | 0 | } else |
3309 | 0 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
3310 | 0 | } |
3311 | | |
3312 | | // If there is a macro, process it. |
3313 | 0 | if (MI) // Mark it used. |
3314 | 0 | markMacroAsUsed(MI); |
3315 | |
|
3316 | 0 | if (Callbacks) { |
3317 | 0 | if (isIfndef) |
3318 | 0 | Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD); |
3319 | 0 | else |
3320 | 0 | Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD); |
3321 | 0 | } |
3322 | |
|
3323 | 0 | bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks && |
3324 | 0 | getSourceManager().isInMainFile(DirectiveTok.getLocation()); |
3325 | | |
3326 | | // Should we include the stuff contained by this directive? |
3327 | 0 | if (PPOpts->SingleFileParseMode && !MI) { |
3328 | | // In 'single-file-parse mode' undefined identifiers trigger parsing of all |
3329 | | // the directive blocks. |
3330 | 0 | CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), |
3331 | 0 | /*wasskip*/false, /*foundnonskip*/false, |
3332 | 0 | /*foundelse*/false); |
3333 | 0 | } else if (!MI == isIfndef || RetainExcludedCB) { |
3334 | | // Yes, remember that we are inside a conditional, then lex the next token. |
3335 | 0 | CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), |
3336 | 0 | /*wasskip*/false, /*foundnonskip*/true, |
3337 | 0 | /*foundelse*/false); |
3338 | 0 | } else { |
3339 | | // No, skip the contents of this block. |
3340 | 0 | SkipExcludedConditionalBlock(HashToken.getLocation(), |
3341 | 0 | DirectiveTok.getLocation(), |
3342 | 0 | /*Foundnonskip*/ false, |
3343 | 0 | /*FoundElse*/ false); |
3344 | 0 | } |
3345 | 0 | } |
3346 | | |
3347 | | /// HandleIfDirective - Implements the \#if directive. |
3348 | | /// |
3349 | | void Preprocessor::HandleIfDirective(Token &IfToken, |
3350 | | const Token &HashToken, |
3351 | 0 | bool ReadAnyTokensBeforeDirective) { |
3352 | 0 | ++NumIf; |
3353 | | |
3354 | | // Parse and evaluate the conditional expression. |
3355 | 0 | IdentifierInfo *IfNDefMacro = nullptr; |
3356 | 0 | const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro); |
3357 | 0 | const bool ConditionalTrue = DER.Conditional; |
3358 | | // Lexer might become invalid if we hit code completion point while evaluating |
3359 | | // expression. |
3360 | 0 | if (!CurPPLexer) |
3361 | 0 | return; |
3362 | | |
3363 | | // If this condition is equivalent to #ifndef X, and if this is the first |
3364 | | // directive seen, handle it for the multiple-include optimization. |
3365 | 0 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
3366 | 0 | if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue) |
3367 | | // FIXME: Pass in the location of the macro name, not the 'if' token. |
3368 | 0 | CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation()); |
3369 | 0 | else |
3370 | 0 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
3371 | 0 | } |
3372 | |
|
3373 | 0 | if (Callbacks) |
3374 | 0 | Callbacks->If( |
3375 | 0 | IfToken.getLocation(), DER.ExprRange, |
3376 | 0 | (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False)); |
3377 | |
|
3378 | 0 | bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks && |
3379 | 0 | getSourceManager().isInMainFile(IfToken.getLocation()); |
3380 | | |
3381 | | // Should we include the stuff contained by this directive? |
3382 | 0 | if (PPOpts->SingleFileParseMode && DER.IncludedUndefinedIds) { |
3383 | | // In 'single-file-parse mode' undefined identifiers trigger parsing of all |
3384 | | // the directive blocks. |
3385 | 0 | CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, |
3386 | 0 | /*foundnonskip*/false, /*foundelse*/false); |
3387 | 0 | } else if (ConditionalTrue || RetainExcludedCB) { |
3388 | | // Yes, remember that we are inside a conditional, then lex the next token. |
3389 | 0 | CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, |
3390 | 0 | /*foundnonskip*/true, /*foundelse*/false); |
3391 | 0 | } else { |
3392 | | // No, skip the contents of this block. |
3393 | 0 | SkipExcludedConditionalBlock(HashToken.getLocation(), IfToken.getLocation(), |
3394 | 0 | /*Foundnonskip*/ false, |
3395 | 0 | /*FoundElse*/ false); |
3396 | 0 | } |
3397 | 0 | } |
3398 | | |
3399 | | /// HandleEndifDirective - Implements the \#endif directive. |
3400 | | /// |
3401 | 0 | void Preprocessor::HandleEndifDirective(Token &EndifToken) { |
3402 | 0 | ++NumEndif; |
3403 | | |
3404 | | // Check that this is the whole directive. |
3405 | 0 | CheckEndOfDirective("endif"); |
3406 | |
|
3407 | 0 | PPConditionalInfo CondInfo; |
3408 | 0 | if (CurPPLexer->popConditionalLevel(CondInfo)) { |
3409 | | // No conditionals on the stack: this is an #endif without an #if. |
3410 | 0 | Diag(EndifToken, diag::err_pp_endif_without_if); |
3411 | 0 | return; |
3412 | 0 | } |
3413 | | |
3414 | | // If this the end of a top-level #endif, inform MIOpt. |
3415 | 0 | if (CurPPLexer->getConditionalStackDepth() == 0) |
3416 | 0 | CurPPLexer->MIOpt.ExitTopLevelConditional(); |
3417 | |
|
3418 | 0 | assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode && |
3419 | 0 | "This code should only be reachable in the non-skipping case!"); |
3420 | | |
3421 | 0 | if (Callbacks) |
3422 | 0 | Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc); |
3423 | 0 | } |
3424 | | |
3425 | | /// HandleElseDirective - Implements the \#else directive. |
3426 | | /// |
3427 | 0 | void Preprocessor::HandleElseDirective(Token &Result, const Token &HashToken) { |
3428 | 0 | ++NumElse; |
3429 | | |
3430 | | // #else directive in a non-skipping conditional... start skipping. |
3431 | 0 | CheckEndOfDirective("else"); |
3432 | |
|
3433 | 0 | PPConditionalInfo CI; |
3434 | 0 | if (CurPPLexer->popConditionalLevel(CI)) { |
3435 | 0 | Diag(Result, diag::pp_err_else_without_if); |
3436 | 0 | return; |
3437 | 0 | } |
3438 | | |
3439 | | // If this is a top-level #else, inform the MIOpt. |
3440 | 0 | if (CurPPLexer->getConditionalStackDepth() == 0) |
3441 | 0 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
3442 | | |
3443 | | // If this is a #else with a #else before it, report the error. |
3444 | 0 | if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); |
3445 | |
|
3446 | 0 | if (Callbacks) |
3447 | 0 | Callbacks->Else(Result.getLocation(), CI.IfLoc); |
3448 | |
|
3449 | 0 | bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks && |
3450 | 0 | getSourceManager().isInMainFile(Result.getLocation()); |
3451 | |
|
3452 | 0 | if ((PPOpts->SingleFileParseMode && !CI.FoundNonSkip) || RetainExcludedCB) { |
3453 | | // In 'single-file-parse mode' undefined identifiers trigger parsing of all |
3454 | | // the directive blocks. |
3455 | 0 | CurPPLexer->pushConditionalLevel(CI.IfLoc, /*wasskip*/false, |
3456 | 0 | /*foundnonskip*/false, /*foundelse*/true); |
3457 | 0 | return; |
3458 | 0 | } |
3459 | | |
3460 | | // Finally, skip the rest of the contents of this block. |
3461 | 0 | SkipExcludedConditionalBlock(HashToken.getLocation(), CI.IfLoc, |
3462 | 0 | /*Foundnonskip*/ true, |
3463 | 0 | /*FoundElse*/ true, Result.getLocation()); |
3464 | 0 | } |
3465 | | |
3466 | | /// Implements the \#elif, \#elifdef, and \#elifndef directives. |
3467 | | void Preprocessor::HandleElifFamilyDirective(Token &ElifToken, |
3468 | | const Token &HashToken, |
3469 | 0 | tok::PPKeywordKind Kind) { |
3470 | 0 | PPElifDiag DirKind = Kind == tok::pp_elif ? PED_Elif |
3471 | 0 | : Kind == tok::pp_elifdef ? PED_Elifdef |
3472 | 0 | : PED_Elifndef; |
3473 | 0 | ++NumElse; |
3474 | | |
3475 | | // Warn if using `#elifdef` & `#elifndef` in not C23 & C++23 mode. |
3476 | 0 | switch (DirKind) { |
3477 | 0 | case PED_Elifdef: |
3478 | 0 | case PED_Elifndef: |
3479 | 0 | unsigned DiagID; |
3480 | 0 | if (LangOpts.CPlusPlus) |
3481 | 0 | DiagID = LangOpts.CPlusPlus23 ? diag::warn_cxx23_compat_pp_directive |
3482 | 0 | : diag::ext_cxx23_pp_directive; |
3483 | 0 | else |
3484 | 0 | DiagID = LangOpts.C23 ? diag::warn_c23_compat_pp_directive |
3485 | 0 | : diag::ext_c23_pp_directive; |
3486 | 0 | Diag(ElifToken, DiagID) << DirKind; |
3487 | 0 | break; |
3488 | 0 | default: |
3489 | 0 | break; |
3490 | 0 | } |
3491 | | |
3492 | | // #elif directive in a non-skipping conditional... start skipping. |
3493 | | // We don't care what the condition is, because we will always skip it (since |
3494 | | // the block immediately before it was included). |
3495 | 0 | SourceRange ConditionRange = DiscardUntilEndOfDirective(); |
3496 | |
|
3497 | 0 | PPConditionalInfo CI; |
3498 | 0 | if (CurPPLexer->popConditionalLevel(CI)) { |
3499 | 0 | Diag(ElifToken, diag::pp_err_elif_without_if) << DirKind; |
3500 | 0 | return; |
3501 | 0 | } |
3502 | | |
3503 | | // If this is a top-level #elif, inform the MIOpt. |
3504 | 0 | if (CurPPLexer->getConditionalStackDepth() == 0) |
3505 | 0 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
3506 | | |
3507 | | // If this is a #elif with a #else before it, report the error. |
3508 | 0 | if (CI.FoundElse) |
3509 | 0 | Diag(ElifToken, diag::pp_err_elif_after_else) << DirKind; |
3510 | |
|
3511 | 0 | if (Callbacks) { |
3512 | 0 | switch (Kind) { |
3513 | 0 | case tok::pp_elif: |
3514 | 0 | Callbacks->Elif(ElifToken.getLocation(), ConditionRange, |
3515 | 0 | PPCallbacks::CVK_NotEvaluated, CI.IfLoc); |
3516 | 0 | break; |
3517 | 0 | case tok::pp_elifdef: |
3518 | 0 | Callbacks->Elifdef(ElifToken.getLocation(), ConditionRange, CI.IfLoc); |
3519 | 0 | break; |
3520 | 0 | case tok::pp_elifndef: |
3521 | 0 | Callbacks->Elifndef(ElifToken.getLocation(), ConditionRange, CI.IfLoc); |
3522 | 0 | break; |
3523 | 0 | default: |
3524 | 0 | assert(false && "unexpected directive kind"); |
3525 | 0 | break; |
3526 | 0 | } |
3527 | 0 | } |
3528 | | |
3529 | 0 | bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks && |
3530 | 0 | getSourceManager().isInMainFile(ElifToken.getLocation()); |
3531 | |
|
3532 | 0 | if ((PPOpts->SingleFileParseMode && !CI.FoundNonSkip) || RetainExcludedCB) { |
3533 | | // In 'single-file-parse mode' undefined identifiers trigger parsing of all |
3534 | | // the directive blocks. |
3535 | 0 | CurPPLexer->pushConditionalLevel(ElifToken.getLocation(), /*wasskip*/false, |
3536 | 0 | /*foundnonskip*/false, /*foundelse*/false); |
3537 | 0 | return; |
3538 | 0 | } |
3539 | | |
3540 | | // Finally, skip the rest of the contents of this block. |
3541 | 0 | SkipExcludedConditionalBlock( |
3542 | 0 | HashToken.getLocation(), CI.IfLoc, /*Foundnonskip*/ true, |
3543 | 0 | /*FoundElse*/ CI.FoundElse, ElifToken.getLocation()); |
3544 | 0 | } |