Skip to content

Commit

Permalink
Always use braces with InsertBraces: true in .clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 committed Jan 28, 2025
1 parent 25c9f8f commit cae3100
Show file tree
Hide file tree
Showing 29 changed files with 1,344 additions and 711 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ IndentPPDirectives: BeforeHash
IndentRequires: true
IndentWidth: 4
IndentWrappedFunctionNames: true
InsertBraces: true
InsertNewlineAtEOF: true
# Only support for Javascript as of clang-format 14...
# InsertTrailingCommas: Wrapped
Expand Down
44 changes: 28 additions & 16 deletions src/asm/charmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,19 @@ bool charmap_ForEach(
auto [nodeIdx, mapping] = std::move(prefixes.top());
prefixes.pop();
CharmapNode const &node = charmap.nodes[nodeIdx];
if (node.isTerminal())
if (node.isTerminal()) {
mappings[nodeIdx] = mapping;
}
for (unsigned c = 0; c < 256; c++) {
if (size_t nextIdx = node.next[c]; nextIdx)
if (size_t nextIdx = node.next[c]; nextIdx) {
prefixes.push({nextIdx, mapping + static_cast<char>(c)});
}
}
}
mapFunc(charmap.name);
for (auto [nodeIdx, mapping] : mappings)
for (auto [nodeIdx, mapping] : mappings) {
charFunc(mapping, charmap.nodes[nodeIdx].value);
}
}
return !charmapList.empty();
}
Expand All @@ -70,10 +73,11 @@ void charmap_New(std::string const &name, std::string const *baseName) {
size_t baseIdx = SIZE_MAX;

if (baseName != nullptr) {
if (auto search = charmapMap.find(*baseName); search == charmapMap.end())
if (auto search = charmapMap.find(*baseName); search == charmapMap.end()) {
error("Base charmap '%s' doesn't exist\n", baseName->c_str());
else
} else {
baseIdx = search->second;
}
}

if (charmapMap.find(name) != charmapMap.end()) {
Expand All @@ -85,21 +89,23 @@ void charmap_New(std::string const &name, std::string const *baseName) {
charmapMap[name] = charmapList.size();
Charmap &charmap = charmapList.emplace_back();

if (baseIdx != SIZE_MAX)
if (baseIdx != SIZE_MAX) {
charmap.nodes = charmapList[baseIdx].nodes; // Copies `charmapList[baseIdx].nodes`
else
} else {
charmap.nodes.emplace_back(); // Zero-init the root node
}

charmap.name = name;

currentCharmap = &charmap;
}

void charmap_Set(std::string const &name) {
if (auto search = charmapMap.find(name); search == charmapMap.end())
if (auto search = charmapMap.find(name); search == charmapMap.end()) {
error("Charmap '%s' doesn't exist\n", name.c_str());
else
} else {
currentCharmap = &charmapList[search->second];
}
}

void charmap_Push() {
Expand Down Expand Up @@ -149,8 +155,9 @@ void charmap_Add(std::string const &mapping, std::vector<int32_t> &&value) {

CharmapNode &node = charmap.nodes[nodeIdx];

if (node.isTerminal())
if (node.isTerminal()) {
warning(WARNING_CHARMAP_REDEF, "Overriding charmap mapping\n");
}

std::swap(node.value, value);
}
Expand All @@ -162,8 +169,9 @@ bool charmap_HasChar(std::string const &input) {
for (char c : input) {
nodeIdx = charmap.nodes[nodeIdx].next[static_cast<uint8_t>(c)];

if (!nodeIdx)
if (!nodeIdx) {
return false;
}
}

return charmap.nodes[nodeIdx].isTerminal();
Expand All @@ -188,8 +196,9 @@ size_t charmap_ConvertNext(std::string_view &input, std::vector<int32_t> *output
for (size_t nodeIdx = 0; inputIdx < input.length();) {
nodeIdx = charmap.nodes[nodeIdx].next[static_cast<uint8_t>(input[inputIdx])];

if (!nodeIdx)
if (!nodeIdx) {
break;
}

inputIdx++; // Consume that char

Expand All @@ -209,27 +218,30 @@ size_t charmap_ConvertNext(std::string_view &input, std::vector<int32_t> *output
if (matchIdx) { // A match was found, use it
std::vector<int32_t> const &value = charmap.nodes[matchIdx].value;

if (output)
if (output) {
output->insert(output->end(), RANGE(value));
}

matchLen = value.size();
} else if (inputIdx < input.length()) { // No match found, but there is some input left
int firstChar = input[inputIdx];
// This will write the codepoint's value to `output`, little-endian
size_t codepointLen = readUTF8Char(output, input.data() + inputIdx);

if (codepointLen == 0)
if (codepointLen == 0) {
error("Input string is not valid UTF-8\n");
}

// Warn if this character is not mapped but any others are
if (charmap.nodes.size() > 1)
if (charmap.nodes.size() > 1) {
warning(WARNING_UNMAPPED_CHAR_1, "Unmapped character %s\n", printChar(firstChar));
else if (charmap.name != DEFAULT_CHARMAP_NAME)
} else if (charmap.name != DEFAULT_CHARMAP_NAME) {
warning(
WARNING_UNMAPPED_CHAR_2,
"Unmapped character %s not in " DEFAULT_CHARMAP_NAME " charmap\n",
printChar(firstChar)
);
}

inputIdx += codepointLen;
matchLen = codepointLen;
Expand Down
12 changes: 8 additions & 4 deletions src/asm/fixpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ static double fix2double(int32_t i, int32_t q) {
}

static int32_t double2fix(double d, int32_t q) {
if (isnan(d))
if (isnan(d)) {
return 0;
if (isinf(d))
}
if (isinf(d)) {
return d < 0 ? INT32_MIN : INT32_MAX;
}
return static_cast<int32_t>(round(d * pow(2.0, q)));
}

Expand Down Expand Up @@ -75,8 +77,9 @@ int32_t fix_Mul(int32_t i, int32_t j, int32_t q) {
int32_t fix_Div(int32_t i, int32_t j, int32_t q) {
double dividend = fix2double(i, q);
double divisor = fix2double(j, q);
if (fpclassify(divisor) == FP_ZERO)
if (fpclassify(divisor) == FP_ZERO) {
return dividend < 0 ? INT32_MIN : dividend > 0 ? INT32_MAX : 0;
}
return double2fix(dividend / divisor, q);
}

Expand All @@ -90,8 +93,9 @@ int32_t fix_Pow(int32_t i, int32_t j, int32_t q) {

int32_t fix_Log(int32_t i, int32_t j, int32_t q) {
double divisor = log(fix2double(j, q));
if (fpclassify(divisor) == FP_ZERO)
if (fpclassify(divisor) == FP_ZERO) {
return INT32_MAX;
}
return double2fix(log(fix2double(i, q)) / divisor, q);
}

Expand Down
80 changes: 53 additions & 27 deletions src/asm/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,44 @@
#include "asm/warning.hpp"

void FormatSpec::useCharacter(int c) {
if (state == FORMAT_INVALID)
if (state == FORMAT_INVALID) {
return;
}

switch (c) {
// sign
case ' ':
case '+':
if (state > FORMAT_SIGN)
if (state > FORMAT_SIGN) {
goto invalid;
}
state = FORMAT_EXACT;
sign = c;
break;

// exact
case '#':
if (state > FORMAT_EXACT)
if (state > FORMAT_EXACT) {
goto invalid;
}
state = FORMAT_ALIGN;
exact = true;
break;

// align
case '-':
if (state > FORMAT_ALIGN)
if (state > FORMAT_ALIGN) {
goto invalid;
}
state = FORMAT_WIDTH;
alignLeft = true;
break;

// pad, width, and prec values
case '0':
if (state < FORMAT_WIDTH)
if (state < FORMAT_WIDTH) {
padZero = true;
}
[[fallthrough]];
case '1':
case '2':
Expand All @@ -72,16 +77,18 @@ void FormatSpec::useCharacter(int c) {

// width
case '.':
if (state > FORMAT_WIDTH)
if (state > FORMAT_WIDTH) {
goto invalid;
}
state = FORMAT_FRAC;
hasFrac = true;
break;

// prec
case 'q':
if (state > FORMAT_PREC)
if (state > FORMAT_PREC) {
goto invalid;
}
state = FORMAT_PREC;
hasPrec = true;
break;
Expand All @@ -95,8 +102,9 @@ void FormatSpec::useCharacter(int c) {
case 'o':
case 'f':
case 's':
if (state >= FORMAT_DONE)
if (state >= FORMAT_DONE) {
goto invalid;
}
state = FORMAT_DONE;
valid = true;
type = c;
Expand All @@ -110,8 +118,9 @@ void FormatSpec::useCharacter(int c) {
}

void FormatSpec::finishCharacters() {
if (!isValid())
if (!isValid()) {
state = FORMAT_INVALID;
}
}

static std::string escapeString(std::string const &str) {
Expand Down Expand Up @@ -151,16 +160,21 @@ void FormatSpec::appendString(std::string &str, std::string const &value) const
useType = 's';
}

if (sign)
if (sign) {
error("Formatting string with sign flag '%c'\n", sign);
if (padZero)
}
if (padZero) {
error("Formatting string with padding flag '0'\n");
if (hasFrac)
}
if (hasFrac) {
error("Formatting string with fractional width\n");
if (hasPrec)
}
if (hasPrec) {
error("Formatting string with fractional precision\n");
if (useType != 's')
}
if (useType != 's') {
error("Formatting string as type '%c'\n", useType);
}

std::string useValue = exact ? escapeString(value) : value;
size_t valueLen = useValue.length();
Expand All @@ -187,22 +201,27 @@ void FormatSpec::appendNumber(std::string &str, uint32_t value) const {
}

if (useType != 'X' && useType != 'x' && useType != 'b' && useType != 'o' && useType != 'f'
&& useExact)
&& useExact) {
error("Formatting type '%c' with exact flag '#'\n", useType);
if (useType != 'f' && hasFrac)
}
if (useType != 'f' && hasFrac) {
error("Formatting type '%c' with fractional width\n", useType);
if (useType != 'f' && hasPrec)
}
if (useType != 'f' && hasPrec) {
error("Formatting type '%c' with fractional precision\n", useType);
if (useType == 's')
}
if (useType == 's') {
error("Formatting number as type 's'\n");
}

char signChar = sign; // 0 or ' ' or '+'

if (useType == 'd' || useType == 'f') {
if (int32_t v = value; v < 0) {
signChar = '-';
if (v != INT32_MIN)
if (v != INT32_MIN) {
value = -v;
}
}
}

Expand Down Expand Up @@ -250,10 +269,11 @@ void FormatSpec::appendNumber(std::string &str, uint32_t value) const {
}

double fval = fabs(value / pow(2.0, usePrec));
if (int fracWidthArg = static_cast<int>(useFracWidth); useExact)
if (int fracWidthArg = static_cast<int>(useFracWidth); useExact) {
snprintf(valueBuf, sizeof(valueBuf), "%.*fq%zu", fracWidthArg, fval, usePrec);
else
} else {
snprintf(valueBuf, sizeof(valueBuf), "%.*f", fracWidthArg, fval);
}
} else if (useType == 'd') {
// Decimal numbers may be formatted with a '-' sign by `snprintf`, so `abs` prevents that,
// with a special case for `INT32_MIN` since `labs(INT32_MIN)` is UB. The sign will be
Expand All @@ -278,27 +298,33 @@ void FormatSpec::appendNumber(std::string &str, uint32_t value) const {

str.reserve(str.length() + totalLen);
if (alignLeft) {
if (signChar)
if (signChar) {
str += signChar;
if (prefixChar)
}
if (prefixChar) {
str += prefixChar;
}
str.append(valueBuf);
str.append(padLen, ' ');
} else {
if (padZero) {
// sign, then prefix, then zero padding
if (signChar)
if (signChar) {
str += signChar;
if (prefixChar)
}
if (prefixChar) {
str += prefixChar;
}
str.append(padLen, '0');
} else {
// space padding, then sign, then prefix
str.append(padLen, ' ');
if (signChar)
if (signChar) {
str += signChar;
if (prefixChar)
}
if (prefixChar) {
str += prefixChar;
}
}
str.append(valueBuf);
}
Expand Down
Loading

0 comments on commit cae3100

Please sign in to comment.