From: Artem Dergachev Date: Wed, 19 Jun 2019 23:33:55 +0000 (+0000) Subject: [analyzer] exploded-graph-rewriter: Fix escaping StringRegions. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=28823bb64502587c484df3ffa707f7f562ef367f;p=clang [analyzer] exploded-graph-rewriter: Fix escaping StringRegions. 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@363897 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/StaticAnalyzer/Core/RegionStore.cpp b/lib/StaticAnalyzer/Core/RegionStore.cpp index fa9f751c3f..9bc0e62c27 100644 --- a/lib/StaticAnalyzer/Core/RegionStore.cpp +++ b/lib/StaticAnalyzer/Core/RegionStore.cpp @@ -210,6 +210,7 @@ public: 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; @@ -217,8 +218,9 @@ public: ++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; diff --git a/test/Analysis/exploded-graph-rewriter/escapes.c b/test/Analysis/exploded-graph-rewriter/escapes.c new file mode 100644 index 0000000000..2fd564cc5d --- /dev/null +++ b/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: Store: + // CHECK-SAME: foo0 + // CHECK-SAME: &Element\{"foo",0 S64b,char\} + // CHECK: Environment: + // CHECK-SAME: "foo" + // CHECK-SAME: &Element\{"foo",0 S64b,char\} + const char *const foo = "foo"; +} diff --git a/test/Analysis/exploded-graph-rewriter/lit.local.cfg b/test/Analysis/exploded-graph-rewriter/lit.local.cfg index 7bc2e107f6..dfeb0a86c4 100644 --- a/test/Analysis/exploded-graph-rewriter/lit.local.cfg +++ b/test/Analysis/exploded-graph-rewriter/lit.local.cfg @@ -15,4 +15,4 @@ config.substitutions.append(('%exploded_graph_rewriter', config.clang_src_dir, 'utils', 'analyzer'))))) -config.suffixes = ['.dot'] +config.suffixes.add('.dot') diff --git a/utils/analyzer/exploded-graph-rewriter.py b/utils/analyzer/exploded-graph-rewriter.py index dbfd086215..7f83f801ae 100755 --- a/utils/analyzer/exploded-graph-rewriter.py +++ b/utils/analyzer/exploded-graph-rewriter.py @@ -199,6 +199,7 @@ class ExplodedGraph(object): .replace('\\"', '"') \ .replace('\\{', '{') \ .replace('\\}', '}') \ + .replace('\\\\', '\\') \ .replace('\\<', '\\\\<') \ .replace('\\>', '\\\\>') \ .rstrip(',')