fixed/suppressed several compiler warnings (#5327)

This commit is contained in:
Oliver Stöneberg 2023-08-16 19:35:53 +02:00 committed by GitHub
parent 84a9b3a5f2
commit d8b44dff56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 96 additions and 42 deletions

View File

@ -85,6 +85,8 @@ jobs:
sudo apt-get update
sudo apt-get install libxml2-utils qtbase5-dev qttools5-dev libqt5charts5-dev qtchooser
# TODO: move latest compiler to separate step
# TODO: bail out on warnings with latest GCC
- name: Set up GCC
uses: egor-tensin/setup-gcc@v1
if: matrix.os == 'ubuntu-22.04'
@ -93,9 +95,9 @@ jobs:
platform: x64
- name: Select compiler
if: matrix.os == 'ubuntu-22.04'
run: |
echo "CXX=g++-13" >> $GITHUB_ENV
if: matrix.os == 'ubuntu-22.04'
# coreutils contains "nproc"
- name: Install missing software on macos

View File

@ -13,6 +13,8 @@ defaults:
run:
shell: cmd
# TODO: choose/add a step to bail out on compiler warnings (maybe even the release build)
jobs:
build_qt:

View File

@ -545,7 +545,7 @@ $(libcppdir)/clangimport.o: lib/clangimport.cpp lib/clangimport.h lib/config.h l
$(libcppdir)/color.o: lib/color.cpp lib/color.h lib/config.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/color.cpp
$(libcppdir)/cppcheck.o: lib/cppcheck.cpp externals/picojson/picojson.h externals/simplecpp/simplecpp.h externals/tinyxml2/tinyxml2.h lib/analyzerinfo.h lib/check.h lib/checkunusedfunctions.h lib/clangimport.h lib/color.h lib/config.h lib/cppcheck.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/version.h lib/vfvalue.h
$(libcppdir)/cppcheck.o: lib/cppcheck.cpp externals/picojson/picojson.h externals/simplecpp/simplecpp.h externals/tinyxml2/tinyxml2.h lib/analyzerinfo.h lib/check.h lib/checkunusedfunctions.h lib/clangimport.h lib/color.h lib/config.h lib/cppcheck.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/json.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/version.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/cppcheck.cpp
$(libcppdir)/ctu.o: lib/ctu.cpp externals/tinyxml2/tinyxml2.h lib/astutils.h lib/check.h lib/color.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
@ -563,7 +563,7 @@ $(libcppdir)/forwardanalyzer.o: lib/forwardanalyzer.cpp lib/analyzer.h lib/astut
$(libcppdir)/fwdanalysis.o: lib/fwdanalysis.cpp lib/astutils.h lib/config.h lib/errortypes.h lib/fwdanalysis.h lib/library.h lib/mathlib.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/fwdanalysis.cpp
$(libcppdir)/importproject.o: lib/importproject.cpp externals/picojson/picojson.h externals/simplecpp/simplecpp.h externals/tinyxml2/tinyxml2.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
$(libcppdir)/importproject.o: lib/importproject.cpp externals/picojson/picojson.h externals/simplecpp/simplecpp.h externals/tinyxml2/tinyxml2.h lib/config.h lib/errortypes.h lib/importproject.h lib/json.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/importproject.cpp
$(libcppdir)/infer.o: lib/infer.cpp lib/calculate.h lib/config.h lib/errortypes.h lib/infer.h lib/mathlib.h lib/valueptr.h lib/vfvalue.h
@ -599,7 +599,7 @@ $(libcppdir)/programmemory.o: lib/programmemory.cpp lib/astutils.h lib/calculate
$(libcppdir)/reverseanalyzer.o: lib/reverseanalyzer.cpp lib/analyzer.h lib/astutils.h lib/config.h lib/errortypes.h lib/forwardanalyzer.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/reverseanalyzer.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueptr.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/reverseanalyzer.cpp
$(libcppdir)/settings.o: lib/settings.cpp externals/picojson/picojson.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/summaries.h lib/suppressions.h lib/timer.h lib/utils.h lib/vfvalue.h
$(libcppdir)/settings.o: lib/settings.cpp externals/picojson/picojson.h lib/config.h lib/errortypes.h lib/importproject.h lib/json.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/summaries.h lib/suppressions.h lib/timer.h lib/utils.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/settings.cpp
$(libcppdir)/summaries.o: lib/summaries.cpp lib/analyzerinfo.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/summaries.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h

View File

@ -95,8 +95,6 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options_safe(-Wno-suggest-override) # TODO: enable when warnings are fixed in in tinyxml2
add_compile_options_safe(-Wno-suggest-destructor-override) # TODO: enable when warnings are fixed in in tinyxml2
add_compile_options_safe(-Wno-extra-semi-stmt) # TODO: enable when warnings are fixed in in tinyxml2
add_compile_options_safe(-Wno-implicitly-unsigned-literal)
add_compile_options_safe(-Wno-tautological-type-limit-compare)
add_compile_options(-Wno-disabled-macro-expansion)
add_compile_options_safe(-Wno-bitwise-instead-of-logical)

View File

@ -812,7 +812,15 @@ void ResultsTree::startApplication(QStandardItem *target, int application)
const QString cmdLine = QString("%1 %2").arg(program).arg(params);
// this is reported as deprecated in Qt 5.15.2 but no longer in Qt 6
const bool success = SUPPRESS_DEPRECATED_WARNING(QProcess::startDetached(cmdLine));
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
SUPPRESS_WARNING_CLANG_PUSH("-Wdeprecated")
SUPPRESS_WARNING_GCC_PUSH("-Wdeprecated-declarations")
#endif
const bool success = QProcess::startDetached(cmdLine);
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
SUPPRESS_WARNING_GCC_POP
SUPPRESS_WARNING_CLANG_POP
#endif
if (!success) {
QString text = tr("Could not start %1\n\nPlease check the application path and parameters are correct.").arg(program);

View File

@ -56,16 +56,3 @@ endif()
if (NOT CMAKE_DISABLE_PRECOMPILE_HEADERS)
target_precompile_headers(cppcheck-core PRIVATE precompiled.h)
endif()
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# -Wfloat-equal is generated by picojson.h
if (NOT USE_MATCHCOMPILER_OPT STREQUAL "Off")
set_source_files_properties(mc_cppcheck.cpp PROPERTIES COMPILE_FLAGS -Wno-float-equal)
set_source_files_properties(mc_importproject.cpp PROPERTIES COMPILE_FLAGS -Wno-float-equal)
set_source_files_properties(mc_settings.cpp PROPERTIES COMPILE_FLAGS -Wno-float-equal)
else()
set_source_files_properties(cppcheck.cpp PROPERTIES COMPILE_FLAGS -Wno-float-equal)
set_source_files_properties(importproject.cpp PROPERTIES COMPILE_FLAGS -Wno-float-equal)
set_source_files_properties(settings.cpp PROPERTIES COMPILE_FLAGS -Wno-float-equal)
endif()
endif()

View File

@ -133,13 +133,26 @@ static const std::string emptyString;
#define STRINGISIZE(...) #__VA_ARGS__
#ifdef __clang__
#define SUPPRESS_WARNING(warning, ...)_Pragma("clang diagnostic push") _Pragma(STRINGISIZE(clang diagnostic ignored warning)) __VA_ARGS__ _Pragma("clang diagnostic pop")
#define SUPPRESS_DEPRECATED_WARNING(...) SUPPRESS_WARNING("-Wdeprecated", __VA_ARGS__)
#define SUPPRESS_FLOAT_EQUAL_WARNING(...) SUPPRESS_WARNING("-Wfloat-equal", __VA_ARGS__)
#define SUPPRESS_WARNING_PUSH(warning) _Pragma("clang diagnostic push") _Pragma(STRINGISIZE(clang diagnostic ignored warning))
#define SUPPRESS_WARNING_POP _Pragma("clang diagnostic pop")
#define SUPPRESS_WARNING_GCC_PUSH(warning)
#define SUPPRESS_WARNING_GCC_POP
#define SUPPRESS_WARNING_CLANG_PUSH(warning) SUPPRESS_WARNING_PUSH(warning)
#define SUPPRESS_WARNING_CLANG_POP SUPPRESS_WARNING_POP
#elif defined(__GNUC__)
#define SUPPRESS_WARNING_PUSH(warning) _Pragma("GCC diagnostic push") _Pragma(STRINGISIZE(GCC diagnostic ignored warning))
#define SUPPRESS_WARNING_POP _Pragma("GCC diagnostic pop")
#define SUPPRESS_WARNING_GCC_PUSH(warning) SUPPRESS_WARNING_PUSH(warning)
#define SUPPRESS_WARNING_GCC_POP SUPPRESS_WARNING_POP
#define SUPPRESS_WARNING_CLANG_PUSH(warning)
#define SUPPRESS_WARNING_CLANG_POP
#else
#define SUPPRESS_WARNING(warning, ...) __VA_ARGS__
#define SUPPRESS_DEPRECATED_WARNING(...) __VA_ARGS__
#define SUPPRESS_FLOAT_EQUAL_WARNING(...) __VA_ARGS__
#define SUPPRESS_WARNING_PUSH(warning)
#define SUPPRESS_WARNING_POP
#define SUPPRESS_WARNING_GCC_PUSH(warning)
#define SUPPRESS_WARNING_GCC_POP
#define SUPPRESS_WARNING_CLANG_PUSH(warning)
#define SUPPRESS_WARNING_CLANG_POP
#endif
#if !defined(NO_WINDOWS_SEH) && defined(_WIN32) && defined(_MSC_VER)

View File

@ -63,8 +63,7 @@
#include <process.h>
#endif
#define PICOJSON_USE_INT64
#include <picojson.h>
#include "json.h"
#include <simplecpp.h>
@ -940,7 +939,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
#ifdef HAVE_RULES
// handling of "simple" rules has been removed.
if (mSimplify && hasRule("simple"))
if (hasRule("simple"))
throw InternalError(nullptr, "Handling of \"simple\" rules has been removed in Cppcheck. Use --addon instead.");
#endif

View File

@ -224,9 +224,6 @@ private:
/** Are there too many configs? */
bool mTooManyConfigs{};
/** Simplify code? true by default */
bool mSimplify = true;
/** File info used for whole program analysis */
std::list<Check::FileInfo*> mFileInfo;

View File

@ -1,4 +1,4 @@
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug-PCRE|Win32">
<Configuration>Debug-PCRE</Configuration>
@ -150,6 +150,7 @@
<ClInclude Include="fwdanalysis.h" />
<ClInclude Include="importproject.h" />
<ClInclude Include="infer.h" />
<ClInclude Include="json.h" />
<ClInclude Include="keywords.h" />
<ClInclude Include="library.h" />
<ClInclude Include="mathlib.h" />

View File

@ -40,8 +40,7 @@
#include <simplecpp.h>
#define PICOJSON_USE_INT64
#include <picojson.h>
#include "json.h"
ImportProject::ImportProject()
{

33
lib/json.h Normal file
View File

@ -0,0 +1,33 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2023 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef jsonH
#define jsonH
SUPPRESS_WARNING_PUSH("-Wfloat-equal")
SUPPRESS_WARNING_CLANG_PUSH("-Wtautological-type-limit-compare")
SUPPRESS_WARNING_GCC_PUSH("-Wparentheses")
#define PICOJSON_USE_INT64
#include <picojson.h>
SUPPRESS_WARNING_GCC_POP
SUPPRESS_WARNING_CLANG_POP
SUPPRESS_WARNING_POP
#endif // jsonH

View File

@ -18,6 +18,8 @@
#include "keywords.h"
#include <cassert>
// see https://en.cppreference.com/w/c/keyword
#define C90_KEYWORDS \
@ -162,6 +164,7 @@ const std::unordered_set<std::string>& Keywords::getAll(Standards::cstd_t cStd)
/*case Standards::cstd_t::C23:
return c23_keywords_all;*/
}
assert(false && "unreachable");
}
// cppcheck-suppress unusedFunction
@ -181,6 +184,7 @@ const std::unordered_set<std::string>& Keywords::getAll(Standards::cppstd_t cppS
case Standards::cppstd_t::CPP23:
return cpp23_keywords_all;
}
assert(false && "unreachable");
}
// cppcheck-suppress unusedFunction
@ -197,6 +201,7 @@ const std::unordered_set<std::string>& Keywords::getOnly(Standards::cstd_t cStd)
/*case Standards::cstd_t::C23:
return c23_keywords_all;*/
}
assert(false && "unreachable");
}
// cppcheck-suppress unusedFunction
@ -217,5 +222,6 @@ const std::unordered_set<std::string>& Keywords::getOnly(Standards::cppstd_t cpp
case Standards::cppstd_t::CPP23:
return cpp23_keywords;
}
assert(false && "unreachable");
}

View File

@ -44,6 +44,7 @@ HEADERS += $${PWD}/analyzer.h \
$${PWD}/fwdanalysis.h \
$${PWD}/importproject.h \
$${PWD}/infer.h \
$${PWD}/json.h \
$${PWD}/keywords.h \
$${PWD}/library.h \
$${PWD}/mathlib.h \

View File

@ -24,8 +24,7 @@
#include <fstream>
#define PICOJSON_USE_INT64
#include <picojson.h>
#include "json.h"
std::atomic<bool> Settings::mTerminated;

View File

@ -3025,21 +3025,21 @@ bool Function::returnsReference(const Function* function, bool unknown, bool inc
bool Function::returnsPointer(const Function* function, bool unknown)
{
return checkReturns(function, unknown, false, [](UNUSED const Token* defStart, const Token* defEnd) {
return checkReturns(function, unknown, false, [](const Token* /*defStart*/, const Token* defEnd) {
return Token::simpleMatch(defEnd->previous(), "*");
});
}
bool Function::returnsStandardType(const Function* function, bool unknown)
{
return checkReturns(function, unknown, true, [](UNUSED const Token* defStart, const Token* defEnd) {
return checkReturns(function, unknown, true, [](const Token* /*defStart*/, const Token* defEnd) {
return defEnd->previous() && defEnd->previous()->isStandardType();
});
}
bool Function::returnsVoid(const Function* function, bool unknown)
{
return checkReturns(function, unknown, true, [](UNUSED const Token* defStart, const Token* defEnd) {
return checkReturns(function, unknown, true, [](const Token* /*defStart*/, const Token* defEnd) {
return Token::simpleMatch(defEnd->previous(), "void");
});
}

View File

@ -382,6 +382,8 @@ private:
ASSERT_THROW_EQUALS(MathLib::toLongNumber("-02000000000000000000000"), InternalError, "Internal Error. MathLib::toLongNumber: out_of_range: -02000000000000000000000");
// min/max and out-of-bounds - decimal
SUPPRESS_WARNING_CLANG_PUSH("-Wimplicitly-unsigned-literal")
SUPPRESS_WARNING_GCC_PUSH("-Woverflow")
{
const MathLib::bigint i = 18446744073709551615;
ASSERT_EQUALS(i, MathLib::toLongNumber(std::to_string(i)));
@ -392,8 +394,10 @@ private:
ASSERT_EQUALS(i, MathLib::toLongNumber(std::to_string(i)));
ASSERT_EQUALS(i, MathLib::toLongNumber("-18446744073709551615"));
}
SUPPRESS_WARNING_GCC_POP
SUPPRESS_WARNING_CLANG_POP
ASSERT_THROW_EQUALS(MathLib::toLongNumber("18446744073709551616"), InternalError, "Internal Error. MathLib::toLongNumber: out_of_range: 18446744073709551616");
ASSERT_THROW_EQUALS(MathLib::toLongNumber("18446744073709551616"), InternalError, "Internal Error. MathLib::toLongNumber: out_of_range: 18446744073709551616");
ASSERT_THROW_EQUALS(MathLib::toLongNumber("-18446744073709551616"), InternalError, "Internal Error. MathLib::toLongNumber: out_of_range: -18446744073709551616");
ASSERT_THROW_EQUALS(MathLib::toLongNumber("invalid"), InternalError, "Internal Error. MathLib::toLongNumber: invalid_argument: invalid");
@ -539,6 +543,8 @@ private:
ASSERT_THROW_EQUALS(MathLib::toULongNumber("-02000000000000000000000"), InternalError, "Internal Error. MathLib::toULongNumber: out_of_range: -02000000000000000000000");
// min/max and out-of-bounds - decimal
SUPPRESS_WARNING_CLANG_PUSH("-Wimplicitly-unsigned-literal")
SUPPRESS_WARNING_GCC_PUSH("-Woverflow")
{
const MathLib::biguint u = 18446744073709551615;
ASSERT_EQUALS(u, MathLib::toULongNumber(std::to_string(u)));
@ -549,8 +555,10 @@ private:
ASSERT_EQUALS(u, MathLib::toULongNumber(std::to_string(u)));
ASSERT_EQUALS(u, MathLib::toULongNumber("-18446744073709551615"));
}
SUPPRESS_WARNING_GCC_POP
SUPPRESS_WARNING_CLANG_POP
ASSERT_THROW_EQUALS(MathLib::toULongNumber("18446744073709551616"), InternalError, "Internal Error. MathLib::toULongNumber: out_of_range: 18446744073709551616");
ASSERT_THROW_EQUALS(MathLib::toULongNumber("18446744073709551616"), InternalError, "Internal Error. MathLib::toULongNumber: out_of_range: 18446744073709551616");
ASSERT_THROW_EQUALS(MathLib::toULongNumber("-18446744073709551616"), InternalError, "Internal Error. MathLib::toULongNumber: out_of_range: -18446744073709551616");
ASSERT_THROW_EQUALS(MathLib::toULongNumber("invalid"), InternalError, "Internal Error. MathLib::toULongNumber: invalid_argument: invalid");

View File

@ -314,6 +314,7 @@ int main(int argc, char **argv)
libfiles_h.emplace_back("analyzer.h");
libfiles_h.emplace_back("calculate.h");
libfiles_h.emplace_back("config.h");
libfiles_h.emplace_back("json.h");
libfiles_h.emplace_back("precompiled.h");
libfiles_h.emplace_back("smallvector.h");
libfiles_h.emplace_back("standards.h");