CMS 3D CMS Logo

edm-global-class.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|flagged) class")
6 farg = re.compile("(.*)\(\w+\)")
7 nsep = re.compile("\:\:")
8 topfunc = re.compile("::(produce|analyze|filter|beginLuminosityBlock|beginRun|beginStream)\(")
9 baseclass = re.compile("edm::(one::|stream::|global::)?ED(Producer|Filter|Analyzer)(Base)?")
10 globalclass = re.compile("^edm::global::ED(Producer|Filter|Analyzer)$")
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 rootclass = re.compile("T(H1|Tree|Enum|DataType|Class|Branch|Named|File)")
15 stdcl = re.compile("^std::(.*)[^>]$")
16 stdptr = re.compile("(.*)_ptr$")
17 statics = set()
18 toplevelfuncs = set()
19 onefuncs = set()
20 dataclassfuncs = set()
21 virtfuncs = set()
22 virtclasses = set()
23 badfuncs = set()
24 badclasses = set()
25 esdclasses = set()
26 dclasses = set()
27 dataclasses = set()
28 flaggedclasses = set()
29 globalclasses = set()
30 import networkx as nx
31 G=nx.DiGraph()
32 H=nx.DiGraph()
33 I=nx.DiGraph()
34 
35 f = open('class-checker.txt')
36 for line in f:
37  if mbcl.search(line):
38  fields = line.split("'")
39  classname = fields[1]
40  funcname = fields[3]
41  badclasses.add(classname)
42  badfuncs.add(funcname)
43 f.close()
44 
45 f = open('const-checker.txt')
46 for line in f:
47  if mbcl.search(line):
48  fields = line.split("'")
49  classname = fields[1]
50  badclasses.add(classname)
51 f.close()
52 
53 
54 f = open('classes.txt.dumperall')
55 for line in f :
56  if mbcl.search(line) :
57  fields = line.split("'")
58  if fields[2] == ' member data class ':
59  H.add_edge(fields[1],fields[3],kind=fields[2])
60  if fields[2] == ' templated member data class ':
61  H.add_edge(fields[1],fields[3],kind=fields[2])
62  if fields[2] == ' base class ':
63  H.add_edge(fields[1],fields[3],kind=fields[2])
64  I.add_edge(fields[3],fields[1],kind=' derived class')
65  if globalclass.match(fields[3]):
66  globalclasses.add(fields[1])
67  print "class '"+fields[1]+"' base class '"+fields[3]+"'"
68 f.close()
69 
70 import fileinput
71 for line in fileinput.input(files =('function-statics-db.txt','function-calls-db.txt')):
72  if not bfunc.search(line) : continue
73  fields = line.split("'")
74  if skip.search(fields[1]) or skip.search(fields[3]) : continue
75  if fields[2] == ' calls function ' :
76  G.add_edge(fields[1],fields[3],kind=' calls function ')
77  if getfunc.search(fields[3]) :
78  dataclassfuncs.add(fields[3])
79  m = getfunc.match(fields[3])
80  n = handle.match(m.group(1))
81  if n : o = n.group(3)
82  else : o = m.group(1)
83  p = re.sub("class ","",o)
84  dataclass = re.sub("struct ","",p)
85  dataclasses.add(dataclass)
86  if fields[2] == ' overrides function ' :
87  if baseclass.search(fields[3]) :
88  G.add_edge(fields[1],fields[3],kind=' overrides function ')
89  if topfunc.search(fields[3]) : toplevelfuncs.add(fields[1])
90  else : G.add_edge(fields[3],fields[1], kind=' calls override function ')
91  if fields[2] == ' static variable ' :
92  G.add_edge(fields[1],fields[3],kind=' static variable ')
93  statics.add(fields[3])
94 fileinput.close()
95 
96 
97 
98 for n,nbrdict in G.adjacency_iter():
99  for nbr,eattr in nbrdict.items():
100  if n in badfuncs or nbr in badfuncs :
101  if 'kind' in eattr and eattr['kind'] == ' overrides function ' :
102  print "'"+n+"'"+eattr['kind']+"'"+nbr+"'"
103  virtfuncs.add(nbr)
104 print
105 
106 print "-----------------------------------------------"
107 print "flagged functions found by checker"
108 print "-----------------------------------------------"
109 for dfunc in sorted(badfuncs) :
110  print dfunc
111 print
112 
113 print "-----------------------------------------------"
114 print "flagged classes found by checker "
115 print "-----------------------------------------------"
116 for dclass in sorted(badclasses) :
117  print dclass
118 print
119 
120 nodes = sorted(badclasses)
121 for node in nodes:
122  visited = set()
123  if node in visited:
124  continue
125  visited.add(node)
126  if node in H : stack = [(node,iter(H[node]))]
127  if node in I :
128  Q=nx.dfs_preorder_nodes(I,node)
129  for q in Q:
130  if q in H :
131  stack.append( ( q, iter( H[q] ) ) )
132  while stack:
133  parent,children = stack[-1]
134  try:
135  child = next(children)
136  if globalclass.search(child): visited.add(child)
137  if rootclass.search(child): visited.add(child)
138  if child not in visited:
139  visited.add(child)
140  stack.append( ( child, iter( H[child] ) ) )
141  kind=H[parent][child]['kind']
142  if stdptr.search(kind):
143  if child in I :
144  Q=nx.dfs_preorder_nodes(I,child)
145  for q in Q :
146  if q in H :
147  stack.append( ( q, iter( H[q] ) ) )
148  except StopIteration:
149  stack.pop()
150  print "flagged class "+node+" contains or inherits from classes ",
151  for v in visited : print v+",",
152  print "\n\n"
153  for v in sorted(visited) :
154  if v in globalclasses:
155  print "EDM global class '"+v+"' is flagged because it is connected to flagged class '"+node+"'"
156