Skip to content

Commit

Permalink
Merge pull request #8194 from diffblue/fluent-type-with-source-location
Browse files Browse the repository at this point in the history
add a fluent-style typet::with_source_location
  • Loading branch information
kroening authored Feb 6, 2024
2 parents fe3ec50 + f3ae685 commit 3dcf5f9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/ansi-c/c_typecheck_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,8 @@ void c_typecheck_baset::typecheck_type(typet &type)
else // give up, just use subtype
result = to_type_with_subtype(type).subtype();

// save the location
result.add_source_location()=type.source_location();

type=result;
// preserve the location
type = result.with_source_location(type);
}
else if(underlying_type.id()==ID_complex)
{
Expand All @@ -303,9 +301,7 @@ void c_typecheck_baset::typecheck_type(typet &type)
result = to_type_with_subtype(type).subtype();

// save the location
result.add_source_location()=type.source_location();

type=complex_typet(result);
type = complex_typet(result).with_source_location(type);
}
else
{
Expand Down Expand Up @@ -726,9 +722,8 @@ void c_typecheck_baset::typecheck_vector_type(typet &type)
// produce the type with ID_vector
vector_typet new_type(
c_index_type(), subtype, from_integer(s, signed_size_type()));
new_type.add_source_location() = source_location;
new_type.size().add_source_location() = source_location;
type = new_type;
type = new_type.with_source_location(source_location);
}

void c_typecheck_baset::typecheck_compound_type(struct_union_typet &type)
Expand Down
33 changes: 33 additions & 0 deletions src/util/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,39 @@ class typet:public irept
return static_cast<source_locationt &>(add(ID_C_source_location));
}

/// This is a 'fluent style' method for creating a new type
/// with an added-on source location.
typet &&with_source_location(source_locationt location) &&
{
if(location.is_not_nil())
add_source_location() = std::move(location);
return std::move(*this);
}

/// This is a 'fluent style' method for adding a source location.
typet &with_source_location(source_locationt location) &
{
if(location.is_not_nil())
add_source_location() = std::move(location);
return *this;
}

/// This is a 'fluent style' method for creating a new type
/// with an added-on source location.
typet &&with_source_location(const typet &type) &&
{
return std::move(*this).with_source_location(type.source_location());
}

/// This is a 'fluent style' method for adding a source location.
typet &with_source_location(const typet &type) &
{
auto &location = type.source_location();
if(location.is_not_nil())
add_source_location() = location;
return *this;
}

typet &add_type(const irep_idt &name)
{
return static_cast<typet &>(add(name));
Expand Down

0 comments on commit 3dcf5f9

Please sign in to comment.