Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: tools/metrics/histograms/update_histogram_enum.py

Issue 2797043002: Add a presubmit checker for duplicated enum values in UseCounter::Feature (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/core/frame/PRESUBMIT.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Updates enums in histograms.xml file with values read from provided C++ enum. 5 """Updates enums in histograms.xml file with values read from provided C++ enum.
6 6
7 If the file was pretty-printed, the updated version is pretty-printed too. 7 If the file was pretty-printed, the updated version is pretty-printed too.
8 """ 8 """
9 9
10 import logging 10 import logging
(...skipping 20 matching lines...) Expand all
31 @property 31 @property
32 def message(self): 32 def message(self):
33 return self.args[0] 33 return self.args[0]
34 34
35 35
36 def Log(message): 36 def Log(message):
37 logging.info(message) 37 logging.info(message)
38 38
39 39
40 def ReadHistogramValues(filename, start_marker, end_marker): 40 def ReadHistogramValues(filename, start_marker, end_marker):
41 """Returns a dictionary of enum values, read from a C++ file. 41 """Returns a dictionary of enum values, read from a C++ file.
Rick Byers 2017/04/05 15:26:39 nit: update this comment to reflect the new return
42 42
43 Args: 43 Args:
44 filename: The unix-style path (relative to src/) of the file to open. 44 filename: The unix-style path (relative to src/) of the file to open.
45 start_marker: A regex that signifies the start of the enum values. 45 start_marker: A regex that signifies the start of the enum values.
46 end_marker: A regex that signifies the end of the enum values. 46 end_marker: A regex that signifies the end of the enum values.
47 """ 47 """
48 # Read the file as a list of lines 48 # Read the file as a list of lines
49 with open(path_util.GetInputFile(filename)) as f: 49 with open(path_util.GetInputFile(filename)) as f:
50 content = f.readlines() 50 content = f.readlines()
51 51
(...skipping 16 matching lines...) Expand all
68 m = ITEM_REGEX_WITH_INIT.match(line) 68 m = ITEM_REGEX_WITH_INIT.match(line)
69 if m: 69 if m:
70 enum_value = int(m.group(2)) 70 enum_value = int(m.group(2))
71 label = m.group(1) 71 label = m.group(1)
72 else: 72 else:
73 m = ITEM_REGEX.match(line) 73 m = ITEM_REGEX.match(line)
74 if m: 74 if m:
75 label = m.group(1) 75 label = m.group(1)
76 else: 76 else:
77 continue 77 continue
78 # If two enum constraints have the same value
Rick Byers 2017/04/05 15:26:39 s/constraints/constants/ ?
79 if enum_value in result:
80 return result, (result[enum_value], label)
78 result[enum_value] = label 81 result[enum_value] = label
79 enum_value += 1 82 enum_value += 1
80 else: 83 else:
81 if START_REGEX.match(line): 84 if START_REGEX.match(line):
82 inside_enum = True 85 inside_enum = True
83 enum_value = 0 86 enum_value = 0
84 return result 87 return result, None
85 88
86 89
87 def CreateEnumItemNode(document, value, label): 90 def CreateEnumItemNode(document, value, label):
88 """Creates an int element to append to an enum.""" 91 """Creates an int element to append to an enum."""
89 item_node = document.createElement('int') 92 item_node = document.createElement('int')
90 item_node.attributes['value'] = str(value) 93 item_node.attributes['value'] = str(value)
91 item_node.attributes['label'] = label 94 item_node.attributes['label'] = label
92 return item_node 95 return item_node
93 96
94 97
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 changed. 176 changed.
174 177
175 Args: 178 Args:
176 histogram_enum_name: The name of the XML <enum> attribute to update. 179 histogram_enum_name: The name of the XML <enum> attribute to update.
177 source_enum_path: A unix-style path, relative to src/, giving 180 source_enum_path: A unix-style path, relative to src/, giving
178 the C++ header file from which to read the enum. 181 the C++ header file from which to read the enum.
179 start_marker: A regular expression that matches the start of the C++ enum. 182 start_marker: A regular expression that matches the start of the C++ enum.
180 end_marker: A regular expression that matches the end of the C++ enum. 183 end_marker: A regular expression that matches the end of the C++ enum.
181 """ 184 """
182 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) 185 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path))
183 source_enum_values = ReadHistogramValues(source_enum_path, start_marker, 186 source_enum_values, duplicated_values = ReadHistogramValues(
184 end_marker) 187 source_enum_path, start_marker, end_marker)
188 if duplicated_values is not None:
189 return False, duplicated_values
185 190
186 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, 191 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values,
187 source_enum_path) 192 source_enum_path)
188 return xml != new_xml 193 return xml != new_xml, None
189 194
190 195
191 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, 196 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values,
192 source_enum_path): 197 source_enum_path):
193 """Updates |histogram_enum_name| enum in histograms.xml file with values 198 """Updates |histogram_enum_name| enum in histograms.xml file with values
194 from the {value: 'key'} dictionary |source_enum_values|. A comment is added 199 from the {value: 'key'} dictionary |source_enum_values|. A comment is added
195 to histograms.xml citing that the values in |histogram_enum_name| were 200 to histograms.xml citing that the values in |histogram_enum_name| were
196 sourced from |source_enum_path|. 201 sourced from |source_enum_path|.
197 """ 202 """
198 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, 203 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values,
(...skipping 20 matching lines...) Expand all
219 start_marker: A regular expression that matches the start of the C++ enum. 224 start_marker: A regular expression that matches the start of the C++ enum.
220 end_marker: A regular expression that matches the end of the C++ enum. 225 end_marker: A regular expression that matches the end of the C++ enum.
221 """ 226 """
222 227
223 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) 228 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path))
224 source_enum_values = ReadHistogramValues(source_enum_path, start_marker, 229 source_enum_values = ReadHistogramValues(source_enum_path, start_marker,
225 end_marker) 230 end_marker)
226 231
227 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, 232 UpdateHistogramFromDict(histogram_enum_name, source_enum_values,
228 source_enum_path) 233 source_enum_path)
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/frame/PRESUBMIT.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698