Add TODO testcase for #5738. Refactoring: add some const
This commit is contained in:
parent
eb0db322eb
commit
e28e9be82f
|
@ -131,6 +131,9 @@ bool isSameExpression(const Token *tok1, const Token *tok2, const std::set<std::
|
||||||
}
|
}
|
||||||
if (tok1->type() == Token::eIncDecOp || tok1->isAssignmentOp())
|
if (tok1->type() == Token::eIncDecOp || tok1->isAssignmentOp())
|
||||||
return false;
|
return false;
|
||||||
|
// bailout when we see ({..})
|
||||||
|
if (tok1->str() == "{")
|
||||||
|
return false;
|
||||||
if (tok1->str() == "(" && tok1->previous() && !tok1->previous()->isName()) { // cast => assert that the casts are equal
|
if (tok1->str() == "(" && tok1->previous() && !tok1->previous()->isName()) { // cast => assert that the casts are equal
|
||||||
const Token *t1 = tok1->next();
|
const Token *t1 = tok1->next();
|
||||||
const Token *t2 = tok2->next();
|
const Token *t2 = tok2->next();
|
||||||
|
@ -141,9 +144,6 @@ bool isSameExpression(const Token *tok1, const Token *tok2, const std::set<std::
|
||||||
if (!t1 || !t2 || t1->str() != ")" || t2->str() != ")")
|
if (!t1 || !t2 || t1->str() != ")" || t2->str() != ")")
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// bailout when we see ({..})
|
|
||||||
if (tok1->str() == "{")
|
|
||||||
return false;
|
|
||||||
bool noncommuative_equals =
|
bool noncommuative_equals =
|
||||||
isSameExpression(tok1->astOperand1(), tok2->astOperand1(), constFunctions);
|
isSameExpression(tok1->astOperand1(), tok2->astOperand1(), constFunctions);
|
||||||
noncommuative_equals = noncommuative_equals &&
|
noncommuative_equals = noncommuative_equals &&
|
||||||
|
@ -152,7 +152,7 @@ bool isSameExpression(const Token *tok1, const Token *tok2, const std::set<std::
|
||||||
if (noncommuative_equals)
|
if (noncommuative_equals)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
bool commutative = tok1->astOperand1() && tok1->astOperand2() && Token::Match(tok1, "%or%|%oror%|+|*|&|&&|^|==|!=");
|
const bool commutative = tok1->astOperand1() && tok1->astOperand2() && Token::Match(tok1, "%or%|%oror%|+|*|&|&&|^|==|!=");
|
||||||
bool commuative_equals = commutative &&
|
bool commuative_equals = commutative &&
|
||||||
isSameExpression(tok1->astOperand2(), tok2->astOperand1(), constFunctions);
|
isSameExpression(tok1->astOperand2(), tok2->astOperand1(), constFunctions);
|
||||||
commuative_equals = commuative_equals &&
|
commuative_equals = commuative_equals &&
|
||||||
|
@ -1170,7 +1170,7 @@ void CheckOther::checkUnreachableCode()
|
||||||
|
|
||||||
// Statements follow directly, no line between them. (#3383)
|
// Statements follow directly, no line between them. (#3383)
|
||||||
// TODO: Try to find a better way to avoid false positives due to preprocessor configurations.
|
// TODO: Try to find a better way to avoid false positives due to preprocessor configurations.
|
||||||
bool inconclusive = secondBreak && (secondBreak->linenr() - 1 > secondBreak->previous()->linenr());
|
const bool inconclusive = secondBreak && (secondBreak->linenr() - 1 > secondBreak->previous()->linenr());
|
||||||
|
|
||||||
if (secondBreak && (printInconclusive || !inconclusive)) {
|
if (secondBreak && (printInconclusive || !inconclusive)) {
|
||||||
if (Token::Match(secondBreak, "continue|goto|throw") ||
|
if (Token::Match(secondBreak, "continue|goto|throw") ||
|
||||||
|
@ -1876,7 +1876,7 @@ void CheckOther::nanInArithmeticExpressionError(const Token *tok)
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
void CheckOther::checkMathFunctions()
|
void CheckOther::checkMathFunctions()
|
||||||
{
|
{
|
||||||
bool styleC99 = _settings->isEnabled("style") && _settings->standards.c != Standards::C89 && _settings->standards.cpp != Standards::CPP03;
|
const bool styleC99 = _settings->isEnabled("style") && _settings->standards.c != Standards::C89 && _settings->standards.cpp != Standards::CPP03;
|
||||||
const bool printWarnings = _settings->isEnabled("warning");
|
const bool printWarnings = _settings->isEnabled("warning");
|
||||||
|
|
||||||
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
|
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
|
||||||
|
@ -2577,7 +2577,7 @@ void CheckOther::checkIncompleteArrayFill()
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (MathLib::toLongNumber(tok->linkAt(1)->strAt(-1)) == var->dimension(0)) {
|
if (MathLib::toLongNumber(tok->linkAt(1)->strAt(-1)) == var->dimension(0)) {
|
||||||
unsigned int size = _tokenizer->sizeOfType(var->typeStartToken());
|
const unsigned int size = _tokenizer->sizeOfType(var->typeStartToken());
|
||||||
if ((size != 1 && size != 100 && size != 0) || var->isPointer()) {
|
if ((size != 1 && size != 100 && size != 0) || var->isPointer()) {
|
||||||
if (printWarning)
|
if (printWarning)
|
||||||
incompleteArrayFillError(tok, var->name(), tok->str(), false);
|
incompleteArrayFillError(tok, var->name(), tok->str(), false);
|
||||||
|
@ -2777,5 +2777,3 @@ void CheckOther::checkLibraryMatchFunctions()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -532,7 +532,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
|
||||||
|
|
||||||
// Remove comments..
|
// Remove comments..
|
||||||
if (str.compare(i, 2, "//") == 0) {
|
if (str.compare(i, 2, "//") == 0) {
|
||||||
std::size_t commentStart = i + 2;
|
const std::size_t commentStart = i + 2;
|
||||||
i = str.find('\n', i);
|
i = str.find('\n', i);
|
||||||
if (i == std::string::npos)
|
if (i == std::string::npos)
|
||||||
break;
|
break;
|
||||||
|
@ -557,7 +557,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
|
||||||
previous = '\n';
|
previous = '\n';
|
||||||
++lineno;
|
++lineno;
|
||||||
} else if (str.compare(i, 2, "/*") == 0) {
|
} else if (str.compare(i, 2, "/*") == 0) {
|
||||||
std::size_t commentStart = i + 2;
|
const std::size_t commentStart = i + 2;
|
||||||
unsigned char chPrev = 0;
|
unsigned char chPrev = 0;
|
||||||
++i;
|
++i;
|
||||||
while (i < str.length() && (chPrev != '*' || ch != '/')) {
|
while (i < str.length() && (chPrev != '*' || ch != '/')) {
|
||||||
|
@ -631,8 +631,8 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
|
||||||
// First check for a "fall through" comment match, but only
|
// First check for a "fall through" comment match, but only
|
||||||
// add a suppression if the next token is 'case' or 'default'
|
// add a suppression if the next token is 'case' or 'default'
|
||||||
if (detectFallThroughComments && fallThroughComment) {
|
if (detectFallThroughComments && fallThroughComment) {
|
||||||
std::string::size_type j = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz", i);
|
const std::string::size_type j = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz", i);
|
||||||
std::string tok = str.substr(i, j - i);
|
const std::string tok = str.substr(i, j - i);
|
||||||
if (tok == "case" || tok == "default")
|
if (tok == "case" || tok == "default")
|
||||||
suppressionIDs.push_back("switchCaseFallThrough");
|
suppressionIDs.push_back("switchCaseFallThrough");
|
||||||
fallThroughComment = false;
|
fallThroughComment = false;
|
||||||
|
@ -967,7 +967,7 @@ void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processe
|
||||||
processedFile = read(srcCodeStream, filename);
|
processedFile = read(srcCodeStream, filename);
|
||||||
|
|
||||||
if (_settings) {
|
if (_settings) {
|
||||||
for (std::list<std::string>::iterator it = _settings->userIncludes.begin();
|
for (std::list<std::string>::const_iterator it = _settings->userIncludes.begin();
|
||||||
it != _settings->userIncludes.end();
|
it != _settings->userIncludes.end();
|
||||||
++it) {
|
++it) {
|
||||||
std::string cur = *it;
|
std::string cur = *it;
|
||||||
|
@ -997,7 +997,7 @@ void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processe
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (std::vector<std::string>::iterator it = _settings->library.defines.begin();
|
for (std::vector<std::string>::const_iterator it = _settings->library.defines.begin();
|
||||||
it != _settings->library.defines.end();
|
it != _settings->library.defines.end();
|
||||||
++it) {
|
++it) {
|
||||||
forcedIncludes += *it;
|
forcedIncludes += *it;
|
||||||
|
|
|
@ -400,7 +400,7 @@ unsigned long long TokenList::calculateChecksum() const
|
||||||
{
|
{
|
||||||
unsigned long long checksum = 0;
|
unsigned long long checksum = 0;
|
||||||
for (const Token* tok = front(); tok; tok = tok->next()) {
|
for (const Token* tok = front(); tok; tok = tok->next()) {
|
||||||
unsigned int subchecksum1 = tok->flags() + tok->varId() + static_cast<unsigned int>(tok->type());
|
const unsigned int subchecksum1 = tok->flags() + tok->varId() + static_cast<unsigned int>(tok->type());
|
||||||
unsigned int subchecksum2 = 0;
|
unsigned int subchecksum2 = 0;
|
||||||
for (std::size_t i = 0; i < tok->str().size(); i++)
|
for (std::size_t i = 0; i < tok->str().size(); i++)
|
||||||
subchecksum2 += (unsigned int)tok->str()[i];
|
subchecksum2 += (unsigned int)tok->str()[i];
|
||||||
|
@ -618,8 +618,8 @@ static void compilePrecedence2(Token *&tok, AST_state& state)
|
||||||
} else if (tok->str() == "(" && (!iscast(tok) || Token::Match(tok->previous(), "if|while|for|switch|catch"))) {
|
} else if (tok->str() == "(" && (!iscast(tok) || Token::Match(tok->previous(), "if|while|for|switch|catch"))) {
|
||||||
Token* tok2 = tok;
|
Token* tok2 = tok;
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
bool opPrevTopSquare = !state.op.empty() && state.op.top() && state.op.top()->str() == "[";
|
const bool opPrevTopSquare = !state.op.empty() && state.op.top() && state.op.top()->str() == "[";
|
||||||
std::size_t oldOpSize = state.op.size();
|
const std::size_t oldOpSize = state.op.size();
|
||||||
compileExpression(tok, state);
|
compileExpression(tok, state);
|
||||||
tok = tok2;
|
tok = tok2;
|
||||||
if ((tok->previous() && tok->previous()->isName() && (tok->strAt(-1) != "return" && (!state.cpp || !Token::Match(tok->previous(), "throw|delete"))))
|
if ((tok->previous() && tok->previous()->isName() && (tok->strAt(-1) != "return" && (!state.cpp || !Token::Match(tok->previous(), "throw|delete"))))
|
||||||
|
|
|
@ -164,6 +164,7 @@ private:
|
||||||
TEST_CASE(checkIgnoredReturnValue);
|
TEST_CASE(checkIgnoredReturnValue);
|
||||||
|
|
||||||
TEST_CASE(redundantPointerOp);
|
TEST_CASE(redundantPointerOp);
|
||||||
|
TEST_CASE(test_isSameExpression);
|
||||||
}
|
}
|
||||||
|
|
||||||
void check(const char raw_code[], const char *filename = nullptr, bool experimental = false, bool inconclusive = true, bool runSimpleChecks=true, Settings* settings = 0, bool verify = true) {
|
void check(const char raw_code[], const char *filename = nullptr, bool experimental = false, bool inconclusive = true, bool runSimpleChecks=true, Settings* settings = 0, bool verify = true) {
|
||||||
|
@ -6150,6 +6151,14 @@ private:
|
||||||
" MUTEX_LOCK(*mut);\n"
|
" MUTEX_LOCK(*mut);\n"
|
||||||
"}\n", nullptr, false, true);
|
"}\n", nullptr, false, true);
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_isSameExpression() { // see #5738
|
||||||
|
check("bool isInUnoIncludeFile(StringRef name) {"
|
||||||
|
" return name.startswith(SRCDIR \"/com/\") || name.startswith(SRCDIR \"/uno/\");\n"
|
||||||
|
"};", "test.cpp", false, false);
|
||||||
|
TODO_ASSERT_EQUALS("", "[test.cpp:1] -> [test.cpp:1]: (style) Same expression on both sides of '||'.\n", errout.str());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue