diff --git a/Source/cmCMakePresetsGraphResolve.cxx b/Source/cmCMakePresetsGraphResolve.cxx index c024a25c80..473d2e9118 100644 --- a/Source/cmCMakePresetsGraphResolve.cxx +++ b/Source/cmCMakePresetsGraphResolve.cxx @@ -1,6 +1,5 @@ /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying file LICENSE.rst or https://cmake.org/licensing for details. */ -#include #include #include @@ -8,6 +7,7 @@ #include "cmCMakePresetsGraph.h" #include "cmStringAlgorithms.h" +#include "cmUnreachable.h" template cmCMakePresetsGraph::PresetResolveResult cmCMakePresetsGraph::ResolvePreset( @@ -94,7 +94,7 @@ cm::optional cmCMakePresetsGraph::FormatPresetError( case Status::Success: return cm::nullopt; } - assert(false && "Unreachable."); + CM_UNREACHABLE; return cm::nullopt; } diff --git a/Source/cmCryptoHash.cxx b/Source/cmCryptoHash.cxx index e01f2d685e..9ee52604c5 100644 --- a/Source/cmCryptoHash.cxx +++ b/Source/cmCryptoHash.cxx @@ -2,8 +2,6 @@ file LICENSE.rst or https://cmake.org/licensing for details. */ #include "cmCryptoHash.h" -#include - #include #include @@ -11,6 +9,8 @@ #include "cmsys/FStream.hxx" +#include "cmUnreachable.h" + static unsigned int const cmCryptoHashAlgoToId[] = { /* clang-format needs this comment to break after the opening brace */ RHASH_MD5, // @@ -109,7 +109,7 @@ std::string cmCryptoHash::GetHashAlgoName() const case RHASH_SHA3_512: return "SHA3_512"; } - assert(false); + CM_UNREACHABLE; return "UNKNOWN"; } diff --git a/Source/cmCxxModuleMapper.cxx b/Source/cmCxxModuleMapper.cxx index 343e0d61f2..2a263ec9cc 100644 --- a/Source/cmCxxModuleMapper.cxx +++ b/Source/cmCxxModuleMapper.cxx @@ -2,7 +2,6 @@ file LICENSE.rst or https://cmake.org/licensing for details. */ #include "cmCxxModuleMapper.h" -#include #include #include #include @@ -16,6 +15,7 @@ #include "cmScanDepFormat.h" #include "cmStringAlgorithms.h" #include "cmSystemTools.h" +#include "cmUnreachable.h" CxxBmiLocation::CxxBmiLocation() = default; @@ -216,7 +216,7 @@ std::string CxxModuleMapContentMsvc(CxxModuleLocations const& loc, case LookupMethod::IncludeQuote: return "-headerUnit:quote"_s; } - assert(false && "unsupported lookup method"); + CM_UNREACHABLE; return ""_s; }; @@ -265,7 +265,7 @@ bool CxxModuleUsage::AddReference(std::string const& logical, case LookupMethod::IncludeQuote: return "include-quote"_s; } - assert(false && "unsupported lookup method"); + CM_UNREACHABLE; return ""_s; }; @@ -432,7 +432,7 @@ std::string CxxModuleMapContent(CxxModuleMapFormat format, return CxxModuleMapContentMsvc(loc, obj, usages); } - assert(false); + CM_UNREACHABLE; return {}; } @@ -446,6 +446,6 @@ CxxModuleMapMode CxxModuleMapOpenMode(CxxModuleMapFormat format) return CxxModuleMapMode::Default; } - assert(false); + CM_UNREACHABLE; return CxxModuleMapMode::Default; } diff --git a/Source/cmFileAPI.cxx b/Source/cmFileAPI.cxx index df9131e696..709ceeead1 100644 --- a/Source/cmFileAPI.cxx +++ b/Source/cmFileAPI.cxx @@ -29,6 +29,7 @@ #include "cmStringAlgorithms.h" #include "cmSystemTools.h" #include "cmTimestamp.h" +#include "cmUnreachable.h" #include "cmake.h" #if defined(__clang__) && defined(__has_warning) @@ -835,7 +836,7 @@ Json::Value cmFileAPI::BuildCodeModel(Object object) if (object.Version == 2) { version = BuildVersion(2, CodeModelV2Minor); } else { - return codemodel; // should be unreachable + CM_UNREACHABLE; } return codemodel; @@ -870,7 +871,7 @@ Json::Value cmFileAPI::BuildConfigureLog(Object object) if (object.Version == 1) { version = BuildVersion(1, ConfigureLogV1Minor); } else { - return configureLog; // should be unreachable + CM_UNREACHABLE; } return configureLog; @@ -904,7 +905,7 @@ Json::Value cmFileAPI::BuildCache(Object object) if (object.Version == 2) { version = BuildVersion(2, CacheV2Minor); } else { - return cache; // should be unreachable + CM_UNREACHABLE; } return cache; @@ -938,7 +939,7 @@ Json::Value cmFileAPI::BuildCMakeFiles(Object object) if (object.Version == 1) { version = BuildVersion(1, CMakeFilesV1Minor); } else { - return cmakeFiles; // should be unreachable + CM_UNREACHABLE; } return cmakeFiles; @@ -972,7 +973,7 @@ Json::Value cmFileAPI::BuildToolchains(Object object) if (object.Version == 1) { version = BuildVersion(1, ToolchainsV1Minor); } else { - return toolchains; // should be unreachable + CM_UNREACHABLE; } return toolchains; diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index 25934a2f93..2f7e489d8e 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -43,6 +43,7 @@ #include "cmStringAlgorithms.h" #include "cmSystemTools.h" #include "cmTargetTypes.h" +#include "cmUnreachable.h" #include "cmValue.h" #include "cmVersionMacros.h" #include "cmWindowsRegistry.h" @@ -3668,7 +3669,7 @@ void cmFindPackageDebugState::WriteEvent(cmConfigureLog& log, case pdt::Cps: return "cps"; } - assert(false); + CM_UNREACHABLE; return ""; }; @@ -3712,7 +3713,7 @@ void cmFindPackageDebugState::WriteEvent(cmConfigureLog& log, case cmFindPackageCommand::RequiredStatus::RequiredFromFindVar: return "required_from_find_variable"; } - assert(false); + CM_UNREACHABLE; return ""; }; log.BeginObject("settings"_s); @@ -3749,7 +3750,7 @@ void cmFindPackageDebugState::WriteEvent(cmConfigureLog& log, case cmFindCommon::RootPathModeBoth: return "BOTH"; } - assert(false); + CM_UNREACHABLE; return ""; }; log.BeginObject("paths"_s); @@ -3789,7 +3790,7 @@ void cmFindPackageDebugState::WriteEvent(cmConfigureLog& log, case cmFindPackageCommand::FoundPackageMode::Provider: return "provider"; } - assert(false); + CM_UNREACHABLE; return ""; }; if (!fpc->ConsideredPaths.empty()) { @@ -3813,7 +3814,7 @@ void cmFindPackageDebugState::WriteEvent(cmConfigureLog& log, case cmFindPackageCommand::SearchResult::NotFound: return "not_found"; } - assert(false); + CM_UNREACHABLE; return ""; }; diff --git a/Source/cmGeneratorExpression.cxx b/Source/cmGeneratorExpression.cxx index 7c8da0413b..90081591ae 100644 --- a/Source/cmGeneratorExpression.cxx +++ b/Source/cmGeneratorExpression.cxx @@ -3,7 +3,6 @@ #include "cmGeneratorExpression.h" #include -#include #include #include #include @@ -26,6 +25,7 @@ #include "cmMessageType.h" #include "cmStringAlgorithms.h" #include "cmSystemTools.h" +#include "cmUnreachable.h" #include "cmake.h" cmGeneratorExpression::cmGeneratorExpression(cmake& cmakeInstance, @@ -283,7 +283,7 @@ static std::string stripExportInterface( foundGenex = FoundGenex::BuildLocalInterface; pos += cmStrLen("$ #include "cmGeneratorExpressionEvaluator.h" +#include "cmUnreachable.h" cmGeneratorExpressionParser::cmGeneratorExpressionParser( std::vector tokens) @@ -234,11 +235,11 @@ void cmGeneratorExpressionParser::ParseContent( if (this->NestingLevel == 0) { extendText(result, this->it); } else { - assert(false && "Got unexpected syntax token."); + CM_UNREACHABLE; } assert(this->it != this->Tokens.end()); ++this->it; return; } - assert(false && "Unhandled token in generator expression."); + CM_UNREACHABLE; } diff --git a/Source/cmGeneratorTarget_CompatibleInterface.cxx b/Source/cmGeneratorTarget_CompatibleInterface.cxx index bed83143d1..602126d6f4 100644 --- a/Source/cmGeneratorTarget_CompatibleInterface.cxx +++ b/Source/cmGeneratorTarget_CompatibleInterface.cxx @@ -30,6 +30,7 @@ #include "cmStringAlgorithms.h" #include "cmSystemTools.h" #include "cmTargetTypes.h" +#include "cmUnreachable.h" #include "cmValue.h" namespace { @@ -136,8 +137,7 @@ char const* getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt, { switch (t) { case BoolType: - assert(false && - "String compatibility check function called for boolean"); + CM_UNREACHABLE; return nullptr; case StringType: return tgt->GetLinkInterfaceDependentStringProperty(prop, config); @@ -146,7 +146,7 @@ char const* getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt, case NumberMaxType: return tgt->GetLinkInterfaceDependentNumberMaxProperty(prop, config); } - assert(false && "Unreachable!"); + CM_UNREACHABLE; return nullptr; } @@ -367,7 +367,7 @@ static std::string compatibilityType(CompatibleType t) case NumberMinType: return "Numeric minimum compatibility"; } - assert(false && "Unreachable!"); + CM_UNREACHABLE; return ""; } @@ -381,7 +381,7 @@ static std::string compatibilityAgree(CompatibleType t, bool dominant) case NumberMinType: return dominant ? "(Dominant)\n" : "(Ignored)\n"; } - assert(false && "Unreachable!"); + CM_UNREACHABLE; return ""; } @@ -524,7 +524,7 @@ std::pair consistentProperty(char const* lhs, case NumberMaxType: return consistentNumberProperty(lhs, rhs, t); } - assert(false && "Unreachable!"); + CM_UNREACHABLE; return { false, nullptr }; } @@ -558,7 +558,7 @@ static std::pair consistentProperty(std::string const& lhs, value.first ? std::string(value.second) : null_ptr }; } } - assert(false && "Unreachable!"); + CM_UNREACHABLE; return { false, null_ptr }; } diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx index 4110460f96..31ac2d715f 100644 --- a/Source/cmGlobalGenerator.cxx +++ b/Source/cmGlobalGenerator.cxx @@ -66,6 +66,7 @@ #include "cmStringAlgorithms.h" #include "cmSystemTools.h" #include "cmTargetExport.h" +#include "cmUnreachable.h" #include "cmValue.h" #include "cmVersion.h" #include "cmWorkingDirectory.h" @@ -2423,7 +2424,7 @@ bool cmGlobalGenerator::UseShortObjectNames( strategy = this->QtAutogenIntDirStrategy; break; default: - assert(false); + CM_UNREACHABLE; break; } return this->SupportsShortObjectNames() && diff --git a/Source/cmGlobalNinjaGenerator.cxx b/Source/cmGlobalNinjaGenerator.cxx index 1d45ba2788..52a7158f4b 100644 --- a/Source/cmGlobalNinjaGenerator.cxx +++ b/Source/cmGlobalNinjaGenerator.cxx @@ -3,7 +3,6 @@ #include "cmGlobalNinjaGenerator.h" #include -#include #include #include #include @@ -57,6 +56,7 @@ #include "cmTargetTypes.h" #include "cmTest.h" #include "cmTestGenerator.h" +#include "cmUnreachable.h" #include "cmValue.h" #include "cmVersion.h" #include "cmake.h" @@ -3021,7 +3021,7 @@ bool cmGlobalNinjaGenerator::WriteDyndepFile( case LookupMethod::IncludeQuote: return "include-quote"_s; } - assert(false && "unsupported lookup method"); + CM_UNREACHABLE; return ""_s; }; diff --git a/Source/cmInstallCommand.cxx b/Source/cmInstallCommand.cxx index 568386e0d2..978772b022 100644 --- a/Source/cmInstallCommand.cxx +++ b/Source/cmInstallCommand.cxx @@ -3,7 +3,6 @@ #include "cmInstallCommand.h" #include -#include #include #include #include @@ -58,6 +57,7 @@ #include "cmTarget.h" #include "cmTargetExport.h" #include "cmTargetTypes.h" +#include "cmUnreachable.h" #include "cmValue.h" namespace { @@ -1490,7 +1490,7 @@ bool HandleImportedRuntimeArtifactsMode(std::vector const& args, } break; default: - assert(false && "This should never happen"); + CM_UNREACHABLE; break; } diff --git a/Source/cmInstallImportedRuntimeArtifactsGenerator.cxx b/Source/cmInstallImportedRuntimeArtifactsGenerator.cxx index afa5eeface..a70fdc3615 100644 --- a/Source/cmInstallImportedRuntimeArtifactsGenerator.cxx +++ b/Source/cmInstallImportedRuntimeArtifactsGenerator.cxx @@ -2,7 +2,6 @@ file LICENSE.rst or https://cmake.org/licensing for details. */ #include "cmInstallImportedRuntimeArtifactsGenerator.h" -#include #include #include #include @@ -16,6 +15,7 @@ #include "cmLocalGenerator.h" #include "cmStringAlgorithms.h" #include "cmTargetTypes.h" +#include "cmUnreachable.h" namespace { cmsys::RegularExpression const FrameworkRegularExpression( @@ -143,7 +143,7 @@ void cmInstallImportedRuntimeArtifactsGenerator::GenerateScriptForConfig( } break; default: - assert(false && "This should never happen"); + CM_UNREACHABLE; break; } } diff --git a/Source/cmInstallTargetGenerator.cxx b/Source/cmInstallTargetGenerator.cxx index 93d1483e2a..8fa751a018 100644 --- a/Source/cmInstallTargetGenerator.cxx +++ b/Source/cmInstallTargetGenerator.cxx @@ -33,6 +33,7 @@ #include "cmSystemTools.h" #include "cmTarget.h" #include "cmTargetTypes.h" +#include "cmUnreachable.h" #include "cmValue.h" #include "cmake.h" @@ -246,8 +247,7 @@ cmInstallTargetGenerator::Files cmInstallTargetGenerator::GetFiles( case cm::TargetType::INTERFACE_LIBRARY: // Not reachable. We never create a cmInstallTargetGenerator for // an INTERFACE_LIBRARY. - assert(false && - "INTERFACE_LIBRARY targets have no installable outputs."); + CM_UNREACHABLE; break; case cm::TargetType::OBJECT_LIBRARY: { diff --git a/Source/cmNinjaNormalTargetGenerator.cxx b/Source/cmNinjaNormalTargetGenerator.cxx index a2bce861e3..53f8902e30 100644 --- a/Source/cmNinjaNormalTargetGenerator.cxx +++ b/Source/cmNinjaNormalTargetGenerator.cxx @@ -3,7 +3,6 @@ #include "cmNinjaNormalTargetGenerator.h" #include -#include #include #include #include @@ -44,6 +43,7 @@ #include "cmSystemTools.h" #include "cmTarget.h" #include "cmTargetTypes.h" +#include "cmUnreachable.h" #include "cmValue.h" cmNinjaNormalTargetGenerator::cmNinjaNormalTargetGenerator( @@ -831,7 +831,7 @@ std::vector cmNinjaNormalTargetGenerator::ComputeLinkCmd( case cm::TargetType::EXECUTABLE: break; default: - assert(false && "Unexpected target type"); + CM_UNREACHABLE; } return std::move(linkCmds.data()); } diff --git a/Source/cmState.cxx b/Source/cmState.cxx index 8844f021e0..d5e1234590 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -27,6 +27,7 @@ #include "cmStringAlgorithms.h" #include "cmSystemTools.h" #include "cmTargetTypes.h" +#include "cmUnreachable.h" #include "cmake.h" namespace cmStateDetail { @@ -78,7 +79,7 @@ std::string const& cmState::GetTargetTypeName(cm::TargetType targetType) case cm::TargetType::UNKNOWN_LIBRARY: return propUNKNOWN_LIBRARY; } - assert(false && "Unexpected target type"); + CM_UNREACHABLE; return propEmpty; } diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index 54f1c4953a..d1d81fd39f 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -44,6 +44,7 @@ #include "cmStringAlgorithms.h" #include "cmSystemTools.h" #include "cmTargetPropertyComputer.h" +#include "cmUnreachable.h" #include "cmValue.h" #include "cmXcFramework.h" #include "cmake.h" @@ -2972,7 +2973,7 @@ bool cmTarget::IsNormal() const case Visibility::Foreign: return false; } - assert(false && "unknown visibility (IsNormal)"); + CM_UNREACHABLE; return false; } @@ -2987,7 +2988,7 @@ bool cmTarget::IsSynthetic() const case Visibility::Foreign: return false; } - assert(false && "unknown visibility (IsSynthetic)"); + CM_UNREACHABLE; return false; } @@ -3002,7 +3003,7 @@ bool cmTargetInternals::IsImported() const case cmTarget::Visibility::Generated: return false; } - assert(false && "unknown visibility (IsImported)"); + CM_UNREACHABLE; return false; } @@ -3022,7 +3023,7 @@ bool cmTarget::IsImportedGloballyVisible() const case Visibility::Foreign: return false; } - assert(false && "unknown visibility (IsImportedGloballyVisible)"); + CM_UNREACHABLE; return false; } @@ -3037,7 +3038,7 @@ bool cmTarget::IsForeign() const case Visibility::ImportedGlobally: return false; } - assert(false && "unknown visibility (isForeign)"); + CM_UNREACHABLE; return false; } diff --git a/Source/cmUnreachable.h b/Source/cmUnreachable.h new file mode 100644 index 0000000000..ef331d6b50 --- /dev/null +++ b/Source/cmUnreachable.h @@ -0,0 +1,22 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file LICENSE.rst or https://cmake.org/licensing for details. */ +#pragma once + +#if defined(NDEBUG) +# include +# define CM_UNREACHABLE \ + do { \ + cm::unreachable(); \ + } while (false) +#else +# include +# include +# include +# define CM_UNREACHABLE \ + do { \ + std::cerr << "unreachable code path at " << __FILE__ << ':' << __LINE__ \ + << " in " << __func__ << '\n'; \ + assert(false && "unreachable code path"); \ + std::abort(); \ + } while (false) +#endif diff --git a/Source/cmVisualStudioSlnParser.cxx b/Source/cmVisualStudioSlnParser.cxx index 986ac0aa37..6051958054 100644 --- a/Source/cmVisualStudioSlnParser.cxx +++ b/Source/cmVisualStudioSlnParser.cxx @@ -14,6 +14,7 @@ #include "cmStringAlgorithms.h" #include "cmSystemTools.h" +#include "cmUnreachable.h" #include "cmVisualStudioSlnData.h" namespace { @@ -182,7 +183,7 @@ LineFormat cmVisualStudioSlnParser::State::NextLineFormat() const case FileStateIgnore: return LineVerbatim; default: - assert(false); + CM_UNREACHABLE; return LineVerbatim; } } diff --git a/Source/cmXCodeObject.cxx b/Source/cmXCodeObject.cxx index 6968f515b5..b9f472697f 100644 --- a/Source/cmXCodeObject.cxx +++ b/Source/cmXCodeObject.cxx @@ -7,6 +7,8 @@ #include #include +#include "cmUnreachable.h" + char const* cmXCodeObject::PBXTypeNames[] = { /* clang-format needs this comment to break after the opening brace */ "PBXGroup", @@ -68,7 +70,8 @@ bool cmXCodeObject::IsEmpty() const case OBJECT: return this->Object == nullptr; } - return true; // unreachable, but quiets warnings + CM_UNREACHABLE; + return true; } void cmXCodeObject::Indent(int level, std::ostream& out)