Skip to content
7 changes: 7 additions & 0 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
else if (std::strncmp(argv[i], "--max-ctu-depth=", 16) == 0)
mSettings->maxCtuDepth = std::atoi(argv[i] + 16);

// output file management
else if (std::strncmp(argv[i], "--output-file-type=", 19) == 0)
mSettings->outputFileType = Path::simplifyPath(Path::fromNativeSeparators(argv[i] + 19));

// Write results in file
else if (std::strncmp(argv[i], "--output-file=", 14) == 0)
mSettings->outputFile = Path::simplifyPath(Path::fromNativeSeparators(argv[i] + 14));
Expand Down Expand Up @@ -1109,6 +1113,9 @@ void CmdLineParser::printHelp()
" is 2. A larger value will mean more errors can be found\n"
" but also means the analysis will be slower.\n"
" --output-file=<file> Write results to file, rather than standard error.\n"
" --output-file-type=<append or uniq>\n"
" append: will append to file if it exists\n"
" uniq: will generate a filename base on --output-file and the files being analyzed\n"
" --project=<file> Run Cppcheck on project. The <file> can be a Visual\n"
" Studio Solution (*.sln), Visual Studio Project\n"
" (*.vcxproj), compile database (compile_commands.json),\n"
Expand Down
23 changes: 22 additions & 1 deletion cli/cppcheckexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,29 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
mLatestProgressOutputTime = std::time(nullptr);

if (!settings.outputFile.empty()) {

if (settings.outputFileType == "append") {
using std::ios;

mErrorOutput = new std::ofstream(settings.outputFile, ios::app); // open it in append mode
} else if (settings.outputFileType == "uniq") {
std::string filename = settings.outputFile;
std::size_t extensionOffset = settings.outputFile.find_last_of(".");
if (extensionOffset != std::string::npos)
filename = filename.substr(0, extensionOffset);

for (std::map<std::string, std::size_t>::const_iterator i = mFiles.begin(); i != mFiles.end(); ++i) {
std::size_t curFileNameOffset = i->first.find_last_of("/\\");
std::string tmp = i->first.substr(curFileNameOffset + 1);
std::replace(tmp.begin(), tmp.end(), '.', '_');
filename += "_" + tmp;
}
if (extensionOffset != std::string::npos)
filename += settings.outputFile.substr(extensionOffset);
mErrorOutput = new std::ofstream(filename); // open it in uniq mode
} else {
mErrorOutput = new std::ofstream(settings.outputFile);
}
}

if (settings.xml) {
Expand Down Expand Up @@ -1207,4 +1229,3 @@ bool CppCheckExecutor::executeCommand(std::string exe, std::vector<std::string>
*output_ += buffer;
return true;
}

3 changes: 3 additions & 0 deletions lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ class CPPCHECKLIB Settings : public cppcheck::Platform {
/** @brief suppress message (--suppressions) */
Suppressions nomsg;

/** @brief write results type (--output-file-type=&lt;None|append|uniq&gt;) */
std::string outputFileType;

/** @brief write results (--output-file=&lt;file&gt;) */
std::string outputFile;

Expand Down