aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
diff options
context:
space:
mode:
authorZiqing Luo <ziqing@udel.edu>2025-03-25 16:45:44 -0700
committerGitHub <noreply@github.com>2025-03-25 16:45:44 -0700
commit584b24cd6de5fd8bcfefa0b4a57ddbbf58c14af1 (patch)
tree30ffdc4b3b483c1790d2eebe4420dde146d31f83 /clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
parentaf267993a7e102e710d4c29a5253738038e1d2a4 (diff)
downloadllvm-584b24cd6de5fd8bcfefa0b4a57ddbbf58c14af1.zip
llvm-584b24cd6de5fd8bcfefa0b4a57ddbbf58c14af1.tar.gz
llvm-584b24cd6de5fd8bcfefa0b4a57ddbbf58c14af1.tar.bz2
[NFC][StaticAnalyzer] Rename `NotNullConstraint` & `NotNullBufferConstraint` (#131374)
`NotNullConstraint` is used to check both null and non-null of a pointer. So the name, which was created originally for just checking non-nullness, becomes less suitable. The same reason applies to `NotNullBufferConstraint`. This commit renames them. In addition, messages of the assertions in `describe` and ` describeArgumentValue` are updated to indicate that these two functions can be called on any constraint though they were partially implemented for `NotNullConstraint` & `NotNullBufferConstraint`.
Diffstat (limited to 'clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp')
-rw-r--r--clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp59
1 files changed, 31 insertions, 28 deletions
diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
index fef19b4..b9f743c 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
@@ -367,13 +367,13 @@ class StdLibraryFunctionsChecker
};
/// Check null or non-null-ness of an argument that is of pointer type.
- class NotNullConstraint : public ValueConstraint {
+ class NullnessConstraint : public ValueConstraint {
using ValueConstraint::ValueConstraint;
// This variable has a role when we negate the constraint.
bool CannotBeNull = true;
public:
- NotNullConstraint(ArgNo ArgN, bool CannotBeNull = true)
+ NullnessConstraint(ArgNo ArgN, bool CannotBeNull = true)
: ValueConstraint(ArgN), CannotBeNull(CannotBeNull) {}
ProgramStateRef apply(ProgramStateRef State, const CallEvent &Call,
@@ -389,9 +389,9 @@ class StdLibraryFunctionsChecker
llvm::raw_ostream &Out) const override;
ValueConstraintPtr negate() const override {
- NotNullConstraint Tmp(*this);
+ NullnessConstraint Tmp(*this);
Tmp.CannotBeNull = !this->CannotBeNull;
- return std::make_shared<NotNullConstraint>(Tmp);
+ return std::make_shared<NullnessConstraint>(Tmp);
}
protected:
@@ -407,9 +407,9 @@ class StdLibraryFunctionsChecker
/// The argument is meant to be a buffer that has a size constraint, and it
/// is allowed to have a NULL value if the size is 0. The size can depend on
/// 1 or 2 additional arguments, if one of these is 0 the buffer is allowed to
- /// be NULL. This is useful for functions like `fread` which have this special
- /// property.
- class NotNullBufferConstraint : public ValueConstraint {
+ /// be NULL. Otherwise, the buffer pointer must be non-null. This is useful
+ /// for functions like `fread` which have this special property.
+ class BufferNullnessConstraint : public ValueConstraint {
using ValueConstraint::ValueConstraint;
ArgNo SizeArg1N;
std::optional<ArgNo> SizeArg2N;
@@ -417,9 +417,9 @@ class StdLibraryFunctionsChecker
bool CannotBeNull = true;
public:
- NotNullBufferConstraint(ArgNo ArgN, ArgNo SizeArg1N,
- std::optional<ArgNo> SizeArg2N,
- bool CannotBeNull = true)
+ BufferNullnessConstraint(ArgNo ArgN, ArgNo SizeArg1N,
+ std::optional<ArgNo> SizeArg2N,
+ bool CannotBeNull = true)
: ValueConstraint(ArgN), SizeArg1N(SizeArg1N), SizeArg2N(SizeArg2N),
CannotBeNull(CannotBeNull) {}
@@ -436,9 +436,9 @@ class StdLibraryFunctionsChecker
llvm::raw_ostream &Out) const override;
ValueConstraintPtr negate() const override {
- NotNullBufferConstraint Tmp(*this);
+ BufferNullnessConstraint Tmp(*this);
Tmp.CannotBeNull = !this->CannotBeNull;
- return std::make_shared<NotNullBufferConstraint>(Tmp);
+ return std::make_shared<BufferNullnessConstraint>(Tmp);
}
protected:
@@ -1151,7 +1151,7 @@ ProgramStateRef StdLibraryFunctionsChecker::ComparisonConstraint::apply(
return State;
}
-ProgramStateRef StdLibraryFunctionsChecker::NotNullConstraint::apply(
+ProgramStateRef StdLibraryFunctionsChecker::NullnessConstraint::apply(
ProgramStateRef State, const CallEvent &Call, const Summary &Summary,
CheckerContext &C) const {
SVal V = getArgSVal(Call, getArgNo());
@@ -1165,26 +1165,27 @@ ProgramStateRef StdLibraryFunctionsChecker::NotNullConstraint::apply(
return State->assume(L, CannotBeNull);
}
-void StdLibraryFunctionsChecker::NotNullConstraint::describe(
+void StdLibraryFunctionsChecker::NullnessConstraint::describe(
DescriptionKind DK, const CallEvent &Call, ProgramStateRef State,
const Summary &Summary, llvm::raw_ostream &Out) const {
assert(CannotBeNull &&
- "Describe should not be used when the value must be NULL");
+ "'describe' is not implemented when the value must be NULL");
if (DK == Violation)
Out << "should not be NULL";
else
Out << "is not NULL";
}
-bool StdLibraryFunctionsChecker::NotNullConstraint::describeArgumentValue(
+bool StdLibraryFunctionsChecker::NullnessConstraint::describeArgumentValue(
const CallEvent &Call, ProgramStateRef State, const Summary &Summary,
llvm::raw_ostream &Out) const {
- assert(!CannotBeNull && "This function is used when the value is NULL");
+ assert(!CannotBeNull && "'describeArgumentValue' is not implemented when the "
+ "value must be non-NULL");
Out << "is NULL";
return true;
}
-ProgramStateRef StdLibraryFunctionsChecker::NotNullBufferConstraint::apply(
+ProgramStateRef StdLibraryFunctionsChecker::BufferNullnessConstraint::apply(
ProgramStateRef State, const CallEvent &Call, const Summary &Summary,
CheckerContext &C) const {
SVal V = getArgSVal(Call, getArgNo());
@@ -1213,21 +1214,23 @@ ProgramStateRef StdLibraryFunctionsChecker::NotNullBufferConstraint::apply(
return State->assume(L, CannotBeNull);
}
-void StdLibraryFunctionsChecker::NotNullBufferConstraint::describe(
+void StdLibraryFunctionsChecker::BufferNullnessConstraint::describe(
DescriptionKind DK, const CallEvent &Call, ProgramStateRef State,
const Summary &Summary, llvm::raw_ostream &Out) const {
assert(CannotBeNull &&
- "Describe should not be used when the value must be NULL");
+ "'describe' is not implemented when the buffer must be NULL");
if (DK == Violation)
Out << "should not be NULL";
else
Out << "is not NULL";
}
-bool StdLibraryFunctionsChecker::NotNullBufferConstraint::describeArgumentValue(
- const CallEvent &Call, ProgramStateRef State, const Summary &Summary,
- llvm::raw_ostream &Out) const {
- assert(!CannotBeNull && "This function is used when the value is NULL");
+bool StdLibraryFunctionsChecker::BufferNullnessConstraint::
+ describeArgumentValue(const CallEvent &Call, ProgramStateRef State,
+ const Summary &Summary,
+ llvm::raw_ostream &Out) const {
+ assert(!CannotBeNull && "'describeArgumentValue' is not implemented when the "
+ "buffer must be non-NULL");
Out << "is NULL";
return true;
}
@@ -1792,15 +1795,15 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
};
auto LessThanOrEq = BO_LE;
auto NotNull = [&](ArgNo ArgN) {
- return std::make_shared<NotNullConstraint>(ArgN);
+ return std::make_shared<NullnessConstraint>(ArgN);
};
auto IsNull = [&](ArgNo ArgN) {
- return std::make_shared<NotNullConstraint>(ArgN, false);
+ return std::make_shared<NullnessConstraint>(ArgN, false);
};
auto NotNullBuffer = [&](ArgNo ArgN, ArgNo SizeArg1N,
std::optional<ArgNo> SizeArg2N = std::nullopt) {
- return std::make_shared<NotNullBufferConstraint>(ArgN, SizeArg1N,
- SizeArg2N);
+ return std::make_shared<BufferNullnessConstraint>(ArgN, SizeArg1N,
+ SizeArg2N);
};
std::optional<QualType> FileTy = lookupTy("FILE");