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))
+@interface InterWithProp // expected-note 2 {{marked partial here}}
+@property int x;
+@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;
 }
 


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))
+@interface InterWithProp // expected-note 2 {{marked partial here}}
+@property int x;
+@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;
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to