From 2300a773e104339ce151e4def065e9cabbccbeb8 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Fri, 9 Jul 2021 00:22:24 -0500 Subject: [PATCH] Fix 10336: AST cyclic dependency on valid C++ code (#3331) --- lib/tokenlist.cpp | 3 ++- test/testtokenize.cpp | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index cef633b9d..aa81112ea 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -19,6 +19,7 @@ //--------------------------------------------------------------------------- #include "tokenlist.h" +#include "astutils.h" #include "errorlogger.h" #include "library.h" #include "path.h" @@ -1351,7 +1352,7 @@ static Token * createAstAtToken(Token *tok, bool cpp); // Compile inner expressions inside inner ({..}) and lambda bodies static void createAstAtTokenInner(Token * const tok1, const Token *endToken, bool cpp) { - for (Token *tok = tok1; tok && tok != endToken; tok = tok ? tok->next() : nullptr) { + for (Token* tok = tok1; precedes(tok, endToken); tok = tok ? tok->next() : nullptr) { if (tok->str() == "{" && !iscpp11init(tok)) { const Token * const endToken2 = tok->link(); bool hasAst = false; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index af0e8b416..beddd58cc 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -6620,6 +6620,19 @@ private: "}\n")); ASSERT_NO_THROW(tokenizeAndStringify("a()==3;")); + + // #10336 + ASSERT_NO_THROW(tokenizeAndStringify("struct a {\n" + " template a(b);\n" + "};\n" + "struct c;\n" + "void fn1(int, a);\n" + "void f() { fn1(0, {a{0}}); }\n" + "template std::vector g() {\n" + " int d;\n" + " for (size_t e = 0; e < d; e++)\n" + " ;\n" + "}\n")); } void checkNamespaces() {