Skip to content

Commit 5f8717c

Browse files
authored
Fix static analyzer warnings (onnx#6449)
### Description More fixes for static analyzer warnings, mostly about adding const to parameter references/pointers. ### Motivation and Context For better code. <!-- - Why is this change required? What problem does it solve? --> <!-- - If it fixes an open issue, please link to the issue here. --> --------- Signed-off-by: cyy <cyyever@outlook.com>
1 parent bf15a99 commit 5f8717c

File tree

8 files changed

+44
-34
lines changed

8 files changed

+44
-34
lines changed

onnx/common/array_ref.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
// removed a bunch of slice variants for simplicity...
2424

2525
#pragma once
26-
#include <assert.h>
27-
2826
#include <array>
27+
#include <cassert>
2928
#include <vector>
3029

3130
namespace ONNX_NAMESPACE {
@@ -43,11 +42,11 @@ namespace ONNX_NAMESPACE {
4342
template <typename T>
4443
class ArrayRef {
4544
public:
46-
typedef const T* iterator;
47-
typedef const T* const_iterator;
48-
typedef size_t size_type;
45+
using iterator = const T*;
46+
using const_iterator = const T*;
47+
using size_type = size_t;
4948

50-
typedef std::reverse_iterator<iterator> reverse_iterator;
49+
using reverse_iterator = std::reverse_iterator<iterator>;
5150

5251
private:
5352
/// The start of the array, in an external buffer.
@@ -64,6 +63,7 @@ class ArrayRef {
6463
/*implicit*/ ArrayRef() : Data(nullptr), Length(0) {}
6564

6665
/// Construct an ArrayRef from a single element.
66+
/// NOLINTNEXTLINE(google-explicit-constructor)
6767
/*implicit*/ ArrayRef(const T& OneElt) : Data(&OneElt), Length(1) {}
6868

6969
/// Construct an ArrayRef from a pointer and length.
@@ -74,14 +74,17 @@ class ArrayRef {
7474

7575
/// Construct an ArrayRef from a std::vector.
7676
template <typename A>
77+
/// NOLINTNEXTLINE(google-explicit-constructor)
7778
/*implicit*/ ArrayRef(const std::vector<T, A>& Vec) : Data(Vec.data()), Length(Vec.size()) {}
7879

7980
/// Construct an ArrayRef from a std::array
8081
template <size_t N>
82+
/// NOLINTNEXTLINE(google-explicit-constructor)
8183
/*implicit*/ constexpr ArrayRef(const std::array<T, N>& Arr) : Data(Arr.data()), Length(N) {}
8284

8385
/// Construct an ArrayRef from a C array.
8486
template <size_t N>
87+
/// NOLINTNEXTLINE(google-explicit-constructor, *array*)
8588
/*implicit*/ constexpr ArrayRef(const T (&Arr)[N]) : Data(Arr), Length(N) {}
8689

8790
/// Construct an ArrayRef from a std::initializer_list.
@@ -170,14 +173,14 @@ class ArrayRef {
170173
/// The declaration here is extra complicated so that "arrayRef = {}"
171174
/// continues to select the move assignment operator.
172175
template <typename U>
173-
typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type& operator=(U&& Temporary) = delete;
176+
std::enable_if_t<std::is_same_v<U, T>, ArrayRef<T>>& operator=(U&& Temporary) = delete;
174177

175178
/// Disallow accidental assignment from a temporary.
176179
///
177180
/// The declaration here is extra complicated so that "arrayRef = {}"
178181
/// continues to select the move assignment operator.
179182
template <typename U>
180-
typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type& operator=(std::initializer_list<U>) = delete;
183+
std::enable_if_t<std::is_same_v<U, T>, ArrayRef<T>>& operator=(std::initializer_list<U>) = delete;
181184

182185
/// @}
183186
/// @name Expensive Operations
@@ -189,6 +192,7 @@ class ArrayRef {
189192
/// @}
190193
/// @name Conversion operators
191194
/// @{
195+
/// NOLINTNEXTLINE(google-explicit-constructor)
192196
operator std::vector<T>() const {
193197
return std::vector<T>(Data, Data + Length);
194198
}

onnx/common/ir.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ struct Value final {
387387
// %5 = h(%6, %6)
388388
void replaceAllUsesWith(Value* newValue);
389389

390-
Value* copyMetadata(Value* from) {
390+
Value* copyMetadata(const Value* from) {
391391
setElemType(from->elemType());
392392
setSizes(from->sizes());
393393
if (from->has_unique_name()) {
@@ -728,7 +728,7 @@ struct Node : public Attributes<Node> {
728728
}
729729

730730
// Check whether this node is before node n in the graph.
731-
bool isBefore(Node* n);
731+
bool isBefore(const Node* n);
732732

733733
// iterators of the node list starting at this node
734734
// useful for resuming a search starting at this node
@@ -1354,7 +1354,7 @@ inline void Node::eraseOutput(size_t i) {
13541354
}
13551355
}
13561356

1357-
inline bool Node::isBefore(Node* n) {
1357+
inline bool Node::isBefore(const Node* n) {
13581358
if (n == nullptr || this == n) {
13591359
// Bail out early.
13601360
return false;

onnx/common/ir_pb_converter.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static void convertAttribute(const ONNX_NAMESPACE::AttributeProto& ap, Node* n,
193193
}
194194
}
195195

196-
static void convertAttributes(ONNX_NAMESPACE::NodeProto& np, Node* n, const int ir_version = IR_VERSION) {
196+
static void convertAttributes(const ONNX_NAMESPACE::NodeProto& np, Node* n, const int ir_version = IR_VERSION) {
197197
for (int i = 0; i < np.attribute_size(); i++) {
198198
convertAttribute(np.attribute(i), n, ir_version);
199199
}
@@ -217,7 +217,7 @@ static std::vector<Dimension> tensorShapeProtoToDimensions(const ONNX_NAMESPACE:
217217
}
218218

219219
static void createDummyValue(
220-
std::unique_ptr<Graph>& g,
220+
const std::unique_ptr<Graph>& g,
221221
const std::string& name,
222222
std::unordered_map<std::string, Value*>& value_by_name_of) {
223223
auto* undef = g->create(kCaptured, 1);
@@ -301,7 +301,7 @@ std::unique_ptr<Graph> graphProtoToGraph(const ONNX_NAMESPACE::GraphProto& gp, b
301301
}
302302

303303
for (int i = 0; i < gp.node_size(); i++) {
304-
auto np = gp.node(i);
304+
const auto& np = gp.node(i);
305305
auto* n = g->create(Symbol(np.op_type()), /* num_outputs = */ np.output_size());
306306
g->appendNode(n);
307307
for (int j = 0; j < np.output_size(); j++) {
@@ -406,7 +406,7 @@ std::unique_ptr<Graph> ImportModelProto(const ModelProto& mp) {
406406
}
407407

408408
// Part 2: convert IR to ONNX Protobuf
409-
static std::string value_name(Value* n) {
409+
static std::string value_name(const Value* n) {
410410
return n->uniqueName();
411411
}
412412

@@ -551,7 +551,7 @@ static void addAttribute(ONNX_NAMESPACE::NodeProto* n_p, Node* n, Symbol name) {
551551
}
552552
}
553553

554-
static void encodeTypeProtoTensorType(ONNX_NAMESPACE::TypeProto_Tensor* tensor_type, Value* n) {
554+
static void encodeTypeProtoTensorType(ONNX_NAMESPACE::TypeProto_Tensor* tensor_type, const Value* n) {
555555
if (n->elemType() != 0) {
556556
tensor_type->set_elem_type(n->elemType());
557557
}

onnx/cpp2py_export.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <climits>
99
#include <limits>
10+
#include <string>
1011
#include <tuple>
1112
#include <unordered_map>
1213
#include <utility>
@@ -47,16 +48,17 @@ static std::string ProtoBytesToText(const py::bytes& bytes) {
4748
}
4849

4950
template <typename T, typename Ts = std::remove_const_t<T>>
50-
static std::pair<std::unique_ptr<Ts[]>, std::unordered_map<std::string, T*>> ParseProtoFromBytesMap(
51+
static std::pair<std::vector<Ts>, std::unordered_map<std::string, T*>> ParseProtoFromBytesMap(
5152
const std::unordered_map<std::string, py::bytes>& bytesMap) {
52-
std::unique_ptr<Ts[]> values(new Ts[bytesMap.size()]);
53+
std::vector<Ts> values(bytesMap.size());
5354
std::unordered_map<std::string, T*> result;
5455
size_t i = 0;
5556
for (const auto& kv : bytesMap) {
5657
ParseProtoFromPyBytes(&values[i], kv.second);
5758
result[kv.first] = &values[i];
5859
i++;
5960
}
61+
// C++ guarantees that the pointers remain valid after std::vector<Ts> is moved.
6062
return std::make_pair(std::move(values), result);
6163
}
6264

onnx/defs/shape_inference.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,22 @@ inline bool getRepeatedAttribute(InferenceContext& ctx, const std::string& attr_
154154
}
155155
}
156156

157-
inline int64_t getAttribute(InferenceContext& ctx, const std::string& attributeName, int64_t defaultValue) {
157+
inline int64_t getAttribute(const InferenceContext& ctx, const std::string& attributeName, int64_t defaultValue) {
158158
auto attr_proto = ctx.getAttribute(attributeName);
159159
if ((nullptr != attr_proto) && attr_proto->has_i())
160160
return attr_proto->i();
161161
return defaultValue;
162162
}
163163

164-
inline int64_t getAttribute(DataPropagationContext& ctx, const std::string& attributeName, int64_t defaultValue) {
164+
inline int64_t getAttribute(const DataPropagationContext& ctx, const std::string& attributeName, int64_t defaultValue) {
165165
auto attr_proto = ctx.getAttribute(attributeName);
166166
if ((nullptr != attr_proto) && attr_proto->has_i())
167167
return attr_proto->i();
168168
return defaultValue;
169169
}
170170

171171
inline std::string
172-
getAttribute(InferenceContext& ctx, const std::string& attributeName, const std::string& defaultValue) {
172+
getAttribute(const InferenceContext& ctx, const std::string& attributeName, const std::string& defaultValue) {
173173
auto attr_proto = ctx.getAttribute(attributeName);
174174
if ((nullptr != attr_proto) && attr_proto->has_s())
175175
return attr_proto->s();
@@ -352,7 +352,7 @@ inline const TensorShapeProto& getInputShape(const InferenceContext& ctx, size_t
352352
}
353353
}
354354

355-
inline const TensorShapeProto* getOptionalInputShape(InferenceContext& ctx, size_t n) {
355+
inline const TensorShapeProto* getOptionalInputShape(const InferenceContext& ctx, size_t n) {
356356
const auto* input_type = ctx.getInputType(n);
357357

358358
if (input_type == nullptr) {
@@ -782,7 +782,7 @@ static constexpr T narrow_cast(U&& u) noexcept {
782782
return static_cast<T>(std::forward<U>(u));
783783
}
784784

785-
inline void checkInputRank(InferenceContext& ctx, size_t input_index, int expected_rank) {
785+
inline void checkInputRank(const InferenceContext& ctx, size_t input_index, int expected_rank) {
786786
// We check the rank only if a rank is known for the input:
787787
if (hasInputShape(ctx, input_index)) {
788788
auto rank = getInputShape(ctx, input_index).dim_size();
@@ -842,7 +842,7 @@ inline void unifyDim(const Dim& source_dim, Dim& target_dim) {
842842
}
843843
}
844844

845-
inline void unifyInputDim(InferenceContext& ctx, size_t input_index, int dim_index, Dim& dim) {
845+
inline void unifyInputDim(const InferenceContext& ctx, size_t input_index, int dim_index, Dim& dim) {
846846
// We unify the dimensions only if it is available for specified input:
847847
if (hasInputShape(ctx, input_index)) {
848848
auto& input_shape = getInputShape(ctx, input_index);

onnx/test/cpp/function_verify_test.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include "onnx/defs/parser.h"
1515
#include "onnx/defs/printer.h"
1616
#include "onnx/defs/schema.h"
17-
#include "onnx/onnx-operators_pb.h"
18-
#include "onnx/onnx_pb.h"
1917
#include "onnx/shape_inference/implementation.h"
2018

2119
namespace ONNX_NAMESPACE {
@@ -92,8 +90,13 @@ static void VerifyTypeConstraint(const OpSchema& function_op, const FunctionProt
9290
for (auto& actual_type : iter->second) {
9391
if (allowed_types.find(actual_type) == allowed_types.end()) {
9492
fail_check(
95-
"Input type " + actual_type + " of parameter " + actual_param_name + " of function " +
96-
function_op.Name() + " is not allowed by operator " + op_type);
93+
"Input type ",
94+
actual_type,
95+
" of parameter ",
96+
actual_param_name + " of function ",
97+
function_op.Name(),
98+
" is not allowed by operator ",
99+
op_type);
97100
}
98101
}
99102
}
@@ -331,7 +334,7 @@ TEST(FunctionVerification, VerifyFunctionOps) {
331334
VerifyFunction(s, function_body, verified_counter);
332335
}
333336
}
334-
ONNX_CATCH(ONNX_NAMESPACE::checker::ValidationError e) {
337+
ONNX_CATCH(const ONNX_NAMESPACE::checker::ValidationError& e) {
335338
ONNX_HANDLE_EXCEPTION([&]() { FAIL() << e.what(); });
336339
}
337340
}

onnx/test/cpp/shape_inference_test.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
#include "onnx/defs/parser.h"
1111
#include "onnx/defs/schema.h"
1212
#include "onnx/defs/shape_inference.h"
13-
#include "onnx/onnx_pb.h"
1413
#include "onnx/shape_inference/implementation.h"
1514

1615
using namespace ONNX_NAMESPACE::shape_inference;
1716

1817
namespace ONNX_NAMESPACE {
1918
// onnx/defs/controlflow/old.cc
19+
// NOLINTNEXTLINE(misc-use-internal-linkage)
2020
void ScanInferenceFunctionOpset8(InferenceContext& ctx);
2121
// onnx/defs/controlflow/defs.cc
22+
// NOLINTNEXTLINE(misc-use-internal-linkage)
2223
void ScanInferenceFunction(InferenceContext& ctx);
2324

2425
namespace Test {

onnx/version_converter/adapters/type_restriction.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ class TypeRestriction : public Adapter {
2626
const std::vector<TensorProto_DataType>& unallowed_types)
2727
: Adapter(op_name, initial, target), unallowed_types_(unallowed_types) {}
2828

29-
void adapt_type_restriction(const std::shared_ptr<Graph>&, Node* node) const {
29+
void adapt_type_restriction(const std::shared_ptr<Graph>&, const Node* node) const {
3030
// Since consumed_inputs is optional, no need to add it (as in batchnorm)
3131
// Iterate over all inputs and outputs
32-
for (Value* input : node->inputs()) {
32+
for (const Value* input : node->inputs()) {
3333
isUnallowed(input);
3434
}
35-
for (Value* output : node->outputs()) {
35+
for (const Value* output : node->outputs()) {
3636
isUnallowed(output);
3737
}
3838
}
@@ -45,7 +45,7 @@ class TypeRestriction : public Adapter {
4545
private:
4646
std::vector<TensorProto_DataType> unallowed_types_;
4747

48-
void isUnallowed(Value* val) const {
48+
void isUnallowed(const Value* val) const {
4949
ONNX_ASSERTM(
5050
std::find(std::begin(unallowed_types_), std::end(unallowed_types_), val->elemType()) ==
5151
std::end(unallowed_types_),

0 commit comments

Comments
 (0)