From bd18d4584aae0ed6f7111ef5713784cf29fe663f Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Sat, 8 Jan 2011 05:28:38 +0000 Subject: [PATCH] Add printf format string parsing support for ' prefix to format conversions (POSIX extension). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@123054 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Analysis/Analyses/FormatString.h | 9 +++++++-- lib/Analysis/PrintfFormatString.cpp | 6 +++++- test/Sema/format-strings.c | 6 ++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/include/clang/Analysis/Analyses/FormatString.h b/include/clang/Analysis/Analyses/FormatString.h index 280b1260ac..cc1d3427e7 100644 --- a/include/clang/Analysis/Analyses/FormatString.h +++ b/include/clang/Analysis/Analyses/FormatString.h @@ -379,6 +379,7 @@ using analyze_format_string::OptionalAmount; using analyze_format_string::OptionalFlag; class PrintfSpecifier : public analyze_format_string::FormatSpecifier { + OptionalFlag HasThousandsGrouping; // ''', POSIX extension. OptionalFlag IsLeftJustified; // '-' OptionalFlag HasPlusPrefix; // '+' OptionalFlag HasSpacePrefix; // ' ' @@ -388,8 +389,8 @@ class PrintfSpecifier : public analyze_format_string::FormatSpecifier { public: PrintfSpecifier() : FormatSpecifier(/* isPrintf = */ true), - IsLeftJustified("-"), HasPlusPrefix("+"), HasSpacePrefix(" "), - HasAlternativeForm("#"), HasLeadingZeroes("0") {} + HasThousandsGrouping("'"), IsLeftJustified("-"), HasPlusPrefix("+"), + HasSpacePrefix(" "), HasAlternativeForm("#"), HasLeadingZeroes("0") {} static PrintfSpecifier Parse(const char *beg, const char *end); @@ -397,6 +398,10 @@ public: void setConversionSpecifier(const PrintfConversionSpecifier &cs) { CS = cs; } + void setHasThousandsGrouping(const char *position) { + HasThousandsGrouping = true; + HasThousandsGrouping.setPosition(position); + } void setIsLeftJustified(const char *position) { IsLeftJustified = true; IsLeftJustified.setPosition(position); diff --git a/lib/Analysis/PrintfFormatString.cpp b/lib/Analysis/PrintfFormatString.cpp index ff688e07d3..d99de2215d 100644 --- a/lib/Analysis/PrintfFormatString.cpp +++ b/lib/Analysis/PrintfFormatString.cpp @@ -100,6 +100,10 @@ static PrintfSpecifierResult ParsePrintfSpecifier(FormatStringHandler &H, for ( ; I != E; ++I) { switch (*I) { default: hasMore = false; break; + case '\'': + // FIXME: POSIX specific. Always accept? + FS.setHasThousandsGrouping(I); + break; case '-': FS.setIsLeftJustified(I); break; case '+': FS.setHasPlusPrefix(I); break; case ' ': FS.setHasSpacePrefix(I); break; @@ -185,7 +189,7 @@ static PrintfSpecifierResult ParsePrintfSpecifier(FormatStringHandler &H, case 's': k = ConversionSpecifier::sArg; break; case 'u': k = ConversionSpecifier::uArg; break; case 'x': k = ConversionSpecifier::xArg; break; - // Mac OS X (unicode) specific + // POSIX specific. case 'C': k = ConversionSpecifier::CArg; break; case 'S': k = ConversionSpecifier::SArg; break; // Objective-C. diff --git a/test/Sema/format-strings.c b/test/Sema/format-strings.c index c51dafdae5..93b6eb52b2 100644 --- a/test/Sema/format-strings.c +++ b/test/Sema/format-strings.c @@ -331,3 +331,9 @@ void pr8641() { printf("%#x\n", 10); printf("%#X\n", 10); } + +void posix_extensions() { + // Test %'d, "thousands grouping". + // + printf("%'d\n", 123456789); // no-warning +} -- 2.40.0