[Sema] Avoid duplicate -Wunguarded-availability warnings in nested functions

rdar://31862310

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@303170 91177308-0d34-0410-b5e6-96231b3b80d8
(cherry picked from commit f1eb6badafec640a73d6bada228f7bbc74a4c22c)
diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp
index 02ce763..7d4f126 100644
--- a/lib/Sema/SemaDeclAttr.cpp
+++ b/lib/Sema/SemaDeclAttr.cpp
@@ -7515,6 +7515,13 @@
         SemaRef.Context.getTargetInfo().getPlatformMinVersion());
   }
 
+  bool TraverseDecl(Decl *D) {
+    // Avoid visiting nested functions to prevent duplicate warnings.
+    if (!D || isa<FunctionDecl>(D))
+      return true;
+    return Base::TraverseDecl(D);
+  }
+
   bool TraverseStmt(Stmt *S) {
     if (!S)
       return true;
@@ -7528,6 +7535,8 @@
 
   bool TraverseIfStmt(IfStmt *If);
 
+  bool TraverseLambdaExpr(LambdaExpr *E) { return true; }
+
   bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
     if (ObjCMethodDecl *D = Msg->getMethodDecl())
       DiagnoseDeclAvailability(
diff --git a/test/SemaObjC/unguarded-availability.m b/test/SemaObjC/unguarded-availability.m
index a83b839..071a21e 100644
--- a/test/SemaObjC/unguarded-availability.m
+++ b/test/SemaObjC/unguarded-availability.m
@@ -8,7 +8,7 @@
 int func_10_11() AVAILABLE_10_11; // expected-note 4 {{'func_10_11' has been explicitly marked partial here}}
 
 #ifdef OBJCPP
-// expected-note@+2 2 {{marked partial here}}
+// expected-note@+2 6 {{marked partial here}}
 #endif
 int func_10_12() AVAILABLE_10_12; // expected-note 6 {{'func_10_12' has been explicitly marked partial here}}
 
@@ -188,4 +188,19 @@
     func_10_12();
 };
 
+void functionInFunction() {
+  func_10_12(); // expected-warning{{'func_10_12' is only available on macOS 10.12 or newer}} expected-note{{@available}}
+  struct DontWarnTwice {
+    void f() {
+      func_10_12(); // expected-warning{{'func_10_12' is only available on macOS 10.12 or newer}} expected-note{{@available}}
+    }
+  };
+  void([] () {
+    func_10_12(); // expected-warning{{'func_10_12' is only available on macOS 10.12 or newer}} expected-note{{@available}}
+  });
+  (void)(^ {
+    func_10_12(); // expected-warning{{'func_10_12' is only available on macOS 10.12 or newer}} expected-note{{@available}}
+  });
+}
+
 #endif