[clang] 62eec15 - [clang-rename] Exit gracefully when no input provided

Shivam Gupta via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 26 07:54:51 PDT 2023


Author: Shivam Gupta
Date: 2023-04-26T20:23:14+05:30
New Revision: 62eec1584d2cd7634c31d4b82215fa8e260cf3b8

URL: https://github.com/llvm/llvm-project/commit/62eec1584d2cd7634c31d4b82215fa8e260cf3b8
DIFF: https://github.com/llvm/llvm-project/commit/62eec1584d2cd7634c31d4b82215fa8e260cf3b8.diff

LOG: [clang-rename] Exit gracefully when no input provided

clang-rename on a non existing file segfaults

Command to run -
$ clang-rename -offset=0 -new-name=plop asdasd

Error while processing llvm-project/asdasd.
clang-rename: llvm-project/llvm/include/llvm/Support/ErrorOr.h:237:
llvm::ErrorOr<T>::storage_type* llvm::ErrorOr<T>::getStorage()
[with T = const clang::FileEntry*; llvm::ErrorOr<T>::storage_type = const clang::FileEntry*]:
Assertion `!HasError && "Cannot get value when an error exists!"' failed.

[1]    827497 IOT instruction  clang-rename -offset=0 -new-name=plop asdasd

This fixes https://github.com/llvm/llvm-project/issues/36471.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D148439

Added: 
    clang/test/clang-rename/NonExistFile.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/tools/clang-rename/ClangRename.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 55ec1cdef52fa..7dfca82c00876 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -235,6 +235,8 @@ Improvements to Clang's diagnostics
 Bug Fixes in This Version
 -------------------------
 
+- Fix segfault while running clang-rename on a non existing file.
+  (`#36471 <https://github.com/llvm/llvm-project/issues/36471>`_)
 - Fix crash when diagnosing incorrect usage of ``_Nullable`` involving alias
   templates.
   (`#60344 <https://github.com/llvm/llvm-project/issues/60344>`_)

diff  --git a/clang/test/clang-rename/NonExistFile.cpp b/clang/test/clang-rename/NonExistFile.cpp
new file mode 100644
index 0000000000000..f45839be80473
--- /dev/null
+++ b/clang/test/clang-rename/NonExistFile.cpp
@@ -0,0 +1,2 @@
+// RUN: not clang-rename -offset=0 -new-name=bar non-existing-file 2>&1 | FileCheck %s
+// CHECK: clang-rename: non-existing-file does not exist.

diff  --git a/clang/tools/clang-rename/ClangRename.cpp b/clang/tools/clang-rename/ClangRename.cpp
index e7ceac7dbf303..24c9d8521dc27 100644
--- a/clang/tools/clang-rename/ClangRename.cpp
+++ b/clang/tools/clang-rename/ClangRename.cpp
@@ -229,6 +229,10 @@ int main(int argc, const char **argv) {
     Tool.applyAllReplacements(Rewrite);
     for (const auto &File : Files) {
       auto Entry = FileMgr.getFile(File);
+      if (!Entry) {
+        errs() << "clang-rename: " << File << " does not exist.\n";
+        return 1;
+      }
       const auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User);
       Rewrite.getEditBuffer(ID).write(outs());
     }


        


More information about the cfe-commits mailing list