Skip to content

Commit 5e6e40f

Browse files
authored
[clang-tidy] Improve performance of google-runtime-int (#86596)
Main problem with performance of this check is caused by hasAncestor matcher, and to be more precise by an llvm::DenseSet and std::deque in matchesAnyAncestorOf. To reduce impact of this matcher, multiple conditions that were checked in check method were copied into AST matcher that is now checked before hasAncestor. Using custom getCheckTraversalKind to exclude template instances that shouldn't be checked anyway is an additional improvement, but gain from that one is low. Tested on ffl_tests.cc, visible reduction from ~442 seconds to ~15 seconds (~96% reduction). Closes #86553
1 parent 28ddbd4 commit 5e6e40f

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

clang-tools-extra/clang-tidy/google/IntegerTypesCheck.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,34 @@ namespace {
4040
AST_MATCHER(FunctionDecl, isUserDefineLiteral) {
4141
return Node.getLiteralIdentifier() != nullptr;
4242
}
43+
44+
AST_MATCHER(TypeLoc, isValidAndNotInMacro) {
45+
const SourceLocation Loc = Node.getBeginLoc();
46+
return Loc.isValid() && !Loc.isMacroID();
47+
}
48+
49+
AST_MATCHER(TypeLoc, isBuiltinType) {
50+
TypeLoc TL = Node;
51+
if (auto QualLoc = Node.getAs<QualifiedTypeLoc>())
52+
TL = QualLoc.getUnqualifiedLoc();
53+
54+
const auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>();
55+
if (!BuiltinLoc)
56+
return false;
57+
58+
switch (BuiltinLoc.getTypePtr()->getKind()) {
59+
case BuiltinType::Short:
60+
case BuiltinType::Long:
61+
case BuiltinType::LongLong:
62+
case BuiltinType::UShort:
63+
case BuiltinType::ULong:
64+
case BuiltinType::ULongLong:
65+
return true;
66+
default:
67+
return false;
68+
}
69+
}
70+
4371
} // namespace
4472

4573
namespace tidy::google::runtime {
@@ -63,11 +91,11 @@ void IntegerTypesCheck::registerMatchers(MatchFinder *Finder) {
6391
// "Where possible, avoid passing arguments of types specified by
6492
// bitwidth typedefs to printf-based APIs."
6593
Finder->addMatcher(
66-
typeLoc(loc(isInteger()),
67-
unless(anyOf(hasAncestor(callExpr(
68-
callee(functionDecl(hasAttr(attr::Format))))),
69-
hasParent(parmVarDecl(hasAncestor(
70-
functionDecl(isUserDefineLiteral())))))))
94+
typeLoc(loc(isInteger()), isValidAndNotInMacro(), isBuiltinType(),
95+
unless(hasAncestor(
96+
callExpr(callee(functionDecl(hasAttr(attr::Format)))))),
97+
unless(hasParent(parmVarDecl(
98+
hasAncestor(functionDecl(isUserDefineLiteral()))))))
7199
.bind("tl"),
72100
this);
73101
IdentTable = std::make_unique<IdentifierTable>(getLangOpts());
@@ -77,9 +105,6 @@ void IntegerTypesCheck::check(const MatchFinder::MatchResult &Result) {
77105
auto TL = *Result.Nodes.getNodeAs<TypeLoc>("tl");
78106
SourceLocation Loc = TL.getBeginLoc();
79107

80-
if (Loc.isInvalid() || Loc.isMacroID())
81-
return;
82-
83108
// Look through qualification.
84109
if (auto QualLoc = TL.getAs<QualifiedTypeLoc>())
85110
TL = QualLoc.getUnqualifiedLoc();

clang-tools-extra/clang-tidy/google/IntegerTypesCheck.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class IntegerTypesCheck : public ClangTidyCheck {
3535
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
3636
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
3737
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
38+
std::optional<TraversalKind> getCheckTraversalKind() const override {
39+
return TK_IgnoreUnlessSpelledInSource;
40+
}
3841

3942
private:
4043
const StringRef UnsignedTypePrefix;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ Changes in existing checks
201201
<clang-tidy/checks/google/global-names-in-headers>` check by replacing the local
202202
option `HeaderFileExtensions` by the global option of the same name.
203203

204+
- Improved :doc:`google-runtime-int <clang-tidy/checks/google/runtime-int>`
205+
check performance through optimizations.
206+
204207
- Improved :doc:`llvm-header-guard
205208
<clang-tidy/checks/llvm/header-guard>` check by replacing the local
206209
option `HeaderFileExtensions` by the global option of the same name.

0 commit comments

Comments
 (0)