blob: aef4f550fb26e4f172e288d4b3ac5ac91ab7e7f1 [file] [log] [blame]
Josh Gaobf8a2852016-05-27 11:59:09 -07001/*
Elliott Hughesdfb74c52016-10-24 12:53:17 -07002 * Copyright (C) 2016 The Android Open Source Project
Josh Gaobf8a2852016-05-27 11:59:09 -07003 *
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 *
Elliott Hughesdfb74c52016-10-24 12:53:17 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Josh Gaobf8a2852016-05-27 11:59:09 -07009 *
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
Josh Gao0062b3e2017-10-24 17:03:58 -070057 // The decl might not have a name (e.g. bitfields).
Josh Gao0a284f52016-12-15 13:56:00 -080058 if (auto identifier = decl->getIdentifier()) {
Josh Gao0062b3e2017-10-24 17:03:58 -070059 if (mangler->shouldMangleDeclName(decl)) {
60 std::string mangled;
61 llvm::raw_string_ostream ss(mangled);
62 mangler->mangleName(decl, ss);
63 return mangled;
64 }
65
Josh Gao0a284f52016-12-15 13:56:00 -080066 return identifier->getName();
Josh Gaobf8a2852016-05-27 11:59:09 -070067 }
Josh Gao0062b3e2017-10-24 17:03:58 -070068
69 return "<unnamed>";
Josh Gaobf8a2852016-05-27 11:59:09 -070070 }
71
72 bool VisitDecl(Decl* decl) {
73 // Skip declarations inside of functions (function arguments, variable declarations inside of
74 // inline functions, etc).
75 if (decl->getParentFunctionOrMethod()) {
76 return true;
77 }
78
79 auto named_decl = dyn_cast<NamedDecl>(decl);
80 if (!named_decl) {
81 return true;
82 }
83
84 DeclarationType declaration_type;
85 std::string declaration_name = getDeclName(named_decl);
86 bool is_extern = named_decl->getFormalLinkage() == ExternalLinkage;
87 bool is_definition = false;
Josh Gaofff29fe2016-09-07 18:29:08 -070088 bool no_guard = false;
Josh Gaobf8a2852016-05-27 11:59:09 -070089
90 if (auto function_decl = dyn_cast<FunctionDecl>(decl)) {
91 declaration_type = DeclarationType::function;
92 is_definition = function_decl->isThisDeclarationADefinition();
93 } else if (auto var_decl = dyn_cast<VarDecl>(decl)) {
94 if (!var_decl->isFileVarDecl()) {
95 return true;
96 }
97
98 declaration_type = DeclarationType::variable;
99 switch (var_decl->isThisDeclarationADefinition()) {
100 case VarDecl::DeclarationOnly:
101 is_definition = false;
102 break;
103
104 case VarDecl::Definition:
105 is_definition = true;
106 break;
107
108 case VarDecl::TentativeDefinition:
109 // Forbid tentative definitions in headers.
110 fprintf(stderr, "ERROR: declaration '%s' is a tentative definition\n",
111 declaration_name.c_str());
112 decl->dump();
113 abort();
114 }
115 } else {
116 // We only care about function and variable declarations.
117 return true;
118 }
119
120 if (decl->hasAttr<UnavailableAttr>()) {
121 // Skip declarations that exist only for compile-time diagnostics.
122 return true;
123 }
124
Josh Gaobfb6bae2016-07-15 17:25:21 -0700125 auto start_loc = src_manager.getPresumedLoc(decl->getLocStart());
126 auto end_loc = src_manager.getPresumedLoc(decl->getLocEnd());
127
128 Location location = {
129 .filename = start_loc.getFilename(),
130 .start = {
131 .line = start_loc.getLine(),
132 .column = start_loc.getColumn(),
133 },
134 .end = {
135 .line = end_loc.getLine(),
136 .column = end_loc.getColumn(),
137 }
138 };
139
Josh Gaobf8a2852016-05-27 11:59:09 -0700140 DeclarationAvailability availability;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700141
142 // Find and parse __ANDROID_AVAILABILITY_DUMP__ annotations.
143 for (const AnnotateAttr* attr : decl->specific_attrs<AnnotateAttr>()) {
144 llvm::StringRef annotation = attr->getAnnotation();
Josh Gaofff29fe2016-09-07 18:29:08 -0700145 if (annotation == "versioner_no_guard") {
146 no_guard = true;
147 } else if (annotation == "introduced_in_future") {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700148 // Tag the compiled-for arch, since this can vary across archs.
149 availability.arch_availability[type.arch].future = true;
150 } else {
151 llvm::SmallVector<llvm::StringRef, 2> fragments;
152 annotation.split(fragments, "=");
153 if (fragments.size() != 2) {
154 continue;
155 }
156
157 auto& global_availability = availability.global_availability;
158 auto& arch_availability = availability.arch_availability;
159 std::map<std::string, std::vector<int*>> prefix_map = {
160 { "introduced_in", { &global_availability.introduced } },
161 { "deprecated_in", { &global_availability.deprecated } },
162 { "obsoleted_in", { &global_availability.obsoleted } },
163 { "introduced_in_arm", { &arch_availability[Arch::arm].introduced } },
164 { "introduced_in_mips", { &arch_availability[Arch::mips].introduced } },
165 { "introduced_in_x86", { &arch_availability[Arch::x86].introduced } },
166 { "introduced_in_32",
167 { &arch_availability[Arch::arm].introduced,
168 &arch_availability[Arch::mips].introduced,
169 &arch_availability[Arch::x86].introduced } },
170 { "introduced_in_64",
171 { &arch_availability[Arch::arm64].introduced,
172 &arch_availability[Arch::mips64].introduced,
173 &arch_availability[Arch::x86_64].introduced } },
174 };
175
Josh Gao0a284f52016-12-15 13:56:00 -0800176 if (auto it = prefix_map.find(fragments[0]); it != prefix_map.end()) {
177 int value;
178 if (fragments[1].getAsInteger(10, value)) {
179 errx(1, "invalid __ANDROID_AVAILABILITY_DUMP__ annotation: '%s'",
180 annotation.str().c_str());
181 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700182
Josh Gao0a284f52016-12-15 13:56:00 -0800183 for (int* ptr : it->second) {
184 *ptr = value;
185 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700186 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700187 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700188 }
189
190 auto symbol_it = database.symbols.find(declaration_name);
191 if (symbol_it == database.symbols.end()) {
192 Symbol symbol = {.name = declaration_name };
193 bool dummy;
194 std::tie(symbol_it, dummy) = database.symbols.insert({ declaration_name, symbol });
Josh Gaobf8a2852016-05-27 11:59:09 -0700195 }
196
197 // Find or insert an entry for the declaration.
Josh Gao0a284f52016-12-15 13:56:00 -0800198 if (auto declaration_it = symbol_it->second.declarations.find(location);
199 declaration_it != symbol_it->second.declarations.end()) {
200 if (declaration_it->second.is_extern != is_extern ||
201 declaration_it->second.is_definition != is_definition ||
202 declaration_it->second.no_guard != no_guard) {
203 errx(1, "varying declaration of '%s' at %s:%u:%u", declaration_name.c_str(),
204 location.filename.c_str(), location.start.line, location.start.column);
205 }
206 declaration_it->second.availability.insert(std::make_pair(type, availability));
207 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700208 Declaration declaration;
Josh Gao16057882016-08-02 14:54:09 -0700209 declaration.name = declaration_name;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700210 declaration.location = location;
211 declaration.is_extern = is_extern;
212 declaration.is_definition = is_definition;
Josh Gaofff29fe2016-09-07 18:29:08 -0700213 declaration.no_guard = no_guard;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700214 declaration.availability.insert(std::make_pair(type, availability));
215 symbol_it->second.declarations.insert(std::make_pair(location, declaration));
Josh Gaobf8a2852016-05-27 11:59:09 -0700216 }
217
218 return true;
219 }
220};
221
Josh Gaobfb6bae2016-07-15 17:25:21 -0700222bool DeclarationAvailability::merge(const DeclarationAvailability& other) {
223#define check_avail(expr) error |= (!this->expr.empty() && this->expr != other.expr);
224 bool error = false;
225
226 if (!other.global_availability.empty()) {
227 check_avail(global_availability);
228 this->global_availability = other.global_availability;
229 }
230
231 for (Arch arch : supported_archs) {
232 if (!other.arch_availability[arch].empty()) {
233 check_avail(arch_availability[arch]);
234 this->arch_availability[arch] = other.arch_availability[arch];
235 }
236 }
237#undef check_avail
238
239 return !error;
240}
241
242bool Declaration::calculateAvailability(DeclarationAvailability* output) const {
243 DeclarationAvailability avail;
244 for (const auto& it : this->availability) {
245 if (!avail.merge(it.second)) {
246 return false;
247 }
248 }
249 *output = avail;
250 return true;
251}
252
253bool Symbol::calculateAvailability(DeclarationAvailability* output) const {
254 DeclarationAvailability avail;
255 for (const auto& it : this->declarations) {
256 // Don't merge availability for inline functions (because they shouldn't have any).
257 if (it.second.is_definition) {
258 continue;
259 }
260
261 DeclarationAvailability decl_availability;
262 if (!it.second.calculateAvailability(&decl_availability)) {
263 return false;
264 abort();
265 }
266
267 if (!avail.merge(decl_availability)) {
268 return false;
269 }
270 }
271 *output = avail;
272 return true;
273}
274
275bool Symbol::hasDeclaration(const CompilationType& type) const {
276 for (const auto& decl_it : this->declarations) {
277 for (const auto& compilation_it : decl_it.second.availability) {
278 if (compilation_it.first == type) {
279 return true;
280 }
281 }
282 }
283 return false;
284}
285
Josh Gao16016df2016-11-07 18:27:16 -0800286void HeaderDatabase::parseAST(CompilationType type, ASTContext& ctx) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700287 std::unique_lock<std::mutex> lock(this->mutex);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700288 Visitor visitor(*this, type, ctx);
Josh Gaobf8a2852016-05-27 11:59:09 -0700289 visitor.TraverseDecl(ctx.getTranslationUnitDecl());
290}
Josh Gaobfb6bae2016-07-15 17:25:21 -0700291
Josh Gaobfb6bae2016-07-15 17:25:21 -0700292std::string to_string(const AvailabilityValues& av) {
293 std::stringstream ss;
294
295 if (av.future) {
296 ss << "future, ";
297 }
298
299 if (av.introduced != 0) {
300 ss << "introduced = " << av.introduced << ", ";
301 }
302
303 if (av.deprecated != 0) {
304 ss << "deprecated = " << av.deprecated << ", ";
305 }
306
307 if (av.obsoleted != 0) {
308 ss << "obsoleted = " << av.obsoleted << ", ";
309 }
310
311 std::string result = ss.str();
312 if (!result.empty()) {
313 result = result.substr(0, result.length() - 2);
314 }
315 return result;
316}
317
318std::string to_string(const DeclarationType& type) {
319 switch (type) {
320 case DeclarationType::function:
321 return "function";
322 case DeclarationType::variable:
323 return "variable";
324 case DeclarationType::inconsistent:
325 return "inconsistent";
326 }
327 abort();
328}
329
330std::string to_string(const DeclarationAvailability& decl_av) {
331 std::stringstream ss;
332 if (!decl_av.global_availability.empty()) {
333 ss << to_string(decl_av.global_availability) << ", ";
334 }
335
Josh Gao16057882016-08-02 14:54:09 -0700336 for (const auto& it : decl_av.arch_availability) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700337 if (!it.second.empty()) {
338 ss << to_string(it.first) << ": " << to_string(it.second) << ", ";
339 }
340 }
341
342 std::string result = ss.str();
343 if (result.size() == 0) {
344 return "no availability";
345 }
346
347 return result.substr(0, result.length() - 2);
348}
349
350std::string to_string(const Location& loc) {
351 std::stringstream ss;
352 ss << loc.filename << ":" << loc.start.line << ":" << loc.start.column;
353 return ss.str();
354}