From e16a1a6f6ec173906b8353cedbc9ad8ec49f32df Mon Sep 17 00:00:00 2001 From: erius Date: Wed, 23 Oct 2024 14:41:12 +0300 Subject: [PATCH] Integrated clang-tidy into cmake Enabled warnings as errors when building project target Changed checks for clang-tidy Changed indentation from 2 spaces to 4 in clang-format Added pre-commit hook to apply clang-format formatting to the source tree --- .clang-format | 2 +- .clang-tidy | 57 +++++++++----------------- .pre-commit-config.yaml | 5 +++ CMakeLists.txt | 10 ++++- include/filetape.h | 90 ++++++++++++++++++++--------------------- include/tape.h | 66 +++++++++++++++--------------- include/tapeconfig.h | 12 ++++++ src/filetape.cpp | 13 +++--- src/main.cpp | 6 +-- src/tapeconfig.cpp | 18 +++++++++ 10 files changed, 151 insertions(+), 128 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 include/tapeconfig.h create mode 100644 src/tapeconfig.cpp diff --git a/.clang-format b/.clang-format index 690a1e6..05e5ff0 100644 --- a/.clang-format +++ b/.clang-format @@ -1,3 +1,3 @@ Language: Cpp BasedOnStyle: LLVM - +IndentWidth: 4 diff --git a/.clang-tidy b/.clang-tidy index e469776..2c37bc7 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,39 +1,20 @@ -Checks: "*, - -abseil-*, - -altera-*, - -android-*, - -fuchsia-*, - -google-*, - -llvm*, - -modernize-use-trailing-return-type, - -zircon-*, - -readability-else-after-return, - -readability-static-accessed-through-instance, - -readability-avoid-const-params-in-decls, - -cppcoreguidelines-non-private-member-variables-in-classes, - -misc-non-private-member-variables-in-classes, -" -WarningsAsErrors: '' -HeaderFilterRegex: '' -FormatStyle: none +Checks: 'clang-diagnostic-*,clang-analyzer-*,cppcoreguidelines-*,modernize-*,-modernize-use-trailing-return-type,readability-*' +WarningsAsErrors: '*' CheckOptions: - - { key: readability-identifier-naming.NamespaceCase, value: lower_case } - - { key: readability-identifier-naming.ClassCase, value: CamelCase } - - { key: readability-identifier-naming.StructCase, value: CamelCase } - - { key: readability-identifier-naming.TemplateParameterCase, value: CamelCase } - - { key: readability-identifier-naming.FunctionCase, value: camelBack } - - { key: readability-identifier-naming.VariableCase, value: lower_case } - - { key: readability-identifier-naming.PrivateMemberSuffix, value: _ } - - { key: readability-identifier-naming.ProtectedMemberSuffix, value: _ } - - { key: readability-identifier-naming.MacroDefinitionCase, value: UPPER_CASE } - - { key: readability-identifier-naming.EnumConstantCase, value: CamelCase } - - { key: readability-identifier-naming.EnumConstantPrefix, value: k } - - { key: readability-identifier-naming.ConstexprVariableCase, value: CamelCase } - - { key: readability-identifier-naming.ConstexprVariablePrefix, value: k } - - { key: readability-identifier-naming.GlobalConstantCase, value: CamelCase } - - { key: readability-identifier-naming.GlobalConstantPrefix, value: k } - - { key: readability-identifier-naming.MemberConstantCase, value: CamelCase } - - { key: readability-identifier-naming.MemberConstantPrefix, value: k } - - { key: readability-identifier-naming.StaticConstantCase, value: CamelCase } - - { key: readability-identifier-naming.StaticConstantPrefix, value: k } - + - { key: readability-identifier-naming.NamespaceCase, value: lower_case } + - { key: readability-identifier-naming.ClassCase, value: CamelCase } + - { key: readability-identifier-naming.ClassConstantCase, value: UPPER_CASE } + - { key: readability-identifier-naming.ClassMemberCase, value: lower_case } + - { key: readability-identifier-naming.ClassMethodCase, value: lower_case } + - { key: readability-identifier-naming.EnumCase, value: CamelCase } + - { key: readability-identifier-naming.EnumConstantCase, value: UPPER_CASE } + - { key: readability-identifier-naming.FunctionCase, value: lower_case } + - { key: readability-identifier-naming.GlobalConstantCase, value: UPPER_CASE } + - { key: readability-identifier-naming.MemberCase, value: lower_case } + - { key: readability-identifier-naming.MethodCase, value: lower_case } + - { key: readability-identifier-naming.ParameterCase, value: lower_case } + - { key: readability-identifier-naming.StructCase, value: CamelCase } + - { key: readability-identifier-naming.UnionCase, value: CamelCase } + - { key: readability-identifier-naming.TypedefCase, value: CamelCase } + - { key: readability-identifier-naming.TypeAliasCase, value: CamelCase } + - { key: readability-identifier-naming.VariableCase, value: lower_case } diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..2dbbec4 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,5 @@ +repos: +- repo: https://github.com/pre-commit/mirrors-clang-format + rev: v19.1.2 + hooks: + - id: clang-format diff --git a/CMakeLists.txt b/CMakeLists.txt index 12cbc91..884260b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,15 @@ cmake_minimum_required(VERSION 3.5) -project(yadro-task VERSION 0.1) +project(yadro-task VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -file(GLOB SOURCES "src/*.cpp") +# clang-tidy +find_program(CLANG_TIDY_EXE NAMES clang-tidy REQUIRED) +set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_EXE}) + +file(GLOB SOURCES src/*.cpp) add_executable(${PROJECT_NAME} ${SOURCES}) @@ -13,3 +18,4 @@ target_include_directories(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/include ) +set_property(TARGET ${PROJECT_NAME} PROPERTY COMPILE_WARNINGS_AS_ERRORS ON) diff --git a/include/filetape.h b/include/filetape.h index c0c28e9..4921792 100644 --- a/include/filetape.h +++ b/include/filetape.h @@ -13,10 +13,10 @@ namespace tape { * such as read dealy, write delay etc. */ struct FileTapeSettings { - unsigned int readDelayMs; - unsigned int writeDelayMs; - unsigned int seekForwardDelayMs; - unsigned int seekBackwardsDelayMs; + unsigned int read_delay_ms; + unsigned int write_delay_ms; + unsigned int seek_forward_delay_ms; + unsigned int seek_backwards_delay_ms; }; /** @@ -28,53 +28,53 @@ struct FileTapeSettings { * cell data is stored as plain text. */ class FileTape : public Tape { -private: - std::fstream file_; - FileTapeSettings settings_; + private: + std::fstream file; + FileTapeSettings settings; -public: - FileTape &operator=(const FileTape &) = delete; - FileTape(const FileTape &) = delete; - FileTape(FileTape &&) = delete; - FileTape &operator=(FileTape &&) = delete; - ~FileTape() override; + public: + FileTape &operator=(const FileTape &) = delete; + FileTape(const FileTape &) = delete; + FileTape(FileTape &&) = delete; + FileTape &operator=(FileTape &&) = delete; + ~FileTape() override; - /** - * Initialize a new instance of FileTape that will open a file with the - * specified name. File must be read and write accessible. - * - * @param file_name Name of a file to be opened. - * @param settings File tape settings - */ - explicit FileTape(const std::string &fileName, FileTapeSettings settings); + /** + * Initialize a new instance of FileTape that will open a file with the + * specified name. File must be read and write accessible. + * + * @param file_name Name of a file to be opened. + * @param settings File tape settings + */ + explicit FileTape(const std::string &file_name, FileTapeSettings settings); - /** - * Advances the underlying fstream to a new line. - * - * @return false if EOF is reached, otherwise true. - */ - bool seekForward() override; + /** + * Advances the underlying fstream to a new line. + * + * @return false if EOF is reached, otherwise true. + */ + bool seek_forward() override; - /** - * Rewinds the underlying fstream to a previous line. - * - * @return false if called from the first line of a file, otherwise true. - */ - bool seekBackwards() override; + /** + * Rewinds the underlying fstream to a previous line. + * + * @return false if called from the first line of a file, otherwise true. + */ + bool seek_backwards() override; - /** - * Reads and parses data from the current line. - * - * @return number from the line, or 0 if data can not be parsed. - */ - int32_t read() override; + /** + * Reads and parses data from the current line. + * + * @return number from the line, or 0 if data can not be parsed. + */ + int32_t read() override; - /** - * Writes data to the current line. - * - * @param data Number, that should be written to the current line. - */ - void write(int32_t data) override; + /** + * Writes data to the current line. + * + * @param data Number, that should be written to the current line. + */ + void write(int32_t data) override; }; } // namespace tape diff --git a/include/tape.h b/include/tape.h index 9e9dd10..d54c7d7 100644 --- a/include/tape.h +++ b/include/tape.h @@ -16,43 +16,43 @@ namespace tape { * forward or backwards is a very time-consuming operation. */ class Tape { -public: - Tape(const Tape &) = default; - Tape(Tape &&) = default; - Tape &operator=(const Tape &) = default; - Tape &operator=(Tape &&) = default; - Tape() = default; - virtual ~Tape() = default; + public: + Tape(const Tape &) = default; + Tape(Tape &&) = default; + Tape &operator=(const Tape &) = default; + Tape &operator=(Tape &&) = default; + Tape() = default; + virtual ~Tape() = default; - /** - * Advances the tape one cell forward. - * - * @return false if the next cell doesn't exist (reached end of tape), - * otherwise true. - */ - virtual bool seekForward() = 0; + /** + * Advances the tape one cell forward. + * + * @return false if the next cell doesn't exist (reached end of tape), + * otherwise true. + */ + virtual bool seek_forward() = 0; - /** - * Rewinds the tape one cell backwards. - * - * @return false if the previous cell doesn't exist (reached beginning of - * tape), otherwise true. - */ - virtual bool seekBackwards() = 0; + /** + * Rewinds the tape one cell backwards. + * + * @return false if the previous cell doesn't exist (reached beginning of + * tape), otherwise true. + */ + virtual bool seek_backwards() = 0; - /** - * Reads data from the current cell. - * - * @return number, that is stored on the current cell. - */ - virtual int32_t read() = 0; + /** + * Reads data from the current cell. + * + * @return number, that is stored on the current cell. + */ + virtual int32_t read() = 0; - /** - * Writes data to the current cell. - * - * @param data Number, that should be written to the current cell. - */ - virtual void write(int32_t data) = 0; + /** + * Writes data to the current cell. + * + * @param data Number, that should be written to the current cell. + */ + virtual void write(int32_t data) = 0; }; } // namespace tape diff --git a/include/tapeconfig.h b/include/tapeconfig.h new file mode 100644 index 0000000..9e98043 --- /dev/null +++ b/include/tapeconfig.h @@ -0,0 +1,12 @@ +#ifndef TAPE_CONFIG_H +#define TAPE_CONFIG_H + +#include "filetape.h" + +namespace tape { + +FileTapeSettings init_settings(int argc, char **argv); + +} // namespace tape + +#endif // !TAPE_CONFIG_H diff --git a/src/filetape.cpp b/src/filetape.cpp index 5390eda..21d6655 100644 --- a/src/filetape.cpp +++ b/src/filetape.cpp @@ -3,17 +3,18 @@ #include #include -tape::FileTape::FileTape(const std::string &fileName, FileTapeSettings settings) - : settings_(settings) { - this->file_ = std::fstream(fileName); +tape::FileTape::FileTape(const std::string &file_name, + FileTapeSettings settings) + : settings(settings) { + this->file = std::fstream(file_name); } -bool tape::FileTape::seekForward() { return true; } +bool tape::FileTape::seek_forward() { return true; } -bool tape::FileTape::seekBackwards() { return true; } +bool tape::FileTape::seek_backwards() { return true; } int32_t tape::FileTape::read() { return 0; } void tape::FileTape::write(int32_t data) {} -tape::FileTape::~FileTape() { this->file_.close(); } +tape::FileTape::~FileTape() { this->file.close(); } diff --git a/src/main.cpp b/src/main.cpp index 9f7372b..c5ca49e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ #include #include -int main(int /*argc*/, char * /*argv*/[]) { - std::cout << "Hello, World!" << std::endl; - return 0; +int main(int argc, char *argv[]) { + std::cout << "Hello, World!" << std::endl; + return 0; } diff --git a/src/tapeconfig.cpp b/src/tapeconfig.cpp new file mode 100644 index 0000000..1fa42d3 --- /dev/null +++ b/src/tapeconfig.cpp @@ -0,0 +1,18 @@ +#include "tapeconfig.h" +#include "filetape.h" +#include + +struct CommandLineArgs { + std::string config_file_path; + tape::FileTapeSettings settings; +}; + +// const static struct Options COMMAND_LINE_OPTIONS[]{}; + +CommandLineArgs parse_command_line(int argc, char **argv) { + return CommandLineArgs{}; +} + +tape::FileTapeSettings tape::init_settings(int argc, char **argv) { + return FileTapeSettings{}; +}