Skip to content

Commit

Permalink
ci: Add MSVC code analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Jul 18, 2023
1 parent 03c24f6 commit 2c544a2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 11 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/msvc-code-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Microsoft C++ Code Analysis

on:
push:
branches:
- main
pull_request:
branches:
- main

env:
build: ${{ github.workspace }}/build
config: Debug

permissions:
contents: read

jobs:
analyze:
permissions:
contents: read
security-events: write
actions: read
name: Analyze
runs-on: windows-latest

steps:
- name: Check out repository
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9

- name: Check out vcpkg
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
with:
repository: microsoft/vcpkg
path: ${{ github.workspace }}\vcpkg

- name: Bootstrap vcpkg
run: .\vcpkg\bootstrap-vcpkg.bat -disableMetrics

- name: Apply vcpkg cache
id: vcpkg-cache
uses: actions/cache@v3
with:
path: |-
%VCPKG_DEFAULT_BINARY_CACHE%
${{ github.workspace }}\vcpkg
key: >-
${{ runner.os }}-vcpkg-archives-cache-${{ hashFiles(
'vcpkg/ports/catch2/vcpkg.json',
'vcpkg/ports/fmt/vcpkg.json',
'vcpkg/ports/nameof/vcpkg.json',
'vcpkg/ports/nanobench/vcpkg.json'
) }}
- name: Configure CMake
run: cmake -B "${{ env.build }}" -DCMAKE_BUILD_TYPE="${{ env.config }}"

- name: Initialize MSVC Code Analysis
uses: microsoft/msvc-code-analysis-action@24c285ab36952c9e9182f4b78dfafbac38a7e5ee
id: run-analysis
with:
buildConfiguration: ${{ env.config }}
cmakeBuildDirectory: ${{ env.build }}
ruleset: NativeRecommendedRules.ruleset

- name: Upload SARIF to GitHub
uses: github/codeql-action/upload-sarif@46a6823b81f2d7c67ddf123851eea88365bc8a67
with:
sarif_file: ${{ steps.run-analysis.outputs.sarif }}

- name: Upload SARIF as an Artifact
uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce
with:
name: sarif-file
path: ${{ steps.run-analysis.outputs.sarif }}
11 changes: 4 additions & 7 deletions include/forfun/palindrome.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ is_palindrome(std::string_view const& s) noexcept {
return true;
}

[[nodiscard]] constexpr inline bool
is_palindrome_ci(std::string_view const& s) noexcept {
[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept {
auto const end{s.length() - 1};
auto const mid{s.length() / 2};

Expand Down Expand Up @@ -68,8 +67,7 @@ is_palindrome(std::string_view const& s) noexcept {
return true;
}

[[nodiscard]] constexpr inline bool
is_palindrome_ci(std::string_view const& s) noexcept {
[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept {
auto upper{s.cend() - 1};
auto const mid{s.cbegin() + (s.length() / 2)};

Expand Down Expand Up @@ -98,15 +96,14 @@ is_palindrome(std::string_view const& s) noexcept {
}

namespace {
[[nodiscard]] constexpr inline bool
[[nodiscard]] inline bool
equal_case_insensitive(char const a, char const b) noexcept {
return std::tolower(static_cast<unsigned char>(a))
== std::tolower(static_cast<unsigned char>(b));
}
} // namespace

[[nodiscard]] constexpr inline bool
is_palindrome_ci(std::string_view const& s) noexcept {
[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept {
return std::equal(
s.cbegin(), std::next(s.cbegin(), s.size() / 2), s.crbegin(),
equal_case_insensitive);
Expand Down
2 changes: 1 addition & 1 deletion include/forfun/palindromic_number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
#ifndef FORFUN_PALINDROMIC_NUMBER_HPP_
#define FORFUN_PALINDROMIC_NUMBER_HPP_

[[nodiscard]] bool is_palindrome(int const n) noexcept;
[[nodiscard]] /* constexpr */ bool is_palindrome(int const n) noexcept;

#endif // FORFUN_PALINDROMIC_NUMBER_HPP_
5 changes: 3 additions & 2 deletions include/forfun/project_euler/p0001_multiples_of_3_or_5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
#include <algorithm>

namespace {
[[nodiscard]] inline constexpr int sum_2x(int const n, int const q) noexcept {
[[nodiscard]] inline /* constexpr */ int
sum_2x(int const n, int const q) noexcept {
auto const d = std::div(n, q);

return d.quot * (q + n - d.rem);
}
} // namespace

[[nodiscard]] constexpr int find_sum_mult_three_five(int n) noexcept {
[[nodiscard]] /* constexpr */ int find_sum_mult_three_five(int n) noexcept {
--n;

return (sum_2x(n, 3) + sum_2x(n, 5) - sum_2x(n, 15)) / 2;
Expand Down
2 changes: 1 addition & 1 deletion src/palindromic_number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <cstdlib>

[[nodiscard]] bool is_palindrome(int const n) noexcept {
[[nodiscard]] /* constexpr */ bool is_palindrome(int const n) noexcept {
if (n < 0) {
return false;
}
Expand Down

0 comments on commit 2c544a2

Please sign in to comment.