Author: Qiongsi Wu
Date: 2026-06-23T08:16:55-07:00
New Revision: 820314fae19408e906ef8144573fca292b411613

URL: 
https://github.com/llvm/llvm-project/commit/820314fae19408e906ef8144573fca292b411613
DIFF: 
https://github.com/llvm/llvm-project/commit/820314fae19408e906ef8144573fca292b411613.diff

LOG: [clang][Dependency Scanning] Fix the Input File for By-Name Lookup's Input 
CC1 Command Line (#205214)

When the command line is a CC1 command, the scanner does not append the
fake input file to the command line when initializing the compiler
instance. This PR fixes that by passing the compiler instance
initialization the correct modified command line. Without specifying the
fake input file, clang picks up `-` as its input.

An observable behavior is that the diagnostics are pointing to incorrect
files for cc1 commands, hence a test is added to check the diagnostics
messages contain the correct file name.

Added: 
    

Modified: 
    clang/lib/Tooling/DependencyScanningTool.cpp
    clang/test/ClangScanDeps/modules-full-by-mult-mod-names-diagnostics.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Tooling/DependencyScanningTool.cpp 
b/clang/lib/Tooling/DependencyScanningTool.cpp
index b4b45798c514d..d55367107862d 100644
--- a/clang/lib/Tooling/DependencyScanningTool.cpp
+++ b/clang/lib/Tooling/DependencyScanningTool.cpp
@@ -378,10 +378,11 @@ CompilerInstanceWithContext::initializeFromCommandline(
       std::make_unique<DiagnosticsEngineWithDiagOpts>(ModifiedCommandLine, FS,
                                                       DC);
 
-  if (CommandLine.size() >= 2 && CommandLine[1] == "-cc1") {
+  if (ModifiedCommandLine.size() >= 2 && ModifiedCommandLine[1] == "-cc1") {
     // The input command line is already a -cc1 invocation; initialize the
     // compiler instance directly from it.
-    CompilerInstanceWithContext CIWithContext(Tool.Worker, CWD, CommandLine);
+    CompilerInstanceWithContext CIWithContext(Tool.Worker, CWD,
+                                              ModifiedCommandLine);
     if (!CIWithContext.initialize(Controller,
                                   std::move(DiagEngineWithCmdAndOpts),
                                   std::move(OverlayFS)))

diff  --git 
a/clang/test/ClangScanDeps/modules-full-by-mult-mod-names-diagnostics.c 
b/clang/test/ClangScanDeps/modules-full-by-mult-mod-names-diagnostics.c
index cf34e19be48a9..0eafb119c6bfe 100644
--- a/clang/test/ClangScanDeps/modules-full-by-mult-mod-names-diagnostics.c
+++ b/clang/test/ClangScanDeps/modules-full-by-mult-mod-names-diagnostics.c
@@ -26,6 +26,20 @@ module root2 { header "root2.h" }
 // RUN: cat %t/error.txt | FileCheck %s --check-prefixes=ERROR
 // RUN: cat %t/result.json | sed 's:\\\\\?:/:g' | FileCheck -DPREFIX=%/t %s
 
+//--- cdb.cc1.json.template
+[{
+  "file": "",
+  "directory": "DIR",
+  "command": "clang -cc1 -nostdsysteminc -nobuiltininc -fmodules 
-fimplicit-module-maps -fmodules-cache-path=DIR/cache -I DIR -x c"
+}]
+
+// RUN: sed "s|DIR|%/t|g" %t/cdb.cc1.json.template > %t/cdb.cc1.json
+// RUN: not clang-scan-deps -compilation-database %t/cdb.cc1.json -format \
+// RUN:   experimental-full -module-names=modA,root,modB,modC,root2 2> \
+// RUN:   %t/error.cc1.txt > %t/result.cc1.json
+// RUN: cat %t/error.cc1.txt | FileCheck %s --check-prefixes=ERROR
+// RUN: cat %t/result.cc1.json | sed 's:\\\\\?:/:g' | FileCheck -DPREFIX=%/t %s
+
 // ERROR: Error while scanning dependencies for modA:
 // ERROR-NEXT: module-include.input:1:1: fatal error: module 'modA' not found
 // ERROR-NEXT: Error while scanning dependencies for modB:


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to