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