Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | dfb74c5 | 2016-10-24 12:53:17 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 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 | * |
Elliott Hughes | dfb74c5 | 2016-10-24 12:53:17 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 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 { |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame^] | 33 | class ASTContext; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 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; |
Josh Gao | a77b3a9 | 2016-08-15 16:39:27 -0700 | [diff] [blame] | 46 | int file_offset_bits; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 47 | |
| 48 | private: |
| 49 | auto tie() const { |
Josh Gao | a77b3a9 | 2016-08-15 16:39:27 -0700 | [diff] [blame] | 50 | return std::tie(arch, api_level, file_offset_bits); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | public: |
| 54 | bool operator<(const CompilationType& other) const { |
| 55 | return tie() < other.tie(); |
| 56 | } |
| 57 | |
| 58 | bool operator==(const CompilationType& other) const { |
| 59 | return tie() == other.tie(); |
| 60 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 63 | std::string to_string(const CompilationType& type); |
| 64 | |
| 65 | struct AvailabilityValues { |
| 66 | bool future = false; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 67 | int introduced = 0; |
| 68 | int deprecated = 0; |
| 69 | int obsoleted = 0; |
| 70 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 71 | bool empty() const { |
| 72 | return !(future || introduced || deprecated || obsoleted); |
Josh Gao | 173e7c0 | 2016-06-03 13:38:00 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 75 | bool operator==(const AvailabilityValues& rhs) const { |
| 76 | return std::tie(introduced, deprecated, obsoleted) == |
| 77 | std::tie(rhs.introduced, rhs.deprecated, rhs.obsoleted); |
| 78 | } |
| 79 | |
| 80 | bool operator!=(const AvailabilityValues& rhs) const { |
| 81 | return !(*this == rhs); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | std::string to_string(const AvailabilityValues& av); |
| 86 | |
| 87 | struct DeclarationAvailability { |
| 88 | AvailabilityValues global_availability; |
| 89 | ArchMap<AvailabilityValues> arch_availability; |
| 90 | |
Josh Gao | 173e7c0 | 2016-06-03 13:38:00 -0700 | [diff] [blame] | 91 | bool empty() const { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 92 | if (!global_availability.empty()) { |
| 93 | return false; |
| 94 | } |
Josh Gao | 173e7c0 | 2016-06-03 13:38:00 -0700 | [diff] [blame] | 95 | |
Josh Gao | 1605788 | 2016-08-02 14:54:09 -0700 | [diff] [blame] | 96 | for (const auto& it : arch_availability) { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 97 | if (!it.second.empty()) { |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return true; |
Josh Gao | 173e7c0 | 2016-06-03 13:38:00 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | bool operator==(const DeclarationAvailability& rhs) const { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 106 | return std::tie(global_availability, arch_availability) == |
| 107 | std::tie(rhs.global_availability, rhs.arch_availability); |
Josh Gao | 173e7c0 | 2016-06-03 13:38:00 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool operator!=(const DeclarationAvailability& rhs) const { |
| 111 | return !(*this == rhs); |
| 112 | } |
| 113 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 114 | // Returns false if the availability declarations conflict. |
| 115 | bool merge(const DeclarationAvailability& other); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 118 | std::string to_string(const DeclarationAvailability& decl_av); |
| 119 | |
| 120 | struct FileLocation { |
| 121 | unsigned line; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 122 | unsigned column; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 123 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 124 | bool operator<(const FileLocation& rhs) const { |
| 125 | return std::tie(line, column) < std::tie(rhs.line, rhs.column); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 128 | bool operator==(const FileLocation& rhs) const { |
| 129 | return std::tie(line, column) == std::tie(rhs.line, rhs.column); |
Josh Gao | 173e7c0 | 2016-06-03 13:38:00 -0700 | [diff] [blame] | 130 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 133 | struct Location { |
| 134 | std::string filename; |
| 135 | FileLocation start; |
| 136 | FileLocation end; |
| 137 | |
| 138 | bool operator<(const Location& rhs) const { |
| 139 | return std::tie(filename, start, end) < std::tie(rhs.filename, rhs.start, rhs.end); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | std::string to_string(const Location& loc); |
| 144 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 145 | struct Declaration { |
Josh Gao | 1605788 | 2016-08-02 14:54:09 -0700 | [diff] [blame] | 146 | std::string name; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 147 | Location location; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 148 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 149 | bool is_extern; |
| 150 | bool is_definition; |
Josh Gao | fff29fe | 2016-09-07 18:29:08 -0700 | [diff] [blame] | 151 | bool no_guard; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 152 | std::map<CompilationType, DeclarationAvailability> availability; |
| 153 | |
| 154 | bool calculateAvailability(DeclarationAvailability* output) const; |
| 155 | bool operator<(const Declaration& rhs) const { |
| 156 | return location < rhs.location; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 159 | 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] | 160 | std::string indent_str(indent, ' '); |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 161 | fprintf(out, "%s", indent_str.c_str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 162 | |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 163 | fprintf(out, "%s ", is_extern ? "extern" : "static"); |
| 164 | fprintf(out, "%s ", is_definition ? "definition" : "declaration"); |
Josh Gao | fff29fe | 2016-09-07 18:29:08 -0700 | [diff] [blame] | 165 | if (no_guard) { |
| 166 | fprintf(out, "no_guard "); |
| 167 | } |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 168 | fprintf(out, "@ %s:%u:%u", StripPrefix(location.filename, base_path).str().c_str(), |
| 169 | location.start.line, location.start.column); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 170 | |
| 171 | if (!availability.empty()) { |
| 172 | DeclarationAvailability avail; |
| 173 | |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 174 | fprintf(out, "\n%s ", indent_str.c_str()); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 175 | if (!calculateAvailability(&avail)) { |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 176 | fprintf(out, "invalid availability"); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 177 | } else { |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 178 | fprintf(out, "%s", to_string(avail).c_str()); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 179 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | }; |
| 183 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 184 | struct Symbol { |
| 185 | std::string name; |
| 186 | std::map<Location, Declaration> declarations; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 187 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 188 | bool calculateAvailability(DeclarationAvailability* output) const; |
| 189 | bool hasDeclaration(const CompilationType& type) const; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 190 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 191 | bool operator<(const Symbol& rhs) const { |
| 192 | return name < rhs.name; |
| 193 | } |
| 194 | |
| 195 | bool operator==(const Symbol& rhs) const { |
| 196 | return name == rhs.name; |
| 197 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 198 | |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 199 | void dump(const std::string& base_path = "", FILE* out = stdout) const { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 200 | DeclarationAvailability availability; |
| 201 | bool valid_availability = calculateAvailability(&availability); |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 202 | fprintf(out, " %s: ", name.c_str()); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 203 | |
| 204 | if (valid_availability) { |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 205 | fprintf(out, "%s\n", to_string(availability).c_str()); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 206 | } else { |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 207 | fprintf(out, "invalid\n"); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 210 | for (auto& it : declarations) { |
| 211 | it.second.dump(base_path, out, 4); |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 212 | fprintf(out, "\n"); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | class HeaderDatabase { |
| 218 | std::mutex mutex; |
| 219 | |
| 220 | public: |
| 221 | std::map<std::string, Symbol> symbols; |
| 222 | |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame^] | 223 | void parseAST(CompilationType type, clang::ASTContext& ast); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 224 | |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 225 | void dump(const std::string& base_path = "", FILE* out = stdout) const { |
| 226 | fprintf(out, "HeaderDatabase contains %zu symbols:\n", symbols.size()); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 227 | for (const auto& pair : symbols) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 228 | pair.second.dump(base_path, out); |
| 229 | } |
| 230 | } |
| 231 | }; |