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

* origin/swift-3.1-branch:
  Updated ASTMerge/macro.m to use _Nullable.
  When the ASTImporter imports a source location, it avoids importing macro expansions by calling getSpellingLoc(). That's great in most cases, but for macros defined in the '<built-in>' source file, the source file is invalid and does not import correctly, causing an assertion failure (the assertion is Invalid SLocOffset or bad function choice).
diff --git a/lib/AST/ASTImporter.cpp b/lib/AST/ASTImporter.cpp
index 7c0a87d..5ac10af 100644
--- a/lib/AST/ASTImporter.cpp
+++ b/lib/AST/ASTImporter.cpp
@@ -6403,10 +6403,10 @@
 
   SourceManager &FromSM = FromContext.getSourceManager();
   
-  // For now, map everything down to its spelling location, so that we
+  // For now, map everything down to its file location, so that we
   // don't have to import macro expansions.
   // FIXME: Import macro expansions!
-  FromLoc = FromSM.getSpellingLoc(FromLoc);
+  FromLoc = FromSM.getFileLoc(FromLoc);
   std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
   SourceManager &ToSM = ToContext.getSourceManager();
   FileID ToFileID = Import(Decomposed.first);
diff --git a/test/ASTMerge/Inputs/macro.modulemap b/test/ASTMerge/Inputs/macro.modulemap
new file mode 100644
index 0000000..dba1f22
--- /dev/null
+++ b/test/ASTMerge/Inputs/macro.modulemap
@@ -0,0 +1,4 @@
+module macro1 [extern_c] {
+  header "macro1.h"
+  export *
+}
diff --git a/test/ASTMerge/Inputs/macro1.h b/test/ASTMerge/Inputs/macro1.h
new file mode 100644
index 0000000..9613394
--- /dev/null
+++ b/test/ASTMerge/Inputs/macro1.h
@@ -0,0 +1,5 @@
+typedef void *VoidRef;
+
+void maybeNull(
+  int i,
+  _Nullable VoidRef *_Nullable);
diff --git a/test/ASTMerge/Inputs/macro1.m b/test/ASTMerge/Inputs/macro1.m
new file mode 100644
index 0000000..2612613
--- /dev/null
+++ b/test/ASTMerge/Inputs/macro1.m
@@ -0,0 +1,5 @@
+@import macro1;
+
+void foo() {
+  maybeNull(0, 0);
+}
diff --git a/test/ASTMerge/Inputs/macro2.m b/test/ASTMerge/Inputs/macro2.m
new file mode 100644
index 0000000..b5b155a
--- /dev/null
+++ b/test/ASTMerge/Inputs/macro2.m
@@ -0,0 +1,5 @@
+void foo();
+
+void bar() {
+  foo();
+}
diff --git a/test/ASTMerge/macro.m b/test/ASTMerge/macro.m
new file mode 100644
index 0000000..77e596d
--- /dev/null
+++ b/test/ASTMerge/macro.m
@@ -0,0 +1,6 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t/cache
+// RUN: %clang_cc1 -fmodules -fmodules-cache-path=%t/cache -fmodule-map-file=%S/Inputs/macro.modulemap -I%S/Inputs -emit-pch -o %t.1.ast %S/Inputs/macro1.m
+// RUN: %clang_cc1 -fmodules -fmodules-cache-path=%t/cache -fmodule-map-file=%S/Inputs/macro.modulemap -I%S/Inputs -emit-pch -o %t.2.ast %S/Inputs/macro2.m
+// RUN: %clang_cc1 -fmodules -ast-merge %t.1.ast -ast-merge %t.2.ast -fsyntax-only -verify %s
+// expected-no-diagnostics