Skip to content

Commit 0da7b9c

Browse files
author
Daniel Kroening
authored
Merge pull request #107 from tautschnig/ansi-c-errors
Output source location with all type checking errors
2 parents 95f4a47 + e3b892e commit 0da7b9c

File tree

6 files changed

+230
-109
lines changed

6 files changed

+230
-109
lines changed

src/ansi-c/c_typecheck_argc_argv.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ void c_typecheck_baset::add_argc_argv(const symbolt &main_symbol)
3535
parameters.size()!=3)
3636
{
3737
err_location(main_symbol.location);
38-
throw "main expected to have no or two or three parameters";
38+
str << "main expected to have no or two or three parameters";
39+
throw 0;
3940
}
4041

4142
symbolt *argc_new_symbol;

src/ansi-c/c_typecheck_base.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ void c_typecheck_baset::move_symbol(symbolt &symbol, symbolt *&new_symbol)
7171
if(symbol_table.move(symbol, new_symbol))
7272
{
7373
err_location(symbol.location);
74-
throw "failed to move symbol `"+id2string(symbol.name)+
75-
"' into symbol table";
74+
str << "failed to move symbol `" << symbol.name
75+
<< "' into symbol table";
76+
throw 0;
7677
}
7778
}
7879

@@ -117,7 +118,8 @@ void c_typecheck_baset::typecheck_symbol(symbolt &symbol)
117118
else if(!is_function && symbol.value.id()==ID_code)
118119
{
119120
err_location(symbol.value);
120-
throw "only functions can have a function body";
121+
str << "only functions can have a function body";
122+
throw 0;
121123
}
122124

123125
// set the pretty name
@@ -371,7 +373,8 @@ void c_typecheck_baset::typecheck_redefinition_non_type(
371373
if(final_new.id()==ID_code)
372374
{
373375
err_location(new_symbol.location);
374-
throw "function type not allowed for K&R function parameter";
376+
str << "function type not allowed for K&R function parameter";
377+
throw 0;
375378
}
376379

377380
// fix up old symbol -- we now got the type
@@ -446,7 +449,6 @@ void c_typecheck_baset::typecheck_redefinition_non_type(
446449
err_location(new_symbol.location);
447450
str << "function body `" << new_symbol.display_name()
448451
<< "' defined twice";
449-
error_msg();
450452
throw 0;
451453
}
452454
}

src/ansi-c/c_typecheck_code.cpp

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ Function: c_typecheck_baset::typecheck_code
4444
void c_typecheck_baset::typecheck_code(codet &code)
4545
{
4646
if(code.id()!=ID_code)
47-
throw "expected code, got "+code.pretty();
47+
{
48+
err_location(code);
49+
str << "expected code, got " << code.pretty();
50+
throw 0;
51+
}
4852

4953
code.type()=code_typet();
5054

@@ -205,7 +209,11 @@ Function: c_typecheck_baset::typecheck_assign
205209
void c_typecheck_baset::typecheck_assign(codet &code)
206210
{
207211
if(code.operands().size()!=2)
208-
throw "assignment statement expected to have two operands";
212+
{
213+
err_location(code);
214+
str << "assignment statement expected to have two operands";
215+
throw 0;
216+
}
209217

210218
typecheck_expr(code.op0());
211219
typecheck_expr(code.op1());
@@ -280,7 +288,8 @@ void c_typecheck_baset::typecheck_break(codet &code)
280288
if(!break_is_allowed)
281289
{
282290
err_location(code);
283-
throw "break not allowed here";
291+
str << "break not allowed here";
292+
throw 0;
284293
}
285294
}
286295

@@ -301,7 +310,8 @@ void c_typecheck_baset::typecheck_continue(codet &code)
301310
if(!continue_is_allowed)
302311
{
303312
err_location(code);
304-
throw "continue not allowed here";
313+
str << "continue not allowed here";
314+
throw 0;
305315
}
306316
}
307317

@@ -323,14 +333,16 @@ void c_typecheck_baset::typecheck_decl(codet &code)
323333
if(code.operands().size()!=1)
324334
{
325335
err_location(code);
326-
throw "decl expected to have 1 operand";
336+
str << "decl expected to have 1 operand";
337+
throw 0;
327338
}
328339

329340
// op0 must be declaration
330341
if(code.op0().id()!=ID_declaration)
331342
{
332343
err_location(code);
333-
throw "decl statement expected to have declaration as operand";
344+
str << "decl statement expected to have declaration as operand";
345+
throw 0;
334346
}
335347

336348
ansi_c_declarationt declaration;
@@ -381,7 +393,8 @@ void c_typecheck_baset::typecheck_decl(codet &code)
381393
!is_complete_type(symbol.type))
382394
{
383395
err_location(symbol.location);
384-
throw "incomplete type not permitted here";
396+
str << "incomplete type not permitted here";
397+
throw 0;
385398
}
386399

387400
// see if it's a typedef
@@ -489,7 +502,11 @@ Function: c_typecheck_baset::typecheck_expression
489502
void c_typecheck_baset::typecheck_expression(codet &code)
490503
{
491504
if(code.operands().size()!=1)
492-
throw "expression statement expected to have one operand";
505+
{
506+
err_location(code);
507+
str << "expression statement expected to have one operand";
508+
throw 0;
509+
}
493510

494511
exprt &op=code.op0();
495512
typecheck_expr(op);
@@ -510,7 +527,11 @@ Function: c_typecheck_baset::typecheck_for
510527
void c_typecheck_baset::typecheck_for(codet &code)
511528
{
512529
if(code.operands().size()!=4)
513-
throw "for expected to have four operands";
530+
{
531+
err_location(code);
532+
str << "for expected to have four operands";
533+
throw 0;
534+
}
514535

515536
// the "for" statement has an implicit block around it,
516537
// since code.op0() may contain declarations
@@ -627,7 +648,8 @@ void c_typecheck_baset::typecheck_switch_case(code_switch_caset &code)
627648
if(code.operands().size()!=2)
628649
{
629650
err_location(code);
630-
throw "switch_case expected to have two operands";
651+
str << "switch_case expected to have two operands";
652+
throw 0;
631653
}
632654

633655
typecheck_code(code.code());
@@ -637,15 +659,17 @@ void c_typecheck_baset::typecheck_switch_case(code_switch_caset &code)
637659
if(!case_is_allowed)
638660
{
639661
err_location(code);
640-
throw "did not expect default label here";
662+
str << "did not expect default label here";
663+
throw 0;
641664
}
642665
}
643666
else
644667
{
645668
if(!case_is_allowed)
646669
{
647670
err_location(code);
648-
throw "did not expect `case' here";
671+
str << "did not expect `case' here";
672+
throw 0;
649673
}
650674

651675
exprt &case_expr=code.case_op();
@@ -671,15 +695,17 @@ void c_typecheck_baset::typecheck_gcc_switch_case_range(codet &code)
671695
if(code.operands().size()!=3)
672696
{
673697
err_location(code);
674-
throw "gcc_switch_case_range expected to have three operands";
698+
str << "gcc_switch_case_range expected to have three operands";
699+
throw 0;
675700
}
676701

677702
typecheck_code(to_code(code.op2()));
678703

679704
if(!case_is_allowed)
680705
{
681706
err_location(code);
682-
throw "did not expect `case' here";
707+
str << "did not expect `case' here";
708+
throw 0;
683709
}
684710

685711
typecheck_expr(code.op0());
@@ -741,15 +767,17 @@ void c_typecheck_baset::typecheck_gcc_computed_goto(codet &code)
741767
if(code.operands().size()!=1)
742768
{
743769
err_location(code);
744-
throw "computed-goto expected to have one operand";
770+
str << "computed-goto expected to have one operand";
771+
throw 0;
745772
}
746773

747774
exprt &dest=code.op0();
748775

749776
if(dest.id()!=ID_dereference)
750777
{
751778
err_location(dest);
752-
throw "computed-goto expected to have dereferencing operand";
779+
str << "computed-goto expected to have dereferencing operand";
780+
throw 0;
753781
}
754782

755783
assert(dest.operands().size()==1);
@@ -773,7 +801,11 @@ Function: c_typecheck_baset::typecheck_ifthenelse
773801
void c_typecheck_baset::typecheck_ifthenelse(code_ifthenelset &code)
774802
{
775803
if(code.operands().size()!=3)
776-
throw "ifthenelse expected to have three operands";
804+
{
805+
err_location(code);
806+
str << "ifthenelse expected to have three operands";
807+
throw 0;
808+
}
777809

778810
exprt &cond=code.cond();
779811

@@ -831,7 +863,11 @@ Function: c_typecheck_baset::typecheck_start_thread
831863
void c_typecheck_baset::typecheck_start_thread(codet &code)
832864
{
833865
if(code.operands().size()!=1)
834-
throw "start_thread expected to have one operand";
866+
{
867+
err_location(code);
868+
str << "start_thread expected to have one operand";
869+
throw 0;
870+
}
835871

836872
typecheck_code(to_code(code.op0()));
837873
}
@@ -876,7 +912,8 @@ void c_typecheck_baset::typecheck_return(codet &code)
876912
else
877913
{
878914
err_location(code);
879-
throw "return expected to have 0 or 1 operands";
915+
str << "return expected to have 0 or 1 operands";
916+
throw 0;
880917
}
881918
}
882919

@@ -895,7 +932,11 @@ Function: c_typecheck_baset::typecheck_switch
895932
void c_typecheck_baset::typecheck_switch(code_switcht &code)
896933
{
897934
if(code.operands().size()!=2)
898-
throw "switch expects two operands";
935+
{
936+
err_location(code);
937+
str << "switch expects two operands";
938+
throw 0;
939+
}
899940

900941
typecheck_expr(code.value());
901942

@@ -934,7 +975,11 @@ Function: c_typecheck_baset::typecheck_while
934975
void c_typecheck_baset::typecheck_while(code_whilet &code)
935976
{
936977
if(code.operands().size()!=2)
937-
throw "while expected to have two operands";
978+
{
979+
err_location(code);
980+
str << "while expected to have two operands";
981+
throw 0;
982+
}
938983

939984
typecheck_expr(code.cond());
940985
implicit_typecast_bool(code.cond());
@@ -977,7 +1022,11 @@ Function: c_typecheck_baset::typecheck_dowhile
9771022
void c_typecheck_baset::typecheck_dowhile(code_dowhilet &code)
9781023
{
9791024
if(code.operands().size()!=2)
980-
throw "do while expected to have two operands";
1025+
{
1026+
err_location(code);
1027+
str << "do while expected to have two operands";
1028+
throw 0;
1029+
}
9811030

9821031
typecheck_expr(code.cond());
9831032
implicit_typecast_bool(code.cond());

0 commit comments

Comments
 (0)