diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp index 0855e6dec6158..02d0de5bea6e9 100644 --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp @@ -391,10 +391,14 @@ class DependencyScanningAction { IntrusiveRefCntPtr FS, std::shared_ptr PCHContainerOps, DiagnosticConsumer *DiagConsumer) { + // Making sure that we canonicalize the defines before we create the deep + // copy to avoid unnecessary variants in the scanner and in the resulting + // explicit command lines. + 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 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.