[AST] Sort introspection results without instantiating other data
authorStephen Kelly <steveire@gmail.com>
Thu, 22 Apr 2021 11:53:52 +0000 (12:53 +0100)
committerStephen Kelly <steveire@gmail.com>
Fri, 23 Apr 2021 15:21:01 +0000 (16:21 +0100)
Avoid string allocation in particular, but also avoid attempting to
impose any particular ordering based on formatted results.

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

clang/lib/Tooling/NodeIntrospection.cpp

index 8e2d446..f01bb1c 100644 (file)
@@ -41,6 +41,23 @@ std::string LocationCallFormatterCpp::format(const LocationCall &Call) {
 }
 
 namespace internal {
+
+static bool locationCallLessThan(const LocationCall *LHS,
+                                 const LocationCall *RHS) {
+  if (!LHS && !RHS)
+    return false;
+  if (LHS && !RHS)
+    return true;
+  if (!LHS && RHS)
+    return false;
+  auto compareResult = LHS->name().compare(RHS->name());
+  if (compareResult < 0)
+    return true;
+  if (compareResult > 0)
+    return false;
+  return locationCallLessThan(LHS->on(), RHS->on());
+}
+
 bool RangeLessThan::operator()(
     std::pair<SourceRange, SharedLocationCall> const &LHS,
     std::pair<SourceRange, SharedLocationCall> const &RHS) const {
@@ -54,15 +71,13 @@ bool RangeLessThan::operator()(
   else if (LHS.first.getEnd() != RHS.first.getEnd())
     return false;
 
-  return LocationCallFormatterCpp::format(*LHS.second) <
-         LocationCallFormatterCpp::format(*RHS.second);
+  return locationCallLessThan(LHS.second.get(), RHS.second.get());
 }
 bool RangeLessThan::operator()(
     std::pair<SourceLocation, SharedLocationCall> const &LHS,
     std::pair<SourceLocation, SharedLocationCall> const &RHS) const {
   if (LHS.first == RHS.first)
-    return LocationCallFormatterCpp::format(*LHS.second) <
-           LocationCallFormatterCpp::format(*RHS.second);
+    return locationCallLessThan(LHS.second.get(), RHS.second.get());
   return LHS.first < RHS.first;
 }
 } // namespace internal