Author: ioeric Date: Mon Feb 13 11:24:14 2017 New Revision: 294969 URL: http://llvm.org/viewvc/llvm-project?rev=294969&view=rev Log: [change-namespace] add an option to dump changed files in YAML.
Reviewers: hokein Reviewed By: hokein Subscribers: fhahn, cfe-commits Differential Revision: https://reviews.llvm.org/D29893 Modified: clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp clang-tools-extra/trunk/change-namespace/tool/ClangChangeNamespace.cpp Modified: clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp?rev=294969&r1=294968&r2=294969&view=diff ============================================================================== --- clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp (original) +++ clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp Mon Feb 13 11:24:14 2017 @@ -275,7 +275,7 @@ bool isNestedDeclContext(const DeclConte // 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) && Modified: clang-tools-extra/trunk/change-namespace/tool/ClangChangeNamespace.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/change-namespace/tool/ClangChangeNamespace.cpp?rev=294969&r1=294968&r2=294969&view=diff ============================================================================== --- clang-tools-extra/trunk/change-namespace/tool/ClangChangeNamespace.cpp (original) +++ clang-tools-extra/trunk/change-namespace/tool/ClangChangeNamespace.cpp Mon Feb 13 11:24:14 2017 @@ -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<std::string> FilePattern( 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 @@ int main(int argc, const char **argv) { 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; } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits