Skip to content

Commit c35339a

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

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
@@ -320,6 +320,11 @@ class ModuleDepCollector final : public DependencyCollector {
320320
ModuleDeps &Deps);
321321
};
322322

323+
/// Resets codegen options that don't affect modules/PCH.
324+
void resetBenignCodeGenOptions(frontend::ActionKind ProgramAction,
325+
const LangOptions &LangOpts,
326+
CodeGenOptions &CGOpts);
327+
323328
} // end namespace dependencies
324329
} // end namespace tooling
325330
} // end namespace clang

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,26 @@ void ModuleDepCollector::addOutputPaths(CowCompilerInvocation &CI,
160160
}
161161
}
162162

163+
void dependencies::resetBenignCodeGenOptions(frontend::ActionKind ProgramAction,
164+
const LangOptions &LangOpts,
165+
CodeGenOptions &CGOpts) {
166+
// TODO: Figure out better way to set options to their default value.
167+
if (ProgramAction == frontend::GenerateModule) {
168+
CGOpts.MainFileName.clear();
169+
CGOpts.DwarfDebugFlags.clear();
170+
}
171+
if (ProgramAction == frontend::GeneratePCH ||
172+
(ProgramAction == frontend::GenerateModule && !LangOpts.ModulesCodegen)) {
173+
CGOpts.DebugCompilationDir.clear();
174+
CGOpts.CoverageCompilationDir.clear();
175+
CGOpts.CoverageDataFile.clear();
176+
CGOpts.CoverageNotesFile.clear();
177+
CGOpts.ProfileInstrumentUsePath.clear();
178+
CGOpts.SampleProfileFile.clear();
179+
CGOpts.ProfileRemappingFile.clear();
180+
}
181+
}
182+
163183
static CowCompilerInvocation
164184
makeCommonInvocationForModuleBuild(CompilerInvocation CI) {
165185
CI.resetNonModularOptions();
@@ -176,18 +196,8 @@ makeCommonInvocationForModuleBuild(CompilerInvocation CI) {
176196
// LLVM options are not going to affect the AST
177197
CI.getFrontendOpts().LLVMArgs.clear();
178198

179-
// TODO: Figure out better way to set options to their default value.
180-
CI.getCodeGenOpts().MainFileName.clear();
181-
CI.getCodeGenOpts().DwarfDebugFlags.clear();
182-
if (!CI.getLangOpts().ModulesCodegen) {
183-
CI.getCodeGenOpts().DebugCompilationDir.clear();
184-
CI.getCodeGenOpts().CoverageCompilationDir.clear();
185-
CI.getCodeGenOpts().CoverageDataFile.clear();
186-
CI.getCodeGenOpts().CoverageNotesFile.clear();
187-
CI.getCodeGenOpts().ProfileInstrumentUsePath.clear();
188-
CI.getCodeGenOpts().SampleProfileFile.clear();
189-
CI.getCodeGenOpts().ProfileRemappingFile.clear();
190-
}
199+
resetBenignCodeGenOptions(frontend::GenerateModule, CI.getLangOpts(),
200+
CI.getCodeGenOpts());
191201

192202
// Map output paths that affect behaviour to "-" so their existence is in the
193203
// context hash. The final path will be computed in addOutputPaths.
@@ -371,6 +381,8 @@ static bool needsModules(FrontendInputFile FIF) {
371381

372382
void ModuleDepCollector::applyDiscoveredDependencies(CompilerInvocation &CI) {
373383
CI.clearImplicitModuleBuildOptions();
384+
resetBenignCodeGenOptions(CI.getFrontendOpts().ProgramAction,
385+
CI.getLangOpts(), CI.getCodeGenOpts());
374386

375387
if (llvm::any_of(CI.getFrontendOpts().Inputs, needsModules)) {
376388
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)