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

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 17 04:49:21 PDT 2020


njames93 created this revision.
njames93 added reviewers: klimek, gribozavr2, aaron.ballman, alexfh.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D82004

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


Index: clang-tools-extra/clang-tidy/utils/IncludeInserter.h
===================================================================
--- clang-tools-extra/clang-tidy/utils/IncludeInserter.h
+++ clang-tools-extra/clang-tidy/utils/IncludeInserter.h
@@ -71,6 +71,8 @@
   void AddInclude(StringRef FileName, bool IsAngled,
                   SourceLocation HashLocation, SourceLocation EndLocation);
 
+  IncludeSorter &getOrCreate(FileID File);
+
   llvm::DenseMap<FileID, std::unique_ptr<IncludeSorter>> IncludeSorterByFile;
   llvm::DenseMap<FileID, std::set<std::string>> InsertedHeaders;
   const SourceManager &SourceMgr;
Index: clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
===================================================================
--- clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
+++ clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "IncludeInserter.h"
+#include "IncludeSorter.h"
 #include "clang/Lex/Token.h"
 
 namespace clang {
@@ -45,6 +46,19 @@
   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 +67,14 @@
   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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82004.271334.patch
Type: text/x-patch
Size: 3356 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200617/7d81728a/attachment-0001.bin>


More information about the cfe-commits mailing list