From c70d38e997b6857ec960bd08532539882f5ab49d Mon Sep 17 00:00:00 2001 From: Alexander Kornienko Date: Mon, 8 Apr 2019 14:18:26 +0000 Subject: [PATCH] Remove a useless assertion in clang-check. The assertion prevents it from applying fixes when used along with compilation databases with relative paths. Added a test that demonstrates the assertion failure. An example of the assertion: input.cpp:11:14: error: expected ';' after top level declarator typedef int T ^ ; input.cpp:11:14: note: FIX-IT applied suggested code changes clang-check: clang/tools/clang-check/ClangCheck.cpp:94: virtual std::string (anonymous namespace)::FixItOptions::RewriteFilename(const std::string &, int &): Assertion `llvm::sys::path::is_absolute(filename) && "clang-fixit expects absolute paths only."' failed. #0 llvm::sys::PrintStackTrace(llvm::raw_ostream&) llvm/lib/Support/Unix/Signals.inc:494:13 #1 llvm::sys::RunSignalHandlers() llvm/lib/Support/Signals.cpp:69:18 #2 SignalHandler(int) llvm/lib/Support/Unix/Signals.inc:357:1 #3 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x110c0) #4 raise (/lib/x86_64-linux-gnu/libc.so.6+0x32fcf) #5 abort (/lib/x86_64-linux-gnu/libc.so.6+0x343fa) #6 (/lib/x86_64-linux-gnu/libc.so.6+0x2be37) #7 (/lib/x86_64-linux-gnu/libc.so.6+0x2bee2) #8 void std::__cxx11::basic_string, std::allocator >::_M_construct(char*, char*, std::forward_iterator_tag) #9 void std::__cxx11::basic_string, std::allocator >::_M_construct_aux(char*, char*, std::__false_type) #10 void std::__cxx11::basic_string, std::allocator >::_M_construct(char*, char*) #11 std::__cxx11::basic_string, std::allocator >::basic_string(std::__cxx11::basic_string, std::allocator > const&) #12 (anonymous namespace)::FixItOptions::RewriteFilename(std::__cxx11::basic_string, std::allocator > const&, int&) clang/tools/clang-check/ClangCheck.cpp:101:0 #13 std::__cxx11::basic_string, std::allocator >::_M_data() const #14 std::__cxx11::basic_string, std::allocator >::_M_is_local() const #15 std::__cxx11::basic_string, std::allocator >::_M_dispose() #16 std::__cxx11::basic_string, std::allocator >::~basic_string() #17 clang::FixItRewriter::WriteFixedFiles(std::vector, std::allocator >, std::__cxx11::basic_string, std::allocator > >, std::allocator, std::allocator >, std::__cxx11::basic_string, std::allocator > > > >*) clang/lib/Frontend/Rewrite/FixItRewriter.cpp:98:0 #18 std::__shared_ptr::get() const #19 std::__shared_ptr_access::_M_get() const #20 std::__shared_ptr_access::operator->() const #21 clang::CompilerInstance::getFrontendOpts() clang/include/clang/Frontend/CompilerInstance.h:290:0 #22 clang::FrontendAction::EndSourceFile() clang/lib/Frontend/FrontendAction.cpp:966:0 #23 __gnu_cxx::__normal_iterator > >::operator++() #24 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) clang/lib/Frontend/CompilerInstance.cpp:943:0 #25 clang::tooling::FrontendActionFactory::runInvocation(std::shared_ptr, clang::FileManager*, std::shared_ptr, clang::DiagnosticConsumer*) clang/lib/Tooling/Tooling.cpp:369:33 #26 clang::tooling::ToolInvocation::runInvocation(char const*, clang::driver::Compilation*, std::shared_ptr, std::shared_ptr) clang/lib/Tooling/Tooling.cpp:344:18 #27 clang::tooling::ToolInvocation::run() clang/lib/Tooling/Tooling.cpp:329:10 #28 clang::tooling::ClangTool::run(clang::tooling::ToolAction*) clang/lib/Tooling/Tooling.cpp:518:11 #29 main clang/tools/clang-check/ClangCheck.cpp:187:15 llvm-svn: 357915 --- clang/test/Tooling/clang-check-fixit.cpp | 25 ++++++++++++++++++++++++ clang/tools/clang-check/ClangCheck.cpp | 3 --- 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 clang/test/Tooling/clang-check-fixit.cpp diff --git a/clang/test/Tooling/clang-check-fixit.cpp b/clang/test/Tooling/clang-check-fixit.cpp new file mode 100644 index 0000000000000..6b03334f72cd2 --- /dev/null +++ b/clang/test/Tooling/clang-check-fixit.cpp @@ -0,0 +1,25 @@ +// RUN: rm -rf %t +// RUN: mkdir %t +// +// RUN: sed -s 's,^//.*,//,' %s > %t/absolute-fixed.cpp +// RUN: sed -s 's,^//.*,//,' %s > %t/absolute-json.cpp +// RUN: sed -s 's,^//.*,//,' %s > %t/relative-fixed.cpp +// RUN: sed -s 's,^//.*,//,' %s > %t/relative-json.cpp +// +// RUN: clang-check %t/absolute-fixed.cpp -fixit -- 2>&1 | FileCheck %s +// +// RUN: echo '[{ "directory": "%t", \ +// RUN: "command": "/path/to/clang -c %t/absolute-json.cpp", \ +// RUN: "file": "%t/absolute-json.cpp" }]' > %t/compile_commands.json +// RUN: clang-check %t/absolute-json.cpp -fixit 2>&1 | FileCheck %s +// +// RUN: cd %t +// RUN: clang-check relative-fixed.cpp -fixit -- 2>&1 | FileCheck %s +// +// RUN: echo '[{ "directory": "%t", \ +// RUN: "command": "/path/to/clang -c relative-json.cpp", \ +// RUN: "file": "relative-json.cpp" }]' > %t/compile_commands.json +// RUN: clang-check relative-json.cpp -fixit 2>&1 | FileCheck %s +typedef int T +// CHECK: .cpp:[[@LINE-1]]:14: error: expected ';' after top level declarator +// CHECK: .cpp:[[@LINE-2]]:14: note: FIX-IT applied suggested code changes diff --git a/clang/tools/clang-check/ClangCheck.cpp b/clang/tools/clang-check/ClangCheck.cpp index 96a593a29b46f..ce400b5c20005 100644 --- a/clang/tools/clang-check/ClangCheck.cpp +++ b/clang/tools/clang-check/ClangCheck.cpp @@ -90,9 +90,6 @@ class FixItOptions : public clang::FixItOptions { } std::string RewriteFilename(const std::string& filename, int &fd) override { - assert(llvm::sys::path::is_absolute(filename) && - "clang-fixit expects absolute paths only."); - // We don't need to do permission checking here since clang will diagnose // any I/O errors itself.