Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 1 | /* |
| 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 Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 21 | #include <mutex> |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 22 | #include <set> |
Josh Gao | 173e7c0 | 2016-06-03 13:38:00 -0700 | [diff] [blame] | 23 | #include <sstream> |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include <llvm/ADT/StringRef.h> |
| 28 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 29 | #include "Arch.h" |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 30 | #include "Utils.h" |
| 31 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 32 | namespace clang { |
| 33 | class ASTUnit; |
| 34 | class Decl; |
| 35 | } |
| 36 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 37 | enum class DeclarationType { |
| 38 | function, |
| 39 | variable, |
| 40 | inconsistent, |
| 41 | }; |
| 42 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 43 | struct CompilationType { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 44 | Arch arch; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 45 | 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 Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 62 | std::string to_string(const CompilationType& type); |
| 63 | |
| 64 | struct AvailabilityValues { |
| 65 | bool future = false; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 66 | int introduced = 0; |
| 67 | int deprecated = 0; |
| 68 | int obsoleted = 0; |
| 69 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 70 | bool empty() const { |
| 71 | return !(future || introduced || deprecated || obsoleted); |
Josh Gao | 173e7c0 | 2016-06-03 13:38:00 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 74 | 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 | |
| 84 | std::string to_string(const AvailabilityValues& av); |
| 85 | |
| 86 | struct DeclarationAvailability { |
| 87 | AvailabilityValues global_availability; |
| 88 | ArchMap<AvailabilityValues> arch_availability; |
| 89 | |
Josh Gao | 173e7c0 | 2016-06-03 13:38:00 -0700 | [diff] [blame] | 90 | bool empty() const { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 91 | if (!global_availability.empty()) { |
| 92 | return false; |
| 93 | } |
Josh Gao | 173e7c0 | 2016-06-03 13:38:00 -0700 | [diff] [blame] | 94 | |
Josh Gao | 1605788 | 2016-08-02 14:54:09 -0700 | [diff] [blame^] | 95 | for (const auto& it : arch_availability) { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 96 | if (!it.second.empty()) { |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return true; |
Josh Gao | 173e7c0 | 2016-06-03 13:38:00 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | bool operator==(const DeclarationAvailability& rhs) const { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 105 | return std::tie(global_availability, arch_availability) == |
| 106 | std::tie(rhs.global_availability, rhs.arch_availability); |
Josh Gao | 173e7c0 | 2016-06-03 13:38:00 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | bool operator!=(const DeclarationAvailability& rhs) const { |
| 110 | return !(*this == rhs); |
| 111 | } |
| 112 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 113 | // Returns false if the availability declarations conflict. |
| 114 | bool merge(const DeclarationAvailability& other); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 117 | std::string to_string(const DeclarationAvailability& decl_av); |
| 118 | |
| 119 | struct FileLocation { |
| 120 | unsigned line; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 121 | unsigned column; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 122 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 123 | bool operator<(const FileLocation& rhs) const { |
| 124 | return std::tie(line, column) < std::tie(rhs.line, rhs.column); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 127 | bool operator==(const FileLocation& rhs) const { |
| 128 | return std::tie(line, column) == std::tie(rhs.line, rhs.column); |
Josh Gao | 173e7c0 | 2016-06-03 13:38:00 -0700 | [diff] [blame] | 129 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 132 | struct 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 | |
| 142 | std::string to_string(const Location& loc); |
| 143 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 144 | struct Declaration { |
Josh Gao | 1605788 | 2016-08-02 14:54:09 -0700 | [diff] [blame^] | 145 | std::string name; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 146 | Location location; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 147 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 148 | 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 Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 157 | 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 Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 161 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 162 | 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 Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | }; |
| 189 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 190 | struct Symbol { |
| 191 | std::string name; |
| 192 | std::map<Location, Declaration> declarations; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 193 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 194 | bool calculateAvailability(DeclarationAvailability* output) const; |
| 195 | bool hasDeclaration(const CompilationType& type) const; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 196 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 197 | 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 Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 204 | |
| 205 | void dump(const std::string& base_path = "", std::ostream& out = std::cout) const { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 206 | 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 | |
| 225 | class 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 Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 236 | pair.second.dump(base_path, out); |
| 237 | } |
| 238 | } |
| 239 | }; |