This revision was automatically updated to reflect the committed changes. Closed by commit rG08c83ed75752: [clang-tidy][NFC] Remove the double look-up on IncludeInserter (authored by njames93).
Changed prior to commit: https://reviews.llvm.org/D82004?vs=271334&id=271432#toc Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D82004/new/ 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 FileID); + 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 @@ -45,6 +45,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 +66,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
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 FileID); + 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 @@ -45,6 +45,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 +66,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
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits