# *created  "Fri Nov 20 11:03:51 2015" *by "Paul E. Black"
# *modified "Thu Dec 24 10:41:17 2015" *by "Paul E. Black"
#
# SATE V Ockham Criteria
#
# Run extractions and checks for uninitialized variable class
#

all: checks statistics discrimination

checks: checkWsubseteqU checkFcapBisNull

SITES_FILE = ../ExtractSites/variable_ref_sites.xml
XML2CSV = ../ExtractSites/sitesxml2csv.py -f $(SITES_FILE) -w uninitVar

# (1) Produce a list of divide sites in CSV format
uninitVar_universe.csv: $(SITES_FILE)
	@echo "(1) Reformat divide sites into CSV format: U"
	-mv -f $@ $@.bak
	# sort -u needed since buggy/good and variable info is discarded.
	python $(XML2CSV) | sort -u > $@

# (2) Select uninitialized variable warnings
frama_c_uninitVar.csv:  ../ExtractWarnings/frama_c_warnings.csv
	@echo "(2) Select uninitialized variable warnings: W"
	-mv -f $@ $@.bak
	grep uninitVar $^ > $@

# (3) Check W \subseteq U
checkWsubseteqU: uninitVar_notsubset.csv

uninitVar_notsubset.csv: uninitVar_universe.csv frama_c_uninitVar.csv
	@echo "(3) Check W subseteq U"
	-mv -f $@ $@.bak
	comm -23 frama_c_uninitVar.csv uninitVar_universe.csv > $@
	wc -l $@
	test -e $@ -a ! -s $@

# (4) Select buggy sites and reformat into CSV format
uninitVar_buggy.csv: $(SITES_FILE)
	@echo "(4) Select buggy sites and reformat into CSV format: B"
	-mv -f $@ $@.bak
	# -u needed since same bugs reported in both runs
	python $(XML2CSV) -b | sort -u > $@

# (5) Compute list of Findings: F = U - W
uninitVar_findings.csv: uninitVar_universe.csv frama_c_uninitVar.csv
	@echo "(5) Compute list of Findings: F = U - W"
	-mv -f $@ $@.bak
	comm -23 uninitVar_universe.csv frama_c_uninitVar.csv > $@

# (6) Check F \cap B = \null
checkFcapBisNull: uninitVar_false_find.csv

uninitVar_false_find.csv: uninitVar_buggy.csv uninitVar_findings.csv
	@echo "(6) Check F cap B = null"
	-mv -f $@ $@.bak
	comm -12 uninitVar_findings.csv uninitVar_buggy.csv > $@
	wc -l $@
	test -e $@ -a ! -s $@

statistics: uninitVar_universe.csv frama_c_uninitVar.csv \
	uninitVar_findings.csv uninitVar_buggy.csv \
	uninitVar_notsubset.csv uninitVar_false_find.csv
	-mv -f $@ $@.bak
	@echo "Class: Uninitialized Variable" | tee -a $@
	@echo -n "Number of sites (|U|) " | tee -a $@
	@wc -l < uninitVar_universe.csv | tee -a $@
	@echo -n "Number of warnings (|W|) " | tee -a $@
	@wc -l < frama_c_uninitVar.csv | tee -a $@
	@echo -n "Number of warnings not in universe (|W - U|) " | tee -a $@
	@wc -l < uninitVar_notsubset.csv | tee -a $@
	@echo -n "Number of findings (|F|) " | tee -a $@
	@wc -l < uninitVar_findings.csv | tee -a $@
	@echo -n "Number of buggy sites (|B|) " | tee -a $@
	@wc -l < uninitVar_buggy.csv | tee -a $@
	@echo -n "Number of buggy findings (|F cap B|) " | tee -a $@
	@wc -l < uninitVar_false_find.csv | tee -a $@

Ufile = uninitVar_universe.csv
Wfile = frama_c_uninitVar.csv
Ffile = uninitVar_findings.csv
# prefixes of those test cases that are made for this class
Prefixes = '^CWE457_Use_of_Uninitialized_Variable_'
# remove multi-file test case suffixes, e.g. 64a.c, 64b.c, 64c.c -> 64, as well
# as information we don't need, like line numbers and weakness class name
REMOVE_SUFFIXES = perl -pwe 's/[a-g]?[.]c, [\d]+, [a-zA-Z]+//'

discrimination: $(Ufile) $(Ffile) $(Wfile)
	@echo "Class: Uninitialized Variable" | tee -a $@
	@echo -n "Number of test cases made for this class " | tee -a $@
	@grep -e $(Prefixes) $(Ufile) | $(REMOVE_SUFFIXES) | uniq | wc -l | tee -a $@
	@echo -n "Maximum number of points " | tee -a $@
	@# i.e., pertinent test cases with good sites that don't have warnings
	@grep -e $(Prefixes) $(Ffile) | $(REMOVE_SUFFIXES) | uniq | wc -l | tee -a $@
	@echo -n "Minimum number of points " | tee -a $@
	@# i.e., pertinent test cases with only one (relevant) warning
	@grep -e $(Prefixes) $(Wfile) | $(REMOVE_SUFFIXES) | uniq -c | grep '   1 ' | wc -l | tee -a $@
	@# Read the file back in and compute the percentages
	@perl -nwe 'if(/Number \D*(\d+)/){$$t=$$1};if(/Maximum \D*(\d+)/){$$max=$$1};if(/Minimum \D*(\d+)/){$$min=$$1};END{$$maxpts=100*$$max/$$t;$$minpts=100*$$min/$$t;print "Discrimination is between $$minpts% and $$maxpts%\n"}' $@ | tee -a $@


clean:
	rm -f *.bak uninitVar_universe.csv frama_c_uninitVar.csv \
	uninitVar_notsubset.csv uninitVar_buggy.csv \
	uninitVar_findings.csv uninitVar_false_find.csv \
	statistics

# end of Makefile
