Aliasing.cpp 3.1 KB

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