CodeSonar C++ API
[For improved navigation, enable JavaScript.]
cs_sfile_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_SFILE_DECL_HPP
17 #define CS_SFILE_DECL_HPP
18 
19 #include "cs_ir_boilerplate.hpp"
20 #include "cs_iterator_adapter.hpp"
21 #include "cs_sfileinst_decl.hpp"
22 #include "cs_procedure_decl.hpp"
23 #include "cs_directory_fwd.hpp"
24 #include "cs_xref_fwd.hpp"
25 
28 namespace cs{
29  CS_IR_BOILERPLATE_FORWARD(
30  sfile,
31  cs_sf,
32  friend class sfileinst;
33  friend class compunit;
34  friend class project;
35  friend class iterator_adapter<directory_files_iterator_policy>;
36  friend class general_metric_manager;
37  friend class cglue<xr_tuple>;
38  friend class xr_definition_iterator_policy;
39  friend class xr_occurrence_iterator_policy;
40  friend class xr_homonym_iterator_policy;
41  friend class xr_intra_definition_iterator_policy;
42  friend class xr_query_iterator_policy_container;
43  friend class xr_tuple;
44  friend class metric_sfile_filter;
45  friend class metric_granularity_policy<sfile>;
46  friend class metricclass_granularity_policy<sfile>;
47  friend class iterator_adapter<project_sfiles_iterator_policy>;
49  friend struct FatTypeTraits<sfile>;
51  friend class directory_files_iterator_policy;
52  friend class project_sfiles_iterator_policy;
53  );
54 
55  class sfile_instance_iterator_policy{
56  public:
57  typedef cs_sf_instance_iter iterator_impl;
58  typedef sfileinst key;
59  typedef cs_sf ctype;
60 
61  static cs_result iter_first(
62  ctype container,
63  cglue<key>::ctype *val,
64  iterator_impl *it)
65  { return cs_sf_instance_iter_first( container, val, it ); }
66  static cs_result iter_next(
67  ctype container,
68  cglue<key>::ctype *val,
69  iterator_impl *it)
70  { return cs_sf_instance_iter_next( val, it ); }
71  static cs_result iter_close(iterator_impl *it)
72  { return cs_sf_instance_iter_close( it ); }
73  static const char *name()
74  { return "sfile_instance_iterator"; }
75  };
76 
77 
98  class sfile{
104  CS_IR_BOILERPLATE(sfile);
105 
106 
124  csuint64 stable_hash() const
125  {
126  return cs_sf_stable_hash(inner);
127  }
128 
129 
156  int stable_cmp(const sfile& other) const
157  {
158  return cs_sf_stable_compare(inner, other.inner);
159  }
160 
161 
167  csuint64 hash64() const
168  { return cs_sf_hash64(inner); }
169 
170 
189  {
190  cs_sfid rv;
191  check(cs_sf_arbitrary_sfid(inner, &rv));
192  return sfileinst::;
193  }
194 
195 
213  { return sfile_instance_iterator(inner); }
214 
215 
231  std::vector<procedure> procedures_on_line(
232  line_number ln,
233  bool exact=false) const
234  {
235  typedef scratchpad<procedure> sp;
236  sp::to_vector_functor3
237  <sfile, cs_line, cs_boolean, cs_sf_line_pdgs>
238  functor(inner, ln, exact);
239  return sp::to_vector(functor);
240  }
241 
243 #if defined(CSONAR_API_SWIG_HPP) || !defined(SWIG)
244  /* documented in csonar_visitor.hpp */
245  std::vector<procedure> procedures_on_line_fast(
246  line_number ln,
247  bool closest) const;
248 
249  std::vector<procedure> procedures_on_line_fast(
250  line_number ln,
251 #ifdef SWIG
252  csuint32
253 #else
254  cs_hash_t
255 #endif
256  friendly_name_hash,
257  bool closest) const;
258 #endif
259 
281  line_number line,
282  csuint64 token_namehash,
284 
303  line_number line,
304  const std::string &token,
306 
307 
338  line_number line,
339  csuint64 token_namehash,
340  xr_kind_role kr_filter,
341  size_t limit_per_kind_role = 0,
343 
374  line_number line,
375  const std::string &token,
376  xr_kind_role kr_filter,
377  size_t limit_per_kind_role = 0,
379 
408  line_number line,
409  csuint64 token_namehash,
410  size_t limit_per_kind_role = 0,
412 
440  line_number line,
441  const std::string &token,
442  size_t limit_per_kind_role = 0,
444 
484  line_number line,
485  csuint64 token_namehash,
486  xr_kind kind_filter,
487  size_t limit_per_kind_role = 0,
489 
531  line_number line,
532  const std::string &token,
533  xr_kind kind_filter,
534  size_t limit_per_kind_role = 0,
536 
565  line_number line,
566  csuint64 token_namehash,
567  size_t limit_per_kind_role = 0,
569 
596  line_number line,
597  const std::string &token,
598  size_t limit_per_kind_role = 0,
600 
601 
626  line_number lb,
627  line_number ub) const;
628 
629 
635  directory parent() const;
636 
637 
651  std::string name() const
652  { return arbitrary_instance().name(); }
653 
654 
674  std::string normalized_name() const
675  { return arbitrary_instance().normalized_name(); }
676 
678  std::string as_string() const
679  {
680 #if !CS_CPP_NO_EXCEPTIONS
681  try{
682 #endif
683  return name();
684 #if !CS_CPP_NO_EXCEPTIONS
685  }catch(result){
686  return "<unknown_file>";
687  }
688 #endif
689  }
690 
691 
693  std::string as_repr() const
694  { return CS_AS_REPR_FROM_STRING(sfile); }
695 
696 
703  /* In a type-safe C++ setting, it would be hard to get into a
704  * state where this doesn't return \c true.
705  */
706  bool is_valid() const
707  {
708  cs_result r = cs_sf_is_valid(inner);
709  if( r == CS_ELEMENT_NOT_PRESENT )
710  return false;
711  check(r);
712  return true;
713  }
732  std::string handle() const
733  {
734  string_scratchpad::
735  to_string_functor1_limit<sfile, cs_sf_get_handle>
736  functor(inner, -1);
737  return string_scratchpad::to_string(functor);
738  }
739 
740  };
741  CS_IR_BOILERPLATE_FRIENDS_PLUS_OSTREAM(sfile, cs)
742 
743  inline sfile cglue<sfile>::wrap(const cs_sf &c)
744  { return sfile(c); }
745 
746  inline cs_sf cglue<sfile>::unwrap(const sfile &c)
747  { return c.unwrap(); }
748 }
749 
750 #endif /* CS_SFILE_DECL_HPP */
cs_line line_number
A line number in a source file (sfile) or source file instance (sfileinst).
Definition: cs_types.hpp:387
std::string name() const
Get the unnormalized absolute path name for a source file.
Definition: cs_sfile_decl.hpp:651
sfile_instance_iterator instances() const
Get an iterator over the instances (sfileinst) of a source file.
Definition: cs_sfile_decl.hpp:212
Namespace for CodeSonar/CodeSurfer API.
Definition: cs_ast.hpp:33
xr_intra_definition_iterator range_definition_iterator(line_number lb, line_number ub) const
Given a code region in a source file, get an iterator over the definitions of all source entities wit...
Definition: cs_sfile.hpp:156
xr_definition_iterator token_definition_iterator(line_number line, csuint64 token_namehash, xr_def_iter_flags flags=xr_def_iter_flags::NONE) const
Given a token occurrence in a source file, get an iterator over all definitions of the corresponding ...
Definition: cs_sfile.hpp:26
cs::sfile_instance_iterator_policy
Definition: cs_sfile_decl.hpp:55
Iterator over the definitions (xr_tuple) of a token.
Definition: cs_tplt_instantiations.hpp:1140
A directory.
Definition: cs_directory_decl.hpp:91
Iterator over the definitions (xr_tuple) of a token.
Definition: cs_tplt_instantiations.hpp:1182
std::string name() const
Get the unnormalized absolute path name for a source file instance.
Definition: cs_sfileinst_decl.hpp:376
static const xr_occ_iter_flags DROP_LIBMODEL_DEFS
Singleton set containing the "drop library model definitions" flag: if an occurrence&#39;s associated def...
Definition: cs_xref_fwd.hpp:91
static const xr_occ_iter_flags DROP_LIBMODEL_OCCS
Singleton set containing the "drop library model occurrences" flag: if an occurrence is in a library ...
Definition: cs_xref_fwd.hpp:97
std::string normalized_name() const
Get the normalized absolute path name for a source file instance.
Definition: cs_sfileinst_decl.hpp:402
sfileinst arbitrary_instance() const
Get an (arbitrary) instance of a source file.
Definition: cs_sfile_decl.hpp:188
std::string as_string() const
Get a simple string representation of a sfile object.
Definition: cs_sfile_decl.hpp:680
Flag class: properties for xr_occurrence_iterator and xr_homonym_iterator iterators.
Definition: cs_xref_fwd.hpp:70
csuint64 stable_hash() const
Get a hash value for a sfile, with stable results across sufficiently-similar analyses.
Definition: cs_sfile_decl.hpp:124
int stable_cmp(const sfile &other) const
Comparison function for sfile, with stable results across sufficiently-similar analyses.
Definition: cs_sfile_decl.hpp:156
Iterator over the homonyms (xr_tuple) of a token.
Definition: cs_tplt_instantiations.hpp:1248
std::string handle() const
Get a handle for this sfile.
Definition: cs_sfile_decl.hpp:737
directory parent() const
Get the parent directory for a source file.
Definition: cs_sfile.hpp:165
Iterator over the occurrences (xr_tuple) of a token.
Definition: cs_tplt_instantiations.hpp:1215
std::string as_repr() const
Get a representation of a sfile object that includes information useful for debugging.
Definition: cs_sfile_decl.hpp:698
A source file.
Definition: cs_sfile_decl.hpp:98
The result of an API operation.
Definition: cs_result.hpp:50
static const xr_def_iter_flags NONE
Empty set: contains no flags.
Definition: cs_xref_fwd.hpp:46
xr_occurrence_iterator token_occurrence_iterator(line_number line, csuint64 token_namehash, xr_kind_role kr_filter, size_t limit_per_kind_role=0, xr_occ_iter_flags flags=xr_occ_iter_flags::DROP_LIBMODEL_DEFS|xr_occ_iter_flags::DROP_LIBMODEL_OCCS) const
Given a token occurrence in a source file, where the token is specified by namehash, get an iterator over all occurrences of the corresponding source entity in a specific kind/role.
Definition: cs_sfile.hpp:52
A source file instance.
Definition: cs_sfileinst_decl.hpp:302
Enumeration class: describes the kind of a (non-keyword) token.
Definition: cs_xr_kind_role.hpp:31
csuint64 hash64() const
64-bit hash function for sfile.
Definition: cs_sfile_decl.hpp:167
std::vector< procedure > procedures_on_line(line_number ln, bool exact=false) const
Get the procedures whose definition appears on the specified line in a source file.
Definition: cs_sfile_decl.hpp:231
xr_homonym_iterator token_homonym_iterator(line_number line, csuint64 token_namehash, xr_kind kind_filter, size_t limit_per_kind_role=0, xr_occ_iter_flags flags=xr_occ_iter_flags::DROP_LIBMODEL_DEFS|xr_occ_iter_flags::DROP_LIBMODEL_OCCS) const
Given a token occurrence (the "focal occurrence") in a source file, where the token is specified by n...
Definition: cs_sfile.hpp:104
Iterator over the instances (sfileinst) of a source file (sfile).
Definition: cs_tplt_instantiations.hpp:1001
std::string normalized_name() const
Get the normalized absolute path name for a source file.
Definition: cs_sfile_decl.hpp:674
std::vector< procedure > procedures_on_line_fast(line_number ln, bool closest) const
Get all procedures defined on the specified line of a source file.
Definition: csonar_visitor.hpp:6127
Enumeration class: describes a valid token kind-role (kind-usage) pair.
Definition: cs_xr_kind_role.hpp:67
Flag class: properties for an xr_definition_iterator.
Definition: cs_xref_fwd.hpp:38