From ea351cc2654f0f4d8d054c494f5a1a88134998e4 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Mon, 7 Oct 2024 03:49:07 -0700 Subject: [PATCH] rewriter: immediately make directory CLI args absolute This fixes #408, as `libclangTooling` handles relative paths so poorly. This moves the logic into the `DirectoryParser`, which centralizes it a bit more, and ensures there's no non-absolute path created before we start using it. --- tools/rewriter/SourceRewriter.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tools/rewriter/SourceRewriter.cpp b/tools/rewriter/SourceRewriter.cpp index 24e8ab540..4820d1004 100644 --- a/tools/rewriter/SourceRewriter.cpp +++ b/tools/rewriter/SourceRewriter.cpp @@ -66,13 +66,19 @@ struct DirectoryParser : public llvm::cl::parser { bool parse(llvm::cl::Option &O, llvm::StringRef ArgName, const llvm::StringRef &ArgValue, std::string &Value) { llvm::cl::parser::parse(O, ArgName, ArgValue, Value); - auto dir = Value; - auto exists = llvm::sys::fs::is_directory(dir); + llvm::SmallString dir { llvm::StringRef(Value) }; + bool exists = llvm::sys::fs::is_directory(dir); if (!exists) { llvm::errs() << "error: directory does not exist: " << dir << "\n"; + return true; // true on error } - // true on error - return !exists; + auto ec = llvm::sys::fs::make_absolute(dir); + if (ec) { + llvm::errs() << ec.message() << '\n'; + return true; + } + Value = std::string(dir); + return false; } };