Skip to content

Commit

Permalink
kvprintf %b enhancements
Browse files Browse the repository at this point in the history
Make the %b formatter accept number formatting flags.  It will now allow
formats like "%freebsd#20.2hhb" in order to print "0x03<F_FOO,F_BAR>   ".

This still doesn't support right-adjustment when given a width argument.
I was tempted to add that but as far as I could see it would have meant
either more code duplication or more refactoring of kvprintf, so I
decided to leave it alone for now.

reviewers: cem, rdivacky, rgrimes
subscribers: dim, markj
  • Loading branch information
rlibby committed Jun 5, 2017
1 parent 5651d12 commit 5195c67
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
2 changes: 2 additions & 0 deletions contrib/llvm/tools/clang/lib/Analysis/FormatString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const {
case ConversionSpecifier::XArg:
case ConversionSpecifier::nArg:
return true;
case ConversionSpecifier::FreeBSDbArg:
case ConversionSpecifier::FreeBSDrArg:
case ConversionSpecifier::FreeBSDyArg:
return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
Expand Down Expand Up @@ -739,6 +740,7 @@ bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const {
case ConversionSpecifier::ScanListArg:
case ConversionSpecifier::ZArg:
return true;
case ConversionSpecifier::FreeBSDbArg:
case ConversionSpecifier::FreeBSDrArg:
case ConversionSpecifier::FreeBSDyArg:
return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
Expand Down
2 changes: 2 additions & 0 deletions contrib/llvm/tools/clang/lib/Analysis/PrintfFormatString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ bool PrintfSpecifier::hasValidAlternativeForm() const {
case ConversionSpecifier::FArg:
case ConversionSpecifier::gArg:
case ConversionSpecifier::GArg:
case ConversionSpecifier::FreeBSDbArg:
case ConversionSpecifier::FreeBSDrArg:
case ConversionSpecifier::FreeBSDyArg:
return true;
Expand Down Expand Up @@ -958,6 +959,7 @@ bool PrintfSpecifier::hasValidPrecision() const {
case ConversionSpecifier::gArg:
case ConversionSpecifier::GArg:
case ConversionSpecifier::sArg:
case ConversionSpecifier::FreeBSDbArg:
case ConversionSpecifier::FreeBSDrArg:
case ConversionSpecifier::FreeBSDyArg:
case ConversionSpecifier::PArg:
Expand Down
53 changes: 29 additions & 24 deletions sys/kern/subr_prf.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_lis
uintmax_t num;
int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
int cflag, hflag, jflag, tflag, zflag;
int dwidth, upper;
int bconv, dwidth, upper;
char padc;
int stop = 0, retval = 0;

Expand All @@ -677,7 +677,7 @@ kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_lis
}
percent = fmt - 1;
qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
sign = 0; dot = 0; dwidth = 0; upper = 0;
sign = 0; dot = 0; bconv = 0; dwidth = 0; upper = 0;
cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
reswitch: switch (ch = (u_char)*fmt++) {
case '.':
Expand Down Expand Up @@ -725,28 +725,9 @@ reswitch: switch (ch = (u_char)*fmt++) {
width = n;
goto reswitch;
case 'b':
num = (u_int)va_arg(ap, int);
p = va_arg(ap, char *);
for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
PCHAR(*q--);

if (num == 0)
break;

for (tmp = 0; *p;) {
n = *p++;
if (num & (1 << (n - 1))) {
PCHAR(tmp ? ',' : '<');
for (; (n = *p) > ' '; ++p)
PCHAR(n);
tmp = 1;
} else
for (; *p > ' '; ++p)
continue;
}
if (tmp)
PCHAR('>');
break;
ladjust = 1;
bconv = 1;
goto handle_nosign;
case 'c':
width -= 1;

Expand Down Expand Up @@ -884,6 +865,10 @@ reswitch: switch (ch = (u_char)*fmt++) {
num = (u_char)va_arg(ap, int);
else
num = va_arg(ap, u_int);
if (bconv) {
q = va_arg(ap, char *);
base = *q++;
}
goto number;
handle_sign:
if (jflag)
Expand Down Expand Up @@ -941,6 +926,26 @@ reswitch: switch (ch = (u_char)*fmt++) {
while (*p)
PCHAR(*p--);

if (bconv && num != 0) {
/* %b conversion flag format. */
tmp = retval;
while (*q) {
n = *q++;
if (num & (1 << (n - 1))) {
PCHAR(retval == tmp ?
',' : '<');
for (; (n = *q) > ' '; ++q)
PCHAR(n);
} else
for (; *q > ' '; ++q)
continue;
}
if (retval != tmp) {
PCHAR('>');
width -= retval - tmp;
}
}

if (ladjust)
while (width-- > 0)
PCHAR(' ');
Expand Down

0 comments on commit 5195c67

Please sign in to comment.