Skip to content
Merged
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
7 changes: 7 additions & 0 deletions src/compiler/compile_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ inline auto requires_evaluation(const Context &context,
return requires_evaluation(context, entry.pointer);
}

inline auto annotations_enabled(const Context &context,
const std::string_view keyword) -> bool {
return context.mode == Mode::Exhaustive &&
(!context.tweaks.annotations.has_value() ||
context.tweaks.annotations.value().contains(keyword));
}

// TODO: Elevate to Core and test

inline auto
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/default_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ auto sourcemeta::blaze::default_schema_compiler(
return {};
}

if (context.mode == Mode::FastValidation ||
if (!annotations_enabled(context, dynamic_context.keyword) ||
schema_context.is_property_name) {
return {};
}
Expand Down
12 changes: 6 additions & 6 deletions src/compiler/default_compiler_2019_09.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ auto compiler_2019_09_applicator_contains_with_options(
sourcemeta::core::empty_weak_pointer,
sourcemeta::core::empty_weak_pointer)};

if (annotate) {
if (annotate && annotations_enabled(context, dynamic_context.keyword)) {
children.push_back(
make(sourcemeta::blaze::InstructionIndex::AnnotationBasenameToParent,
context, schema_context, relative_dynamic_context(), ValueNone{}));
Expand Down Expand Up @@ -282,7 +282,7 @@ auto compiler_2019_09_applicator_unevaluateditems(
sourcemeta::core::empty_weak_pointer,
sourcemeta::core::empty_weak_pointer)};

if (context.mode == Mode::Exhaustive) {
if (annotations_enabled(context, dynamic_context.keyword)) {
children.push_back(
make(sourcemeta::blaze::InstructionIndex::AnnotationToParent, context,
schema_context, relative_dynamic_context(),
Expand Down Expand Up @@ -322,7 +322,7 @@ auto compiler_2019_09_applicator_unevaluatedproperties(
sourcemeta::core::empty_weak_pointer,
sourcemeta::core::empty_weak_pointer)};

if (context.mode == Mode::Exhaustive) {
if (annotations_enabled(context, dynamic_context.keyword)) {
children.push_back(
make(sourcemeta::blaze::InstructionIndex::AnnotationBasenameToParent,
context, schema_context, relative_dynamic_context(), ValueNone{}));
Expand Down Expand Up @@ -451,7 +451,7 @@ auto compiler_2019_09_content_contentencoding(
const Context &context, const SchemaContext &schema_context,
const DynamicContext &dynamic_context, const Instructions &)
-> Instructions {
if (context.mode == Mode::FastValidation) {
if (!annotations_enabled(context, dynamic_context.keyword)) {
return {};
}

Expand All @@ -470,7 +470,7 @@ auto compiler_2019_09_content_contentmediatype(
const Context &context, const SchemaContext &schema_context,
const DynamicContext &dynamic_context, const Instructions &)
-> Instructions {
if (context.mode == Mode::FastValidation) {
if (!annotations_enabled(context, dynamic_context.keyword)) {
return {};
}

Expand All @@ -489,7 +489,7 @@ auto compiler_2019_09_content_contentschema(
const Context &context, const SchemaContext &schema_context,
const DynamicContext &dynamic_context, const Instructions &)
-> Instructions {
if (context.mode == Mode::FastValidation) {
if (!annotations_enabled(context, dynamic_context.keyword)) {
return {};
}

Expand Down
38 changes: 25 additions & 13 deletions src/compiler/default_compiler_draft3.h
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,9 @@ auto compiler_draft3_applicator_properties_with_options(
return {};
}

const bool emit_annotation{
annotate && annotations_enabled(context, dynamic_context.keyword)};

if (properties_as_loop(context, schema_context,
schema_context.schema.at(dynamic_context.keyword))) {
ValueNamedIndexes indexes;
Expand All @@ -562,7 +565,7 @@ auto compiler_draft3_applicator_properties_with_options(
schema_context, relative_dynamic_context(), ValuePointer{name}));
}

if (annotate) {
if (emit_annotation) {
substeps.push_back(
make(sourcemeta::blaze::InstructionIndex::AnnotationEmit, context,
schema_context, relative_dynamic_context(),
Expand Down Expand Up @@ -715,7 +718,7 @@ auto compiler_draft3_applicator_properties_with_options(
bool fusion_possible{attempt_object_fusion};

for (auto &&[name, substeps] : properties) {
if (annotate) {
if (emit_annotation) {
substeps.push_back(
make(sourcemeta::blaze::InstructionIndex::AnnotationEmit, context,
schema_context, effective_dynamic_context,
Expand Down Expand Up @@ -1016,7 +1019,7 @@ auto compiler_draft3_applicator_patternproperties_with_options(
auto substeps{compile(context, schema_context, relative_dynamic_context(),
sourcemeta::blaze::make_weak_pointer(pattern))};

if (annotate) {
if (annotate && annotations_enabled(context, dynamic_context.keyword)) {
substeps.push_back(make(
sourcemeta::blaze::InstructionIndex::AnnotationBasenameToParent,
context, schema_context, relative_dynamic_context(), ValueNone{}));
Expand Down Expand Up @@ -1141,7 +1144,7 @@ auto compiler_draft3_applicator_additionalproperties_with_options(
sourcemeta::core::empty_weak_pointer,
sourcemeta::core::empty_weak_pointer)};

if (annotate) {
if (annotate && annotations_enabled(context, dynamic_context.keyword)) {
children.push_back(
make(sourcemeta::blaze::InstructionIndex::AnnotationBasenameToParent,
context, schema_context, relative_dynamic_context(), ValueNone{}));
Expand Down Expand Up @@ -1316,6 +1319,9 @@ auto compiler_draft3_applicator_items_array(
return {};
}

const bool emit_annotation{
annotate && annotations_enabled(context, dynamic_context.keyword)};

// Precompile subschemas
std::vector<Instructions> subschemas;
subschemas.reserve(items_size);
Expand All @@ -1336,7 +1342,7 @@ auto compiler_draft3_applicator_items_array(
}
}

if (annotate) {
if (emit_annotation) {
subchildren.push_back(
make(sourcemeta::blaze::InstructionIndex::AnnotationEmit, context,
schema_context, relative_dynamic_context(),
Expand All @@ -1355,7 +1361,7 @@ auto compiler_draft3_applicator_items_array(
}
}

if (annotate) {
if (emit_annotation) {
tail.push_back(make(sourcemeta::blaze::InstructionIndex::AnnotationEmit,
context, schema_context, relative_dynamic_context(),
sourcemeta::core::JSON{children.size() - 1}));
Expand Down Expand Up @@ -1433,8 +1439,11 @@ auto compiler_draft3_applicator_items_with_options(
return {};
}

const bool emit_annotation{
annotate && annotations_enabled(context, dynamic_context.keyword)};

if (is_schema(schema_context.schema.at(dynamic_context.keyword))) {
if (annotate || track_evaluation) {
if (emit_annotation || track_evaluation) {
Instructions subchildren{compile(context, schema_context,
relative_dynamic_context(),
sourcemeta::core::empty_weak_pointer,
Expand All @@ -1448,13 +1457,13 @@ auto compiler_draft3_applicator_items_with_options(
ValueNone{}, std::move(subchildren)));
}

if (!annotate && !track_evaluation) {
if (!emit_annotation && !track_evaluation) {
return children;
}

Instructions tail;

if (annotate) {
if (emit_annotation) {
tail.push_back(make(sourcemeta::blaze::InstructionIndex::AnnotationEmit,
context, schema_context, relative_dynamic_context(),
sourcemeta::core::JSON{true}));
Expand Down Expand Up @@ -1562,6 +1571,9 @@ auto compiler_draft3_applicator_additionalitems_from_cursor(
return {};
}

const bool emit_annotation{
annotate && annotations_enabled(context, dynamic_context.keyword)};

Instructions subchildren{compile(context, schema_context,
relative_dynamic_context(),
sourcemeta::core::empty_weak_pointer,
Expand All @@ -1586,13 +1598,13 @@ auto compiler_draft3_applicator_additionalitems_from_cursor(
}

// Avoid one extra wrapper instruction if possible
if (!annotate && !track_evaluation) {
if (!emit_annotation && !track_evaluation) {
return children;
}

Instructions tail;

if (annotate) {
if (emit_annotation) {
tail.push_back(make(sourcemeta::blaze::InstructionIndex::AnnotationEmit,
context, schema_context, relative_dynamic_context(),
sourcemeta::core::JSON{true}));
Expand Down Expand Up @@ -2577,7 +2589,7 @@ auto compiler_draft3_validation_format(const Context &context,
make(sourcemeta::blaze::InstructionIndex::AssertionStringType, context,
schema_context, dynamic_context, type)};

if (context.mode == Mode::Exhaustive) {
if (annotations_enabled(context, dynamic_context.keyword)) {
Instructions annotation_children{
make(sourcemeta::blaze::InstructionIndex::AnnotationEmit, context,
schema_context, dynamic_context,
Expand All @@ -2593,7 +2605,7 @@ auto compiler_draft3_validation_format(const Context &context,
}

if (is_2019_09_format || is_2020_12_format_annotation) {
if (context.mode == Mode::FastValidation) {
if (!annotations_enabled(context, dynamic_context.keyword)) {
return {};
}

Expand Down
5 changes: 5 additions & 0 deletions src/compiler/include/sourcemeta/blaze/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <string_view> // std::string_view
#include <tuple> // std::tuple
#include <unordered_map> // std::unordered_map
#include <unordered_set> // std::unordered_set
#include <vector> // std::vector

/// @defgroup compiler Compiler
Expand Down Expand Up @@ -98,6 +99,10 @@ struct Tweaks {
std::size_t target_inline_threshold{50};
/// When set, force `format` to be compiled as an assertion
bool format_assertion{false};
/// Select which keywords emit annotations in exhaustive mode. When not set,
/// every annotation keyword is emitted
std::optional<std::unordered_set<sourcemeta::core::JSON::StringView>>

@augmentcode augmentcode Bot Jun 22, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/compiler/include/sourcemeta/blaze/compiler.h:104: Tweaks::annotations stores JSON::StringView (std::string_view) inside an unordered_set, which can dangle if callers build the set from temporary/short-lived strings (UB when annotations_enabled() checks contains). It might be worth clearly documenting the required lifetime expectations for these views (or otherwise ensuring the keys are owned).

Severity: medium

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

Comment thread
jviotti marked this conversation as resolved.
annotations{};
};

/// @ingroup compiler
Expand Down
Loading
Loading