Skip to content

Commit 2248164

Browse files
committed
Revert "[clang] Move state out of PreprocessorOptions (1/n) (#86358)"
This reverts commit 407a2f2 which stopped propagating the callback to module compiles, effectively disabling dependency directive scanning for all modular dependencies. Also added a regression test.
1 parent 4ae8694 commit 2248164

File tree

9 files changed

+67
-65
lines changed

9 files changed

+67
-65
lines changed

clang/include/clang/Frontend/FrontendActions.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,12 @@ class InitOnlyAction : public FrontendAction {
3434

3535
/// Preprocessor-based frontend action that also loads PCH files.
3636
class ReadPCHAndPreprocessAction : public FrontendAction {
37-
llvm::unique_function<void(CompilerInstance &)> AdjustCI;
38-
3937
void ExecuteAction() override;
4038

4139
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
4240
StringRef InFile) override;
4341

4442
public:
45-
ReadPCHAndPreprocessAction(
46-
llvm::unique_function<void(CompilerInstance &)> AdjustCI)
47-
: AdjustCI(std::move(AdjustCI)) {}
48-
4943
bool usesPreprocessorOnly() const override { return false; }
5044
};
5145

@@ -327,15 +321,11 @@ class PrintPreprocessedAction : public PreprocessorFrontendAction {
327321

328322
class GetDependenciesByModuleNameAction : public PreprocessOnlyAction {
329323
StringRef ModuleName;
330-
llvm::unique_function<void(CompilerInstance &)> AdjustCI;
331-
332324
void ExecuteAction() override;
333325

334326
public:
335-
GetDependenciesByModuleNameAction(
336-
StringRef ModuleName,
337-
llvm::unique_function<void(CompilerInstance &)> AdjustCI)
338-
: ModuleName(ModuleName), AdjustCI(std::move(AdjustCI)) {}
327+
GetDependenciesByModuleNameAction(StringRef ModuleName)
328+
: ModuleName(ModuleName) {}
339329
};
340330

341331
} // end namespace clang

clang/include/clang/Lex/Preprocessor.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -736,19 +736,6 @@ class Preprocessor {
736736
State ConditionalStackState = Off;
737737
} PreambleConditionalStack;
738738

739-
/// Function for getting the dependency preprocessor directives of a file.
740-
///
741-
/// These are directives derived from a special form of lexing where the
742-
/// source input is scanned for the preprocessor directives that might have an
743-
/// effect on the dependencies for a compilation unit.
744-
///
745-
/// Enables a client to cache the directives for a file and provide them
746-
/// across multiple compiler invocations.
747-
/// FIXME: Allow returning an error.
748-
using DependencyDirectivesFn = llvm::unique_function<std::optional<
749-
ArrayRef<dependency_directives_scan::Directive>>(FileEntryRef)>;
750-
DependencyDirectivesFn DependencyDirectivesForFile;
751-
752739
/// The current top of the stack that we're lexing from if
753740
/// not expanding a macro and we are lexing directly from source code.
754741
///
@@ -1283,11 +1270,6 @@ class Preprocessor {
12831270
/// false if it is producing tokens to be consumed by Parse and Sema.
12841271
bool isPreprocessedOutput() const { return PreprocessedOutput; }
12851272

1286-
/// Set the function used to get dependency directives for a file.
1287-
void setDependencyDirectivesFn(DependencyDirectivesFn Fn) {
1288-
DependencyDirectivesForFile = std::move(Fn);
1289-
}
1290-
12911273
/// Return true if we are lexing directly from the specified lexer.
12921274
bool isCurrentLexer(const PreprocessorLexer *L) const {
12931275
return CurPPLexer == L;

clang/include/clang/Lex/PreprocessorOptions.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ class PreprocessorOptions {
186186
/// with support for lifetime-qualified pointers.
187187
ObjCXXARCStandardLibraryKind ObjCXXARCStandardLibrary = ARCXX_nolib;
188188

189+
/// Function for getting the dependency preprocessor directives of a file.
190+
///
191+
/// These are directives derived from a special form of lexing where the
192+
/// source input is scanned for the preprocessor directives that might have an
193+
/// effect on the dependencies for a compilation unit.
194+
///
195+
/// Enables a client to cache the directives for a file and provide them
196+
/// across multiple compiler invocations.
197+
/// FIXME: Allow returning an error.
198+
std::function<std::optional<ArrayRef<dependency_directives_scan::Directive>>(
199+
FileEntryRef)>
200+
DependencyDirectivesForFile;
201+
189202
/// Set up preprocessor for RunAnalysis action.
190203
bool SetUpStaticAnalyzer = false;
191204

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ void InitOnlyAction::ExecuteAction() {
6969

7070
// Basically PreprocessOnlyAction::ExecuteAction.
7171
void ReadPCHAndPreprocessAction::ExecuteAction() {
72-
CompilerInstance &CI = getCompilerInstance();
73-
AdjustCI(CI);
74-
75-
Preprocessor &PP = CI.getPreprocessor();
72+
Preprocessor &PP = getCompilerInstance().getPreprocessor();
7673

7774
// Ignore unknown pragmas.
7875
PP.IgnorePragmas();
@@ -1193,8 +1190,6 @@ void PrintDependencyDirectivesSourceMinimizerAction::ExecuteAction() {
11931190

11941191
void GetDependenciesByModuleNameAction::ExecuteAction() {
11951192
CompilerInstance &CI = getCompilerInstance();
1196-
AdjustCI(CI);
1197-
11981193
Preprocessor &PP = CI.getPreprocessor();
11991194
SourceManager &SM = PP.getSourceManager();
12001195
FileID MainFileID = SM.getMainFileID();

clang/lib/Lex/PPLexerChange.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,16 @@ bool Preprocessor::EnterSourceFile(FileID FID, ConstSearchDirIterator CurDir,
9393
}
9494

9595
Lexer *TheLexer = new Lexer(FID, *InputFile, *this, IsFirstIncludeOfFile);
96-
if (DependencyDirectivesForFile && FID != PredefinesFileID)
97-
if (OptionalFileEntryRef File = SourceMgr.getFileEntryRefForID(FID))
98-
if (auto DepDirectives = DependencyDirectivesForFile(*File))
96+
if (getPreprocessorOpts().DependencyDirectivesForFile &&
97+
FID != PredefinesFileID) {
98+
if (OptionalFileEntryRef File = SourceMgr.getFileEntryRefForID(FID)) {
99+
if (std::optional<ArrayRef<dependency_directives_scan::Directive>>
100+
DepDirectives =
101+
getPreprocessorOpts().DependencyDirectivesForFile(*File)) {
99102
TheLexer->DepDirectives = *DepDirectives;
103+
}
104+
}
105+
}
100106

101107
EnterSourceFileWithLexer(TheLexer, CurDir);
102108
return false;

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -363,22 +363,17 @@ class DependencyScanningAction : public tooling::ToolAction {
363363
PrebuiltModuleVFSMap, ScanInstance.getDiagnostics()))
364364
return false;
365365

366-
auto AdjustCI = [&](CompilerInstance &CI) {
367-
// Set up the dependency scanning file system callback if requested.
368-
if (DepFS) {
369-
auto GetDependencyDirectives = [LocalDepFS = DepFS](FileEntryRef File)
370-
-> std::optional<ArrayRef<dependency_directives_scan::Directive>> {
371-
if (llvm::ErrorOr<EntryRef> Entry =
372-
LocalDepFS->getOrCreateFileSystemEntry(File.getName()))
373-
if (LocalDepFS->ensureDirectiveTokensArePopulated(*Entry))
374-
return Entry->getDirectiveTokens();
375-
return std::nullopt;
376-
};
377-
378-
CI.getPreprocessor().setDependencyDirectivesFn(
379-
std::move(GetDependencyDirectives));
380-
}
381-
};
366+
// Use the dependency scanning optimized file system if requested to do so.
367+
if (DepFS)
368+
ScanInstance.getPreprocessorOpts().DependencyDirectivesForFile =
369+
[LocalDepFS = DepFS](FileEntryRef File)
370+
-> std::optional<ArrayRef<dependency_directives_scan::Directive>> {
371+
if (llvm::ErrorOr<EntryRef> Entry =
372+
LocalDepFS->getOrCreateFileSystemEntry(File.getName()))
373+
if (LocalDepFS->ensureDirectiveTokensArePopulated(*Entry))
374+
return Entry->getDirectiveTokens();
375+
return std::nullopt;
376+
};
382377

383378
// Create the dependency collector that will collect the produced
384379
// dependencies.
@@ -430,11 +425,9 @@ class DependencyScanningAction : public tooling::ToolAction {
430425
std::unique_ptr<FrontendAction> Action;
431426

432427
if (ModuleName)
433-
Action = std::make_unique<GetDependenciesByModuleNameAction>(
434-
*ModuleName, std::move(AdjustCI));
428+
Action = std::make_unique<GetDependenciesByModuleNameAction>(*ModuleName);
435429
else
436-
Action =
437-
std::make_unique<ReadPCHAndPreprocessAction>(std::move(AdjustCI));
430+
Action = std::make_unique<ReadPCHAndPreprocessAction>();
438431

439432
if (ScanInstance.getDiagnostics().hasErrorOccurred())
440433
return false;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
4+
// This test checks that source files of modules undergo dependency directives
5+
// scan. If a.h would not, the scan would fail when lexing `#error`.
6+
7+
//--- module.modulemap
8+
module A { header "a.h" }
9+
10+
//--- a.h
11+
#error blah
12+
13+
//--- tu.c
14+
#include "a.h"
15+
16+
//--- cdb.json.in
17+
[{
18+
"directory": "DIR",
19+
"file": "DIR/tu.c",
20+
"command": "clang -c DIR/tu.c -fmodules -fmodules-cache-path=DIR/cache"
21+
}]
22+
23+
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.in > %t/cdb.json
24+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full > %t/deps.json

clang/unittests/Lex/PPDependencyDirectivesTest.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ TEST_F(PPDependencyDirectivesTest, MacroGuard) {
117117
};
118118

119119
auto PPOpts = std::make_shared<PreprocessorOptions>();
120+
PPOpts->DependencyDirectivesForFile = [&](FileEntryRef File)
121+
-> std::optional<ArrayRef<dependency_directives_scan::Directive>> {
122+
return getDependencyDirectives(File);
123+
};
124+
120125
TrivialModuleLoader ModLoader;
121126
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
122127
Diags, LangOpts, Target.get());
@@ -125,12 +130,6 @@ TEST_F(PPDependencyDirectivesTest, MacroGuard) {
125130
/*OwnsHeaderSearch =*/false);
126131
PP.Initialize(*Target);
127132

128-
PP.setDependencyDirectivesFn(
129-
[&](FileEntryRef File)
130-
-> std::optional<ArrayRef<dependency_directives_scan::Directive>> {
131-
return getDependencyDirectives(File);
132-
});
133-
134133
SmallVector<StringRef> IncludedFiles;
135134
PP.addPPCallbacks(std::make_unique<IncludeCollector>(PP, IncludedFiles));
136135
PP.EnterMainSourceFile();

0 commit comments

Comments
 (0)