[PATCH] D100423: [AST] Add a print method to Introspection LocationCall

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 13 16:13:04 PDT 2021


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

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D100423

Files:
  clang/include/clang/Tooling/NodeIntrospection.h
  clang/lib/Tooling/NodeIntrospection.cpp


Index: clang/lib/Tooling/NodeIntrospection.cpp
===================================================================
--- clang/lib/Tooling/NodeIntrospection.cpp
+++ clang/lib/Tooling/NodeIntrospection.cpp
@@ -13,25 +13,39 @@
 #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 LocationCall::print(llvm::raw_ostream &OS) const {
+  if (const LocationCall *On = on()) {
+    On->print(OS);
+    if (On->returnsPointer())
+      OS << "->";
+    else
+      OS << '.';
+  }
+
+  OS << name();
+  if (args().empty()) {
+    OS << "()";
+    return;
   }
-  std::string result;
-  for (auto *VecCall : llvm::reverse(llvm::makeArrayRef(vec).drop_front())) {
-    result +=
-        (VecCall->name() + "()" + (VecCall->returnsPointer() ? "->" : "."))
-            .str();
+  OS << '(' << args().front();
+  for (const std::string &Arg : args().drop_front()) {
+    OS << ", " << Arg;
   }
-  result += (vec.back()->name() + "()").str();
-  return result;
+  OS << ')';
+}
+
+std::string LocationCallFormatterCpp::format(LocationCall *Call) {
+  std::string Result;
+  llvm::raw_string_ostream OS(Result);
+  Call->print(OS);
+  OS.flush();
+  return Result;
 }
 
 namespace internal {
Index: clang/include/clang/Tooling/NodeIntrospection.h
===================================================================
--- clang/include/clang/Tooling/NodeIntrospection.h
+++ clang/include/clang/Tooling/NodeIntrospection.h
@@ -39,9 +39,10 @@
 
   LocationCall *on() const { return m_on.get(); }
   StringRef name() const { return m_name; }
-  std::vector<std::string> const &args() const { return m_args; }
+  ArrayRef<std::string> args() const { return m_args; }
   bool returnsPointer() const { return m_flags & ReturnsPointer; }
   bool isCast() const { return m_flags & IsCast; }
+  void print(llvm::raw_ostream &OS) const;
 
 private:
   std::shared_ptr<LocationCall> m_on;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100423.337283.patch
Type: text/x-patch
Size: 2146 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210413/adc7ebb4/attachment.bin>


More information about the cfe-commits mailing list