blob: 8e8d84f5c0f668ae60c821a7b95e527bddeed81f [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;
Josh Gao16057882016-08-02 14:54:09 -0700199 declaration.name = declaration_name;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700200 declaration.location = location;
201 declaration.is_extern = is_extern;
202 declaration.is_definition = is_definition;
203 declaration.availability.insert(std::make_pair(type, availability));
204 symbol_it->second.declarations.insert(std::make_pair(location, declaration));
205 } else {
206 if (declaration_it->second.is_extern != is_extern ||
207 declaration_it->second.is_definition != is_definition) {
208 errx(1, "varying declaration of '%s' at %s:%u:%u", declaration_name.c_str(),
209 location.filename.c_str(), location.start.line, location.start.column);
Josh Gaobf8a2852016-05-27 11:59:09 -0700210 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700211 declaration_it->second.availability.insert(std::make_pair(type, availability));
Josh Gaobf8a2852016-05-27 11:59:09 -0700212 }
213
214 return true;
215 }
216};
217
Josh Gaobfb6bae2016-07-15 17:25:21 -0700218bool DeclarationAvailability::merge(const DeclarationAvailability& other) {
219#define check_avail(expr) error |= (!this->expr.empty() && this->expr != other.expr);
220 bool error = false;
221
222 if (!other.global_availability.empty()) {
223 check_avail(global_availability);
224 this->global_availability = other.global_availability;
225 }
226
227 for (Arch arch : supported_archs) {
228 if (!other.arch_availability[arch].empty()) {
229 check_avail(arch_availability[arch]);
230 this->arch_availability[arch] = other.arch_availability[arch];
231 }
232 }
233#undef check_avail
234
235 return !error;
236}
237
238bool Declaration::calculateAvailability(DeclarationAvailability* output) const {
239 DeclarationAvailability avail;
240 for (const auto& it : this->availability) {
241 if (!avail.merge(it.second)) {
242 return false;
243 }
244 }
245 *output = avail;
246 return true;
247}
248
249bool Symbol::calculateAvailability(DeclarationAvailability* output) const {
250 DeclarationAvailability avail;
251 for (const auto& it : this->declarations) {
252 // Don't merge availability for inline functions (because they shouldn't have any).
253 if (it.second.is_definition) {
254 continue;
255 }
256
257 DeclarationAvailability decl_availability;
258 if (!it.second.calculateAvailability(&decl_availability)) {
259 return false;
260 abort();
261 }
262
263 if (!avail.merge(decl_availability)) {
264 return false;
265 }
266 }
267 *output = avail;
268 return true;
269}
270
271bool Symbol::hasDeclaration(const CompilationType& type) const {
272 for (const auto& decl_it : this->declarations) {
273 for (const auto& compilation_it : decl_it.second.availability) {
274 if (compilation_it.first == type) {
275 return true;
276 }
277 }
278 }
279 return false;
280}
281
282void HeaderDatabase::parseAST(CompilationType type, ASTUnit* ast) {
283 std::unique_lock<std::mutex> lock(this->mutex);
Josh Gaobf8a2852016-05-27 11:59:09 -0700284 ASTContext& ctx = ast->getASTContext();
Josh Gaobfb6bae2016-07-15 17:25:21 -0700285 Visitor visitor(*this, type, ctx);
Josh Gaobf8a2852016-05-27 11:59:09 -0700286 visitor.TraverseDecl(ctx.getTranslationUnitDecl());
287}
Josh Gaobfb6bae2016-07-15 17:25:21 -0700288
289std::string to_string(const CompilationType& type) {
290 std::stringstream ss;
291 ss << to_string(type.arch) << "-" << type.api_level;
292 return ss.str();
293}
294
295std::string to_string(const AvailabilityValues& av) {
296 std::stringstream ss;
297
298 if (av.future) {
299 ss << "future, ";
300 }
301
302 if (av.introduced != 0) {
303 ss << "introduced = " << av.introduced << ", ";
304 }
305
306 if (av.deprecated != 0) {
307 ss << "deprecated = " << av.deprecated << ", ";
308 }
309
310 if (av.obsoleted != 0) {
311 ss << "obsoleted = " << av.obsoleted << ", ";
312 }
313
314 std::string result = ss.str();
315 if (!result.empty()) {
316 result = result.substr(0, result.length() - 2);
317 }
318 return result;
319}
320
321std::string to_string(const DeclarationType& type) {
322 switch (type) {
323 case DeclarationType::function:
324 return "function";
325 case DeclarationType::variable:
326 return "variable";
327 case DeclarationType::inconsistent:
328 return "inconsistent";
329 }
330 abort();
331}
332
333std::string to_string(const DeclarationAvailability& decl_av) {
334 std::stringstream ss;
335 if (!decl_av.global_availability.empty()) {
336 ss << to_string(decl_av.global_availability) << ", ";
337 }
338
Josh Gao16057882016-08-02 14:54:09 -0700339 for (const auto& it : decl_av.arch_availability) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700340 if (!it.second.empty()) {
341 ss << to_string(it.first) << ": " << to_string(it.second) << ", ";
342 }
343 }
344
345 std::string result = ss.str();
346 if (result.size() == 0) {
347 return "no availability";
348 }
349
350 return result.substr(0, result.length() - 2);
351}
352
353std::string to_string(const Location& loc) {
354 std::stringstream ss;
355 ss << loc.filename << ":" << loc.start.line << ":" << loc.start.column;
356 return ss.str();
357}