[clang] [clang] NFCI: Use `FileEntryRef` for `FileID` creation (PR #67838)

Ben Langmuir via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 29 11:01:48 PDT 2023


================
@@ -152,7 +152,7 @@ int main(int argc, const char **argv) {
       for (auto I = ChangedFiles.begin(), E = ChangedFiles.end(); I != E; ++I) {
         OS << "  {\n";
         OS << "    \"FilePath\": \"" << *I << "\",\n";
-        const auto Entry = FileMgr.getFile(*I);
+        auto Entry = FileMgr.getOptionalFileRef(*I);
----------------
benlangmuir wrote:

In cases like this where we immediately dereference the optional, I suggest either
```c++
auto Entry = llvm::cantFail(FileMgr.getFileRef(*I));
```
or 
```c++
auto Entry = FileMgr.getFileRef(*I);
if (!Entry)
  llvm::report_fatal_error(Entry.takeError);
```

depending on whether you want to assert the value (cantFail) or want to also report the error in release builds (report_fatal_error).

The benefit here is that you get the error message if it goes wrong, instead of just a segfault.

https://github.com/llvm/llvm-project/pull/67838


More information about the cfe-commits mailing list