Skip to content

Commit 9441a92

Browse files
author
Daniel Kroening
authored
Merge pull request diffblue#2317 from tautschnig/c++-attributes-lists
C++ initializer lists and GCC attributes
2 parents cad7b3a + 1d95ab4 commit 9441a92

14 files changed

+312
-73
lines changed

regression/cpp/List_initialization1/test.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
KNOWNBUG
1+
CORE
22
main.cpp
33
-std=c++11
44
^EXIT=0$

regression/cpp/gcc_vector1/main.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifdef __GNUC__
2+
3+
typedef float __m128 __attribute__ ((__vector_size__ (16), __may_alias__));
4+
5+
__m128 setzero(void)
6+
{
7+
return (__m128){ 0.0f, 0.0f, 0.0f, 0.0f };
8+
}
9+
10+
#else
11+
12+
void setzero()
13+
{
14+
}
15+
16+
#endif
17+
18+
int main(int argc, char* argv[])
19+
{
20+
setzero();
21+
return 0;
22+
}

regression/cpp/gcc_vector1/test.desc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.cpp
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^CONVERSION ERROR$
8+
^warning: ignoring

src/ansi-c/scanner.l

+3-8
Original file line numberDiff line numberDiff line change
@@ -948,14 +948,9 @@ __decltype { if(PARSER.cpp98 &&
948948
PARSER.mode==configt::ansi_ct::flavourt::CODEWARRIOR ||
949949
PARSER.mode==configt::ansi_ct::flavourt::ARM)
950950
{
951-
if(PARSER.cpp98)
952-
BEGIN(IGNORE_PARENS);
953-
else
954-
{
955-
BEGIN(GCC_ATTRIBUTE1);
956-
loc();
957-
return TOK_GCC_ATTRIBUTE;
958-
}
951+
BEGIN(GCC_ATTRIBUTE1);
952+
loc();
953+
return TOK_GCC_ATTRIBUTE;
959954
}
960955
else
961956
return make_identifier();

src/cpp/cpp_convert_type.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class cpp_convert_typet
3030
unsigned unsigned_cnt, signed_cnt, char_cnt, int_cnt, short_cnt,
3131
long_cnt, const_cnt, restrict_cnt, constexpr_cnt, volatile_cnt,
3232
double_cnt, float_cnt, complex_cnt, cpp_bool_cnt, proper_bool_cnt,
33-
extern_cnt, wchar_t_cnt, char16_t_cnt, char32_t_cnt,
33+
extern_cnt, noreturn_cnt, wchar_t_cnt, char16_t_cnt, char32_t_cnt,
3434
int8_cnt, int16_cnt, int32_cnt, int64_cnt, ptr32_cnt, ptr64_cnt,
3535
float128_cnt, int128_cnt;
3636

@@ -53,7 +53,7 @@ void cpp_convert_typet::read(const typet &type)
5353
unsigned_cnt=signed_cnt=char_cnt=int_cnt=short_cnt=
5454
long_cnt=const_cnt=restrict_cnt=constexpr_cnt=volatile_cnt=
5555
double_cnt=float_cnt=complex_cnt=cpp_bool_cnt=proper_bool_cnt=
56-
extern_cnt=wchar_t_cnt=char16_t_cnt=char32_t_cnt=
56+
extern_cnt=noreturn_cnt=wchar_t_cnt=char16_t_cnt=char32_t_cnt=
5757
int8_cnt=int16_cnt=int32_cnt=int64_cnt=
5858
ptr32_cnt=ptr64_cnt=float128_cnt=int128_cnt=0;
5959

@@ -132,6 +132,10 @@ void cpp_convert_typet::read_rec(const typet &type)
132132
constexpr_cnt++;
133133
else if(type.id()==ID_extern)
134134
extern_cnt++;
135+
else if(type.id()==ID_noreturn)
136+
{
137+
noreturn_cnt++;
138+
}
135139
else if(type.id()==ID_function_type)
136140
{
137141
read_function_type(type);

src/cpp/cpp_typecheck.h

-2
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,6 @@ class cpp_typecheckt:public c_typecheck_baset
484484
void typecheck_method_application(
485485
side_effect_expr_function_callt &expr);
486486

487-
void typecheck_assign(codet &code);
488-
489487
public:
490488
//
491489
// Type Conversions

src/cpp/cpp_typecheck_code.cpp

+3-22
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ void cpp_typecheckt::typecheck_decl(codet &code)
388388
return;
389389
}
390390

391+
// mark as 'already typechecked'
392+
make_already_typechecked(type);
393+
391394
codet new_code(ID_decl_block);
392395
new_code.reserve_operands(declaration.declarators().size());
393396

@@ -453,25 +456,3 @@ void cpp_typecheckt::typecheck_block(codet &code)
453456

454457
c_typecheck_baset::typecheck_block(code);
455458
}
456-
457-
void cpp_typecheckt::typecheck_assign(codet &code)
458-
{
459-
if(code.operands().size()!=2)
460-
{
461-
error().source_location=code.find_source_location();
462-
error() << "assignment statement expected to have two operands"
463-
<< eom;
464-
throw 0;
465-
}
466-
467-
// turn into a side effect
468-
side_effect_exprt expr(code.get(ID_statement));
469-
expr.operands() = code.operands();
470-
typecheck_expr(expr);
471-
472-
code_expressiont code_expr;
473-
code_expr.expression()=expr;
474-
code_expr.add_source_location() = code.source_location();
475-
476-
code.swap(code_expr);
477-
}

src/cpp/cpp_typecheck_conversions.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,13 @@ void cpp_typecheckt::implicit_typecast(exprt &expr, const typet &type)
15081508
{
15091509
exprt e=expr;
15101510

1511+
if(
1512+
e.id() == ID_initializer_list && cpp_is_pod(type) &&
1513+
e.operands().size() == 1)
1514+
{
1515+
e = expr.op0();
1516+
}
1517+
15111518
if(!implicit_conversion_sequence(e, type, expr))
15121519
{
15131520
show_instantiation_stack(error());

src/cpp/cpp_typecheck_declaration.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ void cpp_typecheckt::convert_non_template_declaration(
130130
if(!is_typedef)
131131
elaborate_class_template(declaration_type);
132132

133+
// mark as 'already typechecked'
134+
if(!declaration.declarators().empty())
135+
make_already_typechecked(declaration_type);
136+
133137
// Special treatment for anonymous unions
134138
if(declaration.declarators().empty() &&
135139
follow(declaration.type()).get_bool(ID_C_is_anonymous))

src/cpp/cpp_typecheck_expr.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,25 @@ void cpp_typecheckt::typecheck_expr_explicit_typecast(exprt &expr)
939939
else
940940
typecheck_type(expr.type());
941941

942+
// We allow (TYPE){ initializer_list }
943+
// This is called "compound literal", and is syntactic
944+
// sugar for a (possibly local) declaration.
945+
if(expr.op0().id()==ID_initializer_list)
946+
{
947+
// just do a normal initialization
948+
do_initializer(expr.op0(), expr.type(), false);
949+
950+
// This produces a struct-expression,
951+
// union-expression, array-expression,
952+
// or an expression for a pointer or scalar.
953+
// We produce a compound_literal expression.
954+
exprt tmp(ID_compound_literal, expr.type());
955+
tmp.move_to_operands(expr.op0());
956+
expr=tmp;
957+
expr.set(ID_C_lvalue, true); // these are l-values
958+
return;
959+
}
960+
942961
exprt new_expr;
943962

944963
if(const_typecast(expr.op0(), expr.type(), new_expr) ||

src/cpp/cpp_typecheck_fargs.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ bool cpp_typecheck_fargst::match(
129129
std::cout << "OK " << rank << '\n';
130130
#endif
131131
}
132+
else if(
133+
operand.id() == ID_initializer_list && cpp_typecheck.cpp_is_pod(type) &&
134+
operand.operands().size() == 1 &&
135+
cpp_typecheck.implicit_conversion_sequence(
136+
operand.op0(), type, new_expr, rank))
137+
{
138+
distance += rank;
139+
}
132140
else
133141
{
134142
#if 0

src/cpp/cpp_typecheck_type.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ void cpp_typecheckt::typecheck_type(typet &type)
140140
if(type.subtype().get_bool(ID_C_volatile))
141141
type.set(ID_C_volatile, true);
142142
}
143+
else if(type.id()==ID_vector)
144+
{
145+
typecheck_vector_type(to_vector_type(type));
146+
}
143147
else if(type.id()==ID_code)
144148
{
145149
code_typet &code_type=to_code_type(type);
@@ -259,6 +263,10 @@ void cpp_typecheckt::typecheck_type(typet &type)
259263
else if(type.id()==ID_nullptr)
260264
{
261265
}
266+
else if(type.id()==ID_already_typechecked)
267+
{
268+
c_typecheck_baset::typecheck_type(type);
269+
}
262270
else
263271
{
264272
error().source_location=type.source_location();

src/cpp/cpp_util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exprt cpp_symbol_expr(const symbolt &symbol);
1717

1818
inline void already_typechecked(irept &irep)
1919
{
20-
exprt tmp("already_typechecked");
20+
exprt tmp(ID_already_typechecked);
2121
tmp.copy_to_operands(static_cast<exprt &>(irep));
2222
irep.swap(tmp);
2323
}

0 commit comments

Comments
 (0)