From e05fd7f1d069b8315f6884980383fe5302da7001 Mon Sep 17 00:00:00 2001 From: Eric Leblond Date: Fri, 5 Jul 2013 12:28:30 +0200 Subject: [PATCH] coccinelle: add script to generate flags test This patch adds a script which can be used to generate a test on coherence of flag usage. By adding comment in the code, it is possible to declare that we link a flag in a structure to a specific family of constant: For example: /* coccinelle: Packet:flowflags:FLOW_PKT_ */ will trigger the generation on a test which verifies that the flowflags field in Packet structure is only used with constant starting by FLOW_PKT_. --- qa/coccinelle/struct-flags.py | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 qa/coccinelle/struct-flags.py diff --git a/qa/coccinelle/struct-flags.py b/qa/coccinelle/struct-flags.py new file mode 100755 index 0000000000..3a91157b4a --- /dev/null +++ b/qa/coccinelle/struct-flags.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +import re +from os import listdir + +SRC_DIR="../../src/" + +class Structure: + def __init__(self, string): + (self.struct, self.flags, self.values) = string.split(":") + +cmd = "grep -h coccinelle ../../src/*[ch] | sed -e 's/.*coccinelle: \(.*\) \*\//\1/'" + +struct_list = [] + +dirList = listdir(SRC_DIR) +for fname in dirList: + if re.search("\.[ch]$", fname): + for line in open(SRC_DIR + fname): + if "coccinelle:" in line: + m = re.search("coccinelle: (.*) \*\/", line) + struct = Structure(m.group(1)) + struct_list.append(struct) + +header = "@flags@" +body = [] + +i = 0 +for struct in struct_list: + header += """ +%s *struct%d; +identifier struct_flags%d =~ "^(?!%s).+";""" % ( struct.struct, i, i, struct.values) + + body.append(""" +struct%d->%s@p1 |= struct_flags%d +| +struct%d->%s@p1 & struct_flags%d +| +struct%d->%s@p1 &= ~struct_flags%d +""" % (i, struct.flags, i, i, struct.flags, i, i, struct.flags, i)) + + i+=1 + +print header +print "position p1;" +print "@@" +print "" +print "(" + "|".join(body) + ")" +print "" +print """@script:python@ +p1 << flags.p1; +@@ + +print "Invalid usage of flags field at %s:%s, flags value is incorrect (wrong family)." % (p1[0].file, p1[0].line) +import sys +sys.exit(1)"""