[clang] [clang][Dependency Scanning] Canonicalize Defines of a Compiler Invocation As Early As Possible (PR #159620)

Qiongsi Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 18 14:56:50 PDT 2025


https://github.com/qiongsiwu updated https://github.com/llvm/llvm-project/pull/159620

>From 250d52ddcafee193a987b9dde639ab2cd5aa81e7 Mon Sep 17 00:00:00 2001
From: Qiongsi Wu <qiongsi_wu at apple.com>
Date: Thu, 18 Sep 2025 11:04:37 -0700
Subject: [PATCH 1/3] Making sure the scanning `CompilerInvocation`'s `-D`s are
 canonicalized.

---
 .../DependencyScanning/DependencyScanningWorker.cpp        | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
index 0855e6dec6158..f2bd5a0c767a1 100644
--- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
+++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
@@ -391,10 +391,13 @@ class DependencyScanningAction {
                      IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
                      std::shared_ptr<PCHContainerOperations> PCHContainerOps,
                      DiagnosticConsumer *DiagConsumer) {
+    // Making sure that we canonicalize the defines before we create the deep
+    // copy.
+    if (any(Service.getOptimizeArgs() & ScanningOptimizations::Macros))
+      canonicalizeDefines(Invocation->getPreprocessorOpts());
+
     // Make a deep copy of the original Clang invocation.
     CompilerInvocation OriginalInvocation(*Invocation);
-    if (any(Service.getOptimizeArgs() & ScanningOptimizations::Macros))
-      canonicalizeDefines(OriginalInvocation.getPreprocessorOpts());
 
     if (Scanned) {
       // Scanning runs once for the first -cc1 invocation in a chain of driver

>From 6520631cfe748b9d9125cd67d25ead04b59842ce Mon Sep 17 00:00:00 2001
From: Qiongsi Wu <qiongsi_wu at apple.com>
Date: Thu, 18 Sep 2025 14:55:39 -0700
Subject: [PATCH 2/3] Adding a test to check number of scanning module
 variants.

---
 clang/test/ClangScanDeps/optimize-canonicalize-macros.m | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/test/ClangScanDeps/optimize-canonicalize-macros.m b/clang/test/ClangScanDeps/optimize-canonicalize-macros.m
index 46ae1c84ec7ba..bc434b516f942 100644
--- a/clang/test/ClangScanDeps/optimize-canonicalize-macros.m
+++ b/clang/test/ClangScanDeps/optimize-canonicalize-macros.m
@@ -8,6 +8,9 @@
 // RUN:   -j 1 -format experimental-full -optimize-args=canonicalize-macros > %t/deps.db
 // RUN: cat %t/deps.db | FileCheck %s -DPREFIX=%/t
 
+// This tests that we have two scanning module variants.
+// RUN: find %t/module-cache -name "*.pcm" | wc -l | grep 2
+
 // Verify that there are only two variants and that the expected merges have
 // happened.
 

>From ec423dbfedc2fb771e80ebd28e71d24359569c81 Mon Sep 17 00:00:00 2001
From: Qiongsi Wu <qiongsiwu at gmail.com>
Date: Thu, 18 Sep 2025 14:56:41 -0700
Subject: [PATCH 3/3] Update
 clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Modifying the comment to make it more explicit.

Co-authored-by: Jan Svoboda <jan at svoboda.ai>
---
 .../Tooling/DependencyScanning/DependencyScanningWorker.cpp    | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
index f2bd5a0c767a1..02d0de5bea6e9 100644
--- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
+++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
@@ -392,7 +392,8 @@ class DependencyScanningAction {
                      std::shared_ptr<PCHContainerOperations> PCHContainerOps,
                      DiagnosticConsumer *DiagConsumer) {
     // Making sure that we canonicalize the defines before we create the deep
-    // copy.
+    // copy to avoid unnecessary variants in the scanner and in the resulting
+    // explicit command lines.
     if (any(Service.getOptimizeArgs() & ScanningOptimizations::Macros))
       canonicalizeDefines(Invocation->getPreprocessorOpts());
 



More information about the cfe-commits mailing list