Skip to content

Commit b0c4412

Browse files
[CI] Add clang-tidy check with clang-analyzer and performance checks (#490)
1 parent 0d20509 commit b0c4412

File tree

123 files changed

+1024
-786
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+1024
-786
lines changed

.clang-tidy

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
---
19+
# TODO: add more checks
20+
# - bugprone
21+
# - cppcoreguidelines
22+
# - portability
23+
# - readability
24+
Checks: >
25+
-*,
26+
clang-analyzer-*,
27+
-clang-analyzer-security.insecureAPI.rand,
28+
performance-*,
29+
-performance-inefficient-string-concatenation
30+
WarningsAsErrors: '*'
31+
HeaderFilterRegex: '^(?!.*PulsarApi\.pb\.h$).*$'
32+
FormatStyle: none

.github/workflows/ci-pr-validation.yaml

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,37 @@ jobs:
7777
cmake -S wireshark -B build-wireshark
7878
cmake --build build-wireshark
7979
80+
lint:
81+
name: Lint
82+
needs: formatting-check
83+
runs-on: ubuntu-24.04
84+
timeout-minutes: 120
85+
86+
steps:
87+
- name: checkout
88+
uses: actions/checkout@v3
89+
with:
90+
fetch-depth: 0
91+
submodules: recursive
92+
93+
- name: Build the project
94+
run: |
95+
cmake -B build -DINTEGRATE_VCPKG=ON -DBUILD_TESTS=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
96+
cmake --build build -j8
97+
98+
- name: Tidy check
99+
run: |
100+
sudo apt-get install -y clang-tidy
101+
./build-support/run_clang_tidy.sh
102+
if [[ $? -ne 0 ]]; then
103+
echo "clang-tidy failed"
104+
exit 1
105+
fi
106+
80107
unit-tests:
81108
name: Run unit tests
82109
needs: formatting-check
83-
runs-on: ubuntu-22.04
110+
runs-on: ubuntu-24.04
84111
timeout-minutes: 120
85112

86113
steps:
@@ -92,8 +119,8 @@ jobs:
92119

93120
- name: Build core libraries
94121
run: |
95-
cmake . -DINTEGRATE_VCPKG=ON -DBUILD_TESTS=OFF
96-
cmake --build . -j8
122+
cmake -B build -DINTEGRATE_VCPKG=ON -DBUILD_TESTS=OFF
123+
cmake --build build -j8
97124
98125
- name: Check formatting
99126
run: |
@@ -105,29 +132,29 @@ jobs:
105132
106133
- name: Build tests
107134
run: |
108-
cmake . -DINTEGRATE_VCPKG=ON -DBUILD_TESTS=ON
109-
cmake --build . -j8
135+
cmake -B build -DINTEGRATE_VCPKG=ON -DBUILD_TESTS=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
136+
cmake --build build -j8
110137
111138
- name: Install gtest-parallel
112139
run: |
113140
sudo curl -o /gtest-parallel https://raw.githubusercontent.com/google/gtest-parallel/master/gtest_parallel.py
114141
115142
- name: Run unit tests
116-
run: RETRY_FAILED=3 ./run-unit-tests.sh
143+
run: RETRY_FAILED=3 CMAKE_BUILD_DIRECTORY=./build ./run-unit-tests.sh
117144

118145
- name: Build perf tools
119146
run: |
120-
cmake . -DINTEGRATE_VCPKG=ON -DBUILD_TESTS=ON -DBUILD_PERF_TOOLS=ON
121-
cmake --build . -j8
147+
cmake -B build -DINTEGRATE_VCPKG=ON -DBUILD_TESTS=ON -DBUILD_PERF_TOOLS=ON
148+
cmake --build build -j8
122149
123150
- name: Verify custom vcpkg installation
124151
run: |
125152
mv vcpkg /tmp/
126-
cmake -B build -DINTEGRATE_VCPKG=ON -DCMAKE_TOOLCHAIN_FILE="/tmp/vcpkg/scripts/buildsystems/vcpkg.cmake"
153+
cmake -B build-2 -DINTEGRATE_VCPKG=ON -DCMAKE_TOOLCHAIN_FILE="/tmp/vcpkg/scripts/buildsystems/vcpkg.cmake"
127154
128155
cpp20-build:
129156
name: Build with the C++20 standard
130-
needs: formatting-check
157+
needs: lint
131158
runs-on: ubuntu-22.04
132159
timeout-minutes: 60
133160

@@ -270,7 +297,7 @@ jobs:
270297
package:
271298
name: Build ${{matrix.pkg.name}} ${{matrix.cpu.platform}}
272299
runs-on: ubuntu-22.04
273-
needs: unit-tests
300+
needs: [lint, unit-tests]
274301
timeout-minutes: 500
275302

276303
strategy:
@@ -314,7 +341,7 @@ jobs:
314341
timeout-minutes: 120
315342
name: Build CPP Client on macOS with static dependencies
316343
runs-on: macos-14
317-
needs: formatting-check
344+
needs: lint
318345
steps:
319346
- name: checkout
320347
uses: actions/checkout@v3
@@ -329,7 +356,7 @@ jobs:
329356
check-completion:
330357
name: Check Completion
331358
runs-on: ubuntu-latest
332-
needs: [formatting-check, wireshark-dissector-build, unit-tests, cpp20-build, cpp-build-windows, package, cpp-build-macos]
359+
needs: [formatting-check, wireshark-dissector-build, lint, unit-tests, cpp20-build, cpp-build-windows, package, cpp-build-macos]
333360

334361
steps:
335362
- run: true

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,6 @@ Testing
106106
.test-token.txt
107107
pkg/mac/.build
108108
pkg/mac/.install
109+
110+
# Used in ./build-support/run_clang_tidy.sh
111+
files.txt

CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ else() # GCC or Clang are mostly compatible:
9797
# Options unique to Clang or GCC:
9898
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
9999
add_compile_options(-Qunused-arguments)
100-
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.1))
101-
add_compile_options(-Wno-stringop-truncation)
102100
endif()
103101
endif()
104102

build-support/run_clang_tidy.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Licensed to the Apache Software Foundation (ASF) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The ASF licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
#
20+
21+
set -e
22+
cd `dirname $0`/..
23+
24+
FILES=$(find $PWD/include $PWD/lib $PWD/tests $PWD/examples -name "*.h" -o -name "*.cc" \
25+
| grep -v "lib\/c\/" | grep -v "lib\/checksum\/" | grep -v "lib\/lz4\/" \
26+
| grep -v "include\/pulsar\/c\/" | grep -v "tests\/c\/")
27+
28+
rm -f files.txt
29+
for FILE in $FILES; do
30+
echo $FILE >> files.txt
31+
done
32+
# run-clang-tidy from older version of LLVM requires python but not python3 as the env, so we cannot run it directly
33+
SCRIPT=$(which run-clang-tidy)
34+
set +e
35+
nproc
36+
if [[ $? == 0 ]]; then
37+
python3 $SCRIPT -p build -j$(nproc) $(cat files.txt)
38+
else
39+
python3 $SCRIPT -p build -j8 $(cat files.txt)
40+
fi
41+
rm -f files.txt

include/pulsar/Authentication.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,7 @@ class PULSAR_PUBLIC AuthAthenz : public Authentication {
394394
// currently mainly works for access token
395395
class Oauth2TokenResult {
396396
public:
397-
enum
398-
{
399-
undefined_expiration = -1
400-
};
397+
static constexpr int undefined_expiration = -1;
401398

402399
Oauth2TokenResult();
403400
~Oauth2TokenResult();

include/pulsar/Client.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class PULSAR_PUBLIC Client {
101101
* @param callback the callback that is triggered when the producer is created successfully or not
102102
* @param callback Callback function that is invoked when the operation is completed
103103
*/
104-
void createProducerAsync(const std::string& topic, CreateProducerCallback callback);
104+
void createProducerAsync(const std::string& topic, const CreateProducerCallback& callback);
105105

106106
/**
107107
* Asynchronously create a producer with the customized ProducerConfiguration for publishing on a specific
@@ -110,8 +110,8 @@ class PULSAR_PUBLIC Client {
110110
* @param topic the name of the topic where to produce
111111
* @param conf the customized ProducerConfiguration
112112
*/
113-
void createProducerAsync(const std::string& topic, ProducerConfiguration conf,
114-
CreateProducerCallback callback);
113+
void createProducerAsync(const std::string& topic, const ProducerConfiguration& conf,
114+
const CreateProducerCallback& callback);
115115

116116
/**
117117
* Subscribe to a given topic and subscription combination with the default ConsumerConfiguration
@@ -144,7 +144,7 @@ class PULSAR_PUBLIC Client {
144144
* default ConsumerConfiguration are asynchronously subscribed successfully or not
145145
*/
146146
void subscribeAsync(const std::string& topic, const std::string& subscriptionName,
147-
SubscribeCallback callback);
147+
const SubscribeCallback& callback);
148148

149149
/**
150150
* Asynchronously subscribe to a given topic and subscription combination with the customized
@@ -157,7 +157,7 @@ class PULSAR_PUBLIC Client {
157157
* customized ConsumerConfiguration are asynchronously subscribed successfully or not
158158
*/
159159
void subscribeAsync(const std::string& topic, const std::string& subscriptionName,
160-
const ConsumerConfiguration& conf, SubscribeCallback callback);
160+
const ConsumerConfiguration& conf, const SubscribeCallback& callback);
161161

162162
/**
163163
* Subscribe to multiple topics under the same namespace.
@@ -191,7 +191,7 @@ class PULSAR_PUBLIC Client {
191191
192192
*/
193193
void subscribeAsync(const std::vector<std::string>& topics, const std::string& subscriptionName,
194-
SubscribeCallback callback);
194+
const SubscribeCallback& callback);
195195

196196
/**
197197
* Asynchronously subscribe to a list of topics and subscription combination using the customized
@@ -204,7 +204,7 @@ class PULSAR_PUBLIC Client {
204204
* the customized ConsumerConfiguration are asynchronously subscribed successfully or not
205205
*/
206206
void subscribeAsync(const std::vector<std::string>& topics, const std::string& subscriptionName,
207-
const ConsumerConfiguration& conf, SubscribeCallback callback);
207+
const ConsumerConfiguration& conf, const SubscribeCallback& callback);
208208

209209
/**
210210
* Subscribe to multiple topics, which match given regexPattern, under the same namespace.
@@ -227,7 +227,7 @@ class PULSAR_PUBLIC Client {
227227
* SubscribeCallback)
228228
*/
229229
void subscribeWithRegexAsync(const std::string& regexPattern, const std::string& subscriptionName,
230-
SubscribeCallback callback);
230+
const SubscribeCallback& callback);
231231

232232
/**
233233
* Asynchronously subscribe to multiple topics (which match given regexPatterns) with the customized
@@ -240,7 +240,7 @@ class PULSAR_PUBLIC Client {
240240
* ConsumerConfiguration under the same namespace are asynchronously subscribed successfully or not
241241
*/
242242
void subscribeWithRegexAsync(const std::string& regexPattern, const std::string& subscriptionName,
243-
const ConsumerConfiguration& conf, SubscribeCallback callback);
243+
const ConsumerConfiguration& conf, const SubscribeCallback& callback);
244244

245245
/**
246246
* Create a topic reader with given {@code ReaderConfiguration} for reading messages from the specified
@@ -301,7 +301,7 @@ class PULSAR_PUBLIC Client {
301301
* @return the Reader object
302302
*/
303303
void createReaderAsync(const std::string& topic, const MessageId& startMessageId,
304-
const ReaderConfiguration& conf, ReaderCallback callback);
304+
const ReaderConfiguration& conf, const ReaderCallback& callback);
305305

306306
/**
307307
* Create a table view with given {@code TableViewConfiguration} for specified topic.
@@ -331,7 +331,7 @@ class PULSAR_PUBLIC Client {
331331
* built from a message that already exists
332332
*/
333333
void createTableViewAsync(const std::string& topic, const TableViewConfiguration& conf,
334-
TableViewCallback callBack);
334+
const TableViewCallback& callBack);
335335

336336
/**
337337
* Get the list of partitions for a given topic.
@@ -363,7 +363,7 @@ class PULSAR_PUBLIC Client {
363363
* the callback that will be invoked when the list of partitions is available
364364
* @since 2.3.0
365365
*/
366-
void getPartitionsForTopicAsync(const std::string& topic, GetPartitionsCallback callback);
366+
void getPartitionsForTopicAsync(const std::string& topic, const GetPartitionsCallback& callback);
367367

368368
/**
369369
*
@@ -380,7 +380,7 @@ class PULSAR_PUBLIC Client {
380380
* @param callback the callback that is triggered when the Pulsar client is asynchronously closed
381381
* successfully or not
382382
*/
383-
void closeAsync(CloseCallback callback);
383+
void closeAsync(const CloseCallback& callback);
384384

385385
/**
386386
* Perform immediate shutdown of Pulsar client.
@@ -415,7 +415,7 @@ class PULSAR_PUBLIC Client {
415415
std::function<void(Result, const SchemaInfo&)> callback);
416416

417417
private:
418-
Client(const std::shared_ptr<ClientImpl>);
418+
Client(const std::shared_ptr<ClientImpl>&);
419419

420420
friend class PulsarFriend;
421421
friend class PulsarWrapper;

include/pulsar/ClientConfiguration.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <pulsar/Logger.h>
2424
#include <pulsar/defines.h>
2525

26+
#include <cstdint>
27+
2628
namespace pulsar {
2729
class PulsarWrapper;
2830
struct ClientConfigurationImpl;
@@ -32,7 +34,7 @@ class PULSAR_PUBLIC ClientConfiguration {
3234
~ClientConfiguration();
3335
ClientConfiguration(const ClientConfiguration&);
3436
ClientConfiguration& operator=(const ClientConfiguration&);
35-
enum ProxyProtocol
37+
enum ProxyProtocol : uint8_t
3638
{
3739
SNI = 0
3840
};

include/pulsar/CompressionType.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
#ifndef PULSAR_COMPRESSIONTYPE_H_
2020
#define PULSAR_COMPRESSIONTYPE_H_
2121

22+
#include <cstdint>
23+
2224
namespace pulsar {
23-
enum CompressionType
25+
enum CompressionType : std::uint8_t
2426
{
2527
CompressionNone = 0,
2628
CompressionLZ4 = 1,

include/pulsar/ConsoleLoggerFactory.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include <pulsar/Logger.h>
2323

24+
#include <memory>
25+
2426
namespace pulsar {
2527

2628
class ConsoleLoggerFactoryImpl;

0 commit comments

Comments
 (0)