From 4c6021995032a898fb0502d5d1fd2df37638e57b Mon Sep 17 00:00:00 2001 From: Tom Care Date: Fri, 18 Jun 2010 03:02:16 +0000 Subject: [PATCH] Printf format strings: Added some more tests and fixed some minor bugs. - Precision toStrings shouldn't print a dot when they have no value. - Length of char length modifier is now returned correctly. - Added several fixit tests. Note: fixit tests are currently broken due to a bug in HighlightRange. Marking as XFAIL for now. M test/Sema/format-strings-fixit.c M include/clang/Analysis/Analyses/PrintfFormatString.h M lib/Analysis/PrintfFormatString.cpp git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@106275 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../clang/Analysis/Analyses/PrintfFormatString.h | 6 ++++-- lib/Analysis/PrintfFormatString.cpp | 7 ++++--- test/Sema/format-strings-fixit.c | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/include/clang/Analysis/Analyses/PrintfFormatString.h b/include/clang/Analysis/Analyses/PrintfFormatString.h index 79500bb116..d907637d39 100644 --- a/include/clang/Analysis/Analyses/PrintfFormatString.h +++ b/include/clang/Analysis/Analyses/PrintfFormatString.h @@ -166,6 +166,7 @@ public: default: return 1; case AsLongLong: + case AsChar: return 2; case None: return 0; @@ -218,12 +219,13 @@ public: } const char *getStart() const { - return start; + // We include the . character if it is given. + return start - UsesDotPrefix; } unsigned getConstantLength() const { assert(hs == Constant); - return length; + return length + UsesDotPrefix; } ArgTypeResult getArgType(ASTContext &Ctx) const; diff --git a/lib/Analysis/PrintfFormatString.cpp b/lib/Analysis/PrintfFormatString.cpp index 951be17e2f..8ad1d21312 100644 --- a/lib/Analysis/PrintfFormatString.cpp +++ b/lib/Analysis/PrintfFormatString.cpp @@ -611,20 +611,21 @@ const char *LengthModifier::toString() const { //===----------------------------------------------------------------------===// void OptionalAmount::toString(llvm::raw_ostream &os) const { - if (UsesDotPrefix) - os << "."; - switch (hs) { case Invalid: case NotSpecified: return; case Arg: + if (UsesDotPrefix) + os << "."; if (usesPositionalArg()) os << "*" << getPositionalArgIndex() << "$"; else os << "*"; break; case Constant: + if (UsesDotPrefix) + os << "."; os << amt; break; } diff --git a/test/Sema/format-strings-fixit.c b/test/Sema/format-strings-fixit.c index 84f69f059e..f74ce4e81a 100644 --- a/test/Sema/format-strings-fixit.c +++ b/test/Sema/format-strings-fixit.c @@ -1,6 +1,9 @@ // RUN: cp %s %t // RUN: %clang_cc1 -pedantic -Wall -fixit %t || true // RUN: %clang_cc1 -fsyntax-only -pedantic -Wall -Werror %t +// XFAIL: * +// FIXME: Some of these tests currently fail due to a bug in the HighlightRange +// function in lib/Frontend/TextDiagnosticPrinter.cpp. /* This is a test of the various code modification hints that are provided as part of warning or extension diagnostics. All of the @@ -25,7 +28,19 @@ void test() { // Flag handling printf("%0+s", (unsigned) 31337); // flags should stay printf("%0f", "test"); // flag should be removed + printf("%#p", (void *) 0); // Positional arguments printf("%1$f:%2$.*3$f:%4$.*3$f\n", 1, 2, 3, 4); + + // Precision + printf("%10.5d", 1l); // (bug 7394) + printf("%.2c", 'a'); + + // Ignored flags + printf("%0-f", 1.23); + + // Bad length modifiers + printf("%hhs", "foo"); + printf("%1$zp", (void *)0); } -- 2.40.0