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