[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

llvm-svn: 361514
This commit is contained in:
Yitzhak Mandelbaum 2019-05-23 17:11:33 +00:00
parent c5ec2a2bc1
commit aa7a2c547e
2 changed files with 22 additions and 22 deletions

View File

@ -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(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

View File

@ -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<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(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<CharSourceRange> {
Expected<DynTypedNode> 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<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 MatchResult &,
}
} // 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(const MatchResult &Result,
}
} // 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 MatchResult &,
}
} // 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) {