Skip to content

Fix #13769 (TokenList: throw internal error when assertion fails) #7459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@

#include <simplecpp.h>

//#define N_ASSERT_LANG

#ifndef N_ASSERT_LANG
#include <cassert>
#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
Expand Down Expand Up @@ -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);
}

Expand Down
7 changes: 7 additions & 0 deletions test/testtokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class TestTokenList : public TestFixture {
TEST_CASE(isKeyword);
TEST_CASE(notokens);
TEST_CASE(ast1);
TEST_CASE(setLangInternalError);
}

// inspired by #5895
Expand Down Expand Up @@ -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)
Loading