clang-tools 22.0.0git
Aliasing.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Aliasing.h"
10
11#include "clang/AST/Expr.h"
12#include "clang/AST/ExprCXX.h"
13
15
16/// Return whether \p S is a reference to the declaration of \p Var.
17static bool isAccessForVar(const Stmt *S, const ValueDecl *Var) {
18 if (const auto *DRE = dyn_cast<DeclRefExpr>(S))
19 return DRE->getDecl() == Var;
20
21 return false;
22}
23
24static bool capturesByRef(const CXXRecordDecl *RD, const ValueDecl *Var) {
25 return llvm::any_of(RD->captures(), [Var](const LambdaCapture &C) {
26 return C.capturesVariable() && C.getCaptureKind() == LCK_ByRef &&
27 C.getCapturedVar() == Var;
28 });
29}
30
31/// Return whether \p Var has a pointer or reference in \p S.
32static bool isPtrOrReferenceForVar(const Stmt *S, const ValueDecl *Var) {
33 // Treat block capture by reference as a form of taking a reference.
34 if (const auto *VD = dyn_cast<VarDecl>(Var); VD && VD->isEscapingByref())
35 return true;
36
37 if (const auto *DS = dyn_cast<DeclStmt>(S)) {
38 for (const Decl *D : DS->getDeclGroup()) {
39 if (const auto *LeftVar = dyn_cast<VarDecl>(D)) {
40 if (LeftVar->hasInit() && LeftVar->getType()->isReferenceType())
41 return isAccessForVar(LeftVar->getInit(), Var);
42 }
43 }
44 } else if (const auto *UnOp = dyn_cast<UnaryOperator>(S)) {
45 if (UnOp->getOpcode() == UO_AddrOf)
46 return isAccessForVar(UnOp->getSubExpr(), Var);
47 } else if (const auto *LE = dyn_cast<LambdaExpr>(S)) {
48 // Treat lambda capture by reference as a form of taking a reference.
49 return capturesByRef(LE->getLambdaClass(), Var);
50 } else if (const auto *ILE = dyn_cast<InitListExpr>(S)) {
51 return llvm::any_of(ILE->inits(), [Var](const Expr *ChildE) {
52 // If the child expression is a reference to Var, this means that it's
53 // used as an initializer of a reference-typed field. Otherwise
54 // it would have been surrounded with an implicit lvalue-to-rvalue cast.
55 return isAccessForVar(ChildE, Var);
56 });
57 }
58
59 return false;
60}
61
62/// Return whether \p Var has a pointer or reference in \p S.
63static bool hasPtrOrReferenceInStmt(const Stmt *S, const ValueDecl *Var) {
64 if (isPtrOrReferenceForVar(S, Var))
65 return true;
66
67 return llvm::any_of(S->children(), [&](const Stmt *Child) {
68 return Child && hasPtrOrReferenceInStmt(Child, Var);
69 });
70}
71
72static bool refersToEnclosingLambdaCaptureByRef(const Decl *Func,
73 const ValueDecl *Var) {
74 const auto *MD = dyn_cast<CXXMethodDecl>(Func);
75 if (!MD)
76 return false;
77
78 const CXXRecordDecl *RD = MD->getParent();
79 if (!RD->isLambda())
80 return false;
81
82 return capturesByRef(RD, Var);
83}
84
85bool hasPtrOrReferenceInFunc(const Decl *Func, const ValueDecl *Var) {
86 return hasPtrOrReferenceInStmt(Func->getBody(), Var) ||
88}
89
90} // namespace clang::tidy::utils
bool hasPtrOrReferenceInFunc(const Decl *Func, const ValueDecl *Var)
Returns whether Var has a pointer or reference in Func.
Definition Aliasing.cpp:85
static bool isAccessForVar(const Stmt *S, const ValueDecl *Var)
Return whether S is a reference to the declaration of Var.
Definition Aliasing.cpp:17
static bool isPtrOrReferenceForVar(const Stmt *S, const ValueDecl *Var)
Return whether Var has a pointer or reference in S.
Definition Aliasing.cpp:32
static bool refersToEnclosingLambdaCaptureByRef(const Decl *Func, const ValueDecl *Var)
Definition Aliasing.cpp:72
static bool hasPtrOrReferenceInStmt(const Stmt *S, const ValueDecl *Var)
Return whether Var has a pointer or reference in S.
Definition Aliasing.cpp:63
static bool capturesByRef(const CXXRecordDecl *RD, const ValueDecl *Var)
Definition Aliasing.cpp:24