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 
<https://github.com/llvm/llvm-project/issues/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<BinaryOperator>(expr))
+      return static_cast<CXBinaryOperatorKind>(op->getOpcode() + 1);
+
+    if (auto op = dyn_cast<CXXRewrittenBinaryOperator>(expr))
+      return static_cast<CXBinaryOperatorKind>(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("LNot");
+  case CXUnaryOperator_Real:
+    return cxstring::createRef("Real");
+  case CXUnaryOperator_Imag:
+    return cxstring::createRef("Imag");
+  case CXUnaryOperator_Extension:
+    return cxstring::createRef("Extension");
+  case CXUnaryOperator_Coawait:
+    return cxstring::createRef("Coawait");
+  }
+
+  llvm_unreachable("Unhandled CXUnaryOperatorKind");
+}
+
+enum CXUnaryOperatorKind clang_getCursorUnaryOperatorKind(CXCursor cursor) {
+  if (clang_isExpression(cursor.kind)) {
+    auto expr = getCursorExpr(cursor);
+
+    if (auto op = dyn_cast<UnaryOperator>(expr))
+      return static_cast<CXUnaryOperatorKind>(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
===================================================================
--- clang/include/clang-c/Index.h
+++ clang/include/clang-c/Index.h
@@ -6510,6 +6510,144 @@
 CINDEX_LINKAGE unsigned clang_Type_visitFields(CXType T, CXFieldVisitor visitor,
                                                CXClientData client_data);
 
+/**
+ * Describes the kind of binary operators.
+ */
+enum CXBinaryOperatorKind {
+  /** This value describes cursors which are not binary operators. */
+  CXBinaryOperator_Invalid,
+  /** C++ Pointer - to - member operator. */
+  CXBinaryOperator_PtrMemD,
+  /** C++ Pointer - to - member operator. */
+  CXBinaryOperator_PtrMemI,
+  /** Multiplication operator. */
+  CXBinaryOperator_Mul,
+  /** Division operator. */
+  CXBinaryOperator_Div,
+  /** Remainder operator. */
+  CXBinaryOperator_Rem,
+  /** Addition operator. */
+  CXBinaryOperator_Add,
+  /** Subtraction operator. */
+  CXBinaryOperator_Sub,
+  /** Bitwise shift left operator. */
+  CXBinaryOperator_Shl,
+  /** Bitwise shift right operator. */
+  CXBinaryOperator_Shr,
+  /** C++ three-way comparison (spaceship) operator. */
+  CXBinaryOperator_Cmp,
+  /** Less than operator. */
+  CXBinaryOperator_LT,
+  /** Greater than operator. */
+  CXBinaryOperator_GT,
+  /** Less or equal operator. */
+  CXBinaryOperator_LE,
+  /** Greater or equal operator. */
+  CXBinaryOperator_GE,
+  /** Equal operator. */
+  CXBinaryOperator_EQ,
+  /** Not equal operator. */
+  CXBinaryOperator_NE,
+  /** Bitwise AND operator. */
+  CXBinaryOperator_And,
+  /** Bitwise XOR operator. */
+  CXBinaryOperator_Xor,
+  /** Bitwise OR operator. */
+  CXBinaryOperator_Or,
+  /** Logical AND operator. */
+  CXBinaryOperator_LAnd,
+  /** Logical OR operator. */
+  CXBinaryOperator_LOr,
+  /** Assignment operator. */
+  CXBinaryOperator_Assign,
+  /** Multiplication assignment operator. */
+  CXBinaryOperator_MulAssign,
+  /** Division assignment operator. */
+  CXBinaryOperator_DivAssign,
+  /** Remainder assignment operator. */
+  CXBinaryOperator_RemAssign,
+  /** Addition assignment operator. */
+  CXBinaryOperator_AddAssign,
+  /** Subtraction assignment operator. */
+  CXBinaryOperator_SubAssign,
+  /** Bitwise shift left assignment operator. */
+  CXBinaryOperator_ShlAssign,
+  /** Bitwise shift right assignment operator. */
+  CXBinaryOperator_ShrAssign,
+  /** Bitwise AND assignment operator. */
+  CXBinaryOperator_AndAssign,
+  /** Bitwise XOR assignment operator. */
+  CXBinaryOperator_XorAssign,
+  /** Bitwise OR assignment operator. */
+  CXBinaryOperator_OrAssign,
+  /** Comma operator. */
+  CXBinaryOperator_Comma
+};
+
+/**
+ * Retrieve the spelling of a given CXBinaryOperatorKind.
+ */
+CINDEX_LINKAGE CXString
+clang_getBinaryOperatorKindSpelling(enum CXBinaryOperatorKind kind);
+
+/**
+ * Retrieve the binary operator kind of this cursor.
+ *
+ * If this cursor is not a binary operator then returns Invalid.
+ */
+CINDEX_LINKAGE enum CXBinaryOperatorKind
+    clang_getCursorBinaryOperatorKind(CXCursor);
+
+/**
+ * Describes the kind of unary operators.
+ */
+enum CXUnaryOperatorKind {
+  /** This value describes cursors which are not unary operators. */
+  CXUnaryOperator_Invalid,
+  /** Postfix increment operator. */
+  CXUnaryOperator_PostInc,
+  /** Postfix decrement operator. */
+  CXUnaryOperator_PostDec,
+  /** Prefix increment operator. */
+  CXUnaryOperator_PreInc,
+  /** Prefix decrement operator. */
+  CXUnaryOperator_PreDec,
+  /** Address of operator. */
+  CXUnaryOperator_AddrOf,
+  /** Dereference operator. */
+  CXUnaryOperator_Deref,
+  /** Plus operator. */
+  CXUnaryOperator_Plus,
+  /** Minus operator. */
+  CXUnaryOperator_Minus,
+  /** Not operator. */
+  CXUnaryOperator_Not,
+  /** LNot operator. */
+  CXUnaryOperator_LNot,
+  /** "__real expr" operator. */
+  CXUnaryOperator_Real,
+  /** "__imag expr" operator. */
+  CXUnaryOperator_Imag,
+  /** __extension__ marker operator. */
+  CXUnaryOperator_Extension,
+  /** C++ co_await operator. */
+  CXUnaryOperator_Coawait
+};
+
+/**
+ * Retrieve the spelling of a given CXUnaryOperatorKind.
+ */
+CINDEX_LINKAGE CXString
+clang_getUnaryOperatorKindSpelling(enum CXUnaryOperatorKind kind);
+
+/**
+ * Retrieve the unary operator kind of this cursor.
+ *
+ * If this cursor is not a unary operator then returns Invalid.
+ */
+CINDEX_LINKAGE enum CXUnaryOperatorKind
+    clang_getCursorUnaryOperatorKind(CXCursor);
+
 /**
  * @}
  */
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D150910: [libclang] Ad... MineGame159 via Phabricator via cfe-commits

Reply via email to