Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 74f81c7

Browse files
committed
C++: test for fold expressions
1 parent 4352a20 commit 74f81c7

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// semmle-extractor-options: --edg --c++17
2+
3+
template<typename ...Args>
4+
int sum(Args&&... args) {
5+
return (args + ...);
6+
}
7+
8+
template<typename ...Args>
9+
int product(Args&&... args) {
10+
return (... * args);
11+
}
12+
13+
template<typename ...Args>
14+
bool all(Args&&... args) {
15+
return (args && ... && true);
16+
}
17+
18+
template<typename ...Args>
19+
bool any(Args&&... args) {
20+
return (false || ... || args);
21+
}
22+
23+
void f() {
24+
int x = sum(1, 2, 3, 4, 5);
25+
int y = product(2, 4, 6, 8);
26+
bool a = all(true, true, false, true);
27+
bool b = any(false, true, false, false);
28+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| fold.cpp:5:10:5:21 | ( pack + ... ) | + | fold.cpp:5:11:5:14 | args | <no init> |
2+
| fold.cpp:10:10:10:21 | ( ... * pack ) | * | fold.cpp:10:17:10:20 | args | <no init> |
3+
| fold.cpp:15:10:15:30 | ( pack && ... && init ) | && | fold.cpp:15:11:15:14 | args | 1 |
4+
| fold.cpp:20:10:20:31 | ( init \|\| ... \|\| pack ) | \|\| | fold.cpp:20:27:20:30 | args | 0 |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import cpp
2+
3+
from FoldExpr fe, Expr pack, string init
4+
where
5+
pack = fe.getPackExpr() and
6+
if fe.hasInitExpr() then init = fe.getInitExpr().toString() else init = "<no init>"
7+
select fe, fe.getOperatorString(), pack, init

0 commit comments

Comments
 (0)