Skip to content

[clang][transformer] Introduce a constructExprArgs range selector. #95901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/include/clang/Tooling/Transformer/RangeSelector.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ RangeSelector name(std::string ID);
// source between the call's parentheses).
RangeSelector callArgs(std::string ID);

// Given a \c CXXConstructExpr (bound to \p ID), selects the
// arguments' source text. Depending on the syntactic form of the construct,
// this is the range between parentheses or braces.
RangeSelector constructExprArgs(std::string ID);

// Given a \c CompoundStmt (bound to \p ID), selects the source of the
// statements (all source between the braces).
RangeSelector statements(std::string ID);
Expand Down
51 changes: 38 additions & 13 deletions clang/lib/Tooling/Transformer/RangeSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,6 @@ static SourceLocation findPreviousTokenKind(SourceLocation Start,
}
}

static SourceLocation findOpenParen(const CallExpr &E, const SourceManager &SM,
const LangOptions &LangOpts) {
SourceLocation EndLoc =
E.getNumArgs() == 0 ? E.getRParenLoc() : E.getArg(0)->getBeginLoc();
return findPreviousTokenKind(EndLoc, SM, LangOpts, tok::TokenKind::l_paren);
}

RangeSelector transformer::before(RangeSelector Selector) {
return [Selector](const MatchResult &Result) -> Expected<CharSourceRange> {
Expected<CharSourceRange> SelectedRange = Selector(Result);
Expand Down Expand Up @@ -287,18 +280,50 @@ RangeSelector transformer::statements(std::string ID) {
}

namespace {
// Returns the range of the source between the call's parentheses.
CharSourceRange getCallArgumentsRange(const MatchResult &Result,
const CallExpr &CE) {

SourceLocation getRLoc(const CallExpr &E) { return E.getRParenLoc(); }

SourceLocation getRLoc(const CXXConstructExpr &E) {
return E.getParenOrBraceRange().getEnd();
}

tok::TokenKind getStartToken(const CallExpr &E) {
return tok::TokenKind::l_paren;
}

tok::TokenKind getStartToken(const CXXConstructExpr &E) {
return isa<CXXTemporaryObjectExpr>(E) ? tok::TokenKind::l_paren
: tok::TokenKind::l_brace;
}

template <typename ExprWithArgs>
SourceLocation findArgStartDelimiter(const ExprWithArgs &E, SourceLocation RLoc,
const SourceManager &SM,
const LangOptions &LangOpts) {
SourceLocation Loc = E.getNumArgs() == 0 ? RLoc : E.getArg(0)->getBeginLoc();
return findPreviousTokenKind(Loc, SM, LangOpts, getStartToken(E));
}
// Returns the range of the source between the call's or construct expr's
// parentheses/braces.
template <typename ExprWithArgs>
CharSourceRange getArgumentsRange(const MatchResult &Result,
const ExprWithArgs &CE) {
const SourceLocation RLoc = getRLoc(CE);
return CharSourceRange::getCharRange(
findOpenParen(CE, *Result.SourceManager, Result.Context->getLangOpts())
findArgStartDelimiter(CE, RLoc, *Result.SourceManager,
Result.Context->getLangOpts())
.getLocWithOffset(1),
CE.getRParenLoc());
RLoc);
}
} // namespace

RangeSelector transformer::callArgs(std::string ID) {
return RelativeSelector<CallExpr, getCallArgumentsRange>(std::move(ID));
return RelativeSelector<CallExpr, getArgumentsRange<CallExpr>>(std::move(ID));
}

RangeSelector transformer::constructExprArgs(std::string ID) {
return RelativeSelector<CXXConstructExpr,
getArgumentsRange<CXXConstructExpr>>(std::move(ID));
}

namespace {
Expand Down
42 changes: 42 additions & 0 deletions clang/unittests/Tooling/RangeSelectorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,48 @@ TEST(RangeSelectorTest, CallArgsErrors) {
Failed<StringError>(withTypeErrorMessage("stmt")));
}

TEST(RangeSelectorTest, ConstructExprArgs) {
const StringRef Code = R"cc(
struct C {
C(int, int);
};
C f() {
return C(1, 2);
}
)cc";
const char *ID = "id";
TestMatch Match = matchCode(Code, cxxTemporaryObjectExpr().bind(ID));
EXPECT_THAT_EXPECTED(select(constructExprArgs(ID), Match), HasValue("1, 2"));
}

TEST(RangeSelectorTest, ConstructExprBracedArgs) {
const StringRef Code = R"cc(
struct C {
C(int, int);
};
C f() {
return {1, 2};
}
)cc";
const char *ID = "id";
TestMatch Match = matchCode(Code, cxxConstructExpr().bind(ID));
EXPECT_THAT_EXPECTED(select(constructExprArgs(ID), Match), HasValue("1, 2"));
}

TEST(RangeSelectorTest, ConstructExprNoArgs) {
const StringRef Code = R"cc(
struct C {
C();
};
C f() {
return C();
}
)cc";
const char *ID = "id";
TestMatch Match = matchCode(Code, cxxTemporaryObjectExpr().bind(ID));
EXPECT_THAT_EXPECTED(select(constructExprArgs(ID), Match), HasValue(""));
}

TEST(RangeSelectorTest, StatementsOp) {
StringRef Code = R"cc(
void g();
Expand Down
Loading