blob: 6f577a50b386303adf5106be83e36736ce987011 [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#pragma once
18
Josh Gao566735d2016-08-02 15:07:32 -070019#include <stdio.h>
20
Josh Gaobf8a2852016-05-27 11:59:09 -070021#include <map>
Josh Gaobfb6bae2016-07-15 17:25:21 -070022#include <mutex>
Josh Gaobf8a2852016-05-27 11:59:09 -070023#include <set>
24#include <string>
25#include <vector>
26
27#include <llvm/ADT/StringRef.h>
28
Josh Gaobfb6bae2016-07-15 17:25:21 -070029#include "Arch.h"
Josh Gaob5c49632016-11-08 22:21:31 -080030#include "CompilationType.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070031#include "Utils.h"
32
Josh Gaobfb6bae2016-07-15 17:25:21 -070033namespace clang {
Josh Gao16016df2016-11-07 18:27:16 -080034class ASTContext;
Josh Gaobfb6bae2016-07-15 17:25:21 -070035class Decl;
36}
37
Josh Gaobf8a2852016-05-27 11:59:09 -070038enum class DeclarationType {
39 function,
40 variable,
41 inconsistent,
42};
43
Josh Gaobfb6bae2016-07-15 17:25:21 -070044struct AvailabilityValues {
45 bool future = false;
Josh Gaobf8a2852016-05-27 11:59:09 -070046 int introduced = 0;
47 int deprecated = 0;
48 int obsoleted = 0;
49
Josh Gaobfb6bae2016-07-15 17:25:21 -070050 bool empty() const {
51 return !(future || introduced || deprecated || obsoleted);
Josh Gao173e7c02016-06-03 13:38:00 -070052 }
53
Josh Gaobfb6bae2016-07-15 17:25:21 -070054 bool operator==(const AvailabilityValues& rhs) const {
55 return std::tie(introduced, deprecated, obsoleted) ==
56 std::tie(rhs.introduced, rhs.deprecated, rhs.obsoleted);
57 }
58
59 bool operator!=(const AvailabilityValues& rhs) const {
60 return !(*this == rhs);
61 }
62};
63
64std::string to_string(const AvailabilityValues& av);
65
66struct DeclarationAvailability {
67 AvailabilityValues global_availability;
68 ArchMap<AvailabilityValues> arch_availability;
69
Josh Gao173e7c02016-06-03 13:38:00 -070070 bool empty() const {
Josh Gaobfb6bae2016-07-15 17:25:21 -070071 if (!global_availability.empty()) {
72 return false;
73 }
Josh Gao173e7c02016-06-03 13:38:00 -070074
Josh Gao16057882016-08-02 14:54:09 -070075 for (const auto& it : arch_availability) {
Josh Gaobfb6bae2016-07-15 17:25:21 -070076 if (!it.second.empty()) {
77 return false;
78 }
79 }
80
81 return true;
Josh Gao173e7c02016-06-03 13:38:00 -070082 }
83
84 bool operator==(const DeclarationAvailability& rhs) const {
Josh Gaobfb6bae2016-07-15 17:25:21 -070085 return std::tie(global_availability, arch_availability) ==
86 std::tie(rhs.global_availability, rhs.arch_availability);
Josh Gao173e7c02016-06-03 13:38:00 -070087 }
88
89 bool operator!=(const DeclarationAvailability& rhs) const {
90 return !(*this == rhs);
91 }
92
Josh Gaobfb6bae2016-07-15 17:25:21 -070093 // Returns false if the availability declarations conflict.
94 bool merge(const DeclarationAvailability& other);
Josh Gaobf8a2852016-05-27 11:59:09 -070095};
96
Josh Gaobfb6bae2016-07-15 17:25:21 -070097std::string to_string(const DeclarationAvailability& decl_av);
98
99struct FileLocation {
100 unsigned line;
Josh Gaobf8a2852016-05-27 11:59:09 -0700101 unsigned column;
Josh Gaobf8a2852016-05-27 11:59:09 -0700102
Josh Gaobfb6bae2016-07-15 17:25:21 -0700103 bool operator<(const FileLocation& rhs) const {
104 return std::tie(line, column) < std::tie(rhs.line, rhs.column);
Josh Gaobf8a2852016-05-27 11:59:09 -0700105 }
106
Josh Gaobfb6bae2016-07-15 17:25:21 -0700107 bool operator==(const FileLocation& rhs) const {
108 return std::tie(line, column) == std::tie(rhs.line, rhs.column);
Josh Gao173e7c02016-06-03 13:38:00 -0700109 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700110};
111
Josh Gaobfb6bae2016-07-15 17:25:21 -0700112struct Location {
113 std::string filename;
114 FileLocation start;
115 FileLocation end;
116
117 bool operator<(const Location& rhs) const {
118 return std::tie(filename, start, end) < std::tie(rhs.filename, rhs.start, rhs.end);
119 }
120};
121
122std::string to_string(const Location& loc);
123
Josh Gaobf8a2852016-05-27 11:59:09 -0700124struct Declaration {
Josh Gao16057882016-08-02 14:54:09 -0700125 std::string name;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700126 Location location;
Josh Gaobf8a2852016-05-27 11:59:09 -0700127
Josh Gaobfb6bae2016-07-15 17:25:21 -0700128 bool is_extern;
129 bool is_definition;
Josh Gaofff29fe2016-09-07 18:29:08 -0700130 bool no_guard;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700131 std::map<CompilationType, DeclarationAvailability> availability;
132
133 bool calculateAvailability(DeclarationAvailability* output) const;
134 bool operator<(const Declaration& rhs) const {
135 return location < rhs.location;
Josh Gaobf8a2852016-05-27 11:59:09 -0700136 }
137
Josh Gao566735d2016-08-02 15:07:32 -0700138 void dump(const std::string& base_path = "", FILE* out = stdout, unsigned indent = 0) const {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700139 std::string indent_str(indent, ' ');
Josh Gao566735d2016-08-02 15:07:32 -0700140 fprintf(out, "%s", indent_str.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700141
Josh Gao566735d2016-08-02 15:07:32 -0700142 fprintf(out, "%s ", is_extern ? "extern" : "static");
143 fprintf(out, "%s ", is_definition ? "definition" : "declaration");
Josh Gaofff29fe2016-09-07 18:29:08 -0700144 if (no_guard) {
145 fprintf(out, "no_guard ");
146 }
Josh Gao566735d2016-08-02 15:07:32 -0700147 fprintf(out, "@ %s:%u:%u", StripPrefix(location.filename, base_path).str().c_str(),
148 location.start.line, location.start.column);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700149
150 if (!availability.empty()) {
151 DeclarationAvailability avail;
152
Josh Gao566735d2016-08-02 15:07:32 -0700153 fprintf(out, "\n%s ", indent_str.c_str());
Josh Gaobfb6bae2016-07-15 17:25:21 -0700154 if (!calculateAvailability(&avail)) {
Josh Gao566735d2016-08-02 15:07:32 -0700155 fprintf(out, "invalid availability");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700156 } else {
Josh Gao566735d2016-08-02 15:07:32 -0700157 fprintf(out, "%s", to_string(avail).c_str());
Josh Gaobfb6bae2016-07-15 17:25:21 -0700158 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700159 }
160 }
161};
162
Josh Gaobfb6bae2016-07-15 17:25:21 -0700163struct Symbol {
164 std::string name;
165 std::map<Location, Declaration> declarations;
Josh Gaobf8a2852016-05-27 11:59:09 -0700166
Josh Gaobfb6bae2016-07-15 17:25:21 -0700167 bool calculateAvailability(DeclarationAvailability* output) const;
168 bool hasDeclaration(const CompilationType& type) const;
Josh Gaobf8a2852016-05-27 11:59:09 -0700169
Josh Gaobfb6bae2016-07-15 17:25:21 -0700170 bool operator<(const Symbol& rhs) const {
171 return name < rhs.name;
172 }
173
174 bool operator==(const Symbol& rhs) const {
175 return name == rhs.name;
176 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700177
Josh Gao566735d2016-08-02 15:07:32 -0700178 void dump(const std::string& base_path = "", FILE* out = stdout) const {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700179 DeclarationAvailability availability;
180 bool valid_availability = calculateAvailability(&availability);
Josh Gao566735d2016-08-02 15:07:32 -0700181 fprintf(out, " %s: ", name.c_str());
Josh Gaobfb6bae2016-07-15 17:25:21 -0700182
183 if (valid_availability) {
Josh Gao566735d2016-08-02 15:07:32 -0700184 fprintf(out, "%s\n", to_string(availability).c_str());
Josh Gaobfb6bae2016-07-15 17:25:21 -0700185 } else {
Josh Gao566735d2016-08-02 15:07:32 -0700186 fprintf(out, "invalid\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700187 }
188
Josh Gaobfb6bae2016-07-15 17:25:21 -0700189 for (auto& it : declarations) {
190 it.second.dump(base_path, out, 4);
Josh Gao566735d2016-08-02 15:07:32 -0700191 fprintf(out, "\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700192 }
193 }
194};
195
196class HeaderDatabase {
197 std::mutex mutex;
198
199 public:
200 std::map<std::string, Symbol> symbols;
201
Josh Gao16016df2016-11-07 18:27:16 -0800202 void parseAST(CompilationType type, clang::ASTContext& ast);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700203
Josh Gao566735d2016-08-02 15:07:32 -0700204 void dump(const std::string& base_path = "", FILE* out = stdout) const {
205 fprintf(out, "HeaderDatabase contains %zu symbols:\n", symbols.size());
Josh Gaobfb6bae2016-07-15 17:25:21 -0700206 for (const auto& pair : symbols) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700207 pair.second.dump(base_path, out);
208 }
209 }
210};