removed or annotated some code which is only used in test code (#3656)

This commit is contained in:
Oliver Stöneberg 2022-01-03 12:40:20 +01:00 committed by GitHub
parent dfd22919bc
commit 6739995e79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 5 additions and 224 deletions

View File

@ -73,7 +73,7 @@ public:
* @brief method that is run in a thread
*
*/
void run();
void run() override;
void stop();

View File

@ -68,7 +68,7 @@ public:
Error load(const char exename[], const char path[]);
Error load(const tinyxml2::XMLDocument &doc);
/** this is primarily meant for unit tests. it only returns true/false */
/** this is used for unit tests */
bool loadxmldata(const char xmldata[], std::size_t len);
struct AllocFunc {

View File

@ -357,105 +357,6 @@ unsigned int MathLib::encodeMultiChar(const std::string& str)
return retval;
}
std::string MathLib::normalizeCharacterLiteral(const std::string& iLiteral)
{
std::string normalizedLiteral;
const std::string::size_type iLiteralLen = iLiteral.size();
for (std::string::size_type idx = 0; idx < iLiteralLen; ++idx) {
if (iLiteral[idx] != '\\') {
normalizedLiteral.push_back(iLiteral[idx]);
continue;
}
++idx;
if (idx == iLiteralLen) {
throw InternalError(nullptr, "Internal Error. MathLib::normalizeCharacterLiteral: Unhandled char constant '" + iLiteral + "'.");
}
switch (iLiteral[idx]) {
case 'x':
// Hexa-decimal number: skip \x and interpret the next two characters
{
if (++idx == iLiteralLen)
throw InternalError(nullptr, "Internal Error. MathLib::normalizeCharacterLiteral: Unhandled char constant '" + iLiteral + "'.");
std::string tempBuf;
tempBuf.push_back(iLiteral[idx]);
if (++idx != iLiteralLen)
tempBuf.push_back(iLiteral[idx]);
normalizedLiteral.push_back(static_cast<char>(MathLib::toULongNumber("0x" + tempBuf)));
continue;
}
case 'u':
case 'U':
// Unicode string; just skip the \u or \U
if (idx + 1 == iLiteralLen)
throw InternalError(nullptr, "Internal Error. MathLib::characterLiteralToLongNumber: Unhandled char constant '" + iLiteral + "'.");
continue;
}
// Single digit octal number
if (1 == iLiteralLen - idx) {
switch (iLiteral[idx]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
normalizedLiteral.push_back(iLiteral[idx]-'0');
break;
case 'a':
normalizedLiteral.push_back('\a');
break;
case 'b':
normalizedLiteral.push_back('\b');
break;
case 'e':
normalizedLiteral.push_back(0x1B); // clang, gcc, tcc interpnormalizedLiteral this as 0x1B - escape character
break;
case 'f':
normalizedLiteral.push_back('\f');
break;
case 'n':
normalizedLiteral.push_back('\n');
break;
case 'r':
normalizedLiteral.push_back('\r');
break;
case 't':
normalizedLiteral.push_back('\t');
break;
case 'v':
normalizedLiteral.push_back('\v');
break;
case '\\':
case '\?':
case '\'':
case '\"':
normalizedLiteral.push_back(iLiteral[idx]);
break;
default:
throw InternalError(nullptr, "Internal Error. MathLib::normalizeCharacterLiteral: Unhandled char constant '" + iLiteral + "'.");
}
continue;
}
// 2-3 digit octal number
if (!MathLib::isOctalDigit(iLiteral[idx]))
throw InternalError(nullptr, "Internal Error. MathLib::normalizeCharacterLiteral: Unhandled char constant '" + iLiteral + "'.");
std::string tempBuf;
tempBuf.push_back(iLiteral[idx]);
++idx;
if (MathLib::isOctalDigit(iLiteral[idx])) {
tempBuf.push_back(iLiteral[idx]);
++idx;
if (MathLib::isOctalDigit(iLiteral[idx])) {
tempBuf.push_back(iLiteral[idx]);
}
}
normalizedLiteral.push_back(static_cast<char>(MathLib::toLongNumber("0" + tempBuf)));
}
return normalizedLiteral;
}
MathLib::bigint MathLib::toLongNumber(const std::string & str)
{
// hexadecimal numbers:

View File

@ -93,6 +93,8 @@ public:
static std::string getSuffix(const std::string& value);
/**
* Only used in unit tests
*
* \param[in] str string
* \param[in] supportMicrosoftExtensions support Microsoft extension: i64
* \return true if str is a non-empty valid integer suffix
@ -133,13 +135,6 @@ public:
* \return Whether iCode[iPos] is a C++14 digit separator
*/
static bool isDigitSeparator(const std::string& iCode, std::string::size_type iPos);
private:
/*
* \param iLiteral A character literal
* \return The equivalent character literal with all escapes interpreted
*/
static std::string normalizeCharacterLiteral(const std::string& iLiteral);
};
MathLib::value operator+(const MathLib::value &v1, const MathLib::value &v2);

View File

@ -1375,6 +1375,7 @@ public:
*/
const Function *findFunction(const Token *tok) const;
/** For unit testing only */
const Scope *findScopeByName(const std::string& name) const;
const Type* findType(const Token *startTok, const Scope *startScope) const;

View File

@ -9187,85 +9187,6 @@ void Tokenizer::simplifyNestedStrcat()
}
}
// Check if this statement is a duplicate definition. A duplicate
// definition will hide the enumerator within it's scope so just
// skip the entire scope of the duplicate.
bool Tokenizer::duplicateDefinition(Token ** tokPtr)
{
// check for an end of definition
const Token * tok = *tokPtr;
if (tok && Token::Match(tok->next(), ";|,|[|=|)|>")) {
const Token * end = tok->next();
if (end->str() == "[") {
end = end->link()->next();
} else if (end->str() == ",") {
// check for function argument
if (Token::Match(tok->previous(), "(|,"))
return false;
// find end of definition
int level = 0;
while (end->next() && (!Token::Match(end->next(), ";|)|>") ||
(end->next()->str() == ")" && level == 0))) {
if (end->next()->str() == "(")
++level;
else if (end->next()->str() == ")")
--level;
end = end->next();
}
} else if (end->str() == ")") {
// check for function argument
if (tok->previous()->str() == ",")
return false;
}
if (end) {
if (Token::simpleMatch(end, ") {")) { // function parameter ?
// make sure it's not a conditional
if (Token::Match(end->link()->previous(), "if|for|while|switch|BOOST_FOREACH") || Token::Match(end->link()->tokAt(-2), ":|,"))
return false;
// look backwards
if (tok->previous()->str() == "enum" ||
(Token::Match(tok->previous(), "%type%") &&
tok->previous()->str() != "return") ||
Token::Match(tok->tokAt(-2), "%type% &|*")) {
// duplicate definition so skip entire function
*tokPtr = end->next()->link();
return true;
}
} else if (end->str() == ">") { // template parameter ?
// look backwards
if (tok->previous()->str() == "enum" ||
(Token::Match(tok->previous(), "%type%") &&
tok->previous()->str() != "return")) {
// duplicate definition so skip entire template
while (end && end->str() != "{")
end = end->next();
if (end) {
*tokPtr = end->link();
return true;
}
}
} else {
if (Token::Match(tok->previous(), "enum|,"))
return true;
else if (Token::Match(tok->previous(), "%type%")) {
// look backwards
const Token *back = tok;
while (back && back->isName())
back = back->previous();
if (!back || (Token::Match(back, "[(,;{}]") && !Token::Match(back->next(),"return|throw")))
return true;
}
}
}
}
return false;
}
static const std::set<std::string> stdFunctionsPresentInC = {
"strcat",
"strcpy",

View File

@ -810,11 +810,6 @@ private:
* */
void prepareTernaryOpForAST();
/**
* check for duplicate enum definition
*/
static bool duplicateDefinition(Token **tokPtr);
/**
* report error message
*/

View File

@ -62,7 +62,6 @@ private:
TEST_CASE(tan);
TEST_CASE(abs);
TEST_CASE(toString);
TEST_CASE(characterLiteralsNormalization);
TEST_CASE(CPP14DigitSeparators);
}
@ -1246,27 +1245,6 @@ private:
ASSERT_EQUALS("-0", MathLib::toString(-0.0L));
}
void characterLiteralsNormalization() const {
// `A` is 0x41 and 0101
ASSERT_EQUALS("A", MathLib::normalizeCharacterLiteral("\\x41"));
ASSERT_EQUALS("A", MathLib::normalizeCharacterLiteral("\\101"));
// Hexa and octal numbers should not only be interpreted in byte 1
ASSERT_EQUALS("TESTATEST", MathLib::normalizeCharacterLiteral("TEST\\x41TEST"));
ASSERT_EQUALS("TESTATEST", MathLib::normalizeCharacterLiteral("TEST\\101TEST"));
ASSERT_EQUALS("TESTTESTA", MathLib::normalizeCharacterLiteral("TESTTEST\\x41"));
ASSERT_EQUALS("TESTTESTA", MathLib::normalizeCharacterLiteral("TESTTEST\\101"));
// Single escape sequences
ASSERT_EQUALS("\?", MathLib::normalizeCharacterLiteral("\\?"));
ASSERT_EQUALS("\'", MathLib::normalizeCharacterLiteral("\\'"));
// Incomplete hexa and octal sequences
ASSERT_THROW(MathLib::normalizeCharacterLiteral("\\"), InternalError);
ASSERT_THROW(MathLib::normalizeCharacterLiteral("\\x"), InternalError);
// No octal digit in an octal sequence
ASSERT_THROW(MathLib::normalizeCharacterLiteral("\\9"), InternalError);
// Unsupported single escape sequence
ASSERT_THROW(MathLib::normalizeCharacterLiteral("\\c"), InternalError);
}
void CPP14DigitSeparators() const { // Ticket #7137, #7565
ASSERT(MathLib::isDigitSeparator("'", 0) == false);
ASSERT(MathLib::isDigitSeparator("123'0;", 3));

View File

@ -205,8 +205,6 @@ private:
// ticket #3140
TEST_CASE(while0for);
TEST_CASE(duplicateDefinition); // ticket #3565
// remove "std::" on some standard functions
TEST_CASE(removestd);
@ -4451,14 +4449,6 @@ private:
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; }"));
}
void duplicateDefinition() { // #3565 - wrongly detects duplicate definition
Tokenizer tokenizer(&settings0, this);
std::istringstream istr("{ x ; return a not_eq x; }");
ASSERT(tokenizer.tokenize(istr, "test.c"));
Token *x_token = tokenizer.list.front()->tokAt(5);
ASSERT_EQUALS(false, tokenizer.duplicateDefinition(&x_token));
}
void removestd() {
ASSERT_EQUALS("; strcpy ( a , b ) ;", tok("; std::strcpy(a,b);"));
ASSERT_EQUALS("; strcat ( a , b ) ;", tok("; std::strcat(a,b);"));