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