Daniel Carvalho has submitted this change. ( https://gem5-review.googlesource.com/c/public/gem5/+/43365 )

Change subject: util: Revamp opening brace verifier
......................................................................

util: Revamp opening brace verifier

1 - Rename the verifier from ClassBraces to
StructureBraces.

2 - Add support for anonymous structures. This
includes anonymous classes, anonymous structs,
anonymous enums and anonymous unions. e.g.:

  struct {

3 - Make the verifier not trigger error for
structures that do not currently abide to gem5's
coding style and use non-uppercase characters as
their first character. e.g.:

  struct test {

4 - Improve handling of nested structures. e.g.:

  struct { enum { VAR, VAR2

becomes

  struct
  {
      enum {
          VAR, VAR2

But the verifier will fail for declarations like:

  struct { int a; }; struct {

which becomes

  struct
  {
      int a; struct {

However, this later issue is not a desired coding
style, so it should be handled by another kind of
verifier if desired.

Change-Id: I8f0536dcc2c164e2d3d2a2e5b7a35d5ee351a814
Signed-off-by: Daniel R. Carvalho <[email protected]>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/43365
Tested-by: kokoro <[email protected]>
Reviewed-by: Gabe Black <[email protected]>
Reviewed-by: Bobby R. Bruce <[email protected]>
Maintainer: Gabe Black <[email protected]>
Maintainer: Bobby R. Bruce <[email protected]>
---
M util/style/verifiers.py
1 file changed, 61 insertions(+), 11 deletions(-)

Approvals:
Gabe Black: Looks good to me, but someone else must approve; Looks good to me, approved
  Bobby R. Bruce: Looks good to me, approved; Looks good to me, approved
  kokoro: Regressions pass



diff --git a/util/style/verifiers.py b/util/style/verifiers.py
index c5beb02..efc347e 100644
--- a/util/style/verifiers.py
+++ b/util/style/verifiers.py
@@ -456,31 +456,81 @@
                               "comparisons with false/False.\n")
         return line

-class ClassBraces(LineVerifier):
-    """ Check if the opening braces of classes are not on the same line of
-        the class name.
+class StructureBraces(LineVerifier):
+ """ Check if the opening braces of structures are not on the same line of + the structure name. This includes classes, structs, enums and unions.

-        @todo Make this work for multi-line class declarations. e.g.,
+ This verifier matches lines starting in optional indent, followed by
+        an optional typedef and the structure's keyword, followed by any
+ character until the first opening brace is seen. Any extra characters
+        after the opening brace are saved for a recursive check, if needed.
+
+        This fixes, for example:
+            1) "struct A {"
+            2) "enum{"
+            3) "    class B { // This is a class"
+            4) "union { struct C {"
+        to:
+            1) "struct A\n{"
+            2) "enum\n{"
+            3) "    class B\n    {\n        // This is a class"
+            4) "union\n{\n        struct C\n        {"
+
+        @todo Make this work for multi-line structure declarations. e.g.,

             class MultiLineClass
               : public BaseClass {
     """

     languages = set(('C', 'C++'))
-    test_name = 'class opening brace position'
-    opt_name = 'classbrace'
+    test_name = 'structure opening brace position'
+    opt_name = 'structurebrace'

- regex = re.compile(r'\A(\s*)((class|struct|enum| union)\s+[A-Z].*\S)\s*\{')
+    # Matches the indentation of the line
+    regex_indentation = '(?P<indentation>\s*)'
+    # Matches an optional "typedef" before the keyword
+    regex_typedef = '(?P<typedef>(typedef\s+)?)'
+    # Matches the structure's keyword
+    regex_keyword = '(?P<keyword>class|struct|enum|union)'
+    # A negative lookahead to avoid incorrect matches with variable's names
+    # e.g., "classifications = {" should not be fixed here.
+    regex_avoid = '(?![^\{\s])'
+    # Matches anything after the keyword and before the opening brace.
+    # e.g., structure name, base type, type of inheritance, etc
+    regex_name = '(?P<name>[^\{]*)'
+    # Matches anything after the opening brace, which should be
+    # parsed recursively
+    regex_extra = '(?P<extra>.*)$'
+    regex = re.compile(r'^' + regex_indentation + regex_typedef +
+        regex_keyword + regex_avoid + regex_name + '\{' + regex_extra)

     def check_line(self, line, **kwargs):
-        return self.regex.search(line) == None
+        return (self.regex.search(line) == None) or \
+            (line.count('{') == line.count('};'))

     def fix_line(self, line, **kwargs):
         match = self.regex.search(line)
+
         if match:
-            # Group 1 is indentation, group 2 is class declaration
-            line = match.group(1) + match.group(2) + "\n" + \
-                match.group(1) + "{"
+            # Move the opening brace to the next line
+            match_indentation = match.group('indentation')
+            match_typedef = match.group('typedef')
+            match_keyword = match.group('keyword')
+            match_name = match.group('name').rstrip()
+            match_extra = match.group('extra').lstrip()
+            line = match_indentation + match_typedef + match_keyword + \
+                match_name + "\n" + match_indentation + "{"
+
+ # The opening brace should be alone in its own line, so move any
+            # extra contents to the next line
+            if match_extra != '':
+                # Check if the extra line obeys the opening brace rule
+                # (in case there are nested declarations)
+                line_extra = match_indentation + "    " + match_extra
+                if not self.check_line(line_extra):
+                    line_extra = self.fix_line(line_extra)
+                line += "\n" + line_extra
+
         return line

 def is_verifier(cls):



5 is the latest approved patch-set.
No files were changed between the latest approved patch-set and the submitted one.
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/43365
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I8f0536dcc2c164e2d3d2a2e5b7a35d5ee351a814
Gerrit-Change-Number: 43365
Gerrit-PatchSet: 7
Gerrit-Owner: Daniel Carvalho <[email protected]>
Gerrit-Reviewer: Bobby R. Bruce <[email protected]>
Gerrit-Reviewer: Daniel Carvalho <[email protected]>
Gerrit-Reviewer: Gabe Black <[email protected]>
Gerrit-Reviewer: kokoro <[email protected]>
Gerrit-MessageType: merged
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to