[llvm-rc] Allow specifying language with a leading 0x prefix
authorMartin Storsjö <martin@martin.st>
Mon, 2 Aug 2021 11:10:22 +0000 (14:10 +0300)
committerMartin Storsjö <martin@martin.st>
Thu, 5 Aug 2021 07:19:55 +0000 (10:19 +0300)
This option is always interpreted strictly as a hexadecimal string,
even if it has no prefix that indicates the number format, hence
the existing call to StringRef::getAsInteger(16, ...).

StringRef::getAsInteger(0, ...) consumes a leading "0x" prefix is
present, but when the radix is specified, the radix shouldn't
be included.

Both MS rc.exe and GNU windres accept the language with that
prefix.

Also allow specifying the codepage to llvm-windres with a different
radix, as GNU windres allows that (but MS rc.exe doesn't).

This fixes https://llvm.org/PR51295.

Differential Revision: https://reviews.llvm.org/D107263

llvm/test/tools/llvm-rc/codepage.test
llvm/test/tools/llvm-rc/language.test
llvm/tools/llvm-rc/llvm-rc.cpp

index 20639d4..a55764c 100644 (file)
@@ -4,6 +4,8 @@
 ; RUN: llvm-readobj %t.utf8.res | FileCheck %s --check-prefix=UTF8
 ; RUN: llvm-windres --no-preprocess --codepage 65001 %p/Inputs/utf8.rc %t.utf8.res
 ; RUN: llvm-readobj %t.utf8.res | FileCheck %s --check-prefix=UTF8
+; RUN: llvm-windres --no-preprocess --codepage 0xfde9 %p/Inputs/utf8.rc %t.utf8.res
+; RUN: llvm-readobj %t.utf8.res | FileCheck %s --check-prefix=UTF8
 
 ; UTF8:      Resource type (int): STRINGTABLE (ID 6)
 ; UTF8-NEXT: Resource name (int): 1
index 9960ae1..5393710 100644 (file)
@@ -6,6 +6,8 @@
 ; RUN: llvm-readobj %t.res | FileCheck %s
 ; RUN: llvm-windres --no-preprocess --language 40A %p/Inputs/language.rc %t.res
 ; RUN: llvm-readobj %t.res | FileCheck %s
+; RUN: llvm-windres --no-preprocess -l 0x40A %p/Inputs/language.rc %t.res
+; RUN: llvm-readobj %t.res | FileCheck %s
 
 ; CHECK:      Resource name (int): 1
 ; CHECK-NEXT: Data version:
index c7c93ae..6965b7f 100644 (file)
@@ -476,13 +476,14 @@ RcOptions parseWindresOptions(ArrayRef<const char *> ArgsArr,
   Opts.Params.CodePage = CpWin1252; // Different default
   if (InputArgs.hasArg(WINDRES_codepage)) {
     if (InputArgs.getLastArgValue(WINDRES_codepage)
-            .getAsInteger(10, Opts.Params.CodePage))
+            .getAsInteger(0, Opts.Params.CodePage))
       fatalError("Invalid code page: " +
                  InputArgs.getLastArgValue(WINDRES_codepage));
   }
   if (InputArgs.hasArg(WINDRES_language)) {
-    if (InputArgs.getLastArgValue(WINDRES_language)
-            .getAsInteger(16, Opts.LangId))
+    StringRef Val = InputArgs.getLastArgValue(WINDRES_language);
+    Val.consume_front_insensitive("0x");
+    if (Val.getAsInteger(16, Opts.LangId))
       fatalError("Invalid language id: " +
                  InputArgs.getLastArgValue(WINDRES_language));
   }
@@ -565,7 +566,9 @@ RcOptions parseRcOptions(ArrayRef<const char *> ArgsArr,
   }
   Opts.AppendNull = InputArgs.hasArg(OPT_add_null);
   if (InputArgs.hasArg(OPT_lang_id)) {
-    if (InputArgs.getLastArgValue(OPT_lang_id).getAsInteger(16, Opts.LangId))
+    StringRef Val = InputArgs.getLastArgValue(OPT_lang_id);
+    Val.consume_front_insensitive("0x");
+    if (Val.getAsInteger(16, Opts.LangId))
       fatalError("Invalid language id: " +
                  InputArgs.getLastArgValue(OPT_lang_id));
   }