diff options
author | Manuel Klimek <klimek@google.com> | 2013-02-05 15:52:21 +0000 |
---|---|---|
committer | Manuel Klimek <klimek@google.com> | 2013-02-05 15:52:21 +0000 |
commit | 0fe1e723b1c20525b56ce68ff6e2b5a7f4cdd9cb (patch) | |
tree | 9e6281fea4424bfec5561d75eef8cb965480e108 /clang-format | |
parent | 4696b4e1b2fc08845434bd8d24807597a3549c00 (diff) |
Adds JSON output for replacements, to simplify tools integration.
Using -output-replacements will now output the replacements instead
of the changed code. This allows easier integration with tools that
need full control over what changed.
The format is an array of objects with the members "offset" (number),
"length" (number) and "replacement_text" (string), for example:
[
{
"offset": 42,
"length": 5,
"replacement_text": " "
},
{
"offset": 105,
"length": 4,
"replacement_text": ""
}
]
git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@174382 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'clang-format')
-rw-r--r-- | clang-format/ClangFormat.cpp | 51 |
1 files changed, 36 insertions, 15 deletions
diff --git a/clang-format/ClangFormat.cpp b/clang-format/ClangFormat.cpp index 937ed0cc..7b3da2d1 100644 --- a/clang-format/ClangFormat.cpp +++ b/clang-format/ClangFormat.cpp @@ -38,6 +38,9 @@ static cl::opt<std::string> Style( static cl::opt<bool> Inplace("i", cl::desc("Inplace edit <file>, if specified.")); +static cl::opt<bool> OutputReplacements( + "output-replacements", cl::desc("Output replacements as JSON.")); + // FIXME: Remove this when styles are configurable through files. static cl::opt<bool> InvertPointerBinding( "invert-pointer-binding", cl::desc("Inverts the side to which */& bind"), @@ -104,23 +107,41 @@ static void format() { Ranges.push_back(CharSourceRange::getCharRange(Start, End)); } tooling::Replacements Replaces = reformat(getStyle(), Lex, Sources, Ranges); - Rewriter Rewrite(Sources, LangOptions()); - tooling::applyAllReplacements(Replaces, Rewrite); - if (Inplace) { - if (Replaces.size() == 0) - return; // Nothing changed, don't touch the file. - - std::string ErrorInfo; - llvm::raw_fd_ostream FileStream(FileName.c_str(), ErrorInfo, - llvm::raw_fd_ostream::F_Binary); - if (!ErrorInfo.empty()) { - llvm::errs() << "Error while writing file: " << ErrorInfo << "\n"; - return; + if (OutputReplacements) { + llvm::outs() << "[\n"; + for (tooling::Replacements::const_iterator I = Replaces.begin(), + E = Replaces.end(); + I != E; ++I) { + if (I != Replaces.begin()) { + llvm::outs() << ",\n"; + } + llvm::outs() << " {\n" + << " \"offset\": " << I->getOffset() << ",\n" + << " \"length\": " << I->getLength() << ",\n" + << " \"replacement_text\": \"" << I->getReplacementText() + << "\"\n" + << " }"; } - Rewrite.getEditBuffer(ID).write(FileStream); - FileStream.flush(); + llvm::outs() << "\n]\n"; } else { - Rewrite.getEditBuffer(ID).write(outs()); + Rewriter Rewrite(Sources, LangOptions()); + tooling::applyAllReplacements(Replaces, Rewrite); + if (Inplace) { + if (Replaces.size() == 0) + return; // Nothing changed, don't touch the file. + + std::string ErrorInfo; + llvm::raw_fd_ostream FileStream(FileName.c_str(), ErrorInfo, + llvm::raw_fd_ostream::F_Binary); + if (!ErrorInfo.empty()) { + llvm::errs() << "Error while writing file: " << ErrorInfo << "\n"; + return; + } + Rewrite.getEditBuffer(ID).write(FileStream); + FileStream.flush(); + } else { + Rewrite.getEditBuffer(ID).write(outs()); + } } } |