Skip to content

Commit

Permalink
fixed norminette validating wrong includes (#405)
Browse files Browse the repository at this point in the history
* fixed norminette validating includes without spaces between include and filename or with one tab between include and filename

* fixed small bug and added test cases
  • Loading branch information
pruiz-ca authored Jul 11, 2023
1 parent 4ae8971 commit 898ebd9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions norminette/norm_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"MIXED_SPACE_TAB": "Mixed spaces and tabs",
"ATTR_EOL": "Function attribute must be at the end of line",
"INVALID_HEADER": "Missing or invalid 42 header",
"INCLUDE_MISSING_SP": "Missing space between include and filename",
}


Expand Down
6 changes: 4 additions & 2 deletions norminette/rules/check_preprocessor_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def run(self, context):
val = context.peek_token(i).value.split("include", 1)[1]
content = Lexer(val, context.peek_token(i).pos[0])
tkns = content.get_tokens()
i = 1
i = 0
if tkns[i].type not in ["TAB", "SPACE"]:
context.new_error("INCLUDE_MISSING_SP", tkns[i])
while i < len(tkns) and tkns[i].type in ["TAB", "SPACE"]:
if tkns[i].type == "TAB":
context.new_error("TAB_INSTEAD_SPC", tkns[i])
Expand All @@ -44,7 +46,7 @@ def run(self, context):
filetype = tkns[i].value.split(".")[-1][0]
except:
filetype = ""
while tkns[i].type != "NEWLINE" and i < len(tkns) - 1:
while i < len(tkns) - 1 and tkns[i].type != "NEWLINE":
i += 1
if (tkns[i].type == "NEWLINE" and tkns[i - 1].type in ["SPACE", "TAB"]) or tkns[
i
Expand Down
6 changes: 6 additions & 0 deletions tests/rules/samples/ko_include2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include"libft.h"
#include "libft.h"
#include "libft.h"
#include<unistd.h>
#include <unistd.h>
#include <unistd.h>
22 changes: 22 additions & 0 deletions tests/rules/samples/ko_include2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
ko_include2.c - IsPreprocessorStatement In "GlobalScope" from "None" line 1":
<INCLUDE=#include"libft.h"> <NEWLINE>
ko_include2.c - IsPreprocessorStatement In "GlobalScope" from "None" line 2":
<INCLUDE=#include "libft.h"> <NEWLINE>
ko_include2.c - IsPreprocessorStatement In "GlobalScope" from "None" line 3":
<INCLUDE=#include "libft.h"> <NEWLINE>
ko_include2.c - IsPreprocessorStatement In "GlobalScope" from "None" line 4":
<INCLUDE=#include<unistd.h>> <NEWLINE>
ko_include2.c - IsPreprocessorStatement In "GlobalScope" from "None" line 5":
<INCLUDE=#include <unistd.h>> <NEWLINE>
ko_include2.c - IsPreprocessorStatement In "GlobalScope" from "None" line 6":
<INCLUDE=#include <unistd.h>> <NEWLINE>
ko_include2.c: Error!
Error: INCLUDE_MISSING_SP (line: 1, col: 1): Missing space between include and filename
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Error: TAB_INSTEAD_SPC (line: 2, col: 2): Found tab when expecting space
Error: TAB_INSTEAD_SPC (line: 3, col: 3): Found tab when expecting space
Error: TAB_INSTEAD_SPC (line: 3, col: 5): Found tab when expecting space
Error: INCLUDE_MISSING_SP (line: 4, col: 4): Missing space between include and filename
Error: TAB_INSTEAD_SPC (line: 5, col: 5): Found tab when expecting space
Error: TAB_INSTEAD_SPC (line: 6, col: 6): Found tab when expecting space
Error: TAB_INSTEAD_SPC (line: 6, col: 9): Found tab when expecting space

0 comments on commit 898ebd9

Please sign in to comment.