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