Coverage Report

Created: 2023-09-21 18:56

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/TrustReturnsNonnullChecker.cpp
Line
Count
Source
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
42
  TrustReturnsNonnullChecker(ASTContext &Ctx) {}
29
30
209
  void checkPostCall(const CallEvent &Call, CheckerContext &C) const {
31
209
    ProgramStateRef State = C.getState();
32
33
209
    if (isNonNullPtr(Call))
34
1
      if (auto L = Call.getReturnValue().getAs<Loc>())
35
1
        State = State->assume(*L, /*assumption=*/true);
36
37
209
    C.addTransition(State);
38
209
  }
39
40
private:
41
  /// \returns Whether the method declaration has the attribute returns_nonnull.
42
209
  bool isNonNullPtr(const CallEvent &Call) const {
43
209
    QualType ExprRetType = Call.getResultType();
44
209
    const Decl *CallDeclaration =  Call.getDecl();
45
209
    if (!ExprRetType->isAnyPointerType() || 
!CallDeclaration9
)
46
201
      return false;
47
48
8
    return CallDeclaration->hasAttr<ReturnsNonNullAttr>();
49
209
  }
50
};
51
52
} // namespace
53
54
42
void ento::registerTrustReturnsNonnullChecker(CheckerManager &Mgr) {
55
42
  Mgr.registerChecker<TrustReturnsNonnullChecker>(Mgr.getASTContext());
56
42
}
57
58
88
bool ento::shouldRegisterTrustReturnsNonnullChecker(const CheckerManager &mgr) {
59
88
  return true;
60
88
}