TransProtectedScope.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //===--- TransProtectedScope.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. //
  9. // Adds brackets in case statements that "contain" initialization of retaining
  10. // variable, thus emitting the "switch case is in protected scope" error.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "Transforms.h"
  14. #include "Internals.h"
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/Sema/SemaDiagnostic.h"
  17. using namespace clang;
  18. using namespace arcmt;
  19. using namespace trans;
  20. namespace {
  21. class LocalRefsCollector : public RecursiveASTVisitor<LocalRefsCollector> {
  22. SmallVectorImpl<DeclRefExpr *> &Refs;
  23. public:
  24. LocalRefsCollector(SmallVectorImpl<DeclRefExpr *> &refs)
  25. : Refs(refs) { }
  26. bool VisitDeclRefExpr(DeclRefExpr *E) {
  27. if (ValueDecl *D = E->getDecl())
  28. if (D->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
  29. Refs.push_back(E);
  30. return true;
  31. }
  32. };
  33. struct CaseInfo {
  34. SwitchCase *SC;
  35. SourceRange Range;
  36. enum {
  37. St_Unchecked,
  38. St_CannotFix,
  39. St_Fixed
  40. } State;
  41. CaseInfo() : SC(nullptr), State(St_Unchecked) {}
  42. CaseInfo(SwitchCase *S, SourceRange Range)
  43. : SC(S), Range(Range), State(St_Unchecked) {}
  44. };
  45. class CaseCollector : public RecursiveASTVisitor<CaseCollector> {
  46. ParentMap &PMap;
  47. SmallVectorImpl<CaseInfo> &Cases;
  48. public:
  49. CaseCollector(ParentMap &PMap, SmallVectorImpl<CaseInfo> &Cases)
  50. : PMap(PMap), Cases(Cases) { }
  51. bool VisitSwitchStmt(SwitchStmt *S) {
  52. SwitchCase *Curr = S->getSwitchCaseList();
  53. if (!Curr)
  54. return true;
  55. Stmt *Parent = getCaseParent(Curr);
  56. Curr = Curr->getNextSwitchCase();
  57. // Make sure all case statements are in the same scope.
  58. while (Curr) {
  59. if (getCaseParent(Curr) != Parent)
  60. return true;
  61. Curr = Curr->getNextSwitchCase();
  62. }
  63. SourceLocation NextLoc = S->getEndLoc();
  64. Curr = S->getSwitchCaseList();
  65. // We iterate over case statements in reverse source-order.
  66. while (Curr) {
  67. Cases.push_back(
  68. CaseInfo(Curr, SourceRange(Curr->getBeginLoc(), NextLoc)));
  69. NextLoc = Curr->getBeginLoc();
  70. Curr = Curr->getNextSwitchCase();
  71. }
  72. return true;
  73. }
  74. Stmt *getCaseParent(SwitchCase *S) {
  75. Stmt *Parent = PMap.getParent(S);
  76. while (Parent && (isa<SwitchCase>(Parent) || isa<LabelStmt>(Parent)))
  77. Parent = PMap.getParent(Parent);
  78. return Parent;
  79. }
  80. };
  81. class ProtectedScopeFixer {
  82. MigrationPass &Pass;
  83. SourceManager &SM;
  84. SmallVector<CaseInfo, 16> Cases;
  85. SmallVector<DeclRefExpr *, 16> LocalRefs;
  86. public:
  87. ProtectedScopeFixer(BodyContext &BodyCtx)
  88. : Pass(BodyCtx.getMigrationContext().Pass),
  89. SM(Pass.Ctx.getSourceManager()) {
  90. CaseCollector(BodyCtx.getParentMap(), Cases)
  91. .TraverseStmt(BodyCtx.getTopStmt());
  92. LocalRefsCollector(LocalRefs).TraverseStmt(BodyCtx.getTopStmt());
  93. SourceRange BodyRange = BodyCtx.getTopStmt()->getSourceRange();
  94. const CapturedDiagList &DiagList = Pass.getDiags();
  95. // Copy the diagnostics so we don't have to worry about invaliding iterators
  96. // from the diagnostic list.
  97. SmallVector<StoredDiagnostic, 16> StoredDiags;
  98. StoredDiags.append(DiagList.begin(), DiagList.end());
  99. SmallVectorImpl<StoredDiagnostic>::iterator
  100. I = StoredDiags.begin(), E = StoredDiags.end();
  101. while (I != E) {
  102. if (I->getID() == diag::err_switch_into_protected_scope &&
  103. isInRange(I->getLocation(), BodyRange)) {
  104. handleProtectedScopeError(I, E);
  105. continue;
  106. }
  107. ++I;
  108. }
  109. }
  110. void handleProtectedScopeError(
  111. SmallVectorImpl<StoredDiagnostic>::iterator &DiagI,
  112. SmallVectorImpl<StoredDiagnostic>::iterator DiagE){
  113. Transaction Trans(Pass.TA);
  114. assert(DiagI->getID() == diag::err_switch_into_protected_scope);
  115. SourceLocation ErrLoc = DiagI->getLocation();
  116. bool handledAllNotes = true;
  117. ++DiagI;
  118. for (; DiagI != DiagE && DiagI->getLevel() == DiagnosticsEngine::Note;
  119. ++DiagI) {
  120. if (!handleProtectedNote(*DiagI))
  121. handledAllNotes = false;
  122. }
  123. if (handledAllNotes)
  124. Pass.TA.clearDiagnostic(diag::err_switch_into_protected_scope, ErrLoc);
  125. }
  126. bool handleProtectedNote(const StoredDiagnostic &Diag) {
  127. assert(Diag.getLevel() == DiagnosticsEngine::Note);
  128. for (unsigned i = 0; i != Cases.size(); i++) {
  129. CaseInfo &info = Cases[i];
  130. if (isInRange(Diag.getLocation(), info.Range)) {
  131. if (info.State == CaseInfo::St_Unchecked)
  132. tryFixing(info);
  133. assert(info.State != CaseInfo::St_Unchecked);
  134. if (info.State == CaseInfo::St_Fixed) {
  135. Pass.TA.clearDiagnostic(Diag.getID(), Diag.getLocation());
  136. return true;
  137. }
  138. return false;
  139. }
  140. }
  141. return false;
  142. }
  143. void tryFixing(CaseInfo &info) {
  144. assert(info.State == CaseInfo::St_Unchecked);
  145. if (hasVarReferencedOutside(info)) {
  146. info.State = CaseInfo::St_CannotFix;
  147. return;
  148. }
  149. Pass.TA.insertAfterToken(info.SC->getColonLoc(), " {");
  150. Pass.TA.insert(info.Range.getEnd(), "}\n");
  151. info.State = CaseInfo::St_Fixed;
  152. }
  153. bool hasVarReferencedOutside(CaseInfo &info) {
  154. for (unsigned i = 0, e = LocalRefs.size(); i != e; ++i) {
  155. DeclRefExpr *DRE = LocalRefs[i];
  156. if (isInRange(DRE->getDecl()->getLocation(), info.Range) &&
  157. !isInRange(DRE->getLocation(), info.Range))
  158. return true;
  159. }
  160. return false;
  161. }
  162. bool isInRange(SourceLocation Loc, SourceRange R) {
  163. if (Loc.isInvalid())
  164. return false;
  165. return !SM.isBeforeInTranslationUnit(Loc, R.getBegin()) &&
  166. SM.isBeforeInTranslationUnit(Loc, R.getEnd());
  167. }
  168. };
  169. } // anonymous namespace
  170. void ProtectedScopeTraverser::traverseBody(BodyContext &BodyCtx) {
  171. ProtectedScopeFixer Fix(BodyCtx);
  172. }