[libc++] Add a new FILE_DEPENDENCIES parser

Instead of considering all the .dat files to be dependencies of a test,
only consider those that are listed in FILE_DEPENDENCIES.

Cr-Mirrored-From: https://chromium.googlesource.com/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: 7efbd851adf3124f7e756c37348ddd3ff7427ad7
diff --git a/test/libcxx/input.output/file.streams/fstreams/ifstream.cons/wchar_pointer.pass.cpp b/test/libcxx/input.output/file.streams/fstreams/ifstream.cons/wchar_pointer.pass.cpp
index 9dd79a1..1f6956e 100644
--- a/test/libcxx/input.output/file.streams/fstreams/ifstream.cons/wchar_pointer.pass.cpp
+++ b/test/libcxx/input.output/file.streams/fstreams/ifstream.cons/wchar_pointer.pass.cpp
@@ -13,6 +13,8 @@
 
 // explicit basic_ifstream(const wchar_t* s, ios_base::openmode mode = ios_base::in);
 
+// FILE_DEPENDENCIES: test.dat
+
 #include <fstream>
 #include <cassert>
 
diff --git a/test/libcxx/input.output/file.streams/fstreams/ifstream.members/open_wchar_pointer.pass.cpp b/test/libcxx/input.output/file.streams/fstreams/ifstream.members/open_wchar_pointer.pass.cpp
index 226cf8d..7d91b6d 100644
--- a/test/libcxx/input.output/file.streams/fstreams/ifstream.members/open_wchar_pointer.pass.cpp
+++ b/test/libcxx/input.output/file.streams/fstreams/ifstream.members/open_wchar_pointer.pass.cpp
@@ -13,6 +13,8 @@
 
 // void open(const wchar_t* s, ios_base::openmode mode = ios_base::in);
 
+// FILE_DEPENDENCIES: test.dat
+
 #include <fstream>
 #include <cassert>
 
diff --git a/test/std/input.output/file.streams/fstreams/filebuf.virtuals/seekoff.pass.cpp b/test/std/input.output/file.streams/fstreams/filebuf.virtuals/seekoff.pass.cpp
index 6f50357..f9e8938 100644
--- a/test/std/input.output/file.streams/fstreams/filebuf.virtuals/seekoff.pass.cpp
+++ b/test/std/input.output/file.streams/fstreams/filebuf.virtuals/seekoff.pass.cpp
@@ -13,6 +13,8 @@
 // pos_type seekpos(pos_type sp,
 //                  ios_base::openmode which = ios_base::in | ios_base::out);
 
+// FILE_DEPENDENCIES: underflow.dat
+
 #include <fstream>
 #include <cassert>
 
diff --git a/test/std/input.output/file.streams/fstreams/filebuf.virtuals/underflow.pass.cpp b/test/std/input.output/file.streams/fstreams/filebuf.virtuals/underflow.pass.cpp
index 1e3029a..0d1f7a3 100644
--- a/test/std/input.output/file.streams/fstreams/filebuf.virtuals/underflow.pass.cpp
+++ b/test/std/input.output/file.streams/fstreams/filebuf.virtuals/underflow.pass.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 // REQUIRES: locale.en_US.UTF-8
+// FILE_DEPENDENCIES: underflow.dat, underflow_utf8.dat
 
 // <fstream>
 
diff --git a/test/std/input.output/file.streams/fstreams/ifstream.assign/member_swap.pass.cpp b/test/std/input.output/file.streams/fstreams/ifstream.assign/member_swap.pass.cpp
index 7832464..7a5d9c1 100644
--- a/test/std/input.output/file.streams/fstreams/ifstream.assign/member_swap.pass.cpp
+++ b/test/std/input.output/file.streams/fstreams/ifstream.assign/member_swap.pass.cpp
@@ -6,6 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+// FILE_DEPENDENCIES: test.dat, test2.dat
+
 // <fstream>
 
 // template <class charT, class traits = char_traits<charT> >
diff --git a/test/std/input.output/file.streams/fstreams/ifstream.assign/move_assign.pass.cpp b/test/std/input.output/file.streams/fstreams/ifstream.assign/move_assign.pass.cpp
index ec50ec6..115703c 100644
--- a/test/std/input.output/file.streams/fstreams/ifstream.assign/move_assign.pass.cpp
+++ b/test/std/input.output/file.streams/fstreams/ifstream.assign/move_assign.pass.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 // UNSUPPORTED: c++98, c++03
+// FILE_DEPENDENCIES: test.dat
 
 // <fstream>
 
diff --git a/test/std/input.output/file.streams/fstreams/ifstream.assign/nonmember_swap.pass.cpp b/test/std/input.output/file.streams/fstreams/ifstream.assign/nonmember_swap.pass.cpp
index 587dcc2..f04c5e4 100644
--- a/test/std/input.output/file.streams/fstreams/ifstream.assign/nonmember_swap.pass.cpp
+++ b/test/std/input.output/file.streams/fstreams/ifstream.assign/nonmember_swap.pass.cpp
@@ -6,6 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+// FILE_DEPENDENCIES: test.dat, test2.dat
+
 // <fstream>
 
 // template <class charT, class traits = char_traits<charT> >
diff --git a/test/std/input.output/file.streams/fstreams/ifstream.cons/move.pass.cpp b/test/std/input.output/file.streams/fstreams/ifstream.cons/move.pass.cpp
index ac19eea..af99878 100644
--- a/test/std/input.output/file.streams/fstreams/ifstream.cons/move.pass.cpp
+++ b/test/std/input.output/file.streams/fstreams/ifstream.cons/move.pass.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 // UNSUPPORTED: c++98, c++03
+// FILE_DEPENDENCIES: test.dat
 
 // <fstream>
 
diff --git a/test/std/input.output/file.streams/fstreams/ifstream.cons/path.pass.cpp b/test/std/input.output/file.streams/fstreams/ifstream.cons/path.pass.cpp
index 52a3670..d433067 100644
--- a/test/std/input.output/file.streams/fstreams/ifstream.cons/path.pass.cpp
+++ b/test/std/input.output/file.streams/fstreams/ifstream.cons/path.pass.cpp
@@ -8,6 +8,7 @@
 
 // UNSUPPORTED: c++98, c++03, c++11, c++14
 // XFAIL: dylib-has-no-filesystem
+// FILE_DEPENDENCIES: test.dat
 
 // <fstream>
 
diff --git a/test/std/input.output/file.streams/fstreams/ifstream.cons/pointer.pass.cpp b/test/std/input.output/file.streams/fstreams/ifstream.cons/pointer.pass.cpp
index 5776701..e667425 100644
--- a/test/std/input.output/file.streams/fstreams/ifstream.cons/pointer.pass.cpp
+++ b/test/std/input.output/file.streams/fstreams/ifstream.cons/pointer.pass.cpp
@@ -6,6 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+// FILE_DEPENDENCIES: test.dat
+
 // <fstream>
 
 // template <class charT, class traits = char_traits<charT> >
diff --git a/test/std/input.output/file.streams/fstreams/ifstream.cons/string.pass.cpp b/test/std/input.output/file.streams/fstreams/ifstream.cons/string.pass.cpp
index c1ab706..f0e9625 100644
--- a/test/std/input.output/file.streams/fstreams/ifstream.cons/string.pass.cpp
+++ b/test/std/input.output/file.streams/fstreams/ifstream.cons/string.pass.cpp
@@ -6,6 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+// FILE_DEPENDENCIES: test.dat
+
 // <fstream>
 
 // template <class charT, class traits = char_traits<charT> >
diff --git a/test/std/input.output/file.streams/fstreams/ifstream.members/close.pass.cpp b/test/std/input.output/file.streams/fstreams/ifstream.members/close.pass.cpp
index 9bd4f72..3641dfd 100644
--- a/test/std/input.output/file.streams/fstreams/ifstream.members/close.pass.cpp
+++ b/test/std/input.output/file.streams/fstreams/ifstream.members/close.pass.cpp
@@ -6,6 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+// FILE_DEPENDENCIES: test.dat
+
 // <fstream>
 
 // template <class charT, class traits = char_traits<charT> >
diff --git a/test/std/input.output/file.streams/fstreams/ifstream.members/open_path.pass.cpp b/test/std/input.output/file.streams/fstreams/ifstream.members/open_path.pass.cpp
index 9d30e8e..c348663 100644
--- a/test/std/input.output/file.streams/fstreams/ifstream.members/open_path.pass.cpp
+++ b/test/std/input.output/file.streams/fstreams/ifstream.members/open_path.pass.cpp
@@ -8,6 +8,7 @@
 
 // UNSUPPORTED: c++98, c++03, c++11, c++14
 // XFAIL: dylib-has-no-filesystem
+// FILE_DEPENDENCIES: test.dat
 
 // <fstream>
 
diff --git a/test/std/input.output/file.streams/fstreams/ifstream.members/open_pointer.pass.cpp b/test/std/input.output/file.streams/fstreams/ifstream.members/open_pointer.pass.cpp
index fd2adba..4626f96 100644
--- a/test/std/input.output/file.streams/fstreams/ifstream.members/open_pointer.pass.cpp
+++ b/test/std/input.output/file.streams/fstreams/ifstream.members/open_pointer.pass.cpp
@@ -6,6 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+// FILE_DEPENDENCIES: test.dat
+
 // <fstream>
 
 // template <class charT, class traits = char_traits<charT> >
diff --git a/test/std/input.output/file.streams/fstreams/ifstream.members/open_string.pass.cpp b/test/std/input.output/file.streams/fstreams/ifstream.members/open_string.pass.cpp
index b5fd9fc..35722d5 100644
--- a/test/std/input.output/file.streams/fstreams/ifstream.members/open_string.pass.cpp
+++ b/test/std/input.output/file.streams/fstreams/ifstream.members/open_string.pass.cpp
@@ -6,6 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+// FILE_DEPENDENCIES: test.dat
+
 // <fstream>
 
 // template <class charT, class traits = char_traits<charT> >
diff --git a/test/std/input.output/file.streams/fstreams/ifstream.members/rdbuf.pass.cpp b/test/std/input.output/file.streams/fstreams/ifstream.members/rdbuf.pass.cpp
index c09f761..aa6cbb2 100644
--- a/test/std/input.output/file.streams/fstreams/ifstream.members/rdbuf.pass.cpp
+++ b/test/std/input.output/file.streams/fstreams/ifstream.members/rdbuf.pass.cpp
@@ -6,6 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+// FILE_DEPENDENCIES: test.dat
+
 // <fstream>
 
 // template <class charT, class traits = char_traits<charT> >
diff --git a/test/std/localization/locales/locale.convenience/conversions/conversions.buffer/pbackfail.pass.cpp b/test/std/localization/locales/locale.convenience/conversions/conversions.buffer/pbackfail.pass.cpp
index d8e60c0..264a67e 100644
--- a/test/std/localization/locales/locale.convenience/conversions/conversions.buffer/pbackfail.pass.cpp
+++ b/test/std/localization/locales/locale.convenience/conversions/conversions.buffer/pbackfail.pass.cpp
@@ -6,6 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+// FILE_DEPENDENCIES: underflow.dat
+
 // <locale>
 
 // wbuffer_convert<Codecvt, Elem, Tr>
diff --git a/test/std/localization/locales/locale.convenience/conversions/conversions.buffer/underflow.pass.cpp b/test/std/localization/locales/locale.convenience/conversions/conversions.buffer/underflow.pass.cpp
index 0b25e13..6642488 100644
--- a/test/std/localization/locales/locale.convenience/conversions/conversions.buffer/underflow.pass.cpp
+++ b/test/std/localization/locales/locale.convenience/conversions/conversions.buffer/underflow.pass.cpp
@@ -6,6 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+// FILE_DEPENDENCIES: underflow.dat, underflow_utf8.dat
+
 // <locale>
 
 // wbuffer_convert<Codecvt, Elem, Tr>
diff --git a/utils/libcxx/test/format.py b/utils/libcxx/test/format.py
index 55f179a..4f7283d 100644
--- a/utils/libcxx/test/format.py
+++ b/utils/libcxx/test/format.py
@@ -41,12 +41,14 @@
         self.exec_env = dict(exec_env)
 
     @staticmethod
-    def _make_custom_parsers():
+    def _make_custom_parsers(test):
         return [
             IntegratedTestKeywordParser('FLAKY_TEST.', ParserKind.TAG,
                                         initial_value=False),
             IntegratedTestKeywordParser('MODULES_DEFINES:', ParserKind.LIST,
-                                        initial_value=[])
+                                        initial_value=[]),
+            IntegratedTestKeywordParser('FILE_DEPENDENCIES:', ParserKind.LIST,
+                                        initial_value=test.file_dependencies)
         ]
 
     @staticmethod
@@ -102,7 +104,8 @@
            'objective-c++' in test.config.available_features:
             return (lit.Test.UNSUPPORTED, "Objective-C++ is not supported")
 
-        parsers = self._make_custom_parsers()
+        setattr(test, 'file_dependencies', [])
+        parsers = self._make_custom_parsers(test)
         script = lit.TestRunner.parseIntegratedTestScript(
             test, additional_parsers=parsers, require_script=is_sh_test)
         # Check if a result for the test was returned. If so return that
@@ -193,12 +196,7 @@
             env = None
             if self.exec_env:
                 env = self.exec_env
-            # TODO: Only list actually needed files in file_deps.
-            # Right now we just mark all of the .dat files in the same
-            # directory as dependencies, but it's likely less than that. We
-            # should add a `// FILE-DEP: foo.dat` to each test to track this.
-            data_files = [os.path.join(local_cwd, f)
-                          for f in os.listdir(local_cwd) if f.endswith('.dat')]
+            data_files = [os.path.join(local_cwd, f) for f in test.file_dependencies]
             is_flaky = self._get_parser('FLAKY_TEST.', parsers).getValue()
             max_retry = 3 if is_flaky else 1
             for retry_count in range(max_retry):