[clang] 542e780 - [AST] Add a print method to Introspection LocationCall

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 15 14:18:46 PDT 2021


Author: Nathan James
Date: 2021-04-15T22:18:29+01:00
New Revision: 542e7806e61089da833338a2ff467006073cbe8d

URL: https://github.com/llvm/llvm-project/commit/542e7806e61089da833338a2ff467006073cbe8d
DIFF: https://github.com/llvm/llvm-project/commit/542e7806e61089da833338a2ff467006073cbe8d.diff

LOG: [AST] Add a print method to Introspection LocationCall

Add a print method that takes a raw_ostream.
Change LocationCallFormatterCpp::format to call that method.

Reviewed By: steveire

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

Added: 
    

Modified: 
    clang/include/clang/Tooling/NodeIntrospection.h
    clang/lib/Tooling/NodeIntrospection.cpp
    clang/unittests/Introspection/IntrospectionTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Tooling/NodeIntrospection.h b/clang/include/clang/Tooling/NodeIntrospection.h
index 406e17f1351a4..9147c7db6c271 100644
--- a/clang/include/clang/Tooling/NodeIntrospection.h
+++ b/clang/include/clang/Tooling/NodeIntrospection.h
@@ -58,7 +58,8 @@ class LocationCall : public llvm::ThreadSafeRefCountedBase<LocationCall> {
 
 class LocationCallFormatterCpp {
 public:
-  static std::string format(LocationCall *Call);
+  static void print(const LocationCall &Call, llvm::raw_ostream &OS);
+  static std::string format(const LocationCall &Call);
 };
 
 namespace internal {

diff  --git a/clang/lib/Tooling/NodeIntrospection.cpp b/clang/lib/Tooling/NodeIntrospection.cpp
index bb0e6ec412d1f..2ee0b1cae55b0 100644
--- a/clang/lib/Tooling/NodeIntrospection.cpp
+++ b/clang/lib/Tooling/NodeIntrospection.cpp
@@ -13,25 +13,40 @@
 #include "clang/Tooling/NodeIntrospection.h"
 
 #include "clang/AST/AST.h"
+#include "llvm/Support/raw_ostream.h"
 
 namespace clang {
 
 namespace tooling {
 
-std::string LocationCallFormatterCpp::format(LocationCall *Call) {
-  SmallVector<LocationCall *> vec;
-  while (Call) {
-    vec.push_back(Call);
-    Call = Call->on();
+void LocationCallFormatterCpp::print(const LocationCall &Call,
+                                     llvm::raw_ostream &OS) {
+  if (const LocationCall *On = Call.on()) {
+    print(*On, OS);
+    if (On->returnsPointer())
+      OS << "->";
+    else
+      OS << '.';
   }
-  std::string result;
-  for (auto *VecCall : llvm::reverse(llvm::makeArrayRef(vec).drop_front())) {
-    result +=
-        (VecCall->name() + "()" + (VecCall->returnsPointer() ? "->" : "."))
-            .str();
+
+  OS << Call.name();
+  if (Call.args().empty()) {
+    OS << "()";
+    return;
+  }
+  OS << '(' << Call.args().front();
+  for (const std::string &Arg : Call.args().drop_front()) {
+    OS << ", " << Arg;
   }
-  result += (vec.back()->name() + "()").str();
-  return result;
+  OS << ')';
+}
+
+std::string LocationCallFormatterCpp::format(const LocationCall &Call) {
+  std::string Result;
+  llvm::raw_string_ostream OS(Result);
+  print(Call, OS);
+  OS.flush();
+  return Result;
 }
 
 namespace internal {

diff  --git a/clang/unittests/Introspection/IntrospectionTest.cpp b/clang/unittests/Introspection/IntrospectionTest.cpp
index 2bfdcd0a26979..4a684f26a6248 100644
--- a/clang/unittests/Introspection/IntrospectionTest.cpp
+++ b/clang/unittests/Introspection/IntrospectionTest.cpp
@@ -36,9 +36,9 @@ FormatExpected(const MapType &Accessors) {
                                           }),
                   std::inserter(Result, Result.end()),
                   [](const auto &Accessor) {
-                    return std::make_pair(
-                        LocationCallFormatterCpp::format(Accessor.second.get()),
-                        Accessor.first);
+                    return std::make_pair(LocationCallFormatterCpp::format(
+                                              *Accessor.second.get()),
+                                          Accessor.first);
                   });
   return Result;
 }


        


More information about the cfe-commits mailing list