Skip to content

Commit 3dcf5f9

Browse files
authored
Merge pull request #8194 from diffblue/fluent-type-with-source-location
add a fluent-style typet::with_source_location
2 parents fe3ec50 + f3ae685 commit 3dcf5f9

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/ansi-c/c_typecheck_type.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,8 @@ void c_typecheck_baset::typecheck_type(typet &type)
283283
else // give up, just use subtype
284284
result = to_type_with_subtype(type).subtype();
285285

286-
// save the location
287-
result.add_source_location()=type.source_location();
288-
289-
type=result;
286+
// preserve the location
287+
type = result.with_source_location(type);
290288
}
291289
else if(underlying_type.id()==ID_complex)
292290
{
@@ -303,9 +301,7 @@ void c_typecheck_baset::typecheck_type(typet &type)
303301
result = to_type_with_subtype(type).subtype();
304302

305303
// save the location
306-
result.add_source_location()=type.source_location();
307-
308-
type=complex_typet(result);
304+
type = complex_typet(result).with_source_location(type);
309305
}
310306
else
311307
{
@@ -726,9 +722,8 @@ void c_typecheck_baset::typecheck_vector_type(typet &type)
726722
// produce the type with ID_vector
727723
vector_typet new_type(
728724
c_index_type(), subtype, from_integer(s, signed_size_type()));
729-
new_type.add_source_location() = source_location;
730725
new_type.size().add_source_location() = source_location;
731-
type = new_type;
726+
type = new_type.with_source_location(source_location);
732727
}
733728

734729
void c_typecheck_baset::typecheck_compound_type(struct_union_typet &type)

src/util/type.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,39 @@ class typet:public irept
7979
return static_cast<source_locationt &>(add(ID_C_source_location));
8080
}
8181

82+
/// This is a 'fluent style' method for creating a new type
83+
/// with an added-on source location.
84+
typet &&with_source_location(source_locationt location) &&
85+
{
86+
if(location.is_not_nil())
87+
add_source_location() = std::move(location);
88+
return std::move(*this);
89+
}
90+
91+
/// This is a 'fluent style' method for adding a source location.
92+
typet &with_source_location(source_locationt location) &
93+
{
94+
if(location.is_not_nil())
95+
add_source_location() = std::move(location);
96+
return *this;
97+
}
98+
99+
/// This is a 'fluent style' method for creating a new type
100+
/// with an added-on source location.
101+
typet &&with_source_location(const typet &type) &&
102+
{
103+
return std::move(*this).with_source_location(type.source_location());
104+
}
105+
106+
/// This is a 'fluent style' method for adding a source location.
107+
typet &with_source_location(const typet &type) &
108+
{
109+
auto &location = type.source_location();
110+
if(location.is_not_nil())
111+
add_source_location() = location;
112+
return *this;
113+
}
114+
82115
typet &add_type(const irep_idt &name)
83116
{
84117
return static_cast<typet &>(add(name));

0 commit comments

Comments
 (0)