Skip to content

Commit 7a8c67a

Browse files
authored
Fix #15007: isAttributeNoreturn not set for _Noreturn (#8823)
1 parent f9d4a64 commit 7a8c67a

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

lib/tokenize.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10020,6 +10020,15 @@ void Tokenizer::simplifyKeyword()
1002010020
}
1002110021
}
1002210022

10023+
if (isC() && (tok->str() == "_Noreturn" || tok->str() == "noreturn")) {
10024+
Token *nameTok = tok;
10025+
while (Token::Match(nameTok, "%name%|*"))
10026+
nameTok = nameTok->next();
10027+
if (nameTok && nameTok->str() == "(" && TokenList::isFunctionHead(nameTok, "{;")) {
10028+
nameTok->previous()->isAttributeNoreturn(true);
10029+
}
10030+
}
10031+
1002310032
if (isC() || mSettings.standards.cpp == Standards::CPP03) {
1002410033
if (tok->str() == "auto")
1002510034
tok->deleteThis();

test/testtokenize.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ class TestTokenizer : public TestFixture {
154154

155155
TEST_CASE(simplifyExternC);
156156
TEST_CASE(simplifyKeyword); // #5842 - remove C99 static keyword between []
157+
TEST_CASE(simplifyKeywordNoreturn1);
158+
TEST_CASE(simplifyKeywordNoreturn2);
157159

158160
TEST_CASE(isOneNumber);
159161

@@ -3094,6 +3096,32 @@ class TestTokenizer : public TestFixture {
30943096
ASSERT_EQUALS("class Fred { } ;", tokenizeAndStringify("class DLLEXPORT Fred final { };\n"));
30953097
}
30963098

3099+
void simplifyKeywordNoreturn1() {
3100+
const char code[] = "_Noreturn void f(void) {}\n";
3101+
const char expected[] = "_Noreturn void f ( ) { }";
3102+
SimpleTokenizer tokenizer(*this, false);
3103+
ASSERT(tokenizer.tokenize(code));
3104+
3105+
ASSERT_EQUALS(expected, tokenizer.tokens()->stringifyList(nullptr, false));
3106+
3107+
const Token *f = Token::findsimplematch(tokenizer.tokens(), "f");
3108+
ASSERT(f);
3109+
ASSERT(f->isAttributeNoreturn());
3110+
}
3111+
3112+
void simplifyKeywordNoreturn2() {
3113+
const char code[] = "noreturn void f(void) {}\n";
3114+
const char expected[] = "noreturn void f ( ) { }";
3115+
SimpleTokenizer tokenizer(*this, false);
3116+
ASSERT(tokenizer.tokenize(code));
3117+
3118+
ASSERT_EQUALS(expected, tokenizer.tokens()->stringifyList(nullptr, false));
3119+
3120+
const Token *f = Token::findsimplematch(tokenizer.tokens(), "f");
3121+
ASSERT(f);
3122+
ASSERT(f->isAttributeNoreturn());
3123+
}
3124+
30973125
void implicitIntConst() {
30983126
ASSERT_EQUALS("const int x ;", tokenizeAndStringify("const x;\n", dinit(TokenizeOptions, $.cpp = false)));
30993127
ASSERT_EQUALS("const int * x ;", tokenizeAndStringify("const *x;\n", dinit(TokenizeOptions, $.cpp = false)));

0 commit comments

Comments
 (0)