blob: bba826d00b4756c86a66e647b3a5cd6ca65953b8 [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#pragma once
18
19#include <iostream>
20#include <map>
Josh Gaobfb6bae2016-07-15 17:25:21 -070021#include <mutex>
Josh Gaobf8a2852016-05-27 11:59:09 -070022#include <set>
Josh Gao173e7c02016-06-03 13:38:00 -070023#include <sstream>
Josh Gaobf8a2852016-05-27 11:59:09 -070024#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 Gaobf8a2852016-05-27 11:59:09 -070030#include "Utils.h"
31
Josh Gaobfb6bae2016-07-15 17:25:21 -070032namespace clang {
33class ASTUnit;
34class Decl;
35}
36
Josh Gaobf8a2852016-05-27 11:59:09 -070037enum class DeclarationType {
38 function,
39 variable,
40 inconsistent,
41};
42
Josh Gaobf8a2852016-05-27 11:59:09 -070043struct CompilationType {
Josh Gaobfb6bae2016-07-15 17:25:21 -070044 Arch arch;
Josh Gaobf8a2852016-05-27 11:59:09 -070045 int api_level;
46
47 private:
48 auto tie() const {
49 return std::tie(arch, api_level);
50 }
51
52 public:
53 bool operator<(const CompilationType& other) const {
54 return tie() < other.tie();
55 }
56
57 bool operator==(const CompilationType& other) const {
58 return tie() == other.tie();
59 }
Josh Gaobf8a2852016-05-27 11:59:09 -070060};
61
Josh Gaobfb6bae2016-07-15 17:25:21 -070062std::string to_string(const CompilationType& type);
63
64struct AvailabilityValues {
65 bool future = false;
Josh Gaobf8a2852016-05-27 11:59:09 -070066 int introduced = 0;
67 int deprecated = 0;
68 int obsoleted = 0;
69
Josh Gaobfb6bae2016-07-15 17:25:21 -070070 bool empty() const {
71 return !(future || introduced || deprecated || obsoleted);
Josh Gao173e7c02016-06-03 13:38:00 -070072 }
73
Josh Gaobfb6bae2016-07-15 17:25:21 -070074 bool operator==(const AvailabilityValues& rhs) const {
75 return std::tie(introduced, deprecated, obsoleted) ==
76 std::tie(rhs.introduced, rhs.deprecated, rhs.obsoleted);
77 }
78
79 bool operator!=(const AvailabilityValues& rhs) const {
80 return !(*this == rhs);
81 }
82};
83
84std::string to_string(const AvailabilityValues& av);
85
86struct DeclarationAvailability {
87 AvailabilityValues global_availability;
88 ArchMap<AvailabilityValues> arch_availability;
89
Josh Gao173e7c02016-06-03 13:38:00 -070090 bool empty() const {
Josh Gaobfb6bae2016-07-15 17:25:21 -070091 if (!global_availability.empty()) {
92 return false;
93 }
Josh Gao173e7c02016-06-03 13:38:00 -070094
Josh Gao16057882016-08-02 14:54:09 -070095 for (const auto& it : arch_availability) {
Josh Gaobfb6bae2016-07-15 17:25:21 -070096 if (!it.second.empty()) {
97 return false;
98 }
99 }
100
101 return true;
Josh Gao173e7c02016-06-03 13:38:00 -0700102 }
103
104 bool operator==(const DeclarationAvailability& rhs) const {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700105 return std::tie(global_availability, arch_availability) ==
106 std::tie(rhs.global_availability, rhs.arch_availability);
Josh Gao173e7c02016-06-03 13:38:00 -0700107 }
108
109 bool operator!=(const DeclarationAvailability& rhs) const {
110 return !(*this == rhs);
111 }
112
Josh Gaobfb6bae2016-07-15 17:25:21 -0700113 // Returns false if the availability declarations conflict.
114 bool merge(const DeclarationAvailability& other);
Josh Gaobf8a2852016-05-27 11:59:09 -0700115};
116
Josh Gaobfb6bae2016-07-15 17:25:21 -0700117std::string to_string(const DeclarationAvailability& decl_av);
118
119struct FileLocation {
120 unsigned line;
Josh Gaobf8a2852016-05-27 11:59:09 -0700121 unsigned column;
Josh Gaobf8a2852016-05-27 11:59:09 -0700122
Josh Gaobfb6bae2016-07-15 17:25:21 -0700123 bool operator<(const FileLocation& rhs) const {
124 return std::tie(line, column) < std::tie(rhs.line, rhs.column);
Josh Gaobf8a2852016-05-27 11:59:09 -0700125 }
126
Josh Gaobfb6bae2016-07-15 17:25:21 -0700127 bool operator==(const FileLocation& rhs) const {
128 return std::tie(line, column) == std::tie(rhs.line, rhs.column);
Josh Gao173e7c02016-06-03 13:38:00 -0700129 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700130};
131
Josh Gaobfb6bae2016-07-15 17:25:21 -0700132struct Location {
133 std::string filename;
134 FileLocation start;
135 FileLocation end;
136
137 bool operator<(const Location& rhs) const {
138 return std::tie(filename, start, end) < std::tie(rhs.filename, rhs.start, rhs.end);
139 }
140};
141
142std::string to_string(const Location& loc);
143
Josh Gaobf8a2852016-05-27 11:59:09 -0700144struct Declaration {
Josh Gao16057882016-08-02 14:54:09 -0700145 std::string name;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700146 Location location;
Josh Gaobf8a2852016-05-27 11:59:09 -0700147
Josh Gaobfb6bae2016-07-15 17:25:21 -0700148 bool is_extern;
149 bool is_definition;
150 std::map<CompilationType, DeclarationAvailability> availability;
151
152 bool calculateAvailability(DeclarationAvailability* output) const;
153 bool operator<(const Declaration& rhs) const {
154 return location < rhs.location;
Josh Gaobf8a2852016-05-27 11:59:09 -0700155 }
156
Josh Gaobfb6bae2016-07-15 17:25:21 -0700157 void dump(const std::string& base_path = "", std::ostream& out = std::cout,
158 unsigned indent = 0) const {
159 std::string indent_str(indent, ' ');
160 out << indent_str;
Josh Gaobf8a2852016-05-27 11:59:09 -0700161
Josh Gaobfb6bae2016-07-15 17:25:21 -0700162 if (is_extern) {
163 out << "extern";
164 } else {
165 out << "static";
166 }
167
168 if (is_definition) {
169 out << " definition";
170 } else {
171 out << " declaration";
172 }
173
174 out << " @ " << StripPrefix(location.filename, base_path).str() << ":" << location.start.line
175 << ":" << location.start.column;
176
177 if (!availability.empty()) {
178 DeclarationAvailability avail;
179
180 out << "\n" << indent_str << " ";
181 if (!calculateAvailability(&avail)) {
182 out << "invalid availability";
183 } else {
184 out << to_string(avail);
185 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700186 }
187 }
188};
189
Josh Gaobfb6bae2016-07-15 17:25:21 -0700190struct Symbol {
191 std::string name;
192 std::map<Location, Declaration> declarations;
Josh Gaobf8a2852016-05-27 11:59:09 -0700193
Josh Gaobfb6bae2016-07-15 17:25:21 -0700194 bool calculateAvailability(DeclarationAvailability* output) const;
195 bool hasDeclaration(const CompilationType& type) const;
Josh Gaobf8a2852016-05-27 11:59:09 -0700196
Josh Gaobfb6bae2016-07-15 17:25:21 -0700197 bool operator<(const Symbol& rhs) const {
198 return name < rhs.name;
199 }
200
201 bool operator==(const Symbol& rhs) const {
202 return name == rhs.name;
203 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700204
205 void dump(const std::string& base_path = "", std::ostream& out = std::cout) const {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700206 DeclarationAvailability availability;
207 bool valid_availability = calculateAvailability(&availability);
208 out << " " << name << ": ";
209
210 if (valid_availability) {
211 out << to_string(availability);
212 } else {
213 out << "invalid";
214 }
215
216 out << "\n";
217
218 for (auto& it : declarations) {
219 it.second.dump(base_path, out, 4);
220 out << "\n";
221 }
222 }
223};
224
225class HeaderDatabase {
226 std::mutex mutex;
227
228 public:
229 std::map<std::string, Symbol> symbols;
230
231 void parseAST(CompilationType type, clang::ASTUnit* ast);
232
233 void dump(const std::string& base_path = "", std::ostream& out = std::cout) const {
234 out << "HeaderDatabase contains " << symbols.size() << " symbols:\n";
235 for (const auto& pair : symbols) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700236 pair.second.dump(base_path, out);
237 }
238 }
239};