-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.py
115 lines (83 loc) · 4.21 KB
/
analyzer.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
import clang.cindex
import enchant
clang.cindex.Config.set_library_path('/usr/lib/llvm-3.5/lib/')
class custom_text:
def __init__(self):
self.HEADER = '\033[95m'
self.OKBLUE = '\033[94m'
self.OKGREEN = '\033[92m'
self.WARNING = '\033[93m'
self.FAIL = '\033[91m'
self.ENDC = '\033[0m'
self.BOLD = '\033[1m'
self.UNDERLINE = '\033[4m'
def underlined(self, message):
return self.UNDERLINE + message + self.ENDC
def useful_link(self, message):
return self.OKBLUE + "Useful Link: " + self.ENDC + message + '\n'
def useful_doc(self, message):
return self.OKBLUE + "Useful Document: " + self.ENDC + message + '\n'
def warning(self):
return self.WARNING + "WARNING: " + self.ENDC
def comment(self, message):
return self.OKGREEN + message + self.ENDC
custom_text_ = custom_text()
is_var_init = False
def is_var_initialized(cursor):
for c in cursor.get_children():
if "LITERAL" in str(c.kind) or c.kind == clang.cindex.CursorKind.DECL_REF_EXPR:
global is_var_init
is_var_init = True
is_var_initialized(c)
def inspect_name(cursor):
dic = enchant.Dict("en_US")
name = str(cursor.spelling).replace('_', ' ')
suggest = False
names = []
if not dic.check(name):
suggest = True
suggested = dic.suggest(name)
names = [item.replace(' ', '_') for item in suggested]
if suggest:
print custom_text_.warning() + "Name suggestions for " + custom_text_.underlined(cursor.spelling) + \
" at line " + str(cursor.location.line) + ", column " + str(cursor.location.column) + '\n' + str(names) + \
'\n' + custom_text_.useful_doc("Robert C. Martin, Clean Code, Chapter 2: Meaningful Names")
def inspect_variable(cursor):
global is_var_init
is_var_init = False
is_var_initialized(cursor)
if not is_var_init:
print custom_text_.warning() + "Uninitialized variable " + custom_text_.underlined(
cursor.spelling) + " at line " + \
str(cursor.location.line) + " column " + str(cursor.location.column) + '\n'
if 'unsigned' in cursor.type.get_canonical().spelling:
print custom_text_.warning() + "Unsigned data type can introduce bugs, try to aviod if possible, " + \
custom_text_.underlined(cursor.spelling) + " at line " + \
str(cursor.location.line) + " column " + str(cursor.location.column) + ".\n" + \
custom_text_.useful_link("https://google.github.io/styleguide/cppguide.html, On Unsigned Integers")
inspect_name(cursor)
def inspect_func(cursor):
if (cursor.extent.end.line - cursor.extent.start.line) > 10:
print custom_text_.warning() + "Function " + custom_text_.underlined(cursor.spelling) + \
" is too long. It is better to extract functions from it." + "\n" + \
custom_text_.useful_doc("Robert C. Martin, Clean Code, Chapter 3:Functions")
inspect_name(cursor)
# Only works for functions now
def inspect_comp_stmt(cursor):
parent = cursor.semantic_parent
if parent:
if parent.location.line == cursor.location.line:
print custom_text_.warning() + "You may leave braces which start at line " + str(cursor.location.line) + \
" on their own in a line to indicate them as a sign of scope." + "\n"
def inspect_using_directive(cursor):
namespace_ref = cursor.get_children().next()
print custom_text_.warning() + "Avoid " + custom_text_.underlined(
"using") + " directives to prevent confusions, instead try to use " + \
custom_text_.underlined(str(namespace_ref.displayname) + "::<member>") + '\n'
def inspect_comment(cursor):
print custom_text_.warning() + "You may try to remove the comment " + '\n' + \
custom_text_.comment(cursor.raw_comment) + \
" and embed into the name " + custom_text_.underlined(cursor.spelling) + "\n" + \
" at line " + str(cursor.location.line) + " column " + str(cursor.location.column) + \
", so that the reader can infer its meaning by just reading it.\n" + \
custom_text_.useful_doc("Robert C. Martin, Clean Code, Chapter 2: Meaningful Names")