瀏覽代碼

Add a Python script that makes the output of modes.cocci pretty.

Also moves the coccinelle files to a modes/ directory.
Cyril Roelandt 13 年之前
父節點
當前提交
42343c2f77

+ 81 - 0
tools/dev/experimental/modes/fix_cocci_output.py

@@ -0,0 +1,81 @@
+#!/usr/bin/env python
+
+import sys
+import re
+
+# XXX Could be something else, like a file.
+output = sys.stdout
+
+def get_indentation_level(s):
+	cnt = 0
+	for c in s:
+		if c != '\t':
+			return -1
+		cnt = cnt + 1
+
+	return cnt
+
+def fix(f):
+	modes = {}
+	trailing_comma = False
+	indentation_level = -1
+	for line in f.readlines():
+		# This regexp could be more precise, but it should be good
+		# enough
+		regexp = "((\s)*)\.modes\[(\d)+\](\s)*=(\s)*(.*)"
+		m = re.search(regexp, line)
+		if not m:
+			if modes:
+				output.write("".join(["\t" for i in range(indentation_level)]))
+				output.write(".modes = { ")
+				idx = 0
+				while modes.get(str(idx)):
+					if idx != 0:
+						output.write(", ")
+					output.write(modes[str(idx)])
+					idx = idx+1
+				if trailing_comma:
+					output.write(" },\n")
+				else:
+					output.write(" }\n")
+
+				# Resetting these..
+				modes.clear()
+				trailing_comma = False
+				indentation_level = -1
+			output.write(line)
+		else:
+			idx = m.group(3)
+			mode = m.group(6)
+
+			# Remove traling comma
+			if mode[-1] == ',':
+				mode = mode[:-1]
+				# This is the last mode for this 
+				# codelet. Was this also the last
+				# field ?
+				if int(idx) == 0:
+					trailing_comma = True
+
+			# Try and guess the level of indentation
+			if int(idx) == 0:
+				s = m.group(1)
+				indentation_level = get_indentation_level(s)
+
+			modes[idx] = mode
+
+def fix_file(filename):
+	with open(filename, 'r') as f:
+		fix(f)
+
+
+def usage():
+	print "%s <filename>" % sys.argv[0]
+	sys.exit(1)
+
+if __name__ == '__main__':
+	if len(sys.argv) != 2:
+		usage()
+		sys.exit(1)
+
+	fix_file(sys.argv[1])

+ 20 - 0
tools/dev/experimental/modes/fix_cocci_output_test.c

@@ -0,0 +1,20 @@
+struct starpu_codelet cl = {
+	.where = STARPU_CPU,
+	/* => .modes = { STARPU_R, STARPU_W }, */
+	.modes[1] = STARPU_W,
+	.modes[0] = STARPU_R,
+	.cpu_func = foo
+};
+
+
+static void
+foo(void)
+{
+	struct starpu_codelet cl = {
+		.where = STARPU_CPU,
+		/* .modes = STARPU_R, STARPU_RW, STARPU_W } */
+		.modes[2] = STARPU_W,
+		.modes[1] = STARPU_RW,
+		.modes[0] = STARPU_R
+	};
+}

tools/dev/experimental/modes.cocci → tools/dev/experimental/modes/modes.cocci


+ 8 - 0
tools/dev/experimental/modes/modes.sh

@@ -0,0 +1,8 @@
+#!/bin/bash
+
+PREFIX=tools/dev/experimental/modes/
+tmp=`mktemp`
+
+spatch -in_place -sp_file $PREFIX/modes.cocci $1
+$PREFIX/fix_cocci_output.py $1 > $tmp
+mv $tmp $1

tools/dev/experimental/modes_test.c → tools/dev/experimental/modes/modes_test.c