From: Yitzhak Mandelbaum Date: Thu, 23 May 2019 17:11:33 +0000 (+0000) Subject: [LibTooling] Fix dangling references in RangeSelector. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=33b8ef199182819269df7b4045983d30f86cf1ea;p=clang [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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@361514 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Tooling/Refactoring/RangeSelector.h b/include/clang/Tooling/Refactoring/RangeSelector.h index d1d6bc8b6a..2d878b90aa 100644 --- a/include/clang/Tooling/Refactoring/RangeSelector.h +++ b/include/clang/Tooling/Refactoring/RangeSelector.h @@ -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 +#include namespace clang { namespace tooling { @@ -35,19 +35,19 @@ inline RangeSelector charRange(CharSourceRange R) { 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 diff --git a/lib/Tooling/Refactoring/RangeSelector.cpp b/lib/Tooling/Refactoring/RangeSelector.cpp index 92426db3a5..d5f82d4262 100644 --- a/lib/Tooling/Refactoring/RangeSelector.cpp +++ b/lib/Tooling/Refactoring/RangeSelector.cpp @@ -104,7 +104,7 @@ static SourceLocation findOpenParen(const CallExpr &E, const SourceManager &SM, 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 { Expected 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 { Expected Node = getNode(Result.Nodes, ID); if (!Node) @@ -143,11 +143,11 @@ RangeSelector tooling::range(RangeSelector Begin, RangeSelector End) { }; } -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 { Expected Node = getNode(Result.Nodes, ID); if (!Node) @@ -159,7 +159,7 @@ RangeSelector tooling::member(StringRef ID) { }; } -RangeSelector tooling::name(StringRef ID) { +RangeSelector tooling::name(std::string ID) { return [ID](const MatchResult &Result) -> Expected { Expected 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 operator()(const MatchResult &Result) { Expected N = getNode(Result.Nodes, ID); @@ -231,8 +231,8 @@ CharSourceRange getStatementsRange(const MatchResult &, } } // namespace -RangeSelector tooling::statements(StringRef ID) { - return RelativeSelector(ID); +RangeSelector tooling::statements(std::string ID) { + return RelativeSelector(std::move(ID)); } namespace { @@ -246,8 +246,8 @@ CharSourceRange getCallArgumentsRange(const MatchResult &Result, } } // namespace -RangeSelector tooling::callArgs(StringRef ID) { - return RelativeSelector(ID); +RangeSelector tooling::callArgs(std::string ID) { + return RelativeSelector(std::move(ID)); } namespace { @@ -260,8 +260,8 @@ CharSourceRange getElementsRange(const MatchResult &, } } // namespace -RangeSelector tooling::initListElements(StringRef ID) { - return RelativeSelector(ID); +RangeSelector tooling::initListElements(std::string ID) { + return RelativeSelector(std::move(ID)); } RangeSelector tooling::expansion(RangeSelector S) {