Skip to content

Commit 5fdd217

Browse files
committed
Changes based on cppcheck static analysis
* Pass variables by reference when not basic types * Prefix iterators for non basic types * Variable scope reduction * Remove exceptions from destructors
1 parent bdcc64c commit 5fdd217

32 files changed

Lines changed: 82 additions & 83 deletions

include/af/data.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ namespace af
154154
\ingroup data_mat
155155
\ingroup arrafire_func
156156
*/
157-
AFAPI array iota(const dim4 dims, const dim4 tile_dims = dim4(1), dtype ty=f32);
157+
AFAPI array iota(const dim4 &dims, const dim4 &tile_dims = dim4(1), dtype ty=f32);
158158

159159
/**
160160
@}

include/af/statistics.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ AFAPI array var(const array& in, bool isbiased=false, dim_type dim=-1);
6767
6868
\note \p dim is -1 by default. -1 denotes the first non-signleton dimension.
6969
*/
70-
AFAPI array var(const array& in, const array weights, dim_type dim=-1);
70+
AFAPI array var(const array& in, const array &weights, dim_type dim=-1);
7171

7272
/**
7373
C++ Interface for standard deviation
@@ -153,7 +153,7 @@ AFAPI T var(const array& in, bool isbiased=false);
153153
\ingroup stat_func_var
154154
*/
155155
template<typename T>
156-
AFAPI T var(const array& in, const array weights);
156+
AFAPI T var(const array& in, const array& weights);
157157

158158
/**
159159
C++ Interface for standard deviation of all elements

src/api/c/data.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ af_err af_create_array(af_array *result, const void * const data,
5555
const unsigned ndims, const dim_type * const dims,
5656
const af_dtype type)
5757
{
58-
af_array out;
5958
try {
59+
af_array out;
6060
AF_CHECK(af_init());
6161
dim4 d((size_t)dims[0]);
6262
for(unsigned i = 1; i < ndims; i++) {
@@ -86,8 +86,8 @@ af_err af_constant(af_array *result, const double value,
8686
const unsigned ndims, const dim_type * const dims,
8787
const af_dtype type)
8888
{
89-
af_array out;
9089
try {
90+
af_array out;
9191
AF_CHECK(af_init());
9292
dim4 d((size_t)dims[0]);
9393
for(unsigned i = 1; i < ndims; i++) {
@@ -123,8 +123,8 @@ static inline af_array createCplx(dim4 dims, const Ti real, const Ti imag)
123123
af_err af_constant_complex(af_array *result, const double real, const double imag,
124124
const unsigned ndims, const dim_type * const dims, af_dtype type)
125125
{
126-
af_array out;
127126
try {
127+
af_array out;
128128
AF_CHECK(af_init());
129129

130130
dim4 d((size_t)dims[0]);
@@ -147,8 +147,8 @@ af_err af_constant_complex(af_array *result, const double real, const double ima
147147
af_err af_constant_long(af_array *result, const intl val,
148148
const unsigned ndims, const dim_type * const dims)
149149
{
150-
af_array out;
151150
try {
151+
af_array out;
152152
AF_CHECK(af_init());
153153

154154
dim4 d((size_t)dims[0]);
@@ -167,8 +167,8 @@ af_err af_constant_long(af_array *result, const intl val,
167167
af_err af_constant_ulong(af_array *result, const uintl val,
168168
const unsigned ndims, const dim_type * const dims)
169169
{
170-
af_array out;
171170
try {
171+
af_array out;
172172
AF_CHECK(af_init());
173173

174174
dim4 d((size_t)dims[0]);
@@ -188,8 +188,8 @@ af_err af_constant_ulong(af_array *result, const uintl val,
188188
af_err af_create_handle(af_array *result, const unsigned ndims, const dim_type * const dims,
189189
const af_dtype type)
190190
{
191-
af_array out;
192191
try {
192+
af_array out;
193193
AF_CHECK(af_init());
194194
dim4 d((size_t)dims[0]);
195195
for(unsigned i = 1; i < ndims; i++) {
@@ -217,11 +217,11 @@ af_err af_create_handle(af_array *result, const unsigned ndims, const dim_type *
217217
//Strong Exception Guarantee
218218
af_err af_copy_array(af_array *out, const af_array in)
219219
{
220-
ArrayInfo info = getInfo(in);
221-
const af_dtype type = info.getType();
222-
223-
af_array res;
224220
try {
221+
ArrayInfo info = getInfo(in);
222+
const af_dtype type = info.getType();
223+
224+
af_array res;
225225
switch(type) {
226226
case f32: res = copyArray<float >(in); break;
227227
case c32: res = copyArray<cfloat >(in); break;
@@ -261,8 +261,8 @@ static inline af_array identity_(const af::dim4 &dims)
261261

262262
af_err af_randu(af_array *out, const unsigned ndims, const dim_type * const dims, const af_dtype type)
263263
{
264-
af_array result;
265264
try {
265+
af_array result;
266266
AF_CHECK(af_init());
267267

268268
dim4 d((size_t)dims[0]);
@@ -285,13 +285,13 @@ af_err af_randu(af_array *out, const unsigned ndims, const dim_type * const dims
285285
std::swap(*out, result);
286286
}
287287
CATCHALL
288-
return AF_SUCCESS;
288+
return AF_SUCCESS;
289289
}
290290

291291
af_err af_randn(af_array *out, const unsigned ndims, const dim_type * const dims, const af_dtype type)
292292
{
293-
af_array result;
294293
try {
294+
af_array result;
295295
AF_CHECK(af_init());
296296

297297
dim4 d((size_t)dims[0]);
@@ -314,8 +314,8 @@ af_err af_randn(af_array *out, const unsigned ndims, const dim_type * const dims
314314

315315
af_err af_identity(af_array *out, const unsigned ndims, const dim_type * const dims, const af_dtype type)
316316
{
317-
af_array result;
318317
try {
318+
af_array result;
319319
AF_CHECK(af_init());
320320
dim4 d((size_t)dims[0]);
321321

@@ -413,8 +413,8 @@ static inline af_array range_(const dim4& d, const int seq_dim)
413413
af_err af_range(af_array *result, const unsigned ndims, const dim_type * const dims,
414414
const int seq_dim, const af_dtype type)
415415
{
416-
af_array out;
417416
try {
417+
af_array out;
418418
AF_CHECK(af_init());
419419

420420
DIM_ASSERT(1, ndims > 0 && ndims <= 4);
@@ -448,8 +448,8 @@ static inline af_array iota_(const dim4 &dims, const dim4 &tile_dims)
448448
af_err af_iota(af_array *result, const unsigned ndims, const dim_type * const dims,
449449
const unsigned t_ndims, const dim_type * const tdims, const af_dtype type)
450450
{
451-
af_array out;
452451
try {
452+
af_array out;
453453
AF_CHECK(af_init());
454454

455455
DIM_ASSERT(1, ndims > 0 && ndims <= 4);

src/api/c/exampleFunction.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ af_err af_example_function(af_array* out,
5252
const af_array in,
5353
const af_someenum_t param)
5454
{
55-
af_array output = 0;
56-
5755
try {
56+
af_array output = 0;
5857
ArrayInfo info = getInfo(in); // ArrayInfo is the base class which
5958
// each backend specific Array inherits
6059
// This class stores the basic array meta-data

src/api/c/gaussian_kernel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ af_err af_gaussian_kernel(af_array *out,
8080
const int rows, const int cols,
8181
const double sigma_r, const double sigma_c)
8282
{
83-
af_array res;
8483
try {
84+
af_array res;
8585
res = getHandle<float>(gaussianKernel<float>(rows, cols, sigma_r, sigma_c));
8686
std::swap(*out, res);
8787
}CATCHALL;

src/api/c/imageio.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void FreeImageErrorHandler(FREE_IMAGE_FORMAT oFif, const char* zMessage)
4646

4747
// Split a MxNx3 image into 3 separate channel matrices.
4848
// Produce 3 channels if needed
49-
static af_err channel_split(const af_array rgb, const af::dim4 dims,
49+
static af_err channel_split(const af_array rgb, const af::dim4 &dims,
5050
af_array *outr, af_array *outg, af_array *outb, af_array *outa)
5151
{
5252
try {

src/api/c/stdev.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ static af_array stdev(const af_array& in, int dim)
4848
dim4 iDims = input.dims();
4949

5050
Array<outType> meanArr = mean<outType>(input, dim);
51-
dim4 oDims = meanArr.dims();
5251

5352
/* now tile meanArr along dim and use it for variance computation */
5453
dim4 tileDims(1);
@@ -59,7 +58,7 @@ static af_array stdev(const af_array& in, int dim)
5958
Array<outType> diff = detail::arithOp<outType, af_sub_t>(input, tMeanArr, tMeanArr.dims());
6059
Array<outType> diffSq = detail::arithOp<outType, af_mul_t>(diff, diff, diff.dims());
6160
Array<outType> redDiff = reduce<af_add_t, outType, outType>(diffSq, dim);
62-
oDims = redDiff.dims();
61+
dim4 oDims = redDiff.dims();
6362

6463
Array<outType> divArr = createValueArray<outType>(oDims, scalar<outType>(iDims[dim]));
6564
Array<outType> varArr = detail::arithOp<outType, af_div_t>(redDiff, divArr, redDiff.dims());

src/api/c/var.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ static af_array var(const af_array& in, bool isbiased, int dim)
6969
dim4 iDims = input.dims();
7070

7171
Array<outType> meanArr = mean<outType>(input, dim);
72-
dim4 oDims = meanArr.dims();
7372

7473
/* now tile meanArr along dim and use it for variance computation */
7574
dim4 tileDims(1);
@@ -80,7 +79,7 @@ static af_array var(const af_array& in, bool isbiased, int dim)
8079
Array<outType> diff = arithOp<outType, af_sub_t>(input, tMeanArr, tMeanArr.dims());
8180
Array<outType> diffSq = arithOp<outType, af_mul_t>(diff, diff, diff.dims());
8281
Array<outType> redDiff = reduce<af_add_t, outType, outType>(diffSq, dim);
83-
oDims = redDiff.dims();
82+
dim4 oDims = redDiff.dims();
8483

8584
Array<outType> divArr = createValueArray<outType>(oDims, scalar<outType>(isbiased ? iDims[dim] : iDims[dim]-1));
8685
Array<outType> result = arithOp<outType, af_div_t>(redDiff, divArr, redDiff.dims());

src/api/cpp/array.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,11 @@ namespace af
238238
array::~array()
239239
{
240240
af_array tmp = get();
241-
if (tmp != 0) AF_THROW(af_destroy_array(tmp));
241+
if (tmp != 0){
242+
if(AF_SUCCESS != af_destroy_array(tmp)) {
243+
fprintf(stderr, "Error: Couldn't destroy af::array %p", this);
244+
}
245+
}
242246
}
243247

244248
af::dtype array::type() const

src/api/cpp/data.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ namespace af
188188
return range(dim4(d0, d1, d2, d3), seq_dim, ty);
189189
}
190190

191-
array iota(const dim4 dims, const dim4 tile_dims, af::dtype ty)
191+
array iota(const dim4 &dims, const dim4 &tile_dims, af::dtype ty)
192192
{
193193
af_array out;
194194
AF_THROW(af_iota(&out, dims.ndims(), dims.get(), tile_dims.ndims(), tile_dims.get(), ty));

0 commit comments

Comments
 (0)