TransGCCalls.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //===--- TransGCCalls.cpp - Transformations to ARC mode -------------------===//
  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 "Transforms.h"
  9. #include "Internals.h"
  10. #include "clang/AST/ASTContext.h"
  11. #include "clang/Sema/SemaDiagnostic.h"
  12. using namespace clang;
  13. using namespace arcmt;
  14. using namespace trans;
  15. namespace {
  16. class GCCollectableCallsChecker :
  17. public RecursiveASTVisitor<GCCollectableCallsChecker> {
  18. MigrationContext &MigrateCtx;
  19. IdentifierInfo *NSMakeCollectableII;
  20. IdentifierInfo *CFMakeCollectableII;
  21. public:
  22. GCCollectableCallsChecker(MigrationContext &ctx)
  23. : MigrateCtx(ctx) {
  24. IdentifierTable &Ids = MigrateCtx.Pass.Ctx.Idents;
  25. NSMakeCollectableII = &Ids.get("NSMakeCollectable");
  26. CFMakeCollectableII = &Ids.get("CFMakeCollectable");
  27. }
  28. bool shouldWalkTypesOfTypeLocs() const { return false; }
  29. bool VisitCallExpr(CallExpr *E) {
  30. TransformActions &TA = MigrateCtx.Pass.TA;
  31. if (MigrateCtx.isGCOwnedNonObjC(E->getType())) {
  32. TA.report(E->getBeginLoc(), diag::warn_arcmt_nsalloc_realloc,
  33. E->getSourceRange());
  34. return true;
  35. }
  36. Expr *CEE = E->getCallee()->IgnoreParenImpCasts();
  37. if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) {
  38. if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl())) {
  39. if (!FD->getDeclContext()->getRedeclContext()->isFileContext())
  40. return true;
  41. if (FD->getIdentifier() == NSMakeCollectableII) {
  42. Transaction Trans(TA);
  43. TA.clearDiagnostic(diag::err_unavailable,
  44. diag::err_unavailable_message,
  45. diag::err_ovl_deleted_call, // ObjC++
  46. DRE->getSourceRange());
  47. TA.replace(DRE->getSourceRange(), "CFBridgingRelease");
  48. } else if (FD->getIdentifier() == CFMakeCollectableII) {
  49. TA.reportError("CFMakeCollectable will leak the object that it "
  50. "receives in ARC", DRE->getLocation(),
  51. DRE->getSourceRange());
  52. }
  53. }
  54. }
  55. return true;
  56. }
  57. };
  58. } // anonymous namespace
  59. void GCCollectableCallsTraverser::traverseBody(BodyContext &BodyCtx) {
  60. GCCollectableCallsChecker(BodyCtx.getMigrationContext())
  61. .TraverseStmt(BodyCtx.getTopStmt());
  62. }