summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Syromyatnikov <evgsyr@gmail.com>2021-10-11 20:43:25 +0200
committerEugene Syromyatnikov <evgsyr@gmail.com>2021-10-12 00:35:48 +0200
commit5034a5eb4abe477925554c3e5705b5619bef7749 (patch)
treede678ae040e13be1dff80c500264ec41797a8f4d
parenta077e8abd1f59e9645531fbaf5e353ca1f0a4b01 (diff)
downloadstrace-5034a5eb4abe477925554c3e5705b5619bef7749.tar.gz
xlat/gen.sh: add a compile-time check for sorted xlat order
* src/xlat/gen.sh (check_sort_order): New function. (cond_def): Accept xlat_type parameter, call check_sort_order if xlat_type is XT_SORTED. (gen_header): Call check_sort_order for lines that start with "[A-Z_]*" (only of $unconditional is set to non-empty string: otherwise, call is done inside cond_def), "'1<<'[A-Z_]*", and "[0-9]*" if xlat_type is XT_SORTED. Suggested-by: Dmitry V. Levin <ldv@strace.io>
-rwxr-xr-xsrc/xlat/gen.sh39
1 files changed, 37 insertions, 2 deletions
diff --git a/src/xlat/gen.sh b/src/xlat/gen.sh
index 4d8d77c7b..9c4c61cef 100755
--- a/src/xlat/gen.sh
+++ b/src/xlat/gen.sh
@@ -40,6 +40,8 @@ cond_def()
{
local line
line="$1"; shift
+ local xlat_type
+ xlat_type="$1"; shift
local val
val="${line%%[!A-Za-z0-9_]*}"
@@ -63,6 +65,26 @@ cond_def()
"# define $val $def" \
"#endif"
fi
+
+ [ XT_SORTED != "$xlat_type" ] ||
+ check_sort_order "$val"
+}
+
+check_sort_order()
+{
+ local val
+ val="$1"; shift
+
+ cat <<-EOF
+ #if defined XLAT_PREV_VAL
+ static_assert((unsigned long long) ($val)
+ > (unsigned long long) (XLAT_PREV_VAL),
+ "Incorrect order in #sorted xlat: $val"
+ " is not larger than the previous value");
+ #endif
+ #undef XLAT_PREV_VAL
+ #define XLAT_PREV_VAL ($val)
+ EOF
}
print_xlat()
@@ -207,13 +229,26 @@ gen_header()
echo "${line}"
;;
[A-Z_]*)
- [ -n "$unconditional" ] ||
- cond_def "$line"
+ if [ -n "$unconditional" ]; then
+ [ XT_SORTED != "$xlat_type" ] ||
+ check_sort_order "${line}"
+ else
+ cond_def "$line" "$xlat_type"
+ fi
+ ;;
+ '1<<'[A-Z_]*) # symbolic constants with shift
+ [ XT_SORTED != "$xlat_type" ] ||
+ check_sort_order "1ULL<<${line#1<<}"
+ ;;
+ [0-9]*) # numeric constants
+ [ XT_SORTED != "$xlat_type" ] ||
+ check_sort_order "${line}"
;;
esac
done < "$input"
cat <<-EOF
+ #undef XLAT_PREV_VAL
#ifndef XLAT_MACROS_ONLY