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 <odanrc@yahoo.com.br>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/43365
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Gabe Black <gabe.black@gmail.com>
Reviewed-by: Bobby R. Bruce <bbruce@ucdavis.edu>
Maintainer: Gabe Black <gabe.black@gmail.com>
Maintainer: Bobby R. Bruce <bbruce@ucdavis.edu>
This commit is contained in:
committed by
Daniel Carvalho
parent
5c8983fc18
commit
d67f28473a
@@ -456,31 +456,81 @@ class BoolCompare(LineVerifier):
|
||||
"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):
|
||||
|
||||
Reference in New Issue
Block a user