Skip to content

Commit

Permalink
[LibTooling] Add Explanation parameter to makeRule.
Browse files Browse the repository at this point in the history
Summary:
Conceptually, a single-case RewriteRule has a matcher, edit(s) and an (optional)
explanation. `makeRule` previously only took the matcher and edit(s). This
change adds (optional) support for the explanation.

Reviewers: ilya-biryukov

Subscribers: cfe-commits

Tags: #clang

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@361643 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
ymand committed May 24, 2019
1 parent c76fe39 commit cc107a0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
8 changes: 5 additions & 3 deletions include/clang/Tooling/Refactoring/Transformer.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,16 @@ struct RewriteRule {

/// Convenience function for constructing a simple \c RewriteRule.
RewriteRule makeRule(ast_matchers::internal::DynTypedMatcher M,
SmallVector<ASTEdit, 1> Edits);
SmallVector<ASTEdit, 1> Edits,
TextGenerator Explanation = nullptr);

/// Convenience overload of \c makeRule for common case of only one edit.
inline RewriteRule makeRule(ast_matchers::internal::DynTypedMatcher M,
ASTEdit Edit) {
ASTEdit Edit,
TextGenerator Explanation = nullptr) {
SmallVector<ASTEdit, 1> Edits;
Edits.emplace_back(std::move(Edit));
return makeRule(std::move(M), std::move(Edits));
return makeRule(std::move(M), std::move(Edits), std::move(Explanation));
}

/// Applies the first rule whose pattern matches; other rules are ignored.
Expand Down
8 changes: 4 additions & 4 deletions lib/Tooling/Refactoring/Transformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ ASTEdit tooling::change(RangeSelector S, TextGenerator Replacement) {
return E;
}

RewriteRule tooling::makeRule(DynTypedMatcher M,
SmallVector<ASTEdit, 1> Edits) {
return RewriteRule{
{RewriteRule::Case{std::move(M), std::move(Edits), nullptr}}};
RewriteRule tooling::makeRule(DynTypedMatcher M, SmallVector<ASTEdit, 1> Edits,
TextGenerator Explanation) {
return RewriteRule{{RewriteRule::Case{std::move(M), std::move(Edits),
std::move(Explanation)}}};
}

// Determines whether A is a base type of B in the class hierarchy, including
Expand Down
3 changes: 1 addition & 2 deletions unittests/Tooling/TransformerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ static RewriteRule ruleStrlenSize() {
on(expr(hasType(isOrPointsTo(StringType)))
.bind(StringExpr)),
callee(cxxMethodDecl(hasName("c_str")))))),
change(text("REPLACED")));
R.Cases[0].Explanation = text("Use size() method directly on string.");
change(text("REPLACED")), text("Use size() method directly on string."));
return R;
}

Expand Down

0 comments on commit cc107a0

Please sign in to comment.