]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - contrib/check_GNU_style_lib.py
Update copyright years.
[thirdparty/gcc.git] / contrib / check_GNU_style_lib.py
index 63d0538aa572c4ac946fd47eaa7d1a704a97c718..9f5851c271f83ee0508168c6d03e919e77fd0367 100755 (executable)
@@ -1,4 +1,6 @@
 #!/usr/bin/env python3
+
+# Copyright (C) 2017-2024 Free Software Foundation, Inc.
 #
 # Checks some of the GNU style formatting rules in a set of patches.
 # The script is a rewritten of the same bash script and should eventually
@@ -99,6 +101,18 @@ class SpacesCheck:
                 line.replace(self.expanded_tab, error_string(ws_char * ts)),
                 'blocks of 8 spaces should be replaced with tabs', i)
 
+class SpacesAndTabsMixedCheck:
+    def __init__(self):
+        self.re = re.compile('\ \t')
+
+    def check(self, filename, lineno, line):
+        stripped = line.lstrip()
+        start = line[:len(line) - len(stripped)]
+        if self.re.search(line):
+            return CheckError(filename, lineno,
+                error_string(start.replace('\t', ws_char * ts)) + line[len(start):],
+                'a space should not precede a tab', 0)
+
 class TrailingWhitespaceCheck:
     def __init__(self):
         self.re = re.compile('(\s+)$')
@@ -168,6 +182,9 @@ class SquareBracketCheck:
         self.re = re.compile('\w\s+(\[)')
 
     def check(self, filename, lineno, line):
+        if filename.endswith('.md'):
+            return None
+
         m = self.re.search(line)
         if m != None:
             return CheckError(filename, lineno,
@@ -236,20 +253,35 @@ class TrailingWhitespaceTest(unittest.TestCase):
         r = self.check.check('foo', 123, 'a = 123;\t')
         self.assertIsNotNone(r)
 
-def check_GNU_style_file(file, file_encoding, format):
+class SpacesAndTabsMixedTest(unittest.TestCase):
+    def setUp(self):
+        self.check = SpacesAndTabsMixedCheck()
+
+    def test_trailing_whitespace_check_basic(self):
+        r = self.check.check('foo', 123, '   \ta = 123;')
+        self.assertEqual('foo', r.filename)
+        self.assertEqual(0, r.column)
+        self.assertIsNotNone(r.console_error)
+        r = self.check.check('foo', 123, '   \t  a = 123;')
+        self.assertIsNotNone(r.console_error)
+        r = self.check.check('foo', 123, '\t  a = 123;')
+        self.assertIsNone(r)
+
+def check_GNU_style_file(file, format):
     checks = [LineLengthCheck(), SpacesCheck(), TrailingWhitespaceCheck(),
         SentenceSeparatorCheck(), SentenceEndOfCommentCheck(),
         SentenceDotEndCheck(), FunctionParenthesisCheck(),
         SquareBracketCheck(), ClosingParenthesisCheck(),
-        BracesOnSeparateLineCheck(), TrailinigOperatorCheck()]
+        BracesOnSeparateLineCheck(), TrailinigOperatorCheck(),
+        SpacesAndTabsMixedCheck()]
     errors = []
 
-    patch = PatchSet(file, encoding=file_encoding)
+    patch = PatchSet(file)
 
     for pfile in patch.added_files + patch.modified_files:
         t = pfile.target_file.lstrip('b/')
         # Skip testsuite files
-        if 'testsuite' in t:
+        if 'testsuite' in t or t.endswith('.py'):
             continue
 
         for hunk in pfile: