Skip to content

Commit 6331024

Browse files
authored
[clang/DependencyScanning/ModuleDepCollector] Refactor part of makeCommonInvocationForModuleBuild into its own function (#88447)
The new function is about clearing out benign codegen options and can be applied for PCH invocations as well.
1 parent a1ed652 commit 6331024

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ class ModuleDepCollector final : public DependencyCollector {
308308
ModuleDeps &Deps);
309309
};
310310

311+
/// Resets codegen options that don't affect modules/PCH.
312+
void resetBenignCodeGenOptions(frontend::ActionKind ProgramAction,
313+
const LangOptions &LangOpts,
314+
CodeGenOptions &CGOpts);
315+
311316
} // end namespace dependencies
312317
} // end namespace tooling
313318
} // end namespace clang

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,26 @@ void ModuleDepCollector::addOutputPaths(CowCompilerInvocation &CI,
154154
}
155155
}
156156

157+
void dependencies::resetBenignCodeGenOptions(frontend::ActionKind ProgramAction,
158+
const LangOptions &LangOpts,
159+
CodeGenOptions &CGOpts) {
160+
// TODO: Figure out better way to set options to their default value.
161+
if (ProgramAction == frontend::GenerateModule) {
162+
CGOpts.MainFileName.clear();
163+
CGOpts.DwarfDebugFlags.clear();
164+
}
165+
if (ProgramAction == frontend::GeneratePCH ||
166+
(ProgramAction == frontend::GenerateModule && !LangOpts.ModulesCodegen)) {
167+
CGOpts.DebugCompilationDir.clear();
168+
CGOpts.CoverageCompilationDir.clear();
169+
CGOpts.CoverageDataFile.clear();
170+
CGOpts.CoverageNotesFile.clear();
171+
CGOpts.ProfileInstrumentUsePath.clear();
172+
CGOpts.SampleProfileFile.clear();
173+
CGOpts.ProfileRemappingFile.clear();
174+
}
175+
}
176+
157177
static CowCompilerInvocation
158178
makeCommonInvocationForModuleBuild(CompilerInvocation CI) {
159179
CI.resetNonModularOptions();
@@ -167,18 +187,8 @@ makeCommonInvocationForModuleBuild(CompilerInvocation CI) {
167187
// LLVM options are not going to affect the AST
168188
CI.getFrontendOpts().LLVMArgs.clear();
169189

170-
// TODO: Figure out better way to set options to their default value.
171-
CI.getCodeGenOpts().MainFileName.clear();
172-
CI.getCodeGenOpts().DwarfDebugFlags.clear();
173-
if (!CI.getLangOpts().ModulesCodegen) {
174-
CI.getCodeGenOpts().DebugCompilationDir.clear();
175-
CI.getCodeGenOpts().CoverageCompilationDir.clear();
176-
CI.getCodeGenOpts().CoverageDataFile.clear();
177-
CI.getCodeGenOpts().CoverageNotesFile.clear();
178-
CI.getCodeGenOpts().ProfileInstrumentUsePath.clear();
179-
CI.getCodeGenOpts().SampleProfileFile.clear();
180-
CI.getCodeGenOpts().ProfileRemappingFile.clear();
181-
}
190+
resetBenignCodeGenOptions(frontend::GenerateModule, CI.getLangOpts(),
191+
CI.getCodeGenOpts());
182192

183193
// Map output paths that affect behaviour to "-" so their existence is in the
184194
// context hash. The final path will be computed in addOutputPaths.
@@ -342,6 +352,8 @@ static bool needsModules(FrontendInputFile FIF) {
342352

343353
void ModuleDepCollector::applyDiscoveredDependencies(CompilerInvocation &CI) {
344354
CI.clearImplicitModuleBuildOptions();
355+
resetBenignCodeGenOptions(CI.getFrontendOpts().ProgramAction,
356+
CI.getLangOpts(), CI.getCodeGenOpts());
345357

346358
if (llvm::any_of(CI.getFrontendOpts().Inputs, needsModules)) {
347359
Preprocessor &PP = ScanInstance.getPreprocessor();

clang/test/ClangScanDeps/removed-args.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,31 @@
9393
// CHECK-NOT: "-fmodules-prune-interval=
9494
// CHECK-NOT: "-fmodules-prune-after=
9595
// CHECK: ],
96+
97+
// Check for removed args for PCH invocations.
98+
99+
// RUN: split-file %s %t
100+
// RUN: sed "s|DIR|%/t|g" %t/cdb-pch.json.template > %t/cdb-pch.json
101+
// RUN: clang-scan-deps -compilation-database %t/cdb-pch.json -format experimental-full > %t/result-pch.json
102+
// RUN: cat %t/result-pch.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t -check-prefix=PCH
103+
//
104+
// PCH-NOT: "-fdebug-compilation-dir="
105+
// PCH-NOT: "-fcoverage-compilation-dir="
106+
// PCH-NOT: "-coverage-notes-file
107+
// PCH-NOT: "-coverage-data-file
108+
// PCH-NOT: "-fprofile-instrument-use-path
109+
// PCH-NOT: "-include"
110+
// PCH-NOT: "-fmodules-cache-path=
111+
// PCH-NOT: "-fmodules-validate-once-per-build-session"
112+
// PCH-NOT: "-fbuild-session-timestamp=
113+
// PCH-NOT: "-fmodules-prune-interval=
114+
// PCH-NOT: "-fmodules-prune-after=
115+
116+
//--- cdb-pch.json.template
117+
[
118+
{
119+
"directory": "DIR",
120+
"command": "clang -x c-header DIR/header.h -fmodules -fimplicit-module-maps -fmodules-cache-path=DIR/cache -fdebug-compilation-dir=DIR/debug -fcoverage-compilation-dir=DIR/coverage -ftest-coverage -fprofile-instr-use=DIR/tu.profdata -o DIR/header.h.pch -serialize-diagnostics DIR/header.h.pch.diag ",
121+
"file": "DIR/header.h.pch"
122+
}
123+
]

0 commit comments

Comments
 (0)