[PATCH] D33250: [Sema][ObjC] Fix a bug where -Wunguarded-availability was emitted at the wrong location

Erik Pilkington via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 16 11:17:40 PDT 2017


erik.pilkington created this revision.

Previously, we used TypeLocs to find the correct SourceLocations to emit -Wunguarded-availability. Unfortunately, TypeLocs can't be trusted as they sometimes have an an empty SourceLocation component. This new patch maintains the enclosing SourceLocation to be used instead.
Thanks for taking a look,
Erik


https://reviews.llvm.org/D33250

Files:
  lib/Sema/SemaDeclAttr.cpp
  test/SemaObjC/unguarded-availability.m


Index: test/SemaObjC/unguarded-availability.m
===================================================================
--- test/SemaObjC/unguarded-availability.m
+++ test/SemaObjC/unguarded-availability.m
@@ -135,6 +135,17 @@
     func_10_12();
 };
 
+AVAILABLE_10_12
+__attribute__((objc_root_class))
+ at interface InterWithProp // expected-note 2 {{marked partial here}}
+ at property int x;
+ at end
+
+void test_property(void) {
+  int y = InterWithProp.x; // expected-warning{{'InterWithProp' is only available on macOS 10.12 or newer}} expected-note{{@available}}
+  InterWithProp.x = y; // expected-warning{{'InterWithProp' is only available on macOS 10.12 or newer}} expected-note{{@available}}
+}
+
 #ifdef OBJCPP
 
 int f(char) AVAILABLE_10_12;
Index: lib/Sema/SemaDeclAttr.cpp
===================================================================
--- lib/Sema/SemaDeclAttr.cpp
+++ lib/Sema/SemaDeclAttr.cpp
@@ -7221,6 +7221,8 @@
   SmallVector<VersionTuple, 8> AvailabilityStack;
   SmallVector<const Stmt *, 16> StmtStack;
 
+  SourceRange CurrentRange;
+
   void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range);
 
 public:
@@ -7234,7 +7236,10 @@
     if (!S)
       return true;
     StmtStack.push_back(S);
+    SourceRange LastRange(S->getLocStart(), S->getLocEnd());
+    std::swap(LastRange, CurrentRange);
     bool Result = Base::TraverseStmt(S);
+    std::swap(LastRange, CurrentRange);
     StmtStack.pop_back();
     return Result;
   }
@@ -7370,21 +7375,18 @@
 
 bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
   const Type *TyPtr = Ty.getTypePtr();
-  SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()};
-
   if (const TagType *TT = dyn_cast<TagType>(TyPtr)) {
     TagDecl *TD = TT->getDecl();
-    DiagnoseDeclAvailability(TD, Range);
+    DiagnoseDeclAvailability(TD, CurrentRange);
 
   } else if (const TypedefType *TD = dyn_cast<TypedefType>(TyPtr)) {
     TypedefNameDecl *D = TD->getDecl();
-    DiagnoseDeclAvailability(D, Range);
+    DiagnoseDeclAvailability(D, CurrentRange);
 
   } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
     if (NamedDecl *D = ObjCO->getInterface())
-      DiagnoseDeclAvailability(D, Range);
+      DiagnoseDeclAvailability(D, CurrentRange);
   }
-
   return true;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33250.99168.patch
Type: text/x-patch
Size: 2263 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170516/7d245d2e/attachment.bin>


More information about the cfe-commits mailing list