| 6509 | |
| 6510 | template<size_t size> |
| 6511 | std::string testAst(const char (&data)[size], AstStyle style = AstStyle::Simple, ListSimplification ls = ListSimplification::Partial) { |
| 6512 | // tokenize given code.. |
| 6513 | TokenList tokenlist{settingsDefault, Standards::Language::CPP}; |
| 6514 | tokenlist.appendFileIfNew("test.cpp"); |
| 6515 | if (!tokenlist.createTokensFromString(data)) |
| 6516 | return "ERROR"; |
| 6517 | |
| 6518 | TokenizerTest tokenizer(std::move(tokenlist), *this); |
| 6519 | if (ls == ListSimplification::Partial) { |
| 6520 | tokenizer.combineStringAndCharLiterals(); |
| 6521 | tokenizer.combineOperators(); |
| 6522 | tokenizer.simplifySpaceshipOperator(); |
| 6523 | tokenizer.createLinks(); |
| 6524 | tokenizer.createLinks2(); |
| 6525 | tokenizer.simplifyCAlternativeTokens(); |
| 6526 | tokenizer.list.front()->assignIndexes(); |
| 6527 | |
| 6528 | // set varid.. |
| 6529 | for (Token *tok = tokenizer.list.front(); tok; tok = tok->next()) { |
| 6530 | if (tok->str() == "var") |
| 6531 | tok->varId(1); |
| 6532 | } |
| 6533 | |
| 6534 | // Create AST.. |
| 6535 | tokenizer.prepareTernaryOpForAST(); |
| 6536 | tokenizer.list.createAst(); |
| 6537 | |
| 6538 | tokenizer.list.validateAst(false); |
| 6539 | } else { // Full |
| 6540 | tokenizer.simplifyTokens1(""); |
| 6541 | } |
| 6542 | |
| 6543 | // Basic AST validation |
| 6544 | for (const Token *tok = tokenizer.list.front(); tok; tok = tok->next()) { |
| 6545 | if (tok->astOperand2() && !tok->astOperand1() && tok->str() != ";" && tok->str() != ":") |
| 6546 | return "Op2 but no Op1 for token: " + tok->str(); |
| 6547 | } |
| 6548 | |
| 6549 | // Return stringified AST |
| 6550 | if (style == AstStyle::Z3) |
| 6551 | return tokenizer.list.front()->astTop()->astStringZ3(); |
| 6552 | |
| 6553 | std::string ret; |
| 6554 | std::set<const Token *> astTop; |
| 6555 | for (const Token *tok = tokenizer.list.front(); tok; tok = tok->next()) { |
| 6556 | if (tok->astOperand1() && astTop.find(tok->astTop()) == astTop.end()) { |
| 6557 | astTop.insert(tok->astTop()); |
| 6558 | if (!ret.empty()) |
| 6559 | ret += " "; |
| 6560 | ret += tok->astTop()->astString(); |
| 6561 | } |
| 6562 | } |
| 6563 | return ret; |
| 6564 | } |
| 6565 | |
| 6566 | void astexpr() { // simple expressions with arithmetical ops |
| 6567 | ASSERT_EQUALS("12+3+", testAst("1+2+3")); |
nothing calls this directly
no test coverage detected