forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[analyzer][NFC] Introduce APSIntPtr, a safe wrapper of APSInt (1/4) (l…
…lvm#120435) One could create dangling APSInt references in various ways in the past, that were sometimes assumed to be persisted in the BasicValueFactor. One should always use BasicValueFactory to create persistent APSInts, that could be used by ConcreteInts or SymIntExprs and similar long-living objects. If one used a temporary or local variables for this, these would dangle. To enforce the contract of the analyzer BasicValueFactory and the uses of APSInts, let's have a dedicated strong-type for this. The idea is that APSIntPtr is always owned by the BasicValueFactory, and that is the only component that can construct it. These PRs are all NFC - besides fixing dangling APSInt references.
- Loading branch information
Showing
9 changed files
with
148 additions
and
90 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
clang/include/clang/StaticAnalyzer/Core/PathSensitive/APSIntPtr.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//== APSIntPtr.h - Wrapper for APSInt objects owned separately -*- C++ -*--==// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_APSIntPtr_H | ||
#define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_APSIntPtr_H | ||
|
||
#include "llvm/ADT/APSInt.h" | ||
#include "llvm/Support/Compiler.h" | ||
|
||
namespace clang::ento { | ||
|
||
/// A safe wrapper around APSInt objects allocated and owned by | ||
/// \c BasicValueFactory. This just wraps a common llvm::APSInt. | ||
class APSIntPtr { | ||
using APSInt = llvm::APSInt; | ||
|
||
public: | ||
APSIntPtr() = delete; | ||
APSIntPtr(const APSIntPtr &) = default; | ||
APSIntPtr &operator=(const APSIntPtr &) & = default; | ||
~APSIntPtr() = default; | ||
|
||
/// You should not use this API. | ||
/// If do, ensure that the \p Ptr not going to dangle. | ||
/// Prefer using \c BasicValueFactory::getValue() to get an APSIntPtr object. | ||
static APSIntPtr unsafeConstructor(const APSInt *Ptr) { | ||
return APSIntPtr(Ptr); | ||
} | ||
|
||
LLVM_ATTRIBUTE_RETURNS_NONNULL | ||
const APSInt *get() const { return Ptr; } | ||
/*implicit*/ operator const APSInt &() const { return *get(); } | ||
|
||
APSInt operator-() const { return -*Ptr; } | ||
APSInt operator~() const { return ~*Ptr; } | ||
|
||
#define DEFINE_OPERATOR(OP) \ | ||
bool operator OP(APSIntPtr Other) const { return (*Ptr)OP(*Other.Ptr); } | ||
DEFINE_OPERATOR(>) | ||
DEFINE_OPERATOR(>=) | ||
DEFINE_OPERATOR(<) | ||
DEFINE_OPERATOR(<=) | ||
DEFINE_OPERATOR(==) | ||
DEFINE_OPERATOR(!=) | ||
#undef DEFINE_OPERATOR | ||
|
||
const APSInt &operator*() const { return *Ptr; } | ||
const APSInt *operator->() const { return Ptr; } | ||
|
||
private: | ||
explicit APSIntPtr(const APSInt *Ptr) : Ptr(Ptr) {} | ||
|
||
/// Owned by \c BasicValueFactory. | ||
const APSInt *Ptr; | ||
}; | ||
|
||
} // namespace clang::ento | ||
|
||
#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_APSIntPtr_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.