P4C
The P4 Compiler
Loading...
Searching...
No Matches
pragma/pragma.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_COMMON_PRAGMA_PRAGMA_H_
20#define BACKENDS_TOFINO_BF_P4C_COMMON_PRAGMA_PRAGMA_H_
21#include <algorithm>
22#include <cstring>
23#include <iomanip>
24#include <iostream>
25#include <set>
26#include <string>
27
28namespace BFN {
29
39class Pragma {
40 public:
41 Pragma(const char *name, const char *description, const char *help)
42 : _name(name), _description(description), _help(help) {}
43
44 static void registerPragma(const Pragma *p, bool internal = false) {
45 if (!internal)
46 _supported_pragmas.insert(p);
47 else
48 _internal_pragmas.insert(p);
49 }
50
52 static std::ostream &printHelp(std::ostream &o) {
53 // format a string to width
54 auto format = [](const char *str, size_t width /*, size_t indent = 0 */) {
55 std::string *res = new std::string("");
56 do {
57 std::string chunk = std::string(str).substr(0, width);
58 auto newline = chunk.find('\n');
59 if (newline != std::string::npos) {
60 *res += chunk.substr(0, newline + 1);
61 str += newline + 1;
62 } else if (strlen(str) < width) {
63 *res += str;
64 break; // no need to go further
65 } else {
66 // split at the last space before the desired width
67 auto lastspace = chunk.rfind(' ');
68 if (lastspace != std::string::npos) {
69 *res += chunk.substr(0, lastspace) + "\n";
70 str += lastspace + 1;
71 } else {
72 // no space? unlikely, so then we just print the string
73 *res += chunk + "\n";
74 str += chunk.size();
75 }
76 }
77 } while (strlen(str) > 0);
78 return res;
79 };
80
81 auto formatPragma = [format](const Pragma *p, std::ostream &o) {
82 o << std::endl << std::string(80, '-') << std::endl;
83 auto d = format(p->_description, 80 - (strlen(p->_name) + 3));
84 auto h = format(p->_help, 80);
85 o << p->_name << ": " << d->c_str() << std::endl
86 << std::endl
87 << h->c_str() << std::endl;
88 delete d;
89 delete h;
90 };
91
92 o << "Supported pragmas:" << std::endl;
93 for (auto &p : _supported_pragmas) formatPragma(p, o);
94#if BAREFOOT_INTERNAL
95 o << std::endl
96 << std::string(80, '*') << std::endl
97 << "Barefoot internal pragmas" << std::endl;
98 for (auto &p : _internal_pragmas) formatPragma(p, o);
99#endif
100 return o;
101 }
102
103 private:
104 const char *_name;
105 const char *_description;
106 const char *_help;
107
108 struct PragmaCmp {
109 bool operator()(const Pragma *a, const Pragma *b) const {
110 return std::string(a->_name) < std::string(b->_name);
111 }
112 };
113
115 static std::set<const Pragma *, PragmaCmp> _supported_pragmas;
117 static std::set<const Pragma *, PragmaCmp> _internal_pragmas;
118};
119
120} // end namespace BFN
121
122#endif // BACKENDS_TOFINO_BF_P4C_COMMON_PRAGMA_PRAGMA_H_
virtual const char * help() const =0
detailed description that will be printed as help
static bool registerPragma(const Pragma *p, bool internal=false)
Definition pragma.h:92
virtual const char * description() const =0
short description that will be printed as help
virtual const char * name() const
the pragma name – should be overriden
Definition pragma.h:35
static std::ostream & printHelp(std::ostream &o)
print the currently supported pragmas
Definition pragma/pragma.h:52
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21
Definition pretty_print.h:25