steveire created this revision.
steveire added a reviewer: njames93.
steveire requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Also fix the formatter to use the front element of a chained call
instead of the back element.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D100548

Files:
  clang/lib/Tooling/NodeIntrospection.cpp
  clang/unittests/Introspection/IntrospectionTest.cpp


Index: clang/unittests/Introspection/IntrospectionTest.cpp
===================================================================
--- clang/unittests/Introspection/IntrospectionTest.cpp
+++ clang/unittests/Introspection/IntrospectionTest.cpp
@@ -45,6 +45,31 @@
 
 #define STRING_LOCATION_PAIR(INSTANCE, LOC) Pair(#LOC, INSTANCE->LOC)
 
+TEST(Introspection, SourceLocations_CallContainer) {
+  SourceLocationMap slm;
+  SharedLocationCall Prefix;
+  slm.insert(std::make_pair(
+      SourceLocation(),
+      llvm::makeIntrusiveRefCnt<LocationCall>(Prefix, "getSourceRange")));
+  EXPECT_EQ(slm.size(), 1u);
+
+  auto callTypeLoc =
+      llvm::makeIntrusiveRefCnt<LocationCall>(Prefix, "getTypeLoc");
+  slm.insert(std::make_pair(
+      SourceLocation(),
+      llvm::makeIntrusiveRefCnt<LocationCall>(callTypeLoc, "getSourceRange")));
+  EXPECT_EQ(slm.size(), 2u);
+}
+
+TEST(Introspection, SourceLocations_CallChainFormatting) {
+  SharedLocationCall Prefix;
+  auto chainedCall = llvm::makeIntrusiveRefCnt<LocationCall>(
+      llvm::makeIntrusiveRefCnt<LocationCall>(Prefix, "getTypeLoc"),
+      "getSourceRange");
+  EXPECT_EQ(LocationCallFormatterCpp::format(chainedCall.get()),
+            "getTypeLoc().getSourceRange()");
+}
+
 TEST(Introspection, SourceLocations_Stmt) {
   auto AST = buildASTFromCode("void foo() {} void bar() { foo(); }", "foo.cpp",
                               std::make_shared<PCHContainerOperations>());
Index: clang/lib/Tooling/NodeIntrospection.cpp
===================================================================
--- clang/lib/Tooling/NodeIntrospection.cpp
+++ clang/lib/Tooling/NodeIntrospection.cpp
@@ -30,7 +30,7 @@
         (VecCall->name() + "()" + (VecCall->returnsPointer() ? "->" : "."))
             .str();
   }
-  result += (vec.back()->name() + "()").str();
+  result += (vec.front()->name() + "()").str();
   return result;
 }
 
@@ -51,13 +51,15 @@
   else if (LHS.first.getEnd() != RHS.first.getEnd())
     return false;
 
-  return LHS.second->name() < RHS.second->name();
+  return LocationCallFormatterCpp::format(LHS.second.get()) <
+         LocationCallFormatterCpp::format(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 LHS.second->name() < RHS.second->name();
+    return LocationCallFormatterCpp::format(LHS.second.get()) <
+           LocationCallFormatterCpp::format(RHS.second.get());
   return LHS.first < RHS.first;
 }
 } // namespace internal


Index: clang/unittests/Introspection/IntrospectionTest.cpp
===================================================================
--- clang/unittests/Introspection/IntrospectionTest.cpp
+++ clang/unittests/Introspection/IntrospectionTest.cpp
@@ -45,6 +45,31 @@
 
 #define STRING_LOCATION_PAIR(INSTANCE, LOC) Pair(#LOC, INSTANCE->LOC)
 
+TEST(Introspection, SourceLocations_CallContainer) {
+  SourceLocationMap slm;
+  SharedLocationCall Prefix;
+  slm.insert(std::make_pair(
+      SourceLocation(),
+      llvm::makeIntrusiveRefCnt<LocationCall>(Prefix, "getSourceRange")));
+  EXPECT_EQ(slm.size(), 1u);
+
+  auto callTypeLoc =
+      llvm::makeIntrusiveRefCnt<LocationCall>(Prefix, "getTypeLoc");
+  slm.insert(std::make_pair(
+      SourceLocation(),
+      llvm::makeIntrusiveRefCnt<LocationCall>(callTypeLoc, "getSourceRange")));
+  EXPECT_EQ(slm.size(), 2u);
+}
+
+TEST(Introspection, SourceLocations_CallChainFormatting) {
+  SharedLocationCall Prefix;
+  auto chainedCall = llvm::makeIntrusiveRefCnt<LocationCall>(
+      llvm::makeIntrusiveRefCnt<LocationCall>(Prefix, "getTypeLoc"),
+      "getSourceRange");
+  EXPECT_EQ(LocationCallFormatterCpp::format(chainedCall.get()),
+            "getTypeLoc().getSourceRange()");
+}
+
 TEST(Introspection, SourceLocations_Stmt) {
   auto AST = buildASTFromCode("void foo() {} void bar() { foo(); }", "foo.cpp",
                               std::make_shared<PCHContainerOperations>());
Index: clang/lib/Tooling/NodeIntrospection.cpp
===================================================================
--- clang/lib/Tooling/NodeIntrospection.cpp
+++ clang/lib/Tooling/NodeIntrospection.cpp
@@ -30,7 +30,7 @@
         (VecCall->name() + "()" + (VecCall->returnsPointer() ? "->" : "."))
             .str();
   }
-  result += (vec.back()->name() + "()").str();
+  result += (vec.front()->name() + "()").str();
   return result;
 }
 
@@ -51,13 +51,15 @@
   else if (LHS.first.getEnd() != RHS.first.getEnd())
     return false;
 
-  return LHS.second->name() < RHS.second->name();
+  return LocationCallFormatterCpp::format(LHS.second.get()) <
+         LocationCallFormatterCpp::format(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 LHS.second->name() < RHS.second->name();
+    return LocationCallFormatterCpp::format(LHS.second.get()) <
+           LocationCallFormatterCpp::format(RHS.second.get());
   return LHS.first < RHS.first;
 }
 } // namespace internal
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to