Skip to content

Commit 0557549

Browse files
Address comments by @danpoe
1 parent 702ab09 commit 0557549

File tree

5 files changed

+59
-21
lines changed

5 files changed

+59
-21
lines changed

regression/invariants/driver.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@ class structured_error_testt: public invariant_failedt
5656
}
5757
};
5858

59+
struct DiagnosticA {};
60+
struct DiagnosticB {};
61+
62+
template<>
63+
struct diagnostics_helpert<DiagnosticA>
64+
{
65+
static std::string diagnostics_as_string(const DiagnosticA&)
66+
{
67+
return "Diagnostic A";
68+
}
69+
};
70+
71+
template<>
72+
struct diagnostics_helpert<DiagnosticB>
73+
{
74+
static std::string diagnostics_as_string(const DiagnosticB&)
75+
{
76+
return "Diagnostic B";
77+
}
78+
};
79+
5980
/// Causes an invariant failure dependent on first argument value.
6081
/// One ignored argument is accepted to conform with the test.pl script,
6182
/// which would be the input source file for other cbmc driver programs.
@@ -118,6 +139,12 @@ int main(int argc, char** argv)
118139
"diagnostic 2",
119140
"diagnostic 3",
120141
"diagnostic 4");
142+
else if(arg == "invariant-with-custom-diagnostics")
143+
INVARIANT_WITH_DIAGNOSTICS(
144+
false,
145+
"an invariant with some custom diagnostics",
146+
DiagnosticA{},
147+
DiagnosticB{});
121148
else
122149
return 1;
123150
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
dummy_parameter.c
3+
invariant-with-lots-of-diagnostics
4+
^EXIT=(0|127|134|137)$
5+
^SIGNAL=0$
6+
--- begin invariant violation report ---
7+
Invariant check failed
8+
diagnostic 1
9+
diagnostic 2
10+
diagnostic 3
11+
diagnostic 4
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
dummy_parameter.c
3+
invariant-with-custom-diagnostics
4+
^EXIT=(0|127|134|137)$
5+
^SIGNAL=0$
6+
--- begin invariant violation report ---
7+
Invariant check failed
8+
Diagnostic A
9+
Diagnostic B

src/util/invariant.h

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,6 @@ class invariant_with_diagnostics_failedt : public invariant_failedt
142142
#else
143143
#define CBMC_NORETURN
144144
#endif
145-
// The macros MACRO<n> (e.g., INVARIANT2) are for internal use in this file
146-
// only. The corresponding macros that take a variable number of arguments (see
147-
// further below) should be used instead, which in turn call those with a fixed
148-
// number of arguments. For example, if INVARIANT(...) is called with two
149-
// arguments, it calls INVARIANT2().
150145

151146
#if defined(CPROVER_INVARIANT_CPROVER_ASSERT)
152147
// Used to allow CPROVER to check itself
@@ -321,17 +316,17 @@ inline void write_rest_diagnostics(std::ostream &)
321316
template <typename T, typename... Ts>
322317
void write_rest_diagnostics(std::ostream &out, T &&next, Ts &&... rest)
323318
{
324-
out << ", " << diagnostic_as_string(std::forward<T>(next));
319+
out << "\n" << diagnostic_as_string(std::forward<T>(next));
325320
write_rest_diagnostics(out, std::forward<Ts>(rest)...);
326321
}
327322

328-
template <typename T, typename... Ts>
329-
std::string assemble_diagnostics(T &&first, Ts &&... rest)
323+
template <typename... Diagnostics>
324+
std::string assemble_diagnostics(Diagnostics &&... args)
330325
{
331326
std::ostringstream out;
332-
out << "[ " << diagnostic_as_string(std::forward<T>(first));
333-
write_rest_diagnostics(out, std::forward<Ts>(rest)...);
334-
out << " ]";
327+
out << "\n<< EXTRA DIAGNOSTICS >>";
328+
write_rest_diagnostics(out, std::forward<Diagnostics>(args)...);
329+
out << "\n<< END EXTRA DIAGNOSTICS >>";
335330
return out.str();
336331
}
337332
} // namespace detail
@@ -457,18 +452,14 @@ CBMC_NORETURN void report_invariant_failure(
457452
#define CHECK_RETURN_STRUCTURED(CONDITION, TYPENAME, ...) \
458453
EXPAND_MACRO(INVARIANT_STRUCTURED(CONDITION, TYPENAME, __VA_ARGS__))
459454

460-
// This should be used to mark dead code
455+
/// This should be used to mark dead code
461456
#define UNREACHABLE INVARIANT(false, "Unreachable")
462457
#define UNREACHABLE_STRUCTURED(TYPENAME, ...) \
463458
EXPAND_MACRO(INVARIANT_STRUCTURED(false, TYPENAME, __VA_ARGS__))
464459

465-
// This condition should be used to document that assumptions that are
466-
// made on goto_functions, goto_programs, exprts, etc. being well formed.
467-
// "The data structure is not corrupt or malformed"
468-
#define DATA_INVARIANT2(CONDITION, REASON) INVARIANT2(CONDITION, REASON)
469-
#define DATA_INVARIANT3(CONDITION, REASON, DIAGNOSTICS) \
470-
INVARIANT3(CONDITION, REASON, DIAGNOSTICS)
471-
460+
/// This condition should be used to document that assumptions that are
461+
/// made on goto_functions, goto_programs, exprts, etc. being well formed.
462+
/// "The data structure is not corrupt or malformed"
472463
#define DATA_INVARIANT(CONDITION, REASON) INVARIANT(CONDITION, REASON)
473464
#define DATA_INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON, ...) \
474465
INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON, __VA_ARGS__)
@@ -482,6 +473,6 @@ CBMC_NORETURN void report_invariant_failure(
482473
// to migrate documentation and "error handling" in older code.
483474
#define TODO INVARIANT(false, "Todo")
484475
#define UNIMPLEMENTED INVARIANT(false, "Unimplemented")
485-
#define UNHANDLED_CASE INVARIANT2(false, "Unhandled case")
476+
#define UNHANDLED_CASE INVARIANT(false, "Unhandled case")
486477

487478
#endif // CPROVER_UTIL_INVARIANT_H

src/util/source_location.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ struct diagnostics_helpert<source_locationt>
197197
static std::string
198198
diagnostics_as_string(const source_locationt &source_location)
199199
{
200-
return source_location.as_string();
200+
return "source location: " + source_location.as_string();
201201
}
202202
};
203203

0 commit comments

Comments
 (0)