|
| 1 | +//===--- swift-scan-test.cpp - Test libSwiftScan Dylib --------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// A simple program to test libSwiftScan interfaces. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#include "swift-c/DependencyScan/DependencyScan.h" |
| 18 | +#include "swift/Basic/Defer.h" |
| 19 | +#include "swift/Basic/FileTypes.h" |
| 20 | +#include "llvm/Support/Allocator.h" |
| 21 | +#include "llvm/Support/CommandLine.h" |
| 22 | +#include "llvm/Support/StringSaver.h" |
| 23 | + |
| 24 | +using namespace llvm; |
| 25 | + |
| 26 | +namespace { |
| 27 | +enum Actions { |
| 28 | + compute_cache_key, |
| 29 | + cache_query, |
| 30 | + replay_result, |
| 31 | +}; |
| 32 | + |
| 33 | +llvm::cl::OptionCategory Category("swift-scan-test Options"); |
| 34 | +llvm::cl::opt<std::string> CASPath("cas-path", llvm::cl::desc("<path>"), |
| 35 | + llvm::cl::cat(Category)); |
| 36 | +llvm::cl::opt<std::string> CASID("id", llvm::cl::desc("<casid>"), |
| 37 | + llvm::cl::cat(Category)); |
| 38 | +llvm::cl::opt<std::string> Input("input", llvm::cl::desc("<file>"), |
| 39 | + llvm::cl::cat(Category)); |
| 40 | +llvm::cl::opt<Actions> |
| 41 | + Action("action", llvm::cl::desc("<action>"), |
| 42 | + llvm::cl::values(clEnumVal(compute_cache_key, "compute cache key"), |
| 43 | + clEnumVal(cache_query, "cache query"), |
| 44 | + clEnumVal(replay_result, "replay result")), |
| 45 | + llvm::cl::cat(Category)); |
| 46 | +llvm::cl::list<std::string> |
| 47 | + SwiftCommands(llvm::cl::Positional, llvm::cl::desc("<swift-frontend args>"), |
| 48 | + llvm::cl::cat(Category)); |
| 49 | + |
| 50 | +} // namespace |
| 51 | + |
| 52 | +StringRef toString(swiftscan_string_ref_t str) { |
| 53 | + return StringRef((const char *)str.data, str.length); |
| 54 | +} |
| 55 | + |
| 56 | +int printError(swiftscan_string_ref_t err) { |
| 57 | + llvm::errs() << toString(err) << "\n"; |
| 58 | + swiftscan_string_dispose(err); |
| 59 | + return EXIT_FAILURE; |
| 60 | +} |
| 61 | + |
| 62 | +int action_compute_cache_key(swiftscan_cas_t cas, StringRef input, |
| 63 | + std::vector<const char *> &Args) { |
| 64 | + if (input.empty()) { |
| 65 | + llvm::errs() << "-input is not specified for compute_cache_key\n"; |
| 66 | + return EXIT_FAILURE; |
| 67 | + } |
| 68 | + |
| 69 | + swiftscan_string_ref_t err_msg; |
| 70 | + auto key = swiftscan_cache_compute_key(cas, Args.size(), Args.data(), |
| 71 | + input.str().c_str(), &err_msg); |
| 72 | + if (key.length == 0) |
| 73 | + return printError(err_msg); |
| 74 | + |
| 75 | + llvm::outs() << toString(key) << "\n"; |
| 76 | + swiftscan_string_dispose(key); |
| 77 | + |
| 78 | + return EXIT_SUCCESS; |
| 79 | +} |
| 80 | + |
| 81 | +static swift::file_types::ID |
| 82 | +getFileTypeFromScanOutputKind(swiftscan_output_kind_t kind) { |
| 83 | + switch (kind) { |
| 84 | + case SWIFTSCAN_OUTPUT_TYPE_OBJECT: |
| 85 | + return swift::file_types::ID::TY_Object; |
| 86 | + case SWIFTSCAN_OUTPUT_TYPE_SWIFTMODULE: |
| 87 | + return swift::file_types::ID::TY_SwiftModuleFile; |
| 88 | + case SWIFTSCAN_OUTPUT_TYPE_SWIFTINTERFACE: |
| 89 | + return swift::file_types::ID::TY_SwiftModuleInterfaceFile; |
| 90 | + case SWIFTSCAN_OUTPUT_TYPE_SWIFTPRIVATEINTERFACE: |
| 91 | + return swift::file_types::ID::TY_PrivateSwiftModuleInterfaceFile; |
| 92 | + case SWIFTSCAN_OUTPUT_TYPE_CLANG_MODULE: |
| 93 | + return swift::file_types::ID::TY_ClangModuleFile; |
| 94 | + case SWIFTSCAN_OUTPUT_TYPE_CLANG_PCH: |
| 95 | + return swift::file_types::ID::TY_PCH; |
| 96 | + case SWIFTSCAN_OUTPUT_TYPE_CLANG_HEADER: |
| 97 | + return swift::file_types::ID::TY_ClangHeader; |
| 98 | + case SWIFTSCAN_OUTPUT_TYPE_SWIFT_SOURCE_INFO: |
| 99 | + return swift::file_types::ID::TY_SwiftSourceInfoFile; |
| 100 | + case SWIFTSCAN_OUTPUT_TYPE_SWIFT_MODULE_DOC: |
| 101 | + return swift::file_types::ID::TY_SwiftModuleDocFile; |
| 102 | + case SWIFTSCAN_OUTPUT_TYPE_DEPENDENCIES: |
| 103 | + return swift::file_types::ID::TY_Dependencies; |
| 104 | + case SWIFTSCAN_OUTPUT_TYPE_SWIFT_DEPS: |
| 105 | + return swift::file_types::ID::TY_SwiftDeps; |
| 106 | + case SWIFTSCAN_OUTPUT_TYPE_MODULE_TRACE: |
| 107 | + return swift::file_types::ID::TY_ModuleTrace; |
| 108 | + case SWIFTSCAN_OUTPUT_TYPE_TBD: |
| 109 | + return swift::file_types::ID::TY_TBD; |
| 110 | + case SWIFTSCAN_OUTPUT_TYPE_SWIFT_MODULE_SUMMARY: |
| 111 | + return swift::file_types::ID::TY_SwiftModuleSummaryFile; |
| 112 | + case SWIFTSCAN_OUTPUT_TYPE_SWIFT_ABI_DESCRIPTOR: |
| 113 | + return swift::file_types::ID::TY_SwiftABIDescriptor; |
| 114 | + case SWIFTSCAN_OUTPUT_TYPE_SWIFT_API_DESCRIPTOR: |
| 115 | + return swift::file_types::ID::TY_SwiftAPIDescriptor; |
| 116 | + case SWIFTSCAN_OUTPUT_TYPE_CONST_VALUE: |
| 117 | + return swift::file_types::ID::TY_ConstValues; |
| 118 | + case SWIFTSCAN_OUTPUT_TYPE_MODULE_SEMANTIC_INFO: |
| 119 | + return swift::file_types::ID::TY_ModuleSemanticInfo; |
| 120 | + case SWIFTSCAN_OUTPUT_TYPE_YAML_OPT_RECORD: |
| 121 | + return swift::file_types::ID::TY_YAMLOptRecord; |
| 122 | + case SWIFTSCAN_OUTPUT_TYPE_BITSTREAM_OPT_RECORD: |
| 123 | + return swift::file_types::ID::TY_BitstreamOptRecord; |
| 124 | + case SWIFTSCAN_OUTPUT_TYPE_CACHED_DIAGNOSTICS: |
| 125 | + return swift::file_types::ID::TY_CachedDiagnostics; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +int print_cached_compilation(swiftscan_cached_compilation_t comp, |
| 130 | + const char *key) { |
| 131 | + swiftscan_string_ref_t err_msg; |
| 132 | + auto numOutput = swiftscan_cache_get_num_cached_outputs(comp, &err_msg); |
| 133 | + if (err_msg.length != 0) { |
| 134 | + printError(err_msg); |
| 135 | + return EXIT_FAILURE; |
| 136 | + } |
| 137 | + |
| 138 | + llvm::outs() << "Cached Compilation for key \"" << key << "\" has " |
| 139 | + << numOutput << " outputs: "; |
| 140 | + |
| 141 | + for (unsigned enumValue = 0; enumValue <= SWIFTSCAN_OUTPUT_TYPE_LAST; |
| 142 | + ++enumValue) { |
| 143 | + swiftscan_output_kind_t kind = |
| 144 | + static_cast<swiftscan_output_kind_t>(enumValue); |
| 145 | + bool hasOutput = |
| 146 | + swiftscan_cache_compilation_has_output_kind(comp, kind, &err_msg); |
| 147 | + if (err_msg.length != 0) |
| 148 | + return printError(err_msg); |
| 149 | + |
| 150 | + if (hasOutput) { |
| 151 | + llvm::outs() << "(" |
| 152 | + << swift::file_types::getTypeName( |
| 153 | + getFileTypeFromScanOutputKind(kind)) |
| 154 | + << ") "; |
| 155 | + } |
| 156 | + } |
| 157 | + llvm::outs() << "\n"; |
| 158 | + return EXIT_SUCCESS; |
| 159 | +} |
| 160 | + |
| 161 | +int action_cache_query(swiftscan_cas_t cas, const char *key) { |
| 162 | + swiftscan_string_ref_t err_msg; |
| 163 | + auto comp = swiftscan_cache_query(cas, key, /*globally=*/false, &err_msg); |
| 164 | + if (err_msg.length != 0) |
| 165 | + return printError(err_msg); |
| 166 | + |
| 167 | + if (!comp) { |
| 168 | + llvm::errs() << "cached output not found for \"" << key << "\n"; |
| 169 | + return EXIT_FAILURE; |
| 170 | + } |
| 171 | + |
| 172 | + SWIFT_DEFER { swiftscan_cached_compilation_dispose(comp); }; |
| 173 | + auto result = swiftscan_cache_load_cached_compilation(cas, comp, &err_msg); |
| 174 | + if (result == SWIFTSCAN_CACHE_RESULT_NOT_FOUND) { |
| 175 | + llvm::errs() << "failed to load output from cache key \"" << key << "\"\n"; |
| 176 | + return EXIT_FAILURE; |
| 177 | + } |
| 178 | + if (result == SWIFTSCAN_CACHE_RESULT_ERROR) |
| 179 | + return printError(err_msg); |
| 180 | + |
| 181 | + assert(result == SWIFTSCAN_CACHE_RESULT_SUCCESS && "unexpected result"); |
| 182 | + return print_cached_compilation(comp, key); |
| 183 | +} |
| 184 | + |
| 185 | +int action_replay_result(swiftscan_cas_t cas, const char *key, |
| 186 | + std::vector<const char *> &Args) { |
| 187 | + swiftscan_string_ref_t err_msg; |
| 188 | + auto comp = swiftscan_cache_query(cas, key, /*globally=*/false, &err_msg); |
| 189 | + if (!comp) |
| 190 | + return printError(err_msg); |
| 191 | + |
| 192 | + SWIFT_DEFER { swiftscan_cached_compilation_dispose(comp); }; |
| 193 | + auto load = swiftscan_cache_load_cached_compilation(cas, comp, &err_msg); |
| 194 | + if (load == SWIFTSCAN_CACHE_RESULT_NOT_FOUND) { |
| 195 | + llvm::errs() << "failed to load output from cache key \"" << key << "\"\n"; |
| 196 | + return EXIT_FAILURE; |
| 197 | + } |
| 198 | + if (load == SWIFTSCAN_CACHE_RESULT_ERROR) |
| 199 | + return printError(err_msg); |
| 200 | + |
| 201 | + assert(load == SWIFTSCAN_CACHE_RESULT_SUCCESS && "unexpected result"); |
| 202 | + |
| 203 | + auto instance = swiftscan_cache_replay_instance_create(Args.size(), |
| 204 | + Args.data(), &err_msg); |
| 205 | + if (!instance) |
| 206 | + return printError(err_msg); |
| 207 | + SWIFT_DEFER { swiftscan_cache_replay_instance_dispose(instance); }; |
| 208 | + |
| 209 | + auto result = |
| 210 | + swiftscan_cache_replay_compilation(cas, instance, comp, &err_msg); |
| 211 | + if (result == SWIFTSCAN_CACHE_RESULT_NOT_FOUND) { |
| 212 | + llvm::errs() << "failed to load output from cache key \"" << key << "\"\n"; |
| 213 | + return EXIT_FAILURE; |
| 214 | + } |
| 215 | + if (result == SWIFTSCAN_CACHE_RESULT_ERROR) |
| 216 | + return printError(err_msg); |
| 217 | + |
| 218 | + assert(result == SWIFTSCAN_CACHE_RESULT_SUCCESS && "unexpected result"); |
| 219 | + return EXIT_SUCCESS; |
| 220 | +} |
| 221 | + |
| 222 | +std::vector<const char *> createArgs(ArrayRef<std::string> Cmd, |
| 223 | + StringSaver &Saver) { |
| 224 | + if (!Cmd.empty() && StringRef(Cmd.front()).endswith("swift-frontend")) |
| 225 | + Cmd = Cmd.drop_front(); |
| 226 | + |
| 227 | + std::vector<const char *> Args; |
| 228 | + for (auto A : Cmd) { |
| 229 | + StringRef Arg = Saver.save(A); |
| 230 | + Args.push_back(Arg.data()); |
| 231 | + } |
| 232 | + |
| 233 | + return Args; |
| 234 | +} |
| 235 | + |
| 236 | +int main(int argc, char *argv[]) { |
| 237 | + llvm::cl::HideUnrelatedOptions(Category); |
| 238 | + llvm::cl::ParseCommandLineOptions(argc, argv, |
| 239 | + "Test libSwiftScan interfaces\n"); |
| 240 | + |
| 241 | + // Create CAS. |
| 242 | + auto option = swiftscan_cas_options_create(); |
| 243 | + SWIFT_DEFER { swiftscan_cas_options_dispose(option); }; |
| 244 | + swiftscan_cas_options_set_ondisk_path(option, CASPath.c_str()); |
| 245 | + |
| 246 | + swiftscan_string_ref_t err_msg; |
| 247 | + auto cas = swiftscan_cas_create_from_options(option, &err_msg); |
| 248 | + if (!cas) |
| 249 | + return printError(err_msg); |
| 250 | + SWIFT_DEFER { swiftscan_cas_dispose(cas); }; |
| 251 | + |
| 252 | + // Convert commands. |
| 253 | + llvm::BumpPtrAllocator Alloc; |
| 254 | + llvm::StringSaver Saver(Alloc); |
| 255 | + auto Args = createArgs(SwiftCommands, Saver); |
| 256 | + |
| 257 | + switch (Action) { |
| 258 | + case compute_cache_key: |
| 259 | + return action_compute_cache_key(cas, Input, Args); |
| 260 | + case cache_query: |
| 261 | + return action_cache_query(cas, CASID.c_str()); |
| 262 | + case replay_result: |
| 263 | + return action_replay_result(cas, CASID.c_str(), Args); |
| 264 | + } |
| 265 | + |
| 266 | + return EXIT_SUCCESS; |
| 267 | +} |
0 commit comments