diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 399d0e5c4d7..15cad7fadce 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -45,14 +45,7 @@ #include -//#define N_ASSERT_LANG - -#ifndef N_ASSERT_LANG -#include -#define ASSERT_LANG(x) assert(x) -#else -#define ASSERT_LANG(x) -#endif +#define ASSERT_LANG(x) do { if (!(x)) throw InternalError(nullptr, __FILE__ ":" + std::to_string(__LINE__) + ": Assertion " #x " failed"); } while (0) // How many compileExpression recursions are allowed? // For practical code this could be endless. But in some special torture test @@ -2302,6 +2295,7 @@ void TokenList::setLang(Standards::Language lang, bool force) ASSERT_LANG(lang != Standards::Language::None); if (!force) { + // cppcheck-suppress premium-misra-cpp-2023-19.3.3; this is a false positive, will be fixed ASSERT_LANG(mLang == Standards::Language::None); } diff --git a/test/testtokenlist.cpp b/test/testtokenlist.cpp index f98a0ea2dd2..6bda24d8afe 100644 --- a/test/testtokenlist.cpp +++ b/test/testtokenlist.cpp @@ -49,6 +49,7 @@ class TestTokenList : public TestFixture { TEST_CASE(isKeyword); TEST_CASE(notokens); TEST_CASE(ast1); + TEST_CASE(setLangInternalError); } // inspired by #5895 @@ -191,6 +192,12 @@ class TestTokenList : public TestFixture { } tokenlist.createAst(); // do not crash } + + void setLangInternalError() const { + TokenList tokenList(nullptr); + tokenList.setLang(Standards::Language::C, true); + ASSERT_THROW_INTERNAL(tokenList.setLang(Standards::Language::CPP, false), InternalError::INTERNAL); + } }; REGISTER_TEST(TestTokenList)