diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 3e3a66e24..b58692026 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -3913,3 +3913,35 @@ void Tokenizer::simplifyComma() } } + + +bool Tokenizer::validate() const +{ + std::stack linktok; + + for (const Token *tok = tokens(); tok; tok = tok->next()) + { + if (Token::Match(tok, "[{(]")) + { + assert(tok->link() != 0); + linktok.push(tok); + continue; + } + + else if (Token::Match(tok, "[})]")) + { + assert(tok->link() != 0); + assert(linktok.empty() == false); + assert(tok->link() == linktok.top()); + assert(tok == tok->link()->link()); + linktok.pop(); + continue; + } + + assert(tok->link() == 0); + } + + assert(linktok.empty()); + + return true; +} diff --git a/src/tokenize.h b/src/tokenize.h index 0fb66852f..0e87f6a96 100644 --- a/src/tokenize.h +++ b/src/tokenize.h @@ -305,6 +305,13 @@ private: /** Disable assignments.. */ Tokenizer(const Tokenizer &); + /** + * assert that tokens are ok - used during debugging for example + * to catch problems in simplifyTokenList. + * @return always true. + */ + bool validate() const; + /** Disable assignment operator */ void operator=(const Tokenizer &);