Add TODO testcase for #5738. Refactoring: add some const

This commit is contained in:
Alexander Mai 2015-05-25 08:20:14 +02:00
parent eb0db322eb
commit e28e9be82f
4 changed files with 25 additions and 18 deletions

View File

@ -131,6 +131,9 @@ bool isSameExpression(const Token *tok1, const Token *tok2, const std::set<std::
}
if (tok1->type() == Token::eIncDecOp || tok1->isAssignmentOp())
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
const Token *t1 = tok1->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() != ")")
return false;
}
// bailout when we see ({..})
if (tok1->str() == "{")
return false;
bool noncommuative_equals =
isSameExpression(tok1->astOperand1(), tok2->astOperand1(), constFunctions);
noncommuative_equals = noncommuative_equals &&
@ -152,7 +152,7 @@ bool isSameExpression(const Token *tok1, const Token *tok2, const std::set<std::
if (noncommuative_equals)
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 &&
isSameExpression(tok1->astOperand2(), tok2->astOperand1(), constFunctions);
commuative_equals = commuative_equals &&
@ -1170,7 +1170,7 @@ void CheckOther::checkUnreachableCode()
// Statements follow directly, no line between them. (#3383)
// 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 (Token::Match(secondBreak, "continue|goto|throw") ||
@ -1876,7 +1876,7 @@ void CheckOther::nanInArithmeticExpressionError(const Token *tok)
//---------------------------------------------------------------------------
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 SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
@ -2577,7 +2577,7 @@ void CheckOther::checkIncompleteArrayFill()
continue;
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 (printWarning)
incompleteArrayFillError(tok, var->name(), tok->str(), false);
@ -2777,5 +2777,3 @@ void CheckOther::checkLibraryMatchFunctions()
}
}
}

View File

@ -532,7 +532,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
// Remove comments..
if (str.compare(i, 2, "//") == 0) {
std::size_t commentStart = i + 2;
const std::size_t commentStart = i + 2;
i = str.find('\n', i);
if (i == std::string::npos)
break;
@ -557,7 +557,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
previous = '\n';
++lineno;
} else if (str.compare(i, 2, "/*") == 0) {
std::size_t commentStart = i + 2;
const std::size_t commentStart = i + 2;
unsigned char chPrev = 0;
++i;
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
// add a suppression if the next token is 'case' or 'default'
if (detectFallThroughComments && fallThroughComment) {
std::string::size_type j = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz", i);
std::string tok = str.substr(i, j - i);
const std::string::size_type j = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz", i);
const std::string tok = str.substr(i, j - i);
if (tok == "case" || tok == "default")
suppressionIDs.push_back("switchCaseFallThrough");
fallThroughComment = false;
@ -967,7 +967,7 @@ void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processe
processedFile = read(srcCodeStream, filename);
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) {
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) {
forcedIncludes += *it;

View File

@ -400,7 +400,7 @@ unsigned long long TokenList::calculateChecksum() const
{
unsigned long long checksum = 0;
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;
for (std::size_t i = 0; i < tok->str().size(); 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"))) {
Token* tok2 = tok;
tok = tok->next();
bool opPrevTopSquare = !state.op.empty() && state.op.top() && state.op.top()->str() == "[";
std::size_t oldOpSize = state.op.size();
const bool opPrevTopSquare = !state.op.empty() && state.op.top() && state.op.top()->str() == "[";
const std::size_t oldOpSize = state.op.size();
compileExpression(tok, state);
tok = tok2;
if ((tok->previous() && tok->previous()->isName() && (tok->strAt(-1) != "return" && (!state.cpp || !Token::Match(tok->previous(), "throw|delete"))))

View File

@ -164,6 +164,7 @@ private:
TEST_CASE(checkIgnoredReturnValue);
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) {
@ -6150,6 +6151,14 @@ private:
" MUTEX_LOCK(*mut);\n"
"}\n", nullptr, false, true);
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());
}
};