[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 08:57:06 PST 2017


ioeric updated this revision to Diff 88208.
ioeric marked 2 inline comments as done.
ioeric added a comment.

- Addressed review comments.


https://reviews.llvm.org/D29893

Files:
  change-namespace/ChangeNamespace.cpp
  change-namespace/tool/ClangChangeNamespace.cpp


Index: change-namespace/tool/ClangChangeNamespace.cpp
===================================================================
--- change-namespace/tool/ClangChangeNamespace.cpp
+++ 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: change-namespace/ChangeNamespace.cpp
===================================================================
--- change-namespace/ChangeNamespace.cpp
+++ 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.88208.patch
Type: text/x-patch
Size: 3138 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170213/c60474a4/attachment-0001.bin>


More information about the cfe-commits mailing list