TransProtectedScope.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //===--- TransProtectedScope.cpp - Transformations to ARC mode ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Adds brackets in case statements that "contain" initialization of retaining
  11. // variable, thus emitting the "switch case is in protected scope" error.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "Transforms.h"
  15. #include "Internals.h"
  16. #include "clang/AST/ASTContext.h"
  17. #include "clang/Sema/SemaDiagnostic.h"
  18. using namespace clang;
  19. using namespace arcmt;
  20. using namespace trans;
  21. namespace {
  22. class LocalRefsCollector : public RecursiveASTVisitor<LocalRefsCollector> {
  23. SmallVectorImpl<DeclRefExpr *> &Refs;
  24. public:
  25. LocalRefsCollector(SmallVectorImpl<DeclRefExpr *> &refs)
  26. : Refs(refs) { }
  27. bool VisitDeclRefExpr(DeclRefExpr *E) {
  28. if (ValueDecl *D = E->getDecl())
  29. if (D->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
  30. Refs.push_back(E);
  31. return true;
  32. }
  33. };
  34. struct CaseInfo {
  35. SwitchCase *SC;
  36. SourceRange Range;
  37. enum {
  38. St_Unchecked,
  39. St_CannotFix,
  40. St_Fixed
  41. } State;
  42. CaseInfo() : SC(nullptr), State(St_Unchecked) {}
  43. CaseInfo(SwitchCase *S, SourceRange Range)
  44. : SC(S), Range(Range), State(St_Unchecked) {}
  45. };
  46. class CaseCollector : public RecursiveASTVisitor<CaseCollector> {
  47. ParentMap &PMap;
  48. SmallVectorImpl<CaseInfo> &Cases;
  49. public:
  50. CaseCollector(ParentMap &PMap, SmallVectorImpl<CaseInfo> &Cases)
  51. : PMap(PMap), Cases(Cases) { }
  52. bool VisitSwitchStmt(SwitchStmt *S) {
  53. SwitchCase *Curr = S->getSwitchCaseList();
  54. if (!Curr)
  55. return true;
  56. Stmt *Parent = getCaseParent(Curr);
  57. Curr = Curr->getNextSwitchCase();
  58. // Make sure all case statements are in the same scope.
  59. while (Curr) {
  60. if (getCaseParent(Curr) != Parent)
  61. return true;
  62. Curr = Curr->getNextSwitchCase();
  63. }
  64. SourceLocation NextLoc = S->getLocEnd();
  65. Curr = S->getSwitchCaseList();
  66. // We iterate over case statements in reverse source-order.
  67. while (Curr) {
  68. Cases.push_back(CaseInfo(Curr,SourceRange(Curr->getLocStart(), NextLoc)));
  69. NextLoc = Curr->getLocStart();
  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. }