ASTUtils.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include <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. std::optional<bool> IsGetterOfRefCt = isGetterOfRefCounted(memberCall->getMethodDecl());
  35. if (IsGetterOfRefCt && *IsGetterOfRefCt) {
  36. E = memberCall->getImplicitObjectArgument();
  37. if (StopAtFirstRefCountedObj) {
  38. return {E, true};
  39. }
  40. continue;
  41. }
  42. }
  43. if (auto *operatorCall = dyn_cast<CXXOperatorCallExpr>(E)) {
  44. if (operatorCall->getNumArgs() == 1) {
  45. E = operatorCall->getArg(0);
  46. continue;
  47. }
  48. }
  49. if (auto *callee = call->getDirectCallee()) {
  50. if (isCtorOfRefCounted(callee)) {
  51. if (StopAtFirstRefCountedObj)
  52. return {E, true};
  53. E = call->getArg(0);
  54. continue;
  55. }
  56. if (isPtrConversion(callee)) {
  57. E = call->getArg(0);
  58. continue;
  59. }
  60. }
  61. }
  62. if (auto *unaryOp = dyn_cast<UnaryOperator>(E)) {
  63. // FIXME: Currently accepts ANY unary operator. Is it OK?
  64. E = unaryOp->getSubExpr();
  65. continue;
  66. }
  67. break;
  68. }
  69. // Some other expression.
  70. return {E, false};
  71. }
  72. bool isASafeCallArg(const Expr *E) {
  73. assert(E);
  74. if (auto *Ref = dyn_cast<DeclRefExpr>(E)) {
  75. if (auto *D = dyn_cast_or_null<VarDecl>(Ref->getFoundDecl())) {
  76. if (isa<ParmVarDecl>(D) || D->isLocalVarDecl())
  77. return true;
  78. }
  79. }
  80. // TODO: checker for method calls on non-refcounted objects
  81. return isa<CXXThisExpr>(E);
  82. }
  83. } // namespace clang