Skip to content

Commit 6ee101e

Browse files
author
Your Name
committed
Add unit tests
1 parent 0d8e8cc commit 6ee101e

3 files changed

Lines changed: 102 additions & 1 deletion

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ test/testthreadexecutor.o: test/testthreadexecutor.cpp cli/executor.h cli/thread
924924
test/testtimer.o: test/testtimer.cpp lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/timer.h lib/utils.h test/fixture.h test/redirect.h
925925
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testtimer.cpp
926926

927-
test/testtoken.o: test/testtoken.cpp lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/smallvector.h lib/standards.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
927+
test/testtoken.o: test/testtoken.cpp lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
928928
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testtoken.cpp
929929

930930
test/testtokenize.o: test/testtokenize.cpp externals/simplecpp/simplecpp.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.h lib/settings.h lib/smallvector.h lib/standards.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h

test/testsimplifytemplate.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ class TestSimplifyTemplate : public TestFixture {
302302
TEST_CASE(templateTypeDeduction14); // a non template overload might be a better match
303303
TEST_CASE(templateTypeDeduction15); // final classes
304304
TEST_CASE(templateTypeDeduction16); // template between two blocks of the same namespace
305+
TEST_CASE(templateTypeDeductionTokenTypes); // standard types/keywords keep their token type
305306
TEST_CASE(templateTypeDeductionFullRebuild); // --template-full-rebuild gives the same result
306307

307308
TEST_CASE(simplifyTemplateArgs1);
@@ -6935,6 +6936,34 @@ class TestSimplifyTemplate : public TestFixture {
69356936
ASSERT_EQUALS(exp, tok(code));
69366937
}
69376938

6939+
void templateTypeDeductionTokenTypes()
6940+
{ // standard types and keywords must keep their token type when the deduce loop
6941+
// rebuilds the symbol database (the teardown clears the symbol associations of
6942+
// all tokens) - a MATCHCOMPILER=Verify build reports the resulting difference
6943+
// between the compiled and the parsed Token::Match
6944+
const char code[] = "template<class T> T id(T x) { return x; }\n"
6945+
"void* g(void* p) { return id(p); }\n"
6946+
"int h() { auto y = id(1); return y; }";
6947+
for (const Settings* s : {&settings1, &settings1_fr}) {
6948+
SimpleTokenizer tokenizer(*s, *this);
6949+
ASSERT(tokenizer.tokenize(code));
6950+
int voidCount = 0;
6951+
int autoCount = 0;
6952+
for (const Token* tok2 = tokenizer.tokens(); tok2; tok2 = tok2->next()) {
6953+
if (tok2->str() == "void") {
6954+
++voidCount;
6955+
ASSERT_EQUALS_ENUM(Token::Type::eType, tok2->tokType());
6956+
ASSERT_EQUALS(true, tok2->isStandardType());
6957+
} else if (tok2->str() == "auto") {
6958+
++autoCount;
6959+
ASSERT(tok2->tokType() == Token::Type::eKeyword || tok2->tokType() == Token::Type::eType);
6960+
}
6961+
}
6962+
ASSERT(voidCount >= 2); // the declaration and the instantiation
6963+
ASSERT_EQUALS(1, autoCount);
6964+
}
6965+
}
6966+
69386967
void templateTypeDeductionFullRebuild()
69396968
{ // --template-full-rebuild recreates the symbol database etc. after each type
69406969
// deduction round instead of updating them incrementally - the result must be

test/testtoken.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "helpers.h"
2121
#include "settings.h"
2222
#include "standards.h"
23+
#include "symboldatabase.h"
2324
#include "token.h"
2425
#include "tokenlist.h"
2526
#include "vfvalue.h"
@@ -131,6 +132,7 @@ class TestToken : public TestFixture {
131132
TEST_CASE(update_property_info_etype_c);
132133
TEST_CASE(update_property_info_etype_cpp);
133134
TEST_CASE(update_property_info_replace); // #13743
135+
TEST_CASE(update_property_info_symbol_cleared);
134136

135137
TEST_CASE(varid_reset);
136138
}
@@ -1504,6 +1506,76 @@ class TestToken : public TestFixture {
15041506
assert_tok(&tok, Token::Type::eType, false, true);
15051507
}
15061508

1509+
void update_property_info_symbol_cleared() const
1510+
{
1511+
// clearing the symbol association of a token must restore the token type that
1512+
// follows from the string. The compiled Token::Match variants (MATCHCOMPILER)
1513+
// check the token type for literal patterns, so a standard type or keyword left
1514+
// as eName makes the compiled and the parsed pattern matching disagree.
1515+
const ::Type type;
1516+
{
1517+
// a standard type is eType again after the associated type is cleared
1518+
TokenList list_cpp{settingsDefault, Standards::Language::CPP};
1519+
auto tokensFrontBack = std::make_shared<TokensFrontBack>();
1520+
Token tok(list_cpp, std::move(tokensFrontBack));
1521+
tok.str("void");
1522+
assert_tok(&tok, Token::Type::eType, /*l=*/ false, /*std=*/ true);
1523+
tok.type(&type);
1524+
assert_tok(&tok, Token::Type::eType, /*l=*/ false, /*std=*/ true);
1525+
tok.type(nullptr);
1526+
assert_tok(&tok, Token::Type::eType, /*l=*/ false, /*std=*/ true);
1527+
}
1528+
{
1529+
// a keyword is eKeyword again after the associated type is cleared
1530+
// ("auto" gets the deduced type attached and cleared)
1531+
TokenList list_cpp{settingsDefault, Standards::Language::CPP};
1532+
auto tokensFrontBack = std::make_shared<TokensFrontBack>();
1533+
Token tok(list_cpp, std::move(tokensFrontBack));
1534+
tok.str("auto");
1535+
assert_tok(&tok, Token::Type::eKeyword);
1536+
tok.type(&type);
1537+
assert_tok(&tok, Token::Type::eType);
1538+
tok.type(nullptr);
1539+
assert_tok(&tok, Token::Type::eKeyword);
1540+
}
1541+
{
1542+
// a plain name is eName again after the associated type is cleared
1543+
TokenList list_cpp{settingsDefault, Standards::Language::CPP};
1544+
auto tokensFrontBack = std::make_shared<TokensFrontBack>();
1545+
Token tok(list_cpp, std::move(tokensFrontBack));
1546+
tok.str("MyClass");
1547+
assert_tok(&tok, Token::Type::eName);
1548+
tok.type(&type);
1549+
assert_tok(&tok, Token::Type::eType);
1550+
tok.type(nullptr);
1551+
assert_tok(&tok, Token::Type::eName);
1552+
}
1553+
{
1554+
// a plain name is eName again after the associated function is cleared
1555+
TokenList list_cpp{settingsDefault, Standards::Language::CPP};
1556+
auto tokensFrontBack = std::make_shared<TokensFrontBack>();
1557+
Token tok(list_cpp, std::move(tokensFrontBack));
1558+
tok.str("f");
1559+
const Function function(&tok, "");
1560+
tok.function(&function);
1561+
assert_tok(&tok, Token::Type::eFunction);
1562+
tok.function(nullptr);
1563+
assert_tok(&tok, Token::Type::eName);
1564+
}
1565+
{
1566+
// a plain name is eName again after the associated variable is cleared
1567+
TokenList list_cpp{settingsDefault, Standards::Language::CPP};
1568+
auto tokensFrontBack = std::make_shared<TokensFrontBack>();
1569+
Token tok(list_cpp, std::move(tokensFrontBack));
1570+
tok.str("x");
1571+
const Variable var(&tok, nullptr, nullptr, 0, AccessControl::Public, nullptr, nullptr, settingsDefault);
1572+
tok.variable(&var);
1573+
assert_tok(&tok, Token::Type::eVariable);
1574+
tok.variable(nullptr);
1575+
assert_tok(&tok, Token::Type::eName);
1576+
}
1577+
}
1578+
15071579
void varid_reset() const
15081580
{
15091581
TokenList list_c{settingsDefault, Standards::Language::C};

0 commit comments

Comments
 (0)