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) && 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 &File : Files) { + std::set 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; }