forked from qt-creator/qt-creator
CppTools: Add project directory filter to HeaderPathFilter
For the indexer we want to filter every not builtin include search path to as system include search path which is not inside of the project or build directory. Change-Id: I33663a1f3eb53ec39d5b16a8ca64b57d1b57bd9c Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
This commit is contained in:
@@ -48,6 +48,11 @@ void HeaderPathFilter::process()
|
|||||||
tweakHeaderPaths();
|
tweakHeaderPaths();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool HeaderPathFilter::isProjectHeaderPath(const QString &path) const
|
||||||
|
{
|
||||||
|
return path.startsWith(projectDirectory) || path.startsWith(buildDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
void HeaderPathFilter::filterHeaderPath(const ProjectExplorer::HeaderPath &headerPath)
|
void HeaderPathFilter::filterHeaderPath(const ProjectExplorer::HeaderPath &headerPath)
|
||||||
{
|
{
|
||||||
if (headerPath.path.isEmpty())
|
if (headerPath.path.isEmpty())
|
||||||
@@ -62,7 +67,10 @@ void HeaderPathFilter::filterHeaderPath(const ProjectExplorer::HeaderPath &heade
|
|||||||
systemHeaderPaths.push_back(headerPath);
|
systemHeaderPaths.push_back(headerPath);
|
||||||
break;
|
break;
|
||||||
case HeaderPathType::User:
|
case HeaderPathType::User:
|
||||||
|
if (isProjectHeaderPath(headerPath.path))
|
||||||
userHeaderPaths.push_back(headerPath);
|
userHeaderPaths.push_back(headerPath);
|
||||||
|
else
|
||||||
|
systemHeaderPaths.push_back(headerPath);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -133,4 +141,13 @@ void HeaderPathFilter::tweakHeaderPaths()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString HeaderPathFilter::ensurePathWithSlashEnding(const QString &path)
|
||||||
|
{
|
||||||
|
QString pathWithSlashEnding = path;
|
||||||
|
if (!pathWithSlashEnding.isEmpty() && *pathWithSlashEnding.rbegin() != '/')
|
||||||
|
pathWithSlashEnding.push_back('/');
|
||||||
|
|
||||||
|
return pathWithSlashEnding;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace CppTools
|
} // namespace CppTools
|
||||||
|
@@ -34,11 +34,15 @@ class CPPTOOLS_EXPORT HeaderPathFilter
|
|||||||
public:
|
public:
|
||||||
HeaderPathFilter(const ProjectPart &projectPart,
|
HeaderPathFilter(const ProjectPart &projectPart,
|
||||||
UseTweakedHeaderPaths useTweakedHeaderPaths = UseTweakedHeaderPaths::Yes,
|
UseTweakedHeaderPaths useTweakedHeaderPaths = UseTweakedHeaderPaths::Yes,
|
||||||
const QString &clangVersion = QString(),
|
const QString &clangVersion = {},
|
||||||
const QString &clangResourceDirectory = QString())
|
const QString &clangResourceDirectory = {},
|
||||||
|
const QString &projectDirectory = {},
|
||||||
|
const QString &buildDirectory = {})
|
||||||
: projectPart{projectPart}
|
: projectPart{projectPart}
|
||||||
, clangVersion{clangVersion}
|
, clangVersion{clangVersion}
|
||||||
, clangResourceDirectory{clangResourceDirectory}
|
, clangResourceDirectory{clangResourceDirectory}
|
||||||
|
, projectDirectory(ensurePathWithSlashEnding(projectDirectory))
|
||||||
|
, buildDirectory(ensurePathWithSlashEnding(buildDirectory))
|
||||||
, useTweakedHeaderPaths{useTweakedHeaderPaths}
|
, useTweakedHeaderPaths{useTweakedHeaderPaths}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@@ -49,6 +53,10 @@ private:
|
|||||||
|
|
||||||
void tweakHeaderPaths();
|
void tweakHeaderPaths();
|
||||||
|
|
||||||
|
bool isProjectHeaderPath(const QString &path) const;
|
||||||
|
|
||||||
|
static QString ensurePathWithSlashEnding(const QString &path);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ProjectExplorer::HeaderPaths builtInHeaderPaths;
|
ProjectExplorer::HeaderPaths builtInHeaderPaths;
|
||||||
ProjectExplorer::HeaderPaths systemHeaderPaths;
|
ProjectExplorer::HeaderPaths systemHeaderPaths;
|
||||||
@@ -56,6 +64,8 @@ public:
|
|||||||
const ProjectPart &projectPart;
|
const ProjectPart &projectPart;
|
||||||
const QString clangVersion;
|
const QString clangVersion;
|
||||||
const QString clangResourceDirectory;
|
const QString clangResourceDirectory;
|
||||||
|
const QString projectDirectory;
|
||||||
|
const QString buildDirectory;
|
||||||
const UseTweakedHeaderPaths useTweakedHeaderPaths;
|
const UseTweakedHeaderPaths useTweakedHeaderPaths;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -43,7 +43,7 @@ MATCHER_P(HasBuiltIn,
|
|||||||
MATCHER_P(HasSystem,
|
MATCHER_P(HasSystem,
|
||||||
path,
|
path,
|
||||||
std::string(negation ? "isn't " : "is ")
|
std::string(negation ? "isn't " : "is ")
|
||||||
+ PrintToString(HeaderPath{QString::fromUtf8(path), HeaderPathType::BuiltIn}))
|
+ PrintToString(HeaderPath{QString::fromUtf8(path), HeaderPathType::System}))
|
||||||
{
|
{
|
||||||
return arg.path == path && arg.type == HeaderPathType::System;
|
return arg.path == path && arg.type == HeaderPathType::System;
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ MATCHER_P(HasSystem,
|
|||||||
MATCHER_P(HasFramework,
|
MATCHER_P(HasFramework,
|
||||||
path,
|
path,
|
||||||
std::string(negation ? "isn't " : "is ")
|
std::string(negation ? "isn't " : "is ")
|
||||||
+ PrintToString(HeaderPath{QString::fromUtf8(path), HeaderPathType::BuiltIn}))
|
+ PrintToString(HeaderPath{QString::fromUtf8(path), HeaderPathType::Framework}))
|
||||||
{
|
{
|
||||||
return arg.path == path && arg.type == HeaderPathType::Framework;
|
return arg.path == path && arg.type == HeaderPathType::Framework;
|
||||||
}
|
}
|
||||||
@@ -59,7 +59,7 @@ MATCHER_P(HasFramework,
|
|||||||
MATCHER_P(HasUser,
|
MATCHER_P(HasUser,
|
||||||
path,
|
path,
|
||||||
std::string(negation ? "isn't " : "is ")
|
std::string(negation ? "isn't " : "is ")
|
||||||
+ PrintToString(HeaderPath{QString::fromUtf8(path), HeaderPathType::BuiltIn}))
|
+ PrintToString(HeaderPath{QString::fromUtf8(path), HeaderPathType::User}))
|
||||||
{
|
{
|
||||||
return arg.path == path && arg.type == HeaderPathType::User;
|
return arg.path == path && arg.type == HeaderPathType::User;
|
||||||
}
|
}
|
||||||
@@ -73,42 +73,60 @@ protected:
|
|||||||
HeaderPath{"/builtin_path", HeaderPathType::BuiltIn},
|
HeaderPath{"/builtin_path", HeaderPathType::BuiltIn},
|
||||||
HeaderPath{"/system_path", HeaderPathType::System},
|
HeaderPath{"/system_path", HeaderPathType::System},
|
||||||
HeaderPath{"/framework_path", HeaderPathType::Framework},
|
HeaderPath{"/framework_path", HeaderPathType::Framework},
|
||||||
HeaderPath{"/user_path", HeaderPathType::User}};
|
HeaderPath{"/outside_project_user_path", HeaderPathType::User},
|
||||||
|
HeaderPath{"/build/user_path", HeaderPathType::User},
|
||||||
|
HeaderPath{"/buildb/user_path", HeaderPathType::User},
|
||||||
|
HeaderPath{"/projectb/user_path", HeaderPathType::User},
|
||||||
|
HeaderPath{"/project/user_path", HeaderPathType::User}};
|
||||||
|
|
||||||
projectPart.headerPaths = headerPaths;
|
projectPart.headerPaths = headerPaths;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
CppTools::ProjectPart projectPart;
|
CppTools::ProjectPart projectPart;
|
||||||
CppTools::HeaderPathFilter filter{projectPart, CppTools::UseTweakedHeaderPaths::No};
|
CppTools::HeaderPathFilter filter{
|
||||||
|
projectPart, CppTools::UseTweakedHeaderPaths::No, {}, {}, "/project", "/build"};
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(HeaderPathFilter, BuiltIn)
|
TEST_F(HeaderPathFilter, BuiltIn)
|
||||||
{
|
{
|
||||||
filter.process();
|
filter.process();
|
||||||
|
|
||||||
ASSERT_THAT(filter.builtInHeaderPaths, Contains(HasBuiltIn("/builtin_path")));
|
ASSERT_THAT(filter.builtInHeaderPaths, ElementsAre(HasBuiltIn("/builtin_path")));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(HeaderPathFilter, System)
|
TEST_F(HeaderPathFilter, System)
|
||||||
{
|
{
|
||||||
filter.process();
|
filter.process();
|
||||||
|
|
||||||
ASSERT_THAT(filter.systemHeaderPaths, Contains(HasSystem("/system_path")));
|
ASSERT_THAT(filter.systemHeaderPaths,
|
||||||
|
ElementsAre(HasSystem("/system_path"),
|
||||||
|
HasFramework("/framework_path"),
|
||||||
|
HasUser("/outside_project_user_path"),
|
||||||
|
HasUser("/buildb/user_path"),
|
||||||
|
HasUser("/projectb/user_path")));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(HeaderPathFilter, User)
|
TEST_F(HeaderPathFilter, User)
|
||||||
{
|
{
|
||||||
filter.process();
|
filter.process();
|
||||||
|
|
||||||
ASSERT_THAT(filter.userHeaderPaths, Contains(HasUser("/user_path")));
|
ASSERT_THAT(filter.userHeaderPaths,
|
||||||
|
ElementsAre(HasUser("/build/user_path"), HasUser("/project/user_path")));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(HeaderPathFilter, Framework)
|
TEST_F(HeaderPathFilter, NoProjectPathSet)
|
||||||
{
|
{
|
||||||
|
CppTools::HeaderPathFilter filter{projectPart, CppTools::UseTweakedHeaderPaths::No};
|
||||||
|
|
||||||
filter.process();
|
filter.process();
|
||||||
|
|
||||||
ASSERT_THAT(filter.systemHeaderPaths, Contains(HasFramework("/framework_path")));
|
ASSERT_THAT(filter.userHeaderPaths,
|
||||||
|
ElementsAre(HasUser("/outside_project_user_path"),
|
||||||
|
HasUser("/build/user_path"),
|
||||||
|
HasUser("/buildb/user_path"),
|
||||||
|
HasUser("/projectb/user_path"),
|
||||||
|
HasUser("/project/user_path")));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(HeaderPathFilter, DontAddInvalidPath)
|
TEST_F(HeaderPathFilter, DontAddInvalidPath)
|
||||||
@@ -119,9 +137,13 @@ TEST_F(HeaderPathFilter, DontAddInvalidPath)
|
|||||||
AllOf(Field(&CppTools::HeaderPathFilter::builtInHeaderPaths,
|
AllOf(Field(&CppTools::HeaderPathFilter::builtInHeaderPaths,
|
||||||
ElementsAre(HasBuiltIn("/builtin_path"))),
|
ElementsAre(HasBuiltIn("/builtin_path"))),
|
||||||
Field(&CppTools::HeaderPathFilter::systemHeaderPaths,
|
Field(&CppTools::HeaderPathFilter::systemHeaderPaths,
|
||||||
ElementsAre(HasSystem("/system_path"), HasFramework("/framework_path"))),
|
ElementsAre(HasSystem("/system_path"),
|
||||||
|
HasFramework("/framework_path"),
|
||||||
|
HasUser("/outside_project_user_path"),
|
||||||
|
HasUser("/buildb/user_path"),
|
||||||
|
HasUser("/projectb/user_path"))),
|
||||||
Field(&CppTools::HeaderPathFilter::userHeaderPaths,
|
Field(&CppTools::HeaderPathFilter::userHeaderPaths,
|
||||||
ElementsAre(HasUser("/user_path")))));
|
ElementsAre(HasUser("/build/user_path"), HasUser("/project/user_path")))));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(HeaderPathFilter, ClangHeadersPath)
|
TEST_F(HeaderPathFilter, ClangHeadersPath)
|
||||||
|
Reference in New Issue
Block a user