Skip to content

Commit

Permalink
rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Martinez authored and Quentin Martinez committed Mar 7, 2018
1 parent ca5eff4 commit 20d6ae7
Show file tree
Hide file tree
Showing 24 changed files with 505 additions and 101 deletions.
Binary file modified .DS_Store
Binary file not shown.
6 changes: 4 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@ private static void Main(string[] args)


List<IRules> rules = new List<IRules>();


rules.Add(new Header());
rules.Add(new StructureDefinition());
rules.Add(new TypedefUsage());
rules.Add(new NamingConvention("union","u_"));
rules.Add(new NamingConvention("struct", "s_"));
rules.Add(new NamingConvention("enum", "e_"));
rules.Add(new NamingConvention("global", "g_"));
rules.Add(new FunctionsDefinition());
rules.Add(new Variables());
rules.Add(new Indentation());
//rules.Add(new NamingConvention("typedef struct", "s_"));
rules.Add(new Formating());
rules.Add(new Misc());
rules.Add(new Comment());
foreach (var rule in rules)
{
rule.Verify("ft_app.c", str);
Expand Down
33 changes: 33 additions & 0 deletions Rules/Comment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Text.RegularExpressions;

namespace appRegex.Rules
{
public class Comment : IRules
{
public Comment()
{
}
public void Verify(string filename, string[] content)
{
for (int i = 0; i < content.Length; i++)
{
string line = content[i];

Regex reg = new Regex(@"^((\s)*\/\/)\s*");

if (reg.Match(line).Success)
{
Console.WriteLine($"[{filename}]:[{i}] Wrong type comment.");
}
reg = new Regex(@"^((\s)*\/\*)\s*");
if (reg.Match(line).Success)
{
//Console.WriteLine($"[{filename}]:[{i}] Comment in function forbiden.");
}

}

}
}
}
64 changes: 21 additions & 43 deletions Rules/Formating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,43 @@ public void Verify(string filename, string[] content)
Console.WriteLine($"[{filename}]:[{0}] Filenames should respect the snake_case naming convention.");
}
CheckSpaceAfterKeyword(filename, content);

for (int i = 0; i < content.Length; i++)
{
string line = content[i];
if (line.IsComment()) continue;
CheckSpaceVirgule(filename, i, line);
CheckOperatorSpace(filename, i, line);
CheckOperandSpace(filename, i, line);
int count = 0;
foreach (var c in line)
{
if (c == '\t')
count = count + 4;
else
count++;
}
if (count > 80)
// CheckOperatorSpace(filename, i, line);

if (new Regex(@"^\s*\w+\s+\*?\w+\s*\,").Match(line).Success)
Console.WriteLine($"[{filename}]:[{i}] You cannot declare multiple variable at the same time.");

if (line.Length > 80)
Console.WriteLine($"[{filename}]:[{i}] Line size exceeded.");

if (line.EndsWith(" ") || line.EndsWith("\t") || line.EndsWith(" ;") || line.EndsWith("\t;"))
{
Console.WriteLine($"[{filename}]:[{i}] Trailing space(s) at the end of the line.");
}
if (new Regex(@";.+").Match(line).Success)
Console.WriteLine($"[{filename}]:[{i}] Something has been detected after a brace(;).");

if (new Regex(".*\\s$").Match(line).Success)
Console.WriteLine($"[{filename}]:[{i}] Trailing space(s) at the end of the line.");

if (new Regex(@"(if|else)\s*?\(.*\)\s*?\{?.*\}?;$").Match(line).Success)
Console.WriteLine($"[{filename}]:[{i}] Statement cannot be on one line with if/else/else if.");

Regex reg = new Regex("(if.*[^&|=^><+\\-*%\\/!]=[^=].*==.*)|(if.*==.*[^&|=^><+\\-*%\\/!]=[^=].*)", RegexOptions.IgnoreCase);
if (reg.Match(line).Success)
{
Console.WriteLine($"[{filename}]:[{i}] Condition and assignment on the same line.");
}
if (new Regex("([^(\t ]+_t|int|signed|unsigned|char|long|short|float|double|void|const|struct [^ ]+)\\*").Match(line).Success)
{
Console.WriteLine($"[{filename}]:[{i}] Misplaced pointer symbol.");
}

if (new Regex(";").Matches(line).Count > 1)
{
Console.WriteLine($"[{filename}]:[{i}] Multiple statement on same line.");
}
}
}

private void CheckSpaceVirgule(string filename, int position, string str)
{
for (int i = 0; i < str.Length; i++)
{
if (str[i] == ';' || str[i] == ',')
if (str[i] == ',')
{
if (i + 1 != str.Length && str[i + 1] != ' ')
{
Expand Down Expand Up @@ -97,29 +90,14 @@ private void CheckOperatorSpace(string filename, int position, string str)

}

private void CheckOperandSpace(string filename, int position, string str)
{
List<string> list = new List<string>() { "\\=\\=", "\\!\\=", "\\<\\=", "\\>\\=", "\\&\\&", "\\|\\|", "\\+\\=", "\\-\\=", "\\*\\=", "\\/\\=", "\\%\\=", "\\&\\=", "\\^\\=", "\\|\\=", "\\>\\>", "\\<\\<", "\\>\\>\\=", "\\<\\<\\=" };
foreach (var item in list)
{
Regex regStructDefinition = new Regex(@"(?<before>.*)(" + item + ")(?<after>.*)", RegexOptions.IgnoreCase);

Match match = regStructDefinition.Match(str);
string outputOperand = item.Replace("\\", "");
if (match.Success)
{
if (!match.Groups["before"].Value.EndsWith(" ") || !match.Groups["after"].Value.StartsWith(" "))
Console.WriteLine($"[{filename}]:[{position}] The '{outputOperand}' operand must followed by a space.");
}
}
}


private void CheckSpaceAfterKeyword(string filename, string[] content)
{
for (int i = 0; i < content.Length; i++)
{
string line = content[i];

if (line.IsComment()) continue;

Regex regStructDefinition = new Regex("(return|if|else if|else|while)\\(", RegexOptions.IgnoreCase);
Match match = regStructDefinition.Match(line);
Expand All @@ -131,7 +109,7 @@ private void CheckSpaceAfterKeyword(string filename, string[] content)

if (new Regex("return[ \t]+[^;\\(=]+", RegexOptions.IgnoreCase).Match(line).Success && !match.Success)
{
Console.WriteLine($"[{filename}]:[{i}] Missing bracket after keyword return.");
Console.WriteLine($"[{filename}]:[{i}] Missing parenthesis after keyword return.");
}
}
}
Expand Down
164 changes: 153 additions & 11 deletions Rules/FunctionsDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,161 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

using System.Linq;
namespace appRegex.Rules
{
class FunctionsDefinition : IRules
{

public void Verify(string filename, string[] content)
{
CheckDefinition(filename,content);

CheckDefinition(filename,content);
CheckFunction(filename, content);

}
public void CheckFunction(string filename, string[] content)
{
int level = 0;
int line_func_start = 0;
int count = 0;
for (int i = 0; i < content.Length; i++)
{

string line = content[i];
if (line.Contains("{"))
{
if (level == 0)
{
line_func_start = i;
count = -1;
}
level++;
}
else if (line.Contains("}"))
{
level--;
if (level == 0){
if (count > 25){
Console.WriteLine($"[{filename}]:[{line_func_start}] Too many lines for this function ({count}).");
}
CheckVariableLineUp(filename, content, line_func_start, i);
CheckHavingComment(filename, content, line_func_start, i);
CheckVariablePosition(filename, content, line_func_start, i);
CheckVariableCount(filename, content, line_func_start, i);
}

}
count++;

}
}

private void CheckVariablePosition(string filename, string[] content, int from, int to)
{
List<int> lines = new List<int>();
Regex reg = new Regex(@"^( |\t)*((struct|unsigned|long|static)( |\t))*[a-zA-Z0-9_]+( |\t)+(?<variablename>[a-zA-Z0-9_\*\[\]]+)( = [a-zA-Z0-9_\(\)\+\-\/%* \""\',{}\?\:\>\[\]]+)*;", RegexOptions.IgnoreCase);
for (int i = from; i < to; i++)
{
string line = content[i];
if (line.IsComment()) continue;
Match match = reg.Match(line);
if (match.Success)
{
lines.Add(i);
}
}
if (lines.Count() == 0)
return;
bool not_up = false;
int firstVariable = lines.First();
for (int i = 0; i < lines.Count(); i++)
{
if (firstVariable != lines[i])
{
not_up = true;
break;
}
firstVariable++;
}
if (not_up)
{
Console.WriteLine($"[{filename}]:[{from}] Variable must be on the top from the function.");
}
else{
if (!string.IsNullOrEmpty(content[lines.Last() + 1]))
Console.WriteLine($"[{filename}]:[{lines.Last() + 1}] A blank line must be used after variables definitions.");
}


}

private void CheckHavingComment(string filename, string[] content, int from, int to)
{
for (int i = from; i < to; i++)
{
string line = content[i];
if (line.IsComment())
{
Console.WriteLine($"[{filename}]:[{i}] Comment it's forbidden in function.");
}
}
}

private void CheckVariableLineUp(string filename, string[] content, int from, int to)
{
List<int> tab = new List<int>();
List<int> lines = new List<int>();

Regex reg = new Regex(@"^( |\t)*((struct|unsigned|long|static)( |\t))*[a-zA-Z0-9_]+( |\t)+(?<variablename>[a-zA-Z0-9_\*\[\]]+)( = [a-zA-Z0-9_\(\)\+\-\/%* \""\',{}\?\:\>\[\]]+)*;", RegexOptions.IgnoreCase);
for (int i = from; i < to; i++)
{
string line = content[i];
if (line.IsComment()) continue;
Match match = reg.Match(line);
if (match.Success)
{
int lenght = line.GetColumLenght(match.Groups["variablename"].Index);
tab.Add(lenght);
lines.Add(i);
}

}
if (tab.Distinct().Skip(1).Any())
{
Console.WriteLine($"[{filename}]:[{lines.First()}] Variables name has to be lined up.");
}

}

private void CheckVariableCount(string filename, string[] content, int from, int to)
{
List<int> lines = new List<int>();
Regex reg = new Regex(@"^( |\t)*((struct|unsigned|long|static)( |\t))*[a-zA-Z0-9_]+( |\t)+(?<variablename>[a-zA-Z0-9_\*\[\]]+)( = [a-zA-Z0-9_\(\)\+\-\/%* \""\',{}\?\:\>\[\]]+)*;", RegexOptions.IgnoreCase);
for (int i = from; i < to; i++)
{
string line = content[i];
if (line.IsComment()) continue;
Match match = reg.Match(line);
if (match.Success)
{
lines.Add(i);
}

}
if (lines.Count() > 5)
{
Console.WriteLine($"[{filename}]:[{lines.First()}] Too many variables in this function.");
}

}

public void CheckDefinition(string filename, string[] content){
Regex reg = new Regex("(static )?(inline )?(const )?[a-z0-9_]+[ \t]+(\\*)*[a-zA-Z0-9_]+\\(([a-zA-Z0-9_*,]*[ \t]+[a-zA-Z0-9_*,]+,?)*\\)[ \t]*;?",RegexOptions.IgnoreCase);
Regex reg = new Regex("(static )?(inline )?(const )?[a-z0-9_]+[ \t]+(\\*)*[a-zA-Z0-9_]+\\(([a-zA-Z0-9_*,]*[ \t]+[a-zA-Z0-9_*,]+,?|[a-zA-Z0-9_*,])*\\)[ \t]*;?",RegexOptions.IgnoreCase);

for (int i = 0; i < content.Length; i++)
{
string line = content[i];

if (line.IsComment()) continue;

var res = reg.Match(line);
if (res.Success)
Expand All @@ -36,17 +172,23 @@ public void Verify(string filename, string[] content)
Console.WriteLine($"[{filename}]:[{i}] Missing void word for this function.");
}

if (new Regex("[A-Z]+").Match(line).Success){
if (new Regex("[A-Z]+").Match(func_name).Success && !line.Contains("#define")){
Console.WriteLine($"[{filename}]:[{i}] Forbidden uppercase in function name.");
}

if (line.EndsWith(";") && !filename.EndsWith("h") && !filename.StartsWith("static")){
Console.WriteLine($"[{filename}]:[{i}] Forbidden function declaration in c file.");
if (line.EndsWith(";") && !filename.StartsWith("static"))
{
if (filename.EndsWith("c"))
{
Console.WriteLine($"[{filename}]:[{i}] Forbidden function declaration in c file.");
}
}

if (!line.EndsWith(";") && filename.EndsWith("c") && !line.Contains("#define"))
else
{
Console.WriteLine($"[{filename}]:[{i}] Forbidden function implementation in h file.");
if (filename.EndsWith("h") && !line.EndsWith(";") && !line.Contains("#define")){
Console.WriteLine($"[{filename}]:[{i}] Forbidden function implementation in h file.");
}

}

}
Expand Down
21 changes: 20 additions & 1 deletion Rules/Header.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
using System;
using System.Text.RegularExpressions;

namespace appRegex.Rules
{
public class Header
public class Header : IRules
{
public Header()
{
}

public void Verify(string filename, string[] content)
{

var str = string.Concat(content);
var match = new Regex(@"(\/\*([\s\w_\.\>@\<#\/\:\+]|\*{74})+\*\/)", RegexOptions.Multiline).Matches(str);
if (match.Count <= 10){
Console.WriteLine($"[{filename}]:[1] Missing or corrupted header.");
} else{
if (!String.IsNullOrEmpty(content[match.Count]))
{
Console.WriteLine($"[{filename}]:[{match.Count}] Missing an empty line after header.");
}
}


}
}
}
Loading

0 comments on commit 20d6ae7

Please sign in to comment.