jansvoboda11 updated this revision to Diff 408876.
jansvoboda11 added a comment.

Move `SearchDirIdx::get()` to `HeaderSearch`


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D119825

Files:
  clang/include/clang/Lex/HeaderSearch.h
  clang/lib/Lex/HeaderSearch.cpp

Index: clang/lib/Lex/HeaderSearch.cpp
===================================================================
--- clang/lib/Lex/HeaderSearch.cpp
+++ clang/lib/Lex/HeaderSearch.cpp
@@ -110,18 +110,22 @@
   assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() &&
          "Directory indices are unordered");
   SearchDirs = std::move(dirs);
-  SearchDirsUsage.assign(SearchDirs.size(), false);
   AngledDirIdx = angledDirIdx;
   SystemDirIdx = systemDirIdx;
   NoCurDirSearch = noCurDirSearch;
-  SearchDirToHSEntry = std::move(searchDirToHSEntry);
+
+  UsedSearchDirs.clear();
+
+  SearchDirToHSEntry.clear();
+  for (const auto &Entry : searchDirToHSEntry)
+    SearchDirToHSEntry.insert({SearchDirIdx(Entry.first), Entry.second});
+
   //LookupFileCache.clear();
 }
 
 void HeaderSearch::AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
   unsigned idx = isAngled ? SystemDirIdx : AngledDirIdx;
   SearchDirs.insert(SearchDirs.begin() + idx, dir);
-  SearchDirsUsage.insert(SearchDirsUsage.begin() + idx, false);
   if (!isAngled)
     AngledDirIdx++;
   SystemDirIdx++;
@@ -129,14 +133,11 @@
 
 std::vector<bool> HeaderSearch::computeUserEntryUsage() const {
   std::vector<bool> UserEntryUsage(HSOpts->UserEntries.size());
-  for (unsigned I = 0, E = SearchDirsUsage.size(); I < E; ++I) {
-    // Check whether this DirectoryLookup has been successfully used.
-    if (SearchDirsUsage[I]) {
-      auto UserEntryIdxIt = SearchDirToHSEntry.find(I);
-      // Check whether this DirectoryLookup maps to a HeaderSearch::UserEntry.
-      if (UserEntryIdxIt != SearchDirToHSEntry.end())
-        UserEntryUsage[UserEntryIdxIt->second] = true;
-    }
+  for (SearchDirIdx Idx : UsedSearchDirs) {
+    auto UserEntryIdxIt = SearchDirToHSEntry.find(Idx);
+    // Check whether this DirectoryLookup maps to a HeaderSearch::UserEntry.
+    if (UserEntryIdxIt != SearchDirToHSEntry.end())
+      UserEntryUsage[UserEntryIdxIt->second] = true;
   }
   return UserEntryUsage;
 }
@@ -704,8 +705,8 @@
   noteLookupUsage(HitIt.Idx, Loc);
 }
 
-void HeaderSearch::noteLookupUsage(unsigned HitIdx, SourceLocation Loc) {
-  SearchDirsUsage[HitIdx] = true;
+void HeaderSearch::noteLookupUsage(SearchDirIdx HitIdx, SourceLocation Loc) {
+  UsedSearchDirs.insert(HitIdx);
 
   auto UserEntryIdxIt = SearchDirToHSEntry.find(HitIdx);
   if (UserEntryIdxIt != SearchDirToHSEntry.end())
@@ -713,6 +714,14 @@
         << HSOpts->UserEntries[UserEntryIdxIt->second].Path;
 }
 
+const DirectoryLookup &HeaderSearch::getSearchDirAt(SearchDirIdx Idx) const {
+  return SearchDirs[Idx.Idx];
+}
+
+DirectoryLookup &HeaderSearch::getSearchDirAt(SearchDirIdx Idx) {
+  return SearchDirs[Idx.Idx];
+}
+
 void HeaderSearch::setTarget(const TargetInfo &Target) {
   ModMap.setTarget(Target);
 }
@@ -1446,8 +1455,8 @@
     + FrameworkMap.getAllocator().getTotalMemory();
 }
 
-unsigned HeaderSearch::searchDirIdx(const DirectoryLookup &DL) const {
-  return &DL - &*SearchDirs.begin();
+SearchDirIdx HeaderSearch::searchDirIdx(const DirectoryLookup &DL) const {
+  return SearchDirIdx(&DL - &*SearchDirs.begin());
 }
 
 StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
Index: clang/include/clang/Lex/HeaderSearch.h
===================================================================
--- clang/include/clang/Lex/HeaderSearch.h
+++ clang/include/clang/Lex/HeaderSearch.h
@@ -163,6 +163,43 @@
   bool IsUserSpecifiedSystemFramework;
 };
 
+/// Index of a search directory.
+class SearchDirIdx {
+  /// The underlying index.
+  size_t Idx;
+
+  friend HeaderSearch;
+
+public:
+  explicit SearchDirIdx(size_t Idx) : Idx(Idx) {}
+
+  bool operator==(const SearchDirIdx &Other) const { return Idx == Other.Idx; }
+
+  llvm::hash_code hash_value() const { return Idx; }
+
+  void increment() { ++Idx; }
+};
+} // namespace clang
+
+namespace llvm {
+template <> struct DenseMapInfo<clang::SearchDirIdx> {
+  static inline clang::SearchDirIdx getEmptyKey() {
+    return clang::SearchDirIdx(-1);
+  }
+  static inline clang::SearchDirIdx getTombstoneKey() {
+    return clang::SearchDirIdx(-2);
+  }
+  static unsigned getHashValue(const clang::SearchDirIdx &Val) {
+    return Val.hash_value();
+  }
+  static bool isEqual(const clang::SearchDirIdx &LHS,
+                      const clang::SearchDirIdx &RHS) {
+    return LHS == RHS;
+  }
+};
+} // namespace llvm
+
+namespace clang {
 namespace detail {
 template <bool Const, typename T>
 using Qualified = std::conditional_t<Const, const T, T>;
@@ -188,13 +225,13 @@
 
   SearchDirIteratorImpl &operator++() {
     assert(*this && "Invalid iterator.");
-    ++Idx;
+    Idx.increment();
     return *this;
   }
 
   Qualified<IsConst, DirectoryLookup> &operator*() const {
     assert(*this && "Invalid iterator.");
-    return HS->SearchDirs[Idx];
+    return HS->getSearchDirAt(Idx);
   }
 
   /// Creates an invalid iterator.
@@ -208,7 +245,7 @@
   Qualified<IsConst, HeaderSearch> *HS;
 
   /// The index of the current element.
-  size_t Idx;
+  SearchDirIdx Idx;
 
   /// The constructor that creates a valid iterator.
   SearchDirIteratorImpl(Qualified<IsConst, HeaderSearch> &HS, size_t Idx)
@@ -240,7 +277,7 @@
   std::shared_ptr<HeaderSearchOptions> HSOpts;
 
   /// Mapping from SearchDir to HeaderSearchOptions::UserEntries indices.
-  llvm::DenseMap<unsigned, unsigned> SearchDirToHSEntry;
+  llvm::DenseMap<SearchDirIdx, unsigned> SearchDirToHSEntry;
 
   DiagnosticsEngine &Diags;
   FileManager &FileMgr;
@@ -254,7 +291,7 @@
   std::vector<DirectoryLookup> SearchDirs;
   /// Whether the DirectoryLookup at the corresponding index in SearchDirs has
   /// been successfully used to lookup a file.
-  std::vector<bool> SearchDirsUsage;
+  llvm::DenseSet<SearchDirIdx> UsedSearchDirs;
   unsigned AngledDirIdx = 0;
   unsigned SystemDirIdx = 0;
   bool NoCurDirSearch = false;
@@ -363,7 +400,6 @@
   /// Add an additional system search path.
   void AddSystemSearchPath(const DirectoryLookup &dir) {
     SearchDirs.push_back(dir);
-    SearchDirsUsage.push_back(false);
   }
 
   /// Set the list of system header prefixes.
@@ -779,8 +815,13 @@
                           SourceLocation IncludeLoc);
 
   /// Note that a lookup at the given include location was successful using the
-  /// search path at index `HitIdx`.
-  void noteLookupUsage(unsigned HitIdx, SourceLocation IncludeLoc);
+  /// search path at index \c HitIdx.
+  void noteLookupUsage(SearchDirIdx HitIdx, SourceLocation IncludeLoc);
+
+  /// Get search directory stored at the given index.
+  const DirectoryLookup &getSearchDirAt(SearchDirIdx Idx) const;
+  /// Get search directory stored at the given index.
+  DirectoryLookup &getSearchDirAt(SearchDirIdx Idx);
 
 public:
   /// Retrieve the module map.
@@ -832,7 +873,7 @@
   }
 
   /// Get the index of the given search directory.
-  unsigned searchDirIdx(const DirectoryLookup &DL) const;
+  SearchDirIdx searchDirIdx(const DirectoryLookup &DL) const;
 
   /// Retrieve a uniqued framework name.
   StringRef getUniqueFrameworkName(StringRef Framework);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to