1 //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Implements # directive processing for the Preprocessor.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Basic/CharInfo.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/IdentifierTable.h"
18 #include "clang/Basic/LangOptions.h"
19 #include "clang/Basic/Module.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Basic/TokenKinds.h"
23 #include "clang/Lex/CodeCompletionHandler.h"
24 #include "clang/Lex/HeaderSearch.h"
25 #include "clang/Lex/LexDiagnostic.h"
26 #include "clang/Lex/LiteralSupport.h"
27 #include "clang/Lex/MacroInfo.h"
28 #include "clang/Lex/ModuleLoader.h"
29 #include "clang/Lex/ModuleMap.h"
30 #include "clang/Lex/PPCallbacks.h"
31 #include "clang/Lex/Pragma.h"
32 #include "clang/Lex/Preprocessor.h"
33 #include "clang/Lex/PreprocessorOptions.h"
34 #include "clang/Lex/Token.h"
35 #include "clang/Lex/VariadicMacroSupport.h"
36 #include "llvm/ADT/ArrayRef.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 void Preprocessor::DiscardUntilEndOfDirective() {
85 LexUnexpandedToken(Tmp);
86 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
87 } while (Tmp.isNot(tok::eod));
90 /// Enumerates possible cases of #define/#undef a reserved identifier.
92 MD_NoWarn, //> Not a reserved identifier
93 MD_KeywordDef, //> Macro hides keyword, enabled by default
94 MD_ReservedMacro //> #define of #undef reserved id, disabled by default
97 /// Checks if the specified identifier is reserved in the specified
99 /// This function does not check if the identifier is a keyword.
100 static bool isReservedId(StringRef Text, const LangOptions &Lang) {
101 // C++ [macro.names], C11 7.1.3:
102 // All identifiers that begin with an underscore and either an uppercase
103 // letter or another underscore are always reserved for any use.
104 if (Text.size() >= 2 && Text[0] == '_' &&
105 (isUppercase(Text[1]) || Text[1] == '_'))
107 // C++ [global.names]
108 // Each name that contains a double underscore ... is reserved to the
109 // implementation for any use.
110 if (Lang.CPlusPlus) {
111 if (Text.find("__") != StringRef::npos)
117 // The -fmodule-name option tells the compiler to textually include headers in
118 // the specified module, meaning clang won't build the specified module. This is
119 // useful in a number of situations, for instance, when building a library that
120 // vends a module map, one might want to avoid hitting intermediate build
121 // products containimg the the module map or avoid finding the system installed
122 // modulemap for that library.
123 static bool isForModuleBuilding(Module *M, StringRef CurrentModule,
124 StringRef ModuleName) {
125 StringRef TopLevelName = M->getTopLevelModuleName();
127 // When building framework Foo, we wanna make sure that Foo *and* Foo_Private
128 // are textually included and no modules are built for both.
129 if (M->getTopLevelModule()->IsFramework && CurrentModule == ModuleName &&
130 !CurrentModule.endswith("_Private") && TopLevelName.endswith("_Private"))
131 TopLevelName = TopLevelName.drop_back(8);
133 return TopLevelName == CurrentModule;
136 static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
137 const LangOptions &Lang = PP.getLangOpts();
138 StringRef Text = II->getName();
139 if (isReservedId(Text, Lang))
140 return MD_ReservedMacro;
141 if (II->isKeyword(Lang))
142 return MD_KeywordDef;
143 if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final")))
144 return MD_KeywordDef;
148 static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
149 const LangOptions &Lang = PP.getLangOpts();
150 StringRef Text = II->getName();
151 // Do not warn on keyword undef. It is generally harmless and widely used.
152 if (isReservedId(Text, Lang))
153 return MD_ReservedMacro;
157 // Return true if we want to issue a diagnostic by default if we
158 // encounter this name in a #include with the wrong case. For now,
159 // this includes the standard C and C++ headers, Posix headers,
160 // and Boost headers. Improper case for these #includes is a
161 // potential portability issue.
162 static bool warnByDefaultOnWrongCase(StringRef Include) {
163 // If the first component of the path is "boost", treat this like a standard header
164 // for the purposes of diagnostics.
165 if (::llvm::sys::path::begin(Include)->equals_lower("boost"))
168 // "condition_variable" is the longest standard header name at 18 characters.
169 // If the include file name is longer than that, it can't be a standard header.
170 static const size_t MaxStdHeaderNameLen = 18u;
171 if (Include.size() > MaxStdHeaderNameLen)
174 // Lowercase and normalize the search string.
175 SmallString<32> LowerInclude{Include};
176 for (char &Ch : LowerInclude) {
177 // In the ASCII range?
178 if (static_cast<unsigned char>(Ch) > 0x7f)
179 return false; // Can't be a standard header
181 if (Ch >= 'A' && Ch <= 'Z')
183 // Normalize path separators for comparison purposes.
184 else if (::llvm::sys::path::is_separator(Ch))
188 // The standard C/C++ and Posix headers
189 return llvm::StringSwitch<bool>(LowerInclude)
191 .Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true)
192 .Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true)
193 .Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true)
194 .Cases("stdatomic.h", "stdbool.h", "stddef.h", "stdint.h", "stdio.h", true)
195 .Cases("stdlib.h", "stdnoreturn.h", "string.h", "tgmath.h", "threads.h", true)
196 .Cases("time.h", "uchar.h", "wchar.h", "wctype.h", true)
198 // C++ headers for C library facilities
199 .Cases("cassert", "ccomplex", "cctype", "cerrno", "cfenv", true)
200 .Cases("cfloat", "cinttypes", "ciso646", "climits", "clocale", true)
201 .Cases("cmath", "csetjmp", "csignal", "cstdalign", "cstdarg", true)
202 .Cases("cstdbool", "cstddef", "cstdint", "cstdio", "cstdlib", true)
203 .Cases("cstring", "ctgmath", "ctime", "cuchar", "cwchar", true)
204 .Case("cwctype", true)
206 // C++ library headers
207 .Cases("algorithm", "fstream", "list", "regex", "thread", true)
208 .Cases("array", "functional", "locale", "scoped_allocator", "tuple", true)
209 .Cases("atomic", "future", "map", "set", "type_traits", true)
210 .Cases("bitset", "initializer_list", "memory", "shared_mutex", "typeindex", true)
211 .Cases("chrono", "iomanip", "mutex", "sstream", "typeinfo", true)
212 .Cases("codecvt", "ios", "new", "stack", "unordered_map", true)
213 .Cases("complex", "iosfwd", "numeric", "stdexcept", "unordered_set", true)
214 .Cases("condition_variable", "iostream", "ostream", "streambuf", "utility", true)
215 .Cases("deque", "istream", "queue", "string", "valarray", true)
216 .Cases("exception", "iterator", "random", "strstream", "vector", true)
217 .Cases("forward_list", "limits", "ratio", "system_error", true)
219 // POSIX headers (which aren't also C headers)
220 .Cases("aio.h", "arpa/inet.h", "cpio.h", "dirent.h", "dlfcn.h", true)
221 .Cases("fcntl.h", "fmtmsg.h", "fnmatch.h", "ftw.h", "glob.h", true)
222 .Cases("grp.h", "iconv.h", "langinfo.h", "libgen.h", "monetary.h", true)
223 .Cases("mqueue.h", "ndbm.h", "net/if.h", "netdb.h", "netinet/in.h", true)
224 .Cases("netinet/tcp.h", "nl_types.h", "poll.h", "pthread.h", "pwd.h", true)
225 .Cases("regex.h", "sched.h", "search.h", "semaphore.h", "spawn.h", true)
226 .Cases("strings.h", "stropts.h", "sys/ipc.h", "sys/mman.h", "sys/msg.h", true)
227 .Cases("sys/resource.h", "sys/select.h", "sys/sem.h", "sys/shm.h", "sys/socket.h", true)
228 .Cases("sys/stat.h", "sys/statvfs.h", "sys/time.h", "sys/times.h", "sys/types.h", true)
229 .Cases("sys/uio.h", "sys/un.h", "sys/utsname.h", "sys/wait.h", "syslog.h", true)
230 .Cases("tar.h", "termios.h", "trace.h", "ulimit.h", true)
231 .Cases("unistd.h", "utime.h", "utmpx.h", "wordexp.h", true)
235 bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
237 // Missing macro name?
238 if (MacroNameTok.is(tok::eod))
239 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
241 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
243 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
245 if (II->isCPlusPlusOperatorKeyword()) {
246 // C++ 2.5p2: Alternative tokens behave the same as its primary token
247 // except for their spellings.
248 Diag(MacroNameTok, getLangOpts().MicrosoftExt
249 ? diag::ext_pp_operator_used_as_macro_name
250 : diag::err_pp_operator_used_as_macro_name)
251 << II << MacroNameTok.getKind();
252 // Allow #defining |and| and friends for Microsoft compatibility or
253 // recovery when legacy C headers are included in C++.
256 if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
257 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
258 return Diag(MacroNameTok, diag::err_defined_macro_name);
261 if (isDefineUndef == MU_Undef) {
262 auto *MI = getMacroInfo(II);
263 if (MI && MI->isBuiltinMacro()) {
264 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
265 // and C++ [cpp.predefined]p4], but allow it as an extension.
266 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
270 // If defining/undefining reserved identifier or a keyword, we need to issue
272 SourceLocation MacroNameLoc = MacroNameTok.getLocation();
275 if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
276 (SourceMgr.getBufferName(MacroNameLoc) != "<built-in>")) {
277 MacroDiag D = MD_NoWarn;
278 if (isDefineUndef == MU_Define) {
279 D = shouldWarnOnMacroDef(*this, II);
281 else if (isDefineUndef == MU_Undef)
282 D = shouldWarnOnMacroUndef(*this, II);
283 if (D == MD_KeywordDef) {
284 // We do not want to warn on some patterns widely used in configuration
285 // scripts. This requires analyzing next tokens, so do not issue warnings
286 // now, only inform caller.
290 if (D == MD_ReservedMacro)
291 Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
294 // Okay, we got a good identifier.
298 /// Lex and validate a macro name, which occurs after a
299 /// \#define or \#undef.
301 /// This sets the token kind to eod and discards the rest of the macro line if
302 /// the macro name is invalid.
304 /// \param MacroNameTok Token that is expected to be a macro name.
305 /// \param isDefineUndef Context in which macro is used.
306 /// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
307 void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
309 // Read the token, don't allow macro expansion on it.
310 LexUnexpandedToken(MacroNameTok);
312 if (MacroNameTok.is(tok::code_completion)) {
314 CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
315 setCodeCompletionReached();
316 LexUnexpandedToken(MacroNameTok);
319 if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
322 // Invalid macro name, read and discard the rest of the line and set the
323 // token kind to tok::eod if necessary.
324 if (MacroNameTok.isNot(tok::eod)) {
325 MacroNameTok.setKind(tok::eod);
326 DiscardUntilEndOfDirective();
330 /// Ensure that the next token is a tok::eod token.
332 /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
333 /// true, then we consider macros that expand to zero tokens as being ok.
334 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
336 // Lex unexpanded tokens for most directives: macros might expand to zero
337 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
338 // #line) allow empty macros.
342 LexUnexpandedToken(Tmp);
344 // There should be no tokens after the directive, but we allow them as an
346 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
347 LexUnexpandedToken(Tmp);
349 if (Tmp.isNot(tok::eod)) {
350 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
351 // or if this is a macro-style preprocessing directive, because it is more
352 // trouble than it is worth to insert /**/ and check that there is no /**/
353 // in the range also.
355 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
357 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
358 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
359 DiscardUntilEndOfDirective();
363 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
364 /// decided that the subsequent tokens are in the \#if'd out portion of the
365 /// file. Lex the rest of the file, until we see an \#endif. If
366 /// FoundNonSkipPortion is true, then we have already emitted code for part of
367 /// this \#if directive, so \#else/\#elif blocks should never be entered.
368 /// If ElseOk is true, then \#else directives are ok, if not, then we have
369 /// already seen one so a \#else directive is a duplicate. When this returns,
370 /// the caller can lex the first valid token.
371 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation HashTokenLoc,
372 SourceLocation IfTokenLoc,
373 bool FoundNonSkipPortion,
375 SourceLocation ElseLoc) {
377 assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
379 if (PreambleConditionalStack.reachedEOFWhileSkipping())
380 PreambleConditionalStack.clearSkipInfo();
382 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/ false,
383 FoundNonSkipPortion, FoundElse);
385 // Enter raw mode to disable identifier lookup (and thus macro expansion),
386 // disabling warnings, etc.
387 CurPPLexer->LexingRawMode = true;
392 if (Tok.is(tok::code_completion)) {
394 CodeComplete->CodeCompleteInConditionalExclusion();
395 setCodeCompletionReached();
399 // If this is the end of the buffer, we have an error.
400 if (Tok.is(tok::eof)) {
401 // We don't emit errors for unterminated conditionals here,
402 // Lexer::LexEndOfFile can do that properly.
403 // Just return and let the caller lex after this #include.
404 if (PreambleConditionalStack.isRecording())
405 PreambleConditionalStack.SkipInfo.emplace(
406 HashTokenLoc, IfTokenLoc, FoundNonSkipPortion, FoundElse, ElseLoc);
410 // If this token is not a preprocessor directive, just skip it.
411 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
414 // We just parsed a # character at the start of a line, so we're in
415 // directive mode. Tell the lexer this so any newlines we see will be
416 // converted into an EOD token (this terminates the macro).
417 CurPPLexer->ParsingPreprocessorDirective = true;
418 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
421 // Read the next token, the directive flavor.
422 LexUnexpandedToken(Tok);
424 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
425 // something bogus), skip it.
426 if (Tok.isNot(tok::raw_identifier)) {
427 CurPPLexer->ParsingPreprocessorDirective = false;
428 // Restore comment saving mode.
429 if (CurLexer) CurLexer->resetExtendedTokenMode();
433 // If the first letter isn't i or e, it isn't intesting to us. We know that
434 // this is safe in the face of spelling differences, because there is no way
435 // to spell an i/e in a strange way that is another letter. Skipping this
436 // allows us to avoid looking up the identifier info for #define/#undef and
437 // other common directives.
438 StringRef RI = Tok.getRawIdentifier();
440 char FirstChar = RI[0];
441 if (FirstChar >= 'a' && FirstChar <= 'z' &&
442 FirstChar != 'i' && FirstChar != 'e') {
443 CurPPLexer->ParsingPreprocessorDirective = false;
444 // Restore comment saving mode.
445 if (CurLexer) CurLexer->resetExtendedTokenMode();
449 // Get the identifier name without trigraphs or embedded newlines. Note
450 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
452 char DirectiveBuf[20];
454 if (!Tok.needsCleaning() && RI.size() < 20) {
457 std::string DirectiveStr = getSpelling(Tok);
458 size_t IdLen = DirectiveStr.size();
460 CurPPLexer->ParsingPreprocessorDirective = false;
461 // Restore comment saving mode.
462 if (CurLexer) CurLexer->resetExtendedTokenMode();
465 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
466 Directive = StringRef(DirectiveBuf, IdLen);
469 if (Directive.startswith("if")) {
470 StringRef Sub = Directive.substr(2);
471 if (Sub.empty() || // "if"
472 Sub == "def" || // "ifdef"
473 Sub == "ndef") { // "ifndef"
474 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
475 // bother parsing the condition.
476 DiscardUntilEndOfDirective();
477 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
478 /*foundnonskip*/false,
481 } else if (Directive[0] == 'e') {
482 StringRef Sub = Directive.substr(1);
483 if (Sub == "ndif") { // "endif"
484 PPConditionalInfo CondInfo;
485 CondInfo.WasSkipping = true; // Silence bogus warning.
486 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
487 (void)InCond; // Silence warning in no-asserts mode.
488 assert(!InCond && "Can't be skipping if not in a conditional!");
490 // If we popped the outermost skipping block, we're done skipping!
491 if (!CondInfo.WasSkipping) {
492 // Restore the value of LexingRawMode so that trailing comments
493 // are handled correctly, if we've reached the outermost block.
494 CurPPLexer->LexingRawMode = false;
495 CheckEndOfDirective("endif");
496 CurPPLexer->LexingRawMode = true;
498 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
501 DiscardUntilEndOfDirective();
503 } else if (Sub == "lse") { // "else".
504 // #else directive in a skipping conditional. If not in some other
505 // skipping conditional, and if #else hasn't already been seen, enter it
506 // as a non-skipping conditional.
507 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
509 // If this is a #else with a #else before it, report the error.
510 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
512 // Note that we've seen a #else in this conditional.
513 CondInfo.FoundElse = true;
515 // If the conditional is at the top level, and the #if block wasn't
516 // entered, enter the #else block now.
517 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
518 CondInfo.FoundNonSkip = true;
519 // Restore the value of LexingRawMode so that trailing comments
520 // are handled correctly.
521 CurPPLexer->LexingRawMode = false;
522 CheckEndOfDirective("else");
523 CurPPLexer->LexingRawMode = true;
525 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
528 DiscardUntilEndOfDirective(); // C99 6.10p4.
530 } else if (Sub == "lif") { // "elif".
531 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
533 // If this is a #elif with a #else before it, report the error.
534 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
536 // If this is in a skipping block or if we're already handled this #if
537 // block, don't bother parsing the condition.
538 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
539 DiscardUntilEndOfDirective();
541 const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
542 // Restore the value of LexingRawMode so that identifiers are
543 // looked up, etc, inside the #elif expression.
544 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
545 CurPPLexer->LexingRawMode = false;
546 IdentifierInfo *IfNDefMacro = nullptr;
547 const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro).Conditional;
548 CurPPLexer->LexingRawMode = true;
550 const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
551 Callbacks->Elif(Tok.getLocation(),
552 SourceRange(CondBegin, CondEnd),
553 (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
555 // If this condition is true, enter it!
557 CondInfo.FoundNonSkip = true;
564 CurPPLexer->ParsingPreprocessorDirective = false;
565 // Restore comment saving mode.
566 if (CurLexer) CurLexer->resetExtendedTokenMode();
569 // Finally, if we are out of the conditional (saw an #endif or ran off the end
570 // of the file, just stop skipping and return to lexing whatever came after
572 CurPPLexer->LexingRawMode = false;
574 // The last skipped range isn't actually skipped yet if it's truncated
575 // by the end of the preamble; we'll resume parsing after the preamble.
576 if (Callbacks && (Tok.isNot(tok::eof) || !isRecordingPreamble()))
577 Callbacks->SourceRangeSkipped(
578 SourceRange(HashTokenLoc, CurPPLexer->getSourceLocation()),
582 Module *Preprocessor::getModuleForLocation(SourceLocation Loc) {
583 if (!SourceMgr.isInMainFile(Loc)) {
584 // Try to determine the module of the include directive.
585 // FIXME: Look into directly passing the FileEntry from LookupFile instead.
586 FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
587 if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
588 // The include comes from an included file.
589 return HeaderInfo.getModuleMap()
590 .findModuleForHeader(EntryOfIncl)
595 // This is either in the main file or not in a file at all. It belongs
596 // to the current module, if there is one.
597 return getLangOpts().CurrentModule.empty()
599 : HeaderInfo.lookupModule(getLangOpts().CurrentModule);
603 Preprocessor::getModuleHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
605 SourceLocation Loc) {
606 assert(M && "no module to include");
608 // If we have a module import syntax, we shouldn't include a header to
609 // make a particular module visible.
610 if (getLangOpts().ObjC)
613 Module *TopM = M->getTopLevelModule();
614 Module *IncM = getModuleForLocation(IncLoc);
616 // Walk up through the include stack, looking through textual headers of M
617 // until we hit a non-textual header that we can #include. (We assume textual
618 // headers of a module with non-textual headers aren't meant to be used to
619 // import entities from the module.)
620 auto &SM = getSourceManager();
621 while (!Loc.isInvalid() && !SM.isInMainFile(Loc)) {
622 auto ID = SM.getFileID(SM.getExpansionLoc(Loc));
623 auto *FE = SM.getFileEntryForID(ID);
627 bool InTextualHeader = false;
628 for (auto Header : HeaderInfo.getModuleMap().findAllModulesForHeader(FE)) {
629 if (!Header.getModule()->isSubModuleOf(TopM))
632 if (!(Header.getRole() & ModuleMap::TextualHeader)) {
633 // If this is an accessible, non-textual header of M's top-level module
634 // that transitively includes the given location and makes the
635 // corresponding module visible, this is the thing to #include.
636 if (Header.isAccessibleFrom(IncM))
639 // It's in a private header; we can't #include it.
640 // FIXME: If there's a public header in some module that re-exports it,
641 // then we could suggest including that, but it's not clear that's the
642 // expected way to make this entity visible.
646 InTextualHeader = true;
649 if (!InTextualHeader)
652 Loc = SM.getIncludeLoc(ID);
658 const FileEntry *Preprocessor::LookupFile(
659 SourceLocation FilenameLoc, StringRef Filename, bool isAngled,
660 const DirectoryLookup *FromDir, const FileEntry *FromFile,
661 const DirectoryLookup *&CurDir, SmallVectorImpl<char> *SearchPath,
662 SmallVectorImpl<char> *RelativePath,
663 ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, bool SkipCache) {
664 Module *RequestingModule = getModuleForLocation(FilenameLoc);
665 bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc);
667 // If the header lookup mechanism may be relative to the current inclusion
668 // stack, record the parent #includes.
669 SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 16>
671 bool BuildSystemModule = false;
672 if (!FromDir && !FromFile) {
673 FileID FID = getCurrentFileLexer()->getFileID();
674 const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
676 // If there is no file entry associated with this file, it must be the
677 // predefines buffer or the module includes buffer. Any other file is not
678 // lexed with a normal lexer, so it won't be scanned for preprocessor
681 // If we have the predefines buffer, resolve #include references (which come
682 // from the -include command line argument) from the current working
683 // directory instead of relative to the main file.
685 // If we have the module includes buffer, resolve #include references (which
686 // come from header declarations in the module map) relative to the module
689 if (FID == SourceMgr.getMainFileID() && MainFileDir) {
690 Includers.push_back(std::make_pair(nullptr, MainFileDir));
691 BuildSystemModule = getCurrentModule()->IsSystem;
692 } else if ((FileEnt =
693 SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())))
694 Includers.push_back(std::make_pair(FileEnt, FileMgr.getDirectory(".")));
696 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
699 // MSVC searches the current include stack from top to bottom for
700 // headers included by quoted include directives.
701 // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
702 if (LangOpts.MSVCCompat && !isAngled) {
703 for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
704 if (IsFileLexer(ISEntry))
705 if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
706 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
711 CurDir = CurDirLookup;
714 // We're supposed to start looking from after a particular file. Search
715 // the include path until we find that file or run out of files.
716 const DirectoryLookup *TmpCurDir = CurDir;
717 const DirectoryLookup *TmpFromDir = nullptr;
718 while (const FileEntry *FE = HeaderInfo.LookupFile(
719 Filename, FilenameLoc, isAngled, TmpFromDir, TmpCurDir,
720 Includers, SearchPath, RelativePath, RequestingModule,
721 SuggestedModule, /*IsMapped=*/nullptr, SkipCache)) {
722 // Keep looking as if this file did a #include_next.
723 TmpFromDir = TmpCurDir;
725 if (FE == FromFile) {
727 FromDir = TmpFromDir;
734 // Do a standard file entry lookup.
735 const FileEntry *FE = HeaderInfo.LookupFile(
736 Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
737 RelativePath, RequestingModule, SuggestedModule, IsMapped, SkipCache,
740 if (SuggestedModule && !LangOpts.AsmPreprocessor)
741 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
742 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
747 const FileEntry *CurFileEnt;
748 // Otherwise, see if this is a subframework header. If so, this is relative
749 // to one of the headers on the #include stack. Walk the list of the current
750 // headers on the #include stack and pass them to HeaderInfo.
752 if ((CurFileEnt = CurPPLexer->getFileEntry())) {
753 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
754 SearchPath, RelativePath,
757 if (SuggestedModule && !LangOpts.AsmPreprocessor)
758 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
759 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
766 for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
767 if (IsFileLexer(ISEntry)) {
768 if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
769 if ((FE = HeaderInfo.LookupSubframeworkHeader(
770 Filename, CurFileEnt, SearchPath, RelativePath,
771 RequestingModule, SuggestedModule))) {
772 if (SuggestedModule && !LangOpts.AsmPreprocessor)
773 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
774 RequestingModule, RequestingModuleIsModuleInterface,
775 FilenameLoc, Filename, FE);
782 // Otherwise, we really couldn't find the file.
786 //===----------------------------------------------------------------------===//
787 // Preprocessor Directive Handling.
788 //===----------------------------------------------------------------------===//
790 class Preprocessor::ResetMacroExpansionHelper {
792 ResetMacroExpansionHelper(Preprocessor *pp)
793 : PP(pp), save(pp->DisableMacroExpansion) {
794 if (pp->MacroExpansionInDirectivesOverride)
795 pp->DisableMacroExpansion = false;
798 ~ResetMacroExpansionHelper() {
799 PP->DisableMacroExpansion = save;
807 /// Process a directive while looking for the through header or a #pragma
808 /// hdrstop. The following directives are handled:
809 /// #include (to check if it is the through header)
810 /// #define (to warn about macros that don't match the PCH)
811 /// #pragma (to check for pragma hdrstop).
812 /// All other directives are completely discarded.
813 void Preprocessor::HandleSkippedDirectiveWhileUsingPCH(Token &Result,
814 SourceLocation HashLoc) {
815 if (const IdentifierInfo *II = Result.getIdentifierInfo()) {
816 if (II->getPPKeywordID() == tok::pp_define) {
817 return HandleDefineDirective(Result,
818 /*ImmediatelyAfterHeaderGuard=*/false);
820 if (SkippingUntilPCHThroughHeader &&
821 II->getPPKeywordID() == tok::pp_include) {
822 return HandleIncludeDirective(HashLoc, Result);
824 if (SkippingUntilPragmaHdrStop && II->getPPKeywordID() == tok::pp_pragma) {
825 Token P = LookAhead(0);
826 auto *II = P.getIdentifierInfo();
827 if (II && II->getName() == "hdrstop")
828 return HandlePragmaDirective(HashLoc, PIK_HashPragma);
831 DiscardUntilEndOfDirective();
834 /// HandleDirective - This callback is invoked when the lexer sees a # token
835 /// at the start of a line. This consumes the directive, modifies the
836 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
837 /// read is the correct one.
838 void Preprocessor::HandleDirective(Token &Result) {
839 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
841 // We just parsed a # character at the start of a line, so we're in directive
842 // mode. Tell the lexer this so any newlines we see will be converted into an
843 // EOD token (which terminates the directive).
844 CurPPLexer->ParsingPreprocessorDirective = true;
845 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
847 bool ImmediatelyAfterTopLevelIfndef =
848 CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
849 CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
853 // We are about to read a token. For the multiple-include optimization FA to
854 // work, we have to remember if we had read any tokens *before* this
856 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
858 // Save the '#' token in case we need to return it later.
859 Token SavedHash = Result;
861 // Read the next token, the directive flavor. This isn't expanded due to
863 LexUnexpandedToken(Result);
865 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
870 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
871 // not support this for #include-like directives, since that can result in
872 // terrible diagnostics, and does not work in GCC.
874 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
875 switch (II->getPPKeywordID()) {
876 case tok::pp_include:
878 case tok::pp_include_next:
879 case tok::pp___include_macros:
881 Diag(Result, diag::err_embedded_directive) << II->getName();
882 DiscardUntilEndOfDirective();
888 Diag(Result, diag::ext_embedded_directive);
891 // Temporarily enable macro expansion if set so
892 // and reset to previous state when returning from this function.
893 ResetMacroExpansionHelper helper(this);
895 if (SkippingUntilPCHThroughHeader || SkippingUntilPragmaHdrStop)
896 return HandleSkippedDirectiveWhileUsingPCH(Result, SavedHash.getLocation());
898 switch (Result.getKind()) {
900 return; // null directive.
901 case tok::code_completion:
903 CodeComplete->CodeCompleteDirective(
904 CurPPLexer->getConditionalStackDepth() > 0);
905 setCodeCompletionReached();
907 case tok::numeric_constant: // # 7 GNU line marker directive.
908 if (getLangOpts().AsmPreprocessor)
909 break; // # 4 is not a preprocessor directive in .S files.
910 return HandleDigitDirective(Result);
912 IdentifierInfo *II = Result.getIdentifierInfo();
913 if (!II) break; // Not an identifier.
915 // Ask what the preprocessor keyword ID is.
916 switch (II->getPPKeywordID()) {
918 // C99 6.10.1 - Conditional Inclusion.
920 return HandleIfDirective(Result, SavedHash, ReadAnyTokensBeforeDirective);
922 return HandleIfdefDirective(Result, SavedHash, false,
923 true /*not valid for miopt*/);
925 return HandleIfdefDirective(Result, SavedHash, true,
926 ReadAnyTokensBeforeDirective);
928 return HandleElifDirective(Result, SavedHash);
930 return HandleElseDirective(Result, SavedHash);
932 return HandleEndifDirective(Result);
934 // C99 6.10.2 - Source File Inclusion.
935 case tok::pp_include:
937 return HandleIncludeDirective(SavedHash.getLocation(), Result);
938 case tok::pp___include_macros:
940 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
942 // C99 6.10.3 - Macro Replacement.
944 return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
946 return HandleUndefDirective();
948 // C99 6.10.4 - Line Control.
950 return HandleLineDirective();
952 // C99 6.10.5 - Error Directive.
954 return HandleUserDiagnosticDirective(Result, false);
956 // C99 6.10.6 - Pragma Directive.
958 return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
962 return HandleImportDirective(SavedHash.getLocation(), Result);
963 case tok::pp_include_next:
964 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
966 case tok::pp_warning:
967 Diag(Result, diag::ext_pp_warning_directive);
968 return HandleUserDiagnosticDirective(Result, true);
970 return HandleIdentSCCSDirective(Result);
972 return HandleIdentSCCSDirective(Result);
974 //isExtension = true; // FIXME: implement #assert
976 case tok::pp_unassert:
977 //isExtension = true; // FIXME: implement #unassert
980 case tok::pp___public_macro:
981 if (getLangOpts().Modules)
982 return HandleMacroPublicDirective(Result);
985 case tok::pp___private_macro:
986 if (getLangOpts().Modules)
987 return HandleMacroPrivateDirective();
993 // If this is a .S file, treat unknown # directives as non-preprocessor
994 // directives. This is important because # may be a comment or introduce
995 // various pseudo-ops. Just return the # token and push back the following
996 // token to be lexed next time.
997 if (getLangOpts().AsmPreprocessor) {
998 auto Toks = llvm::make_unique<Token[]>(2);
999 // Return the # and the token after it.
1000 Toks[0] = SavedHash;
1003 // If the second token is a hashhash token, then we need to translate it to
1004 // unknown so the token lexer doesn't try to perform token pasting.
1005 if (Result.is(tok::hashhash))
1006 Toks[1].setKind(tok::unknown);
1008 // Enter this token stream so that we re-lex the tokens. Make sure to
1009 // enable macro expansion, in case the token after the # is an identifier
1010 // that is expanded.
1011 EnterTokenStream(std::move(Toks), 2, false);
1015 // If we reached here, the preprocessing token is not valid!
1016 Diag(Result, diag::err_pp_invalid_directive);
1018 // Read the rest of the PP line.
1019 DiscardUntilEndOfDirective();
1021 // Okay, we're done parsing the directive.
1024 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
1025 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
1026 static bool GetLineValue(Token &DigitTok, unsigned &Val,
1027 unsigned DiagID, Preprocessor &PP,
1028 bool IsGNULineDirective=false) {
1029 if (DigitTok.isNot(tok::numeric_constant)) {
1030 PP.Diag(DigitTok, DiagID);
1032 if (DigitTok.isNot(tok::eod))
1033 PP.DiscardUntilEndOfDirective();
1037 SmallString<64> IntegerBuffer;
1038 IntegerBuffer.resize(DigitTok.getLength());
1039 const char *DigitTokBegin = &IntegerBuffer[0];
1040 bool Invalid = false;
1041 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
1045 // Verify that we have a simple digit-sequence, and compute the value. This
1046 // is always a simple digit string computed in decimal, so we do this manually
1049 for (unsigned i = 0; i != ActualLength; ++i) {
1050 // C++1y [lex.fcon]p1:
1051 // Optional separating single quotes in a digit-sequence are ignored
1052 if (DigitTokBegin[i] == '\'')
1055 if (!isDigit(DigitTokBegin[i])) {
1056 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
1057 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
1058 PP.DiscardUntilEndOfDirective();
1062 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
1063 if (NextVal < Val) { // overflow.
1064 PP.Diag(DigitTok, DiagID);
1065 PP.DiscardUntilEndOfDirective();
1071 if (DigitTokBegin[0] == '0' && Val)
1072 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
1073 << IsGNULineDirective;
1078 /// Handle a \#line directive: C99 6.10.4.
1080 /// The two acceptable forms are:
1082 /// # line digit-sequence
1083 /// # line digit-sequence "s-char-sequence"
1085 void Preprocessor::HandleLineDirective() {
1086 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
1091 // Validate the number and convert it to an unsigned.
1093 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
1097 Diag(DigitTok, diag::ext_pp_line_zero);
1099 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1100 // number greater than 2147483647". C90 requires that the line # be <= 32767.
1101 unsigned LineLimit = 32768U;
1102 if (LangOpts.C99 || LangOpts.CPlusPlus11)
1103 LineLimit = 2147483648U;
1104 if (LineNo >= LineLimit)
1105 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
1106 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
1107 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
1109 int FilenameID = -1;
1113 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1114 // string followed by eod.
1115 if (StrTok.is(tok::eod))
1117 else if (StrTok.isNot(tok::string_literal)) {
1118 Diag(StrTok, diag::err_pp_line_invalid_filename);
1119 return DiscardUntilEndOfDirective();
1120 } else if (StrTok.hasUDSuffix()) {
1121 Diag(StrTok, diag::err_invalid_string_udl);
1122 return DiscardUntilEndOfDirective();
1124 // Parse and validate the string, converting it into a unique ID.
1125 StringLiteralParser Literal(StrTok, *this);
1126 assert(Literal.isAscii() && "Didn't allow wide strings in");
1127 if (Literal.hadError)
1128 return DiscardUntilEndOfDirective();
1129 if (Literal.Pascal) {
1130 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1131 return DiscardUntilEndOfDirective();
1133 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1135 // Verify that there is nothing after the string, other than EOD. Because
1136 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1137 CheckEndOfDirective("line", true);
1140 // Take the file kind of the file containing the #line directive. #line
1141 // directives are often used for generated sources from the same codebase, so
1142 // the new file should generally be classified the same way as the current
1143 // file. This is visible in GCC's pre-processed output, which rewrites #line
1144 // to GNU line markers.
1145 SrcMgr::CharacteristicKind FileKind =
1146 SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1148 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, false,
1152 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1153 PPCallbacks::RenameFile, FileKind);
1156 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1157 /// marker directive.
1158 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1159 SrcMgr::CharacteristicKind &FileKind,
1164 if (FlagTok.is(tok::eod)) return false;
1165 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1172 if (FlagTok.is(tok::eod)) return false;
1173 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1175 } else if (FlagVal == 2) {
1178 SourceManager &SM = PP.getSourceManager();
1179 // If we are leaving the current presumed file, check to make sure the
1180 // presumed include stack isn't empty!
1182 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1183 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1184 if (PLoc.isInvalid())
1187 // If there is no include loc (main file) or if the include loc is in a
1188 // different physical file, then we aren't in a "1" line marker flag region.
1189 SourceLocation IncLoc = PLoc.getIncludeLoc();
1190 if (IncLoc.isInvalid() ||
1191 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
1192 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1193 PP.DiscardUntilEndOfDirective();
1198 if (FlagTok.is(tok::eod)) return false;
1199 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1203 // We must have 3 if there are still flags.
1205 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1206 PP.DiscardUntilEndOfDirective();
1210 FileKind = SrcMgr::C_System;
1213 if (FlagTok.is(tok::eod)) return false;
1214 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1217 // We must have 4 if there is yet another flag.
1219 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1220 PP.DiscardUntilEndOfDirective();
1224 FileKind = SrcMgr::C_ExternCSystem;
1227 if (FlagTok.is(tok::eod)) return false;
1229 // There are no more valid flags here.
1230 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1231 PP.DiscardUntilEndOfDirective();
1235 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1236 /// one of the following forms:
1239 /// # 42 "file" ('1' | '2')?
1240 /// # 42 "file" ('1' | '2')? '3' '4'?
1242 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1243 // Validate the number and convert it to an unsigned. GNU does not have a
1244 // line # limit other than it fit in 32-bits.
1246 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1253 bool IsFileEntry = false, IsFileExit = false;
1254 int FilenameID = -1;
1255 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1257 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1258 // string followed by eod.
1259 if (StrTok.is(tok::eod)) {
1260 // Treat this like "#line NN", which doesn't change file characteristics.
1261 FileKind = SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1262 } else if (StrTok.isNot(tok::string_literal)) {
1263 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1264 return DiscardUntilEndOfDirective();
1265 } else if (StrTok.hasUDSuffix()) {
1266 Diag(StrTok, diag::err_invalid_string_udl);
1267 return DiscardUntilEndOfDirective();
1269 // Parse and validate the string, converting it into a unique ID.
1270 StringLiteralParser Literal(StrTok, *this);
1271 assert(Literal.isAscii() && "Didn't allow wide strings in");
1272 if (Literal.hadError)
1273 return DiscardUntilEndOfDirective();
1274 if (Literal.Pascal) {
1275 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1276 return DiscardUntilEndOfDirective();
1278 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1280 // If a filename was present, read any flags that are present.
1281 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, FileKind, *this))
1285 // Create a line note with this information.
1286 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, IsFileEntry,
1287 IsFileExit, FileKind);
1289 // If the preprocessor has callbacks installed, notify them of the #line
1290 // change. This is used so that the line marker comes out in -E mode for
1293 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1295 Reason = PPCallbacks::EnterFile;
1296 else if (IsFileExit)
1297 Reason = PPCallbacks::ExitFile;
1299 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1303 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1305 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1307 // Read the rest of the line raw. We do this because we don't want macros
1308 // to be expanded and we don't require that the tokens be valid preprocessing
1309 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1310 // collapse multiple consecutive white space between tokens, but this isn't
1311 // specified by the standard.
1312 SmallString<128> Message;
1313 CurLexer->ReadToEndOfLine(&Message);
1315 // Find the first non-whitespace character, so that we can make the
1316 // diagnostic more succinct.
1317 StringRef Msg = StringRef(Message).ltrim(' ');
1320 Diag(Tok, diag::pp_hash_warning) << Msg;
1322 Diag(Tok, diag::err_pp_hash_error) << Msg;
1325 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1327 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1328 // Yes, this directive is an extension.
1329 Diag(Tok, diag::ext_pp_ident_directive);
1331 // Read the string argument.
1335 // If the token kind isn't a string, it's a malformed directive.
1336 if (StrTok.isNot(tok::string_literal) &&
1337 StrTok.isNot(tok::wide_string_literal)) {
1338 Diag(StrTok, diag::err_pp_malformed_ident);
1339 if (StrTok.isNot(tok::eod))
1340 DiscardUntilEndOfDirective();
1344 if (StrTok.hasUDSuffix()) {
1345 Diag(StrTok, diag::err_invalid_string_udl);
1346 return DiscardUntilEndOfDirective();
1349 // Verify that there is nothing after the string, other than EOD.
1350 CheckEndOfDirective("ident");
1353 bool Invalid = false;
1354 std::string Str = getSpelling(StrTok, &Invalid);
1356 Callbacks->Ident(Tok.getLocation(), Str);
1360 /// Handle a #public directive.
1361 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1363 ReadMacroName(MacroNameTok, MU_Undef);
1365 // Error reading macro name? If so, diagnostic already issued.
1366 if (MacroNameTok.is(tok::eod))
1369 // Check to see if this is the last token on the #__public_macro line.
1370 CheckEndOfDirective("__public_macro");
1372 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1373 // Okay, we finally have a valid identifier to undef.
1374 MacroDirective *MD = getLocalMacroDirective(II);
1376 // If the macro is not defined, this is an error.
1378 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1382 // Note that this macro has now been exported.
1383 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1384 MacroNameTok.getLocation(), /*IsPublic=*/true));
1387 /// Handle a #private directive.
1388 void Preprocessor::HandleMacroPrivateDirective() {
1390 ReadMacroName(MacroNameTok, MU_Undef);
1392 // Error reading macro name? If so, diagnostic already issued.
1393 if (MacroNameTok.is(tok::eod))
1396 // Check to see if this is the last token on the #__private_macro line.
1397 CheckEndOfDirective("__private_macro");
1399 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1400 // Okay, we finally have a valid identifier to undef.
1401 MacroDirective *MD = getLocalMacroDirective(II);
1403 // If the macro is not defined, this is an error.
1405 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1409 // Note that this macro has now been marked private.
1410 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1411 MacroNameTok.getLocation(), /*IsPublic=*/false));
1414 //===----------------------------------------------------------------------===//
1415 // Preprocessor Include Directive Handling.
1416 //===----------------------------------------------------------------------===//
1418 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1419 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1420 /// true if the input filename was in <>'s or false if it were in ""'s. The
1421 /// caller is expected to provide a buffer that is large enough to hold the
1422 /// spelling of the filename, but is also expected to handle the case when
1423 /// this method decides to use a different buffer.
1424 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1425 StringRef &Buffer) {
1426 // Get the text form of the filename.
1427 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1429 // Make sure the filename is <x> or "x".
1431 if (Buffer[0] == '<') {
1432 if (Buffer.back() != '>') {
1433 Diag(Loc, diag::err_pp_expects_filename);
1434 Buffer = StringRef();
1438 } else if (Buffer[0] == '"') {
1439 if (Buffer.back() != '"') {
1440 Diag(Loc, diag::err_pp_expects_filename);
1441 Buffer = StringRef();
1446 Diag(Loc, diag::err_pp_expects_filename);
1447 Buffer = StringRef();
1451 // Diagnose #include "" as invalid.
1452 if (Buffer.size() <= 2) {
1453 Diag(Loc, diag::err_pp_empty_filename);
1454 Buffer = StringRef();
1458 // Skip the brackets.
1459 Buffer = Buffer.substr(1, Buffer.size()-2);
1463 // Handle cases where the \#include name is expanded from a macro
1464 // as multiple tokens, which need to be glued together.
1466 // This occurs for code like:
1468 // \#define FOO <a/b.h>
1471 // because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1473 // This code concatenates and consumes tokens up to the '>' token. It returns
1474 // false if the > was found, otherwise it returns true if it finds and consumes
1476 bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
1477 SourceLocation &End) {
1481 while (CurTok.isNot(tok::eod)) {
1482 End = CurTok.getLocation();
1484 // FIXME: Provide code completion for #includes.
1485 if (CurTok.is(tok::code_completion)) {
1486 setCodeCompletionReached();
1491 // Append the spelling of this token to the buffer. If there was a space
1492 // before it, add it now.
1493 if (CurTok.hasLeadingSpace())
1494 FilenameBuffer.push_back(' ');
1496 // Get the spelling of the token, directly into FilenameBuffer if possible.
1497 size_t PreAppendSize = FilenameBuffer.size();
1498 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1500 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1501 unsigned ActualLen = getSpelling(CurTok, BufPtr);
1503 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1504 if (BufPtr != &FilenameBuffer[PreAppendSize])
1505 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1507 // Resize FilenameBuffer to the correct size.
1508 if (CurTok.getLength() != ActualLen)
1509 FilenameBuffer.resize(PreAppendSize+ActualLen);
1511 // If we found the '>' marker, return success.
1512 if (CurTok.is(tok::greater))
1518 // If we hit the eod marker, emit an error and return true so that the caller
1519 // knows the EOD has been read.
1520 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1524 /// Push a token onto the token stream containing an annotation.
1525 void Preprocessor::EnterAnnotationToken(SourceRange Range,
1526 tok::TokenKind Kind,
1527 void *AnnotationVal) {
1528 // FIXME: Produce this as the current token directly, rather than
1529 // allocating a new token for it.
1530 auto Tok = llvm::make_unique<Token[]>(1);
1531 Tok[0].startToken();
1532 Tok[0].setKind(Kind);
1533 Tok[0].setLocation(Range.getBegin());
1534 Tok[0].setAnnotationEndLoc(Range.getEnd());
1535 Tok[0].setAnnotationValue(AnnotationVal);
1536 EnterTokenStream(std::move(Tok), 1, true);
1539 /// Produce a diagnostic informing the user that a #include or similar
1540 /// was implicitly treated as a module import.
1541 static void diagnoseAutoModuleImport(
1542 Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
1543 ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
1544 SourceLocation PathEnd) {
1545 assert(PP.getLangOpts().ObjC && "no import syntax available");
1547 SmallString<128> PathString;
1548 for (size_t I = 0, N = Path.size(); I != N; ++I) {
1551 PathString += Path[I].first->getName();
1553 int IncludeKind = 0;
1555 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1556 case tok::pp_include:
1560 case tok::pp_import:
1564 case tok::pp_include_next:
1568 case tok::pp___include_macros:
1573 llvm_unreachable("unknown include directive kind");
1576 CharSourceRange ReplaceRange(SourceRange(HashLoc, PathEnd),
1577 /*IsTokenRange=*/false);
1578 PP.Diag(HashLoc, diag::warn_auto_module_import)
1579 << IncludeKind << PathString
1580 << FixItHint::CreateReplacement(ReplaceRange,
1581 ("@import " + PathString + ";").str());
1584 // Given a vector of path components and a string containing the real
1585 // path to the file, build a properly-cased replacement in the vector,
1586 // and return true if the replacement should be suggested.
1587 static bool trySimplifyPath(SmallVectorImpl<StringRef> &Components,
1588 StringRef RealPathName) {
1589 auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName);
1590 auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName);
1592 bool SuggestReplacement = false;
1593 // Below is a best-effort to handle ".." in paths. It is admittedly
1594 // not 100% correct in the presence of symlinks.
1595 for (auto &Component : llvm::reverse(Components)) {
1596 if ("." == Component) {
1597 } else if (".." == Component) {
1601 } else if (RealPathComponentIter != RealPathComponentEnd) {
1602 if (Component != *RealPathComponentIter) {
1603 // If these path components differ by more than just case, then we
1604 // may be looking at symlinked paths. Bail on this diagnostic to avoid
1605 // noisy false positives.
1606 SuggestReplacement = RealPathComponentIter->equals_lower(Component);
1607 if (!SuggestReplacement)
1609 Component = *RealPathComponentIter;
1611 ++RealPathComponentIter;
1614 return SuggestReplacement;
1617 bool Preprocessor::checkModuleIsAvailable(const LangOptions &LangOpts,
1618 const TargetInfo &TargetInfo,
1619 DiagnosticsEngine &Diags, Module *M) {
1620 Module::Requirement Requirement;
1621 Module::UnresolvedHeaderDirective MissingHeader;
1622 Module *ShadowingModule = nullptr;
1623 if (M->isAvailable(LangOpts, TargetInfo, Requirement, MissingHeader,
1627 if (MissingHeader.FileNameLoc.isValid()) {
1628 Diags.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
1629 << MissingHeader.IsUmbrella << MissingHeader.FileName;
1630 } else if (ShadowingModule) {
1631 Diags.Report(M->DefinitionLoc, diag::err_module_shadowed) << M->Name;
1632 Diags.Report(ShadowingModule->DefinitionLoc,
1633 diag::note_previous_definition);
1635 // FIXME: Track the location at which the requirement was specified, and
1637 Diags.Report(M->DefinitionLoc, diag::err_module_unavailable)
1638 << M->getFullModuleName() << Requirement.second << Requirement.first;
1643 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1644 /// the file to be included from the lexer, then include it! This is a common
1645 /// routine with functionality shared between \#include, \#include_next and
1646 /// \#import. LookupFrom is set when this is a \#include_next directive, it
1647 /// specifies the file to start searching from.
1648 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1650 const DirectoryLookup *LookupFrom,
1651 const FileEntry *LookupFromFile,
1654 CurPPLexer->LexIncludeFilename(FilenameTok);
1656 // Reserve a buffer to get the spelling.
1657 SmallString<128> FilenameBuffer;
1660 SourceLocation CharEnd; // the end of this directive, in characters
1662 switch (FilenameTok.getKind()) {
1664 // If the token kind is EOD, the error has already been diagnosed.
1667 case tok::angle_string_literal:
1668 case tok::string_literal:
1669 Filename = getSpelling(FilenameTok, FilenameBuffer);
1670 End = FilenameTok.getLocation();
1671 CharEnd = End.getLocWithOffset(FilenameTok.getLength());
1675 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1676 // case, glue the tokens together into FilenameBuffer and interpret those.
1677 FilenameBuffer.push_back('<');
1678 if (ConcatenateIncludeName(FilenameBuffer, End))
1679 return; // Found <eod> but no ">"? Diagnostic already emitted.
1680 Filename = FilenameBuffer;
1681 CharEnd = End.getLocWithOffset(1);
1684 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1685 DiscardUntilEndOfDirective();
1689 CharSourceRange FilenameRange
1690 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1691 StringRef OriginalFilename = Filename;
1693 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1694 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1696 if (Filename.empty()) {
1697 DiscardUntilEndOfDirective();
1701 // Verify that there is nothing after the filename, other than EOD. Note that
1702 // we allow macros that expand to nothing after the filename, because this
1703 // falls into the category of "#include pp-tokens new-line" specified in
1705 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1707 // Check that we don't have infinite #include recursion.
1708 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1709 Diag(FilenameTok, diag::err_pp_include_too_deep);
1710 HasReachedMaxIncludeDepth = true;
1714 // Complain about attempts to #include files in an audit pragma.
1715 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1716 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1717 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1719 // Immediately leave the pragma.
1720 PragmaARCCFCodeAuditedLoc = SourceLocation();
1723 // Complain about attempts to #include files in an assume-nonnull pragma.
1724 if (PragmaAssumeNonNullLoc.isValid()) {
1725 Diag(HashLoc, diag::err_pp_include_in_assume_nonnull);
1726 Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
1728 // Immediately leave the pragma.
1729 PragmaAssumeNonNullLoc = SourceLocation();
1732 if (HeaderInfo.HasIncludeAliasMap()) {
1733 // Map the filename with the brackets still attached. If the name doesn't
1734 // map to anything, fall back on the filename we've already gotten the
1736 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1737 if (!NewName.empty())
1741 // Search include directories.
1742 bool IsMapped = false;
1743 const DirectoryLookup *CurDir;
1744 SmallString<1024> SearchPath;
1745 SmallString<1024> RelativePath;
1746 // We get the raw path only if we have 'Callbacks' to which we later pass
1748 ModuleMap::KnownHeader SuggestedModule;
1749 SourceLocation FilenameLoc = FilenameTok.getLocation();
1750 SmallString<128> NormalizedPath;
1751 if (LangOpts.MSVCCompat) {
1752 NormalizedPath = Filename.str();
1754 llvm::sys::path::native(NormalizedPath);
1757 const FileEntry *File = LookupFile(
1758 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
1759 isAngled, LookupFrom, LookupFromFile, CurDir,
1760 Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
1761 &SuggestedModule, &IsMapped);
1765 // Give the clients a chance to recover.
1766 SmallString<128> RecoveryPath;
1767 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1768 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1769 // Add the recovery path to the list of search paths.
1770 DirectoryLookup DL(DE, SrcMgr::C_User, false);
1771 HeaderInfo.AddSearchPath(DL, isAngled);
1773 // Try the lookup again, skipping the cache.
1776 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1777 LookupFrom, LookupFromFile, CurDir, nullptr, nullptr,
1778 &SuggestedModule, &IsMapped, /*SkipCache*/ true);
1783 if (!SuppressIncludeNotFoundError) {
1784 // If the file could not be located and it was included via angle
1785 // brackets, we can attempt a lookup as though it were a quoted path to
1786 // provide the user with a possible fixit.
1790 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, false,
1791 LookupFrom, LookupFromFile, CurDir,
1792 Callbacks ? &SearchPath : nullptr,
1793 Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped);
1795 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1796 Diag(FilenameTok, diag::err_pp_file_not_found_angled_include_not_fatal) <<
1798 FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1802 // Check for likely typos due to leading or trailing non-isAlphanumeric
1804 StringRef OriginalFilename = Filename;
1805 if (LangOpts.SpellChecking && !File) {
1806 // A heuristic to correct a typo file name by removing leading and
1807 // trailing non-isAlphanumeric characters.
1808 auto CorrectTypoFilename = [](llvm::StringRef Filename) {
1809 Filename = Filename.drop_until(isAlphanumeric);
1810 while (!Filename.empty() && !isAlphanumeric(Filename.back())) {
1811 Filename = Filename.drop_back();
1815 StringRef TypoCorrectionName = CorrectTypoFilename(Filename);
1816 SmallString<128> NormalizedTypoCorrectionPath;
1817 if (LangOpts.MSVCCompat) {
1818 NormalizedTypoCorrectionPath = TypoCorrectionName.str();
1820 llvm::sys::path::native(NormalizedTypoCorrectionPath);
1825 LangOpts.MSVCCompat ? NormalizedTypoCorrectionPath.c_str()
1826 : TypoCorrectionName,
1827 isAngled, LookupFrom, LookupFromFile, CurDir,
1828 Callbacks ? &SearchPath : nullptr,
1829 Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped);
1831 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1832 auto Hint = isAngled
1833 ? FixItHint::CreateReplacement(
1834 Range, "<" + TypoCorrectionName.str() + ">")
1835 : FixItHint::CreateReplacement(
1836 Range, "\"" + TypoCorrectionName.str() + "\"");
1837 Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
1838 << OriginalFilename << TypoCorrectionName << Hint;
1839 // We found the file, so set the Filename to the name after typo
1841 Filename = TypoCorrectionName;
1845 // If the file is still not found, just go with the vanilla diagnostic
1847 Diag(FilenameTok, diag::err_pp_file_not_found) << OriginalFilename
1852 if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) {
1853 if (isPCHThroughHeader(File))
1854 SkippingUntilPCHThroughHeader = false;
1858 // Should we enter the source file? Set to false if either the source file is
1859 // known to have no effect beyond its effect on module visibility -- that is,
1860 // if it's got an include guard that is already defined or is a modular header
1861 // we've imported or already built.
1862 bool ShouldEnter = true;
1864 if (PPOpts->SingleFileParseMode)
1865 ShouldEnter = false;
1867 // If we've reached the max allowed include depth, it is usually due to an
1868 // include cycle. Don't enter already processed files again as it can lead to
1869 // reaching the max allowed include depth again.
1870 if (ShouldEnter && HasReachedMaxIncludeDepth && File &&
1871 HeaderInfo.getFileInfo(File).NumIncludes)
1872 ShouldEnter = false;
1874 // Determine whether we should try to import the module for this #include, if
1875 // there is one. Don't do so if precompiled module support is disabled or we
1876 // are processing this module textually (because we're building the module).
1877 if (ShouldEnter && File && SuggestedModule && getLangOpts().Modules &&
1878 !isForModuleBuilding(SuggestedModule.getModule(),
1879 getLangOpts().CurrentModule,
1880 getLangOpts().ModuleName)) {
1881 // If this include corresponds to a module but that module is
1882 // unavailable, diagnose the situation and bail out.
1883 // FIXME: Remove this; loadModule does the same check (but produces
1884 // slightly worse diagnostics).
1885 if (checkModuleIsAvailable(getLangOpts(), getTargetInfo(), getDiagnostics(),
1886 SuggestedModule.getModule())) {
1887 Diag(FilenameTok.getLocation(),
1888 diag::note_implicit_top_level_module_import_here)
1889 << SuggestedModule.getModule()->getTopLevelModuleName();
1893 // Compute the module access path corresponding to this module.
1894 // FIXME: Should we have a second loadModule() overload to avoid this
1895 // extra lookup step?
1896 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1897 for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
1898 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1899 FilenameTok.getLocation()));
1900 std::reverse(Path.begin(), Path.end());
1902 // Warn that we're replacing the include/import with a module import.
1903 // We only do this in Objective-C, where we have a module-import syntax.
1904 if (getLangOpts().ObjC)
1905 diagnoseAutoModuleImport(*this, HashLoc, IncludeTok, Path, CharEnd);
1907 // Load the module to import its macros. We'll make the declarations
1908 // visible when the parser gets here.
1909 // FIXME: Pass SuggestedModule in here rather than converting it to a path
1910 // and making the module loader convert it back again.
1911 ModuleLoadResult Imported = TheModuleLoader.loadModule(
1912 IncludeTok.getLocation(), Path, Module::Hidden,
1913 /*IsIncludeDirective=*/true);
1914 assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
1915 "the imported module is different than the suggested one");
1918 ShouldEnter = false;
1919 else if (Imported.isMissingExpected()) {
1920 // We failed to find a submodule that we assumed would exist (because it
1921 // was in the directory of an umbrella header, for instance), but no
1922 // actual module containing it exists (because the umbrella header is
1923 // incomplete). Treat this as a textual inclusion.
1924 SuggestedModule = ModuleMap::KnownHeader();
1925 } else if (Imported.isConfigMismatch()) {
1926 // On a configuration mismatch, enter the header textually. We still know
1927 // that it's part of the corresponding module.
1929 // We hit an error processing the import. Bail out.
1930 if (hadModuleLoaderFatalFailure()) {
1931 // With a fatal failure in the module loader, we abort parsing.
1932 Token &Result = IncludeTok;
1933 assert(CurLexer && "#include but no current lexer set!");
1934 Result.startToken();
1935 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1936 CurLexer->cutOffLexing();
1942 // The #included file will be considered to be a system header if either it is
1943 // in a system include directory, or if the #includer is a system include
1945 SrcMgr::CharacteristicKind FileCharacter =
1946 SourceMgr.getFileCharacteristic(FilenameTok.getLocation());
1948 FileCharacter = std::max(HeaderInfo.getFileDirFlavor(File), FileCharacter);
1950 // Ask HeaderInfo if we should enter this #include file. If not, #including
1951 // this file will have no effect.
1952 bool SkipHeader = false;
1953 if (ShouldEnter && File &&
1954 !HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport,
1955 getLangOpts().Modules,
1956 SuggestedModule.getModule())) {
1957 ShouldEnter = false;
1962 // Notify the callback object that we've seen an inclusion directive.
1963 Callbacks->InclusionDirective(
1964 HashLoc, IncludeTok,
1965 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1966 FilenameRange, File, SearchPath, RelativePath,
1967 ShouldEnter ? nullptr : SuggestedModule.getModule(), FileCharacter);
1968 if (SkipHeader && !SuggestedModule.getModule())
1969 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1975 // FIXME: If we have a suggested module, and we've already visited this file,
1976 // don't bother entering it again. We know it has no further effect.
1978 // Issue a diagnostic if the name of the file on disk has a different case
1979 // than the one we're about to open.
1980 const bool CheckIncludePathPortability =
1981 !IsMapped && File && !File->tryGetRealPathName().empty();
1983 if (CheckIncludePathPortability) {
1984 StringRef Name = LangOpts.MSVCCompat ? NormalizedPath.str() : Filename;
1985 StringRef RealPathName = File->tryGetRealPathName();
1986 SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name),
1987 llvm::sys::path::end(Name));
1989 if (trySimplifyPath(Components, RealPathName)) {
1990 SmallString<128> Path;
1991 Path.reserve(Name.size()+2);
1992 Path.push_back(isAngled ? '<' : '"');
1993 bool isLeadingSeparator = llvm::sys::path::is_absolute(Name);
1994 for (auto Component : Components) {
1995 if (isLeadingSeparator)
1996 isLeadingSeparator = false;
1998 Path.append(Component);
1999 // Append the separator the user used, or the close quote
2001 Path.size() <= Filename.size() ? Filename[Path.size()-1] :
2002 (isAngled ? '>' : '"'));
2004 // For user files and known standard headers, by default we issue a diagnostic.
2005 // For other system headers, we don't. They can be controlled separately.
2006 auto DiagId = (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name)) ?
2007 diag::pp_nonportable_path : diag::pp_nonportable_system_path;
2008 SourceRange Range(FilenameTok.getLocation(), CharEnd);
2009 Diag(FilenameTok, DiagId) << Path <<
2010 FixItHint::CreateReplacement(Range, Path);
2014 // If we don't need to enter the file, stop now.
2016 // If this is a module import, make it visible if needed.
2017 if (auto *M = SuggestedModule.getModule()) {
2018 // When building a pch, -fmodule-name tells the compiler to textually
2019 // include headers in the specified module. But it is possible that
2020 // ShouldEnter is false because we are skipping the header. In that
2021 // case, We are not importing the specified module.
2022 if (SkipHeader && getLangOpts().CompilingPCH &&
2023 isForModuleBuilding(M, getLangOpts().CurrentModule,
2024 getLangOpts().ModuleName))
2027 makeModuleVisible(M, HashLoc);
2029 if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
2030 tok::pp___include_macros)
2031 EnterAnnotationToken(SourceRange(HashLoc, End),
2032 tok::annot_module_include, M);
2037 // Look up the file, create a File ID for it.
2038 SourceLocation IncludePos = End;
2039 // If the filename string was the result of macro expansions, set the include
2040 // position on the file where it will be included and after the expansions.
2041 if (IncludePos.isMacroID())
2042 IncludePos = SourceMgr.getExpansionRange(IncludePos).getEnd();
2043 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
2044 assert(FID.isValid() && "Expected valid file ID");
2046 // If all is good, enter the new file!
2047 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
2050 // Determine if we're switching to building a new submodule, and which one.
2051 if (auto *M = SuggestedModule.getModule()) {
2052 if (M->getTopLevelModule()->ShadowingModule) {
2053 // We are building a submodule that belongs to a shadowed module. This
2054 // means we find header files in the shadowed module.
2055 Diag(M->DefinitionLoc, diag::err_module_build_shadowed_submodule)
2056 << M->getFullModuleName();
2057 Diag(M->getTopLevelModule()->ShadowingModule->DefinitionLoc,
2058 diag::note_previous_definition);
2061 // When building a pch, -fmodule-name tells the compiler to textually
2062 // include headers in the specified module. We are not building the
2063 // specified module.
2064 if (getLangOpts().CompilingPCH &&
2065 isForModuleBuilding(M, getLangOpts().CurrentModule,
2066 getLangOpts().ModuleName))
2069 assert(!CurLexerSubmodule && "should not have marked this as a module yet");
2070 CurLexerSubmodule = M;
2072 // Let the macro handling code know that any future macros are within
2073 // the new submodule.
2074 EnterSubmodule(M, HashLoc, /*ForPragma*/false);
2076 // Let the parser know that any future declarations are within the new
2078 // FIXME: There's no point doing this if we're handling a #__include_macros
2080 EnterAnnotationToken(SourceRange(HashLoc, End), tok::annot_module_begin, M);
2084 /// HandleIncludeNextDirective - Implements \#include_next.
2086 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
2087 Token &IncludeNextTok) {
2088 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
2090 // #include_next is like #include, except that we start searching after
2091 // the current found directory. If we can't do this, issue a
2093 const DirectoryLookup *Lookup = CurDirLookup;
2094 const FileEntry *LookupFromFile = nullptr;
2095 if (isInPrimaryFile() && LangOpts.IsHeaderFile) {
2096 // If the main file is a header, then it's either for PCH/AST generation,
2097 // or libclang opened it. Either way, handle it as a normal include below
2098 // and do not complain about include_next.
2099 } else if (isInPrimaryFile()) {
2101 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
2102 } else if (CurLexerSubmodule) {
2103 // Start looking up in the directory *after* the one in which the current
2104 // file would be found, if any.
2105 assert(CurPPLexer && "#include_next directive in macro?");
2106 LookupFromFile = CurPPLexer->getFileEntry();
2108 } else if (!Lookup) {
2109 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
2111 // Start looking up in the next directory.
2115 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
2119 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
2120 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
2121 // The Microsoft #import directive takes a type library and generates header
2122 // files from it, and includes those. This is beyond the scope of what clang
2123 // does, so we ignore it and error out. However, #import can optionally have
2124 // trailing attributes that span multiple lines. We're going to eat those
2125 // so we can continue processing from there.
2126 Diag(Tok, diag::err_pp_import_directive_ms );
2128 // Read tokens until we get to the end of the directive. Note that the
2129 // directive can be split over multiple lines using the backslash character.
2130 DiscardUntilEndOfDirective();
2133 /// HandleImportDirective - Implements \#import.
2135 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
2137 if (!LangOpts.ObjC) { // #import is standard for ObjC.
2138 if (LangOpts.MSVCCompat)
2139 return HandleMicrosoftImportDirective(ImportTok);
2140 Diag(ImportTok, diag::ext_pp_import_directive);
2142 return HandleIncludeDirective(HashLoc, ImportTok, nullptr, nullptr, true);
2145 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
2146 /// pseudo directive in the predefines buffer. This handles it by sucking all
2147 /// tokens through the preprocessor and discarding them (only keeping the side
2148 /// effects on the preprocessor).
2149 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
2150 Token &IncludeMacrosTok) {
2151 // This directive should only occur in the predefines buffer. If not, emit an
2152 // error and reject it.
2153 SourceLocation Loc = IncludeMacrosTok.getLocation();
2154 if (SourceMgr.getBufferName(Loc) != "<built-in>") {
2155 Diag(IncludeMacrosTok.getLocation(),
2156 diag::pp_include_macros_out_of_predefines);
2157 DiscardUntilEndOfDirective();
2161 // Treat this as a normal #include for checking purposes. If this is
2162 // successful, it will push a new lexer onto the include stack.
2163 HandleIncludeDirective(HashLoc, IncludeMacrosTok);
2168 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
2169 } while (TmpTok.isNot(tok::hashhash));
2172 //===----------------------------------------------------------------------===//
2173 // Preprocessor Macro Directive Handling.
2174 //===----------------------------------------------------------------------===//
2176 /// ReadMacroParameterList - The ( starting a parameter list of a macro
2177 /// definition has just been read. Lex the rest of the parameters and the
2178 /// closing ), updating MI with what we learn. Return true if an error occurs
2179 /// parsing the param list.
2180 bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
2181 SmallVector<IdentifierInfo*, 32> Parameters;
2184 LexUnexpandedToken(Tok);
2185 switch (Tok.getKind()) {
2187 // Found the end of the parameter list.
2188 if (Parameters.empty()) // #define FOO()
2190 // Otherwise we have #define FOO(A,)
2191 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2193 case tok::ellipsis: // #define X(... -> C99 varargs
2195 Diag(Tok, LangOpts.CPlusPlus11 ?
2196 diag::warn_cxx98_compat_variadic_macro :
2197 diag::ext_variadic_macro);
2199 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
2200 if (LangOpts.OpenCL) {
2201 Diag(Tok, diag::err_pp_opencl_variadic_macros);
2205 // Lex the token after the identifier.
2206 LexUnexpandedToken(Tok);
2207 if (Tok.isNot(tok::r_paren)) {
2208 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2211 // Add the __VA_ARGS__ identifier as a parameter.
2212 Parameters.push_back(Ident__VA_ARGS__);
2213 MI->setIsC99Varargs();
2214 MI->setParameterList(Parameters, BP);
2216 case tok::eod: // #define X(
2217 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2220 // Handle keywords and identifiers here to accept things like
2221 // #define Foo(for) for.
2222 IdentifierInfo *II = Tok.getIdentifierInfo();
2225 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2229 // If this is already used as a parameter, it is used multiple times (e.g.
2231 if (std::find(Parameters.begin(), Parameters.end(), II) !=
2232 Parameters.end()) { // C99 6.10.3p6
2233 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
2237 // Add the parameter to the macro info.
2238 Parameters.push_back(II);
2240 // Lex the token after the identifier.
2241 LexUnexpandedToken(Tok);
2243 switch (Tok.getKind()) {
2244 default: // #define X(A B
2245 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2247 case tok::r_paren: // #define X(A)
2248 MI->setParameterList(Parameters, BP);
2250 case tok::comma: // #define X(A,
2252 case tok::ellipsis: // #define X(A... -> GCC extension
2253 // Diagnose extension.
2254 Diag(Tok, diag::ext_named_variadic_macro);
2256 // Lex the token after the identifier.
2257 LexUnexpandedToken(Tok);
2258 if (Tok.isNot(tok::r_paren)) {
2259 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2263 MI->setIsGNUVarargs();
2264 MI->setParameterList(Parameters, BP);
2271 static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2272 const LangOptions &LOptions) {
2273 if (MI->getNumTokens() == 1) {
2274 const Token &Value = MI->getReplacementToken(0);
2276 // Macro that is identity, like '#define inline inline' is a valid pattern.
2277 if (MacroName.getKind() == Value.getKind())
2280 // Macro that maps a keyword to the same keyword decorated with leading/
2281 // trailing underscores is a valid pattern:
2282 // #define inline __inline
2283 // #define inline __inline__
2284 // #define inline _inline (in MS compatibility mode)
2285 StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2286 if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2287 if (!II->isKeyword(LOptions))
2289 StringRef ValueText = II->getName();
2290 StringRef TrimmedValue = ValueText;
2291 if (!ValueText.startswith("__")) {
2292 if (ValueText.startswith("_"))
2293 TrimmedValue = TrimmedValue.drop_front(1);
2297 TrimmedValue = TrimmedValue.drop_front(2);
2298 if (TrimmedValue.endswith("__"))
2299 TrimmedValue = TrimmedValue.drop_back(2);
2301 return TrimmedValue.equals(MacroText);
2308 return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2310 MI->getNumTokens() == 0;
2313 // ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
2314 // entire line) of the macro's tokens and adds them to MacroInfo, and while
2315 // doing so performs certain validity checks including (but not limited to):
2316 // - # (stringization) is followed by a macro parameter
2318 // Returns a nullptr if an invalid sequence of tokens is encountered or returns
2319 // a pointer to a MacroInfo object.
2321 MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody(
2322 const Token &MacroNameTok, const bool ImmediatelyAfterHeaderGuard) {
2324 Token LastTok = MacroNameTok;
2325 // Create the new macro.
2326 MacroInfo *const MI = AllocateMacroInfo(MacroNameTok.getLocation());
2329 LexUnexpandedToken(Tok);
2331 // Used to un-poison and then re-poison identifiers of the __VA_ARGS__ ilk
2332 // within their appropriate context.
2333 VariadicMacroScopeGuard VariadicMacroScopeGuard(*this);
2335 // If this is a function-like macro definition, parse the argument list,
2336 // marking each of the identifiers as being used as macro arguments. Also,
2337 // check other constraints on the first token of the macro body.
2338 if (Tok.is(tok::eod)) {
2339 if (ImmediatelyAfterHeaderGuard) {
2340 // Save this macro information since it may part of a header guard.
2341 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2342 MacroNameTok.getLocation());
2344 // If there is no body to this macro, we have no special handling here.
2345 } else if (Tok.hasLeadingSpace()) {
2346 // This is a normal token with leading space. Clear the leading space
2347 // marker on the first token to get proper expansion.
2348 Tok.clearFlag(Token::LeadingSpace);
2349 } else if (Tok.is(tok::l_paren)) {
2350 // This is a function-like macro definition. Read the argument list.
2351 MI->setIsFunctionLike();
2352 if (ReadMacroParameterList(MI, LastTok)) {
2353 // Throw away the rest of the line.
2354 if (CurPPLexer->ParsingPreprocessorDirective)
2355 DiscardUntilEndOfDirective();
2359 // If this is a definition of an ISO C/C++ variadic function-like macro (not
2360 // using the GNU named varargs extension) inform our variadic scope guard
2361 // which un-poisons and re-poisons certain identifiers (e.g. __VA_ARGS__)
2362 // allowed only within the definition of a variadic macro.
2364 if (MI->isC99Varargs()) {
2365 VariadicMacroScopeGuard.enterScope();
2368 // Read the first token after the arg list for down below.
2369 LexUnexpandedToken(Tok);
2370 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
2371 // C99 requires whitespace between the macro definition and the body. Emit
2372 // a diagnostic for something like "#define X+".
2373 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2375 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2376 // first character of a replacement list is not a character required by
2377 // subclause 5.2.1, then there shall be white-space separation between the
2378 // identifier and the replacement list.". 5.2.1 lists this set:
2379 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2380 // is irrelevant here.
2381 bool isInvalid = false;
2382 if (Tok.is(tok::at)) // @ is not in the list above.
2384 else if (Tok.is(tok::unknown)) {
2385 // If we have an unknown token, it is something strange like "`". Since
2386 // all of valid characters would have lexed into a single character
2387 // token of some sort, we know this is not a valid case.
2391 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2393 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2396 if (!Tok.is(tok::eod))
2399 // Read the rest of the macro body.
2400 if (MI->isObjectLike()) {
2401 // Object-like macros are very simple, just read their body.
2402 while (Tok.isNot(tok::eod)) {
2404 MI->AddTokenToBody(Tok);
2405 // Get the next token of the macro.
2406 LexUnexpandedToken(Tok);
2409 // Otherwise, read the body of a function-like macro. While we are at it,
2410 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2411 // parameters in function-like macro expansions.
2413 VAOptDefinitionContext VAOCtx(*this);
2415 while (Tok.isNot(tok::eod)) {
2418 if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
2419 MI->AddTokenToBody(Tok);
2421 if (VAOCtx.isVAOptToken(Tok)) {
2422 // If we're already within a VAOPT, emit an error.
2423 if (VAOCtx.isInVAOpt()) {
2424 Diag(Tok, diag::err_pp_vaopt_nested_use);
2427 // Ensure VAOPT is followed by a '(' .
2428 LexUnexpandedToken(Tok);
2429 if (Tok.isNot(tok::l_paren)) {
2430 Diag(Tok, diag::err_pp_missing_lparen_in_vaopt_use);
2433 MI->AddTokenToBody(Tok);
2434 VAOCtx.sawVAOptFollowedByOpeningParens(Tok.getLocation());
2435 LexUnexpandedToken(Tok);
2436 if (Tok.is(tok::hashhash)) {
2437 Diag(Tok, diag::err_vaopt_paste_at_start);
2441 } else if (VAOCtx.isInVAOpt()) {
2442 if (Tok.is(tok::r_paren)) {
2443 if (VAOCtx.sawClosingParen()) {
2444 const unsigned NumTokens = MI->getNumTokens();
2445 assert(NumTokens >= 3 && "Must have seen at least __VA_OPT__( "
2446 "and a subsequent tok::r_paren");
2447 if (MI->getReplacementToken(NumTokens - 2).is(tok::hashhash)) {
2448 Diag(Tok, diag::err_vaopt_paste_at_end);
2452 } else if (Tok.is(tok::l_paren)) {
2453 VAOCtx.sawOpeningParen(Tok.getLocation());
2456 // Get the next token of the macro.
2457 LexUnexpandedToken(Tok);
2461 // If we're in -traditional mode, then we should ignore stringification
2462 // and token pasting. Mark the tokens as unknown so as not to confuse
2464 if (getLangOpts().TraditionalCPP) {
2465 Tok.setKind(tok::unknown);
2466 MI->AddTokenToBody(Tok);
2468 // Get the next token of the macro.
2469 LexUnexpandedToken(Tok);
2473 if (Tok.is(tok::hashhash)) {
2474 // If we see token pasting, check if it looks like the gcc comma
2475 // pasting extension. We'll use this information to suppress
2476 // diagnostics later on.
2478 // Get the next token of the macro.
2479 LexUnexpandedToken(Tok);
2481 if (Tok.is(tok::eod)) {
2482 MI->AddTokenToBody(LastTok);
2486 unsigned NumTokens = MI->getNumTokens();
2487 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2488 MI->getReplacementToken(NumTokens-1).is(tok::comma))
2489 MI->setHasCommaPasting();
2491 // Things look ok, add the '##' token to the macro.
2492 MI->AddTokenToBody(LastTok);
2496 // Our Token is a stringization operator.
2497 // Get the next token of the macro.
2498 LexUnexpandedToken(Tok);
2500 // Check for a valid macro arg identifier or __VA_OPT__.
2501 if (!VAOCtx.isVAOptToken(Tok) &&
2502 (Tok.getIdentifierInfo() == nullptr ||
2503 MI->getParameterNum(Tok.getIdentifierInfo()) == -1)) {
2505 // If this is assembler-with-cpp mode, we accept random gibberish after
2506 // the '#' because '#' is often a comment character. However, change
2507 // the kind of the token to tok::unknown so that the preprocessor isn't
2509 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
2510 LastTok.setKind(tok::unknown);
2511 MI->AddTokenToBody(LastTok);
2514 Diag(Tok, diag::err_pp_stringize_not_parameter)
2515 << LastTok.is(tok::hashat);
2520 // Things look ok, add the '#' and param name tokens to the macro.
2521 MI->AddTokenToBody(LastTok);
2523 // If the token following '#' is VAOPT, let the next iteration handle it
2524 // and check it for correctness, otherwise add the token and prime the
2525 // loop with the next one.
2526 if (!VAOCtx.isVAOptToken(Tok)) {
2527 MI->AddTokenToBody(Tok);
2530 // Get the next token of the macro.
2531 LexUnexpandedToken(Tok);
2534 if (VAOCtx.isInVAOpt()) {
2535 assert(Tok.is(tok::eod) && "Must be at End Of preprocessing Directive");
2536 Diag(Tok, diag::err_pp_expected_after)
2537 << LastTok.getKind() << tok::r_paren;
2538 Diag(VAOCtx.getUnmatchedOpeningParenLoc(), diag::note_matching) << tok::l_paren;
2542 MI->setDefinitionEndLoc(LastTok.getLocation());
2545 /// HandleDefineDirective - Implements \#define. This consumes the entire macro
2546 /// line then lets the caller lex the next real token.
2547 void Preprocessor::HandleDefineDirective(
2548 Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) {
2552 bool MacroShadowsKeyword;
2553 ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
2555 // Error reading macro name? If so, diagnostic already issued.
2556 if (MacroNameTok.is(tok::eod))
2559 // If we are supposed to keep comments in #defines, reenable comment saving
2561 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
2563 MacroInfo *const MI = ReadOptionalMacroParameterListAndBody(
2564 MacroNameTok, ImmediatelyAfterHeaderGuard);
2568 if (MacroShadowsKeyword &&
2569 !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
2570 Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
2572 // Check that there is no paste (##) operator at the beginning or end of the
2573 // replacement list.
2574 unsigned NumTokens = MI->getNumTokens();
2575 if (NumTokens != 0) {
2576 if (MI->getReplacementToken(0).is(tok::hashhash)) {
2577 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2580 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2581 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2586 // When skipping just warn about macros that do not match.
2587 if (SkippingUntilPCHThroughHeader) {
2588 const MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo());
2589 if (!OtherMI || !MI->isIdenticalTo(*OtherMI, *this,
2590 /*Syntactic=*/LangOpts.MicrosoftExt))
2591 Diag(MI->getDefinitionLoc(), diag::warn_pp_macro_def_mismatch_with_pch)
2592 << MacroNameTok.getIdentifierInfo();
2596 // Finally, if this identifier already had a macro defined for it, verify that
2597 // the macro bodies are identical, and issue diagnostics if they are not.
2598 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
2599 // In Objective-C, ignore attempts to directly redefine the builtin
2600 // definitions of the ownership qualifiers. It's still possible to
2602 auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
2603 return II->isStr("__strong") ||
2604 II->isStr("__weak") ||
2605 II->isStr("__unsafe_unretained") ||
2606 II->isStr("__autoreleasing");
2608 if (getLangOpts().ObjC &&
2609 SourceMgr.getFileID(OtherMI->getDefinitionLoc())
2610 == getPredefinesFileID() &&
2611 isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
2612 // Warn if it changes the tokens.
2613 if ((!getDiagnostics().getSuppressSystemWarnings() ||
2614 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
2615 !MI->isIdenticalTo(*OtherMI, *this,
2616 /*Syntactic=*/LangOpts.MicrosoftExt)) {
2617 Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
2619 assert(!OtherMI->isWarnIfUnused());
2623 // It is very common for system headers to have tons of macro redefinitions
2624 // and for warnings to be disabled in system headers. If this is the case,
2625 // then don't bother calling MacroInfo::isIdenticalTo.
2626 if (!getDiagnostics().getSuppressSystemWarnings() ||
2627 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
2628 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
2629 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2631 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2632 // C++ [cpp.predefined]p4, but allow it as an extension.
2633 if (OtherMI->isBuiltinMacro())
2634 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
2635 // Macros must be identical. This means all tokens and whitespace
2636 // separation must be the same. C99 6.10.3p2.
2637 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
2638 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
2639 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2640 << MacroNameTok.getIdentifierInfo();
2641 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2644 if (OtherMI->isWarnIfUnused())
2645 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
2648 DefMacroDirective *MD =
2649 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
2651 assert(!MI->isUsed());
2652 // If we need warning for not using the macro, add its location in the
2653 // warn-because-unused-macro set. If it gets used it will be removed from set.
2654 if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
2655 !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
2656 MI->setIsWarnIfUnused(true);
2657 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2660 // If the callbacks want to know, tell them about the macro definition.
2662 Callbacks->MacroDefined(MacroNameTok, MD);
2665 /// HandleUndefDirective - Implements \#undef.
2667 void Preprocessor::HandleUndefDirective() {
2671 ReadMacroName(MacroNameTok, MU_Undef);
2673 // Error reading macro name? If so, diagnostic already issued.
2674 if (MacroNameTok.is(tok::eod))
2677 // Check to see if this is the last token on the #undef line.
2678 CheckEndOfDirective("undef");
2680 // Okay, we have a valid identifier to undef.
2681 auto *II = MacroNameTok.getIdentifierInfo();
2682 auto MD = getMacroDefinition(II);
2683 UndefMacroDirective *Undef = nullptr;
2685 // If the macro is not defined, this is a noop undef.
2686 if (const MacroInfo *MI = MD.getMacroInfo()) {
2687 if (!MI->isUsed() && MI->isWarnIfUnused())
2688 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2690 if (MI->isWarnIfUnused())
2691 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2693 Undef = AllocateUndefMacroDirective(MacroNameTok.getLocation());
2696 // If the callbacks want to know, tell them about the macro #undef.
2697 // Note: no matter if the macro was defined or not.
2699 Callbacks->MacroUndefined(MacroNameTok, MD, Undef);
2702 appendMacroDirective(II, Undef);
2705 //===----------------------------------------------------------------------===//
2706 // Preprocessor Conditional Directive Handling.
2707 //===----------------------------------------------------------------------===//
2709 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2710 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2711 /// true if any tokens have been returned or pp-directives activated before this
2712 /// \#ifndef has been lexed.
2714 void Preprocessor::HandleIfdefDirective(Token &Result,
2715 const Token &HashToken,
2717 bool ReadAnyTokensBeforeDirective) {
2719 Token DirectiveTok = Result;
2722 ReadMacroName(MacroNameTok);
2724 // Error reading macro name? If so, diagnostic already issued.
2725 if (MacroNameTok.is(tok::eod)) {
2726 // Skip code until we get to #endif. This helps with recovery by not
2727 // emitting an error when the #endif is reached.
2728 SkipExcludedConditionalBlock(HashToken.getLocation(),
2729 DirectiveTok.getLocation(),
2730 /*Foundnonskip*/ false, /*FoundElse*/ false);
2734 // Check to see if this is the last token on the #if[n]def line.
2735 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2737 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2738 auto MD = getMacroDefinition(MII);
2739 MacroInfo *MI = MD.getMacroInfo();
2741 if (CurPPLexer->getConditionalStackDepth() == 0) {
2742 // If the start of a top-level #ifdef and if the macro is not defined,
2743 // inform MIOpt that this might be the start of a proper include guard.
2744 // Otherwise it is some other form of unknown conditional which we can't
2746 if (!ReadAnyTokensBeforeDirective && !MI) {
2747 assert(isIfndef && "#ifdef shouldn't reach here");
2748 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
2750 CurPPLexer->MIOpt.EnterTopLevelConditional();
2753 // If there is a macro, process it.
2754 if (MI) // Mark it used.
2755 markMacroAsUsed(MI);
2759 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2761 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2764 // Should we include the stuff contained by this directive?
2765 if (PPOpts->SingleFileParseMode && !MI) {
2766 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2767 // the directive blocks.
2768 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2769 /*wasskip*/false, /*foundnonskip*/false,
2770 /*foundelse*/false);
2771 } else if (!MI == isIfndef) {
2772 // Yes, remember that we are inside a conditional, then lex the next token.
2773 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2774 /*wasskip*/false, /*foundnonskip*/true,
2775 /*foundelse*/false);
2777 // No, skip the contents of this block.
2778 SkipExcludedConditionalBlock(HashToken.getLocation(),
2779 DirectiveTok.getLocation(),
2780 /*Foundnonskip*/ false,
2781 /*FoundElse*/ false);
2785 /// HandleIfDirective - Implements the \#if directive.
2787 void Preprocessor::HandleIfDirective(Token &IfToken,
2788 const Token &HashToken,
2789 bool ReadAnyTokensBeforeDirective) {
2792 // Parse and evaluate the conditional expression.
2793 IdentifierInfo *IfNDefMacro = nullptr;
2794 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2795 const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
2796 const bool ConditionalTrue = DER.Conditional;
2797 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2799 // If this condition is equivalent to #ifndef X, and if this is the first
2800 // directive seen, handle it for the multiple-include optimization.
2801 if (CurPPLexer->getConditionalStackDepth() == 0) {
2802 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2803 // FIXME: Pass in the location of the macro name, not the 'if' token.
2804 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
2806 CurPPLexer->MIOpt.EnterTopLevelConditional();
2810 Callbacks->If(IfToken.getLocation(),
2811 SourceRange(ConditionalBegin, ConditionalEnd),
2812 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
2814 // Should we include the stuff contained by this directive?
2815 if (PPOpts->SingleFileParseMode && DER.IncludedUndefinedIds) {
2816 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2817 // the directive blocks.
2818 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2819 /*foundnonskip*/false, /*foundelse*/false);
2820 } else if (ConditionalTrue) {
2821 // Yes, remember that we are inside a conditional, then lex the next token.
2822 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2823 /*foundnonskip*/true, /*foundelse*/false);
2825 // No, skip the contents of this block.
2826 SkipExcludedConditionalBlock(HashToken.getLocation(), IfToken.getLocation(),
2827 /*Foundnonskip*/ false,
2828 /*FoundElse*/ false);
2832 /// HandleEndifDirective - Implements the \#endif directive.
2834 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2837 // Check that this is the whole directive.
2838 CheckEndOfDirective("endif");
2840 PPConditionalInfo CondInfo;
2841 if (CurPPLexer->popConditionalLevel(CondInfo)) {
2842 // No conditionals on the stack: this is an #endif without an #if.
2843 Diag(EndifToken, diag::err_pp_endif_without_if);
2847 // If this the end of a top-level #endif, inform MIOpt.
2848 if (CurPPLexer->getConditionalStackDepth() == 0)
2849 CurPPLexer->MIOpt.ExitTopLevelConditional();
2851 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2852 "This code should only be reachable in the non-skipping case!");
2855 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2858 /// HandleElseDirective - Implements the \#else directive.
2860 void Preprocessor::HandleElseDirective(Token &Result, const Token &HashToken) {
2863 // #else directive in a non-skipping conditional... start skipping.
2864 CheckEndOfDirective("else");
2866 PPConditionalInfo CI;
2867 if (CurPPLexer->popConditionalLevel(CI)) {
2868 Diag(Result, diag::pp_err_else_without_if);
2872 // If this is a top-level #else, inform the MIOpt.
2873 if (CurPPLexer->getConditionalStackDepth() == 0)
2874 CurPPLexer->MIOpt.EnterTopLevelConditional();
2876 // If this is a #else with a #else before it, report the error.
2877 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2880 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2882 if (PPOpts->SingleFileParseMode && !CI.FoundNonSkip) {
2883 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2884 // the directive blocks.
2885 CurPPLexer->pushConditionalLevel(CI.IfLoc, /*wasskip*/false,
2886 /*foundnonskip*/false, /*foundelse*/true);
2890 // Finally, skip the rest of the contents of this block.
2891 SkipExcludedConditionalBlock(HashToken.getLocation(), CI.IfLoc,
2892 /*Foundnonskip*/ true,
2893 /*FoundElse*/ true, Result.getLocation());
2896 /// HandleElifDirective - Implements the \#elif directive.
2898 void Preprocessor::HandleElifDirective(Token &ElifToken,
2899 const Token &HashToken) {
2902 // #elif directive in a non-skipping conditional... start skipping.
2903 // We don't care what the condition is, because we will always skip it (since
2904 // the block immediately before it was included).
2905 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2906 DiscardUntilEndOfDirective();
2907 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2909 PPConditionalInfo CI;
2910 if (CurPPLexer->popConditionalLevel(CI)) {
2911 Diag(ElifToken, diag::pp_err_elif_without_if);
2915 // If this is a top-level #elif, inform the MIOpt.
2916 if (CurPPLexer->getConditionalStackDepth() == 0)
2917 CurPPLexer->MIOpt.EnterTopLevelConditional();
2919 // If this is a #elif with a #else before it, report the error.
2920 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2923 Callbacks->Elif(ElifToken.getLocation(),
2924 SourceRange(ConditionalBegin, ConditionalEnd),
2925 PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
2927 if (PPOpts->SingleFileParseMode && !CI.FoundNonSkip) {
2928 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2929 // the directive blocks.
2930 CurPPLexer->pushConditionalLevel(ElifToken.getLocation(), /*wasskip*/false,
2931 /*foundnonskip*/false, /*foundelse*/false);
2935 // Finally, skip the rest of the contents of this block.
2936 SkipExcludedConditionalBlock(
2937 HashToken.getLocation(), CI.IfLoc, /*Foundnonskip*/ true,
2938 /*FoundElse*/ CI.FoundElse, ElifToken.getLocation());