10#include "clang/AST/ASTContext.h"
11#include "clang/AST/StmtVisitor.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Lex/Lexer.h"
24class IsSamePtrExpr :
public StmtVisitor<IsSamePtrExpr, bool> {
29 const Expr *OtherE =
nullptr;
32 bool VisitDeclRefExpr(
const DeclRefExpr *E1) {
33 const auto *E2 = dyn_cast<DeclRefExpr>(OtherE);
36 const Decl *D1 = E1->getDecl()->getCanonicalDecl();
37 return isa<VarDecl, FieldDecl>(D1) &&
38 D1 == E2->getDecl()->getCanonicalDecl();
41 bool VisitMemberExpr(
const MemberExpr *E1) {
42 const auto *E2 = dyn_cast<MemberExpr>(OtherE);
45 if (!
check(E1->getBase(), E2->getBase()))
47 DeclAccessPair FD = E1->getFoundDecl();
48 return isa<FieldDecl>(FD.getDecl()) && FD == E2->getFoundDecl();
51 bool check(
const Expr *E1,
const Expr *E2) {
52 E1 = E1->IgnoreParenCasts();
53 E2 = E2->IgnoreParenCasts();
55 return Visit(
const_cast<Expr *
>(E1));
62class FindAssignToVarBefore
63 :
public ConstStmtVisitor<FindAssignToVarBefore, bool> {
65 const DeclRefExpr *VarRef;
68 bool isAccessForVar(
const Expr *E)
const {
69 if (
const auto *DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
70 return DeclRef->getDecl() &&
71 DeclRef->getDecl()->getCanonicalDecl() == Var &&
72 SM.isBeforeInTranslationUnit(E->getBeginLoc(),
73 VarRef->getBeginLoc());
78 FindAssignToVarBefore(
const VarDecl *Var,
const DeclRefExpr *VarRef,
80 : Var(Var->getCanonicalDecl()), VarRef(VarRef), SM(SM) {}
82 bool VisitDeclStmt(
const DeclStmt *S) {
83 for (
const Decl *D : S->getDeclGroup())
84 if (
const auto *LeftVar = dyn_cast<VarDecl>(D))
85 if (LeftVar->hasInit())
86 return isAccessForVar(LeftVar->getInit());
89 bool VisitBinaryOperator(
const BinaryOperator *S) {
90 if (S->getOpcode() == BO_Assign)
91 return isAccessForVar(S->getRHS());
94 bool VisitStmt(
const Stmt *S) {
95 for (
const Stmt *Child : S->children())
96 if (Child && Visit(Child))
109 functionDecl(hasName(
"::realloc"), parameterCountIs(2),
110 hasParameter(0, hasType(pointerType(pointee(voidType())))),
111 hasParameter(1, hasType(isInteger())))
115 callExpr(callee(ReallocDecl), hasArgument(0, expr().bind(
"ptr_input")),
116 hasAncestor(functionDecl().bind(
"parent_function")))
118 Finder->addMatcher(binaryOperator(hasOperatorName(
"="),
119 hasLHS(expr().bind(
"ptr_result")),
120 hasRHS(ignoringParenCasts(ReallocCall))),
125 const MatchFinder::MatchResult &Result) {
126 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"call");
129 const auto *PtrInputExpr = Result.Nodes.getNodeAs<Expr>(
"ptr_input");
130 const auto *PtrResultExpr = Result.Nodes.getNodeAs<Expr>(
"ptr_result");
131 if (!PtrInputExpr || !PtrResultExpr)
133 const auto *ReallocD = Result.Nodes.getNodeAs<Decl>(
"realloc");
134 assert(ReallocD &&
"Value for 'realloc' should exist if 'call' was found.");
135 SourceManager &SM = ReallocD->getASTContext().getSourceManager();
137 if (!IsSamePtrExpr{}.check(PtrInputExpr, PtrResultExpr))
140 if (
const auto *DeclRef =
141 dyn_cast<DeclRefExpr>(PtrInputExpr->IgnoreParenImpCasts()))
142 if (
const auto *Var = dyn_cast<VarDecl>(DeclRef->getDecl()))
143 if (
const auto *Func =
144 Result.Nodes.getNodeAs<FunctionDecl>(
"parent_function"))
145 if (FindAssignToVarBefore{Var, DeclRef, SM}.Visit(Func->getBody()))
148 StringRef CodeOfAssignedExpr = Lexer::getSourceText(
149 CharSourceRange::getTokenRange(PtrResultExpr->getSourceRange()), SM,
151 diag(Call->getBeginLoc(),
"'%0' may be set to null if 'realloc' fails, which "
152 "may result in a leak of the original buffer")
153 << CodeOfAssignedExpr << PtrInputExpr->getSourceRange()
154 << PtrResultExpr->getSourceRange();
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
bool check(llvm::StringRef File, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//