[PATCH] D149518: [clang][NFC] Optimize clang::ASTNodeKind::isBaseOf

Piotr Zegar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat May 6 05:22:41 PDT 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGc3d16514d061: [clang][NFC] Optimize clang::ASTNodeKind::isBaseOf (authored by PiotrZSL).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149518

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


Index: clang/lib/AST/ASTTypeTraits.cpp
===================================================================
--- clang/lib/AST/ASTTypeTraits.cpp
+++ clang/lib/AST/ASTTypeTraits.cpp
@@ -56,10 +56,23 @@
     {NKI_None, "ObjCProtocolLoc"},
 };
 
+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::isBaseOf(NodeKindId Base, NodeKindId Derived) {
+  if (Base == NKI_None || Derived == NKI_None)
+    return false;
+  while (Derived != Base && Derived != NKI_None) {
+    Derived = AllKindInfo[Derived].ParentId;
+  }
+  return Derived == Base;
+}
+
 bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
                            unsigned *Distance) {
   if (Base == NKI_None || Derived == NKI_None) return false;
@@ -96,7 +109,7 @@
 ASTNodeKind ASTNodeKind::getMostDerivedCommonAncestor(ASTNodeKind Kind1,
                                                       ASTNodeKind Kind2) {
   NodeKindId Parent = Kind1.KindId;
-  while (!isBaseOf(Parent, Kind2.KindId, nullptr) && Parent != NKI_None) {
+  while (!isBaseOf(Parent, Kind2.KindId) && Parent != NKI_None) {
     Parent = AllKindInfo[Parent].ParentId;
   }
   return ASTNodeKind(Parent);
Index: clang/include/clang/AST/ASTTypeTraits.h
===================================================================
--- clang/include/clang/AST/ASTTypeTraits.h
+++ clang/include/clang/AST/ASTTypeTraits.h
@@ -77,10 +77,13 @@
   /// Returns \c true only for the default \c ASTNodeKind()
   constexpr bool isNone() const { return KindId == NKI_None; }
 
+  /// Returns \c true if \c this is a base kind of (or same as) \c Other.
+  bool isBaseOf(ASTNodeKind Other) const;
+
   /// Returns \c true if \c this is a base kind of (or same as) \c Other.
   /// \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 = nullptr) const;
+  bool isBaseOf(ASTNodeKind Other, unsigned *Distance) const;
 
   /// String representation of the kind.
   StringRef asStringRef() const;
@@ -166,6 +169,10 @@
   /// Use getFromNodeKind<T>() to construct the kind.
   constexpr ASTNodeKind(NodeKindId KindId) : KindId(KindId) {}
 
+  /// Returns \c true if \c Base is a base kind of (or same as) \c
+  ///   Derived.
+  static bool isBaseOf(NodeKindId Base, NodeKindId Derived);
+
   /// Returns \c true if \c Base is a base kind of (or same as) \c
   ///   Derived.
   /// \param Distance If non-null, used to return the distance between \c Base


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149518.520073.patch
Type: text/x-patch
Size: 2691 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230506/f9637d95/attachment.bin>


More information about the cfe-commits mailing list