22#include "llvm/ADT/DenseSet.h"
23#include "llvm/Support/SaveAndRestore.h"
30class ForwardDeclChecker :
public Checker<check::ASTDecl<TranslationUnitDecl>> {
32 mutable BugReporter *BR =
nullptr;
33 mutable RetainTypeChecker RTC;
34 mutable llvm::DenseSet<const Type *> SystemTypes;
38 : Bug(this,
"Forward declared member or local variable or parameter",
39 "WebKit coding guidelines") {}
41 void checkASTDecl(
const TranslationUnitDecl *TUD, AnalysisManager &MGR,
42 BugReporter &BRArg)
const {
49 const ForwardDeclChecker *Checker;
50 Decl *DeclWithIssue{
nullptr};
52 explicit LocalVisitor(
const ForwardDeclChecker *Checker)
55 ShouldVisitTemplateInstantiations =
true;
56 ShouldVisitImplicitCode =
false;
59 bool VisitTypedefDecl(TypedefDecl *TD)
override {
60 Checker->visitTypedef(TD);
64 bool VisitRecordDecl(RecordDecl *RD)
override {
65 Checker->visitRecordDecl(RD, DeclWithIssue);
69 bool TraverseDecl(Decl *D)
override {
70 llvm::SaveAndRestore SavedDecl(DeclWithIssue);
76 bool VisitVarDecl(VarDecl *
V)
override {
77 if (
V->isLocalVarDecl())
78 Checker->visitVarDecl(
V, DeclWithIssue);
82 bool VisitCallExpr(CallExpr *CE)
override {
83 Checker->visitCallExpr(CE, DeclWithIssue);
87 bool VisitCXXConstructExpr(CXXConstructExpr *CE)
override {
88 Checker->visitConstructExpr(CE, DeclWithIssue);
92 bool VisitObjCMessageExpr(ObjCMessageExpr *ObjCMsgExpr)
override {
93 Checker->visitObjCMessageExpr(ObjCMsgExpr, DeclWithIssue);
98 LocalVisitor visitor(
this);
99 RTC.visitTranslationUnitDecl(TUD);
100 visitor.TraverseDecl(
const_cast<TranslationUnitDecl *
>(TUD));
103 void visitTypedef(
const TypedefDecl *TD)
const {
104 RTC.visitTypedef(TD);
106 assert(BR &&
"expected nonnull BugReporter");
107 if (BR->getSourceManager().isInSystemHeader(TD->
getBeginLoc())) {
108 if (
auto *
Type = QT.getTypePtrOrNull())
109 SystemTypes.insert(
Type);
113 bool isUnknownType(QualType QT)
const {
121 auto *
R = PointeeType->getAsCXXRecordDecl();
124 auto Name =
R->getName();
125 if (
R->hasDefinition())
128 if (
auto *
Specialization = dyn_cast<ClassTemplateSpecializationDecl>(R)) {
130 for (S = S->getMostRecentDecl(); S; S = S->getPreviousDecl()) {
131 if (S->isThisDeclarationADefinition())
136 return !RTC.isUnretained(QT) && !SystemTypes.contains(CanonicalType) &&
137 !SystemTypes.contains(PointeeType) && !Name.starts_with(
"Opaque") &&
141 void visitRecordDecl(
const RecordDecl *RD,
const Decl *DeclWithIssue)
const {
149 if (!RDLocation.isValid())
153 if (Kind != TagTypeKind::Struct && Kind != TagTypeKind::Class)
156 assert(BR &&
"expected nonnull BugReporter");
157 if (BR->getSourceManager().isInSystemHeader(RDLocation))
162 auto R = llvm::dyn_cast_or_null<CXXRecordDecl>(RD);
167 auto QT =
Member->getType();
168 if (isUnknownType(QT)) {
169 SmallString<100> Buf;
170 llvm::raw_svector_ostream Os(Buf);
173 Os <<
"Member variable ";
175 Os <<
" uses a forward declared type '" <<
TypeName <<
"'";
177 const SourceLocation SrcLocToReport =
Member->getBeginLoc();
178 PathDiagnosticLocation BSLoc(SrcLocToReport, BR->getSourceManager());
179 auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
181 Report->setDeclWithIssue(DeclWithIssue);
182 BR->emitReport(std::move(
Report));
187 void visitVarDecl(
const VarDecl *
V,
const Decl *DeclWithIssue)
const {
188 assert(BR &&
"expected nonnull BugReporter");
189 if (BR->getSourceManager().isInSystemHeader(
V->getBeginLoc()))
192 auto QT =
V->getType();
193 if (!isUnknownType(QT))
196 SmallString<100> Buf;
197 llvm::raw_svector_ostream Os(Buf);
198 Os <<
"Local variable ";
201 reportBug(
V->getBeginLoc(),
V->getSourceRange(), DeclWithIssue, Os.str(),
205 void visitCallExpr(
const CallExpr *CE,
const Decl *DeclWithIssue)
const {
206 assert(BR &&
"expected nonnull BugReporter");
207 if (BR->getSourceManager().isInSystemHeader(CE->
getExprLoc()))
215 for (
auto P = F->param_begin();
216 P < F->param_end() && ArgIdx < CE->getNumArgs(); ++P, ++ArgIdx)
217 visitCallArg(CE->
getArg(ArgIdx), *P, DeclWithIssue);
221 void visitConstructExpr(
const CXXConstructExpr *CE,
222 const Decl *DeclWithIssue)
const {
223 assert(BR &&
"expected nonnull BugReporter");
224 if (BR->getSourceManager().isInSystemHeader(CE->
getExprLoc()))
229 for (
auto P = F->param_begin();
230 P < F->param_end() && ArgIdx < CE->getNumArgs(); ++P, ++ArgIdx)
231 visitCallArg(CE->
getArg(ArgIdx), *P, DeclWithIssue);
235 void visitObjCMessageExpr(
const ObjCMessageExpr *E,
236 const Decl *DeclWithIssue)
const {
237 assert(BR &&
"expected nonnull BugReporter");
238 if (BR->getSourceManager().isInSystemHeader(E->
getExprLoc()))
242 Receiver = Receiver->IgnoreParenCasts();
244 reportUnknownReceiverType(Receiver, DeclWithIssue);
252 for (
unsigned i = 0; i < ArgCount && i < MethodDecl->param_size(); ++i)
253 visitCallArg(E->
getArg(i), MethodDecl->getParamDecl(i), DeclWithIssue);
256 void visitCallArg(
const Expr *Arg,
const ParmVarDecl *Param,
257 const Decl *DeclWithIssue)
const {
260 ArgExpr = ArgExpr->IgnoreParenCasts();
261 if (
auto *InnerCE = dyn_cast<CallExpr>(ArgExpr)) {
262 if (
auto *InnerCallee = InnerCE->getDirectCallee()) {
264 ArgExpr = InnerCE->getArg(0);
269 if (
auto *UO = dyn_cast<UnaryOperator>(ArgExpr)) {
270 auto OpCode = UO->getOpcode();
271 if (OpCode == UO_Deref || OpCode == UO_AddrOf) {
272 ArgExpr = UO->getSubExpr();
279 if (
auto *MemberCallExpr = dyn_cast<CXXMemberCallExpr>(ArgExpr)) {
284 if (
auto *OpCE = dyn_cast<CXXOperatorCallExpr>(ArgExpr)) {
285 auto *
Method = dyn_cast_or_null<CXXMethodDecl>(OpCE->getDirectCallee());
287 if (OpCE->getOperator() == OO_Star && OpCE->getNumArgs() == 1)
296 if (
auto *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) {
297 if (
auto *ValDecl = DRE->getDecl()) {
303 QualType ArgType = Param->
getType();
304 if (!isUnknownType(ArgType))
307 reportUnknownArgType(Arg, Param, DeclWithIssue);
310 void reportUnknownArgType(
const Expr *CA,
const ParmVarDecl *Param,
311 const Decl *DeclWithIssue)
const {
314 SmallString<100> Buf;
315 llvm::raw_svector_ostream Os(Buf);
318 Os <<
"Call argument";
319 if (!paramName.empty()) {
320 Os <<
" for parameter ";
328 void reportUnknownReceiverType(
const Expr *Receiver,
329 const Decl *DeclWithIssue)
const {
332 "Receiver", Receiver->
getType());
335 void reportBug(
const SourceLocation &SrcLoc,
const SourceRange &SrcRange,
336 const Decl *DeclWithIssue,
const StringRef &Description,
337 QualType
Type)
const {
338 SmallString<100> Buf;
339 llvm::raw_svector_ostream Os(Buf);
342 Os << Description <<
" uses a forward declared type '" <<
TypeName <<
"'";
344 assert(BR &&
"expected nonnull BugReporter");
345 PathDiagnosticLocation BSLoc(SrcLoc, BR->getSourceManager());
346 auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
347 Report->addRange(SrcRange);
348 Report->setDeclWithIssue(DeclWithIssue);
349 BR->emitReport(std::move(
Report));
359bool ento::shouldRegisterForwardDeclChecker(
const CheckerManager &) {
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
Expr * getArg(unsigned Arg)
Return the specified argument.
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
SourceLocation getLocation() const
virtual bool TraverseDecl(MaybeConst< Decl > *D)
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Expr * getInstanceReceiver()
Returns the object expression (receiver) for an instance message, or null for a message that is not a...
const ObjCMethodDecl * getMethodDecl() const
QualType getReceiverType() const
Retrieve the receiver type to which this message is being directed.
unsigned getNumArgs() const
Return the number of actual arguments in this message, not counting the receiver.
QualType getCanonicalType() const
const Type * getTypePtrOrNull() const
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
bool isLambda() const
Determine whether this record is a class describing a lambda function object.
field_range fields() const
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
bool isThisDeclarationADefinition() const
Return true if this declaration is a completion definition of the type.
TagKind getTagKind() const
SourceLocation getBeginLoc() const LLVM_READONLY
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
QualType getUnderlyingType() const
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
@ Specialization
We are substituting template parameters for template arguments in order to form a template specializa...
void printQuotedQualifiedName(llvm::raw_ostream &Os, const NamedDeclDerivedT &D)
void printQuotedName(llvm::raw_ostream &Os, const NamedDeclDerivedT &D)
bool isOwnerPtr(const std::string &Name)
bool isRefCounted(const CXXRecordDecl *R)
bool isOwnerPtrType(const clang::QualType T)
bool isRetainPtrOrOSPtr(const std::string &Name)
std::string safeGetName(const T *ASTNode)
bool isNullPtr(const clang::Expr *E)
bool isCheckedPtr(const std::string &Name)
DynamicRecursiveASTVisitorBase< false > DynamicRecursiveASTVisitor
bool isStdOrWTFMove(const clang::FunctionDecl *F)