From f5c274b3b1d416c2209c618bed79d4ffdc5ad9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 2 Mar 2019 19:52:15 +0100 Subject: [PATCH] Added '--check-headers=no' option. --- cli/cmdlineparser.cpp | 6 ++++++ lib/settings.cpp | 1 + lib/settings.h | 4 ++++ lib/tokenize.cpp | 36 ++++++++++++++++++++++++++++++++++++ lib/tokenize.h | 7 +++++++ 5 files changed, 54 insertions(+) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 3189d3094..aaec81ad4 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -155,6 +155,10 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) else if (std::strcmp(argv[i], "--dump") == 0) mSettings->dump = true; + // --check-headers=no + else if (std::strcmp(argv[i], "--check-headers=no") == 0) + mSettings->checkHeaders = false; + // max ctu depth else if (std::strncmp(argv[i], "--max-ctu-depth=", 16) == 0) mSettings->maxCtuDepth = std::atoi(argv[i] + 16); @@ -914,6 +918,8 @@ void CmdLineParser::printHelp() " incremental analysis, distributed analysis.\n" " --check-config Check cppcheck configuration. The normal code\n" " analysis is disabled by this flag.\n" + " --check-headers=no Turn off checking of included files, to make the\n" + " analysis faster.\n" " --check-library Show information messages when library files have\n" " incomplete info.\n" " --config-exclude=\n" diff --git a/lib/settings.cpp b/lib/settings.cpp index 63de049e1..9d4c7438d 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -49,6 +49,7 @@ Settings::Settings() maxConfigs(12), enforcedLang(None), reportProgress(false), + checkHeaders(true), checkConfiguration(false), checkLibrary(false) { diff --git a/lib/settings.h b/lib/settings.h index b39ba49d7..e6c9f38b7 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -285,6 +285,10 @@ public: /** Check for incomplete info in library files? */ bool checkLibrary; + /** Check code in the headers, this is on by default but can + * be turned off to save CPU */ + bool checkHeaders; + /** Struct contains standards settings */ Standards standards; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4ab0bd1c1..1b53532bf 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3630,6 +3630,8 @@ bool Tokenizer::simplifyTokenList1(const char FileName[]) createLinks(); + simplifyHeaders(); + // Remove __asm.. simplifyAsm(); @@ -4257,6 +4259,40 @@ void Tokenizer::dump(std::ostream &out) const list.front()->printValueFlow(true, out); } +void Tokenizer::simplifyHeaders() +{ + // TODO : can we remove anything in headers here? Like unused declarations. + // Maybe if --dump is used we want to have _everything_. + + if (mSettings->checkHeaders) + // Default=full analysis. All information in the headers are kept. + return; + + // We want to remove selected stuff from the headers but not *everything*. + // The intention here is to not damage the analysis of the source file. + + // TODO: Remove unused types/variables/etc in headers.. + + for (Token *tok = list.front(); tok; tok = tok->next()) { + if (tok->fileIndex() == 0) + // Keep all code in the source file + continue; + + // Remove executable code + if (tok->str() == "{") { + const Token *prev = tok->previous(); + while (prev && prev->isName()) + prev = prev->previous(); + if (Token::simpleMatch(prev, ")")) { + // Replace all tokens from { to } with a ";". + Token::eraseTokens(tok,tok->link()->next()); + tok->str(";"); + tok->link(nullptr); + } + } + } +} + void Tokenizer::removeMacrosInGlobalScope() { for (Token *tok = list.front(); tok; tok = tok->next()) { diff --git a/lib/tokenize.h b/lib/tokenize.h index 2a9872ed2..e08f3c8e5 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -164,6 +164,13 @@ public: */ bool simplifyTokenList2(); + /** + * If --check-headers=no has been given; then remove unneeded code in headers. + * - All executable code. + * - Unused types/variables/etc + */ + void simplifyHeaders(); + /** * Deletes dead code between 'begin' and 'end'. * In general not everything can be erased, such as: