Skip to content

Commit e0cd5c1

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

File tree

19 files changed

+94
-60
lines changed

19 files changed

+94
-60
lines changed

.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/.clang-tidy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
InheritParentConfig: true
2+
Checks: >
3+
-bugprone-exception-escape

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: 31 additions & 16 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);
@@ -676,17 +684,24 @@ ParseResult parse_json_model(const std::string& json_path, bool comments) {
676684
continue;
677685
}
678686
} else {
679-
it_lab_ai::BinaryOpLayer::Operation op;
680-
if (layer_type == "Add")
687+
it_lab_ai::BinaryOpLayer::Operation op =
688+
it_lab_ai::BinaryOpLayer::Operation::kAdd;
689+
bool supported_type = true;
690+
691+
if (layer_type == "Add") {
681692
op = it_lab_ai::BinaryOpLayer::Operation::kAdd;
682-
else if (layer_type == "Sub")
693+
} else if (layer_type == "Sub") {
683694
op = it_lab_ai::BinaryOpLayer::Operation::kSub;
684-
else if (layer_type == "Mul")
695+
} else if (layer_type == "Mul") {
685696
op = it_lab_ai::BinaryOpLayer::Operation::kMul;
686-
else if (layer_type == "Div")
697+
} else if (layer_type == "Div") {
687698
op = it_lab_ai::BinaryOpLayer::Operation::kDiv;
688-
else {
689-
op = it_lab_ai::BinaryOpLayer::Operation::kAdd;
699+
} else {
700+
supported_type = false;
701+
}
702+
703+
if (!supported_type) {
704+
continue;
690705
}
691706

692707
auto bin_layer = std::make_unique<it_lab_ai::BinaryOpLayer>(op);
@@ -1232,4 +1247,4 @@ void print_time_stats(Graph& graph) {
12321247
#else
12331248
(void)graph;
12341249
#endif
1235-
}
1250+
}

include/layers/ConvLayer.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,14 @@ class ConvImpl : public LayerImpl<ValueType> {
125125
if (input_width_ == 0) {
126126
throw std::out_of_range("Input = 0");
127127
}
128-
auto kercol = static_cast<size_t>(coloms / input_width_ + 1);
129-
color +=
130-
matrix[(i + coloms + str) * input_flow_ + x] *
131-
kernel[kercol * kernel_size + static_cast<size_t>(str + 1)];
128+
int kercol_index = coloms / input_width_ + 1;
129+
if (kercol_index < 0) {
130+
throw std::out_of_range("Kernel column index is negative");
131+
}
132+
auto kercol = static_cast<size_t>(kercol_index);
133+
color +=
134+
matrix[(i + coloms + str) * input_flow_ + x] *
135+
kernel[kercol * kernel_size + static_cast<size_t>(str + 1)];
132136
}
133137
}
134138
if (!bias_.empty() && static_cast<size_t>(x) < bias_.size()) {

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_; }

0 commit comments

Comments
 (0)