Skip to content

Commit 0df1088

Browse files
authored
[ci] [c++] use 'pre-commit' to run 'cpplint', upgrade to 'cpplint' 2.0.2 (#7002)
* [ci] [c++] use 'pre-commit' to run 'cpplint', upgrade to 'cpplint' 2.0.2 * remove bashisms * one more pipefail use * another pipefail
1 parent d9bdda6 commit 0df1088

26 files changed

+224
-81
lines changed

.ci/lint-cpp.sh renamed to .ci/check-omp-pragmas.sh

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
#!/bin/bash
22

3-
set -e -E -u -o pipefail
4-
5-
echo "running cpplint"
6-
cpplint \
7-
--filter=-build/c++11,-build/include_subdir,-build/header_guard,-whitespace/line_length \
8-
--recursive ./src ./include ./R-package ./swig ./tests \
9-
|| exit 1
10-
echo "done running cpplint"
3+
set -e -u
114

125
echo "checking that all OpenMP pragmas specify num_threads()"
136
get_omp_pragmas_without_num_threads() {
@@ -28,11 +21,11 @@ get_omp_pragmas_without_num_threads() {
2821
# consider this a failure and stop execution of the script.
2922
#
3023
# ref: https://www.gnu.org/software/grep/manual/html_node/Exit-Status.html
31-
set +e +o pipefail
24+
set +e
3225
PROBLEMATIC_LINES=$(
3326
get_omp_pragmas_without_num_threads
3427
)
35-
set -e -o pipefail
28+
set -e
3629
if test "${PROBLEMATIC_LINES}" != ""; then
3730
get_omp_pragmas_without_num_threads
3831
echo "Found '#pragma omp parallel' not using explicit num_threads() configuration. Fix those."

.ci/test.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ if [[ $TASK == "lint" ]]; then
105105
conda create -q -y -n "${CONDA_ENV}" \
106106
"${CONDA_PYTHON_REQUIREMENT}" \
107107
'biome>=1.9.3' \
108-
'cpplint>=1.6.0' \
109108
'matplotlib-base>=3.9.1' \
110109
'mypy>=1.11.1' \
111110
'pre-commit>=3.8.0' \
@@ -118,8 +117,6 @@ if [[ $TASK == "lint" ]]; then
118117
bash ./.ci/run-pre-commit-mypy.sh || exit 1
119118
echo "Linting R code"
120119
Rscript ./.ci/lint-r-code.R "${BUILD_DIRECTORY}" || exit 1
121-
echo "Linting C++ code"
122-
bash ./.ci/lint-cpp.sh || exit 1
123120
echo "Linting JavaScript code"
124121
bash ./.ci/lint-js.sh || exit 1
125122
exit 0

.pre-commit-config.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ repos:
2222
hooks:
2323
- id: cmakelint
2424
args: ["--linelength=120", "--filter=-convention/filename,-package/stdargs,-readability/wonkycase"]
25+
- repo: https://github.com/cpplint/cpplint
26+
rev: '2.0.2'
27+
hooks:
28+
- id: cpplint
29+
args:
30+
- --recursive
31+
- --filter=-build/c++11,-build/include_subdir,-build/include_what_you_use,-build/header_guard,-whitespace/indent_namespace,-whitespace/line_length
32+
- repo: local
33+
hooks:
34+
- id: check-omp-pragmas
35+
name: check-omp-pragmas
36+
entry: sh
37+
args:
38+
- .ci/check-omp-pragmas.sh
39+
language: system
40+
pass_filenames: false
2541
- repo: https://github.com/adrienverge/yamllint
2642
rev: v1.37.1
2743
hooks:

include/LightGBM/dataset.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,13 @@ class Dataset {
554554
}
555555

556556
inline void FinishOneRow(int tid, data_size_t row_idx, const std::vector<bool>& is_feature_added) {
557-
if (is_finish_load_) { return; }
557+
if (is_finish_load_) {
558+
return;
559+
}
558560
for (auto fidx : feature_need_push_zeros_) {
559-
if (is_feature_added[fidx]) { continue; }
561+
if (is_feature_added[fidx]) {
562+
continue;
563+
}
560564
const int group = feature2group_[fidx];
561565
const int sub_feature = feature2subfeature_[fidx];
562566
feature_groups_[group]->PushData(tid, sub_feature, row_idx, 0.0f);
@@ -587,10 +591,14 @@ class Dataset {
587591
}
588592

589593
inline void PushOneRow(int tid, data_size_t row_idx, const std::vector<std::pair<int, double>>& feature_values) {
590-
if (is_finish_load_) { return; }
594+
if (is_finish_load_) {
595+
return;
596+
}
591597
std::vector<bool> is_feature_added(num_features_, false);
592598
for (auto& inner_data : feature_values) {
593-
if (inner_data.first >= num_total_features_) { continue; }
599+
if (inner_data.first >= num_total_features_) {
600+
continue;
601+
}
594602
int feature_idx = used_feature_map_[inner_data.first];
595603
if (feature_idx >= 0) {
596604
is_feature_added[feature_idx] = true;

include/LightGBM/utils/array_args.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,33 @@ class ArrayArgs {
112112
VAL_T v = ref[end - 1];
113113
for (;;) {
114114
while (ref[++i] > v) {}
115-
while (v > ref[--j]) { if (j == start) { break; } }
116-
if (i >= j) { break; }
115+
while (v > ref[--j]) {
116+
if (j == start) {
117+
break;
118+
}
119+
}
120+
if (i >= j) {
121+
break;
122+
}
117123
std::swap(ref[i], ref[j]);
118-
if (ref[i] == v) { p++; std::swap(ref[p], ref[i]); }
119-
if (v == ref[j]) { q--; std::swap(ref[j], ref[q]); }
124+
if (ref[i] == v) {
125+
p++;
126+
std::swap(ref[p], ref[i]);
127+
}
128+
if (v == ref[j]) {
129+
q--;
130+
std::swap(ref[j], ref[q]);
131+
}
120132
}
121133
std::swap(ref[i], ref[end - 1]);
122134
j = i - 1;
123135
i = i + 1;
124-
for (int k = start; k <= p; k++, j--) { std::swap(ref[k], ref[j]); }
125-
for (int k = end - 2; k >= q; k--, i++) { std::swap(ref[i], ref[k]); }
136+
for (int k = start; k <= p; k++, j--) {
137+
std::swap(ref[k], ref[j]);
138+
}
139+
for (int k = end - 2; k >= q; k--, i++) {
140+
std::swap(ref[i], ref[k]);
141+
}
126142
*l = j;
127143
*r = i;
128144
}

include/LightGBM/utils/common.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,18 @@ inline static const char* Atof(const char* p, double* out) {
315315
}
316316
if (expon > 308) expon = 308;
317317
// Calculate scaling factor.
318-
while (expon >= 50) { scale *= 1E50; expon -= 50; }
319-
while (expon >= 8) { scale *= 1E8; expon -= 8; }
320-
while (expon > 0) { scale *= 10.0; expon -= 1; }
318+
while (expon >= 50) {
319+
scale *= 1E50;
320+
expon -= 50;
321+
}
322+
while (expon >= 8) {
323+
scale *= 1E8;
324+
expon -= 8;
325+
}
326+
while (expon > 0) {
327+
scale *= 10.0;
328+
expon -= 1;
329+
}
321330
}
322331
// Return signed and scaled floating point result.
323332
*out = sign * (frac ? (value / scale) : (value * scale));
@@ -713,7 +722,9 @@ static void ParallelSort(_RanIt _First, _RanIt _Last, _Pr _Pred, _VTRanIt*) {
713722
size_t mid = left + s;
714723
size_t right = mid + s;
715724
right = std::min(len, right);
716-
if (mid >= right) { continue; }
725+
if (mid >= right) {
726+
continue;
727+
}
717728
std::copy(_First + left, _First + mid, buf + left);
718729
std::merge(buf + left, buf + mid, _First + mid, _First + right, _First + left, _Pred);
719730
}

include/LightGBM/utils/openmp_wrapper.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,13 @@ class ThreadExceptionHelper {
6262
}
6363
void CaptureException() {
6464
// only catch first exception.
65-
if (ex_ptr_ != nullptr) { return; }
65+
if (ex_ptr_ != nullptr) {
66+
return;
67+
}
6668
std::unique_lock<std::mutex> guard(lock_);
67-
if (ex_ptr_ != nullptr) { return; }
69+
if (ex_ptr_ != nullptr) {
70+
return;
71+
}
6872
ex_ptr_ = std::current_exception();
6973
}
7074

include/LightGBM/utils/text_reader.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ class TextReader {
124124
++i;
125125
++total_cnt;
126126
// skip end of line
127-
while ((buffer_process[i] == '\n' || buffer_process[i] == '\r') && i < read_cnt) { ++i; }
127+
while ((buffer_process[i] == '\n' || buffer_process[i] == '\r') && i < read_cnt) {
128+
++i;
129+
}
128130
last_i = i;
129131
} else {
130132
++i;
@@ -284,7 +286,9 @@ class TextReader {
284286
++i;
285287
++total_cnt;
286288
// skip end of line
287-
while ((buffer_process[i] == '\n' || buffer_process[i] == '\r') && i < read_cnt) { ++i; }
289+
while ((buffer_process[i] == '\n' || buffer_process[i] == '\r') && i < read_cnt) {
290+
++i;
291+
}
288292
last_i = i;
289293
} else {
290294
++i;

src/application/application.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ void Application::LoadData() {
121121
if (config_.is_provide_training_metric) {
122122
for (auto metric_type : config_.metric) {
123123
auto metric = std::unique_ptr<Metric>(Metric::CreateMetric(metric_type, config_));
124-
if (metric == nullptr) { continue; }
124+
if (metric == nullptr) {
125+
continue;
126+
}
125127
metric->Init(train_data_->metadata(), train_data_->num_data());
126128
train_metric_.push_back(std::move(metric));
127129
}
@@ -149,7 +151,9 @@ void Application::LoadData() {
149151
valid_metrics_.emplace_back();
150152
for (auto metric_type : config_.metric) {
151153
auto metric = std::unique_ptr<Metric>(Metric::CreateMetric(metric_type, config_));
152-
if (metric == nullptr) { continue; }
154+
if (metric == nullptr) {
155+
continue;
156+
}
153157
metric->Init(valid_datas_.back()->metadata(),
154158
valid_datas_.back()->num_data());
155159
valid_metrics_.back().push_back(std::move(metric));

src/boosting/gbdt.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ void GBDT::AddValidDataset(const Dataset* valid_data,
210210

211211
if (early_stopping_round_ > 0) {
212212
auto num_metrics = valid_metrics.size();
213-
if (es_first_metric_only_) { num_metrics = 1; }
213+
if (es_first_metric_only_) {
214+
num_metrics = 1;
215+
}
214216
best_iter_.emplace_back(num_metrics, 0);
215217
best_score_.emplace_back(num_metrics, kMinScore);
216218
best_msg_.emplace_back(num_metrics);
@@ -452,7 +454,9 @@ bool GBDT::TrainOneIter(const score_t* gradients, const score_t* hessians) {
452454
}
453455

454456
void GBDT::RollbackOneIter() {
455-
if (iter_ <= 0) { return; }
457+
if (iter_ <= 0) {
458+
return;
459+
}
456460
// reset score
457461
for (int cur_tree_id = 0; cur_tree_id < num_tree_per_iteration_; ++cur_tree_id) {
458462
auto curr_tree = models_.size() - num_tree_per_iteration_ + cur_tree_id;
@@ -588,15 +592,19 @@ std::string GBDT::OutputMetric(int iter) {
588592
msg_buf << tmp_buf.str() << '\n';
589593
}
590594
}
591-
if (es_first_metric_only_ && j > 0) { continue; }
595+
if (es_first_metric_only_ && j > 0) {
596+
continue;
597+
}
592598
if (ret.empty() && early_stopping_round_ > 0) {
593599
auto cur_score = valid_metrics_[i][j]->factor_to_bigger_better() * test_scores.back();
594600
if (cur_score - best_score_[i][j] > early_stopping_min_delta_) {
595601
best_score_[i][j] = cur_score;
596602
best_iter_[i][j] = iter;
597603
meet_early_stopping_pairs.emplace_back(i, j);
598604
} else {
599-
if (iter - best_iter_[i][j] >= early_stopping_round_) { ret = best_msg_[i][j]; }
605+
if (iter - best_iter_[i][j] >= early_stopping_round_) {
606+
ret = best_msg_[i][j];
607+
}
600608
}
601609
}
602610
}

0 commit comments

Comments
 (0)