Skip to content

Commit cc01a04

Browse files
ensured minimal requirements for a goal entry
1 parent c172622 commit cc01a04

File tree

2 files changed

+66
-26
lines changed

2 files changed

+66
-26
lines changed

src/goto-instrument/cover.cpp

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ class basic_blockst
108108
bool coverage_goalst::get_coverage_goals(
109109
const std::string &coverage_file,
110110
message_handlert &message_handler,
111-
coverage_goalst &goals)
111+
coverage_goalst &goals,
112+
const irep_idt &mode)
112113
{
113114
jsont json;
114115
source_locationt source_location;
@@ -141,22 +142,53 @@ bool coverage_goalst::get_coverage_goals(
141142
// store the source location for each existing goal
142143
for(const auto &each_goal : goals_in_json["goals"].array)
143144
{
144-
// get and set the bytecode_index
145-
irep_idt bytecodeIndex=
146-
each_goal["sourceLocation"]["bytecode_index"].value;
147-
source_location.set_java_bytecode_index(bytecodeIndex);
145+
// ensure minimal requirements for a goal entry
146+
PRECONDITION(
147+
(!each_goal["goal"].is_null()) ||
148+
(!each_goal["sourceLocation"]["bytecode_index"].is_null()) ||
149+
(!each_goal["sourceLocation"]["file"].is_null() &&
150+
!each_goal["sourceLocation"]["function"].is_null() &&
151+
!each_goal["sourceLocation"]["line"].is_null()));
152+
153+
// check whether bytecode_index is provided for Java programs
154+
if(mode==ID_java &&
155+
each_goal["sourceLocation"]["bytecode_index"].is_null())
156+
{
157+
messaget message(message_handler);
158+
message.error() << coverage_file
159+
<< " file does not contain bytecode_index"
160+
<< messaget::eom;
161+
return true;
162+
}
148163

149-
// get and set the file
150-
irep_idt file=each_goal["sourceLocation"]["file"].value;
151-
source_location.set_file(file);
164+
if(!each_goal["sourceLocation"]["bytecode_index"].is_null())
165+
{
166+
// get and set the bytecode_index
167+
irep_idt bytecode_index=
168+
each_goal["sourceLocation"]["bytecode_index"].value;
169+
source_location.set_java_bytecode_index(bytecode_index);
170+
}
152171

153-
// get and set the function
154-
irep_idt function=each_goal["sourceLocation"]["function"].value;
155-
source_location.set_function(function);
172+
if(!each_goal["sourceLocation"]["file"].is_null())
173+
{
174+
// get and set the file
175+
irep_idt file=each_goal["sourceLocation"]["file"].value;
176+
source_location.set_file(file);
177+
}
156178

157-
// get and set the line
158-
irep_idt line=each_goal["sourceLocation"]["line"].value;
159-
source_location.set_line(line);
179+
if(!each_goal["sourceLocation"]["function"].is_null())
180+
{
181+
// get and set the function
182+
irep_idt function=each_goal["sourceLocation"]["function"].value;
183+
source_location.set_function(function);
184+
}
185+
186+
if(!each_goal["sourceLocation"]["line"].is_null())
187+
{
188+
// get and set the line
189+
irep_idt line=each_goal["sourceLocation"]["line"].value;
190+
source_location.set_line(line);
191+
}
160192

161193
// store the existing goal
162194
goals.add_goal(source_location);
@@ -175,17 +207,18 @@ void coverage_goalst::add_goal(source_locationt goal)
175207

176208
/// check whether we have an existing goal that is uncovered
177209
/// \param msg: message to be printed about the uncovered goal
178-
void coverage_goalst::check_uncovered_goals(messaget msg)
210+
void coverage_goalst::check_uncovered_goals(messaget &msg)
179211
{
180-
for(auto const &existing_loc : existing_goals)
212+
for(const auto &existing_loc : existing_goals)
181213
{
182214
if(!existing_loc.second)
183215
{
184-
msg.warning() << "Warning: existing goal in file "
185-
<< existing_loc.first.get_file()
186-
<< " line " << existing_loc.first.get_line()
187-
<< " function " << existing_loc.first.get_function()
188-
<< " is uncovered" << messaget::eom;
216+
msg.warning()
217+
<< "Warning: existing goal in file "
218+
<< existing_loc.first.get_file()
219+
<< " line " << existing_loc.first.get_line()
220+
<< " function " << existing_loc.first.get_function()
221+
<< " is uncovered" << messaget::eom;
189222
}
190223
}
191224
}
@@ -195,7 +228,7 @@ void coverage_goalst::check_uncovered_goals(messaget msg)
195228
/// \return true : if the current goal exists false : otherwise
196229
bool coverage_goalst::is_existing_goal(source_locationt source_loc)
197230
{
198-
for(auto const &existing_loc : existing_goals)
231+
for(const auto &existing_loc : existing_goals)
199232
{
200233
if((source_loc.get_file()==existing_loc.first.get_file()) &&
201234
(source_loc.get_function()==existing_loc.first.get_function()) &&
@@ -1472,14 +1505,20 @@ bool instrument_cover_goals(
14721505
}
14731506

14741507
// check existing test goals
1475-
static coverage_goalst existing_goals;
1508+
coverage_goalst existing_goals;
14761509
if(cmdline.isset("existing-coverage"))
14771510
{
1511+
// get the mode to ensure invariants
1512+
// (e.g., bytecode_index for Java programs)
1513+
namespacet ns(symbol_table);
1514+
const irep_idt &mode=ns.lookup(goto_functions.entry_point()).mode;
1515+
14781516
msg.status() << "Add existing coverage goals" << messaget::eom;
14791517
// get file with covered test goals
14801518
const std::string coverage=cmdline.get_value("existing-coverage");
14811519
// get a coverage_goalst object
1482-
if(coverage_goalst::get_coverage_goals(coverage, msgh, existing_goals))
1520+
if(coverage_goalst::get_coverage_goals(
1521+
coverage, msgh, existing_goals, mode))
14831522
{
14841523
msg.error() << "get_coverage_goals failed" << messaget::eom;
14851524
return true;

src/goto-instrument/cover.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ class coverage_goalst
2323
static bool get_coverage_goals(
2424
const std::string &coverage,
2525
message_handlert &message_handler,
26-
coverage_goalst &goals);
26+
coverage_goalst &goals,
27+
const irep_idt &mode);
2728
void add_goal(source_locationt goal);
2829
bool is_existing_goal(source_locationt source_loc);
29-
void check_uncovered_goals(messaget msg);
30+
void check_uncovered_goals(messaget &msg);
3031

3132
private:
3233
std::map<source_locationt, bool> existing_goals;

0 commit comments

Comments
 (0)