Skip to content

Commit 2300a77

Browse files
authored
Fix 10336: AST cyclic dependency on valid C++ code (cppcheck-opensource#3331)
1 parent 7e2ba80 commit 2300a77

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

lib/tokenlist.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//---------------------------------------------------------------------------
2020
#include "tokenlist.h"
2121

22+
#include "astutils.h"
2223
#include "errorlogger.h"
2324
#include "library.h"
2425
#include "path.h"
@@ -1351,7 +1352,7 @@ static Token * createAstAtToken(Token *tok, bool cpp);
13511352
// Compile inner expressions inside inner ({..}) and lambda bodies
13521353
static void createAstAtTokenInner(Token * const tok1, const Token *endToken, bool cpp)
13531354
{
1354-
for (Token *tok = tok1; tok && tok != endToken; tok = tok ? tok->next() : nullptr) {
1355+
for (Token* tok = tok1; precedes(tok, endToken); tok = tok ? tok->next() : nullptr) {
13551356
if (tok->str() == "{" && !iscpp11init(tok)) {
13561357
const Token * const endToken2 = tok->link();
13571358
bool hasAst = false;

test/testtokenize.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6620,6 +6620,19 @@ class TestTokenizer : public TestFixture {
66206620
"}\n"));
66216621

66226622
ASSERT_NO_THROW(tokenizeAndStringify("a<b?0:1>()==3;"));
6623+
6624+
// #10336
6625+
ASSERT_NO_THROW(tokenizeAndStringify("struct a {\n"
6626+
" template <class b> a(b);\n"
6627+
"};\n"
6628+
"struct c;\n"
6629+
"void fn1(int, a);\n"
6630+
"void f() { fn1(0, {a{0}}); }\n"
6631+
"template <class> std::vector<c> g() {\n"
6632+
" int d;\n"
6633+
" for (size_t e = 0; e < d; e++)\n"
6634+
" ;\n"
6635+
"}\n"));
66236636
}
66246637

66256638
void checkNamespaces() {

0 commit comments

Comments
 (0)