-
Notifications
You must be signed in to change notification settings - Fork 90
/
ncc.py
661 lines (572 loc) · 30.3 KB
/
ncc.py
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#!/usr/bin/env python
# MIT License
#
# Copyright (c) 2018 Nithin Nellikunnu ([email protected])
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import logging
import argparse
import yaml
import re
import sys
import difflib
import os
import fnmatch
from clang.cindex import Index
from clang.cindex import CursorKind
from clang.cindex import StorageClass
from clang.cindex import TypeKind
from clang.cindex import Config
# Clang cursor kind to ncc Defined cursor map
default_rules_db = {}
clang_to_user_map = {}
special_kind = {CursorKind.STRUCT_DECL: 1, CursorKind.CLASS_DECL: 1}
file_extensions = [".c", ".cpp", ".h", ".hpp"]
class Rule(object):
def __init__(self, name, clang_kind, parent_kind=None, pattern_str='^.*$'):
self.name = name
self.clang_kind = clang_kind
self.parent_kind = parent_kind
self.pattern_str = pattern_str
self.pattern = re.compile(pattern_str)
self.includes = []
self.excludes = []
def evaluate(self, node, scope=None):
if not self.pattern.match(node.spelling):
fmt = '{}:{}:{}: "{}" does not match "{}" associated with {}\n'
msg = fmt.format(node.location.file.name, node.location.line, node.location.column,
node.displayname, self.pattern_str, self.name)
sys.stderr.write(msg)
return False
return True
class ScopePrefixRule(object):
def __init__(self, pattern_obj):
self.name = "ScopePrefixRule"
self.rule_names = ["Global", "Static", "ClassMember", "StructMember"]
self.global_prefix = ""
self.static_prefix = ""
self.class_member_prefix = ""
self.struct_member_prefix = ""
try:
for key, value in pattern_obj.items():
if key == "Global":
self.global_prefix = value
elif key == "Static":
self.static_prefix = value
elif key == "ClassMember":
self.class_member_prefix = value
elif key == "StructMember":
self.struct_member_prefix = value
else:
raise ValueError(key)
except ValueError as e:
sys.stderr.write('{} is not a valid rule name\n'.format(e.message))
fixit = difflib.get_close_matches(e.message, self.rule_names, n=1, cutoff=0.8)
if fixit:
sys.stderr.write('Did you mean rule name: {} ?\n'.format(fixit[0]))
sys.exit(1)
class DataTypePrefixRule(object):
def __init__(self, pattern_obj):
self.name = "DataTypePrefix"
self.rule_names = ["String", "Integer", "Bool", "Pointer"]
self.string_prefix = ""
try:
for key, value in pattern_obj.items():
if key == "String":
self.string_prefix = value
elif key == "Integer":
self.integer_prefix = value
elif key == "Bool":
self.bool_prefix = value
elif key == "Pointer":
self.pointer_prefix = value
else:
raise ValueError(key)
except ValueError as e:
sys.stderr.write('{} is not a valid rule name\n'.format(e.message))
fixit = difflib.get_close_matches(e.message, self.rule_names, n=1, cutoff=0.8)
if fixit:
sys.stderr.write('Did you mean rule name: {} ?\n'.format(fixit[0]))
sys.exit(1)
class VariableNameRule(object):
def __init__(self, pattern_obj=None):
self.name = "VariableName"
self.pattern_str = "^.*$"
self.rule_names = ["ScopePrefix", "DataTypePrefix", "Pattern"]
self.scope_prefix_rule = None
self.datatype_prefix_rule = None
try:
for key, value in pattern_obj.items():
if key == "ScopePrefix":
self.scope_prefix_rule = ScopePrefixRule(value)
elif key == "DataTypePrefix":
self.datatype_prefix_rule = DataTypePrefixRule(value)
elif key == "Pattern":
self.pattern_str = value
else:
raise ValueError(key)
except ValueError as e:
sys.stderr.write('{} is not a valid rule name\n'.format(e.message))
fixit = difflib.get_close_matches(e.message, self.rule_names, n=1, cutoff=0.8)
if fixit:
sys.stderr.write('Did you mean rule name: {} ?\n'.format(fixit[0]))
sys.exit(1)
except re.error as e:
sys.stderr.write('{} is not a valid pattern \n'.format(e.message))
sys.exit(1)
def get_scope_prefix(self, node, scope=None):
if node.storage_class == StorageClass.STATIC:
return self.scope_prefix_rule.static_prefix
elif (scope is None) and (node.storage_class == StorageClass.EXTERN or
node.storage_class == StorageClass.NONE):
return self.scope_prefix_rule.global_prefix
elif (scope is CursorKind.CLASS_DECL) or (scope is CursorKind.CLASS_TEMPLATE):
return self.scope_prefix_rule.class_member_prefix
elif (scope is CursorKind.STRUCT_DECL):
return self.scope_prefix_rule.struct_member_prefix
return ""
def get_datatype_prefix(self, node):
if node.type.kind is TypeKind.ELABORATED:
if node.type.spelling.startswith('std::string'):
return self.datatype_prefix_rule.string_prefix
elif (node.type.spelling.startswith('std::unique_ptr') or
node.type.spelling.startswith("std::shared_ptr")):
return self.datatype_prefix_rule.pointer_prefix
elif node.type.kind is TypeKind.POINTER:
return self.datatype_prefix_rule.pointer_prefix
else:
if node.type.spelling == "int":
return self.datatype_prefix_rule.integer_prefix
elif node.type.spelling.startswith('bool'):
return self.datatype_prefix_rule.bool_prefix
return ""
def evaluate(self, node, scope=None):
pattern_str = self.pattern_str
scope_prefix = self.get_scope_prefix(node, scope)
datatype_prefix = self.get_datatype_prefix(node)
pattern_str = pattern_str[0] + scope_prefix + datatype_prefix + pattern_str[1:]
pattern = re.compile(pattern_str)
if not pattern.match(node.spelling):
fmt = '{}:{}:{}: "{}" does not have the pattern {} associated with Variable name\n'
msg = fmt.format(node.location.file.name, node.location.line, node.location.column,
node.displayname, pattern_str)
sys.stderr.write(msg)
return False
return True
# All supported rules
default_rules_db["StructName"] = Rule("StructName", CursorKind.STRUCT_DECL)
default_rules_db["UnionName"] = Rule("UnionName", CursorKind.UNION_DECL)
default_rules_db["ClassName"] = Rule("ClassName", CursorKind.CLASS_DECL)
default_rules_db["EnumName"] = Rule("EnumName", CursorKind.ENUM_DECL)
default_rules_db["EnumConstantName"] = Rule("EnumConstantName", CursorKind.ENUM_CONSTANT_DECL)
default_rules_db["FunctionName"] = Rule("FunctionName", CursorKind.FUNCTION_DECL)
default_rules_db["ParameterName"] = Rule("ParameterName", CursorKind.PARM_DECL)
default_rules_db["TypedefName"] = Rule("TypedefName", CursorKind.TYPEDEF_DECL)
default_rules_db["CppMethod"] = Rule("CppMethod", CursorKind.CXX_METHOD)
default_rules_db["Namespace"] = Rule("Namespace", CursorKind.NAMESPACE)
default_rules_db["ConversionFunction"] = Rule("ConversionFunction", CursorKind.CONVERSION_FUNCTION)
default_rules_db["TemplateTypeParameter"] = Rule(
"TemplateTypeParameter", CursorKind.TEMPLATE_TYPE_PARAMETER)
default_rules_db["TemplateNonTypeParameter"] = Rule(
"TemplateNonTypeParameter", CursorKind.TEMPLATE_NON_TYPE_PARAMETER)
default_rules_db["TemplateTemplateParameter"] = Rule(
"TemplateTemplateParameter", CursorKind.TEMPLATE_TEMPLATE_PARAMETER)
default_rules_db["FunctionTemplate"] = Rule("FunctionTemplate", CursorKind.FUNCTION_TEMPLATE)
default_rules_db["ClassTemplate"] = Rule("ClassTemplate", CursorKind.CLASS_TEMPLATE)
default_rules_db["ClassTemplatePartialSpecialization"] = Rule(
"ClassTemplatePartialSpecialization", CursorKind.CLASS_TEMPLATE_PARTIAL_SPECIALIZATION)
default_rules_db["NamespaceAlias"] = Rule("NamespaceAlias", CursorKind.NAMESPACE_ALIAS)
default_rules_db["UsingDirective"] = Rule("UsingDirective", CursorKind.USING_DIRECTIVE)
default_rules_db["UsingDeclaration"] = Rule("UsingDeclaration", CursorKind.USING_DECLARATION)
default_rules_db["TypeAliasName"] = Rule("TypeAliasName", CursorKind.TYPE_ALIAS_DECL)
default_rules_db["ClassAccessSpecifier"] = Rule(
"ClassAccessSpecifier", CursorKind.CXX_ACCESS_SPEC_DECL)
default_rules_db["TypeReference"] = Rule("TypeReference", CursorKind.TYPE_REF)
default_rules_db["CxxBaseSpecifier"] = Rule("CxxBaseSpecifier", CursorKind.CXX_BASE_SPECIFIER)
default_rules_db["TemplateReference"] = Rule("TemplateReference", CursorKind.TEMPLATE_REF)
default_rules_db["NamespaceReference"] = Rule("NamespaceReference", CursorKind.NAMESPACE_REF)
default_rules_db["MemberReference"] = Rule("MemberReference", CursorKind.MEMBER_REF)
default_rules_db["LabelReference"] = Rule("LabelReference", CursorKind.LABEL_REF)
default_rules_db["OverloadedDeclarationReference"] = Rule(
"OverloadedDeclarationReference", CursorKind.OVERLOADED_DECL_REF)
default_rules_db["VariableReference"] = Rule("VariableReference", CursorKind.VARIABLE_REF)
default_rules_db["InvalidFile"] = Rule("InvalidFile", CursorKind.INVALID_FILE)
default_rules_db["NoDeclarationFound"] = Rule("NoDeclarationFound", CursorKind.NO_DECL_FOUND)
default_rules_db["NotImplemented"] = Rule("NotImplemented", CursorKind.NOT_IMPLEMENTED)
default_rules_db["InvalidCode"] = Rule("InvalidCode", CursorKind.INVALID_CODE)
default_rules_db["UnexposedExpression"] = Rule("UnexposedExpression", CursorKind.UNEXPOSED_EXPR)
default_rules_db["DeclarationReferenceExpression"] = Rule(
"DeclarationReferenceExpression", CursorKind.DECL_REF_EXPR)
default_rules_db["MemberReferenceExpression"] = Rule(
"MemberReferenceExpression", CursorKind.MEMBER_REF_EXPR)
default_rules_db["CallExpression"] = Rule("CallExpression", CursorKind.CALL_EXPR)
default_rules_db["BlockExpression"] = Rule("BlockExpression", CursorKind.BLOCK_EXPR)
default_rules_db["IntegerLiteral"] = Rule("IntegerLiteral", CursorKind.INTEGER_LITERAL)
default_rules_db["FloatingLiteral"] = Rule("FloatingLiteral", CursorKind.FLOATING_LITERAL)
default_rules_db["ImaginaryLiteral"] = Rule("ImaginaryLiteral", CursorKind.IMAGINARY_LITERAL)
default_rules_db["StringLiteral"] = Rule("StringLiteral", CursorKind.STRING_LITERAL)
default_rules_db["CharacterLiteral"] = Rule("CharacterLiteral", CursorKind.CHARACTER_LITERAL)
default_rules_db["ParenExpression"] = Rule("ParenExpression", CursorKind.PAREN_EXPR)
default_rules_db["UnaryOperator"] = Rule("UnaryOperator", CursorKind.UNARY_OPERATOR)
default_rules_db["ArraySubscriptExpression"] = Rule(
"ArraySubscriptExpression", CursorKind.ARRAY_SUBSCRIPT_EXPR)
default_rules_db["BinaryOperator"] = Rule("BinaryOperator", CursorKind.BINARY_OPERATOR)
default_rules_db["CompoundAssignmentOperator"] = Rule(
"CompoundAssignmentOperator", CursorKind.COMPOUND_ASSIGNMENT_OPERATOR)
default_rules_db["ConditionalOperator"] = Rule(
"ConditionalOperator", CursorKind.CONDITIONAL_OPERATOR)
default_rules_db["CstyleCastExpression"] = Rule(
"CstyleCastExpression", CursorKind.CSTYLE_CAST_EXPR)
default_rules_db["CompoundLiteralExpression"] = Rule(
"CompoundLiteralExpression", CursorKind.COMPOUND_LITERAL_EXPR)
default_rules_db["InitListExpression"] = Rule("InitListExpression", CursorKind.INIT_LIST_EXPR)
default_rules_db["AddrLabelExpression"] = Rule("AddrLabelExpression", CursorKind.ADDR_LABEL_EXPR)
default_rules_db["StatementExpression"] = Rule("StatementExpression", CursorKind.StmtExpr)
default_rules_db["GenericSelectionExpression"] = Rule(
"GenericSelectionExpression", CursorKind.GENERIC_SELECTION_EXPR)
default_rules_db["GnuNullExpression"] = Rule("GnuNullExpression", CursorKind.GNU_NULL_EXPR)
default_rules_db["CxxStaticCastExpression"] = Rule(
"CxxStaticCastExpression", CursorKind.CXX_STATIC_CAST_EXPR)
default_rules_db["CxxDynamicCastExpression"] = Rule(
"CxxDynamicCastExpression", CursorKind.CXX_DYNAMIC_CAST_EXPR)
default_rules_db["CxxReinterpretCastExpression"] = Rule(
"CxxReinterpretCastExpression", CursorKind.CXX_REINTERPRET_CAST_EXPR)
default_rules_db["CxxConstCastExpression"] = Rule(
"CxxConstCastExpression", CursorKind.CXX_CONST_CAST_EXPR)
default_rules_db["CxxFunctionalCastExpression"] = Rule(
"CxxFunctionalCastExpression", CursorKind.CXX_FUNCTIONAL_CAST_EXPR)
default_rules_db["CxxTypeidExpression"] = Rule("CxxTypeidExpression", CursorKind.CXX_TYPEID_EXPR)
default_rules_db["CxxBoolLiteralExpression"] = Rule(
"CxxBoolLiteralExpression", CursorKind.CXX_BOOL_LITERAL_EXPR)
default_rules_db["CxxNullPointerLiteralExpression"] = Rule(
"CxxNullPointerLiteralExpression", CursorKind.CXX_NULL_PTR_LITERAL_EXPR)
default_rules_db["CxxThisExpression"] = Rule("CxxThisExpression", CursorKind.CXX_THIS_EXPR)
default_rules_db["CxxThrowExpression"] = Rule("CxxThrowExpression", CursorKind.CXX_THROW_EXPR)
default_rules_db["CxxNewExpression"] = Rule("CxxNewExpression", CursorKind.CXX_NEW_EXPR)
default_rules_db["CxxDeleteExpression"] = Rule("CxxDeleteExpression", CursorKind.CXX_DELETE_EXPR)
default_rules_db["CxxUnaryExpression"] = Rule("CxxUnaryExpression", CursorKind.CXX_UNARY_EXPR)
default_rules_db["PackExpansionExpression"] = Rule(
"PackExpansionExpression", CursorKind.PACK_EXPANSION_EXPR)
default_rules_db["SizeOfPackExpression"] = Rule(
"SizeOfPackExpression", CursorKind.SIZE_OF_PACK_EXPR)
default_rules_db["LambdaExpression"] = Rule("LambdaExpression", CursorKind.LAMBDA_EXPR)
default_rules_db["ObjectBoolLiteralExpression"] = Rule(
"ObjectBoolLiteralExpression", CursorKind.OBJ_BOOL_LITERAL_EXPR)
default_rules_db["ObjectSelfExpression"] = Rule("ObjectSelfExpression", CursorKind.OBJ_SELF_EXPR)
default_rules_db["UnexposedStatement"] = Rule("UnexposedStatement", CursorKind.UNEXPOSED_STMT)
default_rules_db["LabelStatement"] = Rule("LabelStatement", CursorKind.LABEL_STMT)
default_rules_db["CompoundStatement"] = Rule("CompoundStatement", CursorKind.COMPOUND_STMT)
default_rules_db["CaseStatement"] = Rule("CaseStatement", CursorKind.CASE_STMT)
default_rules_db["DefaultStatement"] = Rule("DefaultStatement", CursorKind.DEFAULT_STMT)
default_rules_db["IfStatement"] = Rule("IfStatement", CursorKind.IF_STMT)
default_rules_db["SwitchStatement"] = Rule("SwitchStatement", CursorKind.SWITCH_STMT)
default_rules_db["WhileStatement"] = Rule("WhileStatement", CursorKind.WHILE_STMT)
default_rules_db["DoStatement"] = Rule("DoStatement", CursorKind.DO_STMT)
default_rules_db["ForStatement"] = Rule("ForStatement", CursorKind.FOR_STMT)
default_rules_db["GotoStatement"] = Rule("GotoStatement", CursorKind.GOTO_STMT)
default_rules_db["IndirectGotoStatement"] = Rule(
"IndirectGotoStatement", CursorKind.INDIRECT_GOTO_STMT)
default_rules_db["ContinueStatement"] = Rule("ContinueStatement", CursorKind.CONTINUE_STMT)
default_rules_db["BreakStatement"] = Rule("BreakStatement", CursorKind.BREAK_STMT)
default_rules_db["ReturnStatement"] = Rule("ReturnStatement", CursorKind.RETURN_STMT)
default_rules_db["AsmStatement"] = Rule("AsmStatement", CursorKind.ASM_STMT)
default_rules_db["CxxCatchStatement"] = Rule("CxxCatchStatement", CursorKind.CXX_CATCH_STMT)
default_rules_db["CxxTryStatement"] = Rule("CxxTryStatement", CursorKind.CXX_TRY_STMT)
default_rules_db["CxxForRangeStatement"] = Rule(
"CxxForRangeStatement", CursorKind.CXX_FOR_RANGE_STMT)
default_rules_db["MsAsmStatement"] = Rule("MsAsmStatement", CursorKind.MS_ASM_STMT)
default_rules_db["NullStatement"] = Rule("NullStatement", CursorKind.NULL_STMT)
default_rules_db["DeclarationStatement"] = Rule("DeclarationStatement", CursorKind.DECL_STMT)
default_rules_db["TranslationUnit"] = Rule("TranslationUnit", CursorKind.TRANSLATION_UNIT)
default_rules_db["UnexposedAttribute"] = Rule("UnexposedAttribute", CursorKind.UNEXPOSED_ATTR)
default_rules_db["CxxFinalAttribute"] = Rule("CxxFinalAttribute", CursorKind.CXX_FINAL_ATTR)
default_rules_db["CxxOverrideAttribute"] = Rule(
"CxxOverrideAttribute", CursorKind.CXX_OVERRIDE_ATTR)
default_rules_db["AnnotateAttribute"] = Rule("AnnotateAttribute", CursorKind.ANNOTATE_ATTR)
default_rules_db["AsmLabelAttribute"] = Rule("AsmLabelAttribute", CursorKind.ASM_LABEL_ATTR)
default_rules_db["PackedAttribute"] = Rule("PackedAttribute", CursorKind.PACKED_ATTR)
default_rules_db["PureAttribute"] = Rule("PureAttribute", CursorKind.PURE_ATTR)
default_rules_db["ConstAttribute"] = Rule("ConstAttribute", CursorKind.CONST_ATTR)
default_rules_db["NoduplicateAttribute"] = Rule(
"NoduplicateAttribute", CursorKind.NODUPLICATE_ATTR)
default_rules_db["PreprocessingDirective"] = Rule(
"PreprocessingDirective", CursorKind.PREPROCESSING_DIRECTIVE)
default_rules_db["MacroDefinition"] = Rule("MacroDefinition", CursorKind.MACRO_DEFINITION)
default_rules_db["MacroInstantiation"] = Rule("MacroInstantiation", CursorKind.MACRO_INSTANTIATION)
default_rules_db["InclusionDirective"] = Rule("InclusionDirective", CursorKind.INCLUSION_DIRECTIVE)
default_rules_db["TypeAliasTeplateDeclaration"] = Rule(
"TypeAliasTeplateDeclaration", CursorKind.TYPE_ALIAS_TEMPLATE_DECL)
# Reverse lookup map. The parse identifies Clang cursor kinds, which must be mapped
# to user defined types
for key, value in default_rules_db.items():
clang_to_user_map[value.clang_kind] = key
default_rules_db["VariableName"] = Rule("VariableName", CursorKind.VAR_DECL)
clang_to_user_map[CursorKind.FIELD_DECL] = "VariableName"
clang_to_user_map[CursorKind.VAR_DECL] = "VariableName"
class AstNodeStack(object):
def __init__(self):
self.stack = []
def pop(self):
return self.stack.pop()
def push(self, kind):
self.stack.append(kind)
def peek(self):
if len(self.stack) > 0:
return self.stack[-1]
return None
class Options:
def __init__(self):
self.args = None
self._style_file = None
self.file_exclusions = None
self._skip_file = None
self.parser = argparse.ArgumentParser(
prog="ncc.py",
description="ncc is a development tool to help programmers "
"write C/C++ code that adheres to adhere some naming conventions. It automates the "
"process of checking C code to spare humans of this boring "
"(but important) task. This makes it ideal for projects that want "
"to enforce a coding standard.")
self.parser.add_argument('--recurse', action='store_true', dest="recurse",
help="Read all files under each directory, recursively")
self.parser.add_argument('--style', dest="style_file",
help="Read rules from the specified file. If the user does not"
"provide a style file ncc will use all style rules. To print"
"all style rules use --dump option")
self.parser.add_argument('--include', dest='include', nargs="+", help="User defined "
"header file path, this is same as -I argument to the compiler")
self.parser.add_argument('--definition', dest='definition', nargs="+", help="User specified "
"definitions, this is same as -D argument to the compiler")
self.parser.add_argument('--dump', dest='dump', action='store_true',
help="Dump all available options")
self.parser.add_argument('--output', dest='output', help="output file name where"
"naming convenction vialoations will be stored")
self.parser.add_argument('--filetype', dest='filetype', help="File extentions type"
"that are applicable for naming convection validation")
self.parser.add_argument('--clang-lib', dest='clang_lib',
help="Custom location of clang library")
self.parser.add_argument('--exclude', dest='exclude', nargs="+", help="Skip files "
"matching the pattern specified from recursive searches. It "
"matches a specified pattern according to the rules used by "
"the Unix shell")
self.parser.add_argument('--skip', '-s', dest="skip_file",
help="Read list of items to ignore during the check. "
"User can use the skip file to specify character sequences that should "
"be ignored by ncc")
# self.parser.add_argument('--exclude-dir', dest='exclude_dir', help="Skip the directories"
# "matching the pattern specified")
self.parser.add_argument('--path', dest='path', nargs="+",
help="Path of file or directory")
def parse_cmd_line(self):
self.args = self.parser.parse_args()
if self.args.dump:
self.dump_all_rules()
if self.args.style_file:
self._style_file = self.args.style_file
if not os.path.exists(self._style_file):
sys.stderr.write("Style file '{}' not found!\n".format(self._style_file))
sys.exit(1)
if self.args.skip_file:
self._skip_file = self.args.skip_file
if not os.path.exists(self._skip_file):
sys.stderr.write("Skip file '{}' not found!\n".format(self._skip_file))
def dump_all_rules(self):
print("----------------------------------------------------------")
print("{:<35} | {}".format("Rule Name", "Pattern"))
print("----------------------------------------------------------")
for (key, value) in default_rules_db.items():
print("{:<35} : {}".format(key, value.pattern_str))
class SkipDb(object):
def __init__(self, skip_file=None):
self.__skip_db = {}
if skip_file:
self.build_skip_db(skip_file)
def build_skip_db(self, skip_file):
with open(skip_file) as stylefile:
style_rules = yaml.safe_load(stylefile)
for (skip_string, skip_comment) in style_rules.items():
self.__skip_db[skip_string] = skip_comment
def check_skip_db(self, input_query):
if input_query in self.__skip_db.keys():
return 1
else:
return 0
class RulesDb(object):
def __init__(self, style_file=None):
self.__rule_db = {}
self.__clang_db = {}
if style_file:
self.build_rules_db(style_file)
else:
self.__rule_db = default_rules_db
self.__clang_db = clang_to_user_map
def build_rules_db(self, style_file):
with open(style_file) as stylefile:
style_rules = yaml.safe_load(stylefile)
for (rule_name, pattern_str) in style_rules.items():
try:
clang_kind = default_rules_db[rule_name].clang_kind
if clang_kind:
if rule_name == "VariableName":
self.__rule_db[rule_name] = VariableNameRule(pattern_str)
self.__clang_db[CursorKind.FIELD_DECL] = rule_name
self.__clang_db[CursorKind.VAR_DECL] = rule_name
else:
self.__rule_db[rule_name] = default_rules_db[rule_name]
self.__rule_db[rule_name].pattern_str = pattern_str
self.__rule_db[rule_name].pattern = re.compile(pattern_str)
self.__clang_db[clang_kind] = rule_name
except KeyError as e:
sys.stderr.write('{} is not a valid C/C++ construct name\n'.format(e.message))
fixit = difflib.get_close_matches(e.message, default_rules_db.keys(),
n=1, cutoff=0.8)
if fixit:
sys.stderr.write('Did you mean rule name: {} ?\n'.format(fixit[0]))
sys.exit(1)
except re.error as e:
sys.stderr.write('"{}" pattern {} has {} \n'.
format(rule_name, pattern_str, e.message))
sys.exit(1)
def is_rule_enabled(self, kind):
if self.__clang_db.get(kind):
return True
return False
def get_rule_names(self, kind):
"""
Multiple user defined rules can be configured against one type of ClangKind
For e.g. ClassMemberVariable, StructMemberVariable are types of FIELD_DECL
"""
return self.__clang_db.get(kind)
def get_rule(self, rule_name):
return self.__rule_db.get(rule_name)
class Validator(object):
def __init__(self, rule_db, filename, options, skip_db=None):
self.filename = filename
self.rule_db = rule_db
self.skip_db = skip_db
self.options = options
self.node_stack = AstNodeStack()
index = Index.create()
args = []
args.append('-x')
args.append('c++')
args.append('-D_GLIBCXX_USE_CXX11_ABI=0')
if self.options.args.definition:
for item in self.options.args.definition:
defintion = r'-D' + item
args.append(defintion)
if self.options.args.include:
for item in self.options.args.include:
inc = r'-I' + item
args.append(inc)
self.cursor = index.parse(filename, args).cursor
def validate(self):
return self.check(self.cursor)
def check(self, node):
"""
Recursively visit all nodes of the AST and match against the patter provided by
the user. Return the total number of errors caught in the file
"""
errors = 0
for child in node.get_children():
if self.is_local(child, self.filename):
# This is the case when typedef of struct is causing double reporting of error
# TODO: Find a better way to handle it
parent = self.node_stack.peek()
if (parent and parent == CursorKind.TYPEDEF_DECL and
child.kind == CursorKind.STRUCT_DECL):
return 0
errors += self.evaluate(child)
# Members struct, class, and unions must be treated differently.
# So parent ast node information is pushed in to the stack.
# Once all its children are validated pop it out of the stack
self.node_stack.push(child.kind)
errors += self.check(child)
self.node_stack.pop()
return errors
def evaluate(self, node):
"""
get the node's rule and match the pattern. Report and error if pattern
matching fails
"""
if not self.rule_db.is_rule_enabled(node.kind):
return 0
# If the pattern is in the skip list, ignore it
if self.skip_db.check_skip_db(node.displayname):
return 0
rule_name = self.rule_db.get_rule_names(node.kind)
rule = self.rule_db.get_rule(rule_name)
if rule.evaluate(node, self.node_stack.peek()) is False:
return 1
return 0
def is_local(self, node, filename):
""" Returns True is node belongs to the file being validated and not an include file """
if node.location.file and node.location.file.name in filename:
return True
return False
def do_validate(options, filename):
"""
Returns true if the file should be validated
- Check if its a c/c++ file
- Check if the file is not excluded
"""
path, extension = os.path.splitext(filename)
if extension not in file_extensions:
return False
if options.args.exclude:
for item in options.args.exclude:
if fnmatch.fnmatch(filename, item):
return False
return True
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s',
filename='log.txt', filemode='w')
""" Parse all command line arguments and validate """
op = Options()
op.parse_cmd_line()
if op.args.path is None:
sys.exit(1)
if op.args.clang_lib:
Config.set_library_file(op.args.clang_lib)
""" Creating the rules database """
rules_db = RulesDb(op._style_file)
""" Creating the skip database """
skip_db = SkipDb(op._skip_file)
""" Check the source code against the configured rules """
errors = 0
for path in op.args.path:
if os.path.isfile(path):
if do_validate(op, path):
v = Validator(rules_db, path, op, skip_db)
errors += v.validate()
elif os.path.isdir(path):
for (root, subdirs, files) in os.walk(path):
for filename in files:
path = root + '/' + filename
if do_validate(op, path):
v = Validator(rules_db, path, op, skip_db)
errors += v.validate()
if not op.args.recurse:
break
else:
sys.stderr.write("File '{}' not found!\n".format(path))
sys.exit(1)
if errors:
print("Total number of errors = {}".format(errors))
sys.exit(1)