r362000 - [analyzer][AST] print() JSONify: Stmt implementation
Csaba Dabis via cfe-commits
cfe-commits at lists.llvm.org
Wed May 29 11:17:18 PDT 2019
Author: charusso
Date: Wed May 29 11:17:18 2019
New Revision: 362000
URL: http://llvm.org/viewvc/llvm-project?rev=362000&view=rev
Log:
[analyzer][AST] print() JSONify: Stmt implementation
Summary:
This patch also adds a function called `JsonFormat()` which:
- Flattens the string so removes the new-lines.
- Escapes double quotes.
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/D62494
Modified:
cfe/trunk/include/clang/AST/Stmt.h
cfe/trunk/include/clang/Basic/JsonSupport.h
cfe/trunk/lib/AST/StmtPrinter.cpp
cfe/trunk/lib/Analysis/ProgramPoint.cpp
cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp
cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp
Modified: cfe/trunk/include/clang/AST/Stmt.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Stmt.h?rev=362000&r1=361999&r2=362000&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/trunk/include/clang/AST/Stmt.h Wed May 29 11:17:18 2019
@@ -1100,6 +1100,10 @@ public:
StringRef NewlineSymbol = "\n",
const ASTContext *Context = nullptr) const;
+ /// Pretty-prints in JSON format.
+ void printJson(raw_ostream &Out, PrinterHelper *Helper,
+ const PrintingPolicy &Policy, bool AddQuotes) const;
+
/// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
/// works on systems with GraphViz (Mac OS X) or dot+gv installed.
void viewAST() const;
Modified: cfe/trunk/include/clang/Basic/JsonSupport.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/JsonSupport.h?rev=362000&r1=361999&r2=362000&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/JsonSupport.h (original)
+++ cfe/trunk/include/clang/Basic/JsonSupport.h Wed May 29 11:17:18 2019
@@ -10,6 +10,7 @@
#define LLVM_CLANG_BASIC_JSONSUPPORT_H
#include "clang/Basic/LLVM.h"
+#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
@@ -22,6 +23,41 @@ inline raw_ostream &Indent(raw_ostream &
return Out;
}
+inline std::string JsonFormat(StringRef RawSR, bool AddQuotes) {
+ if (RawSR.empty())
+ return "null";
+
+ // Trim special characters.
+ std::string Str = RawSR.trim().str();
+ size_t Pos = 0;
+
+ // Escape double quotes.
+ while (true) {
+ Pos = Str.find('\"', Pos);
+ if (Pos == std::string::npos)
+ break;
+
+ // Prevent bad conversions.
+ size_t TempPos = (Pos != 0) ? Pos - 1 : 0;
+
+ // See whether the current double quote is escaped.
+ if (TempPos != Str.find("\\\"", TempPos)) {
+ Str.insert(Pos, "\\");
+ ++Pos; // As we insert the escape-character move plus one.
+ }
+
+ ++Pos;
+ }
+
+ // Remove new-lines.
+ Str.erase(std::remove(Str.begin(), Str.end(), '\n'), Str.end());
+
+ if (!AddQuotes)
+ return Str;
+
+ return '\"' + Str + '\"';
+}
+
} // namespace clang
#endif // LLVM_CLANG_BASIC_JSONSUPPORT_H
Modified: cfe/trunk/lib/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/StmtPrinter.cpp?rev=362000&r1=361999&r2=362000&view=diff
==============================================================================
--- cfe/trunk/lib/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/lib/AST/StmtPrinter.cpp Wed May 29 11:17:18 2019
@@ -36,6 +36,7 @@
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/ExpressionTraits.h"
#include "clang/Basic/IdentifierTable.h"
+#include "clang/Basic/JsonSupport.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/Lambda.h"
#include "clang/Basic/OpenMPKinds.h"
@@ -2395,12 +2396,21 @@ void Stmt::dumpPretty(const ASTContext &
printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
}
-void Stmt::printPretty(raw_ostream &OS, PrinterHelper *Helper,
+void Stmt::printPretty(raw_ostream &Out, PrinterHelper *Helper,
const PrintingPolicy &Policy, unsigned Indentation,
- StringRef NL,
- const ASTContext *Context) const {
- StmtPrinter P(OS, Helper, Policy, Indentation, NL, Context);
- P.Visit(const_cast<Stmt*>(this));
+ StringRef NL, const ASTContext *Context) const {
+ StmtPrinter P(Out, Helper, Policy, Indentation, NL, Context);
+ P.Visit(const_cast<Stmt *>(this));
+}
+
+void Stmt::printJson(raw_ostream &Out, PrinterHelper *Helper,
+ const PrintingPolicy &Policy, bool AddQuotes) const {
+ std::string Buf;
+ llvm::raw_string_ostream TempOut(Buf);
+
+ printPretty(TempOut, Helper, Policy);
+
+ Out << JsonFormat(TempOut.str(), AddQuotes);
}
//===----------------------------------------------------------------------===//
Modified: cfe/trunk/lib/Analysis/ProgramPoint.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ProgramPoint.cpp?rev=362000&r1=361999&r2=362000&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ProgramPoint.cpp (original)
+++ cfe/trunk/lib/Analysis/ProgramPoint.cpp Wed May 29 11:17:18 2019
@@ -46,8 +46,8 @@ LLVM_DUMP_METHOD void ProgramPoint::dump
return printJson(llvm::errs());
}
-static void printLocation(raw_ostream &Out, SourceLocation Loc,
- const SourceManager &SM) {
+static void printLocJson(raw_ostream &Out, SourceLocation Loc,
+ const SourceManager &SM) {
Out << "\"location\": ";
if (!Loc.isFileID()) {
Out << "null";
@@ -62,6 +62,8 @@ void ProgramPoint::printJson(llvm::raw_o
const ASTContext &Context =
getLocationContext()->getAnalysisDeclContext()->getASTContext();
const SourceManager &SM = Context.getSourceManager();
+ const PrintingPolicy &PP = Context.getPrintingPolicy();
+ const bool AddQuotes = true;
Out << "\"kind\": \"";
switch (getKind()) {
@@ -78,9 +80,8 @@ void ProgramPoint::printJson(llvm::raw_o
<< ", \"stmt_id\": ";
if (const ReturnStmt *RS = FEP->getStmt()) {
- Out << RS->getID(Context) << ", \"stmt\": \"";
- RS->printPretty(Out, /*Helper=*/nullptr, Context.getPrintingPolicy());
- Out << '\"';
+ Out << RS->getID(Context) << ", \"stmt\": ";
+ RS->printJson(Out, nullptr, PP, AddQuotes);
} else {
Out << "null, \"stmt\": null";
}
@@ -118,7 +119,7 @@ void ProgramPoint::printJson(llvm::raw_o
Out << "PreCall\", \"stmt\": \"";
PC.getDecl()->print(Out, Context.getLangOpts());
Out << "\", ";
- printLocation(Out, PC.getLocation(), SM);
+ printLocJson(Out, PC.getLocation(), SM);
break;
}
@@ -127,7 +128,7 @@ void ProgramPoint::printJson(llvm::raw_o
Out << "PostCall\", \"stmt\": \"";
PC.getDecl()->print(Out, Context.getLangOpts());
Out << "\", ";
- printLocation(Out, PC.getLocation(), SM);
+ printLocJson(Out, PC.getLocation(), SM);
break;
}
@@ -157,23 +158,26 @@ void ProgramPoint::printJson(llvm::raw_o
E.getSrc()->printTerminator(Out, Context.getLangOpts());
Out << "\", ";
- printLocation(Out, T->getBeginLoc(), SM);
- Out << ", \"term_kind\": \"";
+ printLocJson(Out, T->getBeginLoc(), SM);
+ Out << ", \"term_kind\": \"";
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
+ if (const Stmt *LHS = C->getLHS()) {
+ LHS->printJson(Out, nullptr, PP, AddQuotes);
+ } else {
Out << "null";
+ }
+
Out << ", \"rhs\": ";
- if (const Stmt *RHS = C->getRHS())
- RHS->printPretty(Out, nullptr, Context.getPrintingPolicy());
- else
+ if (const Stmt *RHS = C->getRHS()) {
+ RHS->printJson(Out, nullptr, PP, AddQuotes);
+ } else {
Out << "null";
+ }
Out << " }";
} else {
assert(isa<DefaultStmt>(Label));
@@ -196,23 +200,14 @@ void ProgramPoint::printJson(llvm::raw_o
const Stmt *S = castAs<StmtPoint>().getStmt();
assert(S != nullptr && "Expecting non-null Stmt");
- 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, ";
- }
+ S->printJson(Out, nullptr, PP, AddQuotes);
- printLocation(Out, S->getBeginLoc(), SM);
+ Out << ", ";
+ printLocJson(Out, S->getBeginLoc(), SM);
Out << ", \"stmt_point_kind\": ";
if (getAs<PreStmt>())
Modified: cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp?rev=362000&r1=361999&r2=362000&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp Wed May 29 11:17:18 2019
@@ -235,9 +235,6 @@ void Environment::printJson(raw_ostream
bool HasItem = false;
unsigned int InnerSpace = Space + 1;
- llvm::SmallString<256> TempBuf;
- llvm::raw_svector_ostream TempOut(TempBuf);
-
// Store the last ExprBinding which we will print.
BindingsTy::iterator LastI = ExprBindings.end();
for (BindingsTy::iterator I = ExprBindings.begin(); I != ExprBindings.end();
@@ -266,14 +263,7 @@ void Environment::printJson(raw_ostream
<< "{ \"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";
- }
+ S->printJson(Out, nullptr, PP, /*AddQuotes=*/true);
Out << ", \"value\": \"" << I->second << "\" }";
Modified: cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp?rev=362000&r1=361999&r2=362000&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp Wed May 29 11:17:18 2019
@@ -170,17 +170,7 @@ public:
Out << ", \"pretty\": ";
if (S) {
- llvm::SmallString<256> TempBuf;
- llvm::raw_svector_ostream TempOut(TempBuf);
-
- // See whether the current statement is pretty-printable.
- S->printPretty(TempOut, Helper, PP);
- if (!TempBuf.empty()) {
- Out << '\"' << TempBuf.str().trim() << '\"';
- TempBuf.clear();
- } else {
- Out << "null";
- }
+ S->printJson(Out, Helper, PP, /*AddQuotes=*/true);
} else {
Out << '\"' << I->getAnyMember()->getNameAsString() << '\"';
}
More information about the cfe-commits
mailing list