10#include "../utils/Aliasing.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/AST/DeclVisitor.h"
13#include "clang/AST/StmtVisitor.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
15#include "clang/ASTMatchers/ASTMatchers.h"
16#include "clang/Lex/Lexer.h"
26class IsSamePtrExpr :
public StmtVisitor<IsSamePtrExpr, bool> {
31 const Expr *OtherE =
nullptr;
34 bool VisitDeclRefExpr(
const DeclRefExpr *E1) {
35 const auto *E2 = dyn_cast<DeclRefExpr>(OtherE);
38 const Decl *D1 = E1->getDecl()->getCanonicalDecl();
39 return isa<VarDecl, FieldDecl>(D1) &&
40 D1 == E2->getDecl()->getCanonicalDecl();
43 bool VisitMemberExpr(
const MemberExpr *E1) {
44 const auto *E2 = dyn_cast<MemberExpr>(OtherE);
47 if (!
check(E1->getBase(), E2->getBase()))
49 DeclAccessPair FD = E1->getFoundDecl();
50 return isa<FieldDecl>(FD.getDecl()) && FD == E2->getFoundDecl();
53 bool check(
const Expr *E1,
const Expr *E2) {
54 E1 = E1->IgnoreParenCasts();
55 E2 = E2->IgnoreParenCasts();
57 return Visit(
const_cast<Expr *
>(E1));
64class FindAssignToVarBefore
65 :
public ConstStmtVisitor<FindAssignToVarBefore, bool> {
67 const DeclRefExpr *VarRef;
70 bool isAccessForVar(
const Expr *
E)
const {
71 if (
const auto *
DeclRef = dyn_cast<DeclRefExpr>(
E->IgnoreParenCasts()))
73 DeclRef->getDecl()->getCanonicalDecl() == Var &&
74 SM.isBeforeInTranslationUnit(
E->getBeginLoc(),
75 VarRef->getBeginLoc());
80 FindAssignToVarBefore(
const VarDecl *Var,
const DeclRefExpr *VarRef,
82 : Var(Var->getCanonicalDecl()), VarRef(VarRef), SM(SM) {}
84 bool VisitDeclStmt(
const DeclStmt *S) {
85 for (
const Decl *D : S->getDeclGroup())
86 if (
const auto *LeftVar = dyn_cast<VarDecl>(D))
87 if (LeftVar->hasInit())
88 return isAccessForVar(LeftVar->getInit());
91 bool VisitBinaryOperator(
const BinaryOperator *S) {
92 if (S->getOpcode() == BO_Assign)
93 return isAccessForVar(S->getRHS());
96 bool VisitStmt(
const Stmt *S) {
97 for (
const Stmt *Child : S->children())
98 if (Child && Visit(Child))
111 functionDecl(hasName(
"::realloc"), parameterCountIs(2),
112 hasParameter(0, hasType(pointerType(pointee(voidType())))),
113 hasParameter(1, hasType(isInteger())))
117 callExpr(callee(ReallocDecl), hasArgument(0, expr().bind(
"ptr_input")),
118 hasAncestor(functionDecl().bind(
"parent_function")))
120 Finder->addMatcher(binaryOperator(hasOperatorName(
"="),
121 hasLHS(expr().bind(
"ptr_result")),
122 hasRHS(ignoringParenCasts(ReallocCall))),
127 const MatchFinder::MatchResult &Result) {
128 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"call");
131 const auto *PtrInputExpr = Result.Nodes.getNodeAs<Expr>(
"ptr_input");
132 const auto *PtrResultExpr = Result.Nodes.getNodeAs<Expr>(
"ptr_result");
133 if (!PtrInputExpr || !PtrResultExpr)
135 const auto *ReallocD = Result.Nodes.getNodeAs<
Decl>(
"realloc");
136 assert(ReallocD &&
"Value for 'realloc' should exist if 'call' was found.");
137 SourceManager &SM = ReallocD->getASTContext().getSourceManager();
139 if (!IsSamePtrExpr{}.check(PtrInputExpr, PtrResultExpr))
143 dyn_cast<DeclRefExpr>(PtrInputExpr->IgnoreParenImpCasts()))
144 if (
const auto *Var = dyn_cast<VarDecl>(
DeclRef->getDecl()))
145 if (
const auto *Func =
146 Result.Nodes.getNodeAs<FunctionDecl>(
"parent_function"))
147 if (FindAssignToVarBefore{Var,
DeclRef, SM}.Visit(Func->getBody()))
150 StringRef CodeOfAssignedExpr = Lexer::getSourceText(
151 CharSourceRange::getTokenRange(PtrResultExpr->getSourceRange()), SM,
153 diag(Call->getBeginLoc(),
"'%0' may be set to null if 'realloc' fails, which "
154 "may result in a leak of the original buffer")
155 << CodeOfAssignedExpr << PtrInputExpr->getSourceRange()
156 << PtrResultExpr->getSourceRange();
const FunctionDecl * Decl
const DeclRefExpr * DeclRef
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
bool check(llvm::StringRef File, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//