Skip to content

cppcheck options: add --output-file-type option #3365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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