Reduce nesting depth in diagnostics.cpp

This commit is contained in:
Rangi42
2025-07-09 15:40:23 -04:00
parent 44f5b47bf0
commit bf6875f160

View File

@@ -39,20 +39,19 @@ std::pair<WarningState, std::optional<uint8_t>> getInitialWarningState(std::stri
state = {.state = WARNING_ENABLED, .error = WARNING_DEFAULT}; state = {.state = WARNING_ENABLED, .error = WARNING_DEFAULT};
} }
// Check for an `=` parameter to process as a parametric warning // Check if there is an "equals" sign followed by a decimal number
// `-Wno-<flag>` and `-Wno-error=<flag>` negation cannot have an `=` parameter, but without a // Ignore an equals sign at the very end of the string
// parameter, the 0 value will apply to all levels of a parametric warning auto equals = flag.find('=');
uint8_t param = 0; // `-Wno-<flag>` and `-Wno-error=<flag>` negation cannot have an `=` parameter, but without
bool hasParam = false; // one, the 0 value will apply to all levels of a parametric warning
if (state.state == WARNING_ENABLED) { if (state.state != WARNING_ENABLED || equals == flag.npos || equals == flag.size() - 1) {
// First, check if there is an "equals" sign followed by a decimal number return {state, std::nullopt};
// Ignore an equal sign at the very end of the string }
if (auto equals = flag.find('='); equals != flag.npos && equals != flag.size() - 1) {
hasParam = true;
// Is the rest of the string a decimal number? // Is the rest of the string a decimal number?
// We want to avoid `strtoul`'s whitespace and sign, so we parse manually // We want to avoid `strtoul`'s whitespace and sign, so we parse manually
char const *ptr = flag.c_str() + equals + 1; char const *ptr = flag.c_str() + equals + 1;
uint8_t param = 0;
bool warned = false; bool warned = false;
// The `if`'s condition above ensures that this will run at least once // The `if`'s condition above ensures that this will run at least once
@@ -64,9 +63,7 @@ std::pair<WarningState, std::optional<uint8_t>> getInitialWarningState(std::stri
// Avoid overflowing! // Avoid overflowing!
if (param > UINT8_MAX - (*ptr - '0')) { if (param > UINT8_MAX - (*ptr - '0')) {
if (!warned) { if (!warned) {
warnx( warnx("Invalid warning flag \"%s\": capping parameter at 255", flag.c_str());
"Invalid warning flag \"%s\": capping parameter at 255", flag.c_str()
);
} }
warned = true; // Only warn once, cap always warned = true; // Only warn once, cap always
param = 255; param = 255;
@@ -85,11 +82,6 @@ std::pair<WarningState, std::optional<uint8_t>> getInitialWarningState(std::stri
state.state = WARNING_DISABLED; state.state = WARNING_DISABLED;
} }
} }
}
}
if (hasParam) {
return {state, param}; return {state, param};
}
return {state, std::nullopt};
} }