Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
Line
Count
Source (jump to first uncovered line)
1
//=======- ASTUtils.cpp ------------------------------------------*- 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
#include "ASTUtils.h"
10
#include "PtrTypesSemantics.h"
11
#include "clang/AST/CXXInheritance.h"
12
#include "clang/AST/Decl.h"
13
#include "clang/AST/DeclCXX.h"
14
#include "clang/AST/ExprCXX.h"
15
16
using llvm::Optional;
17
namespace clang {
18
19
std::pair<const Expr *, bool>
20
54
tryToFindPtrOrigin(const Expr *E, bool StopAtFirstRefCountedObj) {
21
106
  while (E) {
22
106
    if (auto *cast = dyn_cast<CastExpr>(E)) {
23
27
      if (StopAtFirstRefCountedObj) {
24
25
        if (auto *ConversionFunc =
25
25
                dyn_cast_or_null<FunctionDecl>(cast->getConversionFunction())) {
26
1
          if (isCtorOfRefCounted(ConversionFunc))
27
0
            return {E, true};
28
1
        }
29
25
      }
30
      // FIXME: This can give false "origin" that would lead to false negatives
31
      // in checkers. See https://reviews.llvm.org/D37023 for reference.
32
27
      E = cast->getSubExpr();
33
27
      continue;
34
27
    }
35
79
    if (auto *call = dyn_cast<CallExpr>(E)) {
36
40
      if (auto *memberCall = dyn_cast<CXXMemberCallExpr>(call)) {
37
16
        Optional<bool> IsGetterOfRefCt =
38
16
            isGetterOfRefCounted(memberCall->getMethodDecl());
39
16
        if (IsGetterOfRefCt && *IsGetterOfRefCt) {
40
12
          E = memberCall->getImplicitObjectArgument();
41
12
          if (StopAtFirstRefCountedObj) {
42
5
            return {E, true};
43
5
          }
44
7
          continue;
45
12
        }
46
16
      }
47
48
28
      if (auto *operatorCall = dyn_cast<CXXOperatorCallExpr>(E)) {
49
0
        if (operatorCall->getNumArgs() == 1) {
50
0
          E = operatorCall->getArg(0);
51
0
          continue;
52
0
        }
53
0
      }
54
55
28
      if (auto *callee = call->getDirectCallee()) {
56
28
        if (isCtorOfRefCounted(callee)) {
57
0
          if (StopAtFirstRefCountedObj)
58
0
            return {E, true};
59
60
0
          E = call->getArg(0);
61
0
          continue;
62
0
        }
63
64
28
        if (isPtrConversion(callee)) {
65
11
          E = call->getArg(0);
66
11
          continue;
67
11
        }
68
28
      }
69
28
    }
70
56
    if (auto *unaryOp = dyn_cast<UnaryOperator>(E)) {
71
      // FIXME: Currently accepts ANY unary operator. Is it OK?
72
7
      E = unaryOp->getSubExpr();
73
7
      continue;
74
7
    }
75
76
49
    break;
77
56
  }
78
  // Some other expression.
79
49
  return {E, false};
80
54
}
81
82
36
bool isASafeCallArg(const Expr *E) {
83
36
  assert(E);
84
36
  if (auto *Ref = dyn_cast<DeclRefExpr>(E)) {
85
21
    if (auto *D = dyn_cast_or_null<VarDecl>(Ref->getFoundDecl())) {
86
21
      if (isa<ParmVarDecl>(D) || 
D->isLocalVarDecl()3
)
87
18
        return true;
88
21
    }
89
21
  }
90
91
  // TODO: checker for method calls on non-refcounted objects
92
18
  return isa<CXXThisExpr>(E);
93
36
}
94
95
} // namespace clang