Cleanup: Removed Tokenizer::simplifyTokenList2. As a side-effect, rules for "simple" token list are now executed on normal token list.

This commit is contained in:
PKEuS 2020-05-20 21:06:39 +02:00
parent eb9576cdf8
commit 187cde183d
12 changed files with 350 additions and 9069 deletions

View File

@ -258,10 +258,6 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
else if (std::strcmp(argv[i], "--debug-bug-hunting") == 0) else if (std::strcmp(argv[i], "--debug-bug-hunting") == 0)
mSettings->debugBugHunting = true; mSettings->debugBugHunting = true;
// Flag used for various purposes during debugging
else if (std::strcmp(argv[i], "--debug-simplified") == 0)
mSettings->debugSimplified = true;
// Show template information // Show template information
else if (std::strcmp(argv[i], "--debug-template") == 0) else if (std::strcmp(argv[i], "--debug-template") == 0)
mSettings->debugtemplate = true; mSettings->debugtemplate = true;

View File

@ -249,7 +249,6 @@ CppCheck::CppCheck(ErrorLogger &errorLogger,
, mSuppressInternalErrorFound(false) , mSuppressInternalErrorFound(false)
, mUseGlobalSuppressions(useGlobalSuppressions) , mUseGlobalSuppressions(useGlobalSuppressions)
, mTooManyConfigs(false) , mTooManyConfigs(false)
, mSimplify(true)
, mExecuteCommand(executeCommand) , mExecuteCommand(executeCommand)
{ {
} }
@ -385,7 +384,7 @@ unsigned int CppCheck::check(const std::string &path)
clangimport::parseClangAstDump(&tokenizer, ast); clangimport::parseClangAstDump(&tokenizer, ast);
ValueFlow::setValues(&tokenizer.list, const_cast<SymbolDatabase *>(tokenizer.getSymbolDatabase()), this, &mSettings); ValueFlow::setValues(&tokenizer.list, const_cast<SymbolDatabase *>(tokenizer.getSymbolDatabase()), this, &mSettings);
if (mSettings.debugnormal) if (mSettings.debugnormal)
tokenizer.printDebugOutput(1); tokenizer.printDebugOutput();
checkNormalTokens(tokenizer); checkNormalTokens(tokenizer);
return mExitCode; return mExitCode;
} }
@ -756,18 +755,8 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
checkUnusedFunctions.parseTokens(mTokenizer, filename.c_str(), &mSettings); checkUnusedFunctions.parseTokens(mTokenizer, filename.c_str(), &mSettings);
// simplify more if required, skip rest of iteration if failed // simplify more if required, skip rest of iteration if failed
if (mSimplify && hasRule("simple")) { if (hasRule("simple")) {
std::cout << "Handling of \"simple\" rules is deprecated and will be removed in Cppcheck 2.5." << std::endl; std::cout << "Handling of \"simple\" rules was removed in Cppcheck 2.1. Rule is executed on normal token list instead." << std::endl;
// if further simplification fails then skip rest of iteration
Timer timer3("Tokenizer::simplifyTokenList2", mSettings.showtime, &s_timerResults);
result = mTokenizer.simplifyTokenList2();
timer3.stop();
if (!result)
continue;
if (!Settings::terminated())
executeRules("simple", mTokenizer);
} }
} catch (const simplecpp::Output &o) { } catch (const simplecpp::Output &o) {
@ -971,6 +960,7 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
} }
executeRules("normal", tokenizer); executeRules("normal", tokenizer);
executeRules("simple", tokenizer);
} }
} }

View File

@ -128,10 +128,6 @@ public:
void tooManyConfigsError(const std::string &file, const std::size_t numberOfConfigurations); void tooManyConfigsError(const std::string &file, const std::size_t numberOfConfigurations);
void purgedConfigurationMessage(const std::string &file, const std::string& configuration); void purgedConfigurationMessage(const std::string &file, const std::string& configuration);
void dontSimplify() {
mSimplify = false;
}
/** Analyse whole program, run this after all TUs has been scanned. /** Analyse whole program, run this after all TUs has been scanned.
* This is deprecated and the plan is to remove this when * This is deprecated and the plan is to remove this when
* .analyzeinfo is good enough. * .analyzeinfo is good enough.
@ -227,9 +223,6 @@ private:
/** Are there too many configs? */ /** Are there too many configs? */
bool mTooManyConfigs; bool mTooManyConfigs;
/** Simplify code? true by default */
bool mSimplify;
/** File info used for whole program analysis */ /** File info used for whole program analysis */
std::list<Check::FileInfo*> mFileInfo; std::list<Check::FileInfo*> mFileInfo;

View File

@ -41,7 +41,6 @@ Settings::Settings()
daca(false), daca(false),
debugBugHunting(false), debugBugHunting(false),
debugnormal(false), debugnormal(false),
debugSimplified(false),
debugtemplate(false), debugtemplate(false),
debugwarnings(false), debugwarnings(false),
dump(false), dump(false),

View File

@ -130,9 +130,6 @@ public:
/** @brief Is --debug-normal given? */ /** @brief Is --debug-normal given? */
bool debugnormal; bool debugnormal;
/** @brief Is --debug-simplified given? */
bool debugSimplified;
/** @brief Is --debug-template given? */ /** @brief Is --debug-template given? */
bool debugtemplate; bool debugtemplate;

File diff suppressed because it is too large Load Diff

View File

@ -155,14 +155,6 @@ public:
*/ */
bool simplifyTokenList1(const char FileName[]); bool simplifyTokenList1(const char FileName[]);
/**
* Most aggressive simplification of tokenlist
*
* @return false if there is an error that requires aborting
* the checking of this file.
*/
bool simplifyTokenList2();
/** /**
* If --check-headers=no has been given; then remove unneeded code in headers. * If --check-headers=no has been given; then remove unneeded code in headers.
* - All executable code. * - All executable code.
@ -208,10 +200,6 @@ public:
*/ */
bool isFunctionParameterPassedByValue(const Token *fpar) const; bool isFunctionParameterPassedByValue(const Token *fpar) const;
/** Simplify assignment in function call "f(x=g());" => "x=g();f(x);"
*/
void simplifyAssignmentInFunctionCall();
/** Simplify assignment where rhs is a block : "x=({123;});" => "{x=123;}" */ /** Simplify assignment where rhs is a block : "x=({123;});" => "{x=123;}" */
void simplifyAssignmentBlock(); void simplifyAssignmentBlock();
@ -222,19 +210,6 @@ public:
*/ */
bool simplifyCalculations(); bool simplifyCalculations();
/**
* Simplify dereferencing a pointer offset by a number:
* "*(ptr + num)" => "ptr[num]"
* "*(ptr - num)" => "ptr[-num]"
*/
void simplifyOffsetPointerDereference();
/**
* Simplify referencing a pointer offset:
* "Replace "&str[num]" => "(str + num)"
*/
void simplifyOffsetPointerReference();
/** Insert array size where it isn't given */ /** Insert array size where it isn't given */
void arraySize(); void arraySize();
@ -260,25 +235,9 @@ public:
/** Remove unknown macro in variable declarations: PROGMEM char x; */ /** Remove unknown macro in variable declarations: PROGMEM char x; */
void removeMacroInVarDecl(); void removeMacroInVarDecl();
/** Remove redundant assignment */
void removeRedundantAssignment();
/** Simplifies some realloc usage like
* 'x = realloc (0, n);' => 'x = malloc(n);'
* 'x = realloc (y, 0);' => 'x = 0; free(y);'
*/
void simplifyRealloc();
/** Add parentheses for sizeof: sizeof x => sizeof(x) */ /** Add parentheses for sizeof: sizeof x => sizeof(x) */
void sizeofAddParentheses(); void sizeofAddParentheses();
/**
* Replace sizeof() to appropriate size.
* @return true if modifications to token-list are done.
* false if no modifications are done.
*/
bool simplifySizeof();
/** /**
* Simplify variable declarations (split up) * Simplify variable declarations (split up)
* \param only_k_r_fpar Only simplify K&R function parameters * \param only_k_r_fpar Only simplify K&R function parameters
@ -293,20 +252,6 @@ public:
void simplifyInitVar(); void simplifyInitVar();
Token * initVar(Token * tok); Token * initVar(Token * tok);
/**
* Simplify easy constant '?:' operation
* Example: 0 ? (2/0) : 0 => 0
* @return true if something is modified
* false if nothing is done.
*/
bool simplifyConstTernaryOp();
/**
* Simplify compound assignments
* Example: ";a+=b;" => ";a=a+b;"
*/
void simplifyCompoundAssignment();
/** /**
* Simplify the location of "static" and "const" qualifiers in * Simplify the location of "static" and "const" qualifiers in
* a variable declaration or definition. * a variable declaration or definition.
@ -315,14 +260,6 @@ public:
*/ */
void simplifyStaticConst(); void simplifyStaticConst();
/**
* Simplify assignments in "if" and "while" conditions
* Example: "if(a=b);" => "a=b;if(a);"
* Example: "while(a=b) { f(a); }" => "a = b; while(a){ f(a); a = b; }"
* Example: "do { f(a); } while(a=b);" => "do { f(a); a = b; } while(a);"
*/
void simplifyIfAndWhileAssign();
/** /**
* Simplify multiple assignments. * Simplify multiple assignments.
* Example: "a = b = c = 0;" => "a = 0; b = 0; c = 0;" * Example: "a = b = c = 0;" => "a = 0; b = 0; c = 0;"
@ -338,14 +275,6 @@ public:
*/ */
bool simplifyCAlternativeTokens(); bool simplifyCAlternativeTokens();
/**
* Simplify comma into a semicolon when possible:
* - "delete a, delete b" => "delete a; delete b;"
* - "a = 0, b = 0;" => "a = 0; b = 0;"
* - "return a(), b;" => "a(); return b;"
*/
void simplifyComma();
/** Add braces to an if-block, for-block, etc. /** Add braces to an if-block, for-block, etc.
* @return true if no syntax errors * @return true if no syntax errors
*/ */
@ -388,82 +317,18 @@ public:
*/ */
bool simplifyUsing(); bool simplifyUsing();
/**
* Simplify casts
*/
void simplifyCasts();
/**
* Change (multiple) arrays to (multiple) pointers.
*/
void simplifyUndefinedSizeArray();
/**
* A simplify function that replaces a variable with its value in cases
* when the value is known. e.g. "x=10; if(x)" => "x=10;if(10)"
*
* @return true if modifications to token-list are done.
* false if no modifications are done.
*/
bool simplifyKnownVariables();
/**
* Utility function for simplifyKnownVariables. Get data about an
* assigned variable.
*/
static bool simplifyKnownVariablesGetData(nonneg int varid, Token **_tok2, Token **_tok3, std::string &value, nonneg int &valueVarId, bool &valueIsPointer, bool floatvar);
/**
* utility function for simplifyKnownVariables. Perform simplification
* of a given variable
*/
bool simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, nonneg int varid, const std::string &structname, std::string &value, nonneg int valueVarId, bool valueIsPointer, const Token * const valueToken, int indentlevel) const;
/** Simplify useless C++ empty namespaces, like: 'namespace %name% { }'*/ /** Simplify useless C++ empty namespaces, like: 'namespace %name% { }'*/
void simplifyEmptyNamespaces(); void simplifyEmptyNamespaces();
/** Simplify redundant code placed after control flow statements :
* 'return', 'throw', 'goto', 'break' and 'continue'
*/
void simplifyFlowControl();
/** Expand nested strcat() calls. */
void simplifyNestedStrcat();
/** Simplify "if else" */ /** Simplify "if else" */
void elseif(); void elseif();
/** Simplify conditions
* @return true if something is modified
* false if nothing is done.
*/
bool simplifyConditions();
/** Remove redundant code, e.g. if( false ) { int a; } should be
* removed, because it is never executed.
* @return true if something is modified
* false if nothing is done.
*/
bool removeRedundantConditions();
/**
* Remove redundant for:
* "for (x=0;x<1;x++) { }" => "{ x = 1; }"
*/
void removeRedundantFor();
/** /**
* Reduces "; ;" to ";", except in "( ; ; )" * Reduces "; ;" to ";", except in "( ; ; )"
*/ */
void removeRedundantSemicolons(); void removeRedundantSemicolons();
/** Simplify function calls - constant return value
* @return true if something is modified
* false if nothing is done.
*/
bool simplifyFunctionReturn();
/** Struct simplification /** Struct simplification
* "struct S { } s;" => "struct S { }; S s;" * "struct S { } s;" => "struct S { }; S s;"
*/ */
@ -481,11 +346,6 @@ public:
*/ */
bool simplifyRedundantParentheses(); bool simplifyRedundantParentheses();
void simplifyCharAt();
/** Simplify references */
void simplifyReference();
/** /**
* Simplify functions like "void f(x) int x; {" * Simplify functions like "void f(x) int x; {"
* into "void f(int x) {" * into "void f(int x) {"
@ -525,11 +385,6 @@ public:
void findComplicatedSyntaxErrorsInTemplates(); void findComplicatedSyntaxErrorsInTemplates();
/**
* Simplify e.g. 'atol("0")' into '0'
*/
void simplifyMathFunctions();
/** /**
* Simplify e.g. 'sin(0)' into '0' * Simplify e.g. 'sin(0)' into '0'
*/ */
@ -562,26 +417,6 @@ public:
private: private:
/**
* simplify "while (0)"
*/
void simplifyWhile0();
/**
* Simplify while(func() && errno==EINTR)
*/
void simplifyErrNoInWhile();
/**
* Simplify while(func(f))
*/
void simplifyFuncInWhile();
/**
* Remove "std::" before some function names
*/
void simplifyStd();
/** Simplify pointer to standard type (C only) */ /** Simplify pointer to standard type (C only) */
void simplifyPointerToStandardType(); void simplifyPointerToStandardType();
@ -738,12 +573,6 @@ private:
*/ */
void simplifyCPPAttribute(); void simplifyCPPAttribute();
/**
* Replace strlen(str)
* @return true if any replacement took place, false else
* */
bool simplifyStrlen();
/** /**
* Convert namespace aliases * Convert namespace aliases
*/ */
@ -790,12 +619,6 @@ private:
std::map<int, std::map<std::string,int> >& structMembers, std::map<int, std::map<std::string,int> >& structMembers,
nonneg int *varId_); nonneg int *varId_);
/**
* Simplify e.g. 'return(strncat(temp,"a",1));' into
* strncat(temp,"a",1); return temp;
*/
void simplifyReturnStrncat();
/** /**
* Output list of unknown types. * Output list of unknown types.
*/ */
@ -830,7 +653,7 @@ public:
* 1=1st simplifications * 1=1st simplifications
* 2=2nd simplifications * 2=2nd simplifications
*/ */
void printDebugOutput(int simplification) const; void printDebugOutput() const;
void dump(std::ostream &out) const; void dump(std::ostream &out) const;

View File

@ -52,7 +52,6 @@ private:
TEST_CASE(optionwithoutfile); TEST_CASE(optionwithoutfile);
TEST_CASE(verboseshort); TEST_CASE(verboseshort);
TEST_CASE(verboselong); TEST_CASE(verboselong);
TEST_CASE(debugSimplified);
TEST_CASE(debugwarnings); TEST_CASE(debugwarnings);
TEST_CASE(forceshort); TEST_CASE(forceshort);
TEST_CASE(forcelong); TEST_CASE(forcelong);
@ -224,14 +223,6 @@ private:
ASSERT_EQUALS(true, settings.verbose); ASSERT_EQUALS(true, settings.verbose);
} }
void debugSimplified() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--debug-simplified", "file.cpp"};
settings.debugSimplified = false;
ASSERT(defParser.parseFromArgs(3, argv));
ASSERT_EQUALS(true, settings.debugSimplified);
}
void debugwarnings() { void debugwarnings() {
REDIRECT; REDIRECT;
const char * const argv[] = {"cppcheck", "--debug-warnings", "file.cpp"}; const char * const argv[] = {"cppcheck", "--debug-warnings", "file.cpp"};

File diff suppressed because it is too large Load Diff

View File

@ -2491,7 +2491,7 @@ private:
"Dummy<intvec> y;"; "Dummy<intvec> y;";
const char exp [] = "Dummy < int [ 1 ] > y ;"; const char exp [] = "Dummy < int [ 1 ] > y ;";
ASSERT_EQUALS(exp, tok(code, false)); ASSERT_EQUALS(exp, tok(code, false));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (debug) Unknown type 'Dummy<int[1]>'.\n", errout.str());
} }
void simplifyTypedef124() { // ticket #7792 void simplifyTypedef124() { // ticket #7792

File diff suppressed because it is too large Load Diff

View File

@ -1456,7 +1456,8 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS_WITHOUT_LINENUMBERS( ASSERT_EQUALS_WITHOUT_LINENUMBERS(
"[test.cpp:2]: (debug) valueflow.cpp:1035:valueFlowReverse bailout: assignment of abc\n" "[test.cpp:2]: (debug) valueflow.cpp:1035:valueFlowReverse bailout: assignment of abc\n"
"[test.cpp:8]: (debug) valueflow.cpp:1131:valueFlowReverse bailout: variable abc stopping on goto label\n", "[test.cpp:8]: (debug) valueflow.cpp:1131:valueFlowReverse bailout: variable abc stopping on goto label\n"
"[test.cpp:2]: (debug) Unknown type 'ABC'.\n",
errout.str()); errout.str());
} }