Line data Source code
1 : /* valaunusedattr.vala
2 : *
3 : * Copyright (C) 2014-2015 Jürg Billeter
4 : * Copyright (C) 2014-2015 Luca Bruno
5 : *
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.
10 :
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.
15 :
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
19 : *
20 : * Author:
21 : * Luca Bruno <lucabru@src.gnome.org>
22 : */
23 :
24 : using GLib;
25 :
26 : /**
27 : * Code visitor to warn about unused attributes
28 : */
29 3076 : public class Vala.UsedAttr : CodeVisitor {
30 3076 : public Vala.Map<string,Vala.Set<string>> marked = new HashMap<string,Vala.Set<string>> (str_hash, str_equal);
31 :
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", "has_typedef", "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", "type_get_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",
43 : "use_inplace", "feature_test_macro", "default_value_on_error", "async_result_pos", "error_pos", "destroy_notify_cname", "",
44 :
45 : "Immutable", "",
46 : "SingleInstance", "",
47 : "Compact", "opaque", "",
48 : "NoWrapper", "",
49 : "NoThrow", "",
50 : "DestroysInstance", "",
51 : "Flags", "",
52 : "Experimental", "", // deprecated
53 : "NoReturn", "",
54 : "NoArrayLength", "", // deprecated
55 : "Assert", "",
56 : "ErrorBase", "",
57 : "GenericAccessors", "",
58 : "Diagnostics", "",
59 : "NoAccessorMethod", "",
60 : "ConcreteAccessor", "",
61 : "HasEmitter", "",
62 : "ReturnsModifiedPointer", "",
63 : "Deprecated", "since", "replacement", "", // deprecated
64 : "Version", "since", "replacement", "deprecated", "deprecated_since", "experimental", "experimental_until", "",
65 : "Signal", "detailed", "run", "no_recurse", "action", "no_hooks", "",
66 : "Description", "nick", "blurb", "",
67 :
68 : "IntegerType", "rank", "min", "max", "signed", "width", "",
69 : "FloatingType", "rank", "decimal", "width", "",
70 : "BooleanType", "",
71 : "SimpleType", "",
72 : "PointerType", "",
73 :
74 : "Print", "",
75 : "PrintfFormat", "",
76 : "ScanfFormat", "",
77 : "FormatArg", "",
78 :
79 : "Source", "filename", "line", "column", "",
80 :
81 : "GtkChild", "name", "internal", "",
82 : "GtkTemplate", "ui", "",
83 : "GtkCallback", "name", "",
84 :
85 : "ModuleInit", "",
86 : "Profile", "",
87 :
88 : "DBus", "name", "no_reply", "result", "use_string_marshalling", "value", "signature", "visible", "timeout", "",
89 :
90 : "GIR", "fullname", "name", "visible", ""
91 :
92 : };
93 :
94 4614 : public UsedAttr () {
95 : // mark default valac attrs
96 1538 : var curattr = "";
97 295296 : foreach (unowned string val in valac_default_attrs) {
98 293758 : if (val == "") {
99 61520 : curattr = "";
100 : } else {
101 232238 : if (curattr == "") {
102 123040 : curattr = val;
103 61520 : mark (curattr, null);
104 : } else {
105 170718 : mark (curattr, val);
106 : }
107 : }
108 : }
109 : }
110 :
111 : /**
112 : * Mark the attribute or attribute argument as used by the compiler
113 : */
114 464476 : public void mark (string attribute, string? argument) {
115 232238 : var set = marked.get (attribute);
116 232238 : if (set == null) {
117 61520 : set = new HashSet<string> (str_hash, str_equal);
118 61520 : marked.set (attribute, set);
119 : }
120 :
121 232238 : if (argument != null) {
122 170718 : set.add (argument);
123 : }
124 : }
125 :
126 : /**
127 : * Traverse the code tree and warn about unused attributes.
128 : *
129 : * @param context a code context
130 : */
131 958 : public void check_unused (CodeContext context) {
132 958 : context.root.accept (this);
133 : }
134 :
135 11803128 : void check_unused_attr (Symbol sym) {
136 16400526 : foreach (unowned Attribute attr in sym.attributes) {
137 4597398 : var set = marked.get (attr.name);
138 4597398 : if (set == null) {
139 0 : Report.warning (attr.source_reference, "Attribute `%s' never used", attr.name);
140 : } else {
141 15239249 : foreach (var arg in attr.args.get_keys()) {
142 6044453 : if (!set.contains (arg)) {
143 0 : Report.warning (attr.source_reference, "Argument `%s' never used", arg);
144 : }
145 : }
146 : }
147 : }
148 : }
149 :
150 41573 : public override void visit_namespace (Namespace ns) {
151 41573 : check_unused_attr (ns);
152 41573 : ns.accept_children (this);
153 : }
154 :
155 235544 : public override void visit_class (Class cl) {
156 235544 : check_unused_attr (cl);
157 235544 : cl.accept_children (this);
158 : }
159 :
160 90589 : public override void visit_struct (Struct st) {
161 90589 : check_unused_attr (st);
162 90589 : st.accept_children (this);
163 : }
164 :
165 36372 : public override void visit_interface (Interface iface) {
166 36372 : check_unused_attr (iface);
167 36372 : iface.accept_children (this);
168 : }
169 :
170 131891 : public override void visit_enum (Enum en) {
171 131891 : check_unused_attr (en);
172 131891 : en.accept_children (this);
173 : }
174 :
175 19187 : public override void visit_error_domain (ErrorDomain ed) {
176 19187 : check_unused_attr (ed);
177 19187 : ed.accept_children (this);
178 : }
179 :
180 119640 : public override void visit_delegate (Delegate cb) {
181 119640 : check_unused_attr (cb);
182 119640 : cb.accept_children (this);
183 : }
184 :
185 430715 : public override void visit_constant (Constant c) {
186 430715 : check_unused_attr (c);
187 : }
188 :
189 373325 : public override void visit_field (Field f) {
190 373325 : check_unused_attr (f);
191 : }
192 :
193 3840811 : public override void visit_method (Method m) {
194 3840811 : check_unused_attr (m);
195 3840811 : m.accept_children (this);
196 : }
197 :
198 355041 : public override void visit_creation_method (CreationMethod m) {
199 355041 : check_unused_attr (m);
200 355041 : m.accept_children (this);
201 : }
202 :
203 5755457 : public override void visit_formal_parameter (Parameter p) {
204 5755457 : check_unused_attr (p);
205 : }
206 :
207 285881 : public override void visit_property (Property prop) {
208 285881 : check_unused_attr (prop);
209 : }
210 :
211 87102 : public override void visit_signal (Signal sig) {
212 87102 : check_unused_attr (sig);
213 87102 : sig.accept_children (this);
214 : }
215 : }
|