2 from __future__
import print_function
4 datacl = re.compile(
"^class ")
5 bfunc = re.compile(
"^function ")
6 mbcl = re.compile(
"(base|data) class")
7 farg = re.compile(
"(.*)\(\w+\)")
8 nsep = re.compile(
"\:\:")
9 topfunc = re.compile(
"::(produce|analyze|filter|beginLuminosityBlock|beginRun|beginStream)\(")
10 baseclass = re.compile(
"edm::(one::|stream::|global::)?ED(Producer|Filter|Analyzer)(Base)?")
11 getfunc = re.compile(
"edm::eventsetup::EventSetupRecord::get<.*>\((.*)&\) const")
12 handle = re.compile(
"(.*),?class edm::ES(.*)Handle<(.*)>")
13 skip = re.compile(
"edm::serviceregistry::ServicesManager::MakerHolder::add() const")
17 dataclassfuncs = set()
25 flaggedclasses = set()
30 f = open(
'classes.txt.dumperft')
32 if datacl.search(line) :
33 classname = line.split(
"'")[1]
34 dclasses.add(classname)
37 f = open(
'classes.txt.inherits')
39 if datacl.search(line) :
40 classname = line.split(
"'")[1]
41 dataclasses.add(classname)
45 f = open(
'class-checker.txt')
48 fields = line.split(
"'")
51 badclasses.add(classname)
52 badfuncs.add(funcname)
56 f = open(
'classes.txt.dumperall')
58 if mbcl.search(line) :
59 fields = line.split(
"'")
60 if fields[2] ==
' member data class ':
61 H.add_edge(fields[1],fields[3],kind=fields[2])
62 if fields[2] ==
' templated member data class ':
63 H.add_edge(fields[1],fields[3],kind=fields[2])
64 if fields[2] ==
' base class ':
65 H.add_edge(fields[1],fields[3],kind=fields[2])
70 for line
in fileinput.input(files =(
'function-statics-db.txt',
'function-calls-db.txt')):
71 if not bfunc.search(line) :
continue
72 fields = line.split(
"'")
73 if skip.search(fields[1])
or skip.search(fields[3]) :
continue
74 if fields[2] ==
' calls function ' :
75 G.add_edge(fields[1],fields[3],kind=
' calls function ')
76 if getfunc.search(fields[3]) :
77 dataclassfuncs.add(fields[3])
78 m = getfunc.match(fields[3])
79 n = handle.match(m.group(1))
82 p = re.sub(
"class ",
"",o)
83 dataclass = re.sub(
"struct ",
"",p)
84 dataclasses.add(dataclass)
85 if fields[2] ==
' overrides function ' :
86 if baseclass.search(fields[3]) :
87 G.add_edge(fields[1],fields[3],kind=
' overrides function ')
88 if topfunc.search(fields[3]) : toplevelfuncs.add(fields[1])
89 else : G.add_edge(fields[3],fields[1], kind=
' calls override function ')
90 if fields[2] ==
' static variable ' :
91 G.add_edge(fields[1],fields[3],kind=
' static variable ')
92 statics.add(fields[3])
95 for n,nbrdict
in G.adjacency():
96 for nbr,eattr
in nbrdict.items():
97 if n
in badfuncs
or nbr
in badfuncs :
98 if 'kind' in eattr
and eattr[
'kind'] ==
' overrides function ' :
99 print(
"'"+n+
"'"+eattr[
'kind']+
"'"+nbr+
"'")
103 for n,nbrdict
in H.adjacency():
104 for nbr,eattr
in nbrdict.items():
105 if n
in badclasses
and 'kind' in eattr
and eattr[
'kind'] ==
' base class ' :
109 for n,nbrdict
in H.adjacency():
110 for nbr,eattr
in nbrdict.items():
111 if nbr
in dclasses
and 'kind' in eattr
and eattr[
'kind'] ==
' base class ' :
114 print(
"flagged functions found by checker")
115 for dfunc
in sorted(badfuncs) :
119 print(
"flagged classes found by checker ")
120 for dclass
in sorted(badclasses) :
124 print(
"flagged classes found by checker union get")
125 for dclass
in sorted(dclasses.intersection(badclasses)) :
130 print(
"classes inheriting from flagged classes")
131 for dclass
in sorted(virtclasses):
135 print(
"functions overridden by flagged functions")
136 for dfunc
in sorted(virtfuncs):
141 for badclass
in sorted(badclasses):
142 print(
"Event setup data class '"+badclass+
"' is flagged.")
143 flaggedclasses.add(badclass)
146 for virtclass
in sorted(virtclasses):
147 print(
"Event setup data class '"+virtclass+
"' is flagged because inheriting class is flagged")
148 flaggedclasses.add(virtclass)
151 for badclass
in sorted(badclasses):
152 for dataclass
in sorted(dataclasses):
153 if H.has_node(badclass)
and H.has_node(dataclass):
154 if nx.has_path(H,dataclass, badclass) :
155 print(
"Event setup data class '"+dataclass+
"' contains or inherits from flagged class '"+badclass+
"'.")
156 flaggedclasses.add(dataclass)
161 for dataclassfunc
in sorted(dataclassfuncs):
162 for tfunc
in sorted(toplevelfuncs):
163 if G.has_node(tfunc)
and G.has_node(dataclassfunc)
and nx.has_path(G,tfunc,dataclassfunc):
164 m = getfunc.match(dataclassfunc)
165 n = handle.match(m.group(1))
166 if n : o = n.group(3)
167 else : o = m.group(1)
168 p = re.sub(
"class ",
"",o)
169 q = re.sub(
"struct ",
"",p)
170 dataclass = re.sub(
"<.*> ",
"",q)
171 for flaggedclass
in sorted(flaggedclasses):
172 exact=
r"^" + re.escape(flaggedclass) +
r"$"
173 exactmatch=re.match(exact,dataclass)
175 print(
"Flagged event setup data class '"+dataclass+
"' is accessed in call stack '", end=
' ')
176 path = nx.shortest_path(G,tfunc,dataclassfunc)
178 print(p+
"; ", end=
' ')
180 for key
in G[tfunc].
keys() :
181 if 'kind' in G[tfunc][key]
and G[tfunc][key][
'kind'] ==
' overrides function ' :
182 print(
"'"+tfunc+
"'"+G[tfunc][key][
'kind']+
"'"+key+
"'", end=
' ')
184 print(
"In call stack '", end=
' ')
185 path = nx.shortest_path(G,tfunc,dataclassfunc)
187 print(p+
"; ", end=
' ')
188 print(
"' flagged event setup data class '"+dataclass+
"' is accessed. ", end=
' ')
189 for key
in G[tfunc].
keys() :
190 if 'kind' in G[tfunc][key]
and G[tfunc][key][
'kind'] ==
' overrides function ' :
191 print(
"'"+tfunc+
"'"+G[tfunc][key][
'kind']+
"'"+key+
"'", end=
' ')