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
15using namespace llvm;
16using namespace jitlink;
17
18#define DEBUG_TYPE "jitlink"
19
20#define OPTTABLE_STR_TABLE_CODE
21#include "COFFOptions.inc"
22#undef OPTTABLE_STR_TABLE_CODE
23
24#define OPTTABLE_PREFIXES_TABLE_CODE
25#include "COFFOptions.inc"
26#undef OPTTABLE_PREFIXES_TABLE_CODE
27
28#define OPTTABLE_PREFIXES_UNION_CODE
29#include "COFFOptions.inc"
30#undef OPTTABLE_PREFIXES_UNION_CODE
31
32// Create table mapping all options defined in COFFOptions.td
33using namespace llvm::opt;
34static constexpr opt::OptTable::Info infoTable[] = {
35#define OPTION(...) \
36 LLVM_CONSTRUCT_OPT_INFO_WITH_ID_PREFIX(COFF_OPT_, __VA_ARGS__),
37#include "COFFOptions.inc"
38#undef OPTION
39};
40
41class COFFOptTable : public opt::PrecomputedOptTable {
42public:
43 COFFOptTable()
44 : PrecomputedOptTable(OptionStrTable, OptionPrefixesTable, infoTable,
45 OptionPrefixesUnion, true) {}
46};
47
48static COFFOptTable optTable;
49
50Expected<opt::InputArgList> COFFDirectiveParser::parse(StringRef Str) {
51 SmallVector<StringRef, 16> Tokens;
52 SmallVector<const char *, 16> Buffer;
53 cl::TokenizeWindowsCommandLineNoCopy(Source: Str, Saver&: saver, NewArgv&: Tokens);
54 for (StringRef Tok : Tokens) {
55 bool HasNul = Tok.end() != Str.end() && Tok.data()[Tok.size()] == '\0';
56 Buffer.push_back(Elt: HasNul ? Tok.data() : saver.save(S: Tok).data());
57 }
58
59 unsigned missingIndex;
60 unsigned missingCount;
61
62 auto Result = optTable.ParseArgs(Args: Buffer, MissingArgIndex&: missingIndex, MissingArgCount&: missingCount);
63
64 if (missingCount)
65 return make_error<JITLinkError>(Args: Twine("COFF directive parsing failed: ") +
66 Result.getArgString(Index: missingIndex) +
67 " missing argument");
68 LLVM_DEBUG({
69 for (auto *arg : Result.filtered(COFF_OPT_UNKNOWN))
70 dbgs() << "Unknown coff option argument: " << arg->getAsString(Result)
71 << "\n";
72 });
73 return std::move(Result);
74}
75