Skip to content

Commit db155a5

Browse files
committed
Tokenizer: remove simplifyTokenList2
1 parent b08aabe commit db155a5

9 files changed

Lines changed: 148 additions & 656 deletions

File tree

cli/main.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
* - Macros are expanded
5353
* -# Tokenize the file (see Tokenizer)
5454
* -# Run the runChecks of all check classes.
55-
* -# Simplify the tokenlist (Tokenizer::simplifyTokenList2)
56-
* -# Run the runSimplifiedChecks of all check classes
5755
*
5856
* When errors are found, they are reported back to the CppCheckExecutor through the ErrorLogger interface.
5957
*/

gui/test/benchmark/simple/benchmarksimple.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,4 @@ void BenchmarkSimple::tokenize()
4444
}
4545
}
4646

47-
void BenchmarkSimple::simplify()
48-
{
49-
QFile file(QString(SRCDIR) + "/../../data/benchmark/simple.cpp");
50-
QByteArray data = file.readAll();
51-
52-
Settings settings;
53-
settings.debugwarnings = true;
54-
55-
// tokenize..
56-
Tokenizer tokenizer(&settings, this);
57-
std::istringstream istr(data.constData());
58-
tokenizer.tokenize(istr, "test.cpp");
59-
QBENCHMARK {
60-
tokenizer.simplifyTokenList2();
61-
}
62-
}
63-
64-
void BenchmarkSimple::tokenizeAndSimplify()
65-
{
66-
QFile file(QString(SRCDIR) + "/../../data/benchmark/simple.cpp");
67-
QByteArray data = file.readAll();
68-
69-
Settings settings;
70-
settings.debugwarnings = true;
71-
72-
// tokenize..
73-
Tokenizer tokenizer(&settings, this);
74-
std::istringstream istr(data.constData());
75-
QBENCHMARK {
76-
tokenizer.tokenize(istr, "test.cpp");
77-
tokenizer.simplifyTokenList2();
78-
}
79-
}
80-
8147
QTEST_MAIN(BenchmarkSimple)

gui/test/benchmark/simple/benchmarksimple.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class BenchmarkSimple : public QObject, public ErrorLogger {
2828

2929
private slots:
3030
void tokenize();
31-
void simplify();
32-
void tokenizeAndSimplify();
3331

3432
private:
3533
// Empty implementations of ErrorLogger methods.

lib/cppcheck.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -893,11 +893,8 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
893893
checkUnusedFunctions.parseTokens(tokenizer, filename.c_str(), &mSettings);
894894

895895
// handling of "simple" rules has been removed.
896-
if (mSimplify && hasRule("simple")) {
897-
// FIXME Remove this function
898-
tokenizer.simplifyTokenList2();
896+
if (mSimplify && hasRule("simple"))
899897
throw InternalError(nullptr, "Handling of \"simple\" rules has been removed in Cppcheck. Use --addon instead.");
900-
}
901898

902899
} catch (const simplecpp::Output &o) {
903900
// #error etc during preprocessing

lib/token.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2411,10 +2411,11 @@ TokenImpl::~TokenImpl()
24112411
delete mValueType;
24122412
delete mValues;
24132413

2414-
if (mTemplateSimplifierPointers)
2414+
if (mTemplateSimplifierPointers) {
24152415
for (auto *templateSimplifierPointer : *mTemplateSimplifierPointers) {
24162416
templateSimplifierPointer->token(nullptr);
24172417
}
2418+
}
24182419
delete mTemplateSimplifierPointers;
24192420

24202421
while (mCppcheckAttributes) {

lib/token.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,10 +1395,6 @@ class CPPCHECKLIB Token {
13951395
*/
13961396
bool isCalculation() const;
13971397

1398-
void clearAst() {
1399-
mImpl->mAstOperand1 = mImpl->mAstOperand2 = mImpl->mAstParent = nullptr;
1400-
}
1401-
14021398
void clearValueFlow() {
14031399
delete mImpl->mValues;
14041400
mImpl->mValues = nullptr;

lib/tokenize.cpp

Lines changed: 1 addition & 246 deletions
Original file line numberDiff line numberDiff line change
@@ -5150,38 +5150,6 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
51505150

51515151
return true;
51525152
}
5153-
5154-
bool Tokenizer::simplifyTokenList2()
5155-
{
5156-
// clear the _functionList so it can't contain dead pointers
5157-
deleteSymbolDatabase();
5158-
5159-
// Clear AST,ValueFlow. These will be created again at the end of this function.
5160-
for (Token *tok = list.front(); tok; tok = tok->next()) {
5161-
tok->clearAst();
5162-
tok->clearValueFlow();
5163-
}
5164-
5165-
if (Settings::terminated())
5166-
return false;
5167-
5168-
5169-
bool modified = true;
5170-
while (modified) {
5171-
if (Settings::terminated())
5172-
return false;
5173-
5174-
modified = false;
5175-
modified |= simplifyConstTernaryOp();
5176-
validate();
5177-
}
5178-
5179-
simplifyComma();
5180-
5181-
removeRedundantSemicolons();
5182-
5183-
return true;
5184-
}
51855153
//---------------------------------------------------------------------------
51865154

51875155
void Tokenizer::printDebugOutput(int simplification) const
@@ -5873,87 +5841,6 @@ Token *Tokenizer::simplifyAddBracesPair(Token *tok, bool commandWithCondition)
58735841
return tokBracesEnd;
58745842
}
58755843

5876-
bool Tokenizer::simplifyConstTernaryOp()
5877-
{
5878-
bool ret = false;
5879-
const Token *templateParameterEnd = nullptr; // The end of the current template parameter list, if any
5880-
for (Token *tok = list.front(); tok; tok = tok->next()) {
5881-
if (tok->str() == "<" && TemplateSimplifier::templateParameters(tok))
5882-
templateParameterEnd = tok->findClosingBracket();
5883-
if (tok == templateParameterEnd)
5884-
templateParameterEnd = nullptr; // End of the current template parameter list
5885-
if (tok->str() != "?")
5886-
continue;
5887-
5888-
if (!Token::Match(tok->tokAt(-2), "<|=|,|(|[|{|}|;|case|return %bool%|%num%") &&
5889-
!Token::Match(tok->tokAt(-4), "<|=|,|(|[|{|}|;|case|return ( %bool%|%num% )"))
5890-
continue;
5891-
5892-
const int offset = (tok->previous()->str() == ")") ? 2 : 1;
5893-
5894-
if (tok->strAt(-2*offset) == "<") {
5895-
if (isC() || !TemplateSimplifier::templateParameters(tok->tokAt(-2*offset)))
5896-
continue; // '<' is less than; the condition is not a constant
5897-
}
5898-
5899-
// Find the token ":" then go to the next token
5900-
Token *colon = skipTernaryOp(tok);
5901-
if (!colon || colon->previous()->str() != ":" || !colon->next())
5902-
continue;
5903-
5904-
//handle the GNU extension: "x ? : y" <-> "x ? x : y"
5905-
if (colon->previous() == tok->next())
5906-
tok->insertToken(tok->strAt(-offset));
5907-
5908-
// go back before the condition, if possible
5909-
tok = tok->tokAt(-2);
5910-
if (offset == 2) {
5911-
// go further back before the "("
5912-
tok = tok->tokAt(-2);
5913-
//simplify the parentheses
5914-
tok->deleteNext();
5915-
tok->next()->deleteNext();
5916-
}
5917-
5918-
if (Token::Match(tok->next(), "false|0")) {
5919-
// Use code after colon, remove code before it.
5920-
Token::eraseTokens(tok, colon);
5921-
5922-
tok = tok->next();
5923-
ret = true;
5924-
}
5925-
5926-
// The condition is true. Delete the operator after the ":"..
5927-
else {
5928-
// delete the condition token and the "?"
5929-
tok->deleteNext(2);
5930-
5931-
int ternaryOplevel = 0;
5932-
for (const Token *endTok = colon; endTok; endTok = endTok->next()) {
5933-
if (Token::Match(endTok, "(|[|{"))
5934-
endTok = endTok->link();
5935-
else if (endTok->str() == "<" && (endTok->strAt(1) == ">" || TemplateSimplifier::templateParameters(endTok)))
5936-
endTok = endTok->findClosingBracket();
5937-
else if (endTok->str() == "?")
5938-
++ternaryOplevel;
5939-
else if (Token::Match(endTok, ")|}|]|;|,|:|>")) {
5940-
if (endTok->str() == ":" && ternaryOplevel)
5941-
--ternaryOplevel;
5942-
else if (endTok->str() == ">" && !templateParameterEnd)
5943-
;
5944-
else {
5945-
Token::eraseTokens(colon->tokAt(-2), endTok);
5946-
ret = true;
5947-
break;
5948-
}
5949-
}
5950-
}
5951-
}
5952-
}
5953-
return ret;
5954-
}
5955-
5956-
59575844
void Tokenizer::simplifyFunctionParameters()
59585845
{
59595846
for (Token *tok = list.front(); tok; tok = tok->next()) {
@@ -7352,133 +7239,7 @@ bool Tokenizer::isOneNumber(const std::string &s)
73527239
return false;
73537240
return isNumberOneOf(s, 1L, "1.0");
73547241
}
7355-
7356-
void Tokenizer::simplifyComma()
7357-
{
7358-
bool inReturn = false;
7359-
7360-
for (Token *tok = list.front(); tok; tok = tok->next()) {
7361-
7362-
// skip enums
7363-
if (Token::Match(tok, "enum class|struct| %name%| :|{")) {
7364-
skipEnumBody(&tok);
7365-
}
7366-
if (!tok)
7367-
syntaxError(nullptr); // invalid code like in #4195
7368-
7369-
if (Token::Match(tok, "(|[") || Token::Match(tok->previous(), "%name%|= {")) {
7370-
tok = tok->link();
7371-
continue;
7372-
}
7373-
7374-
if (Token::simpleMatch(tok, "= (") && Token::simpleMatch(tok->linkAt(1), ") {")) {
7375-
tok = tok->linkAt(1)->linkAt(1);
7376-
continue;
7377-
}
7378-
7379-
// Skip unhandled template specifiers..
7380-
if (tok->link() && tok->str() == "<")
7381-
tok = tok->link();
7382-
7383-
if (tok->str() == "return" && Token::Match(tok->previous(), "[;{}]"))
7384-
inReturn = true;
7385-
7386-
if (inReturn && Token::Match(tok, "[;{}?:]"))
7387-
inReturn = false;
7388-
7389-
if (!tok->next() || tok->str() != ",")
7390-
continue;
7391-
7392-
// We must not accept just any keyword, e.g. accepting int
7393-
// would cause function parameters to corrupt.
7394-
if (isCPP() && tok->strAt(1) == "delete") {
7395-
// Handle "delete a, delete b;"
7396-
tok->str(";");
7397-
}
7398-
7399-
if (isCPP() && Token::Match(tok->tokAt(-2), "delete %name% , %name% ;") &&
7400-
tok->next()->varId() != 0) {
7401-
// Handle "delete a, b;" - convert to delete a; b;
7402-
tok->str(";");
7403-
} else if (!inReturn && tok->tokAt(-2)) {
7404-
bool replace = false;
7405-
for (Token *tok2 = tok->previous(); tok2; tok2 = tok2->previous()) {
7406-
if (tok2->str() == "=") {
7407-
// Handle "a = 0, b = 0;"
7408-
replace = true;
7409-
} else if (isCPP() && (Token::Match(tok2, "delete %name%") ||
7410-
Token::Match(tok2, "delete [ ] %name%"))) {
7411-
// Handle "delete a, a = 0;"
7412-
replace = true;
7413-
} else if (Token::Match(tok2, "[?:;,{}()]")) {
7414-
if (replace && Token::Match(tok2, "[;{}]"))
7415-
tok->str(";");
7416-
break;
7417-
}
7418-
}
7419-
}
7420-
7421-
// find token where return ends and also count commas
7422-
if (inReturn) {
7423-
Token *startFrom = nullptr; // "[;{}]" token before "return"
7424-
Token *endAt = nullptr; // first ";" token after "[;{}] return"
7425-
7426-
// find "; return" pattern before comma
7427-
for (Token *tok2 = tok->previous(); tok2; tok2 = tok2->previous()) {
7428-
if (tok2->str() == "return") {
7429-
startFrom = tok2->previous();
7430-
break;
7431-
}
7432-
}
7433-
if (!startFrom)
7434-
// to be very sure...
7435-
return;
7436-
int commaCounter = 0;
7437-
for (Token *tok2 = startFrom->next(); tok2; tok2 = tok2->next()) {
7438-
if (tok2->str() == ";") {
7439-
endAt = tok2;
7440-
break;
7441-
7442-
} else if (Token::Match(tok2, "(|[") ||
7443-
(tok2->str() == "{" && tok2->previous() && tok2->previous()->str() == "=")) {
7444-
tok2 = tok2->link();
7445-
7446-
} else if (tok2->str() == ",") {
7447-
++commaCounter;
7448-
}
7449-
}
7450-
7451-
if (!endAt)
7452-
//probably a syntax error
7453-
return;
7454-
7455-
if (commaCounter) {
7456-
// change tokens:
7457-
// "; return a ( ) , b ( ) , c ;"
7458-
// to
7459-
// "; a ( ) ; b ( ) ; return c ;"
7460-
7461-
// remove "return"
7462-
startFrom->deleteNext();
7463-
for (Token *tok2 = startFrom->next(); tok2 != endAt; tok2 = tok2->next()) {
7464-
if (Token::Match(tok2, "(|[") ||
7465-
(tok2->str() == "{" && tok2->previous() && tok2->previous()->str() == "=")) {
7466-
tok2 = tok2->link();
7467-
7468-
} else if (tok2->str() == ",") {
7469-
tok2->str(";");
7470-
--commaCounter;
7471-
if (commaCounter == 0) {
7472-
tok2->insertToken("return");
7473-
}
7474-
}
7475-
}
7476-
tok = endAt;
7477-
}
7478-
}
7479-
}
7480-
}
7481-
7242+
// ------------------------------------------------------------------------
74827243
void Tokenizer::checkConfiguration() const
74837244
{
74847245
if (!mSettings->checkConfiguration)
@@ -9352,12 +9113,6 @@ void Tokenizer::createSymbolDatabase()
93529113
mSymbolDatabase->validate();
93539114
}
93549115

9355-
void Tokenizer::deleteSymbolDatabase()
9356-
{
9357-
delete mSymbolDatabase;
9358-
mSymbolDatabase = nullptr;
9359-
}
9360-
93619116
bool Tokenizer::operatorEnd(const Token * tok) const
93629117
{
93639118
if (tok && tok->str() == ")") {

0 commit comments

Comments
 (0)