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