Skip to content

Commit 9304746

Browse files
Fix #13123 False unreadVariable for variable declared in if-condition… (danmar#7035)
… and with the same name with the target of a desigated initializer
1 parent 64db6ed commit 9304746

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/tokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4915,7 +4915,7 @@ void Tokenizer::setVarIdPass1()
49154915
}
49164916

49174917
if (tok->varId() == 0 && (!scopeStack.top().isEnum || !(Token::Match(tok->previous(), "{|,") && Token::Match(tok->next(), ",|=|}"))) &&
4918-
!Token::simpleMatch(tok->next(), ": ;")) {
4918+
!Token::simpleMatch(tok->next(), ": ;") && !(tok->tokAt(-1) && Token::Match(tok->tokAt(-2), "{|, ."))) {
49194919
const std::unordered_map<std::string, nonneg int>::const_iterator it = variableMap.map(globalNamespace).find(tok->str());
49204920
if (it != variableMap.map(globalNamespace).end()) {
49214921
tok->varId(it->second);

test/testvarid.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3134,6 +3134,21 @@ class TestVarID : public TestFixture {
31343134
"void f(int a) {\n"
31353135
" struct T t = { .a = 1 };\n"
31363136
"}\n"));
3137+
3138+
ASSERT_EQUALS("1: struct S { int x@1 ; } ;\n" // TODO: set some varid for designated initializer?
3139+
"2: void f0 ( S s@2 ) ;\n"
3140+
"3: void f1 ( int n@3 ) {\n"
3141+
"4: if ( int x@4 = n@3 ) {\n"
3142+
"5: f0 ( S { . x = x@4 } ) ;\n"
3143+
"6: }\n"
3144+
"7: }\n",
3145+
tokenize("struct S { int x; };\n"
3146+
"void f0(S s);\n"
3147+
"void f1(int n) {\n"
3148+
" if (int x = n) {\n"
3149+
" f0(S{ .x = x });\n"
3150+
" }\n"
3151+
"}\n"));
31373152
}
31383153

31393154
void varid_arrayinit() {

0 commit comments

Comments
 (0)