CodeSonar C++ API
[For improved navigation, enable JavaScript.]
cs_symbol_decl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023, 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_SYMBOL_DECL_HPP
17 #define CS_SYMBOL_DECL_HPP
18 
19 #include "cs_binaries.h"
20 #include "cs_symbol_fwd.hpp"
21 #include "cs_scratchpad.hpp"
22 #include "cs_compunit_decl.hpp"
23 #include "cs_symbol_set_decl.hpp"
24 #include "cs_point_set_decl.hpp"
26 
29 namespace cs{
30 
37  class var_attrs{
38  CS_FLAGS_BOILERPLATE(var_attrs, cs_var_attrs,
39  cs_var_attr_all,
40  "weak",
41  "threadlocal");
43  static const var_attrs NONE;
44 
49  static const var_attrs WEAK;
50 
54  static const var_attrs THREADLOCAL;
55  };
56  CS_FLAGS_BOILERPLATE_FRIENDS(var_attrs,
57  friend class symbol;,
58  cs
59  )
60 #ifdef CS_CPP_IMPL
61  const var_attrs var_attrs::NONE = var_attrs(cs_var_attr_none);
62  const var_attrs var_attrs::WEAK = var_attrs(cs_var_attr_weak);
63  const var_attrs var_attrs::THREADLOCAL = var_attrs(cs_var_attr_threadlocal);
64 #endif
65 
72  class func_attrs{
73  CS_FLAGS_BOILERPLATE(func_attrs, cs_func_attrs,
74  cs_func_attr_all,
75  "weak",
76  "inline",
77  "initialization",
78  "finalization",
79  "library",
80  "compiler-generated",
81  "cs-generated",
82  "constructor",
83  "destructor",
84  "assignment-operator",
85  "template",
86  "csonar-library-refine",
87  "multi-define",
88  "main",
89  "method",
90  "declared-static");
92  static const func_attrs NONE;
93 
98  static const func_attrs WEAK;
99 
102  static const func_attrs INLINE_FUNCTION; /* used to be named
103  * INLINE, but
104  * conflicts with a
105  * common GT-internal
106  * macro */
113 
118  static const func_attrs FINALIZATION;
119 
124  static const func_attrs LIBRARY;
125 
130  static const func_attrs COMPILER_GENERATED;
131 
135  static const func_attrs CS_GENERATED;
136 
140  static const func_attrs CONSTRUCTOR;
141 
145  static const func_attrs DESTRUCTOR;
146 
151 
155  static const func_attrs TEMPLATE;
156 
166  static const func_attrs CSONAR_LIBRARY_REFINE;
167 
173  static const func_attrs MULTI_DEFINE;
174 
178  static const func_attrs MAIN;
179 
183  static const func_attrs METHOD;
187  static const func_attrs DECLARED_STATIC;
188  };
189  CS_FLAGS_BOILERPLATE_FRIENDS(func_attrs,
190  friend class symbol;,
191  cs
192  )
193 #ifdef CS_CPP_IMPL
194  const func_attrs func_attrs::NONE = func_attrs(cs_func_attr_none);
195  const func_attrs func_attrs::WEAK = func_attrs(cs_func_attr_weak);
196  const func_attrs func_attrs::INLINE_FUNCTION = func_attrs(cs_func_attr_inline);
197  const func_attrs func_attrs::INITIALIZATION = func_attrs(cs_func_attr_initialization);
198  const func_attrs func_attrs::FINALIZATION = func_attrs(cs_func_attr_finalization);
199  const func_attrs func_attrs::LIBRARY = func_attrs(cs_func_attr_library);
200  const func_attrs func_attrs::COMPILER_GENERATED = func_attrs(cs_func_attr_compiler_generated);
201  const func_attrs func_attrs::CS_GENERATED = func_attrs(cs_func_attr_cs_generated);
202  const func_attrs func_attrs::CONSTRUCTOR = func_attrs(cs_func_attr_constructor);
203  const func_attrs func_attrs::DESTRUCTOR = func_attrs(cs_func_attr_destructor);
204  const func_attrs func_attrs::ASSIGNMENT_OPERATOR = func_attrs(cs_func_attr_assignment_operator);
205  const func_attrs func_attrs::TEMPLATE = func_attrs(cs_func_attr_template);
206  const func_attrs func_attrs::CSONAR_LIBRARY_REFINE = func_attrs(cs_func_attr_csonar_library_refine);
207  const func_attrs func_attrs::MULTI_DEFINE = func_attrs(cs_func_attr_multi_define);
208  const func_attrs func_attrs::MAIN = func_attrs(cs_func_attr_main);
209  const func_attrs func_attrs::METHOD = func_attrs(cs_func_attr_method);
210  const func_attrs func_attrs::DECLARED_STATIC = func_attrs(cs_func_attr_declared_static);
211 #endif
212 
214  class symbol_kind{
215  CS_ENUM_BOILERPLATE_UB(symbol_kind, cs_abs_loc_kind,
216  check(cs_abs_loc_kind_name(
217  inner, &rv)),
218  cs_abs_loc_kind_count);
219 #include "cs_symbol_kind_decls.hpp"
220  };
221  CS_ENUM_BOILERPLATE_FRIENDS(symbol_kind,
222  friend class ir_factory;
223  friend class symbol;,
225  )
226 #include "cs_symbol_kind_defs.hpp"
227 
243  class symbol{
249  CS_IR_BOILERPLATE(symbol);
250 
269  csuint64 stable_hash() const
270  {
271  return cs_abs_loc_stable_hash(inner);
272  }
273 
274 
297  int stable_cmp(const symbol& other) const
298  {
299  return cs_abs_loc_stable_compare(inner, other.inner);
300  }
301 
302 
312  {
313  cs_abs_loc_kind rv;
314  check(cs_abs_loc_get_kind(inner, &rv));
315  return symbol_kind::;
316  }
317 
318 
329  {
330  cs_var_attrs rv;
331  check(cs_abs_loc_var_attrs(inner, &rv));
332  return var_attrs::;
333  }
334 
335 
345  {
346  cs_func_attrs rv;
347  check(cs_abs_loc_func_attrs(inner, &rv));
348  return func_attrs::;
349  }
350 
351 
360  bool is_global() const
361  {
362  return !!cs_abs_loc_is_global(inner);
363  }
364 
365 
374  bool is_heap() const
375  {
376  return !!cs_abs_loc_is_heap(inner);
377  }
378 
379 
386  bool is_function() const
387  {
388  return !!cs_abs_loc_is_function(inner);
389  }
390 
391 
398  bool is_static_function() const
399  {
400  return !!cs_abs_loc_is_static_function(inner);
401  }
402 
403 
415  bool is_string() const
416  {
417  return !!cs_abs_loc_is_string(inner);
418  }
419 
420 
429  bool is_file_static() const
430  {
431  return !!cs_abs_loc_is_file_static(inner);
432  }
433 
434 
443  bool is_local_static() const
444  {
445  return !!cs_abs_loc_is_local_static(inner);
446  }
447 
448 
457  bool is_local() const
458  {
459  return !!cs_abs_loc_is_local(inner);
460  }
461 
462 
471  bool is_formal() const
472  {
473  return !!cs_abs_loc_is_formal(inner);
474  }
475 
476 
485  bool extern_only() const
486  {
487  return !!cs_abs_loc_extern_only(inner);
488  }
489 
495  std::string name() const
496  {
497  /* The non-friendly name contains an extra "-42" or "'42"
498  * suffix that serves no useful purpose and has
499  * historically been a common cause of bugs in API clients
500  * that did not expect it (why would they? it's not part
501  * of the name).
502  */
503  return cs_abs_loc_friendly_string(inner);
504  }
505 
506 
516  std::string verbose_name() const
517  {
518  return cs_abs_loc_string(inner);
519  }
520 
535  std::string demangled_name() const
536  {
537  string_scratchpad::
538  to_string_functor1_limit<symbol, cs_abs_loc_demangled_name>
539  functor(inner, -1);
540  return string_scratchpad::to_string(functor);
541  }
542 
551  procedure as_procedure() const;
552 
553 
563  std::string represented_string() const
564  {
565  string_scratchpad::
566  to_string_functor1_limit<symbol, cs_abs_loc_represented_string>
567  functor(inner, -1);
568  return string_scratchpad::to_string(functor);
569  }
570 
586  point_set declarations() const;
610  point primary_declaration() const;
625  procedure get_procedure() const;
626 
627 
638  {
639  cs_uid rv;
640  check(cs_abs_loc_compilation_uid(inner, &rv));
641  return compunit::;
642  }
643 
644 
670  {
671  cs_ast rv;
672  check(cs_abs_loc_ast(inner, cglue<ast_family>::unwrap(family), &rv));
673  return ast::;
674  }
675 
676 
694  {
695  cs_ast rv;
696  return cs_abs_loc_ast(inner, cglue<ast_family>::unwrap(family), &rv) == CS_SUCCESS;
697  }
698 
699 
713  void set_ast(ast_family family, ast val)
714  {
715  check(cs_abs_loc_set_ast(inner, cglue<ast_family>::unwrap(family),
716  cglue<ast>::unwrap(val)));
717  }
746  {
747  cs_ast rv;
748  check(cs_abs_loc_type(inner, cglue<ast_family>::unwrap(family), &rv));
749  return ast::;
750  }
751 
752 
769  {
770  cs_ast rv;
771  return cs_abs_loc_type(inner, cglue<ast_family>::unwrap(family), &rv) == CS_SUCCESS;
772  }
773 
774 
789  void set_type(ast_family family, ast val)
790  {
791  check(cs_abs_loc_set_type(inner, cglue<ast_family>::unwrap(family),
792  cglue<ast>::unwrap(val)));
793  }
806  {
807  cs_sfid rvsfid;
808  cs_line rvline;
809  check(cs_abs_loc_file_line(inner, &rvsfid, &rvline));
810  return sfileinst_line_pair(sfileinst::, rvline);
811  }
812 
813 
826  {
827  cs_uid rvuid;
828  check(cs_abs_loc_source_uid(inner, &rvuid));
829  return compunit::;
830  }
831 
832 
847  symbol_set pointed_to_by() const
848  {
849  cs_const_abs_loc_set rv;
850  check(cs_abs_loc_pointed_to_by(inner, &rv));
851  return cglue<symbol_set>::wrap(rv, true);
852  }
870  symbol_set points_to() const
871  {
872  cs_const_abs_loc_set rv;
873  check(cs_abs_loc_points_to(inner, &rv));
874  return cglue<symbol_set>::wrap(rv, true);
875  }
893  point_set used_points() const
894  {
895  cs_const_pdg_vertex_set rv;
896  check(cs_abs_loc_used_vertices( inner, &rv ) );
897  return cglue<point_set>::wrap(rv, true);
898  }
916  point_set killed_points() const
917  {
918  cs_const_pdg_vertex_set rv;
919  check(cs_abs_loc_killed_vertices( inner, &rv ) );
920  return cglue<point_set>::wrap(rv, true);
921  }
940  point_set cond_killed_points() const
941  {
942  cs_const_pdg_vertex_set rv;
943  check(cs_abs_loc_cond_killed_vertices( inner, &rv ) );
944  return cglue<point_set>::wrap(rv, true);
945  }
964  point_set may_killed_points() const
965  {
966  cs_const_pdg_vertex_set rv;
967  check(cs_abs_loc_may_killed_vertices( inner, &rv ) );
968  return cglue<point_set>::wrap(rv, true);
969  }
986  std::vector<symbol> fields() const
987  {
988  typedef scratchpad<symbol> sp;
989  sp::to_vector_functor1<symbol, cs_abs_loc_get_fields>
990  functor(inner);
991  return sp::to_vector(functor);
992  }
1010  symbol container() const
1011  {
1012  cs_abs_loc rv;
1013  check(cs_abs_loc_get_struct(inner, &rv));
1014  return symbol(rv);
1015  }
1034  std::vector<symbol> inline_targets() const
1035  {
1036  typedef scratchpad<symbol> sp;
1037  sp::to_vector_functor1<symbol, cs_abs_loc_get_inline_targets>
1038  functor(inner);
1039  return sp::to_vector(functor);
1040  }
1062  symbol inline_source() const
1063  {
1064  cs_abs_loc rv;
1065  check(cs_abs_loc_get_inline_source(inner, &rv));
1066  return symbol(rv);
1067  }
1085  std::string temp_source() const
1086  {
1087  string_scratchpad::
1088  to_string_functor1_limit<symbol, cs_abs_loc_temp_source>
1089  functor(inner, -1);
1090  return string_scratchpad::to_string(functor);
1091  }
1092 
1093 
1095  std::string as_string() const
1096  { return name(); }
1098 
1100  std::string as_repr() const
1101  { return CS_AS_REPR_FROM_STRING(symbol); }
1102 
1103 
1120  std::string handle() const
1121  {
1122  string_scratchpad::
1123  to_string_functor1_limit<symbol, cs_abs_loc_get_handle>
1124  functor(inner, -1);
1125  return string_scratchpad::to_string(functor);
1126  }
1127 
1128  };
1129  CS_IR_BOILERPLATE_FRIENDS_PLUS_OSTREAM(symbol, cs)
1130 }
1131 
1132 #endif /* CS_SYMBOL_DECL_HPP */
static const func_attrs ASSIGNMENT_OPERATOR
Singleton set containing the "assignment operator" flag: the function is an operator= overload...
Definition: cs_symbol_decl.hpp:160
static const func_attrs MULTI_DEFINE
Singleton set containing the "multi-define" flag: the linkage for this function permits multiple defi...
Definition: cs_symbol_decl.hpp:183
Namespace for CodeSonar/CodeSurfer API.
Definition: cs_ast.hpp:33
static const var_attrs NONE
Empty set: contains no flags.
Definition: cs_symbol_decl.hpp:46
bool is_formal() const
Check: does the symbol represent a formal parameter?
Definition: cs_symbol_decl.hpp:471
symbol_kind get_kind() const
Get the symbol_kind of a symbol.
Definition: cs_symbol_decl.hpp:311
bool extern_only() const
Check: does the symbol have only non-storage-allocating declarations?
Definition: cs_symbol_decl.hpp:485
static const func_attrs CONSTRUCTOR
Singleton set containing the "constructor" flag: the function is a constructor.
Definition: cs_symbol_decl.hpp:150
static const func_attrs INITIALIZATION
Singleton set containing the "initialization" flag: the function is declared with __attribute__((cons...
Definition: cs_symbol_decl.hpp:122
static const func_attrs CS_GENERATED
Singleton set containing the "CS-generated" flag: the function is synthesized by CodeSonar/CodeSurfer...
Definition: cs_symbol_decl.hpp:145
std::pair< cs::sfileinst, cs::line_number > sfileinst_line_pair
A std::pair (sfi, line) where:
Definition: cs_tplt_instantiations.hpp:107
bool is_local() const
Check: does the symbol represent a local?
Definition: cs_symbol_decl.hpp:457
bool is_string() const
Check: does the symbol represent a string literal?
Definition: cs_symbol_decl.hpp:415
bool is_file_static() const
Check: does the symbol represent a file static variable?
Definition: cs_symbol_decl.hpp:429
A function or variable.
Definition: cs_symbol_decl.hpp:243
var_attrs get_var_attrs() const
Get the attributes associated with a non-function symbol.
Definition: cs_symbol_decl.hpp:328
static const func_attrs DESTRUCTOR
Singleton set containing the "destructor" flag: the function is a destructor.
Definition: cs_symbol_decl.hpp:155
A single program point.
Definition: cs_point_decl.hpp:66
An Abstract Syntax Tree (AST).
Definition: cs_ast_decl.hpp:448
bool is_function() const
Check: does the symbol represent a function?
Definition: cs_symbol_decl.hpp:386
A single procedure/function/method.
Definition: cs_procedure_decl.hpp:173
static const var_attrs THREADLOCAL
Singleton set containing the "thread local" flag: the variable has thread-local storage (e...
Definition: cs_symbol_decl.hpp:59
std::string verbose_name() const
Get the verbose name of a symbol.
Definition: cs_symbol_decl.hpp:516
Flag class: describes the attributes of a symbol of any kind EXCEPT symbol_kind::FUNCTION.
Definition: cs_symbol_decl.hpp:37
ast get_ast(ast_family family=ast_family::DEFAULT) const
Get an AST from a symbol.
Definition: cs_symbol_decl.hpp:669
static const func_attrs LIBRARY
Singleton set containing the "library" flag: __CSURF_MARKER_LIBRARY_FUNCTION__ was #define&#39;d to 1 at ...
Definition: cs_symbol_decl.hpp:134
std::string demangled_name() const
[Binary analyses only] Get the demangled user-friendly name of a symbol.
Definition: cs_symbol_decl.hpp:535
csuint64 stable_hash() const
Get a hash value for a symbol, with stable results across sufficiently-similar analyses.
Definition: cs_symbol_decl.hpp:269
static const func_attrs FINALIZATION
Singleton set containing the "finalization" flag: the function is declared with __attribute__((destru...
Definition: cs_symbol_decl.hpp:128
static const ast_family DEFAULT
Represents the first AST family, whatever it might be.
Definition: cs_ast_decl.hpp:25
static const func_attrs CSONAR_LIBRARY_REFINE
Singleton set containing the "CodeSonar library refine" flag: CodeSonar is encouraged to "step into" ...
Definition: cs_symbol_decl.hpp:176
A set of points (point).
Definition: cs_tplt_instantiations.hpp:214
static const func_attrs WEAK
Singleton set containing the "weak" flag: the function has weak linkage (e.g., __attribute__((weak)))...
Definition: cs_symbol_decl.hpp:108
static const func_attrs MAIN
Singleton set containing the "main" flag: this is a main or WinMain function.
Definition: cs_symbol_decl.hpp:188
compunit get_compunit() const
Get the compilation unit to which a symbol is scoped.
Definition: cs_symbol_decl.hpp:637
Enumeration class: the kind of a symbol.
Definition: cs_symbol_decl.hpp:224
bool is_static_function() const
Check: does the symbol represent a static function?
Definition: cs_symbol_decl.hpp:398
sfileinst_line_pair file_line() const
Get the file instance and line of the smallest offset of a declaration point for a symbol...
Definition: cs_symbol_decl.hpp:805
bool is_local_static() const
Check: does the symbol represent a static variable local to a procedure?
Definition: cs_symbol_decl.hpp:443
A set of symbols (symbol).
Definition: cs_tplt_instantiations.hpp:231
Flag class: describes the attributes of a symbol of kind symbol_kind::FUNCTION.
Definition: cs_symbol_decl.hpp:77
static const func_attrs DECLARED_STATIC
Singleton set containing the "declared-static" flag: this was declared with the keyword static...
Definition: cs_symbol_decl.hpp:197
std::string temp_source() const
If a symbol represents a temporary variable, get its pretty-printed unnnormalized C/C++ AST...
Definition: cs_symbol_decl.hpp:1085
bool is_heap() const
Check: does the symbol represent a heap variable?
Definition: cs_symbol_decl.hpp:374
Enumeration class for AST family.
Definition: cs_ast_decl.hpp:50
static const func_attrs COMPILER_GENERATED
Singleton set containing the "compiler-generated" flag: the function is synthesized by the compiler; ...
Definition: cs_symbol_decl.hpp:140
bool is_global() const
Check: does the symbol represent a global variable?
Definition: cs_symbol_decl.hpp:360
bool has_type(ast_family family=ast_family::DEFAULT) const
Check: does a symbol have an associated type AST?
Definition: cs_symbol_decl.hpp:768
static const var_attrs WEAK
Singleton set containing the "weak" flag: the variable has weak linkage (e.g., __attribute__((weak)))...
Definition: cs_symbol_decl.hpp:54
std::string name() const
Get the name of a symbol.
Definition: cs_symbol_decl.hpp:495
static const func_attrs TEMPLATE
Singleton set containing the "template" flag: the function is the result of a template instantiation...
Definition: cs_symbol_decl.hpp:165
compunit source_compunit() const
Get the compilation unit containing the declaration point for a symbol.
Definition: cs_symbol_decl.hpp:825
A compilation unit.
Definition: cs_compunit_decl.hpp:70
func_attrs get_func_attrs() const
Get the attributes associated with a function symbol.
Definition: cs_symbol_decl.hpp:344
static const func_attrs INLINE_FUNCTION
Singleton set containing the "inline function" flag: the function has inline linkage.
Definition: cs_symbol_decl.hpp:112
std::string represented_string() const
For a symbol that represents a string literal, get the string.
Definition: cs_symbol_decl.hpp:563
bool has_ast(ast_family family=ast_family::DEFAULT) const
Check: does a symbol have an associated AST (ast) of the specified family (ast_family)?
Definition: cs_symbol_decl.hpp:693
int stable_cmp(const symbol &other) const
Compare with another point, with stable results across sufficiently-similar analyses.
Definition: cs_symbol_decl.hpp:297
static const func_attrs METHOD
Singleton set containing the "method" flag: this is a method.
Definition: cs_symbol_decl.hpp:193
ast get_type(ast_family family=ast_family::DEFAULT) const
Get the type AST that is associated with a symbol.
Definition: cs_symbol_decl.hpp:745
static const func_attrs NONE
Empty set: contains no flags.
Definition: cs_symbol_decl.hpp:100