changeset 38174:36d55f90e2a3

fuzzutil: make it possible to use absl when C++17 isn't supported Differential Revision: https://phab.mercurial-scm.org/D3676
author Augie Fackler <augie@google.com>
date Mon, 30 Apr 2018 22:02:07 -0400
parents fa0ddd5e8fff
children fbe239064064
files contrib/fuzz/Makefile contrib/fuzz/fuzzutil.cc contrib/fuzz/fuzzutil.h
diffstat 3 files changed, 36 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/fuzz/Makefile	Sat Apr 28 22:18:50 2018 -0400
+++ b/contrib/fuzz/Makefile	Mon Apr 30 22:02:07 2018 -0400
@@ -3,6 +3,10 @@
 	  -std=c++17 \
 	  -I../../mercurial -c -o fuzzutil.o fuzzutil.cc
 
+fuzzutil-oss-fuzz.o: fuzzutil.cc fuzzutil.h
+	$$CXX $$CXXFLAGS -std=c++17 \
+	  -I../../mercurial -c -o fuzzutil-oss-fuzz.o fuzzutil.cc
+
 bdiff.o: ../../mercurial/bdiff.c
 	$$CC $$CFLAGS -fsanitize=fuzzer-no-link,address -c -o bdiff.o \
 	  ../../mercurial/bdiff.c
@@ -15,9 +19,10 @@
 bdiff-oss-fuzz.o: ../../mercurial/bdiff.c
 	$$CC $$CFLAGS -c -o bdiff-oss-fuzz.o ../../mercurial/bdiff.c
 
-bdiff_fuzzer: bdiff.cc bdiff-oss-fuzz.o fuzzutil.o
+bdiff_fuzzer: bdiff.cc bdiff-oss-fuzz.o fuzzutil-oss-fuzz.o
 	$$CXX $$CXXFLAGS -std=c++17 -I../../mercurial bdiff.cc \
-	  bdiff-oss-fuzz.o fuzzutil.o -lFuzzingEngine -o $$OUT/bdiff_fuzzer
+	  bdiff-oss-fuzz.o fuzzutil-oss-fuzz.o -lFuzzingEngine -o \
+	  $$OUT/bdiff_fuzzer
 
 x%.o: ../../mercurial/thirdparty/xdiff/x%.c ../../mercurial/thirdparty/xdiff/*.h
 	$$CC -g -O1 -fsanitize=fuzzer-no-link,address -c \
@@ -34,9 +39,9 @@
 	  -o $@ \
 	  $<
 
-xdiff_fuzzer: xdiff.cc fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil.o
+xdiff_fuzzer: xdiff.cc fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil-oss-fuzz.o
 	$$CXX $$CXXFLAGS -std=c++17 -I../../mercurial xdiff.cc \
-	  fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil.o \
+	  fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil-oss-fuzz.o \
 	  -lFuzzingEngine -o $$OUT/xdiff_fuzzer
 
 all: bdiff xdiff
--- a/contrib/fuzz/fuzzutil.cc	Sat Apr 28 22:18:50 2018 -0400
+++ b/contrib/fuzz/fuzzutil.cc	Mon Apr 30 22:02:07 2018 -0400
@@ -2,10 +2,10 @@
 
 #include <utility>
 
-std::optional<two_inputs> SplitInputs(const uint8_t *Data, size_t Size)
+contrib::optional<two_inputs> SplitInputs(const uint8_t *Data, size_t Size)
 {
 	if (!Size) {
-		return std::nullopt;
+		return contrib::nullopt;
 	}
 	// figure out a random point in [0, Size] to split our input.
 	size_t left_size = (Data[0] / 255.0) * (Size - 1);
--- a/contrib/fuzz/fuzzutil.h	Sat Apr 28 22:18:50 2018 -0400
+++ b/contrib/fuzz/fuzzutil.h	Mon Apr 30 22:02:07 2018 -0400
@@ -2,8 +2,31 @@
 #define CONTRIB_FUZZ_FUZZUTIL_H
 #include <iostream>
 #include <memory>
+#include <stdint.h>
+
+/* Try and use std::optional, but failing that assume we'll have a
+ * workable https://abseil.io/ install on the include path to get
+ * their backport of std::optional. */
+#ifdef __has_include
+#if __has_include(<optional>) && __cplusplus >= 201703L
 #include <optional>
-#include <stdint.h>
+#define CONTRIB_FUZZ_HAVE_STD_OPTIONAL
+#endif
+#endif
+#ifdef CONTRIB_FUZZ_HAVE_STD_OPTIONAL
+namespace contrib
+{
+using std::nullopt;
+using std::optional;
+} /* namespace contrib */
+#else
+#include "third_party/absl/types/optional.h"
+namespace contrib
+{
+using absl::nullopt;
+using absl::optional;
+} /* namespace contrib */
+#endif
 
 /* set DEBUG to 1 for a few debugging prints, or 2 for a lot */
 #define DEBUG 0
@@ -19,6 +42,6 @@
 };
 
 /* Split a non-zero-length input into two inputs. */
-std::optional<two_inputs> SplitInputs(const uint8_t *Data, size_t Size);
+contrib::optional<two_inputs> SplitInputs(const uint8_t *Data, size_t Size);
 
 #endif /* CONTRIB_FUZZ_FUZZUTIL_H */