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", "",
48 "DestroysInstance", "",
50 "Experimental", "", // deprecated
52 "NoArrayLength", "", // deprecated
55 "GenericAccessors", "",
57 "NoAccessorMethod", "",
58 "ConcreteAccessor", "",
60 "ReturnsModifiedPointer", "",
61 "Deprecated", "since", "replacement", "", // deprecated
62 "Version", "since", "replacement", "deprecated", "deprecated_since", "experimental", "experimental_until", "",
63 "Signal", "detailed", "run", "no_recurse", "action", "no_hooks", "",
64 "Description", "nick", "blurb", "",
66 "IntegerType", "rank", "min", "max", "signed", "width", "",
67 "FloatingType", "rank", "decimal", "width", "",
77 "GtkChild", "name", "internal", "",
78 "GtkTemplate", "ui", "",
79 "GtkCallback", "name", "",
83 "DBus", "name", "no_reply", "result", "use_string_marshalling", "value", "signature", "visible", "timeout", "",
85 "GIR", "fullname", "name", ""
90 // mark default valac attrs
92 foreach (unowned
string val
in valac_default_attrs
) {
107 * Mark the attribute or attribute argument as used by the compiler
109 public void mark (string attribute
, string? argument
) {
110 var set = marked
.get (attribute
);
112 set = new HashSet
<string> (str_hash
, str_equal
);
113 marked
.set (attribute
, set);
116 if (argument
!= null) {
122 * Traverse the code tree and warn about unused attributes.
124 * @param context a code context
126 public void check_unused (CodeContext context
) {
127 context
.root
.accept (this
);
130 void check_unused_attr (Symbol sym
) {
131 // optimize by not looking at all the symbols
133 foreach (unowned Attribute attr
in sym
.attributes
) {
134 var set = marked
.get (attr
.name
);
136 Report
.warning (attr
.source_reference
, "attribute `%s' never used".printf (attr
.name
));
138 foreach (var arg
in attr
.args
.get_keys()) {
139 if (!set.contains (arg
)) {
140 Report
.warning (attr
.source_reference
, "argument `%s' never used".printf (arg
));
148 public override void visit_namespace (Namespace ns
) {
149 check_unused_attr (ns
);
150 ns
.accept_children (this
);
153 public override void visit_class (Class cl
) {
154 check_unused_attr (cl
);
155 cl
.accept_children (this
);
158 public override void visit_struct (Struct st
) {
159 check_unused_attr (st
);
160 st
.accept_children (this
);
163 public override void visit_interface (Interface iface
) {
164 check_unused_attr (iface
);
165 iface
.accept_children (this
);
168 public override void visit_enum (Enum en
) {
169 check_unused_attr (en
);
170 en
.accept_children (this
);
173 public override void visit_error_domain (ErrorDomain ed
) {
174 check_unused_attr (ed
);
175 ed
.accept_children (this
);
178 public override void visit_delegate (Delegate cb
) {
179 check_unused_attr (cb
);
180 cb
.accept_children (this
);
183 public override void visit_constant (Constant c
) {
184 check_unused_attr (c
);
187 public override void visit_field (Field f
) {
188 check_unused_attr (f
);
191 public override void visit_method (Method m
) {
192 check_unused_attr (m
);
193 m
.accept_children (this
);
196 public override void visit_creation_method (CreationMethod m
) {
197 check_unused_attr (m
);
198 m
.accept_children (this
);
201 public override void visit_formal_parameter (Parameter p
) {
202 check_unused_attr (p
);
205 public override void visit_property (Property prop
) {
206 check_unused_attr (prop
);
209 public override void visit_signal (Signal sig
) {
210 check_unused_attr (sig
);
211 sig
.accept_children (this
);