1 //===-- COFFDirectiveParser.cpp - JITLink coff directive parser --*- C++ -*===//
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
7 //===----------------------------------------------------------------------===//
9 // MSVC COFF directive parser
11 //===----------------------------------------------------------------------===//
13 #include "COFFDirectiveParser.h"
18 using namespace jitlink
;
20 #define DEBUG_TYPE "jitlink"
22 // Create prefix string literals used in Options.td
23 #define PREFIX(NAME, VALUE) \
24 static constexpr StringLiteral NAME##_init[] = VALUE; \
25 static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
26 std::size(NAME##_init) - 1);
27 #include "COFFOptions.inc"
30 static constexpr const StringLiteral PrefixTable_init
[] =
31 #define PREFIX_UNION(VALUES) VALUES
32 #include "COFFOptions.inc"
35 static constexpr const ArrayRef
<StringLiteral
>
36 PrefixTable(PrefixTable_init
, std::size(PrefixTable_init
) - 1);
38 // Create table mapping all options defined in COFFOptions.td
39 using namespace llvm::opt
;
40 static constexpr opt::OptTable::Info infoTable
[] = {
42 LLVM_CONSTRUCT_OPT_INFO_WITH_ID_PREFIX(COFF_OPT_, __VA_ARGS__),
43 #include "COFFOptions.inc"
47 class COFFOptTable
: public opt::PrecomputedOptTable
{
49 COFFOptTable() : PrecomputedOptTable(infoTable
, PrefixTable
, true) {}
52 static COFFOptTable optTable
;
54 Expected
<opt::InputArgList
> COFFDirectiveParser::parse(StringRef Str
) {
55 SmallVector
<StringRef
, 16> Tokens
;
56 SmallVector
<const char *, 16> Buffer
;
57 cl::TokenizeWindowsCommandLineNoCopy(Str
, saver
, Tokens
);
58 for (StringRef Tok
: Tokens
) {
59 bool HasNul
= Tok
.end() != Str
.end() && Tok
.data()[Tok
.size()] == '\0';
60 Buffer
.push_back(HasNul
? Tok
.data() : saver
.save(Tok
).data());
63 unsigned missingIndex
;
64 unsigned missingCount
;
66 auto Result
= optTable
.ParseArgs(Buffer
, missingIndex
, missingCount
);
69 return make_error
<JITLinkError
>(Twine("COFF directive parsing failed: ") +
70 Result
.getArgString(missingIndex
) +
73 for (auto *arg
: Result
.filtered(COFF_OPT_UNKNOWN
))
74 dbgs() << "Unknown coff option argument: " << arg
->getAsString(Result
)
77 return std::move(Result
);