Skip to content

Commit bb4be5c

Browse files
committed
libclang: deprecate duplicate API
Explicitly mark the clang_Cursor_getBinaryOpcode and clang_Cursor_getBinaryOpcodeStr as deprecated and mutualize the implementation, accounting for comments made when implementing the second implementation in the first implementation.
1 parent 1f96788 commit bb4be5c

File tree

5 files changed

+58
-64
lines changed

5 files changed

+58
-64
lines changed

clang/bindings/python/clang/cindex.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1876,7 +1876,7 @@ def binary_operator(self):
18761876
"""
18771877

18781878
if not hasattr(self, "_binopcode"):
1879-
self._binopcode = conf.lib.clang_Cursor_getBinaryOpcode(self)
1879+
self._binopcode = conf.lib.clang_getCursorBinaryOperatorKind(self)
18801880

18811881
return BinaryOperator.from_id(self._binopcode)
18821882

@@ -4043,7 +4043,7 @@ def set_property(self, property, value):
40434043
("clang_Cursor_getTemplateArgumentType", [Cursor, c_uint], Type),
40444044
("clang_Cursor_getTemplateArgumentValue", [Cursor, c_uint], c_longlong),
40454045
("clang_Cursor_getTemplateArgumentUnsignedValue", [Cursor, c_uint], c_ulonglong),
4046-
("clang_Cursor_getBinaryOpcode", [Cursor], c_int),
4046+
("clang_getCursorBinaryOperatorKind", [Cursor], c_int),
40474047
("clang_Cursor_getBriefCommentText", [Cursor], _CXString),
40484048
("clang_Cursor_getRawCommentText", [Cursor], _CXString),
40494049
("clang_Cursor_getOffsetOfField", [Cursor], c_longlong),

clang/docs/ReleaseNotes.rst

+4
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,10 @@ libclang
483483
- Fixed a buffer overflow in ``CXString`` implementation. The fix may result in
484484
increased memory allocation.
485485

486+
- Deprecate ``clang_Cursor_GetBinaryOpcode`` and ``clang_Cursor_getBinaryOpcodeStr``
487+
implementations, which are duplicates of ``clang_getCursorBinaryOperatorKind``
488+
and ``clang_getBinaryOperatorKindSpelling`` respectively.
489+
486490
Code Completion
487491
---------------
488492

clang/include/clang-c/Index.h

+39-34
Original file line numberDiff line numberDiff line change
@@ -3882,12 +3882,16 @@ enum CX_BinaryOperatorKind {
38823882

38833883
/**
38843884
* \brief Returns the operator code for the binary operator.
3885+
*
3886+
* @deprecated: use clang_getCursorBinaryOperatorKind instead.
38853887
*/
38863888
CINDEX_LINKAGE enum CX_BinaryOperatorKind
38873889
clang_Cursor_getBinaryOpcode(CXCursor C);
38883890

38893891
/**
38903892
* \brief Returns a string containing the spelling of the binary operator.
3893+
*
3894+
* @deprecated: use clang_getBinaryOperatorKindSpelling instead
38913895
*/
38923896
CINDEX_LINKAGE CXString
38933897
clang_Cursor_getBinaryOpcodeStr(enum CX_BinaryOperatorKind Op);
@@ -6671,73 +6675,74 @@ CINDEX_LINKAGE unsigned clang_visitCXXMethods(CXType T, CXFieldVisitor visitor,
66716675
*/
66726676
enum CXBinaryOperatorKind {
66736677
/** This value describes cursors which are not binary operators. */
6674-
CXBinaryOperator_Invalid,
6678+
CXBinaryOperator_Invalid = 0,
66756679
/** C++ Pointer - to - member operator. */
6676-
CXBinaryOperator_PtrMemD,
6680+
CXBinaryOperator_PtrMemD = 1,
66776681
/** C++ Pointer - to - member operator. */
6678-
CXBinaryOperator_PtrMemI,
6682+
CXBinaryOperator_PtrMemI = 2,
66796683
/** Multiplication operator. */
6680-
CXBinaryOperator_Mul,
6684+
CXBinaryOperator_Mul = 3,
66816685
/** Division operator. */
6682-
CXBinaryOperator_Div,
6686+
CXBinaryOperator_Div = 4,
66836687
/** Remainder operator. */
6684-
CXBinaryOperator_Rem,
6688+
CXBinaryOperator_Rem = 5,
66856689
/** Addition operator. */
6686-
CXBinaryOperator_Add,
6690+
CXBinaryOperator_Add = 6,
66876691
/** Subtraction operator. */
6688-
CXBinaryOperator_Sub,
6692+
CXBinaryOperator_Sub = 7,
66896693
/** Bitwise shift left operator. */
6690-
CXBinaryOperator_Shl,
6694+
CXBinaryOperator_Shl = 8,
66916695
/** Bitwise shift right operator. */
6692-
CXBinaryOperator_Shr,
6696+
CXBinaryOperator_Shr = 9,
66936697
/** C++ three-way comparison (spaceship) operator. */
6694-
CXBinaryOperator_Cmp,
6698+
CXBinaryOperator_Cmp = 10,
66956699
/** Less than operator. */
6696-
CXBinaryOperator_LT,
6700+
CXBinaryOperator_LT = 11,
66976701
/** Greater than operator. */
6698-
CXBinaryOperator_GT,
6702+
CXBinaryOperator_GT = 12,
66996703
/** Less or equal operator. */
6700-
CXBinaryOperator_LE,
6704+
CXBinaryOperator_LE = 13,
67016705
/** Greater or equal operator. */
6702-
CXBinaryOperator_GE,
6706+
CXBinaryOperator_GE = 14,
67036707
/** Equal operator. */
6704-
CXBinaryOperator_EQ,
6708+
CXBinaryOperator_EQ = 15,
67056709
/** Not equal operator. */
6706-
CXBinaryOperator_NE,
6710+
CXBinaryOperator_NE = 16,
67076711
/** Bitwise AND operator. */
6708-
CXBinaryOperator_And,
6712+
CXBinaryOperator_And = 17,
67096713
/** Bitwise XOR operator. */
6710-
CXBinaryOperator_Xor,
6714+
CXBinaryOperator_Xor = 18,
67116715
/** Bitwise OR operator. */
6712-
CXBinaryOperator_Or,
6716+
CXBinaryOperator_Or = 19,
67136717
/** Logical AND operator. */
6714-
CXBinaryOperator_LAnd,
6718+
CXBinaryOperator_LAnd = 20,
67156719
/** Logical OR operator. */
6716-
CXBinaryOperator_LOr,
6720+
CXBinaryOperator_LOr = 21,
67176721
/** Assignment operator. */
6718-
CXBinaryOperator_Assign,
6722+
CXBinaryOperator_Assign = 22,
67196723
/** Multiplication assignment operator. */
6720-
CXBinaryOperator_MulAssign,
6724+
CXBinaryOperator_MulAssign = 23,
67216725
/** Division assignment operator. */
6722-
CXBinaryOperator_DivAssign,
6726+
CXBinaryOperator_DivAssign = 24,
67236727
/** Remainder assignment operator. */
6724-
CXBinaryOperator_RemAssign,
6728+
CXBinaryOperator_RemAssign = 25,
67256729
/** Addition assignment operator. */
6726-
CXBinaryOperator_AddAssign,
6730+
CXBinaryOperator_AddAssign = 26,
67276731
/** Subtraction assignment operator. */
6728-
CXBinaryOperator_SubAssign,
6732+
CXBinaryOperator_SubAssign = 27,
67296733
/** Bitwise shift left assignment operator. */
6730-
CXBinaryOperator_ShlAssign,
6734+
CXBinaryOperator_ShlAssign = 28,
67316735
/** Bitwise shift right assignment operator. */
6732-
CXBinaryOperator_ShrAssign,
6736+
CXBinaryOperator_ShrAssign = 29,
67336737
/** Bitwise AND assignment operator. */
6734-
CXBinaryOperator_AndAssign,
6738+
CXBinaryOperator_AndAssign = 30,
67356739
/** Bitwise XOR assignment operator. */
6736-
CXBinaryOperator_XorAssign,
6740+
CXBinaryOperator_XorAssign = 31,
67376741
/** Bitwise OR assignment operator. */
6738-
CXBinaryOperator_OrAssign,
6742+
CXBinaryOperator_OrAssign = 32,
67396743
/** Comma operator. */
6740-
CXBinaryOperator_Comma
6744+
CXBinaryOperator_Comma = 33,
6745+
CXBinaryOperator_Last = CXBinaryOperator_Comma
67416746
};
67426747

67436748
/**

clang/tools/c-index-test/c-index-test.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1863,14 +1863,14 @@ static enum CXChildVisitResult PrintTypeSize(CXCursor cursor, CXCursor p,
18631863
static enum CXChildVisitResult PrintBinOps(CXCursor C, CXCursor p,
18641864
CXClientData d) {
18651865
enum CXCursorKind ck = clang_getCursorKind(C);
1866-
enum CX_BinaryOperatorKind bok;
1866+
enum CXBinaryOperatorKind bok;
18671867
CXString opstr;
18681868
if (ck != CXCursor_BinaryOperator && ck != CXCursor_CompoundAssignOperator)
18691869
return CXChildVisit_Recurse;
18701870

18711871
PrintCursor(C, NULL);
1872-
bok = clang_Cursor_getBinaryOpcode(C);
1873-
opstr = clang_Cursor_getBinaryOpcodeStr(bok);
1872+
bok = clang_getCursorBinaryOperatorKind(C);
1873+
opstr = clang_getBinaryOperatorKindSpelling(bok);
18741874
printf(" BinOp=%s %d\n", clang_getCString(opstr), bok);
18751875
clang_disposeString(opstr);
18761876
return CXChildVisit_Recurse;

clang/tools/libclang/CIndex.cpp

+10-25
Original file line numberDiff line numberDiff line change
@@ -5442,7 +5442,8 @@ CXString clang_getCursorSpelling(CXCursor C) {
54425442

54435443
if (C.kind == CXCursor_BinaryOperator ||
54445444
C.kind == CXCursor_CompoundAssignOperator) {
5445-
return clang_Cursor_getBinaryOpcodeStr(clang_Cursor_getBinaryOpcode(C));
5445+
return clang_getBinaryOperatorKindSpelling(
5446+
clang_getCursorBinaryOperatorKind(C));
54465447
}
54475448

54485449
const Decl *D = getDeclFromExpr(getCursorExpr(C));
@@ -9211,32 +9212,13 @@ unsigned clang_Cursor_isExternalSymbol(CXCursor C, CXString *language,
92119212
}
92129213

92139214
enum CX_BinaryOperatorKind clang_Cursor_getBinaryOpcode(CXCursor C) {
9214-
if (C.kind != CXCursor_BinaryOperator &&
9215-
C.kind != CXCursor_CompoundAssignOperator) {
9216-
return CX_BO_Invalid;
9217-
}
9218-
9219-
const Expr *D = getCursorExpr(C);
9220-
if (const auto *BinOp = dyn_cast<BinaryOperator>(D)) {
9221-
switch (BinOp->getOpcode()) {
9222-
#define BINARY_OPERATION(Name, Spelling) \
9223-
case BO_##Name: \
9224-
return CX_BO_##Name;
9225-
#include "clang/AST/OperationKinds.def"
9226-
}
9227-
}
9228-
9229-
return CX_BO_Invalid;
9215+
return static_cast<CX_BinaryOperatorKind>(
9216+
clang_getCursorBinaryOperatorKind(C));
92309217
}
92319218

92329219
CXString clang_Cursor_getBinaryOpcodeStr(enum CX_BinaryOperatorKind Op) {
9233-
if (Op > CX_BO_LAST)
9234-
return cxstring::createEmpty();
9235-
9236-
return cxstring::createDup(
9237-
// BinaryOperator::getOpcodeStr has no case for CX_BO_Invalid,
9238-
// so subtract 1
9239-
BinaryOperator::getOpcodeStr(static_cast<BinaryOperatorKind>(Op - 1)));
9220+
return clang_getBinaryOperatorKindSpelling(
9221+
static_cast<CXBinaryOperatorKind>(Op));
92409222
}
92419223

92429224
CXSourceRange clang_Cursor_getCommentRange(CXCursor C) {
@@ -10111,7 +10093,10 @@ cxindex::Logger::~Logger() {
1011110093
}
1011210094

1011310095
CXString clang_getBinaryOperatorKindSpelling(enum CXBinaryOperatorKind kind) {
10114-
return cxstring::createRef(
10096+
if (kind > CXBinaryOperator_Last)
10097+
return cxstring::createEmpty();
10098+
10099+
return cxstring::createDup(
1011510100
BinaryOperator::getOpcodeStr(static_cast<BinaryOperatorKind>(kind - 1)));
1011610101
}
1011710102

0 commit comments

Comments
 (0)