C++: test for fold expressions

This commit is contained in:
Nick Rolfe
2019-04-23 15:27:05 +01:00
parent 4352a20be0
commit 74f81c7f46
3 changed files with 39 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
// semmle-extractor-options: --edg --c++17
template<typename ...Args>
int sum(Args&&... args) {
return (args + ...);
}
template<typename ...Args>
int product(Args&&... args) {
return (... * args);
}
template<typename ...Args>
bool all(Args&&... args) {
return (args && ... && true);
}
template<typename ...Args>
bool any(Args&&... args) {
return (false || ... || args);
}
void f() {
int x = sum(1, 2, 3, 4, 5);
int y = product(2, 4, 6, 8);
bool a = all(true, true, false, true);
bool b = any(false, true, false, false);
}

View File

@@ -0,0 +1,4 @@
| fold.cpp:5:10:5:21 | ( pack + ... ) | + | fold.cpp:5:11:5:14 | args | <no init> |
| fold.cpp:10:10:10:21 | ( ... * pack ) | * | fold.cpp:10:17:10:20 | args | <no init> |
| fold.cpp:15:10:15:30 | ( pack && ... && init ) | && | fold.cpp:15:11:15:14 | args | 1 |
| fold.cpp:20:10:20:31 | ( init \|\| ... \|\| pack ) | \|\| | fold.cpp:20:27:20:30 | args | 0 |

View File

@@ -0,0 +1,7 @@
import cpp
from FoldExpr fe, Expr pack, string init
where
pack = fe.getPackExpr() and
if fe.hasInitExpr() then init = fe.getInitExpr().toString() else init = "<no init>"
select fe, fe.getOperatorString(), pack, init