1 //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
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
7 //===----------------------------------------------------------------------===//
10 /// Implements # directive processing for the Preprocessor.
12 //===----------------------------------------------------------------------===//
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"
52 using namespace clang;
54 //===----------------------------------------------------------------------===//
55 // Utility Methods for Preprocessor Directive Handling.
56 //===----------------------------------------------------------------------===//
58 MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
59 auto *MIChain = new (BP) MacroInfoChain{L, MIChainHead};
60 MIChainHead = MIChain;
64 DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
66 return new (BP) DefMacroDirective(MI, Loc);
70 Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
71 return new (BP) UndefMacroDirective(UndefLoc);
74 VisibilityMacroDirective *
75 Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
77 return new (BP) VisibilityMacroDirective(Loc, isPublic);
80 /// Read and discard all tokens remaining on the current line until
81 /// the tok::eod token is found.
82 SourceRange Preprocessor::DiscardUntilEndOfDirective() {
86 LexUnexpandedToken(Tmp);
87 Res.setBegin(Tmp.getLocation());
88 while (Tmp.isNot(tok::eod)) {
89 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
90 LexUnexpandedToken(Tmp);
92 Res.setEnd(Tmp.getLocation());
96 /// Enumerates possible cases of #define/#undef a reserved identifier.
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
103 /// Checks if the specified identifier is reserved in the specified
105 /// This function does not check if the identifier is a keyword.
106 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 if (Text.size() >= 2 && Text[0] == '_' &&
111 (isUppercase(Text[1]) || Text[1] == '_'))
113 // C++ [global.names]
114 // Each name that contains a double underscore ... is reserved to the
115 // implementation for any use.
116 if (Lang.CPlusPlus) {
117 if (Text.find("__") != StringRef::npos)
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 StringRef ModuleName) {
131 StringRef TopLevelName = M->getTopLevelModuleName();
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 if (M->getTopLevelModule()->IsFramework && CurrentModule == ModuleName &&
136 !CurrentModule.endswith("_Private") && TopLevelName.endswith("_Private"))
137 TopLevelName = TopLevelName.drop_back(8);
139 return TopLevelName == CurrentModule;
142 static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
143 const LangOptions &Lang = PP.getLangOpts();
144 StringRef Text = II->getName();
145 if (isReservedId(Text, Lang))
146 return MD_ReservedMacro;
147 if (II->isKeyword(Lang))
148 return MD_KeywordDef;
149 if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final")))
150 return MD_KeywordDef;
154 static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
155 const LangOptions &Lang = PP.getLangOpts();
156 StringRef Text = II->getName();
157 // Do not warn on keyword undef. It is generally harmless and widely used.
158 if (isReservedId(Text, Lang))
159 return MD_ReservedMacro;
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 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 if (::llvm::sys::path::begin(Include)->equals_lower("boost"))
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 static const size_t MaxStdHeaderNameLen = 18u;
177 if (Include.size() > MaxStdHeaderNameLen)
180 // Lowercase and normalize the search string.
181 SmallString<32> LowerInclude{Include};
182 for (char &Ch : LowerInclude) {
183 // In the ASCII range?
184 if (static_cast<unsigned char>(Ch) > 0x7f)
185 return false; // Can't be a standard header
187 if (Ch >= 'A' && Ch <= 'Z')
189 // Normalize path separators for comparison purposes.
190 else if (::llvm::sys::path::is_separator(Ch))
194 // The standard C/C++ and Posix headers
195 return llvm::StringSwitch<bool>(LowerInclude)
197 .Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true)
198 .Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true)
199 .Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true)
200 .Cases("stdatomic.h", "stdbool.h", "stddef.h", "stdint.h", "stdio.h", true)
201 .Cases("stdlib.h", "stdnoreturn.h", "string.h", "tgmath.h", "threads.h", true)
202 .Cases("time.h", "uchar.h", "wchar.h", "wctype.h", true)
204 // C++ headers for C library facilities
205 .Cases("cassert", "ccomplex", "cctype", "cerrno", "cfenv", true)
206 .Cases("cfloat", "cinttypes", "ciso646", "climits", "clocale", true)
207 .Cases("cmath", "csetjmp", "csignal", "cstdalign", "cstdarg", true)
208 .Cases("cstdbool", "cstddef", "cstdint", "cstdio", "cstdlib", true)
209 .Cases("cstring", "ctgmath", "ctime", "cuchar", "cwchar", true)
210 .Case("cwctype", true)
212 // C++ library headers
213 .Cases("algorithm", "fstream", "list", "regex", "thread", true)
214 .Cases("array", "functional", "locale", "scoped_allocator", "tuple", true)
215 .Cases("atomic", "future", "map", "set", "type_traits", true)
216 .Cases("bitset", "initializer_list", "memory", "shared_mutex", "typeindex", true)
217 .Cases("chrono", "iomanip", "mutex", "sstream", "typeinfo", true)
218 .Cases("codecvt", "ios", "new", "stack", "unordered_map", true)
219 .Cases("complex", "iosfwd", "numeric", "stdexcept", "unordered_set", true)
220 .Cases("condition_variable", "iostream", "ostream", "streambuf", "utility", true)
221 .Cases("deque", "istream", "queue", "string", "valarray", true)
222 .Cases("exception", "iterator", "random", "strstream", "vector", true)
223 .Cases("forward_list", "limits", "ratio", "system_error", true)
225 // POSIX headers (which aren't also C headers)
226 .Cases("aio.h", "arpa/inet.h", "cpio.h", "dirent.h", "dlfcn.h", true)
227 .Cases("fcntl.h", "fmtmsg.h", "fnmatch.h", "ftw.h", "glob.h", true)
228 .Cases("grp.h", "iconv.h", "langinfo.h", "libgen.h", "monetary.h", true)
229 .Cases("mqueue.h", "ndbm.h", "net/if.h", "netdb.h", "netinet/in.h", true)
230 .Cases("netinet/tcp.h", "nl_types.h", "poll.h", "pthread.h", "pwd.h", true)
231 .Cases("regex.h", "sched.h", "search.h", "semaphore.h", "spawn.h", true)
232 .Cases("strings.h", "stropts.h", "sys/ipc.h", "sys/mman.h", "sys/msg.h", true)
233 .Cases("sys/resource.h", "sys/select.h", "sys/sem.h", "sys/shm.h", "sys/socket.h", true)
234 .Cases("sys/stat.h", "sys/statvfs.h", "sys/time.h", "sys/times.h", "sys/types.h", true)
235 .Cases("sys/uio.h", "sys/un.h", "sys/utsname.h", "sys/wait.h", "syslog.h", true)
236 .Cases("tar.h", "termios.h", "trace.h", "ulimit.h", true)
237 .Cases("unistd.h", "utime.h", "utmpx.h", "wordexp.h", true)
241 bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
243 // Missing macro name?
244 if (MacroNameTok.is(tok::eod))
245 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
247 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
249 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
251 if (II->isCPlusPlusOperatorKeyword()) {
252 // C++ 2.5p2: Alternative tokens behave the same as its primary token
253 // except for their spellings.
254 Diag(MacroNameTok, getLangOpts().MicrosoftExt
255 ? diag::ext_pp_operator_used_as_macro_name
256 : diag::err_pp_operator_used_as_macro_name)
257 << II << MacroNameTok.getKind();
258 // Allow #defining |and| and friends for Microsoft compatibility or
259 // recovery when legacy C headers are included in C++.
262 if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
263 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
264 return Diag(MacroNameTok, diag::err_defined_macro_name);
267 if (isDefineUndef == MU_Undef) {
268 auto *MI = getMacroInfo(II);
269 if (MI && MI->isBuiltinMacro()) {
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 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
276 // If defining/undefining reserved identifier or a keyword, we need to issue
278 SourceLocation MacroNameLoc = MacroNameTok.getLocation();
281 if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
282 (SourceMgr.getBufferName(MacroNameLoc) != "<built-in>")) {
283 MacroDiag D = MD_NoWarn;
284 if (isDefineUndef == MU_Define) {
285 D = shouldWarnOnMacroDef(*this, II);
287 else if (isDefineUndef == MU_Undef)
288 D = shouldWarnOnMacroUndef(*this, II);
289 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.
296 if (D == MD_ReservedMacro)
297 Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
300 // Okay, we got a good identifier.
304 /// Lex and validate a macro name, which occurs after a
305 /// \#define or \#undef.
307 /// This sets the token kind to eod and discards the rest of the macro line if
308 /// the macro name is invalid.
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,
315 // Read the token, don't allow macro expansion on it.
316 LexUnexpandedToken(MacroNameTok);
318 if (MacroNameTok.is(tok::code_completion)) {
320 CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
321 setCodeCompletionReached();
322 LexUnexpandedToken(MacroNameTok);
325 if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
328 // Invalid macro name, read and discard the rest of the line and set the
329 // token kind to tok::eod if necessary.
330 if (MacroNameTok.isNot(tok::eod)) {
331 MacroNameTok.setKind(tok::eod);
332 DiscardUntilEndOfDirective();
336 /// Ensure that the next token is a tok::eod token.
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.
341 /// Returns the location of the end of the directive.
342 SourceLocation Preprocessor::CheckEndOfDirective(const char *DirType,
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.
351 LexUnexpandedToken(Tmp);
353 // There should be no tokens after the directive, but we allow them as an
355 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
356 LexUnexpandedToken(Tmp);
358 if (Tmp.is(tok::eod))
359 return Tmp.getLocation();
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.
366 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
368 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
369 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
370 return DiscardUntilEndOfDirective().getEnd();
373 Optional<unsigned> Preprocessor::getSkippedRangeForExcludedConditionalBlock(
374 SourceLocation HashLoc) {
375 if (!ExcludedConditionalDirectiveSkipMappings)
377 if (!HashLoc.isFileID())
380 std::pair<FileID, unsigned> HashFileOffset =
381 SourceMgr.getDecomposedLoc(HashLoc);
382 const llvm::MemoryBuffer *Buf = SourceMgr.getBuffer(HashFileOffset.first);
383 auto It = ExcludedConditionalDirectiveSkipMappings->find(Buf);
384 if (It == ExcludedConditionalDirectiveSkipMappings->end())
387 const PreprocessorSkippedRangeMapping &SkippedRanges = *It->getSecond();
388 // Check if the offset of '#' is mapped in the skipped ranges.
389 auto MappingIt = SkippedRanges.find(HashFileOffset.second);
390 if (MappingIt == SkippedRanges.end())
393 unsigned BytesToSkip = MappingIt->getSecond();
394 unsigned CurLexerBufferOffset = CurLexer->getCurrentBufferOffset();
395 assert(CurLexerBufferOffset >= HashFileOffset.second &&
396 "lexer is before the hash?");
397 // Take into account the fact that the lexer has already advanced, so the
398 // number of bytes to skip must be adjusted.
399 unsigned LengthDiff = CurLexerBufferOffset - HashFileOffset.second;
400 assert(BytesToSkip >= LengthDiff && "lexer is after the skipped range?");
401 return BytesToSkip - LengthDiff;
404 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
405 /// decided that the subsequent tokens are in the \#if'd out portion of the
406 /// file. Lex the rest of the file, until we see an \#endif. If
407 /// FoundNonSkipPortion is true, then we have already emitted code for part of
408 /// this \#if directive, so \#else/\#elif blocks should never be entered.
409 /// If ElseOk is true, then \#else directives are ok, if not, then we have
410 /// already seen one so a \#else directive is a duplicate. When this returns,
411 /// the caller can lex the first valid token.
412 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation HashTokenLoc,
413 SourceLocation IfTokenLoc,
414 bool FoundNonSkipPortion,
416 SourceLocation ElseLoc) {
418 assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
420 if (PreambleConditionalStack.reachedEOFWhileSkipping())
421 PreambleConditionalStack.clearSkipInfo();
423 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/ false,
424 FoundNonSkipPortion, FoundElse);
426 // Enter raw mode to disable identifier lookup (and thus macro expansion),
427 // disabling warnings, etc.
428 CurPPLexer->LexingRawMode = true;
430 if (auto SkipLength =
431 getSkippedRangeForExcludedConditionalBlock(HashTokenLoc)) {
432 // Skip to the next '#endif' / '#else' / '#elif'.
433 CurLexer->skipOver(*SkipLength);
438 if (Tok.is(tok::code_completion)) {
440 CodeComplete->CodeCompleteInConditionalExclusion();
441 setCodeCompletionReached();
445 // If this is the end of the buffer, we have an error.
446 if (Tok.is(tok::eof)) {
447 // We don't emit errors for unterminated conditionals here,
448 // Lexer::LexEndOfFile can do that properly.
449 // Just return and let the caller lex after this #include.
450 if (PreambleConditionalStack.isRecording())
451 PreambleConditionalStack.SkipInfo.emplace(
452 HashTokenLoc, IfTokenLoc, FoundNonSkipPortion, FoundElse, ElseLoc);
456 // If this token is not a preprocessor directive, just skip it.
457 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
460 // We just parsed a # character at the start of a line, so we're in
461 // directive mode. Tell the lexer this so any newlines we see will be
462 // converted into an EOD token (this terminates the macro).
463 CurPPLexer->ParsingPreprocessorDirective = true;
464 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
467 // Read the next token, the directive flavor.
468 LexUnexpandedToken(Tok);
470 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
471 // something bogus), skip it.
472 if (Tok.isNot(tok::raw_identifier)) {
473 CurPPLexer->ParsingPreprocessorDirective = false;
474 // Restore comment saving mode.
475 if (CurLexer) CurLexer->resetExtendedTokenMode();
479 // If the first letter isn't i or e, it isn't intesting to us. We know that
480 // this is safe in the face of spelling differences, because there is no way
481 // to spell an i/e in a strange way that is another letter. Skipping this
482 // allows us to avoid looking up the identifier info for #define/#undef and
483 // other common directives.
484 StringRef RI = Tok.getRawIdentifier();
486 char FirstChar = RI[0];
487 if (FirstChar >= 'a' && FirstChar <= 'z' &&
488 FirstChar != 'i' && FirstChar != 'e') {
489 CurPPLexer->ParsingPreprocessorDirective = false;
490 // Restore comment saving mode.
491 if (CurLexer) CurLexer->resetExtendedTokenMode();
495 // Get the identifier name without trigraphs or embedded newlines. Note
496 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
498 char DirectiveBuf[20];
500 if (!Tok.needsCleaning() && RI.size() < 20) {
503 std::string DirectiveStr = getSpelling(Tok);
504 size_t IdLen = DirectiveStr.size();
506 CurPPLexer->ParsingPreprocessorDirective = false;
507 // Restore comment saving mode.
508 if (CurLexer) CurLexer->resetExtendedTokenMode();
511 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
512 Directive = StringRef(DirectiveBuf, IdLen);
515 if (Directive.startswith("if")) {
516 StringRef Sub = Directive.substr(2);
517 if (Sub.empty() || // "if"
518 Sub == "def" || // "ifdef"
519 Sub == "ndef") { // "ifndef"
520 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
521 // bother parsing the condition.
522 DiscardUntilEndOfDirective();
523 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
524 /*foundnonskip*/false,
527 } else if (Directive[0] == 'e') {
528 StringRef Sub = Directive.substr(1);
529 if (Sub == "ndif") { // "endif"
530 PPConditionalInfo CondInfo;
531 CondInfo.WasSkipping = true; // Silence bogus warning.
532 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
533 (void)InCond; // Silence warning in no-asserts mode.
534 assert(!InCond && "Can't be skipping if not in a conditional!");
536 // If we popped the outermost skipping block, we're done skipping!
537 if (!CondInfo.WasSkipping) {
538 // Restore the value of LexingRawMode so that trailing comments
539 // are handled correctly, if we've reached the outermost block.
540 CurPPLexer->LexingRawMode = false;
541 CheckEndOfDirective("endif");
542 CurPPLexer->LexingRawMode = true;
544 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
547 DiscardUntilEndOfDirective();
549 } else if (Sub == "lse") { // "else".
550 // #else directive in a skipping conditional. If not in some other
551 // skipping conditional, and if #else hasn't already been seen, enter it
552 // as a non-skipping conditional.
553 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
555 // If this is a #else with a #else before it, report the error.
556 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
558 // Note that we've seen a #else in this conditional.
559 CondInfo.FoundElse = true;
561 // If the conditional is at the top level, and the #if block wasn't
562 // entered, enter the #else block now.
563 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
564 CondInfo.FoundNonSkip = true;
565 // Restore the value of LexingRawMode so that trailing comments
566 // are handled correctly.
567 CurPPLexer->LexingRawMode = false;
568 CheckEndOfDirective("else");
569 CurPPLexer->LexingRawMode = true;
571 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
574 DiscardUntilEndOfDirective(); // C99 6.10p4.
576 } else if (Sub == "lif") { // "elif".
577 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
579 // If this is a #elif with a #else before it, report the error.
580 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
582 // If this is in a skipping block or if we're already handled this #if
583 // block, don't bother parsing the condition.
584 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
585 DiscardUntilEndOfDirective();
587 // Restore the value of LexingRawMode so that identifiers are
588 // looked up, etc, inside the #elif expression.
589 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
590 CurPPLexer->LexingRawMode = false;
591 IdentifierInfo *IfNDefMacro = nullptr;
592 DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
593 const bool CondValue = DER.Conditional;
594 CurPPLexer->LexingRawMode = true;
597 Tok.getLocation(), DER.ExprRange,
598 (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False),
601 // If this condition is true, enter it!
603 CondInfo.FoundNonSkip = true;
610 CurPPLexer->ParsingPreprocessorDirective = false;
611 // Restore comment saving mode.
612 if (CurLexer) CurLexer->resetExtendedTokenMode();
615 // Finally, if we are out of the conditional (saw an #endif or ran off the end
616 // of the file, just stop skipping and return to lexing whatever came after
618 CurPPLexer->LexingRawMode = false;
620 // The last skipped range isn't actually skipped yet if it's truncated
621 // by the end of the preamble; we'll resume parsing after the preamble.
622 if (Callbacks && (Tok.isNot(tok::eof) || !isRecordingPreamble()))
623 Callbacks->SourceRangeSkipped(
624 SourceRange(HashTokenLoc, CurPPLexer->getSourceLocation()),
628 Module *Preprocessor::getModuleForLocation(SourceLocation Loc) {
629 if (!SourceMgr.isInMainFile(Loc)) {
630 // Try to determine the module of the include directive.
631 // FIXME: Look into directly passing the FileEntry from LookupFile instead.
632 FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
633 if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
634 // The include comes from an included file.
635 return HeaderInfo.getModuleMap()
636 .findModuleForHeader(EntryOfIncl)
641 // This is either in the main file or not in a file at all. It belongs
642 // to the current module, if there is one.
643 return getLangOpts().CurrentModule.empty()
645 : HeaderInfo.lookupModule(getLangOpts().CurrentModule);
649 Preprocessor::getModuleHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
651 SourceLocation Loc) {
652 assert(M && "no module to include");
654 // If the context is the global module fragment of some module, we never
655 // want to return that file; instead, we want the innermost include-guarded
656 // header that it included.
657 bool InGlobalModuleFragment = M->Kind == Module::GlobalModuleFragment;
659 // If we have a module import syntax, we shouldn't include a header to
660 // make a particular module visible.
661 if ((getLangOpts().ObjC || getLangOpts().CPlusPlusModules ||
662 getLangOpts().ModulesTS) &&
663 !InGlobalModuleFragment)
666 Module *TopM = M->getTopLevelModule();
667 Module *IncM = getModuleForLocation(IncLoc);
669 // Walk up through the include stack, looking through textual headers of M
670 // until we hit a non-textual header that we can #include. (We assume textual
671 // headers of a module with non-textual headers aren't meant to be used to
672 // import entities from the module.)
673 auto &SM = getSourceManager();
674 while (!Loc.isInvalid() && !SM.isInMainFile(Loc)) {
675 auto ID = SM.getFileID(SM.getExpansionLoc(Loc));
676 auto *FE = SM.getFileEntryForID(ID);
680 if (InGlobalModuleFragment) {
681 if (getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE))
683 Loc = SM.getIncludeLoc(ID);
687 bool InTextualHeader = false;
688 for (auto Header : HeaderInfo.getModuleMap().findAllModulesForHeader(FE)) {
689 if (!Header.getModule()->isSubModuleOf(TopM))
692 if (!(Header.getRole() & ModuleMap::TextualHeader)) {
693 // If this is an accessible, non-textual header of M's top-level module
694 // that transitively includes the given location and makes the
695 // corresponding module visible, this is the thing to #include.
696 if (Header.isAccessibleFrom(IncM))
699 // It's in a private header; we can't #include it.
700 // FIXME: If there's a public header in some module that re-exports it,
701 // then we could suggest including that, but it's not clear that's the
702 // expected way to make this entity visible.
706 InTextualHeader = true;
709 if (!InTextualHeader)
712 Loc = SM.getIncludeLoc(ID);
718 Optional<FileEntryRef> Preprocessor::LookupFile(
719 SourceLocation FilenameLoc, StringRef Filename, bool isAngled,
720 const DirectoryLookup *FromDir, const FileEntry *FromFile,
721 const DirectoryLookup *&CurDir, SmallVectorImpl<char> *SearchPath,
722 SmallVectorImpl<char> *RelativePath,
723 ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped,
724 bool *IsFrameworkFound, bool SkipCache) {
725 Module *RequestingModule = getModuleForLocation(FilenameLoc);
726 bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc);
728 // If the header lookup mechanism may be relative to the current inclusion
729 // stack, record the parent #includes.
730 SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 16>
732 bool BuildSystemModule = false;
733 if (!FromDir && !FromFile) {
734 FileID FID = getCurrentFileLexer()->getFileID();
735 const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
737 // If there is no file entry associated with this file, it must be the
738 // predefines buffer or the module includes buffer. Any other file is not
739 // lexed with a normal lexer, so it won't be scanned for preprocessor
742 // If we have the predefines buffer, resolve #include references (which come
743 // from the -include command line argument) from the current working
744 // directory instead of relative to the main file.
746 // If we have the module includes buffer, resolve #include references (which
747 // come from header declarations in the module map) relative to the module
750 if (FID == SourceMgr.getMainFileID() && MainFileDir) {
751 Includers.push_back(std::make_pair(nullptr, MainFileDir));
752 BuildSystemModule = getCurrentModule()->IsSystem;
753 } else if ((FileEnt =
754 SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())))
755 Includers.push_back(std::make_pair(FileEnt, *FileMgr.getDirectory(".")));
757 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
760 // MSVC searches the current include stack from top to bottom for
761 // headers included by quoted include directives.
762 // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
763 if (LangOpts.MSVCCompat && !isAngled) {
764 for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
765 if (IsFileLexer(ISEntry))
766 if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
767 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
772 CurDir = CurDirLookup;
775 // We're supposed to start looking from after a particular file. Search
776 // the include path until we find that file or run out of files.
777 const DirectoryLookup *TmpCurDir = CurDir;
778 const DirectoryLookup *TmpFromDir = nullptr;
779 while (Optional<FileEntryRef> FE = HeaderInfo.LookupFile(
780 Filename, FilenameLoc, isAngled, TmpFromDir, TmpCurDir,
781 Includers, SearchPath, RelativePath, RequestingModule,
782 SuggestedModule, /*IsMapped=*/nullptr,
783 /*IsFrameworkFound=*/nullptr, SkipCache)) {
784 // Keep looking as if this file did a #include_next.
785 TmpFromDir = TmpCurDir;
787 if (&FE->getFileEntry() == FromFile) {
789 FromDir = TmpFromDir;
796 // Do a standard file entry lookup.
797 Optional<FileEntryRef> FE = HeaderInfo.LookupFile(
798 Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
799 RelativePath, RequestingModule, SuggestedModule, IsMapped,
800 IsFrameworkFound, SkipCache, BuildSystemModule);
802 if (SuggestedModule && !LangOpts.AsmPreprocessor)
803 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
804 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
805 Filename, &FE->getFileEntry());
809 const FileEntry *CurFileEnt;
810 // Otherwise, see if this is a subframework header. If so, this is relative
811 // to one of the headers on the #include stack. Walk the list of the current
812 // headers on the #include stack and pass them to HeaderInfo.
814 if ((CurFileEnt = CurPPLexer->getFileEntry())) {
815 if (Optional<FileEntryRef> FE = HeaderInfo.LookupSubframeworkHeader(
816 Filename, CurFileEnt, SearchPath, RelativePath, RequestingModule,
818 if (SuggestedModule && !LangOpts.AsmPreprocessor)
819 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
820 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
821 Filename, &FE->getFileEntry());
827 for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
828 if (IsFileLexer(ISEntry)) {
829 if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
830 if (Optional<FileEntryRef> FE = HeaderInfo.LookupSubframeworkHeader(
831 Filename, CurFileEnt, SearchPath, RelativePath,
832 RequestingModule, SuggestedModule)) {
833 if (SuggestedModule && !LangOpts.AsmPreprocessor)
834 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
835 RequestingModule, RequestingModuleIsModuleInterface,
836 FilenameLoc, Filename, &FE->getFileEntry());
843 // Otherwise, we really couldn't find the file.
847 //===----------------------------------------------------------------------===//
848 // Preprocessor Directive Handling.
849 //===----------------------------------------------------------------------===//
851 class Preprocessor::ResetMacroExpansionHelper {
853 ResetMacroExpansionHelper(Preprocessor *pp)
854 : PP(pp), save(pp->DisableMacroExpansion) {
855 if (pp->MacroExpansionInDirectivesOverride)
856 pp->DisableMacroExpansion = false;
859 ~ResetMacroExpansionHelper() {
860 PP->DisableMacroExpansion = save;
868 /// Process a directive while looking for the through header or a #pragma
869 /// hdrstop. The following directives are handled:
870 /// #include (to check if it is the through header)
871 /// #define (to warn about macros that don't match the PCH)
872 /// #pragma (to check for pragma hdrstop).
873 /// All other directives are completely discarded.
874 void Preprocessor::HandleSkippedDirectiveWhileUsingPCH(Token &Result,
875 SourceLocation HashLoc) {
876 if (const IdentifierInfo *II = Result.getIdentifierInfo()) {
877 if (II->getPPKeywordID() == tok::pp_define) {
878 return HandleDefineDirective(Result,
879 /*ImmediatelyAfterHeaderGuard=*/false);
881 if (SkippingUntilPCHThroughHeader &&
882 II->getPPKeywordID() == tok::pp_include) {
883 return HandleIncludeDirective(HashLoc, Result);
885 if (SkippingUntilPragmaHdrStop && II->getPPKeywordID() == tok::pp_pragma) {
887 auto *II = Result.getIdentifierInfo();
888 if (II && II->getName() == "hdrstop")
889 return HandlePragmaHdrstop(Result);
892 DiscardUntilEndOfDirective();
895 /// HandleDirective - This callback is invoked when the lexer sees a # token
896 /// at the start of a line. This consumes the directive, modifies the
897 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
898 /// read is the correct one.
899 void Preprocessor::HandleDirective(Token &Result) {
900 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
902 // We just parsed a # character at the start of a line, so we're in directive
903 // mode. Tell the lexer this so any newlines we see will be converted into an
904 // EOD token (which terminates the directive).
905 CurPPLexer->ParsingPreprocessorDirective = true;
906 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
908 bool ImmediatelyAfterTopLevelIfndef =
909 CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
910 CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
914 // We are about to read a token. For the multiple-include optimization FA to
915 // work, we have to remember if we had read any tokens *before* this
917 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
919 // Save the '#' token in case we need to return it later.
920 Token SavedHash = Result;
922 // Read the next token, the directive flavor. This isn't expanded due to
924 LexUnexpandedToken(Result);
926 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
931 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
932 // not support this for #include-like directives, since that can result in
933 // terrible diagnostics, and does not work in GCC.
935 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
936 switch (II->getPPKeywordID()) {
937 case tok::pp_include:
939 case tok::pp_include_next:
940 case tok::pp___include_macros:
942 Diag(Result, diag::err_embedded_directive) << II->getName();
943 Diag(*ArgMacro, diag::note_macro_expansion_here)
944 << ArgMacro->getIdentifierInfo();
945 DiscardUntilEndOfDirective();
951 Diag(Result, diag::ext_embedded_directive);
954 // Temporarily enable macro expansion if set so
955 // and reset to previous state when returning from this function.
956 ResetMacroExpansionHelper helper(this);
958 if (SkippingUntilPCHThroughHeader || SkippingUntilPragmaHdrStop)
959 return HandleSkippedDirectiveWhileUsingPCH(Result, SavedHash.getLocation());
961 switch (Result.getKind()) {
963 return; // null directive.
964 case tok::code_completion:
966 CodeComplete->CodeCompleteDirective(
967 CurPPLexer->getConditionalStackDepth() > 0);
968 setCodeCompletionReached();
970 case tok::numeric_constant: // # 7 GNU line marker directive.
971 if (getLangOpts().AsmPreprocessor)
972 break; // # 4 is not a preprocessor directive in .S files.
973 return HandleDigitDirective(Result);
975 IdentifierInfo *II = Result.getIdentifierInfo();
976 if (!II) break; // Not an identifier.
978 // Ask what the preprocessor keyword ID is.
979 switch (II->getPPKeywordID()) {
981 // C99 6.10.1 - Conditional Inclusion.
983 return HandleIfDirective(Result, SavedHash, ReadAnyTokensBeforeDirective);
985 return HandleIfdefDirective(Result, SavedHash, false,
986 true /*not valid for miopt*/);
988 return HandleIfdefDirective(Result, SavedHash, true,
989 ReadAnyTokensBeforeDirective);
991 return HandleElifDirective(Result, SavedHash);
993 return HandleElseDirective(Result, SavedHash);
995 return HandleEndifDirective(Result);
997 // C99 6.10.2 - Source File Inclusion.
998 case tok::pp_include:
1000 return HandleIncludeDirective(SavedHash.getLocation(), Result);
1001 case tok::pp___include_macros:
1003 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
1005 // C99 6.10.3 - Macro Replacement.
1006 case tok::pp_define:
1007 return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
1009 return HandleUndefDirective();
1011 // C99 6.10.4 - Line Control.
1013 return HandleLineDirective();
1015 // C99 6.10.5 - Error Directive.
1017 return HandleUserDiagnosticDirective(Result, false);
1019 // C99 6.10.6 - Pragma Directive.
1020 case tok::pp_pragma:
1021 return HandlePragmaDirective({PIK_HashPragma, SavedHash.getLocation()});
1024 case tok::pp_import:
1025 return HandleImportDirective(SavedHash.getLocation(), Result);
1026 case tok::pp_include_next:
1027 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
1029 case tok::pp_warning:
1030 Diag(Result, diag::ext_pp_warning_directive);
1031 return HandleUserDiagnosticDirective(Result, true);
1033 return HandleIdentSCCSDirective(Result);
1035 return HandleIdentSCCSDirective(Result);
1036 case tok::pp_assert:
1037 //isExtension = true; // FIXME: implement #assert
1039 case tok::pp_unassert:
1040 //isExtension = true; // FIXME: implement #unassert
1043 case tok::pp___public_macro:
1044 if (getLangOpts().Modules)
1045 return HandleMacroPublicDirective(Result);
1048 case tok::pp___private_macro:
1049 if (getLangOpts().Modules)
1050 return HandleMacroPrivateDirective();
1056 // If this is a .S file, treat unknown # directives as non-preprocessor
1057 // directives. This is important because # may be a comment or introduce
1058 // various pseudo-ops. Just return the # token and push back the following
1059 // token to be lexed next time.
1060 if (getLangOpts().AsmPreprocessor) {
1061 auto Toks = std::make_unique<Token[]>(2);
1062 // Return the # and the token after it.
1063 Toks[0] = SavedHash;
1066 // If the second token is a hashhash token, then we need to translate it to
1067 // unknown so the token lexer doesn't try to perform token pasting.
1068 if (Result.is(tok::hashhash))
1069 Toks[1].setKind(tok::unknown);
1071 // Enter this token stream so that we re-lex the tokens. Make sure to
1072 // enable macro expansion, in case the token after the # is an identifier
1073 // that is expanded.
1074 EnterTokenStream(std::move(Toks), 2, false, /*IsReinject*/false);
1078 // If we reached here, the preprocessing token is not valid!
1079 Diag(Result, diag::err_pp_invalid_directive);
1081 // Read the rest of the PP line.
1082 DiscardUntilEndOfDirective();
1084 // Okay, we're done parsing the directive.
1087 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
1088 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
1089 static bool GetLineValue(Token &DigitTok, unsigned &Val,
1090 unsigned DiagID, Preprocessor &PP,
1091 bool IsGNULineDirective=false) {
1092 if (DigitTok.isNot(tok::numeric_constant)) {
1093 PP.Diag(DigitTok, DiagID);
1095 if (DigitTok.isNot(tok::eod))
1096 PP.DiscardUntilEndOfDirective();
1100 SmallString<64> IntegerBuffer;
1101 IntegerBuffer.resize(DigitTok.getLength());
1102 const char *DigitTokBegin = &IntegerBuffer[0];
1103 bool Invalid = false;
1104 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
1108 // Verify that we have a simple digit-sequence, and compute the value. This
1109 // is always a simple digit string computed in decimal, so we do this manually
1112 for (unsigned i = 0; i != ActualLength; ++i) {
1113 // C++1y [lex.fcon]p1:
1114 // Optional separating single quotes in a digit-sequence are ignored
1115 if (DigitTokBegin[i] == '\'')
1118 if (!isDigit(DigitTokBegin[i])) {
1119 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
1120 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
1121 PP.DiscardUntilEndOfDirective();
1125 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
1126 if (NextVal < Val) { // overflow.
1127 PP.Diag(DigitTok, DiagID);
1128 PP.DiscardUntilEndOfDirective();
1134 if (DigitTokBegin[0] == '0' && Val)
1135 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
1136 << IsGNULineDirective;
1141 /// Handle a \#line directive: C99 6.10.4.
1143 /// The two acceptable forms are:
1145 /// # line digit-sequence
1146 /// # line digit-sequence "s-char-sequence"
1148 void Preprocessor::HandleLineDirective() {
1149 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
1154 // Validate the number and convert it to an unsigned.
1156 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
1160 Diag(DigitTok, diag::ext_pp_line_zero);
1162 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1163 // number greater than 2147483647". C90 requires that the line # be <= 32767.
1164 unsigned LineLimit = 32768U;
1165 if (LangOpts.C99 || LangOpts.CPlusPlus11)
1166 LineLimit = 2147483648U;
1167 if (LineNo >= LineLimit)
1168 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
1169 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
1170 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
1172 int FilenameID = -1;
1176 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1177 // string followed by eod.
1178 if (StrTok.is(tok::eod))
1180 else if (StrTok.isNot(tok::string_literal)) {
1181 Diag(StrTok, diag::err_pp_line_invalid_filename);
1182 DiscardUntilEndOfDirective();
1184 } else if (StrTok.hasUDSuffix()) {
1185 Diag(StrTok, diag::err_invalid_string_udl);
1186 DiscardUntilEndOfDirective();
1189 // Parse and validate the string, converting it into a unique ID.
1190 StringLiteralParser Literal(StrTok, *this);
1191 assert(Literal.isAscii() && "Didn't allow wide strings in");
1192 if (Literal.hadError) {
1193 DiscardUntilEndOfDirective();
1196 if (Literal.Pascal) {
1197 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1198 DiscardUntilEndOfDirective();
1201 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1203 // Verify that there is nothing after the string, other than EOD. Because
1204 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1205 CheckEndOfDirective("line", true);
1208 // Take the file kind of the file containing the #line directive. #line
1209 // directives are often used for generated sources from the same codebase, so
1210 // the new file should generally be classified the same way as the current
1211 // file. This is visible in GCC's pre-processed output, which rewrites #line
1212 // to GNU line markers.
1213 SrcMgr::CharacteristicKind FileKind =
1214 SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1216 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, false,
1220 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1221 PPCallbacks::RenameFile, FileKind);
1224 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1225 /// marker directive.
1226 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1227 SrcMgr::CharacteristicKind &FileKind,
1232 if (FlagTok.is(tok::eod)) return false;
1233 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1240 if (FlagTok.is(tok::eod)) return false;
1241 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1243 } else if (FlagVal == 2) {
1246 SourceManager &SM = PP.getSourceManager();
1247 // If we are leaving the current presumed file, check to make sure the
1248 // presumed include stack isn't empty!
1250 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1251 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1252 if (PLoc.isInvalid())
1255 // If there is no include loc (main file) or if the include loc is in a
1256 // different physical file, then we aren't in a "1" line marker flag region.
1257 SourceLocation IncLoc = PLoc.getIncludeLoc();
1258 if (IncLoc.isInvalid() ||
1259 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
1260 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1261 PP.DiscardUntilEndOfDirective();
1266 if (FlagTok.is(tok::eod)) return false;
1267 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1271 // We must have 3 if there are still flags.
1273 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1274 PP.DiscardUntilEndOfDirective();
1278 FileKind = SrcMgr::C_System;
1281 if (FlagTok.is(tok::eod)) return false;
1282 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1285 // We must have 4 if there is yet another flag.
1287 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1288 PP.DiscardUntilEndOfDirective();
1292 FileKind = SrcMgr::C_ExternCSystem;
1295 if (FlagTok.is(tok::eod)) return false;
1297 // There are no more valid flags here.
1298 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1299 PP.DiscardUntilEndOfDirective();
1303 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1304 /// one of the following forms:
1307 /// # 42 "file" ('1' | '2')?
1308 /// # 42 "file" ('1' | '2')? '3' '4'?
1310 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1311 // Validate the number and convert it to an unsigned. GNU does not have a
1312 // line # limit other than it fit in 32-bits.
1314 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1321 bool IsFileEntry = false, IsFileExit = false;
1322 int FilenameID = -1;
1323 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1325 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1326 // string followed by eod.
1327 if (StrTok.is(tok::eod)) {
1328 // Treat this like "#line NN", which doesn't change file characteristics.
1329 FileKind = SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1330 } else if (StrTok.isNot(tok::string_literal)) {
1331 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1332 DiscardUntilEndOfDirective();
1334 } else if (StrTok.hasUDSuffix()) {
1335 Diag(StrTok, diag::err_invalid_string_udl);
1336 DiscardUntilEndOfDirective();
1339 // Parse and validate the string, converting it into a unique ID.
1340 StringLiteralParser Literal(StrTok, *this);
1341 assert(Literal.isAscii() && "Didn't allow wide strings in");
1342 if (Literal.hadError) {
1343 DiscardUntilEndOfDirective();
1346 if (Literal.Pascal) {
1347 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1348 DiscardUntilEndOfDirective();
1351 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1353 // If a filename was present, read any flags that are present.
1354 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, FileKind, *this))
1358 // Create a line note with this information.
1359 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, IsFileEntry,
1360 IsFileExit, FileKind);
1362 // If the preprocessor has callbacks installed, notify them of the #line
1363 // change. This is used so that the line marker comes out in -E mode for
1366 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1368 Reason = PPCallbacks::EnterFile;
1369 else if (IsFileExit)
1370 Reason = PPCallbacks::ExitFile;
1372 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1376 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1378 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1380 // Read the rest of the line raw. We do this because we don't want macros
1381 // to be expanded and we don't require that the tokens be valid preprocessing
1382 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1383 // collapse multiple consecutive white space between tokens, but this isn't
1384 // specified by the standard.
1385 SmallString<128> Message;
1386 CurLexer->ReadToEndOfLine(&Message);
1388 // Find the first non-whitespace character, so that we can make the
1389 // diagnostic more succinct.
1390 StringRef Msg = StringRef(Message).ltrim(' ');
1393 Diag(Tok, diag::pp_hash_warning) << Msg;
1395 Diag(Tok, diag::err_pp_hash_error) << Msg;
1398 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1400 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1401 // Yes, this directive is an extension.
1402 Diag(Tok, diag::ext_pp_ident_directive);
1404 // Read the string argument.
1408 // If the token kind isn't a string, it's a malformed directive.
1409 if (StrTok.isNot(tok::string_literal) &&
1410 StrTok.isNot(tok::wide_string_literal)) {
1411 Diag(StrTok, diag::err_pp_malformed_ident);
1412 if (StrTok.isNot(tok::eod))
1413 DiscardUntilEndOfDirective();
1417 if (StrTok.hasUDSuffix()) {
1418 Diag(StrTok, diag::err_invalid_string_udl);
1419 DiscardUntilEndOfDirective();
1423 // Verify that there is nothing after the string, other than EOD.
1424 CheckEndOfDirective("ident");
1427 bool Invalid = false;
1428 std::string Str = getSpelling(StrTok, &Invalid);
1430 Callbacks->Ident(Tok.getLocation(), Str);
1434 /// Handle a #public directive.
1435 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1437 ReadMacroName(MacroNameTok, MU_Undef);
1439 // Error reading macro name? If so, diagnostic already issued.
1440 if (MacroNameTok.is(tok::eod))
1443 // Check to see if this is the last token on the #__public_macro line.
1444 CheckEndOfDirective("__public_macro");
1446 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1447 // Okay, we finally have a valid identifier to undef.
1448 MacroDirective *MD = getLocalMacroDirective(II);
1450 // If the macro is not defined, this is an error.
1452 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1456 // Note that this macro has now been exported.
1457 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1458 MacroNameTok.getLocation(), /*isPublic=*/true));
1461 /// Handle a #private directive.
1462 void Preprocessor::HandleMacroPrivateDirective() {
1464 ReadMacroName(MacroNameTok, MU_Undef);
1466 // Error reading macro name? If so, diagnostic already issued.
1467 if (MacroNameTok.is(tok::eod))
1470 // Check to see if this is the last token on the #__private_macro line.
1471 CheckEndOfDirective("__private_macro");
1473 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1474 // Okay, we finally have a valid identifier to undef.
1475 MacroDirective *MD = getLocalMacroDirective(II);
1477 // If the macro is not defined, this is an error.
1479 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1483 // Note that this macro has now been marked private.
1484 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1485 MacroNameTok.getLocation(), /*isPublic=*/false));
1488 //===----------------------------------------------------------------------===//
1489 // Preprocessor Include Directive Handling.
1490 //===----------------------------------------------------------------------===//
1492 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1493 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1494 /// true if the input filename was in <>'s or false if it were in ""'s. The
1495 /// caller is expected to provide a buffer that is large enough to hold the
1496 /// spelling of the filename, but is also expected to handle the case when
1497 /// this method decides to use a different buffer.
1498 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1499 StringRef &Buffer) {
1500 // Get the text form of the filename.
1501 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1503 // FIXME: Consider warning on some of the cases described in C11 6.4.7/3 and
1504 // C++20 [lex.header]/2:
1506 // If `"`, `'`, `\`, `/*`, or `//` appears in a header-name, then
1507 // in C: behavior is undefined
1508 // in C++: program is conditionally-supported with implementation-defined
1511 // Make sure the filename is <x> or "x".
1513 if (Buffer[0] == '<') {
1514 if (Buffer.back() != '>') {
1515 Diag(Loc, diag::err_pp_expects_filename);
1516 Buffer = StringRef();
1520 } else if (Buffer[0] == '"') {
1521 if (Buffer.back() != '"') {
1522 Diag(Loc, diag::err_pp_expects_filename);
1523 Buffer = StringRef();
1528 Diag(Loc, diag::err_pp_expects_filename);
1529 Buffer = StringRef();
1533 // Diagnose #include "" as invalid.
1534 if (Buffer.size() <= 2) {
1535 Diag(Loc, diag::err_pp_empty_filename);
1536 Buffer = StringRef();
1540 // Skip the brackets.
1541 Buffer = Buffer.substr(1, Buffer.size()-2);
1545 /// Push a token onto the token stream containing an annotation.
1546 void Preprocessor::EnterAnnotationToken(SourceRange Range,
1547 tok::TokenKind Kind,
1548 void *AnnotationVal) {
1549 // FIXME: Produce this as the current token directly, rather than
1550 // allocating a new token for it.
1551 auto Tok = std::make_unique<Token[]>(1);
1552 Tok[0].startToken();
1553 Tok[0].setKind(Kind);
1554 Tok[0].setLocation(Range.getBegin());
1555 Tok[0].setAnnotationEndLoc(Range.getEnd());
1556 Tok[0].setAnnotationValue(AnnotationVal);
1557 EnterTokenStream(std::move(Tok), 1, true, /*IsReinject*/ false);
1560 /// Produce a diagnostic informing the user that a #include or similar
1561 /// was implicitly treated as a module import.
1562 static void diagnoseAutoModuleImport(
1563 Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
1564 ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
1565 SourceLocation PathEnd) {
1566 StringRef ImportKeyword;
1567 if (PP.getLangOpts().ObjC)
1568 ImportKeyword = "@import";
1569 else if (PP.getLangOpts().ModulesTS || PP.getLangOpts().CPlusPlusModules)
1570 ImportKeyword = "import";
1572 return; // no import syntax available
1574 SmallString<128> PathString;
1575 for (size_t I = 0, N = Path.size(); I != N; ++I) {
1578 PathString += Path[I].first->getName();
1580 int IncludeKind = 0;
1582 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1583 case tok::pp_include:
1587 case tok::pp_import:
1591 case tok::pp_include_next:
1595 case tok::pp___include_macros:
1600 llvm_unreachable("unknown include directive kind");
1603 CharSourceRange ReplaceRange(SourceRange(HashLoc, PathEnd),
1604 /*IsTokenRange=*/false);
1605 PP.Diag(HashLoc, diag::warn_auto_module_import)
1606 << IncludeKind << PathString
1607 << FixItHint::CreateReplacement(
1608 ReplaceRange, (ImportKeyword + " " + PathString + ";").str());
1611 // Given a vector of path components and a string containing the real
1612 // path to the file, build a properly-cased replacement in the vector,
1613 // and return true if the replacement should be suggested.
1614 static bool trySimplifyPath(SmallVectorImpl<StringRef> &Components,
1615 StringRef RealPathName) {
1616 auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName);
1617 auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName);
1619 bool SuggestReplacement = false;
1620 // Below is a best-effort to handle ".." in paths. It is admittedly
1621 // not 100% correct in the presence of symlinks.
1622 for (auto &Component : llvm::reverse(Components)) {
1623 if ("." == Component) {
1624 } else if (".." == Component) {
1628 } else if (RealPathComponentIter != RealPathComponentEnd) {
1629 if (Component != *RealPathComponentIter) {
1630 // If these path components differ by more than just case, then we
1631 // may be looking at symlinked paths. Bail on this diagnostic to avoid
1632 // noisy false positives.
1633 SuggestReplacement = RealPathComponentIter->equals_lower(Component);
1634 if (!SuggestReplacement)
1636 Component = *RealPathComponentIter;
1638 ++RealPathComponentIter;
1641 return SuggestReplacement;
1644 bool Preprocessor::checkModuleIsAvailable(const LangOptions &LangOpts,
1645 const TargetInfo &TargetInfo,
1646 DiagnosticsEngine &Diags, Module *M) {
1647 Module::Requirement Requirement;
1648 Module::UnresolvedHeaderDirective MissingHeader;
1649 Module *ShadowingModule = nullptr;
1650 if (M->isAvailable(LangOpts, TargetInfo, Requirement, MissingHeader,
1654 if (MissingHeader.FileNameLoc.isValid()) {
1655 Diags.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
1656 << MissingHeader.IsUmbrella << MissingHeader.FileName;
1657 } else if (ShadowingModule) {
1658 Diags.Report(M->DefinitionLoc, diag::err_module_shadowed) << M->Name;
1659 Diags.Report(ShadowingModule->DefinitionLoc,
1660 diag::note_previous_definition);
1662 // FIXME: Track the location at which the requirement was specified, and
1664 Diags.Report(M->DefinitionLoc, diag::err_module_unavailable)
1665 << M->getFullModuleName() << Requirement.second << Requirement.first;
1670 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1671 /// the file to be included from the lexer, then include it! This is a common
1672 /// routine with functionality shared between \#include, \#include_next and
1673 /// \#import. LookupFrom is set when this is a \#include_next directive, it
1674 /// specifies the file to start searching from.
1675 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1677 const DirectoryLookup *LookupFrom,
1678 const FileEntry *LookupFromFile) {
1680 if (LexHeaderName(FilenameTok))
1683 if (FilenameTok.isNot(tok::header_name)) {
1684 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1685 if (FilenameTok.isNot(tok::eod))
1686 DiscardUntilEndOfDirective();
1690 // Verify that there is nothing after the filename, other than EOD. Note
1691 // that we allow macros that expand to nothing after the filename, because
1692 // this falls into the category of "#include pp-tokens new-line" specified
1694 SourceLocation EndLoc =
1695 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1697 auto Action = HandleHeaderIncludeOrImport(HashLoc, IncludeTok, FilenameTok,
1698 EndLoc, LookupFrom, LookupFromFile);
1699 switch (Action.Kind) {
1700 case ImportAction::None:
1701 case ImportAction::SkippedModuleImport:
1703 case ImportAction::ModuleBegin:
1704 EnterAnnotationToken(SourceRange(HashLoc, EndLoc),
1705 tok::annot_module_begin, Action.ModuleForHeader);
1707 case ImportAction::ModuleImport:
1708 EnterAnnotationToken(SourceRange(HashLoc, EndLoc),
1709 tok::annot_module_include, Action.ModuleForHeader);
1714 Optional<FileEntryRef> Preprocessor::LookupHeaderIncludeOrImport(
1715 const DirectoryLookup *&CurDir, StringRef Filename,
1716 SourceLocation FilenameLoc, CharSourceRange FilenameRange,
1717 const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl,
1718 bool &IsMapped, const DirectoryLookup *LookupFrom,
1719 const FileEntry *LookupFromFile, StringRef LookupFilename,
1720 SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath,
1721 ModuleMap::KnownHeader &SuggestedModule, bool isAngled) {
1722 Optional<FileEntryRef> File = LookupFile(
1723 FilenameLoc, LookupFilename,
1724 isAngled, LookupFrom, LookupFromFile, CurDir,
1725 Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
1726 &SuggestedModule, &IsMapped, &IsFrameworkFound);
1731 // Give the clients a chance to recover.
1732 SmallString<128> RecoveryPath;
1733 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1734 if (auto DE = FileMgr.getOptionalDirectoryRef(RecoveryPath)) {
1735 // Add the recovery path to the list of search paths.
1736 DirectoryLookup DL(*DE, SrcMgr::C_User, false);
1737 HeaderInfo.AddSearchPath(DL, isAngled);
1739 // Try the lookup again, skipping the cache.
1740 Optional<FileEntryRef> File = LookupFile(
1742 LookupFilename, isAngled,
1743 LookupFrom, LookupFromFile, CurDir, nullptr, nullptr,
1744 &SuggestedModule, &IsMapped, /*IsFrameworkFound=*/nullptr,
1745 /*SkipCache*/ true);
1752 if (SuppressIncludeNotFoundError)
1755 // If the file could not be located and it was included via angle
1756 // brackets, we can attempt a lookup as though it were a quoted path to
1757 // provide the user with a possible fixit.
1759 Optional<FileEntryRef> File = LookupFile(
1760 FilenameLoc, LookupFilename,
1761 false, LookupFrom, LookupFromFile, CurDir,
1762 Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
1763 &SuggestedModule, &IsMapped,
1764 /*IsFrameworkFound=*/nullptr);
1766 Diag(FilenameTok, diag::err_pp_file_not_found_angled_include_not_fatal)
1767 << Filename << IsImportDecl
1768 << FixItHint::CreateReplacement(FilenameRange,
1769 "\"" + Filename.str() + "\"");
1774 // Check for likely typos due to leading or trailing non-isAlphanumeric
1776 StringRef OriginalFilename = Filename;
1777 if (LangOpts.SpellChecking) {
1778 // A heuristic to correct a typo file name by removing leading and
1779 // trailing non-isAlphanumeric characters.
1780 auto CorrectTypoFilename = [](llvm::StringRef Filename) {
1781 Filename = Filename.drop_until(isAlphanumeric);
1782 while (!Filename.empty() && !isAlphanumeric(Filename.back())) {
1783 Filename = Filename.drop_back();
1787 StringRef TypoCorrectionName = CorrectTypoFilename(Filename);
1790 // Normalize slashes when compiling with -fms-extensions on non-Windows.
1791 // This is unnecessary on Windows since the filesystem there handles
1793 SmallString<128> NormalizedTypoCorrectionPath;
1794 if (LangOpts.MicrosoftExt) {
1795 NormalizedTypoCorrectionPath = TypoCorrectionName;
1796 llvm::sys::path::native(NormalizedTypoCorrectionPath);
1797 TypoCorrectionName = NormalizedTypoCorrectionPath;
1801 Optional<FileEntryRef> File = LookupFile(
1802 FilenameLoc, TypoCorrectionName, isAngled, LookupFrom, LookupFromFile,
1803 CurDir, Callbacks ? &SearchPath : nullptr,
1804 Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped,
1805 /*IsFrameworkFound=*/nullptr);
1808 isAngled ? FixItHint::CreateReplacement(
1809 FilenameRange, "<" + TypoCorrectionName.str() + ">")
1810 : FixItHint::CreateReplacement(
1811 FilenameRange, "\"" + TypoCorrectionName.str() + "\"");
1812 Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
1813 << OriginalFilename << TypoCorrectionName << Hint;
1814 // We found the file, so set the Filename to the name after typo
1816 Filename = TypoCorrectionName;
1821 // If the file is still not found, just go with the vanilla diagnostic
1822 assert(!File.hasValue() && "expected missing file");
1823 Diag(FilenameTok, diag::err_pp_file_not_found)
1824 << OriginalFilename << FilenameRange;
1825 if (IsFrameworkFound) {
1826 size_t SlashPos = OriginalFilename.find('/');
1827 assert(SlashPos != StringRef::npos &&
1828 "Include with framework name should have '/' in the filename");
1829 StringRef FrameworkName = OriginalFilename.substr(0, SlashPos);
1830 FrameworkCacheEntry &CacheEntry =
1831 HeaderInfo.LookupFrameworkCache(FrameworkName);
1832 assert(CacheEntry.Directory && "Found framework should be in cache");
1833 Diag(FilenameTok, diag::note_pp_framework_without_header)
1834 << OriginalFilename.substr(SlashPos + 1) << FrameworkName
1835 << CacheEntry.Directory->getName();
1841 /// Handle either a #include-like directive or an import declaration that names
1844 /// \param HashLoc The location of the '#' token for an include, or
1845 /// SourceLocation() for an import declaration.
1846 /// \param IncludeTok The include / include_next / import token.
1847 /// \param FilenameTok The header-name token.
1848 /// \param EndLoc The location at which any imported macros become visible.
1849 /// \param LookupFrom For #include_next, the starting directory for the
1850 /// directory lookup.
1851 /// \param LookupFromFile For #include_next, the starting file for the directory
1853 Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
1854 SourceLocation HashLoc, Token &IncludeTok, Token &FilenameTok,
1855 SourceLocation EndLoc, const DirectoryLookup *LookupFrom,
1856 const FileEntry *LookupFromFile) {
1857 SmallString<128> FilenameBuffer;
1858 StringRef Filename = getSpelling(FilenameTok, FilenameBuffer);
1859 SourceLocation CharEnd = FilenameTok.getEndLoc();
1861 CharSourceRange FilenameRange
1862 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1863 StringRef OriginalFilename = Filename;
1865 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1867 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1869 if (Filename.empty())
1870 return {ImportAction::None};
1872 bool IsImportDecl = HashLoc.isInvalid();
1873 SourceLocation StartLoc = IsImportDecl ? IncludeTok.getLocation() : HashLoc;
1875 // Complain about attempts to #include files in an audit pragma.
1876 if (PragmaARCCFCodeAuditedInfo.second.isValid()) {
1877 Diag(StartLoc, diag::err_pp_include_in_arc_cf_code_audited) << IsImportDecl;
1878 Diag(PragmaARCCFCodeAuditedInfo.second, diag::note_pragma_entered_here);
1880 // Immediately leave the pragma.
1881 PragmaARCCFCodeAuditedInfo = {nullptr, SourceLocation()};
1884 // Complain about attempts to #include files in an assume-nonnull pragma.
1885 if (PragmaAssumeNonNullLoc.isValid()) {
1886 Diag(StartLoc, diag::err_pp_include_in_assume_nonnull) << IsImportDecl;
1887 Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
1889 // Immediately leave the pragma.
1890 PragmaAssumeNonNullLoc = SourceLocation();
1893 if (HeaderInfo.HasIncludeAliasMap()) {
1894 // Map the filename with the brackets still attached. If the name doesn't
1895 // map to anything, fall back on the filename we've already gotten the
1897 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1898 if (!NewName.empty())
1902 // Search include directories.
1903 bool IsMapped = false;
1904 bool IsFrameworkFound = false;
1905 const DirectoryLookup *CurDir;
1906 SmallString<1024> SearchPath;
1907 SmallString<1024> RelativePath;
1908 // We get the raw path only if we have 'Callbacks' to which we later pass
1910 ModuleMap::KnownHeader SuggestedModule;
1911 SourceLocation FilenameLoc = FilenameTok.getLocation();
1912 StringRef LookupFilename = Filename;
1915 // Normalize slashes when compiling with -fms-extensions on non-Windows. This
1916 // is unnecessary on Windows since the filesystem there handles backslashes.
1917 SmallString<128> NormalizedPath;
1918 if (LangOpts.MicrosoftExt) {
1919 NormalizedPath = Filename.str();
1920 llvm::sys::path::native(NormalizedPath);
1921 LookupFilename = NormalizedPath;
1925 Optional<FileEntryRef> File = LookupHeaderIncludeOrImport(
1926 CurDir, Filename, FilenameLoc, FilenameRange, FilenameTok,
1927 IsFrameworkFound, IsImportDecl, IsMapped, LookupFrom, LookupFromFile,
1928 LookupFilename, RelativePath, SearchPath, SuggestedModule, isAngled);
1930 if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) {
1931 if (File && isPCHThroughHeader(&File->getFileEntry()))
1932 SkippingUntilPCHThroughHeader = false;
1933 return {ImportAction::None};
1936 // Check for circular inclusion of the main file.
1937 // We can't generate a consistent preamble with regard to the conditional
1938 // stack if the main file is included again as due to the preamble bounds
1939 // some directives (e.g. #endif of a header guard) will never be seen.
1940 // Since this will lead to confusing errors, avoid the inclusion.
1941 if (File && PreambleConditionalStack.isRecording() &&
1942 SourceMgr.translateFile(&File->getFileEntry()) ==
1943 SourceMgr.getMainFileID()) {
1944 Diag(FilenameTok.getLocation(),
1945 diag::err_pp_including_mainfile_in_preamble);
1946 return {ImportAction::None};
1949 // Should we enter the source file? Set to Skip if either the source file is
1950 // known to have no effect beyond its effect on module visibility -- that is,
1951 // if it's got an include guard that is already defined, set to Import if it
1952 // is a modular header we've already built and should import.
1953 enum { Enter, Import, Skip, IncludeLimitReached } Action = Enter;
1955 if (PPOpts->SingleFileParseMode)
1956 Action = IncludeLimitReached;
1958 // If we've reached the max allowed include depth, it is usually due to an
1959 // include cycle. Don't enter already processed files again as it can lead to
1960 // reaching the max allowed include depth again.
1961 if (Action == Enter && HasReachedMaxIncludeDepth && File &&
1962 HeaderInfo.getFileInfo(&File->getFileEntry()).NumIncludes)
1963 Action = IncludeLimitReached;
1965 // Determine whether we should try to import the module for this #include, if
1966 // there is one. Don't do so if precompiled module support is disabled or we
1967 // are processing this module textually (because we're building the module).
1968 if (Action == Enter && File && SuggestedModule && getLangOpts().Modules &&
1969 !isForModuleBuilding(SuggestedModule.getModule(),
1970 getLangOpts().CurrentModule,
1971 getLangOpts().ModuleName)) {
1972 // If this include corresponds to a module but that module is
1973 // unavailable, diagnose the situation and bail out.
1974 // FIXME: Remove this; loadModule does the same check (but produces
1975 // slightly worse diagnostics).
1976 if (checkModuleIsAvailable(getLangOpts(), getTargetInfo(), getDiagnostics(),
1977 SuggestedModule.getModule())) {
1978 Diag(FilenameTok.getLocation(),
1979 diag::note_implicit_top_level_module_import_here)
1980 << SuggestedModule.getModule()->getTopLevelModuleName();
1981 return {ImportAction::None};
1984 // Compute the module access path corresponding to this module.
1985 // FIXME: Should we have a second loadModule() overload to avoid this
1986 // extra lookup step?
1987 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1988 for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
1989 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1990 FilenameTok.getLocation()));
1991 std::reverse(Path.begin(), Path.end());
1993 // Warn that we're replacing the include/import with a module import.
1995 diagnoseAutoModuleImport(*this, StartLoc, IncludeTok, Path, CharEnd);
1997 // Load the module to import its macros. We'll make the declarations
1998 // visible when the parser gets here.
1999 // FIXME: Pass SuggestedModule in here rather than converting it to a path
2000 // and making the module loader convert it back again.
2001 ModuleLoadResult Imported = TheModuleLoader.loadModule(
2002 IncludeTok.getLocation(), Path, Module::Hidden,
2003 /*IsInclusionDirective=*/true);
2004 assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
2005 "the imported module is different than the suggested one");
2009 } else if (Imported.isMissingExpected()) {
2010 // We failed to find a submodule that we assumed would exist (because it
2011 // was in the directory of an umbrella header, for instance), but no
2012 // actual module containing it exists (because the umbrella header is
2013 // incomplete). Treat this as a textual inclusion.
2014 SuggestedModule = ModuleMap::KnownHeader();
2015 } else if (Imported.isConfigMismatch()) {
2016 // On a configuration mismatch, enter the header textually. We still know
2017 // that it's part of the corresponding module.
2019 // We hit an error processing the import. Bail out.
2020 if (hadModuleLoaderFatalFailure()) {
2021 // With a fatal failure in the module loader, we abort parsing.
2022 Token &Result = IncludeTok;
2023 assert(CurLexer && "#include but no current lexer set!");
2024 Result.startToken();
2025 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
2026 CurLexer->cutOffLexing();
2028 return {ImportAction::None};
2032 // The #included file will be considered to be a system header if either it is
2033 // in a system include directory, or if the #includer is a system include
2035 SrcMgr::CharacteristicKind FileCharacter =
2036 SourceMgr.getFileCharacteristic(FilenameTok.getLocation());
2038 FileCharacter = std::max(HeaderInfo.getFileDirFlavor(&File->getFileEntry()),
2041 // If this is a '#import' or an import-declaration, don't re-enter the file.
2043 // FIXME: If we have a suggested module for a '#include', and we've already
2044 // visited this file, don't bother entering it again. We know it has no
2048 IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import;
2050 // Ask HeaderInfo if we should enter this #include file. If not, #including
2051 // this file will have no effect.
2052 if (Action == Enter && File &&
2053 !HeaderInfo.ShouldEnterIncludeFile(*this, &File->getFileEntry(),
2054 EnterOnce, getLangOpts().Modules,
2055 SuggestedModule.getModule())) {
2056 // Even if we've already preprocessed this header once and know that we
2057 // don't need to see its contents again, we still need to import it if it's
2058 // modular because we might not have imported it from this submodule before.
2060 // FIXME: We don't do this when compiling a PCH because the AST
2061 // serialization layer can't cope with it. This means we get local
2062 // submodule visibility semantics wrong in that case.
2063 Action = (SuggestedModule && !getLangOpts().CompilingPCH) ? Import : Skip;
2066 if (Callbacks && !IsImportDecl) {
2067 // Notify the callback object that we've seen an inclusion directive.
2068 // FIXME: Use a different callback for a pp-import?
2069 Callbacks->InclusionDirective(
2070 HashLoc, IncludeTok, LookupFilename, isAngled, FilenameRange,
2071 File ? &File->getFileEntry() : nullptr, SearchPath, RelativePath,
2072 Action == Import ? SuggestedModule.getModule() : nullptr,
2074 if (Action == Skip && File)
2075 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
2079 return {ImportAction::None};
2081 // If this is a C++20 pp-import declaration, diagnose if we didn't find any
2082 // module corresponding to the named header.
2083 if (IsImportDecl && !SuggestedModule) {
2084 Diag(FilenameTok, diag::err_header_import_not_header_unit)
2085 << OriginalFilename << File->getName();
2086 return {ImportAction::None};
2089 // Issue a diagnostic if the name of the file on disk has a different case
2090 // than the one we're about to open.
2091 const bool CheckIncludePathPortability =
2092 !IsMapped && !File->getFileEntry().tryGetRealPathName().empty();
2094 if (CheckIncludePathPortability) {
2095 StringRef Name = LookupFilename;
2096 StringRef RealPathName = File->getFileEntry().tryGetRealPathName();
2097 SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name),
2098 llvm::sys::path::end(Name));
2100 if (trySimplifyPath(Components, RealPathName)) {
2101 SmallString<128> Path;
2102 Path.reserve(Name.size()+2);
2103 Path.push_back(isAngled ? '<' : '"');
2104 bool isLeadingSeparator = llvm::sys::path::is_absolute(Name);
2105 for (auto Component : Components) {
2106 if (isLeadingSeparator)
2107 isLeadingSeparator = false;
2109 Path.append(Component);
2110 // Append the separator the user used, or the close quote
2112 Path.size() <= Filename.size() ? Filename[Path.size()-1] :
2113 (isAngled ? '>' : '"'));
2115 // For user files and known standard headers, by default we issue a diagnostic.
2116 // For other system headers, we don't. They can be controlled separately.
2117 auto DiagId = (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name)) ?
2118 diag::pp_nonportable_path : diag::pp_nonportable_system_path;
2119 Diag(FilenameTok, DiagId) << Path <<
2120 FixItHint::CreateReplacement(FilenameRange, Path);
2126 // If we don't need to enter the file, stop now.
2127 if (Module *M = SuggestedModule.getModule())
2128 return {ImportAction::SkippedModuleImport, M};
2129 return {ImportAction::None};
2131 case IncludeLimitReached:
2132 // If we reached our include limit and don't want to enter any more files,
2133 // don't go any further.
2134 return {ImportAction::None};
2137 // If this is a module import, make it visible if needed.
2138 Module *M = SuggestedModule.getModule();
2139 assert(M && "no module to import");
2141 makeModuleVisible(M, EndLoc);
2143 if (IncludeTok.getIdentifierInfo()->getPPKeywordID() ==
2144 tok::pp___include_macros)
2145 return {ImportAction::None};
2147 return {ImportAction::ModuleImport, M};
2154 // Check that we don't have infinite #include recursion.
2155 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
2156 Diag(FilenameTok, diag::err_pp_include_too_deep);
2157 HasReachedMaxIncludeDepth = true;
2158 return {ImportAction::None};
2161 // Look up the file, create a File ID for it.
2162 SourceLocation IncludePos = FilenameTok.getLocation();
2163 // If the filename string was the result of macro expansions, set the include
2164 // position on the file where it will be included and after the expansions.
2165 if (IncludePos.isMacroID())
2166 IncludePos = SourceMgr.getExpansionRange(IncludePos).getEnd();
2167 FileID FID = SourceMgr.createFileID(*File, IncludePos, FileCharacter);
2168 assert(FID.isValid() && "Expected valid file ID");
2170 // If all is good, enter the new file!
2171 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
2172 return {ImportAction::None};
2174 // Determine if we're switching to building a new submodule, and which one.
2175 if (auto *M = SuggestedModule.getModule()) {
2176 if (M->getTopLevelModule()->ShadowingModule) {
2177 // We are building a submodule that belongs to a shadowed module. This
2178 // means we find header files in the shadowed module.
2179 Diag(M->DefinitionLoc, diag::err_module_build_shadowed_submodule)
2180 << M->getFullModuleName();
2181 Diag(M->getTopLevelModule()->ShadowingModule->DefinitionLoc,
2182 diag::note_previous_definition);
2183 return {ImportAction::None};
2185 // When building a pch, -fmodule-name tells the compiler to textually
2186 // include headers in the specified module. We are not building the
2187 // specified module.
2189 // FIXME: This is the wrong way to handle this. We should produce a PCH
2190 // that behaves the same as the header would behave in a compilation using
2191 // that PCH, which means we should enter the submodule. We need to teach
2192 // the AST serialization layer to deal with the resulting AST.
2193 if (getLangOpts().CompilingPCH &&
2194 isForModuleBuilding(M, getLangOpts().CurrentModule,
2195 getLangOpts().ModuleName))
2196 return {ImportAction::None};
2198 assert(!CurLexerSubmodule && "should not have marked this as a module yet");
2199 CurLexerSubmodule = M;
2201 // Let the macro handling code know that any future macros are within
2202 // the new submodule.
2203 EnterSubmodule(M, EndLoc, /*ForPragma*/false);
2205 // Let the parser know that any future declarations are within the new
2207 // FIXME: There's no point doing this if we're handling a #__include_macros
2209 return {ImportAction::ModuleBegin, M};
2212 assert(!IsImportDecl && "failed to diagnose missing module for import decl");
2213 return {ImportAction::None};
2216 /// HandleIncludeNextDirective - Implements \#include_next.
2218 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
2219 Token &IncludeNextTok) {
2220 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
2222 // #include_next is like #include, except that we start searching after
2223 // the current found directory. If we can't do this, issue a
2225 const DirectoryLookup *Lookup = CurDirLookup;
2226 const FileEntry *LookupFromFile = nullptr;
2227 if (isInPrimaryFile() && LangOpts.IsHeaderFile) {
2228 // If the main file is a header, then it's either for PCH/AST generation,
2229 // or libclang opened it. Either way, handle it as a normal include below
2230 // and do not complain about include_next.
2231 } else if (isInPrimaryFile()) {
2233 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
2234 } else if (CurLexerSubmodule) {
2235 // Start looking up in the directory *after* the one in which the current
2236 // file would be found, if any.
2237 assert(CurPPLexer && "#include_next directive in macro?");
2238 LookupFromFile = CurPPLexer->getFileEntry();
2240 } else if (!Lookup) {
2241 // The current file was not found by walking the include path. Either it
2242 // is the primary file (handled above), or it was found by absolute path,
2243 // or it was found relative to such a file.
2244 // FIXME: Track enough information so we know which case we're in.
2245 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
2247 // Start looking up in the next directory.
2251 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
2255 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
2256 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
2257 // The Microsoft #import directive takes a type library and generates header
2258 // files from it, and includes those. This is beyond the scope of what clang
2259 // does, so we ignore it and error out. However, #import can optionally have
2260 // trailing attributes that span multiple lines. We're going to eat those
2261 // so we can continue processing from there.
2262 Diag(Tok, diag::err_pp_import_directive_ms );
2264 // Read tokens until we get to the end of the directive. Note that the
2265 // directive can be split over multiple lines using the backslash character.
2266 DiscardUntilEndOfDirective();
2269 /// HandleImportDirective - Implements \#import.
2271 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
2273 if (!LangOpts.ObjC) { // #import is standard for ObjC.
2274 if (LangOpts.MSVCCompat)
2275 return HandleMicrosoftImportDirective(ImportTok);
2276 Diag(ImportTok, diag::ext_pp_import_directive);
2278 return HandleIncludeDirective(HashLoc, ImportTok);
2281 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
2282 /// pseudo directive in the predefines buffer. This handles it by sucking all
2283 /// tokens through the preprocessor and discarding them (only keeping the side
2284 /// effects on the preprocessor).
2285 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
2286 Token &IncludeMacrosTok) {
2287 // This directive should only occur in the predefines buffer. If not, emit an
2288 // error and reject it.
2289 SourceLocation Loc = IncludeMacrosTok.getLocation();
2290 if (SourceMgr.getBufferName(Loc) != "<built-in>") {
2291 Diag(IncludeMacrosTok.getLocation(),
2292 diag::pp_include_macros_out_of_predefines);
2293 DiscardUntilEndOfDirective();
2297 // Treat this as a normal #include for checking purposes. If this is
2298 // successful, it will push a new lexer onto the include stack.
2299 HandleIncludeDirective(HashLoc, IncludeMacrosTok);
2304 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
2305 } while (TmpTok.isNot(tok::hashhash));
2308 //===----------------------------------------------------------------------===//
2309 // Preprocessor Macro Directive Handling.
2310 //===----------------------------------------------------------------------===//
2312 /// ReadMacroParameterList - The ( starting a parameter list of a macro
2313 /// definition has just been read. Lex the rest of the parameters and the
2314 /// closing ), updating MI with what we learn. Return true if an error occurs
2315 /// parsing the param list.
2316 bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
2317 SmallVector<IdentifierInfo*, 32> Parameters;
2320 LexUnexpandedToken(Tok);
2321 switch (Tok.getKind()) {
2323 // Found the end of the parameter list.
2324 if (Parameters.empty()) // #define FOO()
2326 // Otherwise we have #define FOO(A,)
2327 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2329 case tok::ellipsis: // #define X(... -> C99 varargs
2331 Diag(Tok, LangOpts.CPlusPlus11 ?
2332 diag::warn_cxx98_compat_variadic_macro :
2333 diag::ext_variadic_macro);
2335 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
2336 if (LangOpts.OpenCL) {
2337 Diag(Tok, diag::ext_pp_opencl_variadic_macros);
2340 // Lex the token after the identifier.
2341 LexUnexpandedToken(Tok);
2342 if (Tok.isNot(tok::r_paren)) {
2343 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2346 // Add the __VA_ARGS__ identifier as a parameter.
2347 Parameters.push_back(Ident__VA_ARGS__);
2348 MI->setIsC99Varargs();
2349 MI->setParameterList(Parameters, BP);
2351 case tok::eod: // #define X(
2352 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2355 // Handle keywords and identifiers here to accept things like
2356 // #define Foo(for) for.
2357 IdentifierInfo *II = Tok.getIdentifierInfo();
2360 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2364 // If this is already used as a parameter, it is used multiple times (e.g.
2366 if (llvm::find(Parameters, II) != Parameters.end()) { // C99 6.10.3p6
2367 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
2371 // Add the parameter to the macro info.
2372 Parameters.push_back(II);
2374 // Lex the token after the identifier.
2375 LexUnexpandedToken(Tok);
2377 switch (Tok.getKind()) {
2378 default: // #define X(A B
2379 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2381 case tok::r_paren: // #define X(A)
2382 MI->setParameterList(Parameters, BP);
2384 case tok::comma: // #define X(A,
2386 case tok::ellipsis: // #define X(A... -> GCC extension
2387 // Diagnose extension.
2388 Diag(Tok, diag::ext_named_variadic_macro);
2390 // Lex the token after the identifier.
2391 LexUnexpandedToken(Tok);
2392 if (Tok.isNot(tok::r_paren)) {
2393 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2397 MI->setIsGNUVarargs();
2398 MI->setParameterList(Parameters, BP);
2405 static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2406 const LangOptions &LOptions) {
2407 if (MI->getNumTokens() == 1) {
2408 const Token &Value = MI->getReplacementToken(0);
2410 // Macro that is identity, like '#define inline inline' is a valid pattern.
2411 if (MacroName.getKind() == Value.getKind())
2414 // Macro that maps a keyword to the same keyword decorated with leading/
2415 // trailing underscores is a valid pattern:
2416 // #define inline __inline
2417 // #define inline __inline__
2418 // #define inline _inline (in MS compatibility mode)
2419 StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2420 if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2421 if (!II->isKeyword(LOptions))
2423 StringRef ValueText = II->getName();
2424 StringRef TrimmedValue = ValueText;
2425 if (!ValueText.startswith("__")) {
2426 if (ValueText.startswith("_"))
2427 TrimmedValue = TrimmedValue.drop_front(1);
2431 TrimmedValue = TrimmedValue.drop_front(2);
2432 if (TrimmedValue.endswith("__"))
2433 TrimmedValue = TrimmedValue.drop_back(2);
2435 return TrimmedValue.equals(MacroText);
2442 return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2444 MI->getNumTokens() == 0;
2447 // ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
2448 // entire line) of the macro's tokens and adds them to MacroInfo, and while
2449 // doing so performs certain validity checks including (but not limited to):
2450 // - # (stringization) is followed by a macro parameter
2452 // Returns a nullptr if an invalid sequence of tokens is encountered or returns
2453 // a pointer to a MacroInfo object.
2455 MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody(
2456 const Token &MacroNameTok, const bool ImmediatelyAfterHeaderGuard) {
2458 Token LastTok = MacroNameTok;
2459 // Create the new macro.
2460 MacroInfo *const MI = AllocateMacroInfo(MacroNameTok.getLocation());
2463 LexUnexpandedToken(Tok);
2465 // Ensure we consume the rest of the macro body if errors occur.
2466 auto _ = llvm::make_scope_exit([&]() {
2467 // The flag indicates if we are still waiting for 'eod'.
2468 if (CurLexer->ParsingPreprocessorDirective)
2469 DiscardUntilEndOfDirective();
2472 // Used to un-poison and then re-poison identifiers of the __VA_ARGS__ ilk
2473 // within their appropriate context.
2474 VariadicMacroScopeGuard VariadicMacroScopeGuard(*this);
2476 // If this is a function-like macro definition, parse the argument list,
2477 // marking each of the identifiers as being used as macro arguments. Also,
2478 // check other constraints on the first token of the macro body.
2479 if (Tok.is(tok::eod)) {
2480 if (ImmediatelyAfterHeaderGuard) {
2481 // Save this macro information since it may part of a header guard.
2482 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2483 MacroNameTok.getLocation());
2485 // If there is no body to this macro, we have no special handling here.
2486 } else if (Tok.hasLeadingSpace()) {
2487 // This is a normal token with leading space. Clear the leading space
2488 // marker on the first token to get proper expansion.
2489 Tok.clearFlag(Token::LeadingSpace);
2490 } else if (Tok.is(tok::l_paren)) {
2491 // This is a function-like macro definition. Read the argument list.
2492 MI->setIsFunctionLike();
2493 if (ReadMacroParameterList(MI, LastTok))
2496 // If this is a definition of an ISO C/C++ variadic function-like macro (not
2497 // using the GNU named varargs extension) inform our variadic scope guard
2498 // which un-poisons and re-poisons certain identifiers (e.g. __VA_ARGS__)
2499 // allowed only within the definition of a variadic macro.
2501 if (MI->isC99Varargs()) {
2502 VariadicMacroScopeGuard.enterScope();
2505 // Read the first token after the arg list for down below.
2506 LexUnexpandedToken(Tok);
2507 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
2508 // C99 requires whitespace between the macro definition and the body. Emit
2509 // a diagnostic for something like "#define X+".
2510 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2512 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2513 // first character of a replacement list is not a character required by
2514 // subclause 5.2.1, then there shall be white-space separation between the
2515 // identifier and the replacement list.". 5.2.1 lists this set:
2516 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2517 // is irrelevant here.
2518 bool isInvalid = false;
2519 if (Tok.is(tok::at)) // @ is not in the list above.
2521 else if (Tok.is(tok::unknown)) {
2522 // If we have an unknown token, it is something strange like "`". Since
2523 // all of valid characters would have lexed into a single character
2524 // token of some sort, we know this is not a valid case.
2528 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2530 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2533 if (!Tok.is(tok::eod))
2536 // Read the rest of the macro body.
2537 if (MI->isObjectLike()) {
2538 // Object-like macros are very simple, just read their body.
2539 while (Tok.isNot(tok::eod)) {
2541 MI->AddTokenToBody(Tok);
2542 // Get the next token of the macro.
2543 LexUnexpandedToken(Tok);
2546 // Otherwise, read the body of a function-like macro. While we are at it,
2547 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2548 // parameters in function-like macro expansions.
2550 VAOptDefinitionContext VAOCtx(*this);
2552 while (Tok.isNot(tok::eod)) {
2555 if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
2556 MI->AddTokenToBody(Tok);
2558 if (VAOCtx.isVAOptToken(Tok)) {
2559 // If we're already within a VAOPT, emit an error.
2560 if (VAOCtx.isInVAOpt()) {
2561 Diag(Tok, diag::err_pp_vaopt_nested_use);
2564 // Ensure VAOPT is followed by a '(' .
2565 LexUnexpandedToken(Tok);
2566 if (Tok.isNot(tok::l_paren)) {
2567 Diag(Tok, diag::err_pp_missing_lparen_in_vaopt_use);
2570 MI->AddTokenToBody(Tok);
2571 VAOCtx.sawVAOptFollowedByOpeningParens(Tok.getLocation());
2572 LexUnexpandedToken(Tok);
2573 if (Tok.is(tok::hashhash)) {
2574 Diag(Tok, diag::err_vaopt_paste_at_start);
2578 } else if (VAOCtx.isInVAOpt()) {
2579 if (Tok.is(tok::r_paren)) {
2580 if (VAOCtx.sawClosingParen()) {
2581 const unsigned NumTokens = MI->getNumTokens();
2582 assert(NumTokens >= 3 && "Must have seen at least __VA_OPT__( "
2583 "and a subsequent tok::r_paren");
2584 if (MI->getReplacementToken(NumTokens - 2).is(tok::hashhash)) {
2585 Diag(Tok, diag::err_vaopt_paste_at_end);
2589 } else if (Tok.is(tok::l_paren)) {
2590 VAOCtx.sawOpeningParen(Tok.getLocation());
2593 // Get the next token of the macro.
2594 LexUnexpandedToken(Tok);
2598 // If we're in -traditional mode, then we should ignore stringification
2599 // and token pasting. Mark the tokens as unknown so as not to confuse
2601 if (getLangOpts().TraditionalCPP) {
2602 Tok.setKind(tok::unknown);
2603 MI->AddTokenToBody(Tok);
2605 // Get the next token of the macro.
2606 LexUnexpandedToken(Tok);
2610 if (Tok.is(tok::hashhash)) {
2611 // If we see token pasting, check if it looks like the gcc comma
2612 // pasting extension. We'll use this information to suppress
2613 // diagnostics later on.
2615 // Get the next token of the macro.
2616 LexUnexpandedToken(Tok);
2618 if (Tok.is(tok::eod)) {
2619 MI->AddTokenToBody(LastTok);
2623 unsigned NumTokens = MI->getNumTokens();
2624 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2625 MI->getReplacementToken(NumTokens-1).is(tok::comma))
2626 MI->setHasCommaPasting();
2628 // Things look ok, add the '##' token to the macro.
2629 MI->AddTokenToBody(LastTok);
2633 // Our Token is a stringization operator.
2634 // Get the next token of the macro.
2635 LexUnexpandedToken(Tok);
2637 // Check for a valid macro arg identifier or __VA_OPT__.
2638 if (!VAOCtx.isVAOptToken(Tok) &&
2639 (Tok.getIdentifierInfo() == nullptr ||
2640 MI->getParameterNum(Tok.getIdentifierInfo()) == -1)) {
2642 // If this is assembler-with-cpp mode, we accept random gibberish after
2643 // the '#' because '#' is often a comment character. However, change
2644 // the kind of the token to tok::unknown so that the preprocessor isn't
2646 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
2647 LastTok.setKind(tok::unknown);
2648 MI->AddTokenToBody(LastTok);
2651 Diag(Tok, diag::err_pp_stringize_not_parameter)
2652 << LastTok.is(tok::hashat);
2657 // Things look ok, add the '#' and param name tokens to the macro.
2658 MI->AddTokenToBody(LastTok);
2660 // If the token following '#' is VAOPT, let the next iteration handle it
2661 // and check it for correctness, otherwise add the token and prime the
2662 // loop with the next one.
2663 if (!VAOCtx.isVAOptToken(Tok)) {
2664 MI->AddTokenToBody(Tok);
2667 // Get the next token of the macro.
2668 LexUnexpandedToken(Tok);
2671 if (VAOCtx.isInVAOpt()) {
2672 assert(Tok.is(tok::eod) && "Must be at End Of preprocessing Directive");
2673 Diag(Tok, diag::err_pp_expected_after)
2674 << LastTok.getKind() << tok::r_paren;
2675 Diag(VAOCtx.getUnmatchedOpeningParenLoc(), diag::note_matching) << tok::l_paren;
2679 MI->setDefinitionEndLoc(LastTok.getLocation());
2682 /// HandleDefineDirective - Implements \#define. This consumes the entire macro
2683 /// line then lets the caller lex the next real token.
2684 void Preprocessor::HandleDefineDirective(
2685 Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) {
2689 bool MacroShadowsKeyword;
2690 ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
2692 // Error reading macro name? If so, diagnostic already issued.
2693 if (MacroNameTok.is(tok::eod))
2696 // If we are supposed to keep comments in #defines, reenable comment saving
2698 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
2700 MacroInfo *const MI = ReadOptionalMacroParameterListAndBody(
2701 MacroNameTok, ImmediatelyAfterHeaderGuard);
2705 if (MacroShadowsKeyword &&
2706 !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
2707 Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
2709 // Check that there is no paste (##) operator at the beginning or end of the
2710 // replacement list.
2711 unsigned NumTokens = MI->getNumTokens();
2712 if (NumTokens != 0) {
2713 if (MI->getReplacementToken(0).is(tok::hashhash)) {
2714 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2717 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2718 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2723 // When skipping just warn about macros that do not match.
2724 if (SkippingUntilPCHThroughHeader) {
2725 const MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo());
2726 if (!OtherMI || !MI->isIdenticalTo(*OtherMI, *this,
2727 /*Syntactic=*/LangOpts.MicrosoftExt))
2728 Diag(MI->getDefinitionLoc(), diag::warn_pp_macro_def_mismatch_with_pch)
2729 << MacroNameTok.getIdentifierInfo();
2733 // Finally, if this identifier already had a macro defined for it, verify that
2734 // the macro bodies are identical, and issue diagnostics if they are not.
2735 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
2736 // In Objective-C, ignore attempts to directly redefine the builtin
2737 // definitions of the ownership qualifiers. It's still possible to
2739 auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
2740 return II->isStr("__strong") ||
2741 II->isStr("__weak") ||
2742 II->isStr("__unsafe_unretained") ||
2743 II->isStr("__autoreleasing");
2745 if (getLangOpts().ObjC &&
2746 SourceMgr.getFileID(OtherMI->getDefinitionLoc())
2747 == getPredefinesFileID() &&
2748 isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
2749 // Warn if it changes the tokens.
2750 if ((!getDiagnostics().getSuppressSystemWarnings() ||
2751 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
2752 !MI->isIdenticalTo(*OtherMI, *this,
2753 /*Syntactic=*/LangOpts.MicrosoftExt)) {
2754 Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
2756 assert(!OtherMI->isWarnIfUnused());
2760 // It is very common for system headers to have tons of macro redefinitions
2761 // and for warnings to be disabled in system headers. If this is the case,
2762 // then don't bother calling MacroInfo::isIdenticalTo.
2763 if (!getDiagnostics().getSuppressSystemWarnings() ||
2764 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
2765 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
2766 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2768 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2769 // C++ [cpp.predefined]p4, but allow it as an extension.
2770 if (OtherMI->isBuiltinMacro())
2771 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
2772 // Macros must be identical. This means all tokens and whitespace
2773 // separation must be the same. C99 6.10.3p2.
2774 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
2775 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
2776 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2777 << MacroNameTok.getIdentifierInfo();
2778 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2781 if (OtherMI->isWarnIfUnused())
2782 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
2785 DefMacroDirective *MD =
2786 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
2788 assert(!MI->isUsed());
2789 // If we need warning for not using the macro, add its location in the
2790 // warn-because-unused-macro set. If it gets used it will be removed from set.
2791 if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
2792 !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc()) &&
2793 !MacroExpansionInDirectivesOverride) {
2794 MI->setIsWarnIfUnused(true);
2795 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2798 // If the callbacks want to know, tell them about the macro definition.
2800 Callbacks->MacroDefined(MacroNameTok, MD);
2803 /// HandleUndefDirective - Implements \#undef.
2805 void Preprocessor::HandleUndefDirective() {
2809 ReadMacroName(MacroNameTok, MU_Undef);
2811 // Error reading macro name? If so, diagnostic already issued.
2812 if (MacroNameTok.is(tok::eod))
2815 // Check to see if this is the last token on the #undef line.
2816 CheckEndOfDirective("undef");
2818 // Okay, we have a valid identifier to undef.
2819 auto *II = MacroNameTok.getIdentifierInfo();
2820 auto MD = getMacroDefinition(II);
2821 UndefMacroDirective *Undef = nullptr;
2823 // If the macro is not defined, this is a noop undef.
2824 if (const MacroInfo *MI = MD.getMacroInfo()) {
2825 if (!MI->isUsed() && MI->isWarnIfUnused())
2826 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2828 if (MI->isWarnIfUnused())
2829 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2831 Undef = AllocateUndefMacroDirective(MacroNameTok.getLocation());
2834 // If the callbacks want to know, tell them about the macro #undef.
2835 // Note: no matter if the macro was defined or not.
2837 Callbacks->MacroUndefined(MacroNameTok, MD, Undef);
2840 appendMacroDirective(II, Undef);
2843 //===----------------------------------------------------------------------===//
2844 // Preprocessor Conditional Directive Handling.
2845 //===----------------------------------------------------------------------===//
2847 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2848 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2849 /// true if any tokens have been returned or pp-directives activated before this
2850 /// \#ifndef has been lexed.
2852 void Preprocessor::HandleIfdefDirective(Token &Result,
2853 const Token &HashToken,
2855 bool ReadAnyTokensBeforeDirective) {
2857 Token DirectiveTok = Result;
2860 ReadMacroName(MacroNameTok);
2862 // Error reading macro name? If so, diagnostic already issued.
2863 if (MacroNameTok.is(tok::eod)) {
2864 // Skip code until we get to #endif. This helps with recovery by not
2865 // emitting an error when the #endif is reached.
2866 SkipExcludedConditionalBlock(HashToken.getLocation(),
2867 DirectiveTok.getLocation(),
2868 /*Foundnonskip*/ false, /*FoundElse*/ false);
2872 // Check to see if this is the last token on the #if[n]def line.
2873 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2875 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2876 auto MD = getMacroDefinition(MII);
2877 MacroInfo *MI = MD.getMacroInfo();
2879 if (CurPPLexer->getConditionalStackDepth() == 0) {
2880 // If the start of a top-level #ifdef and if the macro is not defined,
2881 // inform MIOpt that this might be the start of a proper include guard.
2882 // Otherwise it is some other form of unknown conditional which we can't
2884 if (!ReadAnyTokensBeforeDirective && !MI) {
2885 assert(isIfndef && "#ifdef shouldn't reach here");
2886 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
2888 CurPPLexer->MIOpt.EnterTopLevelConditional();
2891 // If there is a macro, process it.
2892 if (MI) // Mark it used.
2893 markMacroAsUsed(MI);
2897 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2899 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2902 bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
2903 getSourceManager().isInMainFile(DirectiveTok.getLocation());
2905 // Should we include the stuff contained by this directive?
2906 if (PPOpts->SingleFileParseMode && !MI) {
2907 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2908 // the directive blocks.
2909 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2910 /*wasskip*/false, /*foundnonskip*/false,
2911 /*foundelse*/false);
2912 } else if (!MI == isIfndef || RetainExcludedCB) {
2913 // Yes, remember that we are inside a conditional, then lex the next token.
2914 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2915 /*wasskip*/false, /*foundnonskip*/true,
2916 /*foundelse*/false);
2918 // No, skip the contents of this block.
2919 SkipExcludedConditionalBlock(HashToken.getLocation(),
2920 DirectiveTok.getLocation(),
2921 /*Foundnonskip*/ false,
2922 /*FoundElse*/ false);
2926 /// HandleIfDirective - Implements the \#if directive.
2928 void Preprocessor::HandleIfDirective(Token &IfToken,
2929 const Token &HashToken,
2930 bool ReadAnyTokensBeforeDirective) {
2933 // Parse and evaluate the conditional expression.
2934 IdentifierInfo *IfNDefMacro = nullptr;
2935 const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
2936 const bool ConditionalTrue = DER.Conditional;
2938 // If this condition is equivalent to #ifndef X, and if this is the first
2939 // directive seen, handle it for the multiple-include optimization.
2940 if (CurPPLexer->getConditionalStackDepth() == 0) {
2941 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2942 // FIXME: Pass in the location of the macro name, not the 'if' token.
2943 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
2945 CurPPLexer->MIOpt.EnterTopLevelConditional();
2950 IfToken.getLocation(), DER.ExprRange,
2951 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
2953 bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
2954 getSourceManager().isInMainFile(IfToken.getLocation());
2956 // Should we include the stuff contained by this directive?
2957 if (PPOpts->SingleFileParseMode && DER.IncludedUndefinedIds) {
2958 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2959 // the directive blocks.
2960 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2961 /*foundnonskip*/false, /*foundelse*/false);
2962 } else if (ConditionalTrue || RetainExcludedCB) {
2963 // Yes, remember that we are inside a conditional, then lex the next token.
2964 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2965 /*foundnonskip*/true, /*foundelse*/false);
2967 // No, skip the contents of this block.
2968 SkipExcludedConditionalBlock(HashToken.getLocation(), IfToken.getLocation(),
2969 /*Foundnonskip*/ false,
2970 /*FoundElse*/ false);
2974 /// HandleEndifDirective - Implements the \#endif directive.
2976 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2979 // Check that this is the whole directive.
2980 CheckEndOfDirective("endif");
2982 PPConditionalInfo CondInfo;
2983 if (CurPPLexer->popConditionalLevel(CondInfo)) {
2984 // No conditionals on the stack: this is an #endif without an #if.
2985 Diag(EndifToken, diag::err_pp_endif_without_if);
2989 // If this the end of a top-level #endif, inform MIOpt.
2990 if (CurPPLexer->getConditionalStackDepth() == 0)
2991 CurPPLexer->MIOpt.ExitTopLevelConditional();
2993 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2994 "This code should only be reachable in the non-skipping case!");
2997 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
3000 /// HandleElseDirective - Implements the \#else directive.
3002 void Preprocessor::HandleElseDirective(Token &Result, const Token &HashToken) {
3005 // #else directive in a non-skipping conditional... start skipping.
3006 CheckEndOfDirective("else");
3008 PPConditionalInfo CI;
3009 if (CurPPLexer->popConditionalLevel(CI)) {
3010 Diag(Result, diag::pp_err_else_without_if);
3014 // If this is a top-level #else, inform the MIOpt.
3015 if (CurPPLexer->getConditionalStackDepth() == 0)
3016 CurPPLexer->MIOpt.EnterTopLevelConditional();
3018 // If this is a #else with a #else before it, report the error.
3019 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
3022 Callbacks->Else(Result.getLocation(), CI.IfLoc);
3024 bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
3025 getSourceManager().isInMainFile(Result.getLocation());
3027 if ((PPOpts->SingleFileParseMode && !CI.FoundNonSkip) || RetainExcludedCB) {
3028 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3029 // the directive blocks.
3030 CurPPLexer->pushConditionalLevel(CI.IfLoc, /*wasskip*/false,
3031 /*foundnonskip*/false, /*foundelse*/true);
3035 // Finally, skip the rest of the contents of this block.
3036 SkipExcludedConditionalBlock(HashToken.getLocation(), CI.IfLoc,
3037 /*Foundnonskip*/ true,
3038 /*FoundElse*/ true, Result.getLocation());
3041 /// HandleElifDirective - Implements the \#elif directive.
3043 void Preprocessor::HandleElifDirective(Token &ElifToken,
3044 const Token &HashToken) {
3047 // #elif directive in a non-skipping conditional... start skipping.
3048 // We don't care what the condition is, because we will always skip it (since
3049 // the block immediately before it was included).
3050 SourceRange ConditionRange = DiscardUntilEndOfDirective();
3052 PPConditionalInfo CI;
3053 if (CurPPLexer->popConditionalLevel(CI)) {
3054 Diag(ElifToken, diag::pp_err_elif_without_if);
3058 // If this is a top-level #elif, inform the MIOpt.
3059 if (CurPPLexer->getConditionalStackDepth() == 0)
3060 CurPPLexer->MIOpt.EnterTopLevelConditional();
3062 // If this is a #elif with a #else before it, report the error.
3063 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
3066 Callbacks->Elif(ElifToken.getLocation(), ConditionRange,
3067 PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
3069 bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
3070 getSourceManager().isInMainFile(ElifToken.getLocation());
3072 if ((PPOpts->SingleFileParseMode && !CI.FoundNonSkip) || RetainExcludedCB) {
3073 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3074 // the directive blocks.
3075 CurPPLexer->pushConditionalLevel(ElifToken.getLocation(), /*wasskip*/false,
3076 /*foundnonskip*/false, /*foundelse*/false);
3080 // Finally, skip the rest of the contents of this block.
3081 SkipExcludedConditionalBlock(
3082 HashToken.getLocation(), CI.IfLoc, /*Foundnonskip*/ true,
3083 /*FoundElse*/ CI.FoundElse, ElifToken.getLocation());