blob: e9ba37ea8a7388f74a64f3b9624b41fcdc9afbc1 [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
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;
Josh Gaofff29fe2016-09-07 18:29:08 -070087 bool no_guard = false;
Josh Gaobf8a2852016-05-27 11:59:09 -070088
89 if (auto function_decl = dyn_cast<FunctionDecl>(decl)) {
90 declaration_type = DeclarationType::function;
91 is_definition = function_decl->isThisDeclarationADefinition();
92 } else if (auto var_decl = dyn_cast<VarDecl>(decl)) {
93 if (!var_decl->isFileVarDecl()) {
94 return true;
95 }
96
97 declaration_type = DeclarationType::variable;
98 switch (var_decl->isThisDeclarationADefinition()) {
99 case VarDecl::DeclarationOnly:
100 is_definition = false;
101 break;
102
103 case VarDecl::Definition:
104 is_definition = true;
105 break;
106
107 case VarDecl::TentativeDefinition:
108 // Forbid tentative definitions in headers.
109 fprintf(stderr, "ERROR: declaration '%s' is a tentative definition\n",
110 declaration_name.c_str());
111 decl->dump();
112 abort();
113 }
114 } else {
115 // We only care about function and variable declarations.
116 return true;
117 }
118
119 if (decl->hasAttr<UnavailableAttr>()) {
120 // Skip declarations that exist only for compile-time diagnostics.
121 return true;
122 }
123
Josh Gaobfb6bae2016-07-15 17:25:21 -0700124 auto start_loc = src_manager.getPresumedLoc(decl->getLocStart());
125 auto end_loc = src_manager.getPresumedLoc(decl->getLocEnd());
126
127 Location location = {
128 .filename = start_loc.getFilename(),
129 .start = {
130 .line = start_loc.getLine(),
131 .column = start_loc.getColumn(),
132 },
133 .end = {
134 .line = end_loc.getLine(),
135 .column = end_loc.getColumn(),
136 }
137 };
138
Josh Gaobf8a2852016-05-27 11:59:09 -0700139 DeclarationAvailability availability;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700140
141 // Find and parse __ANDROID_AVAILABILITY_DUMP__ annotations.
142 for (const AnnotateAttr* attr : decl->specific_attrs<AnnotateAttr>()) {
143 llvm::StringRef annotation = attr->getAnnotation();
Josh Gaofff29fe2016-09-07 18:29:08 -0700144 if (annotation == "versioner_no_guard") {
145 no_guard = true;
146 } else if (annotation == "introduced_in_future") {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700147 // Tag the compiled-for arch, since this can vary across archs.
148 availability.arch_availability[type.arch].future = true;
149 } else {
150 llvm::SmallVector<llvm::StringRef, 2> fragments;
151 annotation.split(fragments, "=");
152 if (fragments.size() != 2) {
153 continue;
154 }
155
156 auto& global_availability = availability.global_availability;
157 auto& arch_availability = availability.arch_availability;
158 std::map<std::string, std::vector<int*>> prefix_map = {
159 { "introduced_in", { &global_availability.introduced } },
160 { "deprecated_in", { &global_availability.deprecated } },
161 { "obsoleted_in", { &global_availability.obsoleted } },
162 { "introduced_in_arm", { &arch_availability[Arch::arm].introduced } },
163 { "introduced_in_mips", { &arch_availability[Arch::mips].introduced } },
164 { "introduced_in_x86", { &arch_availability[Arch::x86].introduced } },
165 { "introduced_in_32",
166 { &arch_availability[Arch::arm].introduced,
167 &arch_availability[Arch::mips].introduced,
168 &arch_availability[Arch::x86].introduced } },
169 { "introduced_in_64",
170 { &arch_availability[Arch::arm64].introduced,
171 &arch_availability[Arch::mips64].introduced,
172 &arch_availability[Arch::x86_64].introduced } },
173 };
174
175 auto it = prefix_map.find(fragments[0]);
176 if (it == prefix_map.end()) {
177 continue;
178 }
179 int value;
180 if (fragments[1].getAsInteger(10, value)) {
181 errx(1, "invalid __ANDROID_AVAILABILITY_DUMP__ annotation: '%s'",
182 annotation.str().c_str());
183 }
184
185 for (int* ptr : it->second) {
186 *ptr = value;
187 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700188 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700189 }
190
191 auto symbol_it = database.symbols.find(declaration_name);
192 if (symbol_it == database.symbols.end()) {
193 Symbol symbol = {.name = declaration_name };
194 bool dummy;
195 std::tie(symbol_it, dummy) = database.symbols.insert({ declaration_name, symbol });
Josh Gaobf8a2852016-05-27 11:59:09 -0700196 }
197
198 // Find or insert an entry for the declaration.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700199 auto declaration_it = symbol_it->second.declarations.find(location);
200 if (declaration_it == symbol_it->second.declarations.end()) {
201 Declaration declaration;
Josh Gao16057882016-08-02 14:54:09 -0700202 declaration.name = declaration_name;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700203 declaration.location = location;
204 declaration.is_extern = is_extern;
205 declaration.is_definition = is_definition;
Josh Gaofff29fe2016-09-07 18:29:08 -0700206 declaration.no_guard = no_guard;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700207 declaration.availability.insert(std::make_pair(type, availability));
208 symbol_it->second.declarations.insert(std::make_pair(location, declaration));
209 } else {
210 if (declaration_it->second.is_extern != is_extern ||
Josh Gaofff29fe2016-09-07 18:29:08 -0700211 declaration_it->second.is_definition != is_definition ||
212 declaration_it->second.no_guard != no_guard) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700213 errx(1, "varying declaration of '%s' at %s:%u:%u", declaration_name.c_str(),
214 location.filename.c_str(), location.start.line, location.start.column);
Josh Gaobf8a2852016-05-27 11:59:09 -0700215 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700216 declaration_it->second.availability.insert(std::make_pair(type, availability));
Josh Gaobf8a2852016-05-27 11:59:09 -0700217 }
218
219 return true;
220 }
221};
222
Josh Gaobfb6bae2016-07-15 17:25:21 -0700223bool DeclarationAvailability::merge(const DeclarationAvailability& other) {
224#define check_avail(expr) error |= (!this->expr.empty() && this->expr != other.expr);
225 bool error = false;
226
227 if (!other.global_availability.empty()) {
228 check_avail(global_availability);
229 this->global_availability = other.global_availability;
230 }
231
232 for (Arch arch : supported_archs) {
233 if (!other.arch_availability[arch].empty()) {
234 check_avail(arch_availability[arch]);
235 this->arch_availability[arch] = other.arch_availability[arch];
236 }
237 }
238#undef check_avail
239
240 return !error;
241}
242
243bool Declaration::calculateAvailability(DeclarationAvailability* output) const {
244 DeclarationAvailability avail;
245 for (const auto& it : this->availability) {
246 if (!avail.merge(it.second)) {
247 return false;
248 }
249 }
250 *output = avail;
251 return true;
252}
253
254bool Symbol::calculateAvailability(DeclarationAvailability* output) const {
255 DeclarationAvailability avail;
256 for (const auto& it : this->declarations) {
257 // Don't merge availability for inline functions (because they shouldn't have any).
258 if (it.second.is_definition) {
259 continue;
260 }
261
262 DeclarationAvailability decl_availability;
263 if (!it.second.calculateAvailability(&decl_availability)) {
264 return false;
265 abort();
266 }
267
268 if (!avail.merge(decl_availability)) {
269 return false;
270 }
271 }
272 *output = avail;
273 return true;
274}
275
276bool Symbol::hasDeclaration(const CompilationType& type) const {
277 for (const auto& decl_it : this->declarations) {
278 for (const auto& compilation_it : decl_it.second.availability) {
279 if (compilation_it.first == type) {
280 return true;
281 }
282 }
283 }
284 return false;
285}
286
Josh Gao16016df2016-11-07 18:27:16 -0800287void HeaderDatabase::parseAST(CompilationType type, ASTContext& ctx) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700288 std::unique_lock<std::mutex> lock(this->mutex);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700289 Visitor visitor(*this, type, ctx);
Josh Gaobf8a2852016-05-27 11:59:09 -0700290 visitor.TraverseDecl(ctx.getTranslationUnitDecl());
291}
Josh Gaobfb6bae2016-07-15 17:25:21 -0700292
Josh Gaobfb6bae2016-07-15 17:25:21 -0700293std::string to_string(const AvailabilityValues& av) {
294 std::stringstream ss;
295
296 if (av.future) {
297 ss << "future, ";
298 }
299
300 if (av.introduced != 0) {
301 ss << "introduced = " << av.introduced << ", ";
302 }
303
304 if (av.deprecated != 0) {
305 ss << "deprecated = " << av.deprecated << ", ";
306 }
307
308 if (av.obsoleted != 0) {
309 ss << "obsoleted = " << av.obsoleted << ", ";
310 }
311
312 std::string result = ss.str();
313 if (!result.empty()) {
314 result = result.substr(0, result.length() - 2);
315 }
316 return result;
317}
318
319std::string to_string(const DeclarationType& type) {
320 switch (type) {
321 case DeclarationType::function:
322 return "function";
323 case DeclarationType::variable:
324 return "variable";
325 case DeclarationType::inconsistent:
326 return "inconsistent";
327 }
328 abort();
329}
330
331std::string to_string(const DeclarationAvailability& decl_av) {
332 std::stringstream ss;
333 if (!decl_av.global_availability.empty()) {
334 ss << to_string(decl_av.global_availability) << ", ";
335 }
336
Josh Gao16057882016-08-02 14:54:09 -0700337 for (const auto& it : decl_av.arch_availability) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700338 if (!it.second.empty()) {
339 ss << to_string(it.first) << ": " << to_string(it.second) << ", ";
340 }
341 }
342
343 std::string result = ss.str();
344 if (result.size() == 0) {
345 return "no availability";
346 }
347
348 return result.substr(0, result.length() - 2);
349}
350
351std::string to_string(const Location& loc) {
352 std::stringstream ss;
353 ss << loc.filename << ":" << loc.start.line << ":" << loc.start.column;
354 return ss.str();
355}