[clang] 5e876c5 - [analyzer] exploded-graph-rewriter: Fix python3 string encoding issues
Balazs Benics via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 11 07:09:00 PDT 2022
Author: Balazs Benics
Date: 2022-08-11T16:07:52+02:00
New Revision: 5e876c54f2d70195dd1bde827e908825aab2f4fb
URL: https://github.com/llvm/llvm-project/commit/5e876c54f2d70195dd1bde827e908825aab2f4fb
DIFF: https://github.com/llvm/llvm-project/commit/5e876c54f2d70195dd1bde827e908825aab2f4fb.diff
LOG: [analyzer] exploded-graph-rewriter: Fix python3 string encoding issues
This encapsulates 3 changes:
- `DotDumpVisitor` now aggregates strings instead of *bytes* for both
`python2` and `python3`. This difference caused crashes when it tried
to write out the content as *strings*, similarly described at D71746.
- `graphviz.pipe()` expects the input in *bytes* instead of unicode
strings. And it results in *bytes*. Due to string concatenations and
similar operations, I'm using unicode string as the default, and
converting to *bytes* on demand.
- `write_temp_file()` now appends the `egraph-` prefix and more
importantly, it will create the temp file in the **current working
directory** instead of in the *temp*. This change makes `Firefox` be
able to open the file even if the `security.sandbox.content.level` is
set to the (default) most restricting `4`.
See https://support.mozilla.org/si/questions/1259285
An artifact of the bad byte handling was previously in the `HTML`
produced by the script that it displayed the `b'` string at the top left
corner. Now it won't anymore :)
I've tested that the following command works on `Ubuntu 22.04`:
```
exploded-graph-rewriter my-egraph.dot
```
Both `python2` and `python3` works as expected.
PS: I'm not adding tests, as the current test infra does not support
testing HTML outputs for this script.
Check the `clang/test/Analysis/exploded-graph-rewriter/lit.local.cfg`.
We always pass the `--dump-dot-only` flag to the script.
Along with that, the default invocation will not only create this HTML
report but also try to open it. In addition to this, I'm not sure if the
buildbots have `graphviz` installed and also if this package is installed
on `pip`.
Unless we change some of these, we cannot test this change.
Given that D71746 had no tests, I'm not too worried about this either.
Reviewed By: NoQ
Differential Revision: https://reviews.llvm.org/D131553
Added:
Modified:
clang/utils/analyzer/exploded-graph-rewriter.py
Removed:
################################################################################
diff --git a/clang/utils/analyzer/exploded-graph-rewriter.py b/clang/utils/analyzer/exploded-graph-rewriter.py
index b8382d151b86d..603bae9d69ac7 100755
--- a/clang/utils/analyzer/exploded-graph-rewriter.py
+++ b/clang/utils/analyzer/exploded-graph-rewriter.py
@@ -18,7 +18,6 @@
import logging
import os
import re
-import sys
#===-----------------------------------------------------------------------===#
@@ -423,10 +422,7 @@ def _dump_raw(self, s):
def output(self):
assert not self._dump_dot_only
- if sys.version_info[0] > 2 and sys.version_info[1] >= 5:
- return ''.join(self._output).encode()
- else:
- return ''.join(self._output)
+ return ''.join(self._output)
def _dump(self, s):
s = s.replace('&', '&') \
@@ -854,8 +850,8 @@ def visit_end_of_graph(self):
import sys
import tempfile
- def write_temp_file(suffix, data):
- fd, filename = tempfile.mkstemp(suffix=suffix)
+ def write_temp_file(suffix, prefix, data):
+ fd, filename = tempfile.mkstemp(suffix, prefix, '.', True)
print('Writing "%s"...' % filename)
with os.fdopen(fd, 'w') as fp:
fp.write(data)
@@ -873,13 +869,13 @@ def write_temp_file(suffix, data):
print('You may also convert DOT to SVG manually via')
print(' $ dot -Tsvg input.dot -o output.svg')
print()
- write_temp_file('.dot', self.output())
+ write_temp_file('.dot', 'egraph-', self.output())
return
- svg = graphviz.pipe('dot', 'svg', self.output())
+ svg = graphviz.pipe('dot', 'svg', self.output().encode()).decode()
filename = write_temp_file(
- '.html', '<html><body bgcolor="%s">%s</body></html>' % (
+ '.html', 'egraph-', '<html><body bgcolor="%s">%s</body></html>' % (
'#1a1a1a' if self._dark_mode else 'white', svg))
if sys.platform == 'win32':
os.startfile(filename)
More information about the cfe-commits
mailing list