Skip to content

Commit 433de81

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 433de81

File tree

4 files changed

+49
-29
lines changed

4 files changed

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

regression/ansi-c/gcc_builtin_constant_p1/test.desc 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)