1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/*******************************************************************\
Module: Test that compound blocks get a location
Author: Kareem Khazem <karkhaz@karkhaz.com>, 2018
\*******************************************************************/
#include "compound_block_locations.h"
#include <util/cmdline.h>
#include <util/config.h>
#include <util/options.h>
#include <util/std_code.h>
#include <util/tempfile.h>
#include <goto-programs/goto_model.h>
#include <ansi-c/ansi_c_language.h>
#include <cbmc/cbmc_parse_options.h>
#include <goto-instrument/goto_program2code.h>
#include <langapi/mode.h>
#include <testing-utils/use_catch.h>
#include <fstream>
#include <utility>
SCENARIO("Compound blocks should have a location")
{
compound_block_locationst checker;
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ int x; \n"
"/* 4 */ if(x) \n"
"/* 5 */ x = 1; \n"
"/* 6 */ } \n",
{{ID_ifthenelse, 4}});
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ int x; \n"
"/* 4 */ if(x) \n"
"/* 5 */ { \n"
"/* 6 */ x = 1; \n"
"/* 7 */ x = 1; \n"
"/* 8 */ } \n"
"/* 9 */ } \n",
{{ID_ifthenelse, 4}});
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ int x; \n"
"/* 4 */ if(x) \n"
"/* 5 */ x = 1; \n"
"/* 6 */ else \n"
"/* 7 */ x = 1; \n"
"/* 8 */ } \n",
{{ID_ifthenelse, 4}});
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ int x; \n"
"/* 4 */ if(x) \n"
"/* 5 */ x = 1; \n"
"/* 6 */ else if(x == 2) \n"
"/* 7 */ { \n"
"/* 8 */ x = 1; \n"
"/* 9 */ x = 1; \n"
"/* 10 */ } \n"
"/* 11 */ else \n"
"/* 12 */ x = 1; \n"
"/* 13 */ } \n",
{{ID_ifthenelse, 4}, {ID_ifthenelse, 6}});
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ while(1) \n"
"/* 4 */ { \n"
"/* 5 */ int x = 2; \n"
"/* 6 */ } \n"
"/* 7 */ } \n",
{{ID_while, 3}});
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ while(1) \n"
"/* 4 */ { \n"
"/* 5 */ while(1) \n"
"/* 5 */ { \n"
"/* 6 */ int x = 1; \n"
"/* 7 */ } \n"
"/* 8 */ } \n"
"/* 9 */ } \n",
{{ID_while, 3}, {ID_while, 5}});
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ while(1) \n"
"/* 4 */ { \n"
"/* 5 */ int x; \n"
"/* 6 */ if(x) \n"
"/* 7 */ { \n"
"/* 8 */ int x = 1; \n"
"/* 9 */ } \n"
"/* 10 */ } \n"
"/* 11 */ } \n",
{{ID_while, 3}, {ID_ifthenelse, 6}});
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ int x; \n"
"/* 4 */ if(x) \n"
"/* 5 */ { \n"
"/* 6 */ while(1) \n"
"/* 7 */ { \n"
"/* 8 */ int y = 1; \n"
"/* 9 */ } \n"
"/* 10 */ } \n"
"/* 11 */ } \n",
{{ID_ifthenelse, 4}, {ID_while, 6}});
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ int x; \n"
"/* 4 */ if(x) \n"
"/* 5 */ { \n"
"/* 6 */ while(1) \n"
"/* 7 */ { \n"
"/* 8 */ int y = 1; \n"
"/* 9 */ } \n"
"/* 10 */ } \n"
"/* 11 */ else \n"
"/* 12 */ { \n"
"/* 13 */ while(1) \n"
"/* 14 */ { \n"
"/* 15 */ while(1) \n"
"/* 16 */ { \n"
"/* 17 */ int y = 1; \n"
"/* 18 */ } \n"
"/* 19 */ } \n"
"/* 20 */ } \n"
"/* 21 */ } \n",
{{ID_ifthenelse, 4}, {ID_while, 6}, {ID_while, 13}, {ID_while, 15}});
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ for(int i = 0; i < 1; ++i) \n"
"/* 4 */ { \n"
"/* 5 */ int x; \n"
"/* 6 */ } \n"
"/* 7 */ } \n",
{{ID_for, 3}});
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ for(int i = 0; i < 1; ++i) \n"
"/* 4 */ { \n"
"/* 5 */ int x; \n"
"/* 6 */ for(int j = 0; j < 3; ++j) \n"
"/* 7 */ { \n"
"/* 8 */ int y; \n"
"/* 9 */ } \n"
"/* 10 */ } \n"
"/* 11 */ } \n",
{{ID_for, 3}, {ID_for, 6}});
// In the absence of a better place to put it, the source location of a
// do-while block is equal to the source location of its first statement.
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ do \n"
"/* 4 */ { \n"
"/* 5 */ int x; \n"
"/* 6 */ } while(1); \n"
"/* 7 */ } \n",
{{ID_dowhile, 5}});
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ do \n"
"/* 4 */ { \n"
"/* 5 */ int x; \n"
"/* 6 */ int y; \n"
"/* 7 */ int z = x + y; \n"
"/* 8 */ } while(1); \n"
"/* 9 */ } \n",
{{ID_dowhile, 5}});
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ int x; \n"
"/* 4 */ do \n"
"/* 5 */ { \n"
"/* 6 */ int x; \n"
"/* 7 */ int y; \n"
"/* 8 */ int z = x + y; \n"
"/* 9 */ } while(x); \n"
"/* 10 */ } \n",
{{ID_dowhile, 6}});
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ int x; \n"
"/* 4 */ if(x) \n"
"/* 5 */ { \n"
"/* 6 */ do \n"
"/* 7 */ { \n"
"/* 8 */ int y; \n"
"/* 9 */ } while(x); \n"
"/* 10 */ } \n"
"/* 11 */ } \n",
{{ID_ifthenelse, 4}, {ID_dowhile, 8}});
checker.check(
"/* 1 */ int main() \n"
"/* 2 */ { \n"
"/* 3 */ int x; \n"
"/* 4 */ switch(x) \n"
"/* 5 */ { \n"
"/* 6 */ case 1: \n"
"/* 7 */ { \n"
"/* 8 */ int y = 1; \n"
"/* 9 */ break; \n"
"/* 10 */ } \n"
"/* 11 */ case 2: \n"
"/* 12 */ { \n"
"/* 13 */ int y = 2; \n"
"/* 14 */ int z = 2; \n"
"/* 15 */ break; \n"
"/* 16 */ } \n"
"/* 17 */ case 3: \n"
"/* 18 */ { \n"
"/* 19 */ int y = 3; \n"
"/* 20 */ } \n"
"/* 21 */ case 4: \n"
"/* 22 */ { \n"
"/* 23 */ int y = 4; \n"
"/* 24 */ break; \n"
"/* 25 */ } \n"
"/* 26 */ default: \n"
"/* 27 */ { \n"
"/* 28 */ int y = 5; \n"
"/* 29 */ } \n"
"/* 30 */ } \n"
"/* 31 */ } \n",
{{ID_switch, 6}});
}
void compound_block_locationst::check(
const std::string &program,
const std::list<std::pair<const irep_idt, const unsigned>> &expected)
{
INFO("Given the following program:\n" + program);
check_compound_block_locations(program, expected);
}
void compound_block_locationst::check_compound_block_locations(
const std::string &program,
const std::list<std::pair<const irep_idt, const unsigned>> &expected)
{
temporary_filet tmp("compound-block-locations_", ".c");
std::ofstream of(tmp().c_str());
REQUIRE(of.is_open());
of << program << std::endl;
of.close();
register_language(new_ansi_c_language);
cmdlinet cmdline;
cmdline.args.push_back(tmp());
config.main = std::string("main");
config.set(cmdline);
optionst opts;
cbmc_parse_optionst::set_default_options(opts);
ui_message_handlert mh(cmdline, "compound-block-locations");
mh.set_verbosity(0);
messaget log(mh);
goto_modelt gm;
int ret;
ret = cbmc_parse_optionst::get_goto_program(gm, opts, cmdline, mh);
REQUIRE(ret == -1);
const auto found = gm.goto_functions.function_map.find("main");
REQUIRE(found != gm.goto_functions.function_map.end());
code_blockt block;
symbol_tablet st_copy(gm.symbol_table);
std::list<irep_idt> local_static, type_names;
std::unordered_set<irep_idt> typedef_names;
std::set<std::string> system_headers;
goto_program2codet g2c(
"main",
found->second.body,
st_copy,
block,
local_static,
type_names,
typedef_names,
system_headers);
g2c();
std::list<std::pair<const irep_idt, const unsigned>> copy(expected);
recurse_on_block(block, copy);
std::stringstream ss;
ss << "Expected but did not observe the following lines to have blocks with "
<< "locations: ";
for(const auto &pair : copy)
ss << "'" << types.at(pair.first) << "' at line "
<< std::to_string(pair.second) << ", ";
INFO(ss.str());
REQUIRE(copy.empty());
}
void compound_block_locationst::recurse_on_block(
const exprt &expr,
std::list<std::pair<const irep_idt, const unsigned>> &expected)
{
if(expr.id() == ID_code)
{
const codet code = to_code(expr);
for(const auto &pair : types)
{
if(code.get_statement() != pair.first)
continue;
INFO("Found an instruction of type " + pair.second);
const auto &top = expected.front();
REQUIRE(code.get_statement() == top.first);
const source_locationt &loc = code.source_location();
REQUIRE(loc.is_not_nil());
REQUIRE(std::stoul(loc.get_line().c_str()) == top.second);
expected.pop_front();
break;
}
}
for(const auto &child : expr.operands())
recurse_on_block(child, expected);
}
|