CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
data-class-funcs.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 import re
3 datacl = re.compile("^class ")
4 mbcl = re.compile("(base|data) class")
5 farg = re.compile("(.*)\(\w+\)")
6 nsep = re.compile("\:\:")
7 topfunc = re.compile("::(produce|analyze|filter|beginLuminosityBlock|beginRun)\(")
8 baseclass = re.compile("edm::(one::|stream::|global::)?ED(Producer|Filter|Analyzer)(Base)?")
9 getfunc = re.compile("edm::eventsetup::EventSetupRecord::get\((.*)&\) const")
10 handle = re.compile("(.*),?class edm::ES(.*)Handle<(.*)>")
11 statics = set()
12 toplevelfuncs = set()
13 onefuncs = set()
14 dataclassfuncs = set()
15 virtfuncs = set()
16 virtclasses = set()
17 badfuncs = set()
18 badclasses = set()
19 esdclasses = set()
20 dataclasses = set()
21 flaggedclasses = set()
22 import networkx as nx
23 G=nx.DiGraph()
24 H=nx.DiGraph()
25 
26 f = open('classes.txt.dumperft')
27 for line in f:
28  if datacl.search(line) :
29  classname = line.split("'")[1]
30  esdclasses.add(classname)
31 f.close()
32 
33 f = open('classes.txt.inherits')
34 for line in f:
35  if datacl.search(line) :
36  classname = line.split("'")[1]
37  esdclasses.add(classname)
38 f.close()
39 
40 
41 f = open('class-checker.txt')
42 for line in f:
43  if mbcl.search(line):
44  fields = line.split("'")
45  classname = fields[1]
46  funcname = fields[3]
47  badclasses.add(classname)
48  badfuncs.add(funcname)
49 f.close()
50 
51 
52 f = open('classes.txt.dumperall')
53 for line in f :
54  if mbcl.search(line) :
55  fields = line.split("'")
56  if fields[2] == ' member data class ':
57  H.add_edge(fields[1],fields[3],kind=fields[2])
58  if fields[2] == ' templated member data class ':
59  H.add_edge(fields[1],fields[3],kind=fields[2])
60  if fields[2] == ' base class ':
61  H.add_edge(fields[1],fields[3],kind=fields[2])
62 f.close()
63 
64 
65 f = open('db.txt')
66 
67 for line in f :
68  fields = line.split("'")
69  if fields[2] == ' calls function ' :
70  G.add_edge(fields[1],fields[3],kind=' calls function ')
71  if getfunc.search(fields[3]) :
72  dataclassfuncs.add(fields[3])
73  m = getfunc.match(fields[3])
74  n = handle.match(m.group(1))
75  if n : o = n.group(3)
76  else : o = m.group(1)
77  p = re.sub("class ","",o)
78  dataclass = re.sub("struct ","",p)
79  dataclasses.add(dataclass)
80  if fields[2] == ' overrides function ' :
81  if baseclass.search(fields[3]) :
82  G.add_edge(fields[1],fields[3],kind=' overrides function ')
83  if topfunc.search(fields[3]) : toplevelfuncs.add(fields[1])
84  else : G.add_edge(fields[3],fields[1], kind=' calls override function ')
85  if fields[2] == ' static variable ' :
86  G.add_edge(fields[1],fields[3],kind=' static variable ')
87  statics.add(fields[3])
88 f.close()
89 
90 
91 for n,nbrdict in G.adjacency_iter():
92  for nbr,eattr in nbrdict.items():
93  if n in badfuncs or nbr in badfuncs :
94  if 'kind' in eattr and eattr['kind'] == ' overrides function ' :
95  print "'"+n+"'"+eattr['kind']+"'"+nbr+"'"
96  virtfuncs.add(nbr)
97 print
98 
99 for n,nbrdict in H.adjacency_iter():
100  for nbr,eattr in nbrdict.items():
101  if n in badclasses and 'kind' in eattr and eattr['kind'] == ' base class ' :
102  virtclasses.add(nbr)
103 
104 
105 for n,nbrdict in H.adjacency_iter():
106  for nbr,eattr in nbrdict.items():
107  if nbr in dataclasses and 'kind' in eattr and eattr['kind'] == ' base class ' :
108  dataclasses.add(n)
109 
110 print "flagged functions found by checker"
111 for dfunc in sorted(badfuncs) :
112  print dfunc
113 print
114 
115 print "flagged classes found by checker "
116 for dclass in sorted(badclasses) :
117  print dclass
118 print
119 
120 print "flagged classes found by checker union get"
121 for dclass in sorted(dataclasses.intersection(badclasses)) :
122  print dclass
123 print
124 
125 print "flagged classes found by checker union dumper"
126 for dclass in sorted(esdclasses.intersection(badclasses)) :
127  print dclass
128 print
129 
130 print "classes inheriting from flagged classes"
131 for dclass in sorted(virtclasses):
132  print dclass
133 print
134 
135 print "functions overridden by flagged functions"
136 for dfunc in sorted(virtfuncs):
137  print dfunc
138 print
139 
140 
141 for badclass in sorted(badclasses):
142  print "Event setup data class '"+badclass+"' is flagged."
143  flaggedclasses.add(badclass)
144 print
145 
146 for virtclass in sorted(virtclasses):
147  print "Event setup data class '"+virtclass+"' is flagged because inheriting class is flagged"
148  flaggedclasses.add(virtclass)
149 print
150 
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)
157 
158 print
159 
160 
161 for dataclassfunc in sorted(dataclassfuncs):
162  for tfunc in sorted(toplevelfuncs):
163  if 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  dataclass = re.sub("struct ","",p)
170  for flaggedclass in sorted(flaggedclasses):
171  if re.search(flaggedclass,dataclass) :
172  print "Flagged event setup data class '"+dataclass+"' is accessed in call stack '",
173  path = nx.shortest_path(G,tfunc,dataclassfunc)
174  for p in path:
175  print p+"; ",
176  print "' ",
177  for key in G[tfunc].keys() :
178  if 'kind' in G[tfunc][key] and G[tfunc][key]['kind'] == ' overrides function ' :
179  print "'"+tfunc+"'"+G[tfunc][key]['kind']+"'"+key+"'",
180  print ""
181  print "In call stack '",
182  path = nx.shortest_path(G,tfunc,dataclassfunc)
183  for p in path:
184  print p+"; ",
185  print "' flagged event setup data class '"+dataclass+"' is accessed. ",
186  for key in G[tfunc].keys() :
187  if 'kind' in G[tfunc][key] and G[tfunc][key]['kind'] == ' overrides function ' :
188  print "'"+tfunc+"'"+G[tfunc][key]['kind']+"'"+key+"'",
189  print ""
190