Skip to content

Commit 8a21dea

Browse files
committed
Re-use validate_expr in to_code_*t
This provides two advantages: 1) Member access such as testing the number of operands does not break sharing, because it is done in a const context. 2) Code re-use: we no longer implement the same check in two different ways. All changes in this commit were applied automatically via text replacement.
1 parent 1d7789f commit 8a21dea

File tree

1 file changed

+86
-68
lines changed

1 file changed

+86
-68
lines changed

src/util/std_code.h

Lines changed: 86 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,17 @@ inline void validate_expr(const code_assumet &x)
562562
inline const code_assumet &to_code_assume(const codet &code)
563563
{
564564
PRECONDITION(code.get_statement() == ID_assume);
565-
return static_cast<const code_assumet &>(code);
565+
const code_assumet &ret = static_cast<const code_assumet &>(code);
566+
validate_expr(ret);
567+
return ret;
566568
}
567569

568570
inline code_assumet &to_code_assume(codet &code)
569571
{
570572
PRECONDITION(code.get_statement() == ID_assume);
571-
return static_cast<code_assumet &>(code);
573+
code_assumet &ret = static_cast<code_assumet &>(code);
574+
validate_expr(ret);
575+
return ret;
572576
}
573577

574578
/// A non-fatal assertion, which checks a condition then permits execution to
@@ -616,13 +620,17 @@ inline void validate_expr(const code_assertt &x)
616620
inline const code_assertt &to_code_assert(const codet &code)
617621
{
618622
PRECONDITION(code.get_statement() == ID_assert);
619-
return static_cast<const code_assertt &>(code);
623+
const code_assertt &ret = static_cast<const code_assertt &>(code);
624+
validate_expr(ret);
625+
return ret;
620626
}
621627

622628
inline code_assertt &to_code_assert(codet &code)
623629
{
624630
PRECONDITION(code.get_statement() == ID_assert);
625-
return static_cast<code_assertt &>(code);
631+
code_assertt &ret = static_cast<code_assertt &>(code);
632+
validate_expr(ret);
633+
return ret;
626634
}
627635

628636
/// Create a fatal assertion, which checks a condition and then halts if it does
@@ -722,17 +730,17 @@ inline void validate_expr(const code_ifthenelset &x)
722730
inline const code_ifthenelset &to_code_ifthenelse(const codet &code)
723731
{
724732
PRECONDITION(code.get_statement() == ID_ifthenelse);
725-
DATA_INVARIANT(
726-
code.operands().size() == 3, "if-then-else must have three operands");
727-
return static_cast<const code_ifthenelset &>(code);
733+
const code_ifthenelset &ret = static_cast<const code_ifthenelset &>(code);
734+
validate_expr(code);
735+
return ret;
728736
}
729737

730738
inline code_ifthenelset &to_code_ifthenelse(codet &code)
731739
{
732740
PRECONDITION(code.get_statement() == ID_ifthenelse);
733-
DATA_INVARIANT(
734-
code.operands().size() == 3, "if-then-else must have three operands");
735-
return static_cast<code_ifthenelset &>(code);
741+
code_ifthenelset &ret = static_cast<code_ifthenelset &>(code);
742+
validate_expr(code);
743+
return ret;
736744
}
737745

738746
/// \ref codet representing a `switch` statement.
@@ -790,15 +798,17 @@ inline void validate_expr(const code_switcht &x)
790798
inline const code_switcht &to_code_switch(const codet &code)
791799
{
792800
PRECONDITION(code.get_statement() == ID_switch);
793-
DATA_INVARIANT(code.operands().size() == 2, "switch must have two operands");
794-
return static_cast<const code_switcht &>(code);
801+
const code_switcht &ret = static_cast<const code_switcht &>(code);
802+
validate_expr(code);
803+
return ret;
795804
}
796805

797806
inline code_switcht &to_code_switch(codet &code)
798807
{
799808
PRECONDITION(code.get_statement() == ID_switch);
800-
DATA_INVARIANT(code.operands().size() == 2, "switch must have two operands");
801-
return static_cast<code_switcht &>(code);
809+
code_switcht &ret = static_cast<code_switcht &>(code);
810+
validate_expr(code);
811+
return ret;
802812
}
803813

804814
/// \ref codet representing a `while` statement.
@@ -856,15 +866,17 @@ inline void validate_expr(const code_whilet &x)
856866
inline const code_whilet &to_code_while(const codet &code)
857867
{
858868
PRECONDITION(code.get_statement() == ID_while);
859-
DATA_INVARIANT(code.operands().size() == 2, "while must have two operands");
860-
return static_cast<const code_whilet &>(code);
869+
const code_whilet &ret = static_cast<const code_whilet &>(code);
870+
validate_expr(code);
871+
return ret;
861872
}
862873

863874
inline code_whilet &to_code_while(codet &code)
864875
{
865876
PRECONDITION(code.get_statement() == ID_while);
866-
DATA_INVARIANT(code.operands().size() == 2, "while must have two operands");
867-
return static_cast<code_whilet &>(code);
877+
code_whilet &ret = static_cast<code_whilet &>(code);
878+
validate_expr(code);
879+
return ret;
868880
}
869881

870882
/// \ref codet representation of a `do while` statement.
@@ -922,17 +934,17 @@ inline void validate_expr(const code_dowhilet &x)
922934
inline const code_dowhilet &to_code_dowhile(const codet &code)
923935
{
924936
PRECONDITION(code.get_statement() == ID_dowhile);
925-
DATA_INVARIANT(
926-
code.operands().size() == 2, "do-while must have two operands");
927-
return static_cast<const code_dowhilet &>(code);
937+
const code_dowhilet &ret = static_cast<const code_dowhilet &>(code);
938+
validate_expr(code);
939+
return ret;
928940
}
929941

930942
inline code_dowhilet &to_code_dowhile(codet &code)
931943
{
932944
PRECONDITION(code.get_statement() == ID_dowhile);
933-
DATA_INVARIANT(
934-
code.operands().size() == 2, "do-while must have two operands");
935-
return static_cast<code_dowhilet &>(code);
945+
code_dowhilet &ret = static_cast<code_dowhilet &>(code);
946+
validate_expr(code);
947+
return ret;
936948
}
937949

938950
/// \ref codet representation of a `for` statement.
@@ -1017,15 +1029,17 @@ inline void validate_expr(const code_fort &x)
10171029
inline const code_fort &to_code_for(const codet &code)
10181030
{
10191031
PRECONDITION(code.get_statement() == ID_for);
1020-
DATA_INVARIANT(code.operands().size() == 4, "for must have four operands");
1021-
return static_cast<const code_fort &>(code);
1032+
const code_fort &ret = static_cast<const code_fort &>(code);
1033+
validate_expr(code);
1034+
return ret;
10221035
}
10231036

10241037
inline code_fort &to_code_for(codet &code)
10251038
{
10261039
PRECONDITION(code.get_statement() == ID_for);
1027-
DATA_INVARIANT(code.operands().size() == 4, "for must have four operands");
1028-
return static_cast<code_fort &>(code);
1040+
code_fort &ret = static_cast<code_fort &>(code);
1041+
validate_expr(code);
1042+
return ret;
10291043
}
10301044

10311045
/// \ref codet representation of a `goto` statement.
@@ -1072,15 +1086,17 @@ inline void validate_expr(const code_gotot &x)
10721086
inline const code_gotot &to_code_goto(const codet &code)
10731087
{
10741088
PRECONDITION(code.get_statement() == ID_goto);
1075-
DATA_INVARIANT(code.operands().empty(), "goto must not have operands");
1076-
return static_cast<const code_gotot &>(code);
1089+
const code_gotot &ret = static_cast<const code_gotot &>(code);
1090+
validate_expr(code);
1091+
return ret;
10771092
}
10781093

10791094
inline code_gotot &to_code_goto(codet &code)
10801095
{
10811096
PRECONDITION(code.get_statement() == ID_goto);
1082-
DATA_INVARIANT(code.operands().empty(), "goto must not have operands");
1083-
return static_cast<code_gotot &>(code);
1097+
code_gotot &ret = static_cast<code_gotot &>(code);
1098+
validate_expr(code);
1099+
return ret;
10841100
}
10851101

10861102
/// \ref codet representation of a function call statement.
@@ -1370,15 +1386,17 @@ inline void validate_expr(const code_labelt &x)
13701386
inline const code_labelt &to_code_label(const codet &code)
13711387
{
13721388
PRECONDITION(code.get_statement() == ID_label);
1373-
DATA_INVARIANT(code.operands().size() == 1, "label must have one operand");
1374-
return static_cast<const code_labelt &>(code);
1389+
const code_labelt &ret = static_cast<const code_labelt &>(code);
1390+
validate_expr(code);
1391+
return ret;
13751392
}
13761393

13771394
inline code_labelt &to_code_label(codet &code)
13781395
{
13791396
PRECONDITION(code.get_statement() == ID_label);
1380-
DATA_INVARIANT(code.operands().size() == 1, "label must have one operand");
1381-
return static_cast<code_labelt &>(code);
1397+
code_labelt &ret = static_cast<code_labelt &>(code);
1398+
validate_expr(code);
1399+
return ret;
13821400
}
13831401

13841402
/// \ref codet representation of a switch-case, i.e.\ a `case` statement within
@@ -1447,17 +1465,17 @@ inline void validate_expr(const code_switch_caset &x)
14471465
inline const code_switch_caset &to_code_switch_case(const codet &code)
14481466
{
14491467
PRECONDITION(code.get_statement() == ID_switch_case);
1450-
DATA_INVARIANT(
1451-
code.operands().size() == 2, "switch-case must have two operands");
1452-
return static_cast<const code_switch_caset &>(code);
1468+
const code_switch_caset &ret = static_cast<const code_switch_caset &>(code);
1469+
validate_expr(code);
1470+
return ret;
14531471
}
14541472

14551473
inline code_switch_caset &to_code_switch_case(codet &code)
14561474
{
14571475
PRECONDITION(code.get_statement() == ID_switch_case);
1458-
DATA_INVARIANT(
1459-
code.operands().size() == 2, "switch-case must have two operands");
1460-
return static_cast<code_switch_caset &>(code);
1476+
code_switch_caset &ret = static_cast<code_switch_caset &>(code);
1477+
validate_expr(code);
1478+
return ret;
14611479
}
14621480

14631481
/// \ref codet representation of a switch-case, i.e.\ a `case` statement
@@ -1531,19 +1549,19 @@ inline const code_gcc_switch_case_ranget &
15311549
to_code_gcc_switch_case_range(const codet &code)
15321550
{
15331551
PRECONDITION(code.get_statement() == ID_gcc_switch_case_range);
1534-
DATA_INVARIANT(
1535-
code.operands().size() == 3,
1536-
"gcc-switch-case-range must have three operands");
1537-
return static_cast<const code_gcc_switch_case_ranget &>(code);
1552+
const code_gcc_switch_case_ranget &ret =
1553+
static_cast<const code_gcc_switch_case_ranget &>(code);
1554+
validate_expr(code);
1555+
return ret;
15381556
}
15391557

15401558
inline code_gcc_switch_case_ranget &to_code_gcc_switch_case_range(codet &code)
15411559
{
15421560
PRECONDITION(code.get_statement() == ID_gcc_switch_case_range);
1543-
DATA_INVARIANT(
1544-
code.operands().size() == 3,
1545-
"gcc-switch-case-range must have three operands");
1546-
return static_cast<code_gcc_switch_case_ranget &>(code);
1561+
code_gcc_switch_case_ranget &ret =
1562+
static_cast<code_gcc_switch_case_ranget &>(code);
1563+
validate_expr(code);
1564+
return ret;
15471565
}
15481566

15491567
/// \ref codet representation of a `break` statement (within a `for` or `while`
@@ -1744,18 +1762,18 @@ inline code_asm_gcct &to_code_asm_gcc(codet &code)
17441762
{
17451763
PRECONDITION(code.get_statement() == ID_asm);
17461764
PRECONDITION(to_code_asm(code).get_flavor() == ID_gcc);
1747-
DATA_INVARIANT(
1748-
code.operands().size() == 5, "code_asm_gcc must have give operands");
1749-
return static_cast<code_asm_gcct &>(code);
1765+
code_asm_gcct &ret = static_cast<code_asm_gcct &>(code);
1766+
validate_expr(code);
1767+
return ret;
17501768
}
17511769

17521770
inline const code_asm_gcct &to_code_asm_gcc(const codet &code)
17531771
{
17541772
PRECONDITION(code.get_statement() == ID_asm);
17551773
PRECONDITION(to_code_asm(code).get_flavor() == ID_gcc);
1756-
DATA_INVARIANT(
1757-
code.operands().size() == 5, "code_asm_gcc must have give operands");
1758-
return static_cast<const code_asm_gcct &>(code);
1774+
const code_asm_gcct &ret = static_cast<const code_asm_gcct &>(code);
1775+
validate_expr(code);
1776+
return ret;
17591777
}
17601778

17611779
/// \ref codet representation of an expression statement.
@@ -1803,17 +1821,17 @@ inline void validate_expr(const code_expressiont &x)
18031821
inline code_expressiont &to_code_expression(codet &code)
18041822
{
18051823
PRECONDITION(code.get_statement() == ID_expression);
1806-
DATA_INVARIANT(
1807-
code.operands().size() == 1, "expression statement must have one operand");
1808-
return static_cast<code_expressiont &>(code);
1824+
code_expressiont &ret = static_cast<code_expressiont &>(code);
1825+
validate_expr(code);
1826+
return ret;
18091827
}
18101828

18111829
inline const code_expressiont &to_code_expression(const codet &code)
18121830
{
18131831
PRECONDITION(code.get_statement() == ID_expression);
1814-
DATA_INVARIANT(
1815-
code.operands().size() == 1, "expression statement must have one operand");
1816-
return static_cast<const code_expressiont &>(code);
1832+
const code_expressiont &ret = static_cast<const code_expressiont &>(code);
1833+
validate_expr(code);
1834+
return ret;
18171835
}
18181836

18191837
/// An expression containing a side effect.
@@ -2456,17 +2474,17 @@ inline void validate_expr(const code_try_catcht &x)
24562474
inline const code_try_catcht &to_code_try_catch(const codet &code)
24572475
{
24582476
PRECONDITION(code.get_statement() == ID_try_catch);
2459-
DATA_INVARIANT(
2460-
code.operands().size() >= 3, "try-catch must have three or more operands");
2461-
return static_cast<const code_try_catcht &>(code);
2477+
const code_try_catcht &ret = static_cast<const code_try_catcht &>(code);
2478+
validate_expr(code);
2479+
return ret;
24622480
}
24632481

24642482
inline code_try_catcht &to_code_try_catch(codet &code)
24652483
{
24662484
PRECONDITION(code.get_statement() == ID_try_catch);
2467-
DATA_INVARIANT(
2468-
code.operands().size() >= 3, "try-catch must have three or more operands");
2469-
return static_cast<code_try_catcht &>(code);
2485+
code_try_catcht &ret = static_cast<code_try_catcht &>(code);
2486+
validate_expr(code);
2487+
return ret;
24702488
}
24712489

24722490
/// This class is used to interface between a language frontend

0 commit comments

Comments
 (0)