https://github.com/qiongsiwu updated https://github.com/llvm/llvm-project/pull/183396
>From e6d7ff78c41e347677ec472c03a2178ef5d869cf Mon Sep 17 00:00:00 2001 From: Qiongsi Wu <[email protected]> Date: Wed, 25 Feb 2026 13:34:42 -0800 Subject: [PATCH 1/2] Rename the temporary file the compiler creates for by-name dependency scanning. --- .../clang/DependencyScanning/DependencyScanningWorker.h | 2 +- clang/lib/DependencyScanning/DependencyScanningWorker.cpp | 8 ++++---- clang/lib/Tooling/DependencyScanningTool.cpp | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/clang/include/clang/DependencyScanning/DependencyScanningWorker.h b/clang/include/clang/DependencyScanning/DependencyScanningWorker.h index e1e0c14c7b52c..4ef0ffeca76ea 100644 --- a/clang/include/clang/DependencyScanning/DependencyScanningWorker.h +++ b/clang/include/clang/DependencyScanning/DependencyScanningWorker.h @@ -193,7 +193,7 @@ std::pair<IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>, std::vector<std::string>> initVFSForByNameScanning(IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS, ArrayRef<std::string> CommandLine, - StringRef WorkingDirectory, StringRef ModuleName); + StringRef WorkingDirectory); } // end namespace dependencies } // end namespace clang diff --git a/clang/lib/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/DependencyScanning/DependencyScanningWorker.cpp index 60e5103fde6ef..f54898b56b768 100644 --- a/clang/lib/DependencyScanning/DependencyScanningWorker.cpp +++ b/clang/lib/DependencyScanning/DependencyScanningWorker.cpp @@ -116,7 +116,7 @@ bool DependencyScanningWorker::computeDependencies( bool DependencyScanningWorker::initializeCompilerInstanceWithContext( StringRef CWD, ArrayRef<std::string> CommandLine, DiagnosticConsumer &DC) { auto [OverlayFS, ModifiedCommandLine] = - initVFSForByNameScanning(DepFS, CommandLine, CWD, "ScanningByName"); + initVFSForByNameScanning(DepFS, CommandLine, CWD); auto DiagEngineWithCmdAndOpts = std::make_unique<DiagnosticsEngineWithDiagOpts>(ModifiedCommandLine, OverlayFS, DC); @@ -174,8 +174,7 @@ std::pair<IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>, std::vector<std::string>> dependencies::initVFSForByNameScanning( IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS, - ArrayRef<std::string> CommandLine, StringRef WorkingDirectory, - StringRef ModuleName) { + ArrayRef<std::string> CommandLine, StringRef WorkingDirectory) { // Reset what might have been modified in the previous worker invocation. BaseFS->setCurrentWorkingDirectory(WorkingDirectory); @@ -188,7 +187,8 @@ dependencies::initVFSForByNameScanning( InMemoryFS->setCurrentWorkingDirectory(WorkingDirectory); SmallString<128> FakeInputPath; // TODO: We should retry the creation if the path already exists. - llvm::sys::fs::createUniquePath(ModuleName + "-%%%%%%%%.input", FakeInputPath, + llvm::sys::fs::createUniquePath("module-includes-%%%%%%%%.input", + FakeInputPath, /*MakeAbsolute=*/false); InMemoryFS->addFile(FakeInputPath, 0, llvm::MemoryBuffer::getMemBuffer("")); IntrusiveRefCntPtr<llvm::vfs::FileSystem> InMemoryOverlay = InMemoryFS; diff --git a/clang/lib/Tooling/DependencyScanningTool.cpp b/clang/lib/Tooling/DependencyScanningTool.cpp index 30d2dc1b7c809..70f57c0573240 100644 --- a/clang/lib/Tooling/DependencyScanningTool.cpp +++ b/clang/lib/Tooling/DependencyScanningTool.cpp @@ -329,8 +329,8 @@ bool DependencyScanningTool::initializeWorkerCIWithContextFromCommandline( // The input command line is either a driver-style command line, or // ill-formed. In this case, we will first call the Driver to build a -cc1 // command line for this compilation or diagnose any ill-formed input. - auto [OverlayFS, ModifiedCommandLine] = initVFSForByNameScanning( - &Worker.getVFS(), CommandLine, CWD, "ScanningByName"); + auto [OverlayFS, ModifiedCommandLine] = + initVFSForByNameScanning(&Worker.getVFS(), CommandLine, CWD); auto DiagEngineWithCmdAndOpts = std::make_unique<DiagnosticsEngineWithDiagOpts>(ModifiedCommandLine, OverlayFS, DC); >From d6914fbb9c6b8316374b0ba8c15d927fd2629beb Mon Sep 17 00:00:00 2001 From: Qiongsi Wu <[email protected]> Date: Mon, 2 Mar 2026 16:37:30 -0800 Subject: [PATCH 2/2] Fixing potential source location overflow. And reduce the length of the random string since we do not need it that wide. --- .../clang/DependencyScanning/DependencyScannerImpl.h | 9 +++++++++ .../lib/DependencyScanning/DependencyScannerImpl.cpp | 8 ++++++++ .../DependencyScanning/DependencyScanningWorker.cpp | 12 +++++++++--- .../modules-full-by-mult-mod-names-diagnostics.c | 6 +++--- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/DependencyScanning/DependencyScannerImpl.h b/clang/include/clang/DependencyScanning/DependencyScannerImpl.h index d50371512667d..19d1ee5285265 100644 --- a/clang/include/clang/DependencyScanning/DependencyScannerImpl.h +++ b/clang/include/clang/DependencyScanning/DependencyScannerImpl.h @@ -157,6 +157,15 @@ class CompilerInstanceWithContext { bool computeDependencies(StringRef ModuleName, DependencyConsumer &Consumer, DependencyActionController &Controller); bool finalize(); + + // FIXME: see if we can use a smaller fake buffer, or set the buffer's size + // dynamically. + // At the time of this commit, the estimated number of total unique importable + // names is around 3000. We usually import them in parallel, so it is unlikely + // that all names are all scanned by the same dependency scanning worker. + // Therefore the 64k (20x bigger than our estimate) size is sufficient to + // hold the unique source locations to report diagnostics per worker. + static const int32_t FakeInputBufferSize = 1 << 16; }; } // namespace dependencies } // namespace clang diff --git a/clang/lib/DependencyScanning/DependencyScannerImpl.cpp b/clang/lib/DependencyScanning/DependencyScannerImpl.cpp index 5e1d12e95c162..60a48e0edd6fc 100644 --- a/clang/lib/DependencyScanning/DependencyScannerImpl.cpp +++ b/clang/lib/DependencyScanning/DependencyScannerImpl.cpp @@ -943,6 +943,14 @@ bool CompilerInstanceWithContext::computeDependencies( // FIXME: Scan modules asynchronously here as well. SrcLocOffset++; + if (SrcLocOffset >= FakeInputBufferSize) { + // This should never happen. We could have replaced this by an assert, + // but reporting a fatal error is more explicit, and we know + // immediately we are overflowing. + llvm::report_fatal_error("exceeded maximum by-name scans per worker; " + "increase module-import-by-name.input's size"); + } + SmallVector<IdentifierLoc, 2> Path; IdentifierInfo *ModuleID = PP.getIdentifierInfo(ModuleName); Path.emplace_back(IDLocation, ModuleID); diff --git a/clang/lib/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/DependencyScanning/DependencyScanningWorker.cpp index f54898b56b768..f773c2231af45 100644 --- a/clang/lib/DependencyScanning/DependencyScanningWorker.cpp +++ b/clang/lib/DependencyScanning/DependencyScanningWorker.cpp @@ -170,6 +170,12 @@ dependencies::initVFSForTUBufferScanning( return std::make_pair(OverlayFS, ModifiedCommandLine); } +// The fake input buffer is read-only, and it is used to produce +// unique source locations for the diagnostics. Therefore sharing +// this global buffer across threads is ok. +static const std::string + FakeInput(" ", CompilerInstanceWithContext::FakeInputBufferSize); + std::pair<IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>, std::vector<std::string>> dependencies::initVFSForByNameScanning( @@ -187,10 +193,10 @@ dependencies::initVFSForByNameScanning( InMemoryFS->setCurrentWorkingDirectory(WorkingDirectory); SmallString<128> FakeInputPath; // TODO: We should retry the creation if the path already exists. - llvm::sys::fs::createUniquePath("module-includes-%%%%%%%%.input", - FakeInputPath, + llvm::sys::fs::createUniquePath("module-import-%%%%.input", FakeInputPath, /*MakeAbsolute=*/false); - InMemoryFS->addFile(FakeInputPath, 0, llvm::MemoryBuffer::getMemBuffer("")); + InMemoryFS->addFile(FakeInputPath, 0, + llvm::MemoryBuffer::getMemBuffer(FakeInput)); IntrusiveRefCntPtr<llvm::vfs::FileSystem> InMemoryOverlay = InMemoryFS; OverlayFS->pushOverlay(InMemoryOverlay); 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 9889982354e90..fa93f5fc92fd7 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 @@ -27,11 +27,11 @@ module root2 { header "root2.h" } // RUN: cat %t/result.json | sed 's:\\\\\?:/:g' | FileCheck -DPREFIX=%/t %s // ERROR: Error while scanning dependencies for modA: -// ERROR-NEXT: {{.*}}: fatal error: module 'modA' not found +// ERROR-NEXT: module-import-[[RAND:[a-zA-Z0-9]{4}]].input:1:1: fatal error: module 'modA' not found // ERROR-NEXT: Error while scanning dependencies for modB: -// ERROR-NEXT: {{.*}}: fatal error: module 'modB' not found +// ERROR-NEXT: module-import-[[RAND]].input:1:3: fatal error: module 'modB' not found // ERROR-NEXT: Error while scanning dependencies for modC: -// ERROR-NEXT: {{.*}}: fatal error: module 'modC' not found +// ERROR-NEXT: module-import-[[RAND]].input:1:4: fatal error: module 'modC' not found // CHECK: { // CHECK-NEXT: "modules": [ // CHECK-NEXT: { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
