10#include "clang/AST/Attrs.inc"
11#include "clang/AST/Expr.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
22 return Node.hasAttr<LifetimeBoundAttr>();
28 const auto DRef = ignoringParens(
30 to(parmVarDecl(hasType(hasCanonicalType(
31 qualType(lValueReferenceType(pointee(
32 qualType(isConstQualified()))))
34 hasDeclContext(functionDecl(
35 equalsBoundNode(
"func"),
36 hasReturnTypeLoc(loc(qualType(
37 hasCanonicalType(equalsBoundNode(
"type"))))))),
38 unless(hasLifetimeBoundAttr()))
44 hasAncestor(functionDecl().bind(
"func")),
46 DRef, ignoringParens(conditionalOperator(eachOf(
47 hasTrueExpression(DRef), hasFalseExpression(DRef))))))),
52 return A.getCanonicalType().withConst() == B.getCanonicalType().withConst();
57 B.getCanonicalType().getNonReferenceType());
61 const ParmVarDecl &PD) {
62 if (FD.getNumParams() != O.getNumParams())
64 for (
unsigned I = 0,
E = FD.getNumParams(); I <
E; ++I) {
65 const ParmVarDecl *DPD = FD.getParamDecl(I);
66 const QualType OPT = O.getParamDecl(I)->getType();
68 if (!llvm::isa<RValueReferenceType>(OPT) ||
80 const ParmVarDecl &PD) {
86 DeclContext::lookup_result LookupResult =
87 FD.getParent()->lookup(FD.getNameInfo().getName());
88 if (LookupResult.isSingleResult()) {
91 for (
const Decl *Overload : LookupResult) {
94 if (
const auto *O = dyn_cast<FunctionDecl>(Overload))
102 const MatchFinder::MatchResult &Result) {
103 const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
104 const auto *PD = Result.Nodes.getNodeAs<ParmVarDecl>(
"param");
105 const auto *DRef = Result.Nodes.getNodeAs<DeclRefExpr>(
"dref");
106 const SourceRange
Range = DRef->getSourceRange();
107 if (
Range.isInvalid())
114 "returning a constant reference parameter may cause use-after-free "
115 "when the parameter is constructed from a temporary")
const FunctionDecl * Decl
CharSourceRange Range
SourceRange for the file name.
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
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.
static bool hasSameParameterTypes(const FunctionDecl &FD, const FunctionDecl &O, const ParmVarDecl &PD)
static bool isSameTypeIgnoringConst(QualType A, QualType B)
static const Decl * findRVRefOverload(const FunctionDecl &FD, const ParmVarDecl &PD)
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)
static bool isSameTypeIgnoringConstRef(QualType A, QualType B)