Skip to content

Commit

Permalink
Fix #12235 performance regression (hang) in 2.13dev (#5715)
Browse files Browse the repository at this point in the history
Co-authored-by: chrchr-github <chrchr@github>
  • Loading branch information
chrchr-github and chrchr-github committed Dec 15, 2023
1 parent 61bbcbe commit 02fed7a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2767,8 +2767,10 @@ static bool isExpressionChangedAt(const F& getExprTok,
{
if (depth < 0)
return true;
if (tok->isLiteral() || tok->isKeyword() || tok->isStandardType() || Token::Match(tok, ",|;|:"))
return false;
if (tok->exprId() != exprid) {
if (globalvar && !tok->isKeyword() && Token::Match(tok, "%name% (") && !(tok->function() && tok->function()->isAttributePure()))
if (globalvar && Token::Match(tok, "%name% (") && !(tok->function() && tok->function()->isAttributePure()))
// TODO: Is global variable really changed by function call?
return true;
int i = 1;
Expand Down
40 changes: 39 additions & 1 deletion test/cli/test-performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,42 @@ def test_slow_exprid(tmpdir):
cppcheck([filename], env=my_env)



@pytest.mark.timeout(10)
def test_slow_initlist_varchanged(tmpdir):
# #12235
filename = os.path.join(tmpdir, 'hang.cpp')
with open(filename, 'wt') as f:
f.write(r"""
struct T {
int* q;
int nx, ny;
};
struct S {
void f();
int n;
T* p;
};
#define ROW 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,
#define ROW4 ROW ROW ROW ROW
#define ROW16 ROW4 ROW4 ROW4 ROW4
#define ROW64 ROW16 ROW16 ROW16 ROW16
#define ROW256 ROW64 ROW64 ROW64 ROW64
#define ROW1K ROW256 ROW256 ROW256 ROW256
#define ROW4K ROW1K ROW1K ROW1K ROW1K
const int A[] = {
ROW4K
};
void S::f() {
for (int i = 0; i < n; ++i) {
T& t = p[i];
for (int y = 0; y < t.ny; y += 4) {
int* row0 = t.q + y * t.nx;
for (int x = 0; x < t.nx; x += 4) {
int s[16] = {};
memcpy(row0, &s[0], 4);
row0 += 4;
}
}
}
}""")
cppcheck([filename]) # should not take more than ~1 second
2 changes: 2 additions & 0 deletions test/testastutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ class TestAstUtils : public TestFixture {
"void f(int x) { g(&x); }\n",
"{",
"}"));

ASSERT_EQUALS(false, isVariableChanged("const int A[] = { 1, 2, 3 };", "[", "]"));
}

#define isVariableChangedByFunctionCall(code, pattern, inconclusive) isVariableChangedByFunctionCall_(code, pattern, inconclusive, __FILE__, __LINE__)
Expand Down

0 comments on commit 02fed7a

Please sign in to comment.