d: Add testcase from PR108962
[official-gcc.git] / gcc / analyzer / kf-lang-cp.cc
blob393b4f25e79336d25fc65e23a3a15578e64cb1fa
1 /* Handling for the known behavior of various functions specific to C++.
2 Copyright (C) 2020-2023 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 #include "config.h"
22 #define INCLUDE_MEMORY
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 #include "function.h"
27 #include "basic-block.h"
28 #include "gimple.h"
29 #include "analyzer/analyzer.h"
30 #include "analyzer/analyzer-logging.h"
31 #include "diagnostic.h"
32 #include "analyzer/region-model.h"
33 #include "analyzer/call-details.h"
34 #include "make-unique.h"
36 #if ENABLE_ANALYZER
38 namespace ana {
40 /* Implementations of specific functions. */
42 /* Handler for "operator new" and "operator new []". */
44 class kf_operator_new : public known_function
46 public:
47 bool matches_call_types_p (const call_details &cd) const final override
49 return cd.num_args () == 1;
52 void impl_call_pre (const call_details &cd) const final override
54 region_model *model = cd.get_model ();
55 region_model_manager *mgr = cd.get_manager ();
56 const svalue *size_sval = cd.get_arg_svalue (0);
57 const region *new_reg
58 = model->get_or_create_region_for_heap_alloc (size_sval, cd.get_ctxt ());
59 if (cd.get_lhs_type ())
61 const svalue *ptr_sval
62 = mgr->get_ptr_svalue (cd.get_lhs_type (), new_reg);
63 cd.maybe_set_lhs (ptr_sval);
68 /* Handler for "operator delete", both the sized and unsized variants
69 (2 arguments and 1 argument respectively), and for "operator delete []" */
71 class kf_operator_delete : public known_function
73 public:
74 kf_operator_delete (unsigned num_args) : m_num_args (num_args) {}
76 bool matches_call_types_p (const call_details &cd) const final override
78 return cd.num_args () == m_num_args;
81 void impl_call_post (const call_details &cd) const final override
83 region_model *model = cd.get_model ();
84 const svalue *ptr_sval = cd.get_arg_svalue (0);
85 if (const region *freed_reg = ptr_sval->maybe_get_region ())
87 /* If the ptr points to an underlying heap region, delete it,
88 poisoning pointers. */
89 model->unbind_region_and_descendents (freed_reg, POISON_KIND_FREED);
93 private:
94 unsigned m_num_args;
97 /* Populate KFM with instances of known functions relating to C++. */
99 void
100 register_known_functions_lang_cp (known_function_manager &kfm)
102 kfm.add ("operator new", make_unique<kf_operator_new> ());
103 kfm.add ("operator new []", make_unique<kf_operator_new> ());
104 kfm.add ("operator delete", make_unique<kf_operator_delete> (1));
105 kfm.add ("operator delete", make_unique<kf_operator_delete> (2));
106 kfm.add ("operator delete []", make_unique<kf_operator_delete> (1));
109 } // namespace ana
111 #endif /* #if ENABLE_ANALYZER */