1 /*
2 * Copyright (C) 2020, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "diagnostics.h"
17
18 #include <functional>
19 #include <stack>
20
21 #include "aidl_language.h"
22 #include "logging.h"
23
24 using std::placeholders::_1;
25
26 namespace android {
27 namespace aidl {
28
29 const std::map<std::string, DiagnosticOption> kAllDiagnostics = {
30 #define DIAG(ENUM, NAME, ENABLED) {NAME, DiagnosticOption{DiagnosticID::ENUM, NAME, ENABLED}},
31 #include "diagnostics.inc"
32 #undef DIAG
33 };
34
35 static const std::map<DiagnosticID, std::string> kDiagnosticsNames = {
36 #define DIAG(ENUM, NAME, ENABLED) {DiagnosticID::ENUM, NAME},
37 #include "diagnostics.inc"
38 #undef DIAG
39 };
40
Severity(DiagnosticID id,DiagnosticSeverity severity)41 void DiagnosticMapping::Severity(DiagnosticID id, DiagnosticSeverity severity) {
42 mapping_[id] = severity;
43 }
44
Severity(DiagnosticID id) const45 DiagnosticSeverity DiagnosticMapping::Severity(DiagnosticID id) const {
46 return mapping_.at(id);
47 }
48
to_string(DiagnosticID id)49 std::string to_string(DiagnosticID id) {
50 return kDiagnosticsNames.at(id);
51 }
52
53 class DiagnosticsContext {
54 public:
DiagnosticsContext(DiagnosticMapping mapping)55 DiagnosticsContext(DiagnosticMapping mapping) : mapping_({std::move(mapping)}) {}
Report(const AidlLocation & loc,DiagnosticID id,DiagnosticSeverity force_severity=DiagnosticSeverity::DISABLED)56 AidlErrorLog Report(const AidlLocation& loc, DiagnosticID id,
57 DiagnosticSeverity force_severity = DiagnosticSeverity::DISABLED) {
58 const std::string suffix = " [-W" + to_string(id) + "]";
59 auto severity = std::max(force_severity, mapping_.top().Severity(id));
60 switch (severity) {
61 case DiagnosticSeverity::DISABLED:
62 return AidlErrorLog(AidlErrorLog::NO_OP, loc);
63 case DiagnosticSeverity::WARNING:
64 return AidlErrorLog(AidlErrorLog::WARNING, loc, suffix);
65 case DiagnosticSeverity::ERROR:
66 error_count_++;
67 return AidlErrorLog(AidlErrorLog::ERROR, loc, suffix);
68 }
69 }
ErrorCount() const70 size_t ErrorCount() const { return error_count_; }
Suppress(const AidlAnnotatable & a)71 void Suppress(const AidlAnnotatable& a) {
72 const auto& warnings = a.SuppressWarnings();
73 DiagnosticMapping new_mapping = mapping_.top();
74 for (const auto& w : warnings) {
75 auto it = kAllDiagnostics.find(w);
76 if (it == kAllDiagnostics.end()) {
77 Report(a.GetLocation(), DiagnosticID::unknown_warning, DiagnosticSeverity::ERROR)
78 << "unknown warning: " << w;
79 continue;
80 }
81 new_mapping.Severity(it->second.id, DiagnosticSeverity::DISABLED);
82 }
83 mapping_.push(std::move(new_mapping));
84 }
Restore(const AidlAnnotatable &)85 void Restore(const AidlAnnotatable&) {
86 mapping_.pop();
87 }
88 private:
89 std::stack<DiagnosticMapping> mapping_;
90 size_t error_count_ = {};
91 };
92
93 class DiagnosticsVisitor : public AidlVisitor {
94 public:
DiagnosticsVisitor(DiagnosticsContext & diag)95 DiagnosticsVisitor(DiagnosticsContext& diag) : diag(diag) {}
Check(const AidlDocument & doc)96 void Check(const AidlDocument& doc) {
97 DiagnosticsVisitor* visitor = this;
98 using Fun = std::function<void(const AidlAnnotatable&)>;
99 struct Hook : public AidlVisitor {
100 Fun fun;
101 Hook(Fun fun) : fun(fun) {}
102 void Visit(const AidlInterface& a) override { fun(a); }
103 void Visit(const AidlEnumDeclaration& a) override { fun(a); }
104 void Visit(const AidlStructuredParcelable& a) override { fun(a); }
105 void Visit(const AidlUnionDecl& a) override { fun(a); }
106 void Visit(const AidlParcelable& a) override { fun(a); }
107 void Visit(const AidlMethod& a) override { fun(a.GetType()); }
108 };
109 Hook suppress{std::bind(&DiagnosticsContext::Suppress, &diag, _1)};
110 Hook restore{std::bind(&DiagnosticsContext::Restore, &diag, _1)};
111 std::function<void(const AidlNode&)> top_down = [&top_down, &suppress, &restore,
112 visitor](const AidlNode& a) {
113 a.DispatchVisit(suppress);
114 a.DispatchVisit(*visitor);
115 a.TraverseChildren(top_down);
116 a.DispatchVisit(restore);
117 };
118 top_down(doc);
119 }
120 protected:
121 DiagnosticsContext& diag;
122 };
123
124 struct DiagnoseInterfaceName : DiagnosticsVisitor {
DiagnoseInterfaceNameandroid::aidl::DiagnoseInterfaceName125 DiagnoseInterfaceName(DiagnosticsContext& diag) : DiagnosticsVisitor(diag) {}
Visitandroid::aidl::DiagnoseInterfaceName126 void Visit(const AidlInterface& i) override {
127 if (auto name = i.GetName(); name.size() < 1 || name[0] != 'I') {
128 diag.Report(i.GetLocation(), DiagnosticID::interface_name)
129 << "Interface names should start with I.";
130 }
131 }
132 };
133
134 struct DiagnoseInoutParameter : DiagnosticsVisitor {
DiagnoseInoutParameterandroid::aidl::DiagnoseInoutParameter135 DiagnoseInoutParameter(DiagnosticsContext& diag) : DiagnosticsVisitor(diag) {}
Visitandroid::aidl::DiagnoseInoutParameter136 void Visit(const AidlArgument& a) override {
137 if (a.GetDirection() == AidlArgument::INOUT_DIR) {
138 diag.Report(a.GetLocation(), DiagnosticID::inout_parameter)
139 << a.GetName()
140 << " is 'inout'. Avoid inout parameters. This is somewhat confusing for clients "
141 "because although the parameters are 'in', they look out 'out' parameters.";
142 }
143 }
144 };
145
146 struct DiagnoseConstName : DiagnosticsVisitor {
DiagnoseConstNameandroid::aidl::DiagnoseConstName147 DiagnoseConstName(DiagnosticsContext& diag) : DiagnosticsVisitor(diag) {}
Visitandroid::aidl::DiagnoseConstName148 void Visit(const AidlEnumerator& e) override {
149 if (ToUpper(e.GetName()) != e.GetName()) {
150 diag.Report(e.GetLocation(), DiagnosticID::const_name)
151 << "Enum values should be named in upper case: " << e.GetName();
152 }
153 }
Visitandroid::aidl::DiagnoseConstName154 void Visit(const AidlConstantDeclaration& c) override {
155 if (ToUpper(c.GetName()) != c.GetName()) {
156 diag.Report(c.GetLocation(), DiagnosticID::const_name)
157 << "Constants should be named in upper case: " << c.GetName();
158 }
159 }
ToUpperandroid::aidl::DiagnoseConstName160 static std::string ToUpper(std::string name) {
161 for (auto& c : name) c = std::toupper(c);
162 return name;
163 }
164 };
165
166 struct DiagnoseExplicitDefault : DiagnosticsVisitor {
DiagnoseExplicitDefaultandroid::aidl::DiagnoseExplicitDefault167 DiagnoseExplicitDefault(DiagnosticsContext& diag) : DiagnosticsVisitor(diag) {}
Visitandroid::aidl::DiagnoseExplicitDefault168 void Visit(const AidlStructuredParcelable& p) override {
169 for (const auto& var : p.GetFields()) {
170 CheckExplicitDefault(*var);
171 }
172 }
Visitandroid::aidl::DiagnoseExplicitDefault173 void Visit(const AidlUnionDecl& u) override {
174 AIDL_FATAL_IF(u.GetFields().empty(), u) << "The union '" << u.GetName() << "' has no fields.";
175 const auto& first = u.GetFields()[0];
176 CheckExplicitDefault(*first);
177 }
CheckExplicitDefaultandroid::aidl::DiagnoseExplicitDefault178 void CheckExplicitDefault(const AidlVariableDeclaration& v) {
179 if (v.IsDefaultUserSpecified()) return;
180 if (v.GetType().IsNullable()) return;
181 if (v.GetType().IsArray()) return;
182 const auto defined_type = v.GetType().GetDefinedType();
183 if (defined_type && defined_type->AsEnumDeclaration()) {
184 diag.Report(v.GetLocation(), DiagnosticID::enum_explicit_default)
185 << "The enum field '" << v.GetName() << "' has no explicit value.";
186 return;
187 }
188 }
189 };
190
191 struct DiagnoseMixedOneway : DiagnosticsVisitor {
DiagnoseMixedOnewayandroid::aidl::DiagnoseMixedOneway192 DiagnoseMixedOneway(DiagnosticsContext& diag) : DiagnosticsVisitor(diag) {}
Visitandroid::aidl::DiagnoseMixedOneway193 void Visit(const AidlInterface& i) override {
194 bool has_oneway = false;
195 bool has_twoway = false;
196 for (const auto& m : i.GetMethods()) {
197 if (!m->IsUserDefined()) continue;
198 if (m->IsOneway()) {
199 has_oneway = true;
200 } else {
201 has_twoway = true;
202 }
203 }
204 if (has_oneway && has_twoway) {
205 diag.Report(i.GetLocation(), DiagnosticID::mixed_oneway)
206 << "The interface '" << i.GetName()
207 << "' has both one-way and two-way methods. This makes it hard to reason about threading "
208 "of client code.";
209 }
210 }
211 };
212
213 struct DiagnoseOutArray : DiagnosticsVisitor {
DiagnoseOutArrayandroid::aidl::DiagnoseOutArray214 DiagnoseOutArray(DiagnosticsContext& diag) : DiagnosticsVisitor(diag) {}
Visitandroid::aidl::DiagnoseOutArray215 void Visit(const AidlMethod& m) override {
216 for (const auto& a : m.GetArguments()) {
217 if (a->GetType().IsArray() && a->IsOut()) {
218 diag.Report(m.GetLocation(), DiagnosticID::out_array)
219 << "The method '" << m.GetName() << "' an array output parameter '" << a->GetName()
220 << "'. Instead prefer APIs like '" << a->GetType().Signature() << " " << m.GetName()
221 << "(...).";
222 }
223 }
224 }
225 };
226
227 struct DiagnoseFileDescriptor : DiagnosticsVisitor {
DiagnoseFileDescriptorandroid::aidl::DiagnoseFileDescriptor228 DiagnoseFileDescriptor(DiagnosticsContext& diag) : DiagnosticsVisitor(diag) {}
Visitandroid::aidl::DiagnoseFileDescriptor229 void Visit(const AidlTypeSpecifier& t) override {
230 if (t.GetName() == "FileDescriptor") {
231 diag.Report(t.GetLocation(), DiagnosticID::file_descriptor)
232 << "Please use ParcelFileDescriptor instead of FileDescriptor.";
233 }
234 }
235 };
236
237 struct DiagnoseOutNullable : DiagnosticsVisitor {
DiagnoseOutNullableandroid::aidl::DiagnoseOutNullable238 DiagnoseOutNullable(DiagnosticsContext& diag) : DiagnosticsVisitor(diag) {}
Visitandroid::aidl::DiagnoseOutNullable239 void Visit(const AidlArgument& a) override {
240 if (a.GetType().IsArray()) return;
241 if (a.IsOut() && a.GetType().IsNullable()) {
242 diag.Report(a.GetLocation(), DiagnosticID::out_nullable)
243 << "'" << a.GetName() << "' is an " << a.GetDirectionSpecifier()
244 << " parameter and also nullable. Some backends don't support setting null value to out "
245 "parameters. Please use it as return value or drop @nullable to avoid potential "
246 "errors.";
247 }
248 }
249 };
250
Diagnose(const AidlDocument & doc,const DiagnosticMapping & mapping)251 bool Diagnose(const AidlDocument& doc, const DiagnosticMapping& mapping) {
252 DiagnosticsContext diag(mapping);
253
254 DiagnoseInterfaceName{diag}.Check(doc);
255 DiagnoseInoutParameter{diag}.Check(doc);
256 DiagnoseConstName{diag}.Check(doc);
257 DiagnoseExplicitDefault{diag}.Check(doc);
258 DiagnoseMixedOneway{diag}.Check(doc);
259 DiagnoseOutArray{diag}.Check(doc);
260 DiagnoseFileDescriptor{diag}.Check(doc);
261 DiagnoseOutNullable{diag}.Check(doc);
262
263 return diag.ErrorCount() == 0;
264 }
265 } // namespace aidl
266 } // namespace android
267