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