Skip to content

Commit 4076a22

Browse files
committed
__builtin_constant_p: properly test, fix behaviour
We previously only compile-time tested what required a run-time check of assertions, so move this to the "cbmc" regression test suite. Also, extend it by behaviour that distinguishes it from actual C-standard specified constant expressions. Finally, fix the behaviour for string literals.
1 parent 8ef7e92 commit 4076a22

File tree

4 files changed

+51
-29
lines changed

4 files changed

+51
-29
lines changed

regression/ansi-c/gcc_builtin_constant_p1/main.c

-28
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <assert.h>
2+
3+
#ifdef __GNUC__
4+
enum
5+
{
6+
E1 = 1
7+
} var;
8+
9+
struct whatnot
10+
{
11+
} whatnot_var;
12+
#endif
13+
14+
int main()
15+
{
16+
// this is gcc only
17+
18+
#ifdef __GNUC__
19+
assert(__builtin_constant_p("some string"));
20+
assert(__builtin_constant_p(1.0f));
21+
assert(__builtin_constant_p(E1));
22+
assert(!__builtin_constant_p(var));
23+
assert(!__builtin_constant_p(main));
24+
assert(!__builtin_constant_p(whatnot_var));
25+
assert(!__builtin_constant_p(&var));
26+
assert(__builtin_constant_p(__builtin_constant_p(var)));
27+
28+
// The following are not constant expressions in the sense of the C standard
29+
// and GCC wouldn't deem them constant expressions either, but they are
30+
// subject to GCC's constant folding. See also regression test ansi-c/sizeof6.
31+
// Clang's behaviour, however, is somewhat different. See
32+
// https://github.com/llvm/llvm-project/issues/55946 for further examples of
33+
// where they differ.
34+
int j;
35+
# ifndef __clang__
36+
assert(__builtin_constant_p(j * 0));
37+
assert(__builtin_constant_p(j - j));
38+
assert(__builtin_constant_p(j ? 0ll : 0ll));
39+
# endif
40+
assert(__builtin_constant_p(0 ? j : 0ll));
41+
42+
// side-effects are _not_ evaluated
43+
int i = 0;
44+
assert(!__builtin_constant_p(i++));
45+
assert(i == 0);
46+
#endif
47+
}

regression/ansi-c/gcc_builtin_constant_p1/test.desc renamed to regression/cbmc/gcc_builtin_constant_p1/test.desc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
CORE gcc-only broken-test-c++-front-end
1+
CORE
22
main.c
33

4+
^VERIFICATION SUCCESSFUL$
45
^EXIT=0$
56
^SIGNAL=0$
67
--

src/ansi-c/c_typecheck_expr.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -3682,6 +3682,8 @@ exprt c_typecheck_baset::do_special_functions(
36823682
{
36833683
is_constant=true;
36843684
}
3685+
else if(tmp1.id() == ID_string_constant)
3686+
is_constant = true;
36853687
else
36863688
is_constant=tmp1.is_constant();
36873689

0 commit comments

Comments
 (0)