| 1 | //===-- COFFDirectiveParser.cpp - JITLink coff directive parser --*- C++ -*===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // MSVC COFF directive parser |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "COFFDirectiveParser.h" |
| 14 | |
| 15 | using namespace llvm; |
| 16 | using namespace jitlink; |
| 17 | |
| 18 | #define DEBUG_TYPE "jitlink" |
| 19 | |
| 20 | using namespace llvm::opt; |
| 21 | #define OPTTABLE_CODE |
| 22 | #include "COFFOptions.inc" |
| 23 | |
| 24 | class COFFOptTable : public opt::OptTable { |
| 25 | public: |
| 26 | COFFOptTable() : OptTable(optionTables(), true) {} |
| 27 | }; |
| 28 | |
| 29 | static COFFOptTable optTable; |
| 30 | |
| 31 | Expected<opt::InputArgList> COFFDirectiveParser::parse(StringRef Str) { |
| 32 | SmallVector<StringRef, 16> Tokens; |
| 33 | SmallVector<const char *, 16> Buffer; |
| 34 | cl::TokenizeWindowsCommandLineNoCopy(Source: Str, Saver&: saver, NewArgv&: Tokens); |
| 35 | for (StringRef Tok : Tokens) { |
| 36 | bool HasNul = Tok.end() != Str.end() && Tok.data()[Tok.size()] == '\0'; |
| 37 | Buffer.push_back(Elt: HasNul ? Tok.data() : saver.save(S: Tok).data()); |
| 38 | } |
| 39 | |
| 40 | unsigned missingIndex; |
| 41 | unsigned missingCount; |
| 42 | |
| 43 | auto Result = optTable.ParseArgs(Args: Buffer, MissingArgIndex&: missingIndex, MissingArgCount&: missingCount); |
| 44 | |
| 45 | if (missingCount) |
| 46 | return make_error<JITLinkError>(Args: Twine("COFF directive parsing failed: ") + |
| 47 | Result.getArgString(Index: missingIndex) + |
| 48 | " missing argument"); |
| 49 | LLVM_DEBUG({ |
| 50 | for (auto *arg : Result.filtered(COFF_OPT_UNKNOWN)) |
| 51 | dbgs() << "Unknown coff option argument: "<< arg->getAsString(Result) |
| 52 | << "\n"; |
| 53 | }); |
| 54 | return std::move(Result); |
| 55 | } |
| 56 |