diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 53dc2054d..0736d356e 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -233,7 +233,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) } // Include paths - else if (strcmp(argv[i], "-I") == 0 || strncmp(argv[i], "-I", 2) == 0) + else if (strncmp(argv[i], "-I", 2) == 0) { std::string path; @@ -252,8 +252,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) // "-Ipath/" else { - path = argv[i]; - path = path.substr(2); + path = 2 + argv[i]; } // If path doesn't end with / or \, add it diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index ac33e8ecf..74c4e67d1 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -56,6 +56,21 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c } } + // Check that all include paths exist + { + std::list::const_iterator iter; + for (iter = _settings._includePaths.begin(); + iter != _settings._includePaths.end(); + ++iter) + { + if (!getFileLister()->isDirectory(iter->c_str())) + { + std::cout << "cppcheck: error: Couldn't find path given by -I '" + *iter + "'" << std::endl; + return false; + } + } + } + std::vector pathnames = parser.GetPathNames(); std::vector filenames; diff --git a/lib/filelister.h b/lib/filelister.h index 7a09b1662..40432752a 100644 --- a/lib/filelister.h +++ b/lib/filelister.h @@ -66,6 +66,11 @@ public: */ virtual bool acceptFile(const std::string &filename); + /** + * @brief Is given path a directory? + * @return returns true if the path is a directory + */ + virtual bool isDirectory(const std::string &path) = 0; }; /** @brief get filelister (platform dependent implementation) */ diff --git a/lib/filelister_unix.cpp b/lib/filelister_unix.cpp index dc71425b3..f430417ad 100644 --- a/lib/filelister_unix.cpp +++ b/lib/filelister_unix.cpp @@ -103,4 +103,23 @@ bool FileListerUnix::sameFileName(const std::string &fname1, const std::string & #endif } +bool FileListerUnix::isDirectory(const std::string &path) +{ + bool ret = false; + + glob_t glob_results; + glob(path.c_str(), GLOB_MARK, 0, &glob_results); + if (glob_results.gl_pathc == 1) + { + const std::string glob_path = glob_results.gl_pathv[0]; + if (!glob_path.empty() && glob_path[glob_path.size() - 1] == '/') + { + ret = true; + } + } + globfree(&glob_results); + + return ret; +} + #endif // _WIN32 diff --git a/lib/filelister_unix.h b/lib/filelister_unix.h index 7effade0a..c04f9e8bf 100644 --- a/lib/filelister_unix.h +++ b/lib/filelister_unix.h @@ -33,6 +33,7 @@ public: virtual void recursiveAddFiles(std::vector &filenames, const std::string &path); virtual bool sameFileName(const std::string &fname1, const std::string &fname2); // virtual static bool acceptFile(const std::string &filename); + virtual bool isDirectory(const std::string &path); private: #ifndef _WIN32 void recursiveAddFiles2(std::vector &relative, diff --git a/lib/filelister_win32.cpp b/lib/filelister_win32.cpp index 565bd579b..c32ee4d0d 100644 --- a/lib/filelister_win32.cpp +++ b/lib/filelister_win32.cpp @@ -205,4 +205,9 @@ bool FileListerWin32::sameFileName(const std::string &fname1, const std::string #endif } +bool FileListerWin32::isDirectory(const std::string &path) +{ + return (MyIsDirectory(path) != FALSE); +} + #endif // _WIN32 diff --git a/lib/filelister_win32.h b/lib/filelister_win32.h index 9f0cb1264..21c7f48ab 100644 --- a/lib/filelister_win32.h +++ b/lib/filelister_win32.h @@ -32,6 +32,7 @@ class FileListerWin32 : public FileLister public: virtual void recursiveAddFiles(std::vector &filenames, const std::string &path); virtual bool sameFileName(const std::string &fname1, const std::string &fname2); + virtual bool isDirectory(const std::string &path); private: };