1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright (C) 2013-2020 Red Hat, Inc.
5 
6 /// @file
7 
8 #ifndef __ABG_CORPUS_H__
9 #define __ABG_CORPUS_H__
10 
11 #include <abg-ir.h>
12 
13 namespace abigail
14 {
15 
16 namespace ir
17 {
18 
19 /// This is the abstraction of a set of translation units (themselves
20 /// seen as bundles of unitary abi artefacts like types and decls)
21 /// bundled together as a corpus.  A corpus is thus the Application
22 /// binary interface of a program, a library or just a set of modules
23 /// put together.
24 class corpus
25 {
26 public:
27   struct priv;
28   /// Convenience typedef for shared_ptr of corpus::priv
29   typedef shared_ptr<priv> priv_sptr;
30 
31   /// A convenience typedef for std::vector<string>.
32   typedef vector<string> strings_type;
33 
34   /// Convenience typedef for std::vector<abigail::ir::function_decl*>
35   typedef vector<function_decl*> functions;
36 
37   ///Convenience typedef for std::vector<abigail::ir::var_decl*>
38   typedef vector<var_decl*> variables;
39 
40   class exported_decls_builder;
41 
42   /// Convenience typedef for shared_ptr<exported_decls_builder>.
43   typedef shared_ptr<exported_decls_builder> exported_decls_builder_sptr;
44 
45   /// This abstracts where the corpus comes from.  That is, either it
46   /// has been read from the native xml format, from DWARF or built
47   /// artificially using the library's API.
48   enum origin
49   {
50     ARTIFICIAL_ORIGIN = 0,
51     NATIVE_XML_ORIGIN,
52     DWARF_ORIGIN,
53     LINUX_KERNEL_BINARY_ORIGIN
54   };
55 
56 private:
57   corpus();
58 
59   void set_group(corpus_group*);
60   void init_format_version();
61 
62 public:
63   shared_ptr<priv> priv_;
64 
65   corpus(ir::environment*, const string& path= "");
66 
~corpus()67   virtual ~corpus() {}
68 
69   const environment*
70   get_environment() const;
71 
72   environment*
73   get_environment();
74 
75   void
76   set_environment(environment*);
77 
78   void
79   add(const translation_unit_sptr);
80 
81   const translation_units&
82   get_translation_units() const;
83 
84   const translation_unit_sptr
85   find_translation_unit(const string &path) const;
86 
87   void
88   drop_translation_units();
89 
90   type_maps&
91   get_types();
92 
93   const type_maps&
94   get_types() const;
95 
96   type_maps&
97   get_type_per_loc_map();
98 
99   const type_maps&
100   get_type_per_loc_map() const;
101 
102   virtual bool
103   recording_types_reachable_from_public_interface_supported();
104 
105   void
106   record_type_as_reachable_from_public_interfaces(const type_base&);
107 
108   bool
109   type_is_reachable_from_public_interfaces(const type_base&) const;
110 
111   const vector<type_base_wptr>&
112   get_types_not_reachable_from_public_interfaces() const;
113 
114   const corpus_group*
115   get_group() const;
116 
117   corpus_group*
118   get_group();
119 
120   origin
121   get_origin() const;
122 
123   void
124   set_origin(origin);
125 
126   string&
127   get_format_major_version_number() const;
128 
129   void
130   set_format_major_version_number(const string&);
131 
132   string&
133   get_format_minor_version_number() const;
134 
135   void
136   set_format_minor_version_number(const string&);
137 
138   string&
139   get_path() const;
140 
141   void
142   set_path(const string&);
143 
144   const vector<string>&
145   get_needed() const;
146 
147   void
148   set_needed(const vector<string>&);
149 
150   const string&
151   get_soname();
152 
153   void
154   set_soname(const string&);
155 
156   const string&
157   get_architecture_name() const;
158 
159   void
160   set_architecture_name(const string&);
161 
162   virtual bool
163   is_empty() const;
164 
165   bool
166   operator==(const corpus&) const;
167 
168   void
169   set_symtab(symtab_reader::symtab_sptr);
170 
171   const symtab_reader::symtab_sptr&
172   get_symtab() const;
173 
174   virtual const string_elf_symbols_map_type&
175   get_fun_symbol_map() const;
176 
177   const string_elf_symbols_map_type&
178   get_undefined_fun_symbol_map() const;
179 
180   virtual const elf_symbols&
181   get_sorted_fun_symbols() const;
182 
183   const elf_symbols&
184   get_sorted_undefined_fun_symbols() const;
185 
186   virtual const string_elf_symbols_map_type&
187   get_var_symbol_map() const;
188 
189   const string_elf_symbols_map_type&
190   get_undefined_var_symbol_map() const;
191 
192   virtual const elf_symbols&
193   get_sorted_var_symbols() const;
194 
195   const elf_symbols&
196   get_sorted_undefined_var_symbols() const;
197 
198   const elf_symbol_sptr
199   lookup_function_symbol(const string& n) const;
200 
201   const elf_symbol_sptr
202   lookup_function_symbol(const string& symbol_name,
203 			 const elf_symbol::version& version) const;
204 
205   const elf_symbol_sptr
206   lookup_function_symbol(const elf_symbol& symbol) const;
207 
208   const elf_symbol_sptr
209   lookup_variable_symbol(const string& n) const;
210 
211   const elf_symbol_sptr
212   lookup_variable_symbol(const string& symbol_name,
213 			 const elf_symbol::version& version) const;
214 
215   const elf_symbol_sptr
216   lookup_variable_symbol(const elf_symbol& symbol) const;
217 
218   virtual const functions&
219   get_functions() const;
220 
221   const vector<function_decl*>*
222   lookup_functions(const string& id) const;
223 
224   void
225   sort_functions();
226 
227   virtual const variables&
228   get_variables() const;
229 
230   void
231   sort_variables();
232 
233   virtual const elf_symbols&
234   get_unreferenced_function_symbols() const;
235 
236   virtual const elf_symbols&
237   get_unreferenced_variable_symbols() const;
238 
239   vector<string>&
240   get_regex_patterns_of_fns_to_suppress();
241 
242   const vector<string>&
243   get_regex_patterns_of_fns_to_suppress() const;
244 
245   vector<string>&
246   get_regex_patterns_of_vars_to_suppress();
247 
248   const vector<string>&
249   get_regex_patterns_of_vars_to_suppress() const;
250 
251   vector<string>&
252   get_regex_patterns_of_fns_to_keep();
253 
254   const vector<string>&
255   get_regex_patterns_of_fns_to_keep() const;
256 
257   vector<string>&
258   get_sym_ids_of_fns_to_keep();
259 
260   const vector<string>&
261   get_sym_ids_of_fns_to_keep() const;
262 
263   vector<string>&
264   get_regex_patterns_of_vars_to_keep();
265 
266   const vector<string>&
267   get_regex_patterns_of_vars_to_keep() const;
268 
269   vector<string>&
270   get_sym_ids_of_vars_to_keep();
271 
272   const vector<string>&
273   get_sym_ids_of_vars_to_keep() const;
274 
275   void
276   maybe_drop_some_exported_decls();
277 
278   exported_decls_builder_sptr
279   get_exported_decls_builder() const;
280 
281   friend class type_base;
282   friend class corpus_group;
283 };// end class corpus.
284 
285 /// Abstracts the building of the set of exported variables and
286 /// functions.
287 ///
288 /// Given a function or variable, this type can decide if it belongs
289 /// to the list of exported functions and variables based on all the
290 /// parameters needed.
291 class corpus::exported_decls_builder
292 {
293 public:
294   class priv;
295 
296   /// Convenience typedef for shared_ptr<priv>
297   typedef shared_ptr<priv> priv_sptr;
298 
299   friend class corpus;
300 
301 private:
302   priv_sptr priv_;
303 
304   // Forbid default construction.
305   exported_decls_builder();
306 
307 public:
308 
309   exported_decls_builder(functions& fns,
310 			 variables& vars,
311 			 strings_type& fns_suppress_regexps,
312 			 strings_type& vars_suppress_regexps,
313 			 strings_type& fns_keep_regexps,
314 			 strings_type& vars_keep_regexps,
315 			 strings_type& sym_id_of_fns_to_keep,
316 			 strings_type& sym_id_of_vars_to_keep);
317 
318 
319   const functions&
320   exported_functions() const;
321 
322   functions&
323   exported_functions();
324 
325   const variables&
326   exported_variables() const;
327 
328   variables&
329   exported_variables();
330 
331   void
332   maybe_add_fn_to_exported_fns(function_decl*);
333 
334   void
335   maybe_add_var_to_exported_vars(var_decl*);
336 }; //corpus::exported_decls_builder
337 
338 /// Abstraction of a group of corpora.
339 ///
340 /// A corpus group is a union of corpora.  It provides a unified view
341 /// of a set of corpora.  It lets you get the set of functions,
342 /// variables and symbols that are defined and exported by a set of
343 /// corpora.
344 class corpus_group : public corpus
345 {
346   struct priv;
347   typedef shared_ptr<priv> priv_sptr;
348 
349   priv_sptr priv_;
350 
351   // Forbid copy
352   corpus_group(const corpus_group&);
353 
354 public:
355   typedef vector<corpus_sptr> corpora_type;
356 
357   corpus_group(ir::environment*, const string&);
358 
359   virtual ~corpus_group();
360 
361   void add_corpus(const corpus_sptr&);
362 
363   const corpora_type&
364   get_corpora() const;
365 
366   const corpus_sptr
367   get_main_corpus() const;
368 
369   corpus_sptr
370   get_main_corpus();
371 
372   virtual bool
373   is_empty() const;
374 
375   virtual const corpus::functions&
376   get_functions() const;
377 
378   virtual const corpus::variables&
379   get_variables() const;
380 
381   virtual const string_elf_symbols_map_type&
382   get_var_symbol_map() const;
383 
384   virtual const string_elf_symbols_map_type&
385   get_fun_symbol_map() const;
386 
387   virtual const elf_symbols&
388   get_sorted_fun_symbols() const;
389 
390   virtual const elf_symbols&
391   get_sorted_var_symbols() const;
392 
393   virtual const elf_symbols&
394   get_unreferenced_function_symbols() const;
395 
396   virtual const elf_symbols&
397   get_unreferenced_variable_symbols() const;
398 
399   unordered_set<interned_string, hash_interned_string>*
400   get_public_types_pretty_representations();
401 
402   virtual bool
403   recording_types_reachable_from_public_interface_supported();
404 
405   bool
406   operator==(const corpus_group&) const;
407 }; // end class corpus_group
408 
409 }// end namespace ir
410 }//end namespace abigail
411 #endif //__ABG_CORPUS_H__
412