c++: Tweaks for -Wredundant-move [PR107363]
[official-gcc.git] / gcc / analyzer / region.h
blobecae887edaf139e7a20583c677e593735a234e8d
1 /* Regions of memory.
2 Copyright (C) 2019-2022 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_ANALYZER_REGION_H
22 #define GCC_ANALYZER_REGION_H
24 #include "analyzer/complexity.h"
26 namespace ana {
28 /* An enum for identifying different spaces within memory. */
30 enum memory_space
32 MEMSPACE_UNKNOWN,
33 MEMSPACE_CODE,
34 MEMSPACE_GLOBALS,
35 MEMSPACE_STACK,
36 MEMSPACE_HEAP,
37 MEMSPACE_READONLY_DATA,
38 MEMSPACE_THREAD_LOCAL
41 /* An enum for discriminating between the different concrete subclasses
42 of region. */
44 enum region_kind
46 RK_FRAME,
47 RK_GLOBALS,
48 RK_CODE,
49 RK_FUNCTION,
50 RK_LABEL,
51 RK_STACK,
52 RK_HEAP,
53 RK_THREAD_LOCAL,
54 RK_ROOT,
55 RK_SYMBOLIC,
56 RK_DECL,
57 RK_FIELD,
58 RK_ELEMENT,
59 RK_OFFSET,
60 RK_SIZED,
61 RK_CAST,
62 RK_HEAP_ALLOCATED,
63 RK_ALLOCA,
64 RK_STRING,
65 RK_BIT_RANGE,
66 RK_VAR_ARG,
67 RK_ERRNO,
68 RK_UNKNOWN,
71 /* Region and its subclasses.
73 The class hierarchy looks like this (using indentation to show
74 inheritance, and with region_kinds shown for the concrete subclasses):
76 region
77 space_region
78 frame_region (RK_FRAME): a function frame on the stack
79 globals_region (RK_GLOBALS): holds globals variables (data and bss)
80 code_region (RK_CODE): represents the code segment, containing functions
81 stack_region (RK_STACK): a stack, containing all stack frames
82 heap_region (RK_HEAP): the heap, containing heap_allocated_regions
83 thread_local_region (RK_THREAD_LOCAL): thread-local data for the thread
84 being analyzed
85 root_region (RK_ROOT): the top-level region
86 function_region (RK_FUNCTION): the code for a particular function
87 label_region (RK_LABEL): a particular label within a function
88 symbolic_region (RK_SYMBOLIC): dereferencing a symbolic pointer
89 decl_region (RK_DECL): the memory occupied by a particular global, local,
90 or SSA name
91 field_region (RK_FIELD): the memory occupied by a field within a struct
92 or union
93 element_region (RK_ELEMENT): an element within an array
94 offset_region (RK_OFFSET): a byte-offset within another region, for
95 handling pointer arithmetic as a region
96 sized_region (RK_SIZED): a subregion of symbolic size (in bytes)
97 within its parent
98 cast_region (RK_CAST): a region that views another region using a
99 different type
100 heap_allocated_region (RK_HEAP_ALLOCATED): an untyped region dynamically
101 allocated on the heap via
102 "malloc" or similar
103 alloca_region (RK_ALLOCA): an untyped region dynamically allocated on the
104 stack via "alloca"
105 string_region (RK_STRING): a region for a STRING_CST
106 bit_range_region (RK_BIT_RANGE): a region for a specific range of bits
107 within another region
108 var_arg_region (RK_VAR_ARG): a region for the N-th vararg within a
109 frame_region for a variadic call
110 errno_region (RK_ERRNO): a region for holding "errno"
111 unknown_region (RK_UNKNOWN): for handling unimplemented tree codes. */
113 /* Abstract base class for representing ways of accessing chunks of memory.
115 Regions form a tree-like hierarchy, with a root region at the base,
116 with memory space regions within it, representing the stack and
117 globals, with frames within the stack, and regions for variables
118 within the frames and the "globals" region. Regions for structs
119 can have subregions for fields. */
121 class region
123 public:
124 virtual ~region ();
126 unsigned get_id () const { return m_id; }
127 static int cmp_ids (const region *reg1, const region *reg2);
129 virtual enum region_kind get_kind () const = 0;
130 virtual const frame_region *
131 dyn_cast_frame_region () const { return NULL; }
132 virtual const function_region *
133 dyn_cast_function_region () const { return NULL; }
134 virtual const symbolic_region *
135 dyn_cast_symbolic_region () const { return NULL; }
136 virtual const decl_region *
137 dyn_cast_decl_region () const { return NULL; }
138 virtual const field_region *
139 dyn_cast_field_region () const { return NULL; }
140 virtual const element_region *
141 dyn_cast_element_region () const { return NULL; }
142 virtual const offset_region *
143 dyn_cast_offset_region () const { return NULL; }
144 virtual const sized_region *
145 dyn_cast_sized_region () const { return NULL; }
146 virtual const cast_region *
147 dyn_cast_cast_region () const { return NULL; }
148 virtual const string_region *
149 dyn_cast_string_region () const { return NULL; }
150 virtual const bit_range_region *
151 dyn_cast_bit_range_region () const { return NULL; }
152 virtual const var_arg_region *
153 dyn_cast_var_arg_region () const { return NULL; }
155 virtual void accept (visitor *v) const;
157 const region *get_parent_region () const { return m_parent; }
158 const region *get_base_region () const;
159 bool base_region_p () const;
160 bool descendent_of_p (const region *elder) const;
161 const frame_region *maybe_get_frame_region () const;
162 enum memory_space get_memory_space () const;
163 bool can_have_initial_svalue_p () const;
165 tree maybe_get_decl () const;
167 tree get_type () const { return m_type; }
169 void print (const region_model &model,
170 pretty_printer *pp) const;
171 label_text get_desc (bool simple=true) const;
173 virtual void dump_to_pp (pretty_printer *pp, bool simple) const = 0;
174 void dump (bool simple) const;
176 json::value *to_json () const;
178 bool non_null_p () const;
180 static int cmp_ptr_ptr (const void *, const void *);
182 bool involves_p (const svalue *sval) const;
184 region_offset get_offset (region_model_manager *mgr) const;
186 /* Attempt to get the size of this region as a concrete number of bytes.
187 If successful, return true and write the size to *OUT.
188 Otherwise return false. */
189 virtual bool get_byte_size (byte_size_t *out) const;
191 /* Attempt to get the size of this region as a concrete number of bits.
192 If successful, return true and write the size to *OUT.
193 Otherwise return false. */
194 virtual bool get_bit_size (bit_size_t *out) const;
196 /* Get a symbolic value describing the size of this region in bytes
197 (which could be "unknown"). */
198 virtual const svalue *get_byte_size_sval (region_model_manager *mgr) const;
200 /* Attempt to get the offset in bits of this region relative to its parent.
201 If successful, return true and write to *OUT.
202 Otherwise return false. */
203 virtual bool get_relative_concrete_offset (bit_offset_t *out) const;
205 /* Get the offset in bytes of this region relative to its parent as a svalue.
206 Might return an unknown_svalue. */
207 virtual const svalue *
208 get_relative_symbolic_offset (region_model_manager *mgr) const;
210 /* Attempt to get the position and size of this region expressed as a
211 concrete range of bytes relative to its parent.
212 If successful, return true and write to *OUT.
213 Otherwise return false. */
214 bool get_relative_concrete_byte_range (byte_range *out) const;
216 void
217 get_subregions_for_binding (region_model_manager *mgr,
218 bit_offset_t start_bit_offset,
219 bit_size_t size_in_bits,
220 tree type,
221 auto_vec <const region *> *out) const;
223 bool symbolic_for_unknown_ptr_p () const;
225 bool symbolic_p () const;
227 /* For most base regions it makes sense to track the bindings of the region
228 within the store. As an optimization, some are not tracked (to avoid
229 bloating the store object with redundant binding clusters). */
230 virtual bool tracked_p () const { return true; }
232 const complexity &get_complexity () const { return m_complexity; }
234 bool is_named_decl_p (const char *decl_name) const;
236 protected:
237 region (complexity c, unsigned id, const region *parent, tree type);
239 private:
240 region_offset calc_offset (region_model_manager *mgr) const;
242 complexity m_complexity;
243 unsigned m_id; // purely for deterministic sorting at this stage, for dumps
244 const region *m_parent;
245 tree m_type;
247 mutable region_offset *m_cached_offset;
250 } // namespace ana
252 template <>
253 template <>
254 inline bool
255 is_a_helper <const region *>::test (const region *)
257 return true;
260 namespace ana {
262 /* Abstract subclass of region, for regions that represent an untyped
263 space within memory, such as the stack or the heap. */
265 class space_region : public region
267 protected:
268 space_region (unsigned id, const region *parent)
269 : region (complexity (parent), id, parent, NULL_TREE)
273 /* Concrete space_region subclass, representing a function frame on the stack,
274 to contain the locals.
275 The parent is the stack region; there's also a hierarchy of call-stack
276 prefixes expressed via m_calling_frame.
277 For example, given "oldest" calling "middle" called "newest" we would have
278 - a stack depth of 3
279 - frame (A) for "oldest" with index 0 for depth 1, calling_frame == NULL
280 - frame (B) for "middle" with index 1 for depth 2, calling_frame == (A)
281 - frame (C) for "newest" with index 2 for depth 3, calling_frame == (B)
282 where the parent region for each of the frames is the "stack" region.
283 The index is the count of frames earlier than this in the stack. */
285 class frame_region : public space_region
287 public:
288 /* A support class for uniquifying instances of frame_region. */
289 struct key_t
291 key_t (const frame_region *calling_frame, function *fun)
292 : m_calling_frame (calling_frame), m_fun (fun)
294 /* calling_frame can be NULL. */
295 gcc_assert (fun);
298 hashval_t hash () const
300 inchash::hash hstate;
301 hstate.add_ptr (m_calling_frame);
302 hstate.add_ptr (m_fun);
303 return hstate.end ();
306 bool operator== (const key_t &other) const
308 return (m_calling_frame == other.m_calling_frame && m_fun == other.m_fun);
311 void mark_deleted () { m_fun = reinterpret_cast<function *> (1); }
312 void mark_empty () { m_fun = NULL; }
313 bool is_deleted () const
315 return m_fun == reinterpret_cast<function *> (1);
317 bool is_empty () const { return m_fun == NULL; }
319 const frame_region *m_calling_frame;
320 function *m_fun;
323 frame_region (unsigned id, const region *parent,
324 const frame_region *calling_frame,
325 function *fun, int index)
326 : space_region (id, parent), m_calling_frame (calling_frame),
327 m_fun (fun), m_index (index)
329 ~frame_region ();
331 /* region vfuncs. */
332 enum region_kind get_kind () const final override { return RK_FRAME; }
333 const frame_region * dyn_cast_frame_region () const final override
335 return this;
337 void accept (visitor *v) const final override;
338 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
340 /* Accessors. */
341 const frame_region *get_calling_frame () const { return m_calling_frame; }
342 function *get_function () const { return m_fun; }
343 tree get_fndecl () const { return get_function ()->decl; }
344 int get_index () const { return m_index; }
345 int get_stack_depth () const { return m_index + 1; }
347 const decl_region *
348 get_region_for_local (region_model_manager *mgr,
349 tree expr,
350 const region_model_context *ctxt) const;
352 unsigned get_num_locals () const { return m_locals.elements (); }
354 /* Implemented in region-model-manager.cc. */
355 void dump_untracked_regions () const;
357 private:
358 const frame_region *m_calling_frame;
359 function *m_fun;
360 int m_index;
362 /* The regions for the decls within this frame are managed by this
363 object, rather than the region_model_manager, to make it a simple
364 lookup by tree. */
365 typedef hash_map<tree, decl_region *> map_t;
366 map_t m_locals;
369 } // namespace ana
371 template <>
372 template <>
373 inline bool
374 is_a_helper <const frame_region *>::test (const region *reg)
376 return reg->get_kind () == RK_FRAME;
379 template <> struct default_hash_traits<frame_region::key_t>
380 : public member_function_hash_traits<frame_region::key_t>
382 static const bool empty_zero_p = true;
385 namespace ana {
387 /* Concrete space_region subclass, to hold global variables (data and bss). */
389 class globals_region : public space_region
391 public:
392 globals_region (unsigned id, const region *parent)
393 : space_region (id, parent)
396 /* region vfuncs. */
397 enum region_kind get_kind () const final override { return RK_GLOBALS; }
398 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
401 } // namespace ana
403 template <>
404 template <>
405 inline bool
406 is_a_helper <const globals_region *>::test (const region *reg)
408 return reg->get_kind () == RK_GLOBALS;
411 namespace ana {
413 /* Concrete space_region subclass, representing the code segment
414 containing functions. */
416 class code_region : public space_region
418 public:
419 code_region (unsigned id, const region *parent)
420 : space_region (id, parent)
423 /* region vfuncs. */
424 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
425 enum region_kind get_kind () const final override { return RK_CODE; }
428 } // namespace ana
430 template <>
431 template <>
432 inline bool
433 is_a_helper <const code_region *>::test (const region *reg)
435 return reg->get_kind () == RK_CODE;
438 namespace ana {
440 /* Concrete region subclass. A region representing the code for
441 a particular function. */
443 class function_region : public region
445 public:
446 function_region (unsigned id, const code_region *parent, tree fndecl)
447 : region (complexity (parent), id, parent, TREE_TYPE (fndecl)),
448 m_fndecl (fndecl)
450 gcc_assert (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (fndecl)));
453 /* region vfuncs. */
454 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
455 enum region_kind get_kind () const final override { return RK_FUNCTION; }
456 const function_region *
457 dyn_cast_function_region () const final override{ return this; }
459 tree get_fndecl () const { return m_fndecl; }
461 private:
462 tree m_fndecl;
465 } // namespace ana
467 template <>
468 template <>
469 inline bool
470 is_a_helper <const function_region *>::test (const region *reg)
472 return reg->get_kind () == RK_FUNCTION;
475 namespace ana {
477 /* Concrete region subclass. A region representing a particular label
478 within a function. */
480 class label_region : public region
482 public:
483 label_region (unsigned id, const function_region *parent, tree label)
484 : region (complexity (parent), id, parent, NULL_TREE), m_label (label)
486 gcc_assert (TREE_CODE (label) == LABEL_DECL);
489 /* region vfuncs. */
490 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
491 enum region_kind get_kind () const final override { return RK_LABEL; }
493 tree get_label () const { return m_label; }
495 private:
496 tree m_label;
499 } // namespace ana
501 template <>
502 template <>
503 inline bool
504 is_a_helper <const label_region *>::test (const region *reg)
506 return reg->get_kind () == RK_LABEL;
509 namespace ana {
511 /* Concrete space_region subclass representing a stack, containing all stack
512 frames. */
514 class stack_region : public space_region
516 public:
517 stack_region (unsigned id, region *parent)
518 : space_region (id, parent)
521 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
523 enum region_kind get_kind () const final override { return RK_STACK; }
526 } // namespace ana
528 template <>
529 template <>
530 inline bool
531 is_a_helper <const stack_region *>::test (const region *reg)
533 return reg->get_kind () == RK_STACK;
536 namespace ana {
538 /* Concrete space_region subclass: a region within which regions can be
539 dynamically allocated. */
541 class heap_region : public space_region
543 public:
544 heap_region (unsigned id, region *parent)
545 : space_region (id, parent)
548 enum region_kind get_kind () const final override { return RK_HEAP; }
549 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
552 } // namespace ana
554 template <>
555 template <>
556 inline bool
557 is_a_helper <const heap_region *>::test (const region *reg)
559 return reg->get_kind () == RK_HEAP;
562 namespace ana {
564 /* Concrete space_region subclass: thread-local data for the thread
565 being analyzed. */
567 class thread_local_region : public space_region
569 public:
570 thread_local_region (unsigned id, region *parent)
571 : space_region (id, parent)
574 enum region_kind get_kind () const final override { return RK_THREAD_LOCAL; }
575 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
578 } // namespace ana
580 template <>
581 template <>
582 inline bool
583 is_a_helper <const thread_local_region *>::test (const region *reg)
585 return reg->get_kind () == RK_THREAD_LOCAL;
588 namespace ana {
590 /* Concrete region subclass. The root region, containing all regions
591 (either directly, or as descendents).
592 Unique within a region_model_manager. */
594 class root_region : public region
596 public:
597 root_region (unsigned id);
599 enum region_kind get_kind () const final override { return RK_ROOT; }
600 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
603 } // namespace ana
605 template <>
606 template <>
607 inline bool
608 is_a_helper <const root_region *>::test (const region *reg)
610 return reg->get_kind () == RK_ROOT;
613 namespace ana {
615 /* Concrete region subclass: a region to use when dereferencing an unknown
616 pointer. */
618 class symbolic_region : public region
620 public:
621 /* A support class for uniquifying instances of symbolic_region. */
622 struct key_t
624 key_t (const region *parent, const svalue *sval_ptr)
625 : m_parent (parent), m_sval_ptr (sval_ptr)
627 gcc_assert (sval_ptr);
630 hashval_t hash () const
632 inchash::hash hstate;
633 hstate.add_ptr (m_parent);
634 hstate.add_ptr (m_sval_ptr);
635 return hstate.end ();
638 bool operator== (const key_t &other) const
640 return (m_parent == other.m_parent && m_sval_ptr == other.m_sval_ptr);
643 void mark_deleted () { m_sval_ptr = reinterpret_cast<const svalue *> (1); }
644 void mark_empty () { m_sval_ptr = NULL; }
645 bool is_deleted () const
647 return m_sval_ptr == reinterpret_cast<const svalue *> (1);
649 bool is_empty () const { return m_sval_ptr == NULL; }
651 const region *m_parent;
652 const svalue *m_sval_ptr;
655 symbolic_region (unsigned id, region *parent, const svalue *sval_ptr);
657 const symbolic_region *
658 dyn_cast_symbolic_region () const final override { return this; }
660 enum region_kind get_kind () const final override { return RK_SYMBOLIC; }
661 void accept (visitor *v) const final override;
662 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
664 const svalue *get_pointer () const { return m_sval_ptr; }
666 private:
667 const svalue *m_sval_ptr;
670 } // namespace ana
672 template <>
673 template <>
674 inline bool
675 is_a_helper <const symbolic_region *>::test (const region *reg)
677 return reg->get_kind () == RK_SYMBOLIC;
680 template <> struct default_hash_traits<symbolic_region::key_t>
681 : public member_function_hash_traits<symbolic_region::key_t>
683 static const bool empty_zero_p = true;
686 namespace ana {
688 /* Concrete region subclass representing the memory occupied by a
689 variable (whether for a global or a local).
690 Also used for representing SSA names, as if they were locals. */
692 class decl_region : public region
694 public:
695 decl_region (unsigned id, const region *parent, tree decl)
696 : region (complexity (parent), id, parent, TREE_TYPE (decl)), m_decl (decl),
697 m_tracked (calc_tracked_p (decl))
700 enum region_kind get_kind () const final override { return RK_DECL; }
701 const decl_region *
702 dyn_cast_decl_region () const final override { return this; }
704 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
706 bool tracked_p () const final override { return m_tracked; }
708 tree get_decl () const { return m_decl; }
709 int get_stack_depth () const;
711 const svalue *maybe_get_constant_value (region_model_manager *mgr) const;
712 const svalue *get_svalue_for_constructor (tree ctor,
713 region_model_manager *mgr) const;
714 const svalue *get_svalue_for_initializer (region_model_manager *mgr) const;
716 private:
717 static bool calc_tracked_p (tree decl);
719 tree m_decl;
721 /* Cached result of calc_tracked_p, so that we can quickly determine when
722 we don't to track a binding_cluster for this decl (to avoid bloating
723 store objects).
724 This can be debugged using -fdump-analyzer-untracked. */
725 bool m_tracked;
728 } // namespace ana
730 template <>
731 template <>
732 inline bool
733 is_a_helper <const decl_region *>::test (const region *reg)
735 return reg->get_kind () == RK_DECL;
738 namespace ana {
740 /* Concrete region subclass representing the memory occupied by a
741 field within a struct or union. */
743 class field_region : public region
745 public:
746 /* A support class for uniquifying instances of field_region. */
747 struct key_t
749 key_t (const region *parent, tree field)
750 : m_parent (parent), m_field (field)
752 gcc_assert (field);
755 hashval_t hash () const
757 inchash::hash hstate;
758 hstate.add_ptr (m_parent);
759 hstate.add_ptr (m_field);
760 return hstate.end ();
763 bool operator== (const key_t &other) const
765 return (m_parent == other.m_parent && m_field == other.m_field);
768 void mark_deleted () { m_field = reinterpret_cast<tree> (1); }
769 void mark_empty () { m_field = NULL_TREE; }
770 bool is_deleted () const { return m_field == reinterpret_cast<tree> (1); }
771 bool is_empty () const { return m_field == NULL_TREE; }
773 const region *m_parent;
774 tree m_field;
777 field_region (unsigned id, const region *parent, tree field)
778 : region (complexity (parent), id, parent, TREE_TYPE (field)),
779 m_field (field)
782 enum region_kind get_kind () const final override { return RK_FIELD; }
784 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
785 const field_region *
786 dyn_cast_field_region () const final override { return this; }
788 tree get_field () const { return m_field; }
790 bool get_relative_concrete_offset (bit_offset_t *out) const final override;
791 const svalue *get_relative_symbolic_offset (region_model_manager *mgr)
792 const final override;
794 private:
795 tree m_field;
798 } // namespace ana
800 template <>
801 template <>
802 inline bool
803 is_a_helper <const field_region *>::test (const region *reg)
805 return reg->get_kind () == RK_FIELD;
808 template <> struct default_hash_traits<field_region::key_t>
809 : public member_function_hash_traits<field_region::key_t>
811 static const bool empty_zero_p = true;
814 namespace ana {
816 /* An element within an array. */
818 class element_region : public region
820 public:
821 /* A support class for uniquifying instances of element_region. */
822 struct key_t
824 key_t (const region *parent, tree element_type, const svalue *index)
825 : m_parent (parent), m_element_type (element_type), m_index (index)
827 gcc_assert (index);
830 hashval_t hash () const
832 inchash::hash hstate;
833 hstate.add_ptr (m_parent);
834 hstate.add_ptr (m_element_type);
835 hstate.add_ptr (m_index);
836 return hstate.end ();
839 bool operator== (const key_t &other) const
841 return (m_parent == other.m_parent
842 && m_element_type == other.m_element_type
843 && m_index == other.m_index);
846 void mark_deleted () { m_index = reinterpret_cast<const svalue *> (1); }
847 void mark_empty () { m_index = NULL; }
848 bool is_deleted () const
850 return m_index == reinterpret_cast<const svalue *> (1);
852 bool is_empty () const { return m_index == NULL; }
854 const region *m_parent;
855 tree m_element_type;
856 const svalue *m_index;
859 element_region (unsigned id, const region *parent, tree element_type,
860 const svalue *index)
861 : region (complexity::from_pair (parent, index), id, parent, element_type),
862 m_index (index)
865 enum region_kind get_kind () const final override { return RK_ELEMENT; }
866 const element_region *
867 dyn_cast_element_region () const final override { return this; }
869 void accept (visitor *v) const final override;
871 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
873 const svalue *get_index () const { return m_index; }
875 virtual bool
876 get_relative_concrete_offset (bit_offset_t *out) const final override;
877 const svalue *get_relative_symbolic_offset (region_model_manager *mgr)
878 const final override;
880 private:
881 const svalue *m_index;
884 } // namespace ana
886 template <>
887 template <>
888 inline bool
889 is_a_helper <const element_region *>::test (const region *reg)
891 return reg->get_kind () == RK_ELEMENT;
894 template <> struct default_hash_traits<element_region::key_t>
895 : public member_function_hash_traits<element_region::key_t>
897 static const bool empty_zero_p = true;
900 namespace ana {
902 /* A byte-offset within another region, for handling pointer arithmetic
903 as a region. */
905 class offset_region : public region
907 public:
908 /* A support class for uniquifying instances of offset_region. */
909 struct key_t
911 key_t (const region *parent, tree element_type, const svalue *byte_offset)
912 : m_parent (parent), m_element_type (element_type), m_byte_offset (byte_offset)
914 gcc_assert (byte_offset);
917 hashval_t hash () const
919 inchash::hash hstate;
920 hstate.add_ptr (m_parent);
921 hstate.add_ptr (m_element_type);
922 hstate.add_ptr (m_byte_offset);
923 return hstate.end ();
926 bool operator== (const key_t &other) const
928 return (m_parent == other.m_parent
929 && m_element_type == other.m_element_type
930 && m_byte_offset == other.m_byte_offset);
933 void mark_deleted () { m_byte_offset = reinterpret_cast<const svalue *> (1); }
934 void mark_empty () { m_byte_offset = NULL; }
935 bool is_deleted () const
937 return m_byte_offset == reinterpret_cast<const svalue *> (1);
939 bool is_empty () const { return m_byte_offset == NULL; }
941 const region *m_parent;
942 tree m_element_type;
943 const svalue *m_byte_offset;
946 offset_region (unsigned id, const region *parent, tree type,
947 const svalue *byte_offset)
948 : region (complexity::from_pair (parent, byte_offset), id, parent, type),
949 m_byte_offset (byte_offset)
952 enum region_kind get_kind () const final override { return RK_OFFSET; }
953 const offset_region *
954 dyn_cast_offset_region () const final override { return this; }
956 void accept (visitor *v) const final override;
958 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
960 const svalue *get_byte_offset () const { return m_byte_offset; }
962 bool get_relative_concrete_offset (bit_offset_t *out) const final override;
963 const svalue *get_relative_symbolic_offset (region_model_manager *mgr)
964 const final override;
965 const svalue * get_byte_size_sval (region_model_manager *mgr)
966 const final override;
969 private:
970 const svalue *m_byte_offset;
973 } // namespace ana
975 template <>
976 template <>
977 inline bool
978 is_a_helper <const offset_region *>::test (const region *reg)
980 return reg->get_kind () == RK_OFFSET;
983 template <> struct default_hash_traits<offset_region::key_t>
984 : public member_function_hash_traits<offset_region::key_t>
986 static const bool empty_zero_p = true;
989 namespace ana {
991 /* A region that is size BYTES_SIZE_SVAL in size within its parent
992 region (or possibly larger, which would lead to an overflow. */
994 class sized_region : public region
996 public:
997 /* A support class for uniquifying instances of sized_region. */
998 struct key_t
1000 key_t (const region *parent, tree element_type,
1001 const svalue *byte_size_sval)
1002 : m_parent (parent), m_element_type (element_type),
1003 m_byte_size_sval (byte_size_sval)
1005 gcc_assert (byte_size_sval);
1008 hashval_t hash () const
1010 inchash::hash hstate;
1011 hstate.add_ptr (m_parent);
1012 hstate.add_ptr (m_element_type);
1013 hstate.add_ptr (m_byte_size_sval);
1014 return hstate.end ();
1017 bool operator== (const key_t &other) const
1019 return (m_parent == other.m_parent
1020 && m_element_type == other.m_element_type
1021 && m_byte_size_sval == other.m_byte_size_sval);
1024 void mark_deleted () { m_byte_size_sval = reinterpret_cast<const svalue *> (1); }
1025 void mark_empty () { m_byte_size_sval = NULL; }
1026 bool is_deleted () const
1028 return m_byte_size_sval == reinterpret_cast<const svalue *> (1);
1030 bool is_empty () const { return m_byte_size_sval == NULL; }
1032 const region *m_parent;
1033 tree m_element_type;
1034 const svalue *m_byte_size_sval;
1035 const svalue *m_end_offset;
1038 sized_region (unsigned id, const region *parent, tree type,
1039 const svalue *byte_size_sval)
1040 : region (complexity::from_pair (parent, byte_size_sval),
1041 id, parent, type),
1042 m_byte_size_sval (byte_size_sval)
1045 enum region_kind get_kind () const final override { return RK_SIZED; }
1046 const sized_region *
1047 dyn_cast_sized_region () const final override { return this; }
1049 void accept (visitor *v) const final override;
1051 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1053 bool get_byte_size (byte_size_t *out) const final override;
1054 bool get_bit_size (bit_size_t *out) const final override;
1056 const svalue *
1057 get_byte_size_sval (region_model_manager *) const final override
1059 return m_byte_size_sval;
1062 private:
1063 const svalue *m_byte_size_sval;
1066 } // namespace ana
1068 template <>
1069 template <>
1070 inline bool
1071 is_a_helper <const sized_region *>::test (const region *reg)
1073 return reg->get_kind () == RK_SIZED;
1076 template <> struct default_hash_traits<sized_region::key_t>
1077 : public member_function_hash_traits<sized_region::key_t>
1079 static const bool empty_zero_p = true;
1082 namespace ana {
1084 /* A region that views another region using a different type. */
1086 class cast_region : public region
1088 public:
1089 /* A support class for uniquifying instances of cast_region. */
1090 struct key_t
1092 key_t (const region *original_region, tree type)
1093 : m_original_region (original_region), m_type (type)
1095 gcc_assert (type);
1098 hashval_t hash () const
1100 inchash::hash hstate;
1101 hstate.add_ptr (m_original_region);
1102 hstate.add_ptr (m_type);
1103 return hstate.end ();
1106 bool operator== (const key_t &other) const
1108 return (m_original_region == other.m_original_region
1109 && m_type == other.m_type);
1112 void mark_deleted () { m_type = reinterpret_cast<tree> (1); }
1113 void mark_empty () { m_type = NULL_TREE; }
1114 bool is_deleted () const { return m_type == reinterpret_cast<tree> (1); }
1115 bool is_empty () const { return m_type == NULL_TREE; }
1117 const region *m_original_region;
1118 tree m_type;
1121 cast_region (unsigned id, const region *original_region, tree type)
1122 : region (complexity (original_region), id,
1123 original_region->get_parent_region (), type),
1124 m_original_region (original_region)
1127 enum region_kind get_kind () const final override { return RK_CAST; }
1128 const cast_region *
1129 dyn_cast_cast_region () const final override { return this; }
1130 void accept (visitor *v) const final override;
1131 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1133 bool get_relative_concrete_offset (bit_offset_t *out) const final override;
1135 const region *get_original_region () const { return m_original_region; }
1137 private:
1138 const region *m_original_region;
1141 } // namespace ana
1143 template <>
1144 template <>
1145 inline bool
1146 is_a_helper <const cast_region *>::test (const region *reg)
1148 return reg->get_kind () == RK_CAST;
1151 template <> struct default_hash_traits<cast_region::key_t>
1152 : public member_function_hash_traits<cast_region::key_t>
1154 static const bool empty_zero_p = true;
1157 namespace ana {
1159 /* An untyped region dynamically allocated on the heap via "malloc"
1160 or similar. */
1162 class heap_allocated_region : public region
1164 public:
1165 heap_allocated_region (unsigned id, const region *parent)
1166 : region (complexity (parent), id, parent, NULL_TREE)
1169 enum region_kind
1170 get_kind () const final override { return RK_HEAP_ALLOCATED; }
1172 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1175 /* An untyped region dynamically allocated on the stack via "alloca". */
1177 class alloca_region : public region
1179 public:
1180 alloca_region (unsigned id, const frame_region *parent)
1181 : region (complexity (parent), id, parent, NULL_TREE)
1184 enum region_kind get_kind () const final override { return RK_ALLOCA; }
1186 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1189 /* A region for a STRING_CST. */
1191 class string_region : public region
1193 public:
1194 string_region (unsigned id, const region *parent, tree string_cst)
1195 : region (complexity (parent), id, parent, TREE_TYPE (string_cst)),
1196 m_string_cst (string_cst)
1199 const string_region *
1200 dyn_cast_string_region () const final override { return this; }
1202 enum region_kind get_kind () const final override { return RK_STRING; }
1204 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1206 /* We assume string literals are immutable, so we don't track them in
1207 the store. */
1208 bool tracked_p () const final override { return false; }
1210 tree get_string_cst () const { return m_string_cst; }
1212 private:
1213 tree m_string_cst;
1216 } // namespace ana
1218 template <>
1219 template <>
1220 inline bool
1221 is_a_helper <const string_region *>::test (const region *reg)
1223 return reg->get_kind () == RK_STRING;
1226 namespace ana {
1228 /* A region for a specific range of bits within another region. */
1230 class bit_range_region : public region
1232 public:
1233 /* A support class for uniquifying instances of bit_range_region. */
1234 struct key_t
1236 key_t (const region *parent, tree type, const bit_range &bits)
1237 : m_parent (parent), m_type (type), m_bits (bits)
1239 gcc_assert (parent);
1242 hashval_t hash () const
1244 inchash::hash hstate;
1245 hstate.add_ptr (m_parent);
1246 hstate.add_ptr (m_type);
1247 hstate.add_wide_int (m_bits.m_start_bit_offset);
1248 hstate.add_wide_int (m_bits.m_size_in_bits);
1249 return hstate.end ();
1252 bool operator== (const key_t &other) const
1254 return (m_parent == other.m_parent
1255 && m_type == other.m_type
1256 && m_bits == other.m_bits);
1259 void mark_deleted () { m_parent = reinterpret_cast<const region *> (1); }
1260 void mark_empty () { m_parent = NULL; }
1261 bool is_deleted () const
1263 return m_parent == reinterpret_cast<const region *> (1);
1265 bool is_empty () const { return m_parent == NULL; }
1267 const region *m_parent;
1268 tree m_type;
1269 bit_range m_bits;
1272 bit_range_region (unsigned id, const region *parent, tree type,
1273 const bit_range &bits)
1274 : region (complexity (parent), id, parent, type),
1275 m_bits (bits)
1278 const bit_range_region *
1279 dyn_cast_bit_range_region () const final override { return this; }
1281 enum region_kind get_kind () const final override { return RK_BIT_RANGE; }
1283 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1285 const bit_range &get_bits () const { return m_bits; }
1287 bool get_byte_size (byte_size_t *out) const final override;
1288 bool get_bit_size (bit_size_t *out) const final override;
1289 const svalue *get_byte_size_sval (region_model_manager *mgr) const final override;
1290 bool get_relative_concrete_offset (bit_offset_t *out) const final override;
1291 const svalue *get_relative_symbolic_offset (region_model_manager *mgr)
1292 const final override;
1294 private:
1295 bit_range m_bits;
1298 } // namespace ana
1300 template <>
1301 template <>
1302 inline bool
1303 is_a_helper <const bit_range_region *>::test (const region *reg)
1305 return reg->get_kind () == RK_BIT_RANGE;
1308 template <> struct default_hash_traits<bit_range_region::key_t>
1309 : public member_function_hash_traits<bit_range_region::key_t>
1311 static const bool empty_zero_p = true;
1314 namespace ana {
1316 /* A region for the N-th vararg within a frame_region for a variadic call. */
1318 class var_arg_region : public region
1320 public:
1321 /* A support class for uniquifying instances of var_arg_region. */
1322 struct key_t
1324 key_t (const frame_region *parent, unsigned idx)
1325 : m_parent (parent), m_idx (idx)
1327 gcc_assert (parent);
1330 hashval_t hash () const
1332 inchash::hash hstate;
1333 hstate.add_ptr (m_parent);
1334 hstate.add_int (m_idx);
1335 return hstate.end ();
1338 bool operator== (const key_t &other) const
1340 return (m_parent == other.m_parent
1341 && m_idx == other.m_idx);
1344 void mark_deleted ()
1346 m_parent = reinterpret_cast<const frame_region *> (1);
1348 void mark_empty () { m_parent = NULL; }
1349 bool is_deleted () const
1351 return m_parent == reinterpret_cast<const frame_region *> (1);
1353 bool is_empty () const { return m_parent == NULL; }
1355 const frame_region *m_parent;
1356 unsigned m_idx;
1359 var_arg_region (unsigned id, const frame_region *parent,
1360 unsigned idx)
1361 : region (complexity (parent), id, parent, NULL_TREE),
1362 m_idx (idx)
1365 const var_arg_region *
1366 dyn_cast_var_arg_region () const final override { return this; }
1368 enum region_kind get_kind () const final override { return RK_VAR_ARG; }
1370 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1372 const frame_region *get_frame_region () const;
1373 unsigned get_index () const { return m_idx; }
1375 private:
1376 unsigned m_idx;
1379 } // namespace ana
1381 template <>
1382 template <>
1383 inline bool
1384 is_a_helper <const var_arg_region *>::test (const region *reg)
1386 return reg->get_kind () == RK_VAR_ARG;
1389 template <> struct default_hash_traits<var_arg_region::key_t>
1390 : public member_function_hash_traits<var_arg_region::key_t>
1392 static const bool empty_zero_p = true;
1395 namespace ana {
1397 /* A region for errno for the current thread. */
1399 class errno_region : public region
1401 public:
1402 errno_region (unsigned id, const thread_local_region *parent)
1403 : region (complexity (parent), id, parent, integer_type_node)
1406 enum region_kind get_kind () const final override { return RK_ERRNO; }
1408 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1411 } // namespace ana
1413 template <>
1414 template <>
1415 inline bool
1416 is_a_helper <const errno_region *>::test (const region *reg)
1418 return reg->get_kind () == RK_ERRNO;
1421 namespace ana {
1423 /* An unknown region, for handling unimplemented tree codes. */
1425 class unknown_region : public region
1427 public:
1428 unknown_region (unsigned id, const region *parent, tree type)
1429 : region (complexity (parent), id, parent, type)
1432 enum region_kind get_kind () const final override { return RK_UNKNOWN; }
1434 void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1437 } // namespace ana
1439 #endif /* GCC_ANALYZER_REGION_H */