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