Skip to content

Commit 610fbe8

Browse files
add GetSpellingFromOperator & GetOperatorFromSpelling
1 parent 360a117 commit 610fbe8

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

include/CppInterOp/CppInterOp.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,12 @@ CPPINTEROP_API std::string GetFunctionArgDefault(TCppFunction_t func,
593593
CPPINTEROP_API std::string GetFunctionArgName(TCppFunction_t func,
594594
TCppIndex_t param_index);
595595

596+
///\returns string representation of the operator
597+
std::string GetSpellingFromOperator(Operator Operator);
598+
599+
///\returns operator of representing the string
600+
Operator GetOperatorFromSpelling(const std::string& op);
601+
596602
///\returns arity of the operator or kNone
597603
CPPINTEROP_API OperatorArity GetOperatorArity(TCppFunction_t op);
598604

lib/CppInterOp/CppInterOp.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2070,7 +2070,8 @@ void make_narg_call(const FunctionDecl* FD, const std::string& return_type,
20702070
if (op_flag) {
20712071
callbuf << ", ";
20722072
} else {
2073-
callbuf << ' ' << Cpp::getOperatorSpelling(FD->getOverloadedOperator())
2073+
callbuf << ' '
2074+
<< clang::getOperatorSpelling(FD->getOverloadedOperator())
20742075
<< ' ';
20752076
}
20762077
}
@@ -3559,6 +3560,19 @@ std::string GetFunctionArgName(TCppFunction_t func, TCppIndex_t param_index) {
35593560
return PI->getNameAsString();
35603561
}
35613562

3563+
std::string GetSpellingFromOperator(Operator Operator) {
3564+
return clang::getOperatorSpelling((clang::OverloadedOperatorKind)Operator);
3565+
}
3566+
3567+
Operator GetOperatorFromSpelling(const std::string& op) {
3568+
#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
3569+
if ((Spelling) == op) { \
3570+
return (Operator)OO_##Name; \
3571+
}
3572+
#include "clang/Basic/OperatorKinds.def"
3573+
return Operator::OP_None;
3574+
}
3575+
35623576
OperatorArity GetOperatorArity(TCppFunction_t op) {
35633577
Decl* D = static_cast<Decl*>(op);
35643578
if (auto* FD = llvm::dyn_cast<FunctionDecl>(D)) {

unittests/CppInterOp/TypeReflectionTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,3 +611,10 @@ TEST(TypeReflectionTest, IsFunctionPointerType) {
611611
EXPECT_FALSE(
612612
Cpp::IsFunctionPointerType(Cpp::GetVariableType(Cpp::GetNamed("i"))));
613613
}
614+
615+
TEST(TypeReflectionTest, OperatorSpelling) {
616+
EXPECT_EQ(Cpp::GetSpellingFromOperator(Cpp::OP_Less), "<");
617+
EXPECT_EQ(Cpp::GetSpellingFromOperator(Cpp::OP_Plus), "+");
618+
EXPECT_EQ(Cpp::GetOperatorFromSpelling("->"), Cpp::OP_Arrow);
619+
EXPECT_EQ(Cpp::GetOperatorFromSpelling("()"), Cpp::OP_Call);
620+
}

0 commit comments

Comments
 (0)