[PATCH] D116659: [llvm][clang][vfs] NFC: Extract directory iteration boilerplate into macro

Duncan P. N. Exon Smith via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 10 20:19:06 PST 2022


dexonsmith added a comment.

I think th eAPI is designed to ensure users don't forget to check the error code.

What about a new API/iterator type that wraps the existing one and has a "normal" `operator++`?

- Take an error as an out-parameter on construction
- Make it an `llvm::Error` which guarantees a crash if not checked
- This would wrap the other type and store a reference to the `Error`. If there's an error during iteration, advances to end and sets the error.

Here's what I'm thinking:

  Optional<llvm::Error> DirError;
  for (vfs::error_directory_iterator File = D.getVFS().dir_begin(Cand.Path, DirError),
                                     FileEnd;
       File != FileEnd; ++File) {
    // ...
  }
  if (DirError)
    return errorToErrorCode(std::move(*IterationEC));

At that point, it'd be easy to add a `dir_range` wrapper that creates an iterator range:

  for (auto &File : dir_range(Path, DirError)) {
    // ...
  }


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116659/new/

https://reviews.llvm.org/D116659



More information about the cfe-commits mailing list