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