diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index d6ed1f20a..49a8a6f9b 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -177,8 +177,12 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c } else if (!pathnames.empty()) { // Execute recursiveAddFiles() to each given file parameter const PathMatch matcher(ignored, caseSensitive); - for (const std::string &pathname : pathnames) - FileLister::recursiveAddFiles(mFiles, Path::toNativeSeparators(pathname), mSettings->library.markupExtensions(), matcher); + for (const std::string &pathname : pathnames) { + std::string err = FileLister::recursiveAddFiles(mFiles, Path::toNativeSeparators(pathname), mSettings->library.markupExtensions(), matcher); + if (!err.empty()) { + std::cout << "cppcheck: " << err << std::endl; + } + } } if (mFiles.empty() && settings.project.fileSettings.empty()) { diff --git a/cli/filelister.cpp b/cli/filelister.cpp index 2b2e61a67..016f63bac 100644 --- a/cli/filelister.cpp +++ b/cli/filelister.cpp @@ -71,12 +71,12 @@ static BOOL myFileExists(const std::string& path) return result; } -void FileLister::recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra, const PathMatch& ignored) +std::string FileLister::recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra, const PathMatch& ignored) { - addFiles(files, path, extra, true, ignored); + return addFiles(files, path, extra, true, ignored); } -void FileLister::addFiles(std::map &files, const std::string &path, const std::set &extra, bool recursive, const PathMatch& ignored) +std::string FileLister::addFiles(std::map &files, const std::string &path, const std::set &extra, bool recursive, const PathMatch& ignored) { const std::string cleanedPath = Path::toNativeSeparators(path); @@ -115,7 +115,7 @@ void FileLister::addFiles(std::map &files, const std:: WIN32_FIND_DATAA ffd; HANDLE hFind = myFindFirstFile(searchPattern, &ffd); if (INVALID_HANDLE_VALUE == hFind) - return; + return ""; do { if (ffd.cFileName[0] == '.' || ffd.cFileName[0] == '\0') @@ -143,13 +143,17 @@ void FileLister::addFiles(std::map &files, const std:: } else { // Directory if (recursive) { - if (!ignored.match(fname)) - FileLister::recursiveAddFiles(files, fname, extra, ignored); + if (!ignored.match(fname)) { + std::string err = FileLister::recursiveAddFiles(files, fname, extra, ignored); + if (!err.empty()) + return err; + } } } } while (FindNextFileA(hFind, &ffd) != FALSE); FindClose(hFind); + return ""; } bool FileLister::isDirectory(const std::string &path) @@ -175,21 +179,22 @@ bool FileLister::fileExists(const std::string &path) #include #include +#include -static void addFiles2(std::map &files, - const std::string &path, - const std::set &extra, - bool recursive, - const PathMatch& ignored - ) +static std::string addFiles2(std::map &files, + const std::string &path, + const std::set &extra, + bool recursive, + const PathMatch& ignored + ) { struct stat file_stat; if (stat(path.c_str(), &file_stat) != -1) { if ((file_stat.st_mode & S_IFMT) == S_IFDIR) { DIR * dir = opendir(path.c_str()); if (!dir) - return; + return ""; dirent * dir_result; // make sure we reserve enough space for the readdir_r() buffer; @@ -225,12 +230,16 @@ static void addFiles2(std::map &files, #endif if (path_is_directory) { if (recursive && !ignored.match(new_path)) { - addFiles2(files, new_path, extra, recursive, ignored); + std::string err = addFiles2(files, new_path, extra, recursive, ignored); + if (!err.empty()) + return err; } } else { if (Path::acceptFile(new_path, extra) && !ignored.match(new_path)) { - stat(new_path.c_str(), &file_stat); - files[new_path] = file_stat.st_size; + if (stat(new_path.c_str(), &file_stat) != -1) + files[new_path] = file_stat.st_size; + else + return "Can't stat " + new_path + " errno: " + std::to_string(errno); } } } @@ -238,22 +247,25 @@ static void addFiles2(std::map &files, } else files[path] = file_stat.st_size; } + return ""; } -void FileLister::recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra, const PathMatch& ignored) +std::string FileLister::recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra, const PathMatch& ignored) { - addFiles(files, path, extra, true, ignored); + return addFiles(files, path, extra, true, ignored); } -void FileLister::addFiles(std::map &files, const std::string &path, const std::set &extra, bool recursive, const PathMatch& ignored) +std::string FileLister::addFiles(std::map &files, const std::string &path, const std::set &extra, bool recursive, const PathMatch& ignored) { if (!path.empty()) { std::string corrected_path = path; if (endsWith(corrected_path, '/')) corrected_path.erase(corrected_path.end() - 1); - addFiles2(files, corrected_path, extra, recursive, ignored); + return addFiles2(files, corrected_path, extra, recursive, ignored); } + + return ""; } bool FileLister::isDirectory(const std::string &path) diff --git a/cli/filelister.h b/cli/filelister.h index 9b0e9ecda..a951df8c9 100644 --- a/cli/filelister.h +++ b/cli/filelister.h @@ -40,10 +40,11 @@ public: * @param files output map that associates the size of each file with its name * @param path root path * @param ignored ignored paths + * @return On success, an empty string is returned. On error, a error message is returned. */ - static void recursiveAddFiles(std::map &files, const std::string &path, const PathMatch& ignored) { + static std::string recursiveAddFiles(std::map &files, const std::string &path, const PathMatch& ignored) { const std::set extra; - recursiveAddFiles(files, path, extra, ignored); + return recursiveAddFiles(files, path, extra, ignored); } /** @@ -55,8 +56,9 @@ public: * @param path root path * @param extra Extra file extensions * @param ignored ignored paths + * @return On success, an empty string is returned. On error, a error message is returned. */ - static void recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra, const PathMatch& ignored); + static std::string recursiveAddFiles(std::map &files, const std::string &path, const std::set &extra, const PathMatch& ignored); /** * @brief (Recursively) add source files to a map. @@ -68,8 +70,9 @@ public: * @param extra Extra file extensions * @param recursive Enable recursion * @param ignored ignored paths + * @return On success, an empty string is returned. On error, a error message is returned. */ - static void addFiles(std::map &files, const std::string &path, const std::set &extra, bool recursive, const PathMatch& ignored); + static std::string addFiles(std::map &files, const std::string &path, const std::set &extra, bool recursive, const PathMatch& ignored); /** * @brief Is given path a directory? diff --git a/test/testfilelister.cpp b/test/testfilelister.cpp index 25c1a206d..e19e62c67 100644 --- a/test/testfilelister.cpp +++ b/test/testfilelister.cpp @@ -55,7 +55,8 @@ private: std::map files; std::vector masks; PathMatch matcher(masks); - FileLister::recursiveAddFiles(files, ".", matcher); + std::string err = FileLister::recursiveAddFiles(files, ".", matcher); + ASSERT(err.empty()); // In case there are leading "./".. for (std::map::iterator i = files.begin(); i != files.end();) { diff --git a/test/testsamples.cpp b/test/testsamples.cpp index cceb6d4cf..5672437b7 100644 --- a/test/testsamples.cpp +++ b/test/testsamples.cpp @@ -53,11 +53,13 @@ private: std::map files; const std::vector masks; const PathMatch matcher(masks); + std::string err = #ifdef _WIN32 - FileLister::recursiveAddFiles(files, "..\\samples", matcher); + FileLister::recursiveAddFiles(files, "..\\samples", matcher); #else - FileLister::recursiveAddFiles(files, "samples", matcher); + FileLister::recursiveAddFiles(files, "samples", matcher); #endif + ASSERT(err.empty()); for (std::map::const_iterator i = files.begin(); i != files.end(); ++i) { if (i->first.find("memleak") != std::string::npos) continue; diff --git a/tools/dmake.cpp b/tools/dmake.cpp index 988553d70..fb3d729b9 100644 --- a/tools/dmake.cpp +++ b/tools/dmake.cpp @@ -58,11 +58,7 @@ static void getDeps(const std::string &filename, std::vector &depfi if (filename == "externals/z3_version.h") return; - static const std::vector externalfolders{"externals", - "externals/picojson", - "externals/simplecpp", - "externals/tinyxml2" - }; + static const std::vector externalfolders{"externals", "externals/picojson", "externals/simplecpp", "externals/tinyxml2" }; // Is the dependency already included? if (std::find(depfiles.begin(), depfiles.end(), filename) != depfiles.end()) @@ -130,19 +126,22 @@ static void compilefiles(std::ostream &fout, const std::vector &fil } } -static void getCppFiles(std::vector &files, const std::string &path, bool recursive) +static std::string getCppFiles(std::vector &files, const std::string &path, bool recursive) { std::map filemap; const std::set extra; const std::vector masks; const PathMatch matcher(masks); - FileLister::addFiles(filemap, path, extra, recursive, matcher); + std::string err = FileLister::addFiles(filemap, path, extra, recursive, matcher); + if (!err.empty()) + return err; // add *.cpp files to the "files" vector.. for (const std::pair file : filemap) { if (file.first.find(".cpp") != std::string::npos) files.push_back(file.first); } + return ""; } @@ -160,19 +159,39 @@ int main(int argc, char **argv) // Get files.. std::vector libfiles; - getCppFiles(libfiles, "lib/", false); + std::string err = getCppFiles(libfiles, "lib/", false); + if (!err.empty()) { + std::cerr << err << std::endl; + return EXIT_FAILURE; + } std::vector extfiles; - getCppFiles(extfiles, "externals/", true); + err = getCppFiles(extfiles, "externals/", true); + if (!err.empty()) { + std::cerr << err << std::endl; + return EXIT_FAILURE; + } std::vector clifiles; - getCppFiles(clifiles, "cli/", false); + err = getCppFiles(clifiles, "cli/", false); + if (!err.empty()) { + std::cerr << err << std::endl; + return EXIT_FAILURE; + } std::vector testfiles; - getCppFiles(testfiles, "test/", false); + err = getCppFiles(testfiles, "test/", false); + if (!err.empty()) { + std::cerr << err << std::endl; + return EXIT_FAILURE; + } std::vector toolsfiles; - getCppFiles(toolsfiles, "tools/", false); + err = getCppFiles(toolsfiles, "tools/", false); + if (!err.empty()) { + std::cerr << err << std::endl; + return EXIT_FAILURE; + } if (libfiles.empty() && clifiles.empty() && testfiles.empty()) { std::cerr << "No files found. Are you in the correct directory?" << std::endl;