diff --git a/clang/include/clang/Tooling/Transformer/MatchConsumer.h b/clang/include/clang/Tooling/Transformer/MatchConsumer.h --- a/clang/include/clang/Tooling/Transformer/MatchConsumer.h +++ b/clang/include/clang/Tooling/Transformer/MatchConsumer.h @@ -42,6 +42,7 @@ "Id not bound: " + Id); } +namespace transformer { /// Chooses between the two consumers, based on whether \p ID is bound in the /// match. template @@ -52,6 +53,11 @@ return (Map.find(ID) != Map.end() ? TrueC : FalseC)(Result); }; } +} // namespace transformer + +// DEPRECATED: Temporary alias supporting client migration to the `transformer` +// namespace. +using transformer::ifBound; } // namespace tooling } // namespace clang diff --git a/clang/include/clang/Tooling/Transformer/RangeSelector.h b/clang/include/clang/Tooling/Transformer/RangeSelector.h --- a/clang/include/clang/Tooling/Transformer/RangeSelector.h +++ b/clang/include/clang/Tooling/Transformer/RangeSelector.h @@ -26,6 +26,7 @@ namespace tooling { using RangeSelector = MatchConsumer; +namespace transformer { inline RangeSelector charRange(CharSourceRange R) { return [R](const ast_matchers::MatchFinder::MatchResult &) -> Expected { return R; }; @@ -87,6 +88,24 @@ /// source), if `S` is an expansion, and `S` itself, otherwise. Corresponds to /// `SourceManager::getExpansionRange`. RangeSelector expansion(RangeSelector S); +} // namespace transformer + +// DEPRECATED: These are temporary aliases supporting client migration to the +// `transformer` namespace. +using transformer::after; +using transformer::before; +using transformer::callArgs; +using transformer::charRange; +using transformer::elseBranch; +using transformer::expansion; +using transformer::initListElements; +using transformer::member; +using transformer::name; +using transformer::node; +using transformer::range; +using transformer::statement; +using transformer::statements; + } // namespace tooling } // namespace clang diff --git a/clang/include/clang/Tooling/Transformer/RewriteRule.h b/clang/include/clang/Tooling/Transformer/RewriteRule.h --- a/clang/include/clang/Tooling/Transformer/RewriteRule.h +++ b/clang/include/clang/Tooling/Transformer/RewriteRule.h @@ -32,11 +32,13 @@ namespace tooling { using TextGenerator = MatchConsumer; +namespace transformer { /// Wraps a string as a TextGenerator. inline TextGenerator text(std::string M) { return [M](const ast_matchers::MatchFinder::MatchResult &) -> Expected { return M; }; } +} // namespace transformer // Description of a source-code edit, expressed in terms of an AST node. // Includes: an ID for the (bound) node, a selector for source related to the @@ -127,6 +129,7 @@ static constexpr llvm::StringLiteral RootID = "___root___"; }; +namespace transformer { /// Convenience function for constructing a simple \c RewriteRule. RewriteRule makeRule(ast_matchers::internal::DynTypedMatcher M, SmallVector Edits, @@ -224,6 +227,7 @@ inline ASTEdit remove(RangeSelector S) { return change(std::move(S), text("")); } +} // namespace transformer /// The following three functions are a low-level part of the RewriteRule /// API. We expose them for use in implementing the fixtures that interpret @@ -282,6 +286,17 @@ translateEdits(const ast_matchers::MatchFinder::MatchResult &Result, llvm::ArrayRef Edits); } // namespace detail + +// DEPRECATED: These are temporary aliases supporting client migration to the +// `transformer` namespace. +using transformer::addInclude; +using transformer::applyFirst; +using transformer::change; +using transformer::insertAfter; +using transformer::insertBefore; +using transformer::makeRule; +using transformer::remove; +using transformer::text; } // namespace tooling } // namespace clang diff --git a/clang/include/clang/Tooling/Transformer/Stencil.h b/clang/include/clang/Tooling/Transformer/Stencil.h --- a/clang/include/clang/Tooling/Transformer/Stencil.h +++ b/clang/include/clang/Tooling/Transformer/Stencil.h @@ -134,7 +134,7 @@ }; // Functions for conveniently building stencils. -namespace stencil { +namespace transformer { /// Convenience wrapper for Stencil::cat that can be imported with a using decl. template Stencil cat(Ts &&... Parts) { return Stencil::cat(std::forward(Parts)...); @@ -146,12 +146,6 @@ /// \returns the source corresponding to the selected range. StencilPart selection(RangeSelector Selector); -/// \returns the source corresponding to the identified node. -/// FIXME: Deprecated. Write `selection(node(Id))` instead. -inline StencilPart node(llvm::StringRef Id) { - return selection(tooling::node(Id)); -} - /// Generates the source of the expression bound to \p Id, wrapping it in /// parentheses if it may parse differently depending on context. For example, a /// binary operation is always wrapped, while a variable reference is never @@ -197,6 +191,26 @@ /// /// \returns the string resulting from calling the node's print() method. StencilPart dPrint(llvm::StringRef Id); +} // namespace transformer + +// DEPRECATED: These are temporary aliases supporting client migration to the +// `transformer` namespace. +namespace stencil { +using transformer::access; +using transformer::addressOf; +using transformer::cat; +using transformer::deref; +using transformer::dPrint; +using transformer::expression; +using transformer::ifBound; +using transformer::run; +using transformer::selection; +using transformer::text; +/// \returns the source corresponding to the identified node. +/// FIXME: Deprecated. Write `selection(node(Id))` instead. +inline StencilPart node(llvm::StringRef Id) { + return selection(tooling::node(Id)); +} } // namespace stencil } // namespace tooling } // namespace clang diff --git a/clang/lib/Tooling/Transformer/RangeSelector.cpp b/clang/lib/Tooling/Transformer/RangeSelector.cpp --- a/clang/lib/Tooling/Transformer/RangeSelector.cpp +++ b/clang/lib/Tooling/Transformer/RangeSelector.cpp @@ -104,7 +104,7 @@ return findPreviousTokenKind(EndLoc, SM, LangOpts, tok::TokenKind::l_paren); } -RangeSelector tooling::before(RangeSelector Selector) { +RangeSelector transformer::before(RangeSelector Selector) { return [Selector](const MatchResult &Result) -> Expected { Expected SelectedRange = Selector(Result); if (!SelectedRange) @@ -113,7 +113,7 @@ }; } -RangeSelector tooling::after(RangeSelector Selector) { +RangeSelector transformer::after(RangeSelector Selector) { return [Selector](const MatchResult &Result) -> Expected { Expected SelectedRange = Selector(Result); if (!SelectedRange) @@ -126,7 +126,7 @@ }; } -RangeSelector tooling::node(std::string ID) { +RangeSelector transformer::node(std::string ID) { return [ID](const MatchResult &Result) -> Expected { Expected Node = getNode(Result.Nodes, ID); if (!Node) @@ -137,7 +137,7 @@ }; } -RangeSelector tooling::statement(std::string ID) { +RangeSelector transformer::statement(std::string ID) { return [ID](const MatchResult &Result) -> Expected { Expected Node = getNode(Result.Nodes, ID); if (!Node) @@ -146,7 +146,7 @@ }; } -RangeSelector tooling::range(RangeSelector Begin, RangeSelector End) { +RangeSelector transformer::range(RangeSelector Begin, RangeSelector End) { return [Begin, End](const MatchResult &Result) -> Expected { Expected BeginRange = Begin(Result); if (!BeginRange) @@ -165,11 +165,11 @@ }; } -RangeSelector tooling::range(std::string BeginID, std::string EndID) { - return tooling::range(node(std::move(BeginID)), node(std::move(EndID))); +RangeSelector transformer::range(std::string BeginID, std::string EndID) { + return transformer::range(node(std::move(BeginID)), node(std::move(EndID))); } -RangeSelector tooling::member(std::string ID) { +RangeSelector transformer::member(std::string ID) { return [ID](const MatchResult &Result) -> Expected { Expected Node = getNode(Result.Nodes, ID); if (!Node) @@ -181,7 +181,7 @@ }; } -RangeSelector tooling::name(std::string ID) { +RangeSelector transformer::name(std::string ID) { return [ID](const MatchResult &Result) -> Expected { Expected N = getNode(Result.Nodes, ID); if (!N) @@ -256,7 +256,7 @@ } } // namespace -RangeSelector tooling::statements(std::string ID) { +RangeSelector transformer::statements(std::string ID) { return RelativeSelector(std::move(ID)); } @@ -271,7 +271,7 @@ } } // namespace -RangeSelector tooling::callArgs(std::string ID) { +RangeSelector transformer::callArgs(std::string ID) { return RelativeSelector(std::move(ID)); } @@ -285,7 +285,7 @@ } } // namespace -RangeSelector tooling::initListElements(std::string ID) { +RangeSelector transformer::initListElements(std::string ID) { return RelativeSelector(std::move(ID)); } @@ -298,11 +298,11 @@ } } // namespace -RangeSelector tooling::elseBranch(std::string ID) { +RangeSelector transformer::elseBranch(std::string ID) { return RelativeSelector(std::move(ID)); } -RangeSelector tooling::expansion(RangeSelector S) { +RangeSelector transformer::expansion(RangeSelector S) { return [S](const MatchResult &Result) -> Expected { Expected SRange = S(Result); if (!SRange) diff --git a/clang/lib/Tooling/Transformer/RewriteRule.cpp b/clang/lib/Tooling/Transformer/RewriteRule.cpp --- a/clang/lib/Tooling/Transformer/RewriteRule.cpp +++ b/clang/lib/Tooling/Transformer/RewriteRule.cpp @@ -54,20 +54,20 @@ return Transformations; } -ASTEdit tooling::change(RangeSelector S, TextGenerator Replacement) { +ASTEdit transformer::change(RangeSelector S, TextGenerator Replacement) { ASTEdit E; E.TargetRange = std::move(S); E.Replacement = std::move(Replacement); return E; } -RewriteRule tooling::makeRule(DynTypedMatcher M, SmallVector Edits, +RewriteRule transformer::makeRule(DynTypedMatcher M, SmallVector Edits, TextGenerator Explanation) { return RewriteRule{{RewriteRule::Case{ std::move(M), std::move(Edits), std::move(Explanation), {}}}}; } -void tooling::addInclude(RewriteRule &Rule, StringRef Header, +void transformer::addInclude(RewriteRule &Rule, StringRef Header, IncludeFormat Format) { for (auto &Case : Rule.Cases) Case.AddedIncludes.emplace_back(Header.str(), Format); @@ -103,7 +103,7 @@ // Simply gathers the contents of the various rules into a single rule. The // actual work to combine these into an ordered choice is deferred to matcher // registration. -RewriteRule tooling::applyFirst(ArrayRef Rules) { +RewriteRule transformer::applyFirst(ArrayRef Rules) { RewriteRule R; for (auto &Rule : Rules) R.Cases.append(Rule.Cases.begin(), Rule.Cases.end()); diff --git a/clang/lib/Tooling/Transformer/Stencil.cpp b/clang/lib/Tooling/Transformer/Stencil.cpp --- a/clang/lib/Tooling/Transformer/Stencil.cpp +++ b/clang/lib/Tooling/Transformer/Stencil.cpp @@ -250,11 +250,11 @@ } // namespace StencilPart Stencil::wrap(StringRef Text) { - return stencil::text(Text); + return transformer::text(Text); } StencilPart Stencil::wrap(RangeSelector Selector) { - return stencil::selection(std::move(Selector)); + return transformer::selection(std::move(Selector)); } void Stencil::append(Stencil OtherStencil) { @@ -271,46 +271,46 @@ return Result; } -StencilPart stencil::text(StringRef Text) { +StencilPart transformer::text(StringRef Text) { return StencilPart(std::make_shared>(Text)); } -StencilPart stencil::selection(RangeSelector Selector) { +StencilPart transformer::selection(RangeSelector Selector) { return StencilPart( std::make_shared>(std::move(Selector))); } -StencilPart stencil::dPrint(StringRef Id) { +StencilPart transformer::dPrint(StringRef Id) { return StencilPart(std::make_shared>(Id)); } -StencilPart stencil::expression(llvm::StringRef Id) { +StencilPart transformer::expression(llvm::StringRef Id) { return StencilPart(std::make_shared>( UnaryNodeOperator::Parens, Id)); } -StencilPart stencil::deref(llvm::StringRef ExprId) { +StencilPart transformer::deref(llvm::StringRef ExprId) { return StencilPart(std::make_shared>( UnaryNodeOperator::Deref, ExprId)); } -StencilPart stencil::addressOf(llvm::StringRef ExprId) { +StencilPart transformer::addressOf(llvm::StringRef ExprId) { return StencilPart(std::make_shared>( UnaryNodeOperator::Address, ExprId)); } -StencilPart stencil::access(StringRef BaseId, StencilPart Member) { +StencilPart transformer::access(StringRef BaseId, StencilPart Member) { return StencilPart( std::make_shared>(BaseId, std::move(Member))); } -StencilPart stencil::ifBound(StringRef Id, StencilPart TruePart, +StencilPart transformer::ifBound(StringRef Id, StencilPart TruePart, StencilPart FalsePart) { return StencilPart(std::make_shared>( Id, std::move(TruePart), std::move(FalsePart))); } -StencilPart stencil::run(MatchConsumer Fn) { +StencilPart transformer::run(MatchConsumer Fn) { return StencilPart( std::make_shared>>( std::move(Fn))); diff --git a/clang/unittests/Tooling/StencilTest.cpp b/clang/unittests/Tooling/StencilTest.cpp --- a/clang/unittests/Tooling/StencilTest.cpp +++ b/clang/unittests/Tooling/StencilTest.cpp @@ -17,6 +17,7 @@ using namespace clang; using namespace tooling; +using namespace transformer; using namespace ast_matchers; namespace { @@ -27,15 +28,6 @@ using ::testing::Eq; using ::testing::HasSubstr; using MatchResult = MatchFinder::MatchResult; -using stencil::access; -using stencil::addressOf; -using stencil::cat; -using stencil::deref; -using stencil::dPrint; -using stencil::expression; -using stencil::ifBound; -using stencil::run; -using stencil::text; // Create a valid translation-unit from a statement. static std::string wrapSnippet(StringRef StatementCode) {