Skip to content

Commit

Permalink
Tidy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
electroly committed May 27, 2024
1 parent 9383229 commit ef4f88c
Show file tree
Hide file tree
Showing 41 changed files with 165 additions and 176 deletions.
2 changes: 1 addition & 1 deletion src/.clang-tidy
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Checks: 'boost-*,clang-analyzer-*,cppcoreguidelines-*,google-*,misc-*,modernize-*,performance-*,portability-*,readability-*,-clang-analyzer-cplusplus.NewDeleteLeaks,-modernize-use-trailing-return-type,-misc-non-private-member-variables-in-classes,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-non-private-member-variables-in-classes,-modernize-use-nodiscard,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-readability-implicit-bool-conversion,-clang-diagnostic-unknown-warning-option,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-vararg,-modernize-raw-string-literal,-google-readability-todo,-readability-identifier-length,-readability-function-cognitive-complexity,-misc-no-recursion'
Checks: 'boost-*,clang-analyzer-*,cppcoreguidelines-*,google-*,misc-*,modernize-*,performance-*,portability-*,readability-*,-clang-analyzer-cplusplus.NewDeleteLeaks,-modernize-use-trailing-return-type,-misc-non-private-member-variables-in-classes,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-non-private-member-variables-in-classes,-modernize-use-nodiscard,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-readability-implicit-bool-conversion,-clang-diagnostic-unknown-warning-option,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-vararg,-modernize-raw-string-literal,-google-readability-todo,-readability-identifier-length,-readability-function-cognitive-complexity,-misc-no-recursion,-misc-include-cleaner,-misc-use-anonymous-namespace,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-const-or-ref-data-members,-readability-avoid-nested-conditional-operator,-performance-enum-size,-clang-analyzer-optin.core.EnumCastOutOfRange'
21 changes: 10 additions & 11 deletions src/buildDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

using boost::trim_copy;
using std::cerr;
using std::endl;
using std::function;
using std::istringstream;
using std::make_unique;
Expand Down Expand Up @@ -158,7 +157,7 @@ static string replaceRegex(const string& haystack, const string& pattern, const
}

static string replaceRegex(string haystack, const string& pattern, const function<string(smatch&)>& replacementFunc) {
regex r(pattern);
regex const r(pattern);
while (true) {
smatch match;
if (regex_search(haystack, match, r)) {
Expand Down Expand Up @@ -330,7 +329,7 @@ static vector<string> splitStringIntoLines(const string& input) {
}

static bool startsWith(const string& str, char ch) {
return str.length() > 0 && str[0] == ch;
return !str.empty() && str[0] == ch;
}

static string readProcedureBlock(const vector<string>& lines, size_t* i) {
Expand Down Expand Up @@ -370,12 +369,12 @@ static unique_ptr<Procedure> parseProcedure(const string& input) {
overload = newOverload.get();
procedure->overloads.push_back(std::move(newOverload));
} else if (section == ".description") {
if (procedure->description.length() > 0) {
if (!procedure->description.empty()) {
throw runtime_error(string("Duplicate description in procedure ") + procedure->name);
}
procedure->description = readProcedureBlock(lines, &i);
} else if (section == ".blurb") {
if (procedure->blurb.length() > 0) {
if (!procedure->blurb.empty()) {
throw runtime_error(string("Duplicate blurb in procedure ") + procedure->name);
}
procedure->blurb = rest;
Expand Down Expand Up @@ -412,12 +411,12 @@ static unique_ptr<Procedure> parseProcedure(const string& input) {
newExample->description = readProcedureBlock(lines, &i);
overload->examples.push_back(std::move(newExample));
} else if (section == ".example-code") {
if (example->code.length() > 0) {
if (!example->code.empty()) {
throw runtime_error(string("Duplicate example-code in procedure ") + procedure->name);
}
example->code = readProcedureBlock(lines, &i);
} else if (section == ".example-output") {
if (example->output.length() > 0) {
if (!example->output.empty()) {
throw runtime_error(string("Duplicate example-output in procedure ") + procedure->name);
}
example->output = readProcedureBlock(lines, &i);
Expand Down Expand Up @@ -676,12 +675,12 @@ int main() {
buildProcedureIndex(procedures, &outputTxt, htmlPageTemplate);
writeFile("../obj/resources/help/help.txt", insertDiagrams(outputTxt.str()));
} catch (const regex_error& ex) {
ostringstream s;
cerr << ex.what() << ": " << NAMEOF_ENUM(ex.code()) << endl;
ostringstream const s;
cerr << ex.what() << ": " << NAMEOF_ENUM(ex.code()) << '\n';
return -1;
} catch (const runtime_error& ex) {
ostringstream s;
cerr << ex.what() << endl;
ostringstream const s;
cerr << ex.what() << '\n';
return -1;
}

Expand Down
10 changes: 0 additions & 10 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,6 @@ intrusive_ptr<T> make_intrusive_ptr(Args&&... args) {
}
} // namespace boost

#ifdef CLANG_TIDY
// clang-tidy gets upset about assert()
#undef assert
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define assert(expr) \
if (!(expr)) { \
throw std::runtime_error(#expr); \
}
#endif

// clang-tidy likes to see gsl::owner to express ownership of raw pointers, but we don't care about any other part of
// the C++ Guidelines Support Library. let's just define our own gsl::owner.
namespace gsl {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/BuiltInRecordTypesList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BuiltInRecordTypesList {
};
}

std::unordered_map<std::string, FieldList> types{};
std::unordered_map<std::string, FieldList> types;

private:
static boost::local_shared_ptr<ParameterNode> ValueField(
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/CompiledProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void CompiledProgram::run() const {

// If it was successful, then execution will never reach this point.
// If we're here, then it failed.
std::cerr << "Failed to start the program: " << strerror(errno) << std::endl;
std::cerr << "Failed to start the program: " << strerror(errno) << '\n';

// ChatGPT says it's important to use _exit and not exit here.
_exit(EXIT_FAILURE);
Expand All @@ -114,7 +114,7 @@ void CompiledProgram::run() const {
int status{};
waitpid(pid, &status, 0);
} else {
std::cout << "Failed to fork a new process: " << strerror(errno) << std::endl;
std::cout << "Failed to fork a new process: " << strerror(errno) << '\n';
}
#endif

Expand Down
6 changes: 3 additions & 3 deletions src/compiler/Publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Publisher::Publisher(const CompiledProgram& compiledProgram, const std::string&
_pcode(compiledProgram.serialize()) {}

std::string Publisher::getPublishDir(const std::string& basFilePath) {
std::string basDir = shared::getDirectoryName(basFilePath);
std::string const basDir = shared::getDirectoryName(basFilePath);
return shared::pathCombine(basDir, "publish");
}

Expand All @@ -34,13 +34,13 @@ std::string Publisher::publish(TargetPlatform platform) {
std::vector<uint8_t> licData{};
licData.insert(licData.end(), licString.begin(), licString.end());
if (isZip) {
std::vector<compiler::ZipEntry> entries{
std::vector<compiler::ZipEntry> const entries{
compiler::ZipEntry{ std::move(exeFilename), std::move(exeData) },
compiler::ZipEntry{ licFilename, std::move(licData) },
};
compiler::zip(archiveFilePath, entries);
} else {
std::vector<shared::TarEntry> entries{
std::vector<shared::TarEntry> const entries{
shared::TarEntry{ std::move(exeFilename), std::move(exeData), 0777 },
shared::TarEntry{ licFilename, std::move(licData), 0664 },
};
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/TargetPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const char* getPlatformExeExtension(TargetPlatform platform) {
}

std::string getLicenseForPlatform(TargetPlatform platform) {
std::string_view sv{ kLicense, kLicense_len };
std::string_view const sv{ kLicense, kLicense_len };
switch (platform) {
case TargetPlatform::kWinX86:
case TargetPlatform::kWinX64: {
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/compileProcedures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,17 @@ void assignProcedureIndices(const SourceProgram& sourceProgram, CompiledProgram*
}

void compileProcedures(const SourceProgram& sourceProgram, CompiledProgram* compiledProgram) {
BuiltInConstantList builtInConstants{};
BuiltInProcedureList builtInProcedures{};
BuiltInConstantList const builtInConstants{};
BuiltInProcedureList const builtInProcedures{};
SymbolScope globalSymbolScope{ *compiledProgram };

// add symbols to the global scope for built-in constants
for (auto& builtInConstant : builtInConstants.constants) {
for (const auto& builtInConstant : builtInConstants.constants) {
globalSymbolScope.addSymbol(builtInConstant.second.get(), SymbolType::kVariable);
}

// add symbols to the global scope for built-in procedures
for (auto& builtInProcedureGroup : builtInProcedures.map) {
for (const auto& builtInProcedureGroup : builtInProcedures.map) {
for (auto& builtInProcedure : *builtInProcedureGroup.second) {
globalSymbolScope.addSymbol(builtInProcedure.get(), SymbolType::kProcedure);
}
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ static void emitAssignStatement(const AssignStatementNode& statementNode, Proced
static void emitCallStatement(const CallStatementNode& statementNode, ProcedureState* state) {
auto numValueArgs = 0;
auto numObjectArgs = 0;
int maxArgs = std::numeric_limits<uint8_t>::max();
int const maxArgs = std::numeric_limits<uint8_t>::max();
for (const auto& arg : statementNode.arguments) {
assert(arg->evaluatedType != nullptr);
arg->evaluatedType->isValueType() ? numValueArgs++ : numObjectArgs++;
Expand Down Expand Up @@ -2163,7 +2163,7 @@ static void emitPrint(const TypeNode& type, const Token& token, ProcedureState*
// ---- O: list [0] | V: [0]

// Print the list. We'll have to construct the type List of T from our type Set of T.
TypeNode listType{ Kind::kList, type.token, type.setKeyType };
TypeNode const listType{ Kind::kList, type.token, type.setKeyType };
emitPrint(listType, token, state);
// ---- O: [0] | V: [0]
break;
Expand Down Expand Up @@ -2377,7 +2377,7 @@ vector<uint8_t> emit(
std::cerr << "--start of emit--";
#endif
ProcedureState state;
int maxLocals = std::numeric_limits<uint16_t>::max();
int const maxLocals = std::numeric_limits<uint16_t>::max();
if (numLocalValues > maxLocals || numLocalObjects > maxLocals) {
throw CompilerException(
CompilerErrorCode::kTooManyLocalVariables, "Too many local variables.", procedureNode.token);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/makeExeFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ static size_t findBytecodeIndex(const vector<uint8_t>& runnerBinary) {
vector<uint8_t> readDebugRunnerFile() {
auto filename = fmt::format("runner{}", getPlatformExeExtension(getNativeTargetPlatform()));

vector<string> candidates{ filename, fmt::format("/code/bin/{}", filename) };
vector<string> const candidates{ filename, fmt::format("/code/bin/{}", filename) };

// Find the first candidate file that actually exists.
for (const auto& candidate : candidates) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static Term zeroOrMore(std::initializer_list<Term> terms) {
static Term capture(int captureId, Term&& subTerm) {
auto t = Term(TermType::kCapture);
t.captureId = captureId;
t.subTerms.push_back(subTerm);
t.subTerms.push_back(std::move(subTerm));
return t;
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ bool Scanner::isCurrentTokenTextEmpty() {
}

TokenKind Scanner::classifyToken(const std::string& text) {
assert(text.length() > 0);
assert(!text.empty());
switch (text[0]) {
case '\n':
return TokenKind::kEndOfLine;
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/typeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,7 @@ static void typeCheckSelectCaseStatement(SelectCaseStatementNode* statementNode)

static void typeCheckYieldStatement(YieldStatementNode* statementNode) {
assert(statementNode->boundCollectionDeclaration != nullptr);
(void)statementNode;
}

static void typeCheckDimListStatement(DimListStatementNode* statementNode) {
Expand Down Expand Up @@ -1425,7 +1426,7 @@ void typeCheck(
CompiledProgram* compiledProgram,
const BuiltInProcedureList& builtInProcedures) {
TypeCheckState state{ *procedureNode, sourceProgram, compiledProgram, builtInProcedures };
return typeCheckBody(procedureNode->body.get(), &state);
typeCheckBody(procedureNode->body.get(), &state);
}

}; // namespace compiler
6 changes: 3 additions & 3 deletions src/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ int main(int /*argc*/, const char* /*argv*/[]) {

auto error = interpreter.getError();
if (error.has_value()) {
std::cerr << "Error " << error->code.getString() << ": " << error->message << std::endl;
std::cerr << "Error " << error->code.getString() << ": " << error->message << '\n';
return 1;
}

return 0;
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
std::cerr << e.what() << '\n';
return 1;
} catch (...) {
std::cerr << "Unknown fatal error." << std::endl;
std::cerr << "Unknown fatal error." << '\n';
return 1;
}
}
2 changes: 1 addition & 1 deletion src/shared/InputLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace shared {
InputLine::InputLine(const std::string& text, int width, int aMaxLen)
: TInputLine(
TRect(0, 0, width + 1 /*why is +1 needed?*/, 1),
std::max(text.size() + 1, static_cast<size_t>(aMaxLen) + 1)) {
std::max(static_cast<int>(text.size()) + 1, aMaxLen + 1)) {
memcpy(data, text.c_str(), text.size());
data[text.size()] = 0; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
}
Expand Down
2 changes: 1 addition & 1 deletion src/shared/Label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace shared {

Label::Label(const TRect& r) : TLabel(r, "", nullptr) {}

Label::Label(TStringView text, TView* link) : TLabel(TRect(0, 0, static_cast<int>(cstrlen(text) + 3), 1), text, link) {}
Label::Label(TStringView text, TView* link) : TLabel(TRect(0, 0, (cstrlen(text) + 3), 1), text, link) {}

Label::Label(const TRect& r, TStringView text, TView* link) : TLabel(r, text, link) {}

Expand Down
4 changes: 2 additions & 2 deletions src/shared/PictureView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ std::string Picture::exportToString() {
uint32_t previousBg = 0;
auto previousTransparent = false;
std::string previousChar = " ";
std::function<void()> newlineOrSpace = [&s, &lineStart]() -> void {
std::function<void()> const newlineOrSpace = [&s, &lineStart]() -> void {
if (s.tellp() - lineStart >= 110) {
s << "\n";
lineStart = s.tellp();
Expand All @@ -109,7 +109,7 @@ std::string Picture::exportToString() {
auto changesBitMask = (previousChar != cell.ch ? 0x01 : 0) |
(previousTransparent != cell.transparent ? 0x02 : 0) | (previousFg != fg ? 0x04 : 0) |
(previousBg != bg ? 0x08 : 0);
char command = static_cast<char>('A' + changesBitMask);
char const command = static_cast<char>('A' + changesBitMask);

s << command;

Expand Down
8 changes: 4 additions & 4 deletions src/shared/StatusLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ void StatusLine::draw() {
TDrawBuffer b;
TAttrPair color{};

TAttrPair cNormal = getColor(0x0301);
TAttrPair cNormDisabled = getColor(0x0202);
TAttrPair const cNormal = getColor(0x0301);
TAttrPair const cNormDisabled = getColor(0x0202);
b.moveChar(0, ' ', cNormal, size.x);
auto* t = items;
ushort i = 0;
Expand All @@ -22,7 +22,7 @@ void StatusLine::draw() {
}

if (t->text != nullptr) {
ushort l = cstrlen(t->text);
ushort const l = cstrlen(t->text);
if (i + l < size.x) {
if (commandEnabled(t->command)) {
if (colors != nullptr && colors->colorPairNormal.has_value()) {
Expand All @@ -47,7 +47,7 @@ void StatusLine::draw() {
t = t->next;
}
if (i < size.x - 2) {
TStringView hintText = hint(helpCtx);
TStringView const hintText = hint(helpCtx);
if (!hintText.empty()) {
b.moveStr(i, "\xB3 ", cNormal);
i += 2;
Expand Down
4 changes: 2 additions & 2 deletions src/shared/ThinButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void ThinButton::drawState(bool down) {
cButton = { 0xA0, 0xA0 };
}

int s = size.x - 1;
int const s = size.x - 1;
b.moveChar(0, ' ', cButton, size.x);

if (title != nullptr) {
Expand Down Expand Up @@ -99,7 +99,7 @@ void ThinButton::handleEvent(TEvent& event) {
TView::handleEvent(event);
}

char c = hotKey(title);
char const c = hotKey(title);
switch (event.what) {
case evMouseDown:
if ((state & sfDisabled) == 0) {
Expand Down
10 changes: 5 additions & 5 deletions src/shared/decimal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ Decimal doubleToDecimal(double x) {
// Decompose double using bit manipulation
uint64_t bits = 0;
std::memcpy(&bits, &x, sizeof(double));
uint32_t negative = (bits >> 63) & 1U;
uint32_t exponent = (bits >> 52) & 0x7FFU;
uint64_t mantissa = bits & 0xFFFFFFFFFFFFFU;
uint32_t const negative = (bits >> 63) & 1U;
uint32_t const exponent = (bits >> 52) & 0x7FFU;
uint64_t const mantissa = bits & 0xFFFFFFFFFFFFFU;

if (std::isinf(x)) {
mpd_uint128_triple_t infTriple{};
Expand All @@ -73,7 +73,7 @@ Decimal doubleToDecimal(double x) {
int64_t binaryExponent = exponent;
binaryExponent -= 0x3ff; // IEEE 754 double bias

Decimal fraction = mantissa / kDecimalDoubleMantissaDenominator;
Decimal const fraction = mantissa / kDecimalDoubleMantissaDenominator;

Decimal magnitude = 2;
auto isSubnormal = exponent == 0U;
Expand All @@ -83,7 +83,7 @@ Decimal doubleToDecimal(double x) {
}
magnitude = magnitude.pow(binaryExponent);

Decimal sign = negative == 0U ? 1 : -1;
Decimal const sign = negative == 0U ? 1 : -1;

if (exponent == 0U && mantissa == 0U) {
return kDecimalZero;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/tar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ TarEntry::TarEntry(std::string name, std::vector<uint8_t> data, uint mode)

class OutputTarArchive {
public:
std::vector<uint8_t> bytes{};
std::vector<uint8_t> bytes;
size_t index{ 0 };

static int read(mtar_t* tar, void* data, unsigned size) {
Expand Down
2 changes: 1 addition & 1 deletion src/tmbasic/AboutDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static const std::string kLogoPicture =
static Picture getLogoPicture() {
Picture picture{ kLogoPicture };
// replace RGB #C0C0C0 with BIOS color 7 otherwise it won't exactly match the background of the dialog box
TColorRGB c0c0c0{ 0xC0C0C0 };
TColorRGB const c0c0c0{ 0xC0C0C0 };
for (auto& cell : picture.cells) {
if (getBack(cell.colorAttr) == c0c0c0) {
cell.colorAttr = { getFore(cell.colorAttr), TColorBIOS{ 7 } };
Expand Down
Loading

0 comments on commit ef4f88c

Please sign in to comment.