Commit 17a846f2bc04ee012d13b93cd4d34316147f50ba

Daniel Mendler 2019-10-26T19:33:36

add "make c99" to convert back

diff --git a/.travis.yml b/.travis.yml
index 42960d0..738c62b 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -61,6 +61,9 @@ matrix:
     # We have only one program and the variable $BUILDOPTIONS
     # has only the options to that program: testme.sh
 
+    # Check c89 <-> c99 roundtrip
+    - env: BUILDOPTIONS='--c89-c99-roundtrip'
+
     # Check source code format
     - env: BUILDOPTIONS='--format'
       addons:
diff --git a/makefile b/makefile
index be7ef0c..c06849a 100644
--- a/makefile
+++ b/makefile
@@ -154,8 +154,9 @@ perlcritic:
 
 c89:
 	@echo "Applying substitutions for c89 compatibility..."
+	@if grep mp_bool tommath.h > /dev/null; then echo "Already applied"; exit 1; fi
 	@sed -i \
-	-e 's/#include <stdbool.h>//g' \
+	-e '/#include <stdbool.h>/d' \
 	-e 's/#include <stdint.h>/#include "tommath_c89.h"/g' \
 	-e 's/bool/mp_bool/g' \
 	-e 's/true/MP_YES/g' \
@@ -175,6 +176,21 @@ c89:
 	@echo "typedef __UINT64_TYPE__ mp_u64;"         >> tommath_c89.h
 
 
+c99:
+	@echo "Applying substitutions for c99 compatibility..."
+	@if ! grep mp_bool tommath.h > /dev/null; then echo "Already applied"; exit 1; fi
+	@sed -i \
+	-e 's/#include "tommath_c89.h"/#include <stdint.h>\n#include <stdbool.h>/g' \
+	-e 's/mp_bool/bool/g' \
+	-e 's/MP_YES/true/g' \
+	-e 's/MP_NO/false/g' \
+	-e 's/false_/MP_NO_/g' \
+	-e 's/mp_u\([0-9][0-9]*\)/uint\1_t/g' \
+	-e 's/mp_i\([0-9][0-9]*\)/int\1_t/g' \
+	*.c *.h demo/*.c demo/*.h etc/*.c
+	@rm -f tommath_c89.h
+
+
 astyle:
 	@echo "   * run astyle on all sources"
 	@astyle --options=astylerc --formatted $(OBJECTS:.o=.c) tommath*.h demo/*.c etc/*.c mtest/mtest.c
diff --git a/testme.sh b/testme.sh
index 3375fa6..6c71ac4 100755
--- a/testme.sh
+++ b/testme.sh
@@ -196,6 +196,7 @@ VALGRIND_OPTS=" --leak-check=full --show-leak-kinds=all --error-exitcode=1 "
 VALGRIND_BIN=""
 CHECK_FORMAT=""
 C89=""
+C89_C99_ROUNDTRIP=""
 TUNE_CMD="./etc/tune -t -r 10 -L 3"
 
 alive_pid=0
@@ -221,6 +222,9 @@ do
     --c89)
       C89="1"
     ;;
+    --c89-c99-roundtrip)
+      C89_C99_ROUNDTRIP="1"
+    ;;
     --with-cc=*)
       COMPILERS="$COMPILERS ${1#*=}"
     ;;
@@ -297,6 +301,14 @@ function _check_git() {
 
 [[ "$C89" == "1" ]] && make c89
 
+if [[ "$C89_C99_ROUNDTRIP" == "1" ]]
+then
+  make c89
+  make c99
+  _check_git "make c89; make c99"
+  exit $?
+fi
+
 if [[ "$CHECK_FORMAT" == "1" ]]
 then
   make astyle
diff --git a/tommath.h b/tommath.h
index a55c57f..5ebd50c 100644
--- a/tommath.h
+++ b/tommath.h
@@ -4,8 +4,8 @@
 #ifndef TOMMATH_H_
 #define TOMMATH_H_
 
-#include <stdint.h>
 #include <stddef.h>
+#include <stdint.h>
 #include <stdbool.h>
 
 #ifndef MP_NO_FILE