Skip to content
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

C++: Support if consteval and if ! consteval #18502

Merged
merged 8 commits into from
Jan 20, 2025
Merged

Conversation

jketema
Copy link
Contributor

@jketema jketema commented Jan 15, 2025

Pull Request checklist

All query authors

Internal query authors only

  • Autofixes generated based on these changes are valid, only needed if this PR makes significant changes to .ql, .qll, or .qhelp files. See the documentation (internal access required).
  • Changes are validated at scale (internal access required).
  • Adding a new query? Consider also adding the query to autofix.

@github-actions github-actions bot added the C++ label Jan 15, 2025
@jketema jketema added the depends on internal PR This PR should only be merged in sync with an internal Semmle PR label Jan 15, 2025
@jketema jketema force-pushed the consteval branch 2 times, most recently from c4673e1 to f70bf5b Compare January 15, 2025 19:30
@jketema jketema marked this pull request as ready for review January 16, 2025 09:19
@jketema jketema requested a review from a team as a code owner January 16, 2025 09:19
Comment on lines +881 to +885
p1.nodeAt(n1, s) and
p2.nodeBefore(n2, s.getThen())
or
p1.nodeAt(n1, s) and
p2.nodeBefore(n2, s.getElse())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here we allow both branches of the consteval if to be reached.

Comment on lines +1145 to +1147
TranslatedStmt getEvaluatedBranch() {
result = getTranslatedStmt(stmt.getRuntimeEvaluatedBranch())
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We pick a single branch that can be reached. For now this is the branch that is taken at runtime, we can change this later if we see that using the compile time branch improves analysis quality.

Copy link
Contributor

@calumgrant calumgrant left a comment

Choose a reason for hiding this comment

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

I wasn't able to give the IR a full review.
Overall LGTM, feel free to ignore class naming suggestions.
I wasn't sure about the need for consteval_if_then/2 and consteval_if_else/2 tables in the dbscheme as this doesn't seem to align with other parent/child relationships.

cpp/ql/lib/change-notes/2024-01-16-consteval-if.md Outdated Show resolved Hide resolved
* }
* ```
*/
class ConstevalOrNotConstevalIfStmt extends Stmt, @stmt_consteval_or_not_consteval_if {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I would personally be ok by calling this class ConstevalIfStmt and just having an isNot() predicate on it. Users might use ConstevalIfStmt and forget about its complement.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Or even IfConstevalStmt and IfNotConsteval since that is the reading order.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ConstevalIfStmt is in line with ConstexprIfStmt, which has as syntax if constexpr.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Folded the classes and introduced the isNot.

@@ -2194,6 +2196,18 @@ constexpr_if_else(
int else_id: @stmt ref
);

@stmt_consteval_or_not_consteval_if = @stmt_consteval_if | @stmt_not_consteval_if;

consteval_if_then(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should use getChild(1) and getChild(2) for this to be consistent withIfStmt, then we don't need these tables.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What I'm doing here is consistent with if. See the if_then and if_else tables just above here.

cpp/ql/lib/semmle/code/cpp/stmts/Stmt.qll Show resolved Hide resolved
@jketema
Copy link
Contributor Author

jketema commented Jan 20, 2025

I wasn't sure about the need for consteval_if_then/2 and consteval_if_else/2 tables in the dbscheme as this doesn't seem to align with other parent/child relationships.

This aligns with what is done for if and constexpr if

@calumgrant
Copy link
Contributor

I wasn't sure about the need for consteval_if_then/2 and consteval_if_else/2 tables in the dbscheme as this doesn't seem to align with other parent/child relationships.

This aligns with what is done for if and constexpr if

Oh I see! Yes, I see that all the If stmts have been written in a similar way.

@MathiasVP
Copy link
Contributor

The IR construction changes LGTM! I'll let someone from the team approve the PR 🙂

@jketema jketema requested a review from calumgrant January 20, 2025 11:37
@jketema jketema merged commit 4b85ea3 into github:main Jan 20, 2025
14 of 15 checks passed
@jketema jketema deleted the consteval branch January 20, 2025 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++ depends on internal PR This PR should only be merged in sync with an internal Semmle PR documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants