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(
141  symbol_table, get_message_handler(), object_factory_params);
142 }
143 
144 void cpp_languaget::show_parse(std::ostream &out)
145 {
146  for(const auto &i : cpp_parse_tree.items)
147  show_parse(out, i);
148 }
149 
151  std::ostream &out,
152  const cpp_itemt &item)
153 {
154  if(item.is_linkage_spec())
155  {
156  const cpp_linkage_spect &linkage_spec=
157  item.get_linkage_spec();
158 
159  out << "LINKAGE " << linkage_spec.linkage().get(ID_value) << ":\n";
160 
161  for(const auto &i : linkage_spec.items())
162  show_parse(out, i);
163 
164  out << '\n';
165  }
166  else if(item.is_namespace_spec())
167  {
168  const cpp_namespace_spect &namespace_spec=
169  item.get_namespace_spec();
170 
171  out << "NAMESPACE " << namespace_spec.get_namespace()
172  << ":\n";
173 
174  for(const auto &i : namespace_spec.items())
175  show_parse(out, i);
176 
177  out << '\n';
178  }
179  else if(item.is_using())
180  {
181  const cpp_usingt &cpp_using=item.get_using();
182 
183  out << "USING ";
184  if(cpp_using.get_namespace())
185  out << "NAMESPACE ";
186  out << cpp_using.name().pretty() << '\n';
187  out << '\n';
188  }
189  else if(item.is_declaration())
190  {
191  item.get_declaration().output(out);
192  }
193  else
194  out << "UNKNOWN: " << item.pretty() << '\n';
195 }
196 
197 std::unique_ptr<languaget> new_cpp_language()
198 {
199  return util_make_unique<cpp_languaget>();
200 }
201 
203  const exprt &expr,
204  std::string &code,
205  const namespacet &ns)
206 {
207  code=expr2cpp(expr, ns);
208  return false;
209 }
210 
212  const typet &type,
213  std::string &code,
214  const namespacet &ns)
215 {
216  code=type2cpp(type, ns);
217  return false;
218 }
219 
221  const typet &type,
222  std::string &name,
223  const namespacet &)
224 {
225  name=cpp_type2name(type);
226  return false;
227 }
228 
230  const std::string &code,
231  const std::string &,
232  exprt &expr,
233  const namespacet &ns)
234 {
235  expr.make_nil();
236 
237  // no preprocessing yet...
238 
239  std::istringstream i_preprocessed(code);
240 
241  // parsing
242 
243  cpp_parser.clear();
245  cpp_parser.in=&i_preprocessed;
247 
248  bool result=cpp_parser.parse();
249 
250  if(cpp_parser.parse_tree.items.empty())
251  result=true;
252  else
253  {
254  // TODO
255  // expr.swap(cpp_parser.parse_tree.declarations.front());
256 
257  // typecheck it
259  }
260 
261  // save some memory
262  cpp_parser.clear();
263 
264  return result;
265 }
266 
268 {
269 }
c_object_factory_parameterst object_factory_params
Definition: cpp_language.h:98
The type of an expression, extends irept.
Definition: type.h:27
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:640
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:1391
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:24
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:95
void cpp_internal_additions(std::ostream &out)
void show_parse(std::ostream &out) override
flavourt mode
Definition: config.h:115
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:475
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:212
C++ Language Module.
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:93
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:169
ansi_c_parsert::modet mode
Definition: cpp_parser.h:50
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:174
std::set< std::string > extensions() const override
mstreamt & result() const
Definition: message.h:396
const irep_idt & get_namespace() const
std::string type2cpp(const typet &type, const namespacet &ns)
Definition: expr2cpp.cpp:482
bool ansi_c_entry_point(symbol_tablet &symbol_table, message_handlert &message_handler, const c_object_factory_parameterst &object_factory_parameters)
Base class for all expressions.
Definition: expr.h:54
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:96
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