Skip to content

Commit ab441f3

Browse files
committed
Tokenizer:vardecl: split up reference variables declared in class better
1 parent a434e0f commit ab441f3

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

lib/tokenize.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5393,12 +5393,15 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_
53935393
if (tok2->strAt(1) == "*")
53945394
break;
53955395

5396+
if (Token::Match(tok2->next(), "& %name% ,"))
5397+
break;
5398+
53965399
tok2 = tok2->next();
53975400
++typelen;
53985401
}
53995402

54005403
// strange looking variable declaration => don't split up.
5401-
if (Token::Match(tok2, "%type% *| %name% , %type% *| %name%"))
5404+
if (Token::Match(tok2, "%type% *|&| %name% , %type% *|&| %name%"))
54025405
continue;
54035406

54045407
if (Token::Match(tok2, "struct|union|class %type%")) {
@@ -5470,9 +5473,9 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_
54705473
else
54715474
--typelen;
54725475
//skip all the pointer part
5473-
bool ispointer = false;
5474-
while (varName && varName->str() == "*") {
5475-
ispointer = true;
5476+
bool isPointerOrRef = false;
5477+
while (Token::simpleMatch(varName, "*") || Token::Match(varName, "& %name% ,")) {
5478+
isPointerOrRef = true;
54765479
varName = varName->next();
54775480
}
54785481

@@ -5487,7 +5490,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_
54875490
if (varName->str() != "operator") {
54885491
tok2 = varName->next(); // The ',' or '=' token
54895492

5490-
if (tok2->str() == "=" && (isstatic || (isconst && !ispointer))) {
5493+
if (tok2->str() == "=" && (isstatic || (isconst && !isPointerOrRef))) {
54915494
//do not split const non-pointer variables..
54925495
while (tok2 && tok2->str() != "," && tok2->str() != ";") {
54935496
if (Token::Match(tok2, "{|(|["))

test/testtokenize.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ class TestTokenizer : public TestFixture {
288288
TEST_CASE(vardecl_par); // #2743 - set links if variable type contains parentheses
289289
TEST_CASE(vardecl_par2); // #3912 - set correct links
290290
TEST_CASE(vardecl_par3); // #6556 - Fred x1(a), x2(b);
291+
TEST_CASE(vardecl_class_ref);
291292
TEST_CASE(volatile_variables);
292293

293294
// unsigned i; => unsigned int i;
@@ -3630,6 +3631,11 @@ class TestTokenizer : public TestFixture {
36303631
ASSERT_EQUALS("Fred x1 ( a ) ; Fred x2 ( b ) ;", tokenizeAndStringify(code));
36313632
}
36323633

3634+
void vardecl_class_ref() {
3635+
const char code[] = "class A { B &b1,&b2; };";
3636+
ASSERT_EQUALS("class A { B & b1 ; B & b2 ; } ;", tokenizeAndStringify(code));
3637+
}
3638+
36333639
void vardec_static() {
36343640
{
36353641
// don't simplify declarations of static variables

0 commit comments

Comments
 (0)