define math_warnings
$(eval type:= $1)
$(eval all_headers := $(shell find $2 -name '*.hpp' -o -name '*.cpp'))
$(eval pattern := $3)
$(eval msg := $4)
$(foreach file,\
   $(shell grep -r -n -e $(pattern) $(all_headers) | sed 's|\([^:]*\):\([0-9]*\):.*|\1(\2)|g'),\
   echo '$(file): $(msg) [$(type)]';)
endef

.PHONY: test-math-dependencies
test-math-dependencies:
## Check for files inside stan/math/prim that contain
##   stan/math/rev or stan/math/fwd
	@$(call math_warnings, 'prim', 'stan/math/prim',\
             '<stan/math/rev/', 'File includes a stan/math/rev header file.')
	@$(call math_warnings, 'prim', 'stan/math/prim',\
             'stan::math::rev', 'File uses stan::math::var.')
	@$(call math_warnings, 'prim', 'stan/math/prim',\
             '<stan/math/fwd/', 'File includes a stan/math/fwd header file.')
	@$(call math_warnings, 'prim', 'stan/math/prim',\
             'stan::math::fvar', 'File uses stan::math::fvar.')
	@$(call math_warnings, 'prim', 'stan/math/prim',\
             '<stan/math/mix/', 'File includes a stan/math/mix header file.')
## Check for files inside stan/math/rev that contain
##   stan/math/fwd or stan/math/mix
	@$(call math_warnings, 'rev', 'stan/math/rev',\
             '<stan/math/fwd/', 'File includes a stan/math/fwd header file.')
	@$(call math_warnings, 'rev', 'stan/math/rev',\
             'stan::math::fvar', 'File uses stan::math::fvar.')
	@$(call math_warnings, 'rev', 'stan/math/rev',\
             '<stan/math/mix/', 'File includes a stan/math/mix header file.')
## Check for files inside stan/math/rev that contain
##   stan/math/fwd or stan/math/mix
	@$(call math_warnings, 'fwd', 'stan/math/fwd',\
             '<stan/math/rev/', 'File includes a stan/math/rev header file.')
	@$(call math_warnings, 'fwd', 'stan/math/fwd',\
             'stan::math::var', 'File uses stan::math::var.')
	@$(call math_warnings, 'fwd', 'stan/math/fwd',\
             '<stan/math/mix/', 'File includes a stan/math/mix header file.')
## Check for files inside stan/math/*/scal that contain
##   stan/math/*/arr or stan/math/*/mat
	@$(call math_warnings, 'scal', stan/math/*/scal,\
             '<stan/math/.*/arr/', 'File includes an array header file.')
	@$(call math_warnings, 'scal', stan/math/*/scal,\
             '<vector>', 'File includes an std::vector header.')
	@$(call math_warnings, 'scal', stan/math/*/scal,\
             'std::vector', 'File uses std::vector.')
	@$(call math_warnings, 'scal', stan/math/*/scal,\
             '<stan/math/.*/mat/', 'File includes a matrix header file.')
	@$(call math_warnings, 'scal', stan/math/*/scal,\
             '<Eigen', 'File includes an Eigen header.')
	@$(call math_warnings, 'scal', stan/math/*/scal,\
             'Eigen::', 'File uses Eigen.')
## Check for files inside stan/math/*/arr that contain
##   stan/math/*/mat
	@$(call math_warnings, 'arr', stan/math/*/arr,\
             '<stan/math/.*/mat/', 'File includes an matrix header file.')
	@$(call math_warnings, 'arr', stan/math/*/arr,\
             '<Eigen', 'File includes an Eigen header.')
	@$(call math_warnings, 'arr', stan/math/*/arr,\
             'Eigen::', 'File uses Eigen.')
