blob: afae509aee30ac1b47a636dee7cf23c01fc74d22 [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
Josh Gaoab25d0b2017-08-10 10:50:33 -070038static bool shouldMangle(MangleContext* mangler, NamedDecl* decl) {
39 // Passing a decl with static linkage to the mangler gives incorrect results.
40 // Check some things ourselves before handing it off to the mangler.
41 if (auto FD = dyn_cast<FunctionDecl>(decl)) {
42 if (FD->isExternC()) {
43 return false;
44 }
45
46 if (FD->isInExternCContext()) {
47 return false;
48 }
49 }
50
51 return mangler->shouldMangleDeclName(decl);
52}
53
Josh Gaobf8a2852016-05-27 11:59:09 -070054class Visitor : public RecursiveASTVisitor<Visitor> {
55 HeaderDatabase& database;
Josh Gaobfb6bae2016-07-15 17:25:21 -070056 CompilationType type;
Josh Gaobf8a2852016-05-27 11:59:09 -070057 SourceManager& src_manager;
58 std::unique_ptr<MangleContext> mangler;
59
60 public:
Josh Gaobfb6bae2016-07-15 17:25:21 -070061 Visitor(HeaderDatabase& database, CompilationType type, ASTContext& ctx)
62 : database(database), type(type), src_manager(ctx.getSourceManager()) {
Josh Gaobf8a2852016-05-27 11:59:09 -070063 mangler.reset(ItaniumMangleContext::create(ctx, ctx.getDiagnostics()));
64 }
65
66 std::string getDeclName(NamedDecl* decl) {
67 if (auto var_decl = dyn_cast<VarDecl>(decl)) {
68 if (!var_decl->isFileVarDecl()) {
69 return "<local var>";
70 }
71 }
72
Josh Gao38685e12017-10-30 12:45:09 -070073 // <math.h> maps fool onto foo on 32-bit, since long double is the same as double.
74 if (auto asm_attr = decl->getAttr<AsmLabelAttr>()) {
75 return asm_attr->getLabel();
76 }
77
Josh Gao0062b3e2017-10-24 17:03:58 -070078 // The decl might not have a name (e.g. bitfields).
Josh Gao0a284f52016-12-15 13:56:00 -080079 if (auto identifier = decl->getIdentifier()) {
Josh Gaoab25d0b2017-08-10 10:50:33 -070080 if (shouldMangle(mangler.get(), decl)) {
Josh Gao0062b3e2017-10-24 17:03:58 -070081 std::string mangled;
82 llvm::raw_string_ostream ss(mangled);
83 mangler->mangleName(decl, ss);
84 return mangled;
85 }
86
Josh Gao0a284f52016-12-15 13:56:00 -080087 return identifier->getName();
Josh Gaobf8a2852016-05-27 11:59:09 -070088 }
Josh Gao0062b3e2017-10-24 17:03:58 -070089
90 return "<unnamed>";
Josh Gaobf8a2852016-05-27 11:59:09 -070091 }
92
Josh Gao8e13b672017-11-06 17:15:47 -080093 bool VisitDeclaratorDecl(DeclaratorDecl* decl, SourceRange range) {
Josh Gaobf8a2852016-05-27 11:59:09 -070094 // Skip declarations inside of functions (function arguments, variable declarations inside of
95 // inline functions, etc).
96 if (decl->getParentFunctionOrMethod()) {
97 return true;
98 }
99
100 auto named_decl = dyn_cast<NamedDecl>(decl);
101 if (!named_decl) {
102 return true;
103 }
104
Josh Gaobf8a2852016-05-27 11:59:09 -0700105 std::string declaration_name = getDeclName(named_decl);
106 bool is_extern = named_decl->getFormalLinkage() == ExternalLinkage;
107 bool is_definition = false;
Josh Gaofff29fe2016-09-07 18:29:08 -0700108 bool no_guard = false;
Logan Chienc88331b2019-12-20 10:27:45 -0800109 bool fortify_inline = false;
Josh Gaobf8a2852016-05-27 11:59:09 -0700110
111 if (auto function_decl = dyn_cast<FunctionDecl>(decl)) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700112 is_definition = function_decl->isThisDeclarationADefinition();
113 } else if (auto var_decl = dyn_cast<VarDecl>(decl)) {
114 if (!var_decl->isFileVarDecl()) {
115 return true;
116 }
117
Josh Gaobf8a2852016-05-27 11:59:09 -0700118 switch (var_decl->isThisDeclarationADefinition()) {
119 case VarDecl::DeclarationOnly:
120 is_definition = false;
121 break;
122
123 case VarDecl::Definition:
124 is_definition = true;
125 break;
126
127 case VarDecl::TentativeDefinition:
128 // Forbid tentative definitions in headers.
129 fprintf(stderr, "ERROR: declaration '%s' is a tentative definition\n",
130 declaration_name.c_str());
131 decl->dump();
132 abort();
133 }
134 } else {
135 // We only care about function and variable declarations.
136 return true;
137 }
138
139 if (decl->hasAttr<UnavailableAttr>()) {
140 // Skip declarations that exist only for compile-time diagnostics.
141 return true;
142 }
143
Josh Gaobf8a2852016-05-27 11:59:09 -0700144 DeclarationAvailability availability;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700145
146 // Find and parse __ANDROID_AVAILABILITY_DUMP__ annotations.
147 for (const AnnotateAttr* attr : decl->specific_attrs<AnnotateAttr>()) {
148 llvm::StringRef annotation = attr->getAnnotation();
Josh Gaofff29fe2016-09-07 18:29:08 -0700149 if (annotation == "versioner_no_guard") {
150 no_guard = true;
Logan Chienc88331b2019-12-20 10:27:45 -0800151 } else if (annotation == "versioner_fortify_inline") {
152 fortify_inline = true;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700153 } else {
154 llvm::SmallVector<llvm::StringRef, 2> fragments;
155 annotation.split(fragments, "=");
156 if (fragments.size() != 2) {
157 continue;
158 }
159
160 auto& global_availability = availability.global_availability;
161 auto& arch_availability = availability.arch_availability;
162 std::map<std::string, std::vector<int*>> prefix_map = {
163 { "introduced_in", { &global_availability.introduced } },
164 { "deprecated_in", { &global_availability.deprecated } },
165 { "obsoleted_in", { &global_availability.obsoleted } },
166 { "introduced_in_arm", { &arch_availability[Arch::arm].introduced } },
167 { "introduced_in_mips", { &arch_availability[Arch::mips].introduced } },
168 { "introduced_in_x86", { &arch_availability[Arch::x86].introduced } },
169 { "introduced_in_32",
170 { &arch_availability[Arch::arm].introduced,
171 &arch_availability[Arch::mips].introduced,
172 &arch_availability[Arch::x86].introduced } },
173 { "introduced_in_64",
174 { &arch_availability[Arch::arm64].introduced,
175 &arch_availability[Arch::mips64].introduced,
176 &arch_availability[Arch::x86_64].introduced } },
177 };
178
Josh Gao0a284f52016-12-15 13:56:00 -0800179 if (auto it = prefix_map.find(fragments[0]); it != prefix_map.end()) {
180 int value;
181 if (fragments[1].getAsInteger(10, value)) {
182 errx(1, "invalid __ANDROID_AVAILABILITY_DUMP__ annotation: '%s'",
183 annotation.str().c_str());
184 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700185
Josh Gao0a284f52016-12-15 13:56:00 -0800186 for (int* ptr : it->second) {
187 *ptr = value;
188 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700189 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700190 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700191 }
192
193 auto symbol_it = database.symbols.find(declaration_name);
194 if (symbol_it == database.symbols.end()) {
195 Symbol symbol = {.name = declaration_name };
196 bool dummy;
197 std::tie(symbol_it, dummy) = database.symbols.insert({ declaration_name, symbol });
Josh Gaobf8a2852016-05-27 11:59:09 -0700198 }
199
Josh Gao8e13b672017-11-06 17:15:47 -0800200 auto expansion_range = src_manager.getExpansionRange(range);
201 auto filename = src_manager.getFilename(expansion_range.getBegin());
202 if (filename != src_manager.getFilename(expansion_range.getEnd())) {
203 errx(1, "expansion range filenames don't match");
204 }
205
206 Location location = {
207 .filename = filename,
208 .start = {
209 .line = src_manager.getExpansionLineNumber(expansion_range.getBegin()),
210 .column = src_manager.getExpansionColumnNumber(expansion_range.getBegin()),
211 },
212 .end = {
213 .line = src_manager.getExpansionLineNumber(expansion_range.getEnd()),
214 .column = src_manager.getExpansionColumnNumber(expansion_range.getEnd()),
215 }
216 };
217
Josh Gaobf8a2852016-05-27 11:59:09 -0700218 // Find or insert an entry for the declaration.
Josh Gao0a284f52016-12-15 13:56:00 -0800219 if (auto declaration_it = symbol_it->second.declarations.find(location);
220 declaration_it != symbol_it->second.declarations.end()) {
221 if (declaration_it->second.is_extern != is_extern ||
222 declaration_it->second.is_definition != is_definition ||
Logan Chienc88331b2019-12-20 10:27:45 -0800223 declaration_it->second.no_guard != no_guard ||
224 declaration_it->second.fortify_inline != fortify_inline) {
Josh Gao0a284f52016-12-15 13:56:00 -0800225 errx(1, "varying declaration of '%s' at %s:%u:%u", declaration_name.c_str(),
226 location.filename.c_str(), location.start.line, location.start.column);
227 }
228 declaration_it->second.availability.insert(std::make_pair(type, availability));
229 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700230 Declaration declaration;
Josh Gao16057882016-08-02 14:54:09 -0700231 declaration.name = declaration_name;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700232 declaration.location = location;
233 declaration.is_extern = is_extern;
234 declaration.is_definition = is_definition;
Josh Gaofff29fe2016-09-07 18:29:08 -0700235 declaration.no_guard = no_guard;
Logan Chienc88331b2019-12-20 10:27:45 -0800236 declaration.fortify_inline = fortify_inline;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700237 declaration.availability.insert(std::make_pair(type, availability));
238 symbol_it->second.declarations.insert(std::make_pair(location, declaration));
Josh Gaobf8a2852016-05-27 11:59:09 -0700239 }
240
241 return true;
242 }
Josh Gao8e13b672017-11-06 17:15:47 -0800243
244 bool VisitDeclaratorDecl(DeclaratorDecl* decl) {
245 return VisitDeclaratorDecl(decl, decl->getSourceRange());
246 }
247
248 bool TraverseLinkageSpecDecl(LinkageSpecDecl* decl) {
249 // Make sure that we correctly calculate the SourceRange of a declaration that has a non-braced
250 // extern "C"/"C++".
251 if (!decl->hasBraces()) {
252 DeclaratorDecl* child = nullptr;
253 for (auto child_decl : decl->decls()) {
254 if (child != nullptr) {
255 errx(1, "LinkageSpecDecl has multiple children");
256 }
257
258 if (DeclaratorDecl* declarator_decl = dyn_cast<DeclaratorDecl>(child_decl)) {
259 child = declarator_decl;
260 } else {
261 errx(1, "child of LinkageSpecDecl is not a DeclaratorDecl");
262 }
263 }
264
265 return VisitDeclaratorDecl(child, decl->getSourceRange());
266 }
267
268 for (auto child : decl->decls()) {
269 if (!TraverseDecl(child)) {
270 return false;
271 }
272 }
273 return true;
274 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700275};
276
Josh Gaobfb6bae2016-07-15 17:25:21 -0700277bool DeclarationAvailability::merge(const DeclarationAvailability& other) {
278#define check_avail(expr) error |= (!this->expr.empty() && this->expr != other.expr);
279 bool error = false;
280
281 if (!other.global_availability.empty()) {
282 check_avail(global_availability);
283 this->global_availability = other.global_availability;
284 }
285
286 for (Arch arch : supported_archs) {
287 if (!other.arch_availability[arch].empty()) {
288 check_avail(arch_availability[arch]);
289 this->arch_availability[arch] = other.arch_availability[arch];
290 }
291 }
292#undef check_avail
293
294 return !error;
295}
296
297bool Declaration::calculateAvailability(DeclarationAvailability* output) const {
298 DeclarationAvailability avail;
299 for (const auto& it : this->availability) {
300 if (!avail.merge(it.second)) {
301 return false;
302 }
303 }
304 *output = avail;
305 return true;
306}
307
308bool Symbol::calculateAvailability(DeclarationAvailability* output) const {
309 DeclarationAvailability avail;
310 for (const auto& it : this->declarations) {
311 // Don't merge availability for inline functions (because they shouldn't have any).
312 if (it.second.is_definition) {
313 continue;
314 }
315
316 DeclarationAvailability decl_availability;
317 if (!it.second.calculateAvailability(&decl_availability)) {
318 return false;
319 abort();
320 }
321
322 if (!avail.merge(decl_availability)) {
323 return false;
324 }
325 }
326 *output = avail;
327 return true;
328}
329
330bool Symbol::hasDeclaration(const CompilationType& type) const {
331 for (const auto& decl_it : this->declarations) {
332 for (const auto& compilation_it : decl_it.second.availability) {
333 if (compilation_it.first == type) {
334 return true;
335 }
336 }
337 }
338 return false;
339}
340
Josh Gao16016df2016-11-07 18:27:16 -0800341void HeaderDatabase::parseAST(CompilationType type, ASTContext& ctx) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700342 std::unique_lock<std::mutex> lock(this->mutex);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700343 Visitor visitor(*this, type, ctx);
Josh Gaobf8a2852016-05-27 11:59:09 -0700344 visitor.TraverseDecl(ctx.getTranslationUnitDecl());
345}
Josh Gaobfb6bae2016-07-15 17:25:21 -0700346
Josh Gaobfb6bae2016-07-15 17:25:21 -0700347std::string to_string(const AvailabilityValues& av) {
348 std::stringstream ss;
349
Josh Gaobfb6bae2016-07-15 17:25:21 -0700350 if (av.introduced != 0) {
351 ss << "introduced = " << av.introduced << ", ";
352 }
353
354 if (av.deprecated != 0) {
355 ss << "deprecated = " << av.deprecated << ", ";
356 }
357
358 if (av.obsoleted != 0) {
359 ss << "obsoleted = " << av.obsoleted << ", ";
360 }
361
362 std::string result = ss.str();
363 if (!result.empty()) {
364 result = result.substr(0, result.length() - 2);
365 }
366 return result;
367}
368
369std::string to_string(const DeclarationType& type) {
370 switch (type) {
371 case DeclarationType::function:
372 return "function";
373 case DeclarationType::variable:
374 return "variable";
375 case DeclarationType::inconsistent:
376 return "inconsistent";
377 }
378 abort();
379}
380
381std::string to_string(const DeclarationAvailability& decl_av) {
382 std::stringstream ss;
383 if (!decl_av.global_availability.empty()) {
384 ss << to_string(decl_av.global_availability) << ", ";
385 }
386
Josh Gao16057882016-08-02 14:54:09 -0700387 for (const auto& it : decl_av.arch_availability) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700388 if (!it.second.empty()) {
389 ss << to_string(it.first) << ": " << to_string(it.second) << ", ";
390 }
391 }
392
393 std::string result = ss.str();
394 if (result.size() == 0) {
395 return "no availability";
396 }
397
398 return result.substr(0, result.length() - 2);
399}
400
401std::string to_string(const Location& loc) {
402 std::stringstream ss;
403 ss << loc.filename << ":" << loc.start.line << ":" << loc.start.column;
404 return ss.str();
405}