CodeSonar C++ API
[For improved navigation, enable JavaScript.]
cs_compunit_decl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023-2025, an unpublished work by CodeSecure, Inc.
3  * ALL RIGHTS RESERVED
4  *
5  * Copyright (c) 2013-2023, an unpublished work by GrammaTech, Inc.
6  * ALL RIGHTS RESERVED
7  *
8  * This software is furnished under a license and may be used and
9  * copied only in accordance with the terms of such license and the
10  * inclusion of the above copyright notice. This software or any
11  * other copies thereof may not be provided or otherwise made
12  * available to any other person. Title to and ownership of the
13  * software is retained by CodeSecure, Inc.
14  */
15 
16 #ifndef CS_COMPUNIT_DECL_HPP
17 #define CS_COMPUNIT_DECL_HPP
18 
19 #include "cs_types.hpp"
20 #include "cs_source_files.h"
21 #include "cs_ast_decl.hpp"
22 #include "cs_language.hpp"
23 #include "cs_binaries.h"
24 
27 namespace cs{
28 
29  CS_IR_BOILERPLATE_FORWARD(
30  compunit,
31  cs_uid,
32  friend class point;
33  friend class procedure;
34  friend class symbol;
35  friend class sfileinst;
36  friend class amc_analysis;
37  friend class cglue<compunit_points>;
38  friend class cglue<compunit_points_pair>;
39  friend class iterator_adapter<project_compunits_iterator_policy>;
40  friend class ir_factory;
41  friend class metric_granularity_policy<compunit>;
42  friend class warning_retraction_info;
43  friend class visitor<compunit>;
44  friend class metricclass_granularity_policy<compunit>;
46  friend struct FatTypeTraits<compunit>;
48  friend class project_compunits_iterator_policy;
49  friend class project;
50  );
51 
52 
70  class compunit{
71 
78  CS_IR_BOILERPLATE(compunit);
79 
80 
90  std::string name() const
91  {
92  string_scratchpad::
93  to_string_functor1_limit<
94  compunit, cs_file_get_name>
95  functor(inner, -1);
96  return string_scratchpad::to_string(functor);
97  }
98 
99 
113  std::string normalized_name() const
114  {
115  string_scratchpad::
116  to_string_functor1_limit<
117  compunit, cs_file_get_normalized_name>
118  functor(inner, -1);
119  return string_scratchpad::to_string(functor);
120  }
121 
122 
136  symbol lookup_symbol( const std::string &name ) const;
137 
138 
156  {
157  cs_ast rv;
158  check(cs_file_ast(inner, cglue<ast_family>::unwrap(family), &rv));
159  return ast::;
160  }
161 
175  void set_ast(ast_family family, ast val)
176  {
177  check(cs_file_set_ast(inner, cglue<ast_family>::unwrap(family),
178  cglue<ast>::unwrap(val)));
179  }
190  {
191  cs_language rv;
192  check(cs_file_language(inner, &rv));
193  return language::;
194  }
195 
196 
275  csuint32 get_group_uid() const
276  {
277  csuint32 rv;
278  check(cs_file_group_uid(inner, &rv));
279  return rv;
280  }
281 
303  std::string csonar_front_end_run_id() const
304  {
305  string_scratchpad::
306  to_string_functor1_limit<compunit, cs_file_webid>
307  functor(inner, -1);
308  return string_scratchpad::to_string(functor);
309  }
327  std::string compiler_model() const
328  {
329  string_scratchpad::
330  to_string_functor1_limit<compunit, cs_file_compiler_model>
331  functor(inner, -1);
332  return string_scratchpad::to_string(functor);
333  }
334 
335 
353  std::vector<std::string> compiler_command_line() const
354  {
355  /* We could do all this without the copying if c++ had
356  * template typedefs, or even "friend" declarations
357  * against typedefs (as opposed to classes).
358  *
359  * For now, we take a runtime hit. It's possible it would
360  * also be safe to do something type-unsafe since
361  * std_string_wrapper just has one member....
362  */
363  typedef scratchpad<std_string_wrapper> sp;
364  sp::to_vector_functor1
365  <compunit, cs_file_native_command_line>
366  functor(inner);
367  std::vector<std_string_wrapper> v(sp::to_vector(functor));
368  std::vector<std::string> rv;
369  rv.reserve(v.size());
370  for( size_t i = 0; i < v.size(); i++ )
371  rv.push_back(v[i].value);
372  return rv;
373  }
374 
375 
395  std::vector<std::string> effective_compiler_flags() const
396  {
397  /* We could do all this without the copying if c++ had
398  * template typedefs, or even "friend" declarations
399  * against typedefs (as opposed to classes).
400  *
401  * For now, we take a runtime hit. It's possible it would
402  * also be safe to do something type-unsafe since
403  * std_string_wrapper just has one member....
404  */
405  typedef scratchpad<std_string_wrapper> sp;
406  sp::to_vector_functor1
407  <compunit, cs_file_effective_native_flags>
408  functor(inner);
409  std::vector<std_string_wrapper> v(sp::to_vector(functor));
410  std::vector<std::string> rv;
411  rv.reserve(v.size());
412  for( size_t i = 0; i < v.size(); i++ )
413  rv.push_back(v[i].value);
414  return rv;
415  }
416 
417 
439  std::vector<std::string> cs_frontend_command_line() const
440  {
441  /* We could do all this without the copying if c++ had
442  * template typedefs, or even "friend" declarations
443  * against typedefs (as opposed to classes).
444  *
445  * For now, we take a runtime hit. It's possible it would
446  * also be safe to do something type-unsafe since
447  * std_string_wrapper just has one member....
448  */
449  typedef scratchpad<std_string_wrapper> sp;
450  sp::to_vector_functor1
451  <compunit, cs_file_frontend_command_line>
452  functor(inner);
453  std::vector<std_string_wrapper> v(sp::to_vector(functor));
454  std::vector<std::string> rv;
455  rv.reserve(v.size());
456  for( size_t i = 0; i < v.size(); i++ )
457  rv.push_back(v[i].value);
458  return rv;
459  }
460 
461 
471  size_t error_count() const
472  {
473  size_t rv;
474  check(cs_file_error_count(inner, &rv));
475  return rv;
476  }
477 
478 
488  size_t warning_count() const
489  {
490  size_t rv;
491  check(cs_file_warning_count(inner, &rv));
492  return rv;
493  }
494 
495 
508  sfileinst get_sfileinst() const;
509 
510 
517 
518 
525 
532  /* In a type-safe C++ setting, it would be hard to get into a
533  * state where this doesn't return \c true.
534  */
535  bool is_valid() const
536  { return !!cs_uid_is_valid(inner); }
554  bool is_user() const
555  { return !!cs_uid_is_user_unit(inner); }
556 
557 
565  bool is_library_model() const
566  { return !!cs_uid_is_library_model_unit(inner); }
567 
568 
576  bool is_rewriting() const
577  { return !!cs_uid_is_rewriting_unit(inner); }
578 
579 
587  bool is_backend() const
588  { return !!cs_uid_is_backend_unit(inner); }
589 
590 
607  bool is_shared() const
608  {
609  cs_boolean rv;
610  check(cs_uid_is_shared_unit(inner, &rv));
611  return !!rv;
612  }
613 
642  const std::string & sec_name,
643  csuint32 sec_offset)
644  {
645  effective_address addr;
646  check(cs_file_section_offset_ea(inner,
647  sec_name.c_str(),
648  sec_offset,
649  &addr));
650  return addr;
651  }
652 
653 
667  bool is_hidden_binary() const
668  {
669  cs_boolean rv;
670  check(cs_uid_is_hidden_binary(inner, &rv));
671  return !!rv;
672  }
695  bool limit_reached() const
696  {
697  cs_boolean rv;
698  check(cs_uid_limit_reached(inner, &rv));
699  return !!rv;
700  }
711  std::string as_string() const
712  { return name(); }
713 
715  std::string as_repr() const
716  { return CS_AS_REPR_FROM_STRING(compunit); }
717 
734  csuint32 id() const
735  { return inner; }
736 
754  std::string handle() const
755  {
756  string_scratchpad::
757  to_string_functor1_limit<
758  compunit, cs_uid_get_handle>
759  functor(inner, -1);
760  return string_scratchpad::to_string(functor);
761  }
762 
763  };
764  CS_IR_BOILERPLATE_FRIENDS_PLUS_OSTREAM(compunit, cs)
765 
766  inline compunit cglue<compunit>::wrap(const cs_uid &c)
767  { return compunit(c); }
768 
769  inline cs_uid cglue<compunit>::unwrap(const compunit &c)
770  { return c.unwrap(); }
771 }
772 
773 #endif /* CS_COMPUNIT_DECL_HPP */
sfileinst get_sfileinst() const
Get the root source file instance of a compilation unit.
Definition: cs_compunit.hpp:72
Namespace for CodeSonar/CodeSurfer API.
Definition: cs_ast.hpp:33
std::string csonar_front_end_run_id() const
[CodeSonar only] Get the value used to identify a compilation unit to the hub: this will allow you to...
Definition: cs_compunit_decl.hpp:303
std::string name() const
Get the unnormalized file path corresponding to a compilation unit.
Definition: cs_compunit_decl.hpp:90
bool is_backend() const
Check: does a compunit object correspond to a compilation unit generated by the back end to hold #Sys...
Definition: cs_compunit_decl.hpp:587
std::string handle() const
Get a handle for this compilation unit.
Definition: cs_compunit_decl.hpp:757
std::string as_repr() const
Get a representation of a compunit object that includes information useful for debugging.
Definition: cs_compunit_decl.hpp:718
effective_address get_address_for_section_offset(const std::string &sec_name, csuint32 sec_offset)
[Binary analyses only] Returns the address that corresponds to the given (section, offset) location in this compilation unit.
Definition: cs_compunit_decl.hpp:641
ast get_ast(ast_family family=ast_family::DEFAULT) const
Get the AST (of the specified ast_family) for a compilation unit.
Definition: cs_compunit_decl.hpp:155
A function or variable.
Definition: cs_symbol_decl.hpp:243
std::vector< std::string > compiler_command_line() const
Get the native command line used to build a compilation unit.
Definition: cs_compunit_decl.hpp:353
Iterator over the procedures (procedure) in a compilation unit (compunit).
Definition: cs_tplt_instantiations.hpp:653
bool is_hidden_binary() const
[CodeSonar for Binaries only] Check: is a compilation unit an &#39;offstage binary&#39; (that is...
Definition: cs_compunit_decl.hpp:667
An Abstract Syntax Tree (AST).
Definition: cs_ast_decl.hpp:448
std::vector< std::string > cs_frontend_command_line() const
Get the front end command line used to build a compilation unit.
Definition: cs_compunit_decl.hpp:439
std::string normalized_name() const
Get the normalized file path corresponding to a compilation unit.
Definition: cs_compunit_decl.hpp:113
std::string as_string() const
Get the unnormalized file path corresponding to a compilation unit.
Definition: cs_compunit_decl.hpp:711
std::pair< cs::compunit, cs::point_set > compunit_points_pair
A std::pair (c, ps) where:
Definition: cs_tplt_instantiations.hpp:342
static const ast_family DEFAULT
Represents the first AST family, whatever it might be.
Definition: cs_ast_decl.hpp:25
bool is_user() const
Check: does a compunit object correspond to a user-generated compilation unit?
Definition: cs_compunit_decl.hpp:554
compunit_procedure_iterator procedures() const
Get an iterator over the procedures (procedure) in a compilation unit.
Definition: cs_compunit.hpp:79
csuint32 get_group_uid() const
Get the group to which a compilation unit belongs.
Definition: cs_compunit_decl.hpp:275
bool is_shared() const
Check: can a compilation unit be shared by multiple projects and analyses?
Definition: cs_compunit_decl.hpp:607
bool is_library_model() const
Check: does a compunit object correspond to a compilation unit for a library model shipped with CodeS...
Definition: cs_compunit_decl.hpp:565
compunit_global_iterator global_symbols() const
Get an iterator over the global symbols (symbol) in a compilation unit.
Definition: cs_compunit.hpp:82
std::vector< std::string > effective_compiler_flags() const
Get the effective flags for the native build of a compilation unit.
Definition: cs_compunit_decl.hpp:395
symbol lookup_symbol(const std::string &name) const
Get a symbol from the compilation unit by name.
Definition: cs_compunit.hpp:85
Enumeration class for AST family.
Definition: cs_ast_decl.hpp:50
Enumeration class: identifies the source language the compilation unit is in.
Definition: cs_language.hpp:15
std::pair< cs::compunit, cs::procedure_and_points_vector > compunit_points
A std::pair (c, ppv) where:
Definition: cs_tplt_instantiations.hpp:332
A source file instance.
Definition: cs_sfileinst_decl.hpp:302
csuint32 id() const
Get the unique identifier of a compilation unit.
Definition: cs_compunit_decl.hpp:737
std::string compiler_model() const
Get the name of the compiler model used to build a compilation unit.
Definition: cs_compunit_decl.hpp:327
Iterator over the global symbols (symbol) in a compilation unit (compunit).
Definition: cs_tplt_instantiations.hpp:619
size_t error_count() const
Get the number of errors encountered in compiling a compilation unit.
Definition: cs_compunit_decl.hpp:471
A compilation unit.
Definition: cs_compunit_decl.hpp:70
bool limit_reached() const
Check: did the front end reach any limits while parsing the specified compilation unit (which may hav...
Definition: cs_compunit_decl.hpp:695
cs_ea effective_address
An effective address: used for binary analyses to hold the address of an entity such as a point or sy...
Definition: cs_types.hpp:392
size_t warning_count() const
Get the number of warnings encountered in compiling a compilation unit.
Definition: cs_compunit_decl.hpp:488
bool is_rewriting() const
Check: does a compunit object correspond to a rewriting compilation unit ?
Definition: cs_compunit_decl.hpp:576
language get_language() const
Get the source language of a compilation unit.
Definition: cs_compunit_decl.hpp:189