[clang-tools-extra] 08c83ed - [clang-tidy][NFC] Remove the double look-up on IncludeInserter

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 17 11:48:44 PDT 2020


Author: Nathan James
Date: 2020-06-17T19:48:34+01:00
New Revision: 08c83ed75752169bdf27df8f5ea5978c53f2e258

URL: https://github.com/llvm/llvm-project/commit/08c83ed75752169bdf27df8f5ea5978c53f2e258
DIFF: https://github.com/llvm/llvm-project/commit/08c83ed75752169bdf27df8f5ea5978c53f2e258.diff

LOG: [clang-tidy][NFC] Remove the double look-up on IncludeInserter

Refactor out the double lookup in `IncludeInserter` when trying to get the `IncludeSorter` for a specified `FileID`.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D82004

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
    clang-tools-extra/clang-tidy/utils/IncludeInserter.h

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp b/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
index c267789c9412..f7201a9fdcfe 100644
--- a/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
+++ b/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
@@ -45,6 +45,19 @@ std::unique_ptr<PPCallbacks> IncludeInserter::CreatePPCallbacks() {
   return std::make_unique<IncludeInserterCallback>(this);
 }
 
+IncludeSorter &IncludeInserter::getOrCreate(FileID FileID) {
+  // std::unique_ptr is cheap to construct, so force a construction now to save
+  // the lookup needed if we were to insert into the map.
+  std::unique_ptr<IncludeSorter> &Entry = IncludeSorterByFile[FileID];
+  if (!Entry) {
+    // If it wasn't found, Entry will be default constructed to nullptr.
+    Entry = std::make_unique<IncludeSorter>(
+        &SourceMgr, &LangOpts, FileID,
+        SourceMgr.getFilename(SourceMgr.getLocForStartOfFile(FileID)), Style);
+  }
+  return *Entry;
+}
+
 llvm::Optional<FixItHint>
 IncludeInserter::CreateIncludeInsertion(FileID FileID, StringRef Header,
                                         bool IsAngled) {
@@ -53,31 +66,14 @@ IncludeInserter::CreateIncludeInsertion(FileID FileID, StringRef Header,
   if (!InsertedHeaders[FileID].insert(std::string(Header)).second)
     return llvm::None;
 
-  if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) {
-    // This may happen if there have been no preprocessor directives in this
-    // file.
-    IncludeSorterByFile.insert(std::make_pair(
-        FileID,
-        std::make_unique<IncludeSorter>(
-            &SourceMgr, &LangOpts, FileID,
-            SourceMgr.getFilename(SourceMgr.getLocForStartOfFile(FileID)),
-            Style)));
-  }
-  return IncludeSorterByFile[FileID]->CreateIncludeInsertion(Header, IsAngled);
+  return getOrCreate(FileID).CreateIncludeInsertion(Header, IsAngled);
 }
 
 void IncludeInserter::AddInclude(StringRef FileName, bool IsAngled,
                                  SourceLocation HashLocation,
                                  SourceLocation EndLocation) {
   FileID FileID = SourceMgr.getFileID(HashLocation);
-  if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) {
-    IncludeSorterByFile.insert(std::make_pair(
-        FileID, std::make_unique<IncludeSorter>(
-                    &SourceMgr, &LangOpts, FileID,
-                    SourceMgr.getFilename(HashLocation), Style)));
-  }
-  IncludeSorterByFile[FileID]->AddInclude(FileName, IsAngled, HashLocation,
-                                          EndLocation);
+  getOrCreate(FileID).AddInclude(FileName, IsAngled, HashLocation, EndLocation);
 }
 
 } // namespace utils

diff  --git a/clang-tools-extra/clang-tidy/utils/IncludeInserter.h b/clang-tools-extra/clang-tidy/utils/IncludeInserter.h
index e39e002e97a2..35cbb6ce8568 100644
--- a/clang-tools-extra/clang-tidy/utils/IncludeInserter.h
+++ b/clang-tools-extra/clang-tidy/utils/IncludeInserter.h
@@ -71,6 +71,8 @@ class IncludeInserter {
   void AddInclude(StringRef FileName, bool IsAngled,
                   SourceLocation HashLocation, SourceLocation EndLocation);
 
+  IncludeSorter &getOrCreate(FileID FileID);
+
   llvm::DenseMap<FileID, std::unique_ptr<IncludeSorter>> IncludeSorterByFile;
   llvm::DenseMap<FileID, std::set<std::string>> InsertedHeaders;
   const SourceManager &SourceMgr;


        


More information about the cfe-commits mailing list