Skip to content

Commit

Permalink
[analyzer] exploded-graph-rewriter: Fix escaping StringRegions.
Browse files Browse the repository at this point in the history
Quotes around StringRegions are now escaped and unescaped correctly,
producing valid JSON.

Additionally, add a forgotten escape for Store values.

Differential Revision: https://reviews.llvm.org/D63519

llvm-svn: 363897
  • Loading branch information
haoNoQ committed Jun 19, 2019
1 parent 064c8c6 commit b50d167
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions clang/lib/StaticAnalyzer/Core/RegionStore.cpp
Expand Up @@ -210,15 +210,17 @@ class RegionBindingsRef : public llvm::ImmutableMapRef<const MemRegion *,
void printJson(raw_ostream &Out, const char *NL = "\n",
unsigned int Space = 0, bool IsDot = false) const {
for (iterator I = begin(); I != end(); ++I) {
// TODO: We might need a .printJson for I.getKey() as well.
Indent(Out, Space, IsDot)
<< "{ \"cluster\": \"" << I.getKey() << "\", \"pointer\": \""
<< (const void *)I.getKey() << "\", \"items\": [" << NL;

++Space;
const ClusterBindings &CB = I.getData();
for (ClusterBindings::iterator CI = CB.begin(); CI != CB.end(); ++CI) {
Indent(Out, Space, IsDot) << "{ " << CI.getKey() << ", \"value\": \""
<< CI.getData() << "\" }";
Indent(Out, Space, IsDot) << "{ " << CI.getKey() << ", \"value\": ";
CI.getData().printJson(Out, /*AddQuotes=*/true);
Out << " }";
if (std::next(CI) != CB.end())
Out << ',';
Out << NL;
Expand Down
18 changes: 18 additions & 0 deletions clang/test/Analysis/exploded-graph-rewriter/escapes.c
@@ -0,0 +1,18 @@
// FIXME: Figure out how to use %clang_analyze_cc1 with our lit.local.cfg.
// RUN: %clang_cc1 -analyze -triple x86_64-unknown-linux-gnu \
// RUN: -analyzer-checker=core \
// RUN: -analyzer-dump-egraph=%t.dot %s
// RUN: %exploded_graph_rewriter %t.dot | FileCheck %s

// FIXME: Substitution doesn't seem to work on Windows.
// UNSUPPORTED: system-windows

void string_region_escapes() {
// CHECK: <td align="left"><b>Store: </b></td>
// CHECK-SAME: <td align="left">foo</td><td align="left">0</td>
// CHECK-SAME: <td align="left">&amp;Element\{"foo",0 S64b,char\}</td>
// CHECK: <td align="left"><b>Environment: </b></td>
// CHECK-SAME: <td align="left">"foo"</td>
// CHECK-SAME: <td align="left">&amp;Element\{"foo",0 S64b,char\}</td>
const char *const foo = "foo";
}
2 changes: 1 addition & 1 deletion clang/test/Analysis/exploded-graph-rewriter/lit.local.cfg
Expand Up @@ -15,4 +15,4 @@ config.substitutions.append(('%exploded_graph_rewriter',
config.clang_src_dir,
'utils', 'analyzer')))))

config.suffixes = ['.dot']
config.suffixes.add('.dot')
1 change: 1 addition & 0 deletions clang/utils/analyzer/exploded-graph-rewriter.py
Expand Up @@ -199,6 +199,7 @@ def add_raw_line(self, raw_line):
.replace('\\"', '"') \
.replace('\\{', '{') \
.replace('\\}', '}') \
.replace('\\\\', '\\') \
.replace('\\<', '\\\\<') \
.replace('\\>', '\\\\>') \
.rstrip(',')
Expand Down

0 comments on commit b50d167

Please sign in to comment.