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
This commit is contained in:
Oliver Stöneberg 2020-09-21 19:41:28 +02:00 committed by GitHub
parent 7189b303ae
commit 1077b0d2a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 23 deletions

View File

@ -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_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if(CMAKE_BUILD_TYPE MATCHES "Release") if(CMAKE_BUILD_TYPE MATCHES "Release")
# "Release" uses -O3 by default # "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) if (WARNINGS_ARE_ERRORS)
add_compile_options(-Werror) add_compile_options(-Werror)
endif() endif()
endif() add_compile_options(-pedantic)
add_compile_options(-Wall)
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU") add_compile_options(-Wextra)
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(-Wcast-qual) # Cast for removing type qualifiers add_compile_options(-Wcast-qual) # Cast for removing type qualifiers
add_compile_options(-Wno-deprecated-declarations) add_compile_options(-Wno-deprecated-declarations)
add_compile_options(-Wfloat-equal) # Floating values used in equality comparisons 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-declarations) # If a global function is defined without a previous declaration
add_compile_options(-Wmissing-format-attribute) # add_compile_options(-Wmissing-format-attribute) #
add_compile_options(-Wno-long-long) 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(-Wpacked) #
add_compile_options(-Wredundant-decls) # if anything is declared more than once in the same scope add_compile_options(-Wredundant-decls) # if anything is declared more than once in the same scope
add_compile_options(-Wundef) 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-missing-braces)
add_compile_options(-Wno-sign-compare) add_compile_options(-Wno-sign-compare)
add_compile_options(-Wno-multichar) 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 add_compile_options(-Wno-maybe-uninitialized) # there are some false positives
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wno-deprecated-declarations)
add_compile_options(-Wno-four-char-constants) add_compile_options(-Wno-four-char-constants)
add_compile_options(-Wno-missing-braces) 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) add_compile_options(-Wno-unused-function)
if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "8.0.0") add_compile_options_safe(-Wextra-semi-stmt)
add_compile_options(-Wextra-semi-stmt)
endif()
if(ENABLE_COVERAGE OR ENABLE_COVERAGE_XML) 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()
endif() endif()

View File

@ -610,7 +610,7 @@ void MainWindow::updateFunctionContractsTab()
{ {
QStringList addedContracts; QStringList addedContracts;
if (mProjectFile) { if (mProjectFile) {
for (const auto it: mProjectFile->getFunctionContracts()) { for (const auto& it: mProjectFile->getFunctionContracts()) {
addedContracts << QString::fromStdString(it.first); addedContracts << QString::fromStdString(it.first);
} }
} }
@ -876,7 +876,7 @@ Settings MainWindow::getCppcheckSettings()
result.functionContracts = mProjectFile->getFunctionContracts(); result.functionContracts = mProjectFile->getFunctionContracts();
for (const auto vc: mProjectFile->getVariableContracts()) for (const auto& vc: mProjectFile->getVariableContracts())
result.variableContracts[vc.first.toStdString()] = vc.second; result.variableContracts[vc.first.toStdString()] = vc.second;
const QStringList undefines = mProjectFile->getUndefines(); const QStringList undefines = mProjectFile->getUndefines();

View File

@ -958,7 +958,7 @@ bool ProjectFile::write(const QString &filename)
if (!mFunctionContracts.empty()) { if (!mFunctionContracts.empty()) {
xmlWriter.writeStartElement(CppcheckXml::FunctionContracts); xmlWriter.writeStartElement(CppcheckXml::FunctionContracts);
for (const auto contract: mFunctionContracts) { for (const auto& contract: mFunctionContracts) {
xmlWriter.writeStartElement(CppcheckXml::FunctionContract); xmlWriter.writeStartElement(CppcheckXml::FunctionContract);
xmlWriter.writeAttribute(CppcheckXml::ContractFunction, QString::fromStdString(contract.first)); xmlWriter.writeAttribute(CppcheckXml::ContractFunction, QString::fromStdString(contract.first));
xmlWriter.writeAttribute(CppcheckXml::ContractExpects, QString::fromStdString(contract.second)); 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); writeStringList(xmlWriter, mTags, CppcheckXml::TagsElementName, CppcheckXml::TagElementName);
if (!mWarningTags.empty()) { if (!mWarningTags.empty()) {
QStringList tags; QStringList tags;
for (const auto wt: mWarningTags) { for (const auto& wt: mWarningTags) {
if (!tags.contains(wt.second)) if (!tags.contains(wt.second))
tags.append(wt.second); tags.append(wt.second);
} }
@ -1033,7 +1033,7 @@ bool ProjectFile::write(const QString &filename)
xmlWriter.writeStartElement(CppcheckXml::TagWarningsElementName); xmlWriter.writeStartElement(CppcheckXml::TagWarningsElementName);
xmlWriter.writeAttribute(CppcheckXml::TagAttributeName, tag); xmlWriter.writeAttribute(CppcheckXml::TagAttributeName, tag);
QStringList warnings; QStringList warnings;
for (const auto wt: mWarningTags) { for (const auto& wt: mWarningTags) {
if (wt.second == tag) { if (wt.second == tag) {
xmlWriter.writeStartElement(CppcheckXml::WarningElementName); xmlWriter.writeStartElement(CppcheckXml::WarningElementName);
xmlWriter.writeAttribute(CppcheckXml::HashAttributeName, QString::number(wt.first)); xmlWriter.writeAttribute(CppcheckXml::HashAttributeName, QString::number(wt.first));

View File

@ -103,7 +103,7 @@ void ResultsView::setAddedFunctionContracts(const QStringList &addedContracts)
{ {
mUI.mListAddedContracts->clear(); mUI.mListAddedContracts->clear();
mUI.mListAddedContracts->addItems(addedContracts); mUI.mListAddedContracts->addItems(addedContracts);
for (const QString f: addedContracts) { for (const QString& f: addedContracts) {
auto res = mUI.mListMissingContracts->findItems(f, Qt::MatchExactly); auto res = mUI.mListMissingContracts->findItems(f, Qt::MatchExactly);
if (!res.empty()) if (!res.empty())
delete res.front(); delete res.front();
@ -114,7 +114,7 @@ void ResultsView::setAddedVariableContracts(const QStringList &added)
{ {
mUI.mListAddedVariables->clear(); mUI.mListAddedVariables->clear();
mUI.mListAddedVariables->addItems(added); mUI.mListAddedVariables->addItems(added);
for (const QString var: added) { for (const QString& var: added) {
for (auto item: mUI.mListMissingVariables->findItems(var, Qt::MatchExactly)) for (auto item: mUI.mListMissingVariables->findItems(var, Qt::MatchExactly))
delete item; delete item;
mVariableContracts.insert(var); mVariableContracts.insert(var);