|
| 1 | + |
| 2 | +#ifndef IPC_MANAGER_COMPILER_HPP |
| 3 | +#define IPC_MANAGER_COMPILER_HPP |
| 4 | + |
| 5 | +#include "clang/IPC2978/Manager.hpp" |
| 6 | +#include "clang/IPC2978/expected.hpp" |
| 7 | + |
| 8 | +struct CompilerTest; |
| 9 | +namespace N2978 |
| 10 | +{ |
| 11 | + |
| 12 | +enum class FileType : uint8_t |
| 13 | +{ |
| 14 | + MODULE, |
| 15 | + HEADER_UNIT, |
| 16 | + HEADER_FILE |
| 17 | +}; |
| 18 | + |
| 19 | +struct Response |
| 20 | +{ |
| 21 | + // if type == HEADER_FILE, then fileSize has no meaning |
| 22 | + BMIFile file; |
| 23 | + FileType type; |
| 24 | + bool user; |
| 25 | + Response(BMIFile file_, FileType type_, bool user_); |
| 26 | +}; |
| 27 | + |
| 28 | +// IPC Manager Compiler |
| 29 | +class IPCManagerCompiler : Manager |
| 30 | +{ |
| 31 | + friend struct ::CompilerTest; |
| 32 | + template <typename T> tl::expected<T, std::string> receiveMessage() const; |
| 33 | + // This is not exposed. sendCTBLastMessage calls this. |
| 34 | + [[nodiscard]] tl::expected<void, std::string> receiveBTCLastMessage() const; |
| 35 | + [[nodiscard]] tl::expected<BTCModule, std::string> receiveBTCModule(const CTBModule &moduleName); |
| 36 | + [[nodiscard]] tl::expected<BTCNonModule, std::string> receiveBTCNonModule(const CTBNonModule &nonModule); |
| 37 | + |
| 38 | + std::unordered_map<std::string, Response> responses; |
| 39 | + |
| 40 | + public: |
| 41 | + CTBLastMessage lastMessage{}; |
| 42 | +#ifdef _WIN32 |
| 43 | + explicit IPCManagerCompiler(void *hPipe_); |
| 44 | +#else |
| 45 | + explicit IPCManagerCompiler(int fdSocket_); |
| 46 | +#endif |
| 47 | + |
| 48 | + // For FileType::HEADER_FILE, it can return FileType::HEADER_UNIT, otherwise it will return the request |
| 49 | + // response. Either it will return from the cache or it will fetch it from the build-system |
| 50 | + [[nodiscard]] tl::expected<Response, std::string> findResponse(const std::string &logicalName, FileType type); |
| 51 | + [[nodiscard]] tl::expected<void, std::string> sendCTBLastMessage(const CTBLastMessage &lastMessage) const; |
| 52 | + [[nodiscard]] tl::expected<void, std::string> sendCTBLastMessage(const CTBLastMessage &lastMessage, |
| 53 | + const std::string &bmiFile, |
| 54 | + const std::string &filePath) const; |
| 55 | + static tl::expected<ProcessMappingOfBMIFile, std::string> readSharedMemoryBMIFile(const BMIFile &file); |
| 56 | + static tl::expected<void, std::string> closeBMIFileMapping(const ProcessMappingOfBMIFile &processMappingOfBMIFile); |
| 57 | + void closeConnection() const; |
| 58 | +}; |
| 59 | + |
| 60 | +template <typename T> tl::expected<T, std::string> IPCManagerCompiler::receiveMessage() const |
| 61 | +{ |
| 62 | + // Read from the pipe. |
| 63 | + char buffer[BUFFERSIZE]; |
| 64 | + uint32_t bytesRead; |
| 65 | + if (const auto &r = readInternal(buffer); !r) |
| 66 | + { |
| 67 | + return tl::unexpected(r.error()); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + bytesRead = *r; |
| 72 | + } |
| 73 | + |
| 74 | + uint32_t bytesProcessed = 0; |
| 75 | + |
| 76 | + if constexpr (std::is_same_v<T, BTCModule>) |
| 77 | + { |
| 78 | + const auto &r = readProcessMappingOfBMIFileFromPipe(buffer, bytesRead, bytesProcessed); |
| 79 | + if (!r) |
| 80 | + { |
| 81 | + return tl::unexpected(r.error()); |
| 82 | + } |
| 83 | + |
| 84 | + const auto &r2 = readBoolFromPipe(buffer, bytesRead, bytesProcessed); |
| 85 | + if (!r2) |
| 86 | + { |
| 87 | + return tl::unexpected(r2.error()); |
| 88 | + } |
| 89 | + |
| 90 | + const auto &r3 = readVectorOfModuleDepFromPipe(buffer, bytesRead, bytesProcessed); |
| 91 | + if (!r3) |
| 92 | + { |
| 93 | + return tl::unexpected(r3.error()); |
| 94 | + } |
| 95 | + |
| 96 | + BTCModule moduleFile; |
| 97 | + moduleFile.requested = *r; |
| 98 | + moduleFile.user = *r2; |
| 99 | + moduleFile.modDeps = *r3; |
| 100 | + |
| 101 | + if (bytesRead == bytesProcessed) |
| 102 | + { |
| 103 | + return moduleFile; |
| 104 | + } |
| 105 | + } |
| 106 | + else if constexpr (std::is_same_v<T, BTCNonModule>) |
| 107 | + { |
| 108 | + const auto &r = readBoolFromPipe(buffer, bytesRead, bytesProcessed); |
| 109 | + if (!r) |
| 110 | + { |
| 111 | + return tl::unexpected(r.error()); |
| 112 | + } |
| 113 | + |
| 114 | + const auto &r2 = readBoolFromPipe(buffer, bytesRead, bytesProcessed); |
| 115 | + if (!r2) |
| 116 | + { |
| 117 | + return tl::unexpected(r2.error()); |
| 118 | + } |
| 119 | + |
| 120 | + const auto &r3 = readStringFromPipe(buffer, bytesRead, bytesProcessed); |
| 121 | + if (!r3) |
| 122 | + { |
| 123 | + return tl::unexpected(r3.error()); |
| 124 | + } |
| 125 | + |
| 126 | + const auto &r4 = readUInt32FromPipe(buffer, bytesRead, bytesProcessed); |
| 127 | + if (!r4) |
| 128 | + { |
| 129 | + return tl::unexpected(r4.error()); |
| 130 | + } |
| 131 | + |
| 132 | + const auto &r5 = readVectorOfStringFromPipe(buffer, bytesRead, bytesProcessed); |
| 133 | + if (!r5) |
| 134 | + { |
| 135 | + return tl::unexpected(r5.error()); |
| 136 | + } |
| 137 | + |
| 138 | + const auto &r6 = readVectorOfHeaderFileFromPipe(buffer, bytesRead, bytesProcessed); |
| 139 | + if (!r6) |
| 140 | + { |
| 141 | + return tl::unexpected(r6.error()); |
| 142 | + } |
| 143 | + |
| 144 | + const auto &r7 = readVectorOfHuDepFromPipe(buffer, bytesRead, bytesProcessed); |
| 145 | + if (!r7) |
| 146 | + { |
| 147 | + return tl::unexpected(r7.error()); |
| 148 | + } |
| 149 | + |
| 150 | + BTCNonModule nonModule; |
| 151 | + nonModule.isHeaderUnit = *r; |
| 152 | + nonModule.user = *r2; |
| 153 | + nonModule.filePath = *r3; |
| 154 | + nonModule.fileSize = *r4; |
| 155 | + nonModule.logicalNames = *r5; |
| 156 | + nonModule.headerFiles = *r6; |
| 157 | + nonModule.huDeps = *r7; |
| 158 | + |
| 159 | + if (bytesRead == bytesProcessed) |
| 160 | + { |
| 161 | + return nonModule; |
| 162 | + } |
| 163 | + } |
| 164 | + else |
| 165 | + { |
| 166 | + static_assert(false && "Unknown type\n"); |
| 167 | + } |
| 168 | + |
| 169 | + if (bytesRead != bytesProcessed) |
| 170 | + { |
| 171 | + return tl::unexpected(getErrorString(bytesRead, bytesProcessed)); |
| 172 | + } |
| 173 | + std::string str = __FILE__; |
| 174 | + str += ':'; |
| 175 | + str += std::to_string(__LINE__); |
| 176 | + return tl::unexpected(getErrorString("N2978 IPC API internal error" + str)); |
| 177 | +} |
| 178 | +[[nodiscard]] tl::expected<IPCManagerCompiler, std::string> makeIPCManagerCompiler( |
| 179 | + std::string BMIIfHeaderUnitObjOtherwisePath); |
| 180 | +inline IPCManagerCompiler *managerCompiler; |
| 181 | +} // namespace N2978 |
| 182 | +#endif // IPC_MANAGER_COMPILER_HPP |
0 commit comments