Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve BinaryOperator::getOpcode and BinaryOperator::getOpcodeStr via libclang and its python interface #98489

Merged
merged 1 commit into from
Jul 15, 2024

Conversation

thomaswucher
Copy link
Contributor

This is a rework of patch D10833 previously posted on LLVM Phabricator by arthurp in 2015. It allows to retrieve the type of binary operator via libclangs python bindings.

I did clean up the changes, removed unrelated changes and rebased the changeset to the latest main branch. As this is my first contribution to the LLVM project, let me know if any required tests or documentation are missing.

@AaronBallman

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jul 11, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 11, 2024

@llvm/pr-subscribers-clang

Author: None (thomaswucher)

Changes

This is a rework of patch D10833 previously posted on LLVM Phabricator by arthurp in 2015. It allows to retrieve the type of binary operator via libclangs python bindings.

I did clean up the changes, removed unrelated changes and rebased the changeset to the latest main branch. As this is my first contribution to the LLVM project, let me know if any required tests or documentation are missing.

@AaronBallman


Patch is 212.30 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/98489.diff

17 Files Affected:

  • (modified) clang/bindings/python/clang/cindex.py (+66)
  • (modified) clang/bindings/python/tests/cindex/test_cursor.py (+104)
  • (modified) clang/include/clang-c/Index.h (+53)
  • (added) clang/test/Index/binop.cpp (+92)
  • (modified) clang/test/Index/blocks.c (+1-2)
  • (modified) clang/test/Index/c-index-api-loadTU-test.m (+1-1)
  • (modified) clang/test/Index/load-staticassert.cpp (+2-2)
  • (modified) clang/test/Index/nested-binaryoperators.cpp (+1237-724)
  • (modified) clang/test/Index/preamble.c (+1-1)
  • (modified) clang/test/Index/print-type.c (+2-2)
  • (modified) clang/test/Index/print-type.cpp (+1-1)
  • (modified) clang/test/Index/recursive-cxx-member-calls.cpp (+33-33)
  • (modified) clang/test/Index/remap-load.c (+1-1)
  • (modified) clang/test/Index/usrs.m (+1-1)
  • (modified) clang/tools/c-index-test/c-index-test.c (+18)
  • (modified) clang/tools/libclang/CIndex.cpp (+35)
  • (modified) clang/tools/libclang/libclang.map (+2)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index b3d51e4d2a668..e3caed3394c8a 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -1892,6 +1892,18 @@ def availability(self):
 
         return AvailabilityKind.from_id(self._availability)
 
+    @property
+    def binary_operator(self):
+        """
+        Retrieves the opcode if this cursor points to a binary operator
+        :return:
+        """
+
+        if not hasattr(self, '_binopcode'):
+            self._binopcode = conf.lib.clang_Cursor_getBinaryOpcode(self)
+
+        return BinaryOperator.from_id(self._binopcode)
+
     @property
     def access_specifier(self):
         """
@@ -2181,6 +2193,60 @@ def from_cursor_result(res, fn, args):
         res._tu = args[0]._tu
         return res
 
+class BinaryOperator(BaseEnumeration):
+    """
+    Describes the BinaryOperator of a declaration
+    """
+
+    # The unique kind objects, index by id.
+    _kinds = []
+    _name_map = None
+
+    def __nonzero__(self):
+        """ Allows checks of the kind ```if cursor.binary_operator:```"""
+        return self.value != 0
+
+    @property
+    def is_assignment(self):
+        return BinaryOperator.Assign.value <= self.value < BinaryOperator.Comma.value
+
+    def __repr__(self):
+        return 'BinaryOperator.%s' % (self.name,)
+
+BinaryOperator.Invalid = BinaryOperator(0)
+BinaryOperator.PtrMemD = BinaryOperator(1)
+BinaryOperator.PtrMemI = BinaryOperator(2)
+BinaryOperator.Mul = BinaryOperator(3)
+BinaryOperator.Div = BinaryOperator(4)
+BinaryOperator.Rem = BinaryOperator(5)
+BinaryOperator.Add = BinaryOperator(6)
+BinaryOperator.Sub = BinaryOperator(7)
+BinaryOperator.Shl = BinaryOperator(8)
+BinaryOperator.Shr = BinaryOperator(9)
+BinaryOperator.Cmp = BinaryOperator(10)
+BinaryOperator.LT = BinaryOperator(11)
+BinaryOperator.GT = BinaryOperator(12)
+BinaryOperator.LE = BinaryOperator(13)
+BinaryOperator.GE = BinaryOperator(14)
+BinaryOperator.EQ = BinaryOperator(15)
+BinaryOperator.NE = BinaryOperator(16)
+BinaryOperator.And = BinaryOperator(17)
+BinaryOperator.Xor = BinaryOperator(18)
+BinaryOperator.Or = BinaryOperator(19)
+BinaryOperator.LAnd = BinaryOperator(20)
+BinaryOperator.LOr = BinaryOperator(21)
+BinaryOperator.Assign = BinaryOperator(22)
+BinaryOperator.MulAssign = BinaryOperator(23)
+BinaryOperator.DivAssign = BinaryOperator(24)
+BinaryOperator.RemAssign = BinaryOperator(25)
+BinaryOperator.AddAssign = BinaryOperator(26)
+BinaryOperator.SubAssign = BinaryOperator(27)
+BinaryOperator.ShlAssign = BinaryOperator(28)
+BinaryOperator.ShrAssign = BinaryOperator(29)
+BinaryOperator.AndAssign = BinaryOperator(30)
+BinaryOperator.XorAssign = BinaryOperator(31)
+BinaryOperator.OrAssign = BinaryOperator(32)
+BinaryOperator.Comma = BinaryOperator(33)
 
 class StorageClass:
     """
diff --git a/clang/bindings/python/tests/cindex/test_cursor.py b/clang/bindings/python/tests/cindex/test_cursor.py
index 84cd813941844..7476947bde2ea 100644
--- a/clang/bindings/python/tests/cindex/test_cursor.py
+++ b/clang/bindings/python/tests/cindex/test_cursor.py
@@ -13,6 +13,7 @@
 from clang.cindex import TemplateArgumentKind
 from clang.cindex import TranslationUnit
 from clang.cindex import TypeKind
+from clang.cindex import BinaryOperator
 from .util import get_cursor
 from .util import get_cursors
 from .util import get_tu
@@ -54,6 +55,64 @@ class C {
         void foo<-7, float, true>();
     """
 
+kBinops = """\
+struct C {
+   int m;
+ };
+
+ void func(void){
+   int a, b;
+   int C::* p = &C::
+
+   C c;
+   c.*p;
+
+   C* pc;
+   pc->*p;
+
+   a * b;
+   a / b;
+   a % b;
+   a + b;
+   a - b;
+
+   a << b;
+   a >> b;
+
+   a < b;
+   a > b;
+
+   a <= b;
+   a >= b;
+   a == b;
+   a != b;
+
+   a & b;
+   a ^ b;
+   a | b;
+
+   a && b;
+   a || b;
+
+   a = b;
+
+   a *= b;
+   a /= b;
+   a %= b;
+   a += b;
+   a -= b;
+
+   a <<= b;
+   a >>= b;
+
+   a &= b;
+   a ^= b;
+   a |= b;
+   a , b;
+
+ }
+ """
+
 
 class TestCursor(unittest.TestCase):
     def test_get_children(self):
@@ -695,3 +754,48 @@ def test_mangled_name(self):
         self.assertIn(
             foo.mangled_name, ("_Z3fooii", "__Z3fooii", "?foo@@YAHHH", "?foo@@YAHHH@Z")
         )
+
+    def test_binop(self):
+        tu = get_tu(kBinops, lang="cpp")
+
+        operators = {
+            # not exposed yet
+            # ".*" : BinaryOperator.PtrMemD,
+            "->*": BinaryOperator.PtrMemI,
+            "*": BinaryOperator.Mul,
+            "/": BinaryOperator.Div,
+            "%": BinaryOperator.Rem,
+            "+": BinaryOperator.Add,
+            "-": BinaryOperator.Sub,
+            "<<": BinaryOperator.Shl,
+            ">>": BinaryOperator.Shr,
+            # tests do not run in C++2a mode so this operator is not available
+            # "<=>" : BinaryOperator.Cmp,
+            "<": BinaryOperator.LT,
+            ">": BinaryOperator.GT,
+            "<=": BinaryOperator.LE,
+            ">=": BinaryOperator.GE,
+            "==": BinaryOperator.EQ,
+            "!=": BinaryOperator.NE,
+            "&": BinaryOperator.And,
+            "^": BinaryOperator.Xor,
+            "|": BinaryOperator.Or,
+            "&&": BinaryOperator.LAnd,
+            "||": BinaryOperator.LOr,
+            "=": BinaryOperator.Assign,
+            "*=": BinaryOperator.MulAssign,
+            "/=": BinaryOperator.DivAssign,
+            "%=": BinaryOperator.RemAssign,
+            "+=": BinaryOperator.AddAssign,
+            "-=": BinaryOperator.SubAssign,
+            "<<=": BinaryOperator.ShlAssign,
+            ">>=": BinaryOperator.ShrAssign,
+            "&=": BinaryOperator.AndAssign,
+            "^=": BinaryOperator.XorAssign,
+            "|=": BinaryOperator.OrAssign,
+            ",": BinaryOperator.Comma,
+        }
+
+        for op, typ in operators.items():
+            c = get_cursor(tu, op)
+            assert c.binary_operator == typ
diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h
index ce2282937f86c..24ed23a628728 100644
--- a/clang/include/clang-c/Index.h
+++ b/clang/include/clang-c/Index.h
@@ -3750,6 +3750,59 @@ enum CX_StorageClass {
   CX_SC_Register
 };
 
+/**
+ * Represents a specific kind of binary operator which can appear at a cursor.
+ */
+enum CX_BinaryOperatorKind {
+  CX_BO_Invalid = 0,
+  CX_BO_PtrMemD = 1,
+  CX_BO_PtrMemI = 2,
+  CX_BO_Mul = 3,
+  CX_BO_Div = 4,
+  CX_BO_Rem = 5,
+  CX_BO_Add = 6,
+  CX_BO_Sub = 7,
+  CX_BO_Shl = 8,
+  CX_BO_Shr = 9,
+  CX_BO_Cmp = 10,
+  CX_BO_LT = 11,
+  CX_BO_GT = 12,
+  CX_BO_LE = 13,
+  CX_BO_GE = 14,
+  CX_BO_EQ = 15,
+  CX_BO_NE = 16,
+  CX_BO_And = 17,
+  CX_BO_Xor = 18,
+  CX_BO_Or = 19,
+  CX_BO_LAnd = 20,
+  CX_BO_LOr = 21,
+  CX_BO_Assign = 22,
+  CX_BO_MulAssign = 23,
+  CX_BO_DivAssign = 24,
+  CX_BO_RemAssign = 25,
+  CX_BO_AddAssign = 26,
+  CX_BO_SubAssign = 27,
+  CX_BO_ShlAssign = 28,
+  CX_BO_ShrAssign = 29,
+  CX_BO_AndAssign = 30,
+  CX_BO_XorAssign = 31,
+  CX_BO_OrAssign = 32,
+  CX_BO_Comma = 33,
+  CX_BO_LAST = CX_BO_Comma
+};
+
+/**
+ * \brief Returns the operator code for the binary operator.
+ */
+CINDEX_LINKAGE enum CX_BinaryOperatorKind
+clang_Cursor_getBinaryOpcode(CXCursor C);
+
+/**
+ * \brief Returns a string containing the spelling of the binary operator.
+ */
+CINDEX_LINKAGE CXString
+clang_Cursor_getBinaryOpcodeStr(enum CX_BinaryOperatorKind Op);
+
 /**
  * Returns the storage class for a function or variable declaration.
  *
diff --git a/clang/test/Index/binop.cpp b/clang/test/Index/binop.cpp
new file mode 100644
index 0000000000000..576fd73cc2abf
--- /dev/null
+++ b/clang/test/Index/binop.cpp
@@ -0,0 +1,92 @@
+// RUN: c-index-test -test-print-binops %s | FileCheck %s
+
+struct C {
+  int m;
+};
+
+void func(void) {
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wunused-value"
+  int a, b;
+  int C::*p = &C::m;
+
+  C c;
+  c.*p;
+
+  C *pc;
+  pc->*p;
+
+  a *b;
+  a / b;
+  a % b;
+  a + b;
+  a - b;
+
+  a << b;
+  a >> b;
+
+  a < b;
+  a > b;
+
+  a <= b;
+  a >= b;
+  a == b;
+  a != b;
+
+  a &b;
+  a ^ b;
+  a | b;
+
+  a &&b;
+  a || b;
+
+  a = b;
+
+  a *= b;
+  a /= b;
+  a %= b;
+  a += b;
+  a -= b;
+
+  a <<= b;
+  a >>= b;
+
+  a &= b;
+  a ^= b;
+  a |= b;
+  a, b;
+#pragma clang diagnostic pop
+}
+
+// CHECK: BinaryOperator=.* BinOp=.* 1
+// CHECK: BinaryOperator=->* BinOp=->* 2
+// CHECK: BinaryOperator=* BinOp=* 3
+// CHECK: BinaryOperator=/ BinOp=/ 4
+// CHECK: BinaryOperator=% BinOp=% 5
+// CHECK: BinaryOperator=+ BinOp=+ 6
+// CHECK: BinaryOperator=- BinOp=- 7
+// CHECK: BinaryOperator=<< BinOp=<< 8
+// CHECK: BinaryOperator=>> BinOp=>> 9
+// CHECK: BinaryOperator=< BinOp=< 11
+// CHECK: BinaryOperator=> BinOp=> 12
+// CHECK: BinaryOperator=<= BinOp=<= 13
+// CHECK: BinaryOperator=>= BinOp=>= 14
+// CHECK: BinaryOperator=== BinOp=== 15
+// CHECK: BinaryOperator=!= BinOp=!= 16
+// CHECK: BinaryOperator=& BinOp=& 17
+// CHECK: BinaryOperator=^ BinOp=^ 18
+// CHECK: BinaryOperator=| BinOp=| 19
+// CHECK: BinaryOperator=&& BinOp=&& 20
+// CHECK: BinaryOperator=|| BinOp=|| 21
+// CHECK: BinaryOperator== BinOp== 22
+// CHECK: CompoundAssignOperator=*= BinOp=*= 23
+// CHECK: CompoundAssignOperator=/= BinOp=/= 24
+// CHECK: CompoundAssignOperator=%= BinOp=%= 25
+// CHECK: CompoundAssignOperator=+= BinOp=+= 26
+// CHECK: CompoundAssignOperator=-= BinOp=-= 27
+// CHECK: CompoundAssignOperator=<<= BinOp=<<= 28
+// CHECK: CompoundAssignOperator=>>= BinOp=>>= 29
+// CHECK: CompoundAssignOperator=&= BinOp=&= 30
+// CHECK: CompoundAssignOperator=^= BinOp=^= 31
+// CHECK: CompoundAssignOperator=|= BinOp=|= 32
+// CHECK: BinaryOperator=, BinOp=, 33
diff --git a/clang/test/Index/blocks.c b/clang/test/Index/blocks.c
index 3f33e48e4ced0..304c7800cb700 100644
--- a/clang/test/Index/blocks.c
+++ b/clang/test/Index/blocks.c
@@ -23,7 +23,7 @@ void test() {
 // CHECK: blocks.c:9:18: TypeRef=struct foo:4:8 Extent=[9:18 - 9:21]
 // CHECK: blocks.c:9:28: CompoundStmt= Extent=[9:28 - 9:58]
 // CHECK: blocks.c:9:30: ReturnStmt= Extent=[9:30 - 9:55]
-// CHECK: blocks.c:9:37: BinaryOperator= Extent=[9:37 - 9:55]
+// CHECK: blocks.c:9:37: BinaryOperator=+ Extent=[9:37 - 9:55]
 // CHECK: blocks.c:9:37: CStyleCastExpr= Extent=[9:37 - 9:51]
 // CHECK: blocks.c:9:38: TypeRef=int_t:3:13 Extent=[9:38 - 9:43]
 // CHECK: blocks.c:9:50: MemberRefExpr=x:4:19 SingleRefName=[9:50 - 9:51] RefName=[9:50 - 9:51] Extent=[9:45 - 9:51]
@@ -31,4 +31,3 @@ void test() {
 // CHECK: blocks.c:9:54: DeclRefExpr=i:8:11 Extent=[9:54 - 9:55]
 // CHECK: blocks.c:9:59: UnaryOperator= Extent=[9:59 - 9:64]
 // CHECK: blocks.c:9:60: DeclRefExpr=_foo:7:21 Extent=[9:60 - 9:64]
-
diff --git a/clang/test/Index/c-index-api-loadTU-test.m b/clang/test/Index/c-index-api-loadTU-test.m
index 7aa8f800e037c..7ec57cf3ab63d 100644
--- a/clang/test/Index/c-index-api-loadTU-test.m
+++ b/clang/test/Index/c-index-api-loadTU-test.m
@@ -130,7 +130,7 @@ @interface TestAttributes()
 // CHECK: c-index-api-loadTU-test.m:50:13: VarDecl=d:50:13 (Definition) Extent=[50:2 - 50:14]
 // CHECK: c-index-api-loadTU-test.m:50:2: TypeRef=id:0:0 Extent=[50:2 - 50:4]
 // CHECK: c-index-api-loadTU-test.m:50:6: ObjCProtocolRef=Proto:25:11 Extent=[50:6 - 50:11]
-// CHECK: c-index-api-loadTU-test.m:51:2: BinaryOperator= Extent=[51:2 - 51:7]
+// CHECK: c-index-api-loadTU-test.m:51:2: BinaryOperator== Extent=[51:2 - 51:7]
 // CHECK: c-index-api-loadTU-test.m:51:2: DeclRefExpr=d:50:13 Extent=[51:2 - 51:3]
 // CHECK: c-index-api-loadTU-test.m:51:6: UnexposedExpr=c:49:12 Extent=[51:6 - 51:7]
 // CHECK: c-index-api-loadTU-test.m:51:6: UnexposedExpr=c:49:12 Extent=[51:6 - 51:7]
diff --git a/clang/test/Index/load-staticassert.cpp b/clang/test/Index/load-staticassert.cpp
index 04e45c2d74714..99f59885eaed5 100644
--- a/clang/test/Index/load-staticassert.cpp
+++ b/clang/test/Index/load-staticassert.cpp
@@ -3,8 +3,8 @@ static_assert(2 + 2 == 4, "Simple maths");
 
 // RUN: c-index-test -test-load-source all -fno-delayed-template-parsing -std=c++11 %s | FileCheck %s
 // CHECK: load-staticassert.cpp:2:1: StaticAssert=:2:1 (Definition) Extent=[2:1 - 2:42]
-// CHECK: load-staticassert.cpp:2:15: BinaryOperator= Extent=[2:15 - 2:25]
-// CHECK: load-staticassert.cpp:2:15: BinaryOperator= Extent=[2:15 - 2:20]
+// CHECK: load-staticassert.cpp:2:15: BinaryOperator=== Extent=[2:15 - 2:25]
+// CHECK: load-staticassert.cpp:2:15: BinaryOperator=+ Extent=[2:15 - 2:20]
 // CHECK: load-staticassert.cpp:2:15: IntegerLiteral= Extent=[2:15 - 2:16]
 // CHECK: load-staticassert.cpp:2:19: IntegerLiteral= Extent=[2:19 - 2:20]
 // CHECK: load-staticassert.cpp:2:24: IntegerLiteral= Extent=[2:24 - 2:25]
diff --git a/clang/test/Index/nested-binaryoperators.cpp b/clang/test/Index/nested-binaryoperators.cpp
index 57adc6b54664a..443e565744a49 100644
--- a/clang/test/Index/nested-binaryoperators.cpp
+++ b/clang/test/Index/nested-binaryoperators.cpp
@@ -169,1815 +169,2328 @@ int foo(uint c) {
 // CHECK: 3:3: ReturnStmt= Extent=[3:3 - 160:52]
 // CHECK: 3:10: UnexposedExpr= Extent=[3:10 - 160:52]
 // CHECK: 3:10: ParenExpr= Extent=[3:10 - 160:52]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 160:51]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 160:19]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 159:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 158:51]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 158:19]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 157:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 156:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 155:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 154:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 153:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 152:51]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 152:19]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 151:51]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 151:19]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 150:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 149:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 148:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 147:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 146:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 145:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 144:51]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 144:19]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 143:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 142:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 141:81]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 141:49]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 141:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 141:19]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 140:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 139:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 138:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 137:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 136:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 135:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 134:81]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 134:49]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 134:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 134:19]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 133:51]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 133:19]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 132:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 131:33]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 130:64]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 130:49]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 130:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 130:19]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 129:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 128:33]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 127:64]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 127:49]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 127:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 127:19]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 126:51]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 126:19]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 125:63]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 125:31]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 125:16]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 124:64]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 124:49]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 124:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 124:19]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 123:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 122:51]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 122:19]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 121:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 120:51]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 120:19]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 119:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 118:36]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 117:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 116:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 115:48]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 115:18]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 114:48]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 114:18]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 113:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 112:62]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 112:32]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 112:18]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 111:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 110:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 109:62]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 109:32]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 109:18]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 108:48]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 108:18]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 107:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 106:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 105:48]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 105:18]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 104:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 103:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 102:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 101:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 100:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 99:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 98:48]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 98:18]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 97:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 96:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 95:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 94:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 93:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 92:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 91:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 90:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 89:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 88:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 87:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 86:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 85:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 84:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 83:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 82:48]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 82:18]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 81:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 80:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 79:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 78:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 77:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 76:48]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 76:18]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 75:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 74:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 73:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 72:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 71:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 70:62]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 70:32]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 70:18]
-// CHECK: 3:11: BinaryOperator= Extent=[3:11 - 69:34]
-// CHECK: 3:11: BinaryOperator= Extent=[3:...
[truncated]

@AaronBallman AaronBallman added the clang:as-a-library libclang and C++ API label Jul 11, 2024
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for picking this back up! Hopefully @Endilll can comment on the python bits, but the changes generally LGTM aside from some very minor nits. One thing that should be added is a release note (in clang/docs/ReleaseNotes.rst so users know about the new functionality.

Precommit CI did find a relevant failure to address though:

FAIL: Clang :: Index/index-concepts.cpp (12051 of 20439)
******************** TEST 'Clang :: Index/index-concepts.cpp' FAILED ********************
Exit Code: 1
Command Output (stdout):
--
# RUN: at line 1
c:\ws\src\build\bin\c-index-test.exe -test-load-source all C:\ws\src\clang\test\Index\index-concepts.cpp -std=gnu++20 -fno-delayed-template-parsing | c:\ws\src\build\bin\filecheck.exe C:\ws\src\clang\test\Index\index-concepts.cpp
# executed command: 'c:\ws\src\build\bin\c-index-test.exe' -test-load-source all 'C:\ws\src\clang\test\Index\index-concepts.cpp' -std=gnu++20 -fno-delayed-template-parsing
# note: command had no output on stdout or stderr
# executed command: 'c:\ws\src\build\bin\filecheck.exe' 'C:\ws\src\clang\test\Index\index-concepts.cpp'
# .---command stderr------------
# | C:\ws\src\clang\test\Index\index-concepts.cpp:44:11: error: CHECK: expected string not found in input
# | // CHECK: index-concepts.cpp:[[@LINE-3]]:29: BinaryOperator= Extent=[[[@LINE-3]]:29 - [[@LINE-3]]:62]
# |           ^
# | <stdin>:494:118: note: scanning from here
# | // CHECK: index-concepts.cpp:40:17: TemplateTypeParameter=T:40:17 (Definition) Extent=[40:11 - 40:18] [access=public]
# |                                                                                                                      ^
# | <stdin>:494:118: note: with "@LINE-3" equal to "41"
# | // CHECK: index-concepts.cpp:40:17: TemplateTypeParameter=T:40:17 (Definition) Extent=[40:11 - 40:18] [access=public]
# |                                                                                                                      ^
# | <stdin>:494:118: note: with "@LINE-3" equal to "41"
# | // CHECK: index-concepts.cpp:40:17: TemplateTypeParameter=T:40:17 (Definition) Extent=[40:11 - 40:18] [access=public]
# |                                                                                                                      ^
# | <stdin>:494:118: note: with "@LINE-3" equal to "41"
# | // CHECK: index-concepts.cpp:40:17: TemplateTypeParameter=T:40:17 (Definition) Extent=[40:11 - 40:18] [access=public]
# |                                                                                                                      ^
# | <stdin>:495:11: note: possible intended match here
# | // CHECK: index-concepts.cpp:41:29: BinaryOperator=&& Extent=[41:29 - 41:62]
# |           ^
# |
# | Input file: <stdin>
# | Check file: C:\ws\src\clang\test\Index\index-concepts.cpp
# |
# | -dump-input=help explains the following input dump.
# |
# | Input was:
# | <<<<<<
# |             .
# |             .
# |             .
# |           489: // CHECK: index-concepts.cpp:38:15: FunctionDecl=sizeFunc:38:15 (Definition) Extent=[38:1 - 38:39]
# |           490: // CHECK: index-concepts.cpp:38:26: CompoundStmt= Extent=[38:26 - 38:39]
# |           491: // CHECK: index-concepts.cpp:38:28: ReturnStmt= Extent=[38:28 - 38:36]
# |           492: // CHECK: index-concepts.cpp:38:35: IntegerLiteral= Extent=[38:35 - 38:36]
# |           493: // CHECK: index-concepts.cpp:41:9: ConceptDecl=ConWithLogicalAnd:41:9 (Definition) Extent=[40:1 - 41:62]
# |           494: // CHECK: index-concepts.cpp:40:17: TemplateTypeParameter=T:40:17 (Definition) Extent=[40:11 - 40:18] [access=public]
# | check:44'0                                                                                                                          X error: no match found
# | check:44'1                                                                                                                            with "@LINE-3" equal to "41"
# | check:44'2                                                                                                                            with "@LINE-3" equal to "41"
# | check:44'3                                                                                                                            with "@LINE-3" equal to "41"
# |           495: // CHECK: index-concepts.cpp:41:29: BinaryOperator=&& Extent=[41:29 - 41:62]
# | check:44'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | check:44'4               ?                                                                   possible intended match
# |           496: // CHECK: index-concepts.cpp:41:29: ConceptSpecializationExpr= Extent=[41:29 - 41:36]
# | check:44'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |           497: // CHECK: index-concepts.cpp:41:29: TemplateRef=Con1:31:9 Extent=[41:29 - 41:33]
# | check:44'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |           498: // CHECK: index-concepts.cpp:41:34: TypeRef=T:40:17 Extent=[41:34 - 41:35]
# | check:44'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |           499: // CHECK: index-concepts.cpp:41:40: BinaryOperator=> Extent=[41:40 - 41:62]
# | check:44'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |           500: // CHECK: index-concepts.cpp:41:40: UnaryExpr= Extent=[41:40 - 41:49]
# | check:44'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |             .
# |             .
# |             .
# | >>>>>>
# `-----------------------------
# error: command failed with exit status: 1
--
********************

clang/bindings/python/clang/cindex.py Outdated Show resolved Hide resolved
clang/tools/libclang/CIndex.cpp Outdated Show resolved Hide resolved
clang/tools/libclang/CIndex.cpp Outdated Show resolved Hide resolved
clang/tools/libclang/CIndex.cpp Show resolved Hide resolved
Copy link
Contributor

@Endilll Endilll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be rebased on top of #95608 once it lands (soon).
CC @DeinAlptraum

@DeinAlptraum
Copy link
Contributor

For a quick overview of the changes you'll need to make after #95608 in the Python bindings:

  • remove _kind, _name_map and __repr__() from the BinaryOperator definition
  • declare all variants of BinaryOperator within the class definition, i.e. Invalid = 0 instead of BinaryOperator.Invalid = BinaryOperator(0)
  • add BinaryOperator to the list of enum kinds tested in clang/bindings/python/tests/cindex/test_enums.py

@thomaswucher
Copy link
Contributor Author

Thanks for all your reviews and comments, very much appreciated! I will update this PR once #95608 has been merged.

Copy link

github-actions bot commented Jul 11, 2024

✅ With the latest revision this PR passed the Python code formatter.

@thomaswucher thomaswucher force-pushed the libclang-add-get-opcode-python branch from 04b1daf to d0e2b83 Compare July 11, 2024 17:55
@Endilll
Copy link
Contributor

Endilll commented Jul 12, 2024

#95608 has been merged.

@thomaswucher thomaswucher force-pushed the libclang-add-get-opcode-python branch from d0e2b83 to 39b7b5a Compare July 12, 2024 10:08
@thomaswucher
Copy link
Contributor Author

Thanks for your note @Endilll! I've rebased my changes to the latest main branch. Could you give this a review? Thanks in advance!

Copy link
Contributor

@Endilll Endilll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python part seems fine to me, but I'd like @DeinAlptraum to confirm that.

Copy link
Contributor

@DeinAlptraum DeinAlptraum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Python changes look good to me, just one part that might need changes.
Can someone approve the CI workflows so we also get the results for the bindings tests to confirm?

clang/bindings/python/clang/cindex.py Show resolved Hide resolved
Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff a4cdd94ed0afe76854f837ce3c49c74e712d721d 39b7b5a6af4fc7801ce98e33f802a2e17c29cd9b --extensions h,c,cpp -- clang/test/Index/binop.cpp clang/include/clang-c/Index.h clang/test/Index/blocks.c clang/test/Index/index-concepts.cpp clang/test/Index/load-staticassert.cpp clang/test/Index/nested-binaryoperators.cpp clang/test/Index/preamble.c clang/test/Index/print-type.c clang/test/Index/print-type.cpp clang/test/Index/recursive-cxx-member-calls.cpp clang/test/Index/remap-load.c clang/tools/c-index-test/c-index-test.c clang/tools/libclang/CIndex.cpp
View the diff from clang-format here.
diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index a301e06ae4..e53e64e49d 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -8986,7 +8986,8 @@ CXString clang_Cursor_getBinaryOpcodeStr(enum CX_BinaryOperatorKind Op) {
     return cxstring::createEmpty();
 
   return cxstring::createDup(
-      // BinaryOperator::getOpcodeStr has no case for CX_BO_Invalid, so subtract 1
+      // BinaryOperator::getOpcodeStr has no case for CX_BO_Invalid, so subtract
+      // 1
       BinaryOperator::getOpcodeStr(static_cast<BinaryOperatorKind>(Op - 1)));
 }
 

…or::getOpcodeStr via libclang and its python interface
@thomaswucher thomaswucher force-pushed the libclang-add-get-opcode-python branch from 39b7b5a to 468cc11 Compare July 12, 2024 12:24
@DeinAlptraum
Copy link
Contributor

DeinAlptraum commented Jul 13, 2024

Wr.t. Python: LGTM!

@thomaswucher
Copy link
Contributor Author

Thank you for your approval, @DeinAlptraum!

@AaronBallman and @Endilll, is there anything else I need to do to get this merged? Would one of you be so kind to approve the remaining GitHub workflows?

Thanks for all your support and quick responses!

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@AaronBallman AaronBallman merged commit a972a39 into llvm:main Jul 15, 2024
6 checks passed
Copy link

@thomaswucher Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

DeinAlptraum added a commit that referenced this pull request Aug 3, 2024
#98489 resurrected an [old patch](https://reviews.llvm.org/D10833) that
was adding new libclang functions. That PR got merged with old `LLVM_13`
symbol versions for new functions. This patch fixes this oversight.
llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 3, 2024
…1820)

llvm#98489 resurrected an [old patch](https://reviews.llvm.org/D10833) that
was adding new libclang functions. That PR got merged with old `LLVM_13`
symbol versions for new functions. This patch fixes this oversight.

(cherry picked from commit 2bae7ae)
tru pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 4, 2024
…1820)

llvm#98489 resurrected an [old patch](https://reviews.llvm.org/D10833) that
was adding new libclang functions. That PR got merged with old `LLVM_13`
symbol versions for new functions. This patch fixes this oversight.

(cherry picked from commit 2bae7ae)
banach-space pushed a commit to banach-space/llvm-project that referenced this pull request Aug 7, 2024
…1820)

llvm#98489 resurrected an [old patch](https://reviews.llvm.org/D10833) that
was adding new libclang functions. That PR got merged with old `LLVM_13`
symbol versions for new functions. This patch fixes this oversight.
kstoimenov pushed a commit to kstoimenov/llvm-project that referenced this pull request Aug 15, 2024
…1820)

llvm#98489 resurrected an [old patch](https://reviews.llvm.org/D10833) that
was adding new libclang functions. That PR got merged with old `LLVM_13`
symbol versions for new functions. This patch fixes this oversight.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:as-a-library libclang and C++ API clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants