This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
forked from doom/normoulinette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
normoulinette
executable file
·324 lines (292 loc) · 13.7 KB
/
normoulinette
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/python3
#-*- coding: utf-8 -*-
import os, sys, re
def listSourceFiles(baseDir="."):
files = []
for dirpath, dirnames, filenames in os.walk(baseDir):
for f in filenames:
if f.endswith(".c") or f.endswith(".h"):
files.append(dirpath + ('/' if dirpath[-1] != '/' else '') + f)
return files
class TermFormat():
white="\033[39m"
red="\033[31m"
cyan="\033[36m"
bold="\033[1m"
nobold="\033[21m"
class SourceLine(str):
def __new__(cls, data):
self = str.__new__(cls, data)
self.quotedZones = []
self.commentedZones = []
self.findQuotedZones()
self.findCommentedZones()
return self
def addQuotedZone(self, zone):
self.quotedZones.append(zone)
def addCommentedZone(self, zone):
self.commentedZones.append(zone)
def isInQuotedZone(self, idx):
for z in self.quotedZones:
if idx in z:
return True
return False
def isInCommentedZone(self, idx):
for z in self.commentedZones:
if idx in z:
return True
return False
def findCommentedZones(self):
for m in re.finditer(r'/\*.*\*/', self):
self.addCommentedZone(range(m.start(0), m.end(0)))
def findQuotedZones(self):
for m in re.finditer(r'(?:"[^"]*\\(?:.[^"]*\\)*.[^"]*")|(?:"[^"]*")', self):
self.addQuotedZone(range(m.start(0), m.end(0)))
def isInCode(self, idx):
return not (self.isInCommentedZone(idx) or self.isInQuotedZone(idx))
class FileChecker(object):
"""Une classe qui vérifie la norme des fichiers .c, .h et Makefile"""
def __init__(self, path, libcCheck=True):
self.path = path
self.libcCheck = libcCheck
self.lines = None
if path.endswith(".h"):
self.macro = path.split('/')[-1].upper().replace('.', '_') + '_'
def printNormError(self, msg, globalScope=False):
"""Affiche une erreur de norme"""
self.errCount += 1
fmt = TermFormat.red + TermFormat.bold + '[' \
+ self.path + (":" + str(self.cur + 1) if not globalScope else "") \
+ "] " + TermFormat.white
print(fmt + msg + TermFormat.nobold)
def isFound(self, res):
"""Teste si l'objet passé en paramètre correspond à un match valide"""
return res and self.line.isInCode(res.start(0))
def checkHeaderLine(self):
"""Vérifie (basiquement) la présence du header Epitech"""
if (self.cur == 0 and self.line != "/*") \
or (self.cur == 5 and self.line != "*/") \
or (self.cur not in (0, 5) and not self.line.startswith("**")):
self.printNormError("Header invalide")
self.inHeader = False
def checkCommon(self):
"""Vérifie la longueur de la ligne et la présence d'espaces en trop à la fin"""
tmp = self.line.replace('\t', ' ')
l = len(tmp)
if l > 80:
self.printNormError("Ligne trop longue ({} > 80)".format(l))
if l > 0 and tmp[-1] == ' ':
self.printNormError("Espace en fin de ligne")
def checkComments(self):
"""Vérifie la présence et la validité des commentaires"""
if len(self.line.commentedZones) and self.inFunction:
self.printNormError("Commentaire dans le code")
pos = self.line.find("//")
if pos != -1 and self.line.isInCode(pos):
self.printNormError("Mauvais type de commentaire")
self.line = SourceLine(self.line.split("//")[0])
endPos = self.line.find("*/")
if self.inComment:
if endPos != -1 and not self.line.isInQuotedZone(endPos):
self.line = SourceLine(self.line[endPos + 2:])
return False
else:
return True
pos = self.line.find("/*")
if pos != -1 and not self.line.isInQuotedZone(pos) \
and (endPos == -1 or self.line.isInQuotedZone(endPos)):
if self.inFunction:
self.printNormError("Commentaire dans le code")
self.line = SourceLine(self.line[:pos])
return True
return False
def checkIncludes(self):
"""Vérifie l'ordre des directives #include"""
a, b = self.line.startswith("#include"), self.line.startswith("# include")
if b and self.path.endswith(".c") or (a and self.path.endswith(".h")):
self.printNormError("#include mal indenté")
if a or b:
if self.line[-1] == '"' and self.atSysInc:
self.atSysInc = False
elif self.line[-1] == '>' and not self.atSysInc:
self.printNormError("#include système mal placé")
def checkNormalMacro(self):
"""Vérifie les placements, les noms et les longueurs des macros"""
exp = re.compile('#[ \t]*define')
if self.isFound(re.search(exp, self.line)):
if self.path.endswith(".c"):
self.printNormError("Utilisation de #define dans un fichier .c")
name = self.line.replace('\t', ' ').split('define ')[1].split(' ')[0].split('(')[0]
if re.compile('[A-Z0-9_]+').match(name) == None:
self.printNormError("Caractères incorrects dans la macro " + name + " (A-Z, 0-9 et _ autorisés)")
if re.compile('\\\\[ \t]*$').search(self.line):
self.printNormError("La macro " + name + " fait plus d'une ligne")
def checkDeclaration(self):
"""Vérifie les déclarations de variables"""
exp = re.compile('(static )?(const )?[a-z0-9_]+[ \t]+(\*)?[a-z0-9_]+(\[?[A-Z0-9_]*\])*[ \t]*[;=][ \t]*')
if self.isFound(re.search(exp, self.line)) and self.line.find("typedef") == -1:
if not self.inFunction and not self.inStruct:
exp = re.compile('(static )?(const )?[a-z0-9_]+[ \t]+(\*)?g_[a-z0-9_]+(\[?[A-Z0-9_]*\])*[ \t]*[;=][ \t]*')
res = re.search(exp, self.line)
if res == None:
self.printNormError("Nom de globale sans g_")
elif self.path.endswith(".c") and self.inFunction:
return
def checkSpaces(self):
"""Vérifie la présence d'espace après les mots-clefs"""
if self.path.endswith(".c"):
exp = re.compile('[ \t](if|else|return|while)[^ \t]')
res = re.search(exp, self.line)
if self.isFound(res):
self.printNormError("Espace manquant après le mot-clef " + res.group()[1:-1])
if self.isFound(re.compile("return[ \t]+[^;\(]+").search(self.line)):
self.printNormError("Parenthèses manquantes après le mot-clef return")
l = len(self.line)
p = self.line.find(',')
while p != -1:
if p < l - 1 and self.line[p + 1] not in ' \t\'' and not self.line.isInQuotedZone(p):
self.printNormError("Espace manquant après la virgule")
p = self.line.find(',', p + 1)
def checkProto(self):
"""Vérifie les prototypes et les définitions de fonctions"""
exp = re.compile('(static )?(inline )?(const )?[a-z0-9_]+[ \t]+(\*)*[a-zA-Z0-9_]+'
'\(([a-zA-Z0-9_]+[ \t]+[a-zA-Z0-9_]+,?)+\)[ \t]*;?')
res = re.search(exp, self.line)
if res:
m = re.search('[a-zA-Z0-9]+\(', res.group())
name = self.line[m.start(0):m.end(0) - 1]
params = self.line[m.end(0):self.line.rfind(')')]
if re.findall("[A-Z]+", name):
self.printNormError("Majuscule dans le nom de la fonction " + name)
def checkFunction(self):
"""Vérifie le nombre de lignes d'une fonction, et le nombre de fonctions"""
if self.line == "}":
self.inFunction -= 1
elif self.line == "{":
self.inFunction += 1
self.funcLinesCount = 0
self.functionCount += 1
if self.functionCount > 5:
self.printNormError("Fonction en trop (5 fonctions max)")
elif self.inFunction > 0:
self.funcLinesCount += 1
if self.funcLinesCount > 20:
self.printNormError("Ligne en trop dans la fonction (20 lignes max)")
def checkTypedef(self):
"""Vérifie la validité des noms de types personnalisés"""
if self.line.startswith("typedef"):
if self.line[-1] != ';':
lastWord = self.line.rsplit(None, 1)[-1]
typeName = (lastWord[2:] if lastWord[:2] in ("s_", "e_", "u_") else lastWord)
self.typedefs.append((self.inStruct + 1, "t_" + typeName))
else:
words = re.findall('[\w\']+', self.line)
if not words[-1].startswith("t_") and self.line.find("(*") == -1:
self.printNormError("Nom de type personnalisé sans t_")
elif len(words) == 4 and words[1] in ("struct", "enum", "union") \
and words[2][1] == '_' and words[2][2:] != words[3][2:]:
self.printNormError("Le nom du type devrait être t" + words[2][1:])
def checkStruct(self):
"""Vérifie la définition d'une structure ou d'une union"""
if self.isFound(re.match("[ \t]*(typedef[ \t]*)?(struct|union|enum)([ \t]+[_a-zA-Z0-9]+)?$", self.line)) \
and self.line[-1] != ';':
self.inStruct += 1
res = re.search(re.compile("}.*;"), self.line)
if self.inStruct and self.isFound(res):
if len(self.typedefs) and self.typedefs[-1][0] == self.inStruct:
lastWord = self.line.rsplit(None, 1)[-1]
typeName = self.typedefs.pop()[1]
if typeName != lastWord[:-1]:
self.printNormError("Le nom du type devrait être " + typeName)
self.inStruct -= 1
def checkTypeKeyword(self, keyword, desc):
"""Vérifie la nomination des types définis dans le programme"""
res = re.match("[ \t]*(typedef[ \t]*)?" + keyword + '[ \t]+[_a-zA-Z0-9_]+;?$', self.line)
if self.isFound(res):
if not res.group().replace('\t', ' ').split(' ')[-1].startswith(keyword[0] + '_'):
self.printNormError("Nom " + desc + " sans " + keyword[0] + "_")
def checkProtectMacro(self):
"""Vérifie la présence et la syntaxe de la macro de protection des fichier .h"""
def checkForbidden(self):
"""Vérifie la présence de fonctions et de mots-clefs interdits par la norme"""
exp = re.compile('[ \t](goto)([ \t\(])')
#for and switch are allowed on Style-Coding-Epitech2019
#exp = re.compile('[ \t](goto|for|switch)([ \t\(])')
res = re.search(exp, self.line)
if self.isFound(res):
self.printNormError("Mot-clef interdit : " + res.group()[1:-1])
if self.libcCheck:
exp = re.compile('[ \t](strcat|strchr|strcmp|strcoll|strcoll_l|strcpy|strcspn|strdup'
'|strerror|strerror_l|strerror_r|strlen|strncat|strncmp|strncpy'
'|strndup|strnlen|strpbrk|strrchr|strsignal|strspn|strstr|strtok'
'|strtok_r|strxfrm|strncmp|strdup|memmove|memcpy|memcmp|calloc'
'|realloc|printf|dprintf|atoi|atof)([ \t\(])')
res = re.search(exp, self.line)
if self.isFound(res):
self.printNormError("Fonction interdite : " + res.group()[1:-1])
def checkLine(self):
"""Vérifie le respect de la norme sur la ligne courante"""
if self.cur <= 5 and self.inHeader:
self.checkHeaderLine()
return
if not self.line:
if self.lastWasEmpty:
self.printNormError("Double ligne vide")
self.lastWasEmpty = True
return
self.lastWasEmpty = False
self.checkCommon()
self.inComment = self.checkComments()
if not self.inComment:
self.checkIncludes()
if self.path.endswith(".h"):
self.checkProtectMacro()
self.checkNormalMacro()
self.checkTypedef()
self.checkStruct()
self.checkTypeKeyword("struct", "de structure")
self.checkTypeKeyword("enum", "d'énumération")
self.checkTypeKeyword("union", "d'union")
self.checkDeclaration()
self.checkSpaces()
self.checkProto()
if self.inStruct == 0:
self.checkFunction()
self.checkForbidden()
def check(self):
"""Vérifie le respect de la norme dans le fichier associé"""
with open(self.path, 'r') as f:
self.lines = f.read().splitlines()
if self.path.startswith("./"):
self.path = self.path[2:]
print(TermFormat.bold + "Lecture de " + TermFormat.cyan
+ self.path + TermFormat.white + TermFormat.nobold)
self.errCount = 0
self.lastWasEmpty = False
self.inComment = False
self.inHeader = True
self.atSysInc = True
self.inStruct = 0
self.inFunction = 0
self.functionCount = 0
self.typedefs = []
for self.cur, l in enumerate(self.lines):
line = SourceLine(l)
self.line = line
self.checkLine()
if self.lastWasEmpty:
self.printNormError("Ligne vide en fin de fichier")
return self.errCount
if len(sys.argv) > 1:
files = []
for arg in sys.argv[1:]:
files += listSourceFiles(arg) if (os.path.isdir(arg)) else [arg]
else:
files = listSourceFiles()
errCount = 0
for path in files:
f = FileChecker(path)
errCount += f.check()
print(TermFormat.bold + TermFormat.red + "Vous avez fait", errCount, \
("fautes" if errCount > 1 else "faute"), "de norme" \
+ TermFormat.white + TermFormat.nobold)