Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions compiler/back_end/cpp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,17 @@ emboss_cc_test(
],
)

emboss_cc_test(
name = "many_conditionals_benchmark",
srcs = [
"testcode/many_conditionals_benchmark.cc",
],
deps = [
"//testdata:many_conditionals_emboss",
"@com_google_googletest//:gtest_main",
],
)

# New golden test infrastructure
py_library(
name = "one_golden_test_lib",
Expand Down Expand Up @@ -536,6 +547,7 @@ cpp_golden_test(
cpp_golden_test(
name = "no_enum_traits_golden_test",
emb_file = "//testdata:no_enum_traits.emb",
extra_args = ["--no-cc-enum-traits"],
golden_file = "//testdata/golden_cpp:no_enum_traits.emb.h",
)

Expand Down Expand Up @@ -598,3 +610,9 @@ cpp_golden_test(
emb_file = "//testdata:imported_genfiles.emb",
golden_file = "//testdata/golden_cpp:imported_genfiles.emb.h",
)

cpp_golden_test(
name = "many_conditionals_golden_test",
emb_file = "//testdata:many_conditionals.emb",
golden_file = "//testdata/golden_cpp:many_conditionals.emb.h",
)
5 changes: 3 additions & 2 deletions compiler/back_end/cpp/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ def emboss_cc_test(name, copts = None, no_w_sign_compare = False, **kwargs):
**kwargs
)

def cpp_golden_test(name, emb_file, golden_file, import_dirs = []):
def cpp_golden_test(name, emb_file, golden_file, import_dirs = [], extra_args = []):
"""Defines a C++ golden file test.

Args:
name: The name of the test.
emb_file: The .emb file to test.
golden_file: The golden .h file.
import_dirs: A list of import directories.
extra_args: A list of extra arguments to pass to the compiler.
"""
py_test(
name = name,
Expand All @@ -66,7 +67,7 @@ def cpp_golden_test(name, emb_file, golden_file, import_dirs = []):
"$(location :emboss_codegen_cpp)",
"$(location %s)" % emb_file,
"$(location %s)" % golden_file,
] + ["--import-dir=" + d for d in import_dirs],
] + ["--import-dir=" + d for d in import_dirs] + extra_args,
data = [
"//compiler/front_end:emboss_front_end",
":emboss_codegen_cpp",
Expand Down
53 changes: 48 additions & 5 deletions compiler/back_end/cpp/generated_code_templates
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class Generic${name}View final {
bool Ok() const {
if (!IsComplete()) return false;
${parameter_ok_checks}
${ok_subexpressions}
${field_ok_checks}
${requires_check}
return true;
Expand Down Expand Up @@ -391,11 +392,53 @@ ${write_fields}
// ** ok_method_test ** ////////////////////////////////////////////////////////
// If we don't have enough information to determine whether ${field} is
// present in the structure, then structure.Ok() should be false.
if (!has_${field}.Known()) return false;
// If ${field} is present, but not Ok(), then structure.Ok() should be
// false. If ${field} is not present, it does not matter whether it is
// Ok().
if (has_${field}.ValueOrDefault() && !${field}.Ok()) return false;
{
const auto emboss_reserved_local_field_present = ${existence_condition};
if (!emboss_reserved_local_field_present.Known()) return false;
// If ${field} is present, but not Ok(), then structure.Ok() should be
// false. If ${field} is not present, it does not matter whether it is
// Ok().
if (emboss_reserved_local_field_present.ValueOrDefault() && !${field}.Ok()) return false;
}


// ** ok_method_switch_block ** ////////////////////////////////////////////////
// Check fields that depend on a common discriminant using a switch.
{
${inner_scope_definitions}
const auto emboss_reserved_switch_discrim = ${discriminant};
if (!emboss_reserved_switch_discrim.Known()) return false;
switch (emboss_reserved_switch_discrim.ValueOrDefault()) {
${switch_cases}
}
}


// ** ok_method_switch_case ** /////////////////////////////////////////////////
case ${case_value}:
if (!${field}().Ok()) return false;
break;


// ** ok_method_if_block ** ////////////////////////////////////////////////////
// Check fields that share a common condition.
{
${inner_scope_definitions}
const auto emboss_reserved_cond = ${condition};
if (!emboss_reserved_cond.Known()) return false;
if (emboss_reserved_cond.ValueOrDefault()) {
${field_checks}
}
}


// ** ok_method_field_check ** /////////////////////////////////////////////////
if (!${field}().Ok()) return false;


// ** ok_method_unconditional_check ** /////////////////////////////////////////
// Field is always present (condition is statically true).
if (!${field}().Ok()) return false;


// ** equals_method_test ** ////////////////////////////////////////////////////
Expand Down
Loading
Loading