CBMC
ansi_c_language.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "ansi_c_language.h"
10 
11 #include <fstream>
12 
13 #include <util/config.h>
14 #include <util/get_base_name.h>
15 
16 #include <linking/linking.h>
18 
19 #include "ansi_c_entry_point.h"
20 #include "ansi_c_typecheck.h"
21 #include "ansi_c_parser.h"
22 #include "expr2c.h"
23 #include "c_preprocess.h"
25 #include "type2name.h"
26 
27 std::set<std::string> ansi_c_languaget::extensions() const
28 {
29  return { "c", "i" };
30 }
31 
32 void ansi_c_languaget::modules_provided(std::set<std::string> &modules)
33 {
34  modules.insert(get_base_name(parse_path, true));
35 }
36 
39  std::istream &instream,
40  const std::string &path,
41  std::ostream &outstream)
42 {
43  // stdin?
44  if(path.empty())
45  return c_preprocess(instream, outstream, get_message_handler());
46 
47  return c_preprocess(path, outstream, get_message_handler());
48 }
49 
51  std::istream &instream,
52  const std::string &path)
53 {
54  // store the path
55  parse_path=path;
56 
57  // preprocessing
58  std::ostringstream o_preprocessed;
59 
60  if(preprocess(instream, path, o_preprocessed))
61  return true;
62 
63  std::istringstream i_preprocessed(o_preprocessed.str());
64 
65  // parsing
66 
67  std::string code;
69  std::istringstream codestr(code);
70 
72  ansi_c_parser.set_file(ID_built_in);
73  ansi_c_parser.in=&codestr;
77  ansi_c_parser.cpp98=false; // it's not C++
78  ansi_c_parser.cpp11=false; // it's not C++
80 
82 
83  bool result=ansi_c_parser.parse();
84 
85  if(!result)
86  {
88  ansi_c_parser.set_file(path);
89  ansi_c_parser.in=&i_preprocessed;
92  }
93 
94  // save result
96 
97  // save some memory
99 
100  return result;
101 }
102 
104  symbol_tablet &symbol_table,
105  const std::string &module,
106  const bool keep_file_local)
107 {
108  return typecheck(symbol_table, module, keep_file_local, {});
109 }
110 
112  symbol_tablet &symbol_table,
113  const std::string &module,
114  const bool keep_file_local,
115  const std::set<irep_idt> &keep)
116 {
117  symbol_tablet new_symbol_table;
118 
119  if(ansi_c_typecheck(
120  parse_tree,
121  new_symbol_table,
122  module,
124  {
125  return true;
126  }
127 
129  new_symbol_table, this->get_message_handler(), keep_file_local, keep);
130 
131  if(linking(symbol_table, new_symbol_table, get_message_handler()))
132  return true;
133 
134  return false;
135 }
136 
138  symbol_tablet &symbol_table)
139 {
140  // This creates __CPROVER_start and __CPROVER_initialize:
141  return ansi_c_entry_point(
142  symbol_table, get_message_handler(), object_factory_params);
143 }
144 
145 void ansi_c_languaget::show_parse(std::ostream &out)
146 {
147  parse_tree.output(out);
148 }
149 
150 std::unique_ptr<languaget> new_ansi_c_language()
151 {
152  return util_make_unique<ansi_c_languaget>();
153 }
154 
156  const exprt &expr,
157  std::string &code,
158  const namespacet &ns)
159 {
160  code=expr2c(expr, ns);
161  return false;
162 }
163 
165  const typet &type,
166  std::string &code,
167  const namespacet &ns)
168 {
169  code=type2c(type, ns);
170  return false;
171 }
172 
174  const typet &type,
175  std::string &name,
176  const namespacet &ns)
177 {
178  name=type2name(type, ns);
179  return false;
180 }
181 
183  const std::string &code,
184  const std::string &,
185  exprt &expr,
186  const namespacet &ns)
187 {
188  expr.make_nil();
189 
190  // no preprocessing yet...
191 
192  std::istringstream i_preprocessed(
193  "void __my_expression = (void) (\n"+code+"\n);");
194 
195  // parsing
196 
199  ansi_c_parser.in=&i_preprocessed;
204 
205  bool result=ansi_c_parser.parse();
206 
207  if(ansi_c_parser.parse_tree.items.empty())
208  result=true;
209  else
210  {
211  expr=ansi_c_parser.parse_tree.items.front().declarator().value();
212 
213  // typecheck it
215  }
216 
217  // save some memory
219 
220  // now remove that (void) cast
221  if(expr.id()==ID_typecast &&
222  expr.type().id()==ID_empty &&
223  expr.operands().size()==1)
224  {
225  expr = to_typecast_expr(expr).op();
226  }
227 
228  return result;
229 }
230 
232 {
233 }
ansi_c_parser
ansi_c_parsert ansi_c_parser
Definition: ansi_c_parser.cpp:13
configt::ansi_ct::ts_18661_3_Floatn_types
bool ts_18661_3_Floatn_types
Definition: config.h:139
get_base_name
std::string get_base_name(const std::string &in, bool strip_suffix)
cleans a filename from path and extension
Definition: get_base_name.cpp:16
symbol_tablet
The symbol table.
Definition: symbol_table.h:13
ansi_c_typecheck.h
ansi_c_languaget::type_to_name
bool type_to_name(const typet &type, std::string &name, const namespacet &ns) override
Encodes the given type in a language-specific way.
Definition: ansi_c_language.cpp:173
linking
bool linking(symbol_tablet &dest_symbol_table, const symbol_tablet &new_symbol_table, message_handlert &message_handler)
Merges the symbol table new_symbol_table into dest_symbol_table, renaming symbols from new_symbol_tab...
Definition: linking.cpp:1475
configt::ansi_ct::for_has_scope
bool for_has_scope
Definition: config.h:138
ansi_c_entry_point.h
irept::make_nil
void make_nil()
Definition: irep.h:454
typet
The type of an expression, extends irept.
Definition: type.h:28
ansi_c_parsert::cpp98
bool cpp98
Definition: ansi_c_parser.h:74
ansi_c_languaget::~ansi_c_languaget
~ansi_c_languaget() override
Definition: ansi_c_language.cpp:231
ansi_c_languaget::to_expr
bool to_expr(const std::string &code, const std::string &module, exprt &expr, const namespacet &ns) override
Parses the given string into an expression.
Definition: ansi_c_language.cpp:182
ansi_c_parse_treet::items
itemst items
Definition: ansi_c_parse_tree.h:22
type2name.h
c_preprocess.h
exprt
Base class for all expressions.
Definition: expr.h:55
irep_idt
dstringt irep_idt
Definition: irep.h:37
configt::ansi_c
struct configt::ansi_ct ansi_c
remove_internal_symbols
void remove_internal_symbols(symbol_tablet &symbol_table, message_handlert &mh, const bool keep_file_local)
Removes internal symbols from a symbol table A symbol is EXPORTED if it is a.
Definition: remove_internal_symbols.cpp:111
ansi_c_languaget::from_type
bool from_type(const typet &type, std::string &code, const namespacet &ns) override
Formats the given type in a language-specific way.
Definition: ansi_c_language.cpp:164
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:84
ansi_c_parsert::ts_18661_3_Floatn_types
bool ts_18661_3_Floatn_types
Definition: ansi_c_parser.h:80
messaget::result
mstreamt & result() const
Definition: message.h:409
parsert::in
std::istream * in
Definition: parser.h:26
type2name
static std::string type2name(const typet &type, const namespacet &ns, symbol_numbert &symbol_number)
Definition: type2name.cpp:81
expr2c.h
expr2c
std::string expr2c(const exprt &expr, const namespacet &ns, const expr2c_configurationt &configuration)
Definition: expr2c.cpp:4046
ansi_c_parsert::clear
virtual void clear() override
Definition: ansi_c_parser.h:45
ansi_c_internal_additions.h
ansi_c_scanner_init
void ansi_c_scanner_init()
messaget::set_message_handler
virtual void set_message_handler(message_handlert &_message_handler)
Definition: message.h:179
ansi_c_internal_additions
void ansi_c_internal_additions(std::string &code)
Definition: ansi_c_internal_additions.cpp:148
linking.h
ansi_c_parse_treet::swap
void swap(ansi_c_parse_treet &other)
Definition: ansi_c_parse_tree.cpp:13
ansi_c_languaget::preprocess
bool preprocess(std::istream &instream, const std::string &path, std::ostream &outstream) override
ANSI-C preprocessing.
Definition: ansi_c_language.cpp:38
irept::id
const irep_idt & id() const
Definition: irep.h:396
get_base_name.h
unary_exprt::op
const exprt & op() const
Definition: std_expr.h:326
ansi_c_entry_point
bool ansi_c_entry_point(symbol_tablet &symbol_table, message_handlert &message_handler, const c_object_factory_parameterst &object_factory_parameters)
Definition: ansi_c_entry_point.cpp:106
config
configt config
Definition: config.cpp:25
ansi_c_parse_treet::output
void output(std::ostream &out) const
Definition: ansi_c_parse_tree.cpp:23
ansi_c_languaget::object_factory_params
c_object_factory_parameterst object_factory_params
Definition: ansi_c_language.h:115
ansi_c_parsert::cpp11
bool cpp11
Definition: ansi_c_parser.h:74
ansi_c_parsert::mode
modet mode
Definition: ansi_c_parser.h:71
configt::ansi_ct::mode
flavourt mode
Definition: config.h:237
messaget::get_message_handler
message_handlert & get_message_handler()
Definition: message.h:184
ansi_c_parsert::parse_tree
ansi_c_parse_treet parse_tree
Definition: ansi_c_parser.h:26
ansi_c_languaget::parse_tree
ansi_c_parse_treet parse_tree
Definition: ansi_c_language.h:112
ansi_c_language.h
ansi_c_languaget::show_parse
void show_parse(std::ostream &out) override
Definition: ansi_c_language.cpp:145
ansi_c_languaget::parse
bool parse(std::istream &instream, const std::string &path) override
Definition: ansi_c_language.cpp:50
to_typecast_expr
const typecast_exprt & to_typecast_expr(const exprt &expr)
Cast an exprt to a typecast_exprt.
Definition: std_expr.h:2051
ansi_c_languaget::extensions
std::set< std::string > extensions() const override
Definition: ansi_c_language.cpp:27
remove_internal_symbols.h
new_ansi_c_language
std::unique_ptr< languaget > new_ansi_c_language()
Definition: ansi_c_language.cpp:150
ansi_c_parsert::parse
virtual bool parse() override
Definition: ansi_c_parser.h:40
ansi_c_languaget::modules_provided
void modules_provided(std::set< std::string > &modules) override
Definition: ansi_c_language.cpp:32
ansi_c_typecheck
bool ansi_c_typecheck(ansi_c_parse_treet &ansi_c_parse_tree, symbol_tablet &symbol_table, const std::string &module, message_handlert &message_handler)
Definition: ansi_c_typecheck.cpp:24
ansi_c_languaget::generate_support_functions
bool generate_support_functions(symbol_tablet &symbol_table) override
Create language-specific support functions, such as __CPROVER_start, __CPROVER_initialize and languag...
Definition: ansi_c_language.cpp:137
ansi_c_languaget::from_expr
bool from_expr(const exprt &expr, std::string &code, const namespacet &ns) override
Formats the given expression in a language-specific way.
Definition: ansi_c_language.cpp:155
config.h
exprt::operands
operandst & operands()
Definition: expr.h:94
type2c
std::string type2c(const typet &type, const namespacet &ns, const expr2c_configurationt &configuration)
Definition: expr2c.cpp:4062
ansi_c_languaget::typecheck
bool typecheck(symbol_tablet &symbol_table, const std::string &module, const bool keep_file_local) override
typecheck without removing specified entries from the symbol table
Definition: ansi_c_language.cpp:103
ansi_c_parser.h
ansi_c_parsert::for_has_scope
bool for_has_scope
Definition: ansi_c_parser.h:77
c_preprocess
bool c_preprocess(std::istream &instream, std::ostream &outstream, message_handlert &message_handler)
ANSI-C preprocessing.
Definition: c_preprocess.cpp:157
ansi_c_languaget::parse_path
std::string parse_path
Definition: ansi_c_language.h:113
parsert::set_file
void set_file(const irep_idt &file)
Definition: parser.h:85
parsert::set_line_no
void set_line_no(unsigned _line_no)
Definition: parser.h:80