Skip to content

Commit

Permalink
Merge pull request #20 from syyyr/fix-tidy
Browse files Browse the repository at this point in the history
Fix tidy
  • Loading branch information
fvacek authored Feb 12, 2024
2 parents 333f05e + 0bb4aed commit 00ecce9
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 18 deletions.
17 changes: 17 additions & 0 deletions .github/actions/build-and-test/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Build and test
description: "Build the project and run tests via CMake"

runs:
using: "composite"
steps:
- name: Build
run: cmake --build '${{github.workspace}}/build' --parallel "$(nproc)"
shell: bash

- name: Test
working-directory: '${{github.workspace}}/build'
run: |
# https://github.com/DiligentGraphics/github-action/commit/d2f7990b16def1efa337e5cc2fc8fa22b6fae55d
PATH="/c/mingw64/bin:$PATH"
ctest --output-on-failure -j"$(nproc)"
shell: bash
45 changes: 45 additions & 0 deletions .github/actions/cmake/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Setup CMake
description: "Invoke CMake and generate build files"
inputs:
additional_cmake_args:
description: "Additional args to pass to CMake"
default: ""

runs:
using: "composite"
steps:
# Linux deps
- name: Install/cache clazy, and doctest
if: runner.os != 'Windows'
uses: awalsh128/[email protected]
with:
packages: doctest-dev clazy
version: 1.0

# Windows deps
- name: Install Windows deps
if: runner.os == 'Windows'
run: |
vcpkg install doctest:x64-mingw-dynamic
echo cmake_extra_args="'-DCMAKE_TOOLCHAIN_FILE=$VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake' '-DMINGW=ON' '-G MinGW Makefiles'" >> "$GITHUB_ENV"
shell: bash

- name: ccache
uses: hendrikmuhs/[email protected]
with:
key: ${{ github.job }}

- name: Configure CMake
run: |
CFLAGS="-Werror ${CFLAGS}" \
CXXFLAGS="-Werror ${CXXFLAGS}" \
cmake \
-S '${{github.workspace}}' \
-B '${{github.workspace}}/build' \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
${{ env.cmake_extra_args }} \
${{ inputs.additional_cmake_args }}
shell: bash
24 changes: 24 additions & 0 deletions .github/actions/run-linter/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: run-linter
description: "Run a linter on all changed files"
inputs:
lint_program_with_args:
description: "Which program to run"
required: true

runs:
using: "composite"
steps:
- uses: ./.github/actions/cmake

- uses: mjp41/workaround8649@c8550b715ccdc17f89c8d5c28d7a48eeff9c94a8
if: runner.os == 'Linux'
with:
os: ubuntu-latest

- name: Copy compile_commands.json
shell: bash
run: cp build/compile_commands.json compile_commands.json

- name: Run the linter
shell: bash
run: find -name '*.cpp' | parallel --verbose --jobs "$(nproc)" --plus _=[{#}/{##}] ${{ inputs.lint_program_with_args }} {}
41 changes: 41 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# vim: sw=2
name: Lint

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
clang-tidy:
name: clang-tidy / Ubuntu 22.04
runs-on: ubuntu-22.04
env:
CC: clang-15
CXX: clang++-15
steps:
- uses: actions/checkout@v3

- name: Run clang-tidy
uses: ./.github/actions/run-linter
with:
lint_program_with_args: clang-tidy-15 --quiet --warnings-as-errors=*

clazy:
name: clazy / Ubuntu 22.04
runs-on: ubuntu-22.04
env:
CC: clang-15
CXX: clang++-15
steps:
- uses: actions/checkout@v3

- name: Run clazy
uses: ./.github/actions/run-linter
with:
lint_program_with_args: clazy-standalone --checks=level1,no-fully-qualified-moc-types,no-non-pod-global-static,no-range-loop-detach
59 changes: 59 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# vim: sw=2
name: Test

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
ubuntu-qt5:
name: Qt 5.15.2 / Ubuntu 22.04
runs-on: ubuntu-22.04
env:
CFLAGS: -fsanitize=address,undefined
CXXFLAGS: -fsanitize=address,undefined
steps:
- uses: actions/checkout@v3

- name: Setup CMake
uses: ./.github/actions/cmake

- name: Build and test
uses: ./.github/actions/build-and-test

ubuntu-qt6:
name: Qt 6.5.0 / Ubuntu 22.04
runs-on: ubuntu-22.04
env:
CFLAGS: -fsanitize=address,undefined
CXXFLAGS: -fsanitize=address,undefined
steps:
- uses: actions/checkout@v3

- name: Setup CMake
uses: ./.github/actions/cmake

- name: Build and test
uses: ./.github/actions/build-and-test

windows:
name: Qt 6.5.0 / Windows
runs-on: windows-2022
steps:
- uses: actions/checkout@v3

- name: Setup CMake
uses: ./.github/actions/cmake
with:
additional_cmake_args: |
-DCMAKE_RUNTIME_OUTPUT_DIRECTORY='${{github.workspace}}/build/bin' \
-DCMAKE_LIBRARY_OUTPUT_DIRECTORY='${{github.workspace}}/build/bin'
- name: Build and test
uses: ./.github/actions/build-and-test
31 changes: 17 additions & 14 deletions libnecrolog/necrolog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ NecroLog::Options &NecroLog::globalOptions()
return global_options;
}

NecroLog NecroLog::create(Level level, LogContext &&log_context)
NecroLog NecroLog::create(Level level, const LogContext& log_context)
{
return NecroLog(level, std::move(log_context));
return NecroLog(level, log_context);
}

bool NecroLog::shouldLog(Level level, const LogContext &context)
Expand All @@ -31,7 +31,7 @@ bool NecroLog::shouldLog(Level level, const LogContext &context)
//if(!topic_set && opts.fileThresholds.empty())
// return level <= Level::Info; // when thresholds are not set, log non-topic INFO messages

const char *searched_str = "";
const char *searched_str = nullptr;
if(topic_set) {
searched_str = context.topic();
}
Expand Down Expand Up @@ -91,9 +91,7 @@ bool NecroLog::shouldLog(Level level, const LogContext &context)
if(topic_set) {
return level <= Level::Warning;
}
else {
return level <= Level::Info; // log non-topic INFO messages
}
return level <= Level::Info; // log non-topic INFO messages
}
return false;
}
Expand All @@ -105,7 +103,8 @@ NecroLog::MessageHandler NecroLog::setMessageHandler(NecroLog::MessageHandler h)
return ret;
}

static void parse_thresholds_string(const std::string &thresholds, std::map<std::string, NecroLog::Level> &threshold_map)
namespace {
void parse_thresholds_string(const std::string &thresholds, std::map<std::string, NecroLog::Level> &threshold_map)
{
using namespace std;
threshold_map.clear();
Expand Down Expand Up @@ -139,12 +138,14 @@ static void parse_thresholds_string(const std::string &thresholds, std::map<std:
pos = pos2 + 1;
}
}
}

// NOLINTNEXTLINE(modernize-avoid-c-arrays)
std::vector<std::string> NecroLog::setCLIOptions(int argc, char *argv[])
{
std::vector<std::string> params;
for (int i = 0; i < argc; ++i)
params.push_back(argv[i]);
params.emplace_back(argv[i]);
return setCLIOptions(params);
}

Expand Down Expand Up @@ -221,7 +222,8 @@ NecroLog::Level NecroLog::stringToLevel(const char *str)
return NecroLog::Level::Invalid;
}

static std::string levels_to_string(const std::map<std::string, NecroLog::Level> &thresholds)
namespace {
std::string levels_to_string(const std::map<std::string, NecroLog::Level> &thresholds)
{
std::string ret;
for (auto& kv : thresholds) {
Expand All @@ -233,6 +235,7 @@ static std::string levels_to_string(const std::map<std::string, NecroLog::Level>
}
return ret;
}
}

std::string NecroLog::thresholdsLogInfo()
{
Expand Down Expand Up @@ -315,9 +318,9 @@ const char * NecroLog::cliHelp()
return ret;
}

NecroLog::NecroLogSharedData::NecroLogSharedData(NecroLog::Level level, NecroLog::LogContext &&log_context)
NecroLog::NecroLogSharedData::NecroLogSharedData(NecroLog::Level level, const NecroLog::LogContext& log_context)
: m_level(level)
, m_logContext(std::move(log_context))
, m_logContext(log_context)
{
}

Expand Down Expand Up @@ -423,9 +426,9 @@ void NecroLog::writeWithDefaultFormat(std::ostream &os, bool is_colorized, Necro
{
std::time_t t = std::time(nullptr);
std::tm *tm = std::gmtime(&t); /// gmtime is not thread safe!!!
char buffer[80] = {0};
std::strftime(buffer, sizeof(buffer),"%Y-%m-%dT%H:%M:%S", tm);
set_TTY_color(is_colorized, os, TTYColor::Green) << std::string(buffer);
std::array<char, 80> buffer = {0};
std::strftime(buffer.data(), buffer.size() * sizeof(*buffer.data()),"%Y-%m-%dT%H:%M:%S", tm);
set_TTY_color(is_colorized, os, TTYColor::Green) << std::string(buffer.data());
set_TTY_color(is_colorized, os, TTYColor::Brown) << '[' << moduleFromFileName(context.file()) << ':' << context.line() << "]";
if(context.isTopicSet()) {
set_TTY_color(is_colorized, os, TTYColor::White) << '(' << context.topic() << ")";
Expand Down
8 changes: 4 additions & 4 deletions libnecrolog/necrolog.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ class NECROLOG_DECL_EXPORT NecroLog

using MessageHandler = std::function<void (Level level, const LogContext &context, const std::string &msg)>;
public:
NecroLog(Level level, LogContext &&log_context)
NecroLog(Level level, const LogContext& log_context)
{
if(level > Level::Invalid)
m_necro = std::make_shared<NecroLogSharedData>(level, std::move(log_context));
m_necro = std::make_shared<NecroLogSharedData>(level, log_context);
}

template<typename T>
Expand All @@ -57,7 +57,7 @@ class NECROLOG_DECL_EXPORT NecroLog
NecroLog& nospace() {if(m_necro) m_necro->setSpace(false); return *this;}
NecroLog& color(NecroLog::Color c) {if(m_necro) m_necro->setColor(c); return *this;}

static NecroLog create(Level level, LogContext &&log_context);
static NecroLog create(Level level, const LogContext &log_context);
static bool shouldLog(Level level, const LogContext &context);

static MessageHandler setMessageHandler(MessageHandler h);
Expand Down Expand Up @@ -101,7 +101,7 @@ class NECROLOG_DECL_EXPORT NecroLog
class NECROLOG_DECL_EXPORT NecroLogSharedData {
friend class NecroLog;
public:
NecroLogSharedData(NecroLog::Level level, LogContext &&log_context);
NecroLogSharedData(NecroLog::Level level, const LogContext& log_context);
~NecroLogSharedData();

void setSpace(bool b) {m_isSpace = b;}
Expand Down

0 comments on commit 00ecce9

Please sign in to comment.