# *created  "Fri Sep 25 11:43:44 2015" *by "Paul E. Black"
# *modified "Thu Dec 24 10:42:51 2015" *by "Paul E. Black"
#
# SATE V Ockham Criteria
#
# Run extractions and checks for write outside buffer class
#

all: checks statistics discrimination

checks: checkWsubseteqU checkFcapBisNull

SITES_FILE = ../ExtractSites/write_buffer_sites.xml
XML2CSV = ../ExtractSites/sitesxml2csv.py -f $(SITES_FILE) -w writeOutBuf

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

# (2) Select write outside buffer warnings
frama_c_writeOutBuf.csv: ../ExtractWarnings/frama_c_warnings.csv
	@echo "(2) Select write outside buffer warnings"
	-mv -f $@ $@.bak
	grep writeOutBuf $^ > $@

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

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

# (4) Select buggy sites and reformat into CSV format
writeOutBuf_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
writeOutBuf_findings.csv: writeOutBuf_universe.csv frama_c_writeOutBuf.csv
	@echo "(5) Compute list of Findings: F = U - W"
	-mv -f $@ $@.bak
	comm -23 writeOutBuf_universe.csv frama_c_writeOutBuf.csv > $@

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

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

statistics: writeOutBuf_universe.csv frama_c_writeOutBuf.csv \
	writeOutBuf_findings.csv writeOutBuf_buggy.csv \
	writeOutBuf_notsubset.csv writeOutBuf_false_find.csv
	-mv -f $@ $@.bak
	@echo "Class: Write Outside Buffer" | tee -a $@
	@echo -n "Number of sites (|U|) " | tee -a $@
	@wc -l < writeOutBuf_universe.csv | tee -a $@
	@echo -n "Number of warnings (|W|) " | tee -a $@
	@wc -l < frama_c_writeOutBuf.csv | tee -a $@
	@echo -n "Number of warnings not in universe (|W - U|) " | tee -a $@
	@wc -l < writeOutBuf_notsubset.csv | tee -a $@
	@echo -n "Number of findings (|F|) " | tee -a $@
	@wc -l < writeOutBuf_findings.csv | tee -a $@
	@echo -n "Number of buggy sites (|B|) " | tee -a $@
	@wc -l < writeOutBuf_buggy.csv | tee -a $@
	@echo -n "Number of buggy findings (|F cap B|) " | tee -a $@
	@wc -l < writeOutBuf_false_find.csv | tee -a $@

Ufile = writeOutBuf_universe.csv
Wfile = frama_c_writeOutBuf.csv
Ffile = writeOutBuf_findings.csv
# prefixes of those test cases that are made for this class
Prefixes = '^CWE12[124]_'
# 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 Outside Buffer" | 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 writeOutBuf_universe.csv frama_c_writeOutBuf.csv \
	writeOutBuf_notsubset.csv writeOutBuf_buggy.csv \
	writeOutBuf_findings.csv writeOutBuf_false_find.csv \
	statistics

# end of Makefile
