Skip to content

[alpha.webkit.ForwardDeclChecker] Add a new WebKit checker for forward declarations #130554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 13, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extract a function to report bug per review comment
  • Loading branch information
rniwa committed Mar 12, 2025
commit f77cef5967f904110a5c83e369154a7cbc13777d
40 changes: 16 additions & 24 deletions clang/lib/StaticAnalyzer/Checkers/WebKit/ForwardDeclChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,11 @@ class ForwardDeclChecker : public Checker<check::ASTDecl<TranslationUnitDecl>> {

SmallString<100> Buf;
llvm::raw_svector_ostream Os(Buf);

const std::string TypeName = QT.getAsString();
Os << "Local variable ";
printQuotedQualifiedName(Os, V);
Os << " uses a forward declared type '" << TypeName << "'";

const SourceLocation SrcLocToReport = V->getBeginLoc();
PathDiagnosticLocation BSLoc(SrcLocToReport, BR->getSourceManager());
auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
Report->addRange(V->getSourceRange());
Report->setDeclWithIssue(DeclWithIssue);
BR->emitReport(std::move(Report));
reportBug(V->getBeginLoc(), V->getSourceRange(), DeclWithIssue, Os.str(),
QT);
}

void visitCallExpr(const CallExpr *CE, const Decl *DeclWithIssue) const {
Expand Down Expand Up @@ -281,44 +274,43 @@ class ForwardDeclChecker : public Checker<check::ASTDecl<TranslationUnitDecl>> {
reportUnknownArgType(Arg, Param, DeclWithIssue);
}

void reportUnknownArgType(const Expr *CallArg, const ParmVarDecl *Param,
void reportUnknownArgType(const Expr *CA, const ParmVarDecl *Param,
const Decl *DeclWithIssue) const {
assert(CallArg);
assert(CA);

SmallString<100> Buf;
llvm::raw_svector_ostream Os(Buf);

const std::string paramName = safeGetName(Param);
const std::string TypeName = Param->getType().getAsString();
Os << "Call argument";
if (!paramName.empty()) {
Os << " for parameter ";
printQuotedQualifiedName(Os, Param);
}
Os << " uses a forward declared type '" << TypeName << "'";

const SourceLocation SrcLocToReport = CallArg->getExprLoc();
PathDiagnosticLocation BSLoc(SrcLocToReport, BR->getSourceManager());
auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
Report->addRange(CallArg->getSourceRange());
Report->setDeclWithIssue(DeclWithIssue);
BR->emitReport(std::move(Report));
reportBug(CA->getExprLoc(), CA->getSourceRange(), DeclWithIssue, Os.str(),
Param->getType());
}

void reportUnknownRecieverType(const Expr *Receiver,
const Decl *DeclWithIssue) const {
assert(Receiver);
reportBug(Receiver->getExprLoc(), Receiver->getSourceRange(), DeclWithIssue,
"Receiver", Receiver->getType());
}

void reportBug(const SourceLocation &SrcLoc, const SourceRange &SrcRange,
const Decl *DeclWithIssue, const StringRef &Description,
QualType Type) const {
SmallString<100> Buf;
llvm::raw_svector_ostream Os(Buf);

const std::string TypeName = Receiver->getType().getAsString();
Os << "Receiver uses a forward declared type '" << TypeName << "'";
const std::string TypeName = Type.getAsString();
Os << Description << " uses a forward declared type '" << TypeName << "'";

const SourceLocation SrcLocToReport = Receiver->getExprLoc();
PathDiagnosticLocation BSLoc(SrcLocToReport, BR->getSourceManager());
PathDiagnosticLocation BSLoc(SrcLoc, BR->getSourceManager());
auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
Report->addRange(Receiver->getSourceRange());
Report->addRange(SrcRange);
Report->setDeclWithIssue(DeclWithIssue);
BR->emitReport(std::move(Report));
}
Expand Down