Skip to content

Make string_container static init more resilient #1218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/util/dstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ class dstringt final

// this one is not safe for static objects
// NOLINTNEXTLINE(runtime/explicit)
dstringt(const char *s):no(string_container[s])
dstringt(const char *s):no(get_string_container()[s])
{
}

// this one is not safe for static objects
// NOLINTNEXTLINE(runtime/explicit)
dstringt(const std::string &s):no(string_container[s])
dstringt(const std::string &s):no(get_string_container()[s])
{
}

Expand Down Expand Up @@ -152,12 +152,12 @@ class dstringt final

// the reference returned is guaranteed to be stable
const std::string &as_string() const
{ return string_container.get_string(no); }
{ return get_string_container().get_string(no); }
};

// the reference returned is guaranteed to be stable
inline const std::string &as_string(const dstringt &s)
{ return string_container.get_string(s.get_no()); }
{ return get_string_container().get_string(s.get_no()); }

// NOLINTNEXTLINE(readability/identifiers)
struct dstring_hash
Expand Down
9 changes: 5 additions & 4 deletions src/util/irep_ids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ const char *irep_ids_table[]=

#include "irep_ids.def" // NOLINT(build/include)

void initialize_string_container()
string_containert::string_containert()
{
// this is called by the constructor of string_containert
// pre-allocate empty string -- this gets index 0
get("");

// allocate strings
for(unsigned i=0; irep_ids_table[i]!=nullptr; i++)
{
unsigned x;
x=string_container[irep_ids_table[i]];
unsigned x=operator[](irep_ids_table[i]);
INVARIANT(x==i, "i-th element is inserted at position i"); // sanity check
}
}
20 changes: 7 additions & 13 deletions src/util/string_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ Author: Daniel Kroening, [email protected]

#include <cstring>

string_containert string_container;

string_ptrt::string_ptrt(const char *_s):s(_s), len(strlen(_s))
{
}
Expand All @@ -27,17 +25,6 @@ bool string_ptrt::operator==(const string_ptrt &other) const
return len==0 || memcmp(s, other.s, len)==0;
}

void initialize_string_container();

string_containert::string_containert()
{
// pre-allocate empty string -- this gets index 0
get("");

// allocate strings
initialize_string_container();
}

string_containert::~string_containert()
{
}
Expand Down Expand Up @@ -87,3 +74,10 @@ unsigned string_containert::get(const std::string &s)

return r;
}

/// Get a reference to the global string container.
string_containert &get_string_container()
{
static string_containert ret;
return ret;
}
3 changes: 1 addition & 2 deletions src/util/string_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class string_containert
string_vectort string_vector;
};

// an ugly global object
extern string_containert string_container;
string_containert &get_string_container();

#endif // CPROVER_UTIL_STRING_CONTAINER_H