slang-netlist  0.10.0
Loading...
Searching...
No Matches
ReportVisitorBase.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "common/Wildcard.hpp"
5
6#include "slang/ast/ASTVisitor.h"
7#include "slang/ast/Compilation.h"
8#include "slang/text/FormatBuffer.h"
9#include "slang/text/Json.h"
10#include "slang/text/SourceLocation.h"
11
12#include <string>
13#include <string_view>
14#include <vector>
15
16namespace slang::report {
17
30template <typename Derived, typename Info>
32 : public ast::ASTVisitor<Derived, ast::VisitFlags::Expressions |
33 ast::VisitFlags::Canonical> {
34protected:
35 ast::Compilation &compilation;
36 std::vector<Info> items;
37 std::vector<std::string> nameFilters;
38
40 auto locationStr(SourceLocation loc) const -> std::string {
42 }
43
46 auto nameMatches(std::string_view name) const -> bool {
47 if (nameFilters.empty()) {
48 return true;
49 }
50 auto subject = std::string(name);
51 for (auto const &pattern : nameFilters) {
52 if (netlist::wildcardMatch(subject.c_str(), pattern.c_str())) {
53 return true;
54 }
55 }
56 return false;
57 }
58
59public:
60 explicit ReportVisitorBase(ast::Compilation &compilation)
62
67 void setNameFilters(std::vector<std::string> filters) {
68 nameFilters = std::move(filters);
69 }
70
72 void report(FormatBuffer &buffer) {
73 auto header = static_cast<Derived const *>(this)->tableHeader();
74 auto table = netlist::Utilities::Table{};
75 for (auto const &item : items) {
76 static_cast<Derived const *>(this)->appendItemRows(table, item);
77 }
78 netlist::Utilities::formatTable(buffer, header, table);
79 }
80
82 void report(JsonWriter &writer) {
83 writer.startArray();
84 for (auto const &item : items) {
85 static_cast<Derived const *>(this)->emitJsonItem(writer, item);
86 }
87 writer.endArray();
88 }
89};
90
91} // namespace slang::report
ast::Compilation & compilation
Definition ReportVisitorBase.hpp:35
ReportVisitorBase(ast::Compilation &compilation)
Definition ReportVisitorBase.hpp:60
void report(JsonWriter &writer)
Render the collected information as a JSON array of objects.
Definition ReportVisitorBase.hpp:82
std::vector< std::string > nameFilters
Definition ReportVisitorBase.hpp:37
auto locationStr(SourceLocation loc) const -> std::string
Format a source location using the compilation's SourceManager.
Definition ReportVisitorBase.hpp:40
auto nameMatches(std::string_view name) const -> bool
Definition ReportVisitorBase.hpp:46
void report(FormatBuffer &buffer)
Render the collected information as a human-readable table.
Definition ReportVisitorBase.hpp:72
void setNameFilters(std::vector< std::string > filters)
Definition ReportVisitorBase.hpp:67
std::vector< Info > items
Definition ReportVisitorBase.hpp:36
auto wildcardMatch(const char *text, const char *pattern) -> bool
Definition Wildcard.hpp:26
Definition ReportDrivers.hpp:12
std::vector< Row > Table
Definition Utilities.hpp:33
static auto locationStr(ast::Compilation const &compilation, SourceLocation location)
Return a string representation of a slang SourceLocation.
Definition Utilities.hpp:21
static auto formatTable(FormatBuffer &buffer, const Row &header, const Table &rows, TableFormatConfig cfg={})
Format a table of data into the given format buffer.
Definition Utilities.hpp:44