clang
20.0.0git
lib
StaticAnalyzer
Checkers
TrustReturnsNonnullChecker.cpp
Go to the documentation of this file.
1
//== TrustReturnsNonnullChecker.cpp -- API nullability modeling -*- 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
// This checker adds nullability-related assumptions to methods annotated with
10
// returns_nonnull attribute.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "
clang/AST/Attr.h
"
15
#include "
clang/AST/Decl.h
"
16
#include "
clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h
"
17
#include "
clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h
"
18
#include "
clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
"
19
20
using namespace
clang
;
21
using namespace
ento;
22
23
namespace
{
24
25
class
TrustReturnsNonnullChecker :
public
Checker
<check::PostCall> {
26
27
public
:
28
TrustReturnsNonnullChecker(
ASTContext
&Ctx) {}
29
30
void
checkPostCall(
const
CallEvent
&
Call
,
CheckerContext
&
C
)
const
{
31
ProgramStateRef
State =
C
.getState();
32
33
if
(isNonNullPtr(
Call
))
34
if
(
auto
L =
Call
.getReturnValue().getAs<
Loc
>())
35
State = State->assume(*L,
/*assumption=*/
true
);
36
37
C
.addTransition(State);
38
}
39
40
private
:
41
/// \returns Whether the method declaration has the attribute returns_nonnull.
42
bool
isNonNullPtr(
const
CallEvent
&
Call
)
const
{
43
QualType
ExprRetType =
Call
.getResultType();
44
const
Decl
*CallDeclaration =
Call
.getDecl();
45
if
(!ExprRetType->
isAnyPointerType
() || !CallDeclaration)
46
return
false
;
47
48
return
CallDeclaration->
hasAttr
<ReturnsNonNullAttr>();
49
}
50
};
51
52
}
// namespace
53
54
void
ento::registerTrustReturnsNonnullChecker(
CheckerManager
&Mgr) {
55
Mgr.
registerChecker
<TrustReturnsNonnullChecker>(Mgr.
getASTContext
());
56
}
57
58
bool
ento::shouldRegisterTrustReturnsNonnullChecker(
const
CheckerManager
&mgr) {
59
return
true
;
60
}
Attr.h
BuiltinCheckerRegistration.h
CallEvent.h
CheckerContext.h
Decl.h
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition:
ASTContext.h:187
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition:
DeclBase.h:86
clang::Decl::hasAttr
bool hasAttr() const
Definition:
DeclBase.h:584
clang::QualType
A (possibly-)qualified type.
Definition:
Type.h:941
clang::Type::isAnyPointerType
bool isAnyPointerType() const
Definition:
Type.h:8011
clang::ento::CallEvent
Represents an abstract call to a function or method along a particular path.
Definition:
CallEvent.h:153
clang::ento::CheckerContext
Definition:
CheckerContext.h:24
clang::ento::CheckerManager
Definition:
CheckerManager.h:126
clang::ento::CheckerManager::registerChecker
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
Definition:
CheckerManager.h:204
clang::ento::CheckerManager::getASTContext
ASTContext & getASTContext() const
Definition:
CheckerManager.h:179
clang::ento::Checker
Definition:
Checker.h:512
clang::ento::Loc
Definition:
SVals.h:252
llvm::IntrusiveRefCntPtr< const ProgramState >
clang
The JSON file list parser is used to communicate input to InstallAPI.
Definition:
CalledOnceCheck.h:17
clang::LinkageSpecLanguageIDs::C
@ C
clang::OMPDeclareReductionInitKind::Call
@ Call
Generated on Fri Nov 15 2024 16:44:46 for clang by
1.9.6