Skip to content

Commit 573153a

Browse files
authored
Merge pull request #2957 from hannes-steffenhagen-diffblue/invariant-cleanup-util_dir-tz_files-req_exception_review
Invariant cleanup util dir tz files req exception review
2 parents 8c4656b + d786c91 commit 573153a

File tree

7 files changed

+46
-40
lines changed

7 files changed

+46
-40
lines changed

src/util/tempdir.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Author: CM Wintersteiger
3232
#include <unistd.h>
3333
#endif
3434

35-
#include "invariant.h"
35+
#include "exception_utils.h"
3636
#include "file_util.h"
3737

3838
std::string get_temporary_directory(const std::string &name_template)
@@ -45,7 +45,9 @@ std::string get_temporary_directory(const std::string &name_template)
4545
DWORD dwRetVal = GetTempPathA(dwBufSize, lpPathBuffer);
4646

4747
if(dwRetVal > dwBufSize || (dwRetVal == 0))
48-
throw "GetTempPath failed"; // NOLINT(readability/throw)
48+
{
49+
throw system_exceptiont("Couldn't get temporary path");
50+
}
4951

5052
// GetTempFileNameA produces <path>\<pre><uuuu>.TMP
5153
// where <pre> = "TLO"
@@ -54,12 +56,18 @@ std::string get_temporary_directory(const std::string &name_template)
5456
char t[MAX_PATH];
5557
UINT uRetVal=GetTempFileNameA(lpPathBuffer, "TLO", 0, t);
5658
if(uRetVal == 0)
57-
throw "GetTempFileName failed"; // NOLINT(readability/throw)
59+
{
60+
throw system_exceptiont(
61+
std::string("Couldn't get new temporary file name in directory") +
62+
lpPathBuffer);
63+
}
5864

5965
unlink(t);
60-
if(_mkdir(t)!=0)
61-
throw "_mkdir failed";
62-
66+
if(_mkdir(t) != 0)
67+
{
68+
throw system_exceptiont(
69+
std::string("Couldn't create temporary directory at ") + t);
70+
}
6371
result=std::string(t);
6472

6573
#else
@@ -75,7 +83,7 @@ std::string get_temporary_directory(const std::string &name_template)
7583
t.push_back('\0'); // add the zero
7684
const char *td = mkdtemp(t.data());
7785
if(!td)
78-
throw "mkdtemp failed";
86+
throw system_exceptiont("Failed to create temporary directory");
7987
result=std::string(td);
8088
#endif
8189

src/util/tempfile.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Author: Daniel Kroening
3131
#include <cstdio>
3232
#include <cstring>
3333

34+
#include "exception_utils.h"
35+
3436
#if defined(__linux__) || \
3537
defined(__FreeBSD_kernel__) || \
3638
defined(__GNU__) || \
@@ -104,7 +106,7 @@ std::string get_temporary_file(
104106
lpTempPathBuffer); // buffer for path
105107

106108
if(dwRetVal>MAX_PATH || (dwRetVal==0))
107-
throw "GetTempPath failed"; // NOLINT(readability/throw)
109+
throw system_exceptiont("Failed to get temporary directory");
108110

109111
// the path returned by GetTempPath ends with a backslash
110112
std::string t_template=
@@ -127,7 +129,7 @@ std::string get_temporary_file(
127129
int fd=mkstemps(t_ptr, suffix.size());
128130

129131
if(fd<0)
130-
throw "mkstemps failed";
132+
throw system_exceptiont("Failed to open temporary file");
131133

132134
close(fd);
133135

src/util/type_eq.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Author: Daniel Kroening, kroening@kroening.com
1212

1313
#include "type_eq.h"
1414

15+
#include "invariant.h"
1516
#include "namespace.h"
1617
#include "std_types.h"
1718
#include "symbol.h"
@@ -35,18 +36,14 @@ bool type_eq(const typet &type1, const typet &type2, const namespacet &ns)
3536
if(const auto symbol_type1 = type_try_dynamic_cast<symbol_typet>(type1))
3637
{
3738
const symbolt &symbol = ns.lookup(*symbol_type1);
38-
if(!symbol.is_type)
39-
throw "symbol "+id2string(symbol.name)+" is not a type";
40-
39+
CHECK_RETURN(symbol.is_type);
4140
return type_eq(symbol.type, type2, ns);
4241
}
4342

4443
if(const auto symbol_type2 = type_try_dynamic_cast<symbol_typet>(type2))
4544
{
4645
const symbolt &symbol = ns.lookup(*symbol_type2);
47-
if(!symbol.is_type)
48-
throw "symbol "+id2string(symbol.name)+" is not a type";
49-
46+
CHECK_RETURN(symbol.is_type);
5047
return type_eq(type1, symbol.type, ns);
5148
}
5249

src/util/union_find.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,15 @@ void unsigned_union_find::isolate(size_type a)
4646
if(is_root(a))
4747
{
4848
size_type c=nodes[a].count;
49+
DATA_INVARIANT(c != 0, "a root cannot have a node count of zero");
4950

5051
// already isolated?
5152
if(c==1)
5253
return;
5354

54-
assert(c>=2);
55-
5655
// find a new root
5756
size_type new_root=get_other(a);
58-
assert(new_root!=a);
57+
CHECK_RETURN(new_root != a);
5958

6059
re_root(a, new_root);
6160
}
@@ -64,8 +63,6 @@ void unsigned_union_find::isolate(size_type a)
6463
// get its root
6564
size_type r=find(a);
6665

67-
// assert(r!=a);
68-
6966
nodes[r].count--;
7067
nodes[a].parent=a;
7168
nodes[a].count=1;
@@ -80,13 +77,11 @@ void unsigned_union_find::re_root(size_type old_root, size_type new_root)
8077
old_root=find(old_root);
8178

8279
// same set?
83-
// assert(find(new_root)==old_root);
8480
if(find(new_root)!=old_root)
8581
return;
8682

87-
// make sure we actually do something
88-
assert(new_root!=old_root);
89-
assert(nodes[old_root].count>=2);
83+
PRECONDITION(!is_root(new_root));
84+
PRECONDITION(nodes[old_root].count >= 2);
9085

9186
nodes[new_root].parent=new_root;
9287
nodes[new_root].count=nodes[old_root].count;
@@ -110,7 +105,8 @@ unsigned_union_find::size_type unsigned_union_find::get_other(size_type a)
110105
check_index(a);
111106
a=find(a);
112107

113-
assert(nodes[a].count>=2);
108+
// Cannot find another node in a singleton set
109+
PRECONDITION(nodes[a].count >= 2);
114110

115111
// find a different member of the same set
116112
for(size_type i=0; i<size(); i++)

src/util/union_find.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Author: Daniel Kroening, kroening@kroening.com
1313
#include <cassert>
1414
#include <vector>
1515

16+
#include "invariant.h"
1617
#include "numbering.h"
1718

1819
// Standard union find with weighting and path compression.
@@ -63,8 +64,11 @@ class unsigned_union_find
6364

6465
void resize(size_type size)
6566
{
66-
// We only enlarge. Shrinking is yet to be implemented.
67-
assert(nodes.size()<=size);
67+
if(size < nodes.size())
68+
{
69+
INVARIANT(false, "we don't implement shrinking yet");
70+
}
71+
6872
nodes.reserve(size);
6973
while(nodes.size()<size)
7074
nodes.push_back(nodet(nodes.size()));

src/util/xml.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Author: Daniel Kroening, kroening@kroening.com
1010

1111
#include <ostream>
1212

13+
#include "exception_utils.h"
1314
#include "string2int.h"
1415

1516
void xmlt::clear()
@@ -240,7 +241,7 @@ std::string xmlt::unescape(const std::string &str)
240241
result+=c;
241242
}
242243
else
243-
throw "XML escape code not implemented"; // NOLINT(readability/throw)
244+
throw deserialization_exceptiont("unknown XML escape code: " + tmp);
244245
}
245246
else
246247
result+=*it;

src/util/xml_expr.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ Author: Daniel Kroening
1313

1414
#include "xml_expr.h"
1515

16-
#include "namespace.h"
17-
#include "expr.h"
18-
#include "xml.h"
1916
#include "arith_tools.h"
20-
#include "ieee_float.h"
17+
#include "config.h"
18+
#include "expr.h"
2119
#include "fixedbv.h"
20+
#include "ieee_float.h"
21+
#include "invariant.h"
22+
#include "namespace.h"
2223
#include "std_expr.h"
23-
#include "config.h"
24+
#include "xml.h"
2425

2526
xmlt xml(const source_locationt &location)
2627
{
@@ -273,7 +274,7 @@ xmlt xml(
273274
{
274275
const struct_typet &struct_type=to_struct_type(type);
275276
const struct_typet::componentst &components=struct_type.components();
276-
assert(components.size()==expr.operands().size());
277+
PRECONDITION(components.size() == expr.operands().size());
277278

278279
for(unsigned m=0; m<expr.operands().size(); m++)
279280
{
@@ -285,15 +286,12 @@ xmlt xml(
285286
}
286287
else if(expr.id()==ID_union)
287288
{
289+
const union_exprt &union_expr = to_union_expr(expr);
288290
result.name="union";
289291

290-
assert(expr.operands().size()==1);
291-
292292
xmlt &e=result.new_element("member");
293-
e.new_element(xml(expr.op0(), ns));
294-
e.set_attribute(
295-
"member_name",
296-
id2string(to_union_expr(expr).get_component_name()));
293+
e.new_element(xml(union_expr.op(), ns));
294+
e.set_attribute("member_name", id2string(union_expr.get_component_name()));
297295
}
298296
else
299297
result.name="unknown";

0 commit comments

Comments
 (0)