Skip to content

Commit

Permalink
Merge pull request #2732 from guwirth/cpp23-attribute-assume
Browse files Browse the repository at this point in the history
C++23 attributte assume
  • Loading branch information
guwirth committed Aug 29, 2024
2 parents 754e5c9 + 3f0a703 commit 696d8f3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ void classSpecifier_reallife() {
.matches("[[fallthrough]]")
.matches("[[nodiscard]]")
.matches("[[maybe_unused]]")
.matches("[[attr1]] [[attr2]] [[attr3]]");
.matches("[[attr1]] [[attr2]] [[attr3]]")
//C++23
.matches("[[assume((x - 1) * 3 == 12)]]");
}

}
35 changes: 35 additions & 0 deletions cxx-squid/src/test/resources/parser/own/C++23/attributte-assume.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
void f(int& x, int y)
{
void g(int);
void h();

[[assume(x > 0)]]; // Compiler may assume x is positive

g(x / 2); // More efficient code possibly generated

x = 3;
int z = x;

[[assume((h(), x == z))]]; // Compiler may assume x would have the same value after
// calling h
// The assumption does not cause a call to h

h();
g(x); // Compiler may replace this with g(3);

h();
g(x); // Compiler may NOT replace this with g(3);
// An assumption applies only at the point where it appears

z = std::abs(y);

[[assume((g(z), true))]]; // Compiler may assume g(z) will return

g(z); // Due to above and below assumptions, compiler may replace this with g(10);

[[assume(y == -10)]]; // Undefined behavior if y != -10 at this point

[[assume((x - 1) * 3 == 12)]];

g(x); // Compiler may replace this with g(5);
}

0 comments on commit 696d8f3

Please sign in to comment.