Skip to content

[llvm-windres] Change the interpretation of --preprocessor to match Binutils 2.36 #75391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions llvm/test/tools/llvm-rc/windres-preproc.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
; RUN: llvm-windres -### --include-dir %p/incdir1 --include %p/incdir2 "-DFOO1=\\\"foo bar\\\"" -UFOO2 -D FOO3 --preprocessor-arg "-DFOO4=\\\"baz baz\\\"" -DFOO5=\"bar\" %p/Inputs/empty.rc %t.res | FileCheck %s --check-prefix=CHECK1
; RUN: llvm-windres -### --include-dir %p/incdir1 --include %p/incdir2 "-DFOO1=\"foo bar\"" -UFOO2 -D FOO3 --preprocessor-arg "-DFOO4=\"baz baz\"" "-DFOO5=bar" %p/Inputs/empty.rc %t.res --use-temp-file | FileCheck %s --check-prefix=CHECK1
; CHECK1: {{^}} "clang" "--driver-mode=gcc" "-target" "{{.*}}-{{.*}}{{mingw32|windows-gnu}}" "-E" "-xc" "-DRC_INVOKED" "-I" "{{.*}}incdir1" "-I" "{{.*}}incdir2" "-D" "FOO1=\"foo bar\"" "-U" "FOO2" "-D" "FOO3" "-DFOO4=\"baz baz\"" "-D" "FOO5=bar" "{{.*}}empty.rc" "-o" "{{.*}}preproc-{{.*}}.rc"{{$}}
; RUN: llvm-windres -### --preprocessor "i686-w64-mingw32-gcc -E -DFOO=\\\"foo\\ bar\\\"" %p/Inputs/empty.rc %t.res | FileCheck %s --check-prefix=CHECK2
; CHECK2: {{^}} "{{.*}}i686-w64-mingw32-gcc" "-E" "-DFOO=\"foo bar\"" "{{.*}}empty.rc" "-o" "{{.*}}preproc-{{.*}}.rc"{{$}}
; RUN: llvm-windres -### --preprocessor "i686-w64-mingw32-gcc" --preprocessor-arg -E "-DFOO=\\\"foo bar\\\"" %p/Inputs/empty.rc %t.res | FileCheck %s --check-prefix=CHECK2
; CHECK2: {{^}} "{{.*}}i686-w64-mingw32-gcc" "-E" "-D" "FOO=\"foo bar\"" "{{.*}}empty.rc" "-o" "{{.*}}preproc-{{.*}}.rc"{{$}}

;; Test resolving the --preprocessor executable from PATH

Expand All @@ -22,3 +22,8 @@

; RUN: not llvm-windres --preprocessor intentionally-missing-executable %p/Inputs/empty.rc %t.res 2>&1 | FileCheck %s --check-prefix=CHECK4
; CHECK4: llvm-rc: Preprocessing failed: Executable "intentionally-missing-executable" doesn't exist!

;; Test --preprocessor with an argument with spaces.

; RUN: llvm-windres -### --preprocessor "path with spaces/gcc" %p/Inputs/empty.rc %t.res | FileCheck %s --check-prefix=CHECK5
; CHECK5: {{^}} "path with spaces/gcc" "{{.*}}empty.rc" "-o" "{{.*}}preproc-{{.*}}.rc"{{$}}
44 changes: 5 additions & 39 deletions llvm/tools/llvm-rc/llvm-rc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ struct RcOptions {
bool Preprocess = true;
bool PrintCmdAndExit = false;
std::string Triple;
std::vector<std::string> PreprocessCmd;
std::optional<std::string> Preprocessor;
std::vector<std::string> PreprocessArgs;

std::string InputFile;
Expand All @@ -229,7 +229,7 @@ struct RcOptions {
void preprocess(StringRef Src, StringRef Dst, const RcOptions &Opts,
const char *Argv0) {
std::string Clang;
if (Opts.PrintCmdAndExit || !Opts.PreprocessCmd.empty()) {
if (Opts.PrintCmdAndExit || Opts.Preprocessor) {
Clang = "clang";
} else {
ErrorOr<std::string> ClangOrErr = findClang(Argv0, Opts.Triple);
Expand All @@ -249,10 +249,9 @@ void preprocess(StringRef Src, StringRef Dst, const RcOptions &Opts,
Clang, "--driver-mode=gcc", "-target", Opts.Triple, "-E",
"-xc", "-DRC_INVOKED"};
std::string PreprocessorExecutable;
if (!Opts.PreprocessCmd.empty()) {
if (Opts.Preprocessor) {
Args.clear();
for (const auto &S : Opts.PreprocessCmd)
Args.push_back(S);
Args.push_back(*Opts.Preprocessor);
if (!sys::fs::can_execute(Args[0])) {
if (auto P = sys::findProgramByName(Args[0])) {
PreprocessorExecutable = *P;
Expand Down Expand Up @@ -342,36 +341,6 @@ std::string unescape(StringRef S) {
return Out;
}

std::vector<std::string> unescapeSplit(StringRef S) {
std::vector<std::string> OutArgs;
std::string Out;
bool InQuote = false;
for (int I = 0, E = S.size(); I < E; I++) {
if (S[I] == '\\') {
if (I + 1 < E)
Out.push_back(S[++I]);
else
fatalError("Unterminated escape");
continue;
}
if (S[I] == '"') {
InQuote = !InQuote;
continue;
}
if (S[I] == ' ' && !InQuote) {
OutArgs.push_back(Out);
Out.clear();
continue;
}
Out.push_back(S[I]);
}
if (InQuote)
fatalError("Unterminated quote");
if (!Out.empty())
OutArgs.push_back(Out);
return OutArgs;
}

RcOptions parseWindresOptions(ArrayRef<const char *> ArgsArr,
ArrayRef<const char *> InputArgsArray,
std::string Prefix) {
Expand Down Expand Up @@ -506,11 +475,8 @@ RcOptions parseWindresOptions(ArrayRef<const char *> ArgsArr,
break;
}
}
// TODO: If --use-temp-file is set, we shouldn't be unescaping
// the --preprocessor argument either, only splitting it.
if (InputArgs.hasArg(WINDRES_preprocessor))
Opts.PreprocessCmd =
unescapeSplit(InputArgs.getLastArgValue(WINDRES_preprocessor));
Opts.Preprocessor = InputArgs.getLastArgValue(WINDRES_preprocessor);

Opts.Params.CodePage = CpWin1252; // Different default
if (InputArgs.hasArg(WINDRES_codepage)) {
Expand Down