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