Commit a61dd80e authored by Jürg Billeter's avatar Jürg Billeter Committed by Rico Tzschichholz

Support [FormatArg] attribute for parameters

This attribute specifies that the method takes and returns a printf
or scanf format string without modifying the order or types of expected
arguments, e.g., to translate the format string. This allows the
compiler to check the printf/scanf arguments.
parent 7a5ff26e
......@@ -1047,6 +1047,15 @@ public class Vala.Method : Subroutine, Callable {
get_captured_variables ((Collection<LocalVariable>) collection);
}
}
public int get_format_arg_index () {
for (int i = 0; i < parameters.size; i++) {
if (parameters[i].format_arg) {
return i;
}
}
return -1;
}
}
// vim:sw=8 noet
......@@ -434,7 +434,7 @@ public class Vala.MethodCall : Expression {
StringLiteral format_literal = null;
if (last_arg != null) {
// use last argument as format string
format_literal = last_arg as StringLiteral;
format_literal = StringLiteral.get_format_literal (last_arg);
if (format_literal == null && args.size == params.size - 1) {
// insert "%s" to avoid issues with embedded %
format_literal = new StringLiteral ("\"%s\"");
......@@ -454,7 +454,7 @@ public class Vala.MethodCall : Expression {
// use instance as format string for string.printf (...)
var ma = call as MemberAccess;
if (ma != null) {
format_literal = ma.inner as StringLiteral;
format_literal = StringLiteral.get_format_literal (ma.inner);
}
}
if (format_literal != null) {
......@@ -722,4 +722,16 @@ public class Vala.MethodCall : Expression {
arg.get_used_variables (collection);
}
}
public StringLiteral? get_format_literal () {
var mtype = this.call.value_type as MethodType;
if (mtype != null) {
int format_arg = mtype.method_symbol.get_format_arg_index ();
if (format_arg >= 0 && format_arg < argument_list.size) {
return StringLiteral.get_format_literal (argument_list[format_arg]);
}
}
return null;
}
}
......@@ -388,7 +388,7 @@ public class Vala.ObjectCreationExpression : Expression {
StringLiteral format_literal = null;
if (last_arg != null) {
// use last argument as format string
format_literal = last_arg as StringLiteral;
format_literal = StringLiteral.get_format_literal (last_arg);
if (format_literal == null && args.size == m.get_parameters ().size - 1) {
// insert "%s" to avoid issues with embedded %
format_literal = new StringLiteral ("\"%s\"");
......@@ -457,7 +457,7 @@ public class Vala.ObjectCreationExpression : Expression {
Report.error (source_reference, "Invalid type for argument 1");
}
var format_literal = ex as StringLiteral;
var format_literal = StringLiteral.get_format_literal (ex);
if (format_literal != null) {
var format = format_literal.eval ();
if (!context.analyzer.check_print_format (format, arg_it, source_reference)) {
......
......@@ -44,6 +44,12 @@ public class Vala.Parameter : Variable {
public bool captured { get; set; }
public bool format_arg {
get {
return get_attribute ("FormatArg") != null;
}
}
/**
* The base parameter of this parameter relative to the base method.
*/
......
......@@ -96,4 +96,18 @@ public class Vala.StringLiteral : Literal {
codegen.visit_expression (this);
}
public static StringLiteral? get_format_literal (Expression expr) {
var format_literal = expr as StringLiteral;
if (format_literal != null) {
return format_literal;
}
var call = expr as MethodCall;
if (call != null) {
return call.get_format_literal ();
}
return null;
}
}
......@@ -68,6 +68,7 @@ public class Vala.UsedAttr : CodeVisitor {
"SimpleType", "",
"PrintfFormat", "",
"ScanfFormat", "",
"FormatArg", "",
"GtkChild", "name", "internal", "",
"GtkTemplate", "ui", "",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment