[PATCH] D119722: [clang][lex] Use `SearchDirIterator` types in for loops

Jan Svoboda via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 14 23:28:27 PST 2022


jansvoboda11 added inline comments.


================
Comment at: clang/lib/Lex/HeaderSearch.cpp:1450
 Optional<unsigned> HeaderSearch::searchDirIdx(const DirectoryLookup &DL) const {
-  for (unsigned I = 0; I < SearchDirs.size(); ++I)
-    if (&SearchDirs[I] == &DL)
-      return I;
-  return None;
+  return &DL - &*SearchDirs.begin();
 }
----------------
ahoppen wrote:
> Could we change this function to return an `unsigned` instead of `Optional<unsigned>` now?
> 
> Also, is `&DL - &*SearchDirs.begin()` safe and doesn’t trigger the issues we saw previously if start and end are allocated in different memory regions, because I don’t know.
Yes, updating the return type makes sense now, thanks!

Yes, this should be safe as of this patch, since we're still storing `DirectoryLookup` objects in `std::vector<DirectoryLookup>`. I'll be updating this code when we change the storage (separate `std::vector<DirectoryLookup>` for quoted, angled and system search paths).


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D119722



More information about the cfe-commits mailing list