Merge remote-tracking branch 'origin/swift-3.0-branch' into stable

* origin/swift-3.0-branch:
  Fix __builtin_os_log_format to handles specifiers that don't consume arguments
diff --git a/lib/Analysis/OSLog.cpp b/lib/Analysis/OSLog.cpp
index b6e9c49..70e45e9 100644
--- a/lib/Analysis/OSLog.cpp
+++ b/lib/Analysis/OSLog.cpp
@@ -55,6 +55,9 @@
     //  * "%.16P" len (non-arg), pointer to data
     //  * "%@" pointer to objc object
 
+    if (!FS.consumesDataArgument())
+      return false;
+
     unsigned argIndex = FS.getArgIndex();
     if (argIndex >= Args.size()) {
       return false;
diff --git a/test/CodeGen/builtins.c b/test/CodeGen/builtins.c
index f0ab4f4..62f9a74 100644
--- a/test/CodeGen/builtins.c
+++ b/test/CodeGen/builtins.c
@@ -318,4 +318,29 @@
   __builtin_os_log_format(buf, "%d %{public}s %{private}.16P", i, data, data);
 }
 
+// Check that the %% which does not consume any argument is correctly handled
+void test_builtin_os_log_percent(void *buf, const char *data) {
+  volatile int len;
+  // CHECK: store i8* [[BUF]], i8** [[BUF_ADDR:%.*]], align 8
+  // CHECK: store i8* [[DATA]], i8** [[DATA_ADDR:%.*]], align 8
+  // CHECK: store volatile i32 12
+  len = __builtin_os_log_format_buffer_size("%s %%", data);
+
+  // CHECK: [[BUF2:%.*]] = load i8*, i8** [[BUF_ADDR]]
+  // CHECK: [[SUMMARY:%.*]] = getelementptr i8, i8* [[BUF2]], i64 0
+  // CHECK: store i8 2, i8* [[SUMMARY]]
+  // CHECK: [[NUM_ARGS:%.*]] = getelementptr i8, i8* [[BUF2]], i64 1
+  // CHECK: store i8 1, i8* [[NUM_ARGS]]
+  //
+  // CHECK: [[ARG1_DESC:%.*]] = getelementptr i8, i8* [[BUF2]], i64 2
+  // CHECK: store i8 32, i8* [[ARG1_DESC]]
+  // CHECK: [[ARG1_SIZE:%.*]] = getelementptr i8, i8* [[BUF2]], i64 3
+  // CHECK: store i8 8, i8* [[ARG1_SIZE]]
+  // CHECK: [[ARG1:%.*]] = getelementptr i8, i8* [[BUF2]], i64 4
+  // CHECK: [[ARG1_PTR:%.*]] = bitcast i8* [[ARG1]] to i8**
+  // CHECK: [[DATA2:%.*]] = load i8*, i8** [[DATA_ADDR]]
+  // CHECK: store i8* [[DATA2]], i8** [[ARG1_PTR]]
+  __builtin_os_log_format(buf, "%s %%", data);
+}
+
 #endif