Skip to content

Commit dc80c52

Browse files
authored
common : fix jinja warnings with clang 21 (#22313)
Signed-off-by: Adrien Gallouët <angt@huggingface.co>
1 parent e583f3b commit dc80c52

3 files changed

Lines changed: 45 additions & 32 deletions

File tree

common/jinja/runtime.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,16 @@ struct statement {
106106
size_t pos; // position in source, for debugging
107107
virtual ~statement() = default;
108108
virtual std::string type() const { return "Statement"; }
109+
109110
// execute_impl must be overridden by derived classes
110-
virtual value execute_impl(context &) { throw std::runtime_error("cannot exec " + type()); }
111+
virtual value execute_impl(context &) { throw_exec_error(); }
111112
// execute is the public method to execute a statement with error handling
112113
value execute(context &);
114+
115+
private:
116+
[[noreturn]] void throw_exec_error() const {
117+
throw std::runtime_error("cannot exec " + type());
118+
}
113119
};
114120

115121
// Type Checking Utilities
@@ -143,7 +149,7 @@ struct program : public statement {
143149
program() = default;
144150
explicit program(statements && body) : body(std::move(body)) {}
145151
std::string type() const override { return "Program"; }
146-
value execute_impl(context &) override {
152+
[[noreturn]] value execute_impl(context &) override {
147153
throw std::runtime_error("Cannot execute program directly, use jinja::runtime instead");
148154
}
149155
};
@@ -195,7 +201,7 @@ struct break_statement : public statement {
195201
}
196202
};
197203

198-
value execute_impl(context &) override {
204+
[[noreturn]] value execute_impl(context &) override {
199205
throw break_statement::signal();
200206
}
201207
};
@@ -209,7 +215,7 @@ struct continue_statement : public statement {
209215
}
210216
};
211217

212-
value execute_impl(context &) override {
218+
[[noreturn]] value execute_impl(context &) override {
213219
throw continue_statement::signal();
214220
}
215221
};
@@ -509,7 +515,7 @@ struct slice_expression : public expression {
509515
chk_type<expression>(this->step_expr);
510516
}
511517
std::string type() const override { return "SliceExpression"; }
512-
value execute_impl(context &) override {
518+
[[noreturn]] value execute_impl(context &) override {
513519
throw std::runtime_error("must be handled by MemberExpression");
514520
}
515521
};

common/jinja/value.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,10 @@ static bool string_endswith(const std::string & str, const std::string & suffix)
590590
return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
591591
}
592592

593+
[[noreturn]] static value string_join_not_implemented(const func_args &) {
594+
throw not_implemented_exception("String join builtin not implemented");
595+
}
596+
593597
const func_builtins & value_string_t::get_builtins() const {
594598
static const func_builtins builtins = {
595599
{"default", default_value},
@@ -851,9 +855,7 @@ const func_builtins & value_string_t::get_builtins() const {
851855
res->val_str.mark_input_based_on(val_input->as_string());
852856
return res;
853857
}},
854-
{"join", [](const func_args &) -> value {
855-
throw not_implemented_exception("String join builtin not implemented");
856-
}},
858+
{"join", string_join_not_implemented},
857859
};
858860
return builtins;
859861
}
@@ -884,6 +886,9 @@ const func_builtins & value_bool_t::get_builtins() const {
884886
return builtins;
885887
}
886888

889+
[[noreturn]] static value array_unique_not_implemented(const func_args &) {
890+
throw not_implemented_exception("Array unique builtin not implemented");
891+
}
887892

888893
const func_builtins & value_array_t::get_builtins() const {
889894
static const func_builtins builtins = {
@@ -1084,13 +1089,14 @@ const func_builtins & value_array_t::get_builtins() const {
10841089
std::reverse(arr.begin(), arr.end());
10851090
return is_val<value_tuple>(val) ? mk_val<value_tuple>(std::move(arr)) : mk_val<value_array>(std::move(arr));
10861091
}},
1087-
{"unique", [](const func_args &) -> value {
1088-
throw not_implemented_exception("Array unique builtin not implemented");
1089-
}},
1092+
{"unique", array_unique_not_implemented},
10901093
};
10911094
return builtins;
10921095
}
10931096

1097+
[[noreturn]] static value object_join_not_implemented(const func_args &) {
1098+
throw not_implemented_exception("object join not implemented");
1099+
}
10941100

10951101
const func_builtins & value_object_t::get_builtins() const {
10961102
if (!has_builtins) {
@@ -1183,9 +1189,7 @@ const func_builtins & value_object_t::get_builtins() const {
11831189
});
11841190
return result;
11851191
}},
1186-
{"join", [](const func_args &) -> value {
1187-
throw not_implemented_exception("object join not implemented");
1188-
}},
1192+
{"join", object_join_not_implemented},
11891193
};
11901194
return builtins;
11911195
}

common/jinja/value.h

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,25 @@ struct value_t {
129129
// Note: only for debugging and error reporting purposes
130130
virtual std::string type() const { return ""; }
131131

132-
virtual int64_t as_int() const { throw std::runtime_error(type() + " is not an int value"); }
133-
virtual double as_float() const { throw std::runtime_error(type() + " is not a float value"); }
134-
virtual string as_string() const { throw std::runtime_error(type() + " is not a string value"); }
135-
virtual bool as_bool() const { throw std::runtime_error(type() + " is not a bool value"); }
136-
virtual const std::vector<value> & as_array() const { throw std::runtime_error(type() + " is not an array value"); }
137-
virtual const std::vector<std::pair<value, value>> & as_ordered_object() const { throw std::runtime_error(type() + " is not an object value"); }
138-
virtual value invoke(const func_args &) const { throw std::runtime_error(type() + " is not a function value"); }
132+
virtual int64_t as_int() const { throw_type_error("is not an int value"); }
133+
virtual double as_float() const { throw_type_error("is not a float value"); }
134+
virtual string as_string() const { throw_type_error("is not a string value"); }
135+
virtual bool as_bool() const { throw_type_error("is not a bool value"); }
136+
virtual const std::vector<value> & as_array() const { throw_type_error("is not an array value"); }
137+
virtual const std::vector<std::pair<value, value>> & as_ordered_object() const { throw_type_error("is not an object value"); }
138+
virtual value invoke(const func_args &) const { throw_type_error("is not a function value"); }
139139
virtual bool is_none() const { return false; }
140140
virtual bool is_undefined() const { return false; }
141-
virtual const func_builtins & get_builtins() const {
142-
throw std::runtime_error("No builtins available for type " + type());
143-
}
141+
virtual const func_builtins & get_builtins() const { throw_type_error("has no builtins"); }
144142

145-
virtual bool has_key(const value &) { throw std::runtime_error(type() + " is not an object value"); }
146-
virtual void insert(const value & /* key */, const value & /* val */) { throw std::runtime_error(type() + " is not an object value"); }
147-
virtual value & at(const value & /* key */, value & /* default_val */) { throw std::runtime_error(type() + " is not an object value"); }
148-
virtual value & at(const value & /* key */) { throw std::runtime_error(type() + " is not an object value"); }
149-
virtual value & at(const std::string & /* key */, value & /* default_val */) { throw std::runtime_error(type() + " is not an object value"); }
150-
virtual value & at(const std::string & /* key */) { throw std::runtime_error(type() + " is not an object value"); }
151-
virtual value & at(int64_t /* idx */, value & /* default_val */) { throw std::runtime_error(type() + " is not an array value"); }
152-
virtual value & at(int64_t /* idx */) { throw std::runtime_error(type() + " is not an array value"); }
143+
virtual bool has_key(const value &) { throw_type_error("is not an object value"); }
144+
virtual void insert(const value & /* key */, const value & /* val */) { throw_type_error("is not an object value"); }
145+
virtual value & at(const value & /* key */, value & /* default_val */) { throw_type_error("is not an object value"); }
146+
virtual value & at(const value & /* key */) { throw_type_error("is not an object value"); }
147+
virtual value & at(const std::string & /* key */, value & /* default_val */) { throw_type_error("is not an object value"); }
148+
virtual value & at(const std::string & /* key */) { throw_type_error("is not an object value"); }
149+
virtual value & at(int64_t /* idx */, value & /* default_val */) { throw_type_error("is not an array value"); }
150+
virtual value & at(int64_t /* idx */) { throw_type_error("is not an array value"); }
153151

154152
virtual bool is_numeric() const { return false; }
155153
virtual bool is_hashable() const { return false; }
@@ -163,6 +161,11 @@ struct value_t {
163161
// Note: only for debugging purposes
164162
virtual std::string as_repr() const { return as_string().str(); }
165163

164+
private:
165+
[[noreturn]] void throw_type_error(const char* expected) const {
166+
throw std::runtime_error(type() + " " + expected);
167+
}
168+
166169
protected:
167170
virtual bool equivalent(const value_t &) const = 0;
168171
virtual bool nonequal(const value_t & other) const { return !equivalent(other); }

0 commit comments

Comments
 (0)