Skip to content

Commit

Permalink
Added notice for global vars
Browse files Browse the repository at this point in the history
  • Loading branch information
N01ch committed Nov 5, 2021
1 parent 3b7e663 commit 2fe1978
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 4 deletions.
6 changes: 5 additions & 1 deletion norminette/context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from norminette.exceptions import CParsingError
from norminette.lexer.dictionary import operators
from norminette.norm_error import NormError
from norminette.norm_error import NormError, NormWarning
from norminette.scope import GlobalScope, ControlStructure
from norminette.tools.colors import colors
types = [
Expand Down Expand Up @@ -154,6 +154,7 @@ def __init__(self, filename, tokens, debug=0, added_value=[]):
# Rule relative informations
self.history = []
self.errors = []
self.warnings = []
self.tkn_scope = len(tokens)

# Scope informations
Expand Down Expand Up @@ -202,6 +203,9 @@ def find_in_scope(self, value, nested=True):
def new_error(self, errno, tkn):
self.errors.append(NormError(errno, tkn.pos[0], tkn.pos[1]))

def new_warning(self, errno, tkn):
self.warnings.append(NormWarning(errno, tkn.pos[0], tkn.pos[1]))

def get_parent_rule(self):
if len(self.history) == 0:
return ""
Expand Down
16 changes: 16 additions & 0 deletions norminette/norm_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"ENUM_TYPE_NAMING": "Enum name must start with e_",
"UNION_TYPE_NAMING": "Union name must start with u_",
"GLOBAL_VAR_NAMING": "Global variable must start with g_",
"GLOBAL_VAR_DETECTED": "Global variable present in file. Make sure it is a reasonable choice.",
"EOL_OPERATOR": "Logic operator at the end of line",
"EMPTY_LINE_FUNCTION": "Empty line in function",
"EMPTY_LINE_FILE_START": "Empty line at start of file",
Expand Down Expand Up @@ -113,3 +114,18 @@ def __init__(self, errno, line, col=None):

def __str__(self):
return self.prefix + self.error_msg

class NormWarning:
def __init__(self, errno, line, col=None):
self.errno = errno
self.line = line
self.col = col
if col is not None:
self.error_pos = f"(line: {(str(self.line)).rjust(3)}, col: {(str(self.col)).rjust(3)}):\t"
else:
self.error_pos = f"(line: {(str(self.line)).rjust(3)}):\t "
self.prefix = f"Notice: {self.errno:<20} {self.error_pos:>21}"
self.error_msg = f"{errors.get(self.errno, 'WARNING NOT FOUND')}"

def __str__(self):
return self.prefix + self.error_msg
5 changes: 4 additions & 1 deletion norminette/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ def run(self, context, source):
print(context.filename + ": OK!")
else:
print(context.filename + ": Error!")
context.errors = sorted(context.errors, key=cmp_to_key(sort_errs))
context.errors = sorted(context.errors + context.warnings, key=cmp_to_key(sort_errs))
for err in context.errors:
print(err)
# context.warnings = sorted(context.warnings, key=cmp_to_key(sort_errs))
# for warn in context.warnings:
# print (warn)
6 changes: 4 additions & 2 deletions norminette/rules/check_global_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def run(self, context):
i = context.skip_ws(i)
while context.check_token(i, "IDENTIFIER") is False:
i += 1
if context.peek_token(i) is not None and context.peek_token(i).value.startswith("g_") is False and context.peek_token(i).value != "environ":
context.new_error("GLOBAL_VAR_NAMING", context.peek_token(i))
if context.peek_token(i) is not None and context.peek_token(i).value != "environ":
context.new_warning("GLOBAL_VAR_DETECTED", context.peek_token(0))
if context.peek_token(i).value.startswith("g_") is False:
context.new_error("GLOBAL_VAR_NAMING", context.peek_token(i))
return False, i
2 changes: 2 additions & 0 deletions norminette/tests/rules/ko_struct_indent.out
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
<RBRACE> <NEWLINE>
ko_struct_indent.c: Error!
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Notice: GLOBAL_VAR_DETECTED (line: 2, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: GLOBAL_VAR_NAMING (line: 2, col: 29): Global variable must start with g_
Error: MISALIGNED_VAR_DECL (line: 2, col: 29): Misaligned variable declaration
Notice: GLOBAL_VAR_DETECTED (line: 3, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: MISALIGNED_VAR_DECL (line: 3, col: 29): Misaligned variable declaration
Error: MULT_DECL_LINE (line: 3, col: 34): Multiple declarations on a single line
Error: MISALIGNED_VAR_DECL (line: 7, col: 29): Misaligned variable declaration
Expand Down
2 changes: 2 additions & 0 deletions norminette/tests/rules/ko_struct_name.out
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
ko_struct_name.c: Error!
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Error: USER_DEFINED_TYPEDEF (line: 1, col: 25): User defined typedef must start with t_
Notice: GLOBAL_VAR_DETECTED (line: 2, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: GLOBAL_VAR_NAMING (line: 2, col: 25): Global variable must start with g_
Notice: GLOBAL_VAR_DETECTED (line: 3, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: GLOBAL_VAR_NAMING (line: 3, col: 25): Global variable must start with g_
Error: USER_DEFINED_TYPEDEF (line: 8, col: 25): User defined typedef must start with t_
Error: USER_DEFINED_TYPEDEF (line: 10, col: 5): User defined typedef must start with t_
2 changes: 2 additions & 0 deletions norminette/tests/rules/ko_too_many_instruction.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
ko_too_many_instruction.c - IsBlockEnd In "Function" from "GlobalScope" line 6":
<RBRACE> <NEWLINE>
ko_too_many_instruction.c: Error!
Notice: GLOBAL_VAR_DETECTED (line: 1, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Notice: GLOBAL_VAR_DETECTED (line: 1, col: 10): Global variable present in file. Make sure it is a reasonable choice.
Error: TOO_MANY_INSTR (line: 1, col: 10): Too many instructions on a single line
Error: MISALIGNED_VAR_DECL (line: 1, col: 17): Misaligned variable declaration
Error: TOO_FEW_TAB (line: 5, col: 23): Missing tabs for indent level
Expand Down
2 changes: 2 additions & 0 deletions norminette/tests/rules/ko_var_name.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
ko_var_name.c - IsVarDeclaration In "GlobalScope" from "None" line 2":
<INT> <SPACE> <IDENTIFIER=NotValidEither> <SEMI_COLON> <NEWLINE>
ko_var_name.c: Error!
Notice: GLOBAL_VAR_DETECTED (line: 1, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Error: SPACE_REPLACE_TAB (line: 1, col: 4): Found space when expecting tab
Error: FORBIDDEN_CHAR_NAME (line: 1, col: 5): user defined identifiers should contain only lowercase characters, digits or '_'
Error: GLOBAL_VAR_NAMING (line: 1, col: 5): Global variable must start with g_
Notice: GLOBAL_VAR_DETECTED (line: 2, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: SPACE_REPLACE_TAB (line: 2, col: 4): Found space when expecting tab
Error: FORBIDDEN_CHAR_NAME (line: 2, col: 5): user defined identifiers should contain only lowercase characters, digits or '_'
Error: GLOBAL_VAR_NAMING (line: 2, col: 5): Global variable must start with g_
3 changes: 3 additions & 0 deletions norminette/tests/rules/long_test.out
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,12 @@ Error: TOO_FEW_TAB (line: 31, col: 41): Missing tabs for indent level
Error: TOO_MANY_INSTR (line: 31, col: 41): Too many instructions on a single line
Error: RETURN_PARENTHESIS (line: 31, col: 48): Return value must be in parenthesis
Error: TOO_MANY_INSTR (line: 31, col: 52): Too many instructions on a single line
Notice: GLOBAL_VAR_DETECTED (line: 33, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: GLOBAL_VAR_NAMING (line: 33, col: 7): Global variable must start with g_
Notice: GLOBAL_VAR_DETECTED (line: 34, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: GLOBAL_VAR_NAMING (line: 34, col: 9): Global variable must start with g_
Error: MISALIGNED_VAR_DECL (line: 34, col: 19): Misaligned variable declaration
Notice: GLOBAL_VAR_DETECTED (line: 38, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: SPACE_REPLACE_TAB (line: 38, col: 4): Found space when expecting tab
Error: GLOBAL_VAR_NAMING (line: 38, col: 8): Global variable must start with g_
Error: NO_SPC_AFR_PAR (line: 38, col: 17): Extra space after parenthesis (brace/bracket)
Expand Down
1 change: 1 addition & 0 deletions norminette/tests/rules/ok_comment.out
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@
<RBRACE> <NEWLINE>
ok_comment.c: Error!
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Notice: GLOBAL_VAR_DETECTED (line: 3, col: 1): Global variable present in file. Make sure it is a reasonable choice.
1 change: 1 addition & 0 deletions norminette/tests/rules/ok_func_classic.out
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@
ok_func_classic.c - IsBlockEnd In "Function" from "GlobalScope" line 50":
<RBRACE> <NEWLINE>
ok_func_classic.c: Error!
Notice: GLOBAL_VAR_DETECTED (line: 1, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
1 change: 1 addition & 0 deletions norminette/tests/rules/ok_func_name.out
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ ok_func_name.c: Error!
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Error: FORBIDDEN_CHAR_NAME (line: 8, col: 25): user defined identifiers should contain only lowercase characters, digits or '_'
Error: FORBIDDEN_CHAR_NAME (line: 9, col: 25): user defined identifiers should contain only lowercase characters, digits or '_'
Notice: GLOBAL_VAR_DETECTED (line: 11, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: FORBIDDEN_CHAR_NAME (line: 11, col: 25): user defined identifiers should contain only lowercase characters, digits or '_'
Error: FORBIDDEN_CHAR_NAME (line: 13, col: 5): user defined identifiers should contain only lowercase characters, digits or '_'
Error: FORBIDDEN_CHAR_NAME (line: 15, col: 18): user defined identifiers should contain only lowercase characters, digits or '_'
Expand Down
1 change: 1 addition & 0 deletions norminette/tests/rules/ok_func_ptr.out
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
<RBRACE> <NEWLINE>
ok_func_ptr.c: Error!
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Notice: GLOBAL_VAR_DETECTED (line: 2, col: 1): Global variable present in file. Make sure it is a reasonable choice.
1 change: 1 addition & 0 deletions norminette/tests/rules/ok_protection.out
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
<ENDIF=#endif> <NEWLINE>
ok_protection.h: Error!
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Notice: GLOBAL_VAR_DETECTED (line: 6, col: 1): Global variable present in file. Make sure it is a reasonable choice.
2 changes: 2 additions & 0 deletions norminette/tests/rules/ok_struct_name.out
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@
<RBRACE> <NEWLINE>
ok_struct_name.c: Error!
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Notice: GLOBAL_VAR_DETECTED (line: 11, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Notice: GLOBAL_VAR_DETECTED (line: 12, col: 1): Global variable present in file. Make sure it is a reasonable choice.
2 changes: 2 additions & 0 deletions norminette/tests/rules/test_file_1012_4.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
test_file_1012_4.c - IsVarDeclaration In "GlobalScope" from "None" line 2":
<IDENTIFIER=t_montype> <TAB> <IDENTIFIER=g_glob> <SEMI_COLON>
test_file_1012_4.c: Error!
Notice: GLOBAL_VAR_DETECTED (line: 1, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Notice: GLOBAL_VAR_DETECTED (line: 2, col: 1): Global variable present in file. Make sure it is a reasonable choice.
5 changes: 5 additions & 0 deletions norminette/tests/rules/test_file_210316.out
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,16 @@ Error: SPACE_AFTER_KW (line: 3, col: 20): Missing space after keyword
Error: SPACE_AFTER_KW (line: 3, col: 50): Missing space after keyword
Error: SPACE_AFTER_KW (line: 4, col: 28): Missing space after keyword
Error: SPACE_AFTER_KW (line: 4, col: 57): Missing space after keyword
Notice: GLOBAL_VAR_DETECTED (line: 7, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: DECL_ASSIGN_LINE (line: 13, col: 28): Declaration and assignation on a single line
Error: DECL_ASSIGN_LINE (line: 14, col: 12): Declaration and assignation on a single line
Error: DECL_ASSIGN_LINE (line: 15, col: 12): Declaration and assignation on a single line
Error: DECL_ASSIGN_LINE (line: 16, col: 12): Declaration and assignation on a single line
Error: DECL_ASSIGN_LINE (line: 17, col: 12): Declaration and assignation on a single line
Notice: GLOBAL_VAR_DETECTED (line: 20, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Notice: GLOBAL_VAR_DETECTED (line: 34, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Notice: GLOBAL_VAR_DETECTED (line: 35, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: NO_SPC_BFR_PAR (line: 35, col: 65): Extra space before parenthesis (brace/bracket)
Notice: GLOBAL_VAR_DETECTED (line: 36, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: NO_SPC_BFR_PAR (line: 36, col: 64): Extra space before parenthesis (brace/bracket)
Error: NO_SPC_BFR_PAR (line: 38, col: 53): Extra space before parenthesis (brace/bracket)
1 change: 1 addition & 0 deletions norminette/tests/rules/testfile_210104_4.out
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
testfile_210104_4.c - IsBlockEnd In "Function" from "GlobalScope" line 11":
<RBRACE> <NEWLINE>
testfile_210104_4.c: Error!
Notice: GLOBAL_VAR_DETECTED (line: 1, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Error: SPC_AFTER_OPERATOR (line: 5, col: 7): missing space after operator

0 comments on commit 2fe1978

Please sign in to comment.