| 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 |  | 
| Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 19 | #include <stdio.h> | 
|  | 20 |  | 
| Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 21 | #include <map> | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 22 | #include <mutex> | 
| Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 23 | #include <set> | 
|  | 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 | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 157 | void dump(const std::string& base_path = "", FILE* out = stdout, unsigned indent = 0) const { | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 158 | std::string indent_str(indent, ' '); | 
| Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 159 | fprintf(out, "%s", indent_str.c_str()); | 
| Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 160 |  | 
| Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 161 | 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 Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 165 |  | 
|  | 166 | if (!availability.empty()) { | 
|  | 167 | DeclarationAvailability avail; | 
|  | 168 |  | 
| Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 169 | fprintf(out, "\n%s  ", indent_str.c_str()); | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 170 | if (!calculateAvailability(&avail)) { | 
| Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 171 | fprintf(out, "invalid availability"); | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 172 | } else { | 
| Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 173 | fprintf(out, "%s", to_string(avail).c_str()); | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 174 | } | 
| Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 175 | } | 
|  | 176 | } | 
|  | 177 | }; | 
|  | 178 |  | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 179 | struct Symbol { | 
|  | 180 | std::string name; | 
|  | 181 | std::map<Location, Declaration> declarations; | 
| Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 182 |  | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 183 | bool calculateAvailability(DeclarationAvailability* output) const; | 
|  | 184 | bool hasDeclaration(const CompilationType& type) const; | 
| Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 185 |  | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 186 | 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 Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 193 |  | 
| Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 194 | void dump(const std::string& base_path = "", FILE* out = stdout) const { | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 195 | DeclarationAvailability availability; | 
|  | 196 | bool valid_availability = calculateAvailability(&availability); | 
| Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 197 | fprintf(out, "  %s: ", name.c_str()); | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 198 |  | 
|  | 199 | if (valid_availability) { | 
| Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 200 | fprintf(out, "%s\n", to_string(availability).c_str()); | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 201 | } else { | 
| Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 202 | fprintf(out, "invalid\n"); | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 203 | } | 
|  | 204 |  | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 205 | for (auto& it : declarations) { | 
|  | 206 | it.second.dump(base_path, out, 4); | 
| Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 207 | fprintf(out, "\n"); | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 208 | } | 
|  | 209 | } | 
|  | 210 | }; | 
|  | 211 |  | 
|  | 212 | class 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 Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 220 | void dump(const std::string& base_path = "", FILE* out = stdout) const { | 
|  | 221 | fprintf(out, "HeaderDatabase contains %zu symbols:\n", symbols.size()); | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 222 | for (const auto& pair : symbols) { | 
| Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 223 | pair.second.dump(base_path, out); | 
|  | 224 | } | 
|  | 225 | } | 
|  | 226 | }; |