Skip to content

Commit ab59545

Browse files
authored
added clang-tidy CI job and fixed warnings / added CMake option "DISABLE_CPP03_SYNTAX_CHECK" / CMake cleanups (#248)
1 parent 43f1733 commit ab59545

4 files changed

Lines changed: 70 additions & 23 deletions

File tree

.clang-tidy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
Checks: '*,-abseil-*,-altera-*,-android-*,-cert-*,-cppcoreguidelines-*,-fuchsia-*,-google-*,-hicpp-*,-linuxkernel-*,-llvm-*,-llvmlibc-*,-mpi-*,-objc-*,-openmp-*,-zircon-*,-misc-non-private-member-variables-in-classes,-modernize-avoid-c-arrays,-modernize-use-default-member-init,-modernize-use-using,-readability-braces-around-statements,-readability-function-size,-readability-implicit-bool-conversion,-readability-isolate-declaration,-readability-magic-numbers,-readability-simplify-boolean-expr,-readability-uppercase-literal-suffix,-modernize-use-auto,-modernize-use-trailing-return-type,-bugprone-branch-clone,-modernize-pass-by-value,-modernize-loop-convert,-modernize-use-emplace,-modernize-use-equals-default,-performance-noexcept-move-constructor,-modernize-use-equals-delete,-readability-identifier-length,-readability-function-cognitive-complexity,-modernize-return-braced-init-list,-misc-no-recursion,-bugprone-easily-swappable-parameters,-bugprone-narrowing-conversions,-concurrency-mt-unsafe,-modernize-loop-convert,-clang-analyzer-core.NullDereference,-performance-move-constructor-init,-performance-inefficient-string-concatenation,-performance-no-automatic-move'
3+
HeaderFilterRegex: '.*'
4+
WarningsAsErrors: '*'

.github/workflows/clang-tidy.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Syntax reference https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
2+
# Environment reference https://help.github.com/en/actions/reference/virtual-environments-for-github-hosted-runners
3+
name: clang-tidy
4+
5+
on: [push, pull_request]
6+
7+
jobs:
8+
build:
9+
10+
runs-on: ubuntu-22.04
11+
12+
steps:
13+
- uses: actions/checkout@v2
14+
15+
- name: Install missing software
16+
run: |
17+
sudo apt-get update
18+
sudo apt-get install -y cmake make
19+
20+
- name: Install clang
21+
run: |
22+
wget https://apt.llvm.org/llvm.sh
23+
chmod +x llvm.sh
24+
sudo ./llvm.sh 14
25+
26+
- name: Prepare CMake
27+
run: |
28+
mkdir cmake.output
29+
cd cmake.output
30+
cmake -G "Unix Makefiles" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DDISABLE_CPP03_SYNTAX_CHECK=ON ..
31+
cd ..
32+
env:
33+
CXX: clang-14
34+
35+
- name: Clang-Tidy
36+
run: |
37+
run-clang-tidy-14 -q -j $(nproc) -p=cmake.output -extra-arg=-w

CMakeLists.txt

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cmake_minimum_required (VERSION 3.5)
22
project (simplecpp LANGUAGES CXX)
33

4+
option(DISABLE_CPP03_SYNTAX_CHECK "Disable the C++03 syntax check." OFF)
5+
46
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
57
add_compile_options(-Wall -Wextra -pedantic -Wcast-qual -Wfloat-equal -Wmissing-declarations -Wmissing-format-attribute -Wredundant-decls -Wshadow -Wundef -Wold-style-cast -Wno-multichar)
68
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
@@ -22,23 +24,27 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
2224
endif()
2325
endif()
2426

25-
add_executable(simplecpp simplecpp.cpp main.cpp)
27+
add_library(simplecpp_obj OBJECT simplecpp.cpp)
28+
29+
add_executable(simplecpp $<TARGET_OBJECTS:simplecpp_obj> main.cpp)
2630
set_property(TARGET simplecpp PROPERTY CXX_STANDARD 11)
2731

28-
# it is not possible to set a standard older than C++14 with Visual Studio
29-
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
30-
# we need to create a dummy library as -fsyntax-only will not produce any output files causing the build to fail
31-
add_library(simplecpp-03-syntax STATIC simplecpp.cpp)
32-
target_compile_options(simplecpp-03-syntax PRIVATE -std=c++03)
33-
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
34-
target_compile_options(simplecpp-03-syntax PRIVATE -Wno-long-long)
35-
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
36-
target_compile_options(simplecpp-03-syntax PRIVATE -Wno-c++11-long-long -Wno-c++11-compat)
32+
if (NOT DISABLE_CPP03_SYNTAX_CHECK)
33+
# it is not possible to set a standard older than C++14 with Visual Studio
34+
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
35+
# we need to create a dummy library as -fsyntax-only will not produce any output files causing the build to fail
36+
add_library(simplecpp-03-syntax OBJECT simplecpp.cpp)
37+
target_compile_options(simplecpp-03-syntax PRIVATE -std=c++03)
38+
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
39+
target_compile_options(simplecpp-03-syntax PRIVATE -Wno-long-long)
40+
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
41+
target_compile_options(simplecpp-03-syntax PRIVATE -Wno-c++11-long-long -Wno-c++11-compat)
42+
endif()
43+
add_dependencies(simplecpp simplecpp-03-syntax)
3744
endif()
38-
add_dependencies(simplecpp simplecpp-03-syntax)
3945
endif()
4046

41-
add_executable(testrunner simplecpp.cpp test.cpp)
47+
add_executable(testrunner $<TARGET_OBJECTS:simplecpp_obj> test.cpp)
4248
set_property(TARGET testrunner PROPERTY CXX_STANDARD 11)
4349

4450
enable_testing()

simplecpp.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,7 @@ namespace simplecpp {
18421842
return recursiveExpandToken(output, temp, loc, tok2, macros, expandedmacros2, parametertokens);
18431843
}
18441844

1845-
else if (tok->str() == DEFINED) {
1845+
if (tok->str() == DEFINED) {
18461846
const Token *tok2 = tok->next;
18471847
const Token *tok3 = tok2 ? tok2->next : nullptr;
18481848
const Token *tok4 = tok3 ? tok3->next : nullptr;
@@ -1989,7 +1989,7 @@ namespace simplecpp {
19891989
} else {
19901990
std::string strAB;
19911991

1992-
const bool varargs = variadic && args.size() >= 1U && B->str() == args[args.size()-1U];
1992+
const bool varargs = variadic && !args.empty() && B->str() == args[args.size()-1U];
19931993

19941994
if (expandArg(&tokensB, B, parametertokens)) {
19951995
if (tokensB.empty())
@@ -2008,7 +2008,7 @@ namespace simplecpp {
20082008
if (A->previous && A->previous->str() == "\\") {
20092009
if (strAB[0] == 'u' && strAB.size() == 5)
20102010
throw invalidHashHash::universalCharacterUB(tok->location, name(), A, strAB);
2011-
else if (strAB[0] == 'U' && strAB.size() == 9)
2011+
if (strAB[0] == 'U' && strAB.size() == 9)
20122012
throw invalidHashHash::universalCharacterUB(tok->location, name(), A, strAB);
20132013
}
20142014

@@ -2487,7 +2487,7 @@ long long simplecpp::characterLiteralToLL(const std::string& str)
24872487

24882488
std::size_t pos;
24892489

2490-
if (str.size() >= 1 && str[0] == '\'') {
2490+
if (!str.empty() && str[0] == '\'') {
24912491
narrow = true;
24922492
pos = 1;
24932493
} else if (str.size() >= 2 && str[0] == 'u' && str[1] == '\'') {
@@ -2611,7 +2611,7 @@ long long simplecpp::characterLiteralToLL(const std::string& str)
26112611
int additional_bytes;
26122612
if (value >= 0xf5) // higher values would result in code points above 0x10ffff
26132613
throw std::runtime_error("assumed UTF-8 encoded source, but sequence is invalid");
2614-
else if (value >= 0xf0)
2614+
if (value >= 0xf0)
26152615
additional_bytes = 3;
26162616
else if (value >= 0xe0)
26172617
additional_bytes = 2;
@@ -2678,7 +2678,7 @@ static void simplifyNumbers(simplecpp::TokenList &expr)
26782678
continue;
26792679
if (tok->str().compare(0,2,"0x") == 0)
26802680
tok->setstr(toString(stringToULL(tok->str())));
2681-
else if (!tok->number && tok->str().find('\'') != tok->str().npos)
2681+
else if (!tok->number && tok->str().find('\'') != std::string::npos)
26822682
tok->setstr(toString(simplecpp::characterLiteralToLL(tok->str())));
26832683
}
26842684
}
@@ -2840,7 +2840,7 @@ static bool hasFile(const std::map<std::string, simplecpp::TokenList *> &filedat
28402840
return !getFileName(filedata, sourcefile, header, dui, systemheader).empty();
28412841
}
28422842

2843-
std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::TokenList &rawtokens, std::vector<std::string> &fileNumbers, const simplecpp::DUI &dui, simplecpp::OutputList *outputList)
2843+
std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::TokenList &rawtokens, std::vector<std::string> &filenames, const simplecpp::DUI &dui, simplecpp::OutputList *outputList)
28442844
{
28452845
std::map<std::string, simplecpp::TokenList*> ret;
28462846

@@ -2856,16 +2856,16 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
28562856
std::ifstream fin(filename.c_str());
28572857
if (!fin.is_open()) {
28582858
if (outputList) {
2859-
simplecpp::Output err(fileNumbers);
2859+
simplecpp::Output err(filenames);
28602860
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
2861-
err.location = Location(fileNumbers);
2861+
err.location = Location(filenames);
28622862
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
28632863
outputList->push_back(err);
28642864
}
28652865
continue;
28662866
}
28672867

2868-
TokenList *tokenlist = new TokenList(fin, fileNumbers, filename, outputList);
2868+
TokenList *tokenlist = new TokenList(fin, filenames, filename, outputList);
28692869
if (!tokenlist->front()) {
28702870
delete tokenlist;
28712871
continue;
@@ -2904,7 +2904,7 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
29042904
if (!f.is_open())
29052905
continue;
29062906

2907-
TokenList *tokens = new TokenList(f, fileNumbers, header2, outputList);
2907+
TokenList *tokens = new TokenList(f, filenames, header2, outputList);
29082908
ret[header2] = tokens;
29092909
if (tokens->front())
29102910
filelist.push_back(tokens->front());

0 commit comments

Comments
 (0)