[clang] 0cd0eb6 - Add API to retrieve a clade kind from ASTNodeKind

Stephen Kelly via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 19 14:52:17 PST 2021


Author: Stephen Kelly
Date: 2021-01-19T22:51:30Z
New Revision: 0cd0eb6e0a8133ec86d884c1bbc9c3cbd1769c0b

URL: https://github.com/llvm/llvm-project/commit/0cd0eb6e0a8133ec86d884c1bbc9c3cbd1769c0b
DIFF: https://github.com/llvm/llvm-project/commit/0cd0eb6e0a8133ec86d884c1bbc9c3cbd1769c0b.diff

LOG: Add API to retrieve a clade kind from ASTNodeKind

Differential Revision: https://reviews.llvm.org/D94877

Added: 
    

Modified: 
    clang/include/clang/AST/ASTTypeTraits.h
    clang/lib/AST/ASTTypeTraits.cpp
    clang/unittests/AST/ASTTypeTraitsTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/ASTTypeTraits.h b/clang/include/clang/AST/ASTTypeTraits.h
index a91f6b0c1a69..57195a9d6066 100644
--- a/clang/include/clang/AST/ASTTypeTraits.h
+++ b/clang/include/clang/AST/ASTTypeTraits.h
@@ -100,6 +100,8 @@ class ASTNodeKind {
   static ASTNodeKind getMostDerivedCommonAncestor(ASTNodeKind Kind1,
                                                   ASTNodeKind Kind2);
 
+  ASTNodeKind getCladeKind() const;
+
   /// Hooks for using ASTNodeKind as a key in a DenseMap.
   struct DenseMapInfo {
     // ASTNodeKind() is a good empty key because it is represented as a 0.

diff  --git a/clang/lib/AST/ASTTypeTraits.cpp b/clang/lib/AST/ASTTypeTraits.cpp
index 49335ff37c41..b040f9e4cc40 100644
--- a/clang/lib/AST/ASTTypeTraits.cpp
+++ b/clang/lib/AST/ASTTypeTraits.cpp
@@ -63,6 +63,17 @@ bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
   return Derived == Base;
 }
 
+ASTNodeKind ASTNodeKind::getCladeKind() const {
+  NodeKindId LastId = KindId;
+  while (LastId) {
+    NodeKindId ParentId = AllKindInfo[LastId].ParentId;
+    if (ParentId == NKI_None)
+      return LastId;
+    LastId = ParentId;
+  }
+  return NKI_None;
+}
+
 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
 
 ASTNodeKind ASTNodeKind::getMostDerivedType(ASTNodeKind Kind1,

diff  --git a/clang/unittests/AST/ASTTypeTraitsTest.cpp b/clang/unittests/AST/ASTTypeTraitsTest.cpp
index 998488f1f1f9..18e8b8431b4c 100644
--- a/clang/unittests/AST/ASTTypeTraitsTest.cpp
+++ b/clang/unittests/AST/ASTTypeTraitsTest.cpp
@@ -39,6 +39,18 @@ TEST(ASTNodeKind, Bases) {
   EXPECT_TRUE(DNT<Decl>().isSame(DNT<Decl>()));
 }
 
+TEST(DynTypedNode, Clades) {
+  EXPECT_TRUE(DNT<Stmt>().getCladeKind().isSame(DNT<Stmt>()));
+  EXPECT_TRUE(DNT<Decl>().getCladeKind().isSame(DNT<Decl>()));
+
+  EXPECT_TRUE(DNT<CXXMethodDecl>().getCladeKind().isSame(DNT<Decl>()));
+  EXPECT_TRUE(DNT<CXXMemberCallExpr>().getCladeKind().isSame(DNT<Stmt>()));
+
+  EXPECT_FALSE(DNT<CXXMemberCallExpr>().getCladeKind().isSame(DNT<Decl>()));
+
+  EXPECT_TRUE(ASTNodeKind().getCladeKind().isNone());
+}
+
 TEST(ASTNodeKind, BaseDistances) {
   unsigned Distance = 1;
   EXPECT_TRUE(DNT<Expr>().isBaseOf(DNT<Expr>(), &Distance));


        


More information about the cfe-commits mailing list