base: Style fixes in cprintf related files.

Fix braces, capitalization, missing line breaks, and remove mostly
useless "using namespace std"s.

Change-Id: I1210dd3b918e7cba8e576cbbdf47aa986d9278e6
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/39535
Reviewed-by: Gabe Black <gabe.black@gmail.com>
Maintainer: Gabe Black <gabe.black@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Gabe Black
2021-01-20 21:38:47 -08:00
parent 998d5d86dd
commit ee0fdc948a
3 changed files with 262 additions and 205 deletions

View File

@@ -28,33 +28,32 @@
#include "base/cprintf.hh"
#include <cassert>
#include <iomanip>
#include <iostream>
#include <sstream>
#include "base/compiler.hh"
#include "base/logging.hh"
using namespace std;
namespace cp {
namespace cp
{
Print::Print(std::ostream &stream, const std::string &format)
: stream(stream), format(format.c_str()), ptr(format.c_str()), cont(false)
{
saved_flags = stream.flags();
saved_fill = stream.fill();
saved_precision = stream.precision();
saved_width = stream.width();
savedFlags = stream.flags();
savedFill = stream.fill();
savedPrecision = stream.precision();
savedWidth = stream.width();
}
Print::Print(std::ostream &stream, const char *format)
: stream(stream), format(format), ptr(format), cont(false)
{
saved_flags = stream.flags();
saved_fill = stream.fill();
saved_precision = stream.precision();
saved_width = stream.width();
savedFlags = stream.flags();
savedFill = stream.fill();
savedPrecision = stream.precision();
savedWidth = stream.width();
}
Print::~Print()
@@ -72,7 +71,7 @@ Print::process()
switch (*ptr) {
case '%':
if (ptr[1] != '%') {
process_flag();
processFlag();
return;
}
stream.put('%');
@@ -80,13 +79,13 @@ Print::process()
break;
case '\n':
stream << endl;
stream << std::endl;
++ptr;
break;
case '\r':
++ptr;
if (*ptr != '\n')
stream << endl;
stream << std::endl;
break;
default:
@@ -99,7 +98,7 @@ Print::process()
}
void
Print::process_flag()
Print::processFlag()
{
bool done = false;
bool end_number = false;
@@ -107,24 +106,25 @@ Print::process_flag()
int number = 0;
stream.fill(' ');
stream.flags((ios::fmtflags)0);
stream.flags((std::ios::fmtflags)0);
while (!done) {
++ptr;
if (*ptr >= '0' && *ptr <= '9') {
if (end_number)
continue;
} else if (number > 0)
} else if (number > 0) {
end_number = true;
}
switch (*ptr) {
case 's':
fmt.format = Format::string;
fmt.format = Format::String;
done = true;
break;
case 'c':
fmt.format = Format::character;
fmt.format = Format::Character;
done = true;
break;
@@ -132,9 +132,9 @@ Print::process_flag()
continue;
case 'p':
fmt.format = Format::integer;
fmt.base = Format::hex;
fmt.alternate_form = true;
fmt.format = Format::Integer;
fmt.base = Format::Hex;
fmt.alternateForm = true;
done = true;
break;
@@ -142,21 +142,21 @@ Print::process_flag()
fmt.uppercase = true;
M5_FALLTHROUGH;
case 'x':
fmt.base = Format::hex;
fmt.format = Format::integer;
fmt.base = Format::Hex;
fmt.format = Format::Integer;
done = true;
break;
case 'o':
fmt.base = Format::oct;
fmt.format = Format::integer;
fmt.base = Format::Oct;
fmt.format = Format::Integer;
done = true;
break;
case 'd':
case 'i':
case 'u':
fmt.format = Format::integer;
fmt.format = Format::Integer;
done = true;
break;
@@ -164,8 +164,8 @@ Print::process_flag()
fmt.uppercase = true;
M5_FALLTHROUGH;
case 'g':
fmt.format = Format::floating;
fmt.float_format = Format::best;
fmt.format = Format::Floating;
fmt.floatFormat = Format::Best;
done = true;
break;
@@ -173,14 +173,14 @@ Print::process_flag()
fmt.uppercase = true;
M5_FALLTHROUGH;
case 'e':
fmt.format = Format::floating;
fmt.float_format = Format::scientific;
fmt.format = Format::Floating;
fmt.floatFormat = Format::Scientific;
done = true;
break;
case 'f':
fmt.format = Format::floating;
fmt.float_format = Format::fixed;
fmt.format = Format::Floating;
fmt.floatFormat = Format::Fixed;
done = true;
break;
@@ -190,19 +190,19 @@ Print::process_flag()
break;
case '#':
fmt.alternate_form = true;
fmt.alternateForm = true;
break;
case '-':
fmt.flush_left = true;
fmt.flushLeft = true;
break;
case '+':
fmt.print_sign = true;
fmt.printSign = true;
break;
case ' ':
fmt.blank_space = true;
fmt.blankSpace = true;
break;
case '.':
@@ -215,7 +215,7 @@ Print::process_flag()
case '0':
if (number == 0) {
fmt.fill_zero = true;
fmt.fillZero = true;
break;
}
M5_FALLTHROUGH;
@@ -233,13 +233,13 @@ Print::process_flag()
case '*':
if (have_precision)
fmt.get_precision = true;
fmt.getPrecision = true;
else
fmt.get_width = true;
fmt.getWidth = true;
break;
case '%':
assert(false && "we shouldn't get here");
panic("we shouldn't get here");
break;
default:
@@ -258,13 +258,13 @@ Print::process_flag()
}
if (done) {
if ((fmt.format == Format::integer) && have_precision) {
if ((fmt.format == Format::Integer) && have_precision) {
// specified a . but not a float, set width
fmt.width = fmt.precision;
// precision requries digits for width, must fill with 0
fmt.fill_zero = true;
} else if ((fmt.format == Format::floating) && !have_precision &&
fmt.fill_zero) {
fmt.fillZero = true;
} else if ((fmt.format == Format::Floating) && !have_precision &&
fmt.fillZero) {
// ambiguous case, matching printf
fmt.precision = fmt.width;
}
@@ -275,7 +275,7 @@ Print::process_flag()
}
void
Print::end_args()
Print::endArgs()
{
size_t len;
@@ -290,13 +290,13 @@ Print::end_args()
break;
case '\n':
stream << endl;
stream << std::endl;
++ptr;
break;
case '\r':
++ptr;
if (*ptr != '\n')
stream << endl;
stream << std::endl;
break;
default:
@@ -307,10 +307,10 @@ Print::end_args()
}
}
stream.flags(saved_flags);
stream.fill(saved_fill);
stream.precision(saved_precision);
stream.width(saved_width);
stream.flags(savedFlags);
stream.fill(savedFill);
stream.precision(savedPrecision);
stream.width(savedWidth);
}
} // namespace cp

View File

@@ -47,14 +47,14 @@ struct Print
const char *ptr;
bool cont;
std::ios::fmtflags saved_flags;
char saved_fill;
int saved_precision;
int saved_width;
std::ios::fmtflags savedFlags;
char savedFill;
int savedPrecision;
int savedWidth;
Format fmt;
void process();
void process_flag();
void processFlag();
public:
Print(std::ostream &stream, const std::string &format);
@@ -62,54 +62,54 @@ struct Print
~Print();
int
get_number(int data)
getNumber(int data)
{
return data;
}
template <typename T>
int
get_number(const T& data)
getNumber(const T& data)
{
return 0;
}
template <typename T>
void
add_arg(const T &data)
addArg(const T &data)
{
if (!cont)
process();
if (fmt.get_width) {
fmt.get_width = false;
if (fmt.getWidth) {
fmt.getWidth = false;
cont = true;
fmt.width = get_number(data);
fmt.width = getNumber(data);
return;
}
if (fmt.get_precision) {
fmt.get_precision = false;
if (fmt.getPrecision) {
fmt.getPrecision = false;
cont = true;
fmt.precision = get_number(data);
fmt.precision = getNumber(data);
return;
}
switch (fmt.format) {
case Format::character:
format_char(stream, data, fmt);
case Format::Character:
formatChar(stream, data, fmt);
break;
case Format::integer:
format_integer(stream, data, fmt);
case Format::Integer:
formatInteger(stream, data, fmt);
break;
case Format::floating:
format_float(stream, data, fmt);
case Format::Floating:
formatFloat(stream, data, fmt);
break;
case Format::string:
format_string(stream, data, fmt);
case Format::String:
formatString(stream, data, fmt);
break;
default:
@@ -118,7 +118,7 @@ struct Print
}
}
void end_args();
void endArgs();
};
} // namespace cp
@@ -126,14 +126,14 @@ struct Print
inline void
ccprintf(cp::Print &print)
{
print.end_args();
print.endArgs();
}
template<typename T, typename ...Args> void
ccprintf(cp::Print &print, const T &value, const Args &...args)
{
print.add_arg(value);
print.addArg(value);
ccprintf(print, args...);
}

View File

@@ -33,104 +33,119 @@
#include <ostream>
#include <sstream>
namespace cp {
namespace cp
{
struct Format
{
bool alternate_form;
bool flush_left;
bool print_sign;
bool blank_space;
bool fill_zero;
bool alternateForm;
bool flushLeft;
bool printSign;
bool blankSpace;
bool fillZero;
bool uppercase;
enum { dec, hex, oct } base;
enum { none, string, integer, character, floating } format;
enum { best, fixed, scientific } float_format;
enum
{
Dec,
Hex,
Oct
} base;
enum
{
None,
String,
Integer,
Character,
Floating
} format;
enum
{
Best,
Fixed,
Scientific
} floatFormat;
int precision;
int width;
bool get_precision;
bool get_width;
bool getPrecision;
bool getWidth;
Format() { clear(); }
void clear()
void
clear()
{
alternate_form = false;
flush_left = false;
print_sign = false;
blank_space = false;
fill_zero = false;
alternateForm = false;
flushLeft = false;
printSign = false;
blankSpace = false;
fillZero = false;
uppercase = false;
base = dec;
format = none;
float_format = best;
base = Dec;
format = None;
floatFormat = Best;
precision = -1;
width = 0;
get_precision = false;
get_width = false;
getPrecision = false;
getWidth = false;
}
};
template <typename T>
inline void
_format_char(std::ostream &out, const T &data, Format &fmt)
static inline void
_formatChar(std::ostream &out, const T &data, Format &fmt)
{
using namespace std;
out << data;
}
template <typename T>
inline void
_format_integer(std::ostream &out, const T &data, Format &fmt)
static inline void
_formatInteger(std::ostream &out, const T &data, Format &fmt)
{
using namespace std;
ios::fmtflags flags(out.flags());
std::ios::fmtflags flags(out.flags());
switch (fmt.base) {
case Format::hex:
case Format::Hex:
out.setf(std::ios::hex, std::ios::basefield);
break;
case Format::oct:
case Format::Oct:
out.setf(std::ios::oct, std::ios::basefield);
break;
case Format::dec:
case Format::Dec:
out.setf(std::ios::dec, std::ios::basefield);
break;
}
if (fmt.alternate_form) {
if (!fmt.fill_zero)
if (fmt.alternateForm) {
if (!fmt.fillZero) {
out.setf(std::ios::showbase);
else {
} else {
switch (fmt.base) {
case Format::hex:
case Format::Hex:
out << "0x";
fmt.width -= 2;
break;
case Format::oct:
case Format::Oct:
out << "0";
fmt.width -= 1;
break;
case Format::dec:
case Format::Dec:
break;
}
}
}
if (fmt.fill_zero)
if (fmt.fillZero)
out.fill('0');
if (fmt.width > 0)
out.width(fmt.width);
if (fmt.flush_left && !fmt.fill_zero)
if (fmt.flushLeft && !fmt.fillZero)
out.setf(std::ios::left);
if (fmt.print_sign)
if (fmt.printSign)
out.setf(std::ios::showpos);
if (fmt.uppercase)
@@ -142,18 +157,16 @@ _format_integer(std::ostream &out, const T &data, Format &fmt)
}
template <typename T>
inline void
_format_float(std::ostream &out, const T &data, Format &fmt)
static inline void
_formatFloat(std::ostream &out, const T &data, Format &fmt)
{
using namespace std;
std::ios::fmtflags flags(out.flags());
ios::fmtflags flags(out.flags());
if (fmt.fill_zero)
if (fmt.fillZero)
out.fill('0');
switch (fmt.float_format) {
case Format::scientific:
switch (fmt.floatFormat) {
case Format::Scientific:
if (fmt.precision != -1) {
if (fmt.width > 0)
out.width(fmt.width);
@@ -164,24 +177,24 @@ _format_float(std::ostream &out, const T &data, Format &fmt)
out.setf(std::ios::scientific);
out.precision(fmt.precision);
} else
if (fmt.width > 0)
out.width(fmt.width);
} else if (fmt.width > 0) {
out.width(fmt.width);
}
if (fmt.uppercase)
out.setf(std::ios::uppercase);
break;
case Format::fixed:
case Format::Fixed:
if (fmt.precision != -1) {
if (fmt.width > 0)
out.width(fmt.width);
out.setf(std::ios::fixed);
out.precision(fmt.precision);
} else
if (fmt.width > 0)
out.width(fmt.width);
} else if (fmt.width > 0) {
out.width(fmt.width);
}
break;
@@ -201,8 +214,8 @@ _format_float(std::ostream &out, const T &data, Format &fmt)
}
template <typename T>
inline void
_format_string(std::ostream &out, const T &data, Format &fmt)
static inline void
_formatString(std::ostream &out, const T &data, Format &fmt)
{
if (fmt.width > 0) {
std::stringstream foo;
@@ -214,7 +227,7 @@ _format_string(std::ostream &out, const T &data, Format &fmt)
std::memset(spaces, ' ', fmt.width - flen);
spaces[fmt.width - flen] = 0;
if (fmt.flush_left)
if (fmt.flushLeft)
out << foo.str() << spaces;
else
out << spaces << foo.str();
@@ -235,100 +248,144 @@ _format_string(std::ostream &out, const T &data, Format &fmt)
// character formats
//
template <typename T>
inline void
format_char(std::ostream &out, const T &data, Format &fmt)
{ out << "<bad arg type for char format>"; }
static inline void
formatChar(std::ostream &out, const T &data, Format &fmt)
{
out << "<bad arg type for char format>";
}
inline void
format_char(std::ostream &out, char data, Format &fmt)
{ _format_char(out, data, fmt); }
static inline void
formatChar(std::ostream &out, char data, Format &fmt)
{
_formatChar(out, data, fmt);
}
inline void
format_char(std::ostream &out, unsigned char data, Format &fmt)
{ _format_char(out, data, fmt); }
static inline void
formatChar(std::ostream &out, unsigned char data, Format &fmt)
{
_formatChar(out, data, fmt);
}
inline void
format_char(std::ostream &out, signed char data, Format &fmt)
{ _format_char(out, data, fmt); }
static inline void
formatChar(std::ostream &out, signed char data, Format &fmt)
{
_formatChar(out, data, fmt);
}
inline void
format_char(std::ostream &out, short data, Format &fmt)
{ _format_char(out, (char)data, fmt); }
static inline void
formatChar(std::ostream &out, short data, Format &fmt)
{
_formatChar(out, (char)data, fmt);
}
inline void
format_char(std::ostream &out, unsigned short data, Format &fmt)
{ _format_char(out, (char)data, fmt); }
static inline void
formatChar(std::ostream &out, unsigned short data, Format &fmt)
{
_formatChar(out, (char)data, fmt);
}
inline void
format_char(std::ostream &out, int data, Format &fmt)
{ _format_char(out, (char)data, fmt); }
static inline void
formatChar(std::ostream &out, int data, Format &fmt)
{
_formatChar(out, (char)data, fmt);
}
inline void
format_char(std::ostream &out, unsigned int data, Format &fmt)
{ _format_char(out, (char)data, fmt); }
static inline void
formatChar(std::ostream &out, unsigned int data, Format &fmt)
{
_formatChar(out, (char)data, fmt);
}
inline void
format_char(std::ostream &out, long data, Format &fmt)
{ _format_char(out, (char)data, fmt); }
static inline void
formatChar(std::ostream &out, long data, Format &fmt)
{
_formatChar(out, (char)data, fmt);
}
inline void
format_char(std::ostream &out, unsigned long data, Format &fmt)
{ _format_char(out, (char)data, fmt); }
static inline void
formatChar(std::ostream &out, unsigned long data, Format &fmt)
{
_formatChar(out, (char)data, fmt);
}
inline void
format_char(std::ostream &out, long long data, Format &fmt)
{ _format_char(out, (char)data, fmt); }
static inline void
formatChar(std::ostream &out, long long data, Format &fmt)
{
_formatChar(out, (char)data, fmt);
}
inline void
format_char(std::ostream &out, unsigned long long data, Format &fmt)
{ _format_char(out, (char)data, fmt); }
static inline void
formatChar(std::ostream &out, unsigned long long data, Format &fmt)
{
_formatChar(out, (char)data, fmt);
}
//
// integer formats
//
template <typename T>
inline void
format_integer(std::ostream &out, const T &data, Format &fmt)
{ _format_integer(out, data, fmt); }
inline void
format_integer(std::ostream &out, char data, Format &fmt)
{ _format_integer(out, (int)data, fmt); }
inline void
format_integer(std::ostream &out, unsigned char data, Format &fmt)
{ _format_integer(out, (int)data, fmt); }
inline void
format_integer(std::ostream &out, signed char data, Format &fmt)
{ _format_integer(out, (int)data, fmt); }
inline void
format_integer(std::ostream &out, const unsigned char *data, Format &fmt)
{ _format_integer(out, (uintptr_t)data, fmt); }
inline void
format_integer(std::ostream &out, const signed char *data, Format &fmt)
{ _format_integer(out, (uintptr_t)data, fmt); }
static inline void
formatInteger(std::ostream &out, const T &data, Format &fmt)
{
_formatInteger(out, data, fmt);
}
static inline void
formatInteger(std::ostream &out, char data, Format &fmt)
{
_formatInteger(out, (int)data, fmt);
}
static inline void
formatInteger(std::ostream &out, unsigned char data, Format &fmt)
{
_formatInteger(out, (int)data, fmt);
}
static inline void
formatInteger(std::ostream &out, signed char data, Format &fmt)
{
_formatInteger(out, (int)data, fmt);
}
static inline void
formatInteger(std::ostream &out, const unsigned char *data, Format &fmt)
{
_formatInteger(out, (uintptr_t)data, fmt);
}
static inline void
formatInteger(std::ostream &out, const signed char *data, Format &fmt)
{
_formatInteger(out, (uintptr_t)data, fmt);
}
//
// floating point formats
//
template <typename T>
inline void
format_float(std::ostream &out, const T &data, Format &fmt)
{ out << "<bad arg type for float format>"; }
static inline void
formatFloat(std::ostream &out, const T &data, Format &fmt)
{
out << "<bad arg type for float format>";
}
inline void
format_float(std::ostream &out, float data, Format &fmt)
{ _format_float(out, data, fmt); }
static inline void
formatFloat(std::ostream &out, float data, Format &fmt)
{
_formatFloat(out, data, fmt);
}
inline void
format_float(std::ostream &out, double data, Format &fmt)
{ _format_float(out, data, fmt); }
static inline void
formatFloat(std::ostream &out, double data, Format &fmt)
{
_formatFloat(out, data, fmt);
}
//
// string formats
//
template <typename T>
inline void
format_string(std::ostream &out, const T &data, Format &fmt)
{ _format_string(out, data, fmt); }
static inline void
formatString(std::ostream &out, const T &data, Format &fmt)
{
_formatString(out, data, fmt);
}
} // namespace cp