Skip to content

Commit

Permalink
Optimize more codepaths to use operator enum instead of string compar…
Browse files Browse the repository at this point in the history
…isons

This is a small optimization, but checking the operator costs 1/10 of the time of a string comparison.
Especially for the often used Utils and level0 check, this is sensible
to have.
  • Loading branch information
alex1701c authored and gruenich committed Dec 27, 2024
1 parent ee34145 commit 215b2ca
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ bool Utils::isAssignOperator(CXXOperatorCallExpr *op, StringRef className, Strin
}
}

if (functionDecl->getNameAsString() != "operator=") {
if (functionDecl->getOverloadedOperator() != clang::OO_Equal) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/checks/level0/qstring-comparison-to-implicit-char.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void QStringComparisonToImplicitChar::VisitStmt(clang::Stmt *stmt)
}

auto *functionDecl = dyn_cast<FunctionDecl>(callExpr->getCalleeDecl());
if (!functionDecl || functionDecl->getQualifiedNameAsString() != "operator==") {
if (!functionDecl || functionDecl->getOverloadedOperator() != clang::OO_EqualEqual) {
return;
}

Expand Down
5 changes: 2 additions & 3 deletions src/checks/level2/function-args-by-ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ bool FunctionArgsByRef::shouldIgnoreClass(CXXRecordDecl *record)
bool FunctionArgsByRef::shouldIgnoreOperator(FunctionDecl *function)
{
// Too many warnings in operator<<
static const std::vector<StringRef> ignoreList = {"operator<<"};

return clazy::contains(ignoreList, clazy::name(function));
OverloadedOperatorKind op = function->getOverloadedOperator();
return op == clang::OO_LessLess;
}

bool FunctionArgsByRef::shouldIgnoreFunction(clang::FunctionDecl *function)
Expand Down
20 changes: 4 additions & 16 deletions src/checks/level2/old-style-connect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,10 @@ bool OldStyleConnect::isQPointer(Expr *expr) const
std::vector<CXXMemberCallExpr *> memberCalls;
clazy::getChilds<CXXMemberCallExpr>(expr, memberCalls);

for (auto *callExpr : memberCalls) {
if (!callExpr->getDirectCallee()) {
continue;
}
auto *method = dyn_cast<CXXMethodDecl>(callExpr->getDirectCallee());
if (!method) {
continue;
}

// Any better way to detect it's an operator ?
if (clazy::startsWith(method->getNameAsString(), "operator ")) {
return true;
}
}

return false;
return std::any_of(memberCalls.begin(), memberCalls.end(), [](CXXMemberCallExpr *callExpr) {
auto *callee = callExpr->getDirectCallee();
return callee && dyn_cast<CXXConversionDecl>(callee);
});
}

bool OldStyleConnect::isPrivateSlot(const std::string &name) const
Expand Down
3 changes: 2 additions & 1 deletion src/checks/manuallevel/detaching-member.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <clang/AST/Stmt.h>
#include <clang/AST/Type.h>
#include <clang/Basic/LLVM.h>
#include <clang/Basic/OperatorKinds.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/Support/Casting.h>

Expand Down Expand Up @@ -57,7 +58,7 @@ void DetachingMember::VisitStmt(clang::Stmt *stm)
if (operatorExpr) {
FunctionDecl *func = operatorExpr->getDirectCallee();
method = func ? dyn_cast<CXXMethodDecl>(func) : nullptr;
if (!method || clazy::name(method) != "operator[]") {
if (!method || method->getOverloadedOperator() != clang::OO_Subscript) {
return;
}

Expand Down

0 comments on commit 215b2ca

Please sign in to comment.