Skip to content

Commit

Permalink
Upgrade INT_ methods to numeric_limits
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 16, 2018
1 parent 17cd2fd commit c705670
Show file tree
Hide file tree
Showing 18 changed files with 41 additions and 34 deletions.
2 changes: 2 additions & 0 deletions scripts/sipify.pl
Expand Up @@ -437,6 +437,8 @@ sub fix_constants {
$line =~ s/\bstd::numeric_limits<double>::max\(\)/DBL_MAX/g;
$line =~ s/\bstd::numeric_limits<double>::lowest\(\)/-DBL_MAX/g;
$line =~ s/\bstd::numeric_limits<double>::epsilon\(\)/DBL_EPSILON/g;
$line =~ s/\bstd::numeric_limits<int>::max\(\)/INT_MAX/g;
$line =~ s/\bstd::numeric_limits<int>::min\(\)/INT_MIN/g;
return $line;
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/dxf/qgsdxfexport.cpp
Expand Up @@ -444,7 +444,7 @@ void QgsDxfExport::writeGroup( int code, const QgsPoint &p )
void QgsDxfExport::writeGroup( const QColor &color, int exactMatchCode, int rgbCode, int transparencyCode )
{
int minDistAt = -1;
int minDist = INT_MAX;
int minDist = std::numeric_limits<int>::max();

for ( int i = 1; i < static_cast< int >( sizeof( sDxfColors ) / sizeof( *sDxfColors ) ) && minDist > 0; ++i )
{
Expand Down Expand Up @@ -3910,7 +3910,7 @@ QString QgsDxfExport::lineStyleFromSymbolLayer( const QgsSymbolLayer *symbolLaye
int QgsDxfExport::closestColorMatch( QRgb pixel )
{
int idx = 0;
int current_distance = INT_MAX;
int current_distance = std::numeric_limits<int>::max();
for ( int i = 1; i < static_cast< int >( sizeof( sDxfColors ) / sizeof( *sDxfColors ) ); ++i )
{
int dist = color_distance( pixel, i );
Expand Down
2 changes: 1 addition & 1 deletion src/core/dxf/qgsdxfpaintdevice.cpp
Expand Up @@ -46,7 +46,7 @@ int QgsDxfPaintDevice::metric( PaintDeviceMetric metric ) const
case QPaintDevice::PdmHeightMM:
return mDrawingSize.height();
case QPaintDevice::PdmNumColors:
return INT_MAX;
return std::numeric_limits<int>::max();
case QPaintDevice::PdmDepth:
return 32;
case QPaintDevice::PdmDpiX:
Expand Down
4 changes: 2 additions & 2 deletions src/core/pal/problem.cpp
Expand Up @@ -39,7 +39,7 @@
#include "priorityqueue.h"
#include "internalexception.h"
#include <cfloat>
#include <limits> //for INT_MAX
#include <limits> //for std::numeric_limits<int>::max()

#include "qgslabelingengine.h"

Expand Down Expand Up @@ -347,7 +347,7 @@ void Problem::init_sol_falp()
{
if ( sol->s[i] == -1 )
{
nbOverlap = INT_MAX;
nbOverlap = std::numeric_limits<int>::max();
start_p = featStartId[i];
for ( p = 0; p < featNbLp[i]; p++ )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -135,7 +135,7 @@ int QgsProcessingParameters::parameterAsInt( const QgsProcessingParameterDefinit
if ( ok )
{
double round = std::round( dbl );
if ( round > INT_MAX || round < -INT_MAX )
if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
{
//double too large to fit in int
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsfield.cpp
Expand Up @@ -353,7 +353,7 @@ bool QgsField::convertCompatible( QVariant &v ) const
}

double round = std::round( dbl );
if ( round > INT_MAX || round < -INT_MAX )
if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
{
//double too large to fit in int
v = QVariant( d->type );
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsstringstatisticalsummary.cpp
Expand Up @@ -40,7 +40,7 @@ void QgsStringStatisticalSummary::reset()
mCountMissing = 0;
mMin.clear();
mMax.clear();
mMinLength = INT_MAX;
mMinLength = std::numeric_limits<int>::max();
mMaxLength = 0;
mSumLengths = 0;
mMeanLength = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorfilewriter.cpp
Expand Up @@ -2496,7 +2496,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::prepareWriteAsVectorFormat
{
QVariant min = layer->minimumValue( i );
QVariant max = layer->maximumValue( i );
if ( std::max( std::llabs( min.toLongLong() ), std::llabs( max.toLongLong() ) ) < INT_MAX )
if ( std::max( std::llabs( min.toLongLong() ), std::llabs( max.toLongLong() ) ) < std::numeric_limits<int>::max() )
{
details.outputFields[i].setType( QVariant::Int );
}
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsgradientstopeditor.cpp
Expand Up @@ -275,8 +275,8 @@ void QgsGradientStopEditor::mouseMoveEvent( QMouseEvent *e )
int QgsGradientStopEditor::findClosestStop( int x, int threshold ) const
{
int closestStop = -1;
int closestDiff = INT_MAX;
int currentDiff = INT_MAX;
int closestDiff = std::numeric_limits<int>::max();
int currentDiff = std::numeric_limits<int>::max();

// check for matching stops first, so that they take precedence
// otherwise it's impossible to select a stop which sits above the first/last stop, making
Expand Down
2 changes: 1 addition & 1 deletion src/gui/raster/qgspalettedrendererwidget.cpp
Expand Up @@ -704,7 +704,7 @@ bool QgsPalettedRendererModel::removeRows( int row, int count, const QModelIndex
bool QgsPalettedRendererModel::insertRows( int row, int count, const QModelIndex & )
{
QgsPalettedRasterRenderer::ClassData::const_iterator cIt = mData.constBegin();
int currentMaxValue = -INT_MAX;
int currentMaxValue = -std::numeric_limits<int>::max();
for ( ; cIt != mData.constEnd(); ++cIt )
{
int value = cIt->value;
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/grass/qgsgrassmoduleparam.cpp
Expand Up @@ -267,8 +267,8 @@ QgsGrassModuleOption::QgsGrassModuleOption( QgsGrassModule *module, QString key,
, mValueType( String )
, mOutputType( None )
, mHaveLimits( false )
, mMin( INT_MAX )
, mMax( INT_MIN )
, mMin( std::numeric_limits<int>::max() )
, mMax( std::numeric_limits<int>::min() )
, mIsOutput( false )
, mUsesRegion( false )
{
Expand Down
2 changes: 1 addition & 1 deletion src/providers/grass/qgsgrassrasterprovider.cpp
Expand Up @@ -114,7 +114,7 @@ QgsGrassRasterProvider::QgsGrassRasterProvider( QString const &uri )
double myInternalNoDataValue;
if ( mGrassDataType == CELL_TYPE )
{
myInternalNoDataValue = INT_MIN;
myInternalNoDataValue = std::numeric_limits<int>::min();
}
else if ( mGrassDataType == DCELL_TYPE )
{
Expand Down
2 changes: 1 addition & 1 deletion src/providers/postgres/qgscolumntypethread.cpp
Expand Up @@ -83,7 +83,7 @@ void QgsGeomColumnTypeThread::run()

if ( !layerProperty.geometryColName.isNull() &&
( layerProperty.types.value( 0, QgsWkbTypes::Unknown ) == QgsWkbTypes::Unknown ||
layerProperty.srids.value( 0, INT_MIN ) == INT_MIN ) )
layerProperty.srids.value( 0, std::numeric_limits<int>::min() ) == std::numeric_limits<int>::min() ) )
{
if ( dontResolveType )
{
Expand Down
6 changes: 3 additions & 3 deletions src/providers/postgres/qgspgtablemodel.cpp
Expand Up @@ -60,7 +60,7 @@ void QgsPgTableModel::addTableEntry( const QgsPostgresLayerProperty &layerProper
{
tip = tr( "Specify a geometry type in the '%1' column" ).arg( tr( "Data Type" ) );
}
else if ( wkbType != QgsWkbTypes::NoGeometry && srid == INT_MIN )
else if ( wkbType != QgsWkbTypes::NoGeometry && srid == std::numeric_limits<int>::min() )
{
tip = tr( "Enter a SRID into the '%1' column" ).arg( tr( "SRID" ) );
}
Expand All @@ -82,7 +82,7 @@ void QgsPgTableModel::addTableEntry( const QgsPostgresLayerProperty &layerProper
QStandardItem *commentItem = new QStandardItem( layerProperty.tableComment );
QStandardItem *geomItem = new QStandardItem( layerProperty.geometryColName );
QStandardItem *sridItem = new QStandardItem( wkbType != QgsWkbTypes::NoGeometry ? QString::number( srid ) : QLatin1String( "" ) );
sridItem->setEditable( wkbType != QgsWkbTypes::NoGeometry && srid == INT_MIN );
sridItem->setEditable( wkbType != QgsWkbTypes::NoGeometry && srid == std::numeric_limits<int>::min() );
if ( sridItem->isEditable() )
{
sridItem->setText( tr( "Enter…" ) );
Expand Down Expand Up @@ -264,7 +264,7 @@ bool QgsPgTableModel::setData( const QModelIndex &idx, const QVariant &value, in
bool ok;
int srid = idx.sibling( idx.row(), DbtmSrid ).data().toInt( &ok );

if ( !ok || srid == INT_MIN )
if ( !ok || srid == std::numeric_limits<int>::min() )
tip = tr( "Enter a SRID into the '%1' column" ).arg( tr( "SRID" ) );
}

Expand Down
12 changes: 6 additions & 6 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -540,11 +540,11 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
bool isMaterializedView = relkind == QLatin1String( "m" );
QString comment = result.PQgetvalue( idx, 7 );

int srid = ssrid.isEmpty() ? INT_MIN : ssrid.toInt();
int srid = ssrid.isEmpty() ? std::numeric_limits<int>::min() : ssrid.toInt();
if ( majorVersion() >= 2 && srid == 0 )
{
// 0 doesn't constraint => detect
srid = INT_MIN;
srid = std::numeric_limits<int>::min();
}

#if 0
Expand Down Expand Up @@ -665,7 +665,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
//QgsDebugMsg( QString( "%1.%2.%3: %4" ).arg( schemaName ).arg( tableName ).arg( column ).arg( relkind ) );

layerProperty.types = QList<QgsWkbTypes::Type>() << QgsWkbTypes::Unknown;
layerProperty.srids = QList<int>() << INT_MIN;
layerProperty.srids = QList<int>() << std::numeric_limits<int>::min();
layerProperty.schemaName = schemaName;
layerProperty.tableName = tableName;
layerProperty.geometryColName = column;
Expand Down Expand Up @@ -754,7 +754,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
//QgsDebugMsg( QString( "%1.%2: %3" ).arg( schema ).arg( table ).arg( relkind ) );

layerProperty.types = QList<QgsWkbTypes::Type>() << QgsWkbTypes::NoGeometry;
layerProperty.srids = QList<int>() << INT_MIN;
layerProperty.srids = QList<int>() << std::numeric_limits<int>::min();
layerProperty.schemaName = schema;
layerProperty.tableName = table;
layerProperty.geometryColName = QString();
Expand Down Expand Up @@ -1486,8 +1486,8 @@ void QgsPostgresConn::retrieveLayerTypes( QgsPostgresLayerProperty &layerPropert

query += ',';

int srid = layerProperty.srids.value( 0, INT_MIN );
if ( srid == INT_MIN )
int srid = layerProperty.srids.value( 0, std::numeric_limits<int>::min() );
if ( srid == std::numeric_limits<int>::min() )
{
query += QStringLiteral( "%1(%2%3)" )
.arg( majorVersion() < 2 ? "srid" : "st_srid",
Expand Down
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspostgresdataitems.cpp
Expand Up @@ -555,7 +555,7 @@ QVector<QgsDataItem *> QgsPGSchemaItem::createChildren()

if ( !layerProperty.geometryColName.isNull() &&
( layerProperty.types.value( 0, QgsWkbTypes::Unknown ) == QgsWkbTypes::Unknown ||
layerProperty.srids.value( 0, INT_MIN ) == INT_MIN ) )
layerProperty.srids.value( 0, std::numeric_limits<int>::min() ) == std::numeric_limits<int>::min() ) )
{
if ( dontResolveType )
{
Expand Down
16 changes: 8 additions & 8 deletions src/server/services/wms/qgsmediancut.cpp
Expand Up @@ -67,14 +67,14 @@ namespace QgsWms
return false;
}

int rMin = INT_MAX;
int gMin = INT_MAX;
int bMin = INT_MAX;
int aMin = INT_MAX;
int rMax = INT_MIN;
int gMax = INT_MIN;
int bMax = INT_MIN;
int aMax = INT_MIN;
int rMin = std::numeric_limits<int>::max();
int gMin = std::numeric_limits<int>::max();
int bMin = std::numeric_limits<int>::max();
int aMin = std::numeric_limits<int>::max();
int rMax = std::numeric_limits<int>::min();
int gMax = std::numeric_limits<int>::min();
int bMax = std::numeric_limits<int>::min();
int aMax = std::numeric_limits<int>::min();

int currentRed = 0;
int currentGreen = 0;
Expand Down
5 changes: 5 additions & 0 deletions tests/code_layout/test_banned_keywords.sh
Expand Up @@ -17,6 +17,11 @@ HINTS[2]="Use the type-safe method std::numeric_limits<double>::min() instead (b
KEYWORDS[3]="DBL_EPSILON"
HINTS[3]="Use the type-safe method std::numeric_limits<double>::epsilon() instead"

KEYWORDS[4]="INT_MIN"
HINTS[4]="Use the type-safe method std::numeric_limits<int>::min() instead"

KEYWORDS[5]="INT_MAX"
HINTS[5]="Use the type-safe method std::numeric_limits<int>::max() instead"

RES=
DIR=$(git rev-parse --show-toplevel)
Expand Down

0 comments on commit c705670

Please sign in to comment.