Skip to content

Commit 1acfdf6

Browse files
authored
Modernize: C++11/14 (#826)
Perform some modernizations, guided by clang-tidy (v10).
1 parent ba3c177 commit 1acfdf6

12 files changed

Lines changed: 46 additions & 42 deletions

.clang-tidy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Future consideration: cppcoreguidelines-*,clang-analyzer-*,google-*,hicpp-*,llvm-*,misc-*,modernize-*,readability-*,-clang-diagnostic-unused-command-line-argument,-*-namespace-comment*,-*-braces-around-statements,-google-readability-todo,-readability-inconsistent-declaration-parameter-name,-readability-named-parameter
1+
# Future consideration: cppcoreguidelines-*,clang-analyzer-*,google-*,hicpp-*,llvm-*,misc-*,readability-*,-clang-diagnostic-unused-command-line-argument,-*-namespace-comment*,-*-braces-around-statements,-google-readability-todo,-readability-inconsistent-declaration-parameter-name,-readability-named-parameter
22
# FIXME: all performance-* reports
33
# FIXME: all cert-* reports
44
# FIXME: all bugprone-* reports
5-
Checks: -*,bugprone-*,-bugprone-unhandled-self-assignment,-bugprone-parent-virtual-call,-bugprone-narrowing-conversions,-bugprone-exception-escape,-bugprone-string-literal-with-embedded-nul,cppcoreguidelines-slicing,mpi-*,readability-non-const-parameter,modernize-use-override,modernize-use-equals-default
5+
Checks: -*,bugprone-*,-bugprone-unhandled-self-assignment,-bugprone-parent-virtual-call,-bugprone-narrowing-conversions,-bugprone-exception-escape,-bugprone-string-literal-with-embedded-nul,cppcoreguidelines-slicing,mpi-*,readability-non-const-parameter,modernize-*,-modernize-use-trailing-return-type,-modernize-use-bool-literals,-modernize-avoid-c-arrays
66
HeaderFilterRegex: '((^(?!\/share\/openPMD\/).*)*include\/openPMD\/.+\.hpp|src\/^(?!binding).+\.cpp$)'

examples/5_write_parallel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int main(int argc, char *argv[])
4646
{
4747
// global data set to write: [MPI_Size * 10, 300]
4848
// each rank writes a 10x300 slice with its MPI rank as values
49-
float const value = float(mpi_size);
49+
auto const value = float(mpi_size);
5050
std::vector<float> local_data(
5151
10 * 300, value);
5252
if( 0 == mpi_rank )

examples/8a_benchmark_write1D2D_parallel.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class TestInput
151151
return;
152152

153153
// many small writes
154-
srand(time(NULL) * (m_MPIRank + m_MPISize) );
154+
srand(time(nullptr) * (m_MPIRank + m_MPISize) );
155155

156156
auto nBlocks = GetSeg();
157157
if ((rankCount / nBlocks) <= 1)
@@ -165,7 +165,7 @@ class TestInput
165165
if ((rankCount % nBlocks != 0) && (i == (nBlocks -1)))
166166
blockSize = rankCount - blockSize * (nBlocks -1);
167167

168-
m_InRankDistribution.push_back(std::make_pair(rankOffset + counter, blockSize));
168+
m_InRankDistribution.emplace_back(rankOffset + counter, blockSize);
169169
counter += blockSize;
170170
}
171171
}
@@ -248,7 +248,7 @@ class TestInput
248248
Offset meshOffset;
249249
auto const blockSize = getNthMeshExtent(n, meshOffset, meshExtent);
250250
if( blockSize > 0ul ) {
251-
double const value = double(1.0*n + 0.0001*step);
251+
auto const value = double(1.0*n + 0.0001*step);
252252
auto A = createData<double>( blockSize, value, false ) ;
253253
compA.storeChunk( A, meshOffset, meshExtent );
254254
}
@@ -414,8 +414,8 @@ class TestInput
414414
{
415415
unsigned long result = m_Ratio;
416416

417-
for (unsigned int i=0; i<m_GlobalMesh.size(); i++)
418-
result *= m_GlobalMesh[i];
417+
for (unsigned long i : m_GlobalMesh)
418+
result *= i;
419419

420420
return result;
421421
}
@@ -512,7 +512,7 @@ main( int argc, char *argv[] )
512512
}
513513

514514
if( argc >= 3 )
515-
input.m_Bulk = strtoul( argv[2], NULL, 0 );
515+
input.m_Bulk = strtoul( argv[2], nullptr, 0 );
516516

517517
if( argc >= 4 )
518518
input.m_Seg = atoi( argv[3] );

src/IO/ADIOS/ADIOS2IOHandler.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@
2121

2222
#include "openPMD/IO/ADIOS/ADIOS2IOHandler.hpp"
2323

24-
#include <algorithm>
25-
#include <iostream>
26-
#include <iterator>
27-
#include <set>
28-
#include <string>
29-
3024
#include "openPMD/Datatype.hpp"
3125
#include "openPMD/IO/ADIOS/ADIOS2FilePosition.hpp"
3226
#include "openPMD/IO/ADIOS/ADIOS2IOHandler.hpp"
3327
#include "openPMD/auxiliary/Environment.hpp"
3428
#include "openPMD/auxiliary/Filesystem.hpp"
3529
#include "openPMD/auxiliary/StringManip.hpp"
30+
31+
#include <algorithm>
32+
#include <iostream>
33+
#include <iterator>
34+
#include <memory>
35+
#include <set>
36+
#include <string>
3637
#include <type_traits>
3738

3839

@@ -695,8 +696,8 @@ ADIOS2IOHandlerImpl::getFileData( InvalidatableFile file )
695696
{
696697
return *m_fileData
697698
.emplace( std::move( file ),
698-
std::unique_ptr< detail::BufferedActions >{
699-
new detail::BufferedActions{*this, file}} )
699+
std::make_unique< detail::BufferedActions >(
700+
*this, file) )
700701
.first->second;
701702
}
702703
else
@@ -1205,7 +1206,7 @@ namespace detail
12051206
") not found in backend." );
12061207
}
12071208

1208-
Datatype ret =
1209+
auto ret =
12091210
switchType< Datatype >(
12101211
type,
12111212
detail::AttributeReader{},

src/IO/HDF5/HDF5IOHandler.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -917,13 +917,13 @@ HDF5IOHandlerImpl::writeAttribute(Writable* writable,
917917
}
918918
case DT::UCHAR:
919919
{
920-
unsigned char u = att.get< unsigned char >();
920+
auto u = att.get< unsigned char >();
921921
status = H5Awrite(attribute_id, dataType, &u);
922922
break;
923923
}
924924
case DT::SHORT:
925925
{
926-
short i = att.get< short >();
926+
auto i = att.get< short >();
927927
status = H5Awrite(attribute_id, dataType, &i);
928928
break;
929929
}
@@ -941,49 +941,49 @@ HDF5IOHandlerImpl::writeAttribute(Writable* writable,
941941
}
942942
case DT::LONGLONG:
943943
{
944-
long long i = att.get< long long >();
944+
auto i = att.get< long long >();
945945
status = H5Awrite(attribute_id, dataType, &i);
946946
break;
947947
}
948948
case DT::USHORT:
949949
{
950-
unsigned short u = att.get< unsigned short >();
950+
auto u = att.get< unsigned short >();
951951
status = H5Awrite(attribute_id, dataType, &u);
952952
break;
953953
}
954954
case DT::UINT:
955955
{
956-
unsigned int u = att.get< unsigned int >();
956+
auto u = att.get< unsigned int >();
957957
status = H5Awrite(attribute_id, dataType, &u);
958958
break;
959959
}
960960
case DT::ULONG:
961961
{
962-
unsigned long u = att.get< unsigned long >();
962+
auto u = att.get< unsigned long >();
963963
status = H5Awrite(attribute_id, dataType, &u);
964964
break;
965965
}
966966
case DT::ULONGLONG:
967967
{
968-
unsigned long long u = att.get< unsigned long long >();
968+
auto u = att.get< unsigned long long >();
969969
status = H5Awrite(attribute_id, dataType, &u);
970970
break;
971971
}
972972
case DT::FLOAT:
973973
{
974-
float f = att.get< float >();
974+
auto f = att.get< float >();
975975
status = H5Awrite(attribute_id, dataType, &f);
976976
break;
977977
}
978978
case DT::DOUBLE:
979979
{
980-
double d = att.get< double >();
980+
auto d = att.get< double >();
981981
status = H5Awrite(attribute_id, dataType, &d);
982982
break;
983983
}
984984
case DT::LONG_DOUBLE:
985985
{
986-
long double d = att.get< long double >();
986+
auto d = att.get< long double >();
987987
status = H5Awrite(attribute_id, dataType, &d);
988988
break;
989989
}

src/IO/JSON/JSONFilePosition.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#include "openPMD/IO/JSON/JSONFilePosition.hpp"
22

3+
#include <utility>
4+
35

46
namespace openPMD
57
{
68
JSONFilePosition::JSONFilePosition( json::json_pointer ptr):
7-
id( ptr )
9+
id( std::move( ptr ) )
810
{}
911
}

src/ParticleSpecies.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ ParticleSpecies::flush(std::string const& path)
121121
patch.second.flush(patch.first);
122122
} else
123123
{
124-
iterator it = find("position");
124+
auto it = find("position");
125125
if ( it != end() )
126126
it->second.setUnitDimension({{UnitDimension::L, 1}});
127127
it = find("positionOffset");

src/auxiliary/Date.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
#include "openPMD/auxiliary/Date.hpp"
2222

23+
#include <array>
2324
#include <ctime>
2425
#include <string>
2526
#include <sstream>
@@ -32,18 +33,18 @@ namespace auxiliary
3233
std::string getDateString( std::string const & format )
3334
{
3435
constexpr size_t maxLen = 30u;
35-
char buffer [maxLen];
36+
std::array< char, maxLen > buffer;
3637

3738
time_t rawtime;
3839
time( &rawtime );
3940
struct tm* timeinfo;
4041
// https://github.com/openPMD/openPMD-api/pull/657#issuecomment-574424885
4142
timeinfo = localtime( &rawtime ); // lgtm[cpp/potentially-dangerous-function]
4243

43-
strftime( buffer, maxLen, format.c_str(), timeinfo );
44+
strftime( buffer.data(), maxLen, format.c_str(), timeinfo );
4445

4546
std::stringstream dateString;
46-
dateString << buffer;
47+
dateString << buffer.data();
4748

4849
return dateString.str();
4950
}

src/binding/python/Attributable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ bool setAttributeFromBufferInfo(
125125
* - not strided with paddings
126126
* - not a view in another buffer that results in striding
127127
*/
128-
Py_buffer* view = new Py_buffer();
128+
auto* view = new Py_buffer();
129129
int flags = PyBUF_STRIDES | PyBUF_FORMAT;
130130
if( PyObject_GetBuffer( a.ptr(), view, flags ) != 0 )
131131
{

src/binding/python/RecordComponent.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ store_chunk(RecordComponent & r, py::array & a, Offset const & offset, Extent co
264264
* - not strided with paddings
265265
* - not a view in another buffer that results in striding
266266
*/
267-
Py_buffer* view = new Py_buffer();
267+
auto* view = new Py_buffer();
268268
int flags = PyBUF_STRIDES | PyBUF_FORMAT;
269269
if( PyObject_GetBuffer( a.ptr(), view, flags ) != 0 )
270270
{

0 commit comments

Comments
 (0)