From 44141892a3cb4f50180e13315e3db16e963614ef Mon Sep 17 00:00:00 2001 From: Ayush Pareek Date: Fri, 2 May 2025 17:29:48 +0530 Subject: [PATCH 1/2] Reset CodeGenOptions fields for clean module/PCH builds This change resets certain CodeGenOptions fields using = {} instead of .clear(), to clean up build-specific data when generating modules or PCH files. It helps make the output more consistent and easier to maintain --- .../DependencyScanning/ModuleDepCollector.cpp | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp index b3016f90122df..5fbc502b15c3a 100644 --- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp +++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp @@ -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. + // For actions that produce modules or PCHs, reset options that could leak 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 = {}; } } From 16089ec4df9c3f618a084d6e60e2366f0b8c1ff6 Mon Sep 17 00:00:00 2001 From: Ayush Pareek Date: Tue, 6 May 2025 15:01:33 +0530 Subject: [PATCH 2/2] Add files via upload --- clang/test/Modules/reset-codegen-options.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 clang/test/Modules/reset-codegen-options.c diff --git a/clang/test/Modules/reset-codegen-options.c b/clang/test/Modules/reset-codegen-options.c new file mode 100644 index 0000000000000..57b3855232089 --- /dev/null +++ b/clang/test/Modules/reset-codegen-options.c @@ -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; + } + \ No newline at end of file