[clang-scan-deps] Fixes an assertion in clang-scan-deps#193619
Open
romanova-ekaterina wants to merge 1 commit intollvm:mainfrom
Open
[clang-scan-deps] Fixes an assertion in clang-scan-deps#193619romanova-ekaterina wants to merge 1 commit intollvm:mainfrom
romanova-ekaterina wants to merge 1 commit intollvm:mainfrom
Conversation
Member
|
@llvm/pr-subscribers-clang Author: Katya Romanova (romanova-ekaterina) ChangesPlease see ticket #191921 for detailed description of the issue and a reproducer. clang-scan-deps crashes with an assertion failure if a compile_commands.json Full diff: https://github.com/llvm/llvm-project/pull/193619.diff 2 Files Affected:
diff --git a/clang/test/ClangScanDeps/p1689-mf-nested-dir.c b/clang/test/ClangScanDeps/p1689-mf-nested-dir.c
new file mode 100644
index 0000000000000..dca72727f4d6b
--- /dev/null
+++ b/clang/test/ClangScanDeps/p1689-mf-nested-dir.c
@@ -0,0 +1,34 @@
+// UNSUPPORTED: target={{.*}}-aix{{.*}}
+//
+// When using -format=p1689, clang-scan-deps writes make-style dependency output
+// to the path from -MF. The directory where the output file is written may not
+// exist yet. Ensure the tool creates missing directories instead of failing or
+// aborting.
+//
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+// RUN: sed "s|DIR|%/t|g" %t/cdb.json.in > %t/compile_commands.json
+// RUN: clang-scan-deps -compilation-database %t/compile_commands.json -format=p1689 \
+// RUN: -o %t/scan.json
+// RUN: cat %t/obj/nested/hello.o.d | sed 's:\\\\\?:/:g' | FileCheck %s --check-prefix=CHECK-DEP
+// RUN: cat %t/scan.json | FileCheck %s -DPREFIX=%/t --check-prefix=CHECK-JSON
+
+//--- hello.c
+int main(void) { return 0; }
+
+//--- cdb.json.in
+[
+ {
+ "directory": "DIR",
+ "command": "clang -c DIR/hello.c -o DIR/obj/nested/hello.o -MMD -MF DIR/obj/nested/hello.o.d",
+ "file": "DIR/hello.c",
+ "output": "DIR/obj/nested/hello.o"
+ }
+]
+
+// CHECK-DEP-DAG: hello.o
+// CHECK-DEP-DAG: hello.c
+
+// CHECK-JSON-DAG: "primary-output": "[[PREFIX]]/obj/nested/hello.o"
+// CHECK-JSON-DAG: "version": 1
diff --git a/clang/tools/clang-scan-deps/ClangScanDeps.cpp b/clang/tools/clang-scan-deps/ClangScanDeps.cpp
index fd3d70f9498ee..1172895505370 100644
--- a/clang/tools/clang-scan-deps/ClangScanDeps.cpp
+++ b/clang/tools/clang-scan-deps/ClangScanDeps.cpp
@@ -19,6 +19,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/JSON.h"
@@ -1031,6 +1032,7 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
if (!MakeformatOutputPath.empty() && !MakeformatOutput.empty() &&
!HadErrors) {
+ llvm::SmallString<256> FullDepPath;
static std::mutex Lock;
// With compilation database, we may open different files
// concurrently or we may write the same file concurrently. So we
@@ -1039,16 +1041,37 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
static llvm::StringMap<llvm::raw_fd_ostream> OSs;
std::unique_lock<std::mutex> LockGuard(Lock);
- auto OSIter = OSs.find(MakeformatOutputPath);
+ if (llvm::sys::path::is_absolute(MakeformatOutputPath))
+ FullDepPath = MakeformatOutputPath;
+ else
+ llvm::sys::path::append(FullDepPath, CWD, MakeformatOutputPath);
+
+ if (llvm::StringRef Parent =
+ llvm::sys::path::parent_path(FullDepPath);
+ !Parent.empty()) {
+ if (std::error_code DirEC =
+ llvm::sys::fs::create_directories(Parent)) {
+ llvm::errs() << "Failed to create directory \"" << Parent
+ << "\" for P1689 make format output: "
+ << DirEC.message() << "\n";
+ HadErrors = true;
+ continue;
+ }
+ }
+
+ auto OSIter = OSs.find(FullDepPath);
if (OSIter == OSs.end()) {
std::error_code EC;
- OSIter = OSs.try_emplace(MakeformatOutputPath, MakeformatOutputPath,
- EC, llvm::sys::fs::OF_Text)
- .first;
- if (EC)
+ auto Emplaced = OSs.try_emplace(FullDepPath.str(), FullDepPath, EC,
+ llvm::sys::fs::OF_Text);
+ OSIter = Emplaced.first;
+ if (EC) {
+ OSs.erase(OSIter);
llvm::errs() << "Failed to open P1689 make format output file \""
- << MakeformatOutputPath << "\" for " << EC.message()
- << "\n";
+ << FullDepPath << "\" for " << EC.message() << "\n";
+ HadErrors = true;
+ continue;
+ }
}
SharedStream MakeformatOS(OSIter->second);
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please see ticket #191921 for detailed description of the issue and a reproducer.
clang-scan-deps crashes with an assertion failure if a compile_commands.json
entry contains a depfile path (-MF) whose parent directory does not exist.
The fix is made so that clang-scan-deps tool create the directory if it doesn't exist and finish execution without failing.