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