Skip to content

Commit 6739995

Browse files
authored
removed or annotated some code which is only used in test code (cppcheck-opensource#3656)
1 parent dfd2291 commit 6739995

9 files changed

Lines changed: 5 additions & 224 deletions

File tree

gui/checkthread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class CheckThread : public QThread {
7373
* @brief method that is run in a thread
7474
*
7575
*/
76-
void run();
76+
void run() override;
7777

7878
void stop();
7979

lib/library.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class CPPCHECKLIB Library {
6868
Error load(const char exename[], const char path[]);
6969
Error load(const tinyxml2::XMLDocument &doc);
7070

71-
/** this is primarily meant for unit tests. it only returns true/false */
71+
/** this is used for unit tests */
7272
bool loadxmldata(const char xmldata[], std::size_t len);
7373

7474
struct AllocFunc {

lib/mathlib.cpp

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -357,105 +357,6 @@ unsigned int MathLib::encodeMultiChar(const std::string& str)
357357
return retval;
358358
}
359359

360-
std::string MathLib::normalizeCharacterLiteral(const std::string& iLiteral)
361-
{
362-
std::string normalizedLiteral;
363-
const std::string::size_type iLiteralLen = iLiteral.size();
364-
for (std::string::size_type idx = 0; idx < iLiteralLen; ++idx) {
365-
if (iLiteral[idx] != '\\') {
366-
normalizedLiteral.push_back(iLiteral[idx]);
367-
continue;
368-
}
369-
++idx;
370-
if (idx == iLiteralLen) {
371-
throw InternalError(nullptr, "Internal Error. MathLib::normalizeCharacterLiteral: Unhandled char constant '" + iLiteral + "'.");
372-
}
373-
switch (iLiteral[idx]) {
374-
case 'x':
375-
// Hexa-decimal number: skip \x and interpret the next two characters
376-
{
377-
if (++idx == iLiteralLen)
378-
throw InternalError(nullptr, "Internal Error. MathLib::normalizeCharacterLiteral: Unhandled char constant '" + iLiteral + "'.");
379-
std::string tempBuf;
380-
tempBuf.push_back(iLiteral[idx]);
381-
if (++idx != iLiteralLen)
382-
tempBuf.push_back(iLiteral[idx]);
383-
normalizedLiteral.push_back(static_cast<char>(MathLib::toULongNumber("0x" + tempBuf)));
384-
continue;
385-
}
386-
case 'u':
387-
case 'U':
388-
// Unicode string; just skip the \u or \U
389-
if (idx + 1 == iLiteralLen)
390-
throw InternalError(nullptr, "Internal Error. MathLib::characterLiteralToLongNumber: Unhandled char constant '" + iLiteral + "'.");
391-
continue;
392-
}
393-
// Single digit octal number
394-
if (1 == iLiteralLen - idx) {
395-
switch (iLiteral[idx]) {
396-
case '0':
397-
case '1':
398-
case '2':
399-
case '3':
400-
case '4':
401-
case '5':
402-
case '6':
403-
case '7':
404-
normalizedLiteral.push_back(iLiteral[idx]-'0');
405-
break;
406-
case 'a':
407-
normalizedLiteral.push_back('\a');
408-
break;
409-
case 'b':
410-
normalizedLiteral.push_back('\b');
411-
break;
412-
case 'e':
413-
normalizedLiteral.push_back(0x1B); // clang, gcc, tcc interpnormalizedLiteral this as 0x1B - escape character
414-
break;
415-
case 'f':
416-
normalizedLiteral.push_back('\f');
417-
break;
418-
case 'n':
419-
normalizedLiteral.push_back('\n');
420-
break;
421-
case 'r':
422-
normalizedLiteral.push_back('\r');
423-
break;
424-
case 't':
425-
normalizedLiteral.push_back('\t');
426-
break;
427-
case 'v':
428-
normalizedLiteral.push_back('\v');
429-
break;
430-
case '\\':
431-
case '\?':
432-
case '\'':
433-
case '\"':
434-
normalizedLiteral.push_back(iLiteral[idx]);
435-
break;
436-
default:
437-
throw InternalError(nullptr, "Internal Error. MathLib::normalizeCharacterLiteral: Unhandled char constant '" + iLiteral + "'.");
438-
}
439-
continue;
440-
}
441-
// 2-3 digit octal number
442-
if (!MathLib::isOctalDigit(iLiteral[idx]))
443-
throw InternalError(nullptr, "Internal Error. MathLib::normalizeCharacterLiteral: Unhandled char constant '" + iLiteral + "'.");
444-
std::string tempBuf;
445-
tempBuf.push_back(iLiteral[idx]);
446-
++idx;
447-
if (MathLib::isOctalDigit(iLiteral[idx])) {
448-
tempBuf.push_back(iLiteral[idx]);
449-
++idx;
450-
if (MathLib::isOctalDigit(iLiteral[idx])) {
451-
tempBuf.push_back(iLiteral[idx]);
452-
}
453-
}
454-
normalizedLiteral.push_back(static_cast<char>(MathLib::toLongNumber("0" + tempBuf)));
455-
}
456-
return normalizedLiteral;
457-
}
458-
459360
MathLib::bigint MathLib::toLongNumber(const std::string & str)
460361
{
461362
// hexadecimal numbers:

lib/mathlib.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ class CPPCHECKLIB MathLib {
9393

9494
static std::string getSuffix(const std::string& value);
9595
/**
96+
* Only used in unit tests
97+
*
9698
* \param[in] str string
9799
* \param[in] supportMicrosoftExtensions support Microsoft extension: i64
98100
* \return true if str is a non-empty valid integer suffix
@@ -133,13 +135,6 @@ class CPPCHECKLIB MathLib {
133135
* \return Whether iCode[iPos] is a C++14 digit separator
134136
*/
135137
static bool isDigitSeparator(const std::string& iCode, std::string::size_type iPos);
136-
137-
private:
138-
/*
139-
* \param iLiteral A character literal
140-
* \return The equivalent character literal with all escapes interpreted
141-
*/
142-
static std::string normalizeCharacterLiteral(const std::string& iLiteral);
143138
};
144139

145140
MathLib::value operator+(const MathLib::value &v1, const MathLib::value &v2);

lib/symboldatabase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,7 @@ class CPPCHECKLIB SymbolDatabase {
13751375
*/
13761376
const Function *findFunction(const Token *tok) const;
13771377

1378+
/** For unit testing only */
13781379
const Scope *findScopeByName(const std::string& name) const;
13791380

13801381
const Type* findType(const Token *startTok, const Scope *startScope) const;

lib/tokenize.cpp

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -9187,85 +9187,6 @@ void Tokenizer::simplifyNestedStrcat()
91879187
}
91889188
}
91899189

9190-
// Check if this statement is a duplicate definition. A duplicate
9191-
// definition will hide the enumerator within it's scope so just
9192-
// skip the entire scope of the duplicate.
9193-
bool Tokenizer::duplicateDefinition(Token ** tokPtr)
9194-
{
9195-
// check for an end of definition
9196-
const Token * tok = *tokPtr;
9197-
if (tok && Token::Match(tok->next(), ";|,|[|=|)|>")) {
9198-
const Token * end = tok->next();
9199-
9200-
if (end->str() == "[") {
9201-
end = end->link()->next();
9202-
} else if (end->str() == ",") {
9203-
// check for function argument
9204-
if (Token::Match(tok->previous(), "(|,"))
9205-
return false;
9206-
9207-
// find end of definition
9208-
int level = 0;
9209-
while (end->next() && (!Token::Match(end->next(), ";|)|>") ||
9210-
(end->next()->str() == ")" && level == 0))) {
9211-
if (end->next()->str() == "(")
9212-
++level;
9213-
else if (end->next()->str() == ")")
9214-
--level;
9215-
9216-
end = end->next();
9217-
}
9218-
} else if (end->str() == ")") {
9219-
// check for function argument
9220-
if (tok->previous()->str() == ",")
9221-
return false;
9222-
}
9223-
9224-
if (end) {
9225-
if (Token::simpleMatch(end, ") {")) { // function parameter ?
9226-
// make sure it's not a conditional
9227-
if (Token::Match(end->link()->previous(), "if|for|while|switch|BOOST_FOREACH") || Token::Match(end->link()->tokAt(-2), ":|,"))
9228-
return false;
9229-
9230-
// look backwards
9231-
if (tok->previous()->str() == "enum" ||
9232-
(Token::Match(tok->previous(), "%type%") &&
9233-
tok->previous()->str() != "return") ||
9234-
Token::Match(tok->tokAt(-2), "%type% &|*")) {
9235-
// duplicate definition so skip entire function
9236-
*tokPtr = end->next()->link();
9237-
return true;
9238-
}
9239-
} else if (end->str() == ">") { // template parameter ?
9240-
// look backwards
9241-
if (tok->previous()->str() == "enum" ||
9242-
(Token::Match(tok->previous(), "%type%") &&
9243-
tok->previous()->str() != "return")) {
9244-
// duplicate definition so skip entire template
9245-
while (end && end->str() != "{")
9246-
end = end->next();
9247-
if (end) {
9248-
*tokPtr = end->link();
9249-
return true;
9250-
}
9251-
}
9252-
} else {
9253-
if (Token::Match(tok->previous(), "enum|,"))
9254-
return true;
9255-
else if (Token::Match(tok->previous(), "%type%")) {
9256-
// look backwards
9257-
const Token *back = tok;
9258-
while (back && back->isName())
9259-
back = back->previous();
9260-
if (!back || (Token::Match(back, "[(,;{}]") && !Token::Match(back->next(),"return|throw")))
9261-
return true;
9262-
}
9263-
}
9264-
}
9265-
}
9266-
return false;
9267-
}
9268-
92699190
static const std::set<std::string> stdFunctionsPresentInC = {
92709191
"strcat",
92719192
"strcpy",

lib/tokenize.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -810,11 +810,6 @@ class CPPCHECKLIB Tokenizer {
810810
* */
811811
void prepareTernaryOpForAST();
812812

813-
/**
814-
* check for duplicate enum definition
815-
*/
816-
static bool duplicateDefinition(Token **tokPtr);
817-
818813
/**
819814
* report error message
820815
*/

test/testmathlib.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ class TestMathLib : public TestFixture {
6262
TEST_CASE(tan);
6363
TEST_CASE(abs);
6464
TEST_CASE(toString);
65-
TEST_CASE(characterLiteralsNormalization);
6665
TEST_CASE(CPP14DigitSeparators);
6766
}
6867

@@ -1246,27 +1245,6 @@ class TestMathLib : public TestFixture {
12461245
ASSERT_EQUALS("-0", MathLib::toString(-0.0L));
12471246
}
12481247

1249-
void characterLiteralsNormalization() const {
1250-
// `A` is 0x41 and 0101
1251-
ASSERT_EQUALS("A", MathLib::normalizeCharacterLiteral("\\x41"));
1252-
ASSERT_EQUALS("A", MathLib::normalizeCharacterLiteral("\\101"));
1253-
// Hexa and octal numbers should not only be interpreted in byte 1
1254-
ASSERT_EQUALS("TESTATEST", MathLib::normalizeCharacterLiteral("TEST\\x41TEST"));
1255-
ASSERT_EQUALS("TESTATEST", MathLib::normalizeCharacterLiteral("TEST\\101TEST"));
1256-
ASSERT_EQUALS("TESTTESTA", MathLib::normalizeCharacterLiteral("TESTTEST\\x41"));
1257-
ASSERT_EQUALS("TESTTESTA", MathLib::normalizeCharacterLiteral("TESTTEST\\101"));
1258-
// Single escape sequences
1259-
ASSERT_EQUALS("\?", MathLib::normalizeCharacterLiteral("\\?"));
1260-
ASSERT_EQUALS("\'", MathLib::normalizeCharacterLiteral("\\'"));
1261-
// Incomplete hexa and octal sequences
1262-
ASSERT_THROW(MathLib::normalizeCharacterLiteral("\\"), InternalError);
1263-
ASSERT_THROW(MathLib::normalizeCharacterLiteral("\\x"), InternalError);
1264-
// No octal digit in an octal sequence
1265-
ASSERT_THROW(MathLib::normalizeCharacterLiteral("\\9"), InternalError);
1266-
// Unsupported single escape sequence
1267-
ASSERT_THROW(MathLib::normalizeCharacterLiteral("\\c"), InternalError);
1268-
}
1269-
12701248
void CPP14DigitSeparators() const { // Ticket #7137, #7565
12711249
ASSERT(MathLib::isDigitSeparator("'", 0) == false);
12721250
ASSERT(MathLib::isDigitSeparator("123'0;", 3));

test/testsimplifytokens.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,6 @@ class TestSimplifyTokens : public TestFixture {
205205
// ticket #3140
206206
TEST_CASE(while0for);
207207

208-
TEST_CASE(duplicateDefinition); // ticket #3565
209-
210208
// remove "std::" on some standard functions
211209
TEST_CASE(removestd);
212210

@@ -4451,14 +4449,6 @@ class TestSimplifyTokens : public TestFixture {
44514449
ASSERT_EQUALS("void f ( ) { int i ; for ( i = 0 ; i < 0 ; ++ i ) { } return i ; }", tok("void f() { int i; for (i=0;i<0;++i){ dostuff(); } return i; }"));
44524450
}
44534451

4454-
void duplicateDefinition() { // #3565 - wrongly detects duplicate definition
4455-
Tokenizer tokenizer(&settings0, this);
4456-
std::istringstream istr("{ x ; return a not_eq x; }");
4457-
ASSERT(tokenizer.tokenize(istr, "test.c"));
4458-
Token *x_token = tokenizer.list.front()->tokAt(5);
4459-
ASSERT_EQUALS(false, tokenizer.duplicateDefinition(&x_token));
4460-
}
4461-
44624452
void removestd() {
44634453
ASSERT_EQUALS("; strcpy ( a , b ) ;", tok("; std::strcpy(a,b);"));
44644454
ASSERT_EQUALS("; strcat ( a , b ) ;", tok("; std::strcat(a,b);"));

0 commit comments

Comments
 (0)