Skip to content

Commit d6cc147

Browse files
committed
Added function to libclang for retrieving
BinaryOperationKind/UnaryOperationKind. The new function clang_getCursorOperatorKind() (Cursor.operator_kind() in python binding) will return the exact opcode if the cursor is a BinaryOperation or UnaryOperation.
1 parent 9fd77bd commit d6cc147

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed

bindings/python/clang/cindex.py

+72
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,64 @@ def __repr__(self):
11201120
# A type alias template declaration
11211121
CursorKind.TYPE_ALIAS_TEMPLATE_DECL = CursorKind(601)
11221122

1123+
class OperatorKind(BaseEnumeration):
1124+
"""
1125+
A OperatorKind describes the kind of operator of a cursor of kind
1126+
BinaryOperator or UnaryOperator.
1127+
"""
1128+
1129+
# The required BaseEnumeration declarations.
1130+
_kinds = []
1131+
_name_map = None
1132+
1133+
OperatorKind.NULL = OperatorKind(0)
1134+
OperatorKind.PTR_MEM_D = OperatorKind(1)
1135+
OperatorKind.PTR_MEM_I = OperatorKind(2)
1136+
OperatorKind.MUL = OperatorKind(3)
1137+
OperatorKind.DIV = OperatorKind(4)
1138+
OperatorKind.REM = OperatorKind(5)
1139+
OperatorKind.ADD = OperatorKind(6)
1140+
OperatorKind.SUB = OperatorKind(7)
1141+
OperatorKind.SHL = OperatorKind(8)
1142+
OperatorKind.SHR = OperatorKind(9)
1143+
OperatorKind.LT = OperatorKind(10)
1144+
OperatorKind.GT = OperatorKind(11)
1145+
OperatorKind.LE = OperatorKind(12)
1146+
OperatorKind.GE = OperatorKind(13)
1147+
OperatorKind.EQ = OperatorKind(14)
1148+
OperatorKind.NE = OperatorKind(15)
1149+
OperatorKind.AND = OperatorKind(16)
1150+
OperatorKind.XOR = OperatorKind(17)
1151+
OperatorKind.OR = OperatorKind(18)
1152+
OperatorKind.LAND = OperatorKind(19)
1153+
OperatorKind.LOR = OperatorKind(20)
1154+
OperatorKind.ASSIGN = OperatorKind(21)
1155+
OperatorKind.MUL_ASSIGN = OperatorKind(22)
1156+
OperatorKind.DIV_ASSIGN = OperatorKind(23)
1157+
OperatorKind.REM_ASSIGN = OperatorKind(24)
1158+
OperatorKind.ADD_ASSIGN = OperatorKind(25)
1159+
OperatorKind.SUB_ASSIGN = OperatorKind(26)
1160+
OperatorKind.SHL_ASSIGN = OperatorKind(27)
1161+
OperatorKind.SHR_ASSIGN = OperatorKind(28)
1162+
OperatorKind.AND_ASSIGN = OperatorKind(29)
1163+
OperatorKind.XOR_ASSIGN = OperatorKind(30)
1164+
OperatorKind.OR_ASSIGN = OperatorKind(31)
1165+
OperatorKind.COMMA = OperatorKind(32)
1166+
OperatorKind.POST_INC = OperatorKind(101)
1167+
OperatorKind.POST_DEC = OperatorKind(102)
1168+
OperatorKind.PRE_INC = OperatorKind(103)
1169+
OperatorKind.PRE_DEC = OperatorKind(104)
1170+
OperatorKind.ADDR_OF = OperatorKind(105)
1171+
OperatorKind.DEREF = OperatorKind(106)
1172+
OperatorKind.PLUS = OperatorKind(107)
1173+
OperatorKind.MINUS = OperatorKind(108)
1174+
OperatorKind.NOT = OperatorKind(109)
1175+
OperatorKind.LNOT = OperatorKind(110)
1176+
OperatorKind.REAL = OperatorKind(111)
1177+
OperatorKind.IMAG = OperatorKind(112)
1178+
OperatorKind.EXTENSION = OperatorKind(113)
1179+
OperatorKind.COAWAIT = OperatorKind(114)
1180+
11231181
### Template Argument Kinds ###
11241182
class TemplateArgumentKind(BaseEnumeration):
11251183
"""
@@ -1224,6 +1282,16 @@ def kind(self):
12241282
"""Return the kind of this cursor."""
12251283
return CursorKind.from_id(self._kind_id)
12261284

1285+
@property
1286+
def operator_kind(self):
1287+
"""Return the kind of the operator.
1288+
1289+
If cursor is of kind CursorKind.BINARY_OPERATOR or
1290+
CursorKind.UNARY_OPERATOR the exact kind of operation can be retrieved
1291+
by this property, which returns a OperatorKind enum."""
1292+
operator_kind_id = conf.lib.clang_getCursorOperatorKind(self)
1293+
return OperatorKind.from_id(operator_kind_id)
1294+
12271295
@property
12281296
def spelling(self):
12291297
"""Return the spelling of the entity pointed at by the cursor."""
@@ -3052,6 +3120,10 @@ def cursor(self):
30523120
[Cursor],
30533121
SourceLocation),
30543122

3123+
("clang_getCursorOperatorKind",
3124+
[Cursor],
3125+
c_uint),
3126+
30553127
("clang_getCursorReferenced",
30563128
[Cursor],
30573129
Cursor,

include/clang-c/Index.h

+68
Original file line numberDiff line numberDiff line change
@@ -2454,6 +2454,74 @@ CINDEX_LINKAGE unsigned clang_isPreprocessing(enum CXCursorKind);
24542454
*/
24552455
CINDEX_LINKAGE unsigned clang_isUnexposed(enum CXCursorKind);
24562456

2457+
/**
2458+
* \brief Describes the kind of entity that a cursor refers to.
2459+
*/
2460+
enum CXOperatorKind {
2461+
/* Declarations */
2462+
/**
2463+
* \brief This cursor is not of kind CXCursorKind_BinaryOperator or
2464+
* CXCursorKind_UnaryOperator.
2465+
*/
2466+
CXOperatorKind_Null = 0,
2467+
2468+
CXOperatorKind_PtrMemD = 1,
2469+
CXOperatorKind_PtrMemI = 2,
2470+
CXOperatorKind_Mul = 3,
2471+
CXOperatorKind_Div = 4,
2472+
CXOperatorKind_Rem = 5,
2473+
CXOperatorKind_Add = 6,
2474+
CXOperatorKind_Sub = 7,
2475+
CXOperatorKind_Shl = 8,
2476+
CXOperatorKind_Shr = 9,
2477+
CXOperatorKind_LT = 10,
2478+
CXOperatorKind_GT = 11,
2479+
CXOperatorKind_LE = 12,
2480+
CXOperatorKind_GE = 13,
2481+
CXOperatorKind_EQ = 14,
2482+
CXOperatorKind_NE = 15,
2483+
CXOperatorKind_And = 16,
2484+
CXOperatorKind_Xor = 17,
2485+
CXOperatorKind_Or = 18,
2486+
CXOperatorKind_LAnd = 19,
2487+
CXOperatorKind_LOr = 20,
2488+
CXOperatorKind_Assign = 21,
2489+
CXOperatorKind_MulAssign = 22,
2490+
CXOperatorKind_DivAssign = 23,
2491+
CXOperatorKind_RemAssign = 24,
2492+
CXOperatorKind_AddAssign = 25,
2493+
CXOperatorKind_SubAssign = 26,
2494+
CXOperatorKind_ShlAssign = 27,
2495+
CXOperatorKind_ShrAssign = 28,
2496+
CXOperatorKind_AndAssign = 29,
2497+
CXOperatorKind_XorAssign = 30,
2498+
CXOperatorKind_OrAssign = 31,
2499+
CXOperatorKind_Comma = 32,
2500+
2501+
CXOperatorKind_PostInc = 101,
2502+
CXOperatorKind_PostDec = 102,
2503+
CXOperatorKind_PreInc = 103,
2504+
CXOperatorKind_PreDec = 104,
2505+
CXOperatorKind_AddrOf = 105,
2506+
CXOperatorKind_Deref = 106,
2507+
CXOperatorKind_Plus = 107,
2508+
CXOperatorKind_Minus = 108,
2509+
CXOperatorKind_Not = 109,
2510+
CXOperatorKind_LNot = 110,
2511+
CXOperatorKind_Real = 111,
2512+
CXOperatorKind_Imag = 112,
2513+
CXOperatorKind_Extension = 113,
2514+
CXOperatorKind_Coawait = 114,
2515+
} ;
2516+
2517+
/**
2518+
* \brief Retrieve Operation kind of the given cursor.
2519+
*
2520+
* If cursor is of kind BinaryOperation, this function returns the exact
2521+
* kind of the binary operation.
2522+
*/
2523+
CINDEX_LINKAGE enum CXOperatorKind clang_getCursorOperatorKind(CXCursor);
2524+
24572525
/**
24582526
* \brief Describe the linkage of the entity referred to by a cursor.
24592527
*/

tools/libclang/CIndex.cpp

+111
Original file line numberDiff line numberDiff line change
@@ -5215,6 +5215,117 @@ CXCursor clang_getCursorReferenced(CXCursor C) {
52155215
}
52165216
}
52175217

5218+
enum CXOperatorKind clang_getCursorOperatorKind(CXCursor C) {
5219+
if (C.kind == CXCursor_BinaryOperator) {
5220+
const BinaryOperator * BinOp =
5221+
static_cast<const BinaryOperator *>(getCursorExpr(C));
5222+
5223+
switch (BinOp->getOpcode()) {
5224+
case BO_PtrMemD:
5225+
return CXOperatorKind_PtrMemD;
5226+
case BO_PtrMemI:
5227+
return CXOperatorKind_PtrMemI;
5228+
case BO_Mul:
5229+
return CXOperatorKind_Mul;
5230+
case BO_Div:
5231+
return CXOperatorKind_Div;
5232+
case BO_Rem:
5233+
return CXOperatorKind_Rem;
5234+
case BO_Add:
5235+
return CXOperatorKind_Add;
5236+
case BO_Sub:
5237+
return CXOperatorKind_Sub;
5238+
case BO_Shl:
5239+
return CXOperatorKind_Shl;
5240+
case BO_Shr:
5241+
return CXOperatorKind_Shr;
5242+
case BO_LT:
5243+
return CXOperatorKind_LT;
5244+
case BO_GT:
5245+
return CXOperatorKind_GT;
5246+
case BO_LE:
5247+
return CXOperatorKind_LE;
5248+
case BO_GE:
5249+
return CXOperatorKind_GE;
5250+
case BO_EQ:
5251+
return CXOperatorKind_EQ;
5252+
case BO_NE:
5253+
return CXOperatorKind_NE;
5254+
case BO_And:
5255+
return CXOperatorKind_And;
5256+
case BO_Xor:
5257+
return CXOperatorKind_Xor;
5258+
case BO_Or:
5259+
return CXOperatorKind_Or;
5260+
case BO_LAnd:
5261+
return CXOperatorKind_LAnd;
5262+
case BO_LOr:
5263+
return CXOperatorKind_LOr;
5264+
case BO_Assign:
5265+
return CXOperatorKind_Assign;
5266+
case BO_MulAssign:
5267+
return CXOperatorKind_MulAssign;
5268+
case BO_DivAssign:
5269+
return CXOperatorKind_DivAssign;
5270+
case BO_RemAssign:
5271+
return CXOperatorKind_RemAssign;
5272+
case BO_AddAssign:
5273+
return CXOperatorKind_AddAssign;
5274+
case BO_SubAssign:
5275+
return CXOperatorKind_SubAssign;
5276+
case BO_ShlAssign:
5277+
return CXOperatorKind_ShlAssign;
5278+
case BO_ShrAssign:
5279+
return CXOperatorKind_ShrAssign;
5280+
case BO_AndAssign:
5281+
return CXOperatorKind_AndAssign;
5282+
case BO_XorAssign:
5283+
return CXOperatorKind_XorAssign;
5284+
case BO_OrAssign:
5285+
return CXOperatorKind_OrAssign;
5286+
case BO_Comma:
5287+
return CXOperatorKind_Comma;
5288+
}
5289+
}
5290+
else if (C.kind == CXCursor_UnaryOperator) {
5291+
const UnaryOperator * UnOp =
5292+
static_cast<const UnaryOperator *>(getCursorExpr(C));
5293+
5294+
switch (UnOp->getOpcode()) {
5295+
case UO_PostInc:
5296+
return CXOperatorKind_PostInc;
5297+
case UO_PostDec:
5298+
return CXOperatorKind_PostDec;
5299+
case UO_PreInc:
5300+
return CXOperatorKind_PreInc;
5301+
case UO_PreDec:
5302+
return CXOperatorKind_PreDec;
5303+
case UO_AddrOf:
5304+
return CXOperatorKind_AddrOf;
5305+
case UO_Deref:
5306+
return CXOperatorKind_Deref;
5307+
case UO_Plus:
5308+
return CXOperatorKind_Plus;
5309+
case UO_Minus:
5310+
return CXOperatorKind_Minus;
5311+
case UO_Not:
5312+
return CXOperatorKind_Not;
5313+
case UO_LNot:
5314+
return CXOperatorKind_LNot;
5315+
case UO_Real:
5316+
return CXOperatorKind_Real;
5317+
case UO_Imag:
5318+
return CXOperatorKind_Imag;
5319+
case UO_Extension:
5320+
return CXOperatorKind_Extension;
5321+
case UO_Coawait:
5322+
return CXOperatorKind_Coawait;
5323+
}
5324+
}
5325+
5326+
return CXOperatorKind_Null;
5327+
}
5328+
52185329
CXCursor clang_getCursorDefinition(CXCursor C) {
52195330
if (clang_isInvalid(C.kind))
52205331
return clang_getNullCursor();

tools/libclang/libclang.exports

+1
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,4 @@ clang_VirtualFileOverlay_create
320320
clang_VirtualFileOverlay_dispose
321321
clang_VirtualFileOverlay_setCaseSensitivity
322322
clang_VirtualFileOverlay_writeToBuffer
323+
clang_getBinaryOperatorKind

0 commit comments

Comments
 (0)