[PATCH] Add Distance parameter to ASTNodeKind::isBaseOf.

Peter Collingbourne peter at pcc.me.uk
Mon Nov 18 00:43:22 PST 2013


Hi klimek, sbenza,

This will allow the completer to order results by relevance.

http://llvm-reviews.chandlerc.com/D2209

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

Index: include/clang/AST/ASTTypeTraits.h
===================================================================
--- include/clang/AST/ASTTypeTraits.h
+++ include/clang/AST/ASTTypeTraits.h
@@ -57,7 +57,9 @@
   bool isSame(ASTNodeKind Other) const;
 
   /// \brief Returns \c true if \c this is a base kind of (or same as) \c Other.
-  bool isBaseOf(ASTNodeKind Other) const;
+  /// \param Distance If non-null, used to return the distance between \c this
+  /// and \c Other in the class hierarchy.
+  bool isBaseOf(ASTNodeKind Other, unsigned *Distance = 0) const;
 
   /// \brief String representation of the kind.
   StringRef asStringRef() const;
@@ -91,7 +93,9 @@
 
   /// \brief Returns \c true if \c Base is a base kind of (or same as) \c
   ///   Derived.
-  static bool isBaseOf(NodeKindId Base, NodeKindId Derived);
+  /// \param Distance If non-null, used to return the distance between \c Base
+  /// and \c Derived in the class hierarchy.
+  static bool isBaseOf(NodeKindId Base, NodeKindId Derived, unsigned *Distance);
 
   /// \brief Helper meta-function to convert a kind T to its enum value.
   ///
Index: lib/AST/ASTTypeTraits.cpp
===================================================================
--- lib/AST/ASTTypeTraits.cpp
+++ lib/AST/ASTTypeTraits.cpp
@@ -39,18 +39,24 @@
 #include "clang/AST/TypeNodes.def"
 };
 
-bool ASTNodeKind::isBaseOf(ASTNodeKind Other) const {
-  return isBaseOf(KindId, Other.KindId);
+bool ASTNodeKind::isBaseOf(ASTNodeKind Other, unsigned *Distance) const {
+  return isBaseOf(KindId, Other.KindId, Distance);
 }
 
 bool ASTNodeKind::isSame(ASTNodeKind Other) const {
   return KindId != NKI_None && KindId == Other.KindId;
 }
 
-bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived) {
+bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
+                           unsigned *Distance) {
   if (Base == NKI_None || Derived == NKI_None) return false;
-  while (Derived != Base && Derived != NKI_None)
+  unsigned Dist = 0;
+  while (Derived != Base && Derived != NKI_None) {
     Derived = AllKindInfo[Derived].ParentId;
+    ++Dist;
+  }
+  if (Distance)
+    *Distance = Dist;
   return Derived == Base;
 }
 
Index: unittests/AST/ASTTypeTraitsTest.cpp
===================================================================
--- unittests/AST/ASTTypeTraitsTest.cpp
+++ unittests/AST/ASTTypeTraitsTest.cpp
@@ -34,6 +34,19 @@
   EXPECT_TRUE(DNT<Decl>().isSame(DNT<Decl>()));
 }
 
+TEST(ASTNodeKind, BaseDistances) {
+  unsigned Distance = 1;
+  EXPECT_TRUE(DNT<Expr>().isBaseOf(DNT<Expr>(), &Distance));
+  EXPECT_EQ(0u, Distance);
+
+  EXPECT_TRUE(DNT<Stmt>().isBaseOf(DNT<IfStmt>(), &Distance));
+  EXPECT_EQ(1u, Distance);
+
+  Distance = 0;
+  EXPECT_TRUE(DNT<VarDecl>().isBaseOf(DNT<ParmVarDecl>(), &Distance));
+  EXPECT_EQ(1u, Distance);
+}
+
 TEST(ASTNodeKind, SameBase) {
   EXPECT_TRUE(DNT<Expr>().isBaseOf(DNT<CallExpr>()));
   EXPECT_TRUE(DNT<Expr>().isBaseOf(DNT<BinaryOperator>()));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2209.1.patch
Type: text/x-patch
Size: 2964 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131118/8ccf0049/attachment.bin>


More information about the cfe-commits mailing list