From e28e9be82f76bad0df11eeb2b0323acb6de51104 Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Mon, 25 May 2015 08:20:14 +0200 Subject: [PATCH] Add TODO testcase for #5738. Refactoring: add some const --- lib/checkother.cpp | 16 +++++++--------- lib/preprocessor.cpp | 12 ++++++------ lib/tokenlist.cpp | 6 +++--- test/testother.cpp | 9 +++++++++ 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 0f8fe51dc..0826db497 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -131,6 +131,9 @@ bool isSameExpression(const Token *tok1, const Token *tok2, const std::settype() == 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::setstr() != ")" || 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::setastOperand1() && 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() } } } - - diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 86b6bea47..3c5898e7e 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -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::iterator it = _settings->userIncludes.begin(); + for (std::list::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::iterator it = _settings->library.defines.begin(); + for (std::vector::const_iterator it = _settings->library.defines.begin(); it != _settings->library.defines.end(); ++it) { forcedIncludes += *it; diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 09917e069..b5f1d1b79 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -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(tok->type()); + const unsigned int subchecksum1 = tok->flags() + tok->varId() + static_cast(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")))) diff --git a/test/testother.cpp b/test/testother.cpp index 54b5a25e8..2898f268e 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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()); } };