Skip to content

Commit c3c2401

Browse files
authored
Get rid of implicit char* => cstring conversions (#4694)
* Switch to explicit cstring literals * Get rid of implicit char* => cstring conversion. The amount of changes effectively shows how often this was misused. * Unbreak bazel * Allocate string cache data from non-GC arena * Address review comments * Address review feedback
1 parent f2e9be9 commit c3c2401

File tree

324 files changed

+3998
-3657
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

324 files changed

+3998
-3657
lines changed

backends/bmv2/common/JsonObjects.cpp

+113-113
Large diffs are not rendered by default.

backends/bmv2/common/action.cpp

+19-17
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,20 @@ limitations under the License.
2020

2121
namespace BMV2 {
2222

23+
using namespace P4::literals;
24+
2325
cstring ActionConverter::jsonAssignment(const IR::Type *type) {
24-
if (type->is<IR::Type_Varbits>()) return "assign_VL";
25-
if (type->is<IR::Type_HeaderUnion>()) return "assign_union";
26-
if (type->is<IR::Type_Header>() || type->is<IR::Type_Struct>()) return "assign_header";
26+
if (type->is<IR::Type_Varbits>()) return "assign_VL"_cs;
27+
if (type->is<IR::Type_HeaderUnion>()) return "assign_union"_cs;
28+
if (type->is<IR::Type_Header>() || type->is<IR::Type_Struct>()) return "assign_header"_cs;
2729
if (auto ts = type->to<IR::Type_Stack>()) {
2830
auto et = ts->elementType;
2931
if (et->is<IR::Type_HeaderUnion>())
30-
return "assign_union_stack";
32+
return "assign_union_stack"_cs;
3133
else
32-
return "assign_header_stack";
34+
return "assign_header_stack"_cs;
3335
}
34-
return "assign";
36+
return "assign"_cs;
3537
}
3638

3739
void ActionConverter::convertActionBody(const IR::Vector<IR::StatOrDecl> *body,
@@ -98,9 +100,9 @@ void ActionConverter::convertActionBody(const IR::Vector<IR::StatOrDecl> *body,
98100
} else if (s->is<IR::ReturnStatement>()) {
99101
break;
100102
} else if (s->is<IR::ExitStatement>()) {
101-
auto primitive = mkPrimitive("exit", result);
103+
auto primitive = mkPrimitive("exit"_cs, result);
102104
(void)mkParameters(primitive);
103-
primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
105+
primitive->emplace_non_null("source_info"_cs, s->sourceInfoJsonObj());
104106
break;
105107
} else if (s->is<IR::AssignmentStatement>()) {
106108
const IR::Expression *l, *r;
@@ -111,7 +113,7 @@ void ActionConverter::convertActionBody(const IR::Vector<IR::StatOrDecl> *body,
111113
cstring operation = jsonAssignment(type);
112114
auto primitive = mkPrimitive(operation, result);
113115
auto parameters = mkParameters(primitive);
114-
primitive->emplace_non_null("source_info", assign->sourceInfoJsonObj());
116+
primitive->emplace_non_null("source_info"_cs, assign->sourceInfoJsonObj());
115117
bool convertBool = type->is<IR::Type_Boolean>();
116118
Util::IJson *left;
117119
if (ctxt->conv->isArrayIndexRuntime(l)) {
@@ -141,25 +143,25 @@ void ActionConverter::convertActionBody(const IR::Vector<IR::StatOrDecl> *body,
141143
parameters->append(obj);
142144

143145
if (builtin->name == IR::Type_Header::setValid) {
144-
prim = "add_header";
146+
prim = "add_header"_cs;
145147
} else if (builtin->name == IR::Type_Header::setInvalid) {
146-
prim = "remove_header";
148+
prim = "remove_header"_cs;
147149
} else if (builtin->name == IR::Type_Stack::push_front) {
148150
BUG_CHECK(mc->arguments->size() == 1, "Expected 1 argument for %1%", mc);
149151
auto arg = ctxt->conv->convert(mc->arguments->at(0)->expression);
150-
prim = "push";
152+
prim = "push"_cs;
151153
parameters->append(arg);
152154
} else if (builtin->name == IR::Type_Stack::pop_front) {
153155
BUG_CHECK(mc->arguments->size() == 1, "Expected 1 argument for %1%", mc);
154156
auto arg = ctxt->conv->convert(mc->arguments->at(0)->expression);
155-
prim = "pop";
157+
prim = "pop"_cs;
156158
parameters->append(arg);
157159
} else {
158160
BUG("%1%: Unexpected built-in method", s);
159161
}
160162
auto primitive = mkPrimitive(prim, result);
161-
primitive->emplace("parameters", parameters);
162-
primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
163+
primitive->emplace("parameters"_cs, parameters);
164+
primitive->emplace_non_null("source_info"_cs, s->sourceInfoJsonObj());
163165
continue;
164166
} else if (mi->is<P4::ExternMethod>()) {
165167
auto em = mi->to<P4::ExternMethod>();
@@ -190,14 +192,14 @@ void ActionConverter::convertActionParams(const IR::ParameterList *parameters,
190192
warn(ErrorType::WARN_UNUSED, "Unused action parameter %1%", p);
191193

192194
auto param = new Util::JsonObject();
193-
param->emplace("name", p->externalName());
195+
param->emplace("name"_cs, p->externalName());
194196
auto type = ctxt->typeMap->getType(p, true);
195197
// TODO: added IR::Type_Enum here to support PSA_MeterColor_t
196198
// should re-consider how to support action parameters that is neither bit<> nor int<>
197199
if (!(type->is<IR::Type_Bits>() || type->is<IR::Type_Enum>()))
198200
::error(ErrorType::ERR_INVALID,
199201
"%1%: action parameters must be bit<> or int<> on this target", p);
200-
param->emplace("bitwidth", type->width_bits());
202+
param->emplace("bitwidth"_cs, type->width_bits());
201203
params->append(param);
202204
}
203205
}

backends/bmv2/common/annotations.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@ limitations under the License.
2222

2323
namespace BMV2 {
2424

25+
using namespace P4::literals;
26+
2527
/// Parses BMV2-specific annotations.
2628
class ParseAnnotations : public P4::ParseAnnotations {
2729
public:
2830
ParseAnnotations()
29-
: P4::ParseAnnotations("BMV2", false,
31+
: P4::ParseAnnotations("BMV2"_cs, false,
3032
{
31-
PARSE_EMPTY("metadata"),
32-
PARSE_EXPRESSION_LIST("field_list"),
33-
PARSE("alias", StringLiteral),
34-
PARSE("priority", Constant),
35-
PARSE_EXPRESSION_LIST("p4runtime_translation_mappings"),
36-
PARSE_P4RUNTIME_TRANSLATION("p4runtime_translation"),
33+
PARSE_EMPTY("metadata"_cs),
34+
PARSE_EXPRESSION_LIST("field_list"_cs),
35+
PARSE("alias"_cs, StringLiteral),
36+
PARSE("priority"_cs, Constant),
37+
PARSE_EXPRESSION_LIST("p4runtime_translation_mappings"_cs),
38+
PARSE_P4RUNTIME_TRANSLATION("p4runtime_translation"_cs),
3739
}) {}
3840
};
3941

backends/bmv2/common/backend.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ limitations under the License.
2424
#include "frontends/p4/coreLibrary.h"
2525
#include "helpers.h"
2626
#include "ir/ir.h"
27+
#include "lib/cstring.h"
2728
#include "lib/error.h"
2829
#include "lib/exceptions.h"
2930
#include "lib/gc.h"
@@ -153,7 +154,7 @@ class RenameUserMetadata : public Transform {
153154
IR::IndexedVector<IR::StructField> fields;
154155
for (auto f : type->fields) {
155156
auto anno = f->getAnnotation(IR::Annotation::nameAnnotation);
156-
cstring suffix = "";
157+
cstring suffix = cstring::empty;
157158
if (anno != nullptr) suffix = anno->getName();
158159
if (suffix.startsWith(".")) {
159160
// We can't change the name of this field.
@@ -163,9 +164,9 @@ class RenameUserMetadata : public Transform {
163164
}
164165

165166
if (!suffix.isNullOrEmpty())
166-
suffix = cstring(".") + suffix;
167+
suffix = "."_cs + suffix;
167168
else
168-
suffix = cstring(".") + f->name;
169+
suffix = "."_cs + f->name;
169170
cstring newName = namePrefix + suffix;
170171
auto stringLit = new IR::StringLiteral(newName);
171172
LOG2("Renaming " << f << " to " << newName);

0 commit comments

Comments
 (0)