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