Skip to content

Commit 3ceb89b

Browse files
author
Daniel Kroening
authored
Merge pull request #1390 from diffblue/fix_pointer_type
Fix pointer type
2 parents 8bb39ca + 0cc696b commit 3ceb89b

9 files changed

+72
-24
lines changed

src/ansi-c/ansi_c_convert_type.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,20 @@ void ansi_c_convert_typet::read_rec(const typet &type)
216216
{
217217
c_storage_spec.alias=type.subtype().get(ID_value);
218218
}
219-
else if(type.id()==ID_pointer)
219+
else if(type.id()==ID_frontend_pointer)
220220
{
221-
// pointers have a width, much like integers
222-
pointer_typet tmp=to_pointer_type(type);
223-
tmp.set_width(config.ansi_c.pointer_width);
221+
// We turn ID_frontend_pointer to ID_pointer
222+
// Pointers have a width, much like integers,
223+
// which is added here.
224+
typet tmp(type);
225+
tmp.id(ID_pointer);
226+
tmp.set(ID_width, config.ansi_c.pointer_width);
224227
other.push_back(tmp);
225228
}
229+
else if(type.id()==ID_pointer)
230+
{
231+
UNREACHABLE;
232+
}
226233
else
227234
other.push_back(type);
228235
}

src/ansi-c/ansi_c_declaration.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ typet ansi_c_declarationt::full_type(
9898
// this gets types that are still raw parse trees
9999
while(p->is_not_nil())
100100
{
101-
if(p->id()==ID_pointer || p->id()==ID_array ||
101+
if(p->id()==ID_frontend_pointer || p->id()==ID_array ||
102102
p->id()==ID_vector || p->id()==ID_c_bit_field ||
103103
p->id()==ID_block_pointer || p->id()==ID_code)
104104
p=&p->subtype();

src/ansi-c/c_typecheck_type.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -1492,19 +1492,22 @@ void c_typecheck_baset::adjust_function_parameter(typet &type) const
14921492
{
14931493
if(type.id()==ID_array)
14941494
{
1495+
source_locationt source_location=type.source_location();
14951496
type=pointer_type(type.subtype());
1496-
type.remove(ID_size);
1497-
type.remove(ID_C_constant);
1497+
type.add_source_location()=source_location;
14981498
}
14991499
else if(type.id()==ID_code)
15001500
{
15011501
// see ISO/IEC 9899:1999 page 199 clause 8,
15021502
// may be hidden in typedef
1503+
source_locationt source_location=type.source_location();
15031504
type=pointer_type(type);
1505+
type.add_source_location()=source_location;
15041506
}
15051507
else if(type.id()==ID_KnR)
15061508
{
15071509
// any KnR args without type yet?
15081510
type=signed_int_type(); // the default is integer!
1511+
// no source location
15091512
}
15101513
}

src/ansi-c/parser.y

+28-7
Original file line numberDiff line numberDiff line change
@@ -3065,7 +3065,10 @@ unary_identifier_declarator:
30653065
{
30663066
// the type_qualifier_list is for the pointer,
30673067
// and not the identifier_declarator
3068-
stack_type($1)=pointer_type(typet(ID_abstract));
3068+
// The below is deliberately not using pointer_type();
3069+
// the width is added during conversion.
3070+
stack_type($1).id(ID_frontend_pointer);
3071+
stack_type($1).subtype()=typet(ID_abstract);
30693072
$2=merge($2, $1); // dest=$2
30703073
make_subtype($3, $2); // dest=$3
30713074
$$=$3;
@@ -3249,13 +3252,19 @@ unary_abstract_declarator:
32493252
'*'
32503253
{
32513254
$$=$1;
3252-
stack_type($$)=pointer_type(typet(ID_abstract));
3255+
// The below is deliberately not using pointer_type();
3256+
// the width is added during conversion.
3257+
stack_type($$).id(ID_frontend_pointer);
3258+
stack_type($$).subtype()=typet(ID_abstract);
32533259
}
32543260
| '*' attribute_type_qualifier_list
32553261
{
32563262
// The type_qualifier_list belongs to the pointer,
32573263
// not to the (missing) abstract declarator.
3258-
stack_type($1)=pointer_type(typet(ID_abstract));
3264+
// The below is deliberately not using pointer_type();
3265+
// the width is added during conversion.
3266+
stack_type($1).id(ID_frontend_pointer);
3267+
stack_type($1).subtype()=typet(ID_abstract);
32593268
$$=merge($2, $1);
32603269
}
32613270
| '*' abstract_declarator
@@ -3267,7 +3276,10 @@ unary_abstract_declarator:
32673276
{
32683277
// The type_qualifier_list belongs to the pointer,
32693278
// not to the abstract declarator.
3270-
stack_type($1)=pointer_type(typet(ID_abstract));
3279+
// The below is deliberately not using pointer_type();
3280+
// the width is added during conversion.
3281+
stack_type($1).id(ID_frontend_pointer);
3282+
stack_type($1).subtype()=typet(ID_abstract);
32713283
$2=merge($2, $1); // dest=$2
32723284
make_subtype($3, $2); // dest=$3
32733285
$$=$3;
@@ -3286,13 +3298,19 @@ parameter_unary_abstract_declarator:
32863298
'*'
32873299
{
32883300
$$=$1;
3289-
stack_type($$)=pointer_type(typet(ID_abstract));
3301+
// The below is deliberately not using pointer_type();
3302+
// the width is added during conversion.
3303+
stack_type($$).id(ID_frontend_pointer);
3304+
stack_type($$).subtype()=typet(ID_abstract);
32903305
}
32913306
| '*' attribute_type_qualifier_list
32923307
{
32933308
// The type_qualifier_list belongs to the pointer,
32943309
// not to the (missing) abstract declarator.
3295-
stack_type($1)=pointer_type(typet(ID_abstract));
3310+
// The below is deliberately not using pointer_type();
3311+
// the width is added during conversion.
3312+
stack_type($1).id(ID_frontend_pointer);
3313+
stack_type($1).subtype()=typet(ID_abstract);
32963314
$$=merge($2, $1);
32973315
}
32983316
| '*' parameter_abstract_declarator
@@ -3304,7 +3322,10 @@ parameter_unary_abstract_declarator:
33043322
{
33053323
// The type_qualifier_list belongs to the pointer,
33063324
// not to the (missing) abstract declarator.
3307-
stack_type($1)=pointer_type(typet(ID_abstract));
3325+
// The below is deliberately not using pointer_type();
3326+
// the width is added during conversion.
3327+
stack_type($1).id(ID_frontend_pointer);
3328+
stack_type($1).subtype()=typet(ID_abstract);
33083329
$2=merge($2, $1); // dest=$2
33093330
make_subtype($3, $2); // dest=$3
33103331
$$=$3;

src/ansi-c/parser_static.inc

+6-3
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ static void make_subtype(typet &dest, typet &src)
187187
#endif
188188

189189
assert(src.id()==ID_array ||
190-
src.id()==ID_pointer ||
190+
src.id()==ID_frontend_pointer ||
191191
src.id()==ID_code ||
192192
src.id()==ID_merged_type ||
193193
src.id()==ID_abstract ||
@@ -207,7 +207,7 @@ static void make_subtype(typet &dest, typet &src)
207207
sub=&(p->subtypes().back());
208208
}
209209

210-
if(sub->id()==ID_pointer ||
210+
if(sub->id()==ID_frontend_pointer ||
211211
sub->id()==ID_array ||
212212
sub->id()==ID_code)
213213
{
@@ -290,7 +290,10 @@ Function: make_pointer
290290

291291
static void make_pointer(const YYSTYPE dest)
292292
{
293-
stack_type(dest)=pointer_type(typet(ID_abstract));
293+
// The below deliberately avoids pointer_type().
294+
// The width is set during conversion.
295+
stack_type(dest).id(ID_frontend_pointer);
296+
stack_type(dest).subtype()=typet(ID_abstract);
294297
}
295298

296299
/*******************************************************************\

src/cpp/cpp_convert_type.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ void cpp_convert_typet::read_rec(const typet &type)
159159
tmp.id(ID_empty);
160160
other.push_back(tmp);
161161
}
162+
else if(type.id()==ID_frontend_pointer)
163+
{
164+
// add width and turn into ID_pointer
165+
typet tmp=type;
166+
tmp.id(ID_pointer);
167+
tmp.set(ID_width, config.ansi_c.pointer_width);
168+
other.push_back(tmp);
169+
}
170+
else if(type.id()==ID_pointer)
171+
{
172+
// ignore, we unfortunately convert multiple times
173+
other.push_back(type);
174+
}
162175
else
163176
{
164177
other.push_back(type);
@@ -549,7 +562,6 @@ void cpp_convert_plain_type(typet &type)
549562
if(type.id()==ID_cpp_name ||
550563
type.id()==ID_struct ||
551564
type.id()==ID_union ||
552-
type.id()==ID_pointer ||
553565
type.id()==ID_array ||
554566
type.id()==ID_code ||
555567
type.id()==ID_unsignedbv ||

src/cpp/cpp_convert_type.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ Author: Daniel Kroening, [email protected]
1414

1515
#include <util/type.h>
1616

17-
void cpp_convert_plain_type(typet &type);
17+
void cpp_convert_plain_type(typet &);
1818

1919
#endif // CPROVER_CPP_CPP_CONVERT_TYPE_H

src/cpp/parse.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ Author: Daniel Kroening, [email protected]
1818
#include <util/std_code.h>
1919
#include <util/std_expr.h>
2020
#include <util/std_types.h>
21-
#include <util/c_types.h>
2221

2322
#include <ansi-c/ansi_c_y.tab.h>
23+
#include <util/c_types.h>
2424

2525
#include "cpp_token_buffer.h"
2626
#include "cpp_member_spec.h"
@@ -3016,7 +3016,7 @@ bool Parser::optPtrOperator(typet &ptrs)
30163016

30173017
if(t=='*')
30183018
{
3019-
typet op=pointer_type(typet(ID_nil));
3019+
typet op(ID_frontend_pointer); // width gets set during conversion
30203020
cpp_tokent tk;
30213021
lex.get_token(tk);
30223022
set_location(op, tk);
@@ -3079,7 +3079,7 @@ bool Parser::optPtrOperator(typet &ptrs)
30793079
{
30803080
cpp_tokent tk;
30813081
lex.get_token(tk);
3082-
typet op=pointer_type(typet(ID_nil));
3082+
typet op(ID_frontend_pointer); // width gets set during conversion
30833083
op.set(ID_C_reference, true);
30843084
set_location(op, tk);
30853085
t_list.push_front(op);
@@ -3088,7 +3088,7 @@ bool Parser::optPtrOperator(typet &ptrs)
30883088
{
30893089
cpp_tokent tk;
30903090
lex.get_token(tk);
3091-
typet op=pointer_type(typet(ID_nil));
3091+
typet op(ID_frontend_pointer); // width gets set during conversion
30923092
op.set(ID_C_rvalue_reference, true);
30933093
set_location(op, tk);
30943094
t_list.push_front(op);
@@ -3531,7 +3531,7 @@ bool Parser::rPtrToMember(irept &ptr_to_mem)
35313531
std::cout << std::string(__indent, ' ') << "Parser::rPtrToMember 0\n";
35323532
#endif
35333533

3534-
typet ptm=pointer_type(typet(ID_nil));
3534+
typet ptm(ID_frontend_pointer); // width gets set during conversion
35353535
irept &name = ptm.add("to-member");
35363536
name=cpp_namet();
35373537
irept::subt &components=name.get_sub();
@@ -6478,6 +6478,7 @@ bool Parser::rPrimaryExpr(exprt &exp)
64786478

64796479
case TOK_NULLPTR:
64806480
lex.get_token(tk);
6481+
// as an exception, we set the width of pointer
64816482
exp=constant_exprt(ID_NULL, pointer_type(typet(ID_nullptr)));
64826483
set_location(exp, tk);
64836484
#ifdef DEBUG

src/util/irep_ids.def

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ IREP_ID_ONE(skip)
104104
IREP_ID_ONE(arguments)
105105
IREP_ID_ONE(array)
106106
IREP_ID_ONE(size)
107+
IREP_ID_ONE(frontend_pointer)
107108
IREP_ID_ONE(pointer)
108109
IREP_ID_ONE(block_pointer)
109110
IREP_ID_ONE(switch)

0 commit comments

Comments
 (0)