[clang-tools-extra] [clang-tidy] Prefer the faster LLVM ADT sets and maps over `std::` ones (PR #174357)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 6 13:57:43 PST 2026
https://github.com/localspook updated https://github.com/llvm/llvm-project/pull/174357
>From 33994687b62d3c8d00f01ccb068a15b79bfc88fe Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Sun, 4 Jan 2026 20:31:34 -0800
Subject: [PATCH 1/3] [clang-tidy] Prefer LLVM ADT sets and maps over `std::`
ones
---
.../altera/IdDependentBackwardBranchCheck.h | 4 ++--
.../clang-tidy/bugprone/LambdaFunctionNameCheck.cpp | 5 ++---
.../clang-tidy/bugprone/LambdaFunctionNameCheck.h | 12 ++++--------
.../clang-tidy/google/AvoidNSObjectNewCheck.cpp | 13 +++++++------
.../clang-tidy/llvm/IncludeOrderCheck.cpp | 2 +-
.../clang-tidy/misc/NewDeleteOverloadsCheck.h | 5 +----
.../clang-tidy/misc/UnusedParametersCheck.cpp | 11 ++++++-----
.../clang-tidy/modernize/UseEqualsDefaultCheck.cpp | 9 +++++----
.../readability/BracesAroundStatementsCheck.h | 2 +-
.../clang-tidy/readability/NonConstParameterCheck.h | 2 +-
clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp | 5 +++--
11 files changed, 33 insertions(+), 37 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.h b/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.h
index 297e7751e4f49..01a4ccdf1e717 100644
--- a/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.h
+++ b/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.h
@@ -39,9 +39,9 @@ class IdDependentBackwardBranchCheck : public ClangTidyCheck {
std::string Message;
};
// Stores the locations where ID-dependent variables are created.
- std::map<const VarDecl *, IdDependencyRecord> IdDepVarsMap;
+ llvm::DenseMap<const VarDecl *, IdDependencyRecord> IdDepVarsMap;
// Stores the locations where ID-dependent fields are created.
- std::map<const FieldDecl *, IdDependencyRecord> IdDepFieldsMap;
+ llvm::DenseMap<const FieldDecl *, IdDependencyRecord> IdDepFieldsMap;
/// Returns an IdDependencyRecord if the Expression contains an ID-dependent
/// variable, returns a nullptr otherwise.
const IdDependencyRecord *hasIdDepVar(const Expr *Expression);
diff --git a/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp
index 892dc02b02298..c34f656ab4252 100644
--- a/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp
@@ -48,7 +48,7 @@ class MacroExpansionsWithFileAndLine : public PPCallbacks {
}
}
if (HasFile && HasLine)
- SuppressMacroExpansions->insert(Range);
+ SuppressMacroExpansions->insert({Range.getBegin(), Range.getEnd()});
}
private:
@@ -97,8 +97,7 @@ void LambdaFunctionNameCheck::check(const MatchFinder::MatchResult &Result) {
auto ER =
Result.SourceManager->getImmediateExpansionRange(E->getLocation());
- if (SuppressMacroExpansions.find(ER.getAsRange()) !=
- SuppressMacroExpansions.end()) {
+ if (SuppressMacroExpansions.contains({ER.getBegin(), ER.getEnd()})) {
// This is a macro expansion for which we should not warn.
return;
}
diff --git a/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.h b/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.h
index d5655037847d3..3460fa8391223 100644
--- a/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.h
@@ -21,14 +21,10 @@ namespace clang::tidy::bugprone {
/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/lambda-function-name.html
class LambdaFunctionNameCheck : public ClangTidyCheck {
public:
- struct SourceRangeLessThan {
- bool operator()(const SourceRange &L, const SourceRange &R) const {
- if (L.getBegin() == R.getBegin())
- return L.getEnd() < R.getEnd();
- return L.getBegin() < R.getBegin();
- }
- };
- using SourceRangeSet = std::set<SourceRange, SourceRangeLessThan>;
+ // FIXME: This pair should be a SourceRange, but SourceRange doesn't have
+ // a DenseMapInfo specialization.
+ using SourceRangeSet =
+ llvm::DenseSet<std::pair<SourceLocation, SourceLocation>>;
LambdaFunctionNameCheck(StringRef Name, ClangTidyContext *Context);
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
diff --git a/clang-tools-extra/clang-tidy/google/AvoidNSObjectNewCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidNSObjectNewCheck.cpp
index 41521067be86b..4f66a084dd956 100644
--- a/clang-tools-extra/clang-tidy/google/AvoidNSObjectNewCheck.cpp
+++ b/clang-tools-extra/clang-tidy/google/AvoidNSObjectNewCheck.cpp
@@ -70,12 +70,13 @@ static FixItHint getCallFixItHint(const ObjCMessageExpr *Expr,
StringRef Receiver =
getReceiverString(Expr->getReceiverRange(), SM, LangOpts);
// Some classes should use standard factory methods instead of alloc/init.
- std::map<StringRef, StringRef> ClassToFactoryMethodMap = {{"NSDate", "date"},
- {"NSNull", "null"}};
- auto FoundClassFactory = ClassToFactoryMethodMap.find(Receiver);
- if (FoundClassFactory != ClassToFactoryMethodMap.end()) {
- StringRef ClassName = FoundClassFactory->first;
- StringRef FactorySelector = FoundClassFactory->second;
+ static constexpr std::pair<StringRef, StringRef> ClassToFactoryMethodMap[] = {
+ {"NSDate", "date"}, {"NSNull", "null"}};
+ const auto *FoundClassFactory =
+ llvm::find_if(ClassToFactoryMethodMap,
+ [&](const auto &Entry) { return Entry.first == Receiver; });
+ if (FoundClassFactory != std::end(ClassToFactoryMethodMap)) {
+ const auto &[ClassName, FactorySelector] = *FoundClassFactory;
const std::string NewCall =
std::string(llvm::formatv("[{0} {1}]", ClassName, FactorySelector));
return FixItHint::CreateReplacement(Expr->getSourceRange(), NewCall);
diff --git a/clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp b/clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp
index 416aca188e01c..592a4313dc197 100644
--- a/clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp
+++ b/clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp
@@ -42,7 +42,7 @@ class IncludeOrderPPCallbacks : public PPCallbacks {
};
using FileIncludes = std::vector<IncludeDirective>;
- std::map<clang::FileID, FileIncludes> IncludeDirectives;
+ llvm::DenseMap<FileID, FileIncludes> IncludeDirectives;
bool LookForMainModule = true;
ClangTidyCheck &Check;
diff --git a/clang-tools-extra/clang-tidy/misc/NewDeleteOverloadsCheck.h b/clang-tools-extra/clang-tidy/misc/NewDeleteOverloadsCheck.h
index 9c7aff082f8cd..7053e5d9fc1bc 100644
--- a/clang-tools-extra/clang-tidy/misc/NewDeleteOverloadsCheck.h
+++ b/clang-tools-extra/clang-tidy/misc/NewDeleteOverloadsCheck.h
@@ -10,14 +10,11 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_NEWDELETEOVERLOADSCHECK_H
#include "../ClangTidyCheck.h"
-#include "llvm/ADT/SmallVector.h"
-#include <map>
namespace clang::tidy::misc {
class NewDeleteOverloadsCheck : public ClangTidyCheck {
- std::map<const clang::CXXRecordDecl *,
- llvm::SmallVector<const clang::FunctionDecl *, 4>>
+ llvm::DenseMap<const CXXRecordDecl *, SmallVector<const FunctionDecl *, 4>>
Overloads;
public:
diff --git a/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
index 47363a24abc14..e8dff1d28a3f2 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
@@ -11,6 +11,7 @@
#include "clang/AST/ASTLambda.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Decl.h"
+#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Basic/SourceManager.h"
@@ -84,12 +85,12 @@ class UnusedParametersCheck::IndexerVisitor
public:
IndexerVisitor(ASTContext &Ctx) { TraverseAST(Ctx); }
- const std::unordered_set<const CallExpr *> &
+ const llvm::SmallPtrSetImpl<const CallExpr *> &
getFnCalls(const FunctionDecl *Fn) {
return Index[Fn->getCanonicalDecl()].Calls;
}
- const std::unordered_set<const DeclRefExpr *> &
+ const llvm::SmallPtrSetImpl<const DeclRefExpr *> &
getOtherRefs(const FunctionDecl *Fn) {
return Index[Fn->getCanonicalDecl()].OtherRefs;
}
@@ -119,11 +120,11 @@ class UnusedParametersCheck::IndexerVisitor
private:
struct IndexEntry {
- std::unordered_set<const CallExpr *> Calls;
- std::unordered_set<const DeclRefExpr *> OtherRefs;
+ llvm::SmallPtrSet<const CallExpr *, 2> Calls;
+ llvm::SmallPtrSet<const DeclRefExpr *, 2> OtherRefs;
};
- std::unordered_map<const FunctionDecl *, IndexEntry> Index;
+ llvm::DenseMap<const FunctionDecl *, IndexEntry> Index;
};
UnusedParametersCheck::~UnusedParametersCheck() = default;
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
index bc450ad4a1f2b..137fdb577fca2 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
@@ -21,9 +21,9 @@ namespace clang::tidy::modernize {
static constexpr char SpecialFunction[] = "SpecialFunction";
/// Finds all the named non-static fields of \p Record.
-static std::set<const FieldDecl *>
+static llvm::SmallPtrSet<const FieldDecl *, 0>
getAllNamedFields(const CXXRecordDecl *Record) {
- std::set<const FieldDecl *> Result;
+ llvm::SmallPtrSet<const FieldDecl *, 0> Result;
for (const auto *Field : Record->fields()) {
// Static data members are not in this range.
if (Field->isUnnamedBitField())
@@ -35,8 +35,9 @@ getAllNamedFields(const CXXRecordDecl *Record) {
/// Returns the names of the direct bases of \p Record, both virtual and
/// non-virtual.
-static std::set<const Type *> getAllDirectBases(const CXXRecordDecl *Record) {
- std::set<const Type *> Result;
+static llvm::SmallPtrSet<const Type *, 0>
+getAllDirectBases(const CXXRecordDecl *Record) {
+ llvm::SmallPtrSet<const Type *, 0> Result;
for (auto Base : Record->bases()) {
// CXXBaseSpecifier.
const auto *BaseType = Base.getTypeSourceInfo()->getType().getTypePtr();
diff --git a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.h b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.h
index 183f1fa8b8a8e..c99e4650a44a8 100644
--- a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.h
@@ -57,7 +57,7 @@ class BracesAroundStatementsCheck : public ClangTidyCheck {
return TK_IgnoreUnlessSpelledInSource;
}
- std::set<const Stmt *> ForceBracesStmts;
+ llvm::SmallPtrSet<const Stmt *, 0> ForceBracesStmts;
const unsigned ShortStatementLines;
};
diff --git a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.h b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.h
index 7dcb16e4253b8..7931b6898bbf5 100644
--- a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.h
@@ -39,7 +39,7 @@ class NonConstParameterCheck : public ClangTidyCheck {
};
/// Track all nonconst integer/float parameters.
- std::map<const ParmVarDecl *, ParmInfo> Parameters;
+ llvm::DenseMap<const ParmVarDecl *, ParmInfo> Parameters;
/// Add function parameter.
void addParm(const ParmVarDecl *Parm);
diff --git a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
index 59cae88708377..88012c3a4b48e 100644
--- a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
+++ b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
@@ -261,9 +261,10 @@ class HeaderGuardPPCallbacks : public PPCallbacks {
std::vector<std::pair<Token, const MacroInfo *>> Macros;
llvm::StringMap<const FileEntry *> Files;
- std::map<const IdentifierInfo *, std::pair<SourceLocation, SourceLocation>>
+ llvm::DenseMap<const IdentifierInfo *,
+ std::pair<SourceLocation, SourceLocation>>
Ifndefs;
- std::map<SourceLocation, SourceLocation> EndIfs;
+ llvm::DenseMap<SourceLocation, SourceLocation> EndIfs;
Preprocessor *PP;
HeaderGuardCheck *Check;
>From e7ed507884bef2644397e6be102c77bf5b427ba0 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Sun, 4 Jan 2026 22:41:12 -0800
Subject: [PATCH 2/3] Remove unused headers
---
clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp | 3 ---
1 file changed, 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
index e8dff1d28a3f2..6cebb35a9dad7 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
@@ -11,13 +11,10 @@
#include "clang/AST/ASTLambda.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Decl.h"
-#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
-#include <unordered_map>
-#include <unordered_set>
using namespace clang::ast_matchers;
>From 816032ac9bf1c59462979e817974e8b2263df073 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Tue, 6 Jan 2026 14:56:47 -0700
Subject: [PATCH 3/3] Get rid of workarounds due to lack of
`DenseMapInfo<SourceRange>`
---
.../clang-tidy/bugprone/LambdaFunctionNameCheck.cpp | 9 ++++-----
.../clang-tidy/bugprone/LambdaFunctionNameCheck.h | 7 +------
.../clang-tidy/google/AvoidNSObjectNewCheck.cpp | 2 +-
.../clang-tidy/misc/NewDeleteOverloadsCheck.h | 3 ++-
4 files changed, 8 insertions(+), 13 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp
index c34f656ab4252..11e2d2e6a72f4 100644
--- a/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp
@@ -30,8 +30,7 @@ static constexpr bool DefaultIgnoreMacros = false;
// either a function body or a lambda body.
class MacroExpansionsWithFileAndLine : public PPCallbacks {
public:
- explicit MacroExpansionsWithFileAndLine(
- LambdaFunctionNameCheck::SourceRangeSet *SME)
+ explicit MacroExpansionsWithFileAndLine(llvm::DenseSet<SourceRange> *SME)
: SuppressMacroExpansions(SME) {}
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
@@ -48,11 +47,11 @@ class MacroExpansionsWithFileAndLine : public PPCallbacks {
}
}
if (HasFile && HasLine)
- SuppressMacroExpansions->insert({Range.getBegin(), Range.getEnd()});
+ SuppressMacroExpansions->insert(Range);
}
private:
- LambdaFunctionNameCheck::SourceRangeSet *SuppressMacroExpansions;
+ llvm::DenseSet<SourceRange> *SuppressMacroExpansions;
};
AST_MATCHER(CXXMethodDecl, isInLambda) { return Node.getParent()->isLambda(); }
@@ -97,7 +96,7 @@ void LambdaFunctionNameCheck::check(const MatchFinder::MatchResult &Result) {
auto ER =
Result.SourceManager->getImmediateExpansionRange(E->getLocation());
- if (SuppressMacroExpansions.contains({ER.getBegin(), ER.getEnd()})) {
+ if (SuppressMacroExpansions.contains(ER.getAsRange())) {
// This is a macro expansion for which we should not warn.
return;
}
diff --git a/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.h b/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.h
index 3460fa8391223..5893fe3df1b01 100644
--- a/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.h
@@ -21,11 +21,6 @@ namespace clang::tidy::bugprone {
/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/lambda-function-name.html
class LambdaFunctionNameCheck : public ClangTidyCheck {
public:
- // FIXME: This pair should be a SourceRange, but SourceRange doesn't have
- // a DenseMapInfo specialization.
- using SourceRangeSet =
- llvm::DenseSet<std::pair<SourceLocation, SourceLocation>>;
-
LambdaFunctionNameCheck(StringRef Name, ClangTidyContext *Context);
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
return LangOpts.CPlusPlus11;
@@ -38,7 +33,7 @@ class LambdaFunctionNameCheck : public ClangTidyCheck {
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
- SourceRangeSet SuppressMacroExpansions;
+ llvm::DenseSet<SourceRange> SuppressMacroExpansions;
bool IgnoreMacros;
};
diff --git a/clang-tools-extra/clang-tidy/google/AvoidNSObjectNewCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidNSObjectNewCheck.cpp
index 4f66a084dd956..3bf96fbb94c8d 100644
--- a/clang-tools-extra/clang-tidy/google/AvoidNSObjectNewCheck.cpp
+++ b/clang-tools-extra/clang-tidy/google/AvoidNSObjectNewCheck.cpp
@@ -14,8 +14,8 @@
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
#include "llvm/Support/FormatVariadic.h"
-#include <map>
#include <string>
+#include <utility>
using namespace clang::ast_matchers;
diff --git a/clang-tools-extra/clang-tidy/misc/NewDeleteOverloadsCheck.h b/clang-tools-extra/clang-tidy/misc/NewDeleteOverloadsCheck.h
index 7053e5d9fc1bc..36af07c9fbd17 100644
--- a/clang-tools-extra/clang-tidy/misc/NewDeleteOverloadsCheck.h
+++ b/clang-tools-extra/clang-tidy/misc/NewDeleteOverloadsCheck.h
@@ -14,7 +14,8 @@
namespace clang::tidy::misc {
class NewDeleteOverloadsCheck : public ClangTidyCheck {
- llvm::DenseMap<const CXXRecordDecl *, SmallVector<const FunctionDecl *, 4>>
+ llvm::DenseMap<const CXXRecordDecl *,
+ llvm::SmallVector<const FunctionDecl *, 4>>
Overloads;
public:
More information about the cfe-commits
mailing list