Skip to content

Commit db47f07

Browse files
authored
Merge pull request #6532 from tautschnig/is-null-pointer
Wrap NULL/0 is-a-null-pointer in a utility function
2 parents 1e5b7aa + 210b0ea commit db47f07

23 files changed

+120
-87
lines changed

src/analyses/custom_bitvector_analysis.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,9 @@ void custom_bitvector_domaint::transform(
345345

346346
if(lhs.type().id()==ID_pointer)
347347
{
348-
if(lhs.is_constant() &&
349-
to_constant_expr(lhs).get_value()==ID_NULL) // NULL means all
348+
if(
349+
lhs.is_constant() &&
350+
is_null_pointer(to_constant_expr(lhs))) // NULL means all
350351
{
351352
if(mode==modet::CLEAR_MAY)
352353
{
@@ -473,8 +474,9 @@ void custom_bitvector_domaint::transform(
473474

474475
if(lhs.type().id()==ID_pointer)
475476
{
476-
if(lhs.is_constant() &&
477-
to_constant_expr(lhs).get_value()==ID_NULL) // NULL means all
477+
if(
478+
lhs.is_constant() &&
479+
is_null_pointer(to_constant_expr(lhs))) // NULL means all
478480
{
479481
if(mode==modet::CLEAR_MAY)
480482
{
@@ -708,8 +710,9 @@ exprt custom_bitvector_domaint::eval(
708710
if(pointer.type().id()!=ID_pointer)
709711
return src;
710712

711-
if(pointer.is_constant() &&
712-
to_constant_expr(pointer).get_value()==ID_NULL) // NULL means all
713+
if(
714+
pointer.is_constant() &&
715+
is_null_pointer(to_constant_expr(pointer))) // NULL means all
713716
{
714717
if(src.id() == ID_get_may)
715718
{

src/analyses/invariant_set.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ std::string inv_object_storet::build_string(const exprt &expr) const
121121
if(expr.is_constant())
122122
{
123123
// NULL?
124-
if(expr.type().id()==ID_pointer)
125-
if(expr.get(ID_value)==ID_NULL)
126-
return "0";
124+
if(is_null_pointer(to_constant_expr(expr)))
125+
return "0";
127126

128127
const auto i = numeric_cast<mp_integer>(expr);
129128
if(i.has_value())

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,14 +1602,18 @@ void c_typecheck_baset::typecheck_expr_trinary(if_exprt &expr)
16021602

16031603
// is one of them void * AND null? Convert that to the other.
16041604
// (at least that's how GCC behaves)
1605-
if(operands[1].type().subtype().id()==ID_empty &&
1606-
tmp1.is_constant() &&
1607-
to_constant_expr(tmp1).get_value()==ID_NULL)
1605+
if(
1606+
operands[1].type().subtype().id() == ID_empty && tmp1.is_constant() &&
1607+
is_null_pointer(to_constant_expr(tmp1)))
1608+
{
16081609
implicit_typecast(operands[1], operands[2].type());
1609-
else if(operands[2].type().subtype().id()==ID_empty &&
1610-
tmp2.is_constant() &&
1611-
to_constant_expr(tmp2).get_value()==ID_NULL)
1610+
}
1611+
else if(
1612+
operands[2].type().subtype().id() == ID_empty && tmp2.is_constant() &&
1613+
is_null_pointer(to_constant_expr(tmp2)))
1614+
{
16121615
implicit_typecast(operands[2], operands[1].type());
1616+
}
16131617
else if(operands[1].type().subtype().id()!=ID_code ||
16141618
operands[2].type().subtype().id()!=ID_code)
16151619
{

src/ansi-c/expr2c.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ Author: Daniel Kroening, [email protected]
77
\*******************************************************************/
88

99
#include "expr2c.h"
10-
11-
#include <algorithm>
12-
#include <sstream>
13-
14-
#include <map>
10+
#include "expr2c_class.h"
1511

1612
#include <util/arith_tools.h>
1713
#include <util/c_types.h>
1814
#include <util/config.h>
1915
#include <util/cprover_prefix.h>
16+
#include <util/expr_util.h>
2017
#include <util/find_symbols.h>
2118
#include <util/fixedbv.h>
2219
#include <util/floatbv_expr.h>
@@ -33,7 +30,10 @@ Author: Daniel Kroening, [email protected]
3330

3431
#include "c_misc.h"
3532
#include "c_qualifiers.h"
36-
#include "expr2c_class.h"
33+
34+
#include <algorithm>
35+
#include <map>
36+
#include <sstream>
3737

3838
// clang-format off
3939

@@ -1983,9 +1983,7 @@ std::string expr2ct::convert_constant(
19831983
}
19841984
else if(type.id()==ID_pointer)
19851985
{
1986-
if(
1987-
value == ID_NULL ||
1988-
(value == std::string(value.size(), '0') && config.ansi_c.NULL_is_zero))
1986+
if(is_null_pointer(src))
19891987
{
19901988
if(configuration.use_library_macros)
19911989
dest = "NULL";

src/cpp/cpp_typecheck_conversions.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,7 @@ bool cpp_typecheckt::standard_conversion_pointer_to_member(
601601
if(expr.get_bool(ID_C_lvalue))
602602
return false;
603603

604-
if(expr.id()==ID_constant &&
605-
expr.get(ID_value)==ID_NULL)
604+
if(expr.id() == ID_constant && is_null_pointer(to_constant_expr(expr)))
606605
{
607606
new_expr = typecast_exprt::conditional_cast(expr, type);
608607
return true;

src/goto-instrument/dump_c.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,23 @@ Author: Daniel Kroening, [email protected]
1010
/// Dump Goto-Program as C/C++ Source
1111

1212
#include "dump_c.h"
13+
#include "dump_c_class.h"
1314

1415
#include <util/byte_operators.h>
1516
#include <util/c_types.h>
1617
#include <util/config.h>
1718
#include <util/expr_initializer.h>
19+
#include <util/expr_util.h>
1820
#include <util/find_symbols.h>
1921
#include <util/get_base_name.h>
2022
#include <util/invariant.h>
21-
#include <util/namespace.h>
2223
#include <util/replace_symbol.h>
23-
#include <util/std_code.h>
2424
#include <util/string_utils.h>
2525

2626
#include <ansi-c/expr2c.h>
2727
#include <cpp/expr2cpp.h>
28-
2928
#include <linking/static_lifetime_init.h>
3029

31-
#include "dump_c_class.h"
3230
#include "goto_program2code.h"
3331

3432
dump_c_configurationt dump_c_configurationt::default_configuration =
@@ -1325,11 +1323,9 @@ void dump_ct::cleanup_expr(exprt &expr)
13251323
expr = struct_exprt({}, struct_typet());
13261324
}
13271325
// add a typecast for NULL
1328-
else if(u.op().id()==ID_constant &&
1329-
u.op().type().id()==ID_pointer &&
1330-
u.op().type().subtype().id()==ID_empty &&
1331-
(u.op().is_zero() ||
1332-
to_constant_expr(u.op()).get_value()==ID_NULL))
1326+
else if(
1327+
u.op().id() == ID_constant && is_null_pointer(to_constant_expr(u.op())) &&
1328+
u.op().type().subtype().id() == ID_empty)
13331329
{
13341330
const struct_union_typet::componentt &comp=
13351331
u_type_f.get_component(u.get_component_name());
@@ -1385,8 +1381,7 @@ void dump_ct::cleanup_expr(exprt &expr)
13851381
// add a typecast for NULL or 0
13861382
if(
13871383
argument.id() == ID_constant &&
1388-
(argument.is_zero() ||
1389-
to_constant_expr(argument).get_value() == ID_NULL))
1384+
is_null_pointer(to_constant_expr(argument)))
13901385
{
13911386
const typet &comp_type=
13921387
to_union_type(type).components().front().type();

src/goto-instrument/dump_c_class.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ Author: Daniel Kroening, [email protected]
1212
#ifndef CPROVER_GOTO_INSTRUMENT_DUMP_C_CLASS_H
1313
#define CPROVER_GOTO_INSTRUMENT_DUMP_C_CLASS_H
1414

15-
#include <set>
16-
#include <string>
17-
15+
#include <util/namespace.h>
16+
#include <util/std_code.h>
1817
#include <util/symbol_table.h>
1918

2019
#include <goto-programs/system_library_symbols.h>
2120

21+
#include <set>
22+
#include <string>
23+
#include <unordered_set>
24+
2225
/// Used for configuring the behaviour of dump_c
2326
struct dump_c_configurationt final
2427
{

src/goto-instrument/goto_program2code.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ goto_programt::const_targett goto_program2codet::convert_assign_varargs(
309309
const exprt this_va_list_expr = target->assign_lhs();
310310
const exprt &r = skip_typecast(target->assign_rhs());
311311

312-
if(r.id()==ID_constant &&
313-
(r.is_zero() || to_constant_expr(r).get_value()==ID_NULL))
312+
if(r.id() == ID_constant && is_null_pointer(to_constant_expr(r)))
314313
{
315314
code_function_callt f(
316315
symbol_exprt("va_end", code_typet({}, empty_typet())),

src/goto-programs/xml_expr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Author: Daniel Kroening
1616
#include <util/arith_tools.h>
1717
#include <util/c_types.h>
1818
#include <util/config.h>
19+
#include <util/expr_util.h>
1920
#include <util/fixedbv.h>
2021
#include <util/ieee_float.h>
2122
#include <util/invariant.h>
@@ -220,7 +221,7 @@ xmlt xml(const exprt &expr, const namespacet &ns)
220221
result.name = "pointer";
221222
result.set_attribute(
222223
"binary", integer2binary(bvrep2integer(value, width, false), width));
223-
if(constant_expr.get(ID_value) == ID_NULL)
224+
if(is_null_pointer(constant_expr))
224225
result.data = "NULL";
225226
}
226227
else if(type.id() == ID_bool)

src/goto-symex/symex_goto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static optionalt<renamedt<exprt, L2>> try_evaluate_pointer_comparison(
9191

9292
if(
9393
skip_typecast(other_operand).id() != ID_address_of &&
94-
(!constant_expr || constant_expr->get_value() != ID_NULL))
94+
(!constant_expr || !is_null_pointer(*constant_expr)))
9595
{
9696
return {};
9797
}

0 commit comments

Comments
 (0)