• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021, 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 
17 #include "aidl_dumpapi.h"
18 
19 #include <android-base/strings.h>
20 
21 #include "aidl.h"
22 #include "logging.h"
23 #include "os.h"
24 
25 using android::base::EndsWith;
26 using android::base::Join;
27 using android::base::Split;
28 using std::string;
29 using std::unique_ptr;
30 
31 namespace android {
32 namespace aidl {
33 
DumpType(const AidlDefinedType & dt,const string & type)34 void DumpVisitor::DumpType(const AidlDefinedType& dt, const string& type) {
35   DumpComments(dt);
36   DumpAnnotations(dt);
37   out << type << " " << dt.GetName();
38   if (auto generic_type = dt.AsParameterizable(); generic_type && generic_type->IsGeneric()) {
39     out << "<" << Join(generic_type->GetTypeParameters(), ", ") << ">";
40   }
41   out << " {\n";
42   out.Indent();
43   DumpMembers(dt);
44   out.Dedent();
45   out << "}\n";
46 }
47 
DumpMembers(const AidlDefinedType & dt)48 void DumpVisitor::DumpMembers(const AidlDefinedType& dt) {
49   for (const auto& method : dt.GetMethods()) {
50     if (!method->IsUserDefined()) continue;
51     method->DispatchVisit(*this);
52   }
53   for (const auto& field : dt.GetFields()) {
54     field->DispatchVisit(*this);
55   }
56   for (const auto& constdecl : dt.GetConstantDeclarations()) {
57     constdecl->DispatchVisit(*this);
58   }
59 }
60 
61 // Dumps comment only if its has meaningful tags.
DumpComments(const AidlCommentable & c)62 void DumpVisitor::DumpComments(const AidlCommentable& c) {
63   const auto hidden = c.IsHidden();
64   const auto deprecated = FindDeprecated(c.GetComments());
65   if (hidden && !deprecated) {
66     // to pass --checkapi between the current and the tot in the mainline-prod branch
67     // emit @hide in a legacy dump style
68     out << "/* @hide */\n";
69   } else if (hidden || deprecated) {
70     out << "/**\n";
71     if (hidden) {
72       out << " * @hide\n";
73     }
74     if (deprecated) {
75       out << " * @deprecated " << deprecated->note << "\n";
76     }
77     out << " */\n";
78   }
79 }
80 
DumpAnnotations(const AidlAnnotatable & a)81 void DumpVisitor::DumpAnnotations(const AidlAnnotatable& a) {
82   auto annotations = a.ToString();
83   if (!annotations.empty()) {
84     out << annotations << "\n";
85   }
86 }
87 
DumpConstantValue(const AidlTypeSpecifier & type,const AidlConstantValue & c)88 void DumpVisitor::DumpConstantValue(const AidlTypeSpecifier& type, const AidlConstantValue& c) {
89   out << c.ValueString(type, AidlConstantValueDecorator);
90 }
91 
Visit(const AidlInterface & t)92 void DumpVisitor::Visit(const AidlInterface& t) {
93   DumpType(t, "interface");
94 }
Visit(const AidlParcelable & t)95 void DumpVisitor::Visit(const AidlParcelable& t) {
96   DumpType(t, "parcelable");
97 }
Visit(const AidlStructuredParcelable & t)98 void DumpVisitor::Visit(const AidlStructuredParcelable& t) {
99   DumpType(t, "parcelable");
100 }
Visit(const AidlUnionDecl & t)101 void DumpVisitor::Visit(const AidlUnionDecl& t) {
102   DumpType(t, "union");
103 }
Visit(const AidlEnumDeclaration & t)104 void DumpVisitor::Visit(const AidlEnumDeclaration& t) {
105   DumpComments(t);
106   DumpAnnotations(t);
107   out << "enum " << t.GetName() << " {\n";
108   out.Indent();
109   for (const auto& e : t.GetEnumerators()) {
110     out << e->GetName() << " = ";
111     DumpConstantValue(t.GetBackingType(), *e->GetValue());
112     out << ",\n";
113   }
114   out.Dedent();
115   out << "}\n";
116 }
117 
Visit(const AidlMethod & m)118 void DumpVisitor::Visit(const AidlMethod& m) {
119   DumpComments(m);
120   out << m.ToString() << ";\n";
121 }
Visit(const AidlVariableDeclaration & v)122 void DumpVisitor::Visit(const AidlVariableDeclaration& v) {
123   DumpComments(v);
124   Visit(v.GetType());
125   if (v.IsDefaultUserSpecified()) {
126     out << " " << v.GetName() << " = ";
127     DumpConstantValue(v.GetType(), *v.GetDefaultValue());
128     out << ";\n";
129   } else {
130     out << " " << v.GetName() << ";\n";
131   }
132 }
Visit(const AidlConstantDeclaration & c)133 void DumpVisitor::Visit(const AidlConstantDeclaration& c) {
134   DumpComments(c);
135   out << "const ";
136   Visit(c.GetType());
137   out << " " << c.GetName() << " = ";
138   DumpConstantValue(c.GetType(), c.GetValue());
139   out << ";\n";
140 }
141 
Visit(const AidlEnumerator & e)142 void DumpVisitor::Visit(const AidlEnumerator& e) {
143   out << e.GetName() << " = ";
144 
145   e.GetValue()->DispatchVisit(*this);
146   out << ",\n";
147 }
148 
Visit(const AidlTypeSpecifier & t)149 void DumpVisitor::Visit(const AidlTypeSpecifier& t) {
150   out << t.ToString();
151 }
152 
GetApiDumpPathFor(const AidlDefinedType & defined_type,const Options & options)153 static string GetApiDumpPathFor(const AidlDefinedType& defined_type, const Options& options) {
154   string package_as_path = Join(Split(defined_type.GetPackage(), "."), OS_PATH_SEPARATOR);
155   AIDL_FATAL_IF(options.OutputDir().empty() || options.OutputDir().back() != '/', defined_type);
156   return options.OutputDir() + package_as_path + OS_PATH_SEPARATOR + defined_type.GetName() +
157          ".aidl";
158 }
159 
DumpComments(CodeWriter & out,const Comments & comments)160 static void DumpComments(CodeWriter& out, const Comments& comments) {
161   bool needs_newline = false;
162   for (const auto& c : comments) {
163     out << c.body;
164     needs_newline = !EndsWith(c.body, "\n");
165   }
166   if (needs_newline) {
167     out << "\n";
168   }
169 }
170 
dump_api(const Options & options,const IoDelegate & io_delegate)171 bool dump_api(const Options& options, const IoDelegate& io_delegate) {
172   for (const auto& file : options.InputFiles()) {
173     AidlTypenames typenames;
174     if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames, nullptr) ==
175         AidlError::OK) {
176       const auto& doc = typenames.MainDocument();
177 
178       for (const auto& type : doc.DefinedTypes()) {
179         unique_ptr<CodeWriter> writer =
180             io_delegate.GetCodeWriter(GetApiDumpPathFor(*type, options));
181         if (!options.DumpNoLicense()) {
182           // dump doc comments (license) as well for each type
183           DumpComments(*writer, doc.GetComments());
184         }
185         (*writer) << kPreamble;
186         if (!type->GetPackage().empty()) {
187           (*writer) << "package " << type->GetPackage() << ";\n";
188         }
189         DumpVisitor visitor(*writer);
190         type->DispatchVisit(visitor);
191       }
192     } else {
193       return false;
194     }
195   }
196   return true;
197 }
198 
199 }  // namespace aidl
200 }  // namespace android