| 1 | //===------------- Aliasing.cpp - clang-tidy ------------------------------===// |
| 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 | |
| 14 | namespace clang::tidy::utils { |
| 15 | |
| 16 | /// Return whether \p S is a reference to the declaration of \p Var. |
| 17 | static bool isAccessForVar(const Stmt *S, const VarDecl *Var) { |
| 18 | if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: S)) |
| 19 | return DRE->getDecl() == Var; |
| 20 | |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | static bool capturesByRef(const CXXRecordDecl *RD, const VarDecl *Var) { |
| 25 | return llvm::any_of(Range: RD->captures(), P: [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. |
| 32 | static bool isPtrOrReferenceForVar(const Stmt *S, const VarDecl *Var) { |
| 33 | // Treat block capture by reference as a form of taking a reference. |
| 34 | if (Var->isEscapingByref()) |
| 35 | return true; |
| 36 | |
| 37 | if (const auto *DS = dyn_cast<DeclStmt>(Val: S)) { |
| 38 | for (const Decl *D : DS->getDeclGroup()) { |
| 39 | if (const auto *LeftVar = dyn_cast<VarDecl>(Val: D)) { |
| 40 | if (LeftVar->hasInit() && LeftVar->getType()->isReferenceType()) { |
| 41 | return isAccessForVar(LeftVar->getInit(), Var); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } else if (const auto *UnOp = dyn_cast<UnaryOperator>(Val: S)) { |
| 46 | if (UnOp->getOpcode() == UO_AddrOf) |
| 47 | return isAccessForVar(UnOp->getSubExpr(), Var); |
| 48 | } else if (const auto *LE = dyn_cast<LambdaExpr>(Val: S)) { |
| 49 | // Treat lambda capture by reference as a form of taking a reference. |
| 50 | return capturesByRef(RD: LE->getLambdaClass(), Var); |
| 51 | } else if (const auto *ILE = dyn_cast<InitListExpr>(Val: S)) { |
| 52 | return llvm::any_of(Range: ILE->inits(), P: [Var](const Expr *ChildE) { |
| 53 | // If the child expression is a reference to Var, this means that it's |
| 54 | // used as an initializer of a reference-typed field. Otherwise |
| 55 | // it would have been surrounded with an implicit lvalue-to-rvalue cast. |
| 56 | return isAccessForVar(ChildE, Var); |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | /// Return whether \p Var has a pointer or reference in \p S. |
| 64 | static bool hasPtrOrReferenceInStmt(const Stmt *S, const VarDecl *Var) { |
| 65 | if (isPtrOrReferenceForVar(S, Var)) |
| 66 | return true; |
| 67 | |
| 68 | for (const Stmt *Child : S->children()) { |
| 69 | if (!Child) |
| 70 | continue; |
| 71 | |
| 72 | if (hasPtrOrReferenceInStmt(S: Child, Var)) |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | static bool refersToEnclosingLambdaCaptureByRef(const Decl *Func, |
| 80 | const VarDecl *Var) { |
| 81 | const auto *MD = dyn_cast<CXXMethodDecl>(Val: Func); |
| 82 | if (!MD) |
| 83 | return false; |
| 84 | |
| 85 | const CXXRecordDecl *RD = MD->getParent(); |
| 86 | if (!RD->isLambda()) |
| 87 | return false; |
| 88 | |
| 89 | return capturesByRef(RD, Var); |
| 90 | } |
| 91 | |
| 92 | bool hasPtrOrReferenceInFunc(const Decl *Func, const VarDecl *Var) { |
| 93 | return hasPtrOrReferenceInStmt(S: Func->getBody(), Var) || |
| 94 | refersToEnclosingLambdaCaptureByRef(Func, Var); |
| 95 | } |
| 96 | |
| 97 | } // namespace clang::tidy::utils |
| 98 | |