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