diff options
author | Jay Berkenbilt <ejb@ql.org> | 2022-05-03 14:21:01 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2022-05-04 00:31:22 +0200 |
commit | 62bf296a9c0f5be492f0677ed111b3fa217f4c11 (patch) | |
tree | f49564b3d70a12ee4e16e84c4e4e33a2adce1133 /check_assert | |
parent | 92b692466f7a4dbf4e51e6a77713c029a3e18ab1 (diff) | |
download | qpdf-62bf296a9c0f5be492f0677ed111b3fa217f4c11.tar.zst |
Make assert handling less error-prone
Prevent my future self or other contributors from using assert in
tests and then having that assert not do anything because of the
NDEBUG macro.
Diffstat (limited to 'check_assert')
-rwxr-xr-x | check_assert | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/check_assert b/check_assert new file mode 100755 index 00000000..81ecf788 --- /dev/null +++ b/check_assert @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +require 5.008; +use warnings; +use strict; +use File::Basename; + +my $whoami = basename($0); +chdir(dirname($0)) or die; + +my $errors = 0; +foreach my $file (glob('*/*.c'), glob('*/*.cc'), + glob('*/*/*.h'), glob('*/*/*.hh')) +{ + my $assert_test = 0; + if ($file =~ m,^libqpdf/qpdf/assert_,) + { + next; + } + open(F, "<$file") or die; + my $first_include = undef; + while (<F>) + { + if (m,^\s*#\s*include <qpdf/assert_(.*?).h>,) + { + if ($1 eq 'test') + { + $assert_test = 1; + } + if (defined $first_include) + { + error("$file:$.: qpdf/assert header must be first"); + } + } + if (m,^\s*#\s*include <(.*?)>,) + { + my $header = $1; + if (($header eq 'cassert') || ($header eq 'assert.h')) + { + error("$file:$.: assert.h and cassert are not allowed --" . + " use one of the qpdf/assert_ files instead"); + } + $first_include = 1; + } + if ((! $assert_test) && (m/assert\(/)) + { + error("$file:$.: call qpdf_assert_debug instead of assert"); + } + } + close(F); +} +if ($errors) +{ + die "$whoami: errors detected\n"; +} +print "$whoami: no incorrect use of assert found\n"; + +sub error +{ + my $msg = shift; + warn $msg, "\n"; + $errors = 1; +} |