blob: 8fe12eab4c2af14b68d05e538c336d62a9752521 [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
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 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 Gao566735d2016-08-02 15:07:32 -0700157 void dump(const std::string& base_path = "", FILE* out = stdout, unsigned indent = 0) const {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700158 std::string indent_str(indent, ' ');
Josh Gao566735d2016-08-02 15:07:32 -0700159 fprintf(out, "%s", indent_str.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700160
Josh Gao566735d2016-08-02 15:07:32 -0700161 fprintf(out, "%s ", is_extern ? "extern" : "static");
162 fprintf(out, "%s ", is_definition ? "definition" : "declaration");
163 fprintf(out, "@ %s:%u:%u", StripPrefix(location.filename, base_path).str().c_str(),
164 location.start.line, location.start.column);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700165
166 if (!availability.empty()) {
167 DeclarationAvailability avail;
168
Josh Gao566735d2016-08-02 15:07:32 -0700169 fprintf(out, "\n%s ", indent_str.c_str());
Josh Gaobfb6bae2016-07-15 17:25:21 -0700170 if (!calculateAvailability(&avail)) {
Josh Gao566735d2016-08-02 15:07:32 -0700171 fprintf(out, "invalid availability");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700172 } else {
Josh Gao566735d2016-08-02 15:07:32 -0700173 fprintf(out, "%s", to_string(avail).c_str());
Josh Gaobfb6bae2016-07-15 17:25:21 -0700174 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700175 }
176 }
177};
178
Josh Gaobfb6bae2016-07-15 17:25:21 -0700179struct Symbol {
180 std::string name;
181 std::map<Location, Declaration> declarations;
Josh Gaobf8a2852016-05-27 11:59:09 -0700182
Josh Gaobfb6bae2016-07-15 17:25:21 -0700183 bool calculateAvailability(DeclarationAvailability* output) const;
184 bool hasDeclaration(const CompilationType& type) const;
Josh Gaobf8a2852016-05-27 11:59:09 -0700185
Josh Gaobfb6bae2016-07-15 17:25:21 -0700186 bool operator<(const Symbol& rhs) const {
187 return name < rhs.name;
188 }
189
190 bool operator==(const Symbol& rhs) const {
191 return name == rhs.name;
192 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700193
Josh Gao566735d2016-08-02 15:07:32 -0700194 void dump(const std::string& base_path = "", FILE* out = stdout) const {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700195 DeclarationAvailability availability;
196 bool valid_availability = calculateAvailability(&availability);
Josh Gao566735d2016-08-02 15:07:32 -0700197 fprintf(out, " %s: ", name.c_str());
Josh Gaobfb6bae2016-07-15 17:25:21 -0700198
199 if (valid_availability) {
Josh Gao566735d2016-08-02 15:07:32 -0700200 fprintf(out, "%s\n", to_string(availability).c_str());
Josh Gaobfb6bae2016-07-15 17:25:21 -0700201 } else {
Josh Gao566735d2016-08-02 15:07:32 -0700202 fprintf(out, "invalid\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700203 }
204
Josh Gaobfb6bae2016-07-15 17:25:21 -0700205 for (auto& it : declarations) {
206 it.second.dump(base_path, out, 4);
Josh Gao566735d2016-08-02 15:07:32 -0700207 fprintf(out, "\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700208 }
209 }
210};
211
212class HeaderDatabase {
213 std::mutex mutex;
214
215 public:
216 std::map<std::string, Symbol> symbols;
217
218 void parseAST(CompilationType type, clang::ASTUnit* ast);
219
Josh Gao566735d2016-08-02 15:07:32 -0700220 void dump(const std::string& base_path = "", FILE* out = stdout) const {
221 fprintf(out, "HeaderDatabase contains %zu symbols:\n", symbols.size());
Josh Gaobfb6bae2016-07-15 17:25:21 -0700222 for (const auto& pair : symbols) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700223 pair.second.dump(base_path, out);
224 }
225 }
226};