1 //===--- PPLexerChange.cpp - Handle changing lexers in the preprocessor ---===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements pieces of the Preprocessor interface that manage the
10 // current lexer stack.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/FileManager.h"
15 #include "clang/Basic/SourceManager.h"
16 #include "clang/Lex/HeaderSearch.h"
17 #include "clang/Lex/LexDiagnostic.h"
18 #include "clang/Lex/MacroInfo.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "clang/Lex/PreprocessorOptions.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/Support/MemoryBufferRef.h"
23 #include "llvm/Support/Path.h"
26 using namespace clang
;
28 //===----------------------------------------------------------------------===//
29 // Miscellaneous Methods.
30 //===----------------------------------------------------------------------===//
32 /// isInPrimaryFile - Return true if we're in the top-level file, not in a
33 /// \#include. This looks through macro expansions and active _Pragma lexers.
34 bool Preprocessor::isInPrimaryFile() const {
36 return IncludeMacroStack
.empty();
38 // If there are any stacked lexers, we're in a #include.
39 assert(IsFileLexer(IncludeMacroStack
[0]) &&
40 "Top level include stack isn't our primary lexer?");
42 llvm::drop_begin(IncludeMacroStack
),
43 [&](const IncludeStackInfo
&ISI
) -> bool { return IsFileLexer(ISI
); });
46 /// getCurrentLexer - Return the current file lexer being lexed from. Note
47 /// that this ignores any potentially active macro expansions and _Pragma
48 /// expansions going on at the time.
49 PreprocessorLexer
*Preprocessor::getCurrentFileLexer() const {
53 // Look for a stacked lexer.
54 for (const IncludeStackInfo
&ISI
: llvm::reverse(IncludeMacroStack
)) {
56 return ISI
.ThePPLexer
;
62 //===----------------------------------------------------------------------===//
63 // Methods for Entering and Callbacks for leaving various contexts
64 //===----------------------------------------------------------------------===//
66 /// EnterSourceFile - Add a source file to the top of the include stack and
67 /// start lexing tokens from it instead of the current buffer.
68 bool Preprocessor::EnterSourceFile(FileID FID
, ConstSearchDirIterator CurDir
,
70 bool IsFirstIncludeOfFile
) {
71 assert(!CurTokenLexer
&& "Cannot #include a file inside a macro!");
72 ++NumEnteredSourceFiles
;
74 if (MaxIncludeStackDepth
< IncludeMacroStack
.size())
75 MaxIncludeStackDepth
= IncludeMacroStack
.size();
77 // Get the MemoryBuffer for this FID, if it fails, we fail.
78 std::optional
<llvm::MemoryBufferRef
> InputFile
=
79 getSourceManager().getBufferOrNone(FID
, Loc
);
81 SourceLocation FileStart
= SourceMgr
.getLocForStartOfFile(FID
);
82 Diag(Loc
, diag::err_pp_error_opening_file
)
83 << std::string(SourceMgr
.getBufferName(FileStart
)) << "";
87 if (isCodeCompletionEnabled() &&
88 SourceMgr
.getFileEntryForID(FID
) == CodeCompletionFile
) {
89 CodeCompletionFileLoc
= SourceMgr
.getLocForStartOfFile(FID
);
91 CodeCompletionFileLoc
.getLocWithOffset(CodeCompletionOffset
);
94 Lexer
*TheLexer
= new Lexer(FID
, *InputFile
, *this, IsFirstIncludeOfFile
);
95 if (getPreprocessorOpts().DependencyDirectivesForFile
&&
96 FID
!= PredefinesFileID
) {
97 if (OptionalFileEntryRef File
= SourceMgr
.getFileEntryRefForID(FID
)) {
98 if (std::optional
<ArrayRef
<dependency_directives_scan::Directive
>>
100 getPreprocessorOpts().DependencyDirectivesForFile(*File
)) {
101 TheLexer
->DepDirectives
= *DepDirectives
;
106 EnterSourceFileWithLexer(TheLexer
, CurDir
);
110 /// EnterSourceFileWithLexer - Add a source file to the top of the include stack
111 /// and start lexing tokens from it instead of the current buffer.
112 void Preprocessor::EnterSourceFileWithLexer(Lexer
*TheLexer
,
113 ConstSearchDirIterator CurDir
) {
114 PreprocessorLexer
*PrevPPLexer
= CurPPLexer
;
116 // Add the current lexer to the include stack.
117 if (CurPPLexer
|| CurTokenLexer
)
118 PushIncludeMacroStack();
120 CurLexer
.reset(TheLexer
);
121 CurPPLexer
= TheLexer
;
122 CurDirLookup
= CurDir
;
123 CurLexerSubmodule
= nullptr;
124 if (CurLexerCallback
!= CLK_LexAfterModuleImport
)
125 CurLexerCallback
= TheLexer
->isDependencyDirectivesLexer()
126 ? CLK_DependencyDirectivesLexer
129 // Notify the client, if desired, that we are in a new source file.
130 if (Callbacks
&& !CurLexer
->Is_PragmaLexer
) {
131 SrcMgr::CharacteristicKind FileType
=
132 SourceMgr
.getFileCharacteristic(CurLexer
->getFileLoc());
135 SourceLocation EnterLoc
;
137 PrevFID
= PrevPPLexer
->getFileID();
138 EnterLoc
= PrevPPLexer
->getSourceLocation();
140 Callbacks
->FileChanged(CurLexer
->getFileLoc(), PPCallbacks::EnterFile
,
142 Callbacks
->LexedFileChanged(CurLexer
->getFileID(),
143 PPCallbacks::LexedFileChangeReason::EnterFile
,
144 FileType
, PrevFID
, EnterLoc
);
148 /// EnterMacro - Add a Macro to the top of the include stack and start lexing
149 /// tokens from it instead of the current buffer.
150 void Preprocessor::EnterMacro(Token
&Tok
, SourceLocation ILEnd
,
151 MacroInfo
*Macro
, MacroArgs
*Args
) {
152 std::unique_ptr
<TokenLexer
> TokLexer
;
153 if (NumCachedTokenLexers
== 0) {
154 TokLexer
= std::make_unique
<TokenLexer
>(Tok
, ILEnd
, Macro
, Args
, *this);
156 TokLexer
= std::move(TokenLexerCache
[--NumCachedTokenLexers
]);
157 TokLexer
->Init(Tok
, ILEnd
, Macro
, Args
);
160 PushIncludeMacroStack();
161 CurDirLookup
= nullptr;
162 CurTokenLexer
= std::move(TokLexer
);
163 if (CurLexerCallback
!= CLK_LexAfterModuleImport
)
164 CurLexerCallback
= CLK_TokenLexer
;
167 /// EnterTokenStream - Add a "macro" context to the top of the include stack,
168 /// which will cause the lexer to start returning the specified tokens.
170 /// If DisableMacroExpansion is true, tokens lexed from the token stream will
171 /// not be subject to further macro expansion. Otherwise, these tokens will
172 /// be re-macro-expanded when/if expansion is enabled.
174 /// If OwnsTokens is false, this method assumes that the specified stream of
175 /// tokens has a permanent owner somewhere, so they do not need to be copied.
176 /// If it is true, it assumes the array of tokens is allocated with new[] and
179 void Preprocessor::EnterTokenStream(const Token
*Toks
, unsigned NumToks
,
180 bool DisableMacroExpansion
, bool OwnsTokens
,
182 if (CurLexerCallback
== CLK_CachingLexer
) {
183 if (CachedLexPos
< CachedTokens
.size()) {
184 assert(IsReinject
&& "new tokens in the middle of cached stream");
185 // We're entering tokens into the middle of our cached token stream. We
186 // can't represent that, so just insert the tokens into the buffer.
187 CachedTokens
.insert(CachedTokens
.begin() + CachedLexPos
,
188 Toks
, Toks
+ NumToks
);
194 // New tokens are at the end of the cached token sequnece; insert the
195 // token stream underneath the caching lexer.
196 ExitCachingLexMode();
197 EnterTokenStream(Toks
, NumToks
, DisableMacroExpansion
, OwnsTokens
,
199 EnterCachingLexMode();
203 // Create a macro expander to expand from the specified token stream.
204 std::unique_ptr
<TokenLexer
> TokLexer
;
205 if (NumCachedTokenLexers
== 0) {
206 TokLexer
= std::make_unique
<TokenLexer
>(
207 Toks
, NumToks
, DisableMacroExpansion
, OwnsTokens
, IsReinject
, *this);
209 TokLexer
= std::move(TokenLexerCache
[--NumCachedTokenLexers
]);
210 TokLexer
->Init(Toks
, NumToks
, DisableMacroExpansion
, OwnsTokens
,
214 // Save our current state.
215 PushIncludeMacroStack();
216 CurDirLookup
= nullptr;
217 CurTokenLexer
= std::move(TokLexer
);
218 if (CurLexerCallback
!= CLK_LexAfterModuleImport
)
219 CurLexerCallback
= CLK_TokenLexer
;
222 /// Compute the relative path that names the given file relative to
223 /// the given directory.
224 static void computeRelativePath(FileManager
&FM
, const DirectoryEntry
*Dir
,
225 FileEntryRef File
, SmallString
<128> &Result
) {
228 StringRef FilePath
= File
.getDir().getName();
229 StringRef Path
= FilePath
;
230 while (!Path
.empty()) {
231 if (auto CurDir
= FM
.getOptionalDirectoryRef(Path
)) {
232 if (*CurDir
== Dir
) {
233 Result
= FilePath
.substr(Path
.size());
234 llvm::sys::path::append(Result
,
235 llvm::sys::path::filename(File
.getName()));
240 Path
= llvm::sys::path::parent_path(Path
);
243 Result
= File
.getName();
246 void Preprocessor::PropagateLineStartLeadingSpaceInfo(Token
&Result
) {
248 CurTokenLexer
->PropagateLineStartLeadingSpaceInfo(Result
);
252 CurLexer
->PropagateLineStartLeadingSpaceInfo(Result
);
255 // FIXME: Handle other kinds of lexers? It generally shouldn't matter,
256 // but it might if they're empty?
259 /// Determine the location to use as the end of the buffer for a lexer.
261 /// If the file ends with a newline, form the EOF token on the newline itself,
262 /// rather than "on the line following it", which doesn't exist. This makes
263 /// diagnostics relating to the end of file include the last file that the user
264 /// actually typed, which is goodness.
265 const char *Preprocessor::getCurLexerEndPos() {
266 const char *EndPos
= CurLexer
->BufferEnd
;
267 if (EndPos
!= CurLexer
->BufferStart
&&
268 (EndPos
[-1] == '\n' || EndPos
[-1] == '\r')) {
271 // Handle \n\r and \r\n:
272 if (EndPos
!= CurLexer
->BufferStart
&&
273 (EndPos
[-1] == '\n' || EndPos
[-1] == '\r') &&
274 EndPos
[-1] != EndPos
[0])
281 static void collectAllSubModulesWithUmbrellaHeader(
282 const Module
&Mod
, SmallVectorImpl
<const Module
*> &SubMods
) {
283 if (Mod
.getUmbrellaHeaderAsWritten())
284 SubMods
.push_back(&Mod
);
285 for (auto *M
: Mod
.submodules())
286 collectAllSubModulesWithUmbrellaHeader(*M
, SubMods
);
289 void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(const Module
&Mod
) {
290 std::optional
<Module::Header
> UmbrellaHeader
=
291 Mod
.getUmbrellaHeaderAsWritten();
292 assert(UmbrellaHeader
&& "Module must use umbrella header");
293 const FileID
&File
= SourceMgr
.translateFile(UmbrellaHeader
->Entry
);
294 SourceLocation ExpectedHeadersLoc
= SourceMgr
.getLocForEndOfFile(File
);
295 if (getDiagnostics().isIgnored(diag::warn_uncovered_module_header
,
299 ModuleMap
&ModMap
= getHeaderSearchInfo().getModuleMap();
300 OptionalDirectoryEntryRef Dir
= Mod
.getEffectiveUmbrellaDir();
301 llvm::vfs::FileSystem
&FS
= FileMgr
.getVirtualFileSystem();
303 for (llvm::vfs::recursive_directory_iterator
Entry(FS
, Dir
->getName(), EC
),
305 Entry
!= End
&& !EC
; Entry
.increment(EC
)) {
306 using llvm::StringSwitch
;
308 // Check whether this entry has an extension typically associated with
310 if (!StringSwitch
<bool>(llvm::sys::path::extension(Entry
->path()))
311 .Cases(".h", ".H", ".hh", ".hpp", true)
315 if (auto Header
= getFileManager().getOptionalFileRef(Entry
->path()))
316 if (!getSourceManager().hasFileInfo(*Header
)) {
317 if (!ModMap
.isHeaderInUnavailableModule(*Header
)) {
318 // Find the relative path that would access this header.
319 SmallString
<128> RelativePath
;
320 computeRelativePath(FileMgr
, *Dir
, *Header
, RelativePath
);
321 Diag(ExpectedHeadersLoc
, diag::warn_uncovered_module_header
)
322 << Mod
.getFullModuleName() << RelativePath
;
328 /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
329 /// the current file. This either returns the EOF token or pops a level off
330 /// the include stack and keeps going.
331 bool Preprocessor::HandleEndOfFile(Token
&Result
, bool isEndOfMacro
) {
332 assert(!CurTokenLexer
&&
333 "Ending a file when currently in a macro!");
335 SourceLocation UnclosedSafeBufferOptOutLoc
;
337 if (IncludeMacroStack
.empty() &&
338 isPPInSafeBufferOptOutRegion(UnclosedSafeBufferOptOutLoc
)) {
339 // To warn if a "-Wunsafe-buffer-usage" opt-out region is still open by the
341 Diag(UnclosedSafeBufferOptOutLoc
,
342 diag::err_pp_unclosed_pragma_unsafe_buffer_usage
);
344 // If we have an unclosed module region from a pragma at the end of a
345 // module, complain and close it now.
346 const bool LeavingSubmodule
= CurLexer
&& CurLexerSubmodule
;
347 if ((LeavingSubmodule
|| IncludeMacroStack
.empty()) &&
348 !BuildingSubmoduleStack
.empty() &&
349 BuildingSubmoduleStack
.back().IsPragma
) {
350 Diag(BuildingSubmoduleStack
.back().ImportLoc
,
351 diag::err_pp_module_begin_without_module_end
);
352 Module
*M
= LeaveSubmodule(/*ForPragma*/true);
355 const char *EndPos
= getCurLexerEndPos();
356 CurLexer
->BufferPtr
= EndPos
;
357 CurLexer
->FormTokenWithChars(Result
, EndPos
, tok::annot_module_end
);
358 Result
.setAnnotationEndLoc(Result
.getLocation());
359 Result
.setAnnotationValue(M
);
363 // See if this file had a controlling macro.
364 if (CurPPLexer
) { // Not ending a macro, ignore it.
365 if (const IdentifierInfo
*ControllingMacro
=
366 CurPPLexer
->MIOpt
.GetControllingMacroAtEndOfFile()) {
367 // Okay, this has a controlling macro, remember in HeaderFileInfo.
368 if (OptionalFileEntryRef FE
= CurPPLexer
->getFileEntry()) {
369 HeaderInfo
.SetFileControllingMacro(*FE
, ControllingMacro
);
370 if (MacroInfo
*MI
= getMacroInfo(ControllingMacro
))
371 MI
->setUsedForHeaderGuard(true);
372 if (const IdentifierInfo
*DefinedMacro
=
373 CurPPLexer
->MIOpt
.GetDefinedMacro()) {
374 if (!isMacroDefined(ControllingMacro
) &&
375 DefinedMacro
!= ControllingMacro
&&
376 CurLexer
->isFirstTimeLexingFile()) {
378 // If the edit distance between the two macros is more than 50%,
379 // DefinedMacro may not be header guard, or can be header guard of
380 // another header file. Therefore, it maybe defining something
381 // completely different. This can be observed in the wild when
382 // handling feature macros or header guards in different files.
384 const StringRef ControllingMacroName
= ControllingMacro
->getName();
385 const StringRef DefinedMacroName
= DefinedMacro
->getName();
386 const size_t MaxHalfLength
= std::max(ControllingMacroName
.size(),
387 DefinedMacroName
.size()) / 2;
388 const unsigned ED
= ControllingMacroName
.edit_distance(
389 DefinedMacroName
, true, MaxHalfLength
);
390 if (ED
<= MaxHalfLength
) {
391 // Emit a warning for a bad header guard.
392 Diag(CurPPLexer
->MIOpt
.GetMacroLocation(),
393 diag::warn_header_guard
)
394 << CurPPLexer
->MIOpt
.GetMacroLocation() << ControllingMacro
;
395 Diag(CurPPLexer
->MIOpt
.GetDefinedLocation(),
396 diag::note_header_guard
)
397 << CurPPLexer
->MIOpt
.GetDefinedLocation() << DefinedMacro
399 << FixItHint::CreateReplacement(
400 CurPPLexer
->MIOpt
.GetDefinedLocation(),
401 ControllingMacro
->getName());
409 // Complain about reaching a true EOF within arc_cf_code_audited.
410 // We don't want to complain about reaching the end of a macro
411 // instantiation or a _Pragma.
412 if (PragmaARCCFCodeAuditedInfo
.second
.isValid() && !isEndOfMacro
&&
413 !(CurLexer
&& CurLexer
->Is_PragmaLexer
)) {
414 Diag(PragmaARCCFCodeAuditedInfo
.second
,
415 diag::err_pp_eof_in_arc_cf_code_audited
);
417 // Recover by leaving immediately.
418 PragmaARCCFCodeAuditedInfo
= {nullptr, SourceLocation()};
421 // Complain about reaching a true EOF within assume_nonnull.
422 // We don't want to complain about reaching the end of a macro
423 // instantiation or a _Pragma.
424 if (PragmaAssumeNonNullLoc
.isValid() &&
425 !isEndOfMacro
&& !(CurLexer
&& CurLexer
->Is_PragmaLexer
)) {
426 // If we're at the end of generating a preamble, we should record the
427 // unterminated \#pragma clang assume_nonnull so we can restore it later
428 // when the preamble is loaded into the main file.
429 if (isRecordingPreamble() && isInPrimaryFile())
430 PreambleRecordedPragmaAssumeNonNullLoc
= PragmaAssumeNonNullLoc
;
432 Diag(PragmaAssumeNonNullLoc
, diag::err_pp_eof_in_assume_nonnull
);
433 // Recover by leaving immediately.
434 PragmaAssumeNonNullLoc
= SourceLocation();
437 bool LeavingPCHThroughHeader
= false;
439 // If this is a #include'd file, pop it off the include stack and continue
440 // lexing the #includer file.
441 if (!IncludeMacroStack
.empty()) {
443 // If we lexed the code-completion file, act as if we reached EOF.
444 if (isCodeCompletionEnabled() && CurPPLexer
&&
445 SourceMgr
.getLocForStartOfFile(CurPPLexer
->getFileID()) ==
446 CodeCompletionFileLoc
) {
447 assert(CurLexer
&& "Got EOF but no current lexer set!");
449 CurLexer
->FormTokenWithChars(Result
, CurLexer
->BufferEnd
, tok::eof
);
452 CurPPLexer
= nullptr;
453 recomputeCurLexerKind();
457 if (!isEndOfMacro
&& CurPPLexer
&&
458 (SourceMgr
.getIncludeLoc(CurPPLexer
->getFileID()).isValid() ||
459 // Predefines file doesn't have a valid include location.
460 (PredefinesFileID
.isValid() &&
461 CurPPLexer
->getFileID() == PredefinesFileID
))) {
462 // Notify SourceManager to record the number of FileIDs that were created
463 // during lexing of the #include'd file.
465 SourceMgr
.local_sloc_entry_size() -
466 CurPPLexer
->getInitialNumSLocEntries() + 1/*#include'd file*/;
467 SourceMgr
.setNumCreatedFIDsForFileID(CurPPLexer
->getFileID(), NumFIDs
);
470 bool ExitedFromPredefinesFile
= false;
472 if (!isEndOfMacro
&& CurPPLexer
) {
473 ExitedFID
= CurPPLexer
->getFileID();
475 assert(PredefinesFileID
.isValid() &&
476 "HandleEndOfFile is called before PredefinesFileId is set");
477 ExitedFromPredefinesFile
= (PredefinesFileID
== ExitedFID
);
480 if (LeavingSubmodule
) {
481 // We're done with this submodule.
482 Module
*M
= LeaveSubmodule(/*ForPragma*/false);
484 // Notify the parser that we've left the module.
485 const char *EndPos
= getCurLexerEndPos();
487 CurLexer
->BufferPtr
= EndPos
;
488 CurLexer
->FormTokenWithChars(Result
, EndPos
, tok::annot_module_end
);
489 Result
.setAnnotationEndLoc(Result
.getLocation());
490 Result
.setAnnotationValue(M
);
493 bool FoundPCHThroughHeader
= false;
494 if (CurPPLexer
&& creatingPCHWithThroughHeader() &&
496 SourceMgr
.getFileEntryForID(CurPPLexer
->getFileID())))
497 FoundPCHThroughHeader
= true;
499 // We're done with the #included file.
500 RemoveTopOfLexerStack();
502 // Propagate info about start-of-line/leading white-space/etc.
503 PropagateLineStartLeadingSpaceInfo(Result
);
505 // Notify the client, if desired, that we are in a new source file.
506 if (Callbacks
&& !isEndOfMacro
&& CurPPLexer
) {
507 SourceLocation Loc
= CurPPLexer
->getSourceLocation();
508 SrcMgr::CharacteristicKind FileType
=
509 SourceMgr
.getFileCharacteristic(Loc
);
510 Callbacks
->FileChanged(Loc
, PPCallbacks::ExitFile
, FileType
, ExitedFID
);
511 Callbacks
->LexedFileChanged(CurPPLexer
->getFileID(),
512 PPCallbacks::LexedFileChangeReason::ExitFile
,
513 FileType
, ExitedFID
, Loc
);
516 // Restore conditional stack as well as the recorded
517 // \#pragma clang assume_nonnull from the preamble right after exiting
518 // from the predefines file.
519 if (ExitedFromPredefinesFile
) {
520 replayPreambleConditionalStack();
521 if (PreambleRecordedPragmaAssumeNonNullLoc
.isValid())
522 PragmaAssumeNonNullLoc
= PreambleRecordedPragmaAssumeNonNullLoc
;
525 if (!isEndOfMacro
&& CurPPLexer
&& FoundPCHThroughHeader
&&
526 (isInPrimaryFile() ||
527 CurPPLexer
->getFileID() == getPredefinesFileID())) {
528 // Leaving the through header. Continue directly to end of main file
530 LeavingPCHThroughHeader
= true;
532 // Client should lex another token unless we generated an EOM.
533 return LeavingSubmodule
;
536 // If this is the end of the main file, form an EOF token.
537 assert(CurLexer
&& "Got EOF but no current lexer set!");
538 const char *EndPos
= getCurLexerEndPos();
540 CurLexer
->BufferPtr
= EndPos
;
542 if (getLangOpts().IncrementalExtensions
) {
543 CurLexer
->FormTokenWithChars(Result
, EndPos
, tok::annot_repl_input_end
);
544 Result
.setAnnotationEndLoc(Result
.getLocation());
545 Result
.setAnnotationValue(nullptr);
547 CurLexer
->FormTokenWithChars(Result
, EndPos
, tok::eof
);
550 if (isCodeCompletionEnabled()) {
551 // Inserting the code-completion point increases the source buffer by 1,
552 // but the main FileID was created before inserting the point.
553 // Compensate by reducing the EOF location by 1, otherwise the location
554 // will point to the next FileID.
555 // FIXME: This is hacky, the code-completion point should probably be
556 // inserted before the main FileID is created.
557 if (CurLexer
->getFileLoc() == CodeCompletionFileLoc
)
558 Result
.setLocation(Result
.getLocation().getLocWithOffset(-1));
561 if (creatingPCHWithThroughHeader() && !LeavingPCHThroughHeader
) {
562 // Reached the end of the compilation without finding the through header.
563 Diag(CurLexer
->getFileLoc(), diag::err_pp_through_header_not_seen
)
564 << PPOpts
->PCHThroughHeader
<< 0;
567 if (!isIncrementalProcessingEnabled())
568 // We're done with lexing.
571 if (!isIncrementalProcessingEnabled())
572 CurPPLexer
= nullptr;
574 if (TUKind
== TU_Complete
) {
575 // This is the end of the top-level file. 'WarnUnusedMacroLocs' has
576 // collected all macro locations that we need to warn because they are not
578 for (WarnUnusedMacroLocsTy::iterator
579 I
=WarnUnusedMacroLocs
.begin(), E
=WarnUnusedMacroLocs
.end();
581 Diag(*I
, diag::pp_macro_not_used
);
584 // If we are building a module that has an umbrella header, make sure that
585 // each of the headers within the directory, including all submodules, is
586 // covered by the umbrella header was actually included by the umbrella
588 if (Module
*Mod
= getCurrentModule()) {
589 llvm::SmallVector
<const Module
*, 4> AllMods
;
590 collectAllSubModulesWithUmbrellaHeader(*Mod
, AllMods
);
591 for (auto *M
: AllMods
)
592 diagnoseMissingHeaderInUmbrellaDir(*M
);
598 /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer
599 /// hits the end of its token stream.
600 bool Preprocessor::HandleEndOfTokenLexer(Token
&Result
) {
601 assert(CurTokenLexer
&& !CurPPLexer
&&
602 "Ending a macro when currently in a #include file!");
604 if (!MacroExpandingLexersStack
.empty() &&
605 MacroExpandingLexersStack
.back().first
== CurTokenLexer
.get())
606 removeCachedMacroExpandedTokensOfLastLexer();
608 // Delete or cache the now-dead macro expander.
609 if (NumCachedTokenLexers
== TokenLexerCacheSize
)
610 CurTokenLexer
.reset();
612 TokenLexerCache
[NumCachedTokenLexers
++] = std::move(CurTokenLexer
);
614 // Handle this like a #include file being popped off the stack.
615 return HandleEndOfFile(Result
, true);
618 /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
619 /// lexer stack. This should only be used in situations where the current
620 /// state of the top-of-stack lexer is unknown.
621 void Preprocessor::RemoveTopOfLexerStack() {
622 assert(!IncludeMacroStack
.empty() && "Ran out of stack entries to load");
625 // Delete or cache the now-dead macro expander.
626 if (NumCachedTokenLexers
== TokenLexerCacheSize
)
627 CurTokenLexer
.reset();
629 TokenLexerCache
[NumCachedTokenLexers
++] = std::move(CurTokenLexer
);
632 PopIncludeMacroStack();
635 /// HandleMicrosoftCommentPaste - When the macro expander pastes together a
636 /// comment (/##/) in microsoft mode, this method handles updating the current
637 /// state, returning the token on the next source line.
638 void Preprocessor::HandleMicrosoftCommentPaste(Token
&Tok
) {
639 assert(CurTokenLexer
&& !CurPPLexer
&&
640 "Pasted comment can only be formed from macro");
641 // We handle this by scanning for the closest real lexer, switching it to
642 // raw mode and preprocessor mode. This will cause it to return \n as an
643 // explicit EOD token.
644 PreprocessorLexer
*FoundLexer
= nullptr;
645 bool LexerWasInPPMode
= false;
646 for (const IncludeStackInfo
&ISI
: llvm::reverse(IncludeMacroStack
)) {
647 if (ISI
.ThePPLexer
== nullptr) continue; // Scan for a real lexer.
649 // Once we find a real lexer, mark it as raw mode (disabling macro
650 // expansions) and preprocessor mode (return EOD). We know that the lexer
651 // was *not* in raw mode before, because the macro that the comment came
652 // from was expanded. However, it could have already been in preprocessor
653 // mode (#if COMMENT) in which case we have to return it to that mode and
655 FoundLexer
= ISI
.ThePPLexer
;
656 FoundLexer
->LexingRawMode
= true;
657 LexerWasInPPMode
= FoundLexer
->ParsingPreprocessorDirective
;
658 FoundLexer
->ParsingPreprocessorDirective
= true;
662 // Okay, we either found and switched over the lexer, or we didn't find a
663 // lexer. In either case, finish off the macro the comment came from, getting
665 if (!HandleEndOfTokenLexer(Tok
)) Lex(Tok
);
667 // Discarding comments as long as we don't have EOF or EOD. This 'comments
668 // out' the rest of the line, including any tokens that came from other macros
669 // that were active, as in:
670 // #define submacro a COMMENT b
672 // which should lex to 'a' only: 'b' and 'c' should be removed.
673 while (Tok
.isNot(tok::eod
) && Tok
.isNot(tok::eof
))
676 // If we got an eod token, then we successfully found the end of the line.
677 if (Tok
.is(tok::eod
)) {
678 assert(FoundLexer
&& "Can't get end of line without an active lexer");
679 // Restore the lexer back to normal mode instead of raw mode.
680 FoundLexer
->LexingRawMode
= false;
682 // If the lexer was already in preprocessor mode, just return the EOD token
683 // to finish the preprocessor line.
684 if (LexerWasInPPMode
) return;
686 // Otherwise, switch out of PP mode and return the next lexed token.
687 FoundLexer
->ParsingPreprocessorDirective
= false;
691 // If we got an EOF token, then we reached the end of the token stream but
692 // didn't find an explicit \n. This can only happen if there was no lexer
693 // active (an active lexer would return EOD at EOF if there was no \n in
694 // preprocessor directive mode), so just return EOF as our token.
695 assert(!FoundLexer
&& "Lexer should return EOD before EOF in PP mode");
698 void Preprocessor::EnterSubmodule(Module
*M
, SourceLocation ImportLoc
,
700 if (!getLangOpts().ModulesLocalVisibility
) {
701 // Just track that we entered this submodule.
702 BuildingSubmoduleStack
.push_back(
703 BuildingSubmoduleInfo(M
, ImportLoc
, ForPragma
, CurSubmoduleState
,
704 PendingModuleMacroNames
.size()));
706 Callbacks
->EnteredSubmodule(M
, ImportLoc
, ForPragma
);
710 // Resolve as much of the module definition as we can now, before we enter
711 // one of its headers.
712 // FIXME: Can we enable Complain here?
713 // FIXME: Can we do this when local visibility is disabled?
714 ModuleMap
&ModMap
= getHeaderSearchInfo().getModuleMap();
715 ModMap
.resolveExports(M
, /*Complain=*/false);
716 ModMap
.resolveUses(M
, /*Complain=*/false);
717 ModMap
.resolveConflicts(M
, /*Complain=*/false);
719 // If this is the first time we've entered this module, set up its state.
720 auto R
= Submodules
.insert(std::make_pair(M
, SubmoduleState()));
721 auto &State
= R
.first
->second
;
722 bool FirstTime
= R
.second
;
724 // Determine the set of starting macros for this submodule; take these
725 // from the "null" module (the predefines buffer).
727 // FIXME: If we have local visibility but not modules enabled, the
728 // NullSubmoduleState is polluted by #defines in the top-level source
730 auto &StartingMacros
= NullSubmoduleState
.Macros
;
732 // Restore to the starting state.
733 // FIXME: Do this lazily, when each macro name is first referenced.
734 for (auto &Macro
: StartingMacros
) {
735 // Skip uninteresting macros.
736 if (!Macro
.second
.getLatest() &&
737 Macro
.second
.getOverriddenMacros().empty())
740 MacroState
MS(Macro
.second
.getLatest());
741 MS
.setOverriddenMacros(*this, Macro
.second
.getOverriddenMacros());
742 State
.Macros
.insert(std::make_pair(Macro
.first
, std::move(MS
)));
746 // Track that we entered this module.
747 BuildingSubmoduleStack
.push_back(
748 BuildingSubmoduleInfo(M
, ImportLoc
, ForPragma
, CurSubmoduleState
,
749 PendingModuleMacroNames
.size()));
752 Callbacks
->EnteredSubmodule(M
, ImportLoc
, ForPragma
);
754 // Switch to this submodule as the current submodule.
755 CurSubmoduleState
= &State
;
757 // This module is visible to itself.
759 makeModuleVisible(M
, ImportLoc
);
762 bool Preprocessor::needModuleMacros() const {
763 // If we're not within a submodule, we never need to create ModuleMacros.
764 if (BuildingSubmoduleStack
.empty())
766 // If we are tracking module macro visibility even for textually-included
767 // headers, we need ModuleMacros.
768 if (getLangOpts().ModulesLocalVisibility
)
770 // Otherwise, we only need module macros if we're actually compiling a module
772 return getLangOpts().isCompilingModule();
775 Module
*Preprocessor::LeaveSubmodule(bool ForPragma
) {
776 if (BuildingSubmoduleStack
.empty() ||
777 BuildingSubmoduleStack
.back().IsPragma
!= ForPragma
) {
778 assert(ForPragma
&& "non-pragma module enter/leave mismatch");
782 auto &Info
= BuildingSubmoduleStack
.back();
784 Module
*LeavingMod
= Info
.M
;
785 SourceLocation ImportLoc
= Info
.ImportLoc
;
787 if (!needModuleMacros() ||
788 (!getLangOpts().ModulesLocalVisibility
&&
789 LeavingMod
->getTopLevelModuleName() != getLangOpts().CurrentModule
)) {
790 // If we don't need module macros, or this is not a module for which we
791 // are tracking macro visibility, don't build any, and preserve the list
792 // of pending names for the surrounding submodule.
793 BuildingSubmoduleStack
.pop_back();
796 Callbacks
->LeftSubmodule(LeavingMod
, ImportLoc
, ForPragma
);
798 makeModuleVisible(LeavingMod
, ImportLoc
);
802 // Create ModuleMacros for any macros defined in this submodule.
803 llvm::SmallPtrSet
<const IdentifierInfo
*, 8> VisitedMacros
;
804 for (unsigned I
= Info
.OuterPendingModuleMacroNames
;
805 I
!= PendingModuleMacroNames
.size(); ++I
) {
806 auto *II
= PendingModuleMacroNames
[I
];
807 if (!VisitedMacros
.insert(II
).second
)
810 auto MacroIt
= CurSubmoduleState
->Macros
.find(II
);
811 if (MacroIt
== CurSubmoduleState
->Macros
.end())
813 auto &Macro
= MacroIt
->second
;
815 // Find the starting point for the MacroDirective chain in this submodule.
816 MacroDirective
*OldMD
= nullptr;
817 auto *OldState
= Info
.OuterSubmoduleState
;
818 if (getLangOpts().ModulesLocalVisibility
)
819 OldState
= &NullSubmoduleState
;
820 if (OldState
&& OldState
!= CurSubmoduleState
) {
821 // FIXME: It'd be better to start at the state from when we most recently
822 // entered this submodule, but it doesn't really matter.
823 auto &OldMacros
= OldState
->Macros
;
824 auto OldMacroIt
= OldMacros
.find(II
);
825 if (OldMacroIt
== OldMacros
.end())
828 OldMD
= OldMacroIt
->second
.getLatest();
831 // This module may have exported a new macro. If so, create a ModuleMacro
832 // representing that fact.
833 bool ExplicitlyPublic
= false;
834 for (auto *MD
= Macro
.getLatest(); MD
!= OldMD
; MD
= MD
->getPrevious()) {
835 assert(MD
&& "broken macro directive chain");
837 if (auto *VisMD
= dyn_cast
<VisibilityMacroDirective
>(MD
)) {
838 // The latest visibility directive for a name in a submodule affects
839 // all the directives that come before it.
840 if (VisMD
->isPublic())
841 ExplicitlyPublic
= true;
842 else if (!ExplicitlyPublic
)
843 // Private with no following public directive: not exported.
846 MacroInfo
*Def
= nullptr;
847 if (DefMacroDirective
*DefMD
= dyn_cast
<DefMacroDirective
>(MD
))
848 Def
= DefMD
->getInfo();
850 // FIXME: Issue a warning if multiple headers for the same submodule
851 // define a macro, rather than silently ignoring all but the first.
853 // Don't bother creating a module macro if it would represent a #undef
854 // that doesn't override anything.
855 if (Def
|| !Macro
.getOverriddenMacros().empty())
856 addModuleMacro(LeavingMod
, II
, Def
, Macro
.getOverriddenMacros(),
859 if (!getLangOpts().ModulesLocalVisibility
) {
860 // This macro is exposed to the rest of this compilation as a
861 // ModuleMacro; we don't need to track its MacroDirective any more.
862 Macro
.setLatest(nullptr);
863 Macro
.setOverriddenMacros(*this, {});
869 PendingModuleMacroNames
.resize(Info
.OuterPendingModuleMacroNames
);
871 // FIXME: Before we leave this submodule, we should parse all the other
872 // headers within it. Otherwise, we're left with an inconsistent state
873 // where we've made the module visible but don't yet have its complete
876 // Put back the outer module's state, if we're tracking it.
877 if (getLangOpts().ModulesLocalVisibility
)
878 CurSubmoduleState
= Info
.OuterSubmoduleState
;
880 BuildingSubmoduleStack
.pop_back();
883 Callbacks
->LeftSubmodule(LeavingMod
, ImportLoc
, ForPragma
);
885 // A nested #include makes the included submodule visible.
886 makeModuleVisible(LeavingMod
, ImportLoc
);