Skip to content

add GetSpellingFromOperator & GetOperatorFromSpelling #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/CppInterOp/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,12 @@ CPPINTEROP_API std::string GetFunctionArgDefault(TCppFunction_t func,
CPPINTEROP_API std::string GetFunctionArgName(TCppFunction_t func,
TCppIndex_t param_index);

///\returns string representation of the operator
CPPINTEROP_API std::string GetSpellingFromOperator(Operator Operator);

///\returns operator of representing the string
CPPINTEROP_API Operator GetOperatorFromSpelling(const std::string& op);

///\returns arity of the operator or kNone
CPPINTEROP_API OperatorArity GetOperatorArity(TCppFunction_t op);

Expand Down
16 changes: 15 additions & 1 deletion lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2070,7 +2070,8 @@ void make_narg_call(const FunctionDecl* FD, const std::string& return_type,
if (op_flag) {
callbuf << ", ";
} else {
callbuf << ' ' << Cpp::getOperatorSpelling(FD->getOverloadedOperator())
callbuf << ' '
<< clang::getOperatorSpelling(FD->getOverloadedOperator())
<< ' ';
}
}
Expand Down Expand Up @@ -3559,6 +3560,19 @@ std::string GetFunctionArgName(TCppFunction_t func, TCppIndex_t param_index) {
return PI->getNameAsString();
}

std::string GetSpellingFromOperator(Operator Operator) {
return clang::getOperatorSpelling((clang::OverloadedOperatorKind)Operator);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

  auto* D = (clang::Decl*)func;
            ^

}

Operator GetOperatorFromSpelling(const std::string& op) {
#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
if ((Spelling) == op) { \
return (Operator)OO_##Name; \
}
#include "clang/Basic/OperatorKinds.def"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: Called C++ object pointer is null [clang-analyzer-core.CallAndMessage]

  return PI->getNameAsString();
         ^
Additional context

lib/CppInterOp/CppInterOp.cpp:3564: 'PI' initialized to a null pointer value

  clang::ParmVarDecl* PI = nullptr;
  ^

lib/CppInterOp/CppInterOp.cpp:3566: Assuming null pointer is passed into cast

  if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionDecl>(D))
                 ^

lib/CppInterOp/CppInterOp.cpp:3566: 'FD' is null

  if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionDecl>(D))
            ^

lib/CppInterOp/CppInterOp.cpp:3566: Taking false branch

  if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionDecl>(D))
  ^

lib/CppInterOp/CppInterOp.cpp:3568: Assuming null pointer is passed into cast

  else if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(D))
                      ^

lib/CppInterOp/CppInterOp.cpp:3568: 'FD' is null

  else if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(D))
                 ^

lib/CppInterOp/CppInterOp.cpp:3568: Taking false branch

  else if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(D))
       ^

lib/CppInterOp/CppInterOp.cpp:3571: Called C++ object pointer is null

  return PI->getNameAsString();
         ^

return Operator::OP_None;
}

OperatorArity GetOperatorArity(TCppFunction_t op) {
Decl* D = static_cast<Decl*>(op);
if (auto* FD = llvm::dyn_cast<FunctionDecl>(D)) {
Expand Down
8 changes: 8 additions & 0 deletions unittests/CppInterOp/TypeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,3 +611,11 @@ TEST(TypeReflectionTest, IsFunctionPointerType) {
EXPECT_FALSE(
Cpp::IsFunctionPointerType(Cpp::GetVariableType(Cpp::GetNamed("i"))));
}

TEST(TypeReflectionTest, OperatorSpelling) {
EXPECT_EQ(Cpp::GetSpellingFromOperator(Cpp::OP_Less), "<");
EXPECT_EQ(Cpp::GetSpellingFromOperator(Cpp::OP_Plus), "+");
EXPECT_EQ(Cpp::GetOperatorFromSpelling("->"), Cpp::OP_Arrow);
EXPECT_EQ(Cpp::GetOperatorFromSpelling("()"), Cpp::OP_Call);
EXPECT_EQ(Cpp::GetOperatorFromSpelling("invalid"), Cpp::OP_None);
}
Loading