From bf9c087b5a527a3a4ffd77b559e788ab4a5ab98b Mon Sep 17 00:00:00 2001 From: Aotrix <9aotrix@protonmail.com> Date: Wed, 7 Oct 2026 18:59:23 +0200 Subject: Added custom matcher --- removeFunctionCall.cpp | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'removeFunctionCall.cpp') diff --git a/removeFunctionCall.cpp b/removeFunctionCall.cpp index 42d4645..c9d497e 100644 --- a/removeFunctionCall.cpp +++ b/removeFunctionCall.cpp @@ -57,6 +57,7 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Signals.h" #include "llvm/Support/raw_ostream.h" +#include using namespace clang; using namespace clang::ast_matchers; @@ -76,12 +77,22 @@ namespace ast_matchers { namespace { -AST_MATCHER_P(MemberExpr, - Member, - clang::ast_matchers::internal::Matcher, - InnerMatcher) +AST_POLYMORPHIC_MATCHER_P2(hasVariableNameArgument, + AST_POLYMORPHIC_SUPPORTED_TYPES(CallExpr, + CXXConstructExpr, + CXXUnresolvedConstructExpr, + ObjCMessageExpr), + unsigned, + N, + clang::ast_matchers::internal::Matcher, + InnerMatcher) { - return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder); + if (N >= Node.getNumArgs()) + return false; + const Expr* Arg = Node.getArg(N); + if (Finder->isTraversalIgnoringImplicitNodes() && isa(Arg)) + return false; + return InnerMatcher.matches(*Arg->IgnoreParenImpCasts(), Finder, Builder); } } // namespace } // namespace ast_matchers @@ -153,12 +164,11 @@ int main(int argc, auto FunctionToRemove = functionDecl(matchesName("configValue")); // FIXME: do not clean a function call if it used by strictly more than one parameter // for instance if there is a ternary operator somewhere... - const std::string FunctionCallId{"x"}; - const std::string ExpectedValue("true"); - auto Rule = - makeRule(callExpr(hasArgument(0, hasDescendant(stringLiteral())), callee(FunctionToRemove)) - .bind(FunctionCallId), - transformer::changeTo(node(FunctionCallId), cat(ExpectedValue))); + const std::string FunctionCallId{"functionCallId"}; + const std::string FunctionReturnValue("true"); + auto Rule = makeRule( + callExpr(hasVariableNameArgument(0, expr()), callee(FunctionToRemove)).bind(FunctionCallId), + transformer::changeTo(node(FunctionCallId), cat(FunctionReturnValue))); AtomicChanges AllChanges; -- cgit v1.2.3