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

Conversation

ayushpareek2003
Copy link

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

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
Copy link

github-actions bot commented May 2, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label May 2, 2025
@llvmbot
Copy link
Member

llvmbot commented May 2, 2025

@llvm/pr-subscribers-clang-modules
@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: Ayush Pareek (ayushpareek2003)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/138256.diff

1 Files Affected:

  • (modified) clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp (+11-10)
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 = {};
   }
 }
 

@AaronBallman AaronBallman added clang:codegen IR generation bugs: mangling, exceptions, etc. clang:tooling LibTooling and removed clang:codegen IR generation bugs: mangling, exceptions, etc. labels May 2, 2025
Copy link
Member

@ChuanqiXu9 ChuanqiXu9 left a comment

Choose a reason for hiding this comment

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

Could you elaborate? Why = {} is better than .clear() ?

@ayushpareek2003
Copy link
Author

Could you elaborate? Why = {} is better than .clear() ?

according to my understanding, Clang stores some build-specific data in CodeGenOptions like file paths and debug info. When generating modules or precompiled headers, this data can make the output different from build to build. To avoid that, we reset those fields.

I changed .clear() to = {} because, as I understand, = {} resets the entire field to its default value, not just empties it. This is helpful because it works safely across different types—not just std::string, but also for types like std::optional, smallString, or other structs that might be used in the future. Using = {} makes it easier to refactor or extend the structure later without having to update every .clear() call. It also communicates intent more clearly: we’re not just clearing content, we’re fully resetting the field. This reduces the risk of leaking build-specific paths or flags into the output, and helps make Clang’s module and PCH output more consistent and reproducible across different systems. I did this to make the code more future-proof, maintainable, and reliable.

@ChuanqiXu9
Copy link
Member

I feel .clear() should have a similar semantic with = {};. It will be confusing for people to understand this. It will be helpful if you can provide a test to show the difference.

@ayushpareek2003
Copy link
Author

ayushpareek2003 commented May 6, 2025

I feel .clear() should have a similar semantic with = {};. It will be confusing for people to understand this. It will be helpful if you can provide a test to show the difference.

I’ve tried to demonstrate the difference between .clear() and = {}, not a formal test case, but just a minimal example to illustrate the behavior clearly. It’s meant to show how these two approaches behave differently, especially when dealing with types like std::optional.

`

std::optional<std::string> A = "clang";

auto B = A;
auto C = A;
B->clear(); // empties the string but keeps the optional engaged
C = {};     // fully resets the optional to disengaged (nullopt)

std::cout << "B has value? " << B.has_value() << "\n"; // it will print 1
std::cout << "C has value? " << C.has_value() << "\n"; // it will print 0`

I hope it helps clarify why = {} can be a safer and more expressive choice in some situations

@ChuanqiXu9
Copy link
Member

hmmm for test case, I mean a test case in clang, either a unit test or a lit test, so we can understand the effect on users more clearly.

And if you're talking about the general code policy, I'll suggest you to https://discourse.llvm.org/ to make it more verbose.

@llvmbot llvmbot added the clang:modules C++20 modules and Clang Header Modules label May 6, 2025
@ayushpareek2003
Copy link
Author

ayushpareek2003 commented May 6, 2025

hmmm for test case, I mean a test case in clang, either a unit test or a lit test, so we can understand the effect on users more clearly.

And if you're talking about the general code policy, I'll suggest you to https://discourse.llvm.org/ to make it more verbose.

I've added a test under clang/test/Modules/reset-codegen-options.c to demonstrate that the affected fields (like MainFileName and DebugCompilationDir) are correctly reset and no longer leak into the module or PCH output.
This test helps confirm that switching from .clear() to = {} results in cleaner and more consistent output, particularly by fully resetting the fields to their default-constructed state

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:modules C++20 modules and Clang Header Modules clang:tooling LibTooling clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants