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