Author: ymandel Date: Thu May 23 10:11:33 2019 New Revision: 361514 URL: http://llvm.org/viewvc/llvm-project?rev=361514&view=rev Log: [LibTooling] Fix dangling references in RangeSelector.
Summary: RangeSelector had a number of cases of capturing a StringRef in a lambda, which lead to dangling references. This change converts all uses in the API of `StringRef` to `std::string` to avoid this problem. `std::string` in the API is a reasonable choice, because the combinators are always storing the string beyond the life of the combinator construction. Reviewers: ilya-biryukov, gribozavr Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D62328 Modified: cfe/trunk/include/clang/Tooling/Refactoring/RangeSelector.h cfe/trunk/lib/Tooling/Refactoring/RangeSelector.cpp Modified: cfe/trunk/include/clang/Tooling/Refactoring/RangeSelector.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Refactoring/RangeSelector.h?rev=361514&r1=361513&r2=361514&view=diff ============================================================================== --- cfe/trunk/include/clang/Tooling/Refactoring/RangeSelector.h (original) +++ cfe/trunk/include/clang/Tooling/Refactoring/RangeSelector.h Thu May 23 10:11:33 2019 @@ -17,9 +17,9 @@ #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/SourceLocation.h" -#include "llvm/ADT/StringRef.h" #include "llvm/Support/Error.h" #include <functional> +#include <string> namespace clang { namespace tooling { @@ -35,19 +35,19 @@ inline RangeSelector charRange(CharSourc RangeSelector range(RangeSelector Begin, RangeSelector End); /// Convenience version of \c range where end-points are bound nodes. -RangeSelector range(StringRef BeginID, StringRef EndID); +RangeSelector range(std::string BeginID, std::string EndID); /// Selects a node, including trailing semicolon (for non-expression /// statements). \p ID is the node's binding in the match result. -RangeSelector node(StringRef ID); +RangeSelector node(std::string ID); /// Selects a node, including trailing semicolon (always). Useful for selecting /// expression statements. \p ID is the node's binding in the match result. -RangeSelector statement(StringRef ID); +RangeSelector statement(std::string ID); /// Given a \c MemberExpr, selects the member token. \p ID is the node's /// binding in the match result. -RangeSelector member(StringRef ID); +RangeSelector member(std::string ID); /// Given a node with a "name", (like \c NamedDecl, \c DeclRefExpr or \c /// CxxCtorInitializer) selects the name's token. Only selects the final @@ -56,19 +56,19 @@ RangeSelector member(StringRef ID); /// it selects only `baz`. /// /// \param ID is the node's binding in the match result. -RangeSelector name(StringRef ID); +RangeSelector name(std::string ID); // Given a \c CallExpr (bound to \p ID), selects the arguments' source text (all // source between the call's parentheses). -RangeSelector callArgs(StringRef ID); +RangeSelector callArgs(std::string ID); // Given a \c CompoundStmt (bound to \p ID), selects the source of the // statements (all source between the braces). -RangeSelector statements(StringRef ID); +RangeSelector statements(std::string ID); // Given a \c InitListExpr (bound to \p ID), selects the range of the elements // (all source between the braces). -RangeSelector initListElements(StringRef ID); +RangeSelector initListElements(std::string ID); /// Selects the range from which `S` was expanded (possibly along with other /// source), if `S` is an expansion, and `S` itself, otherwise. Corresponds to Modified: cfe/trunk/lib/Tooling/Refactoring/RangeSelector.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Refactoring/RangeSelector.cpp?rev=361514&r1=361513&r2=361514&view=diff ============================================================================== --- cfe/trunk/lib/Tooling/Refactoring/RangeSelector.cpp (original) +++ cfe/trunk/lib/Tooling/Refactoring/RangeSelector.cpp Thu May 23 10:11:33 2019 @@ -104,7 +104,7 @@ static SourceLocation findOpenParen(cons return findPreviousTokenKind(EndLoc, SM, LangOpts, tok::TokenKind::l_paren); } -RangeSelector tooling::node(StringRef ID) { +RangeSelector tooling::node(std::string ID) { return [ID](const MatchResult &Result) -> Expected<CharSourceRange> { Expected<DynTypedNode> Node = getNode(Result.Nodes, ID); if (!Node) @@ -115,7 +115,7 @@ RangeSelector tooling::node(StringRef ID }; } -RangeSelector tooling::statement(StringRef ID) { +RangeSelector tooling::statement(std::string ID) { return [ID](const MatchResult &Result) -> Expected<CharSourceRange> { Expected<DynTypedNode> Node = getNode(Result.Nodes, ID); if (!Node) @@ -143,11 +143,11 @@ RangeSelector tooling::range(RangeSelect }; } -RangeSelector tooling::range(StringRef BeginID, StringRef EndID) { - return tooling::range(node(BeginID), node(EndID)); +RangeSelector tooling::range(std::string BeginID, std::string EndID) { + return tooling::range(node(std::move(BeginID)), node(std::move(EndID))); } -RangeSelector tooling::member(StringRef ID) { +RangeSelector tooling::member(std::string ID) { return [ID](const MatchResult &Result) -> Expected<CharSourceRange> { Expected<DynTypedNode> Node = getNode(Result.Nodes, ID); if (!Node) @@ -159,7 +159,7 @@ RangeSelector tooling::member(StringRef }; } -RangeSelector tooling::name(StringRef ID) { +RangeSelector tooling::name(std::string ID) { return [ID](const MatchResult &Result) -> Expected<CharSourceRange> { Expected<DynTypedNode> N = getNode(Result.Nodes, ID); if (!N) @@ -205,7 +205,7 @@ class RelativeSelector { std::string ID; public: - RelativeSelector(StringRef ID) : ID(ID) {} + RelativeSelector(std::string ID) : ID(std::move(ID)) {} Expected<CharSourceRange> operator()(const MatchResult &Result) { Expected<DynTypedNode> N = getNode(Result.Nodes, ID); @@ -231,8 +231,8 @@ CharSourceRange getStatementsRange(const } } // namespace -RangeSelector tooling::statements(StringRef ID) { - return RelativeSelector<CompoundStmt, getStatementsRange>(ID); +RangeSelector tooling::statements(std::string ID) { + return RelativeSelector<CompoundStmt, getStatementsRange>(std::move(ID)); } namespace { @@ -246,8 +246,8 @@ CharSourceRange getCallArgumentsRange(co } } // namespace -RangeSelector tooling::callArgs(StringRef ID) { - return RelativeSelector<CallExpr, getCallArgumentsRange>(ID); +RangeSelector tooling::callArgs(std::string ID) { + return RelativeSelector<CallExpr, getCallArgumentsRange>(std::move(ID)); } namespace { @@ -260,8 +260,8 @@ CharSourceRange getElementsRange(const M } } // namespace -RangeSelector tooling::initListElements(StringRef ID) { - return RelativeSelector<InitListExpr, getElementsRange>(ID); +RangeSelector tooling::initListElements(std::string ID) { + return RelativeSelector<InitListExpr, getElementsRange>(std::move(ID)); } RangeSelector tooling::expansion(RangeSelector S) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits