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 constexpr std::initializer_list<StringLiteral> NAME = VALUE; 25 #include "COFFOptions.inc" 26 #undef PREFIX 27 28 // Create table mapping all options defined in COFFOptions.td 29 static constexpr std::initializer_list<opt::OptTable::Info> infoTable = { 30 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \ 31 {X1, \ 32 X2, \ 33 X10, \ 34 X11, \ 35 COFF_OPT_##ID, \ 36 opt::Option::KIND##Class, \ 37 X9, \ 38 X8, \ 39 COFF_OPT_##GROUP, \ 40 COFF_OPT_##ALIAS, \ 41 X7, \ 42 X12}, 43 #include "COFFOptions.inc" 44 #undef OPTION 45 }; 46 47 class COFFOptTable : public opt::OptTable { 48 public: 49 COFFOptTable() : OptTable(infoTable, true) {} 50 }; 51 52 static COFFOptTable optTable; 53 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()); 61 } 62 63 unsigned missingIndex; 64 unsigned missingCount; 65 66 auto Result = optTable.ParseArgs(Buffer, missingIndex, missingCount); 67 68 if (missingCount) 69 return make_error<JITLinkError>(Twine("COFF directive parsing failed: ") + 70 Result.getArgString(missingIndex) + 71 " missing argument"); 72 LLVM_DEBUG({ 73 for (auto *arg : Result.filtered(COFF_OPT_UNKNOWN)) 74 dbgs() << "Unknown coff option argument: " << arg->getAsString(Result) 75 << "\n"; 76 }); 77 return std::move(Result); 78 } 79