C++20 support: Support consteval and constinit (#4203)

Backported from LCppC.
This commit is contained in:
PKEuS 2022-06-11 15:28:20 +02:00 committed by GitHub
parent fc6c203b0e
commit cb382ac52c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View File

@ -10940,6 +10940,7 @@ void Tokenizer::simplifyKeyword()
const bool c99 = isC() && mSettings->standards.c >= Standards::C99; const bool c99 = isC() && mSettings->standards.c >= Standards::C99;
const bool cpp11 = isCPP() && mSettings->standards.cpp >= Standards::CPP11; const bool cpp11 = isCPP() && mSettings->standards.cpp >= Standards::CPP11;
const bool cpp20 = isCPP() && mSettings->standards.cpp >= Standards::CPP20;
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
if (keywords.find(tok->str()) != keywords.end()) { if (keywords.find(tok->str()) != keywords.end()) {
@ -10984,6 +10985,13 @@ void Tokenizer::simplifyKeyword()
} }
else if (cpp11) { else if (cpp11) {
if (cpp20 && tok->str() == "consteval") {
tok->originalName(tok->str());
tok->str("constexpr");
} else if (cpp20 && tok->str() == "constinit") {
tok->deleteThis();
}
// final: // final:
// 1) struct name final { }; <- struct is final // 1) struct name final { }; <- struct is final
if (Token::Match(tok->previous(), "struct|class|union %type% final [:{]")) { if (Token::Match(tok->previous(), "struct|class|union %type% final [:{]")) {

View File

@ -4614,6 +4614,8 @@ private:
ASSERT_EQUALS("int foo ( ) { }", tok("__inline int foo ( ) { }", true)); ASSERT_EQUALS("int foo ( ) { }", tok("__inline int foo ( ) { }", true));
ASSERT_EQUALS("int foo ( ) { }", tok("__forceinline int foo ( ) { }", true)); ASSERT_EQUALS("int foo ( ) { }", tok("__forceinline int foo ( ) { }", true));
ASSERT_EQUALS("constexpr int foo ( ) { }", tok("constexpr int foo() { }", true)); ASSERT_EQUALS("constexpr int foo ( ) { }", tok("constexpr int foo() { }", true));
ASSERT_EQUALS("constexpr int foo ( ) { }", tok("consteval int foo() { }", true));
ASSERT_EQUALS("int x ; x = 0 ;", tok("constinit int x = 0;", true));
ASSERT_EQUALS("void f ( ) { int final [ 10 ] ; }", tok("void f() { int final[10]; }", true)); ASSERT_EQUALS("void f ( ) { int final [ 10 ] ; }", tok("void f() { int final[10]; }", true));
ASSERT_EQUALS("int * p ;", tok("int * __restrict p;", "test.c")); ASSERT_EQUALS("int * p ;", tok("int * __restrict p;", "test.c"));
ASSERT_EQUALS("int * * p ;", tok("int * __restrict__ * p;", "test.c")); ASSERT_EQUALS("int * * p ;", tok("int * __restrict__ * p;", "test.c"));