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

2017-02-13 Thread Eric Liu via Phabricator via cfe-commits
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=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 Inplace("i", cl::desc("Inplace edit s, if specified."),
   cl::cat(ChangeNamespaceCategory));
 
+cl::opt
+DumpYAML("dump_result",
+ cl::desc("Dump new file contents in YAML, if specified."),
+ cl::cat(ChangeNamespaceCategory));
+
 cl::opt 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  : Files) {
+  std::set ChangedFiles;
+  for (const auto  : Tool.getReplacements())
+ChangedFiles.insert(it.first);
+
+  if (DumpYAML) {
+auto WriteToYAML = [&](llvm::raw_ostream ) {
+  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  : 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 , 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) &&


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 Inplace("i", cl::desc("Inplace edit s, if specified."),
   cl::cat(ChangeNamespaceCategory));
 
+cl::opt
+DumpYAML("dump_result",
+ cl::desc("Dump new file contents in YAML, if specified."),
+ cl::cat(ChangeNamespaceCategory));
+
 cl::opt 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  : Files) {
+  std::set ChangedFiles;
+  for (const auto  : Tool.getReplacements())
+ChangedFiles.insert(it.first);
+
+  if (DumpYAML) {
+auto WriteToYAML = [&](llvm::raw_ostream ) {
+  OS << "[\n";
+   

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

2017-02-13 Thread Eric Liu via Phabricator via cfe-commits
ioeric added inline comments.



Comment at: change-namespace/tool/ClangChangeNamespace.cpp:68
+cl::opt
+DumpYAML("dump_yaml",
+ cl::desc("Dump new file content in YAML, if specified."),

hokein wrote:
> hokein wrote:
> > `dump_result` maybe a clearer name, which also is consistent with 
> > clang-move's.
> Nit: Also change the variable name to `DumpResult`.
Since I am also dumping output as plain text, I named this `DumpYAML` for 
clarity.


https://reviews.llvm.org/D29893



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2017-02-13 Thread Haojian Wu via Phabricator via cfe-commits
hokein accepted this revision.
hokein added inline comments.
This revision is now accepted and ready to land.



Comment at: change-namespace/tool/ClangChangeNamespace.cpp:68
+cl::opt
+DumpYAML("dump_yaml",
+ cl::desc("Dump new file content in YAML, if specified."),

hokein wrote:
> `dump_result` maybe a clearer name, which also is consistent with 
> clang-move's.
Nit: Also change the variable name to `DumpResult`.



Comment at: change-namespace/tool/ClangChangeNamespace.cpp:137
+
+  for (const auto  : ChangedFiles) {
 const auto *Entry = FileMgr.getFile(File);

ioeric wrote:
> hokein wrote:
> > Is this duplicated as we already have "dump_results" option now?
> I'd like this to be default since this produces readable results.
OK, it is up to you.


https://reviews.llvm.org/D29893



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2017-02-13 Thread Eric Liu via Phabricator via cfe-commits
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 Inplace("i", cl::desc("Inplace edit s, if specified."),
   cl::cat(ChangeNamespaceCategory));
 
+cl::opt
+DumpYAML("dump_result",
+ cl::desc("Dump new file contents in YAML, if specified."),
+ cl::cat(ChangeNamespaceCategory));
+
 cl::opt 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  : Files) {
+  std::set ChangedFiles;
+  for (const auto  : Tool.getReplacements())
+ChangedFiles.insert(it.first);
+
+  if (DumpYAML) {
+auto WriteToYAML = [&](llvm::raw_ostream ) {
+  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  : 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 , 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) &&


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 Inplace("i", cl::desc("Inplace edit s, if specified."),
   cl::cat(ChangeNamespaceCategory));
 
+cl::opt
+DumpYAML("dump_result",
+ cl::desc("Dump new file contents in YAML, if specified."),
+ cl::cat(ChangeNamespaceCategory));
+
 cl::opt 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  : Files) {
+  std::set ChangedFiles;
+  for (const auto  : Tool.getReplacements())
+ChangedFiles.insert(it.first);
+
+  if (DumpYAML) {
+auto WriteToYAML = [&](llvm::raw_ostream ) {
+  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);
+

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

2017-02-13 Thread Eric Liu via Phabricator via cfe-commits
ioeric added inline comments.



Comment at: change-namespace/tool/ClangChangeNamespace.cpp:137
+
+  for (const auto  : ChangedFiles) {
 const auto *Entry = FileMgr.getFile(File);

hokein wrote:
> Is this duplicated as we already have "dump_results" option now?
I'd like this to be default since this produces readable results.


https://reviews.llvm.org/D29893



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2017-02-13 Thread Haojian Wu via Phabricator via cfe-commits
hokein added inline comments.



Comment at: change-namespace/tool/ClangChangeNamespace.cpp:68
+cl::opt
+DumpYAML("dump_yaml",
+ cl::desc("Dump new file content in YAML, if specified."),

`dump_result` maybe a clearer name, which also is consistent with clang-move's.



Comment at: change-namespace/tool/ClangChangeNamespace.cpp:137
+
+  for (const auto  : ChangedFiles) {
 const auto *Entry = FileMgr.getFile(File);

Is this duplicated as we already have "dump_results" option now?


https://reviews.llvm.org/D29893



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2017-02-13 Thread Eric Liu via Phabricator via cfe-commits
ioeric created this revision.
Herald added a subscriber: fhahn.

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 Inplace("i", cl::desc("Inplace edit s, if specified."),
   cl::cat(ChangeNamespaceCategory));
 
+cl::opt
+DumpYAML("dump_yaml",
+ cl::desc("Dump new file content in YAML, if specified."),
+ cl::cat(ChangeNamespaceCategory));
+
 cl::opt 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  : Files) {
+  std::set ChangedFiles;
+  for (const auto  : Tool.getReplacements())
+ChangedFiles.insert(it.first);
+
+  if (DumpYAML) {
+auto WriteToYAML = [&](llvm::raw_ostream ) {
+  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  : 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 , 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) &&


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 Inplace("i", cl::desc("Inplace edit s, if specified."),
   cl::cat(ChangeNamespaceCategory));
 
+cl::opt
+DumpYAML("dump_yaml",
+ cl::desc("Dump new file content in YAML, if specified."),
+ cl::cat(ChangeNamespaceCategory));
+
 cl::opt 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  : Files) {
+  std::set ChangedFiles;
+  for (const auto  : Tool.getReplacements())
+ChangedFiles.insert(it.first);
+
+  if (DumpYAML) {
+auto WriteToYAML = [&](llvm::raw_ostream ) {
+  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\": \""
+   <<