clang 22.0.0git
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"
19
20using namespace clang;
21using namespace ento;
22
23namespace {
24
25class TrustReturnsNonnullChecker : public Checker<check::PostCall> {
26
27public:
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
40private:
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
54void ento::registerTrustReturnsNonnullChecker(CheckerManager &Mgr) {
55 Mgr.registerChecker<TrustReturnsNonnullChecker>(Mgr.getASTContext());
56}
57
58bool ento::shouldRegisterTrustReturnsNonnullChecker(const CheckerManager &mgr) {
59 return true;
60}
bool hasAttr() const
Definition DeclBase.h:577
bool isAnyPointerType() const
Definition TypeBase.h:8530
ASTContext & getASTContext() const
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
Definition Checker.h:553
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
The JSON file list parser is used to communicate input to InstallAPI.