Skip to content

Commit d551564

Browse files
committed
TestToken: added test for update_property_info()
1 parent 1030369 commit d551564

5 files changed

Lines changed: 287 additions & 2 deletions

File tree

lib/token.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ static const std::unordered_set<std::string> controlFlowKeywords = {
108108
void Token::update_property_info()
109109
{
110110
setFlag(fIsControlFlowKeyword, controlFlowKeywords.find(mStr) != controlFlowKeywords.end());
111+
// TODO: clear fIsLong
111112
isStandardType(false);
112113

113114
if (!mStr.empty()) {
@@ -147,6 +148,7 @@ void Token::update_property_info()
147148
mStr == "||" ||
148149
mStr == "!"))
149150
tokType(eLogicalOp);
151+
// TODO: should link check only apply to < and >? Token::link() suggests so
150152
else if (mStr.size() <= 2 && !mLink &&
151153
(mStr == "==" ||
152154
mStr == "!=" ||
@@ -172,6 +174,9 @@ void Token::update_property_info()
172174
} else {
173175
tokType(eNone);
174176
}
177+
// TODO: make sure varid is only set for eVariable
178+
//assert(!mImpl->mVarId || mTokType == eVariable);
179+
// TODO: validate type for linked token?
175180
}
176181

177182
static const std::unordered_set<std::string> stdTypes = { "bool"

lib/token.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,9 +953,12 @@ class CPPCHECKLIB Token {
953953
}
954954
void varId(nonneg int id) {
955955
mImpl->mVarId = id;
956+
// TODO: remove special handling?
956957
if (id != 0) {
957958
tokType(eVariable);
958959
isStandardType(false);
960+
// TODO: clear fIsLong
961+
// TODO: clear fIsControlFlowKeyword
959962
} else {
960963
update_property_info();
961964
}

test/fixture.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,13 @@ static std::string writestr(const std::string &str, bool gccStyle = false)
162162
return ostr.str();
163163
}
164164

165-
void TestFixture::assert_(const char * const filename, const unsigned int linenr, const bool condition) const
165+
void TestFixture::assert_(const char * const filename, const unsigned int linenr, const bool condition, const std::string& msg) const
166166
{
167167
if (!condition) {
168168
++fails_counter;
169169
errmsg << getLocationStr(filename, linenr) << ": Assertion failed." << std::endl << "_____" << std::endl;
170+
if (!msg.empty())
171+
errmsg << "Hint:" << std::endl << msg << std::endl;
170172
}
171173
}
172174

test/fixture.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class TestFixture : public ErrorLogger {
6868
virtual void teardownTestInternal() {}
6969
std::string getLocationStr(const char * filename, unsigned int linenr) const;
7070

71-
void assert_(const char * filename, unsigned int linenr, bool condition) const;
71+
void assert_(const char * filename, unsigned int linenr, bool condition, const std::string& msg = "") const;
7272

7373
template<typename T>
7474
void assertEquals(const char* const filename, const unsigned int linenr, const T& expected, const T& actual, const std::string& msg = "") const {
@@ -313,14 +313,18 @@ class TestInstance {
313313
// TODO: the asserts do not actually assert i.e. do stop executing the test
314314
#define ASSERT( CONDITION ) assert_(__FILE__, __LINE__, (CONDITION))
315315
#define ASSERT_LOC( CONDITION, FILE_, LINE_ ) assert_(FILE_, LINE_, (CONDITION))
316+
#define ASSERT_LOC_MSG( CONDITION, MSG, FILE_, LINE_ ) assert_(FILE_, LINE_, (CONDITION), MSG)
316317
// *INDENT-OFF*
317318
#define ASSERT_EQUALS( EXPECTED, ACTUAL ) do { try { assertEquals(__FILE__, __LINE__, (EXPECTED), (ACTUAL)); } catch (...) { assertNoThrowFail(__FILE__, __LINE__); } } while (false)
318319
// *INDENT-ON*
319320
#define ASSERT_EQUALS_WITHOUT_LINENUMBERS( EXPECTED, ACTUAL ) assertEqualsWithoutLineNumbers(__FILE__, __LINE__, EXPECTED, ACTUAL)
320321
#define ASSERT_EQUALS_DOUBLE( EXPECTED, ACTUAL, TOLERANCE ) assertEqualsDouble(__FILE__, __LINE__, EXPECTED, ACTUAL, TOLERANCE)
322+
#define ASSERT_EQUALS_LOC( EXPECTED, ACTUAL, FILE_, LINE_ ) assertEquals(FILE_, LINE_, EXPECTED, ACTUAL)
321323
#define ASSERT_EQUALS_LOC_MSG( EXPECTED, ACTUAL, MSG, FILE_, LINE_ ) assertEquals(FILE_, LINE_, EXPECTED, ACTUAL, MSG)
322324
#define ASSERT_EQUALS_MSG( EXPECTED, ACTUAL, MSG ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL, MSG)
323325
#define ASSERT_EQUALS_ENUM( EXPECTED, ACTUAL ) assertEqualsEnum(__FILE__, __LINE__, (EXPECTED), (ACTUAL))
326+
#define ASSERT_EQUALS_ENUM_LOC( EXPECTED, ACTUAL, FILE_, LINE_ ) assertEqualsEnum(FILE_, LINE_, (EXPECTED), (ACTUAL))
327+
#define ASSERT_EQUALS_ENUM_LOC_MSG( EXPECTED, ACTUAL, MSG, FILE_, LINE_ ) assertEqualsEnum(FILE_, LINE_, (EXPECTED), (ACTUAL), MSG)
324328
#define TODO_ASSERT_EQUALS_ENUM( WANTED, CURRENT, ACTUAL ) todoAssertEqualsEnum(__FILE__, __LINE__, WANTED, CURRENT, ACTUAL)
325329
#define ASSERT_THROW_EQUALS( CMD, EXCEPTION, EXPECTED ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&e) { assertEquals(__FILE__, __LINE__, EXPECTED, e.errorMessage); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)
326330
#define ASSERT_THROW_EQUALS_2( CMD, EXCEPTION, EXPECTED ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&e) { assertEquals(__FILE__, __LINE__, EXPECTED, e.what()); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)

test/testtoken.cpp

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ class TestToken : public TestFixture {
114114
TEST_CASE(expressionString);
115115

116116
TEST_CASE(hasKnownIntValue);
117+
118+
TEST_CASE(update_property_info);
119+
TEST_CASE(update_property_info_evariable);
120+
TEST_CASE(update_property_info_ekeyword_c);
121+
TEST_CASE(update_property_info_ekeyword_cpp);
122+
TEST_CASE(update_property_info_ebracket_link);
123+
TEST_CASE(update_property_info_ecomparisonop_link);
124+
TEST_CASE(update_property_info_etype_c);
125+
TEST_CASE(update_property_info_etype_cpp);
117126
}
118127

119128
void nextprevious() const {
@@ -1224,6 +1233,268 @@ class TestToken : public TestFixture {
12241233
ASSERT_EQUALS(true, token.addValue(v2));
12251234
ASSERT_EQUALS(false, token.hasKnownIntValue());
12261235
}
1236+
1237+
#define assert_tok(...) _assert_tok(__FILE__, __LINE__, __VA_ARGS__)
1238+
void _assert_tok(const char* file, int line, const Token* tok, Token::Type t, bool l = false, bool std = false, bool ctrl = false) const
1239+
{
1240+
ASSERT_LOC_MSG(tok, "tok", file, line);
1241+
ASSERT_EQUALS_ENUM_LOC_MSG(t, tok->tokType(), "tokType", file, line);
1242+
ASSERT_EQUALS_LOC_MSG(l, tok->isLong(), "isLong", file, line);
1243+
ASSERT_EQUALS_LOC_MSG(std, tok->isStandardType(), "isStandardType", file, line);
1244+
ASSERT_EQUALS_LOC_MSG(ctrl, tok->isControlFlowKeyword(), "isControlFlowKeyword", file, line);
1245+
}
1246+
1247+
void _assert_tok(const char* file, int line, const std::string& s, Token::Type t, bool l = false, bool std = false, bool ctrl = false) const
1248+
{
1249+
TokensFrontBack tokensFrontBack(list);
1250+
Token tok(tokensFrontBack);
1251+
tok.str(s);
1252+
_assert_tok(file, line, &tok, t, l, std, ctrl);
1253+
}
1254+
1255+
void update_property_info() const
1256+
{
1257+
assert_tok("", Token::Type::eNone);
1258+
assert_tok("true", Token::Type::eBoolean);
1259+
assert_tok("false", Token::Type::eBoolean);
1260+
assert_tok("\"\"", Token::Type::eString);
1261+
assert_tok("L\"\"", Token::Type::eString, /*l=*/ true);
1262+
assert_tok("'a'", Token::Type::eChar);
1263+
assert_tok("L'a'", Token::Type::eChar, /*l=*/ true);
1264+
// eVariable has separate test
1265+
// eKeyword with specific languages and standards has separate tests
1266+
assert_tok("sizeof", Token::Type::eKeyword);
1267+
assert_tok("goto", Token::Type::eKeyword, /*l=*/ false, /*std=*/ false, /*ctrl=*/ true);
1268+
assert_tok("asm", Token::Type::eKeyword);
1269+
assert_tok("a", Token::Type::eName);
1270+
assert_tok("_a", Token::Type::eName);
1271+
assert_tok("$a", Token::Type::eName);
1272+
assert_tok("bool2", Token::Type::eName);
1273+
assert_tok("0", Token::Type::eNumber);
1274+
assert_tok("-0", Token::Type::eNumber);
1275+
assert_tok("+0", Token::Type::eNumber);
1276+
assert_tok("0xa", Token::Type::eNumber);
1277+
assert_tok("010", Token::Type::eNumber);
1278+
assert_tok("0b0", Token::Type::eNumber);
1279+
assert_tok("0.0", Token::Type::eNumber);
1280+
assert_tok("0x0.3p10", Token::Type::eNumber);
1281+
assert_tok("0z", Token::Type::eNumber); // TODO: not a valid number
1282+
assert_tok("0_km", Token::Type::eName); // user literal
1283+
assert_tok("=", Token::Type::eAssignmentOp);
1284+
assert_tok("<<=", Token::Type::eAssignmentOp);
1285+
assert_tok(">>=", Token::Type::eAssignmentOp);
1286+
assert_tok("+=", Token::Type::eAssignmentOp);
1287+
assert_tok("-=", Token::Type::eAssignmentOp);
1288+
assert_tok("*=", Token::Type::eAssignmentOp);
1289+
assert_tok("/=", Token::Type::eAssignmentOp);
1290+
assert_tok("%=", Token::Type::eAssignmentOp);
1291+
assert_tok("&=", Token::Type::eAssignmentOp);
1292+
assert_tok("|=", Token::Type::eAssignmentOp);
1293+
assert_tok("^=", Token::Type::eAssignmentOp);
1294+
assert_tok(",", Token::Type::eExtendedOp);
1295+
assert_tok("[", Token::Type::eExtendedOp);
1296+
assert_tok("]", Token::Type::eExtendedOp);
1297+
assert_tok("(", Token::Type::eExtendedOp);
1298+
assert_tok(")", Token::Type::eExtendedOp);
1299+
assert_tok("?", Token::Type::eExtendedOp);
1300+
assert_tok(":", Token::Type::eExtendedOp);
1301+
assert_tok("<<", Token::Type::eArithmeticalOp);
1302+
assert_tok(">>", Token::Type::eArithmeticalOp);
1303+
assert_tok("+", Token::Type::eArithmeticalOp);
1304+
assert_tok("-", Token::Type::eArithmeticalOp);
1305+
assert_tok("*", Token::Type::eArithmeticalOp);
1306+
assert_tok("/", Token::Type::eArithmeticalOp);
1307+
assert_tok("%", Token::Type::eArithmeticalOp);
1308+
assert_tok("&", Token::Type::eBitOp);
1309+
assert_tok("|", Token::Type::eBitOp);
1310+
assert_tok("^", Token::Type::eBitOp);
1311+
assert_tok("~", Token::Type::eBitOp);
1312+
assert_tok("&&", Token::Type::eLogicalOp);
1313+
assert_tok("||", Token::Type::eLogicalOp);
1314+
assert_tok("!", Token::Type::eLogicalOp);
1315+
assert_tok("==", Token::Type::eComparisonOp);
1316+
assert_tok("!=", Token::Type::eComparisonOp);
1317+
assert_tok("<", Token::Type::eComparisonOp);
1318+
assert_tok("<=", Token::Type::eComparisonOp);
1319+
assert_tok(">", Token::Type::eComparisonOp);
1320+
assert_tok(">=", Token::Type::eComparisonOp);
1321+
// eComparisonOp with link has a separate test
1322+
assert_tok("<=>", Token::Type::eComparisonOp);
1323+
assert_tok("++", Token::Type::eIncDecOp);
1324+
assert_tok("--", Token::Type::eIncDecOp);
1325+
assert_tok("{", Token::Type::eBracket);
1326+
assert_tok("}", Token::Type::eBracket);
1327+
// < and > with link have a separate test
1328+
assert_tok("...", Token::Type::eEllipsis);
1329+
assert_tok(";", Token::Type::eOther);
1330+
// eType with specific languages and standards has separate tests
1331+
assert_tok("void", Token::Type::eType, /*l=*/ false, /*std=*/ true);
1332+
}
1333+
1334+
void update_property_info_evariable() const
1335+
{
1336+
{
1337+
TokensFrontBack tokensFrontBack(list);
1338+
Token tok(tokensFrontBack);
1339+
tok.str("var1");
1340+
tok.varId(17);
1341+
assert_tok(&tok, Token::Type::eVariable);
1342+
}
1343+
{
1344+
TokensFrontBack tokensFrontBack(list);
1345+
Token tok(tokensFrontBack);
1346+
tok.varId(17);
1347+
tok.str("var1");
1348+
assert_tok(&tok, Token::Type::eVariable);
1349+
}
1350+
}
1351+
1352+
void update_property_info_ekeyword_c() const
1353+
{
1354+
{
1355+
const Settings s = settingsBuilder().c(Standards::cstd_t::C89).build();
1356+
TokenList list_c{&s};
1357+
list_c.setLang(Standards::Language::C);
1358+
TokensFrontBack tokensFrontBack(list_c);
1359+
Token tok(tokensFrontBack);
1360+
tok.str("alignas"); // not a C89 keyword
1361+
assert_tok(&tok, Token::Type::eName);
1362+
}
1363+
{
1364+
TokenList list_c{&settingsDefault};
1365+
list_c.setLang(Standards::Language::C);
1366+
TokensFrontBack tokensFrontBack(list_c);
1367+
Token tok(tokensFrontBack);
1368+
tok.str("alignas"); // a C23 keyword
1369+
assert_tok(&tok, Token::Type::eKeyword);
1370+
}
1371+
{
1372+
TokenList list_c{&settingsDefault};
1373+
list_c.setLang(Standards::Language::C);
1374+
TokensFrontBack tokensFrontBack(list_c);
1375+
Token tok(tokensFrontBack);
1376+
tok.str("and_eq"); // a C++ keyword
1377+
assert_tok(&tok, Token::Type::eName);
1378+
}
1379+
}
1380+
1381+
void update_property_info_ekeyword_cpp() const
1382+
{
1383+
{
1384+
const Settings s = settingsBuilder().cpp(Standards::cppstd_t::CPP03).build();
1385+
TokenList list_cpp{&s};
1386+
list_cpp.setLang(Standards::Language::CPP);
1387+
TokensFrontBack tokensFrontBack(list_cpp);
1388+
Token tok(tokensFrontBack);
1389+
tok.str("consteval"); // not a C++03 keyword
1390+
assert_tok(&tok, Token::Type::eName);
1391+
}
1392+
{
1393+
TokenList list_cpp{&settingsDefault};
1394+
list_cpp.setLang(Standards::Language::CPP);
1395+
TokensFrontBack tokensFrontBack(list_cpp);
1396+
Token tok(tokensFrontBack);
1397+
tok.str("consteval"); // a C++20 keyword
1398+
assert_tok(&tok, Token::Type::eKeyword);
1399+
}
1400+
{
1401+
TokenList list_cpp{&settingsDefault};
1402+
list_cpp.setLang(Standards::Language::CPP);
1403+
TokensFrontBack tokensFrontBack(list_cpp);
1404+
Token tok(tokensFrontBack);
1405+
tok.str("typeof_unqual"); // a C keyword
1406+
assert_tok(&tok, Token::Type::eName);
1407+
}
1408+
}
1409+
1410+
void update_property_info_ebracket_link() const
1411+
{
1412+
{
1413+
TokensFrontBack tokensFrontBack(list);
1414+
Token tok(tokensFrontBack);
1415+
tok.str("<");
1416+
1417+
Token tok2(tokensFrontBack);
1418+
tok.link(&tok2);
1419+
assert_tok(&tok, Token::Type::eBracket);
1420+
}
1421+
1422+
{
1423+
TokensFrontBack tokensFrontBack(list);
1424+
Token tok(tokensFrontBack);
1425+
1426+
Token tok2(tokensFrontBack);
1427+
tok.link(&tok2);
1428+
1429+
tok.str("<");
1430+
assert_tok(&tok, Token::Type::eBracket);
1431+
}
1432+
}
1433+
1434+
void update_property_info_ecomparisonop_link() const
1435+
{
1436+
{
1437+
TokensFrontBack tokensFrontBack(list);
1438+
Token tok(tokensFrontBack);
1439+
tok.str("==");
1440+
1441+
Token tok2(tokensFrontBack);
1442+
tok.link(&tok2); // TODO: does not (and probably should not) update
1443+
assert_tok(&tok, Token::Type::eComparisonOp);
1444+
}
1445+
1446+
{
1447+
TokensFrontBack tokensFrontBack(list);
1448+
Token tok(tokensFrontBack);
1449+
1450+
Token tok2(tokensFrontBack);
1451+
tok.link(&tok2);
1452+
1453+
tok.str("==");
1454+
assert_tok(&tok, Token::Type::eOther); // TODO: this looks wrong
1455+
}
1456+
}
1457+
1458+
void update_property_info_etype_c() const
1459+
{
1460+
{
1461+
TokenList list_c{&settingsDefault};
1462+
list_c.setLang(Standards::Language::C);
1463+
TokensFrontBack tokensFrontBack(list_c);
1464+
Token tok(tokensFrontBack);
1465+
tok.str("char"); // not treated as keyword in TokenList::isKeyword()
1466+
assert_tok(&tok, Token::Type::eType, /*l=*/ false, /*std=*/ true);
1467+
}
1468+
{
1469+
TokenList list_c{&settingsDefault};
1470+
list_c.setLang(Standards::Language::C);
1471+
TokensFrontBack tokensFrontBack(list_c);
1472+
Token tok(tokensFrontBack);
1473+
tok.str("size_t"); // not treated as keyword in TokenList::isKeyword()
1474+
assert_tok(&tok, Token::Type::eType, /*l=*/ false, /*std=*/ true);
1475+
}
1476+
}
1477+
1478+
void update_property_info_etype_cpp() const
1479+
{
1480+
{
1481+
TokenList list_cpp{&settingsDefault};
1482+
list_cpp.setLang(Standards::Language::CPP);
1483+
TokensFrontBack tokensFrontBack(list_cpp);
1484+
Token tok(tokensFrontBack);
1485+
tok.str("bool"); // not treated as keyword in TokenList::isKeyword()
1486+
assert_tok(&tok, Token::Type::eType, /*l=*/ false, /*std=*/ true);
1487+
}
1488+
{
1489+
TokenList list_cpp{&settingsDefault};
1490+
list_cpp.setLang(Standards::Language::CPP);
1491+
TokensFrontBack tokensFrontBack(list_cpp);
1492+
Token tok(tokensFrontBack);
1493+
tok.str("size_t");
1494+
assert_tok(&tok, Token::Type::eType, /*l=*/ false, /*std=*/ true);
1495+
}
1496+
}
1497+
#undef assert_tok
12271498
};
12281499

12291500
REGISTER_TEST(TestToken)

0 commit comments

Comments
 (0)