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