cprover
cpp_language.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: C++ Language Module
4 
5 Author: Daniel Kroening, kroening@cs.cmu.edu
6 
7 \*******************************************************************/
8 
11 
12 #include "cpp_language.h"
13 
14 #include <cstring>
15 #include <sstream>
16 #include <fstream>
17 
18 #include <util/config.h>
19 #include <util/replace_symbol.h>
20 #include <util/get_base_name.h>
21 
22 #include <linking/linking.h>
23 
25 #include <ansi-c/c_preprocess.h>
26 
27 #include "cpp_internal_additions.h"
28 #include "expr2cpp.h"
29 #include "cpp_parser.h"
30 #include "cpp_typecheck.h"
31 #include "cpp_type2name.h"
32 
33 std::set<std::string> cpp_languaget::extensions() const
34 {
35  std::set<std::string> s;
36 
37  s.insert("cpp");
38  s.insert("CPP");
39  s.insert("cc");
40  s.insert("c++");
41  s.insert("ii");
42  s.insert("cxx");
43 
44  #ifndef _WIN32
45  s.insert("C");
46  #endif
47 
48  return s;
49 }
50 
51 void cpp_languaget::modules_provided(std::set<std::string> &modules)
52 {
53  modules.insert(get_base_name(parse_path, true));
54 }
55 
58  std::istream &instream,
59  const std::string &path,
60  std::ostream &outstream)
61 {
62  if(path=="")
63  return c_preprocess(instream, outstream, get_message_handler());
64 
65  // check extension
66 
67  const char *ext=strrchr(path.c_str(), '.');
68  if(ext!=nullptr && std::string(ext)==".ipp")
69  {
70  std::ifstream infile(path);
71 
72  char ch;
73 
74  while(infile.read(&ch, 1))
75  outstream << ch;
76 
77  return false;
78  }
79 
80  return c_preprocess(path, outstream, get_message_handler());
81 }
82 
84  std::istream &instream,
85  const std::string &path)
86 {
87  // store the path
88 
89  parse_path=path;
90 
91  // preprocessing
92 
93  std::ostringstream o_preprocessed;
94 
95  cpp_internal_additions(o_preprocessed);
96 
97  if(preprocess(instream, path, o_preprocessed))
98  return true;
99 
100  std::istringstream i_preprocessed(o_preprocessed.str());
101 
102  // parsing
103 
104  cpp_parser.clear();
105  cpp_parser.set_file(path);
106  cpp_parser.in=&i_preprocessed;
109 
110  bool result=cpp_parser.parse();
111 
112  // save result
114 
115  // save some memory
116  cpp_parser.clear();
117 
118  return result;
119 }
120 
122  symbol_tablet &symbol_table,
123  const std::string &module)
124 {
125  if(module=="")
126  return false;
127 
128  symbol_tablet new_symbol_table;
129 
130  if(cpp_typecheck(
131  cpp_parse_tree, new_symbol_table, module, get_message_handler()))
132  return true;
133 
134  return linking(symbol_table, new_symbol_table, get_message_handler());
135 }
136 
138  symbol_tablet &symbol_table)
139 {
140  return ansi_c_entry_point(symbol_table, get_message_handler());
141 }
142 
143 void cpp_languaget::show_parse(std::ostream &out)
144 {
145  for(cpp_parse_treet::itemst::const_iterator it=
146  cpp_parse_tree.items.begin();
147  it!=cpp_parse_tree.items.end();
148  it++)
149  show_parse(out, *it);
150 }
151 
153  std::ostream &out,
154  const cpp_itemt &item)
155 {
156  if(item.is_linkage_spec())
157  {
158  const cpp_linkage_spect &linkage_spec=
159  item.get_linkage_spec();
160 
161  out << "LINKAGE " << linkage_spec.linkage().get("value")
162  << ":\n";
163 
164  for(cpp_linkage_spect::itemst::const_iterator
165  it=linkage_spec.items().begin();
166  it!=linkage_spec.items().end();
167  it++)
168  show_parse(out, *it);
169 
170  out << '\n';
171  }
172  else if(item.is_namespace_spec())
173  {
174  const cpp_namespace_spect &namespace_spec=
175  item.get_namespace_spec();
176 
177  out << "NAMESPACE " << namespace_spec.get_namespace()
178  << ":\n";
179 
180  for(cpp_namespace_spect::itemst::const_iterator
181  it=namespace_spec.items().begin();
182  it!=namespace_spec.items().end();
183  it++)
184  show_parse(out, *it);
185 
186  out << '\n';
187  }
188  else if(item.is_using())
189  {
190  const cpp_usingt &cpp_using=item.get_using();
191 
192  out << "USING ";
193  if(cpp_using.get_namespace())
194  out << "NAMESPACE ";
195  out << cpp_using.name().pretty() << '\n';
196  out << '\n';
197  }
198  else if(item.is_declaration())
199  {
200  item.get_declaration().output(out);
201  }
202  else
203  out << "UNKNOWN: " << item.pretty() << '\n';
204 }
205 
206 std::unique_ptr<languaget> new_cpp_language()
207 {
208  return util_make_unique<cpp_languaget>();
209 }
210 
212  const exprt &expr,
213  std::string &code,
214  const namespacet &ns)
215 {
216  code=expr2cpp(expr, ns);
217  return false;
218 }
219 
221  const typet &type,
222  std::string &code,
223  const namespacet &ns)
224 {
225  code=type2cpp(type, ns);
226  return false;
227 }
228 
230  const typet &type,
231  std::string &name,
232  const namespacet &)
233 {
234  name=cpp_type2name(type);
235  return false;
236 }
237 
239  const std::string &code,
240  const std::string &,
241  exprt &expr,
242  const namespacet &ns)
243 {
244  expr.make_nil();
245 
246  // no preprocessing yet...
247 
248  std::istringstream i_preprocessed(code);
249 
250  // parsing
251 
252  cpp_parser.clear();
254  cpp_parser.in=&i_preprocessed;
256 
257  bool result=cpp_parser.parse();
258 
259  if(cpp_parser.parse_tree.items.empty())
260  result=true;
261  else
262  {
263  // TODO
264  // expr.swap(cpp_parser.parse_tree.declarations.front());
265 
266  // typecheck it
268  }
269 
270  // save some memory
271  cpp_parser.clear();
272 
273  return result;
274 }
275 
277 {
278 }
The type of an expression.
Definition: type.h:22
bool is_using() const
Definition: cpp_item.h:121
struct configt::ansi_ct ansi_c
std::unique_ptr< languaget > new_cpp_language()
virtual void clear() override
Definition: cpp_parser.h:33
cpp_usingt & get_using()
Definition: cpp_item.h:109
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:641
ANSI-C Linking.
std::istream * in
Definition: parser.h:26
void swap(cpp_parse_treet &cpp_parse_tree)
~cpp_languaget() override
bool linking(symbol_tablet &dest_symbol_table, symbol_tablet &new_symbol_table, message_handlert &message_handler)
Definition: linking.cpp:1381
bool cpp_typecheck(cpp_parse_treet &cpp_parse_tree, symbol_tablet &symbol_table, const std::string &module, message_handlert &message_handler)
bool c_preprocess(std::istream &instream, std::ostream &outstream, message_handlert &message_handler)
ANSI-C preprocessing.
configt config
Definition: config.cpp:23
bool from_type(const typet &type, std::string &code, const namespacet &ns) override
Formats the given type in a language-specific way.
std::string get_base_name(const std::string &in, bool strip_suffix)
cleans a filename from path and extension
void modules_provided(std::set< std::string > &modules) override
bool is_declaration() const
Definition: cpp_item.h:46
bool is_namespace_spec() const
Definition: cpp_item.h:96
cpp_namespace_spect & get_namespace_spec()
Definition: cpp_item.h:84
cpp_parse_treet cpp_parse_tree
Definition: cpp_language.h:88
void cpp_internal_additions(std::ostream &out)
void show_parse(std::ostream &out) override
flavourt mode
Definition: config.h:114
virtual bool parse() override
Definition: cpp_parser.cpp:20
void set_file(const irep_idt &file)
Definition: parser.h:85
std::string expr2cpp(const exprt &expr, const namespacet &ns)
Definition: expr2cpp.cpp:504
C++ Language Module.
bool preprocess(std::istream &instream, const std::string &path, std::ostream &outstream) override
ANSI-C preprocessing.
The symbol table.
Definition: symbol_table.h:19
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:213
C++ Language Module.
TO_BE_DOCUMENTED.
Definition: namespace.h:74
cpp_parse_treet parse_tree
Definition: cpp_parser.h:29
std::string cpp_type2name(const typet &type)
virtual void set_message_handler(message_handlert &_message_handler)
Definition: message.h:148
ansi_c_parsert::modet mode
Definition: cpp_parser.h:50
bool ansi_c_entry_point(symbol_tablet &symbol_table, message_handlert &message_handler)
C++ Language Type Checking.
bool get_namespace() const
Definition: cpp_using.h:34
bool from_expr(const exprt &expr, std::string &code, const namespacet &ns) override
Formats the given expression in a language-specific way.
bool to_expr(const std::string &code, const std::string &module, exprt &expr, const namespacet &ns) override
Parses the given string into an expression.
const itemst & items() const
bool parse(std::istream &instream, const std::string &path) override
message_handlert & get_message_handler()
Definition: message.h:153
std::set< std::string > extensions() const override
mstreamt & result() const
Definition: message.h:312
const irep_idt & get_namespace() const
std::string type2cpp(const typet &type, const namespacet &ns)
Definition: expr2cpp.cpp:511
Base class for all expressions.
Definition: expr.h:42
bool type_to_name(const typet &type, std::string &name, const namespacet &ns) override
Encodes the given type in a language-specific way.
cpp_declarationt & get_declaration()
Definition: cpp_item.h:34
cpp_namet & name()
Definition: cpp_using.h:24
cpp_linkage_spect & get_linkage_spec()
Definition: cpp_item.h:59
void make_nil()
Definition: irep.h:315
dstringt irep_idt
Definition: irep.h:32
C++ Parser.
const itemst & items() const
bool typecheck(symbol_tablet &symbol_table, const std::string &module) override
std::string parse_path
Definition: cpp_language.h:89
void output(std::ostream &out) const
bool generate_support_functions(symbol_tablet &symbol_table) override
Create language-specific support functions, such as __CPROVER_start, __CPROVER_initialize and languag...
cpp_parsert cpp_parser
Definition: cpp_parser.cpp:16
bool is_linkage_spec() const
Definition: cpp_item.h:71