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/Basic/CharInfo.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/IdentifierTable.h"
18 #include "clang/Basic/LangOptions.h"
19 #include "clang/Basic/Module.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Basic/TokenKinds.h"
23 #include "clang/Lex/CodeCompletionHandler.h"
24 #include "clang/Lex/HeaderSearch.h"
25 #include "clang/Lex/LexDiagnostic.h"
26 #include "clang/Lex/LiteralSupport.h"
27 #include "clang/Lex/MacroInfo.h"
28 #include "clang/Lex/ModuleLoader.h"
29 #include "clang/Lex/ModuleMap.h"
30 #include "clang/Lex/PPCallbacks.h"
31 #include "clang/Lex/Pragma.h"
32 #include "clang/Lex/Preprocessor.h"
33 #include "clang/Lex/PreprocessorOptions.h"
34 #include "clang/Lex/PTHLexer.h"
35 #include "clang/Lex/Token.h"
36 #include "llvm/ADT/ArrayRef.h"
37 #include "llvm/ADT/SmallString.h"
38 #include "llvm/ADT/SmallVector.h"
39 #include "llvm/ADT/STLExtras.h"
40 #include "llvm/ADT/StringSwitch.h"
41 #include "llvm/ADT/StringRef.h"
42 #include "llvm/Support/AlignOf.h"
43 #include "llvm/Support/ErrorHandling.h"
44 #include "llvm/Support/Path.h"
52 using namespace clang;
54 //===----------------------------------------------------------------------===//
55 // Utility Methods for Preprocessor Directive Handling.
56 //===----------------------------------------------------------------------===//
58 MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
59 auto *MIChain = new (BP) MacroInfoChain{L, MIChainHead};
60 MIChainHead = MIChain;
64 DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
66 return new (BP) DefMacroDirective(MI, Loc);
70 Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
71 return new (BP) UndefMacroDirective(UndefLoc);
74 VisibilityMacroDirective *
75 Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
77 return new (BP) VisibilityMacroDirective(Loc, isPublic);
80 /// \brief Read and discard all tokens remaining on the current line until
81 /// the tok::eod token is found.
82 void Preprocessor::DiscardUntilEndOfDirective() {
85 LexUnexpandedToken(Tmp);
86 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
87 } while (Tmp.isNot(tok::eod));
90 /// \brief Enumerates possible cases of #define/#undef a reserved identifier.
92 MD_NoWarn, //> Not a reserved identifier
93 MD_KeywordDef, //> Macro hides keyword, enabled by default
94 MD_ReservedMacro //> #define of #undef reserved id, disabled by default
97 /// \brief Checks if the specified identifier is reserved in the specified
99 /// This function does not check if the identifier is a keyword.
100 static bool isReservedId(StringRef Text, const LangOptions &Lang) {
101 // C++ [macro.names], C11 7.1.3:
102 // All identifiers that begin with an underscore and either an uppercase
103 // letter or another underscore are always reserved for any use.
104 if (Text.size() >= 2 && Text[0] == '_' &&
105 (isUppercase(Text[1]) || Text[1] == '_'))
107 // C++ [global.names]
108 // Each name that contains a double underscore ... is reserved to the
109 // implementation for any use.
110 if (Lang.CPlusPlus) {
111 if (Text.find("__") != StringRef::npos)
117 static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
118 const LangOptions &Lang = PP.getLangOpts();
119 StringRef Text = II->getName();
120 if (isReservedId(Text, Lang))
121 return MD_ReservedMacro;
122 if (II->isKeyword(Lang))
123 return MD_KeywordDef;
124 if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final")))
125 return MD_KeywordDef;
129 static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
130 const LangOptions &Lang = PP.getLangOpts();
131 StringRef Text = II->getName();
132 // Do not warn on keyword undef. It is generally harmless and widely used.
133 if (isReservedId(Text, Lang))
134 return MD_ReservedMacro;
138 // Return true if we want to issue a diagnostic by default if we
139 // encounter this name in a #include with the wrong case. For now,
140 // this includes the standard C and C++ headers, Posix headers,
141 // and Boost headers. Improper case for these #includes is a
142 // potential portability issue.
143 static bool warnByDefaultOnWrongCase(StringRef Include) {
144 // If the first component of the path is "boost", treat this like a standard header
145 // for the purposes of diagnostics.
146 if (::llvm::sys::path::begin(Include)->equals_lower("boost"))
149 // "condition_variable" is the longest standard header name at 18 characters.
150 // If the include file name is longer than that, it can't be a standard header.
151 static const size_t MaxStdHeaderNameLen = 18u;
152 if (Include.size() > MaxStdHeaderNameLen)
155 // Lowercase and normalize the search string.
156 SmallString<32> LowerInclude{Include};
157 for (char &Ch : LowerInclude) {
158 // In the ASCII range?
159 if (static_cast<unsigned char>(Ch) > 0x7f)
160 return false; // Can't be a standard header
162 if (Ch >= 'A' && Ch <= 'Z')
164 // Normalize path separators for comparison purposes.
165 else if (::llvm::sys::path::is_separator(Ch))
169 // The standard C/C++ and Posix headers
170 return llvm::StringSwitch<bool>(LowerInclude)
172 .Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true)
173 .Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true)
174 .Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true)
175 .Cases("stdatomic.h", "stdbool.h", "stddef.h", "stdint.h", "stdio.h", true)
176 .Cases("stdlib.h", "stdnoreturn.h", "string.h", "tgmath.h", "threads.h", true)
177 .Cases("time.h", "uchar.h", "wchar.h", "wctype.h", true)
179 // C++ headers for C library facilities
180 .Cases("cassert", "ccomplex", "cctype", "cerrno", "cfenv", true)
181 .Cases("cfloat", "cinttypes", "ciso646", "climits", "clocale", true)
182 .Cases("cmath", "csetjmp", "csignal", "cstdalign", "cstdarg", true)
183 .Cases("cstdbool", "cstddef", "cstdint", "cstdio", "cstdlib", true)
184 .Cases("cstring", "ctgmath", "ctime", "cuchar", "cwchar", true)
185 .Case("cwctype", true)
187 // C++ library headers
188 .Cases("algorithm", "fstream", "list", "regex", "thread", true)
189 .Cases("array", "functional", "locale", "scoped_allocator", "tuple", true)
190 .Cases("atomic", "future", "map", "set", "type_traits", true)
191 .Cases("bitset", "initializer_list", "memory", "shared_mutex", "typeindex", true)
192 .Cases("chrono", "iomanip", "mutex", "sstream", "typeinfo", true)
193 .Cases("codecvt", "ios", "new", "stack", "unordered_map", true)
194 .Cases("complex", "iosfwd", "numeric", "stdexcept", "unordered_set", true)
195 .Cases("condition_variable", "iostream", "ostream", "streambuf", "utility", true)
196 .Cases("deque", "istream", "queue", "string", "valarray", true)
197 .Cases("exception", "iterator", "random", "strstream", "vector", true)
198 .Cases("forward_list", "limits", "ratio", "system_error", true)
200 // POSIX headers (which aren't also C headers)
201 .Cases("aio.h", "arpa/inet.h", "cpio.h", "dirent.h", "dlfcn.h", true)
202 .Cases("fcntl.h", "fmtmsg.h", "fnmatch.h", "ftw.h", "glob.h", true)
203 .Cases("grp.h", "iconv.h", "langinfo.h", "libgen.h", "monetary.h", true)
204 .Cases("mqueue.h", "ndbm.h", "net/if.h", "netdb.h", "netinet/in.h", true)
205 .Cases("netinet/tcp.h", "nl_types.h", "poll.h", "pthread.h", "pwd.h", true)
206 .Cases("regex.h", "sched.h", "search.h", "semaphore.h", "spawn.h", true)
207 .Cases("strings.h", "stropts.h", "sys/ipc.h", "sys/mman.h", "sys/msg.h", true)
208 .Cases("sys/resource.h", "sys/select.h", "sys/sem.h", "sys/shm.h", "sys/socket.h", true)
209 .Cases("sys/stat.h", "sys/statvfs.h", "sys/time.h", "sys/times.h", "sys/types.h", true)
210 .Cases("sys/uio.h", "sys/un.h", "sys/utsname.h", "sys/wait.h", "syslog.h", true)
211 .Cases("tar.h", "termios.h", "trace.h", "ulimit.h", true)
212 .Cases("unistd.h", "utime.h", "utmpx.h", "wordexp.h", true)
216 bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
218 // Missing macro name?
219 if (MacroNameTok.is(tok::eod))
220 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
222 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
224 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
226 if (II->isCPlusPlusOperatorKeyword()) {
227 // C++ 2.5p2: Alternative tokens behave the same as its primary token
228 // except for their spellings.
229 Diag(MacroNameTok, getLangOpts().MicrosoftExt
230 ? diag::ext_pp_operator_used_as_macro_name
231 : diag::err_pp_operator_used_as_macro_name)
232 << II << MacroNameTok.getKind();
233 // Allow #defining |and| and friends for Microsoft compatibility or
234 // recovery when legacy C headers are included in C++.
237 if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
238 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
239 return Diag(MacroNameTok, diag::err_defined_macro_name);
242 if (isDefineUndef == MU_Undef) {
243 auto *MI = getMacroInfo(II);
244 if (MI && MI->isBuiltinMacro()) {
245 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
246 // and C++ [cpp.predefined]p4], but allow it as an extension.
247 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
251 // If defining/undefining reserved identifier or a keyword, we need to issue
253 SourceLocation MacroNameLoc = MacroNameTok.getLocation();
256 if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
257 (SourceMgr.getBufferName(MacroNameLoc) != "<built-in>")) {
258 MacroDiag D = MD_NoWarn;
259 if (isDefineUndef == MU_Define) {
260 D = shouldWarnOnMacroDef(*this, II);
262 else if (isDefineUndef == MU_Undef)
263 D = shouldWarnOnMacroUndef(*this, II);
264 if (D == MD_KeywordDef) {
265 // We do not want to warn on some patterns widely used in configuration
266 // scripts. This requires analyzing next tokens, so do not issue warnings
267 // now, only inform caller.
271 if (D == MD_ReservedMacro)
272 Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
275 // Okay, we got a good identifier.
279 /// \brief Lex and validate a macro name, which occurs after a
280 /// \#define or \#undef.
282 /// This sets the token kind to eod and discards the rest of the macro line if
283 /// the macro name is invalid.
285 /// \param MacroNameTok Token that is expected to be a macro name.
286 /// \param isDefineUndef Context in which macro is used.
287 /// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
288 void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
290 // Read the token, don't allow macro expansion on it.
291 LexUnexpandedToken(MacroNameTok);
293 if (MacroNameTok.is(tok::code_completion)) {
295 CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
296 setCodeCompletionReached();
297 LexUnexpandedToken(MacroNameTok);
300 if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
303 // Invalid macro name, read and discard the rest of the line and set the
304 // token kind to tok::eod if necessary.
305 if (MacroNameTok.isNot(tok::eod)) {
306 MacroNameTok.setKind(tok::eod);
307 DiscardUntilEndOfDirective();
311 /// \brief Ensure that the next token is a tok::eod token.
313 /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
314 /// true, then we consider macros that expand to zero tokens as being ok.
315 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
317 // Lex unexpanded tokens for most directives: macros might expand to zero
318 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
319 // #line) allow empty macros.
323 LexUnexpandedToken(Tmp);
325 // There should be no tokens after the directive, but we allow them as an
327 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
328 LexUnexpandedToken(Tmp);
330 if (Tmp.isNot(tok::eod)) {
331 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
332 // or if this is a macro-style preprocessing directive, because it is more
333 // trouble than it is worth to insert /**/ and check that there is no /**/
334 // in the range also.
336 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
338 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
339 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
340 DiscardUntilEndOfDirective();
344 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
345 /// decided that the subsequent tokens are in the \#if'd out portion of the
346 /// file. Lex the rest of the file, until we see an \#endif. If
347 /// FoundNonSkipPortion is true, then we have already emitted code for part of
348 /// this \#if directive, so \#else/\#elif blocks should never be entered.
349 /// If ElseOk is true, then \#else directives are ok, if not, then we have
350 /// already seen one so a \#else directive is a duplicate. When this returns,
351 /// the caller can lex the first valid token.
352 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
353 bool FoundNonSkipPortion,
355 SourceLocation ElseLoc) {
357 assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
359 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
360 FoundNonSkipPortion, FoundElse);
363 PTHSkipExcludedConditionalBlock();
367 // Enter raw mode to disable identifier lookup (and thus macro expansion),
368 // disabling warnings, etc.
369 CurPPLexer->LexingRawMode = true;
374 if (Tok.is(tok::code_completion)) {
376 CodeComplete->CodeCompleteInConditionalExclusion();
377 setCodeCompletionReached();
381 // If this is the end of the buffer, we have an error.
382 if (Tok.is(tok::eof)) {
383 // Emit errors for each unterminated conditional on the stack, including
385 while (!CurPPLexer->ConditionalStack.empty()) {
386 if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
387 Diag(CurPPLexer->ConditionalStack.back().IfLoc,
388 diag::err_pp_unterminated_conditional);
389 CurPPLexer->ConditionalStack.pop_back();
392 // Just return and let the caller lex after this #include.
396 // If this token is not a preprocessor directive, just skip it.
397 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
400 // We just parsed a # character at the start of a line, so we're in
401 // directive mode. Tell the lexer this so any newlines we see will be
402 // converted into an EOD token (this terminates the macro).
403 CurPPLexer->ParsingPreprocessorDirective = true;
404 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
407 // Read the next token, the directive flavor.
408 LexUnexpandedToken(Tok);
410 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
411 // something bogus), skip it.
412 if (Tok.isNot(tok::raw_identifier)) {
413 CurPPLexer->ParsingPreprocessorDirective = false;
414 // Restore comment saving mode.
415 if (CurLexer) CurLexer->resetExtendedTokenMode();
419 // If the first letter isn't i or e, it isn't intesting to us. We know that
420 // this is safe in the face of spelling differences, because there is no way
421 // to spell an i/e in a strange way that is another letter. Skipping this
422 // allows us to avoid looking up the identifier info for #define/#undef and
423 // other common directives.
424 StringRef RI = Tok.getRawIdentifier();
426 char FirstChar = RI[0];
427 if (FirstChar >= 'a' && FirstChar <= 'z' &&
428 FirstChar != 'i' && FirstChar != 'e') {
429 CurPPLexer->ParsingPreprocessorDirective = false;
430 // Restore comment saving mode.
431 if (CurLexer) CurLexer->resetExtendedTokenMode();
435 // Get the identifier name without trigraphs or embedded newlines. Note
436 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
438 char DirectiveBuf[20];
440 if (!Tok.needsCleaning() && RI.size() < 20) {
443 std::string DirectiveStr = getSpelling(Tok);
444 size_t IdLen = DirectiveStr.size();
446 CurPPLexer->ParsingPreprocessorDirective = false;
447 // Restore comment saving mode.
448 if (CurLexer) CurLexer->resetExtendedTokenMode();
451 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
452 Directive = StringRef(DirectiveBuf, IdLen);
455 if (Directive.startswith("if")) {
456 StringRef Sub = Directive.substr(2);
457 if (Sub.empty() || // "if"
458 Sub == "def" || // "ifdef"
459 Sub == "ndef") { // "ifndef"
460 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
461 // bother parsing the condition.
462 DiscardUntilEndOfDirective();
463 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
464 /*foundnonskip*/false,
467 } else if (Directive[0] == 'e') {
468 StringRef Sub = Directive.substr(1);
469 if (Sub == "ndif") { // "endif"
470 PPConditionalInfo CondInfo;
471 CondInfo.WasSkipping = true; // Silence bogus warning.
472 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
473 (void)InCond; // Silence warning in no-asserts mode.
474 assert(!InCond && "Can't be skipping if not in a conditional!");
476 // If we popped the outermost skipping block, we're done skipping!
477 if (!CondInfo.WasSkipping) {
478 // Restore the value of LexingRawMode so that trailing comments
479 // are handled correctly, if we've reached the outermost block.
480 CurPPLexer->LexingRawMode = false;
481 CheckEndOfDirective("endif");
482 CurPPLexer->LexingRawMode = true;
484 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
487 DiscardUntilEndOfDirective();
489 } else if (Sub == "lse") { // "else".
490 // #else directive in a skipping conditional. If not in some other
491 // skipping conditional, and if #else hasn't already been seen, enter it
492 // as a non-skipping conditional.
493 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
495 // If this is a #else with a #else before it, report the error.
496 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
498 // Note that we've seen a #else in this conditional.
499 CondInfo.FoundElse = true;
501 // If the conditional is at the top level, and the #if block wasn't
502 // entered, enter the #else block now.
503 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
504 CondInfo.FoundNonSkip = true;
505 // Restore the value of LexingRawMode so that trailing comments
506 // are handled correctly.
507 CurPPLexer->LexingRawMode = false;
508 CheckEndOfDirective("else");
509 CurPPLexer->LexingRawMode = true;
511 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
514 DiscardUntilEndOfDirective(); // C99 6.10p4.
516 } else if (Sub == "lif") { // "elif".
517 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
519 // If this is a #elif with a #else before it, report the error.
520 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
522 // If this is in a skipping block or if we're already handled this #if
523 // block, don't bother parsing the condition.
524 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
525 DiscardUntilEndOfDirective();
527 const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
528 // Restore the value of LexingRawMode so that identifiers are
529 // looked up, etc, inside the #elif expression.
530 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
531 CurPPLexer->LexingRawMode = false;
532 IdentifierInfo *IfNDefMacro = nullptr;
533 const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro).Conditional;
534 CurPPLexer->LexingRawMode = true;
536 const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
537 Callbacks->Elif(Tok.getLocation(),
538 SourceRange(CondBegin, CondEnd),
539 (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
541 // If this condition is true, enter it!
543 CondInfo.FoundNonSkip = true;
550 CurPPLexer->ParsingPreprocessorDirective = false;
551 // Restore comment saving mode.
552 if (CurLexer) CurLexer->resetExtendedTokenMode();
555 // Finally, if we are out of the conditional (saw an #endif or ran off the end
556 // of the file, just stop skipping and return to lexing whatever came after
558 CurPPLexer->LexingRawMode = false;
561 SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
562 Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
566 void Preprocessor::PTHSkipExcludedConditionalBlock() {
569 assert(CurPTHLexer->LexingRawMode == false);
571 // Skip to the next '#else', '#elif', or #endif.
572 if (CurPTHLexer->SkipBlock()) {
573 // We have reached an #endif. Both the '#' and 'endif' tokens
574 // have been consumed by the PTHLexer. Just pop off the condition level.
575 PPConditionalInfo CondInfo;
576 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
577 (void)InCond; // Silence warning in no-asserts mode.
578 assert(!InCond && "Can't be skipping if not in a conditional!");
582 // We have reached a '#else' or '#elif'. Lex the next token to get
583 // the directive flavor.
585 LexUnexpandedToken(Tok);
587 // We can actually look up the IdentifierInfo here since we aren't in
589 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
591 if (K == tok::pp_else) {
592 // #else: Enter the else condition. We aren't in a nested condition
593 // since we skip those. We're always in the one matching the last
594 // blocked we skipped.
595 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
596 // Note that we've seen a #else in this conditional.
597 CondInfo.FoundElse = true;
599 // If the #if block wasn't entered then enter the #else block now.
600 if (!CondInfo.FoundNonSkip) {
601 CondInfo.FoundNonSkip = true;
603 // Scan until the eod token.
604 CurPTHLexer->ParsingPreprocessorDirective = true;
605 DiscardUntilEndOfDirective();
606 CurPTHLexer->ParsingPreprocessorDirective = false;
611 // Otherwise skip this block.
615 assert(K == tok::pp_elif);
616 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
618 // If this is a #elif with a #else before it, report the error.
619 if (CondInfo.FoundElse)
620 Diag(Tok, diag::pp_err_elif_after_else);
622 // If this is in a skipping block or if we're already handled this #if
623 // block, don't bother parsing the condition. We just skip this block.
624 if (CondInfo.FoundNonSkip)
627 // Evaluate the condition of the #elif.
628 IdentifierInfo *IfNDefMacro = nullptr;
629 CurPTHLexer->ParsingPreprocessorDirective = true;
630 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro).Conditional;
631 CurPTHLexer->ParsingPreprocessorDirective = false;
633 // If this condition is true, enter it!
635 CondInfo.FoundNonSkip = true;
639 // Otherwise, skip this block and go to the next one.
643 Module *Preprocessor::getModuleForLocation(SourceLocation Loc) {
644 if (!SourceMgr.isInMainFile(Loc)) {
645 // Try to determine the module of the include directive.
646 // FIXME: Look into directly passing the FileEntry from LookupFile instead.
647 FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
648 if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
649 // The include comes from an included file.
650 return HeaderInfo.getModuleMap()
651 .findModuleForHeader(EntryOfIncl)
656 // This is either in the main file or not in a file at all. It belongs
657 // to the current module, if there is one.
658 return getLangOpts().CurrentModule.empty()
660 : HeaderInfo.lookupModule(getLangOpts().CurrentModule);
664 Preprocessor::getModuleHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
666 SourceLocation Loc) {
667 assert(M && "no module to include");
669 // If we have a module import syntax, we shouldn't include a header to
670 // make a particular module visible.
671 if (getLangOpts().ObjC2)
674 Module *TopM = M->getTopLevelModule();
675 Module *IncM = getModuleForLocation(IncLoc);
677 // Walk up through the include stack, looking through textual headers of M
678 // until we hit a non-textual header that we can #include. (We assume textual
679 // headers of a module with non-textual headers aren't meant to be used to
680 // import entities from the module.)
681 auto &SM = getSourceManager();
682 while (!Loc.isInvalid() && !SM.isInMainFile(Loc)) {
683 auto ID = SM.getFileID(SM.getExpansionLoc(Loc));
684 auto *FE = SM.getFileEntryForID(ID);
688 bool InTextualHeader = false;
689 for (auto Header : HeaderInfo.getModuleMap().findAllModulesForHeader(FE)) {
690 if (!Header.getModule()->isSubModuleOf(TopM))
693 if (!(Header.getRole() & ModuleMap::TextualHeader)) {
694 // If this is an accessible, non-textual header of M's top-level module
695 // that transitively includes the given location and makes the
696 // corresponding module visible, this is the thing to #include.
697 if (Header.isAccessibleFrom(IncM))
700 // It's in a private header; we can't #include it.
701 // FIXME: If there's a public header in some module that re-exports it,
702 // then we could suggest including that, but it's not clear that's the
703 // expected way to make this entity visible.
707 InTextualHeader = true;
710 if (!InTextualHeader)
713 Loc = SM.getIncludeLoc(ID);
719 const FileEntry *Preprocessor::LookupFile(
720 SourceLocation FilenameLoc, StringRef Filename, bool isAngled,
721 const DirectoryLookup *FromDir, const FileEntry *FromFile,
722 const DirectoryLookup *&CurDir, SmallVectorImpl<char> *SearchPath,
723 SmallVectorImpl<char> *RelativePath,
724 ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, bool SkipCache) {
725 Module *RequestingModule = getModuleForLocation(FilenameLoc);
726 bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc);
728 // If the header lookup mechanism may be relative to the current inclusion
729 // stack, record the parent #includes.
730 SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 16>
732 bool BuildSystemModule = false;
733 if (!FromDir && !FromFile) {
734 FileID FID = getCurrentFileLexer()->getFileID();
735 const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
737 // If there is no file entry associated with this file, it must be the
738 // predefines buffer or the module includes buffer. Any other file is not
739 // lexed with a normal lexer, so it won't be scanned for preprocessor
742 // If we have the predefines buffer, resolve #include references (which come
743 // from the -include command line argument) from the current working
744 // directory instead of relative to the main file.
746 // If we have the module includes buffer, resolve #include references (which
747 // come from header declarations in the module map) relative to the module
750 if (FID == SourceMgr.getMainFileID() && MainFileDir) {
751 Includers.push_back(std::make_pair(nullptr, MainFileDir));
752 BuildSystemModule = getCurrentModule()->IsSystem;
753 } else if ((FileEnt =
754 SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())))
755 Includers.push_back(std::make_pair(FileEnt, FileMgr.getDirectory(".")));
757 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
760 // MSVC searches the current include stack from top to bottom for
761 // headers included by quoted include directives.
762 // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
763 if (LangOpts.MSVCCompat && !isAngled) {
764 for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
765 if (IsFileLexer(ISEntry))
766 if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
767 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
772 CurDir = CurDirLookup;
775 // We're supposed to start looking from after a particular file. Search
776 // the include path until we find that file or run out of files.
777 const DirectoryLookup *TmpCurDir = CurDir;
778 const DirectoryLookup *TmpFromDir = nullptr;
779 while (const FileEntry *FE = HeaderInfo.LookupFile(
780 Filename, FilenameLoc, isAngled, TmpFromDir, TmpCurDir,
781 Includers, SearchPath, RelativePath, RequestingModule,
782 SuggestedModule, /*IsMapped=*/nullptr, SkipCache)) {
783 // Keep looking as if this file did a #include_next.
784 TmpFromDir = TmpCurDir;
786 if (FE == FromFile) {
788 FromDir = TmpFromDir;
795 // Do a standard file entry lookup.
796 const FileEntry *FE = HeaderInfo.LookupFile(
797 Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
798 RelativePath, RequestingModule, SuggestedModule, IsMapped, SkipCache,
801 if (SuggestedModule && !LangOpts.AsmPreprocessor)
802 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
803 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
808 const FileEntry *CurFileEnt;
809 // Otherwise, see if this is a subframework header. If so, this is relative
810 // to one of the headers on the #include stack. Walk the list of the current
811 // headers on the #include stack and pass them to HeaderInfo.
813 if ((CurFileEnt = CurPPLexer->getFileEntry())) {
814 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
815 SearchPath, RelativePath,
818 if (SuggestedModule && !LangOpts.AsmPreprocessor)
819 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
820 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
827 for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
828 if (IsFileLexer(ISEntry)) {
829 if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
830 if ((FE = HeaderInfo.LookupSubframeworkHeader(
831 Filename, CurFileEnt, SearchPath, RelativePath,
832 RequestingModule, SuggestedModule))) {
833 if (SuggestedModule && !LangOpts.AsmPreprocessor)
834 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
835 RequestingModule, RequestingModuleIsModuleInterface,
836 FilenameLoc, Filename, FE);
843 // Otherwise, we really couldn't find the file.
847 //===----------------------------------------------------------------------===//
848 // Preprocessor Directive Handling.
849 //===----------------------------------------------------------------------===//
851 class Preprocessor::ResetMacroExpansionHelper {
853 ResetMacroExpansionHelper(Preprocessor *pp)
854 : PP(pp), save(pp->DisableMacroExpansion) {
855 if (pp->MacroExpansionInDirectivesOverride)
856 pp->DisableMacroExpansion = false;
859 ~ResetMacroExpansionHelper() {
860 PP->DisableMacroExpansion = save;
868 /// HandleDirective - This callback is invoked when the lexer sees a # token
869 /// at the start of a line. This consumes the directive, modifies the
870 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
871 /// read is the correct one.
872 void Preprocessor::HandleDirective(Token &Result) {
873 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
875 // We just parsed a # character at the start of a line, so we're in directive
876 // mode. Tell the lexer this so any newlines we see will be converted into an
877 // EOD token (which terminates the directive).
878 CurPPLexer->ParsingPreprocessorDirective = true;
879 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
881 bool ImmediatelyAfterTopLevelIfndef =
882 CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
883 CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
887 // We are about to read a token. For the multiple-include optimization FA to
888 // work, we have to remember if we had read any tokens *before* this
890 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
892 // Save the '#' token in case we need to return it later.
893 Token SavedHash = Result;
895 // Read the next token, the directive flavor. This isn't expanded due to
897 LexUnexpandedToken(Result);
899 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
904 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
905 // not support this for #include-like directives, since that can result in
906 // terrible diagnostics, and does not work in GCC.
908 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
909 switch (II->getPPKeywordID()) {
910 case tok::pp_include:
912 case tok::pp_include_next:
913 case tok::pp___include_macros:
915 Diag(Result, diag::err_embedded_directive) << II->getName();
916 DiscardUntilEndOfDirective();
922 Diag(Result, diag::ext_embedded_directive);
925 // Temporarily enable macro expansion if set so
926 // and reset to previous state when returning from this function.
927 ResetMacroExpansionHelper helper(this);
929 switch (Result.getKind()) {
931 return; // null directive.
932 case tok::code_completion:
934 CodeComplete->CodeCompleteDirective(
935 CurPPLexer->getConditionalStackDepth() > 0);
936 setCodeCompletionReached();
938 case tok::numeric_constant: // # 7 GNU line marker directive.
939 if (getLangOpts().AsmPreprocessor)
940 break; // # 4 is not a preprocessor directive in .S files.
941 return HandleDigitDirective(Result);
943 IdentifierInfo *II = Result.getIdentifierInfo();
944 if (!II) break; // Not an identifier.
946 // Ask what the preprocessor keyword ID is.
947 switch (II->getPPKeywordID()) {
949 // C99 6.10.1 - Conditional Inclusion.
951 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
953 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
955 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
957 return HandleElifDirective(Result);
959 return HandleElseDirective(Result);
961 return HandleEndifDirective(Result);
963 // C99 6.10.2 - Source File Inclusion.
964 case tok::pp_include:
966 return HandleIncludeDirective(SavedHash.getLocation(), Result);
967 case tok::pp___include_macros:
969 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
971 // C99 6.10.3 - Macro Replacement.
973 return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
975 return HandleUndefDirective();
977 // C99 6.10.4 - Line Control.
979 return HandleLineDirective();
981 // C99 6.10.5 - Error Directive.
983 return HandleUserDiagnosticDirective(Result, false);
985 // C99 6.10.6 - Pragma Directive.
987 return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
991 return HandleImportDirective(SavedHash.getLocation(), Result);
992 case tok::pp_include_next:
993 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
995 case tok::pp_warning:
996 Diag(Result, diag::ext_pp_warning_directive);
997 return HandleUserDiagnosticDirective(Result, true);
999 return HandleIdentSCCSDirective(Result);
1001 return HandleIdentSCCSDirective(Result);
1002 case tok::pp_assert:
1003 //isExtension = true; // FIXME: implement #assert
1005 case tok::pp_unassert:
1006 //isExtension = true; // FIXME: implement #unassert
1009 case tok::pp___public_macro:
1010 if (getLangOpts().Modules)
1011 return HandleMacroPublicDirective(Result);
1014 case tok::pp___private_macro:
1015 if (getLangOpts().Modules)
1016 return HandleMacroPrivateDirective();
1022 // If this is a .S file, treat unknown # directives as non-preprocessor
1023 // directives. This is important because # may be a comment or introduce
1024 // various pseudo-ops. Just return the # token and push back the following
1025 // token to be lexed next time.
1026 if (getLangOpts().AsmPreprocessor) {
1027 auto Toks = llvm::make_unique<Token[]>(2);
1028 // Return the # and the token after it.
1029 Toks[0] = SavedHash;
1032 // If the second token is a hashhash token, then we need to translate it to
1033 // unknown so the token lexer doesn't try to perform token pasting.
1034 if (Result.is(tok::hashhash))
1035 Toks[1].setKind(tok::unknown);
1037 // Enter this token stream so that we re-lex the tokens. Make sure to
1038 // enable macro expansion, in case the token after the # is an identifier
1039 // that is expanded.
1040 EnterTokenStream(std::move(Toks), 2, false);
1044 // If we reached here, the preprocessing token is not valid!
1045 Diag(Result, diag::err_pp_invalid_directive);
1047 // Read the rest of the PP line.
1048 DiscardUntilEndOfDirective();
1050 // Okay, we're done parsing the directive.
1053 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
1054 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
1055 static bool GetLineValue(Token &DigitTok, unsigned &Val,
1056 unsigned DiagID, Preprocessor &PP,
1057 bool IsGNULineDirective=false) {
1058 if (DigitTok.isNot(tok::numeric_constant)) {
1059 PP.Diag(DigitTok, DiagID);
1061 if (DigitTok.isNot(tok::eod))
1062 PP.DiscardUntilEndOfDirective();
1066 SmallString<64> IntegerBuffer;
1067 IntegerBuffer.resize(DigitTok.getLength());
1068 const char *DigitTokBegin = &IntegerBuffer[0];
1069 bool Invalid = false;
1070 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
1074 // Verify that we have a simple digit-sequence, and compute the value. This
1075 // is always a simple digit string computed in decimal, so we do this manually
1078 for (unsigned i = 0; i != ActualLength; ++i) {
1079 // C++1y [lex.fcon]p1:
1080 // Optional separating single quotes in a digit-sequence are ignored
1081 if (DigitTokBegin[i] == '\'')
1084 if (!isDigit(DigitTokBegin[i])) {
1085 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
1086 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
1087 PP.DiscardUntilEndOfDirective();
1091 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
1092 if (NextVal < Val) { // overflow.
1093 PP.Diag(DigitTok, DiagID);
1094 PP.DiscardUntilEndOfDirective();
1100 if (DigitTokBegin[0] == '0' && Val)
1101 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
1102 << IsGNULineDirective;
1107 /// \brief Handle a \#line directive: C99 6.10.4.
1109 /// The two acceptable forms are:
1111 /// # line digit-sequence
1112 /// # line digit-sequence "s-char-sequence"
1114 void Preprocessor::HandleLineDirective() {
1115 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
1120 // Validate the number and convert it to an unsigned.
1122 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
1126 Diag(DigitTok, diag::ext_pp_line_zero);
1128 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1129 // number greater than 2147483647". C90 requires that the line # be <= 32767.
1130 unsigned LineLimit = 32768U;
1131 if (LangOpts.C99 || LangOpts.CPlusPlus11)
1132 LineLimit = 2147483648U;
1133 if (LineNo >= LineLimit)
1134 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
1135 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
1136 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
1138 int FilenameID = -1;
1142 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1143 // string followed by eod.
1144 if (StrTok.is(tok::eod))
1146 else if (StrTok.isNot(tok::string_literal)) {
1147 Diag(StrTok, diag::err_pp_line_invalid_filename);
1148 return DiscardUntilEndOfDirective();
1149 } else if (StrTok.hasUDSuffix()) {
1150 Diag(StrTok, diag::err_invalid_string_udl);
1151 return DiscardUntilEndOfDirective();
1153 // Parse and validate the string, converting it into a unique ID.
1154 StringLiteralParser Literal(StrTok, *this);
1155 assert(Literal.isAscii() && "Didn't allow wide strings in");
1156 if (Literal.hadError)
1157 return DiscardUntilEndOfDirective();
1158 if (Literal.Pascal) {
1159 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1160 return DiscardUntilEndOfDirective();
1162 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1164 // Verify that there is nothing after the string, other than EOD. Because
1165 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1166 CheckEndOfDirective("line", true);
1169 // Take the file kind of the file containing the #line directive. #line
1170 // directives are often used for generated sources from the same codebase, so
1171 // the new file should generally be classified the same way as the current
1172 // file. This is visible in GCC's pre-processed output, which rewrites #line
1173 // to GNU line markers.
1174 SrcMgr::CharacteristicKind FileKind =
1175 SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1177 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, false,
1181 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1182 PPCallbacks::RenameFile, FileKind);
1185 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1186 /// marker directive.
1187 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1188 SrcMgr::CharacteristicKind &FileKind,
1193 if (FlagTok.is(tok::eod)) return false;
1194 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1201 if (FlagTok.is(tok::eod)) return false;
1202 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1204 } else if (FlagVal == 2) {
1207 SourceManager &SM = PP.getSourceManager();
1208 // If we are leaving the current presumed file, check to make sure the
1209 // presumed include stack isn't empty!
1211 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1212 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1213 if (PLoc.isInvalid())
1216 // If there is no include loc (main file) or if the include loc is in a
1217 // different physical file, then we aren't in a "1" line marker flag region.
1218 SourceLocation IncLoc = PLoc.getIncludeLoc();
1219 if (IncLoc.isInvalid() ||
1220 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
1221 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1222 PP.DiscardUntilEndOfDirective();
1227 if (FlagTok.is(tok::eod)) return false;
1228 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1232 // We must have 3 if there are still flags.
1234 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1235 PP.DiscardUntilEndOfDirective();
1239 FileKind = SrcMgr::C_System;
1242 if (FlagTok.is(tok::eod)) return false;
1243 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1246 // We must have 4 if there is yet another flag.
1248 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1249 PP.DiscardUntilEndOfDirective();
1253 FileKind = SrcMgr::C_ExternCSystem;
1256 if (FlagTok.is(tok::eod)) return false;
1258 // There are no more valid flags here.
1259 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1260 PP.DiscardUntilEndOfDirective();
1264 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1265 /// one of the following forms:
1268 /// # 42 "file" ('1' | '2')?
1269 /// # 42 "file" ('1' | '2')? '3' '4'?
1271 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1272 // Validate the number and convert it to an unsigned. GNU does not have a
1273 // line # limit other than it fit in 32-bits.
1275 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1282 bool IsFileEntry = false, IsFileExit = false;
1283 int FilenameID = -1;
1284 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1286 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1287 // string followed by eod.
1288 if (StrTok.is(tok::eod)) {
1289 // Treat this like "#line NN", which doesn't change file characteristics.
1290 FileKind = SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1291 } else if (StrTok.isNot(tok::string_literal)) {
1292 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1293 return DiscardUntilEndOfDirective();
1294 } else if (StrTok.hasUDSuffix()) {
1295 Diag(StrTok, diag::err_invalid_string_udl);
1296 return DiscardUntilEndOfDirective();
1298 // Parse and validate the string, converting it into a unique ID.
1299 StringLiteralParser Literal(StrTok, *this);
1300 assert(Literal.isAscii() && "Didn't allow wide strings in");
1301 if (Literal.hadError)
1302 return DiscardUntilEndOfDirective();
1303 if (Literal.Pascal) {
1304 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1305 return DiscardUntilEndOfDirective();
1307 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1309 // If a filename was present, read any flags that are present.
1310 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, FileKind, *this))
1314 // Create a line note with this information.
1315 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, IsFileEntry,
1316 IsFileExit, FileKind);
1318 // If the preprocessor has callbacks installed, notify them of the #line
1319 // change. This is used so that the line marker comes out in -E mode for
1322 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1324 Reason = PPCallbacks::EnterFile;
1325 else if (IsFileExit)
1326 Reason = PPCallbacks::ExitFile;
1328 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1332 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1334 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1336 // PTH doesn't emit #warning or #error directives.
1338 return CurPTHLexer->DiscardToEndOfLine();
1340 // Read the rest of the line raw. We do this because we don't want macros
1341 // to be expanded and we don't require that the tokens be valid preprocessing
1342 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1343 // collapse multiple consequtive white space between tokens, but this isn't
1344 // specified by the standard.
1345 SmallString<128> Message;
1346 CurLexer->ReadToEndOfLine(&Message);
1348 // Find the first non-whitespace character, so that we can make the
1349 // diagnostic more succinct.
1350 StringRef Msg = StringRef(Message).ltrim(' ');
1353 Diag(Tok, diag::pp_hash_warning) << Msg;
1355 Diag(Tok, diag::err_pp_hash_error) << Msg;
1358 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1360 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1361 // Yes, this directive is an extension.
1362 Diag(Tok, diag::ext_pp_ident_directive);
1364 // Read the string argument.
1368 // If the token kind isn't a string, it's a malformed directive.
1369 if (StrTok.isNot(tok::string_literal) &&
1370 StrTok.isNot(tok::wide_string_literal)) {
1371 Diag(StrTok, diag::err_pp_malformed_ident);
1372 if (StrTok.isNot(tok::eod))
1373 DiscardUntilEndOfDirective();
1377 if (StrTok.hasUDSuffix()) {
1378 Diag(StrTok, diag::err_invalid_string_udl);
1379 return DiscardUntilEndOfDirective();
1382 // Verify that there is nothing after the string, other than EOD.
1383 CheckEndOfDirective("ident");
1386 bool Invalid = false;
1387 std::string Str = getSpelling(StrTok, &Invalid);
1389 Callbacks->Ident(Tok.getLocation(), Str);
1393 /// \brief Handle a #public directive.
1394 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1396 ReadMacroName(MacroNameTok, MU_Undef);
1398 // Error reading macro name? If so, diagnostic already issued.
1399 if (MacroNameTok.is(tok::eod))
1402 // Check to see if this is the last token on the #__public_macro line.
1403 CheckEndOfDirective("__public_macro");
1405 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1406 // Okay, we finally have a valid identifier to undef.
1407 MacroDirective *MD = getLocalMacroDirective(II);
1409 // If the macro is not defined, this is an error.
1411 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1415 // Note that this macro has now been exported.
1416 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1417 MacroNameTok.getLocation(), /*IsPublic=*/true));
1420 /// \brief Handle a #private directive.
1421 void Preprocessor::HandleMacroPrivateDirective() {
1423 ReadMacroName(MacroNameTok, MU_Undef);
1425 // Error reading macro name? If so, diagnostic already issued.
1426 if (MacroNameTok.is(tok::eod))
1429 // Check to see if this is the last token on the #__private_macro line.
1430 CheckEndOfDirective("__private_macro");
1432 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1433 // Okay, we finally have a valid identifier to undef.
1434 MacroDirective *MD = getLocalMacroDirective(II);
1436 // If the macro is not defined, this is an error.
1438 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1442 // Note that this macro has now been marked private.
1443 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1444 MacroNameTok.getLocation(), /*IsPublic=*/false));
1447 //===----------------------------------------------------------------------===//
1448 // Preprocessor Include Directive Handling.
1449 //===----------------------------------------------------------------------===//
1451 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1452 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1453 /// true if the input filename was in <>'s or false if it were in ""'s. The
1454 /// caller is expected to provide a buffer that is large enough to hold the
1455 /// spelling of the filename, but is also expected to handle the case when
1456 /// this method decides to use a different buffer.
1457 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1458 StringRef &Buffer) {
1459 // Get the text form of the filename.
1460 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1462 // Make sure the filename is <x> or "x".
1464 if (Buffer[0] == '<') {
1465 if (Buffer.back() != '>') {
1466 Diag(Loc, diag::err_pp_expects_filename);
1467 Buffer = StringRef();
1471 } else if (Buffer[0] == '"') {
1472 if (Buffer.back() != '"') {
1473 Diag(Loc, diag::err_pp_expects_filename);
1474 Buffer = StringRef();
1479 Diag(Loc, diag::err_pp_expects_filename);
1480 Buffer = StringRef();
1484 // Diagnose #include "" as invalid.
1485 if (Buffer.size() <= 2) {
1486 Diag(Loc, diag::err_pp_empty_filename);
1487 Buffer = StringRef();
1491 // Skip the brackets.
1492 Buffer = Buffer.substr(1, Buffer.size()-2);
1496 // \brief Handle cases where the \#include name is expanded from a macro
1497 // as multiple tokens, which need to be glued together.
1499 // This occurs for code like:
1501 // \#define FOO <a/b.h>
1504 // because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1506 // This code concatenates and consumes tokens up to the '>' token. It returns
1507 // false if the > was found, otherwise it returns true if it finds and consumes
1509 bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
1510 SourceLocation &End) {
1514 while (CurTok.isNot(tok::eod)) {
1515 End = CurTok.getLocation();
1517 // FIXME: Provide code completion for #includes.
1518 if (CurTok.is(tok::code_completion)) {
1519 setCodeCompletionReached();
1524 // Append the spelling of this token to the buffer. If there was a space
1525 // before it, add it now.
1526 if (CurTok.hasLeadingSpace())
1527 FilenameBuffer.push_back(' ');
1529 // Get the spelling of the token, directly into FilenameBuffer if possible.
1530 size_t PreAppendSize = FilenameBuffer.size();
1531 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1533 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1534 unsigned ActualLen = getSpelling(CurTok, BufPtr);
1536 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1537 if (BufPtr != &FilenameBuffer[PreAppendSize])
1538 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1540 // Resize FilenameBuffer to the correct size.
1541 if (CurTok.getLength() != ActualLen)
1542 FilenameBuffer.resize(PreAppendSize+ActualLen);
1544 // If we found the '>' marker, return success.
1545 if (CurTok.is(tok::greater))
1551 // If we hit the eod marker, emit an error and return true so that the caller
1552 // knows the EOD has been read.
1553 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1557 /// \brief Push a token onto the token stream containing an annotation.
1558 void Preprocessor::EnterAnnotationToken(SourceRange Range,
1559 tok::TokenKind Kind,
1560 void *AnnotationVal) {
1561 // FIXME: Produce this as the current token directly, rather than
1562 // allocating a new token for it.
1563 auto Tok = llvm::make_unique<Token[]>(1);
1564 Tok[0].startToken();
1565 Tok[0].setKind(Kind);
1566 Tok[0].setLocation(Range.getBegin());
1567 Tok[0].setAnnotationEndLoc(Range.getEnd());
1568 Tok[0].setAnnotationValue(AnnotationVal);
1569 EnterTokenStream(std::move(Tok), 1, true);
1572 /// \brief Produce a diagnostic informing the user that a #include or similar
1573 /// was implicitly treated as a module import.
1574 static void diagnoseAutoModuleImport(
1575 Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
1576 ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
1577 SourceLocation PathEnd) {
1578 assert(PP.getLangOpts().ObjC2 && "no import syntax available");
1580 SmallString<128> PathString;
1581 for (size_t I = 0, N = Path.size(); I != N; ++I) {
1584 PathString += Path[I].first->getName();
1586 int IncludeKind = 0;
1588 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1589 case tok::pp_include:
1593 case tok::pp_import:
1597 case tok::pp_include_next:
1601 case tok::pp___include_macros:
1606 llvm_unreachable("unknown include directive kind");
1609 CharSourceRange ReplaceRange(SourceRange(HashLoc, PathEnd),
1610 /*IsTokenRange=*/false);
1611 PP.Diag(HashLoc, diag::warn_auto_module_import)
1612 << IncludeKind << PathString
1613 << FixItHint::CreateReplacement(ReplaceRange,
1614 ("@import " + PathString + ";").str());
1617 // Given a vector of path components and a string containing the real
1618 // path to the file, build a properly-cased replacement in the vector,
1619 // and return true if the replacement should be suggested.
1620 static bool trySimplifyPath(SmallVectorImpl<StringRef> &Components,
1621 StringRef RealPathName) {
1622 auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName);
1623 auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName);
1625 bool SuggestReplacement = false;
1626 // Below is a best-effort to handle ".." in paths. It is admittedly
1627 // not 100% correct in the presence of symlinks.
1628 for (auto &Component : llvm::reverse(Components)) {
1629 if ("." == Component) {
1630 } else if (".." == Component) {
1634 } else if (RealPathComponentIter != RealPathComponentEnd) {
1635 if (Component != *RealPathComponentIter) {
1636 // If these path components differ by more than just case, then we
1637 // may be looking at symlinked paths. Bail on this diagnostic to avoid
1638 // noisy false positives.
1639 SuggestReplacement = RealPathComponentIter->equals_lower(Component);
1640 if (!SuggestReplacement)
1642 Component = *RealPathComponentIter;
1644 ++RealPathComponentIter;
1647 return SuggestReplacement;
1650 bool Preprocessor::checkModuleIsAvailable(const LangOptions &LangOpts,
1651 const TargetInfo &TargetInfo,
1652 DiagnosticsEngine &Diags, Module *M) {
1653 Module::Requirement Requirement;
1654 Module::UnresolvedHeaderDirective MissingHeader;
1655 if (M->isAvailable(LangOpts, TargetInfo, Requirement, MissingHeader))
1658 if (MissingHeader.FileNameLoc.isValid()) {
1659 Diags.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
1660 << MissingHeader.IsUmbrella << MissingHeader.FileName;
1662 // FIXME: Track the location at which the requirement was specified, and
1664 Diags.Report(M->DefinitionLoc, diag::err_module_unavailable)
1665 << M->getFullModuleName() << Requirement.second << Requirement.first;
1670 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1671 /// the file to be included from the lexer, then include it! This is a common
1672 /// routine with functionality shared between \#include, \#include_next and
1673 /// \#import. LookupFrom is set when this is a \#include_next directive, it
1674 /// specifies the file to start searching from.
1675 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1677 const DirectoryLookup *LookupFrom,
1678 const FileEntry *LookupFromFile,
1681 CurPPLexer->LexIncludeFilename(FilenameTok);
1683 // Reserve a buffer to get the spelling.
1684 SmallString<128> FilenameBuffer;
1687 SourceLocation CharEnd; // the end of this directive, in characters
1689 switch (FilenameTok.getKind()) {
1691 // If the token kind is EOD, the error has already been diagnosed.
1694 case tok::angle_string_literal:
1695 case tok::string_literal:
1696 Filename = getSpelling(FilenameTok, FilenameBuffer);
1697 End = FilenameTok.getLocation();
1698 CharEnd = End.getLocWithOffset(FilenameTok.getLength());
1702 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1703 // case, glue the tokens together into FilenameBuffer and interpret those.
1704 FilenameBuffer.push_back('<');
1705 if (ConcatenateIncludeName(FilenameBuffer, End))
1706 return; // Found <eod> but no ">"? Diagnostic already emitted.
1707 Filename = FilenameBuffer;
1708 CharEnd = End.getLocWithOffset(1);
1711 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1712 DiscardUntilEndOfDirective();
1716 CharSourceRange FilenameRange
1717 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1718 StringRef OriginalFilename = Filename;
1720 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1721 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1723 if (Filename.empty()) {
1724 DiscardUntilEndOfDirective();
1728 // Verify that there is nothing after the filename, other than EOD. Note that
1729 // we allow macros that expand to nothing after the filename, because this
1730 // falls into the category of "#include pp-tokens new-line" specified in
1732 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1734 // Check that we don't have infinite #include recursion.
1735 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1736 Diag(FilenameTok, diag::err_pp_include_too_deep);
1740 // Complain about attempts to #include files in an audit pragma.
1741 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1742 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1743 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1745 // Immediately leave the pragma.
1746 PragmaARCCFCodeAuditedLoc = SourceLocation();
1749 // Complain about attempts to #include files in an assume-nonnull pragma.
1750 if (PragmaAssumeNonNullLoc.isValid()) {
1751 Diag(HashLoc, diag::err_pp_include_in_assume_nonnull);
1752 Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
1754 // Immediately leave the pragma.
1755 PragmaAssumeNonNullLoc = SourceLocation();
1758 if (HeaderInfo.HasIncludeAliasMap()) {
1759 // Map the filename with the brackets still attached. If the name doesn't
1760 // map to anything, fall back on the filename we've already gotten the
1762 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1763 if (!NewName.empty())
1767 // Search include directories.
1768 bool IsMapped = false;
1769 const DirectoryLookup *CurDir;
1770 SmallString<1024> SearchPath;
1771 SmallString<1024> RelativePath;
1772 // We get the raw path only if we have 'Callbacks' to which we later pass
1774 ModuleMap::KnownHeader SuggestedModule;
1775 SourceLocation FilenameLoc = FilenameTok.getLocation();
1776 SmallString<128> NormalizedPath;
1777 if (LangOpts.MSVCCompat) {
1778 NormalizedPath = Filename.str();
1779 #ifndef LLVM_ON_WIN32
1780 llvm::sys::path::native(NormalizedPath);
1783 const FileEntry *File = LookupFile(
1784 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
1785 isAngled, LookupFrom, LookupFromFile, CurDir,
1786 Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
1787 &SuggestedModule, &IsMapped);
1791 // Give the clients a chance to recover.
1792 SmallString<128> RecoveryPath;
1793 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1794 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1795 // Add the recovery path to the list of search paths.
1796 DirectoryLookup DL(DE, SrcMgr::C_User, false);
1797 HeaderInfo.AddSearchPath(DL, isAngled);
1799 // Try the lookup again, skipping the cache.
1802 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1803 LookupFrom, LookupFromFile, CurDir, nullptr, nullptr,
1804 &SuggestedModule, &IsMapped, /*SkipCache*/ true);
1809 if (!SuppressIncludeNotFoundError) {
1810 // If the file could not be located and it was included via angle
1811 // brackets, we can attempt a lookup as though it were a quoted path to
1812 // provide the user with a possible fixit.
1816 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, false,
1817 LookupFrom, LookupFromFile, CurDir,
1818 Callbacks ? &SearchPath : nullptr,
1819 Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped);
1821 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1822 Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1824 FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1828 // If the file is still not found, just go with the vanilla diagnostic
1830 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename
1835 // Should we enter the source file? Set to false if either the source file is
1836 // known to have no effect beyond its effect on module visibility -- that is,
1837 // if it's got an include guard that is already defined or is a modular header
1838 // we've imported or already built.
1839 bool ShouldEnter = true;
1841 if (PPOpts->SingleFileParseMode)
1842 ShouldEnter = false;
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 (ShouldEnter && 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 (checkModuleIsAvailable(getLangOpts(), getTargetInfo(), getDiagnostics(),
1855 SuggestedModule.getModule())) {
1856 Diag(FilenameTok.getLocation(),
1857 diag::note_implicit_top_level_module_import_here)
1858 << SuggestedModule.getModule()->getTopLevelModuleName();
1862 // Compute the module access path corresponding to this module.
1863 // FIXME: Should we have a second loadModule() overload to avoid this
1864 // extra lookup step?
1865 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1866 for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
1867 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1868 FilenameTok.getLocation()));
1869 std::reverse(Path.begin(), Path.end());
1871 // Warn that we're replacing the include/import with a module import.
1872 // We only do this in Objective-C, where we have a module-import syntax.
1873 if (getLangOpts().ObjC2)
1874 diagnoseAutoModuleImport(*this, HashLoc, IncludeTok, Path, CharEnd);
1876 // Load the module to import its macros. We'll make the declarations
1877 // visible when the parser gets here.
1878 // FIXME: Pass SuggestedModule in here rather than converting it to a path
1879 // and making the module loader convert it back again.
1880 ModuleLoadResult Imported = TheModuleLoader.loadModule(
1881 IncludeTok.getLocation(), Path, Module::Hidden,
1882 /*IsIncludeDirective=*/true);
1883 assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
1884 "the imported module is different than the suggested one");
1887 ShouldEnter = false;
1888 else if (Imported.isMissingExpected()) {
1889 // We failed to find a submodule that we assumed would exist (because it
1890 // was in the directory of an umbrella header, for instance), but no
1891 // actual module containing it exists (because the umbrella header is
1892 // incomplete). Treat this as a textual inclusion.
1893 SuggestedModule = ModuleMap::KnownHeader();
1894 } else if (Imported.isConfigMismatch()) {
1895 // On a configuration mismatch, enter the header textually. We still know
1896 // that it's part of the corresponding module.
1898 // We hit an error processing the import. Bail out.
1899 if (hadModuleLoaderFatalFailure()) {
1900 // With a fatal failure in the module loader, we abort parsing.
1901 Token &Result = IncludeTok;
1903 Result.startToken();
1904 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1905 CurLexer->cutOffLexing();
1907 assert(CurPTHLexer && "#include but no current lexer set!");
1908 CurPTHLexer->getEOF(Result);
1915 // The #included file will be considered to be a system header if either it is
1916 // in a system include directory, or if the #includer is a system include
1918 SrcMgr::CharacteristicKind FileCharacter =
1919 SourceMgr.getFileCharacteristic(FilenameTok.getLocation());
1921 FileCharacter = std::max(HeaderInfo.getFileDirFlavor(File), FileCharacter);
1923 // Ask HeaderInfo if we should enter this #include file. If not, #including
1924 // this file will have no effect.
1925 bool SkipHeader = false;
1926 if (ShouldEnter && File &&
1927 !HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport,
1928 getLangOpts().Modules,
1929 SuggestedModule.getModule())) {
1930 ShouldEnter = false;
1935 // Notify the callback object that we've seen an inclusion directive.
1936 Callbacks->InclusionDirective(
1937 HashLoc, IncludeTok,
1938 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1939 FilenameRange, File, SearchPath, RelativePath,
1940 ShouldEnter ? nullptr : SuggestedModule.getModule());
1941 if (SkipHeader && !SuggestedModule.getModule())
1942 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1948 // FIXME: If we have a suggested module, and we've already visited this file,
1949 // don't bother entering it again. We know it has no further effect.
1951 // Issue a diagnostic if the name of the file on disk has a different case
1952 // than the one we're about to open.
1953 const bool CheckIncludePathPortability =
1954 !IsMapped && File && !File->tryGetRealPathName().empty();
1956 if (CheckIncludePathPortability) {
1957 StringRef Name = LangOpts.MSVCCompat ? NormalizedPath.str() : Filename;
1958 StringRef RealPathName = File->tryGetRealPathName();
1959 SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name),
1960 llvm::sys::path::end(Name));
1962 if (trySimplifyPath(Components, RealPathName)) {
1963 SmallString<128> Path;
1964 Path.reserve(Name.size()+2);
1965 Path.push_back(isAngled ? '<' : '"');
1966 bool isLeadingSeparator = llvm::sys::path::is_absolute(Name);
1967 for (auto Component : Components) {
1968 if (isLeadingSeparator)
1969 isLeadingSeparator = false;
1971 Path.append(Component);
1972 // Append the separator the user used, or the close quote
1974 Path.size() <= Filename.size() ? Filename[Path.size()-1] :
1975 (isAngled ? '>' : '"'));
1977 // For user files and known standard headers, by default we issue a diagnostic.
1978 // For other system headers, we don't. They can be controlled separately.
1979 auto DiagId = (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name)) ?
1980 diag::pp_nonportable_path : diag::pp_nonportable_system_path;
1981 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1982 Diag(FilenameTok, DiagId) << Path <<
1983 FixItHint::CreateReplacement(Range, Path);
1987 // If we don't need to enter the file, stop now.
1989 // If this is a module import, make it visible if needed.
1990 if (auto *M = SuggestedModule.getModule()) {
1991 // When building a pch, -fmodule-name tells the compiler to textually
1992 // include headers in the specified module. But it is possible that
1993 // ShouldEnter is false because we are skipping the header. In that
1994 // case, We are not importing the specified module.
1995 if (SkipHeader && getLangOpts().CompilingPCH &&
1996 M->getTopLevelModuleName() == getLangOpts().CurrentModule)
1999 makeModuleVisible(M, HashLoc);
2001 if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
2002 tok::pp___include_macros)
2003 EnterAnnotationToken(SourceRange(HashLoc, End),
2004 tok::annot_module_include, M);
2009 // Look up the file, create a File ID for it.
2010 SourceLocation IncludePos = End;
2011 // If the filename string was the result of macro expansions, set the include
2012 // position on the file where it will be included and after the expansions.
2013 if (IncludePos.isMacroID())
2014 IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
2015 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
2016 assert(FID.isValid() && "Expected valid file ID");
2018 // If all is good, enter the new file!
2019 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
2022 // Determine if we're switching to building a new submodule, and which one.
2023 if (auto *M = SuggestedModule.getModule()) {
2024 // When building a pch, -fmodule-name tells the compiler to textually
2025 // include headers in the specified module. We are not building the
2026 // specified module.
2027 if (getLangOpts().CompilingPCH &&
2028 M->getTopLevelModuleName() == getLangOpts().CurrentModule)
2031 assert(!CurLexerSubmodule && "should not have marked this as a module yet");
2032 CurLexerSubmodule = M;
2034 // Let the macro handling code know that any future macros are within
2035 // the new submodule.
2036 EnterSubmodule(M, HashLoc, /*ForPragma*/false);
2038 // Let the parser know that any future declarations are within the new
2040 // FIXME: There's no point doing this if we're handling a #__include_macros
2042 EnterAnnotationToken(SourceRange(HashLoc, End), tok::annot_module_begin, M);
2046 /// HandleIncludeNextDirective - Implements \#include_next.
2048 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
2049 Token &IncludeNextTok) {
2050 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
2052 // #include_next is like #include, except that we start searching after
2053 // the current found directory. If we can't do this, issue a
2055 const DirectoryLookup *Lookup = CurDirLookup;
2056 const FileEntry *LookupFromFile = nullptr;
2057 if (isInPrimaryFile() && LangOpts.IsHeaderFile) {
2058 // If the main file is a header, then it's either for PCH/AST generation,
2059 // or libclang opened it. Either way, handle it as a normal include below
2060 // and do not complain about include_next.
2061 } else if (isInPrimaryFile()) {
2063 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
2064 } else if (CurLexerSubmodule) {
2065 // Start looking up in the directory *after* the one in which the current
2066 // file would be found, if any.
2067 assert(CurPPLexer && "#include_next directive in macro?");
2068 LookupFromFile = CurPPLexer->getFileEntry();
2070 } else if (!Lookup) {
2071 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
2073 // Start looking up in the next directory.
2077 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
2081 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
2082 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
2083 // The Microsoft #import directive takes a type library and generates header
2084 // files from it, and includes those. This is beyond the scope of what clang
2085 // does, so we ignore it and error out. However, #import can optionally have
2086 // trailing attributes that span multiple lines. We're going to eat those
2087 // so we can continue processing from there.
2088 Diag(Tok, diag::err_pp_import_directive_ms );
2090 // Read tokens until we get to the end of the directive. Note that the
2091 // directive can be split over multiple lines using the backslash character.
2092 DiscardUntilEndOfDirective();
2095 /// HandleImportDirective - Implements \#import.
2097 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
2099 if (!LangOpts.ObjC1) { // #import is standard for ObjC.
2100 if (LangOpts.MSVCCompat)
2101 return HandleMicrosoftImportDirective(ImportTok);
2102 Diag(ImportTok, diag::ext_pp_import_directive);
2104 return HandleIncludeDirective(HashLoc, ImportTok, nullptr, nullptr, true);
2107 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
2108 /// pseudo directive in the predefines buffer. This handles it by sucking all
2109 /// tokens through the preprocessor and discarding them (only keeping the side
2110 /// effects on the preprocessor).
2111 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
2112 Token &IncludeMacrosTok) {
2113 // This directive should only occur in the predefines buffer. If not, emit an
2114 // error and reject it.
2115 SourceLocation Loc = IncludeMacrosTok.getLocation();
2116 if (SourceMgr.getBufferName(Loc) != "<built-in>") {
2117 Diag(IncludeMacrosTok.getLocation(),
2118 diag::pp_include_macros_out_of_predefines);
2119 DiscardUntilEndOfDirective();
2123 // Treat this as a normal #include for checking purposes. If this is
2124 // successful, it will push a new lexer onto the include stack.
2125 HandleIncludeDirective(HashLoc, IncludeMacrosTok);
2130 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
2131 } while (TmpTok.isNot(tok::hashhash));
2134 //===----------------------------------------------------------------------===//
2135 // Preprocessor Macro Directive Handling.
2136 //===----------------------------------------------------------------------===//
2138 /// ReadMacroParameterList - The ( starting an argument list of a macro
2139 /// definition has just been read. Lex the rest of the arguments and the
2140 /// closing ), updating MI with what we learn. Return true if an error occurs
2141 /// parsing the arg list.
2142 bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
2143 SmallVector<IdentifierInfo*, 32> Arguments;
2146 LexUnexpandedToken(Tok);
2147 switch (Tok.getKind()) {
2149 // Found the end of the argument list.
2150 if (Arguments.empty()) // #define FOO()
2152 // Otherwise we have #define FOO(A,)
2153 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2155 case tok::ellipsis: // #define X(... -> C99 varargs
2157 Diag(Tok, LangOpts.CPlusPlus11 ?
2158 diag::warn_cxx98_compat_variadic_macro :
2159 diag::ext_variadic_macro);
2161 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
2162 if (LangOpts.OpenCL) {
2163 Diag(Tok, diag::err_pp_opencl_variadic_macros);
2167 // Lex the token after the identifier.
2168 LexUnexpandedToken(Tok);
2169 if (Tok.isNot(tok::r_paren)) {
2170 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2173 // Add the __VA_ARGS__ identifier as an argument.
2174 Arguments.push_back(Ident__VA_ARGS__);
2175 MI->setIsC99Varargs();
2176 MI->setParameterList(Arguments, BP);
2178 case tok::eod: // #define X(
2179 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2182 // Handle keywords and identifiers here to accept things like
2183 // #define Foo(for) for.
2184 IdentifierInfo *II = Tok.getIdentifierInfo();
2187 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2191 // If this is already used as an argument, it is used multiple times (e.g.
2193 if (std::find(Arguments.begin(), Arguments.end(), II) !=
2194 Arguments.end()) { // C99 6.10.3p6
2195 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
2199 // Add the argument to the macro info.
2200 Arguments.push_back(II);
2202 // Lex the token after the identifier.
2203 LexUnexpandedToken(Tok);
2205 switch (Tok.getKind()) {
2206 default: // #define X(A B
2207 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2209 case tok::r_paren: // #define X(A)
2210 MI->setParameterList(Arguments, BP);
2212 case tok::comma: // #define X(A,
2214 case tok::ellipsis: // #define X(A... -> GCC extension
2215 // Diagnose extension.
2216 Diag(Tok, diag::ext_named_variadic_macro);
2218 // Lex the token after the identifier.
2219 LexUnexpandedToken(Tok);
2220 if (Tok.isNot(tok::r_paren)) {
2221 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2225 MI->setIsGNUVarargs();
2226 MI->setParameterList(Arguments, BP);
2233 static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2234 const LangOptions &LOptions) {
2235 if (MI->getNumTokens() == 1) {
2236 const Token &Value = MI->getReplacementToken(0);
2238 // Macro that is identity, like '#define inline inline' is a valid pattern.
2239 if (MacroName.getKind() == Value.getKind())
2242 // Macro that maps a keyword to the same keyword decorated with leading/
2243 // trailing underscores is a valid pattern:
2244 // #define inline __inline
2245 // #define inline __inline__
2246 // #define inline _inline (in MS compatibility mode)
2247 StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2248 if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2249 if (!II->isKeyword(LOptions))
2251 StringRef ValueText = II->getName();
2252 StringRef TrimmedValue = ValueText;
2253 if (!ValueText.startswith("__")) {
2254 if (ValueText.startswith("_"))
2255 TrimmedValue = TrimmedValue.drop_front(1);
2259 TrimmedValue = TrimmedValue.drop_front(2);
2260 if (TrimmedValue.endswith("__"))
2261 TrimmedValue = TrimmedValue.drop_back(2);
2263 return TrimmedValue.equals(MacroText);
2270 return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2272 MI->getNumTokens() == 0;
2275 // ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
2276 // entire line) of the macro's tokens and adds them to MacroInfo, and while
2277 // doing so performs certain validity checks including (but not limited to):
2278 // - # (stringization) is followed by a macro parameter
2280 // Returns a nullptr if an invalid sequence of tokens is encountered or returns
2281 // a pointer to a MacroInfo object.
2283 MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody(
2284 const Token &MacroNameTok, const bool ImmediatelyAfterHeaderGuard) {
2286 Token LastTok = MacroNameTok;
2287 // Create the new macro.
2288 MacroInfo *const MI = AllocateMacroInfo(MacroNameTok.getLocation());
2291 LexUnexpandedToken(Tok);
2293 // If this is a function-like macro definition, parse the argument list,
2294 // marking each of the identifiers as being used as macro arguments. Also,
2295 // check other constraints on the first token of the macro body.
2296 if (Tok.is(tok::eod)) {
2297 if (ImmediatelyAfterHeaderGuard) {
2298 // Save this macro information since it may part of a header guard.
2299 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2300 MacroNameTok.getLocation());
2302 // If there is no body to this macro, we have no special handling here.
2303 } else if (Tok.hasLeadingSpace()) {
2304 // This is a normal token with leading space. Clear the leading space
2305 // marker on the first token to get proper expansion.
2306 Tok.clearFlag(Token::LeadingSpace);
2307 } else if (Tok.is(tok::l_paren)) {
2308 // This is a function-like macro definition. Read the argument list.
2309 MI->setIsFunctionLike();
2310 if (ReadMacroParameterList(MI, LastTok)) {
2311 // Throw away the rest of the line.
2312 if (CurPPLexer->ParsingPreprocessorDirective)
2313 DiscardUntilEndOfDirective();
2317 // If this is a definition of a variadic C99 function-like macro, not using
2318 // the GNU named varargs extension, enabled __VA_ARGS__.
2320 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2321 // This gets unpoisoned where it is allowed.
2322 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2323 if (MI->isC99Varargs())
2324 Ident__VA_ARGS__->setIsPoisoned(false);
2326 // Read the first token after the arg list for down below.
2327 LexUnexpandedToken(Tok);
2328 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
2329 // C99 requires whitespace between the macro definition and the body. Emit
2330 // a diagnostic for something like "#define X+".
2331 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2333 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2334 // first character of a replacement list is not a character required by
2335 // subclause 5.2.1, then there shall be white-space separation between the
2336 // identifier and the replacement list.". 5.2.1 lists this set:
2337 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2338 // is irrelevant here.
2339 bool isInvalid = false;
2340 if (Tok.is(tok::at)) // @ is not in the list above.
2342 else if (Tok.is(tok::unknown)) {
2343 // If we have an unknown token, it is something strange like "`". Since
2344 // all of valid characters would have lexed into a single character
2345 // token of some sort, we know this is not a valid case.
2349 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2351 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2354 if (!Tok.is(tok::eod))
2357 // Read the rest of the macro body.
2358 if (MI->isObjectLike()) {
2359 // Object-like macros are very simple, just read their body.
2360 while (Tok.isNot(tok::eod)) {
2362 MI->AddTokenToBody(Tok);
2363 // Get the next token of the macro.
2364 LexUnexpandedToken(Tok);
2367 // Otherwise, read the body of a function-like macro. While we are at it,
2368 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2369 // parameters in function-like macro expansions.
2370 while (Tok.isNot(tok::eod)) {
2373 if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
2374 MI->AddTokenToBody(Tok);
2376 // Get the next token of the macro.
2377 LexUnexpandedToken(Tok);
2381 // If we're in -traditional mode, then we should ignore stringification
2382 // and token pasting. Mark the tokens as unknown so as not to confuse
2384 if (getLangOpts().TraditionalCPP) {
2385 Tok.setKind(tok::unknown);
2386 MI->AddTokenToBody(Tok);
2388 // Get the next token of the macro.
2389 LexUnexpandedToken(Tok);
2393 if (Tok.is(tok::hashhash)) {
2394 // If we see token pasting, check if it looks like the gcc comma
2395 // pasting extension. We'll use this information to suppress
2396 // diagnostics later on.
2398 // Get the next token of the macro.
2399 LexUnexpandedToken(Tok);
2401 if (Tok.is(tok::eod)) {
2402 MI->AddTokenToBody(LastTok);
2406 unsigned NumTokens = MI->getNumTokens();
2407 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2408 MI->getReplacementToken(NumTokens-1).is(tok::comma))
2409 MI->setHasCommaPasting();
2411 // Things look ok, add the '##' token to the macro.
2412 MI->AddTokenToBody(LastTok);
2416 // Get the next token of the macro.
2417 LexUnexpandedToken(Tok);
2419 // Check for a valid macro arg identifier.
2420 if (Tok.getIdentifierInfo() == nullptr ||
2421 MI->getParameterNum(Tok.getIdentifierInfo()) == -1) {
2423 // If this is assembler-with-cpp mode, we accept random gibberish after
2424 // the '#' because '#' is often a comment character. However, change
2425 // the kind of the token to tok::unknown so that the preprocessor isn't
2427 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
2428 LastTok.setKind(tok::unknown);
2429 MI->AddTokenToBody(LastTok);
2432 Diag(Tok, diag::err_pp_stringize_not_parameter)
2433 << LastTok.is(tok::hashat);
2435 // Disable __VA_ARGS__ again.
2436 Ident__VA_ARGS__->setIsPoisoned(true);
2441 // Things look ok, add the '#' and param name tokens to the macro.
2442 MI->AddTokenToBody(LastTok);
2443 MI->AddTokenToBody(Tok);
2446 // Get the next token of the macro.
2447 LexUnexpandedToken(Tok);
2450 MI->setDefinitionEndLoc(LastTok.getLocation());
2451 // Disable __VA_ARGS__ again.
2452 Ident__VA_ARGS__->setIsPoisoned(true);
2456 /// HandleDefineDirective - Implements \#define. This consumes the entire macro
2457 /// line then lets the caller lex the next real token.
2458 void Preprocessor::HandleDefineDirective(
2459 Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) {
2463 bool MacroShadowsKeyword;
2464 ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
2466 // Error reading macro name? If so, diagnostic already issued.
2467 if (MacroNameTok.is(tok::eod))
2470 // If we are supposed to keep comments in #defines, reenable comment saving
2472 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
2474 MacroInfo *const MI = ReadOptionalMacroParameterListAndBody(
2475 MacroNameTok, ImmediatelyAfterHeaderGuard);
2479 if (MacroShadowsKeyword &&
2480 !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
2481 Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
2483 // Check that there is no paste (##) operator at the beginning or end of the
2484 // replacement list.
2485 unsigned NumTokens = MI->getNumTokens();
2486 if (NumTokens != 0) {
2487 if (MI->getReplacementToken(0).is(tok::hashhash)) {
2488 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2491 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2492 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2499 // Finally, if this identifier already had a macro defined for it, verify that
2500 // the macro bodies are identical, and issue diagnostics if they are not.
2501 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
2502 // In Objective-C, ignore attempts to directly redefine the builtin
2503 // definitions of the ownership qualifiers. It's still possible to
2505 auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
2506 return II->isStr("__strong") ||
2507 II->isStr("__weak") ||
2508 II->isStr("__unsafe_unretained") ||
2509 II->isStr("__autoreleasing");
2511 if (getLangOpts().ObjC1 &&
2512 SourceMgr.getFileID(OtherMI->getDefinitionLoc())
2513 == getPredefinesFileID() &&
2514 isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
2515 // Warn if it changes the tokens.
2516 if ((!getDiagnostics().getSuppressSystemWarnings() ||
2517 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
2518 !MI->isIdenticalTo(*OtherMI, *this,
2519 /*Syntactic=*/LangOpts.MicrosoftExt)) {
2520 Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
2522 assert(!OtherMI->isWarnIfUnused());
2526 // It is very common for system headers to have tons of macro redefinitions
2527 // and for warnings to be disabled in system headers. If this is the case,
2528 // then don't bother calling MacroInfo::isIdenticalTo.
2529 if (!getDiagnostics().getSuppressSystemWarnings() ||
2530 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
2531 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
2532 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2534 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2535 // C++ [cpp.predefined]p4, but allow it as an extension.
2536 if (OtherMI->isBuiltinMacro())
2537 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
2538 // Macros must be identical. This means all tokens and whitespace
2539 // separation must be the same. C99 6.10.3p2.
2540 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
2541 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
2542 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2543 << MacroNameTok.getIdentifierInfo();
2544 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2547 if (OtherMI->isWarnIfUnused())
2548 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
2551 DefMacroDirective *MD =
2552 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
2554 assert(!MI->isUsed());
2555 // If we need warning for not using the macro, add its location in the
2556 // warn-because-unused-macro set. If it gets used it will be removed from set.
2557 if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
2558 !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
2559 MI->setIsWarnIfUnused(true);
2560 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2563 // If the callbacks want to know, tell them about the macro definition.
2565 Callbacks->MacroDefined(MacroNameTok, MD);
2568 /// HandleUndefDirective - Implements \#undef.
2570 void Preprocessor::HandleUndefDirective() {
2574 ReadMacroName(MacroNameTok, MU_Undef);
2576 // Error reading macro name? If so, diagnostic already issued.
2577 if (MacroNameTok.is(tok::eod))
2580 // Check to see if this is the last token on the #undef line.
2581 CheckEndOfDirective("undef");
2583 // Okay, we have a valid identifier to undef.
2584 auto *II = MacroNameTok.getIdentifierInfo();
2585 auto MD = getMacroDefinition(II);
2586 UndefMacroDirective *Undef = nullptr;
2588 // If the macro is not defined, this is a noop undef.
2589 if (const MacroInfo *MI = MD.getMacroInfo()) {
2590 if (!MI->isUsed() && MI->isWarnIfUnused())
2591 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2593 if (MI->isWarnIfUnused())
2594 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2596 Undef = AllocateUndefMacroDirective(MacroNameTok.getLocation());
2599 // If the callbacks want to know, tell them about the macro #undef.
2600 // Note: no matter if the macro was defined or not.
2602 Callbacks->MacroUndefined(MacroNameTok, MD, Undef);
2605 appendMacroDirective(II, Undef);
2608 //===----------------------------------------------------------------------===//
2609 // Preprocessor Conditional Directive Handling.
2610 //===----------------------------------------------------------------------===//
2612 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2613 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2614 /// true if any tokens have been returned or pp-directives activated before this
2615 /// \#ifndef has been lexed.
2617 void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2618 bool ReadAnyTokensBeforeDirective) {
2620 Token DirectiveTok = Result;
2623 ReadMacroName(MacroNameTok);
2625 // Error reading macro name? If so, diagnostic already issued.
2626 if (MacroNameTok.is(tok::eod)) {
2627 // Skip code until we get to #endif. This helps with recovery by not
2628 // emitting an error when the #endif is reached.
2629 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2630 /*Foundnonskip*/false, /*FoundElse*/false);
2634 // Check to see if this is the last token on the #if[n]def line.
2635 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2637 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2638 auto MD = getMacroDefinition(MII);
2639 MacroInfo *MI = MD.getMacroInfo();
2641 if (CurPPLexer->getConditionalStackDepth() == 0) {
2642 // If the start of a top-level #ifdef and if the macro is not defined,
2643 // inform MIOpt that this might be the start of a proper include guard.
2644 // Otherwise it is some other form of unknown conditional which we can't
2646 if (!ReadAnyTokensBeforeDirective && !MI) {
2647 assert(isIfndef && "#ifdef shouldn't reach here");
2648 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
2650 CurPPLexer->MIOpt.EnterTopLevelConditional();
2653 // If there is a macro, process it.
2654 if (MI) // Mark it used.
2655 markMacroAsUsed(MI);
2659 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2661 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2664 // Should we include the stuff contained by this directive?
2665 if (PPOpts->SingleFileParseMode && !MI) {
2666 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2667 // the directive blocks.
2668 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2669 /*wasskip*/false, /*foundnonskip*/false,
2670 /*foundelse*/false);
2671 } else if (!MI == isIfndef) {
2672 // Yes, remember that we are inside a conditional, then lex the next token.
2673 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2674 /*wasskip*/false, /*foundnonskip*/true,
2675 /*foundelse*/false);
2677 // No, skip the contents of this block.
2678 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2679 /*Foundnonskip*/false,
2680 /*FoundElse*/false);
2684 /// HandleIfDirective - Implements the \#if directive.
2686 void Preprocessor::HandleIfDirective(Token &IfToken,
2687 bool ReadAnyTokensBeforeDirective) {
2690 // Parse and evaluate the conditional expression.
2691 IdentifierInfo *IfNDefMacro = nullptr;
2692 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2693 const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
2694 const bool ConditionalTrue = DER.Conditional;
2695 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2697 // If this condition is equivalent to #ifndef X, and if this is the first
2698 // directive seen, handle it for the multiple-include optimization.
2699 if (CurPPLexer->getConditionalStackDepth() == 0) {
2700 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2701 // FIXME: Pass in the location of the macro name, not the 'if' token.
2702 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
2704 CurPPLexer->MIOpt.EnterTopLevelConditional();
2708 Callbacks->If(IfToken.getLocation(),
2709 SourceRange(ConditionalBegin, ConditionalEnd),
2710 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
2712 // Should we include the stuff contained by this directive?
2713 if (PPOpts->SingleFileParseMode && DER.IncludedUndefinedIds) {
2714 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2715 // the directive blocks.
2716 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2717 /*foundnonskip*/false, /*foundelse*/false);
2718 } else if (ConditionalTrue) {
2719 // Yes, remember that we are inside a conditional, then lex the next token.
2720 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2721 /*foundnonskip*/true, /*foundelse*/false);
2723 // No, skip the contents of this block.
2724 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2725 /*FoundElse*/false);
2729 /// HandleEndifDirective - Implements the \#endif directive.
2731 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2734 // Check that this is the whole directive.
2735 CheckEndOfDirective("endif");
2737 PPConditionalInfo CondInfo;
2738 if (CurPPLexer->popConditionalLevel(CondInfo)) {
2739 // No conditionals on the stack: this is an #endif without an #if.
2740 Diag(EndifToken, diag::err_pp_endif_without_if);
2744 // If this the end of a top-level #endif, inform MIOpt.
2745 if (CurPPLexer->getConditionalStackDepth() == 0)
2746 CurPPLexer->MIOpt.ExitTopLevelConditional();
2748 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2749 "This code should only be reachable in the non-skipping case!");
2752 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2755 /// HandleElseDirective - Implements the \#else directive.
2757 void Preprocessor::HandleElseDirective(Token &Result) {
2760 // #else directive in a non-skipping conditional... start skipping.
2761 CheckEndOfDirective("else");
2763 PPConditionalInfo CI;
2764 if (CurPPLexer->popConditionalLevel(CI)) {
2765 Diag(Result, diag::pp_err_else_without_if);
2769 // If this is a top-level #else, inform the MIOpt.
2770 if (CurPPLexer->getConditionalStackDepth() == 0)
2771 CurPPLexer->MIOpt.EnterTopLevelConditional();
2773 // If this is a #else with a #else before it, report the error.
2774 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2777 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2779 if (PPOpts->SingleFileParseMode && !CI.FoundNonSkip) {
2780 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2781 // the directive blocks.
2782 CurPPLexer->pushConditionalLevel(CI.IfLoc, /*wasskip*/false,
2783 /*foundnonskip*/false, /*foundelse*/true);
2787 // Finally, skip the rest of the contents of this block.
2788 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2789 /*FoundElse*/true, Result.getLocation());
2792 /// HandleElifDirective - Implements the \#elif directive.
2794 void Preprocessor::HandleElifDirective(Token &ElifToken) {
2797 // #elif directive in a non-skipping conditional... start skipping.
2798 // We don't care what the condition is, because we will always skip it (since
2799 // the block immediately before it was included).
2800 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2801 DiscardUntilEndOfDirective();
2802 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2804 PPConditionalInfo CI;
2805 if (CurPPLexer->popConditionalLevel(CI)) {
2806 Diag(ElifToken, diag::pp_err_elif_without_if);
2810 // If this is a top-level #elif, inform the MIOpt.
2811 if (CurPPLexer->getConditionalStackDepth() == 0)
2812 CurPPLexer->MIOpt.EnterTopLevelConditional();
2814 // If this is a #elif with a #else before it, report the error.
2815 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2818 Callbacks->Elif(ElifToken.getLocation(),
2819 SourceRange(ConditionalBegin, ConditionalEnd),
2820 PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
2822 if (PPOpts->SingleFileParseMode && !CI.FoundNonSkip) {
2823 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2824 // the directive blocks.
2825 CurPPLexer->pushConditionalLevel(ElifToken.getLocation(), /*wasskip*/false,
2826 /*foundnonskip*/false, /*foundelse*/false);
2830 // Finally, skip the rest of the contents of this block.
2831 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2832 /*FoundElse*/CI.FoundElse,
2833 ElifToken.getLocation());