| 1 |
//=======- ASTUtis.h ---------------------------------------------*- 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 |
#ifndef LLVM_CLANG_ANALYZER_WEBKIT_ASTUTILS_H |
| 10 |
#define LLVM_CLANG_ANALYZER_WEBKIT_ASTUTILS_H |
| 11 |
|
| 12 |
#include "clang/AST/Decl.h" |
| 13 |
#include "llvm/ADT/APInt.h" |
| 14 |
#include "llvm/Support/Casting.h" |
| 15 |
|
| 16 |
#include <functional> |
| 17 |
#include <string> |
| 18 |
#include <utility> |
| 19 |
|
| 20 |
namespace clang { |
| 21 |
class Expr; |
| 22 |
|
| 23 |
/// This function de-facto defines a set of transformations that we consider |
| 24 |
/// safe (in heuristical sense). These transformation if passed a safe value as |
| 25 |
/// an input should provide a safe value (or an object that provides safe |
| 26 |
/// values). |
| 27 |
/// |
| 28 |
/// For more context see Static Analyzer checkers documentation - specifically |
| 29 |
/// webkit.UncountedCallArgsChecker checker. Allowed list of transformations: |
| 30 |
/// - constructors of ref-counted types (including factory methods) |
| 31 |
/// - getters of ref-counted types |
| 32 |
/// - member overloaded operators |
| 33 |
/// - casts |
| 34 |
/// - unary operators like ``&`` or ``*`` |
| 35 |
/// |
| 36 |
/// If passed expression is of type uncounted pointer/reference we try to find |
| 37 |
/// the "origin" of the pointer value. |
| 38 |
/// Origin can be for example a local variable, nullptr, constant or |
| 39 |
/// this-pointer. |
| 40 |
/// |
| 41 |
/// Certain subexpression nodes represent transformations that don't affect |
| 42 |
/// where the memory address originates from. We try to traverse such |
| 43 |
/// subexpressions to get to the relevant child nodes. Whenever we encounter a |
| 44 |
/// subexpression that either can't be ignored, we don't model its semantics or |
| 45 |
/// that has multiple children we stop. |
| 46 |
/// |
| 47 |
/// \p E is an expression of uncounted pointer/reference type. |
| 48 |
/// If \p StopAtFirstRefCountedObj is true and we encounter a subexpression that |
| 49 |
/// represents ref-counted object during the traversal we return relevant |
| 50 |
/// sub-expression and true. |
| 51 |
/// |
| 52 |
/// Calls \p callback with the subexpression that we traversed to and if \p |
| 53 |
/// StopAtFirstRefCountedObj is true we also specify whether we stopped early. |
| 54 |
/// Returns false if any of calls to callbacks returned false. Otherwise true. |
| 55 |
bool tryToFindPtrOrigin( |
| 56 |
const clang::Expr *E, bool StopAtFirstRefCountedObj, |
| 57 |
std::function<bool(const clang::CXXRecordDecl *)> isSafePtr, |
| 58 |
std::function<bool(const clang::QualType)> isSafePtrType, |
| 59 |
std::function<bool(const clang::Decl *)> isSafeGlobalDecl, |
| 60 |
std::function<bool(const clang::Expr *, bool)> callback); |
| 61 |
|
| 62 |
/// For \p E referring to a ref-countable/-counted pointer/reference we return |
| 63 |
/// whether it's a safe call argument. Examples: function parameter or |
| 64 |
/// this-pointer. The logic relies on the set of recursive rules we enforce for |
| 65 |
/// WebKit codebase. |
| 66 |
/// |
| 67 |
/// \returns Whether \p E is a safe call arugment. |
| 68 |
bool isASafeCallArg(const clang::Expr *E); |
| 69 |
|
| 70 |
/// \returns true if E is nullptr or __null. |
| 71 |
bool isNullPtr(const clang::Expr *E); |
| 72 |
|
| 73 |
/// \returns true if E is a MemberExpr accessing a const smart pointer type. |
| 74 |
bool isConstOwnerPtrMemberExpr(const clang::Expr *E); |
| 75 |
|
| 76 |
/// \returns true if E is a MemberExpr accessing a member variable which |
| 77 |
/// supports CheckedPtr. |
| 78 |
bool isExprToGetCheckedPtrCapableMember(const clang::Expr *E); |
| 79 |
|
| 80 |
/// \returns true if \p E is a [[alloc] init] pattern expression. |
| 81 |
/// Sets \p InnerExpr to the inner function call or selector invocation. |
| 82 |
bool isAllocInit(const Expr *E, const Expr **InnerExpr = nullptr); |
| 83 |
|
| 84 |
/// \returns true if E is a CXXMemberCallExpr which returns a const smart |
| 85 |
/// pointer type. |
| 86 |
class EnsureFunctionAnalysis { |
| 87 |
using CacheTy = llvm::DenseMap<const FunctionDecl *, bool>; |
| 88 |
mutable CacheTy Cache{}; |
| 89 |
|
| 90 |
public: |
| 91 |
bool isACallToEnsureFn(const Expr *E) const; |
| 92 |
}; |
| 93 |
|
| 94 |
/// \returns name of AST node or empty string. |
| 95 |
template <typename T> std::string safeGetName(const T *ASTNode) { |
| 96 |
const auto *const ND = llvm::dyn_cast_or_null<clang::NamedDecl>(ASTNode); |
| 97 |
if (!ND) |
| 98 |
return ""; |
| 99 |
|
| 100 |
// In case F is for example "operator|" the getName() method below would |
| 101 |
// assert. |
| 102 |
if (!ND->getDeclName().isIdentifier()) |
| 103 |
return ""; |
| 104 |
|
| 105 |
return ND->getName().str(); |
| 106 |
} |
| 107 |
|
| 108 |
} // namespace clang |
| 109 |
|
| 110 |
#endif |