xref: /freebsd/contrib/llvm-project/clang/include/clang/StaticAnalyzer/Core/PathSensitive/APSIntPtr.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //== APSIntPtr.h - Wrapper for APSInt objects owned separately -*- C++ -*--==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_APSIntPtr_H
10 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_APSIntPtr_H
11 
12 #include "llvm/ADT/APSInt.h"
13 #include "llvm/Support/Compiler.h"
14 
15 namespace clang::ento {
16 
17 /// A safe wrapper around APSInt objects allocated and owned by
18 /// \c BasicValueFactory. This just wraps a common llvm::APSInt.
19 class APSIntPtr {
20   using APSInt = llvm::APSInt;
21 
22 public:
23   APSIntPtr() = delete;
24   APSIntPtr(const APSIntPtr &) = default;
25   APSIntPtr &operator=(const APSIntPtr &) & = default;
26   ~APSIntPtr() = default;
27 
28   /// You should not use this API.
29   /// If do, ensure that the \p Ptr not going to dangle.
30   /// Prefer using \c BasicValueFactory::getValue() to get an APSIntPtr object.
unsafeConstructor(const APSInt * Ptr)31   static APSIntPtr unsafeConstructor(const APSInt *Ptr) {
32     return APSIntPtr(Ptr);
33   }
34 
35   LLVM_ATTRIBUTE_RETURNS_NONNULL
get()36   const APSInt *get() const { return Ptr; }
37   /*implicit*/ operator const APSInt &() const { return *get(); }
38 
39   APSInt operator-() const { return -*Ptr; }
40   APSInt operator~() const { return ~*Ptr; }
41 
42 #define DEFINE_OPERATOR(OP)                                                    \
43   bool operator OP(APSIntPtr Other) const { return (*Ptr)OP(*Other.Ptr); }
44   DEFINE_OPERATOR(>)
45   DEFINE_OPERATOR(>=)
46   DEFINE_OPERATOR(<)
47   DEFINE_OPERATOR(<=)
48   DEFINE_OPERATOR(==)
49   DEFINE_OPERATOR(!=)
50 #undef DEFINE_OPERATOR
51 
52   const APSInt &operator*() const { return *Ptr; }
53   const APSInt *operator->() const { return Ptr; }
54 
55 private:
APSIntPtr(const APSInt * Ptr)56   explicit APSIntPtr(const APSInt *Ptr) : Ptr(Ptr) {}
57 
58   /// Owned by \c BasicValueFactory.
59   const APSInt *Ptr;
60 };
61 
62 } // namespace clang::ento
63 
64 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_APSIntPtr_H
65