Chromium Code Reviews| Index: PRESUBMIT.py |
| diff --git a/PRESUBMIT.py b/PRESUBMIT.py |
| index 33008be52923f9a0fb57c56b30139857bb76d6b4..f6da5907d5ca06a0e230887002a6061680026090 100644 |
| --- a/PRESUBMIT.py |
| +++ b/PRESUBMIT.py |
| @@ -359,6 +359,71 @@ def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api): |
| '\n'.join(problems))] |
| +def _FindHistogramNameInLine(histogram_name, line): |
| + """Helper function to smartly try to find a histogram name or prefix in a |
| + line.""" |
|
Alexei Svitkine (slow)
2015/02/02 15:12:08
Nit: No fit into one line - e.g. no need to start
mcasas
2015/02/02 19:24:14
Done.
|
| + if not "affected-histogram" in line: |
| + return histogram_name in line |
| + # A histogram_suffixes tag type has an affected-histogram name as a prefix of |
| + # the histogram_name. |
| + if not '"' in line: |
| + return False |
| + histogram_prefix = line.split('\"')[1] |
| + return histogram_prefix in histogram_name |
| + |
| + |
| +def _CheckUmaHistogramChanges(input_api, output_api): |
| + """Check that UMA histogram names in touched lines can still be found in other |
| + lines of the patch or in histograms.xml. Note that this check would not catch |
| + the reverse: changes in histograms.xml not matched in the code itself.""" |
| + touched_histograms = [] |
| + histograms_xml_modifications = [] |
| + pattern = input_api.re.compile('UMA_HISTOGRAM.*\("(.*)"') |
| + for f in input_api.AffectedFiles(): |
| + # If histograms.xml itself is modified, keep the modified lines for later. |
| + if f.LocalPath().endswith(('histograms.xml')): |
| + histograms_xml_modifications = f.ChangedContents() |
| + continue |
| + if not f.LocalPath().endswith(('cc', 'mm', 'cpp')): |
| + continue |
| + for line_num, line in f.ChangedContents(): |
| + found = pattern.search(line) |
| + if found: |
| + touched_histograms.append([found.group(1), f, line_num]) |
| + |
| + # Search for the touched histogram names in the local modifications to |
| + # histograms.xml, and, if not found, on the base histograms.xml file. |
| + problems = [] |
| + for histogram_info in touched_histograms: |
| + histogram_name_found = False |
| + for line_num, line in histograms_xml_modifications: |
| + histogram_name_found = _FindHistogramNameInLine(histogram_info[0], line) |
| + if histogram_name_found: |
| + break |
| + if histogram_name_found: |
| + touched_histograms.remove(histogram_info) |
|
Alexei Svitkine (slow)
2015/02/02 15:12:07
Is it actually safe to remove an item from the lis
mcasas
2015/02/02 19:24:14
It turns out that the iterator-like semantics are
|
| + continue |
| + |
| + if touched_histograms: |
| + with open('tools/metrics/histograms/histograms.xml') as histograms_xml: |
| + for histogram_name, f, line_num in touched_histograms: |
| + histogram_name_found = False; |
|
Alexei Svitkine (slow)
2015/02/02 15:12:08
No ;
mcasas
2015/02/02 19:24:14
Argh! Done :)
|
| + for line in histograms_xml: |
| + histogram_name_found = _FindHistogramNameInLine(histogram_name, line); |
| + if histogram_name_found: |
| + break |
| + if histogram_name_found: |
| + continue |
|
Alexei Svitkine (slow)
2015/02/02 15:12:08
Nit: Just reverse the condition and put the next l
mcasas
2015/02/02 19:24:14
Done.
|
| + problems.append(' [%s:%d] %s' % |
| + (f.LocalPath(), line_num, histogram_name)) |
| + |
| + if not problems: |
| + return [] |
| + return [output_api.PresubmitPromptWarning('Some UMA_HISTOGRAM lines have ' |
| + 'been modified and the associated histogram name has no match in either ' |
| + 'metrics/histograms.xml or the modifications of it:', problems)] |
| + |
| + |
| def _CheckNoNewWStrings(input_api, output_api): |
| """Checks to make sure we don't introduce use of wstrings.""" |
| problems = [] |
| @@ -1585,6 +1650,7 @@ def CheckChangeOnUpload(input_api, output_api): |
| results.extend(_CheckJavaStyle(input_api, output_api)) |
| results.extend( |
| input_api.canned_checks.CheckGNFormatted(input_api, output_api)) |
| + results.extend(_CheckUmaHistogramChanges(input_api, output_api)) |
| return results |