blob: ac5de275217b0a4ee4943ccbd11360e3e6e51a7f [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 Gaobfb6bae2016-07-15 17:25:21 -070095 for (auto it : arch_availability) {
96 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 Gaobfb6bae2016-07-15 17:25:21 -0700145 Location location;
Josh Gaobf8a2852016-05-27 11:59:09 -0700146
Josh Gaobfb6bae2016-07-15 17:25:21 -0700147 bool is_extern;
148 bool is_definition;
149 std::map<CompilationType, DeclarationAvailability> availability;
150
151 bool calculateAvailability(DeclarationAvailability* output) const;
152 bool operator<(const Declaration& rhs) const {
153 return location < rhs.location;
Josh Gaobf8a2852016-05-27 11:59:09 -0700154 }
155
Josh Gaobfb6bae2016-07-15 17:25:21 -0700156 void dump(const std::string& base_path = "", std::ostream& out = std::cout,
157 unsigned indent = 0) const {
158 std::string indent_str(indent, ' ');
159 out << indent_str;
Josh Gaobf8a2852016-05-27 11:59:09 -0700160
Josh Gaobfb6bae2016-07-15 17:25:21 -0700161 if (is_extern) {
162 out << "extern";
163 } else {
164 out << "static";
165 }
166
167 if (is_definition) {
168 out << " definition";
169 } else {
170 out << " declaration";
171 }
172
173 out << " @ " << StripPrefix(location.filename, base_path).str() << ":" << location.start.line
174 << ":" << location.start.column;
175
176 if (!availability.empty()) {
177 DeclarationAvailability avail;
178
179 out << "\n" << indent_str << " ";
180 if (!calculateAvailability(&avail)) {
181 out << "invalid availability";
182 } else {
183 out << to_string(avail);
184 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700185 }
186 }
187};
188
Josh Gaobfb6bae2016-07-15 17:25:21 -0700189struct Symbol {
190 std::string name;
191 std::map<Location, Declaration> declarations;
Josh Gaobf8a2852016-05-27 11:59:09 -0700192
Josh Gaobfb6bae2016-07-15 17:25:21 -0700193 bool calculateAvailability(DeclarationAvailability* output) const;
194 bool hasDeclaration(const CompilationType& type) const;
Josh Gaobf8a2852016-05-27 11:59:09 -0700195
Josh Gaobfb6bae2016-07-15 17:25:21 -0700196 bool operator<(const Symbol& rhs) const {
197 return name < rhs.name;
198 }
199
200 bool operator==(const Symbol& rhs) const {
201 return name == rhs.name;
202 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700203
204 void dump(const std::string& base_path = "", std::ostream& out = std::cout) const {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700205 DeclarationAvailability availability;
206 bool valid_availability = calculateAvailability(&availability);
207 out << " " << name << ": ";
208
209 if (valid_availability) {
210 out << to_string(availability);
211 } else {
212 out << "invalid";
213 }
214
215 out << "\n";
216
217 for (auto& it : declarations) {
218 it.second.dump(base_path, out, 4);
219 out << "\n";
220 }
221 }
222};
223
224class HeaderDatabase {
225 std::mutex mutex;
226
227 public:
228 std::map<std::string, Symbol> symbols;
229
230 void parseAST(CompilationType type, clang::ASTUnit* ast);
231
232 void dump(const std::string& base_path = "", std::ostream& out = std::cout) const {
233 out << "HeaderDatabase contains " << symbols.size() << " symbols:\n";
234 for (const auto& pair : symbols) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700235 pair.second.dump(base_path, out);
236 }
237 }
238};