3 * Copyright (C) 2014-2015 Jürg Billeter
4 * Copyright (C) 2014-2015 Luca Bruno
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Luca Bruno <lucabru@src.gnome.org>
27 * Code visitor to warn about unused attributes
29 public class Vala
.UsedAttr
: CodeVisitor
{
30 public Vala
.Map
<string,Vala
.Set
<string>> marked
= new HashMap
<string,Vala
.Set
<string>> (str_hash
, str_equal
);
32 const string[] valac_default_attrs
= {
33 "CCode", "type_signature", "default_value", "set_value_function", "type_id", "cprefix", "cheader_filename",
34 "marshaller_type_name", "get_value_function", "cname", "destroy_function", "lvalue_access",
35 "has_type_id", "instance_pos", "const_cname", "take_value_function", "copy_function", "free_function",
36 "param_spec_function", "has_target", "type_cname", "ref_function", "ref_function_void", "unref_function", "type",
37 "has_construct_function", "returns_floating_reference", "gir_namespace", "gir_version", "construct_function",
38 "lower_case_cprefix", "simple_generics", "sentinel", "scope", "has_destroy_function", "ordering", "type_check_function",
39 "has_copy_function", "lower_case_csuffix", "ref_sink_function", "dup_function", "finish_function", "generic_type_pos",
40 "array_length_type", "array_length", "array_length_cname", "array_length_cexpr", "array_null_terminated",
41 "vfunc_name", "finish_vfunc_name", "finish_name", "free_function_address_of", "pos", "delegate_target", "delegate_target_cname",
42 "array_length_pos", "delegate_target_pos", "destroy_notify_pos", "ctype", "has_new_function", "notify", "finish_instance",
49 "DestroysInstance", "",
51 "Experimental", "", // deprecated
53 "NoArrayLength", "", // deprecated
56 "GenericAccessors", "",
58 "NoAccessorMethod", "",
59 "ConcreteAccessor", "",
61 "ReturnsModifiedPointer", "",
62 "Deprecated", "since", "replacement", "", // deprecated
63 "Version", "since", "replacement", "deprecated", "deprecated_since", "experimental", "experimental_until", "",
64 "Signal", "detailed", "run", "no_recurse", "action", "no_hooks", "",
65 "Description", "nick", "blurb", "",
67 "IntegerType", "rank", "min", "max", "signed", "width", "",
68 "FloatingType", "rank", "decimal", "width", "",
78 "GtkChild", "name", "internal", "",
79 "GtkTemplate", "ui", "",
80 "GtkCallback", "name", "",
84 "DBus", "name", "no_reply", "result", "use_string_marshalling", "value", "signature", "visible", "timeout", "",
86 "GIR", "fullname", "name", ""
91 // mark default valac attrs
93 foreach (unowned
string val
in valac_default_attrs
) {
108 * Mark the attribute or attribute argument as used by the compiler
110 public void mark (string attribute
, string? argument
) {
111 var set = marked
.get (attribute
);
113 set = new HashSet
<string> (str_hash
, str_equal
);
114 marked
.set (attribute
, set);
117 if (argument
!= null) {
123 * Traverse the code tree and warn about unused attributes.
125 * @param context a code context
127 public void check_unused (CodeContext context
) {
128 context
.root
.accept (this
);
131 void check_unused_attr (Symbol sym
) {
132 // optimize by not looking at all the symbols
134 foreach (unowned Attribute attr
in sym
.attributes
) {
135 var set = marked
.get (attr
.name
);
137 Report
.warning (attr
.source_reference
, "attribute `%s' never used".printf (attr
.name
));
139 foreach (var arg
in attr
.args
.get_keys()) {
140 if (!set.contains (arg
)) {
141 Report
.warning (attr
.source_reference
, "argument `%s' never used".printf (arg
));
149 public override void visit_namespace (Namespace ns
) {
150 check_unused_attr (ns
);
151 ns
.accept_children (this
);
154 public override void visit_class (Class cl
) {
155 check_unused_attr (cl
);
156 cl
.accept_children (this
);
159 public override void visit_struct (Struct st
) {
160 check_unused_attr (st
);
161 st
.accept_children (this
);
164 public override void visit_interface (Interface iface
) {
165 check_unused_attr (iface
);
166 iface
.accept_children (this
);
169 public override void visit_enum (Enum en
) {
170 check_unused_attr (en
);
171 en
.accept_children (this
);
174 public override void visit_error_domain (ErrorDomain ed
) {
175 check_unused_attr (ed
);
176 ed
.accept_children (this
);
179 public override void visit_delegate (Delegate cb
) {
180 check_unused_attr (cb
);
181 cb
.accept_children (this
);
184 public override void visit_constant (Constant c
) {
185 check_unused_attr (c
);
188 public override void visit_field (Field f
) {
189 check_unused_attr (f
);
192 public override void visit_method (Method m
) {
193 check_unused_attr (m
);
194 m
.accept_children (this
);
197 public override void visit_creation_method (CreationMethod m
) {
198 check_unused_attr (m
);
199 m
.accept_children (this
);
202 public override void visit_formal_parameter (Parameter p
) {
203 check_unused_attr (p
);
206 public override void visit_property (Property prop
) {
207 check_unused_attr (prop
);
210 public override void visit_signal (Signal sig
) {
211 check_unused_attr (sig
);
212 sig
.accept_children (this
);