From 62bf296a9c0f5be492f0677ed111b3fa217f4c11 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 3 May 2022 08:21:01 -0400 Subject: 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. --- check_assert | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 check_assert (limited to 'check_assert') 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 () + { + if (m,^\s*#\s*include ,) + { + 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; +} -- cgit v1.2.3-54-g00ecf