Skip to content

Commit

Permalink
Close 42School#203 - Fixed errors with __attribute__ on func prototyp…
Browse files Browse the repository at this point in the history
…es, declarations
  • Loading branch information
N01ch committed Aug 26, 2021
1 parent d02232a commit d778eb5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions norminette/norm_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"COMMENT_ON_INSTR": "Comment must be on its own line",
"COMMA_START_LINE": "Comma at line start",
"MIXED_SPACE_TAB": "Mixed spaces and tabs",
"ATTR_EOL": "Function attribute must be at the end of line",
}


Expand Down
6 changes: 6 additions & 0 deletions norminette/rules/check_func_spacing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def run(self, context):
"""
Function return type and function name must be separated by a tab
"""
i = 0
while i < context.fname_pos:
if context.check_token(i, "IDENTIFIER") is True and context.peek_token(i).value == "__attribute__":
context.new_error("ATTR_EOL", context.peek_token(i))
break
i += 1
i = context.fname_pos - 1
while context.check_token(i, ["MULT", "BWISE_AND", "LPARENTHESIS"]) is True:
i -= 1
Expand Down
6 changes: 5 additions & 1 deletion norminette/rules/check_prototype_indent.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def run(self, context):
i += 1
i = context.skip_ws(i)
i = context.skip_nest(i) + 1
type_identifier_nb += 1
if context.check_token(i, "LPARENTHESIS") is True:
if context.parenthesis_contain(i)[0] == "pointer":
i += 1
Expand All @@ -72,9 +73,12 @@ def run(self, context):
i = 0
while context.check_token(i, eol) is False:
if context.check_token(i, "IDENTIFIER") is True and context.peek_token(i).value == "__attribute__":
if type_identifier_nb > 0:
context.new_error("ATTR_EOL", context.peek_token(i))
i += 1
i = context.skip_ws(i)
i = context.skip_nest(i) + 1
i = context.skip_nest(i)
type_identifier_nb -= 1
elif context.check_token(i, keywords) is True and type_identifier_nb > 0:
type_identifier_nb -= 1
if context.peek_token(i).length == 0:
Expand Down
8 changes: 6 additions & 2 deletions norminette/rules/is_func_declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from norminette.context import GlobalScope
from norminette.rules import PrimaryRule

import pdb

whitespaces = ["SPACE", "TAB"]
preproc = [
Expand Down Expand Up @@ -124,7 +125,7 @@ def check_func_format(self, context):
if context.check_token(i, "IDENTIFIER") is True and context.peek_token(i).value == "__attribute__":
i += 1
i = context.skip_ws(i)
i = context.skip_nest(i) + 1
i = context.skip_nest(i)
if context.check_token(i, "NEWLINE") is True and identifier == False and misc_id == [] and type_id == []:
return False, 0
if context.check_token(i, misc_identifier) is True:
Expand Down Expand Up @@ -171,7 +172,6 @@ def check_func_format(self, context):
i += 1
else:
i += 1
# print (type_id, args, identifier)
if len(type_id) > 0 and args == True and identifier != None:
i = identifier[1]
i = context.skip_ws(i, nl=True)
Expand All @@ -192,6 +192,10 @@ def check_func_format(self, context):
i = context.skip_nest(i)
i += 1
i = context.skip_ws(i, nl=True)
if context.check_token(i, "IDENTIFIER") is True and context.peek_token(i).value == "__attribute__":
i += 1
i = context.skip_ws(i)
i = context.skip_nest(i) + 1
return True, i
return False, 0

Expand Down
14 changes: 14 additions & 0 deletions norminette/tests/rules/check_attributes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
void fatal(void) __attribute__((noreturn));

extern int ft_printf(void *obj, const char *format, ...)
__attribute__ ((format (printf, 2, 3)));

float __attribute__((overloadable)) len(t_float2 a);

float len(t_float2 a) __attribute__((overloadable))
{
t_float2 v;

v = a;
return (sqrt(a.x * a.x + a.y * a.y));
}
11 changes: 11 additions & 0 deletions norminette/tests/rules/check_attributes.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Missing or invalid header. Header are being reintroduced as a mandatory part of your files. This is not yet an error.
tests/rules/check_attributes.c - IsFuncPrototype In "GlobalScope" from "None" line 1":
<FLOAT> <SPACE> <IDENTIFIER=__attribute__> <LPARENTHESIS> <LPARENTHESIS> <IDENTIFIER=overloadable> <RPARENTHESIS> <RPARENTHESIS> <SPACE> <IDENTIFIER=len> <LPARENTHESIS> <IDENTIFIER=t_float2> <SPACE> <IDENTIFIER=a> <RPARENTHESIS> <SEMI_COLON> <NEWLINE>
tests/rules/check_attributes.c - IsFuncPrototype In "GlobalScope" from "None" line 2":
<VOID> <TAB> <TAB> <IDENTIFIER=fatal> <LPARENTHESIS> <VOID> <RPARENTHESIS> <SPACE> <IDENTIFIER=__attribute__> <LPARENTHESIS> <LPARENTHESIS> <IDENTIFIER=noreturn> <RPARENTHESIS> <RPARENTHESIS> <SEMI_COLON> <NEWLINE>
tests/rules/check_attributes.c - IsEmptyLine In "GlobalScope" from "None" line 3":
<NEWLINE>
tests/rules/check_attributes.c - IsFuncPrototype In "GlobalScope" from "None" line 4":
<EXTERN> <SPACE> <INT> <TAB> <IDENTIFIER=ft_printf> <LPARENTHESIS> <VOID> <SPACE> <MULT> <IDENTIFIER=obj> <COMMA> <SPACE> <CONST> <SPACE> <CHAR> <SPACE> <MULT> <IDENTIFIER=format> <COMMA> <SPACE> <ELLIPSIS> <RPARENTHESIS> <NEWLINE>
<TAB> <TAB> <IDENTIFIER=__attribute__> <SPACE> <LPARENTHESIS> <LPARENTHESIS> <IDENTIFIER=format> <SPACE> <LPARENTHESIS> <IDENTIFIER=printf> <COMMA> <SPACE> <CONSTANT=2> <COMMA> <SPACE> <CONSTANT=3> <RPARENTHESIS> <RPARENTHESIS> <RPARENTHESIS> <SEMI_COLON>
tests/rules/check_attributes.c: OK!

0 comments on commit d778eb5

Please sign in to comment.