Check selected files from project (#2378)

This commit is contained in:
fuzzelhjb 2020-01-10 08:57:37 +01:00 committed by Daniel Marjamäki
parent 3db6502fba
commit fcd5cda97f
5 changed files with 61 additions and 4 deletions

View File

@ -376,6 +376,10 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
mSettings->xml = true; mSettings->xml = true;
} }
// use a file filter
else if (std::strncmp(argv[i], "--file-filter=", 14) == 0)
mSettings->fileFilter = std::string(argv[i] + 14);
// Only print something when there are errors // Only print something when there are errors
else if (std::strcmp(argv[i], "-q") == 0 || std::strcmp(argv[i], "--quiet") == 0) else if (std::strcmp(argv[i], "-q") == 0 || std::strcmp(argv[i], "--quiet") == 0)
mSettings->quiet = true; mSettings->quiet = true;
@ -1035,6 +1039,9 @@ void CmdLineParser::printHelp()
" --exitcode-suppressions=<file>\n" " --exitcode-suppressions=<file>\n"
" Used when certain messages should be displayed but\n" " Used when certain messages should be displayed but\n"
" should not cause a non-zero exitcode.\n" " should not cause a non-zero exitcode.\n"
" --file-filter=<str> Analyze only those files matching the given filter str\n"
" Example: --file-filter=*bar.cpp analyzes only files\n"
" that end with bar.cpp.\n"
" --file-list=<file> Specify the files to check in a text file. Add one\n" " --file-list=<file> Specify the files to check in a text file. Add one\n"
" filename per line. When file is '-,' the file list will\n" " filename per line. When file is '-,' the file list will\n"
" be read from standard input.\n" " be read from standard input.\n"

View File

@ -159,7 +159,23 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
#else #else
const bool caseSensitive = true; const bool caseSensitive = true;
#endif #endif
if (!pathnames.empty()) { if (!mSettings->project.fileSettings.empty() && !mSettings->fileFilter.empty()) {
// filter only for the selected filenames from all project files
std::list<ImportProject::FileSettings> newList;
for (const ImportProject::FileSettings &fsetting : settings.project.fileSettings) {
if (Suppressions::matchglob(mSettings->fileFilter, fsetting.filename)) {
newList.push_back(fsetting);
}
}
if (!newList.empty())
settings.project.fileSettings = newList;
else {
std::cout << "cppcheck: error: could not find any files matching the filter." << std::endl;
return false;
}
}
else if (!pathnames.empty()) {
// Execute recursiveAddFiles() to each given file parameter // Execute recursiveAddFiles() to each given file parameter
const PathMatch matcher(ignored, caseSensitive); const PathMatch matcher(ignored, caseSensitive);
for (const std::string &pathname : pathnames) for (const std::string &pathname : pathnames)
@ -172,6 +188,20 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
std::cout << "cppcheck: Maybe all paths were ignored?" << std::endl; std::cout << "cppcheck: Maybe all paths were ignored?" << std::endl;
return false; return false;
} }
else if(!mSettings->fileFilter.empty()) {
std::map<std::string, std::size_t> newMap;
for (std::map<std::string, std::size_t>::const_iterator i = mFiles.begin(); i != mFiles.end(); ++i)
if (Suppressions::matchglob(mSettings->fileFilter, i->first)) {
newMap[i->first] = i->second;
}
mFiles = newMap;
if (mFiles.empty()) {
std::cout << "cppcheck: error: could not find any files matching the filter." << std::endl;
return false;
}
}
return true; return true;
} }
@ -890,9 +920,8 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
} }
} }
} else { } else {
// filesettings // filesettings
c = 0; // check all files of the project
for (const ImportProject::FileSettings &fs : settings.project.fileSettings) { for (const ImportProject::FileSettings &fs : settings.project.fileSettings) {
returnValue += cppcheck.check(fs); returnValue += cppcheck.check(fs);
++c; ++c;

View File

@ -79,6 +79,9 @@ public:
/** @brief --cppcheck-build-dir */ /** @brief --cppcheck-build-dir */
std::string buildDir; std::string buildDir;
/** @brief --file-filter for analyzing special files */
std::string fileFilter;
/** Is the 'configuration checking' wanted? */ /** Is the 'configuration checking' wanted? */
bool checkConfiguration; bool checkConfiguration;

View File

@ -82,6 +82,16 @@ We don't know which approach (project file or manual configuration) will give yo
Later chapters will describe this in more detail. Later chapters will describe this in more detail.
### Check files matching a given file filter
With `--file-filter=<str>` you can set a file filter and only those files matching the filter will be checked.
For example: if you want to check only those files and folders starting from a subfolder src/ that start with "test" you have to type:
cppcheck src/ --file-filter=src/test*
Cppcheck first collects all files in src/ and will apply the filter after that. So the filter must start with the given start folder.
### Excluding a file or folder from checking ### Excluding a file or folder from checking
To exclude a file or folder, there are two options. The first option is to only provide the paths and files you want to check. To exclude a file or folder, there are two options. The first option is to only provide the paths and files you want to check.

View File

@ -29,6 +29,15 @@ def cppcheck_local(args):
os.chdir(cwd) os.chdir(cwd)
return (ret, stdout, stderr) return (ret, stdout, stderr)
def test_file_filter():
ret, stdout, stderr = cppcheck(['proj2/','--file-filter=proj2/a/*'])
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c')
assert ret == 0
assert stdout.find('Checking %s ...' % (file1)) >= 0
ret, stdout, stderr = cppcheck(['proj2/','--file-filter=proj2/b*'])
assert ret == 0
assert stdout.find('Checking %s ...' % (file2)) >= 0
def test_local_path(): def test_local_path():
create_compile_commands() create_compile_commands()
@ -150,4 +159,3 @@ def test_gui_project_loads_absolute_vs_solution():
import_project=os.path.join(os.getcwd(), 'proj2', 'proj2.sln').replace('\\', '/')) import_project=os.path.join(os.getcwd(), 'proj2', 'proj2.sln').replace('\\', '/'))
ret, stdout, stderr = cppcheck(['--project=test.cppcheck']) ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
assert stderr == ERR_A + ERR_B assert stderr == ERR_A + ERR_B