Skip to content

printf format specifiers for different platforms #13937

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

Closed
rubenvb opened this issue Aug 9, 2012 · 5 comments
Closed

printf format specifiers for different platforms #13937

rubenvb opened this issue Aug 9, 2012 · 5 comments
Labels
bugzilla Issues migrated from bugzilla clang Clang issues not falling into any other category

Comments

@rubenvb
Copy link

rubenvb commented Aug 9, 2012

Bugzilla Link 13565
Resolution FIXED
Resolved on Jul 07, 2017 12:09
Version trunk
OS Windows NT
Blocks llvm/llvm-bugzilla-archive#13707
CC @compnerd,@erichkeane,@zmodem,@tritao,@belkadan,@tkremenek

Extended Description

Running this code through MinGW-w64 based Clang:
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include
int main(int argc,char **argv)
{
uint64_t val=1234567890;
printf("%" PRId64"\n",val);
exit(0);
}
gives following warning:
M:\Development\x64\test>clang++ -std=c++11 test.cpp -c -Wall
test.cpp:11:15: warning: invalid conversion specifier 'I'
[-Wformat-invalid-specifier]
printf("%" PRId64"\n",val);
~~~^
M:/Development/mingw64/bin/../lib/clang/3.1/../../../x86_64-w64-mingw32/include\inttypes.h:42:17: note:
expanded from macro 'PRId64'
#define PRId64 "I64d"
^
1 warning generated.

The PRId64 is defined to a valid msvcrt (Windows C runtime) format specifier, all of which are documented here: http://msdn.microsoft.com/en-us/library/56e442dc.aspx

I dug through the code, and ended up in these files:
http://llvm.org/svn/llvm-project/cfe/trunk/lib/Analysis/PrintfFormatString.cpp
http://llvm.org/svn/llvm-project/cfe/trunk/lib/Analysis/ScanfFormatString.cpp

Which I believe need a new "case" in the "ParsePrintfSpecifier" function, which would look somewhat like this:

case 'I':
    if(triplet.third == "win32" || triplet.third == "mingw32")
        k = ConversionSpecifier::???;
    break;

And perhaps similar modifications for the glibc (which would check for glibc being used) and objc (which should check for objc being used) conversion specifier. I do not know what ??? should be, nor if this well then be properly handled, so I'm leaving this to the experts.

I'll gladly test a patch for this, but I'm too stupid and C-agnostic to even understand what the function in question is doing.

Related mailing list discussion:
http://lists.cs.uiuc.edu/pipermail/cfe-dev/2012-August/023600.html

PS: for testing on Windows, download the GCC dw2 4.6.3-1 (http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/rubenvb/release/i686-w64-mingw32-gcc-dw2-4.6.3-1-release-win32_rubenvb.7z/download) and assorted Clang 3.1 (http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/rubenvb/release/i686-w64-mingw32-clang-3.1-release-win32_rubenvb.7z/download) and extract to the same directory. But as I said, I can take care of the testing myself.

@belkadan
Copy link
Contributor

belkadan commented Sep 6, 2012

The interesting bits (for when we actually do this):

Types:
%c/%s means wint_t/wchar_t* for wprintf
%C/%S means wint_t/wchar_t* for printf and int/char* for wprintf
%Z means UNICODE_STRING * (custom struct type)

http://msdn.microsoft.com/en-us/library/hf4y5e3w.aspx

Sizes:
%I32d/%I64d means __int32 or __int64
%Id means ptrdiff_t (or intptr_t)
%Iu means size_t (or uintptr_t)

%hc/%hC means char, and same for s and S
%lc/%lC means wchar_t, and same for s and S
%wc means wchar_t, and same for s

http://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx

@llvmbot
Copy link
Member

llvmbot commented Feb 13, 2014

*** Bug llvm/llvm-bugzilla-archive#15848 has been marked as a duplicate of this bug. ***

@erichkeane
Copy link
Collaborator

The "I" specifiers were added in -r188937.

W,c,C,z, etc done here: llvm/llvm-bugzilla-archive#20808

@tritao
Copy link
Mannequin

tritao mannequin commented Nov 26, 2021

mentioned in issue llvm/llvm-bugzilla-archive#13707

@llvmbot
Copy link
Member

llvmbot commented Nov 26, 2021

mentioned in issue llvm/llvm-bugzilla-archive#15848

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 3, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla clang Clang issues not falling into any other category
Projects
None yet
Development

No branches or pull requests

4 participants