Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class CPPCHECKLIB Token {
Token* mAstOperand1{};
Token* mAstOperand2{};
Token* mAstParent{};
Token* mAstTop{};

// symbol database information
const Scope* mScope{};
Expand Down Expand Up @@ -1557,6 +1558,9 @@ class CPPCHECKLIB Token {
* @throws InternalError thrown on cyclic dependency
*/
void astParent(Token* tok);
void astTop(Token * tok) {
mImpl->mAstTop = tok;
}

Token * astOperand1() {
return mImpl->mAstOperand1;
Expand Down Expand Up @@ -1597,13 +1601,19 @@ class CPPCHECKLIB Token {

}
RET_NONNULL Token *astTop() {
if (mImpl->mAstTop) {
return mImpl->mAstTop;
}
Token *ret = this;
while (ret->mImpl->mAstParent)
ret = ret->mImpl->mAstParent;
return ret;
}

RET_NONNULL const Token *astTop() const {
if (mImpl->mAstTop) {
return mImpl->mAstTop;
}
const Token *ret = this;
while (ret->mImpl->mAstParent)
ret = ret->mImpl->mAstParent;
Expand Down
10 changes: 10 additions & 0 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1860,6 +1860,16 @@ void TokenList::createAst() const
throw InternalError(tok, "Syntax Error: Infinite loop when creating AST.", InternalError::AST);
tok = nextTok;
}
for (Token *tok = mTokensFrontBack->front; tok; tok = tok ? tok->next() : nullptr) {
if (tok->astParent())
continue;
if (!tok->astOperand1() && !tok->astOperand2())
continue;
visitAstNodes(tok, [&](Token* child) {
child->astTop(tok);
return ChildrenToVisit::op1_and_op2;
});
}
}

namespace {
Expand Down
23 changes: 23 additions & 0 deletions test/cli/performance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def test_slow_many_scopes(tmpdir):
}""")
cppcheck([filename]) # should not take more than ~1 second


@pytest.mark.skipif(sys.platform == 'darwin', reason='GitHub macOS runners are too slow')
@pytest.mark.timeout(20)
def test_crash_array_in_namespace(tmpdir):
Expand All @@ -241,6 +242,28 @@ def test_crash_array_in_namespace(tmpdir):
}""")
cppcheck([filename]) # should not take more than ~5 seconds


@pytest.mark.skipif(sys.platform == 'darwin', reason='GitHub macOS runners are too slow')
@pytest.mark.timeout(20)
def test_crash_array_in_array(tmpdir):
# 12861
filename = os.path.join(tmpdir, 'hang.cpp')
with open(filename, 'wt') as f:
f.write(r"""
#define ROW A, A, A, A, A, A, A, A,
#define ROW8 ROW ROW ROW ROW ROW ROW ROW ROW
#define ROW64 ROW8 ROW8 ROW8 ROW8 ROW8 ROW8 ROW8 ROW8
#define ROW512 ROW64 ROW64 ROW64 ROW64 ROW64 ROW64 ROW64 ROW64
#define ROW4096 ROW512 ROW512 ROW512 ROW512 ROW512 ROW512 ROW512 ROW512
void f() {
static const char A = 'a';
const char a[] = {
ROW4096 ROW4096 ROW4096 ROW4096
};
}""")
cppcheck([filename])


@pytest.mark.timeout(5)
def test_slow_bifurcate(tmpdir):
# #14134
Expand Down
Loading