Skip to content

Reset CodeGenOptions fields for clean module/PCH builds #138256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,21 @@ void ModuleDepCollector::addOutputPaths(CowCompilerInvocation &CI,
void dependencies::resetBenignCodeGenOptions(frontend::ActionKind ProgramAction,
const LangOptions &LangOpts,
CodeGenOptions &CGOpts) {
// TODO: Figure out better way to set options to their default value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is talking about setting it to the default value it would have if no argument is passed, which for these cases is empty, but not all options default to empty.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that makes sense. I was referring to the default-constructed values here, which happen to be empty strings. I used = {} for clearer intent and future-proofing. You're right, not all fields default to empty, I’ll update the comment accordingly.

// Reset to default-constructed values to avoid leaking build-specific data.
if (ProgramAction == frontend::GenerateModule) {
CGOpts.MainFileName.clear();
CGOpts.DwarfDebugFlags.clear();
CGOpts.MainFileName = {};
CGOpts.DwarfDebugFlags = {};
}

if (ProgramAction == frontend::GeneratePCH ||
(ProgramAction == frontend::GenerateModule && !LangOpts.ModulesCodegen)) {
CGOpts.DebugCompilationDir.clear();
CGOpts.CoverageCompilationDir.clear();
CGOpts.CoverageDataFile.clear();
CGOpts.CoverageNotesFile.clear();
CGOpts.ProfileInstrumentUsePath.clear();
CGOpts.SampleProfileFile.clear();
CGOpts.ProfileRemappingFile.clear();
CGOpts.DebugCompilationDir = {};
CGOpts.CoverageCompilationDir = {};
CGOpts.CoverageDataFile = {};
CGOpts.CoverageNotesFile = {};
CGOpts.ProfileInstrumentUsePath = {};
CGOpts.SampleProfileFile = {};
CGOpts.ProfileRemappingFile = {};
}
}

Expand Down
21 changes: 21 additions & 0 deletions clang/test/Modules/reset-codegen-options.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// REQUIRES: modules

// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fmodules -emit-module-interface -fmodule-name=TestModule -o - %s \
// RUN: -fmodule-file=module.pcm 2>&1 | FileCheck %s --implicit-check-not="MainFileName" --implicit-check-not="DebugCompilationDir"

// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-pch -o - %s 2>&1 | FileCheck %s --implicit-check-not="DwarfDebugFlags"

// CHECK-NOT: MainFileName
// CHECK-NOT: DwarfDebugFlags
// CHECK-NOT: DebugCompilationDir
// CHECK-NOT: CoverageCompilationDir
// CHECK-NOT: CoverageDataFile
// CHECK-NOT: CoverageNotesFile
// CHECK-NOT: ProfileInstrumentUsePath
// CHECK-NOT: SampleProfileFile
// CHECK-NOT: ProfileRemappingFile

int foo() {
return 42;
}