xref: /freebsd/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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 tryToFindPtrOrigin(const Expr *E, bool StopAtFirstRefCountedObj) {
21   while (E) {
22     if (auto *cast = dyn_cast<CastExpr>(E)) {
23       if (StopAtFirstRefCountedObj) {
24         if (auto *ConversionFunc =
25                 dyn_cast_or_null<FunctionDecl>(cast->getConversionFunction())) {
26           if (isCtorOfRefCounted(ConversionFunc))
27             return {E, true};
28         }
29       }
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       E = cast->getSubExpr();
33       continue;
34     }
35     if (auto *call = dyn_cast<CallExpr>(E)) {
36       if (auto *memberCall = dyn_cast<CXXMemberCallExpr>(call)) {
37         if (isGetterOfRefCounted(memberCall->getMethodDecl())) {
38           E = memberCall->getImplicitObjectArgument();
39           if (StopAtFirstRefCountedObj) {
40             return {E, true};
41           }
42           continue;
43         }
44       }
45 
46       if (auto *operatorCall = dyn_cast<CXXOperatorCallExpr>(E)) {
47         if (operatorCall->getNumArgs() == 1) {
48           E = operatorCall->getArg(0);
49           continue;
50         }
51       }
52 
53       if (auto *callee = call->getDirectCallee()) {
54         if (isCtorOfRefCounted(callee)) {
55           if (StopAtFirstRefCountedObj)
56             return {E, true};
57 
58           E = call->getArg(0);
59           continue;
60         }
61 
62         if (isPtrConversion(callee)) {
63           E = call->getArg(0);
64           continue;
65         }
66       }
67     }
68     if (auto *unaryOp = dyn_cast<UnaryOperator>(E)) {
69       // FIXME: Currently accepts ANY unary operator. Is it OK?
70       E = unaryOp->getSubExpr();
71       continue;
72     }
73 
74     break;
75   }
76   // Some other expression.
77   return {E, false};
78 }
79 
80 bool isASafeCallArg(const Expr *E) {
81   assert(E);
82   if (auto *Ref = dyn_cast<DeclRefExpr>(E)) {
83     if (auto *D = dyn_cast_or_null<VarDecl>(Ref->getFoundDecl())) {
84       if (isa<ParmVarDecl>(D) || D->isLocalVarDecl())
85         return true;
86     }
87   }
88 
89   // TODO: checker for method calls on non-refcounted objects
90   return isa<CXXThisExpr>(E);
91 }
92 
93 } // namespace clang
94