Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++17: use if constexpr() construct #8716

Merged
merged 4 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/clang_static_analyzer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ permissions:
jobs:

clang_static_analyzer:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
if: "!contains(github.event.head_commit.message, '[ci skip]') && !contains(github.event.head_commit.message, '[skip ci]')"
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Run
run: docker run --rm -v $PWD:$PWD ubuntu:16.04 sh -c "cd $PWD && apt update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends sudo software-properties-common && DEBIAN_FRONTEND=noninteractive sh ./ci/travis/csa_common/before_install.sh && sh ./ci/travis/csa_common/install.sh && sh ./ci/travis/csa_common/script.sh"
run: docker run --rm -v $PWD:$PWD ubuntu:22.04 sh -c "cd $PWD && apt update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends sudo software-properties-common && DEBIAN_FRONTEND=noninteractive sh ./ci/travis/csa_common/before_install.sh && sh ./ci/travis/csa_common/install.sh && sh ./ci/travis/csa_common/script.sh"
31 changes: 17 additions & 14 deletions alg/gdalpansharpen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ void GDALPansharpenOperation::WeightedBroveyWithNoData(
WorkDataType noData, validValue;
GDALCopyWord(psOptions->dfNoData, noData);

if (!(std::numeric_limits<WorkDataType>::is_integer))
if constexpr (!(std::numeric_limits<WorkDataType>::is_integer))
validValue = static_cast<WorkDataType>(noData + 1e-5);
else if (noData == std::numeric_limits<WorkDataType>::min())
validValue = std::numeric_limits<WorkDataType>::min() + 1;
Expand Down Expand Up @@ -655,8 +655,11 @@ void GDALPansharpenOperation::WeightedBrovey3(
j];
WorkDataType nPansharpenedValue;
GDALCopyWord(nRawValue * dfFactor, nPansharpenedValue);
if (bHasBitDepth && nPansharpenedValue > nMaxValue)
nPansharpenedValue = nMaxValue;
if constexpr (bHasBitDepth)
{
if (nPansharpenedValue > nMaxValue)
nPansharpenedValue = nMaxValue;
}
GDALCopyWord(nPansharpenedValue, pDataBuf[i * nBandValues + j]);
}
}
Expand All @@ -673,15 +676,15 @@ size_t GDALPansharpenOperation::WeightedBroveyPositiveWeightsInternal(
const T *pPanBuffer, const T *pUpsampledSpectralBuffer, T *pDataBuf,
size_t nValues, size_t nBandValues, T nMaxValue) const
{
CPL_STATIC_ASSERT(NINPUT == 3 || NINPUT == 4);
CPL_STATIC_ASSERT(NOUTPUT == 3 || NOUTPUT == 4);
static_assert(NINPUT == 3 || NINPUT == 4);
static_assert(NOUTPUT == 3 || NOUTPUT == 4);
const XMMReg4Double w0 =
XMMReg4Double::Load1ValHighAndLow(psOptions->padfWeights + 0);
const XMMReg4Double w1 =
XMMReg4Double::Load1ValHighAndLow(psOptions->padfWeights + 1);
const XMMReg4Double w2 =
XMMReg4Double::Load1ValHighAndLow(psOptions->padfWeights + 2);
const XMMReg4Double w3 =
[[maybe_unused]] const XMMReg4Double w3 =
(NINPUT == 3)
? XMMReg4Double::Zero()
: XMMReg4Double::Load1ValHighAndLow(psOptions->padfWeights + 3);
Expand All @@ -703,7 +706,7 @@ size_t GDALPansharpenOperation::WeightedBroveyPositiveWeightsInternal(
XMMReg4Double val2 = XMMReg4Double::Load4Val(pUpsampledSpectralBuffer +
2 * nBandValues + j);
XMMReg4Double val3;
if (NINPUT == 4 || NOUTPUT == 4)
if constexpr (NINPUT == 4 || NOUTPUT == 4)
{
val3 = XMMReg4Double::Load4Val(pUpsampledSpectralBuffer +
3 * nBandValues + j);
Expand All @@ -712,7 +715,7 @@ size_t GDALPansharpenOperation::WeightedBroveyPositiveWeightsInternal(
pseudoPanchro += w0 * val0;
pseudoPanchro += w1 * val1;
pseudoPanchro += w2 * val2;
if (NINPUT == 4)
if constexpr (NINPUT == 4)
pseudoPanchro += w3 * val3;

/* Little trick to avoid use of ternary operator due to one of the
Expand All @@ -724,14 +727,14 @@ size_t GDALPansharpenOperation::WeightedBroveyPositiveWeightsInternal(
val0 = XMMReg4Double::Min(val0 * factor, maxValue);
val1 = XMMReg4Double::Min(val1 * factor, maxValue);
val2 = XMMReg4Double::Min(val2 * factor, maxValue);
if (NOUTPUT == 4)
if constexpr (NOUTPUT == 4)
{
val3 = XMMReg4Double::Min(val3 * factor, maxValue);
}
val0.Store4Val(pDataBuf + 0 * nBandValues + j);
val1.Store4Val(pDataBuf + 1 * nBandValues + j);
val2.Store4Val(pDataBuf + 2 * nBandValues + j);
if (NOUTPUT == 4)
if constexpr (NOUTPUT == 4)
{
val3.Store4Val(pDataBuf + 3 * nBandValues + j);
}
Expand All @@ -746,13 +749,13 @@ size_t GDALPansharpenOperation::WeightedBroveyPositiveWeightsInternal(
const T *pPanBuffer, const T *pUpsampledSpectralBuffer, T *pDataBuf,
size_t nValues, size_t nBandValues, T nMaxValue) const
{
// cppcheck-suppress knownConditionTrueFalse
CPLAssert(NINPUT == 3 || NINPUT == 4);
static_assert(NINPUT == 3 || NINPUT == 4);
const double dfw0 = psOptions->padfWeights[0];
const double dfw1 = psOptions->padfWeights[1];
const double dfw2 = psOptions->padfWeights[2];
// cppcheck-suppress knownConditionTrueFalse
const double dfw3 = (NINPUT == 3) ? 0 : psOptions->padfWeights[3];
[[maybe_unused]] const double dfw3 =
(NINPUT == 3) ? 0 : psOptions->padfWeights[3];
size_t j = 0; // Used after for.
for (; j + 1 < nValues; j += 2)
{
Expand All @@ -772,7 +775,7 @@ size_t GDALPansharpenOperation::WeightedBroveyPositiveWeightsInternal(
dfPseudoPanchro2 +=
dfw2 * pUpsampledSpectralBuffer[2 * nBandValues + j + 1];

if (NINPUT == 4)
if constexpr (NINPUT == 4)
{
dfPseudoPanchro +=
dfw3 * pUpsampledSpectralBuffer[3 * nBandValues + j];
Expand Down
12 changes: 5 additions & 7 deletions alg/gdalsievefilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <cstring>

#include <algorithm>
#include <cassert>
#include <set>
#include <vector>
#include <utility>
Expand Down Expand Up @@ -321,9 +322,9 @@ CPLErr CPL_STDCALL GDALSieveFilter(GDALRasterBandH hSrcBand,
/* Push the sizes of merged polygon fragments into the */
/* merged polygon id's count. */
/* -------------------------------------------------------------------- */
for (int iPoly = 0; oFirstEnum.panPolyIdMap != nullptr && // for Coverity
iPoly < oFirstEnum.nNextPolygonId;
iPoly++)
assert(oFirstEnum.panPolyIdMap != nullptr); // for Coverity
assert(oFirstEnum.panPolyValue != nullptr); // for Coverity
for (int iPoly = 0; iPoly < oFirstEnum.nNextPolygonId; iPoly++)
{
if (oFirstEnum.panPolyIdMap[iPoly] != iPoly)
{
Expand Down Expand Up @@ -462,10 +463,7 @@ CPLErr CPL_STDCALL GDALSieveFilter(GDALRasterBandH hSrcBand,
int nIsolatedSmall = 0;
int nSieveTargets = 0;

for (int iPoly = 0; oFirstEnum.panPolyIdMap != nullptr && // for Coverity
oFirstEnum.panPolyValue != nullptr && // for Coverity
iPoly < static_cast<int>(anPolySizes.size());
iPoly++)
for (int iPoly = 0; iPoly < static_cast<int>(anPolySizes.size()); iPoly++)
{
if (oFirstEnum.panPolyIdMap[iPoly] != iPoly)
continue;
Expand Down
8 changes: 4 additions & 4 deletions alg/gdalwarpkernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5700,14 +5700,14 @@ static void GWKResampleNoMasksOrDstDensityOnlyThreadInternal(void *pData)
for (int iBand = 0; iBand < poWK->nBands; iBand++)
{
T value = 0;
if (eResample == GRA_NearestNeighbour)
if constexpr (eResample == GRA_NearestNeighbour)
{
value = reinterpret_cast<T *>(
poWK->papabySrcImage[iBand])[iSrcOffset];
}
else if (bUse4SamplesFormula)
else if constexpr (bUse4SamplesFormula)
{
if (eResample == GRA_Bilinear)
if constexpr (eResample == GRA_Bilinear)
GWKBilinearResampleNoMasks4SampleT(
poWK, iBand, padfX[iDstX] - poWK->nSrcXOff,
padfY[iDstX] - poWK->nSrcYOff, &value);
Expand Down Expand Up @@ -5774,7 +5774,7 @@ static void GWKResampleNoMasksOrDstDensityOnlyHas4SampleThread(void *pData)
{
GWKJobStruct *psJob = static_cast<GWKJobStruct *>(pData);
GDALWarpKernel *poWK = psJob->poWK;
CPLAssert(eResample == GRA_Bilinear || eResample == GRA_Cubic);
static_assert(eResample == GRA_Bilinear || eResample == GRA_Cubic);
const bool bUse4SamplesFormula =
poWK->dfXScale >= 0.95 && poWK->dfYScale >= 0.95;
if (bUse4SamplesFormula)
Expand Down
20 changes: 8 additions & 12 deletions apps/gdalmanage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,29 @@ static void Identify(int nArgc, char **papszArgv)
bool bForceRecurse = false;
bool bReportFailures = false;

while (nArgc > 0 && papszArgv[0][0] == '-')
int i = 0;
for (; i < nArgc && papszArgv[i][0] == '-'; ++i)
{
if (EQUAL(papszArgv[0], "-r"))
if (EQUAL(papszArgv[i], "-r"))
bRecursive = true;
else if (EQUAL(papszArgv[0], "-fr"))
else if (EQUAL(papszArgv[i], "-fr"))
{
bForceRecurse = true;
bRecursive = true;
}
else if (EQUAL(papszArgv[0], "-u"))
else if (EQUAL(papszArgv[i], "-u"))
bReportFailures = true;
else
Usage(true);

papszArgv++;
nArgc--;
}

/* -------------------------------------------------------------------- */
/* Process given files. */
/* -------------------------------------------------------------------- */
while (nArgc > 0)
for (; i < nArgc; ++i)
{
ProcessIdentifyTarget(papszArgv[0], nullptr, bRecursive,
ProcessIdentifyTarget(papszArgv[i], nullptr, bRecursive,
bReportFailures, bForceRecurse);
nArgc--;
papszArgv++;
}
}

Expand Down Expand Up @@ -193,7 +189,7 @@ MAIN_START(argc, argv)
if (argc < 1)
exit(-argc);

for (int i = 0; argv != nullptr && argv[i] != nullptr; i++)
for (int i = 0; i < argc; i++)
{
if (EQUAL(argv[i], "--help"))
{
Expand Down
3 changes: 1 addition & 2 deletions apps/test_ogrsf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ MAIN_START(nArgc, papszArgv)
{
pszSQLStatement = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-dialect") &&
papszArgv[iArg + 1] != nullptr)
else if (EQUAL(papszArgv[iArg], "-dialect") && iArg + 1 < nArgc)
{
pszDialect = papszArgv[++iArg];
}
Expand Down
107 changes: 72 additions & 35 deletions ci/travis/csa_common/before_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,85 @@

set -e

sudo apt-get purge -y libgeos*
(sudo mv /etc/apt/sources.list.d/pgdg* /tmp || /bin/true)
#sudo apt-get remove postgresql-9.2
#sudo apt-get remove postgis libpq5 libpq-dev postgresql-9.1-postgis postgresql-9.1-postgis-2.2-scripts postgresql-9.2-postgis postgresql-9.3-postgis postgresql-9.1 postgresql-9.2 postgresql-9.3 libgdal1
# This currently fails as of https://lists.osgeo.org/pipermail/ubuntu/2023-October/002046.html
# sudo add-apt-repository -y ppa:ubuntugis/ubuntugis-unstable
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6B827C12C2D425E227EDCA75089EBE08314DF160
sudo add-apt-repository -y http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu/
#sudo add-apt-repository -y ppa:marlam/gta
sudo apt-get update -qq
sudo apt-get install -y automake python-numpy postgis libpq-dev libpng12-dev libjpeg-dev libgif-dev liblzma-dev libgeos-dev libcurl4-gnutls-dev libproj-dev libxml2-dev libexpat-dev libxerces-c-dev libnetcdf-dev netcdf-bin libpoppler-dev libpoppler-private-dev libspatialite-dev gpsbabel swig libhdf4-alt-dev libhdf5-serial-dev libhdf5-dev libpodofo-dev poppler-utils libfreexl-dev unixodbc-dev libwebp-dev openjdk-8-jdk libepsilon-dev liblcms2-2 libpcre3-dev mercurial cmake libkml-dev libopenjp2-7-dev libzstd1-dev sqlite3 wget unzip curl
# libgda-dev
# libcrypto++-dev
#sudo apt-get install python-lxml
#sudo apt-get install python-pip
sudo apt-get install libogdi3.2-dev
# Boost for Mongo
#sudo apt-get install libboost-regex-dev libboost-system-dev libboost-thread-dev
#sudo pip install pyflakes
#pyflakes autotest
#pyflakes gdal/swig/python/gdal-utils/scripts
#pyflakes gdal/swig/python/gdal-utils/osgeo_utils/samples
#psql -c "drop database if exists autotest" -U postgres
#psql -c "create database autotest" -U postgres
#psql -c "create extension postgis" -d autotest -U postgres
#mysql -e "create database autotest;"
#mysql -e "GRANT ALL ON autotest.* TO 'root'@'localhost';" -u root
#mysql -e "GRANT ALL ON autotest.* TO 'travis'@'localhost';" -u root
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y --allow-unauthenticated autoconf \
automake \
bash \
ccache \
cmake \
curl \
doxygen \
fossil \
g++ \
git \
gpsbabel \
libblosc-dev \
libboost-dev \
libcairo2-dev \
libcfitsio-dev \
libcrypto++-dev \
libcurl4-gnutls-dev \
libexpat-dev \
libfcgi-dev \
libfyba-dev \
libfreexl-dev \
libgeos-dev \
libgeotiff-dev \
libgif-dev \
libhdf4-alt-dev \
libhdf5-serial-dev \
libjpeg-dev \
libkml-dev \
liblcms2-2 \
liblz4-dev \
liblzma-dev \
libmysqlclient-dev \
libnetcdf-dev \
libogdi-dev \
libopenexr-dev \
libopenjp2-7-dev \
libpcre3-dev \
libpng-dev \
libpoppler-dev \
libpoppler-private-dev \
libpq-dev \
libproj-dev \
librasterlite2-dev \
libspatialite-dev \
libssl-dev \
libwebp-dev \
libxerces-c-dev \
libxml2-dev \
libxslt-dev \
libzstd-dev \
locales \
mysql-client-core-8.0 \
netcdf-bin \
openjdk-8-jdk \
poppler-utils \
postgis \
postgresql-client \
python3-dev \
python3-numpy \
python3-pip \
sqlite3 \
swig \
unixodbc-dev \
wget \
zip

sudo DEBIAN_FRONTEND=noninteractive apt-get install -y --allow-unauthenticated clang-tools

wget https://github.com/Esri/file-geodatabase-api/raw/master/FileGDB_API_1.5/FileGDB_API_1_5_64gcc51.tar.gz
tar xzf FileGDB_API_1_5_64gcc51.tar.gz
sudo cp -r FileGDB_API-64gcc51/include/* /usr/local/include
sudo cp -r FileGDB_API-64gcc51/lib/* /usr/local/lib
sudo rm -f /usr/local/lib/libstdc++.so*

wget http://s3.amazonaws.com/etc-data.koordinates.com/gdal-travisci/install-libecwj2-ubuntu12.04-64bit.tar.gz
tar xzf install-libecwj2-ubuntu12.04-64bit.tar.gz
sudo cp -r install-libecwj2/include/* /usr/local/include
sudo cp -r install-libecwj2/lib/* /usr/local/lib
wget https://github.com/rouault/libecwj2-3.3-builds/releases/download/v1/install-libecwj2-3.3-ubuntu-20.04.tar.gz
tar xzf install-libecwj2-3.3-ubuntu-20.04.tar.gz
sudo mv opt/libecwj2-3.3 /opt
sudo sh -c 'echo "/opt/libecwj2-3.3/lib" > /etc/ld.so.conf.d/libecwj2-3.3.conf'

wget https://github.com/ubarsc/kealib/archive/kealib-1.4.12.zip
unzip kealib-1.4.12.zip
Expand All @@ -48,7 +89,3 @@ cmake . -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX
make -j4;
sudo make install)
sudo ldconfig

FILE=clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz
URL_ROOT=https://github.com/rouault/gdal_ci_tools/raw/master/${FILE}
curl -Ls ${URL_ROOT}aa ${URL_ROOT}ab ${URL_ROOT}ac ${URL_ROOT}ad ${URL_ROOT}ae | tar xJf -
17 changes: 3 additions & 14 deletions ci/travis/csa_common/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,15 @@ case $SCRIPT_DIR in
esac
$SCRIPT_DIR/../common_install.sh

# Build proj
(cd proj; ./autogen.sh && CFLAGS='-DPROJ_RENAME_SYMBOLS' CXXFLAGS='-DPROJ_RENAME_SYMBOLS' ./configure --disable-static --prefix=/usr/local && make -j3)
(cd proj; sudo make -j3 install && sudo ldconfig)

export PATH=$PWD/clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-16.04/bin:$PATH

wget -q https://github.com/Kitware/CMake/releases/download/v3.22.3/cmake-3.22.3-linux-x86_64.tar.gz
tar xzf cmake-3.22.3-linux-x86_64.tar.gz
export PATH=$PWD/cmake-3.22.3-linux-x86_64/bin:$PATH

GDAL_TOPDIR=$PWD

mkdir build
cd build
export CXXFLAGS="-std=c++11 -DCSA_BUILD -DPROJ_RENAME_SYMBOLS"
export CFLAGS="-DCSA_BUILD -DPROJ_RENAME_SYMBOLS"
export CXXFLAGS="-DCSA_BUILD"
export CFLAGS="-DCSA_BUILD"
scan-build cmake .. \
-DCMAKE_BUILD_TYPE=Debug \
-DPROJ_ROOT=/usr/local \
-DECW_ROOT=/usr/local \
-DECW_ROOT=/opt \
-DFileGDB_ROOT=/usr/local \
-DGDAL_ENABLE_DRIVER_GRIB=OFF \
-DBUILD_PYTHON_BINDINGS=OFF \
Expand Down
Loading
Loading