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