From 1077b0d2a60224d935951049f65ee34016aa5084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Mon, 21 Sep 2020 19:41:28 +0200 Subject: [PATCH] small compileroptions.cmake cleanup (#2817) * compileroptions.cmake: moved common warnings to proper scope and aligned them with other build systems / fixed some -Wrange-loop-construct clang warnings * compileroptions.cmake: use check_cxx_compiler_flag() via add_compile_options_safe() helper to avoid compiler version checks --- cmake/compileroptions.cmake | 38 +++++++++++++++++++++---------------- gui/mainwindow.cpp | 4 ++-- gui/projectfile.cpp | 6 +++--- gui/resultsview.cpp | 4 ++-- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/cmake/compileroptions.cmake b/cmake/compileroptions.cmake index 829ce3a6d..1021707a2 100644 --- a/cmake/compileroptions.cmake +++ b/cmake/compileroptions.cmake @@ -1,3 +1,12 @@ +include(CheckCXXCompilerFlag) + +function(add_compile_options_safe FLAG) + check_cxx_compiler_flag(${FLAG} _has_flag) + if (_has_flag) + add_compile_options(${FLAG}) + endif() +endfunction() + if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") if(CMAKE_BUILD_TYPE MATCHES "Release") # "Release" uses -O3 by default @@ -6,20 +15,15 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang" if (WARNINGS_ARE_ERRORS) add_compile_options(-Werror) endif() -endif() - -if (CMAKE_CXX_COMPILER_ID MATCHES "GNU") - if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6) - message(FATAL_ERROR "${PROJECT_NAME} c++11 support requires g++ 4.6 or greater, but it is ${CMAKE_CXX_COMPILER_VERSION}") - endif () - + add_compile_options(-pedantic) + add_compile_options(-Wall) + add_compile_options(-Wextra) add_compile_options(-Wcast-qual) # Cast for removing type qualifiers add_compile_options(-Wno-deprecated-declarations) add_compile_options(-Wfloat-equal) # Floating values used in equality comparisons add_compile_options(-Wmissing-declarations) # If a global function is defined without a previous declaration add_compile_options(-Wmissing-format-attribute) # add_compile_options(-Wno-long-long) - add_compile_options(-Woverloaded-virtual) # when a function declaration hides virtual functions from a base class add_compile_options(-Wpacked) # add_compile_options(-Wredundant-decls) # if anything is declared more than once in the same scope add_compile_options(-Wundef) @@ -28,22 +32,24 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU") add_compile_options(-Wno-missing-braces) add_compile_options(-Wno-sign-compare) add_compile_options(-Wno-multichar) +endif() + +if (CMAKE_CXX_COMPILER_ID MATCHES "GNU") + if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6) + message(FATAL_ERROR "${PROJECT_NAME} c++11 support requires g++ 4.6 or greater, but it is ${CMAKE_CXX_COMPILER_VERSION}") + endif () + + add_compile_options(-Woverloaded-virtual) # when a function declaration hides virtual functions from a base class add_compile_options(-Wno-maybe-uninitialized) # there are some false positives elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - add_compile_options(-Wno-deprecated-declarations) add_compile_options(-Wno-four-char-constants) add_compile_options(-Wno-missing-braces) - add_compile_options(-Wno-missing-field-initializers) - add_compile_options(-Wno-multichar) - add_compile_options(-Wno-sign-compare) add_compile_options(-Wno-unused-function) - if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "8.0.0") - add_compile_options(-Wextra-semi-stmt) - endif() + add_compile_options_safe(-Wextra-semi-stmt) if(ENABLE_COVERAGE OR ENABLE_COVERAGE_XML) - message(FATAL_ERROR "Not use clang for generate code coverage. Use gcc.") + message(FATAL_ERROR "Do not use clang for generate code coverage. Use gcc.") endif() endif() diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index d763d6eaf..5fd02499f 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -610,7 +610,7 @@ void MainWindow::updateFunctionContractsTab() { QStringList addedContracts; if (mProjectFile) { - for (const auto it: mProjectFile->getFunctionContracts()) { + for (const auto& it: mProjectFile->getFunctionContracts()) { addedContracts << QString::fromStdString(it.first); } } @@ -876,7 +876,7 @@ Settings MainWindow::getCppcheckSettings() result.functionContracts = mProjectFile->getFunctionContracts(); - for (const auto vc: mProjectFile->getVariableContracts()) + for (const auto& vc: mProjectFile->getVariableContracts()) result.variableContracts[vc.first.toStdString()] = vc.second; const QStringList undefines = mProjectFile->getUndefines(); diff --git a/gui/projectfile.cpp b/gui/projectfile.cpp index 810f42212..926d55d0c 100644 --- a/gui/projectfile.cpp +++ b/gui/projectfile.cpp @@ -958,7 +958,7 @@ bool ProjectFile::write(const QString &filename) if (!mFunctionContracts.empty()) { xmlWriter.writeStartElement(CppcheckXml::FunctionContracts); - for (const auto contract: mFunctionContracts) { + for (const auto& contract: mFunctionContracts) { xmlWriter.writeStartElement(CppcheckXml::FunctionContract); xmlWriter.writeAttribute(CppcheckXml::ContractFunction, QString::fromStdString(contract.first)); xmlWriter.writeAttribute(CppcheckXml::ContractExpects, QString::fromStdString(contract.second)); @@ -1025,7 +1025,7 @@ bool ProjectFile::write(const QString &filename) writeStringList(xmlWriter, mTags, CppcheckXml::TagsElementName, CppcheckXml::TagElementName); if (!mWarningTags.empty()) { QStringList tags; - for (const auto wt: mWarningTags) { + for (const auto& wt: mWarningTags) { if (!tags.contains(wt.second)) tags.append(wt.second); } @@ -1033,7 +1033,7 @@ bool ProjectFile::write(const QString &filename) xmlWriter.writeStartElement(CppcheckXml::TagWarningsElementName); xmlWriter.writeAttribute(CppcheckXml::TagAttributeName, tag); QStringList warnings; - for (const auto wt: mWarningTags) { + for (const auto& wt: mWarningTags) { if (wt.second == tag) { xmlWriter.writeStartElement(CppcheckXml::WarningElementName); xmlWriter.writeAttribute(CppcheckXml::HashAttributeName, QString::number(wt.first)); diff --git a/gui/resultsview.cpp b/gui/resultsview.cpp index cb77e7912..6b43ee998 100644 --- a/gui/resultsview.cpp +++ b/gui/resultsview.cpp @@ -103,7 +103,7 @@ void ResultsView::setAddedFunctionContracts(const QStringList &addedContracts) { mUI.mListAddedContracts->clear(); mUI.mListAddedContracts->addItems(addedContracts); - for (const QString f: addedContracts) { + for (const QString& f: addedContracts) { auto res = mUI.mListMissingContracts->findItems(f, Qt::MatchExactly); if (!res.empty()) delete res.front(); @@ -114,7 +114,7 @@ void ResultsView::setAddedVariableContracts(const QStringList &added) { mUI.mListAddedVariables->clear(); mUI.mListAddedVariables->addItems(added); - for (const QString var: added) { + for (const QString& var: added) { for (auto item: mUI.mListMissingVariables->findItems(var, Qt::MatchExactly)) delete item; mVariableContracts.insert(var);