[analyzer] print() JSONify: getNodeLabel implementation

Summary: This patch also rewrites the ProgramPoint printing.

Reviewers: NoQ, xazax.hun, ravikandhadai, baloghadamsoftware, Szelethus

Reviewed By: NoQ

Subscribers: cfe-commits, szepet, rnkovacs, a.sidorin, mikhail.ramalho,
             donat.nagy, dkrupp

Tags: #clang

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

llvm-svn: 361997
This commit is contained in:
Csaba Dabis 2019-05-29 18:05:53 +00:00
parent f6faa382f3
commit 13e491cca5
5 changed files with 155 additions and 117 deletions

View File

@ -213,7 +213,7 @@ public:
ID.AddPointer(getTag());
}
void print(StringRef CR, llvm::raw_ostream &Out) const;
void printJson(llvm::raw_ostream &Out, const char *NL = "\n") const;
LLVM_DUMP_METHOD void dump() const;

View File

@ -43,151 +43,152 @@ ProgramPoint ProgramPoint::getProgramPoint(const Stmt *S, ProgramPoint::Kind K,
}
LLVM_DUMP_METHOD void ProgramPoint::dump() const {
return print(/*CR=*/"\n", llvm::errs());
return printJson(llvm::errs());
}
static void printLocation(raw_ostream &Out, SourceLocation SLoc,
const SourceManager &SM,
StringRef CR,
StringRef Postfix) {
if (SLoc.isFileID()) {
Out << CR << "line=" << SM.getExpansionLineNumber(SLoc)
<< " col=" << SM.getExpansionColumnNumber(SLoc) << Postfix;
static void printLocation(raw_ostream &Out, SourceLocation Loc,
const SourceManager &SM) {
Out << "\"location\": ";
if (!Loc.isFileID()) {
Out << "null";
return;
}
Out << "{ \"line\": " << SM.getExpansionLineNumber(Loc)
<< ", \"column\": " << SM.getExpansionColumnNumber(Loc) << " }";
}
void ProgramPoint::print(StringRef CR, llvm::raw_ostream &Out) const {
void ProgramPoint::printJson(llvm::raw_ostream &Out, const char *NL) const {
const ASTContext &Context =
getLocationContext()->getAnalysisDeclContext()->getASTContext();
const SourceManager &SM = Context.getSourceManager();
Out << "\"kind\": \"";
switch (getKind()) {
case ProgramPoint::BlockEntranceKind:
Out << "Block Entrance: B"
Out << "BlockEntrance\""
<< ", \"block_id\": "
<< castAs<BlockEntrance>().getBlock()->getBlockID();
break;
case ProgramPoint::FunctionExitKind: {
auto FEP = getAs<FunctionExitPoint>();
Out << "Function Exit: B" << FEP->getBlock()->getBlockID();
Out << "FunctionExit\""
<< ", \"block_id\": " << FEP->getBlock()->getBlockID()
<< ", \"stmt_id\": ";
if (const ReturnStmt *RS = FEP->getStmt()) {
Out << CR << " Return: S" << RS->getID(Context) << CR;
RS->printPretty(Out, /*helper=*/nullptr, Context.getPrintingPolicy(),
/*Indentation=*/2, /*NewlineSymbol=*/CR);
Out << RS->getID(Context) << ", \"stmt\": \"";
RS->printPretty(Out, /*Helper=*/nullptr, Context.getPrintingPolicy());
Out << '\"';
} else {
Out << "null, \"stmt\": null";
}
break;
}
case ProgramPoint::BlockExitKind:
assert(false);
llvm_unreachable("BlockExitKind");
break;
case ProgramPoint::CallEnterKind:
Out << "CallEnter";
Out << "CallEnter\"";
break;
case ProgramPoint::CallExitBeginKind:
Out << "CallExitBegin";
Out << "CallExitBegin\"";
break;
case ProgramPoint::CallExitEndKind:
Out << "CallExitEnd";
Out << "CallExitEnd\"";
break;
case ProgramPoint::PostStmtPurgeDeadSymbolsKind:
Out << "PostStmtPurgeDeadSymbols";
Out << "PostStmtPurgeDeadSymbols\"";
break;
case ProgramPoint::PreStmtPurgeDeadSymbolsKind:
Out << "PreStmtPurgeDeadSymbols";
Out << "PreStmtPurgeDeadSymbols\"";
break;
case ProgramPoint::EpsilonKind:
Out << "Epsilon Point";
Out << "EpsilonPoint\"";
break;
case ProgramPoint::LoopExitKind: {
LoopExit LE = castAs<LoopExit>();
Out << "LoopExit: " << LE.getLoopStmt()->getStmtClassName();
case ProgramPoint::LoopExitKind:
Out << "LoopExit\", \"stmt\": \""
<< castAs<LoopExit>().getLoopStmt()->getStmtClassName() << '\"';
break;
}
case ProgramPoint::PreImplicitCallKind: {
ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
Out << "PreCall: ";
Out << "PreCall\", \"stmt\": \"";
PC.getDecl()->print(Out, Context.getLangOpts());
printLocation(Out, PC.getLocation(), SM, CR, /*Postfix=*/CR);
Out << "\", ";
printLocation(Out, PC.getLocation(), SM);
break;
}
case ProgramPoint::PostImplicitCallKind: {
ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
Out << "PostCall: ";
Out << "PostCall\", \"stmt\": \"";
PC.getDecl()->print(Out, Context.getLangOpts());
printLocation(Out, PC.getLocation(), SM, CR, /*Postfix=*/CR);
Out << "\", ";
printLocation(Out, PC.getLocation(), SM);
break;
}
case ProgramPoint::PostInitializerKind: {
Out << "PostInitializer: ";
Out << "PostInitializer\", ";
const CXXCtorInitializer *Init = castAs<PostInitializer>().getInitializer();
if (const FieldDecl *FD = Init->getAnyMember())
Out << *FD;
else {
if (const FieldDecl *FD = Init->getAnyMember()) {
Out << "\"field_decl\": \"" << *FD << '\"';
} else {
Out << "\"type\": \"";
QualType Ty = Init->getTypeSourceInfo()->getType();
Ty = Ty.getLocalUnqualifiedType();
Ty.print(Out, Context.getLangOpts());
Out << '\"';
}
break;
}
case ProgramPoint::BlockEdgeKind: {
const BlockEdge &E = castAs<BlockEdge>();
Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
<< E.getDst()->getBlockID() << ')';
const Stmt *T = E.getSrc()->getTerminatorStmt();
Out << "Edge\", \"src_id\": " << E.getSrc()->getBlockID()
<< ", \"dst_id\": " << E.getDst()->getBlockID()
<< ", \"terminator\": " << (!T ? "null, \"term_kind\": null" : "\"");
if (!T)
break;
if (const Stmt *T = E.getSrc()->getTerminatorStmt()) {
SourceLocation SLoc = T->getBeginLoc();
E.getSrc()->printTerminator(Out, Context.getLangOpts());
Out << "\", ";
printLocation(Out, T->getBeginLoc(), SM);
Out << ", \"term_kind\": \"";
Out << "\\|Terminator: ";
E.getSrc()->printTerminator(Out, Context.getLangOpts());
printLocation(Out, SLoc, SM, CR, /*Postfix=*/"");
if (isa<SwitchStmt>(T)) {
const Stmt *Label = E.getDst()->getLabel();
if (Label) {
if (const auto *C = dyn_cast<CaseStmt>(Label)) {
Out << CR << "case ";
if (C->getLHS())
C->getLHS()->printPretty(
Out, nullptr, Context.getPrintingPolicy(),
/*Indentation=*/0, /*NewlineSymbol=*/CR);
if (const Stmt *RHS = C->getRHS()) {
Out << " .. ";
RHS->printPretty(Out, nullptr, Context.getPrintingPolicy(),
/*Indetation=*/0, /*NewlineSymbol=*/CR);
}
Out << ":";
} else {
assert(isa<DefaultStmt>(Label));
Out << CR << "default:";
}
} else
Out << CR << "(implicit) default:";
} else if (isa<IndirectGotoStmt>(T)) {
// FIXME
if (isa<SwitchStmt>(T)) {
Out << "SwitchStmt\", \"case\": ";
if (const Stmt *Label = E.getDst()->getLabel()) {
if (const auto *C = dyn_cast<CaseStmt>(Label)) {
Out << "{ \"lhs\": ";
if (const Stmt *LHS = C->getLHS())
LHS->printPretty(Out, nullptr, Context.getPrintingPolicy());
else
Out << "null";
Out << ", \"rhs\": ";
if (const Stmt *RHS = C->getRHS())
RHS->printPretty(Out, nullptr, Context.getPrintingPolicy());
else
Out << "null";
Out << " }";
} else {
assert(isa<DefaultStmt>(Label));
Out << "\"default\"";
}
} else {
Out << CR << "Condition: ";
if (*E.getSrc()->succ_begin() == E.getDst())
Out << "true";
else
Out << "false";
Out << "\"implicit default\"";
}
Out << CR;
} else if (isa<IndirectGotoStmt>(T)) {
// FIXME: More info.
Out << "IndirectGotoStmt\"";
} else {
Out << "Condition\", \"value\": "
<< (*E.getSrc()->succ_begin() == E.getDst() ? "true" : "false");
}
break;
}
@ -195,22 +196,37 @@ void ProgramPoint::print(StringRef CR, llvm::raw_ostream &Out) const {
const Stmt *S = castAs<StmtPoint>().getStmt();
assert(S != nullptr && "Expecting non-null Stmt");
Out << S->getStmtClassName() << " S" << S->getID(Context) << " <"
<< (const void *)S << "> ";
S->printPretty(Out, /*helper=*/nullptr, Context.getPrintingPolicy(),
/*Indentation=*/2, /*NewlineSymbol=*/CR);
printLocation(Out, S->getBeginLoc(), SM, CR, /*Postfix=*/"");
llvm::SmallString<256> TempBuf;
llvm::raw_svector_ostream TempOut(TempBuf);
Out << "Statement\", \"stmt_kind\": \"" << S->getStmtClassName()
<< "\", \"stmt_id\": " << S->getID(Context)
<< ", \"pointer\": \"" << (const void *)S << "\", \"pretty\": ";
// See whether the current statement is pretty-printable.
S->printPretty(TempOut, /*Helper=*/nullptr, Context.getPrintingPolicy());
if (!TempBuf.empty()) {
Out << '\"' << TempBuf.str().trim() << "\", ";
TempBuf.clear();
} else {
Out << "null, ";
}
printLocation(Out, S->getBeginLoc(), SM);
Out << ", \"stmt_point_kind\": ";
if (getAs<PreStmt>())
Out << CR << "PreStmt" << CR;
Out << "\"PreStmt\"";
else if (getAs<PostLoad>())
Out << CR << "PostLoad" << CR;
Out << "\"PostLoad\"";
else if (getAs<PostStore>())
Out << CR << "PostStore" << CR;
Out << "\"PostStore\"";
else if (getAs<PostLValue>())
Out << CR << "PostLValue" << CR;
Out << "\"PostLValue\"";
else if (getAs<PostAllocatorCall>())
Out << CR << "PostAllocatorCall" << CR;
Out << "\"PostAllocatorCall\"";
else
Out << "null";
break;
}

View File

@ -162,12 +162,12 @@ public:
<< "\", \"argument_index\": ";
if (getItem().getKind() == ConstructionContextItem::ArgumentKind)
Out << getItem().getIndex() << '\"';
Out << getItem().getIndex();
else
Out << "null";
// Pretty-print
Out << ", \"pretty\": \"";
Out << ", \"pretty\": ";
if (S) {
llvm::SmallString<256> TempBuf;
@ -176,13 +176,13 @@ public:
// See whether the current statement is pretty-printable.
S->printPretty(TempOut, Helper, PP);
if (!TempBuf.empty()) {
Out << TempBuf.str().trim() << '\"';
Out << '\"' << TempBuf.str().trim() << '\"';
TempBuf.clear();
} else {
Out << "null";
}
} else {
Out << I->getAnyMember()->getNameAsString() << '\"';
Out << '\"' << I->getAnyMember()->getNameAsString() << '\"';
}
}
@ -3079,37 +3079,55 @@ struct DOTGraphTraits<ExplodedGraph*> : public DefaultDOTGraphTraits {
}
static std::string getNodeLabel(const ExplodedNode *N, ExplodedGraph *G){
std::string sbuf;
llvm::raw_string_ostream Out(sbuf);
std::string Buf;
llvm::raw_string_ostream Out(Buf);
const bool IsDot = true;
const unsigned int Space = 1;
ProgramStateRef State = N->getState();
Out << "{ \"node_id\": \"" << (const void *)N
<< "\", \"state_id\": " << State->getID()
<< ", \"has_report\": " << (nodeHasBugReport(N) ? "true" : "false")
<< ",\\l";
Indent(Out, Space, IsDot) << "\"program_points\": [\\l";
// Dump program point for all the previously skipped nodes.
traverseHiddenNodes(
N,
[&](const ExplodedNode *OtherNode) {
OtherNode->getLocation().print(/*CR=*/"\\l", Out);
Indent(Out, Space + 1, IsDot) << "{ ";
OtherNode->getLocation().printJson(Out, /*NL=*/"\\l");
Out << ", \"tag\": ";
if (const ProgramPointTag *Tag = OtherNode->getLocation().getTag())
Out << "\\lTag:" << Tag->getTagDescription();
if (N->isSink())
Out << "\\lNode is sink\\l";
if (nodeHasBugReport(N))
Out << "\\lBug report attached\\l";
Out << '\"' << Tag->getTagDescription() << "\" }";
else
Out << "null }";
},
[&](const ExplodedNode *) { Out << "\\l--------\\l"; },
// Adds a comma and a new-line between each program point.
[&](const ExplodedNode *) { Out << ",\\l"; },
[&](const ExplodedNode *) { return false; });
Out << "\\l\\|";
Out << "StateID: ST" << State->getID() << ", NodeID: N" << N->getID(G)
<< " <" << (const void *)N << ">\\|";
Out << "\\l"; // Adds a new-line to the last program point.
Indent(Out, Space, IsDot) << "],\\l";
bool SameAsAllPredecessors =
std::all_of(N->pred_begin(), N->pred_end(), [&](const ExplodedNode *P) {
return P->getState() == State;
});
if (!SameAsAllPredecessors)
State->printDOT(Out, N->getLocationContext());
if (!SameAsAllPredecessors) {
State->printDOT(Out, N->getLocationContext(), Space);
} else {
Indent(Out, Space, IsDot) << "\"program_state\": null";
}
Out << "\\l}";
if (!N->succ_empty())
Out << ',';
Out << "\\l";
return Out.str();
}
};

View File

@ -11,6 +11,10 @@ int foo() {
}
// CHECK: digraph "Exploded Graph" {
// CHECK: Edge: (B2, B1)
// CHECK: Block Entrance: B1
// CHECK: Bug report attached
// CHECK: \"program_points\": [\l&nbsp;&nbsp;&nbsp;&nbsp;\{ \"kind\": \"Edge\", \"src_id\": 2, \"dst_id\": 1, \"terminator\": null, \"term_kind\": null, \"tag\": null \}\l&nbsp;&nbsp;],\l&nbsp;&nbsp;\"program_state\": null
// CHECK: \"program_points\": [\l&nbsp;&nbsp;&nbsp;&nbsp;\{ \"kind\": \"BlockEntrance\", \"block_id\": 1
// CHECK: \"has_report\": true

View File

@ -16,9 +16,9 @@ void foo() {
T t;
}
// CHECK: \"constructing_objects\": [\l&nbsp;&nbsp;&nbsp;&nbsp;\{ \"location_context\": \"#0 Call\", \"calling\": \"foo\", \"call_line\": null, \"items\": [\l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\{ \"lctx_id\": 1, \"stmt_id\": 1155, \"kind\": \"construct into local variable\", \"argument_index\": null, \"pretty\": \"T t;\", \"value\": \"&t\"
// CHECK: \"constructing_objects\": [\l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\{ \"location_context\": \"#0 Call\", \"calling\": \"foo\", \"call_line\": null, \"items\": [\l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\{ \"lctx_id\": 1, \"stmt_id\": 1155, \"kind\": \"construct into local variable\", \"argument_index\": null, \"pretty\": \"T t;\", \"value\": \"&t\"
// CHECK: \"constructing_objects\": [\l&nbsp;&nbsp;&nbsp;&nbsp;\{ \"location_context\": \"#0 Call\", \"calling\": \"T::T\", \"call_line\": \"16\", \"items\": [\l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\{ \"lctx_id\": 2, \"init_id\": 1092, \"kind\": \"construct into member variable\", \"argument_index\": null, \"pretty\": \"s\", \"value\": \"&t-\>s\"
// CHECK: \"constructing_objects\": [\l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\{ \"location_context\": \"#0 Call\", \"calling\": \"T::T\", \"call_line\": \"16\", \"items\": [\l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\{ \"lctx_id\": 2, \"init_id\": 1092, \"kind\": \"construct into member variable\", \"argument_index\": null, \"pretty\": \"s\", \"value\": \"&t-\>s\"
// CHECK: \"store\": [\l&nbsp;&nbsp;&nbsp;&nbsp;\{ \"cluster\": \"t\", \"items\": [\l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\{ \"kind\": \"Default\", \"offset\": 0, \"value\": \"conj_$3\{int, LC3, no stmt, #1\}\"
// CHECK: \"store\": [\l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\{ \"cluster\": \"t\", \"items\": [\l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\{ \"kind\": \"Default\", \"offset\": 0, \"value\": \"conj_$3\{int, LC3, no stmt, #1\}\"