[PATCH] D148439: [clang-rename] Exit gracefully when no input provided
Shivam Gupta via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 19 00:33:45 PDT 2023
xgupta added inline comments.
================
Comment at: clang/tools/clang-rename/ClangRename.cpp:233
+ if (!Entry) {
+ errs() << "clang-rename: input file does not exist.\n";
+ return 1;
----------------
kbobyrev wrote:
> It is worth including the filename in the error message, otherwise it won't be possible to understand which one is missing (there can be multiple IIRC, right?).
>
> Also, it's better to put this check to the top of main, where the `OP` if first declared, since this is a sanity check and we want to fail if the inputs are corrupted.
Thanks for the suggestion, I agree, I updated the error message regarding the filename.
But I am not sure running a similar `for` loop again should be the correct way.
So just sharing the diff before committing, WDYT -
```
diff --git a/clang/tools/clang-rename/ClangRename.cpp b/clang/tools/clang-rename/ClangRename.cpp
index 24c9d8521..bc777cc01 100644
--- a/clang/tools/clang-rename/ClangRename.cpp
+++ b/clang/tools/clang-rename/ClangRename.cpp
@@ -106,6 +106,17 @@ int main(int argc, const char **argv) {
}
tooling::CommonOptionsParser &OP = ExpectedParser.get();
+ auto Files = OP.getSourcePathList();
+ tooling::RefactoringTool Tool(OP.getCompilations(), Files);
+ auto &FileMgr = Tool.getFiles();
+ for (const auto &File : Files) {
+ auto Entry = FileMgr.getFile(File);
+ if (!Entry) {
+ errs() << "clang-rename: " << File << " does not exist.\n";
+ return 1;
+ }
+ }
+
if (!Input.empty()) {
// Populate QualifiedNames and NewNames from a YAML file.
ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
@@ -162,8 +173,6 @@ int main(int argc, const char **argv) {
return 1;
}
- auto Files = OP.getSourcePathList();
- tooling::RefactoringTool Tool(OP.getCompilations(), Files);
tooling::USRFindingAction FindingAction(SymbolOffsets, QualifiedNames, Force);
Tool.run(tooling::newFrontendActionFactory(&FindingAction).get());
const std::vector<std::vector<std::string>> &USRList =
@@ -222,7 +231,6 @@ int main(int argc, const char **argv) {
DiagnosticsEngine Diagnostics(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
&DiagnosticPrinter, false);
- auto &FileMgr = Tool.getFiles();
SourceManager Sources(Diagnostics, FileMgr);
Rewriter Rewrite(Sources, DefaultLangOptions);
```
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D148439/new/
https://reviews.llvm.org/D148439
More information about the cfe-commits
mailing list