56 std::string _loop_type,
57 std::string _identifier,
62 clause(std::move(_clause)),
72 assertiont(std::string _identifier, std::string _content)
84 std::optional<std::string>
stub;
88 using functionst = std::list<std::pair<std::regex, functiont>>;
96 using objectst = std::list<std::pair<std::regex, objectt>>;
110 auto sources =
config[
"sources"];
112 if(!sources.is_null())
114 if(!sources.is_array())
119 if(!source.is_string())
135 if(!include.is_string())
138 this->
includes.push_back(include.value);
151 if(!define.is_string())
154 this->
defines.push_back(define.value);
171 if(!
function.is_object())
176 const auto function_name = function_entry.first;
177 const auto &items = function_entry.second;
179 if(!items.is_array())
189 if(!function_item.is_string())
192 auto item_string = function_item.value;
198 split[0] ==
"ensures" || split[0] ==
"requires" ||
199 split[0] ==
"assigns")
201 std::ostringstream rest;
202 join_strings(rest, split.begin() + 1, split.end(),
' ');
206 else if(split[0] ==
"assert" && split.size() >= 3)
208 std::ostringstream rest;
209 join_strings(rest, split.begin() + 2, split.end(),
' ');
211 function_config.
assertions.emplace_back(split[1], rest.str());
214 (split[0] ==
"for" || split[0] ==
"while" || split[0] ==
"loop") &&
216 (split[2] ==
"invariant" || split[2] ==
"assigns" ||
217 split[2] ==
"decreases"))
219 std::ostringstream rest;
220 join_strings(rest, split.begin() + 3, split.end(),
' ');
223 split[0], split[1], split[2], rest.str());
225 else if(split[0] ==
"stub")
227 std::ostringstream rest;
228 join_strings(rest, split.begin() + 1, split.end(),
' ');
230 function_config.
stub = rest.str();
232 else if(split[0] ==
"remove")
234 if(split.size() == 1)
237 if(split[1] ==
"static")
241 "unexpected remove entry " + split[1]);
245 "unexpected function entry " + split[0]);
263 if(!
object.is_object())
268 const auto &object_name = object_entry.first;
269 const auto &items = object_entry.second;
271 if(!items.is_array())
280 if(!object_item.is_string())
283 auto item_string = object_item.value;
288 if(split[0] ==
"remove")
290 if(split.size() == 1)
293 if(split[1] ==
"static")
297 "unexpected remove entry " + split[1]);
301 "unexpected object entry " + split[0]);
323 std::vector<std::string> argv = {
"cc",
"-E", source_file};
325 for(
const auto &include :
c_wrangler.includes)
327 argv.push_back(
"-I");
328 argv.push_back(include);
332 argv.push_back(std::string(
"-D") + define);
334 std::ostringstream result;
336 auto run_result =
run(
"cc", argv,
"", result,
"");
346 std::vector<std::string> argv = {
"cc",
"-E",
"-dM", source_file};
348 for(
const auto &include :
config.includes)
350 argv.push_back(
"-I");
351 argv.push_back(include);
354 std::ostringstream result;
356 auto run_result =
run(
"cc", argv,
"", result,
"");
361 defines.
parse(result.str());
371 if(function_config.
stub.has_value() && declaration.
has_body())
374 out << function_config.
stub.value();
382 if(t.text ==
"static")
385 out << std::string(6,
' ');
411 << defines(entry.content) <<
')';
413 std::map<std::string, std::string> loop_invariants;
414 std::map<std::string, std::string> loop_assigns;
415 std::map<std::string, std::string> loop_decreases;
419 if(entry.clause ==
"invariant")
420 loop_invariants[entry.loop_type + entry.identifier] = entry.content;
421 if(entry.clause ==
"assigns")
422 loop_assigns[entry.loop_type + entry.identifier] = entry.content;
423 if(entry.clause ==
"decreases")
424 loop_decreases[entry.loop_type + entry.identifier] = entry.content;
428 loop_invariants.empty() && loop_assigns.empty() && loop_decreases.empty())
435 std::size_t for_count = 0, while_count = 0;
440 const auto &token = *(t++);
442 std::string invariant;
444 std::string decreases;
466 else if(token ==
"for")
469 invariant = loop_invariants.count(
"for" +
std::to_string(for_count))
478 decreases = loop_decreases.count(
"for" +
std::to_string(for_count))
485 for(; t != t_end; t++)
490 out <<
' ' <<
CPROVER_PREFIX <<
"assigns(" << defines(assigns) <<
')';
493 if(!invariant.empty())
496 << defines(invariant) <<
')';
499 if(!decreases.empty())
501 out <<
' ' <<
CPROVER_PREFIX <<
"decreases(" << defines(decreases)
519 if(t.text ==
"static")
522 out << std::string(6,
' ');
549 if(declaration.
is_function() && name_opt.has_value())
551 for(
const auto &entry :
config.functions)
553 if(std::regex_match(name_opt->text, entry.first))
562 else if(!declaration.
is_function() && name_opt.has_value())
564 for(
const auto &entry :
config.objects)
566 if(std::regex_match(name_opt->text, entry.first))
581 const std::string &in,
585 std::ostringstream out;
586 std::istringstream in_str(in);
590 for(
const auto &declaration : parsed)
605 for(
auto &source_file :
c_wrangler.source_files)
619 std::cout << mangled;
static void mangle_function(const c_declarationt &declaration, const c_definest &defines, const c_wranglert::functiont &function_config, std::ostream &out)
static c_definest get_defines(const std::string &source_file, const c_wranglert &config)
static void mangle_object(const c_declarationt &declaration, const c_definest &defines, const c_wranglert::objectt &object_config, std::ostream &out)
void c_wrangler(const jsont &config)
static std::string preprocess(const std::string &source_file, const c_wranglert &c_wrangler)
static void mangle(const c_declarationt &declaration, const c_definest &defines, const c_wranglert &config, std::ostream &out)
This class maintains a representation of one assignment to the preprocessor macros in a C program.
void parse(const std::string &)
Thrown when failing to deserialize a value from some low level format, like JSON or raw bytes.
Thrown when some external system fails unexpectedly.
ctokenitt match_bracket(ctokenitt t, char open, char close)
json_arrayt & to_json_array(jsont &json)
json_objectt & to_json_object(jsont &json)
c_translation_unitt parse_c(std::istream &in)
int run(const std::string &what, const std::vector< std::string > &argv)
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
void split_string(const std::string &s, char delim, std::vector< std::string > &result, bool strip, bool remove_empty)
Stream & join_strings(Stream &&os, const It b, const It e, const Delimiter &delimiter, TransformFunc &&transform_func)
Prints items to an stream, separated by a constant delimiter.
std::optional< ctokent > declared_identifier() const
assertiont(std::string _identifier, std::string _content)
function_contract_clauset(std::string _clause, std::string _content)
std::vector< loop_contract_clauset > loop_contract
std::vector< function_contract_clauset > function_contract
std::vector< assertiont > assertions
std::optional< std::string > stub
loop_contract_clauset(std::string _loop_type, std::string _identifier, std::string _clause, std::string _content)
void configure_output(const jsont &)
void configure_objects(const jsont &)
std::vector< std::string > source_files
std::list< std::pair< std::regex, functiont > > functionst
std::list< std::pair< std::regex, objectt > > objectst
void configure_sources(const jsont &)
std::vector< std::string > includes
std::vector< std::string > defines
void configure_functions(const jsont &)