Skip to content

Swift: fix SequenceExpr extraction #14119

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 1 commit into from
Sep 4, 2023
Merged
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
12 changes: 11 additions & 1 deletion swift/extractor/translators/ExprTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,17 @@ codeql::UnresolvedMemberExpr ExprTranslator::translateUnresolvedMemberExpr(

codeql::SequenceExpr ExprTranslator::translateSequenceExpr(const swift::SequenceExpr& expr) {
auto entry = createExprEntry(expr);
entry.elements = dispatcher.fetchRepeatedLabels(expr.getElements());
// SequenceExpr represents a flat tree of expressions with elements at odd indices being the
// parents of the elements with even indices, so we only extract the "parent" elements here. In
// case there is a single child, we extract it as a parent. See
// https://github.com/github/codeql/pull/14119 and commit message for more details.
if (expr.getNumElements() == 1) {
entry.elements = dispatcher.fetchRepeatedLabels(expr.getElements());
} else {
for (int i = 1; i < expr.getNumElements(); i += 2) {
entry.elements.emplace_back(dispatcher.fetchLabel(expr.getElement(i)));
}
}
Comment on lines +467 to +473
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of future developers looking at this piece of code: Does it maybe deserve an explanation for why we're only extracting the odd elements?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I'll add a short comment and also a link to this PR (the PR description is in the commit message as well).

return entry;
}

Expand Down