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