Skip to content

Commit

Permalink
Remove uses of dynamic_cast from qualifierst hierarchy
Browse files Browse the repository at this point in the history
There is no need to use virtual methods as we always know from context
the exact type we are working with.
  • Loading branch information
tautschnig committed Aug 6, 2024
1 parent b335979 commit 02ae48e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 58 deletions.
28 changes: 9 additions & 19 deletions jbmc/src/java_bytecode/java_qualifiers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,26 @@ void java_qualifierst::write(typet &src) const
type_checked_cast<annotated_typet>(src).get_annotations() = annotations;
}

qualifierst &java_qualifierst::operator+=(const qualifierst &other)
java_qualifierst &java_qualifierst::operator+=(const java_qualifierst &other)

Check warning on line 54 in jbmc/src/java_bytecode/java_qualifiers.cpp

View check run for this annotation

Codecov / codecov/patch

jbmc/src/java_bytecode/java_qualifiers.cpp#L54

Added line #L54 was not covered by tests
{
c_qualifierst::operator+=(other);
auto jq = dynamic_cast<const java_qualifierst *>(&other);
if(jq != nullptr)
{
std::copy(
jq->annotations.begin(),
jq->annotations.end(),
std::back_inserter(annotations));
}
std::copy(

Check warning on line 57 in jbmc/src/java_bytecode/java_qualifiers.cpp

View check run for this annotation

Codecov / codecov/patch

jbmc/src/java_bytecode/java_qualifiers.cpp#L57

Added line #L57 was not covered by tests
other.annotations.begin(),
other.annotations.end(),
std::back_inserter(annotations));

Check warning on line 60 in jbmc/src/java_bytecode/java_qualifiers.cpp

View check run for this annotation

Codecov / codecov/patch

jbmc/src/java_bytecode/java_qualifiers.cpp#L60

Added line #L60 was not covered by tests
return *this;
}

bool java_qualifierst::operator==(const qualifierst &other) const
bool java_qualifierst::operator==(const java_qualifierst &other) const

Check warning on line 64 in jbmc/src/java_bytecode/java_qualifiers.cpp

View check run for this annotation

Codecov / codecov/patch

jbmc/src/java_bytecode/java_qualifiers.cpp#L64

Added line #L64 was not covered by tests
{
auto jq = dynamic_cast<const java_qualifierst *>(&other);
if(jq == nullptr)
return false;
return c_qualifierst::operator==(other) && annotations == jq->annotations;
return c_qualifierst::operator==(other) && annotations == other.annotations;

Check warning on line 66 in jbmc/src/java_bytecode/java_qualifiers.cpp

View check run for this annotation

Codecov / codecov/patch

jbmc/src/java_bytecode/java_qualifiers.cpp#L66

Added line #L66 was not covered by tests
}

bool java_qualifierst::is_subset_of(const qualifierst &other) const
bool java_qualifierst::is_subset_of(const java_qualifierst &other) const

Check warning on line 69 in jbmc/src/java_bytecode/java_qualifiers.cpp

View check run for this annotation

Codecov / codecov/patch

jbmc/src/java_bytecode/java_qualifiers.cpp#L69

Added line #L69 was not covered by tests
{
if(!c_qualifierst::is_subset_of(other))
return false;
auto jq = dynamic_cast<const java_qualifierst *>(&other);
if(jq == nullptr)
return annotations.empty();
auto &other_a = jq->annotations;
auto &other_a = other.annotations;

Check warning on line 73 in jbmc/src/java_bytecode/java_qualifiers.cpp

View check run for this annotation

Codecov / codecov/patch

jbmc/src/java_bytecode/java_qualifiers.cpp#L73

Added line #L73 was not covered by tests
for(const auto &annotation : annotations)
{
if(std::find(other_a.begin(), other_a.end(), annotation) == other_a.end())
Expand Down
10 changes: 7 additions & 3 deletions jbmc/src/java_bytecode/java_qualifiers.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class java_qualifierst : public c_qualifierst
public:
virtual std::unique_ptr<qualifierst> clone() const override;

virtual qualifierst &operator+=(const qualifierst &other) override;
java_qualifierst &operator+=(const java_qualifierst &other);

const std::vector<java_annotationt> &get_annotations() const
{
Expand All @@ -38,8 +38,12 @@ class java_qualifierst : public c_qualifierst
virtual void read(const typet &src) override;
virtual void write(typet &src) const override;

virtual bool is_subset_of(const qualifierst &other) const override;
virtual bool operator==(const qualifierst &other) const override;
bool is_subset_of(const java_qualifierst &other) const;
bool operator==(const java_qualifierst &other) const;
bool operator!=(const java_qualifierst &other) const
{
return !(*this == other);
}

virtual std::string as_string() const override;
};
Expand Down
68 changes: 32 additions & 36 deletions src/ansi-c/c_qualifiers.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,13 @@ class qualifierst
public:
virtual std::unique_ptr<qualifierst> clone() const = 0;

virtual qualifierst &operator+=(const qualifierst &b) = 0;

virtual std::size_t count() const = 0;

virtual void clear() = 0;

virtual void read(const typet &src) = 0;
virtual void write(typet &src) const = 0;

// Comparisons
virtual bool is_subset_of(const qualifierst &q) const = 0;
virtual bool operator==(const qualifierst &other) const = 0;
bool operator!=(const qualifierst &other) const
{
return !(*this == other);
}

// String conversion
virtual std::string as_string() const = 0;
friend std::ostream &operator<<(std::ostream &, const qualifierst &);
Expand Down Expand Up @@ -107,41 +97,47 @@ class c_qualifierst : public qualifierst

static void clear(typet &dest);

virtual bool is_subset_of(const qualifierst &other) const override
bool is_subset_of(const c_qualifierst &other) const
{
const c_qualifierst *cq = dynamic_cast<const c_qualifierst *>(&other);
return (!is_constant || cq->is_constant) &&
(!is_volatile || cq->is_volatile) &&
(!is_restricted || cq->is_restricted) &&
(!is_atomic || cq->is_atomic) && (!is_ptr32 || cq->is_ptr32) &&
(!is_ptr64 || cq->is_ptr64) && (!is_nodiscard || cq->is_nodiscard) &&
(!is_noreturn || cq->is_noreturn);
return (!is_constant || other.is_constant) &&
(!is_volatile || other.is_volatile) &&
(!is_restricted || other.is_restricted) &&
(!is_atomic || other.is_atomic) && (!is_ptr32 || other.is_ptr32) &&
(!is_ptr64 || other.is_ptr64) &&
(!is_nodiscard || other.is_nodiscard) &&
(!is_noreturn || other.is_noreturn);

// is_transparent_union isn't checked
}

virtual bool operator==(const qualifierst &other) const override
bool operator==(const c_qualifierst &other) const
{
const c_qualifierst *cq = dynamic_cast<const c_qualifierst *>(&other);
return is_constant == cq->is_constant && is_volatile == cq->is_volatile &&
is_restricted == cq->is_restricted && is_atomic == cq->is_atomic &&
is_ptr32 == cq->is_ptr32 && is_ptr64 == cq->is_ptr64 &&
is_transparent_union == cq->is_transparent_union &&
is_nodiscard == cq->is_nodiscard && is_noreturn == cq->is_noreturn;
return is_constant == other.is_constant &&
is_volatile == other.is_volatile &&
is_restricted == other.is_restricted &&

Check warning on line 117 in src/ansi-c/c_qualifiers.h

View check run for this annotation

Codecov / codecov/patch

src/ansi-c/c_qualifiers.h#L116-L117

Added lines #L116 - L117 were not covered by tests
is_atomic == other.is_atomic && is_ptr32 == other.is_ptr32 &&
is_ptr64 == other.is_ptr64 &&
is_transparent_union == other.is_transparent_union &&
is_nodiscard == other.is_nodiscard &&
is_noreturn == other.is_noreturn;
}

bool operator!=(const c_qualifierst &other) const
{
return !(*this == other);
}

virtual qualifierst &operator+=(const qualifierst &other) override
c_qualifierst &operator+=(const c_qualifierst &other)
{
const c_qualifierst *cq = dynamic_cast<const c_qualifierst *>(&other);
is_constant |= cq->is_constant;
is_volatile |= cq->is_volatile;
is_restricted |= cq->is_restricted;
is_atomic |= cq->is_atomic;
is_ptr32 |= cq->is_ptr32;
is_ptr64 |= cq->is_ptr64;
is_transparent_union |= cq->is_transparent_union;
is_nodiscard |= cq->is_nodiscard;
is_noreturn |= cq->is_noreturn;
is_constant |= other.is_constant;
is_volatile |= other.is_volatile;
is_restricted |= other.is_restricted;
is_atomic |= other.is_atomic;
is_ptr32 |= other.is_ptr32;
is_ptr64 |= other.is_ptr64;
is_transparent_union |= other.is_transparent_union;
is_nodiscard |= other.is_nodiscard;
is_noreturn |= other.is_noreturn;
return *this;
}

Expand Down

0 comments on commit 02ae48e

Please sign in to comment.