diff options
author | Yitzhak Mandelbaum <yitzhakm@google.com> | 2019-04-30 16:48:33 +0000 |
---|---|---|
committer | Yitzhak Mandelbaum <yitzhakm@google.com> | 2019-04-30 16:48:33 +0000 |
commit | f62caab5336554b0c4389a72a5684b98bc63d4b3 (patch) | |
tree | e9ad260b8b0399742744e8db8c04a8168bae40f5 /include | |
parent | 6332c523cd84170619dbeac19f6204f8eb1d668d (diff) |
[LibTooling] Change Transformer's TextGenerator to a partial function.
Summary:
Changes the signature of the TextGenerator std::function to return an Expected<std::string>
instead of std::string to allow for (non-fatal) failures. Previously, we
expected that any failures would be expressed with assertions. However, that's
unfriendly to running the code in servers or other places that don't want their
library calls to crash the program.
Correspondingly, updates Transformer's handling of failures in TextGenerators
and the signature of `ChangeConsumer`.
Reviewers: ilya-biryukov
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D61015
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@359574 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-rw-r--r-- | include/clang/Tooling/Refactoring/Transformer.h | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/include/clang/Tooling/Refactoring/Transformer.h b/include/clang/Tooling/Refactoring/Transformer.h index e99e279360..454a3b821b 100644 --- a/include/clang/Tooling/Refactoring/Transformer.h +++ b/include/clang/Tooling/Refactoring/Transformer.h @@ -44,12 +44,16 @@ enum class NodePart { Name, }; -using TextGenerator = - std::function<std::string(const ast_matchers::MatchFinder::MatchResult &)>; +// Note that \p TextGenerator is allowed to fail, e.g. when trying to access a +// matched node that was not bound. Allowing this to fail simplifies error +// handling for interactive tools like clang-query. +using TextGenerator = std::function<Expected<std::string>( + const ast_matchers::MatchFinder::MatchResult &)>; /// Wraps a string as a TextGenerator. inline TextGenerator text(std::string M) { - return [M](const ast_matchers::MatchFinder::MatchResult &) { return M; }; + return [M](const ast_matchers::MatchFinder::MatchResult &) + -> Expected<std::string> { return M; }; } // Description of a source-code edit, expressed in terms of an AST node. @@ -222,11 +226,13 @@ translateEdits(const ast_matchers::MatchFinder::MatchResult &Result, class Transformer : public ast_matchers::MatchFinder::MatchCallback { public: using ChangeConsumer = - std::function<void(const clang::tooling::AtomicChange &Change)>; + std::function<void(Expected<clang::tooling::AtomicChange> Change)>; - /// \param Consumer Receives each successful rewrite as an \c AtomicChange. - /// Note that clients are responsible for handling the case that independent - /// \c AtomicChanges conflict with each other. + /// \param Consumer Receives each rewrite or error. Will not necessarily be + /// called for each match; for example, if the rewrite is not applicable + /// because of macros, but doesn't fail. Note that clients are responsible + /// for handling the case that independent \c AtomicChanges conflict with each + /// other. Transformer(RewriteRule Rule, ChangeConsumer Consumer) : Rule(std::move(Rule)), Consumer(std::move(Consumer)) {} |