Start removing #include directives from util/util.h.

Of course, this entails adding them to individual source files.

Change-Id: Iafa89975cfa736ac4c63559ec5e8ec88d4d97eb4
Reviewed-on: https://code-review.googlesource.com/5441
Reviewed-by: Paul Wankadia <junyer@google.com>
diff --git a/re2/bitstate.cc b/re2/bitstate.cc
index 775fbec..535f30d 100644
--- a/re2/bitstate.cc
+++ b/re2/bitstate.cc
@@ -17,6 +17,9 @@
 // SearchBitState is a fast replacement for the NFA code on small
 // regexps and texts when SearchOnePass cannot be used.
 
+#include <stddef.h>
+#include <string.h>
+
 #include "re2/prog.h"
 #include "re2/regexp.h"
 
diff --git a/re2/compile.cc b/re2/compile.cc
index 117679f..f6774d8 100644
--- a/re2/compile.cc
+++ b/re2/compile.cc
@@ -8,6 +8,11 @@
 // This file's external interface is just Regexp::CompileToProg.
 // The Compiler class defined in this file is private.
 
+#include <string.h>
+#include <sys/types.h>
+#include <map>
+#include <utility>
+
 #include "re2/prog.h"
 #include "re2/re2.h"
 #include "re2/regexp.h"
diff --git a/re2/dfa.cc b/re2/dfa.cc
index b0a977a..0293ca6 100644
--- a/re2/dfa.cc
+++ b/re2/dfa.cc
@@ -21,6 +21,18 @@
 //
 // See http://swtch.com/~rsc/regexp/ for a very bare-bones equivalent.
 
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <algorithm>
+#include <atomic>
+#include <map>
+#include <new>
+#include <string>
+#include <unordered_set>
+#include <vector>
+
 #include "util/flags.h"
 #include "util/sparse_set.h"
 #include "re2/prog.h"
diff --git a/re2/filtered_re2.cc b/re2/filtered_re2.cc
index 5dd65d5..5dc471a 100644
--- a/re2/filtered_re2.cc
+++ b/re2/filtered_re2.cc
@@ -2,9 +2,12 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-#include <string>
-#include "util/util.h"
 #include "re2/filtered_re2.h"
+
+#include <stddef.h>
+#include <string>
+
+#include "util/util.h"
 #include "re2/prefilter.h"
 #include "re2/prefilter_tree.h"
 
diff --git a/re2/filtered_re2.h b/re2/filtered_re2.h
index 1035a12..afcf8dd 100644
--- a/re2/filtered_re2.h
+++ b/re2/filtered_re2.h
@@ -21,7 +21,9 @@
 // indices of strings that were found in the text to get the actual
 // regexp matches.
 
+#include <string>
 #include <vector>
+
 #include "re2/re2.h"
 
 namespace re2 {
diff --git a/re2/fuzzing/re2_fuzzer.cc b/re2/fuzzing/re2_fuzzer.cc
index b4e6bb6..df2afee 100644
--- a/re2/fuzzing/re2_fuzzer.cc
+++ b/re2/fuzzing/re2_fuzzer.cc
@@ -4,7 +4,6 @@
 
 #include <stddef.h>
 #include <stdint.h>
-
 #include <map>
 #include <string>
 
diff --git a/re2/nfa.cc b/re2/nfa.cc
index 1de5aa7..bf0f330 100644
--- a/re2/nfa.cc
+++ b/re2/nfa.cc
@@ -24,6 +24,12 @@
 // Like Thompson's original machine and like the DFA implementation, this
 // implementation notices a match only once it is one byte past it.
 
+#include <stdio.h>
+#include <string.h>
+#include <algorithm>
+#include <string>
+#include <vector>
+
 #include "re2/prog.h"
 #include "re2/regexp.h"
 #include "util/sparse_array.h"
diff --git a/re2/onepass.cc b/re2/onepass.cc
index da90a86..bbef608 100644
--- a/re2/onepass.cc
+++ b/re2/onepass.cc
@@ -51,7 +51,11 @@
 // "One-unambiguous regular languages", Information and Computation 142(2).
 
 #include <string.h>
+#include <algorithm>
 #include <map>
+#include <string>
+#include <vector>
+
 #include "util/util.h"
 #include "util/sparse_set.h"
 #include "re2/prog.h"
diff --git a/re2/parse.cc b/re2/parse.cc
index 9cd9cc1..1fb3048 100644
--- a/re2/parse.cc
+++ b/re2/parse.cc
@@ -16,6 +16,13 @@
 // and recognizes the Perl escape sequences \d, \s, \w, \D, \S, and \W.
 // See regexp.h for rationale.
 
+#include <ctype.h>
+#include <stddef.h>
+#include <string.h>
+#include <algorithm>
+#include <map>
+#include <string>
+
 #include "util/util.h"
 #include "re2/regexp.h"
 #include "re2/stringpiece.h"
diff --git a/re2/prefilter.cc b/re2/prefilter.cc
index f171eec..64792a0 100644
--- a/re2/prefilter.cc
+++ b/re2/prefilter.cc
@@ -2,8 +2,14 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-#include "util/util.h"
 #include "re2/prefilter.h"
+
+#include <stddef.h>
+#include <stdint.h>
+#include <string>
+#include <vector>
+
+#include "util/util.h"
 #include "re2/re2.h"
 #include "re2/unicode_casefold.h"
 #include "re2/walker-inl.h"
diff --git a/re2/prefilter.h b/re2/prefilter.h
index e58efe8..aac06d4 100644
--- a/re2/prefilter.h
+++ b/re2/prefilter.h
@@ -9,6 +9,10 @@
 // Rather than using Prefilter class directly, use FilteredRE2.
 // See filtered_re2.h
 
+#include <set>
+#include <string>
+#include <vector>
+
 #include "util/util.h"
 
 namespace re2 {
diff --git a/re2/prefilter_tree.cc b/re2/prefilter_tree.cc
index be9b584..c20ef51 100644
--- a/re2/prefilter_tree.cc
+++ b/re2/prefilter_tree.cc
@@ -2,10 +2,21 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+#include "re2/prefilter_tree.h"
+
+#include <stddef.h>
+#include <stdio.h>
+#include <algorithm>
+#include <map>
+#include <memory>
+#include <set>
+#include <string>
+#include <utility>
+#include <vector>
+
 #include "util/util.h"
 #include "util/flags.h"
 #include "re2/prefilter.h"
-#include "re2/prefilter_tree.h"
 #include "re2/re2.h"
 
 DEFINE_int32(filtered_re2_min_atom_len,
diff --git a/re2/prefilter_tree.h b/re2/prefilter_tree.h
index a8ec589..667ff99 100644
--- a/re2/prefilter_tree.h
+++ b/re2/prefilter_tree.h
@@ -16,6 +16,10 @@
 // atoms) that the user of this class should use to do the string
 // matching.
 
+#include <map>
+#include <string>
+#include <vector>
+
 #include "util/util.h"
 #include "util/sparse_array.h"
 
diff --git a/re2/prog.cc b/re2/prog.cc
index 5d8dd6c..72a42e4 100644
--- a/re2/prog.cc
+++ b/re2/prog.cc
@@ -5,9 +5,15 @@
 // Compiled regular expression representation.
 // Tested by compile_test.cc
 
+#include "re2/prog.h"
+
+#include <string.h>
+#include <algorithm>
+#include <memory>
+#include <utility>
+
 #include "util/util.h"
 #include "util/bitmap.h"
-#include "re2/prog.h"
 #include "re2/stringpiece.h"
 
 namespace re2 {
diff --git a/re2/prog.h b/re2/prog.h
index 140093f..a017cec 100644
--- a/re2/prog.h
+++ b/re2/prog.h
@@ -9,6 +9,11 @@
 // See regexp.h for the Regexp class, which represents a regular
 // expression symbolically.
 
+#include <atomic>
+#include <mutex>
+#include <string>
+#include <vector>
+
 #include "util/util.h"
 #include "util/sparse_array.h"
 #include "util/sparse_set.h"
diff --git a/re2/re2.cc b/re2/re2.cc
index 49388c7..4f20711 100644
--- a/re2/re2.cc
+++ b/re2/re2.cc
@@ -9,9 +9,17 @@
 
 #include "re2/re2.h"
 
-#include <stdio.h>
-#include <string>
+#include <assert.h>
+#include <ctype.h>
 #include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <iterator>
+#include <mutex>
+#include <string>
+#include <utility>
+#include <vector>
+
 #include "util/util.h"
 #include "util/flags.h"
 #include "util/sparse_array.h"
diff --git a/re2/re2.h b/re2/re2.h
index cc35736..ccad381 100644
--- a/re2/re2.h
+++ b/re2/re2.h
@@ -179,10 +179,14 @@
 //         RE2::Octal(&a), RE2::Hex(&b), RE2::CRadix(&c), RE2::CRadix(&d));
 // will leave 64 in a, b, c, and d.
 
+#include <stddef.h>
 #include <stdint.h>
+#include <sys/types.h>
+#include <algorithm>
 #include <map>
 #include <mutex>
 #include <string>
+
 #include "re2/stringpiece.h"
 
 namespace re2 {
diff --git a/re2/regexp.cc b/re2/regexp.cc
index 7b1b174..df701cc 100644
--- a/re2/regexp.cc
+++ b/re2/regexp.cc
@@ -5,8 +5,17 @@
 // Regular expression representation.
 // Tested by parse_test.cc
 
-#include "util/util.h"
 #include "re2/regexp.h"
+
+#include <stddef.h>
+#include <string.h>
+#include <algorithm>
+#include <map>
+#include <mutex>
+#include <string>
+#include <vector>
+
+#include "util/util.h"
 #include "re2/stringpiece.h"
 #include "re2/walker-inl.h"
 
diff --git a/re2/regexp.h b/re2/regexp.h
index a455b5c..abcf439 100644
--- a/re2/regexp.h
+++ b/re2/regexp.h
@@ -86,6 +86,10 @@
 // form accessible to clients, so that client code can analyze the
 // parsed regular expressions.
 
+#include <map>
+#include <set>
+#include <string>
+
 #include "util/util.h"
 #include "re2/stringpiece.h"
 
diff --git a/re2/set.cc b/re2/set.cc
index 0e626a2..aa84946 100644
--- a/re2/set.cc
+++ b/re2/set.cc
@@ -4,6 +4,8 @@
 
 #include "re2/set.h"
 
+#include <stddef.h>
+
 #include "util/util.h"
 #include "re2/stringpiece.h"
 #include "re2/prog.h"
diff --git a/re2/set.h b/re2/set.h
index 6efc972..66366b2 100644
--- a/re2/set.h
+++ b/re2/set.h
@@ -5,7 +5,7 @@
 #ifndef RE2_SET_H_
 #define RE2_SET_H_
 
-#include <utility>
+#include <string>
 #include <vector>
 
 #include "re2/re2.h"
diff --git a/re2/simplify.cc b/re2/simplify.cc
index ecc60e7..f001185 100644
--- a/re2/simplify.cc
+++ b/re2/simplify.cc
@@ -6,6 +6,8 @@
 // to use simple extended regular expression features.
 // Also sort and simplify character classes.
 
+#include <string>
+
 #include "util/util.h"
 #include "re2/regexp.h"
 #include "re2/walker-inl.h"
diff --git a/re2/testing/backtrack.cc b/re2/testing/backtrack.cc
index 95c14c2..ee70465 100644
--- a/re2/testing/backtrack.cc
+++ b/re2/testing/backtrack.cc
@@ -23,6 +23,9 @@
 // not the main library, in order to make it harder to pick up
 // accidentally.
 
+#include <stddef.h>
+#include <string.h>
+
 #include "util/util.h"
 #include "re2/prog.h"
 #include "re2/regexp.h"
diff --git a/re2/testing/charclass_test.cc b/re2/testing/charclass_test.cc
index a3764d4..e3b4434 100644
--- a/re2/testing/charclass_test.cc
+++ b/re2/testing/charclass_test.cc
@@ -4,6 +4,8 @@
 
 // Test character class manipulations.
 
+#include <stdio.h>
+
 #include "util/test.h"
 #include "re2/regexp.h"
 
diff --git a/re2/testing/compile_test.cc b/re2/testing/compile_test.cc
index cd8406d..70ec813 100644
--- a/re2/testing/compile_test.cc
+++ b/re2/testing/compile_test.cc
@@ -5,7 +5,7 @@
 // Test prog.cc, compile.cc
 
 #include <string>
-#include <vector>
+
 #include "util/test.h"
 #include "re2/regexp.h"
 #include "re2/prog.h"
diff --git a/re2/testing/dfa_test.cc b/re2/testing/dfa_test.cc
index e9c7bef..18f5dbf 100644
--- a/re2/testing/dfa_test.cc
+++ b/re2/testing/dfa_test.cc
@@ -2,6 +2,9 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+#include <string>
+#include <vector>
+
 #include "util/thread.h"
 #include "util/test.h"
 #include "re2/prog.h"
diff --git a/re2/testing/dump.cc b/re2/testing/dump.cc
index 9703039..fbd1871 100644
--- a/re2/testing/dump.cc
+++ b/re2/testing/dump.cc
@@ -17,7 +17,7 @@
 // library (see BUILD).
 
 #include <string>
-#include <vector>
+
 #include "util/test.h"
 #include "re2/stringpiece.h"
 #include "re2/regexp.h"
diff --git a/re2/testing/exhaustive1_test.cc b/re2/testing/exhaustive1_test.cc
index c06a10c..7c132c5 100644
--- a/re2/testing/exhaustive1_test.cc
+++ b/re2/testing/exhaustive1_test.cc
@@ -4,6 +4,9 @@
 
 // Exhaustive testing of regular expression matching.
 
+#include <string>
+#include <vector>
+
 #include "util/test.h"
 #include "re2/testing/exhaustive_tester.h"
 
diff --git a/re2/testing/exhaustive2_test.cc b/re2/testing/exhaustive2_test.cc
index f0638b5..b76a83f 100644
--- a/re2/testing/exhaustive2_test.cc
+++ b/re2/testing/exhaustive2_test.cc
@@ -4,6 +4,11 @@
 
 // Exhaustive testing of regular expression matching.
 
+#include <stddef.h>
+#include <memory>
+#include <string>
+#include <vector>
+
 #include "util/test.h"
 #include "re2/re2.h"
 #include "re2/testing/exhaustive_tester.h"
diff --git a/re2/testing/exhaustive3_test.cc b/re2/testing/exhaustive3_test.cc
index 6e46bb4..4f28bea 100644
--- a/re2/testing/exhaustive3_test.cc
+++ b/re2/testing/exhaustive3_test.cc
@@ -4,6 +4,11 @@
 
 // Exhaustive testing of regular expression matching.
 
+#include <stddef.h>
+#include <memory>
+#include <string>
+#include <vector>
+
 #include "util/test.h"
 #include "re2/testing/exhaustive_tester.h"
 
diff --git a/re2/testing/exhaustive_tester.cc b/re2/testing/exhaustive_tester.cc
index 1608c89..d63ed18 100644
--- a/re2/testing/exhaustive_tester.cc
+++ b/re2/testing/exhaustive_tester.cc
@@ -11,7 +11,6 @@
 // the NFA, DFA, and a trivial backtracking implementation agree about
 // the location of the match.
 
-#include <stdlib.h>
 #include <stdio.h>
 
 #include "util/test.h"
diff --git a/re2/testing/exhaustive_tester.h b/re2/testing/exhaustive_tester.h
index a8f39eb..4d31497 100644
--- a/re2/testing/exhaustive_tester.h
+++ b/re2/testing/exhaustive_tester.h
@@ -7,6 +7,7 @@
 
 #include <string>
 #include <vector>
+
 #include "util/util.h"
 #include "re2/testing/regexp_generator.h"
 #include "re2/testing/string_generator.h"
diff --git a/re2/testing/filtered_re2_test.cc b/re2/testing/filtered_re2_test.cc
index 76c1284..9759637 100644
--- a/re2/testing/filtered_re2_test.cc
+++ b/re2/testing/filtered_re2_test.cc
@@ -2,6 +2,12 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+#include <stddef.h>
+#include <algorithm>
+#include <memory>
+#include <string>
+#include <vector>
+
 #include "util/test.h"
 #include "re2/filtered_re2.h"
 #include "re2/re2.h"
diff --git a/re2/testing/parse_test.cc b/re2/testing/parse_test.cc
index e204ce1..eea0f15 100644
--- a/re2/testing/parse_test.cc
+++ b/re2/testing/parse_test.cc
@@ -5,7 +5,7 @@
 // Test parse.cc, dump.cc, and tostring.cc.
 
 #include <string>
-#include <vector>
+
 #include "util/test.h"
 #include "re2/regexp.h"
 
diff --git a/re2/testing/possible_match_test.cc b/re2/testing/possible_match_test.cc
index 4687165..3a02699 100644
--- a/re2/testing/possible_match_test.cc
+++ b/re2/testing/possible_match_test.cc
@@ -2,7 +2,10 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+#include <string.h>
+#include <string>
 #include <vector>
+
 #include "util/test.h"
 #include "re2/prog.h"
 #include "re2/re2.h"
diff --git a/re2/testing/random_test.cc b/re2/testing/random_test.cc
index d67ae64..6060677 100644
--- a/re2/testing/random_test.cc
+++ b/re2/testing/random_test.cc
@@ -5,6 +5,9 @@
 // Random testing of regular expression matching.
 
 #include <stdio.h>
+#include <string>
+#include <vector>
+
 #include "util/test.h"
 #include "re2/testing/exhaustive_tester.h"
 
diff --git a/re2/testing/re2_arg_test.cc b/re2/testing/re2_arg_test.cc
index 06c58f1..a3905ba 100644
--- a/re2/testing/re2_arg_test.cc
+++ b/re2/testing/re2_arg_test.cc
@@ -7,6 +7,8 @@
 // Todo: Expand the test to validate strings parsed to the other types
 // supported by RE2::Arg class
 
+#include <string.h>
+
 #include "util/test.h"
 #include "re2/re2.h"
 
diff --git a/re2/testing/re2_test.cc b/re2/testing/re2_test.cc
index 830b3f7..7ecf0c9 100644
--- a/re2/testing/re2_test.cc
+++ b/re2/testing/re2_test.cc
@@ -5,14 +5,19 @@
 
 // TODO: Test extractions for PartialMatch/Consume
 
+#include <assert.h>
 #include <errno.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <map>
+#include <string>
+#include <utility>
 #if !defined(_MSC_VER) && !defined(__MINGW32__)
-#include <unistd.h>  /* for sysconf */
 #include <sys/mman.h>
+#include <unistd.h>  /* for sysconf */
 #endif
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <vector>
+
 #include "util/test.h"
 #include "re2/re2.h"
 #include "re2/regexp.h"
diff --git a/re2/testing/regexp_benchmark.cc b/re2/testing/regexp_benchmark.cc
index 311a60e..83b5fd7 100644
--- a/re2/testing/regexp_benchmark.cc
+++ b/re2/testing/regexp_benchmark.cc
@@ -4,6 +4,10 @@
 
 // Benchmarks for regular expression implementations.
 
+#include <stdio.h>
+#include <stdlib.h>
+#include <string>
+
 #include "util/test.h"
 #include "re2/prog.h"
 #include "re2/re2.h"
diff --git a/re2/testing/regexp_generator.cc b/re2/testing/regexp_generator.cc
index fd085db..155472d 100644
--- a/re2/testing/regexp_generator.cc
+++ b/re2/testing/regexp_generator.cc
@@ -20,10 +20,14 @@
 // Then RunPostfix turns each sequence into a regular expression
 // and passes the regexp to HandleRegexp.
 
+#include <stddef.h>
+#include <stdio.h>
 #include <string.h>
-#include <string>
+#include <memory>
 #include <stack>
+#include <string>
 #include <vector>
+
 #include "util/test.h"
 #include "re2/testing/regexp_generator.h"
 
diff --git a/re2/testing/regexp_generator.h b/re2/testing/regexp_generator.h
index 06ea4c4..218929e 100644
--- a/re2/testing/regexp_generator.h
+++ b/re2/testing/regexp_generator.h
@@ -10,6 +10,7 @@
 
 #include <string>
 #include <vector>
+
 #include "util/random.h"
 #include "util/util.h"
 #include "re2/stringpiece.h"
diff --git a/re2/testing/regexp_test.cc b/re2/testing/regexp_test.cc
index 31c76a3..75e97bc 100644
--- a/re2/testing/regexp_test.cc
+++ b/re2/testing/regexp_test.cc
@@ -4,8 +4,11 @@
 
 // Test parse.cc, dump.cc, and tostring.cc.
 
+#include <stddef.h>
+#include <map>
 #include <string>
 #include <vector>
+
 #include "util/test.h"
 #include "re2/regexp.h"
 
diff --git a/re2/testing/required_prefix_test.cc b/re2/testing/required_prefix_test.cc
index aed41f7..2368fe4 100644
--- a/re2/testing/required_prefix_test.cc
+++ b/re2/testing/required_prefix_test.cc
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+#include <string>
+
 #include "util/test.h"
 #include "re2/regexp.h"
 
diff --git a/re2/testing/search_test.cc b/re2/testing/search_test.cc
index 85faa46..ac7cace 100644
--- a/re2/testing/search_test.cc
+++ b/re2/testing/search_test.cc
@@ -2,8 +2,6 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-#include <stdlib.h>
-#include <vector>
 #include "util/test.h"
 #include "re2/prog.h"
 #include "re2/regexp.h"
diff --git a/re2/testing/set_test.cc b/re2/testing/set_test.cc
index c613d6a..ea8c213 100644
--- a/re2/testing/set_test.cc
+++ b/re2/testing/set_test.cc
@@ -2,8 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-#include <sys/types.h>
-#include <sys/stat.h>
+#include <stddef.h>
 #include <vector>
 
 #include "util/test.h"
diff --git a/re2/testing/simplify_test.cc b/re2/testing/simplify_test.cc
index 9db41ee..a8fd134 100644
--- a/re2/testing/simplify_test.cc
+++ b/re2/testing/simplify_test.cc
@@ -4,8 +4,9 @@
 
 // Test simplify.cc.
 
+#include <string.h>
 #include <string>
-#include <vector>
+
 #include "util/test.h"
 #include "re2/regexp.h"
 
diff --git a/re2/testing/string_generator.cc b/re2/testing/string_generator.cc
index f96ff20..15cfe1d 100644
--- a/re2/testing/string_generator.cc
+++ b/re2/testing/string_generator.cc
@@ -6,8 +6,10 @@
 // maxlen letters using the set of letters in alpha.
 // Fetch strings using a Java-like Next()/HasNext() interface.
 
+#include <stddef.h>
 #include <string>
 #include <vector>
+
 #include "util/test.h"
 #include "re2/testing/string_generator.h"
 
diff --git a/re2/testing/string_generator.h b/re2/testing/string_generator.h
index ff5a711..ba0ed93 100644
--- a/re2/testing/string_generator.h
+++ b/re2/testing/string_generator.h
@@ -11,6 +11,7 @@
 
 #include <string>
 #include <vector>
+
 #include "util/util.h"
 #include "util/random.h"
 #include "re2/stringpiece.h"
diff --git a/re2/testing/string_generator_test.cc b/re2/testing/string_generator_test.cc
index d13401a..3b5f2ea 100644
--- a/re2/testing/string_generator_test.cc
+++ b/re2/testing/string_generator_test.cc
@@ -4,9 +4,8 @@
 
 // Test StringGenerator.
 
-#include <stdlib.h>
 #include <string>
-#include <vector>
+
 #include "util/test.h"
 #include "re2/testing/string_generator.h"
 #include "re2/testing/regexp_generator.h"
diff --git a/re2/testing/tester.cc b/re2/testing/tester.cc
index 62cbb6c..9d37704 100644
--- a/re2/testing/tester.cc
+++ b/re2/testing/tester.cc
@@ -4,6 +4,11 @@
 
 // Regular expression engine tester -- test all the implementations against each other.
 
+#include <stddef.h>
+#include <string.h>
+#include <sys/types.h>
+#include <string>
+
 #include "util/util.h"
 #include "util/flags.h"
 #include "re2/testing/tester.h"
diff --git a/re2/testing/tester.h b/re2/testing/tester.h
index e2d72cb..2dce8df 100644
--- a/re2/testing/tester.h
+++ b/re2/testing/tester.h
@@ -8,6 +8,8 @@
 // Comparative tester for regular expression matching.
 // Checks all implementations against each other.
 
+#include <vector>
+
 #include "re2/stringpiece.h"
 #include "re2/prog.h"
 #include "re2/regexp.h"
diff --git a/re2/tostring.cc b/re2/tostring.cc
index 0230c8c..b967198 100644
--- a/re2/tostring.cc
+++ b/re2/tostring.cc
@@ -5,6 +5,9 @@
 // Format a regular expression structure as a string.
 // Tested by parse_test.cc
 
+#include <string.h>
+#include <string>
+
 #include "util/util.h"
 #include "re2/regexp.h"
 #include "re2/walker-inl.h"
diff --git a/re2/walker-inl.h b/re2/walker-inl.h
index 6a1113a..94ba1e9 100644
--- a/re2/walker-inl.h
+++ b/re2/walker-inl.h
@@ -13,6 +13,8 @@
 // Not quite the Visitor pattern, because (among other things)
 // the Visitor pattern is recursive.
 
+#include <stack>
+
 #include "re2/regexp.h"
 
 namespace re2 {
diff --git a/util/benchmark.cc b/util/benchmark.cc
index 20b6765..8319df1 100644
--- a/util/benchmark.cc
+++ b/util/benchmark.cc
@@ -2,6 +2,14 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+#if defined(__APPLE__)
+#include <sys/time.h>
+#elif defined(_WIN32)
+#include <windows.h>
+#else
+#include <time.h>
+#endif
+
 #include "util/util.h"
 #include "util/flags.h"
 #include "util/benchmark.h"
diff --git a/util/bitmap.h b/util/bitmap.h
index 8a93d81..41636fa 100644
--- a/util/bitmap.h
+++ b/util/bitmap.h
@@ -8,6 +8,8 @@
 #ifdef _MSC_VER
 #include <intrin.h>
 #endif
+#include <string.h>
+
 #include "util/util.h"
 
 namespace re2 {
diff --git a/util/logging.h b/util/logging.h
index 1573b18..7e69ca5 100644
--- a/util/logging.h
+++ b/util/logging.h
@@ -7,6 +7,7 @@
 
 // Simplified version of Google's logging.
 
+#include <assert.h>
 #include <stdio.h>  /* for fwrite */
 #include <sstream>
 
diff --git a/util/sparse_array.h b/util/sparse_array.h
index d37a10a..c3de793 100644
--- a/util/sparse_array.h
+++ b/util/sparse_array.h
@@ -92,6 +92,8 @@
 // immediately become inaccessible, but they are only guaranteed to be
 // destroyed when the SparseArray destructor is called.
 
+#include <string.h>
+
 #include "util/util.h"
 
 namespace re2 {
diff --git a/util/sparse_set.h b/util/sparse_set.h
index 537a094..9470052 100644
--- a/util/sparse_set.h
+++ b/util/sparse_set.h
@@ -47,6 +47,8 @@
 //
 // See sparse_array.h for implementation details
 
+#include <string.h>
+
 #include "util/util.h"
 
 namespace re2 {
diff --git a/util/stringprintf.cc b/util/stringprintf.cc
index e71d993..766f184 100644
--- a/util/stringprintf.cc
+++ b/util/stringprintf.cc
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+#include <stdarg.h>
+
 #include "util/util.h"
 
 namespace re2 {
diff --git a/util/util.h b/util/util.h
index 27c075f..d7a274b 100644
--- a/util/util.h
+++ b/util/util.h
@@ -5,20 +5,6 @@
 #ifndef UTIL_UTIL_H_
 #define UTIL_UTIL_H_
 
-// C
-#include <stdio.h>
-#include <string.h>
-#include <stdint.h>
-#include <stddef.h>     // For size_t
-#include <assert.h>
-#include <stdarg.h>
-#include <time.h>       // For clock_gettime, CLOCK_REALTIME
-#include <ctype.h>      // For isdigit, isalpha
-
-#if !defined(_WIN32)
-#include <sys/time.h>   // For gettimeofday
-#endif
-
 // C++
 #include <ctime>
 #include <vector>