blob: 85ad9baf0fa90326669f3c164167419facb35b06 [file] [log] [blame]
Josh Gaobf8a2852016-05-27 11:59:09 -07001/*
2 * Copyright 2016 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 "DeclarationDatabase.h"
18
Josh Gaobfb6bae2016-07-15 17:25:21 -070019#include <err.h>
20
Josh Gaobf8a2852016-05-27 11:59:09 -070021#include <iostream>
22#include <map>
Josh Gaobfb6bae2016-07-15 17:25:21 -070023#include <mutex>
Josh Gaobf8a2852016-05-27 11:59:09 -070024#include <set>
Josh Gaobfb6bae2016-07-15 17:25:21 -070025#include <sstream>
Josh Gaobf8a2852016-05-27 11:59:09 -070026#include <string>
Josh Gaobfb6bae2016-07-15 17:25:21 -070027#include <utility>
Josh Gaobf8a2852016-05-27 11:59:09 -070028
29#include <clang/AST/AST.h>
30#include <clang/AST/Attr.h>
31#include <clang/AST/Mangle.h>
32#include <clang/AST/RecursiveASTVisitor.h>
33#include <clang/Frontend/ASTUnit.h>
34#include <llvm/Support/raw_ostream.h>
35
36using namespace clang;
37
38class Visitor : public RecursiveASTVisitor<Visitor> {
39 HeaderDatabase& database;
Josh Gaobfb6bae2016-07-15 17:25:21 -070040 CompilationType type;
Josh Gaobf8a2852016-05-27 11:59:09 -070041 SourceManager& src_manager;
42 std::unique_ptr<MangleContext> mangler;
43
44 public:
Josh Gaobfb6bae2016-07-15 17:25:21 -070045 Visitor(HeaderDatabase& database, CompilationType type, ASTContext& ctx)
46 : database(database), type(type), src_manager(ctx.getSourceManager()) {
Josh Gaobf8a2852016-05-27 11:59:09 -070047 mangler.reset(ItaniumMangleContext::create(ctx, ctx.getDiagnostics()));
48 }
49
50 std::string getDeclName(NamedDecl* decl) {
51 if (auto var_decl = dyn_cast<VarDecl>(decl)) {
52 if (!var_decl->isFileVarDecl()) {
53 return "<local var>";
54 }
55 }
56
57 if (mangler->shouldMangleDeclName(decl)) {
58 std::string mangled;
59 llvm::raw_string_ostream ss(mangled);
60 mangler->mangleName(decl, ss);
61 return mangled;
62 }
63
64 auto identifier = decl->getIdentifier();
65 if (!identifier) {
66 return "<error>";
67 }
68 return identifier->getName();
69 }
70
71 bool VisitDecl(Decl* decl) {
72 // Skip declarations inside of functions (function arguments, variable declarations inside of
73 // inline functions, etc).
74 if (decl->getParentFunctionOrMethod()) {
75 return true;
76 }
77
78 auto named_decl = dyn_cast<NamedDecl>(decl);
79 if (!named_decl) {
80 return true;
81 }
82
83 DeclarationType declaration_type;
84 std::string declaration_name = getDeclName(named_decl);
85 bool is_extern = named_decl->getFormalLinkage() == ExternalLinkage;
86 bool is_definition = false;
87
88 if (auto function_decl = dyn_cast<FunctionDecl>(decl)) {
89 declaration_type = DeclarationType::function;
90 is_definition = function_decl->isThisDeclarationADefinition();
91 } else if (auto var_decl = dyn_cast<VarDecl>(decl)) {
92 if (!var_decl->isFileVarDecl()) {
93 return true;
94 }
95
96 declaration_type = DeclarationType::variable;
97 switch (var_decl->isThisDeclarationADefinition()) {
98 case VarDecl::DeclarationOnly:
99 is_definition = false;
100 break;
101
102 case VarDecl::Definition:
103 is_definition = true;
104 break;
105
106 case VarDecl::TentativeDefinition:
107 // Forbid tentative definitions in headers.
108 fprintf(stderr, "ERROR: declaration '%s' is a tentative definition\n",
109 declaration_name.c_str());
110 decl->dump();
111 abort();
112 }
113 } else {
114 // We only care about function and variable declarations.
115 return true;
116 }
117
118 if (decl->hasAttr<UnavailableAttr>()) {
119 // Skip declarations that exist only for compile-time diagnostics.
120 return true;
121 }
122
Josh Gaobfb6bae2016-07-15 17:25:21 -0700123 auto start_loc = src_manager.getPresumedLoc(decl->getLocStart());
124 auto end_loc = src_manager.getPresumedLoc(decl->getLocEnd());
125
126 Location location = {
127 .filename = start_loc.getFilename(),
128 .start = {
129 .line = start_loc.getLine(),
130 .column = start_loc.getColumn(),
131 },
132 .end = {
133 .line = end_loc.getLine(),
134 .column = end_loc.getColumn(),
135 }
136 };
137
Josh Gaobf8a2852016-05-27 11:59:09 -0700138 DeclarationAvailability availability;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700139
140 // Find and parse __ANDROID_AVAILABILITY_DUMP__ annotations.
141 for (const AnnotateAttr* attr : decl->specific_attrs<AnnotateAttr>()) {
142 llvm::StringRef annotation = attr->getAnnotation();
143 if (annotation == "introduced_in_future") {
144 // Tag the compiled-for arch, since this can vary across archs.
145 availability.arch_availability[type.arch].future = true;
146 } else {
147 llvm::SmallVector<llvm::StringRef, 2> fragments;
148 annotation.split(fragments, "=");
149 if (fragments.size() != 2) {
150 continue;
151 }
152
153 auto& global_availability = availability.global_availability;
154 auto& arch_availability = availability.arch_availability;
155 std::map<std::string, std::vector<int*>> prefix_map = {
156 { "introduced_in", { &global_availability.introduced } },
157 { "deprecated_in", { &global_availability.deprecated } },
158 { "obsoleted_in", { &global_availability.obsoleted } },
159 { "introduced_in_arm", { &arch_availability[Arch::arm].introduced } },
160 { "introduced_in_mips", { &arch_availability[Arch::mips].introduced } },
161 { "introduced_in_x86", { &arch_availability[Arch::x86].introduced } },
162 { "introduced_in_32",
163 { &arch_availability[Arch::arm].introduced,
164 &arch_availability[Arch::mips].introduced,
165 &arch_availability[Arch::x86].introduced } },
166 { "introduced_in_64",
167 { &arch_availability[Arch::arm64].introduced,
168 &arch_availability[Arch::mips64].introduced,
169 &arch_availability[Arch::x86_64].introduced } },
170 };
171
172 auto it = prefix_map.find(fragments[0]);
173 if (it == prefix_map.end()) {
174 continue;
175 }
176 int value;
177 if (fragments[1].getAsInteger(10, value)) {
178 errx(1, "invalid __ANDROID_AVAILABILITY_DUMP__ annotation: '%s'",
179 annotation.str().c_str());
180 }
181
182 for (int* ptr : it->second) {
183 *ptr = value;
184 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700185 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700186 }
187
188 auto symbol_it = database.symbols.find(declaration_name);
189 if (symbol_it == database.symbols.end()) {
190 Symbol symbol = {.name = declaration_name };
191 bool dummy;
192 std::tie(symbol_it, dummy) = database.symbols.insert({ declaration_name, symbol });
Josh Gaobf8a2852016-05-27 11:59:09 -0700193 }
194
195 // Find or insert an entry for the declaration.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700196 auto declaration_it = symbol_it->second.declarations.find(location);
197 if (declaration_it == symbol_it->second.declarations.end()) {
198 Declaration declaration;
199 declaration.location = location;
200 declaration.is_extern = is_extern;
201 declaration.is_definition = is_definition;
202 declaration.availability.insert(std::make_pair(type, availability));
203 symbol_it->second.declarations.insert(std::make_pair(location, declaration));
204 } else {
205 if (declaration_it->second.is_extern != is_extern ||
206 declaration_it->second.is_definition != is_definition) {
207 errx(1, "varying declaration of '%s' at %s:%u:%u", declaration_name.c_str(),
208 location.filename.c_str(), location.start.line, location.start.column);
Josh Gaobf8a2852016-05-27 11:59:09 -0700209 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700210 declaration_it->second.availability.insert(std::make_pair(type, availability));
Josh Gaobf8a2852016-05-27 11:59:09 -0700211 }
212
213 return true;
214 }
215};
216
Josh Gaobfb6bae2016-07-15 17:25:21 -0700217bool DeclarationAvailability::merge(const DeclarationAvailability& other) {
218#define check_avail(expr) error |= (!this->expr.empty() && this->expr != other.expr);
219 bool error = false;
220
221 if (!other.global_availability.empty()) {
222 check_avail(global_availability);
223 this->global_availability = other.global_availability;
224 }
225
226 for (Arch arch : supported_archs) {
227 if (!other.arch_availability[arch].empty()) {
228 check_avail(arch_availability[arch]);
229 this->arch_availability[arch] = other.arch_availability[arch];
230 }
231 }
232#undef check_avail
233
234 return !error;
235}
236
237bool Declaration::calculateAvailability(DeclarationAvailability* output) const {
238 DeclarationAvailability avail;
239 for (const auto& it : this->availability) {
240 if (!avail.merge(it.second)) {
241 return false;
242 }
243 }
244 *output = avail;
245 return true;
246}
247
248bool Symbol::calculateAvailability(DeclarationAvailability* output) const {
249 DeclarationAvailability avail;
250 for (const auto& it : this->declarations) {
251 // Don't merge availability for inline functions (because they shouldn't have any).
252 if (it.second.is_definition) {
253 continue;
254 }
255
256 DeclarationAvailability decl_availability;
257 if (!it.second.calculateAvailability(&decl_availability)) {
258 return false;
259 abort();
260 }
261
262 if (!avail.merge(decl_availability)) {
263 return false;
264 }
265 }
266 *output = avail;
267 return true;
268}
269
270bool Symbol::hasDeclaration(const CompilationType& type) const {
271 for (const auto& decl_it : this->declarations) {
272 for (const auto& compilation_it : decl_it.second.availability) {
273 if (compilation_it.first == type) {
274 return true;
275 }
276 }
277 }
278 return false;
279}
280
281void HeaderDatabase::parseAST(CompilationType type, ASTUnit* ast) {
282 std::unique_lock<std::mutex> lock(this->mutex);
Josh Gaobf8a2852016-05-27 11:59:09 -0700283 ASTContext& ctx = ast->getASTContext();
Josh Gaobfb6bae2016-07-15 17:25:21 -0700284 Visitor visitor(*this, type, ctx);
Josh Gaobf8a2852016-05-27 11:59:09 -0700285 visitor.TraverseDecl(ctx.getTranslationUnitDecl());
286}
Josh Gaobfb6bae2016-07-15 17:25:21 -0700287
288std::string to_string(const CompilationType& type) {
289 std::stringstream ss;
290 ss << to_string(type.arch) << "-" << type.api_level;
291 return ss.str();
292}
293
294std::string to_string(const AvailabilityValues& av) {
295 std::stringstream ss;
296
297 if (av.future) {
298 ss << "future, ";
299 }
300
301 if (av.introduced != 0) {
302 ss << "introduced = " << av.introduced << ", ";
303 }
304
305 if (av.deprecated != 0) {
306 ss << "deprecated = " << av.deprecated << ", ";
307 }
308
309 if (av.obsoleted != 0) {
310 ss << "obsoleted = " << av.obsoleted << ", ";
311 }
312
313 std::string result = ss.str();
314 if (!result.empty()) {
315 result = result.substr(0, result.length() - 2);
316 }
317 return result;
318}
319
320std::string to_string(const DeclarationType& type) {
321 switch (type) {
322 case DeclarationType::function:
323 return "function";
324 case DeclarationType::variable:
325 return "variable";
326 case DeclarationType::inconsistent:
327 return "inconsistent";
328 }
329 abort();
330}
331
332std::string to_string(const DeclarationAvailability& decl_av) {
333 std::stringstream ss;
334 if (!decl_av.global_availability.empty()) {
335 ss << to_string(decl_av.global_availability) << ", ";
336 }
337
338 for (auto it : decl_av.arch_availability) {
339 if (!it.second.empty()) {
340 ss << to_string(it.first) << ": " << to_string(it.second) << ", ";
341 }
342 }
343
344 std::string result = ss.str();
345 if (result.size() == 0) {
346 return "no availability";
347 }
348
349 return result.substr(0, result.length() - 2);
350}
351
352std::string to_string(const Location& loc) {
353 std::stringstream ss;
354 ss << loc.filename << ":" << loc.start.line << ":" << loc.start.column;
355 return ss.str();
356}