ASTUtils.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //=======- ASTUtils.cpp ------------------------------------------*- C++ -*-==//
  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 "ASTUtils.h"
  9. #include "PtrTypesSemantics.h"
  10. #include "clang/AST/CXXInheritance.h"
  11. #include "clang/AST/Decl.h"
  12. #include "clang/AST/DeclCXX.h"
  13. #include "clang/AST/ExprCXX.h"
  14. using llvm::Optional;
  15. namespace clang {
  16. std::pair<const Expr *, bool>
  17. tryToFindPtrOrigin(const Expr *E, bool StopAtFirstRefCountedObj) {
  18. while (E) {
  19. if (auto *cast = dyn_cast<CastExpr>(E)) {
  20. if (StopAtFirstRefCountedObj) {
  21. if (auto *ConversionFunc =
  22. dyn_cast_or_null<FunctionDecl>(cast->getConversionFunction())) {
  23. if (isCtorOfRefCounted(ConversionFunc))
  24. return {E, true};
  25. }
  26. }
  27. // FIXME: This can give false "origin" that would lead to false negatives
  28. // in checkers. See https://reviews.llvm.org/D37023 for reference.
  29. E = cast->getSubExpr();
  30. continue;
  31. }
  32. if (auto *call = dyn_cast<CallExpr>(E)) {
  33. if (auto *memberCall = dyn_cast<CXXMemberCallExpr>(call)) {
  34. Optional<bool> IsGetterOfRefCt =
  35. isGetterOfRefCounted(memberCall->getMethodDecl());
  36. if (IsGetterOfRefCt && *IsGetterOfRefCt) {
  37. E = memberCall->getImplicitObjectArgument();
  38. if (StopAtFirstRefCountedObj) {
  39. return {E, true};
  40. }
  41. continue;
  42. }
  43. }
  44. if (auto *operatorCall = dyn_cast<CXXOperatorCallExpr>(E)) {
  45. if (operatorCall->getNumArgs() == 1) {
  46. E = operatorCall->getArg(0);
  47. continue;
  48. }
  49. }
  50. if (auto *callee = call->getDirectCallee()) {
  51. if (isCtorOfRefCounted(callee)) {
  52. if (StopAtFirstRefCountedObj)
  53. return {E, true};
  54. E = call->getArg(0);
  55. continue;
  56. }
  57. if (isPtrConversion(callee)) {
  58. E = call->getArg(0);
  59. continue;
  60. }
  61. }
  62. }
  63. if (auto *unaryOp = dyn_cast<UnaryOperator>(E)) {
  64. // FIXME: Currently accepts ANY unary operator. Is it OK?
  65. E = unaryOp->getSubExpr();
  66. continue;
  67. }
  68. break;
  69. }
  70. // Some other expression.
  71. return {E, false};
  72. }
  73. bool isASafeCallArg(const Expr *E) {
  74. assert(E);
  75. if (auto *Ref = dyn_cast<DeclRefExpr>(E)) {
  76. if (auto *D = dyn_cast_or_null<VarDecl>(Ref->getFoundDecl())) {
  77. if (isa<ParmVarDecl>(D) || D->isLocalVarDecl())
  78. return true;
  79. }
  80. }
  81. // TODO: checker for method calls on non-refcounted objects
  82. return isa<CXXThisExpr>(E);
  83. }
  84. } // namespace clang