Release 0.41.92
[vala-gnome.git] / vala / valausedattr.vala
blobdab15a864a08ad554d24e7f6dea7e68667ae8db4
1 /* valaunusedattr.vala
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
20 * Author:
21 * Luca Bruno <lucabru@src.gnome.org>
24 using GLib;
26 /**
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", "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",
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", "",
45 "Immutable", "",
46 "Compact", "",
47 "NoWrapper", "",
48 "NoThrow", "",
49 "DestroysInstance", "",
50 "Flags", "",
51 "Experimental", "", // deprecated
52 "NoReturn", "",
53 "NoArrayLength", "", // deprecated
54 "Assert", "",
55 "ErrorBase", "",
56 "GenericAccessors", "",
57 "Diagnostics", "",
58 "NoAccessorMethod", "",
59 "ConcreteAccessor", "",
60 "HasEmitter", "",
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", "",
69 "BooleanType", "",
70 "SimpleType", "",
71 "PointerType", "",
73 "Print", "",
74 "PrintfFormat", "",
75 "ScanfFormat", "",
76 "FormatArg", "",
78 "GtkChild", "name", "internal", "",
79 "GtkTemplate", "ui", "",
80 "GtkCallback", "name", "",
82 "ModuleInit", "",
84 "DBus", "name", "no_reply", "result", "use_string_marshalling", "value", "signature", "visible", "timeout", "",
86 "GIR", "fullname", "name", "visible", ""
90 public UsedAttr () {
91 // mark default valac attrs
92 var curattr = "";
93 foreach (unowned string val in valac_default_attrs) {
94 if (val == "") {
95 curattr = "";
96 } else {
97 if (curattr == "") {
98 curattr = val;
99 mark (curattr, null);
100 } else {
101 mark (curattr, val);
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);
112 if (set == null) {
113 set = new HashSet<string> (str_hash, str_equal);
114 marked.set (attribute, set);
117 if (argument != null) {
118 set.add (argument);
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
133 if (sym.used) {
134 foreach (unowned Attribute attr in sym.attributes) {
135 var set = marked.get (attr.name);
136 if (set == null) {
137 Report.warning (attr.source_reference, "attribute `%s' never used".printf (attr.name));
138 } else {
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);