diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 3b10141a7..a89811344 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -384,6 +384,11 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) _settings->userUndefs.insert(undef); } + // -E + else if (std::strcmp(argv[i], "-E") == 0) { + _settings->preprocessOnly = true; + } + // Include paths else if (std::strncmp(argv[i], "-I", 2) == 0) { std::string path; @@ -729,12 +734,12 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) } } - if (def && !_settings->_force && !maxconfigs) - _settings->_maxConfigs = 1U; - if (_settings->_force) _settings->_maxConfigs = ~0U; + else if ((def || _settings->preprocessOnly) && !maxconfigs) + _settings->_maxConfigs = 1U; + if (_settings->isEnabled("unusedFunction") && _settings->_jobs > 1) { PrintMessage("cppcheck: unusedFunction check can't be used with '-j' option. Disabling unusedFunction check."); } @@ -799,6 +804,8 @@ void CmdLineParser::PrintHelp() " -U Undefine preprocessor symbol. Use -U to explicitly\n" " hide certain #ifdef code paths from checking.\n" " Example: '-UDEBUG'\n" + " -E Print preprocessor output on stdout and don't do any\n" + " further processing.\n" " --enable= Enable additional checks. The available ids are:\n" " * all\n" " Enable all checks. It is recommended to only\n" diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index e669065f6..301f74338 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -187,6 +187,22 @@ unsigned int CppCheck::processFile(const std::string& filename, std::istream& fi codeWithoutCfg += _settings.append(); + if (_settings.preprocessOnly) { + if (codeWithoutCfg.compare(0,5,"#file") == 0) + codeWithoutCfg.insert(0U, "//"); + std::string::size_type pos = 0; + while ((pos = codeWithoutCfg.find("\n#file",pos)) != std::string::npos) + codeWithoutCfg.insert(pos+1U, "//"); + pos = 0; + while ((pos = codeWithoutCfg.find("\n#endfile",pos)) != std::string::npos) + codeWithoutCfg.insert(pos+1U, "//"); + pos = 0; + while ((pos = codeWithoutCfg.find(Preprocessor::macroChar,pos)) != std::string::npos) + codeWithoutCfg[pos] = ' '; + reportOut(codeWithoutCfg); + continue; + } + Tokenizer _tokenizer(&_settings, this); if (_settings._showtime != SHOWTIME_NONE) _tokenizer.setTimerResults(&S_timerResults); diff --git a/lib/settings.cpp b/lib/settings.cpp index 22f510814..c4109d140 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -43,6 +43,7 @@ Settings::Settings() _loadAverage(0), _exitCode(0), _showtime(SHOWTIME_NONE), + preprocessOnly(false), _maxConfigs(12), enforcedLang(None), reportProgress(false), diff --git a/lib/settings.h b/lib/settings.h index f6979b6f5..8021309e0 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -138,6 +138,9 @@ public: /** @brief show timing information (--showtime=file|summary|top5) */ SHOWTIME_MODES _showtime; + /** @brief Using -E for debugging purposes */ + bool preprocessOnly; + /** @brief List of include paths, e.g. "my/includes/" which should be used for finding include files inside source files. (-I) */ std::list _includePaths;