[clang] 332a34c - Revert "[libclang] Expose arguments of clang::annotate"

Vitaly Buka via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 10 15:27:02 PDT 2023


Author: Vitaly Buka
Date: 2023-08-10T15:25:02-07:00
New Revision: 332a34c71e7675ab4e0ebd28b0d2c15302a81a51

URL: https://github.com/llvm/llvm-project/commit/332a34c71e7675ab4e0ebd28b0d2c15302a81a51
DIFF: https://github.com/llvm/llvm-project/commit/332a34c71e7675ab4e0ebd28b0d2c15302a81a51.diff

LOG: Revert "[libclang] Expose arguments of clang::annotate"

Introduced a memory leak.

This reverts commit 5aa06b18940c9b96cbf1c31da6aee3fbb92183ed.

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/tools/libclang/CIndex.cpp
    clang/tools/libclang/CursorVisitor.h
    clang/unittests/libclang/LibclangTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c438db074a19b7..22f7a08ae03c67 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -249,8 +249,6 @@ clang-format
 libclang
 --------
 
-- Exposed arguments of ``clang::annotate``.
-
 Static Analyzer
 ---------------
 

diff  --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index ca9467eb1ac23e..1bdc0bf742a8ce 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -23,7 +23,6 @@
 #include "CursorVisitor.h"
 #include "clang-c/FatalErrorHandler.h"
 #include "clang/AST/Attr.h"
-#include "clang/AST/AttrVisitor.h"
 #include "clang/AST/DeclObjCCommon.h"
 #include "clang/AST/Expr.h"
 #include "clang/AST/ExprCXX.h"
@@ -576,13 +575,6 @@ bool CursorVisitor::VisitChildren(CXCursor Cursor) {
           A->getInterfaceLoc()->getTypeLoc().getBeginLoc(), TU));
   }
 
-  if (clang_isAttribute(Cursor.kind)) {
-    if (const Attr *A = getCursorAttr(Cursor))
-      return Visit(A);
-
-    return false;
-  }
-
   // If pointing inside a macro definition, check if the token is an identifier
   // that was ever defined as a macro. In such a case, create a "pseudo" macro
   // expansion cursor for that token.
@@ -2097,8 +2089,7 @@ class MemberRefVisit : public VisitorJob {
         (SourceLocation::UIntTy)(uintptr_t)data[1]);
   }
 };
-class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void>,
-                       public ConstAttrVisitor<EnqueueVisitor, void> {
+class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> {
   friend class OMPClauseEnqueue;
   VisitorWorkList &WL;
   CXCursor Parent;
@@ -2240,9 +2231,6 @@ class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void>,
   void VisitOMPTargetTeamsDistributeSimdDirective(
       const OMPTargetTeamsDistributeSimdDirective *D);
 
-  // Attributes
-  void VisitAnnotateAttr(const AnnotateAttr *A);
-
 private:
   void AddDeclarationNameInfo(const Stmt *S);
   void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier);
@@ -2254,7 +2242,6 @@ class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void>,
   void AddTypeLoc(TypeSourceInfo *TI);
   void EnqueueChildren(const Stmt *S);
   void EnqueueChildren(const OMPClause *S);
-  void EnqueueChildren(const AnnotateAttr *A);
 };
 } // namespace
 
@@ -2749,20 +2736,6 @@ void EnqueueVisitor::EnqueueChildren(const OMPClause *S) {
   VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
   std::reverse(I, E);
 }
-
-void EnqueueVisitor::EnqueueChildren(const AnnotateAttr *A) {
-  unsigned size = WL.size();
-  for (const Expr *Arg : A->args()) {
-    VisitStmt(Arg);
-  }
-  if (size == WL.size())
-    return;
-  // Now reverse the entries we just added.  This will match the DFS
-  // ordering performed by the worklist.
-  VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
-  std::reverse(I, E);
-}
-
 void EnqueueVisitor::VisitAddrLabelExpr(const AddrLabelExpr *E) {
   WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent));
 }
@@ -3035,7 +3008,7 @@ void EnqueueVisitor::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
   // If the opaque value has a source expression, just transparently
   // visit that.  This is useful for (e.g.) pseudo-object expressions.
   if (Expr *SourceExpr = E->getSourceExpr())
-    return ConstStmtVisitor::Visit(SourceExpr);
+    return Visit(SourceExpr);
 }
 void EnqueueVisitor::VisitLambdaExpr(const LambdaExpr *E) {
   AddStmt(E->getBody());
@@ -3055,7 +3028,7 @@ void EnqueueVisitor::VisitCXXParenListInitExpr(const CXXParenListInitExpr *E) {
 }
 void EnqueueVisitor::VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
   // Treat the expression like its syntactic form.
-  ConstStmtVisitor::Visit(E->getSyntacticForm());
+  Visit(E->getSyntacticForm());
 }
 
 void EnqueueVisitor::VisitOMPExecutableDirective(
@@ -3365,28 +3338,9 @@ void EnqueueVisitor::VisitOMPTargetTeamsDistributeSimdDirective(
   VisitOMPLoopDirective(D);
 }
 
-void EnqueueVisitor::VisitAnnotateAttr(const AnnotateAttr *A) {
-  EnqueueChildren(A);
-}
-
 void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) {
   EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU, RegionOfInterest))
-      .ConstStmtVisitor::Visit(S);
-}
-
-void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Attr *A) {
-  // Parent is the attribute itself when this is indirectly called from
-  // VisitChildren. Because we need to make a CXCursor for A, we need *its*
-  // parent.
-  auto AttrCursor = Parent;
-
-  // Get the attribute's parent as stored in
-  // cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent, CXTranslationUnit
-  // TU)
-  const Decl *AttrParent = static_cast<const Decl *>(AttrCursor.data[1]);
-
-  EnqueueVisitor(WL, MakeCXCursor(A, AttrParent, TU))
-      .ConstAttrVisitor::Visit(A);
+      .Visit(S);
 }
 
 bool CursorVisitor::IsInRegionOfInterest(CXCursor C) {
@@ -3651,22 +3605,6 @@ bool CursorVisitor::Visit(const Stmt *S) {
   return result;
 }
 
-bool CursorVisitor::Visit(const Attr *A) {
-  VisitorWorkList *WL = nullptr;
-  if (!WorkListFreeList.empty()) {
-    WL = WorkListFreeList.back();
-    WL->clear();
-    WorkListFreeList.pop_back();
-  } else {
-    WL = new VisitorWorkList();
-    WorkListCache.push_back(WL);
-  }
-  EnqueueWorkList(*WL, A);
-  bool result = RunVisitorWorkList(*WL);
-  WorkListFreeList.push_back(WL);
-  return result;
-}
-
 namespace {
 typedef SmallVector<SourceRange, 4> RefNamePieces;
 RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr,

diff  --git a/clang/tools/libclang/CursorVisitor.h b/clang/tools/libclang/CursorVisitor.h
index 949b73908c315a..2a9d7a7de168f1 100644
--- a/clang/tools/libclang/CursorVisitor.h
+++ b/clang/tools/libclang/CursorVisitor.h
@@ -276,9 +276,7 @@ class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
   bool IsInRegionOfInterest(CXCursor C);
   bool RunVisitorWorkList(VisitorWorkList &WL);
   void EnqueueWorkList(VisitorWorkList &WL, const Stmt *S);
-  void EnqueueWorkList(VisitorWorkList &WL, const Attr *A);
   LLVM_ATTRIBUTE_NOINLINE bool Visit(const Stmt *S);
-  LLVM_ATTRIBUTE_NOINLINE bool Visit(const Attr *A);
 
 private:
   std::optional<bool> handleDeclForVisitation(const Decl *D);

diff  --git a/clang/unittests/libclang/LibclangTest.cpp b/clang/unittests/libclang/LibclangTest.cpp
index fe403528a7d6ca..295706c9e6ff8b 100644
--- a/clang/unittests/libclang/LibclangTest.cpp
+++ b/clang/unittests/libclang/LibclangTest.cpp
@@ -1246,50 +1246,6 @@ static_assert(true, message);
   EXPECT_EQ(fromCXString(clang_getCursorSpelling(*staticAssertCsr)), "");
 }
 
-TEST_F(LibclangParseTest, ExposesAnnotateArgs) {
-  const char testSource[] = R"cpp(
-[[clang::annotate("category", 42)]]
-void func() {}
-)cpp";
-  std::string fileName = "main.cpp";
-  WriteFile(fileName, testSource);
-
-  const char *Args[] = {"-xc++"};
-  ClangTU = clang_parseTranslationUnit(Index, fileName.c_str(), Args, 1,
-                                       nullptr, 0, TUFlags);
-
-  int attrCount = 0;
-
-  Traverse(
-      [&attrCount](CXCursor cursor, CXCursor parent) -> CXChildVisitResult {
-        if (cursor.kind == CXCursor_AnnotateAttr) {
-          int childCount = 0;
-          clang_visitChildren(
-              cursor,
-              [](CXCursor child, CXCursor,
-                 CXClientData data) -> CXChildVisitResult {
-                int *pcount = static_cast<int *>(data);
-
-                // we only expect one argument here, so bail otherwise
-                EXPECT_EQ(*pcount, 0);
-
-                auto *result = clang_Cursor_Evaluate(child);
-                EXPECT_NE(result, nullptr);
-                EXPECT_EQ(clang_EvalResult_getAsInt(result), 42);
-                ++*pcount;
-
-                return CXChildVisit_Recurse;
-              },
-              &childCount);
-          attrCount++;
-          return CXChildVisit_Continue;
-        }
-        return CXChildVisit_Recurse;
-      });
-
-  EXPECT_EQ(attrCount, 1);
-}
-
 class LibclangRewriteTest : public LibclangParseTest {
 public:
   CXRewriter Rew = nullptr;


        


More information about the cfe-commits mailing list