CodeSonar C++ API
[For improved navigation, enable JavaScript.]
cs_sfileinst_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_SFID_DECL_HPP
17 #define CS_SFID_DECL_HPP
18 
19 #include "cs_ir_boilerplate.hpp"
20 #include "cs_iterator_adapter.hpp"
21 #include "cs_compunit_decl.hpp"
22 #include "cs_syntax_kind.hpp"
23 #include "cs_source_files.h"
24 
27 namespace cs{
28  CS_IR_BOILERPLATE_FORWARD(
29  sfileinst,
30  cs_sfid,
31  friend class project;
32  friend class point;
33  friend class procedure;
34  friend class symbol;
35  friend class compunit;
36  friend class sfile;
37  friend class ast_field;
38  friend class iterator_adapter<sfile_instance_iterator_policy>;
39  friend class iterator_adapter<sfileinst_children_iterator_policy>;
40  friend class set_mixin<int_pair_set>;
41  friend class set_mixin<point_set>;
42  friend class int_pairs_to_points_map;
43  friend class cglue<locations_node>;
44  friend class visitor<sfileinst>;
45  friend class listing_transformer;
46  friend class point_info_collector;
47  friend class warningclass;
48  friend class analysis;
50  friend struct FatTypeTraits<sfileinst>;
52  friend class sfileinst_children_iterator_policy;
53  friend class sfile_instance_iterator_policy;
54  );
55 
56  class sfileinst_children_iterator_policy_container{
57  public:
58  cs_sfid value;
59  sfileinst_children_iterator_policy_container(cs_sfid v): value(v){}
60  };
61 
62  class sfileinst_children_iterator_policy{
63  public:
64  typedef cs_sfid_children_iter iterator_impl;
65  typedef sfileinst key;
66  typedef sfileinst_children_iterator_policy_container ctype;
67 
68  static cs_result iter_first(
69  const ctype &container,
70  cglue<key>::ctype *val,
71  iterator_impl *it)
72  { return cs_file_sfid_children_iter_first( container.value, val, it ); }
73  static cs_result iter_next(
74  const ctype &container,
75  cglue<key>::ctype *val,
76  iterator_impl *it)
77  { return cs_file_sfid_children_iter_next( val, it ); }
78  static cs_result iter_close(iterator_impl *it)
79  { return cs_file_sfid_children_iter_close( it ); }
80  static const char *name()
81  { return "sfileinst_children_iterator"; }
82  };
83 
84  class sfileinst_color_map_iterator_policy_container{
85  public:
86  cs_sfid f;
87  cs_syntax_kind k;
88  cs_line lb;
89  sfileinst_color_map_iterator_policy_container(
90  cs_sfid _f,
91  cs_syntax_kind _k,
92  cs_line _lb)
93  : f(_f), k(_k), lb(_lb){}
94  };
95 
96  template<>
97  class cglue<position_range>
98  {
99  friend class iterator_adapter<sfileinst_color_map_iterator_policy>;
100  friend class sfileinst_color_map_iterator_policy;
101  public:
102  typedef position_range type;
103  typedef position_range ctype;
104  CS_CXX_API_CGLUE_ACCESS_MODIFIER:
105  static type wrap(const type &c){ return c; }
106  static type unwrap(const type &c){ return c; }
107  };
108 
109  class sfileinst_color_map_iterator_policy{
110  public:
111  typedef cs_color_map_iter iterator_impl;
112  typedef position_range key;
113  typedef sfileinst_color_map_iterator_policy_container ctype;
114 
115  static cs_result iter_first(
116  const ctype &container,
117  cglue<key>::ctype *val,
118  iterator_impl *it)
119  {
120  if( container.lb > 1 )
121  {
122  return cs_file_color_map_iter_scan_first(
123  container.f,
124  container.k,
125  container.lb,
126  &val->first.first,
127  &val->first.second,
128  &val->second.first,
129  &val->second.second,
130  it );
131  }
132  else
133  {
134  return cs_file_color_map_iter_first(
135  container.f,
136  container.k,
137  &val->first.first,
138  &val->first.second,
139  &val->second.first,
140  &val->second.second,
141  it );
142  }
143  }
144  static cs_result iter_next(
145  const ctype &container,
146  cglue<key>::ctype *val,
147  iterator_impl *it)
148  { return cs_file_color_map_iter_next(
149  &val->first.first,
150  &val->first.second,
151  &val->second.first,
152  &val->second.second,
153  it ); }
154  static cs_result iter_close(iterator_impl *it)
155  { return cs_file_color_map_iter_close( it ); }
156  static const char *name()
157  { return "sfileinst_color_map_iterator"; }
158  };
159 
160 
162  class line_counts{
163  size_t blank, comment, code, mixed;
164  public:
169  size_t get_blank() const
170  { return blank; }
171 
177  size_t get_comment() const
178  { return comment; }
179 
185  size_t get_code() const
186  { return code; }
187 
188 
195  size_t get_mixed() const
196  { return mixed; }
197 
198 
211  line_counts(size_t _blank, size_t _comment,
212  size_t _code, size_t _mixed)
213  : blank(_blank), comment(_comment),
214  code(_code), mixed(_mixed){}
215 
216  private:
221  void print(std::ostream &out) const
222  {
223  out << "blank: " << blank
224  << ", comment: " << comment
225  << ", code: " << code
226  << ", mixed: " << mixed;
227  }
228 
229  NOTSWIG(friend std::ostream &operator<<( std::ostream &out, const line_counts &a ));
230 
231  public:
234  std::string as_string() const
235  {
236  std::stringstream buf;
237  print(buf);
238  return buf.str();
239  }
240 
241 
244  std::string as_repr() const
245  {
246  std::stringstream buf;
247  buf << CS_AS_REPR_PREFIX
248  << "line_counts ";
249  print(buf);
250  buf << CS_AS_REPR_SUFFIX;
251  return buf.str();
252  }
253  };
254 
264  inline std::ostream &operator<<( std::ostream &out, const line_counts &a )
265  {
266  a.print(out);
267  return out;
268  }
270 
297  class sfileinst{
303  CS_IR_BOILERPLATE(sfileinst);
304 
305 
322  csuint64 stable_hash() const
323  {
324  return cs_sfid_stable_hash(inner);
325  }
326 
355  int stable_cmp(const sfileinst& other) const
356  {
357  return cs_sfid_stable_compare(inner, other.inner);
358  }
359 
371  std::string name() const
372  {
373  string_scratchpad::
374  to_string_functor1_limit<
375  sfileinst, cs_file_get_include_name>
376  functor(inner, -1);
377  return string_scratchpad::to_string(functor);
378  }
379 
380 
397  std::string normalized_name() const
398  {
399  string_scratchpad::
400  to_string_functor1_limit<
401  sfileinst, cs_file_get_normalized_include_name>
402  functor(inner, -1);
403  return string_scratchpad::to_string(functor);
404  }
405 
406 
414  std::vector<sfileinst> include_tree_path() const
415  {
416  typedef scratchpad<sfileinst> sp;
417  sp::to_vector_functor1
418  <sfileinst, cs_file_get_include_pos_name>
419  functor(inner);
420  return sp::to_vector(functor);
421  }
422 
423 
471  std::string read(line_number line_start, column_number col_start,
472  line_number line_end, column_number col_end,
473  size_t limit=SIZE_MAX) const
474  {
475  if( line_end == static_cast<line_number>(-1) )
476  line_end = line_count() + 1;
477  string_scratchpad::
478  to_string_functor5_limit<sfileinst, cs_line, cs_column, cs_line, cs_column, cs_file_get_characters>
479  functor(inner,
480  line_start, col_start,
481  line_end, col_end,
482  limit);
483  return string_scratchpad::to_string(functor);
484  }
485 
486 
529  std::string read_lines(line_number line_start=1,
530  line_number line_end=UINT32_MAX,
531  size_t limit=SIZE_MAX) const
532  {
533  return read(line_start, 0, line_end, 0, limit);
534  }
535 
536 
559  std::string read_line(line_number line, size_t limit=SIZE_MAX) const
560  {
561  return read(line, 0, line, -1, limit);
562  }
563 
569  line_number line_count() const
570  {
571  line_number rv;
572  check(cs_file_get_linecount(inner, &rv));
573  return rv;
574  }
575 
576 
591  {
592  cs_file_offset rv;
593  check(cs_file_get_line_offset(inner, line, &rv));
594  return rv;
595  }
596 
597 
611  {
612  cs_line rv;
613  check(cs_file_get_line_num(inner, offset, &rv));
614  return rv;
615  }
616 
629  std::pair<line_number, column_number> offset_to_line_column(
630  file_offset offset) const
631  {
632  cs_line line;
633  cs_column col;
634  check(cs_file_get_linecol(inner, offset, &line, &col));
635  return std::pair<line_number, column_number>(line, col);
636  }
637 
653  std::pair<file_offset, file_offset> line_range(line_number line) const
654  {
655  cs_file_offset rv1, rv2;
656  check(cs_file_get_line_range(inner, line, &rv1, &rv2));
657  return std::pair<file_offset, file_offset>(rv1, rv2);
658  }
659 
660 
667  compunit get_compunit() const
668  {
669  cs_uid rv;
670  check(cs_file_sfid_get_uid(inner, &rv));
671  return compunit::;
672  }
673 
674 
688  {
690  sfileinst_children_iterator_policy_container(inner));
691  }
693 
703  std::vector<sfileinst> children_vector() const
704  {
705  typedef scratchpad<sfileinst> sp;
706  sp::to_vector_functor1
707  <sfileinst, cs_file_sfid_get_children>
708  functor(inner);
709  return sp::to_vector(functor);
710  }
711 
712 
726  sfileinst parent() const
727  {
728  cs_sfid rv;
729  check(cs_file_sfid_get_parent(inner, &rv));
730  return sfileinst(rv);
731  }
732 
744  {
745  cs_sfid s;
746  cs_line ln;
747  check(cs_file_sfid_get_parent_line(inner, &s, &ln));
748  return sfileinst_line_pair(sfileinst::, ln);
749  }
750 
764  sfileinst child_at(line_number ln) const
765  {
766  cs_sfid rv;
767  check(cs_file_sfid_get_child_at(inner, ln, &rv));
768  return sfileinst(rv);
769  }
770 
785  syntax_kind k,
786  line_number lowerbound=0) const
787  {
789  sfileinst_color_map_iterator_policy_container(
790  inner, cglue<syntax_kind>::unwrap(k), lowerbound));
791  }
792 
793 
808  line_number start=1,
809  line_number end=UINT32_MAX) const
810  {
811  size_t rv1, rv2, rv3, rv4;
812  check(cs_file_count_lines(inner, start, end, &rv1, &rv2, &rv3, &rv4));
813  return line_counts(rv1, rv2, rv3, rv4);
814  }
815 
817 #if defined(CSONAR_API_SWIG_HPP) || !defined(SWIG)
818  bool is_system_include() const;
819 #endif
820 
838  sfile get_sfile() const;
839 
840 
858  std::vector<ast> asts_at(
859  line_number line) const
860  {
861  typedef scratchpad<ast> sp;
862  sp::to_vector_functor3
863  <sfileinst, cs_line, const cs_ast_class*, cs_file_asts_at>
864  functor(inner, line, NULL);
865  size_t cardinality;
866  cs_result r = sp::execute(functor, cardinality);
867  check(r);
868  return sp::to_vector(cardinality);
869  }
870 
871 
893  std::vector<ast> asts_at(
894  line_number line,
895  const std::vector<ast_class> &classes) const
896  {
897  typedef scratchpad<ast> sp;
898  cs_ast_class *cc = new cs_ast_class[classes.size() + 1];
899  cc[classes.size()] = 0;
900  for( size_t i = 0; i < classes.size(); i++ )
901  cc[i] = cglue<ast_class>::unwrap(classes[i]);
902  sp::to_vector_functor3
903  <sfileinst, cs_line, const cs_ast_class*, cs_file_asts_at>
904  functor(inner, line, cc);
905  size_t cardinality;
906  cs_result r = sp::execute(functor, cardinality);
907  delete[] cc;
908  check(r);
909  return sp::to_vector(cardinality);
910  }
911 
912 
923  std::string as_string() const
924  { return name(); }
925 
926 
929  std::string as_repr() const
930  { return CS_AS_REPR_FROM_STRING(sfileinst); }
931 
932 
949  std::string handle() const
950  {
951  string_scratchpad::
952  to_string_functor1_limit<sfileinst, cs_sfid_get_handle>
953  functor(inner, -1);
954  return string_scratchpad::to_string(functor);
955  }
956 
957  };
958  CS_IR_BOILERPLATE_FRIENDS_PLUS_OSTREAM(sfileinst, cs)
959 
960  inline sfileinst cglue<sfileinst>::wrap(const cs_sfid &c)
961  { return sfileinst(c); }
962 
963  inline cs_sfid cglue<sfileinst>::unwrap(const sfileinst &c)
964  { return c.unwrap(); }
965 
969  class sfileinst_range{
970  sfileinst file;
973  public:
974 
979  sfileinst get_file() const
980  { return file; }
981 
982 
988  { return start; }
989 
990 
996  { return end; }
997 
1009  sfileinst_range(sfileinst _file,
1010  line_and_column_number _start,
1012  : file(_file)
1013  , start(_start)
1014  , end(_end){}
1015 
1016  private:
1021  void print(std::ostream &out) const
1022  {
1023  out << file
1024  << ":"
1025  << start.first << "." << start.second
1026  << "-"
1027  << end.first << "." << end.second;
1028  }
1029 
1030  NOTSWIG(friend std::ostream &operator<<( std::ostream &out, const sfileinst_range &a ));
1031 
1032  public:
1035  std::string as_string() const
1036  {
1037  std::stringstream buf;
1038  print(buf);
1039  return buf.str();
1040  }
1041 
1044  std::string as_repr() const
1045  {
1046  std::stringstream buf;
1047  buf << CS_AS_REPR_PREFIX
1048  << "sfileinst_range ";
1049  print(buf);
1050  buf << CS_AS_REPR_SUFFIX;
1051  return buf.str();
1052  }
1053  };
1054 
1064  inline std::ostream &operator<<( std::ostream &out, const sfileinst_range &a )
1065  {
1066  a.print(out);
1067  return out;
1068  }
1069 }
1070 
1071 #endif /* CS_SFILEINST_DECL_HPP */
line_and_column_number get_end() const
Get the end line and column number from a sfileinst_range.
Definition: cs_sfileinst_decl.hpp:1003
cs_line line_number
A line number in a source file (sfile) or source file instance (sfileinst).
Definition: cs_types.hpp:387
sfileinst_children_iterator children() const
Get an iterator over the include-tree children of a source file instance.
Definition: cs_sfileinst_decl.hpp:692
sfileinst_range(sfileinst _file, line_and_column_number _start, line_and_column_number _end)
Constructor.
Definition: cs_sfileinst_decl.hpp:1017
std::pair< line_number, column_number > offset_to_line_column(file_offset offset) const
Get the line number and column number associated with a point identified by an offset in a source fil...
Definition: cs_sfileinst_decl.hpp:634
Namespace for CodeSonar/CodeSurfer API.
Definition: cs_ast.hpp:33
int stable_cmp(const sfileinst &other) const
Comparison function for sfileinst, with stable results across sufficiently-similar analyses...
Definition: cs_sfileinst_decl.hpp:360
std::pair< cs::sfileinst, cs::line_number > sfileinst_line_pair
A std::pair (sfi, line) where:
Definition: cs_tplt_instantiations.hpp:107
Iterator over the include-tree children (sfileinst) of a source file instance.
Definition: cs_tplt_instantiations.hpp:1035
bool is_system_include() const
Check: is a file instance (sfileinst) an instance of a system include file?
Definition: csonar_visitor.hpp:5750
size_t get_code() const
Get the number of code-only lines from a line_counts object.
Definition: cs_sfileinst_decl.hpp:185
line_counts(size_t _blank, size_t _comment, size_t _code, size_t _mixed)
Constructor.
Definition: cs_sfileinst_decl.hpp:211
Enumeration class: the kind of a span within a source file instance (sfileinst).
Definition: cs_syntax_kind.hpp:25
sfile get_sfile() const
Get the source file corresponding to a source file instance.
Definition: cs_sfileinst.hpp:25
A collection of line count statistics.
Definition: cs_sfileinst_decl.hpp:162
sfileinst_color_map_iterator color_map_iterator(syntax_kind k, line_number lowerbound=0) const
Get an iterator over the spans of the specified syntax_kind at or after the specified line in a sourc...
Definition: cs_sfileinst_decl.hpp:789
std::string name() const
Get the unnormalized absolute path name for a source file instance.
Definition: cs_sfileinst_decl.hpp:376
std::string read_line(line_number line, size_t limit=SIZE_MAX) const
Read a single line from the source file.
Definition: cs_sfileinst_decl.hpp:564
compunit get_compunit() const
Get the compilation unit to which a file instance belongs.
Definition: cs_sfileinst_decl.hpp:672
A range of (contiguous) locations in a single source file instance.
Definition: cs_sfileinst_decl.hpp:977
std::string as_string() const
Get the unnormalized absolute path name for a source file instance.
Definition: cs_sfileinst_decl.hpp:928
std::vector< sfileinst > include_tree_path() const
Get the include tree path for a source file instance.
Definition: cs_sfileinst_decl.hpp:419
std::string normalized_name() const
Get the normalized absolute path name for a source file instance.
Definition: cs_sfileinst_decl.hpp:402
std::string as_repr() const
Get a representation of a sfileinst object that includes information useful for debugging.
Definition: cs_sfileinst_decl.hpp:937
std::string as_string() const
Get a simple string representation of a line_counts object.
Definition: cs_sfileinst_decl.hpp:236
line_and_column_number get_start() const
Get the start line and column number from a sfileinst_range.
Definition: cs_sfileinst_decl.hpp:995
cs_column column_number
A character position in a line (line_number) of a source file instance (sfileinst).
Definition: cs_types.hpp:378
std::string as_repr() const
Get a representation of a sfileinst_range object that includes information useful for debugging...
Definition: cs_sfileinst_decl.hpp:1057
std::string read(line_number line_start, column_number col_start, line_number line_end, column_number col_end, size_t limit=SIZE_MAX) const
Get a substring from a source file instance, using (line, column) pairs to specify the beginning and ...
Definition: cs_sfileinst_decl.hpp:476
Iterator over the spans (position_range) of a specific syntax_kind within a source file instance (sfi...
Definition: cs_tplt_instantiations.hpp:1074
std::vector< ast > asts_at(line_number line) const
Get the ASTs at the specified location in a source file instance.
Definition: cs_sfileinst_decl.hpp:863
sfileinst_line_pair parent_line() const
Get the include location of a source file instance.
Definition: cs_sfileinst_decl.hpp:748
std::string as_repr() const
Get a representation of a line_counts object that includes information useful for debugging...
Definition: cs_sfileinst_decl.hpp:249
A source file.
Definition: cs_sfile_decl.hpp:98
std::string handle() const
Get a handle for this sfileinst.
Definition: cs_sfileinst_decl.hpp:957
size_t get_comment() const
Get the number of comment-only lines from a line_counts object.
Definition: cs_sfileinst_decl.hpp:177
sfileinst parent() const
Get the include-tree parent of a source file instance.
Definition: cs_sfileinst_decl.hpp:731
sfileinst child_at(line_number ln) const
Get the source file instance included at the specified line.
Definition: cs_sfileinst_decl.hpp:769
file_offset line_offset(line_number line) const
Get the source file character offset corresponding to the first character of a line within an the sou...
Definition: cs_sfileinst_decl.hpp:595
std::pair< file_offset, file_offset > line_range(line_number line) const
Get the character offsets for the beginning and the end of a line within a source file...
Definition: cs_sfileinst_decl.hpp:658
csuint64 stable_hash() const
Get a hash value for a sfileinst, with stable results across sufficiently-similar analyses...
Definition: cs_sfileinst_decl.hpp:327
A source file instance.
Definition: cs_sfileinst_decl.hpp:302
std::string read_lines(line_number line_start=1, line_number line_end=UINT32_MAX, size_t limit=SIZE_MAX) const
Get a substring from a source file instance, using line numbers to specify the beginning and end of t...
Definition: cs_sfileinst_decl.hpp:534
line_counts count_lines(line_number start=1, line_number end=UINT32_MAX) const
For a set of adjacent lines in a source file instance, retrieve statistics about the classifications ...
Definition: cs_sfileinst_decl.hpp:812
std::pair< std::pair< csuint32, csuint32 >, std::pair< csuint32, csuint32 > > position_range
A std::pair of line_and_column_number, designating the beginning and end of a span in a source file inst...
Definition: cs_tplt_instantiations.hpp:88
cs_file_offset file_offset
A character position in a source file (sfile).
Definition: cs_types.hpp:396
A compilation unit.
Definition: cs_compunit_decl.hpp:70
cs::sfileinst_children_iterator_policy
Definition: cs_sfileinst_decl.hpp:62
int_pair line_and_column_number
An int_pair in which the first element represents a line number in a source file instance, and the second element represents a column (that is, character position) in that line.
Definition: cs_tplt_instantiations.hpp:78
cs::sfileinst_color_map_iterator_policy
Definition: cs_sfileinst_decl.hpp:109
size_t get_mixed() const
Get the number of mixed lines from a line_counts object.
Definition: cs_sfileinst_decl.hpp:195
size_t get_blank() const
Get the number of blank lines.
Definition: cs_sfileinst_decl.hpp:169
std::vector< sfileinst > children_vector() const
Get the include-tree children of a source file instance.
Definition: cs_sfileinst_decl.hpp:708
line_number offset_to_line(file_offset offset) const
Get the line number associated with a point identified by an offset in a source file instance (sfilei...
Definition: cs_sfileinst_decl.hpp:615
sfileinst get_file() const
Get the source file instance from a sfileinst_range.
Definition: cs_sfileinst_decl.hpp:987
line_number line_count() const
Get the number of lines in a source file instance.
Definition: cs_sfileinst_decl.hpp:574
std::string as_string() const
Get a simple string representation of a sfileinst_range object.
Definition: cs_sfileinst_decl.hpp:1045