[PATCH] D150910: [libclang] Add CXBinaryOperatorKind and CXUnaryOperatorKind (implements 29138)

2023-05-31 Thread MineGame159 via Phabricator via cfe-commits
MineGame159 added a comment.

ping?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150910/new/

https://reviews.llvm.org/D150910

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150910: [libclang] Add CXBinaryOperatorKind and CXUnaryOperatorKind (implements 29138)

2023-05-18 Thread MineGame159 via Phabricator via cfe-commits
MineGame159 created this revision.
MineGame159 added reviewers: clang, clang-c.
MineGame159 added projects: clang, clang-c.
Herald added a subscriber: arphaman.
Herald added a project: All.
MineGame159 requested review of this revision.
Herald added a subscriber: cfe-commits.

Adds 2 new functions to the C libclang api for retrieving operator kinds for 
binary and unary operators from cursors. Also adds 2 functions for retrieving 
the spelling of the new enums. This differential implements #29138 
.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150910

Files:
  clang/include/clang-c/Index.h
  clang/include/clang/AST/OperationKinds.def
  clang/tools/libclang/CIndex.cpp

Index: clang/tools/libclang/CIndex.cpp
===
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -9602,3 +9602,140 @@
 OS << "--\n";
   }
 }
+
+CXString clang_getBinaryOperatorKindSpelling(enum CXBinaryOperatorKind kind) {
+  switch (kind) {
+  case CXBinaryOperator_Invalid:
+return cxstring::createRef("Invalid");
+  case CXBinaryOperator_PtrMemD:
+return cxstring::createRef("PtrMemD");
+  case CXBinaryOperator_PtrMemI:
+return cxstring::createRef("PtrMemI");
+  case CXBinaryOperator_Mul:
+return cxstring::createRef("Mul");
+  case CXBinaryOperator_Div:
+return cxstring::createRef("Div");
+  case CXBinaryOperator_Rem:
+return cxstring::createRef("Rem");
+  case CXBinaryOperator_Add:
+return cxstring::createRef("Add");
+  case CXBinaryOperator_Sub:
+return cxstring::createRef("Sub");
+  case CXBinaryOperator_Shl:
+return cxstring::createRef("Shl");
+  case CXBinaryOperator_Shr:
+return cxstring::createRef("Shr");
+  case CXBinaryOperator_Cmp:
+return cxstring::createRef("Cmp");
+  case CXBinaryOperator_LT:
+return cxstring::createRef("LT");
+  case CXBinaryOperator_GT:
+return cxstring::createRef("GT");
+  case CXBinaryOperator_LE:
+return cxstring::createRef("LE");
+  case CXBinaryOperator_GE:
+return cxstring::createRef("GE");
+  case CXBinaryOperator_EQ:
+return cxstring::createRef("EQ");
+  case CXBinaryOperator_NE:
+return cxstring::createRef("NE");
+  case CXBinaryOperator_And:
+return cxstring::createRef("And");
+  case CXBinaryOperator_Xor:
+return cxstring::createRef("Xor");
+  case CXBinaryOperator_Or:
+return cxstring::createRef("Or");
+  case CXBinaryOperator_LAnd:
+return cxstring::createRef("LAnd");
+  case CXBinaryOperator_LOr:
+return cxstring::createRef("LOr");
+  case CXBinaryOperator_Assign:
+return cxstring::createRef("Assign");
+  case CXBinaryOperator_MulAssign:
+return cxstring::createRef("MulAssign");
+  case CXBinaryOperator_DivAssign:
+return cxstring::createRef("DivAssign");
+  case CXBinaryOperator_RemAssign:
+return cxstring::createRef("RemAssign");
+  case CXBinaryOperator_AddAssign:
+return cxstring::createRef("AddAssign");
+  case CXBinaryOperator_SubAssign:
+return cxstring::createRef("SubAssign");
+  case CXBinaryOperator_ShlAssign:
+return cxstring::createRef("ShlAssign");
+  case CXBinaryOperator_ShrAssign:
+return cxstring::createRef("ShrAssign");
+  case CXBinaryOperator_AndAssign:
+return cxstring::createRef("AndAssign");
+  case CXBinaryOperator_XorAssign:
+return cxstring::createRef("XorAssign");
+  case CXBinaryOperator_OrAssign:
+return cxstring::createRef("OrAssign");
+  case CXBinaryOperator_Comma:
+return cxstring::createRef("Comma");
+  }
+
+  llvm_unreachable("Unhandled CXBinaryOperatorKind");
+}
+
+enum CXBinaryOperatorKind clang_getCursorBinaryOperatorKind(CXCursor cursor) {
+  if (clang_isExpression(cursor.kind)) {
+auto expr = getCursorExpr(cursor);
+
+if (auto op = dyn_cast(expr))
+  return static_cast(op->getOpcode() + 1);
+
+if (auto op = dyn_cast(expr))
+  return static_cast(op->getOpcode() + 1);
+  }
+
+  return CXBinaryOperator_Invalid;
+}
+
+CXString clang_getUnaryOperatorKindSpelling(enum CXUnaryOperatorKind kind) {
+  switch (kind) {
+  case CXUnaryOperator_Invalid:
+return cxstring::createRef("Invalid");
+  case CXUnaryOperator_PostInc:
+return cxstring::createRef("PostInc");
+  case CXUnaryOperator_PostDec:
+return cxstring::createRef("PostDec");
+  case CXUnaryOperator_PreInc:
+return cxstring::createRef("PreInc");
+  case CXUnaryOperator_PreDec:
+return cxstring::createRef("PreDec");
+  case CXUnaryOperator_AddrOf:
+return cxstring::createRef("AddrOf");
+  case CXUnaryOperator_Deref:
+return cxstring::createRef("Deref");
+  case CXUnaryOperator_Plus:
+return cxstring::createRef("Plus");
+  case CXUnaryOperator_Minus:
+return cxstring::createRef("Minus");
+  case CXUnaryOperator_Not:
+return cxstring::createRef("Not");
+  case CXUnaryOperator_LNot:
+return cxstring::createRef("L

[PATCH] D150910: [libclang] Add CXBinaryOperatorKind and CXUnaryOperatorKind (implements 29138)

2023-06-08 Thread MineGame159 via Phabricator via cfe-commits
MineGame159 updated this revision to Diff 529556.
MineGame159 added a comment.

Implemented requested changes


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150910/new/

https://reviews.llvm.org/D150910

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang-c/Index.h
  clang/include/clang/AST/OperationKinds.def
  clang/tools/libclang/CIndex.cpp
  clang/unittests/libclang/LibclangTest.cpp

Index: clang/unittests/libclang/LibclangTest.cpp
===
--- clang/unittests/libclang/LibclangTest.cpp
+++ clang/unittests/libclang/LibclangTest.cpp
@@ -1138,6 +1138,40 @@
 "class ns1::Class1");
 }
 
+TEST_F(LibclangParseTest, BinaryOperator) {
+  std::string Main = "main.cpp";
+  WriteFile(Main, "int foo() { return 5 + 9; };");
+  ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr,
+   0, TUFlags);
+
+  Traverse([](CXCursor cursor, CXCursor parent) -> CXChildVisitResult {
+if (cursor.kind == CXCursor_BinaryOperator) {
+  EXPECT_EQ(clang_getCursorBinaryOperatorKind(cursor),
+CXBinaryOperator_Add);
+  return CXChildVisit_Break;
+}
+
+return CXChildVisit_Recurse;
+  });
+}
+
+TEST_F(LibclangParseTest, UnaryOperator) {
+  std::string Main = "main.cpp";
+  WriteFile(Main, "int foo() { int a = 5; return a++; };");
+  ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr,
+   0, TUFlags);
+
+  Traverse([](CXCursor cursor, CXCursor parent) -> CXChildVisitResult {
+if (cursor.kind == CXCursor_UnaryOperator) {
+  EXPECT_EQ(clang_getCursorUnaryOperatorKind(cursor),
+CXUnaryOperator_PostInc);
+  return CXChildVisit_Break;
+}
+
+return CXChildVisit_Recurse;
+  });
+}
+
 class LibclangRewriteTest : public LibclangParseTest {
 public:
   CXRewriter Rew = nullptr;
Index: clang/tools/libclang/CIndex.cpp
===
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -23,8 +23,11 @@
 #include "clang-c/FatalErrorHandler.h"
 #include "clang/AST/Attr.h"
 #include "clang/AST/DeclObjCCommon.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
 #include "clang/AST/Mangle.h"
 #include "clang/AST/OpenMPClause.h"
+#include "clang/AST/OperationKinds.h"
 #include "clang/AST/StmtVisitor.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticCategories.h"
@@ -9604,3 +9607,39 @@
 OS << "--\n";
   }
 }
+
+CXString clang_getBinaryOperatorKindSpelling(enum CXBinaryOperatorKind kind) {
+  return cxstring::createRef(
+  BinaryOperator::getOpcodeStr(static_cast(kind - 1)));
+}
+
+enum CXBinaryOperatorKind clang_getCursorBinaryOperatorKind(CXCursor cursor) {
+  if (clang_isExpression(cursor.kind)) {
+const Expr *expr = getCursorExpr(cursor);
+
+if (const BinaryOperator *op = dyn_cast(expr))
+  return static_cast(op->getOpcode() + 1);
+
+if (const CXXRewrittenBinaryOperator *op =
+dyn_cast(expr))
+  return static_cast(op->getOpcode() + 1);
+  }
+
+  return CXBinaryOperator_Invalid;
+}
+
+CXString clang_getUnaryOperatorKindSpelling(enum CXUnaryOperatorKind kind) {
+  return cxstring::createRef(
+  UnaryOperator::getOpcodeStr(static_cast(kind - 1)));
+}
+
+enum CXUnaryOperatorKind clang_getCursorUnaryOperatorKind(CXCursor cursor) {
+  if (clang_isExpression(cursor.kind)) {
+const Expr *expr = getCursorExpr(cursor);
+
+if (const UnaryOperator *op = dyn_cast(expr))
+  return static_cast(op->getOpcode() + 1);
+  }
+
+  return CXUnaryOperator_Invalid;
+}
Index: clang/include/clang/AST/OperationKinds.def
===
--- clang/include/clang/AST/OperationKinds.def
+++ clang/include/clang/AST/OperationKinds.def
@@ -362,8 +362,8 @@
 
 //===- Binary Operations  -===//
 // Operators listed in order of precedence.
-// Note that additions to this should also update the StmtVisitor class and
-// BinaryOperator::getOverloadedOperator.
+// Note that additions to this should also update the StmtVisitor class,
+// BinaryOperator::getOverloadedOperator and CXBinaryOperatorKind enum.
 
 // [C++ 5.5] Pointer-to-member operators.
 BINARY_OPERATION(PtrMemD, ".*")
@@ -415,8 +415,8 @@
 
 
 //===- Unary Operations ---===//
-// Note that additions to this should also update the StmtVisitor class and
-// UnaryOperator::getOverloadedOperator.
+// Note that additions to this should also update the StmtVisitor class,
+// UnaryOperator::getOverloadedOperator and CXUnaryOperatorKind enum.
 
 // [C99 6.5.2.4] Postfix increment and decrement
 UNARY_OPERATION(PostInc, "++")
Index: clang/include/clang-c/Index.h
===

[PATCH] D150910: [libclang] Add CXBinaryOperatorKind and CXUnaryOperatorKind (implements 29138)

2023-06-08 Thread MineGame159 via Phabricator via cfe-commits
MineGame159 added a comment.

I kinda thought the undefined reference error is just something I broke on my 
machine but guess not. Don't really know what can cause it since it can link to 
other functions from there and the function exists.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150910/new/

https://reviews.llvm.org/D150910

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150910: [libclang] Add CXBinaryOperatorKind and CXUnaryOperatorKind (implements 29138)

2023-06-08 Thread MineGame159 via Phabricator via cfe-commits
MineGame159 updated this revision to Diff 529663.
MineGame159 added a comment.

I have added the new functions into the file and now it successfully compiles 
the test but fails when running it.

Also checked and made sure that `CINDEX_VERSION_MINOR` was already incremented 
in LLVM 17.
Another thing I am not sure is which naming convention I should be using. Some 
functions in the C api use `clang_getCursorBinaryOperatorKind` and others 
`clang_Cursor_getBinaryOperatorKind`.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150910/new/

https://reviews.llvm.org/D150910

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang-c/Index.h
  clang/include/clang/AST/OperationKinds.def
  clang/tools/libclang/CIndex.cpp
  clang/tools/libclang/libclang.map
  clang/unittests/libclang/LibclangTest.cpp

Index: clang/unittests/libclang/LibclangTest.cpp
===
--- clang/unittests/libclang/LibclangTest.cpp
+++ clang/unittests/libclang/LibclangTest.cpp
@@ -1138,6 +1138,40 @@
 "class ns1::Class1");
 }
 
+TEST_F(LibclangParseTest, BinaryOperator) {
+  std::string Main = "main.cpp";
+  WriteFile(Main, "int foo() { return 5 + 9; };");
+  ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr,
+   0, TUFlags);
+
+  Traverse([](CXCursor cursor, CXCursor parent) -> CXChildVisitResult {
+if (cursor.kind == CXCursor_BinaryOperator) {
+  EXPECT_EQ(clang_getCursorBinaryOperatorKind(cursor),
+CXBinaryOperator_Add);
+  return CXChildVisit_Break;
+}
+
+return CXChildVisit_Recurse;
+  });
+}
+
+TEST_F(LibclangParseTest, UnaryOperator) {
+  std::string Main = "main.cpp";
+  WriteFile(Main, "int foo() { int a = 5; return a++; };");
+  ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr,
+   0, TUFlags);
+
+  Traverse([](CXCursor cursor, CXCursor parent) -> CXChildVisitResult {
+if (cursor.kind == CXCursor_UnaryOperator) {
+  EXPECT_EQ(clang_getCursorUnaryOperatorKind(cursor),
+CXUnaryOperator_PostInc);
+  return CXChildVisit_Break;
+}
+
+return CXChildVisit_Recurse;
+  });
+}
+
 class LibclangRewriteTest : public LibclangParseTest {
 public:
   CXRewriter Rew = nullptr;
Index: clang/tools/libclang/libclang.map
===
--- clang/tools/libclang/libclang.map
+++ clang/tools/libclang/libclang.map
@@ -422,6 +422,10 @@
   global:
 clang_CXXMethod_isExplicit;
 clang_createIndexWithOptions;
+clang_getBinaryOperatorKindSpelling;
+clang_getCursorBinaryOperatorKind;
+clang_getUnaryOperatorKindSpelling;
+clang_getCursorUnaryOperatorKind;
 };
 
 # Example of how to add a new symbol version entry.  If you do add a new symbol
Index: clang/tools/libclang/CIndex.cpp
===
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -23,8 +23,11 @@
 #include "clang-c/FatalErrorHandler.h"
 #include "clang/AST/Attr.h"
 #include "clang/AST/DeclObjCCommon.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
 #include "clang/AST/Mangle.h"
 #include "clang/AST/OpenMPClause.h"
+#include "clang/AST/OperationKinds.h"
 #include "clang/AST/StmtVisitor.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticCategories.h"
@@ -9604,3 +9607,39 @@
 OS << "--\n";
   }
 }
+
+CXString clang_getBinaryOperatorKindSpelling(enum CXBinaryOperatorKind kind) {
+  return cxstring::createRef(
+  BinaryOperator::getOpcodeStr(static_cast(kind - 1)));
+}
+
+enum CXBinaryOperatorKind clang_getCursorBinaryOperatorKind(CXCursor cursor) {
+  if (clang_isExpression(cursor.kind)) {
+const Expr *expr = getCursorExpr(cursor);
+
+if (const BinaryOperator *op = dyn_cast(expr))
+  return static_cast(op->getOpcode() + 1);
+
+if (const CXXRewrittenBinaryOperator *op =
+dyn_cast(expr))
+  return static_cast(op->getOpcode() + 1);
+  }
+
+  return CXBinaryOperator_Invalid;
+}
+
+CXString clang_getUnaryOperatorKindSpelling(enum CXUnaryOperatorKind kind) {
+  return cxstring::createRef(
+  UnaryOperator::getOpcodeStr(static_cast(kind - 1)));
+}
+
+enum CXUnaryOperatorKind clang_getCursorUnaryOperatorKind(CXCursor cursor) {
+  if (clang_isExpression(cursor.kind)) {
+const Expr *expr = getCursorExpr(cursor);
+
+if (const UnaryOperator *op = dyn_cast(expr))
+  return static_cast(op->getOpcode() + 1);
+  }
+
+  return CXUnaryOperator_Invalid;
+}
Index: clang/include/clang/AST/OperationKinds.def
===
--- clang/include/clang/AST/OperationKinds.def
+++ clang/include/clang/AST/OperationKinds.def
@@ -362,8 +362,8 @@
 
 //===- Binary Operations  -=

[PATCH] D150910: [libclang] Add CXBinaryOperatorKind and CXUnaryOperatorKind (implements 29138)

2023-06-08 Thread MineGame159 via Phabricator via cfe-commits
MineGame159 updated this revision to Diff 529691.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150910/new/

https://reviews.llvm.org/D150910

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang-c/Index.h
  clang/include/clang/AST/OperationKinds.def
  clang/tools/libclang/CIndex.cpp
  clang/tools/libclang/libclang.map
  clang/unittests/libclang/LibclangTest.cpp

Index: clang/unittests/libclang/LibclangTest.cpp
===
--- clang/unittests/libclang/LibclangTest.cpp
+++ clang/unittests/libclang/LibclangTest.cpp
@@ -1138,6 +1138,40 @@
 "class ns1::Class1");
 }
 
+TEST_F(LibclangParseTest, BinaryOperator) {
+  std::string Main = "main.cpp";
+  WriteFile(Main, "int foo() { return 5 + 9; };");
+  ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr,
+   0, TUFlags);
+
+  Traverse([](CXCursor cursor, CXCursor parent) -> CXChildVisitResult {
+if (cursor.kind == CXCursor_BinaryOperator) {
+  EXPECT_EQ(clang_getCursorBinaryOperatorKind(cursor),
+CXBinaryOperator_Add);
+  return CXChildVisit_Break;
+}
+
+return CXChildVisit_Recurse;
+  });
+}
+
+TEST_F(LibclangParseTest, UnaryOperator) {
+  std::string Main = "main.cpp";
+  WriteFile(Main, "int foo() { int a = 5; return a++; };");
+  ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr,
+   0, TUFlags);
+
+  Traverse([](CXCursor cursor, CXCursor parent) -> CXChildVisitResult {
+if (cursor.kind == CXCursor_UnaryOperator) {
+  EXPECT_EQ(clang_getCursorUnaryOperatorKind(cursor),
+CXUnaryOperator_PostInc);
+  return CXChildVisit_Break;
+}
+
+return CXChildVisit_Recurse;
+  });
+}
+
 class LibclangRewriteTest : public LibclangParseTest {
 public:
   CXRewriter Rew = nullptr;
Index: clang/tools/libclang/libclang.map
===
--- clang/tools/libclang/libclang.map
+++ clang/tools/libclang/libclang.map
@@ -422,6 +422,10 @@
   global:
 clang_CXXMethod_isExplicit;
 clang_createIndexWithOptions;
+clang_getBinaryOperatorKindSpelling;
+clang_getCursorBinaryOperatorKind;
+clang_getUnaryOperatorKindSpelling;
+clang_getCursorUnaryOperatorKind;
 };
 
 # Example of how to add a new symbol version entry.  If you do add a new symbol
Index: clang/tools/libclang/CIndex.cpp
===
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -23,8 +23,11 @@
 #include "clang-c/FatalErrorHandler.h"
 #include "clang/AST/Attr.h"
 #include "clang/AST/DeclObjCCommon.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
 #include "clang/AST/Mangle.h"
 #include "clang/AST/OpenMPClause.h"
+#include "clang/AST/OperationKinds.h"
 #include "clang/AST/StmtVisitor.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticCategories.h"
@@ -9604,3 +9607,39 @@
 OS << "--\n";
   }
 }
+
+CXString clang_getBinaryOperatorKindSpelling(enum CXBinaryOperatorKind kind) {
+  return cxstring::createRef(
+  BinaryOperator::getOpcodeStr(static_cast(kind - 1)));
+}
+
+enum CXBinaryOperatorKind clang_getCursorBinaryOperatorKind(CXCursor cursor) {
+  if (clang_isExpression(cursor.kind)) {
+const Expr *expr = getCursorExpr(cursor);
+
+if (const BinaryOperator *op = dyn_cast(expr))
+  return static_cast(op->getOpcode() + 1);
+
+if (const CXXRewrittenBinaryOperator *op =
+dyn_cast(expr))
+  return static_cast(op->getOpcode() + 1);
+  }
+
+  return CXBinaryOperator_Invalid;
+}
+
+CXString clang_getUnaryOperatorKindSpelling(enum CXUnaryOperatorKind kind) {
+  return cxstring::createRef(
+  UnaryOperator::getOpcodeStr(static_cast(kind - 1)));
+}
+
+enum CXUnaryOperatorKind clang_getCursorUnaryOperatorKind(CXCursor cursor) {
+  if (clang_isExpression(cursor.kind)) {
+const Expr *expr = getCursorExpr(cursor);
+
+if (const UnaryOperator *op = dyn_cast(expr))
+  return static_cast(op->getOpcode() + 1);
+  }
+
+  return CXUnaryOperator_Invalid;
+}
Index: clang/include/clang/AST/OperationKinds.def
===
--- clang/include/clang/AST/OperationKinds.def
+++ clang/include/clang/AST/OperationKinds.def
@@ -362,8 +362,8 @@
 
 //===- Binary Operations  -===//
 // Operators listed in order of precedence.
-// Note that additions to this should also update the StmtVisitor class and
-// BinaryOperator::getOverloadedOperator.
+// Note that additions to this should also update the StmtVisitor class,
+// BinaryOperator::getOverloadedOperator and CXBinaryOperatorKind enum.
 
 // [C++ 5.5] Pointer-to-member operators.
 BINARY_OPERATION(PtrMemD, ".*")
@@ -415,8 +415,8 @@
 
 
 //===- Unary

[PATCH] D150910: [libclang] Add CXBinaryOperatorKind and CXUnaryOperatorKind (implements 29138)

2023-06-09 Thread MineGame159 via Phabricator via cfe-commits
MineGame159 updated this revision to Diff 529938.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150910/new/

https://reviews.llvm.org/D150910

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang-c/Index.h
  clang/include/clang/AST/OperationKinds.def
  clang/tools/libclang/CIndex.cpp
  clang/tools/libclang/libclang.map
  clang/unittests/libclang/LibclangTest.cpp

Index: clang/unittests/libclang/LibclangTest.cpp
===
--- clang/unittests/libclang/LibclangTest.cpp
+++ clang/unittests/libclang/LibclangTest.cpp
@@ -1138,6 +1138,40 @@
 "class ns1::Class1");
 }
 
+TEST_F(LibclangParseTest, BinaryOperator) {
+  std::string Main = "main.cpp";
+  WriteFile(Main, "int foo() { return 5 + 9; }");
+  ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr,
+   0, TUFlags);
+
+  Traverse([](CXCursor cursor, CXCursor parent) -> CXChildVisitResult {
+if (cursor.kind == CXCursor_BinaryOperator) {
+  EXPECT_EQ(clang_getCursorBinaryOperatorKind(cursor),
+CXBinaryOperator_Add);
+  return CXChildVisit_Break;
+}
+
+return CXChildVisit_Recurse;
+  });
+}
+
+TEST_F(LibclangParseTest, UnaryOperator) {
+  std::string Main = "main.cpp";
+  WriteFile(Main, "int foo() { int a = 5; return a++; }");
+  ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr,
+   0, TUFlags);
+
+  Traverse([](CXCursor cursor, CXCursor parent) -> CXChildVisitResult {
+if (cursor.kind == CXCursor_UnaryOperator) {
+  EXPECT_EQ(clang_getCursorUnaryOperatorKind(cursor),
+CXUnaryOperator_PostInc);
+  return CXChildVisit_Break;
+}
+
+return CXChildVisit_Recurse;
+  });
+}
+
 class LibclangRewriteTest : public LibclangParseTest {
 public:
   CXRewriter Rew = nullptr;
Index: clang/tools/libclang/libclang.map
===
--- clang/tools/libclang/libclang.map
+++ clang/tools/libclang/libclang.map
@@ -422,6 +422,10 @@
   global:
 clang_CXXMethod_isExplicit;
 clang_createIndexWithOptions;
+clang_getBinaryOperatorKindSpelling;
+clang_getCursorBinaryOperatorKind;
+clang_getUnaryOperatorKindSpelling;
+clang_getCursorUnaryOperatorKind;
 };
 
 # Example of how to add a new symbol version entry.  If you do add a new symbol
Index: clang/tools/libclang/CIndex.cpp
===
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -23,8 +23,11 @@
 #include "clang-c/FatalErrorHandler.h"
 #include "clang/AST/Attr.h"
 #include "clang/AST/DeclObjCCommon.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
 #include "clang/AST/Mangle.h"
 #include "clang/AST/OpenMPClause.h"
+#include "clang/AST/OperationKinds.h"
 #include "clang/AST/StmtVisitor.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticCategories.h"
@@ -9604,3 +9607,38 @@
 OS << "--\n";
   }
 }
+
+CXString clang_getBinaryOperatorKindSpelling(enum CXBinaryOperatorKind kind) {
+  return cxstring::createRef(
+  BinaryOperator::getOpcodeStr(static_cast(kind - 1)));
+}
+
+enum CXBinaryOperatorKind clang_getCursorBinaryOperatorKind(CXCursor cursor) {
+  if (clang_isExpression(cursor.kind)) {
+const Expr *expr = getCursorExpr(cursor);
+
+if (const auto *op = dyn_cast(expr))
+  return static_cast(op->getOpcode() + 1);
+
+if (const auto *op = dyn_cast(expr))
+  return static_cast(op->getOpcode() + 1);
+  }
+
+  return CXBinaryOperator_Invalid;
+}
+
+CXString clang_getUnaryOperatorKindSpelling(enum CXUnaryOperatorKind kind) {
+  return cxstring::createRef(
+  UnaryOperator::getOpcodeStr(static_cast(kind - 1)));
+}
+
+enum CXUnaryOperatorKind clang_getCursorUnaryOperatorKind(CXCursor cursor) {
+  if (clang_isExpression(cursor.kind)) {
+const Expr *expr = getCursorExpr(cursor);
+
+if (const auto *op = dyn_cast(expr))
+  return static_cast(op->getOpcode() + 1);
+  }
+
+  return CXUnaryOperator_Invalid;
+}
Index: clang/include/clang/AST/OperationKinds.def
===
--- clang/include/clang/AST/OperationKinds.def
+++ clang/include/clang/AST/OperationKinds.def
@@ -362,8 +362,8 @@
 
 //===- Binary Operations  -===//
 // Operators listed in order of precedence.
-// Note that additions to this should also update the StmtVisitor class and
-// BinaryOperator::getOverloadedOperator.
+// Note that additions to this should also update the StmtVisitor class,
+// BinaryOperator::getOverloadedOperator and CXBinaryOperatorKind enum.
 
 // [C++ 5.5] Pointer-to-member operators.
 BINARY_OPERATION(PtrMemD, ".*")
@@ -415,8 +415,8 @@
 
 
 //===- Unary Operations 

[PATCH] D150910: [libclang] Add CXBinaryOperatorKind and CXUnaryOperatorKind (implements 29138)

2023-06-09 Thread MineGame159 via Phabricator via cfe-commits
MineGame159 added a comment.

In D150910#4408585 , @aaron.ballman 
wrote:

> LGTM! Do you need me to commit on your behalf? If so, what name and email 
> address would you like me to use for patch attribution?

Yeah, this is my first contribution, thanks. My name can be `MineGame159` and 
email `petulk...@gmail.com`.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150910/new/

https://reviews.llvm.org/D150910

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits