diff options
author | Csaba Dabis <dabis.csaba98@gmail.com> | 2019-05-29 15:36:58 +0000 |
---|---|---|
committer | Csaba Dabis <dabis.csaba98@gmail.com> | 2019-05-29 15:36:58 +0000 |
commit | 9ce37466043ee2f6ea8ed074f947c528ab0be37d (patch) | |
tree | d7235f1f764206341a6c69c333b2a3b22d2005e4 /clang/lib/StaticAnalyzer/Core/Environment.cpp | |
parent | d506b0a4843f04ea5e7d36dbb595e31025b60b9a (diff) | |
download | bcm5719-llvm-9ce37466043ee2f6ea8ed074f947c528ab0be37d.tar.gz bcm5719-llvm-9ce37466043ee2f6ea8ed074f947c528ab0be37d.zip |
[analyzer] print() JSONify: Environment 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/D62081
llvm-svn: 361976
Diffstat (limited to 'clang/lib/StaticAnalyzer/Core/Environment.cpp')
-rw-r--r-- | clang/lib/StaticAnalyzer/Core/Environment.cpp | 87 |
1 files changed, 69 insertions, 18 deletions
diff --git a/clang/lib/StaticAnalyzer/Core/Environment.cpp b/clang/lib/StaticAnalyzer/Core/Environment.cpp index 9d888ece177..6344bc5a4d6 100644 --- a/clang/lib/StaticAnalyzer/Core/Environment.cpp +++ b/clang/lib/StaticAnalyzer/Core/Environment.cpp @@ -18,6 +18,7 @@ #include "clang/Analysis/AnalysisDeclContext.h" #include "clang/Basic/LLVM.h" #include "clang/Basic/LangOptions.h" +#include "clang/Basic/JsonSupport.h" #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" @@ -199,43 +200,93 @@ EnvironmentManager::removeDeadBindings(Environment Env, return NewEnv; } -void Environment::print(raw_ostream &Out, const char *NL, - const char *Sep, - const ASTContext &Context, - const LocationContext *WithLC) const { - if (ExprBindings.isEmpty()) +void Environment::printJson(raw_ostream &Out, const ASTContext &Ctx, + const LocationContext *LCtx, const char *NL, + unsigned int Space, bool IsDot) const { + Indent(Out, Space, IsDot) << "\"environment\": "; + ++Space; + + if (ExprBindings.isEmpty()) { + Out << "null," << NL; return; + } - if (!WithLC) { + if (!LCtx) { // Find the freshest location context. llvm::SmallPtrSet<const LocationContext *, 16> FoundContexts; - for (auto I : *this) { + for (const auto &I : *this) { const LocationContext *LC = I.first.getLocationContext(); if (FoundContexts.count(LC) == 0) { // This context is fresher than all other contexts so far. - WithLC = LC; + LCtx = LC; for (const LocationContext *LCI = LC; LCI; LCI = LCI->getParent()) FoundContexts.insert(LCI); } } } - assert(WithLC); + assert(LCtx); + + Out << '[' << NL; // Start of Environment. + PrintingPolicy PP = Ctx.getPrintingPolicy(); + + LCtx->printJson(Out, NL, Space, IsDot, [&](const LocationContext *LC) { + // LCtx items begin + bool HasItem = false; + unsigned int InnerSpace = Space + 1; - PrintingPolicy PP = Context.getPrintingPolicy(); + llvm::SmallString<256> TempBuf; + llvm::raw_svector_ostream TempOut(TempBuf); - Out << NL << "Expressions by stack frame:" << NL; - WithLC->dumpStack(Out, "", NL, Sep, [&](const LocationContext *LC) { - for (auto I : ExprBindings) { - if (I.first.getLocationContext() != LC) + // Store the last ExprBinding which we will print. + BindingsTy::iterator LastI = ExprBindings.end(); + for (BindingsTy::iterator I = ExprBindings.begin(); I != ExprBindings.end(); + ++I) { + if (I->first.getLocationContext() != LC) continue; - const Stmt *S = I.first.getStmt(); + if (!HasItem) { + HasItem = true; + Out << '[' << NL; + } + + const Stmt *S = I->first.getStmt(); assert(S != nullptr && "Expected non-null Stmt"); - Out << "(LC" << LC->getID() << ", S" << S->getID(Context) << ") "; - S->printPretty(Out, /*Helper=*/nullptr, PP); - Out << " : " << I.second << NL; + LastI = I; + } + + for (BindingsTy::iterator I = ExprBindings.begin(); I != ExprBindings.end(); + ++I) { + if (I->first.getLocationContext() != LC) + continue; + + const Stmt *S = I->first.getStmt(); + Indent(Out, InnerSpace, IsDot) + << "{ \"lctx_id\": " << LC->getID() + << ", \"stmt_id\": " << S->getID(Ctx) << ", \"pretty\": "; + + // See whether the current statement is pretty-printable. + S->printPretty(TempOut, /*Helper=*/nullptr, PP); + if (!TempBuf.empty()) { + Out << '\"' << TempBuf.str().trim() << '\"'; + TempBuf.clear(); + } else { + Out << "null"; + } + + Out << ", \"value\": \"" << I->second << "\" }"; + + if (I != LastI) + Out << ','; + Out << NL; } + + if (HasItem) + Indent(Out, --InnerSpace, IsDot) << ']'; + else + Out << "null "; }); + + Indent(Out, --Space, IsDot) << "]," << NL; // End of Environment. } |