19#ifndef BF_GTEST_HELPERS_H_
20#define BF_GTEST_HELPERS_H_
26#include <initializer_list>
32#include <boost/operators.hpp>
34#include "lib/compile_context.h"
39struct Visitor_Context;
63std::string trimWhiteSpace(std::string str);
66std::string trimAnnotations(
const std::string &str);
69enum Flag { Raw = 0, TrimWhiteSpace = 1, TrimAnnotations = 2 };
71inline Flag operator|(Flag a, Flag b) {
72 return static_cast<Flag
>(
static_cast<int>(a) |
static_cast<int>(b));
93std::string convert_to_regex(
const std::string &expr);
101constexpr size_t failed = std::string::npos;
102size_t match_basic(
const std::string &expr,
const std::string &str,
size_t pos = 0,
103 size_t n_pos = std::string::npos);
109typedef std::vector<std::string> CheckList;
112struct Result : boost::equality_comparable<Result> {
122 for (std::size_t n = 0; n < match.size(); ++n)
matches.push_back(match[n]);
124 friend bool operator==(
const Result &l,
const Result &r) {
125 return l.success == r.success && l.pos == r.pos && l.count == r.count;
134Result match(
const CheckList &exprs,
const std::string &str,
size_t pos = 0,
135 size_t n_pos = std::string::npos, Flag flag = Raw);
138inline const std::string BraceEnds() {
return "{}"; }
139inline const std::string ParenEnds() {
return "()"; }
140inline const std::string SquareEnds() {
return "[]"; }
141inline const std::string AngleEnds() {
return "<>"; }
144size_t find_next_end(
const std::string &str,
size_t pos,
const std::string &ends);
147std::pair<size_t, size_t> find_next_block(
const std::string &str,
size_t pos,
148 const std::string &ends);
151std::string get_ends(
char opening);
158 const IR::P4Program *program =
nullptr;
159 const IR::BFN::Pipe *pipe =
nullptr;
163 Match::Flag flag = Match::TrimWhiteSpace;
167 std::string phv_log_file = {};
170 const IR::P4Program *get_program() {
return program; }
173 static std::string empty_state() {
return "state start {transition accept;}"; }
174 static std::string empty_appy() {
return "apply {}"; }
182 static std::string min_control_shell_marker() {
return "control testingress" +
any_to_brace(); }
192 static std::string tofino_shell_parser_marker() {
195 static std::string tofino_shell_control_marker() {
198 static std::string tofino_shell_deparser_marker() {
237 const std::initializer_list<std::string> &insertion = {},
238 const std::string &blockMarker =
"",
240 const std::initializer_list<std::string> &options = {});
252 Hdr header = Hdr::None) {
253 std::initializer_list<std::string> insert = {defines, block};
258 void flags(Match::Flag f) { flag = f; }
260 void set_phv_log_file(std::string path) { phv_log_file = path; }
283 bool CreateBlockThreadLocalInstances() {
304 return match(CodeBlock::P4Code, exprs);
325 friend std::ostream &operator<<(std::ostream &out,
const TestCode &tc) {
330 std::string extract_p4()
const;
331 std::string extract_asm(
CodeBlock blk_type)
const;
Definition tofino/bf-p4c/backend.h:46
Definition mau/asm_output.h:85
Definition bf_gtest_helpers.h:155
TestCode(Hdr header, std::string code, const std::initializer_list< std::string > &insertion={}, const std::string &blockMarker="", const std::initializer_list< std::string > &options={})
Definition bf_gtest_helpers.cpp:426
bool CreateBackend()
Runs all the necessary passes to create a backend ready for testing.
Definition bf_gtest_helpers.h:279
CodeBlock
Definition bf_gtest_helpers.h:289
static std::string tofino_shell()
Definition bf_gtest_helpers.cpp:513
static std::string any_to_brace()
Useful strings.
Definition bf_gtest_helpers.h:172
static std::string min_control_shell()
Definition bf_gtest_helpers.cpp:503
std::string get_field_container(const std::string &field, const std::string &str, int idx=0) const
Definition bf_gtest_helpers.cpp:707
Hdr
Definition bf_gtest_helpers.h:210
bool apply_pass(Visitor &pass, const Visitor_Context *context=nullptr)
Runs the pass over either the pipe (if created) else the program.
Definition bf_gtest_helpers.cpp:549
Match::Result match(const Match::CheckList &exprs) const
Calls Match::match() on the P4 code block specified by blockMarker.
Definition bf_gtest_helpers.h:303
std::string extract_code(CodeBlock blk_type, size_t pos=0) const
Definition bf_gtest_helpers.cpp:698
Pass
Runs a preconstructed pass over the pipe or program.
Definition bf_gtest_helpers.h:268
@ FullMidend
Remove the pipe and run over the program.
@ FullBackend
Run over the pipe, creates asm CodeBlocks.
@ ConverterToBackend
Run over the program and create the pipe.
@ FullFrontend
Remove the pipe and run over the program.
@ ThreadLocalInstances
Run over the pipe.
static TestCode TestControlBlock(const std::string &defines, const std::string &block, Hdr header=Hdr::None)
Definition bf_gtest_helpers.h:251
Match::Result match(CodeBlock blk_type, const Match::CheckList &exprs) const
Calls Match::match() on the code block specified by CodeBlock.
Definition bf_gtest_helpers.cpp:629
void flags(Match::Flag f)
Sets the flags to be used by other member functions.
Definition bf_gtest_helpers.h:258
std::string extract_code(size_t pos=0) const
Definition bf_gtest_helpers.h:312
Definition phv_logging.h:115
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21
The namespace encapsulating IR node classes.
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition compile_context.h:77
'Result' is used to report how a match has proceeded.
Definition bf_gtest_helpers.h:112
size_t count
Definition bf_gtest_helpers.h:116
std::vector< std::string > matches
Match strings.
Definition bf_gtest_helpers.h:118
size_t pos
Definition bf_gtest_helpers.h:114
bool success
true if the 'CheckList' was completed.
Definition bf_gtest_helpers.h:113
Definition common/field_defuse.cpp:590
Result contains either an error or all instructions for an action.
Definition action_constraint_solver.h:140