[PATCH] D29893: [change-namespace] add an option to dump changed files in YAML.

Eric Liu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 13 09:35:51 PST 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL294969: [change-namespace] add an option to dump changed files in YAML. (authored by ioeric).

Changed prior to commit:
  https://reviews.llvm.org/D29893?vs=88208&id=88211#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D29893

Files:
  clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp
  clang-tools-extra/trunk/change-namespace/tool/ClangChangeNamespace.cpp


Index: clang-tools-extra/trunk/change-namespace/tool/ClangChangeNamespace.cpp
===================================================================
--- clang-tools-extra/trunk/change-namespace/tool/ClangChangeNamespace.cpp
+++ clang-tools-extra/trunk/change-namespace/tool/ClangChangeNamespace.cpp
@@ -39,6 +39,7 @@
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/YAMLTraits.h"
 
 using namespace clang;
 using namespace llvm;
@@ -63,6 +64,11 @@
 cl::opt<bool> Inplace("i", cl::desc("Inplace edit <file>s, if specified."),
                       cl::cat(ChangeNamespaceCategory));
 
+cl::opt<bool>
+    DumpYAML("dump_result",
+         cl::desc("Dump new file contents in YAML, if specified."),
+         cl::cat(ChangeNamespaceCategory));
+
 cl::opt<std::string> Style("style",
                            cl::desc("The style name used for reformatting."),
                            cl::init("LLVM"), cl::cat(ChangeNamespaceCategory));
@@ -101,14 +107,41 @@
   if (Inplace)
     return Rewrite.overwriteChangedFiles();
 
-  for (const auto &File : Files) {
+  std::set<llvm::StringRef> ChangedFiles;
+  for (const auto &it : Tool.getReplacements())
+    ChangedFiles.insert(it.first);
+
+  if (DumpYAML) {
+    auto WriteToYAML = [&](llvm::raw_ostream &OS) {
+      OS << "[\n";
+      for (auto I = ChangedFiles.begin(), E = ChangedFiles.end(); I != E; ++I) {
+        OS << "  {\n";
+        OS << "    \"FilePath\": \"" << *I << "\",\n";
+        const auto *Entry = FileMgr.getFile(*I);
+        auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User);
+        std::string Content;
+        llvm::raw_string_ostream ContentStream(Content);
+        Rewrite.getEditBuffer(ID).write(ContentStream);
+        OS << "    \"SourceText\": \""
+           << llvm::yaml::escape(ContentStream.str()) << "\"\n";
+        OS << "  }";
+        if (I != std::prev(E))
+          OS << ",\n";
+      }
+      OS << "\n]\n";
+    };
+    WriteToYAML(llvm::outs());
+    return 0;
+  }
+
+  for (const auto &File : ChangedFiles) {
     const auto *Entry = FileMgr.getFile(File);
 
     auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User);
-    // FIXME: print results in parsable format, e.g. JSON.
     outs() << "============== " << File << " ==============\n";
     Rewrite.getEditBuffer(ID).write(llvm::outs());
     outs() << "\n============================================\n";
   }
+
   return 0;
 }
Index: clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp
===================================================================
--- clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp
+++ clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp
@@ -275,7 +275,7 @@
 // Returns true if \p D is visible at \p Loc with DeclContext \p DeclCtx.
 bool isDeclVisibleAtLocation(const SourceManager &SM, const Decl *D,
                              const DeclContext *DeclCtx, SourceLocation Loc) {
-  SourceLocation DeclLoc = SM.getSpellingLoc(D->getLocation());
+  SourceLocation DeclLoc = SM.getSpellingLoc(D->getLocStart());
   Loc = SM.getSpellingLoc(Loc);
   return SM.isBeforeInTranslationUnit(DeclLoc, Loc) &&
          (SM.getFileID(DeclLoc) == SM.getFileID(Loc) &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29893.88211.patch
Type: text/x-patch
Size: 3282 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170213/87be69f9/attachment.bin>


More information about the cfe-commits mailing list