Support lazy stat'ing of files referenced by module maps.

This patch adds support for a `header` declaration in a module map to specify
certain `stat` information (currently, size and mtime) about that header file.
This has two purposes:

- It removes the need to eagerly `stat` every file referenced by a module map.
  Instead, we track a list of unresolved header files with each size / mtime
  (actually, for simplicity, we track submodules with such headers), and when
  attempting to look up a header file based on a `FileEntry`, we check if there
  are any unresolved header directives with that `FileEntry`'s size / mtime and
  perform deferred `stat`s if so.

- It permits a preprocessed module to be compiled without the original files
  being present on disk. The only reason we used to need those files was to get
  the `stat` information in order to do header -> module lookups when using the
  module. If we're provided with the `stat` information in the preprocessed
  module, we can avoid requiring the files to exist.

Unlike most `header` directives, if a `header` directive with `stat`
information has no corresponding on-disk file the enclosing module is *not*
marked unavailable (so that behavior is consistent regardless of whether we've
resolved a header directive, and so that preprocessed modules don't get marked
unavailable). We could actually do this for all `header` directives: the only
reason we mark the module unavailable if headers are missing is to give a
diagnostic slightly earlier (rather than waiting until we actually try to build
the module / load and validate its .pcm file).

Differential Revision: https://reviews.llvm.org/D33703

llvm-svn: 304515
diff --git a/clang/test/Modules/Inputs/header-attribs/bar.h b/clang/test/Modules/Inputs/header-attribs/bar.h
new file mode 100644
index 0000000..796b4eb
--- /dev/null
+++ b/clang/test/Modules/Inputs/header-attribs/bar.h
@@ -0,0 +1 @@
+extern int b;
diff --git a/clang/test/Modules/Inputs/header-attribs/baz.h b/clang/test/Modules/Inputs/header-attribs/baz.h
new file mode 100644
index 0000000..566a522
--- /dev/null
+++ b/clang/test/Modules/Inputs/header-attribs/baz.h
@@ -0,0 +1 @@
+extern int c;
diff --git a/clang/test/Modules/Inputs/header-attribs/foo.h b/clang/test/Modules/Inputs/header-attribs/foo.h
new file mode 100644
index 0000000..a54d4a7
--- /dev/null
+++ b/clang/test/Modules/Inputs/header-attribs/foo.h
@@ -0,0 +1 @@
+extern int a;
\ No newline at end of file
diff --git a/clang/test/Modules/Inputs/header-attribs/modular.modulemap b/clang/test/Modules/Inputs/header-attribs/modular.modulemap
new file mode 100644
index 0000000..945d715
--- /dev/null
+++ b/clang/test/Modules/Inputs/header-attribs/modular.modulemap
@@ -0,0 +1,5 @@
+module A {
+  header "foo.h" { size 13 }
+  header "bar.h" { size 1000 }
+  header "baz.h" { mtime 1 }
+}
diff --git a/clang/test/Modules/Inputs/header-attribs/textual.modulemap b/clang/test/Modules/Inputs/header-attribs/textual.modulemap
new file mode 100644
index 0000000..b73f3698
--- /dev/null
+++ b/clang/test/Modules/Inputs/header-attribs/textual.modulemap
@@ -0,0 +1,5 @@
+module A {
+  textual header "foo.h" { size 13 }
+  textual header "bar.h" { size 1000 }
+  textual header "baz.h" { mtime 1 }
+}
diff --git a/clang/test/Modules/diagnostics.modulemap b/clang/test/Modules/diagnostics.modulemap
index 3b5b6a5..865892b 100644
--- a/clang/test/Modules/diagnostics.modulemap
+++ b/clang/test/Modules/diagnostics.modulemap
@@ -1,4 +1,4 @@
-// RUN: not %clang_cc1 -fmodules -fmodules-cache-path=%t -fmodule-map-file=%S/Inputs/diagnostics-aux.modulemap -fmodule-map-file=%s -fsyntax-only -x c++ /dev/null 2>&1 | FileCheck %s
+// RUN: not %clang_cc1 -fmodules -fmodules-cache-path=%t -fmodule-map-file=%S/Inputs/diagnostics-aux.modulemap -fmodule-map-file=%s -fsyntax-only -x c++ /dev/null 2>&1 | FileCheck %s --implicit-check-not error:
 
 // CHECK: In file included from {{.*}}diagnostics-aux.modulemap:3:
 // CHECK: diagnostics-aux-2.modulemap:2:3: error: expected
@@ -15,3 +15,15 @@
   // CHECK: diagnostics.modulemap:[[@LINE+1]]:22: error: use declarations are only allowed in top-level modules
   module submodule { use foo }
 }
+
+module header_attr {
+  // CHECK: diagnostics.modulemap:[[@LINE+1]]:20: error: expected a header attribute name
+  header "foo.h" { x }
+  // CHECK: diagnostics.modulemap:[[@LINE+1]]:27: error: header attribute 'size' specified multiple times
+  header "bar.h" { size 1 size 2 }
+  // CHECK: diagnostics.modulemap:[[@LINE+1]]:25: error: expected integer literal as value for header attribute 'size'
+  header "baz.h" { size "30 kilobytes" }
+
+  header "quux.h" { size 1 mtime 2 }
+  header "no_attrs.h" {}
+}
diff --git a/clang/test/Modules/header-attribs.cpp b/clang/test/Modules/header-attribs.cpp
new file mode 100644
index 0000000..292a945
--- /dev/null
+++ b/clang/test/Modules/header-attribs.cpp
@@ -0,0 +1,10 @@
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -fmodules -I%S/Inputs/header-attribs -fmodule-map-file=%S/Inputs/header-attribs/textual.modulemap -fmodules-cache-path=%t -verify %s -fmodule-name=A -fmodules-strict-decluse
+// RUN: not %clang_cc1 -fmodules -I%S/Inputs/header-attribs -emit-module -x c++-module-map %S/Inputs/header-attribs/modular.modulemap -fmodules-cache-path=%t -fmodule-name=A 2>&1 | FileCheck %s --check-prefix BUILD-MODULAR
+
+#include "foo.h" // ok, stats match
+#include "bar.h" // expected-error {{does not depend on a module exporting 'bar.h'}}
+#include "baz.h" // expected-error {{does not depend on a module exporting 'baz.h'}}
+
+// FIXME: Explain why the 'bar.h' found on disk doesn't match the module map.
+// BUILD-MODULAR: error: header 'bar.h' not found
diff --git a/clang/test/Modules/preprocess-missing.modulemap b/clang/test/Modules/preprocess-missing.modulemap
new file mode 100644
index 0000000..3004946
--- /dev/null
+++ b/clang/test/Modules/preprocess-missing.modulemap
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -fmodules -fmodule-name=A -x c++-module-map %s -emit-module -o /dev/null -verify
+module A {
+  header "does not exist" { size 12345 } // ok, do not need mtime for explicit module build
+  header "also does not exist" { mtime 12345 }
+}
+#pragma clang module contents
+// expected-error@4 {{cannot emit module A: size must be explicitly specified for missing header file "also does not exist"}}
diff --git a/clang/test/Modules/preprocess-module.cpp b/clang/test/Modules/preprocess-module.cpp
index 9d1a2bb..1e9458e 100644
--- a/clang/test/Modules/preprocess-module.cpp
+++ b/clang/test/Modules/preprocess-module.cpp
@@ -28,12 +28,21 @@
 // RUN: %clang_cc1 -fmodules -fmodule-file=%t/no-rewrite.pcm %s -I%t -verify -fno-modules-error-recovery -DINCLUDE -I%S/Inputs/preprocess
 // RUN: %clang_cc1 -fmodules -fmodule-file=%t/rewrite.pcm %s -I%t -verify -fno-modules-error-recovery -DREWRITE -DINCLUDE -I%S/Inputs/preprocess
 
+// Now try building the module when the header files are missing.
+// RUN: cp %S/Inputs/preprocess/fwd.h %S/Inputs/preprocess/file.h %S/Inputs/preprocess/file2.h %S/Inputs/preprocess/module.modulemap %t
+// RUN: %clang_cc1 -fmodules -fmodule-name=file -fmodule-file=%t/fwd.pcm -I%t -x c++-module-map %t/module.modulemap -E -frewrite-includes -o %t/copy.ii
+// RUN: rm %t/fwd.h %t/file.h %t/file2.h %t/module.modulemap
+// RUN: %clang_cc1 -fmodules -fmodule-name=file -fmodule-file=%t/fwd.pcm -x c++-module-map-cpp-output %t/copy.ii -emit-module -o %t/copy.pcm
+
+// Finally, check that our module contains correct mapping information for the headers.
+// RUN: cp %S/Inputs/preprocess/fwd.h %S/Inputs/preprocess/file.h %S/Inputs/preprocess/file2.h %S/Inputs/preprocess/module.modulemap %t
+// RUN: %clang_cc1 -fmodules -fmodule-file=%t/copy.pcm %s -I%t -verify -fno-modules-error-recovery -DCOPY -DINCLUDE
 
 // == module map
 // CHECK: # 1 "{{.*}}module.modulemap"
 // CHECK: module file {
-// CHECK:   header "file.h"
-// CHECK:   header "file2.h"
+// CHECK:   header "file.h" { size
+// CHECK:   header "file2.h" { size
 // CHECK: }
 
 // == file.h
@@ -98,6 +107,8 @@
 __FILE *a; // expected-error {{declaration of '__FILE' must be imported}}
 #ifdef REWRITE
 // expected-note@rewrite.ii:1 {{here}}
+#elif COPY
+// expected-note@copy.ii:1 {{here}}
 #else
 // expected-note@no-rewrite.ii:1 {{here}}
 #endif