Skip to content

Commit 40ecc1d

Browse files
add GetSpellingFromOperator & GetOperatorFromSpelling (#628)
--------- Co-authored-by: mcbarton <[email protected]>
1 parent 7f21aca commit 40ecc1d

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

include/CppInterOp/CppInterOp.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,12 @@ CPPINTEROP_API std::string GetFunctionArgDefault(TCppFunction_t func,
600600
CPPINTEROP_API std::string GetFunctionArgName(TCppFunction_t func,
601601
TCppIndex_t param_index);
602602

603+
///\returns string representation of the operator
604+
CPPINTEROP_API std::string GetSpellingFromOperator(Operator Operator);
605+
606+
///\returns operator of representing the string
607+
CPPINTEROP_API Operator GetOperatorFromSpelling(const std::string& op);
608+
603609
///\returns arity of the operator or kNone
604610
CPPINTEROP_API OperatorArity GetOperatorArity(TCppFunction_t op);
605611

lib/CppInterOp/CppInterOp.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2082,7 +2082,8 @@ void make_narg_call(const FunctionDecl* FD, const std::string& return_type,
20822082
if (op_flag) {
20832083
callbuf << ", ";
20842084
} else {
2085-
callbuf << ' ' << Cpp::getOperatorSpelling(FD->getOverloadedOperator())
2085+
callbuf << ' '
2086+
<< clang::getOperatorSpelling(FD->getOverloadedOperator())
20862087
<< ' ';
20872088
}
20882089
}
@@ -3571,6 +3572,19 @@ std::string GetFunctionArgName(TCppFunction_t func, TCppIndex_t param_index) {
35713572
return PI->getNameAsString();
35723573
}
35733574

3575+
std::string GetSpellingFromOperator(Operator Operator) {
3576+
return clang::getOperatorSpelling((clang::OverloadedOperatorKind)Operator);
3577+
}
3578+
3579+
Operator GetOperatorFromSpelling(const std::string& op) {
3580+
#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
3581+
if ((Spelling) == op) { \
3582+
return (Operator)OO_##Name; \
3583+
}
3584+
#include "clang/Basic/OperatorKinds.def"
3585+
return Operator::OP_None;
3586+
}
3587+
35743588
OperatorArity GetOperatorArity(TCppFunction_t op) {
35753589
Decl* D = static_cast<Decl*>(op);
35763590
if (auto* FD = llvm::dyn_cast<FunctionDecl>(D)) {

unittests/CppInterOp/TypeReflectionTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,3 +611,11 @@ 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+
EXPECT_EQ(Cpp::GetOperatorFromSpelling("invalid"), Cpp::OP_None);
621+
}

0 commit comments

Comments
 (0)