[clang] [clang][Analysis] Add argument accessors to clang::AnyCall (PR #212934)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 31 01:11:01 PDT 2026
================
@@ -0,0 +1,274 @@
+//===- AnyCallTest.cpp - AnyCall unit tests ---------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Analysis/AnyCall.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
+#include "clang/AST/ExprObjC.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Frontend/ASTUnit.h"
+#include "clang/Tooling/Tooling.h"
+#include "gtest/gtest.h"
+#include <initializer_list>
+#include <memory>
+#include <string>
+#include <vector>
+
+namespace clang {
+namespace {
+
+using namespace ast_matchers;
+
+std::unique_ptr<ASTUnit> buildAST(llvm::StringRef Code,
+ std::vector<std::string> Args = {
+ "-fsyntax-only", "-std=c++17"}) {
+ return tooling::buildASTFromCodeWithArgs(Code, Args);
+}
+
+const IntegerLiteral *asIntegerLiteral(const Expr *E) {
+ return dyn_cast<IntegerLiteral>(E->IgnoreImplicit());
+}
+
+void expectIntegerArguments(const AnyCall &Call,
+ std::initializer_list<int> Expected) {
+ ASSERT_EQ(Call.arg_size(), Expected.size());
+ EXPECT_FALSE(Call.arg_empty());
+
+ unsigned Index = 0;
+ for (int ExpectedValue : Expected) {
+ const auto *Arg = asIntegerLiteral(Call.getArg(Index));
+ ASSERT_NE(Arg, nullptr);
+ EXPECT_EQ(Arg->getValue(), ExpectedValue);
+ ++Index;
+ }
+}
+
+void expectNoArguments(const AnyCall &Call) {
+ EXPECT_TRUE(Call.arg_empty());
+ EXPECT_EQ(Call.arg_size(), 0u);
+ EXPECT_EQ(Call.arg_begin(), Call.arg_end());
+}
+
+TEST(AnyCallTest, ExposesFunctionParameters) {
+ auto AST = buildAST(R"cpp(
+ void callee(int first, int second);
+ )cpp");
+ ASTContext &Ctx = AST->getASTContext();
+ ASSERT_EQ(Ctx.getDiagnostics().getClient()->getNumErrors(), 0U);
+
+ const auto *Callee = selectFirst<FunctionDecl>(
+ "callee", match(functionDecl(hasName("callee")).bind("callee"), Ctx));
+ ASSERT_NE(Callee, nullptr);
+
+ AnyCall Call(Callee);
+ EXPECT_FALSE(Call.param_empty());
+ ASSERT_EQ(Call.param_size(), 2u);
+ EXPECT_EQ(Call.parameters()[0], Callee->getParamDecl(0));
+ EXPECT_EQ(*Call.param_begin(), Callee->getParamDecl(0));
+}
+
+TEST(AnyCallTest, ExposesFunctionCallArguments) {
+ auto AST = buildAST(R"cpp(
+ void callee(int, int);
+ void target() { callee(1, 2); }
+ )cpp");
+ ASTContext &Ctx = AST->getASTContext();
+ ASSERT_EQ(Ctx.getDiagnostics().getClient()->getNumErrors(), 0U);
+
+ const auto *CE = selectFirst<CallExpr>(
+ "call",
+ match(callExpr(callee(functionDecl(hasName("callee")))).bind("call"),
+ Ctx));
+ ASSERT_NE(CE, nullptr);
+
+ AnyCall Call(CE);
----------------
iitianpushkar wrote:
Good point. Thanks!
Will refactor the code in the updated patch accordingly.
https://github.com/llvm/llvm-project/pull/212934
More information about the cfe-commits
mailing list