Add JSON AST dumps (#5020)

This commit is contained in:
Szymon Gizler
2024-03-28 07:32:18 -04:00
committed by GitHub
parent f645382f11
commit 8301fdc6d3
17 changed files with 828 additions and 26 deletions
+20 -11
View File
@@ -17,7 +17,6 @@
#include "V3PchAstMT.h"
#include "V3Broken.h"
#include "V3EmitV.h"
#include "V3File.h"
#include <iomanip>
@@ -1198,6 +1197,23 @@ void AstNode::checkTreeIter(const AstNode* prevBackp) const VL_MT_STABLE {
}
}
// cppcheck-suppress unusedFunction // Debug only
char* AstNode::dumpTreeJsonGdb(const AstNode* nodep) {
if (!nodep) return strdup("{\"addr\":\"NULL\"}\n");
std::stringstream nodepStream;
nodep->dumpTreeJson(nodepStream);
const std::string str = nodepStream.rdbuf()->str();
return strdup(str.c_str());
}
// cppcheck-suppress unusedFunction // Debug only
// identity func to allow for passing already done dumps to jtree
char* AstNode::dumpTreeJsonGdb(const char* str) { return strdup(str); }
// cppcheck-suppress unusedFunction // Debug only
// allow for passing pointer literals like 0x42.. without manual cast
char* AstNode::dumpTreeJsonGdb(intptr_t nodep) {
if (!nodep) return strdup("{\"addr\":\"NULL\"}\n");
return dumpTreeJsonGdb((const AstNode*)nodep);
}
// cppcheck-suppress unusedFunction // Debug only
void AstNode::dumpGdb(const AstNode* nodep) { // For GDB only // LCOV_EXCL_LINE
if (!nodep) {
@@ -1301,7 +1317,7 @@ void AstNode::dumpTreeAndNext(std::ostream& os, const string& indent, int maxDep
}
}
void AstNode::dumpTreeFile(const string& filename, bool doDump, bool doCheck) {
void AstNode::dumpTreeFile(const string& filename, bool doDump) {
// Not const function as calls checkTree
if (doDump) {
{ // Write log & close
@@ -1319,14 +1335,6 @@ void AstNode::dumpTreeFile(const string& filename, bool doDump, bool doCheck) {
}
}
}
if (doDump && v3Global.opt.debugEmitV()) V3EmitV::debugEmitV(filename + ".v");
if (doCheck && (v3Global.opt.debugCheck() || ::dumpTreeLevel())) {
// Error check
checkTree();
// Broken isn't part of check tree because it can munge iterp's
// set by other steps if it is called in the middle of other operations
if (AstNetlist* const netp = VN_CAST(this, Netlist)) V3Broken::brokenAll(netp);
}
}
static void drawChildren(std::ostream& os, const AstNode* thisp, const AstNode* childp,
@@ -1365,10 +1373,11 @@ void AstNode::dumpTreeJsonFile(const string& filename, bool doDump) {
*treejsonp << '\n';
}
void AstNode::dumpJsonMetaFileGdb(const char* filename) { dumpJsonMetaFile(filename); }
void AstNode::dumpJsonMetaFile(const string& filename) {
UINFO(2, "Dumping " << filename << endl);
const std::unique_ptr<std::ofstream> treejsonp{V3File::new_ofstream(filename)};
if (treejsonp->fail()) v3fatal("Can't write " << filename);
if (treejsonp->fail()) v3fatalStatic("Can't write " << filename);
*treejsonp << '{';
FileLine::fileNameNumMapDumpJson(*treejsonp);
*treejsonp << ',';