20#include "llvm/Support/Casting.h"
28class RawPtrRefMemberChecker
29 :
public Checker<check::ASTDecl<TranslationUnitDecl>> {
35 RawPtrRefMemberChecker(
const char *description)
36 : Bug(this, description,
"WebKit coding guidelines") {}
38 virtual std::optional<bool>
41 virtual const char *typeName()
const = 0;
42 virtual const char *invariant()
const = 0;
52 const RawPtrRefMemberChecker *
Checker;
53 explicit LocalVisitor(
const RawPtrRefMemberChecker *
Checker)
56 ShouldVisitTemplateInstantiations =
true;
57 ShouldVisitImplicitCode =
false;
60 bool VisitRecordDecl(
RecordDecl *RD)
override {
66 LocalVisitor visitor(
this);
70 void visitRecordDecl(
const RecordDecl *RD)
const {
71 if (shouldSkipDecl(RD))
75 const Type *MemberType =
Member->getType().getTypePtrOrNull();
81 if (MemberCXXRD->hasDefinition()) {
82 std::optional<bool> isRCAble = isPtrCompatible(MemberCXXRD);
83 if (isRCAble && *isRCAble)
84 reportBug(
Member, MemberType, MemberCXXRD, RD);
90 bool shouldSkipDecl(
const RecordDecl *RD)
const {
103 if (!RDLocation.isValid())
108 if (Kind != TagTypeKind::Struct && Kind != TagTypeKind::Class)
117 auto CXXRD = llvm::dyn_cast_or_null<CXXRecordDecl>(RD);
119 return isPtrCls(CXXRD);
132 llvm::raw_svector_ostream Os(Buf);
134 Os <<
"Member variable ";
139 << (isa<PointerType>(MemberType) ?
"raw pointer" :
"reference") <<
" to "
140 << typeName() <<
" ";
142 Os <<
"; " << invariant() <<
".";
146 auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
152class NoUncountedMemberChecker final :
public RawPtrRefMemberChecker {
154 NoUncountedMemberChecker()
155 : RawPtrRefMemberChecker(
"Member variable is a raw-pointer/reference to "
156 "reference-countable type") {}
167 const char *typeName() const final {
return "ref-countable type"; }
169 const char *invariant() const final {
170 return "member variables must be Ref, RefPtr, WeakRef, or WeakPtr";
174class NoUncheckedPtrMemberChecker final :
public RawPtrRefMemberChecker {
176 NoUncheckedPtrMemberChecker()
177 : RawPtrRefMemberChecker(
"Member variable is a raw-pointer/reference to "
178 "checked-pointer capable type") {}
189 const char *typeName() const final {
return "CheckedPtr capable type"; }
191 const char *invariant() const final {
192 return "member variables must be a CheckedPtr, CheckedRef, WeakRef, or "
203bool ento::shouldRegisterNoUncountedMemberChecker(
const CheckerManager &Mgr) {
207void ento::registerNoUncheckedPtrMemberChecker(
CheckerManager &Mgr) {
211bool ento::shouldRegisterNoUncheckedPtrMemberChecker(
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Represents a C++ struct/union/class.
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
SourceLocation getLocation() const
Recursive AST visitor that supports extension via dynamic dispatch.
Represents a member of a struct/union/class.
Represents a struct/union/class.
bool isLambda() const
Determine whether this record is a class describing a lambda function object.
field_range fields() const
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
bool isThisDeclarationADefinition() const
Return true if this declaration is a completion definition of the type.
TagKind getTagKind() const
The top declaration context.
The base class of the type hierarchy.
const CXXRecordDecl * getPointeeCXXRecordDecl() const
If this is a pointer or reference to a RecordType, return the CXXRecordDecl that the type refers to.
BugReporter is a utility class for generating PathDiagnostics for analysis.
const SourceManager & getSourceManager()
virtual void emitReport(std::unique_ptr< BugReport > R)
Add the given report to the set of reports tracked by BugReporter.
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
The JSON file list parser is used to communicate input to InstallAPI.
std::optional< bool > isCheckedPtrCapable(const clang::CXXRecordDecl *R)
void printQuotedQualifiedName(llvm::raw_ostream &Os, const NamedDeclDerivedT &D)
void printQuotedName(llvm::raw_ostream &Os, const NamedDeclDerivedT &D)
std::optional< bool > isRefCountable(const clang::CXXRecordDecl *R)
bool isRefCounted(const CXXRecordDecl *R)
bool isCheckedPtr(const std::string &Name)