Skip to content

Commit

Permalink
Fix #12667 syntaxError on C23 [[noreturn]] (#6351)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Apr 27, 2024
1 parent 4dde49f commit 96f2527
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
20 changes: 10 additions & 10 deletions lib/keywords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@ static const std::unordered_set<std::string> c11_keywords = {
C11_KEYWORDS
};

/*
static const std::unordered_set<std::string> c23_keywords_all = {
static const std::unordered_set<std::string> c23_keywords_all = {
C90_KEYWORDS, C99_KEYWORDS, C11_KEYWORDS, C23_KEYWORDS
};
};

static const std::unordered_set<std::string> c23_keywords = {
static const std::unordered_set<std::string> c23_keywords = {
C23_KEYWORDS
};
*/
};

// see https://en.cppreference.com/w/cpp/keyword

Expand Down Expand Up @@ -160,9 +158,10 @@ const std::unordered_set<std::string>& Keywords::getAll(Standards::cstd_t cStd)
case Standards::cstd_t::C99:
return c99_keywords_all;
case Standards::cstd_t::C11:
case Standards::cstd_t::C17:
return c11_keywords_all;
/*case Standards::cstd_t::C23:
return c23_keywords_all;*/
case Standards::cstd_t::C23:
return c23_keywords_all;
}
cppcheck::unreachable();
}
Expand Down Expand Up @@ -197,9 +196,10 @@ const std::unordered_set<std::string>& Keywords::getOnly(Standards::cstd_t cStd)
case Standards::cstd_t::C99:
return c99_keywords;
case Standards::cstd_t::C11:
case Standards::cstd_t::C17:
return c11_keywords;
/*case Standards::cstd_t::C23:
return c23_keywords_all;*/
case Standards::cstd_t::C23:
return c23_keywords_all;
}
cppcheck::unreachable();
}
Expand Down
12 changes: 11 additions & 1 deletion lib/standards.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct Standards {
enum Language { None, C, CPP };

/** C code standard */
enum cstd_t { C89, C99, C11, CLatest = C11 } c = CLatest;
enum cstd_t { C89, C99, C11, C17, C23, CLatest = C23 } c = CLatest;

/** C++ code standard */
enum cppstd_t { CPP03, CPP11, CPP14, CPP17, CPP20, CPP23, CPPLatest = CPP23 } cpp = CPPLatest;
Expand Down Expand Up @@ -69,6 +69,10 @@ struct Standards {
return "c99";
case C11:
return "c11";
case C17:
return "c17";
case C23:
return "c23";
}
return "";
}
Expand All @@ -82,6 +86,12 @@ struct Standards {
if (std == "c11") {
return Standards::C11;
}
if (std == "c17") {
return Standards::C17;
}
if (std == "c23") {
return Standards::C23;
}
return Standards::CLatest;
}
bool setCPP(std::string str) {
Expand Down
2 changes: 1 addition & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9234,7 +9234,7 @@ void Tokenizer::simplifyCppcheckAttribute()

void Tokenizer::simplifyCPPAttribute()
{
if (!isCPP())
if ((isCPP() && mSettings.standards.cpp < Standards::CPP11) || (isC() && mSettings.standards.c < Standards::C23))
return;

for (Token *tok = list.front(); tok;) {
Expand Down
2 changes: 1 addition & 1 deletion lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ class CPPCHECKLIB Tokenizer {
void simplifyOverloadedOperators();

/**
* Remove [[attribute]] (C++11 and later) from TokenList
* Remove [[attribute]] (C++11, C23) from TokenList
*/
void simplifyCPPAttribute();

Expand Down
2 changes: 1 addition & 1 deletion test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,7 @@ class TestSymbolDatabase : public TestFixture {
ASSERT(db);
ASSERT_EQUALS(2, db->scopeList.front().varlist.size());
const Variable *x1 = Token::findsimplematch(tokenizer.tokens(), "x")->variable();
ASSERT(x1 && Token::simpleMatch(x1->typeStartToken(), "alignas ( 16 ) int x ;"));
ASSERT(x1 && Token::simpleMatch(x1->typeStartToken(), "int x ;"));
}

void memberVar1() {
Expand Down
8 changes: 6 additions & 2 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,9 @@ class TestTokenizer : public TestFixture {
}

#define tokenizeAndStringify(...) tokenizeAndStringify_(__FILE__, __LINE__, __VA_ARGS__)
std::string tokenizeAndStringify_(const char* file, int linenr, const char code[], bool expand = true, Platform::Type platform = Platform::Type::Native, bool cpp = true, Standards::cppstd_t std = Standards::CPP11) {
const Settings settings = settingsBuilder(settings1).debugwarnings().cpp(std).platform(platform).build();
std::string tokenizeAndStringify_(const char* file, int linenr, const char code[], bool expand = true, Platform::Type platform = Platform::Type::Native,
bool cpp = true, Standards::cppstd_t cppstd = Standards::CPP11, Standards::cstd_t cstd = Standards::C11) {
const Settings settings = settingsBuilder(settings1).debugwarnings().cpp(cppstd).c(cstd).platform(platform).build();

// tokenize..
SimpleTokenizer tokenizer(settings, *this);
Expand Down Expand Up @@ -5879,6 +5880,9 @@ class TestTokenizer : public TestFixture {

ASSERT_EQUALS("int func1 ( ) ;",
tokenizeAndStringify("[[clang::optnone]] [[nodiscard]] int func1();"));

ASSERT_EQUALS("void f ( int i ) { exit ( i ) ; }",
tokenizeAndStringify("[[noreturn]] void f(int i) { exit(i); }", /*expand*/ true, Platform::Type::Native, /*cpp*/ false, Standards::CPP11, Standards::C23));
}

void simplifyCaseRange() {
Expand Down

0 comments on commit 96f2527

Please sign in to comment.