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