Skip to content

Commit

Permalink
Added function to libclang for retrieving
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mrh1997 committed May 12, 2016
1 parent 9fd77bd commit d6cc147
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 0 deletions.
72 changes: 72 additions & 0 deletions bindings/python/clang/cindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,64 @@ def __repr__(self):
# A type alias template declaration
CursorKind.TYPE_ALIAS_TEMPLATE_DECL = CursorKind(601)

class OperatorKind(BaseEnumeration):
"""
A OperatorKind describes the kind of operator of a cursor of kind
BinaryOperator or UnaryOperator.
"""

# The required BaseEnumeration declarations.
_kinds = []
_name_map = None

OperatorKind.NULL = OperatorKind(0)
OperatorKind.PTR_MEM_D = OperatorKind(1)
OperatorKind.PTR_MEM_I = OperatorKind(2)
OperatorKind.MUL = OperatorKind(3)
OperatorKind.DIV = OperatorKind(4)
OperatorKind.REM = OperatorKind(5)
OperatorKind.ADD = OperatorKind(6)
OperatorKind.SUB = OperatorKind(7)
OperatorKind.SHL = OperatorKind(8)
OperatorKind.SHR = OperatorKind(9)
OperatorKind.LT = OperatorKind(10)
OperatorKind.GT = OperatorKind(11)
OperatorKind.LE = OperatorKind(12)
OperatorKind.GE = OperatorKind(13)
OperatorKind.EQ = OperatorKind(14)
OperatorKind.NE = OperatorKind(15)
OperatorKind.AND = OperatorKind(16)
OperatorKind.XOR = OperatorKind(17)
OperatorKind.OR = OperatorKind(18)
OperatorKind.LAND = OperatorKind(19)
OperatorKind.LOR = OperatorKind(20)
OperatorKind.ASSIGN = OperatorKind(21)
OperatorKind.MUL_ASSIGN = OperatorKind(22)
OperatorKind.DIV_ASSIGN = OperatorKind(23)
OperatorKind.REM_ASSIGN = OperatorKind(24)
OperatorKind.ADD_ASSIGN = OperatorKind(25)
OperatorKind.SUB_ASSIGN = OperatorKind(26)
OperatorKind.SHL_ASSIGN = OperatorKind(27)
OperatorKind.SHR_ASSIGN = OperatorKind(28)
OperatorKind.AND_ASSIGN = OperatorKind(29)
OperatorKind.XOR_ASSIGN = OperatorKind(30)
OperatorKind.OR_ASSIGN = OperatorKind(31)
OperatorKind.COMMA = OperatorKind(32)
OperatorKind.POST_INC = OperatorKind(101)
OperatorKind.POST_DEC = OperatorKind(102)
OperatorKind.PRE_INC = OperatorKind(103)
OperatorKind.PRE_DEC = OperatorKind(104)
OperatorKind.ADDR_OF = OperatorKind(105)
OperatorKind.DEREF = OperatorKind(106)
OperatorKind.PLUS = OperatorKind(107)
OperatorKind.MINUS = OperatorKind(108)
OperatorKind.NOT = OperatorKind(109)
OperatorKind.LNOT = OperatorKind(110)
OperatorKind.REAL = OperatorKind(111)
OperatorKind.IMAG = OperatorKind(112)
OperatorKind.EXTENSION = OperatorKind(113)
OperatorKind.COAWAIT = OperatorKind(114)

### Template Argument Kinds ###
class TemplateArgumentKind(BaseEnumeration):
"""
Expand Down Expand Up @@ -1224,6 +1282,16 @@ def kind(self):
"""Return the kind of this cursor."""
return CursorKind.from_id(self._kind_id)

@property
def operator_kind(self):
"""Return the kind of the operator.
If cursor is of kind CursorKind.BINARY_OPERATOR or
CursorKind.UNARY_OPERATOR the exact kind of operation can be retrieved
by this property, which returns a OperatorKind enum."""
operator_kind_id = conf.lib.clang_getCursorOperatorKind(self)
return OperatorKind.from_id(operator_kind_id)

@property
def spelling(self):
"""Return the spelling of the entity pointed at by the cursor."""
Expand Down Expand Up @@ -3052,6 +3120,10 @@ def cursor(self):
[Cursor],
SourceLocation),

("clang_getCursorOperatorKind",
[Cursor],
c_uint),

("clang_getCursorReferenced",
[Cursor],
Cursor,
Expand Down
68 changes: 68 additions & 0 deletions include/clang-c/Index.h
Original file line number Diff line number Diff line change
Expand Up @@ -2454,6 +2454,74 @@ CINDEX_LINKAGE unsigned clang_isPreprocessing(enum CXCursorKind);
*/
CINDEX_LINKAGE unsigned clang_isUnexposed(enum CXCursorKind);

/**
* \brief Describes the kind of entity that a cursor refers to.
*/
enum CXOperatorKind {
/* Declarations */
/**
* \brief This cursor is not of kind CXCursorKind_BinaryOperator or
* CXCursorKind_UnaryOperator.
*/
CXOperatorKind_Null = 0,

CXOperatorKind_PtrMemD = 1,
CXOperatorKind_PtrMemI = 2,
CXOperatorKind_Mul = 3,
CXOperatorKind_Div = 4,
CXOperatorKind_Rem = 5,
CXOperatorKind_Add = 6,
CXOperatorKind_Sub = 7,
CXOperatorKind_Shl = 8,
CXOperatorKind_Shr = 9,
CXOperatorKind_LT = 10,
CXOperatorKind_GT = 11,
CXOperatorKind_LE = 12,
CXOperatorKind_GE = 13,
CXOperatorKind_EQ = 14,
CXOperatorKind_NE = 15,
CXOperatorKind_And = 16,
CXOperatorKind_Xor = 17,
CXOperatorKind_Or = 18,
CXOperatorKind_LAnd = 19,
CXOperatorKind_LOr = 20,
CXOperatorKind_Assign = 21,
CXOperatorKind_MulAssign = 22,
CXOperatorKind_DivAssign = 23,
CXOperatorKind_RemAssign = 24,
CXOperatorKind_AddAssign = 25,
CXOperatorKind_SubAssign = 26,
CXOperatorKind_ShlAssign = 27,
CXOperatorKind_ShrAssign = 28,
CXOperatorKind_AndAssign = 29,
CXOperatorKind_XorAssign = 30,
CXOperatorKind_OrAssign = 31,
CXOperatorKind_Comma = 32,

CXOperatorKind_PostInc = 101,
CXOperatorKind_PostDec = 102,
CXOperatorKind_PreInc = 103,
CXOperatorKind_PreDec = 104,
CXOperatorKind_AddrOf = 105,
CXOperatorKind_Deref = 106,
CXOperatorKind_Plus = 107,
CXOperatorKind_Minus = 108,
CXOperatorKind_Not = 109,
CXOperatorKind_LNot = 110,
CXOperatorKind_Real = 111,
CXOperatorKind_Imag = 112,
CXOperatorKind_Extension = 113,
CXOperatorKind_Coawait = 114,
} ;

/**
* \brief Retrieve Operation kind of the given cursor.
*
* If cursor is of kind BinaryOperation, this function returns the exact
* kind of the binary operation.
*/
CINDEX_LINKAGE enum CXOperatorKind clang_getCursorOperatorKind(CXCursor);

/**
* \brief Describe the linkage of the entity referred to by a cursor.
*/
Expand Down
111 changes: 111 additions & 0 deletions tools/libclang/CIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5215,6 +5215,117 @@ CXCursor clang_getCursorReferenced(CXCursor C) {
}
}

enum CXOperatorKind clang_getCursorOperatorKind(CXCursor C) {
if (C.kind == CXCursor_BinaryOperator) {
const BinaryOperator * BinOp =
static_cast<const BinaryOperator *>(getCursorExpr(C));

switch (BinOp->getOpcode()) {
case BO_PtrMemD:
return CXOperatorKind_PtrMemD;
case BO_PtrMemI:
return CXOperatorKind_PtrMemI;
case BO_Mul:
return CXOperatorKind_Mul;
case BO_Div:
return CXOperatorKind_Div;
case BO_Rem:
return CXOperatorKind_Rem;
case BO_Add:
return CXOperatorKind_Add;
case BO_Sub:
return CXOperatorKind_Sub;
case BO_Shl:
return CXOperatorKind_Shl;
case BO_Shr:
return CXOperatorKind_Shr;
case BO_LT:
return CXOperatorKind_LT;
case BO_GT:
return CXOperatorKind_GT;
case BO_LE:
return CXOperatorKind_LE;
case BO_GE:
return CXOperatorKind_GE;
case BO_EQ:
return CXOperatorKind_EQ;
case BO_NE:
return CXOperatorKind_NE;
case BO_And:
return CXOperatorKind_And;
case BO_Xor:
return CXOperatorKind_Xor;
case BO_Or:
return CXOperatorKind_Or;
case BO_LAnd:
return CXOperatorKind_LAnd;
case BO_LOr:
return CXOperatorKind_LOr;
case BO_Assign:
return CXOperatorKind_Assign;
case BO_MulAssign:
return CXOperatorKind_MulAssign;
case BO_DivAssign:
return CXOperatorKind_DivAssign;
case BO_RemAssign:
return CXOperatorKind_RemAssign;
case BO_AddAssign:
return CXOperatorKind_AddAssign;
case BO_SubAssign:
return CXOperatorKind_SubAssign;
case BO_ShlAssign:
return CXOperatorKind_ShlAssign;
case BO_ShrAssign:
return CXOperatorKind_ShrAssign;
case BO_AndAssign:
return CXOperatorKind_AndAssign;
case BO_XorAssign:
return CXOperatorKind_XorAssign;
case BO_OrAssign:
return CXOperatorKind_OrAssign;
case BO_Comma:
return CXOperatorKind_Comma;
}
}
else if (C.kind == CXCursor_UnaryOperator) {
const UnaryOperator * UnOp =
static_cast<const UnaryOperator *>(getCursorExpr(C));

switch (UnOp->getOpcode()) {
case UO_PostInc:
return CXOperatorKind_PostInc;
case UO_PostDec:
return CXOperatorKind_PostDec;
case UO_PreInc:
return CXOperatorKind_PreInc;
case UO_PreDec:
return CXOperatorKind_PreDec;
case UO_AddrOf:
return CXOperatorKind_AddrOf;
case UO_Deref:
return CXOperatorKind_Deref;
case UO_Plus:
return CXOperatorKind_Plus;
case UO_Minus:
return CXOperatorKind_Minus;
case UO_Not:
return CXOperatorKind_Not;
case UO_LNot:
return CXOperatorKind_LNot;
case UO_Real:
return CXOperatorKind_Real;
case UO_Imag:
return CXOperatorKind_Imag;
case UO_Extension:
return CXOperatorKind_Extension;
case UO_Coawait:
return CXOperatorKind_Coawait;
}
}

return CXOperatorKind_Null;
}

CXCursor clang_getCursorDefinition(CXCursor C) {
if (clang_isInvalid(C.kind))
return clang_getNullCursor();
Expand Down
1 change: 1 addition & 0 deletions tools/libclang/libclang.exports
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,4 @@ clang_VirtualFileOverlay_create
clang_VirtualFileOverlay_dispose
clang_VirtualFileOverlay_setCaseSensitivity
clang_VirtualFileOverlay_writeToBuffer
clang_getBinaryOperatorKind

0 comments on commit d6cc147

Please sign in to comment.