CMS 3D CMS Logo

data-class-funcs.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 from __future__ import print_function
3 import re
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")
14 statics = set()
15 toplevelfuncs = set()
16 onefuncs = set()
17 dataclassfuncs = set()
18 virtfuncs = set()
19 virtclasses = set()
20 badfuncs = set()
21 badclasses = set()
22 esdclasses = set()
23 dclasses = set()
24 dataclasses = set()
25 flaggedclasses = set()
26 import networkx as nx
27 G=nx.DiGraph()
28 H=nx.DiGraph()
29 
30 f = open('classes.txt.dumperft')
31 for line in f:
32  if datacl.search(line) :
33  classname = line.split("'")[1]
34  dclasses.add(classname)
35 f.close()
36 
37 f = open('classes.txt.inherits')
38 for line in f:
39  if datacl.search(line) :
40  classname = line.split("'")[1]
41  dataclasses.add(classname)
42 f.close()
43 
44 
45 f = open('class-checker.txt')
46 for line in f:
47  if mbcl.search(line):
48  fields = line.split("'")
49  classname = fields[1]
50  funcname = fields[3]
51  badclasses.add(classname)
52  badfuncs.add(funcname)
53 f.close()
54 
55 
56 f = open('classes.txt.dumperall')
57 for line in f :
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])
66 f.close()
67 
68 
69 import fileinput
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))
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 fileinput.close()
94 
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+"'")
100  virtfuncs.add(nbr)
101 print()
102 
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 ' :
106  virtclasses.add(nbr)
107 
108 
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 ' :
112  dclasses.add(n)
113 
114 print("flagged functions found by checker")
115 for dfunc in sorted(badfuncs) :
116  print(dfunc)
117 print()
118 
119 print("flagged classes found by checker ")
120 for dclass in sorted(badclasses) :
121  print(dclass)
122 print()
123 
124 print("flagged classes found by checker union get")
125 for dclass in sorted(dclasses.intersection(badclasses)) :
126  print(dclass)
127 print()
128 
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 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)
174  if exactmatch:
175  print("Flagged event setup data class '"+dataclass+"' is accessed in call stack '", end=' ')
176  path = nx.shortest_path(G,tfunc,dataclassfunc)
177  for p in path:
178  print(p+"; ", end=' ')
179  print("' ", 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=' ')
183  print("")
184  print("In call stack '", end=' ')
185  path = nx.shortest_path(G,tfunc,dataclassfunc)
186  for p in path:
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=' ')
192  print("")
193 
relativeConstraints.keys
keys
Definition: relativeConstraints.py:89
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46