# *created  "Fri Oct  2 08:30:12 2015" *by "Paul E. Black"
# *modified "Thu Dec 24 10:40:41 2015" *by "Paul E. Black"
#
# SATE V Ockham Criteria
#
# Run extractions and checks for write what where class
#

all: checks statistics discrimination

checks: checkWsubseteqU checkFcapBisNull

SITES_FILE = ../ExtractSites/write_what_where_sites.xml
XML2CSV = ../ExtractSites/sitesxml2csv.py -f $(SITES_FILE) -w writeWhatWhere

# (1) Produce a list of all sites in CSV format
writeWhatWhere_universe.csv: $(SITES_FILE)
	@echo "(1) Reformat all sites into CSV format"
	-mv -f $@ $@.bak
	python $(XML2CSV) | sort -u > $@

# (2) Select write what where warnings
frama_c_writeWhatWhere.csv:  ../ExtractWarnings/frama_c_warnings.csv
	@echo "(2) Select write what where warnings"
	-mv -f $@ $@.bak
	grep writeWhatWhere $^ > $@

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

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

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

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

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

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

statistics: writeWhatWhere_universe.csv frama_c_writeWhatWhere.csv \
	writeWhatWhere_findings.csv writeWhatWhere_buggy.csv \
	writeWhatWhere_notsubset.csv writeWhatWhere_false_find.csv
	-mv -f $@ $@.bak
	@echo "Class: Write What Where" | tee -a $@
	@echo -n "Number of sites (|U|) " | tee -a $@
	@wc -l < writeWhatWhere_universe.csv | tee -a $@
	@echo -n "Number of warnings (|W|) " | tee -a $@
	@wc -l < frama_c_writeWhatWhere.csv | tee -a $@
	@echo -n "Number of warnings not in universe (|W - U|) " | tee -a $@
	@wc -l < writeWhatWhere_notsubset.csv | tee -a $@
	@echo -n "Number of findings (|F|) " | tee -a $@
	@wc -l < writeWhatWhere_findings.csv | tee -a $@
	@echo -n "Number of buggy sites (|B|) " | tee -a $@
	@wc -l < writeWhatWhere_buggy.csv | tee -a $@
	@echo -n "Number of buggy findings (|F cap B|) " | tee -a $@
	@wc -l < writeWhatWhere_false_find.csv | tee -a $@

Ufile = writeWhatWhere_universe.csv
Wfile = frama_c_writeWhatWhere.csv
Ffile = writeWhatWhere_findings.csv
# prefixes of those test cases that are made for this class
Prefixes = '^CWE123_Write_What_Where_Condition_'
# 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: Write What Where" | 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 writeWhatWhere_universe.csv frama_c_writeWhatWhere.csv \
	writeWhatWhere_notsubset.csv writeWhatWhere_buggy.csv \
	writeWhatWhere_findings.csv writeWhatWhere_false_find.csv \
	statistics

# end of Makefile
