From 8004395d01b18dd4d7167f944bc12725a7e558ac Mon Sep 17 00:00:00 2001 From: Csaba Dabis Date: Wed, 29 May 2019 16:22:21 +0000 Subject: [PATCH] [analyzer] print() JSONify: Program state implementation Summary: - Reviewers: NoQ, xazax.hun, ravikandhadai, baloghadamsoftware, Szelethus Reviewed By: NoQ Subscribers: szepet, rnkovacs, a.sidorin, mikhail.ramalho, donat.nagy, dkrupp Tags: #clang Differential Revision: https://reviews.llvm.org/D62087 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@361983 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Core/PathSensitive/ProgramState.h | 4 +-- lib/StaticAnalyzer/Core/ProgramState.cpp | 23 ++++++++----- test/Analysis/dump_egraph.cpp | 6 ++-- test/Analysis/expr-inspection.c | 34 ++++++++++--------- 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h b/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h index 4092f40747..cddae9d02e 100644 --- a/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h +++ b/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h @@ -425,8 +425,8 @@ public: // Pretty-printing. void printJson(raw_ostream &Out, const LocationContext *LCtx = nullptr, - const char *NL = "\n", const char *Sep = "", - unsigned int Space = 0, bool IsDot = false) const; + const char *NL = "\n", unsigned int Space = 0, + bool IsDot = false) const; void printDOT(raw_ostream &Out, const LocationContext *LCtx = nullptr, unsigned int Space = 0) const; diff --git a/lib/StaticAnalyzer/Core/ProgramState.cpp b/lib/StaticAnalyzer/Core/ProgramState.cpp index 2f5c33ee6c..a1ca0b1b84 100644 --- a/lib/StaticAnalyzer/Core/ProgramState.cpp +++ b/lib/StaticAnalyzer/Core/ProgramState.cpp @@ -10,13 +10,14 @@ // //===----------------------------------------------------------------------===// -#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" #include "clang/Analysis/CFG.h" +#include "clang/Basic/JsonSupport.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h" #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h" -#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h" #include "llvm/Support/raw_ostream.h" using namespace clang; @@ -441,15 +442,18 @@ void ProgramState::setStore(const StoreRef &newStore) { //===----------------------------------------------------------------------===// void ProgramState::printJson(raw_ostream &Out, const LocationContext *LCtx, - const char *NL, const char *Sep, - unsigned int Space, bool IsDot) const { - // Print the store. + const char *NL, unsigned int Space, + bool IsDot) const { + Indent(Out, Space, IsDot) << "\"program_state\": {" << NL; + ++Space; + ProgramStateManager &Mgr = getStateManager(); - const ASTContext &Context = getStateManager().getContext(); + + // Print the store. Mgr.getStoreManager().printJson(Out, getStore(), NL, Space, IsDot); // Print out the environment. - Env.printJson(Out, Context, LCtx, NL, Space, IsDot); + Env.printJson(Out, Mgr.getContext(), LCtx, NL, Space, IsDot); // Print out the constraints. Mgr.getConstraintManager().printJson(Out, this, NL, Space, IsDot); @@ -459,11 +463,14 @@ void ProgramState::printJson(raw_ostream &Out, const LocationContext *LCtx, // Print checker-specific data. Mgr.getOwningEngine().printJson(Out, this, LCtx, NL, Space, IsDot); + + --Space; + Indent(Out, Space, IsDot) << '}'; } void ProgramState::printDOT(raw_ostream &Out, const LocationContext *LCtx, unsigned int Space) const { - printJson(Out, LCtx, "\\l", "\\|", Space, /*IsDot=*/true); + printJson(Out, LCtx, /*NL=*/"\\l", Space, /*IsDot=*/true); } LLVM_DUMP_METHOD void ProgramState::dump() const { diff --git a/test/Analysis/dump_egraph.cpp b/test/Analysis/dump_egraph.cpp index 9b05e4a6ba..e3e55554e4 100644 --- a/test/Analysis/dump_egraph.cpp +++ b/test/Analysis/dump_egraph.cpp @@ -16,9 +16,9 @@ void foo() { T t; } -// CHECK: \"constructing_objects\": [\l  \{ \"location_context\": \"#0 Call\", \"calling\": \"foo\", \"call_line\": null, \"items\": [\l    \{ \"lctx_id\": 1, \"stmt_id\": 1155, \"kind\": \"construct into local variable\", \"argument_index\": null, \"pretty\": \"T t;\" +// CHECK: \"constructing_objects\": [\l    \{ \"location_context\": \"#0 Call\", \"calling\": \"foo\", \"call_line\": null, \"items\": [\l      \{ \"lctx_id\": 1, \"stmt_id\": 1155, \"kind\": \"construct into local variable\", \"argument_index\": null, \"pretty\": \"T t;\", \"value\": \"&t\" -// CHECK: \"constructing_objects\": [\l  \{ \"location_context\": \"#0 Call\", \"calling\": \"T::T\", \"call_line\": \"16\", \"items\": [\l    \{ \"lctx_id\": 2, \"init_id\": 1092, \"kind\": \"construct into member variable\", \"argument_index\": null, \"pretty\": \"s\", \"value\": \"&t-\>s\" +// CHECK: \"constructing_objects\": [\l    \{ \"location_context\": \"#0 Call\", \"calling\": \"T::T\", \"call_line\": \"16\", \"items\": [\l      \{ \"lctx_id\": 2, \"init_id\": 1092, \"kind\": \"construct into member variable\", \"argument_index\": null, \"pretty\": \"s\", \"value\": \"&t-\>s\" -// CHECK: \"store\": [\l  \{ \"cluster\": \"t\", \"items\": [\l    \{ \"kind\": \"Default\", \"offset\": 0, \"value\": \"conj_$3\{int, LC3, no stmt, #1\}\" +// CHECK: \"store\": [\l    \{ \"cluster\": \"t\", \"items\": [\l      \{ \"kind\": \"Default\", \"offset\": 0, \"value\": \"conj_$3\{int, LC3, no stmt, #1\}\" diff --git a/test/Analysis/expr-inspection.c b/test/Analysis/expr-inspection.c index 2837c30d37..aec4c1ebae 100644 --- a/test/Analysis/expr-inspection.c +++ b/test/Analysis/expr-inspection.c @@ -23,20 +23,22 @@ void foo(int x) { } } -// CHECK: "store": [ -// CHECK-NEXT: { "cluster": "y", "items": [ -// CHECK-NEXT: { "kind": "Direct", "offset": 0, "value": "2 S32b" } -// CHECK-NEXT: ]} -// CHECK-NEXT: ], -// CHECK-NEXT: "environment": [ -// CHECK-NEXT: { "location_context": "#0 Call", "calling": "foo", "call_line": null, "items": [ -// CHECK-NEXT: { "lctx_id": 1, "stmt_id": 847, "pretty": "clang_analyzer_printState", "value": "&code{clang_analyzer_printState}" } -// CHECK-NEXT: ]} -// CHECK-NEXT: ], -// CHECK-NEXT: "constraints": [ -// CHECK-NEXT: { "symbol": "reg_$0", "range": "{ [-2147483648, 13] }" } -// CHECK-NEXT: ], -// CHECK-NEXT: "dynamic_types": null, -// CHECK-NEXT: "constructing_objects": null, -// CHECK-NEXT: "checker_messages": null +// CHECK: "program_state": { +// CHECK-NEXT: "store": [ +// CHECK-NEXT: { "cluster": "y", "items": [ +// CHECK-NEXT: { "kind": "Direct", "offset": 0, "value": "2 S32b" } +// CHECK-NEXT: ]} +// CHECK-NEXT: ], +// CHECK-NEXT: "environment": [ +// CHECK-NEXT: { "location_context": "#0 Call", "calling": "foo", "call_line": null, "items": [ +// CHECK-NEXT: { "lctx_id": 1, "stmt_id": 847, "pretty": "clang_analyzer_printState", "value": "&code{clang_analyzer_printState}" } +// CHECK-NEXT: ]} +// CHECK-NEXT: ], +// CHECK-NEXT: "constraints": [ +// CHECK-NEXT: { "symbol": "reg_$0", "range": "{ [-2147483648, 13] }" } +// CHECK-NEXT: ], +// CHECK-NEXT: "dynamic_types": null, +// CHECK-NEXT: "constructing_objects": null, +// CHECK-NEXT: "checker_messages": null +// CHECK-NEXT: } -- 2.40.0