Merge pull request #126 from SimonKagstrom/for-upstream

Misc fixes for the --include= option
This commit is contained in:
Daniel Marjamäki 2012-12-28 23:58:14 -08:00
commit 8491df31ca
5 changed files with 35 additions and 14 deletions

View File

@ -420,7 +420,6 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
path = Path::fromNativeSeparators(path);
_settings->userIncludes.push_back(path);
_settings->userDefines += ";";
} else if (std::strncmp(argv[i], "--includes-file=", 16) == 0) {
// open this file and read every input file (1 file name per line)
AddInclPathsToList(16 + argv[i], _settings->_includePaths);
@ -780,9 +779,11 @@ void CmdLineParser::PrintHelp()
" files first. If paths are relative to source files,\n"
" this is not needed.\n"
" --include=<file>\n"
" Force inclusion of a file. Can be used for example when\n"
" checking the Linux kernel, where autoconf.h needs to be\n"
" included for every file compiled.\n"
" Force inclusion of a file before the checked file. Can\n"
" be used for example when checking the Linux kernel,\n"
" where autoconf.h needs to be included for every file\n"
" compiled. Works the same way as the GCC -include"
" option.\n"
" -i <dir or file> Give a source file or source file directory to exclude\n"
" from the check. This applies only to source files so\n"
" header files included by source files are not matched.\n"

View File

@ -104,6 +104,19 @@ std::string Path::simplifyPath(const char *originalPath)
return oss.str();
}
std::string Path::getPathFromFilename(const std::string &filename)
{
std::string path = "";
std::size_t pos = filename.find_last_of("\\/");
if (pos != std::string::npos)
path = filename.substr(0, 1 + pos);
return path;
}
bool Path::sameFileName(const std::string &fname1, const std::string &fname2)
{
#if defined(__linux__) || defined(__sun) || defined(__hpux)

View File

@ -56,6 +56,13 @@ public:
*/
static std::string simplifyPath(const char *originalPath);
/**
* @brief Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '')
* @param filename filename to lookup, must have / -separators.
* @return path part of the filename
*/
static std::string getPathFromFilename(const std::string &filename);
/**
* @brief Compare filenames to see if they are the same.
* On Linux the comparison is case-sensitive. On Windows it is case-insensitive.

View File

@ -815,15 +815,8 @@ void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processe
fin.open(cur.c_str());
if (!fin.is_open()) {
if (_settings && !_settings->nomsg.isSuppressed("missingInclude", cur, 1)) {
std::string path = "";
std::size_t pos = cur.find_last_of("\\/");
if (pos != std::string::npos)
path = cur.substr(0, 1 + pos);
missingIncludeFlag = true;
missingInclude(Path::toNativeSeparators(path),
missingInclude(Path::toNativeSeparators(Path::getPathFromFilename(cur)),
1,
cur,
true);
@ -834,7 +827,6 @@ void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processe
fin.close();
//handleIncludes("#include \"" + cur + "\"\n", cur, includePaths, defs);
forcedIncludes =
forcedIncludes +
"#file \"" + cur + "\"\n" +
@ -2131,7 +2123,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath
// filename contains now a file name e.g. "menu.h"
std::string processedFile;
std::string filepath;
if (headerType == UserHeader)
if (headerType == UserHeader && !paths.empty())
filepath = paths.back();
std::ifstream fin;
const bool fileOpened(openHeader(filename, includePaths, filepath, fin));

View File

@ -33,6 +33,7 @@ private:
TEST_CASE(getRelative);
TEST_CASE(is_c);
TEST_CASE(is_cpp);
TEST_CASE(get_path_from_filename);
}
void simplify_path() const {
@ -117,6 +118,13 @@ private:
ASSERT(Path::isCPP("C:\\foo\\index.cpp"));
ASSERT(Path::isCPP("C:\\foo\\index.Cpp"));
}
void get_path_from_filename() const {
ASSERT_EQUALS("", Path::getPathFromFilename("index.h"));
ASSERT_EQUALS("/tmp/", Path::getPathFromFilename("/tmp/index.h"));
ASSERT_EQUALS("a/b/c/", Path::getPathFromFilename("a/b/c/index.h"));
ASSERT_EQUALS("a/b/c/", Path::getPathFromFilename("a/b/c/"));
}
};
REGISTER_TEST(TestPath)