Add contrib/tests-normalize-line-number.sh 2017-04-15 Tom de Vries * tests-normalize-line-number.sh: New file. --- contrib/tests-normalize-line-number.sh | 78 ++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/contrib/tests-normalize-line-number.sh b/contrib/tests-normalize-line-number.sh new file mode 100755 index 0000000..9ecba6a --- /dev/null +++ b/contrib/tests-normalize-line-number.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env python2 + +# We hit a bug with python 3 when doing replace_in_file_p() with +# ./libstdc++-v3/testsuite/27_io/basic_filebuf/cons/wchar_t/10132-1.cc, so let's +# use python 2 and import the print function from python 3. +from __future__ import print_function + +import os, re +import fileinput + +# A non-negative number. +number="[0-9][0-9]*" +# Allow quotes around the number. +number=( + "(?:" + + number + "|" + + '"' + number + '"' + "|" + + "'" + number + "'" + + ")" +) +# Directives using a line number. +directive="dg-(?:error|warning|message|bogus)" +# Line containing directive. +directiveline=( + "(.*\{\s*%s.*)" % directive + # Part before line number. Save it. + "\{\s*(%s)\s*\}" % number + # A number in braces. Save the number. + "\s*\}" + # Closing brace of directive. + "(.*)" # Rest of line. Save it. +) +# Match the entire line +directiveline=( + "^" + + directiveline + + "$" +) +pattern=re.compile(directiveline) + +extensions="c|cc|C|cpp|f|F|[fF]03|[fF]08|[fF]90|[fF]95|go|h|i|m|mm|S|adb|ads" +extensionspattern=re.compile(".*\.(?:" + extensions + ")$") + +def do_replace_in_file(file): + for line in fileinput.input(files=file,inplace=True): + match=pattern.search(line) + if match: + line=( + match.group(1) + # Part before line number. + match.group(2) + # Line number. + " }" + # Closing brace of directive. + match.group(3) # Rest of line. + ) + line+="\n" + + print(line, end='') + +def replace_in_file_p(file): + filehandle = open(file, 'r') + for line in filehandle: + match=pattern.search(line) + if match: + return True + + return False + +def do_testsuite_dir(testsuite_dir): + for root, dirs, files in os.walk(testsuite_dir): + for file in files: + if extensionspattern.match(file): + fullname=os.path.join(root, file) + if replace_in_file_p(fullname): + do_replace_in_file(fullname) + +def do_dir(rootdir): + for root, dirs, files in os.walk(rootdir): + for dir in dirs: + if dir == "testsuite": + do_testsuite_dir(os.path.join(root, dir)) + +do_dir(".")