Support coverage flushing on assert()

When assert() is called, which is not uncommon in utility code within NetSurf,
we lose coverage data for anything done before the assert() in the test.  This
commit corrects that oversight but is at least slightly GCC specific and may
need tweaks for non-Linux platforms.

By default, 'make coverage' will enable assert coverage, and it can be disabled
with 'make coverage NOASSERTCOVERAGE=yes' if necessary.

Signed-off-by: Daniel Silverstone <dsilvers@netsurf-browser.org>
This commit is contained in:
Daniel Silverstone 2017-03-31 18:53:52 +01:00
parent 4e750bdab6
commit e499f5c08f
2 changed files with 68 additions and 4 deletions

View File

@ -81,10 +81,19 @@ ifeq ($(MAKECMDGOALS),coverage)
COV_CXXFLAGS ?= -fprofile-arcs -ftest-coverage -O0 COV_CXXFLAGS ?= -fprofile-arcs -ftest-coverage -O0
COV_LDFLAGS ?= -lgcov -fprofile-arcs COV_LDFLAGS ?= -lgcov -fprofile-arcs
TESTROOT := $(COV_ROOT) TESTROOT := $(COV_ROOT)
ifeq ($(NOASSERTCOVERAGE),yes)
NOCOV_TESTSOURCES ?=
COV_CPPFLAGS ?=
else
NOCOV_TESTSOURCES ?= test/assert.c
COV_CPPFLAGS ?= -D__assert_fail=__ns_assert_fail
endif
else else
COV_CFLAGS ?= -O0 COV_CFLAGS ?= -O0
COV_CXXFLAGS ?= -O0 COV_CXXFLAGS ?= -O0
COV_CPPFLAGS ?=
TESTROOT := build/$(HOST)-test TESTROOT := build/$(HOST)-test
NOCOV_TESTSOURCES ?=
endif endif
@ -123,7 +132,7 @@ ifneq ($(CC_MAJOR),2)
COMMON_WARNFLAGS += -Wno-unused-parameter COMMON_WARNFLAGS += -Wno-unused-parameter
endif endif
TESTCFLAGS := -std=c99 -g \ BASE_TESTCFLAGS := -std=c99 -g \
$(COMMON_WARNFLAGS) \ $(COMMON_WARNFLAGS) \
-D_BSD_SOURCE \ -D_BSD_SOURCE \
-D_POSIX_C_SOURCE=200809L \ -D_POSIX_C_SOURCE=200809L \
@ -131,8 +140,10 @@ TESTCFLAGS := -std=c99 -g \
-Itest -Iinclude -Icontent/handlers -Ifrontends -I. -I.. \ -Itest -Iinclude -Icontent/handlers -Ifrontends -I. -I.. \
-Dnsgtk \ -Dnsgtk \
$(shell pkg-config --cflags libcurl libparserutils libwapcaplet libdom libnsutils libutf8proc) \ $(shell pkg-config --cflags libcurl libparserutils libwapcaplet libdom libnsutils libutf8proc) \
$(LIB_CFLAGS) \ $(LIB_CFLAGS)
$(COV_CFLAGS) TESTCFLAGS := $(BASE_TESTCFLAGS) \
$(COV_CFLAGS) \
$(COV_CPPFLAGS)
TESTLDFLAGS := -L$(TESTROOT) \ TESTLDFLAGS := -L$(TESTROOT) \
$(shell pkg-config --libs libcurl libparserutils libwapcaplet libdom libnsutils libutf8proc) -lz \ $(shell pkg-config --libs libcurl libparserutils libwapcaplet libdom libnsutils libutf8proc) -lz \
@ -149,7 +160,7 @@ TESTSOURCES :=
GCOV ?= gcov GCOV ?= gcov
define gen_test_target define gen_test_target
$$(TESTROOT)/$(1): $$(sort $$(addprefix $$(TESTROOT)/,$$(subst /,_,$$(patsubst %.c,%.o,$$(patsubst %.cpp,%.o,$$(patsubst %.m,%.o,$$(patsubst %.s,%.o,$$($(1)_SRCS)))))))) $$(TESTROOT)/$(1): $$(sort $$(addprefix $$(TESTROOT)/,$$(subst /,_,$$(patsubst %.c,%.o,$$(patsubst %.cpp,%.o,$$(patsubst %.m,%.o,$$(patsubst %.s,%.o,$$($(1)_SRCS) $$(NOCOV_TESTSOURCES))))))))
$$(VQ)echo "LINKTEST: $$@" $$(VQ)echo "LINKTEST: $$@"
$$(Q)$$(CC) $$(TESTCFLAGS) $$^ -o $$@ $$($(1)_LD) $$(TESTLDFLAGS) $$(Q)$$(CC) $$(TESTCFLAGS) $$^ -o $$@ $$($(1)_LD) $$(TESTLDFLAGS)
@ -171,12 +182,22 @@ $$(TESTROOT)/$(2): $(1) $$(TESTROOT)/created
endef endef
define compile_test_nocov_target_c
$$(TESTROOT)/$(2): $(1) $$(TESTROOT)/created
$$(VQ)echo " COMPILE: $(1) (No coverage)"
$$(Q)$$(RM) $$(TESTROOT)/$(2)
$$(Q)$$(CC) $$(BASE_TESTCFLAGS) -o $$(TESTROOT)/$(2) -c $(1)
endef
# Generate target for each test program and the list of objects it needs # Generate target for each test program and the list of objects it needs
$(eval $(foreach TST,$(TESTS), $(call gen_test_target,$(TST)))) $(eval $(foreach TST,$(TESTS), $(call gen_test_target,$(TST))))
# generate target rules for test objects # generate target rules for test objects
$(eval $(foreach SOURCE,$(sort $(filter %.c,$(TESTSOURCES))), \ $(eval $(foreach SOURCE,$(sort $(filter %.c,$(TESTSOURCES))), \
$(call compile_test_target_c,$(SOURCE),$(subst /,_,$(SOURCE:.c=.o)),$(subst /,_,$(SOURCE:.c=.d))))) $(call compile_test_target_c,$(SOURCE),$(subst /,_,$(SOURCE:.c=.o)),$(subst /,_,$(SOURCE:.c=.d)))))
$(eval $(foreach SOURCE,$(sort $(filter %.c,$(NOCOV_TESTSOURCES))), \
$(call compile_test_nocov_target_c,$(SOURCE),$(subst /,_,$(SOURCE:.c=.o)),$(subst /,_,$(SOURCE:.c=.d)))))
.PHONY:test coverage .PHONY:test coverage

43
test/assert.c Normal file
View File

@ -0,0 +1,43 @@
/*
* Copyright 2017 Daniel Silverstone <dsilvers@netsurf-browser.org>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* NetSurf is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* NetSurf is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file
* Hack for assertion coverage output
*/
/* Bring in the real __assert_fail */
#include <assert.h>
/* This is what everyone else calls */
extern void
__ns_assert_fail(const char *__assertion, const char *__file,
unsigned int __line, const char *__function)
__THROW __attribute__ ((__noreturn__));
/* We use this to flush coverage data */
extern void __gcov_flush(void);
/* And here's our entry point */
void
__ns_assert_fail(const char *__assertion, const char *__file,
unsigned int __line, const char *__function)
{
__gcov_flush();
__assert_fail(__assertion, __file, __line, __function);
}