forked from SonarOpenCommunity/sonar-cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- [attribute [[assume]]](https://en.cppreference.com/w/cpp/language/attributes/assume) [P1774R8](https://wg21.link/P1774R8) - linked SonarOpenCommunity#2536
- Loading branch information
Showing
2 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
cxx-squid/src/test/resources/parser/own/C++23/attributte-assume.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |