[clang-tools-extra] r174382 - Adds JSON output for replacements, to simplify tools integration.

Dmitri Gribenko gribozavr at gmail.com
Tue Feb 5 07:56:56 PST 2013


Hi Manuel,

On Tue, Feb 5, 2013 at 5:52 PM, Manuel Klimek <klimek at google.com> wrote:
> Author: klimek
> Date: Tue Feb  5 09:52:21 2013
> New Revision: 174382
>
> URL: http://llvm.org/viewvc/llvm-project?rev=174382&view=rev
> Log:
> 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": ""
>   }
> ]
>
> Modified:
>     clang-tools-extra/trunk/clang-format/ClangFormat.cpp
>
> Modified: clang-tools-extra/trunk/clang-format/ClangFormat.cpp
> URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-format/ClangFormat.cpp?rev=174382&r1=174381&r2=174382&view=diff
> ==============================================================================
> --- clang-tools-extra/trunk/clang-format/ClangFormat.cpp (original)
> +++ clang-tools-extra/trunk/clang-format/ClangFormat.cpp Tue Feb  5 09:52:21 2013
> @@ -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()

Isn't there some escaping required?

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/



More information about the cfe-commits mailing list