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