P4C
The P4 Compiler
Loading...
Searching...
No Matches
source_info_logging.h
1
19#ifndef BF_P4C_LOGGING_SOURCE_INFO_LOGGING_H_
20#define BF_P4C_LOGGING_SOURCE_INFO_LOGGING_H_
21
22// #define BF_P4C_LOGGING_SOURCE_INFO_LOGGING_H_DEBUG
23
24#include "frontends/common/resolveReferences/referenceMap.h"
25#include "ir/ir.h"
26#include "source_info_schema.h"
27
28// Logging::Source_Info_Schema_Logger is generated by generate_logging.py
29using Source_Info_Schema_Logger = Logging::Source_Info_Schema_Logger;
30
39class CollectSourceInfoLogging : public Inspector {
40 public:
41 // Source info structure for a P4 symbol
42 struct Symbol {
43 cstring name;
44 cstring type;
45 Util::SourceInfo inst;
46 std::set<Util::SourceInfo> refs;
47
48 Symbol(cstring name, cstring type, Util::SourceInfo inst)
49 : name(name), type(type), inst(inst) {}
50 Symbol() = default;
51 };
52
53 // Constructor (requires P4::ReferenceMap)
55
56 // This method can be used to add symbols later after the pass is applied
57 void addSymbol(const Symbol &);
58
59 private:
60 // Reference map is used to map uses of P4 symbols to their definitions
61 const P4::ReferenceMap &refMap;
62 // Map from P4 symbol names to their source info (used during the pass)
63 std::map<cstring, std::map<cstring, Symbol>> symbolByTypeMap;
64
65 // P4 program source root (base for all relative file paths)
66 std::string sourceRoot;
67 // List of P4 symbols' source info (used for export)
68 std::list<Source_Info_Schema_Logger::SymbolDefinition *> symbols;
69
70 // Init sets "sourceRoot"
71 profile_t init_apply(const IR::Node *root) override;
72 // The pass over IR::Paths should be enough to gather all P4 symbols definitions and uses
73 bool preorder(const IR::Path *) override;
74 // ! The other "preorder()"s are needed because of a bug in the inlining of nested blocks that
75 // ! causes source info to point to the nested block apply() instead of the actual symbol use
76 bool preorder(const IR::Argument *) override;
77 bool preorder(const IR::Member *) override;
78 // TODO: Parser states are not covered
79 // End populates "symbols" by calling "addSymbol()"
80 void end_apply() override;
81
82 // Adds a symbol reference source info into "symbolByTypeMap" (called by "preorder()")
83 void addSymbolReference(const IR::Node *, const IR::IDeclaration *);
84
85 friend class SourceInfoLogging;
86};
87
88/*
89 * This inspector serves to log source information collected by CollectSourceInfoLogging.
90 */
91class SourceInfoLogging : public Inspector {
92 private:
93 const CollectSourceInfoLogging &sourceInfo;
94 Source_Info_Schema_Logger logger;
95 std::string manifestPath; // path to source.json relative to manifest.json
96
97#ifdef BF_P4C_LOGGING_SOURCE_INFO_LOGGING_H_DEBUG
98 std::map<std::string, std::map<unsigned, std::set<std::string>>> sourceCoverage;
99#endif
100
101 public:
102 explicit SourceInfoLogging(const std::string &outdir, const std::string &filename,
103 const CollectSourceInfoLogging &sourceInfo)
104 : Inspector(),
105 sourceInfo(sourceInfo),
106 logger((outdir + "/" + filename).c_str(), SOURCE_INFO_SCHEMA_VERSION,
107 sourceInfo.sourceRoot) {
108 manifestPath = filename;
109
110 for (const auto &symbol : sourceInfo.symbols) {
111 logger.append(symbol);
112
113#ifdef BF_P4C_LOGGING_SOURCE_INFO_LOGGING_H_DEBUG
114 sourceCoverage[symbol->get_declaration()->get_file()]
115 [symbol->get_declaration()->get_line()]
116 .insert(std::string(symbol->get_symbol_name()) + "(" +
117 symbol->get_symbol_type() + ")");
118 for (auto *ref : symbol->get_references()) {
119 sourceCoverage[ref->get_file()][ref->get_line()].insert(
120 std::string(symbol->get_symbol_name()) + "(" + symbol->get_symbol_type() + ")");
121 }
122#endif
123 }
124#ifdef BF_P4C_LOGGING_SOURCE_INFO_LOGGING_H_DEBUG
125 for (auto file : sourceCoverage) {
126 for (auto line : file.second) {
127 std::cout << "COVERAGE ";
128 if (!file.first.empty() && file.first.front() != '/')
129 std::cout << sourceInfo.sourceRoot << "/";
130 std::cout << file.first << ":" << line.first;
131 for (auto ref : line.second) {
132 std::cout << " " << ref;
133 }
134 std::cout << std::endl;
135 }
136 }
137#endif
138 }
139
140 bool preorder(const IR::Node *) override { return false; }
141 void end_apply() override;
142};
143
144#endif /* BF_P4C_LOGGING_SOURCE_INFO_LOGGING_H_ */
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition source_info_logging.h:91
CollectSourceInfoLogging(const P4::ReferenceMap &)
Definition source_info_logging.cpp:27
Pass that collects source information.
Definition source_info_logging.h:39
Definition source_info_logging.h:42