From 719e22d4f49c37dbfb4bd2fbcbea03c33ad1200a Mon Sep 17 00:00:00 2001 From: Marek Sokolowski Date: Thu, 10 Aug 2017 16:21:44 +0000 Subject: Add .rc scripts tokenizer. This extends the shell of llvm-rc tool with the ability of tokenization of the input files. Currently, ASCII and ASCII-compatible UTF-8 files are supported. Thanks to Nico Weber (thakis) for his original work in this area. Differential Revision: https://reviews.llvm.org/D35957 llvm-svn: 310621 --- llvm/tools/llvm-rc/llvm-rc.cpp | 53 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'llvm/tools/llvm-rc/llvm-rc.cpp') diff --git a/llvm/tools/llvm-rc/llvm-rc.cpp b/llvm/tools/llvm-rc/llvm-rc.cpp index aa97bf9c0d5..098daba14e2 100644 --- a/llvm/tools/llvm-rc/llvm-rc.cpp +++ b/llvm/tools/llvm-rc/llvm-rc.cpp @@ -1,4 +1,4 @@ -//===- llvm-rc.cpp - Compile .rc scripts into .res -------------*- C++ -*--===// +//===-- llvm-rc.cpp - Compile .rc scripts into .res -------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -12,6 +12,8 @@ // //===----------------------------------------------------------------------===// +#include "ResourceScriptToken.h" + #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" #include "llvm/Support/Error.h" @@ -60,6 +62,12 @@ public: }; static ExitOnError ExitOnErr; + +LLVM_ATTRIBUTE_NORETURN static void fatalError(Twine Message) { + errs() << Message << "\n"; + exit(1); +} + } // anonymous namespace int main(int argc_, const char *argv_[]) { @@ -81,8 +89,49 @@ int main(int argc_, const char *argv_[]) { opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC); // The tool prints nothing when invoked with no command-line arguments. - if (InputArgs.hasArg(OPT_HELP)) + if (InputArgs.hasArg(OPT_HELP)) { T.PrintHelp(outs(), "rc", "Resource Converter", false); + return 0; + } + + const bool BeVerbose = InputArgs.hasArg(OPT_VERBOSE); + + std::vector InArgsInfo = InputArgs.getAllArgValues(OPT_INPUT); + if (InArgsInfo.size() != 1) { + fatalError("Exactly one input file should be provided."); + } + + // Read and tokenize the input file. + const Twine &Filename = InArgsInfo[0]; + ErrorOr> File = MemoryBuffer::getFile(Filename); + if (!File) { + fatalError("Error opening file '" + Filename + + "': " + File.getError().message()); + } + + std::unique_ptr FileContents = std::move(*File); + StringRef Contents = FileContents->getBuffer(); + + std::vector Tokens = ExitOnErr(tokenizeRC(Contents)); + + if (BeVerbose) { + const Twine TokenNames[] = { +#define TOKEN(Name) #Name, +#define SHORT_TOKEN(Name, Ch) #Name, +#include "ResourceScriptTokenList.h" +#undef TOKEN +#undef SHORT_TOKEN + }; + + for (const RCToken &Token : Tokens) { + outs() << TokenNames[static_cast(Token.kind())] << ": " + << Token.value(); + if (Token.kind() == RCToken::Kind::Int) + outs() << "; int value = " << Token.intValue(); + + outs() << "\n"; + } + } return 0; } -- cgit v1.2.3