Skip to content

Commit

Permalink
[Flang] Fix error messages on Windows.
Browse files Browse the repository at this point in the history
Flang uses positional arguments for `messages::say()`, such as "%1$s" which is only supported in MS Compilers with the `_*printf_p` form of the function. This uses a conditional macro to convert the existing `vsnprintf` used to the one needed in MS-World.

7 tests in D107575 rely on this change.

Reviewed By: Meinersbur

Differential Revision: https://reviews.llvm.org/D107654
  • Loading branch information
Ivan Zhechev authored and Leporacanthicus committed Aug 12, 2021
1 parent f999312 commit 35249cb
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions flang/lib/Parser/message.cpp
Expand Up @@ -38,14 +38,26 @@ void MessageFormattedText::Format(const MessageFixedText *text, ...) {
}
va_list ap;
va_start(ap, text);
#ifdef _MSC_VER
// Microsoft has a separate function for "positional arguments", which is
// used in some messages.
int need{_vsprintf_p(nullptr, 0, p, ap)};
#else
int need{vsnprintf(nullptr, 0, p, ap)};
#endif

CHECK(need >= 0);
char *buffer{
static_cast<char *>(std::malloc(static_cast<std::size_t>(need) + 1))};
CHECK(buffer);
va_end(ap);
va_start(ap, text);
#ifdef _MSC_VER
// Use positional argument variant of printf.
int need2{_vsprintf_p(buffer, need + 1, p, ap)};
#else
int need2{vsnprintf(buffer, need + 1, p, ap)};
#endif
CHECK(need2 == need);
va_end(ap);
string_ = buffer;
Expand Down

0 comments on commit 35249cb

Please sign in to comment.