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