Skip to content

Commit 5aa37c7

Browse files
committed
Address 'bugprone-*' clang-tidy remarks
1 parent c115709 commit 5aa37c7

16 files changed

Lines changed: 69 additions & 48 deletions

.clang-tidy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Checks: >
2+
bugprone-*,
23
modernize-*,
34
performance-*,
45
portability-*,
@@ -51,6 +52,7 @@ CheckOptions:
5152
- { key: readability-identifier-naming.MemberConstantPrefix, value: k }
5253
- { key: readability-identifier-naming.StaticConstantCase, value: CamelCase }
5354
- { key: readability-identifier-naming.StaticConstantPrefix, value: k }
55+
- { key: bugprone-exception-escape.CheckMain, value: 0 }
5456
- { key: readability-implicit-bool-conversion.AllowIntegerConditions, value: 1 }
5557
- { key: readability-implicit-bool-conversion.AllowPointerConditions, value: 1 }
5658
- { key: readability-function-cognitive-complexity.IgnoreMacros, value: 1 }

app/Converters/reader_weights_sample_onnx.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ int main() {
3030
if (value.is_array()) {
3131
std::cout << "[";
3232
for (const auto& v : value) {
33-
if (v.is_number())
33+
if (v.is_number()) {
3434
std::cout << v.get<float>() << " ";
35-
else if (v.is_string())
35+
} else if (v.is_string()) {
3636
std::cout << v.get<std::string>() << " ";
37+
}
3738
}
3839
std::cout << "]";
3940
} else if (value.is_number()) {

app/Graph/build.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
4545

4646
for (const auto& layer_data : model_data) {
4747
std::string layer_type = layer_data["type"];
48-
if (comments)
48+
if (comments) {
4949
std::cout << "Processing layer of type: " << layer_type << std::endl;
50+
}
5051

5152
it_lab_ai::Tensor tensor =
5253
it_lab_ai::create_tensor_from_json(layer_data, it_lab_ai::Type::kFloat);
@@ -93,8 +94,9 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
9394
layer_ptrs.push_back(ew_layer.get());
9495
layers.push_back(std::move(ew_layer));
9596
layerpostop.push_back(true);
96-
if (comments)
97+
if (comments) {
9798
std::cout << "Element wise (relu) added to layers" << std::endl;
99+
}
98100
}
99101
if (layer_type.find("Dense") != std::string::npos) {
100102
it_lab_ai::Tensor tmp_bias = it_lab_ai::make_tensor(tensor.get_bias());
@@ -113,9 +115,10 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
113115
} else {
114116
pooltype = "average";
115117
}
116-
if (comments)
118+
if (comments) {
117119
std::cout << "PoolingLayer shape: " << shape[0] << "x" << shape[1]
118120
<< std::endl;
121+
}
119122
auto pool_layer =
120123
std::make_unique<it_lab_ai::PoolingLayer>(shape, pooltype, kDefault);
121124
layer_ptrs.push_back(pool_layer.get());
@@ -138,15 +141,17 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
138141
layer_ptrs.push_back(dropout_layer.get());
139142
layers.push_back(std::move(dropout_layer));
140143
layerpostop.push_back(false);
141-
if (comments)
144+
if (comments) {
142145
std::cout
143146
<< "DropOutLayer added to layers with probability 0.4 (turned "
144147
"off for inference)."
145148
<< std::endl;
149+
}
146150
}
147151
}
148-
if (comments)
152+
if (comments) {
149153
std::cout << "number of layers - " << layers.size() + 1 << std::endl;
154+
}
150155
auto a1 = std::make_unique<it_lab_ai::InputLayer>(it_lab_ai::kNchw,
151156
it_lab_ai::kNchw);
152157
Layer* a1_ptr = a1.get();
@@ -157,17 +162,19 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
157162
if (comments) std::cout << "Input set in graph." << std::endl;
158163

159164
graph.makeConnection(a1_ptr, layer_ptrs[0]);
160-
if (comments)
165+
if (comments) {
161166
std::cout << "Connection made between InputLayer and first layer."
162167
<< std::endl;
168+
}
163169

164170
for (size_t i = 0; i < layers.size() - 1; ++i) {
165171
if (layerpostop[i]) {
166172
layer_ptrs[i - 1]->postops.layers.push_back(layer_ptrs[i]);
167173
layer_ptrs[i - 1]->postops.count++;
168174
graph.makeConnection(layer_ptrs[i - 1], layer_ptrs[i + 1]);
169-
} else if (!layerpostop[i + 1])
175+
} else if (!layerpostop[i + 1]) {
170176
graph.makeConnection(layer_ptrs[i], layer_ptrs[i + 1]);
177+
}
171178
}
172179

173180
graph.setOutput(layer_ptrs.back(), output);
@@ -469,11 +476,12 @@ ParseResult parse_json_model(const std::string& json_path, bool comments) {
469476
} else if (layer_type.find("Dropout") != std::string::npos) {
470477
auto dropout_layer = std::make_unique<it_lab_ai::DropOutLayer>(0.0);
471478
layer = std::move(dropout_layer);
472-
if (comments)
479+
if (comments) {
473480
std::cout
474481
<< "DropOutLayer added to layers with probability 0.4 (turned "
475482
"off for inference)."
476483
<< std::endl;
484+
}
477485
} else if (layer_type == "GlobalAveragePool") {
478486
auto pool_layer = std::make_unique<it_lab_ai::PoolingLayer>(
479487
it_lab_ai::Shape({0, 0}), "average", kDefault);
@@ -677,15 +685,15 @@ ParseResult parse_json_model(const std::string& json_path, bool comments) {
677685
}
678686
} else {
679687
it_lab_ai::BinaryOpLayer::Operation op;
680-
if (layer_type == "Add")
688+
if (layer_type == "Add") {
681689
op = it_lab_ai::BinaryOpLayer::Operation::kAdd;
682-
else if (layer_type == "Sub")
690+
} else if (layer_type == "Sub") {
683691
op = it_lab_ai::BinaryOpLayer::Operation::kSub;
684-
else if (layer_type == "Mul")
692+
} else if (layer_type == "Mul") {
685693
op = it_lab_ai::BinaryOpLayer::Operation::kMul;
686-
else if (layer_type == "Div")
694+
} else if (layer_type == "Div") {
687695
op = it_lab_ai::BinaryOpLayer::Operation::kDiv;
688-
else {
696+
} else {
689697
op = it_lab_ai::BinaryOpLayer::Operation::kAdd;
690698
}
691699

include/layers/DropOutLayer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class DropOutLayer : public Layer {
1111
bool training_mode_;
1212

1313
public:
14-
DropOutLayer(double drop_rate = 0.0, bool training_mode = false)
14+
explicit DropOutLayer(double drop_rate = 0.0, bool training_mode = false)
1515
: Layer(kDropout) {
1616
drop_rate_ = drop_rate;
1717
training_mode_ = training_mode;

include/layers/EWLayer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ T relu(const T& value) {
2020
class EWLayer : public Layer {
2121
public:
2222
EWLayer() : Layer(kElementWise), func_("none"), alpha_(0.0F), beta_(0.0F) {}
23-
EWLayer(std::string function, float alpha = 0.0F, float beta = 0.0F)
23+
explicit EWLayer(std::string function, float alpha = 0.0F, float beta = 0.0F)
2424
: Layer(kElementWise),
2525
func_(std::move(function)),
2626
alpha_(alpha),

include/layers/FCLayer.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ template <typename ValueType>
116116
FCLayerImpl<ValueType>::FCLayerImpl(const std::vector<ValueType>& input_weights,
117117
const Shape& input_weights_shape,
118118
const std::vector<ValueType>& input_bias)
119-
: LayerImpl<ValueType>(1, 1), weights_(input_weights), bias_(input_bias) {
119+
: LayerImpl<ValueType>(Shape({input_weights_shape[0]}),
120+
Shape({input_weights_shape[1]})),
121+
weights_(input_weights),
122+
bias_(input_bias) {
120123
if (input_weights.empty()) {
121124
throw std::invalid_argument("Empty weights for FCLayer");
122125
}

include/layers/FlattenLayer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class FlattenLayer : public Layer {
1515

1616
public:
1717
FlattenLayer() : Layer(kFlatten), order_({0, 1, 2, 3}), axis_(0) {}
18-
FlattenLayer(int axis) : Layer(kFlatten), order_({}), axis_(axis) {}
19-
FlattenLayer(const std::vector<size_t>& order)
18+
explicit FlattenLayer(int axis) : Layer(kFlatten), order_({}), axis_(axis) {}
19+
explicit FlattenLayer(const std::vector<size_t>& order)
2020
: Layer(kFlatten), order_(order), axis_(0) {}
2121
void run(const std::vector<Tensor>& input,
2222
std::vector<Tensor>& output) override;

include/layers/Layer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct PostOperations {
4949
class Layer {
5050
public:
5151
Layer() = default;
52-
Layer(LayerType type) : type_(type) {}
52+
explicit Layer(LayerType type) : type_(type) {}
5353
virtual ~Layer() = default;
5454
PostOperations postops;
5555
int getID() const { return id_; }

include/layers/OutputLayer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace it_lab_ai {
1212
class OutputLayer : public Layer {
1313
public:
1414
OutputLayer() : Layer(kOutput) {}
15-
OutputLayer(const std::vector<std::string>& labels)
15+
explicit OutputLayer(const std::vector<std::string>& labels)
1616
: Layer(kOutput), labels_(labels) {}
1717
void run(const std::vector<Tensor>& input,
1818
std::vector<Tensor>& output) override {

include/layers/PoolingLayer.hpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ enum PoolingType : uint8_t { kAverage, kMax };
1818

1919
class PoolingLayer : public Layer {
2020
public:
21-
PoolingLayer(const Shape& pooling_shape, const Shape& strides = {2, 2},
22-
const Shape& pads = {0, 0, 0, 0},
23-
const Shape& dilations = {1, 1}, bool ceil_mode = false,
24-
std::string pooling_type = "average",
25-
ImplType implType = kDefault)
21+
explicit PoolingLayer(const Shape& pooling_shape,
22+
const Shape& strides = {2, 2},
23+
const Shape& pads = {0, 0, 0, 0},
24+
const Shape& dilations = {1, 1}, bool ceil_mode = false,
25+
std::string pooling_type = "average",
26+
ImplType implType = kDefault)
2627
: Layer(kPooling),
2728
poolingShape_(pooling_shape),
2829
strides_(strides),
@@ -31,8 +32,9 @@ class PoolingLayer : public Layer {
3132
ceil_mode_(ceil_mode),
3233
poolingType_(std::move(pooling_type)),
3334
implType_(implType) {}
34-
PoolingLayer(const Shape& pooling_shape, std::string pooling_type = "average",
35-
ImplType implType = kDefault)
35+
explicit PoolingLayer(const Shape& pooling_shape,
36+
std::string pooling_type = "average",
37+
ImplType implType = kDefault)
3638
: Layer(kPooling),
3739
poolingShape_(pooling_shape),
3840
strides_({2, 2}),
@@ -250,9 +252,10 @@ std::vector<ValueType> PoolingLayerImpl<ValueType>::run(
250252
if (batch_dim >= 0) input_coords[batch_dim] = n;
251253
if (channel_dim >= 0) input_coords[channel_dim] = c;
252254
input_coords[this->inputShape_.dims() - spatial_dims] = pos_h;
253-
if (spatial_dims > 1)
255+
if (spatial_dims > 1) {
254256
input_coords[this->inputShape_.dims() - spatial_dims + 1] =
255257
pos_w;
258+
}
256259

257260
size_t input_index = this->inputShape_.get_index(input_coords);
258261
pooling_buf.push_back(input[input_index]);
@@ -264,8 +267,9 @@ std::vector<ValueType> PoolingLayerImpl<ValueType>::run(
264267
if (batch_dim >= 0) output_coords[batch_dim] = n;
265268
if (channel_dim >= 0) output_coords[channel_dim] = c;
266269
output_coords[this->outputShape_.dims() - spatial_dims] = h;
267-
if (spatial_dims > 1)
270+
if (spatial_dims > 1) {
268271
output_coords[this->outputShape_.dims() - spatial_dims + 1] = w;
272+
}
269273

270274
size_t output_index = this->outputShape_.get_index(output_coords);
271275

@@ -377,9 +381,10 @@ std::vector<ValueType> PoolingLayerImplTBB<ValueType>::run(
377381
if (channel_dim >= 0) input_coords[channel_dim] = c;
378382
input_coords[this->inputShape_.dims() -
379383
spatial_dims] = pos_h;
380-
if (spatial_dims > 1)
384+
if (spatial_dims > 1) {
381385
input_coords[this->inputShape_.dims() -
382386
spatial_dims + 1] = pos_w;
387+
}
383388

384389
size_t input_index =
385390
this->inputShape_.get_index(input_coords);
@@ -394,9 +399,10 @@ std::vector<ValueType> PoolingLayerImplTBB<ValueType>::run(
394399
if (channel_dim >= 0) output_coords[channel_dim] = c;
395400
output_coords[this->outputShape_.dims() - spatial_dims] =
396401
h;
397-
if (spatial_dims > 1)
402+
if (spatial_dims > 1) {
398403
output_coords[this->outputShape_.dims() - spatial_dims +
399404
1] = w;
405+
}
400406

401407
size_t output_index =
402408
this->outputShape_.get_index(output_coords);

0 commit comments

Comments
 (0)