Accept include paths ending with backslash.

Convert include path to use internal path separators when parsing
command line. Convert back to native separators when using paths.

Ticket #2448 (Error in handling -I command line parameter)
This commit is contained in:
Kimmo Varis 2011-01-11 21:03:18 +02:00
parent 79e52a8c45
commit 846d3dae99
4 changed files with 43 additions and 5 deletions

View File

@ -25,6 +25,7 @@
#include "timer.h" #include "timer.h"
#include "settings.h" #include "settings.h"
#include "cmdlineparser.h" #include "cmdlineparser.h"
#include "path.h"
// xml is used in rules // xml is used in rules
#include "tinyxml/tinyxml.h" #include "tinyxml/tinyxml.h"
@ -254,9 +255,10 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
{ {
path = 2 + argv[i]; path = 2 + argv[i];
} }
path = Path::fromNativeSeparators(path);
// If path doesn't end with / or \, add it // If path doesn't end with / or \, add it
if (path[path.length()-1] != '/' && path[path.length()-1] != '\\') if (path[path.length()-1] != '/')
path += '/'; path += '/';
_settings->_includePaths.push_back(path); _settings->_includePaths.push_back(path);

View File

@ -24,6 +24,7 @@
#include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE #include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE
#include "cmdlineparser.h" #include "cmdlineparser.h"
#include "filelister.h" #include "filelister.h"
#include "path.h"
CppCheckExecutor::CppCheckExecutor() CppCheckExecutor::CppCheckExecutor()
{ {
@ -63,9 +64,10 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
iter != _settings._includePaths.end(); iter != _settings._includePaths.end();
++iter) ++iter)
{ {
if (!getFileLister()->isDirectory(iter->c_str())) const std::string path(Path::toNativeSeparators(*iter));
if (!getFileLister()->isDirectory(path.c_str()))
{ {
std::cout << "cppcheck: error: Couldn't find path given by -I '" + *iter + "'" << std::endl; std::cout << "cppcheck: error: Couldn't find path given by -I '" + path + "'" << std::endl;
return false; return false;
} }
} }

View File

@ -1537,10 +1537,11 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath
includePaths2.push_front(""); includePaths2.push_front("");
for (std::list<std::string>::const_iterator iter = includePaths2.begin(); iter != includePaths2.end(); ++iter) for (std::list<std::string>::const_iterator iter = includePaths2.begin(); iter != includePaths2.end(); ++iter)
{ {
fin.open((*iter + filename).c_str()); const std::string path(Path::toNativeSeparators(*iter));
fin.open((path + filename).c_str());
if (fin.is_open()) if (fin.is_open())
{ {
filename = *iter + filename; filename = path + filename;
fileOpened = true; fileOpened = true;
break; break;
} }

View File

@ -51,6 +51,9 @@ private:
TEST_CASE(defines3); TEST_CASE(defines3);
TEST_CASE(includesnopath); TEST_CASE(includesnopath);
TEST_CASE(includes); TEST_CASE(includes);
TEST_CASE(includesslash);
TEST_CASE(includesbackslash);
TEST_CASE(includesnospace);
TEST_CASE(includes2); TEST_CASE(includes2);
TEST_CASE(enabledAll); TEST_CASE(enabledAll);
TEST_CASE(enabledStyle); TEST_CASE(enabledStyle);
@ -276,6 +279,36 @@ private:
ASSERT_EQUALS(" include/", settings._includePaths.front()); ASSERT_EQUALS(" include/", settings._includePaths.front());
} }
void includesslash()
{
REDIRECT;
const char *argv[] = {"cppcheck", "-I include/", "file.cpp"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(3, argv));
ASSERT_EQUALS(" include/", settings._includePaths.front());
}
void includesbackslash()
{
REDIRECT;
const char *argv[] = {"cppcheck", "-I include\\", "file.cpp"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(3, argv));
ASSERT_EQUALS(" include/", settings._includePaths.front());
}
void includesnospace()
{
REDIRECT;
const char *argv[] = {"cppcheck", "-Iinclude", "file.cpp"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(3, argv));
ASSERT_EQUALS("include/", settings._includePaths.front());
}
void includes2() void includes2()
{ {
REDIRECT; REDIRECT;