[PATCH] D131553: [analyzer] exploded-graph-rewriter: Fix python3 string encoding issues

Balázs Benics via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 10 02:53:50 PDT 2022


steakhal created this revision.
steakhal added reviewers: NoQ, martong, ASDenysPetrov.
Herald added subscribers: manas, dkrupp, donat.nagy, Szelethus, mikhail.ramalho, a.sidorin, rnkovacs, szepet, baloghadamsoftware, xazax.hun.
Herald added a project: All.
steakhal requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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 <https://reviews.llvm.org/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 <https://reviews.llvm.org/D71746> had no tests, I'm not too worried about this either.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131553

Files:
  clang/utils/analyzer/exploded-graph-rewriter.py


Index: clang/utils/analyzer/exploded-graph-rewriter.py
===================================================================
--- clang/utils/analyzer/exploded-graph-rewriter.py
+++ clang/utils/analyzer/exploded-graph-rewriter.py
@@ -18,7 +18,6 @@
 import logging
 import os
 import re
-import sys
 
 
 #===-----------------------------------------------------------------------===#
@@ -423,10 +422,7 @@
 
     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,12 +850,12 @@
             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)
-                print('Done! Please remember to remove the file.')
+                print('Done!')
                 return filename
 
             try:
@@ -873,13 +869,13 @@
                 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)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131553.451401.patch
Type: text/x-patch
Size: 2190 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220810/d75efa96/attachment.bin>


More information about the cfe-commits mailing list