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