Skip to content

Commit 2332289

Browse files
committed
C++: Add tests for new builtin operations
1 parent bee0f97 commit 2332289

7 files changed

Lines changed: 191 additions & 8 deletions

File tree

‎cpp/ql/test/library-tests/builtins/builtins/builtins.expected‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
| choose_expr.c:3:13:3:42 | __builtin_choose_expr |
2+
| spaceship.cpp:22:41:22:101 | __builtin_lt_synthesizes_from_spaceship |
3+
| spaceship.cpp:23:41:23:89 | __builtin_lt_synthesizes_from_spaceship |
4+
| spaceship.cpp:24:41:24:101 | __builtin_gt_synthesizes_from_spaceship |
5+
| spaceship.cpp:25:41:25:89 | __builtin_gt_synthesizes_from_spaceship |
6+
| spaceship.cpp:26:41:26:101 | __builtin_le_synthesizes_from_spaceship |
7+
| spaceship.cpp:27:41:27:89 | __builtin_le_synthesizes_from_spaceship |
8+
| spaceship.cpp:28:41:28:101 | __builtin_ge_synthesizes_from_spaceship |
9+
| spaceship.cpp:29:41:29:89 | __builtin_ge_synthesizes_from_spaceship |
210
| types.c:4:9:4:57 | __builtin_types_compatible_p |
311
| types.c:6:16:6:65 | __builtin_types_compatible_p |
412
| varargs.c:7:5:7:32 | __builtin_va_start |
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
| atomic.cpp:3:10:3:27 | call to __atomic_fetch_add_4 | ptr,1,0 | <none> |
2+
| atomic.cpp:3:10:3:38 | (int)... | | <none> |
3+
| atomic.cpp:3:29:3:31 | (volatile void *)... | | <none> |
4+
| atomic.cpp:3:29:3:31 | ptr | | <none> |
5+
| atomic.cpp:3:34:3:34 | 1 | | 1 |
6+
| atomic.cpp:3:34:3:34 | (unsigned int)... | | 1 |
7+
| atomic.cpp:3:37:3:37 | 0 | | 0 |
8+
| atomic.cpp:8:10:8:27 | call to __atomic_fetch_add_1 | ptr,1,0 | <none> |
9+
| atomic.cpp:8:10:8:38 | (char)... | | <none> |
10+
| atomic.cpp:8:10:8:38 | (double)... | | <none> |
11+
| atomic.cpp:8:29:8:31 | (volatile void *)... | | <none> |
12+
| atomic.cpp:8:29:8:31 | ptr | | <none> |
13+
| atomic.cpp:8:34:8:34 | 1 | | 1 |
14+
| atomic.cpp:8:34:8:34 | (unsigned char)... | | 1 |
15+
| atomic.cpp:8:37:8:37 | 0 | | 0 |
16+
| atomic.cpp:13:10:13:29 | call to __atomic_fetch_add_4 | ptr,1,0 | <none> |
17+
| atomic.cpp:13:10:13:40 | (int)... | | <none> |
18+
| atomic.cpp:13:31:13:33 | (volatile void *)... | | <none> |
19+
| atomic.cpp:13:31:13:33 | ptr | | <none> |
20+
| atomic.cpp:13:36:13:36 | 1 | | 1 |
21+
| atomic.cpp:13:36:13:36 | (unsigned int)... | | 1 |
22+
| atomic.cpp:13:39:13:39 | 0 | | 0 |
23+
| atomic.cpp:18:10:18:29 | call to __atomic_fetch_add_1 | ptr,1,0 | <none> |
24+
| atomic.cpp:18:10:18:40 | (char)... | | <none> |
25+
| atomic.cpp:18:31:18:33 | (volatile void *)... | | <none> |
26+
| atomic.cpp:18:31:18:33 | ptr | | <none> |
27+
| atomic.cpp:18:36:18:36 | 1 | | 1 |
28+
| atomic.cpp:18:36:18:36 | (unsigned char)... | | 1 |
29+
| atomic.cpp:18:39:18:39 | 0 | | 0 |
30+
| choose_expr.c:3:13:3:42 | __builtin_choose_expr | 1,5,6 | <none> |
31+
| choose_expr.c:3:35:3:35 | 1 | | 1 |
32+
| choose_expr.c:3:38:3:38 | 5 | | 5 |
33+
| choose_expr.c:3:41:3:41 | 6 | | 6 |
34+
| spaceship.cpp:6:45:6:45 | this | | <none> |
35+
| spaceship.cpp:9:53:9:56 | {...} | - ... | <none> |
36+
| spaceship.cpp:9:54:9:55 | - ... | 1 | -1 |
37+
| spaceship.cpp:9:55:9:55 | 1 | | 1 |
38+
| spaceship.cpp:10:54:10:56 | {...} | 0 | <none> |
39+
| spaceship.cpp:10:55:10:55 | 0 | | 0 |
40+
| spaceship.cpp:11:56:11:58 | {...} | 1 | <none> |
41+
| spaceship.cpp:11:57:11:57 | 1 | | 1 |
42+
| spaceship.cpp:18:12:18:14 | rhs | | <none> |
43+
| spaceship.cpp:18:12:18:24 | ... <=> ... | rhs,i | <none> |
44+
| spaceship.cpp:18:20:18:22 | lhs | | <none> |
45+
| spaceship.cpp:22:41:22:101 | __builtin_lt_synthesizes_from_spaceship | const S &,const int & | 1 |
46+
| spaceship.cpp:22:41:22:101 | const S & | | <none> |
47+
| spaceship.cpp:22:41:22:101 | const int & | | <none> |
48+
| spaceship.cpp:23:41:23:89 | __builtin_lt_synthesizes_from_spaceship | int,int | 0 |
49+
| spaceship.cpp:23:41:23:89 | int | | <none> |
50+
| spaceship.cpp:23:41:23:89 | int | | <none> |
51+
| spaceship.cpp:24:41:24:101 | __builtin_gt_synthesizes_from_spaceship | const S &,const int & | 1 |
52+
| spaceship.cpp:24:41:24:101 | const S & | | <none> |
53+
| spaceship.cpp:24:41:24:101 | const int & | | <none> |
54+
| spaceship.cpp:25:41:25:89 | __builtin_gt_synthesizes_from_spaceship | int,int | 0 |
55+
| spaceship.cpp:25:41:25:89 | int | | <none> |
56+
| spaceship.cpp:25:41:25:89 | int | | <none> |
57+
| spaceship.cpp:26:41:26:101 | __builtin_le_synthesizes_from_spaceship | const S &,const int & | 1 |
58+
| spaceship.cpp:26:41:26:101 | const S & | | <none> |
59+
| spaceship.cpp:26:41:26:101 | const int & | | <none> |
60+
| spaceship.cpp:27:41:27:89 | __builtin_le_synthesizes_from_spaceship | int,int | 0 |
61+
| spaceship.cpp:27:41:27:89 | int | | <none> |
62+
| spaceship.cpp:27:41:27:89 | int | | <none> |
63+
| spaceship.cpp:28:41:28:101 | __builtin_ge_synthesizes_from_spaceship | const S &,const int & | 1 |
64+
| spaceship.cpp:28:41:28:101 | const S & | | <none> |
65+
| spaceship.cpp:28:41:28:101 | const int & | | <none> |
66+
| spaceship.cpp:29:41:29:89 | __builtin_ge_synthesizes_from_spaceship | int,int | 0 |
67+
| spaceship.cpp:29:41:29:89 | int | | <none> |
68+
| spaceship.cpp:29:41:29:89 | int | | <none> |
69+
| types.c:4:9:4:57 | __builtin_types_compatible_p | typeof(...),long | 0 |
70+
| types.c:4:9:4:57 | long | | <none> |
71+
| types.c:4:9:4:57 | typeof(...) | | <none> |
72+
| types.c:4:49:4:49 | s | | <none> |
73+
| types.c:5:9:5:12 | call to puts | long | <none> |
74+
| types.c:5:14:5:19 | array to pointer conversion | | <none> |
75+
| types.c:5:14:5:19 | long | | long |
76+
| types.c:6:16:6:65 | __builtin_types_compatible_p | typeof(...),char * | 1 |
77+
| types.c:6:16:6:65 | char * | | <none> |
78+
| types.c:6:16:6:65 | typeof(...) | | <none> |
79+
| types.c:6:56:6:56 | s | | <none> |
80+
| types.c:7:9:7:12 | call to puts | str | <none> |
81+
| types.c:7:14:7:18 | array to pointer conversion | | <none> |
82+
| types.c:7:14:7:18 | str | | str |
83+
| types.c:9:12:9:14 | (...) | | 0 |
84+
| types.c:9:13:9:13 | 0 | | 0 |
85+
| varargs.c:7:5:7:32 | __builtin_va_start | ap,args | <none> |
86+
| varargs.c:7:24:7:25 | ap | | <none> |
87+
| varargs.c:7:24:7:25 | array to pointer conversion | | <none> |
88+
| varargs.c:7:28:7:31 | args | | <none> |
89+
| varargs.c:8:10:8:10 | i | | <none> |
90+
| varargs.c:8:10:8:14 | ... = ... | i,0 | <none> |
91+
| varargs.c:8:14:8:14 | 0 | | 0 |
92+
| varargs.c:8:17:8:17 | i | | <none> |
93+
| varargs.c:8:17:8:24 | ... < ... | i,args | <none> |
94+
| varargs.c:8:21:8:24 | args | | <none> |
95+
| varargs.c:8:27:8:27 | i | | <none> |
96+
| varargs.c:8:27:8:29 | ... ++ | i | <none> |
97+
| varargs.c:9:9:9:9 | j | | <none> |
98+
| varargs.c:9:9:9:37 | ... = ... | j,__builtin_va_arg | <none> |
99+
| varargs.c:9:13:9:37 | __builtin_va_arg | ap | <none> |
100+
| varargs.c:9:30:9:31 | ap | | <none> |
101+
| varargs.c:9:30:9:31 | array to pointer conversion | | <none> |
102+
| varargs.c:11:5:11:24 | __builtin_va_end | ap | <none> |
103+
| varargs.c:11:22:11:23 | ap | | <none> |
104+
| varargs.c:11:22:11:23 | array to pointer conversion | | <none> |
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import cpp
2+
3+
string childrenFrom(Expr e, int i) {
4+
(
5+
if i = 0
6+
then result = e.getChild(i).toString() + childrenFrom(e, i + 1)
7+
else result = "," + e.getChild(i).toString() + childrenFrom(e, i + 1)
8+
)
9+
or
10+
i = e.getNumChild() and result = ""
11+
}
12+
13+
from Expr e, string value
14+
where if exists(e.getValue()) then value = e.getValue().toString() else value = "<none>"
15+
select e, childrenFrom(e, 0), value
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// semmle-extractor-options: --clang --clang_version 240000 -std=c++23
2+
3+
namespace std {
4+
struct strong_ordering {
5+
int n;
6+
constexpr operator int() const { return n; }
7+
static const strong_ordering less, equal, greater;
8+
};
9+
constexpr strong_ordering strong_ordering::less = {-1};
10+
constexpr strong_ordering strong_ordering::equal = {0};
11+
constexpr strong_ordering strong_ordering::greater = {1};
12+
}
13+
14+
struct S {
15+
int i;
16+
17+
friend auto operator<=>(S lhs, int rhs) {
18+
return rhs <=> lhs.i;
19+
}
20+
};
21+
22+
bool b_lt_synthesizes_from_spaceship1 = __builtin_lt_synthesizes_from_spaceship(const S&, const int&);
23+
bool b_lt_synthesizes_from_spaceship2 = __builtin_lt_synthesizes_from_spaceship(int, int);
24+
bool b_gt_synthesizes_from_spaceship1 = __builtin_gt_synthesizes_from_spaceship(const S&, const int&);
25+
bool b_gt_synthesizes_from_spaceship2 = __builtin_gt_synthesizes_from_spaceship(int, int);
26+
bool b_le_synthesizes_from_spaceship1 = __builtin_le_synthesizes_from_spaceship(const S&, const int&);
27+
bool b_le_synthesizes_from_spaceship2 = __builtin_le_synthesizes_from_spaceship(int, int);
28+
bool b_ge_synthesizes_from_spaceship1 = __builtin_ge_synthesizes_from_spaceship(const S&, const int&);
29+
bool b_ge_synthesizes_from_spaceship2 = __builtin_ge_synthesizes_from_spaceship(int, int);

‎cpp/ql/test/library-tests/builtins/type_traits/clang.cpp‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,20 @@ bool b_is_scoped_enum2 = __is_scoped_enum(int);
122122
bool b_is_trivially_relocatable1 = __is_trivially_relocatable(int);
123123
bool b_is_trivially_relocatable2 = __is_trivially_relocatable(void);
124124

125-
struct S3{
125+
struct S3 {
126126
S3(S3 &);
127127
S3& operator=(S3&);
128128
};
129129

130-
bool bok_is_bitwise_cloneable1 = __is_bitwise_cloneable(int);
131-
bool bok_is_bitwise_cloneable2 = __is_bitwise_cloneable(S3);
130+
bool b_is_bitwise_cloneable1 = __is_bitwise_cloneable(int);
131+
bool b_is_bitwise_cloneable2 = __is_bitwise_cloneable(S3);
132+
133+
struct S4 : virtual S3 {
134+
~S4() = delete;
135+
};
136+
137+
bool b_is_virtual_base_of1 = __builtin_is_virtual_base_of(int, S4);
138+
bool b_is_virtual_base_of2 = __builtin_is_virtual_base_of(S3, S4);
139+
140+
bool b_is_implicit_lifetime1 = __builtin_is_implicit_lifetime(int);
141+
bool b_is_implicit_lifetime2 = __builtin_is_implicit_lifetime(S4);

‎cpp/ql/test/library-tests/builtins/type_traits/expr.expected‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,20 @@
165165
| clang.cpp:122:36:122:66 | int | | <none> |
166166
| clang.cpp:123:36:123:67 | __is_trivially_relocatable | void | 0 |
167167
| clang.cpp:123:36:123:67 | void | | <none> |
168-
| clang.cpp:130:34:130:60 | __is_bitwise_cloneable | int | 1 |
169-
| clang.cpp:130:34:130:60 | int | | <none> |
170-
| clang.cpp:131:34:131:59 | S3 | | <none> |
171-
| clang.cpp:131:34:131:59 | __is_bitwise_cloneable | S3 | 0 |
168+
| clang.cpp:130:32:130:58 | __is_bitwise_cloneable | int | 1 |
169+
| clang.cpp:130:32:130:58 | int | | <none> |
170+
| clang.cpp:131:32:131:57 | S3 | | <none> |
171+
| clang.cpp:131:32:131:57 | __is_bitwise_cloneable | S3 | 0 |
172+
| clang.cpp:137:30:137:66 | S4 | | <none> |
173+
| clang.cpp:137:30:137:66 | __builtin_is_virtual_base_of | int,S4 | 0 |
174+
| clang.cpp:137:30:137:66 | int | | <none> |
175+
| clang.cpp:138:30:138:65 | S3 | | <none> |
176+
| clang.cpp:138:30:138:65 | S4 | | <none> |
177+
| clang.cpp:138:30:138:65 | __builtin_is_virtual_base_of | S3,S4 | 1 |
178+
| clang.cpp:140:32:140:66 | __builtin_is_implicit_lifetime | int | 1 |
179+
| clang.cpp:140:32:140:66 | int | | <none> |
180+
| clang.cpp:141:32:141:65 | S4 | | <none> |
181+
| clang.cpp:141:32:141:65 | __builtin_is_implicit_lifetime | S4 | 0 |
172182
| file://:0:0:0:0 | 0 | | 0 |
173183
| file://:0:0:0:0 | 0 | | 0 |
174184
| file://:0:0:0:0 | 1 | | 1 |
@@ -225,6 +235,10 @@
225235
| gcc.cpp:37:32:37:67 | __is_nothrow_invocable | f_type,bool | 0 |
226236
| gcc.cpp:37:32:37:67 | bool | | <none> |
227237
| gcc.cpp:37:32:37:67 | f_type | | <none> |
238+
| gcc.cpp:39:25:39:52 | __builtin_is_structural | int | 1 |
239+
| gcc.cpp:39:25:39:52 | int | | <none> |
240+
| gcc.cpp:40:25:40:54 | __builtin_is_structural | int && | 0 |
241+
| gcc.cpp:40:25:40:54 | int && | | <none> |
228242
| ms.cpp:38:41:38:45 | 0 | | 0 |
229243
| ms.cpp:88:27:88:45 | __has_assign | empty | 0 |
230244
| ms.cpp:88:27:88:45 | empty | | <none> |

‎cpp/ql/test/library-tests/builtins/type_traits/gcc.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// semmle-extractor-options: --gnu_version 150000
1+
// semmle-extractor-options: --gnu_version 170000
22

33
__attribute__ ((aligned(8))) int v;
44
bool b_has_attribute1 = __builtin_has_attribute(v, aligned);
@@ -35,3 +35,6 @@ bool b_is_invocable2 = __is_invocable(f_type, bool);
3535

3636
bool b_is_nothrow_invocable1 = __is_nothrow_invocable(f_type);
3737
bool b_is_nothrow_invocable2 = __is_nothrow_invocable(f_type, bool);
38+
39+
bool b_is_structural1 = __builtin_is_structural(int);
40+
bool b_is_structural2 = __builtin_is_structural(int&&);

0 commit comments

Comments
 (0)