Skip to content

Commit 97086d6

Browse files
carlosgalvezpCarlos Gálvez
andauthored
[clang-tidy] Do not crash when an empty directory is used in the comp… (llvm#156873)
…ilation database Currently a hard crash encourages people to report a bug upstream, but this is not really a bug. Instead, print an error and use a reasonable default (the current working directory). Fixes llvm#57264 Co-authored-by: Carlos Gálvez <[email protected]>
1 parent 1acd429 commit 97086d6

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ Improvements to clang-tidy
126126
- Improved :program:`clang-tidy` option `-quiet` by suppressing diagnostic
127127
count messages.
128128

129+
- Improved :program:`clang-tidy` by not crashing when an empty `directory`
130+
field is used in a compilation database; the current working directory
131+
will be used instead, and an error message will be printed.
132+
129133
New checks
130134
^^^^^^^^^^
131135

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[{
2+
"directory":"/invalid/",
3+
"file":"/tmp/",
4+
"arguments": []
5+
}]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// UNSUPPORTED: system-windows
22

3-
// RUN: not --crash clang-tidy -p %S/Inputs/empty-database %s 2>&1 | FileCheck %s
3+
// RUN: clang-tidy -p %S/Inputs/empty-database %s 2>&1 | FileCheck %s
44

5-
// CHECK: LLVM ERROR: Cannot chdir into ""!
5+
// CHECK: 'directory' field of compilation database is empty; using the current working directory instead.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// UNSUPPORTED: system-windows
2+
3+
// RUN: not --crash clang-tidy -p %S/Inputs/invalid-database %s 2>&1 | FileCheck %s
4+
5+
// CHECK: LLVM ERROR: Cannot chdir into "/invalid/"!

clang/lib/Tooling/Tooling.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,22 +574,30 @@ int ClangTool::run(ToolAction *Action) {
574574
continue;
575575
}
576576
for (CompileCommand &CompileCommand : CompileCommandsForFile) {
577+
// If the 'directory' field of the compilation database is empty, display
578+
// an error and use the working directory instead.
579+
StringRef Directory = CompileCommand.Directory;
580+
if (Directory.empty()) {
581+
llvm::errs() << "'directory' field of compilation database is empty; "
582+
"using the current working directory instead.\n";
583+
Directory = InitialWorkingDir;
584+
}
585+
577586
// FIXME: chdir is thread hostile; on the other hand, creating the same
578587
// behavior as chdir is complex: chdir resolves the path once, thus
579588
// guaranteeing that all subsequent relative path operations work
580589
// on the same path the original chdir resulted in. This makes a
581590
// difference for example on network filesystems, where symlinks might be
582591
// switched during runtime of the tool. Fixing this depends on having a
583592
// file system abstraction that allows openat() style interactions.
584-
if (OverlayFileSystem->setCurrentWorkingDirectory(
585-
CompileCommand.Directory))
586-
llvm::report_fatal_error("Cannot chdir into \"" +
587-
Twine(CompileCommand.Directory) + "\"!");
593+
if (OverlayFileSystem->setCurrentWorkingDirectory(Directory))
594+
llvm::report_fatal_error("Cannot chdir into \"" + Twine(Directory) +
595+
"\"!");
588596

589597
// Now fill the in-memory VFS with the relative file mappings so it will
590598
// have the correct relative paths. We never remove mappings but that
591599
// should be fine.
592-
if (SeenWorkingDirectories.insert(CompileCommand.Directory).second)
600+
if (SeenWorkingDirectories.insert(Directory).second)
593601
for (const auto &MappedFile : MappedFileContents)
594602
if (!llvm::sys::path::is_absolute(MappedFile.first))
595603
InMemoryFileSystem->addFile(

0 commit comments

Comments
 (0)