[all-commits] [llvm/llvm-project] fa69b6: [JSON] Add error reporting to fromJSON and ObjectM...

Sam McCall via All-commits all-commits at lists.llvm.org
Wed Sep 23 16:20:51 PDT 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: fa69b608063eecba76fb35d167b063cbfe532c28
      https://github.com/llvm/llvm-project/commit/fa69b608063eecba76fb35d167b063cbfe532c28
  Author: Sam McCall <sam.mccall at gmail.com>
  Date:   2020-09-24 (Thu, 24 Sep 2020)

  Changed paths:
    M clang-tools-extra/clangd/ClangdLSPServer.cpp
    M clang-tools-extra/clangd/ClangdLSPServer.h
    M clang-tools-extra/clangd/Protocol.cpp
    M clang-tools-extra/clangd/Protocol.h
    M clang-tools-extra/clangd/benchmarks/IndexBenchmark.cpp
    M clang-tools-extra/clangd/index/Index.cpp
    M clang-tools-extra/clangd/index/Index.h
    M lldb/source/Utility/StructuredData.cpp
    M llvm/include/llvm/Support/JSON.h
    M llvm/lib/Analysis/TFUtils.cpp
    M llvm/unittests/Support/JSONTest.cpp

  Log Message:
  -----------
  [JSON] Add error reporting to fromJSON and ObjectMapper

Translating between JSON objects and C++ strutctures is common.
>From experience in clangd, fromJSON/ObjectMapper work well and save a lot of
code, but aren't adopted elsewhere at least partly due to total lack of error
reporting beyond "ok"/"bad".

The recently-added error model should be rich enough for most applications.
It requires tracking the path within the root object and reporting local
errors at appropriate places.
To do this, we exploit the fact that the call graph of recursive
parse functions mirror the structure of the JSON itself.
The current path is represented as a linked list of segments, each of which is
on the stack as a parameter. Concretely, fromJSON now looks like:
  bool fromJSON(const Value&, T&, Path);

Beyond the signature change, this is reasonably unobtrusive: building
the path segments is mostly handled by ObjectMapper and the vector<T> fromJSON.
However the root caller of fromJSON must now create a Root object to
store the errors, which is a little clunky.

I've added high-level parse<T>(StringRef) -> Expected<T>, but it's not
general enough to be the primary interface I think (at least, not usable in
clangd).

All existing users (mostly just clangd) are updated in this patch,
making this change backwards-compatible is a bit hairy.

Differential Revision: https://reviews.llvm.org/D88103




More information about the All-commits mailing list