xref: /llvm-project/llvm/lib/ExecutionEngine/JITLink/COFFDirectiveParser.cpp (revision e953ae5bbc313fd0cc980ce021d487e5b5199ea4)
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 #include <array>
16 
17 using namespace llvm;
18 using namespace jitlink;
19 
20 #define DEBUG_TYPE "jitlink"
21 
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"
28 #undef PREFIX
29 
30 // Create table mapping all options defined in COFFOptions.td
31 static constexpr opt::OptTable::Info infoTable[] = {
32 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
33   {X1,                                                                         \
34    X2,                                                                         \
35    X10,                                                                        \
36    X11,                                                                        \
37    COFF_OPT_##ID,                                                              \
38    opt::Option::KIND##Class,                                                   \
39    X9,                                                                         \
40    X8,                                                                         \
41    COFF_OPT_##GROUP,                                                           \
42    COFF_OPT_##ALIAS,                                                           \
43    X7,                                                                         \
44    X12},
45 #include "COFFOptions.inc"
46 #undef OPTION
47 };
48 
49 class COFFOptTable : public opt::OptTable {
50 public:
51   COFFOptTable() : OptTable(infoTable, true) {}
52 };
53 
54 static COFFOptTable optTable;
55 
56 Expected<opt::InputArgList> COFFDirectiveParser::parse(StringRef Str) {
57   SmallVector<StringRef, 16> Tokens;
58   SmallVector<const char *, 16> Buffer;
59   cl::TokenizeWindowsCommandLineNoCopy(Str, saver, Tokens);
60   for (StringRef Tok : Tokens) {
61     bool HasNul = Tok.end() != Str.end() && Tok.data()[Tok.size()] == '\0';
62     Buffer.push_back(HasNul ? Tok.data() : saver.save(Tok).data());
63   }
64 
65   unsigned missingIndex;
66   unsigned missingCount;
67 
68   auto Result = optTable.ParseArgs(Buffer, missingIndex, missingCount);
69 
70   if (missingCount)
71     return make_error<JITLinkError>(Twine("COFF directive parsing failed: ") +
72                                     Result.getArgString(missingIndex) +
73                                     " missing argument");
74   LLVM_DEBUG({
75     for (auto *arg : Result.filtered(COFF_OPT_UNKNOWN))
76       dbgs() << "Unknown coff option argument: " << arg->getAsString(Result)
77              << "\n";
78   });
79   return std::move(Result);
80 }
81