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 /// \brief Implements # directive processing for the Preprocessor.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Lex/Preprocessor.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Lex/CodeCompletionHandler.h"
19 #include "clang/Lex/HeaderSearch.h"
20 #include "clang/Lex/HeaderSearchOptions.h"
21 #include "clang/Lex/LexDiagnostic.h"
22 #include "clang/Lex/LiteralSupport.h"
23 #include "clang/Lex/MacroInfo.h"
24 #include "clang/Lex/ModuleLoader.h"
25 #include "clang/Lex/Pragma.h"
26 #include "llvm/ADT/APInt.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/ADT/StringSwitch.h"
30 #include "llvm/ADT/iterator_range.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/Path.h"
33 #include "llvm/Support/SaveAndRestore.h"
35 using namespace clang;
37 //===----------------------------------------------------------------------===//
38 // Utility Methods for Preprocessor Directive Handling.
39 //===----------------------------------------------------------------------===//
41 MacroInfo *Preprocessor::AllocateMacroInfo() {
42 MacroInfoChain *MIChain = BP.Allocate<MacroInfoChain>();
43 MIChain->Next = MIChainHead;
44 MIChainHead = MIChain;
48 MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
49 MacroInfo *MI = AllocateMacroInfo();
50 new (MI) MacroInfo(L);
54 MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L,
55 unsigned SubModuleID) {
56 static_assert(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID),
57 "alignment for MacroInfo is less than the ID");
58 DeserializedMacroInfoChain *MIChain =
59 BP.Allocate<DeserializedMacroInfoChain>();
60 MIChain->Next = DeserialMIChainHead;
61 DeserialMIChainHead = MIChain;
63 MacroInfo *MI = &MIChain->MI;
64 new (MI) MacroInfo(L);
65 MI->FromASTFile = true;
66 MI->setOwningModuleID(SubModuleID);
70 DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
72 return new (BP) DefMacroDirective(MI, Loc);
76 Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
77 return new (BP) UndefMacroDirective(UndefLoc);
80 VisibilityMacroDirective *
81 Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
83 return new (BP) VisibilityMacroDirective(Loc, isPublic);
86 /// \brief Read and discard all tokens remaining on the current line until
87 /// the tok::eod token is found.
88 void Preprocessor::DiscardUntilEndOfDirective() {
91 LexUnexpandedToken(Tmp);
92 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
93 } while (Tmp.isNot(tok::eod));
96 /// \brief Enumerates possible cases of #define/#undef a reserved identifier.
98 MD_NoWarn, //> Not a reserved identifier
99 MD_KeywordDef, //> Macro hides keyword, enabled by default
100 MD_ReservedMacro //> #define of #undef reserved id, disabled by default
103 /// \brief Checks if the specified identifier is reserved in the specified
105 /// This function does not check if the identifier is a keyword.
106 static bool isReservedId(StringRef Text, const LangOptions &Lang) {
107 // C++ [macro.names], C11 7.1.3:
108 // All identifiers that begin with an underscore and either an uppercase
109 // letter or another underscore are always reserved for any use.
110 if (Text.size() >= 2 && Text[0] == '_' &&
111 (isUppercase(Text[1]) || Text[1] == '_'))
113 // C++ [global.names]
114 // Each name that contains a double underscore ... is reserved to the
115 // implementation for any use.
116 if (Lang.CPlusPlus) {
117 if (Text.find("__") != StringRef::npos)
123 static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
124 const LangOptions &Lang = PP.getLangOpts();
125 StringRef Text = II->getName();
126 if (isReservedId(Text, Lang))
127 return MD_ReservedMacro;
128 if (II->isKeyword(Lang))
129 return MD_KeywordDef;
130 if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final")))
131 return MD_KeywordDef;
135 static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
136 const LangOptions &Lang = PP.getLangOpts();
137 StringRef Text = II->getName();
138 // Do not warn on keyword undef. It is generally harmless and widely used.
139 if (isReservedId(Text, Lang))
140 return MD_ReservedMacro;
144 // Return true if we want to issue a diagnostic by default if we
145 // encounter this name in a #include with the wrong case. For now,
146 // this includes the standard C and C++ headers, Posix headers,
147 // and Boost headers. Improper case for these #includes is a
148 // potential portability issue.
149 static bool warnByDefaultOnWrongCase(StringRef Include) {
150 // If the first component of the path is "boost", treat this like a standard header
151 // for the purposes of diagnostics.
152 if (::llvm::sys::path::begin(Include)->equals_lower("boost"))
155 // "condition_variable" is the longest standard header name at 18 characters.
156 // If the include file name is longer than that, it can't be a standard header.
157 static const size_t MaxStdHeaderNameLen = 18u;
158 if (Include.size() > MaxStdHeaderNameLen)
161 // Lowercase and normalize the search string.
162 SmallString<32> LowerInclude{Include};
163 for (char &Ch : LowerInclude) {
164 // In the ASCII range?
165 if (static_cast<unsigned char>(Ch) > 0x7f)
166 return false; // Can't be a standard header
168 if (Ch >= 'A' && Ch <= 'Z')
170 // Normalize path separators for comparison purposes.
171 else if (::llvm::sys::path::is_separator(Ch))
175 // The standard C/C++ and Posix headers
176 return llvm::StringSwitch<bool>(LowerInclude)
178 .Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true)
179 .Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true)
180 .Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true)
181 .Cases("stdatomic.h", "stdbool.h", "stddef.h", "stdint.h", "stdio.h", true)
182 .Cases("stdlib.h", "stdnoreturn.h", "string.h", "tgmath.h", "threads.h", true)
183 .Cases("time.h", "uchar.h", "wchar.h", "wctype.h", true)
185 // C++ headers for C library facilities
186 .Cases("cassert", "ccomplex", "cctype", "cerrno", "cfenv", true)
187 .Cases("cfloat", "cinttypes", "ciso646", "climits", "clocale", true)
188 .Cases("cmath", "csetjmp", "csignal", "cstdalign", "cstdarg", true)
189 .Cases("cstdbool", "cstddef", "cstdint", "cstdio", "cstdlib", true)
190 .Cases("cstring", "ctgmath", "ctime", "cuchar", "cwchar", true)
191 .Case("cwctype", true)
193 // C++ library headers
194 .Cases("algorithm", "fstream", "list", "regex", "thread", true)
195 .Cases("array", "functional", "locale", "scoped_allocator", "tuple", true)
196 .Cases("atomic", "future", "map", "set", "type_traits", true)
197 .Cases("bitset", "initializer_list", "memory", "shared_mutex", "typeindex", true)
198 .Cases("chrono", "iomanip", "mutex", "sstream", "typeinfo", true)
199 .Cases("codecvt", "ios", "new", "stack", "unordered_map", true)
200 .Cases("complex", "iosfwd", "numeric", "stdexcept", "unordered_set", true)
201 .Cases("condition_variable", "iostream", "ostream", "streambuf", "utility", true)
202 .Cases("deque", "istream", "queue", "string", "valarray", true)
203 .Cases("exception", "iterator", "random", "strstream", "vector", true)
204 .Cases("forward_list", "limits", "ratio", "system_error", true)
206 // POSIX headers (which aren't also C headers)
207 .Cases("aio.h", "arpa/inet.h", "cpio.h", "dirent.h", "dlfcn.h", true)
208 .Cases("fcntl.h", "fmtmsg.h", "fnmatch.h", "ftw.h", "glob.h", true)
209 .Cases("grp.h", "iconv.h", "langinfo.h", "libgen.h", "monetary.h", true)
210 .Cases("mqueue.h", "ndbm.h", "net/if.h", "netdb.h", "netinet/in.h", true)
211 .Cases("netinet/tcp.h", "nl_types.h", "poll.h", "pthread.h", "pwd.h", true)
212 .Cases("regex.h", "sched.h", "search.h", "semaphore.h", "spawn.h", true)
213 .Cases("strings.h", "stropts.h", "sys/ipc.h", "sys/mman.h", "sys/msg.h", true)
214 .Cases("sys/resource.h", "sys/select.h", "sys/sem.h", "sys/shm.h", "sys/socket.h", true)
215 .Cases("sys/stat.h", "sys/statvfs.h", "sys/time.h", "sys/times.h", "sys/types.h", true)
216 .Cases("sys/uio.h", "sys/un.h", "sys/utsname.h", "sys/wait.h", "syslog.h", true)
217 .Cases("tar.h", "termios.h", "trace.h", "ulimit.h", true)
218 .Cases("unistd.h", "utime.h", "utmpx.h", "wordexp.h", true)
222 bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
224 // Missing macro name?
225 if (MacroNameTok.is(tok::eod))
226 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
228 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
230 bool Invalid = false;
231 std::string Spelling = getSpelling(MacroNameTok, &Invalid);
233 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
234 II = getIdentifierInfo(Spelling);
236 if (!II->isCPlusPlusOperatorKeyword())
237 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
239 // C++ 2.5p2: Alternative tokens behave the same as its primary token
240 // except for their spellings.
241 Diag(MacroNameTok, getLangOpts().MicrosoftExt
242 ? diag::ext_pp_operator_used_as_macro_name
243 : diag::err_pp_operator_used_as_macro_name)
244 << II << MacroNameTok.getKind();
246 // Allow #defining |and| and friends for Microsoft compatibility or
247 // recovery when legacy C headers are included in C++.
248 MacroNameTok.setIdentifierInfo(II);
251 if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
252 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
253 return Diag(MacroNameTok, diag::err_defined_macro_name);
256 if (isDefineUndef == MU_Undef) {
257 auto *MI = getMacroInfo(II);
258 if (MI && MI->isBuiltinMacro()) {
259 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
260 // and C++ [cpp.predefined]p4], but allow it as an extension.
261 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
265 // If defining/undefining reserved identifier or a keyword, we need to issue
267 SourceLocation MacroNameLoc = MacroNameTok.getLocation();
270 if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
271 (strcmp(SourceMgr.getBufferName(MacroNameLoc), "<built-in>") != 0)) {
272 MacroDiag D = MD_NoWarn;
273 if (isDefineUndef == MU_Define) {
274 D = shouldWarnOnMacroDef(*this, II);
276 else if (isDefineUndef == MU_Undef)
277 D = shouldWarnOnMacroUndef(*this, II);
278 if (D == MD_KeywordDef) {
279 // We do not want to warn on some patterns widely used in configuration
280 // scripts. This requires analyzing next tokens, so do not issue warnings
281 // now, only inform caller.
285 if (D == MD_ReservedMacro)
286 Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
289 // Okay, we got a good identifier.
293 /// \brief Lex and validate a macro name, which occurs after a
294 /// \#define or \#undef.
296 /// This sets the token kind to eod and discards the rest of the macro line if
297 /// the macro name is invalid.
299 /// \param MacroNameTok Token that is expected to be a macro name.
300 /// \param isDefineUndef Context in which macro is used.
301 /// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
302 void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
304 // Read the token, don't allow macro expansion on it.
305 LexUnexpandedToken(MacroNameTok);
307 if (MacroNameTok.is(tok::code_completion)) {
309 CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
310 setCodeCompletionReached();
311 LexUnexpandedToken(MacroNameTok);
314 if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
317 // Invalid macro name, read and discard the rest of the line and set the
318 // token kind to tok::eod if necessary.
319 if (MacroNameTok.isNot(tok::eod)) {
320 MacroNameTok.setKind(tok::eod);
321 DiscardUntilEndOfDirective();
325 /// \brief Ensure that the next token is a tok::eod token.
327 /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
328 /// true, then we consider macros that expand to zero tokens as being ok.
329 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
331 // Lex unexpanded tokens for most directives: macros might expand to zero
332 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
333 // #line) allow empty macros.
337 LexUnexpandedToken(Tmp);
339 // There should be no tokens after the directive, but we allow them as an
341 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
342 LexUnexpandedToken(Tmp);
344 if (Tmp.isNot(tok::eod)) {
345 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
346 // or if this is a macro-style preprocessing directive, because it is more
347 // trouble than it is worth to insert /**/ and check that there is no /**/
348 // in the range also.
350 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
352 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
353 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
354 DiscardUntilEndOfDirective();
358 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
359 /// decided that the subsequent tokens are in the \#if'd out portion of the
360 /// file. Lex the rest of the file, until we see an \#endif. If
361 /// FoundNonSkipPortion is true, then we have already emitted code for part of
362 /// this \#if directive, so \#else/\#elif blocks should never be entered.
363 /// If ElseOk is true, then \#else directives are ok, if not, then we have
364 /// already seen one so a \#else directive is a duplicate. When this returns,
365 /// the caller can lex the first valid token.
366 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
367 bool FoundNonSkipPortion,
369 SourceLocation ElseLoc) {
371 assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
373 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
374 FoundNonSkipPortion, FoundElse);
377 PTHSkipExcludedConditionalBlock();
381 // Enter raw mode to disable identifier lookup (and thus macro expansion),
382 // disabling warnings, etc.
383 CurPPLexer->LexingRawMode = true;
388 if (Tok.is(tok::code_completion)) {
390 CodeComplete->CodeCompleteInConditionalExclusion();
391 setCodeCompletionReached();
395 // If this is the end of the buffer, we have an error.
396 if (Tok.is(tok::eof)) {
397 // Emit errors for each unterminated conditional on the stack, including
399 while (!CurPPLexer->ConditionalStack.empty()) {
400 if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
401 Diag(CurPPLexer->ConditionalStack.back().IfLoc,
402 diag::err_pp_unterminated_conditional);
403 CurPPLexer->ConditionalStack.pop_back();
406 // Just return and let the caller lex after this #include.
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 unsigned 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);
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;
575 SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
576 Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
580 void Preprocessor::PTHSkipExcludedConditionalBlock() {
583 assert(CurPTHLexer->LexingRawMode == false);
585 // Skip to the next '#else', '#elif', or #endif.
586 if (CurPTHLexer->SkipBlock()) {
587 // We have reached an #endif. Both the '#' and 'endif' tokens
588 // have been consumed by the PTHLexer. Just pop off the condition level.
589 PPConditionalInfo CondInfo;
590 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
591 (void)InCond; // Silence warning in no-asserts mode.
592 assert(!InCond && "Can't be skipping if not in a conditional!");
596 // We have reached a '#else' or '#elif'. Lex the next token to get
597 // the directive flavor.
599 LexUnexpandedToken(Tok);
601 // We can actually look up the IdentifierInfo here since we aren't in
603 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
605 if (K == tok::pp_else) {
606 // #else: Enter the else condition. We aren't in a nested condition
607 // since we skip those. We're always in the one matching the last
608 // blocked we skipped.
609 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
610 // Note that we've seen a #else in this conditional.
611 CondInfo.FoundElse = true;
613 // If the #if block wasn't entered then enter the #else block now.
614 if (!CondInfo.FoundNonSkip) {
615 CondInfo.FoundNonSkip = true;
617 // Scan until the eod token.
618 CurPTHLexer->ParsingPreprocessorDirective = true;
619 DiscardUntilEndOfDirective();
620 CurPTHLexer->ParsingPreprocessorDirective = false;
625 // Otherwise skip this block.
629 assert(K == tok::pp_elif);
630 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
632 // If this is a #elif with a #else before it, report the error.
633 if (CondInfo.FoundElse)
634 Diag(Tok, diag::pp_err_elif_after_else);
636 // If this is in a skipping block or if we're already handled this #if
637 // block, don't bother parsing the condition. We just skip this block.
638 if (CondInfo.FoundNonSkip)
641 // Evaluate the condition of the #elif.
642 IdentifierInfo *IfNDefMacro = nullptr;
643 CurPTHLexer->ParsingPreprocessorDirective = true;
644 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
645 CurPTHLexer->ParsingPreprocessorDirective = false;
647 // If this condition is true, enter it!
649 CondInfo.FoundNonSkip = true;
653 // Otherwise, skip this block and go to the next one.
657 Module *Preprocessor::getModuleForLocation(SourceLocation Loc) {
658 if (!SourceMgr.isInMainFile(Loc)) {
659 // Try to determine the module of the include directive.
660 // FIXME: Look into directly passing the FileEntry from LookupFile instead.
661 FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
662 if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
663 // The include comes from an included file.
664 return HeaderInfo.getModuleMap()
665 .findModuleForHeader(EntryOfIncl)
670 // This is either in the main file or not in a file at all. It belongs
671 // to the current module, if there is one.
672 return getLangOpts().CurrentModule.empty()
674 : HeaderInfo.lookupModule(getLangOpts().CurrentModule);
677 Module *Preprocessor::getModuleContainingLocation(SourceLocation Loc) {
678 return HeaderInfo.getModuleMap().inferModuleFromLocation(
679 FullSourceLoc(Loc, SourceMgr));
683 Preprocessor::getModuleHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
684 SourceLocation Loc) {
685 // If we have a module import syntax, we shouldn't include a header to
686 // make a particular module visible.
687 if (getLangOpts().ObjC2)
690 // Figure out which module we'd want to import.
691 Module *M = getModuleContainingLocation(Loc);
695 Module *TopM = M->getTopLevelModule();
696 Module *IncM = getModuleForLocation(IncLoc);
698 // Walk up through the include stack, looking through textual headers of M
699 // until we hit a non-textual header that we can #include. (We assume textual
700 // headers of a module with non-textual headers aren't meant to be used to
701 // import entities from the module.)
702 auto &SM = getSourceManager();
703 while (!Loc.isInvalid() && !SM.isInMainFile(Loc)) {
704 auto ID = SM.getFileID(SM.getExpansionLoc(Loc));
705 auto *FE = SM.getFileEntryForID(ID);
707 bool InTextualHeader = false;
708 for (auto Header : HeaderInfo.getModuleMap().findAllModulesForHeader(FE)) {
709 if (!Header.getModule()->isSubModuleOf(TopM))
712 if (!(Header.getRole() & ModuleMap::TextualHeader)) {
713 // If this is an accessible, non-textual header of M's top-level module
714 // that transitively includes the given location and makes the
715 // corresponding module visible, this is the thing to #include.
716 if (Header.isAccessibleFrom(IncM))
719 // It's in a private header; we can't #include it.
720 // FIXME: If there's a public header in some module that re-exports it,
721 // then we could suggest including that, but it's not clear that's the
722 // expected way to make this entity visible.
726 InTextualHeader = true;
729 if (!InTextualHeader)
732 Loc = SM.getIncludeLoc(ID);
738 const FileEntry *Preprocessor::LookupFile(
739 SourceLocation FilenameLoc,
742 const DirectoryLookup *FromDir,
743 const FileEntry *FromFile,
744 const DirectoryLookup *&CurDir,
745 SmallVectorImpl<char> *SearchPath,
746 SmallVectorImpl<char> *RelativePath,
747 ModuleMap::KnownHeader *SuggestedModule,
749 Module *RequestingModule = getModuleForLocation(FilenameLoc);
750 bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc);
752 // If the header lookup mechanism may be relative to the current inclusion
753 // stack, record the parent #includes.
754 SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 16>
756 bool BuildSystemModule = false;
757 if (!FromDir && !FromFile) {
758 FileID FID = getCurrentFileLexer()->getFileID();
759 const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
761 // If there is no file entry associated with this file, it must be the
762 // predefines buffer or the module includes buffer. Any other file is not
763 // lexed with a normal lexer, so it won't be scanned for preprocessor
766 // If we have the predefines buffer, resolve #include references (which come
767 // from the -include command line argument) from the current working
768 // directory instead of relative to the main file.
770 // If we have the module includes buffer, resolve #include references (which
771 // come from header declarations in the module map) relative to the module
774 if (FID == SourceMgr.getMainFileID() && MainFileDir) {
775 Includers.push_back(std::make_pair(nullptr, MainFileDir));
776 BuildSystemModule = getCurrentModule()->IsSystem;
777 } else if ((FileEnt =
778 SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())))
779 Includers.push_back(std::make_pair(FileEnt, FileMgr.getDirectory(".")));
781 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
784 // MSVC searches the current include stack from top to bottom for
785 // headers included by quoted include directives.
786 // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
787 if (LangOpts.MSVCCompat && !isAngled) {
788 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
789 IncludeStackInfo &ISEntry = IncludeMacroStack[e - i - 1];
790 if (IsFileLexer(ISEntry))
791 if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
792 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
797 CurDir = CurDirLookup;
800 // We're supposed to start looking from after a particular file. Search
801 // the include path until we find that file or run out of files.
802 const DirectoryLookup *TmpCurDir = CurDir;
803 const DirectoryLookup *TmpFromDir = nullptr;
804 while (const FileEntry *FE = HeaderInfo.LookupFile(
805 Filename, FilenameLoc, isAngled, TmpFromDir, TmpCurDir,
806 Includers, SearchPath, RelativePath, RequestingModule,
807 SuggestedModule, SkipCache)) {
808 // Keep looking as if this file did a #include_next.
809 TmpFromDir = TmpCurDir;
811 if (FE == FromFile) {
813 FromDir = TmpFromDir;
820 // Do a standard file entry lookup.
821 const FileEntry *FE = HeaderInfo.LookupFile(
822 Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
823 RelativePath, RequestingModule, SuggestedModule, SkipCache,
826 if (SuggestedModule && !LangOpts.AsmPreprocessor)
827 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
828 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
833 const FileEntry *CurFileEnt;
834 // Otherwise, see if this is a subframework header. If so, this is relative
835 // to one of the headers on the #include stack. Walk the list of the current
836 // headers on the #include stack and pass them to HeaderInfo.
838 if ((CurFileEnt = CurPPLexer->getFileEntry())) {
839 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
840 SearchPath, RelativePath,
843 if (SuggestedModule && !LangOpts.AsmPreprocessor)
844 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
845 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
852 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
853 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
854 if (IsFileLexer(ISEntry)) {
855 if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
856 if ((FE = HeaderInfo.LookupSubframeworkHeader(
857 Filename, CurFileEnt, SearchPath, RelativePath,
858 RequestingModule, SuggestedModule))) {
859 if (SuggestedModule && !LangOpts.AsmPreprocessor)
860 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
861 RequestingModule, RequestingModuleIsModuleInterface,
862 FilenameLoc, Filename, FE);
869 // Otherwise, we really couldn't find the file.
873 //===----------------------------------------------------------------------===//
874 // Preprocessor Directive Handling.
875 //===----------------------------------------------------------------------===//
877 class Preprocessor::ResetMacroExpansionHelper {
879 ResetMacroExpansionHelper(Preprocessor *pp)
880 : PP(pp), save(pp->DisableMacroExpansion) {
881 if (pp->MacroExpansionInDirectivesOverride)
882 pp->DisableMacroExpansion = false;
885 ~ResetMacroExpansionHelper() {
886 PP->DisableMacroExpansion = save;
894 /// HandleDirective - This callback is invoked when the lexer sees a # token
895 /// at the start of a line. This consumes the directive, modifies the
896 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
897 /// read is the correct one.
898 void Preprocessor::HandleDirective(Token &Result) {
899 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
901 // We just parsed a # character at the start of a line, so we're in directive
902 // mode. Tell the lexer this so any newlines we see will be converted into an
903 // EOD token (which terminates the directive).
904 CurPPLexer->ParsingPreprocessorDirective = true;
905 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
907 bool ImmediatelyAfterTopLevelIfndef =
908 CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
909 CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
913 // We are about to read a token. For the multiple-include optimization FA to
914 // work, we have to remember if we had read any tokens *before* this
916 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
918 // Save the '#' token in case we need to return it later.
919 Token SavedHash = Result;
921 // Read the next token, the directive flavor. This isn't expanded due to
923 LexUnexpandedToken(Result);
925 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
930 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
931 // not support this for #include-like directives, since that can result in
932 // terrible diagnostics, and does not work in GCC.
934 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
935 switch (II->getPPKeywordID()) {
936 case tok::pp_include:
938 case tok::pp_include_next:
939 case tok::pp___include_macros:
941 Diag(Result, diag::err_embedded_directive) << II->getName();
942 DiscardUntilEndOfDirective();
948 Diag(Result, diag::ext_embedded_directive);
951 // Temporarily enable macro expansion if set so
952 // and reset to previous state when returning from this function.
953 ResetMacroExpansionHelper helper(this);
955 switch (Result.getKind()) {
957 return; // null directive.
958 case tok::code_completion:
960 CodeComplete->CodeCompleteDirective(
961 CurPPLexer->getConditionalStackDepth() > 0);
962 setCodeCompletionReached();
964 case tok::numeric_constant: // # 7 GNU line marker directive.
965 if (getLangOpts().AsmPreprocessor)
966 break; // # 4 is not a preprocessor directive in .S files.
967 return HandleDigitDirective(Result);
969 IdentifierInfo *II = Result.getIdentifierInfo();
970 if (!II) break; // Not an identifier.
972 // Ask what the preprocessor keyword ID is.
973 switch (II->getPPKeywordID()) {
975 // C99 6.10.1 - Conditional Inclusion.
977 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
979 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
981 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
983 return HandleElifDirective(Result);
985 return HandleElseDirective(Result);
987 return HandleEndifDirective(Result);
989 // C99 6.10.2 - Source File Inclusion.
990 case tok::pp_include:
992 return HandleIncludeDirective(SavedHash.getLocation(), Result);
993 case tok::pp___include_macros:
995 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
997 // C99 6.10.3 - Macro Replacement.
999 return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
1001 return HandleUndefDirective(Result);
1003 // C99 6.10.4 - Line Control.
1005 return HandleLineDirective(Result);
1007 // C99 6.10.5 - Error Directive.
1009 return HandleUserDiagnosticDirective(Result, false);
1011 // C99 6.10.6 - Pragma Directive.
1012 case tok::pp_pragma:
1013 return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
1016 case tok::pp_import:
1017 return HandleImportDirective(SavedHash.getLocation(), Result);
1018 case tok::pp_include_next:
1019 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
1021 case tok::pp_warning:
1022 Diag(Result, diag::ext_pp_warning_directive);
1023 return HandleUserDiagnosticDirective(Result, true);
1025 return HandleIdentSCCSDirective(Result);
1027 return HandleIdentSCCSDirective(Result);
1028 case tok::pp_assert:
1029 //isExtension = true; // FIXME: implement #assert
1031 case tok::pp_unassert:
1032 //isExtension = true; // FIXME: implement #unassert
1035 case tok::pp___public_macro:
1036 if (getLangOpts().Modules)
1037 return HandleMacroPublicDirective(Result);
1040 case tok::pp___private_macro:
1041 if (getLangOpts().Modules)
1042 return HandleMacroPrivateDirective(Result);
1048 // If this is a .S file, treat unknown # directives as non-preprocessor
1049 // directives. This is important because # may be a comment or introduce
1050 // various pseudo-ops. Just return the # token and push back the following
1051 // token to be lexed next time.
1052 if (getLangOpts().AsmPreprocessor) {
1053 auto Toks = llvm::make_unique<Token[]>(2);
1054 // Return the # and the token after it.
1055 Toks[0] = SavedHash;
1058 // If the second token is a hashhash token, then we need to translate it to
1059 // unknown so the token lexer doesn't try to perform token pasting.
1060 if (Result.is(tok::hashhash))
1061 Toks[1].setKind(tok::unknown);
1063 // Enter this token stream so that we re-lex the tokens. Make sure to
1064 // enable macro expansion, in case the token after the # is an identifier
1065 // that is expanded.
1066 EnterTokenStream(std::move(Toks), 2, false);
1070 // If we reached here, the preprocessing token is not valid!
1071 Diag(Result, diag::err_pp_invalid_directive);
1073 // Read the rest of the PP line.
1074 DiscardUntilEndOfDirective();
1076 // Okay, we're done parsing the directive.
1079 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
1080 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
1081 static bool GetLineValue(Token &DigitTok, unsigned &Val,
1082 unsigned DiagID, Preprocessor &PP,
1083 bool IsGNULineDirective=false) {
1084 if (DigitTok.isNot(tok::numeric_constant)) {
1085 PP.Diag(DigitTok, DiagID);
1087 if (DigitTok.isNot(tok::eod))
1088 PP.DiscardUntilEndOfDirective();
1092 SmallString<64> IntegerBuffer;
1093 IntegerBuffer.resize(DigitTok.getLength());
1094 const char *DigitTokBegin = &IntegerBuffer[0];
1095 bool Invalid = false;
1096 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
1100 // Verify that we have a simple digit-sequence, and compute the value. This
1101 // is always a simple digit string computed in decimal, so we do this manually
1104 for (unsigned i = 0; i != ActualLength; ++i) {
1105 // C++1y [lex.fcon]p1:
1106 // Optional separating single quotes in a digit-sequence are ignored
1107 if (DigitTokBegin[i] == '\'')
1110 if (!isDigit(DigitTokBegin[i])) {
1111 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
1112 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
1113 PP.DiscardUntilEndOfDirective();
1117 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
1118 if (NextVal < Val) { // overflow.
1119 PP.Diag(DigitTok, DiagID);
1120 PP.DiscardUntilEndOfDirective();
1126 if (DigitTokBegin[0] == '0' && Val)
1127 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
1128 << IsGNULineDirective;
1133 /// \brief Handle a \#line directive: C99 6.10.4.
1135 /// The two acceptable forms are:
1137 /// # line digit-sequence
1138 /// # line digit-sequence "s-char-sequence"
1140 void Preprocessor::HandleLineDirective(Token &Tok) {
1141 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
1146 // Validate the number and convert it to an unsigned.
1148 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
1152 Diag(DigitTok, diag::ext_pp_line_zero);
1154 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1155 // number greater than 2147483647". C90 requires that the line # be <= 32767.
1156 unsigned LineLimit = 32768U;
1157 if (LangOpts.C99 || LangOpts.CPlusPlus11)
1158 LineLimit = 2147483648U;
1159 if (LineNo >= LineLimit)
1160 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
1161 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
1162 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
1164 int FilenameID = -1;
1168 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1169 // string followed by eod.
1170 if (StrTok.is(tok::eod))
1172 else if (StrTok.isNot(tok::string_literal)) {
1173 Diag(StrTok, diag::err_pp_line_invalid_filename);
1174 return DiscardUntilEndOfDirective();
1175 } else if (StrTok.hasUDSuffix()) {
1176 Diag(StrTok, diag::err_invalid_string_udl);
1177 return DiscardUntilEndOfDirective();
1179 // Parse and validate the string, converting it into a unique ID.
1180 StringLiteralParser Literal(StrTok, *this);
1181 assert(Literal.isAscii() && "Didn't allow wide strings in");
1182 if (Literal.hadError)
1183 return DiscardUntilEndOfDirective();
1184 if (Literal.Pascal) {
1185 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1186 return DiscardUntilEndOfDirective();
1188 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1190 // Verify that there is nothing after the string, other than EOD. Because
1191 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1192 CheckEndOfDirective("line", true);
1195 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
1198 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1199 PPCallbacks::RenameFile,
1203 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1204 /// marker directive.
1205 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1206 bool &IsSystemHeader, bool &IsExternCHeader,
1211 if (FlagTok.is(tok::eod)) return false;
1212 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1219 if (FlagTok.is(tok::eod)) return false;
1220 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1222 } else if (FlagVal == 2) {
1225 SourceManager &SM = PP.getSourceManager();
1226 // If we are leaving the current presumed file, check to make sure the
1227 // presumed include stack isn't empty!
1229 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1230 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1231 if (PLoc.isInvalid())
1234 // If there is no include loc (main file) or if the include loc is in a
1235 // different physical file, then we aren't in a "1" line marker flag region.
1236 SourceLocation IncLoc = PLoc.getIncludeLoc();
1237 if (IncLoc.isInvalid() ||
1238 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
1239 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1240 PP.DiscardUntilEndOfDirective();
1245 if (FlagTok.is(tok::eod)) return false;
1246 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1250 // We must have 3 if there are still flags.
1252 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1253 PP.DiscardUntilEndOfDirective();
1257 IsSystemHeader = true;
1260 if (FlagTok.is(tok::eod)) return false;
1261 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1264 // We must have 4 if there is yet another flag.
1266 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1267 PP.DiscardUntilEndOfDirective();
1271 IsExternCHeader = true;
1274 if (FlagTok.is(tok::eod)) return false;
1276 // There are no more valid flags here.
1277 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1278 PP.DiscardUntilEndOfDirective();
1282 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1283 /// one of the following forms:
1286 /// # 42 "file" ('1' | '2')?
1287 /// # 42 "file" ('1' | '2')? '3' '4'?
1289 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1290 // Validate the number and convert it to an unsigned. GNU does not have a
1291 // line # limit other than it fit in 32-bits.
1293 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1300 bool IsFileEntry = false, IsFileExit = false;
1301 bool IsSystemHeader = false, IsExternCHeader = false;
1302 int FilenameID = -1;
1304 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1305 // string followed by eod.
1306 if (StrTok.is(tok::eod))
1308 else if (StrTok.isNot(tok::string_literal)) {
1309 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1310 return DiscardUntilEndOfDirective();
1311 } else if (StrTok.hasUDSuffix()) {
1312 Diag(StrTok, diag::err_invalid_string_udl);
1313 return DiscardUntilEndOfDirective();
1315 // Parse and validate the string, converting it into a unique ID.
1316 StringLiteralParser Literal(StrTok, *this);
1317 assert(Literal.isAscii() && "Didn't allow wide strings in");
1318 if (Literal.hadError)
1319 return DiscardUntilEndOfDirective();
1320 if (Literal.Pascal) {
1321 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1322 return DiscardUntilEndOfDirective();
1324 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1326 // If a filename was present, read any flags that are present.
1327 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
1328 IsSystemHeader, IsExternCHeader, *this))
1332 // Create a line note with this information.
1333 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
1334 IsFileEntry, IsFileExit,
1335 IsSystemHeader, IsExternCHeader);
1337 // If the preprocessor has callbacks installed, notify them of the #line
1338 // change. This is used so that the line marker comes out in -E mode for
1341 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1343 Reason = PPCallbacks::EnterFile;
1344 else if (IsFileExit)
1345 Reason = PPCallbacks::ExitFile;
1346 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1347 if (IsExternCHeader)
1348 FileKind = SrcMgr::C_ExternCSystem;
1349 else if (IsSystemHeader)
1350 FileKind = SrcMgr::C_System;
1352 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1356 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1358 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1360 // PTH doesn't emit #warning or #error directives.
1362 return CurPTHLexer->DiscardToEndOfLine();
1364 // Read the rest of the line raw. We do this because we don't want macros
1365 // to be expanded and we don't require that the tokens be valid preprocessing
1366 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1367 // collapse multiple consequtive white space between tokens, but this isn't
1368 // specified by the standard.
1369 SmallString<128> Message;
1370 CurLexer->ReadToEndOfLine(&Message);
1372 // Find the first non-whitespace character, so that we can make the
1373 // diagnostic more succinct.
1374 StringRef Msg = StringRef(Message).ltrim(' ');
1377 Diag(Tok, diag::pp_hash_warning) << Msg;
1379 Diag(Tok, diag::err_pp_hash_error) << Msg;
1382 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1384 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1385 // Yes, this directive is an extension.
1386 Diag(Tok, diag::ext_pp_ident_directive);
1388 // Read the string argument.
1392 // If the token kind isn't a string, it's a malformed directive.
1393 if (StrTok.isNot(tok::string_literal) &&
1394 StrTok.isNot(tok::wide_string_literal)) {
1395 Diag(StrTok, diag::err_pp_malformed_ident);
1396 if (StrTok.isNot(tok::eod))
1397 DiscardUntilEndOfDirective();
1401 if (StrTok.hasUDSuffix()) {
1402 Diag(StrTok, diag::err_invalid_string_udl);
1403 return DiscardUntilEndOfDirective();
1406 // Verify that there is nothing after the string, other than EOD.
1407 CheckEndOfDirective("ident");
1410 bool Invalid = false;
1411 std::string Str = getSpelling(StrTok, &Invalid);
1413 Callbacks->Ident(Tok.getLocation(), Str);
1417 /// \brief Handle a #public directive.
1418 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1420 ReadMacroName(MacroNameTok, MU_Undef);
1422 // Error reading macro name? If so, diagnostic already issued.
1423 if (MacroNameTok.is(tok::eod))
1426 // Check to see if this is the last token on the #__public_macro line.
1427 CheckEndOfDirective("__public_macro");
1429 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1430 // Okay, we finally have a valid identifier to undef.
1431 MacroDirective *MD = getLocalMacroDirective(II);
1433 // If the macro is not defined, this is an error.
1435 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1439 // Note that this macro has now been exported.
1440 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1441 MacroNameTok.getLocation(), /*IsPublic=*/true));
1444 /// \brief Handle a #private directive.
1445 void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1447 ReadMacroName(MacroNameTok, MU_Undef);
1449 // Error reading macro name? If so, diagnostic already issued.
1450 if (MacroNameTok.is(tok::eod))
1453 // Check to see if this is the last token on the #__private_macro line.
1454 CheckEndOfDirective("__private_macro");
1456 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1457 // Okay, we finally have a valid identifier to undef.
1458 MacroDirective *MD = getLocalMacroDirective(II);
1460 // If the macro is not defined, this is an error.
1462 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1466 // Note that this macro has now been marked private.
1467 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1468 MacroNameTok.getLocation(), /*IsPublic=*/false));
1471 //===----------------------------------------------------------------------===//
1472 // Preprocessor Include Directive Handling.
1473 //===----------------------------------------------------------------------===//
1475 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1476 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1477 /// true if the input filename was in <>'s or false if it were in ""'s. The
1478 /// caller is expected to provide a buffer that is large enough to hold the
1479 /// spelling of the filename, but is also expected to handle the case when
1480 /// this method decides to use a different buffer.
1481 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1482 StringRef &Buffer) {
1483 // Get the text form of the filename.
1484 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1486 // Make sure the filename is <x> or "x".
1488 if (Buffer[0] == '<') {
1489 if (Buffer.back() != '>') {
1490 Diag(Loc, diag::err_pp_expects_filename);
1491 Buffer = StringRef();
1495 } else if (Buffer[0] == '"') {
1496 if (Buffer.back() != '"') {
1497 Diag(Loc, diag::err_pp_expects_filename);
1498 Buffer = StringRef();
1503 Diag(Loc, diag::err_pp_expects_filename);
1504 Buffer = StringRef();
1508 // Diagnose #include "" as invalid.
1509 if (Buffer.size() <= 2) {
1510 Diag(Loc, diag::err_pp_empty_filename);
1511 Buffer = StringRef();
1515 // Skip the brackets.
1516 Buffer = Buffer.substr(1, Buffer.size()-2);
1520 // \brief Handle cases where the \#include name is expanded from a macro
1521 // as multiple tokens, which need to be glued together.
1523 // This occurs for code like:
1525 // \#define FOO <a/b.h>
1528 // because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1530 // This code concatenates and consumes tokens up to the '>' token. It returns
1531 // false if the > was found, otherwise it returns true if it finds and consumes
1533 bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
1534 SourceLocation &End) {
1538 while (CurTok.isNot(tok::eod)) {
1539 End = CurTok.getLocation();
1541 // FIXME: Provide code completion for #includes.
1542 if (CurTok.is(tok::code_completion)) {
1543 setCodeCompletionReached();
1548 // Append the spelling of this token to the buffer. If there was a space
1549 // before it, add it now.
1550 if (CurTok.hasLeadingSpace())
1551 FilenameBuffer.push_back(' ');
1553 // Get the spelling of the token, directly into FilenameBuffer if possible.
1554 unsigned PreAppendSize = FilenameBuffer.size();
1555 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1557 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1558 unsigned ActualLen = getSpelling(CurTok, BufPtr);
1560 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1561 if (BufPtr != &FilenameBuffer[PreAppendSize])
1562 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1564 // Resize FilenameBuffer to the correct size.
1565 if (CurTok.getLength() != ActualLen)
1566 FilenameBuffer.resize(PreAppendSize+ActualLen);
1568 // If we found the '>' marker, return success.
1569 if (CurTok.is(tok::greater))
1575 // If we hit the eod marker, emit an error and return true so that the caller
1576 // knows the EOD has been read.
1577 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1581 /// \brief Push a token onto the token stream containing an annotation.
1582 static void EnterAnnotationToken(Preprocessor &PP,
1583 SourceLocation Begin, SourceLocation End,
1584 tok::TokenKind Kind, void *AnnotationVal) {
1585 // FIXME: Produce this as the current token directly, rather than
1586 // allocating a new token for it.
1587 auto Tok = llvm::make_unique<Token[]>(1);
1588 Tok[0].startToken();
1589 Tok[0].setKind(Kind);
1590 Tok[0].setLocation(Begin);
1591 Tok[0].setAnnotationEndLoc(End);
1592 Tok[0].setAnnotationValue(AnnotationVal);
1593 PP.EnterTokenStream(std::move(Tok), 1, true);
1596 /// \brief Produce a diagnostic informing the user that a #include or similar
1597 /// was implicitly treated as a module import.
1598 static void diagnoseAutoModuleImport(
1599 Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
1600 ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
1601 SourceLocation PathEnd) {
1602 assert(PP.getLangOpts().ObjC2 && "no import syntax available");
1604 SmallString<128> PathString;
1605 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1608 PathString += Path[I].first->getName();
1610 int IncludeKind = 0;
1612 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1613 case tok::pp_include:
1617 case tok::pp_import:
1621 case tok::pp_include_next:
1625 case tok::pp___include_macros:
1630 llvm_unreachable("unknown include directive kind");
1633 CharSourceRange ReplaceRange(SourceRange(HashLoc, PathEnd),
1634 /*IsTokenRange=*/false);
1635 PP.Diag(HashLoc, diag::warn_auto_module_import)
1636 << IncludeKind << PathString
1637 << FixItHint::CreateReplacement(ReplaceRange,
1638 ("@import " + PathString + ";").str());
1641 // Given a vector of path components and a string containing the real
1642 // path to the file, build a properly-cased replacement in the vector,
1643 // and return true if the replacement should be suggested.
1644 static bool trySimplifyPath(SmallVectorImpl<StringRef> &Components,
1645 StringRef RealPathName) {
1646 auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName);
1647 auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName);
1649 bool SuggestReplacement = false;
1650 // Below is a best-effort to handle ".." in paths. It is admittedly
1651 // not 100% correct in the presence of symlinks.
1652 for (auto &Component : llvm::reverse(Components)) {
1653 if ("." == Component) {
1654 } else if (".." == Component) {
1658 } else if (RealPathComponentIter != RealPathComponentEnd) {
1659 if (Component != *RealPathComponentIter) {
1660 // If these path components differ by more than just case, then we
1661 // may be looking at symlinked paths. Bail on this diagnostic to avoid
1662 // noisy false positives.
1663 SuggestReplacement = RealPathComponentIter->equals_lower(Component);
1664 if (!SuggestReplacement)
1666 Component = *RealPathComponentIter;
1668 ++RealPathComponentIter;
1671 return SuggestReplacement;
1674 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1675 /// the file to be included from the lexer, then include it! This is a common
1676 /// routine with functionality shared between \#include, \#include_next and
1677 /// \#import. LookupFrom is set when this is a \#include_next directive, it
1678 /// specifies the file to start searching from.
1679 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1681 const DirectoryLookup *LookupFrom,
1682 const FileEntry *LookupFromFile,
1685 CurPPLexer->LexIncludeFilename(FilenameTok);
1687 // Reserve a buffer to get the spelling.
1688 SmallString<128> FilenameBuffer;
1691 SourceLocation CharEnd; // the end of this directive, in characters
1693 switch (FilenameTok.getKind()) {
1695 // If the token kind is EOD, the error has already been diagnosed.
1698 case tok::angle_string_literal:
1699 case tok::string_literal:
1700 Filename = getSpelling(FilenameTok, FilenameBuffer);
1701 End = FilenameTok.getLocation();
1702 CharEnd = End.getLocWithOffset(FilenameTok.getLength());
1706 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1707 // case, glue the tokens together into FilenameBuffer and interpret those.
1708 FilenameBuffer.push_back('<');
1709 if (ConcatenateIncludeName(FilenameBuffer, End))
1710 return; // Found <eod> but no ">"? Diagnostic already emitted.
1711 Filename = FilenameBuffer;
1712 CharEnd = End.getLocWithOffset(1);
1715 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1716 DiscardUntilEndOfDirective();
1720 CharSourceRange FilenameRange
1721 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1722 StringRef OriginalFilename = Filename;
1724 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1725 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1727 if (Filename.empty()) {
1728 DiscardUntilEndOfDirective();
1732 // Verify that there is nothing after the filename, other than EOD. Note that
1733 // we allow macros that expand to nothing after the filename, because this
1734 // falls into the category of "#include pp-tokens new-line" specified in
1736 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1738 // Check that we don't have infinite #include recursion.
1739 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1740 Diag(FilenameTok, diag::err_pp_include_too_deep);
1744 // Complain about attempts to #include files in an audit pragma.
1745 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1746 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1747 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1749 // Immediately leave the pragma.
1750 PragmaARCCFCodeAuditedLoc = SourceLocation();
1753 // Complain about attempts to #include files in an assume-nonnull pragma.
1754 if (PragmaAssumeNonNullLoc.isValid()) {
1755 Diag(HashLoc, diag::err_pp_include_in_assume_nonnull);
1756 Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
1758 // Immediately leave the pragma.
1759 PragmaAssumeNonNullLoc = SourceLocation();
1762 if (HeaderInfo.HasIncludeAliasMap()) {
1763 // Map the filename with the brackets still attached. If the name doesn't
1764 // map to anything, fall back on the filename we've already gotten the
1766 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1767 if (!NewName.empty())
1771 // Search include directories.
1772 const DirectoryLookup *CurDir;
1773 SmallString<1024> SearchPath;
1774 SmallString<1024> RelativePath;
1775 // We get the raw path only if we have 'Callbacks' to which we later pass
1777 ModuleMap::KnownHeader SuggestedModule;
1778 SourceLocation FilenameLoc = FilenameTok.getLocation();
1779 SmallString<128> NormalizedPath;
1780 if (LangOpts.MSVCCompat) {
1781 NormalizedPath = Filename.str();
1782 #ifndef LLVM_ON_WIN32
1783 llvm::sys::path::native(NormalizedPath);
1786 const FileEntry *File = LookupFile(
1787 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
1788 isAngled, LookupFrom, LookupFromFile, CurDir,
1789 Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
1794 // Give the clients a chance to recover.
1795 SmallString<128> RecoveryPath;
1796 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1797 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1798 // Add the recovery path to the list of search paths.
1799 DirectoryLookup DL(DE, SrcMgr::C_User, false);
1800 HeaderInfo.AddSearchPath(DL, isAngled);
1802 // Try the lookup again, skipping the cache.
1805 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1806 LookupFrom, LookupFromFile, CurDir, nullptr, nullptr,
1807 &SuggestedModule, /*SkipCache*/ true);
1812 if (!SuppressIncludeNotFoundError) {
1813 // If the file could not be located and it was included via angle
1814 // brackets, we can attempt a lookup as though it were a quoted path to
1815 // provide the user with a possible fixit.
1819 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, false,
1820 LookupFrom, LookupFromFile, CurDir,
1821 Callbacks ? &SearchPath : nullptr,
1822 Callbacks ? &RelativePath : nullptr,
1825 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1826 Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1828 FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1832 // If the file is still not found, just go with the vanilla diagnostic
1834 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1838 // Should we enter the source file? Set to false if either the source file is
1839 // known to have no effect beyond its effect on module visibility -- that is,
1840 // if it's got an include guard that is already defined or is a modular header
1841 // we've imported or already built.
1842 bool ShouldEnter = true;
1844 // Determine whether we should try to import the module for this #include, if
1845 // there is one. Don't do so if precompiled module support is disabled or we
1846 // are processing this module textually (because we're building the module).
1847 if (File && SuggestedModule && getLangOpts().Modules &&
1848 SuggestedModule.getModule()->getTopLevelModuleName() !=
1849 getLangOpts().CurrentModule) {
1850 // If this include corresponds to a module but that module is
1851 // unavailable, diagnose the situation and bail out.
1852 // FIXME: Remove this; loadModule does the same check (but produces
1853 // slightly worse diagnostics).
1854 if (!SuggestedModule.getModule()->isAvailable() &&
1855 !SuggestedModule.getModule()
1856 ->getTopLevelModule()
1857 ->HasIncompatibleModuleFile) {
1858 clang::Module::Requirement Requirement;
1859 clang::Module::UnresolvedHeaderDirective MissingHeader;
1860 Module *M = SuggestedModule.getModule();
1861 // Identify the cause.
1862 (void)M->isAvailable(getLangOpts(), getTargetInfo(), Requirement,
1864 if (MissingHeader.FileNameLoc.isValid()) {
1865 Diag(MissingHeader.FileNameLoc, diag::err_module_header_missing)
1866 << MissingHeader.IsUmbrella << MissingHeader.FileName;
1868 Diag(M->DefinitionLoc, diag::err_module_unavailable)
1869 << M->getFullModuleName() << Requirement.second << Requirement.first;
1871 Diag(FilenameTok.getLocation(),
1872 diag::note_implicit_top_level_module_import_here)
1873 << M->getTopLevelModuleName();
1877 // Compute the module access path corresponding to this module.
1878 // FIXME: Should we have a second loadModule() overload to avoid this
1879 // extra lookup step?
1880 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1881 for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
1882 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1883 FilenameTok.getLocation()));
1884 std::reverse(Path.begin(), Path.end());
1886 // Warn that we're replacing the include/import with a module import.
1887 // We only do this in Objective-C, where we have a module-import syntax.
1888 if (getLangOpts().ObjC2)
1889 diagnoseAutoModuleImport(*this, HashLoc, IncludeTok, Path, CharEnd);
1891 // Load the module to import its macros. We'll make the declarations
1892 // visible when the parser gets here.
1893 // FIXME: Pass SuggestedModule in here rather than converting it to a path
1894 // and making the module loader convert it back again.
1895 ModuleLoadResult Imported = TheModuleLoader.loadModule(
1896 IncludeTok.getLocation(), Path, Module::Hidden,
1897 /*IsIncludeDirective=*/true);
1898 assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
1899 "the imported module is different than the suggested one");
1902 ShouldEnter = false;
1903 else if (Imported.isMissingExpected()) {
1904 // We failed to find a submodule that we assumed would exist (because it
1905 // was in the directory of an umbrella header, for instance), but no
1906 // actual module exists for it (because the umbrella header is
1907 // incomplete). Treat this as a textual inclusion.
1908 SuggestedModule = ModuleMap::KnownHeader();
1910 // We hit an error processing the import. Bail out.
1911 if (hadModuleLoaderFatalFailure()) {
1912 // With a fatal failure in the module loader, we abort parsing.
1913 Token &Result = IncludeTok;
1915 Result.startToken();
1916 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1917 CurLexer->cutOffLexing();
1919 assert(CurPTHLexer && "#include but no current lexer set!");
1920 CurPTHLexer->getEOF(Result);
1928 // Notify the callback object that we've seen an inclusion directive.
1929 Callbacks->InclusionDirective(
1930 HashLoc, IncludeTok,
1931 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1932 FilenameRange, File, SearchPath, RelativePath,
1933 ShouldEnter ? nullptr : SuggestedModule.getModule());
1939 // The #included file will be considered to be a system header if either it is
1940 // in a system include directory, or if the #includer is a system include
1942 SrcMgr::CharacteristicKind FileCharacter =
1943 std::max(HeaderInfo.getFileDirFlavor(File),
1944 SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
1946 // FIXME: If we have a suggested module, and we've already visited this file,
1947 // don't bother entering it again. We know it has no further effect.
1949 // Issue a diagnostic if the name of the file on disk has a different case
1950 // than the one we're about to open.
1951 const bool CheckIncludePathPortability =
1952 File && !File->tryGetRealPathName().empty();
1954 if (CheckIncludePathPortability) {
1955 StringRef Name = LangOpts.MSVCCompat ? NormalizedPath.str() : Filename;
1956 StringRef RealPathName = File->tryGetRealPathName();
1957 SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name),
1958 llvm::sys::path::end(Name));
1960 if (trySimplifyPath(Components, RealPathName)) {
1961 SmallString<128> Path;
1962 Path.reserve(Name.size()+2);
1963 Path.push_back(isAngled ? '<' : '"');
1964 for (auto Component : Components) {
1965 Path.append(Component);
1966 // Append the separator the user used, or the close quote
1968 Path.size() <= Filename.size() ? Filename[Path.size()-1] :
1969 (isAngled ? '>' : '"'));
1971 auto Replacement = Path.str().str();
1972 // For user files and known standard headers, by default we issue a diagnostic.
1973 // For other system headers, we don't. They can be controlled separately.
1974 auto DiagId = (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name)) ?
1975 diag::pp_nonportable_path : diag::pp_nonportable_system_path;
1976 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1977 Diag(FilenameTok, DiagId) << Replacement <<
1978 FixItHint::CreateReplacement(Range, Replacement);
1982 // Ask HeaderInfo if we should enter this #include file. If not, #including
1983 // this file will have no effect.
1985 !HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport,
1986 SuggestedModule.getModule())) {
1987 ShouldEnter = false;
1989 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1992 // If we don't need to enter the file, stop now.
1994 // If this is a module import, make it visible if needed.
1995 if (auto *M = SuggestedModule.getModule()) {
1996 makeModuleVisible(M, HashLoc);
1998 if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
1999 tok::pp___include_macros)
2000 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_include, M);
2005 // Look up the file, create a File ID for it.
2006 SourceLocation IncludePos = End;
2007 // If the filename string was the result of macro expansions, set the include
2008 // position on the file where it will be included and after the expansions.
2009 if (IncludePos.isMacroID())
2010 IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
2011 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
2012 assert(FID.isValid() && "Expected valid file ID");
2014 // If all is good, enter the new file!
2015 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
2018 // Determine if we're switching to building a new submodule, and which one.
2019 if (auto *M = SuggestedModule.getModule()) {
2020 assert(!CurSubmodule && "should not have marked this as a module yet");
2023 // Let the macro handling code know that any future macros are within
2024 // the new submodule.
2025 EnterSubmodule(M, HashLoc);
2027 // Let the parser know that any future declarations are within the new
2029 // FIXME: There's no point doing this if we're handling a #__include_macros
2031 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin, M);
2035 /// HandleIncludeNextDirective - Implements \#include_next.
2037 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
2038 Token &IncludeNextTok) {
2039 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
2041 // #include_next is like #include, except that we start searching after
2042 // the current found directory. If we can't do this, issue a
2044 const DirectoryLookup *Lookup = CurDirLookup;
2045 const FileEntry *LookupFromFile = nullptr;
2046 if (isInPrimaryFile()) {
2048 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
2049 } else if (CurSubmodule) {
2050 // Start looking up in the directory *after* the one in which the current
2051 // file would be found, if any.
2052 assert(CurPPLexer && "#include_next directive in macro?");
2053 LookupFromFile = CurPPLexer->getFileEntry();
2055 } else if (!Lookup) {
2056 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
2058 // Start looking up in the next directory.
2062 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
2066 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
2067 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
2068 // The Microsoft #import directive takes a type library and generates header
2069 // files from it, and includes those. This is beyond the scope of what clang
2070 // does, so we ignore it and error out. However, #import can optionally have
2071 // trailing attributes that span multiple lines. We're going to eat those
2072 // so we can continue processing from there.
2073 Diag(Tok, diag::err_pp_import_directive_ms );
2075 // Read tokens until we get to the end of the directive. Note that the
2076 // directive can be split over multiple lines using the backslash character.
2077 DiscardUntilEndOfDirective();
2080 /// HandleImportDirective - Implements \#import.
2082 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
2084 if (!LangOpts.ObjC1) { // #import is standard for ObjC.
2085 if (LangOpts.MSVCCompat)
2086 return HandleMicrosoftImportDirective(ImportTok);
2087 Diag(ImportTok, diag::ext_pp_import_directive);
2089 return HandleIncludeDirective(HashLoc, ImportTok, nullptr, nullptr, true);
2092 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
2093 /// pseudo directive in the predefines buffer. This handles it by sucking all
2094 /// tokens through the preprocessor and discarding them (only keeping the side
2095 /// effects on the preprocessor).
2096 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
2097 Token &IncludeMacrosTok) {
2098 // This directive should only occur in the predefines buffer. If not, emit an
2099 // error and reject it.
2100 SourceLocation Loc = IncludeMacrosTok.getLocation();
2101 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
2102 Diag(IncludeMacrosTok.getLocation(),
2103 diag::pp_include_macros_out_of_predefines);
2104 DiscardUntilEndOfDirective();
2108 // Treat this as a normal #include for checking purposes. If this is
2109 // successful, it will push a new lexer onto the include stack.
2110 HandleIncludeDirective(HashLoc, IncludeMacrosTok);
2115 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
2116 } while (TmpTok.isNot(tok::hashhash));
2119 //===----------------------------------------------------------------------===//
2120 // Preprocessor Macro Directive Handling.
2121 //===----------------------------------------------------------------------===//
2123 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
2124 /// definition has just been read. Lex the rest of the arguments and the
2125 /// closing ), updating MI with what we learn. Return true if an error occurs
2126 /// parsing the arg list.
2127 bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
2128 SmallVector<IdentifierInfo*, 32> Arguments;
2131 LexUnexpandedToken(Tok);
2132 switch (Tok.getKind()) {
2134 // Found the end of the argument list.
2135 if (Arguments.empty()) // #define FOO()
2137 // Otherwise we have #define FOO(A,)
2138 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2140 case tok::ellipsis: // #define X(... -> C99 varargs
2142 Diag(Tok, LangOpts.CPlusPlus11 ?
2143 diag::warn_cxx98_compat_variadic_macro :
2144 diag::ext_variadic_macro);
2146 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
2147 if (LangOpts.OpenCL) {
2148 Diag(Tok, diag::err_pp_opencl_variadic_macros);
2152 // Lex the token after the identifier.
2153 LexUnexpandedToken(Tok);
2154 if (Tok.isNot(tok::r_paren)) {
2155 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2158 // Add the __VA_ARGS__ identifier as an argument.
2159 Arguments.push_back(Ident__VA_ARGS__);
2160 MI->setIsC99Varargs();
2161 MI->setArgumentList(Arguments, BP);
2163 case tok::eod: // #define X(
2164 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2167 // Handle keywords and identifiers here to accept things like
2168 // #define Foo(for) for.
2169 IdentifierInfo *II = Tok.getIdentifierInfo();
2172 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2176 // If this is already used as an argument, it is used multiple times (e.g.
2178 if (std::find(Arguments.begin(), Arguments.end(), II) !=
2179 Arguments.end()) { // C99 6.10.3p6
2180 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
2184 // Add the argument to the macro info.
2185 Arguments.push_back(II);
2187 // Lex the token after the identifier.
2188 LexUnexpandedToken(Tok);
2190 switch (Tok.getKind()) {
2191 default: // #define X(A B
2192 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2194 case tok::r_paren: // #define X(A)
2195 MI->setArgumentList(Arguments, BP);
2197 case tok::comma: // #define X(A,
2199 case tok::ellipsis: // #define X(A... -> GCC extension
2200 // Diagnose extension.
2201 Diag(Tok, diag::ext_named_variadic_macro);
2203 // Lex the token after the identifier.
2204 LexUnexpandedToken(Tok);
2205 if (Tok.isNot(tok::r_paren)) {
2206 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2210 MI->setIsGNUVarargs();
2211 MI->setArgumentList(Arguments, BP);
2218 static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2219 const LangOptions &LOptions) {
2220 if (MI->getNumTokens() == 1) {
2221 const Token &Value = MI->getReplacementToken(0);
2223 // Macro that is identity, like '#define inline inline' is a valid pattern.
2224 if (MacroName.getKind() == Value.getKind())
2227 // Macro that maps a keyword to the same keyword decorated with leading/
2228 // trailing underscores is a valid pattern:
2229 // #define inline __inline
2230 // #define inline __inline__
2231 // #define inline _inline (in MS compatibility mode)
2232 StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2233 if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2234 if (!II->isKeyword(LOptions))
2236 StringRef ValueText = II->getName();
2237 StringRef TrimmedValue = ValueText;
2238 if (!ValueText.startswith("__")) {
2239 if (ValueText.startswith("_"))
2240 TrimmedValue = TrimmedValue.drop_front(1);
2244 TrimmedValue = TrimmedValue.drop_front(2);
2245 if (TrimmedValue.endswith("__"))
2246 TrimmedValue = TrimmedValue.drop_back(2);
2248 return TrimmedValue.equals(MacroText);
2255 return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2257 MI->getNumTokens() == 0;
2260 /// HandleDefineDirective - Implements \#define. This consumes the entire macro
2261 /// line then lets the caller lex the next real token.
2262 void Preprocessor::HandleDefineDirective(Token &DefineTok,
2263 bool ImmediatelyAfterHeaderGuard) {
2267 bool MacroShadowsKeyword;
2268 ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
2270 // Error reading macro name? If so, diagnostic already issued.
2271 if (MacroNameTok.is(tok::eod))
2274 Token LastTok = MacroNameTok;
2276 // If we are supposed to keep comments in #defines, reenable comment saving
2278 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
2280 // Create the new macro.
2281 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
2284 LexUnexpandedToken(Tok);
2286 // If this is a function-like macro definition, parse the argument list,
2287 // marking each of the identifiers as being used as macro arguments. Also,
2288 // check other constraints on the first token of the macro body.
2289 if (Tok.is(tok::eod)) {
2290 if (ImmediatelyAfterHeaderGuard) {
2291 // Save this macro information since it may part of a header guard.
2292 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2293 MacroNameTok.getLocation());
2295 // If there is no body to this macro, we have no special handling here.
2296 } else if (Tok.hasLeadingSpace()) {
2297 // This is a normal token with leading space. Clear the leading space
2298 // marker on the first token to get proper expansion.
2299 Tok.clearFlag(Token::LeadingSpace);
2300 } else if (Tok.is(tok::l_paren)) {
2301 // This is a function-like macro definition. Read the argument list.
2302 MI->setIsFunctionLike();
2303 if (ReadMacroDefinitionArgList(MI, LastTok)) {
2304 // Throw away the rest of the line.
2305 if (CurPPLexer->ParsingPreprocessorDirective)
2306 DiscardUntilEndOfDirective();
2310 // If this is a definition of a variadic C99 function-like macro, not using
2311 // the GNU named varargs extension, enabled __VA_ARGS__.
2313 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2314 // This gets unpoisoned where it is allowed.
2315 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2316 if (MI->isC99Varargs())
2317 Ident__VA_ARGS__->setIsPoisoned(false);
2319 // Read the first token after the arg list for down below.
2320 LexUnexpandedToken(Tok);
2321 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
2322 // C99 requires whitespace between the macro definition and the body. Emit
2323 // a diagnostic for something like "#define X+".
2324 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2326 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2327 // first character of a replacement list is not a character required by
2328 // subclause 5.2.1, then there shall be white-space separation between the
2329 // identifier and the replacement list.". 5.2.1 lists this set:
2330 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2331 // is irrelevant here.
2332 bool isInvalid = false;
2333 if (Tok.is(tok::at)) // @ is not in the list above.
2335 else if (Tok.is(tok::unknown)) {
2336 // If we have an unknown token, it is something strange like "`". Since
2337 // all of valid characters would have lexed into a single character
2338 // token of some sort, we know this is not a valid case.
2342 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2344 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2347 if (!Tok.is(tok::eod))
2350 // Read the rest of the macro body.
2351 if (MI->isObjectLike()) {
2352 // Object-like macros are very simple, just read their body.
2353 while (Tok.isNot(tok::eod)) {
2355 MI->AddTokenToBody(Tok);
2356 // Get the next token of the macro.
2357 LexUnexpandedToken(Tok);
2360 // Otherwise, read the body of a function-like macro. While we are at it,
2361 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2362 // parameters in function-like macro expansions.
2363 while (Tok.isNot(tok::eod)) {
2366 if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
2367 MI->AddTokenToBody(Tok);
2369 // Get the next token of the macro.
2370 LexUnexpandedToken(Tok);
2374 // If we're in -traditional mode, then we should ignore stringification
2375 // and token pasting. Mark the tokens as unknown so as not to confuse
2377 if (getLangOpts().TraditionalCPP) {
2378 Tok.setKind(tok::unknown);
2379 MI->AddTokenToBody(Tok);
2381 // Get the next token of the macro.
2382 LexUnexpandedToken(Tok);
2386 if (Tok.is(tok::hashhash)) {
2387 // If we see token pasting, check if it looks like the gcc comma
2388 // pasting extension. We'll use this information to suppress
2389 // diagnostics later on.
2391 // Get the next token of the macro.
2392 LexUnexpandedToken(Tok);
2394 if (Tok.is(tok::eod)) {
2395 MI->AddTokenToBody(LastTok);
2399 unsigned NumTokens = MI->getNumTokens();
2400 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2401 MI->getReplacementToken(NumTokens-1).is(tok::comma))
2402 MI->setHasCommaPasting();
2404 // Things look ok, add the '##' token to the macro.
2405 MI->AddTokenToBody(LastTok);
2409 // Get the next token of the macro.
2410 LexUnexpandedToken(Tok);
2412 // Check for a valid macro arg identifier.
2413 if (Tok.getIdentifierInfo() == nullptr ||
2414 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2416 // If this is assembler-with-cpp mode, we accept random gibberish after
2417 // the '#' because '#' is often a comment character. However, change
2418 // the kind of the token to tok::unknown so that the preprocessor isn't
2420 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
2421 LastTok.setKind(tok::unknown);
2422 MI->AddTokenToBody(LastTok);
2425 Diag(Tok, diag::err_pp_stringize_not_parameter)
2426 << LastTok.is(tok::hashat);
2428 // Disable __VA_ARGS__ again.
2429 Ident__VA_ARGS__->setIsPoisoned(true);
2434 // Things look ok, add the '#' and param name tokens to the macro.
2435 MI->AddTokenToBody(LastTok);
2436 MI->AddTokenToBody(Tok);
2439 // Get the next token of the macro.
2440 LexUnexpandedToken(Tok);
2444 if (MacroShadowsKeyword &&
2445 !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
2446 Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
2449 // Disable __VA_ARGS__ again.
2450 Ident__VA_ARGS__->setIsPoisoned(true);
2452 // Check that there is no paste (##) operator at the beginning or end of the
2453 // replacement list.
2454 unsigned NumTokens = MI->getNumTokens();
2455 if (NumTokens != 0) {
2456 if (MI->getReplacementToken(0).is(tok::hashhash)) {
2457 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2460 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2461 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2466 MI->setDefinitionEndLoc(LastTok.getLocation());
2468 // Finally, if this identifier already had a macro defined for it, verify that
2469 // the macro bodies are identical, and issue diagnostics if they are not.
2470 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
2471 // In Objective-C, ignore attempts to directly redefine the builtin
2472 // definitions of the ownership qualifiers. It's still possible to
2474 auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
2475 return II->isStr("__strong") ||
2476 II->isStr("__weak") ||
2477 II->isStr("__unsafe_unretained") ||
2478 II->isStr("__autoreleasing");
2480 if (getLangOpts().ObjC1 &&
2481 SourceMgr.getFileID(OtherMI->getDefinitionLoc())
2482 == getPredefinesFileID() &&
2483 isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
2484 // Warn if it changes the tokens.
2485 if ((!getDiagnostics().getSuppressSystemWarnings() ||
2486 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
2487 !MI->isIdenticalTo(*OtherMI, *this,
2488 /*Syntactic=*/LangOpts.MicrosoftExt)) {
2489 Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
2491 assert(!OtherMI->isWarnIfUnused());
2495 // It is very common for system headers to have tons of macro redefinitions
2496 // and for warnings to be disabled in system headers. If this is the case,
2497 // then don't bother calling MacroInfo::isIdenticalTo.
2498 if (!getDiagnostics().getSuppressSystemWarnings() ||
2499 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
2500 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
2501 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2503 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2504 // C++ [cpp.predefined]p4, but allow it as an extension.
2505 if (OtherMI->isBuiltinMacro())
2506 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
2507 // Macros must be identical. This means all tokens and whitespace
2508 // separation must be the same. C99 6.10.3p2.
2509 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
2510 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
2511 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2512 << MacroNameTok.getIdentifierInfo();
2513 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2516 if (OtherMI->isWarnIfUnused())
2517 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
2520 DefMacroDirective *MD =
2521 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
2523 assert(!MI->isUsed());
2524 // If we need warning for not using the macro, add its location in the
2525 // warn-because-unused-macro set. If it gets used it will be removed from set.
2526 if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
2527 !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
2528 MI->setIsWarnIfUnused(true);
2529 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2532 // If the callbacks want to know, tell them about the macro definition.
2534 Callbacks->MacroDefined(MacroNameTok, MD);
2537 /// HandleUndefDirective - Implements \#undef.
2539 void Preprocessor::HandleUndefDirective(Token &UndefTok) {
2543 ReadMacroName(MacroNameTok, MU_Undef);
2545 // Error reading macro name? If so, diagnostic already issued.
2546 if (MacroNameTok.is(tok::eod))
2549 // Check to see if this is the last token on the #undef line.
2550 CheckEndOfDirective("undef");
2552 // Okay, we have a valid identifier to undef.
2553 auto *II = MacroNameTok.getIdentifierInfo();
2554 auto MD = getMacroDefinition(II);
2556 // If the callbacks want to know, tell them about the macro #undef.
2557 // Note: no matter if the macro was defined or not.
2559 Callbacks->MacroUndefined(MacroNameTok, MD);
2561 // If the macro is not defined, this is a noop undef, just return.
2562 const MacroInfo *MI = MD.getMacroInfo();
2566 if (!MI->isUsed() && MI->isWarnIfUnused())
2567 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2569 if (MI->isWarnIfUnused())
2570 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2572 appendMacroDirective(MacroNameTok.getIdentifierInfo(),
2573 AllocateUndefMacroDirective(MacroNameTok.getLocation()));
2576 //===----------------------------------------------------------------------===//
2577 // Preprocessor Conditional Directive Handling.
2578 //===----------------------------------------------------------------------===//
2580 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2581 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2582 /// true if any tokens have been returned or pp-directives activated before this
2583 /// \#ifndef has been lexed.
2585 void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2586 bool ReadAnyTokensBeforeDirective) {
2588 Token DirectiveTok = Result;
2591 ReadMacroName(MacroNameTok);
2593 // Error reading macro name? If so, diagnostic already issued.
2594 if (MacroNameTok.is(tok::eod)) {
2595 // Skip code until we get to #endif. This helps with recovery by not
2596 // emitting an error when the #endif is reached.
2597 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2598 /*Foundnonskip*/false, /*FoundElse*/false);
2602 // Check to see if this is the last token on the #if[n]def line.
2603 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2605 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2606 auto MD = getMacroDefinition(MII);
2607 MacroInfo *MI = MD.getMacroInfo();
2609 if (CurPPLexer->getConditionalStackDepth() == 0) {
2610 // If the start of a top-level #ifdef and if the macro is not defined,
2611 // inform MIOpt that this might be the start of a proper include guard.
2612 // Otherwise it is some other form of unknown conditional which we can't
2614 if (!ReadAnyTokensBeforeDirective && !MI) {
2615 assert(isIfndef && "#ifdef shouldn't reach here");
2616 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
2618 CurPPLexer->MIOpt.EnterTopLevelConditional();
2621 // If there is a macro, process it.
2622 if (MI) // Mark it used.
2623 markMacroAsUsed(MI);
2627 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2629 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2632 // Should we include the stuff contained by this directive?
2633 if (!MI == isIfndef) {
2634 // Yes, remember that we are inside a conditional, then lex the next token.
2635 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2636 /*wasskip*/false, /*foundnonskip*/true,
2637 /*foundelse*/false);
2639 // No, skip the contents of this block.
2640 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2641 /*Foundnonskip*/false,
2642 /*FoundElse*/false);
2646 /// HandleIfDirective - Implements the \#if directive.
2648 void Preprocessor::HandleIfDirective(Token &IfToken,
2649 bool ReadAnyTokensBeforeDirective) {
2652 // Parse and evaluate the conditional expression.
2653 IdentifierInfo *IfNDefMacro = nullptr;
2654 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2655 const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2656 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2658 // If this condition is equivalent to #ifndef X, and if this is the first
2659 // directive seen, handle it for the multiple-include optimization.
2660 if (CurPPLexer->getConditionalStackDepth() == 0) {
2661 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2662 // FIXME: Pass in the location of the macro name, not the 'if' token.
2663 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
2665 CurPPLexer->MIOpt.EnterTopLevelConditional();
2669 Callbacks->If(IfToken.getLocation(),
2670 SourceRange(ConditionalBegin, ConditionalEnd),
2671 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
2673 // Should we include the stuff contained by this directive?
2674 if (ConditionalTrue) {
2675 // Yes, remember that we are inside a conditional, then lex the next token.
2676 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2677 /*foundnonskip*/true, /*foundelse*/false);
2679 // No, skip the contents of this block.
2680 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2681 /*FoundElse*/false);
2685 /// HandleEndifDirective - Implements the \#endif directive.
2687 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2690 // Check that this is the whole directive.
2691 CheckEndOfDirective("endif");
2693 PPConditionalInfo CondInfo;
2694 if (CurPPLexer->popConditionalLevel(CondInfo)) {
2695 // No conditionals on the stack: this is an #endif without an #if.
2696 Diag(EndifToken, diag::err_pp_endif_without_if);
2700 // If this the end of a top-level #endif, inform MIOpt.
2701 if (CurPPLexer->getConditionalStackDepth() == 0)
2702 CurPPLexer->MIOpt.ExitTopLevelConditional();
2704 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2705 "This code should only be reachable in the non-skipping case!");
2708 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2711 /// HandleElseDirective - Implements the \#else directive.
2713 void Preprocessor::HandleElseDirective(Token &Result) {
2716 // #else directive in a non-skipping conditional... start skipping.
2717 CheckEndOfDirective("else");
2719 PPConditionalInfo CI;
2720 if (CurPPLexer->popConditionalLevel(CI)) {
2721 Diag(Result, diag::pp_err_else_without_if);
2725 // If this is a top-level #else, inform the MIOpt.
2726 if (CurPPLexer->getConditionalStackDepth() == 0)
2727 CurPPLexer->MIOpt.EnterTopLevelConditional();
2729 // If this is a #else with a #else before it, report the error.
2730 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2733 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2735 // Finally, skip the rest of the contents of this block.
2736 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2737 /*FoundElse*/true, Result.getLocation());
2740 /// HandleElifDirective - Implements the \#elif directive.
2742 void Preprocessor::HandleElifDirective(Token &ElifToken) {
2745 // #elif directive in a non-skipping conditional... start skipping.
2746 // We don't care what the condition is, because we will always skip it (since
2747 // the block immediately before it was included).
2748 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2749 DiscardUntilEndOfDirective();
2750 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2752 PPConditionalInfo CI;
2753 if (CurPPLexer->popConditionalLevel(CI)) {
2754 Diag(ElifToken, diag::pp_err_elif_without_if);
2758 // If this is a top-level #elif, inform the MIOpt.
2759 if (CurPPLexer->getConditionalStackDepth() == 0)
2760 CurPPLexer->MIOpt.EnterTopLevelConditional();
2762 // If this is a #elif with a #else before it, report the error.
2763 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2766 Callbacks->Elif(ElifToken.getLocation(),
2767 SourceRange(ConditionalBegin, ConditionalEnd),
2768 PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
2770 // Finally, skip the rest of the contents of this block.
2771 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2772 /*FoundElse*/CI.FoundElse,
2773 ElifToken.getLocation());