blob: 057e4164cfd6cebdce2b740159b5d6b69325f153 [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>
21#include <set>
22#include <string>
23#include <vector>
24
25#include <llvm/ADT/StringRef.h>
26
27#include "Utils.h"
28
29enum class DeclarationType {
30 function,
31 variable,
32 inconsistent,
33};
34
35static const char* declarationTypeName(DeclarationType type) {
36 switch (type) {
37 case DeclarationType::function:
38 return "function";
39 case DeclarationType::variable:
40 return "variable";
41 case DeclarationType::inconsistent:
42 return "inconsistent";
43 }
44}
45
46struct CompilationType {
47 std::string arch;
48 int api_level;
49
50 private:
51 auto tie() const {
52 return std::tie(arch, api_level);
53 }
54
55 public:
56 bool operator<(const CompilationType& other) const {
57 return tie() < other.tie();
58 }
59
60 bool operator==(const CompilationType& other) const {
61 return tie() == other.tie();
62 }
63
64 std::string describe() const {
65 return arch + "-" + std::to_string(api_level);
66 }
67};
68
69struct DeclarationAvailability {
70 int introduced = 0;
71 int deprecated = 0;
72 int obsoleted = 0;
73
74 void dump(std::ostream& out = std::cout) const {
75 bool need_comma = false;
76 auto comma = [&out, &need_comma]() {
77 if (!need_comma) {
78 need_comma = true;
79 return;
80 }
81 out << ", ";
82 };
83
84 if (introduced != 0) {
85 comma();
86 out << "introduced = " << introduced;
87 }
88 if (deprecated != 0) {
89 comma();
90 out << "deprecated = " << deprecated;
91 }
92 if (obsoleted != 0) {
93 comma();
94 out << "obsoleted = " << obsoleted;
95 }
96 }
97
98 bool empty() const {
99 return !(introduced || deprecated || obsoleted);
100 }
101
102 auto tie() const {
103 return std::tie(introduced, deprecated, obsoleted);
104 }
105
106 bool operator==(const DeclarationAvailability& rhs) const {
107 return this->tie() == rhs.tie();
108 }
109
110 bool operator!=(const DeclarationAvailability& rhs) const {
111 return !(*this == rhs);
112 }
113
114 std::string describe() const {
115 return std::string("[") + std::to_string(introduced) + "," + std::to_string(deprecated) + "," +
116 std::to_string(obsoleted) + "]";
117 }
118};
119
120struct DeclarationLocation {
121 std::string filename;
122 unsigned line_number;
123 unsigned column;
124 DeclarationType type;
125 bool is_extern;
126 bool is_definition;
127 DeclarationAvailability availability;
128
129 auto tie() const {
130 return std::tie(filename, line_number, column, type, is_extern, is_definition);
131 }
132
133 bool operator<(const DeclarationLocation& other) const {
134 return tie() < other.tie();
135 }
136
137 bool operator==(const DeclarationLocation& other) const {
138 return tie() == other.tie();
139 }
140};
141
142struct Declaration {
143 std::string name;
144 std::set<DeclarationLocation> locations;
145
146 bool hasDefinition() const {
147 for (const auto& location : locations) {
148 if (location.is_definition) {
149 return true;
150 }
151 }
152 return false;
153 }
154
155 DeclarationType type() const {
156 DeclarationType result = locations.begin()->type;
157 for (const DeclarationLocation& location : locations) {
158 if (location.type != result) {
159 result = DeclarationType::inconsistent;
160 }
161 }
162 return result;
163 }
164
165 void dump(const std::string& base_path = "", std::ostream& out = std::cout) const {
166 out << " " << name << " declared in " << locations.size() << " locations:\n";
167 for (const DeclarationLocation& location : locations) {
168 const char* var_type = declarationTypeName(location.type);
169 const char* declaration_type = location.is_definition ? "definition" : "declaration";
170 const char* linkage = location.is_extern ? "extern" : "static";
171
172 std::string filename;
173 if (llvm::StringRef(location.filename).startswith(base_path)) {
174 filename = location.filename.substr(base_path.size());
175 } else {
176 filename = location.filename;
177 }
178
179 out << " " << linkage << " " << var_type << " " << declaration_type << " @ "
180 << filename << ":" << location.line_number << ":" << location.column;
181
182 if (!location.availability.empty()) {
183 out << "\t[";
184 location.availability.dump(out);
185 out << "]";
186 } else {
187 out << "\t[no availability]";
188 }
189
190 out << "\n";
191 }
192 }
193};
194
195namespace clang {
196class ASTUnit;
197}
198
199class HeaderDatabase {
200 public:
201 std::map<std::string, Declaration> declarations;
202
203 void parseAST(clang::ASTUnit* ast);
204
205 void dump(const std::string& base_path = "", std::ostream& out = std::cout) const {
206 out << "HeaderDatabase contains " << declarations.size() << " declarations:\n";
207 for (const auto& pair : declarations) {
208 pair.second.dump(base_path, out);
209 }
210 }
211};