From 337155a6488ec0d0857c6f51cf4f449232996d65 Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Thu, 14 Feb 2019 16:08:12 +0100 Subject: [PATCH] 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 --- src/plugins/cpptools/headerpathfilter.cpp | 19 +++++++- src/plugins/cpptools/headerpathfilter.h | 14 +++++- tests/unit/unittest/headerpathfilter-test.cpp | 46 ++++++++++++++----- 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/src/plugins/cpptools/headerpathfilter.cpp b/src/plugins/cpptools/headerpathfilter.cpp index 342ee773b78..b514c468d76 100644 --- a/src/plugins/cpptools/headerpathfilter.cpp +++ b/src/plugins/cpptools/headerpathfilter.cpp @@ -48,6 +48,11 @@ void HeaderPathFilter::process() tweakHeaderPaths(); } +bool HeaderPathFilter::isProjectHeaderPath(const QString &path) const +{ + return path.startsWith(projectDirectory) || path.startsWith(buildDirectory); +} + void HeaderPathFilter::filterHeaderPath(const ProjectExplorer::HeaderPath &headerPath) { if (headerPath.path.isEmpty()) @@ -62,7 +67,10 @@ void HeaderPathFilter::filterHeaderPath(const ProjectExplorer::HeaderPath &heade systemHeaderPaths.push_back(headerPath); break; case HeaderPathType::User: - userHeaderPaths.push_back(headerPath); + if (isProjectHeaderPath(headerPath.path)) + userHeaderPaths.push_back(headerPath); + else + systemHeaderPaths.push_back(headerPath); 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 diff --git a/src/plugins/cpptools/headerpathfilter.h b/src/plugins/cpptools/headerpathfilter.h index ccb5c709c09..8b4e7719942 100644 --- a/src/plugins/cpptools/headerpathfilter.h +++ b/src/plugins/cpptools/headerpathfilter.h @@ -34,11 +34,15 @@ class CPPTOOLS_EXPORT HeaderPathFilter public: HeaderPathFilter(const ProjectPart &projectPart, UseTweakedHeaderPaths useTweakedHeaderPaths = UseTweakedHeaderPaths::Yes, - const QString &clangVersion = QString(), - const QString &clangResourceDirectory = QString()) + const QString &clangVersion = {}, + const QString &clangResourceDirectory = {}, + const QString &projectDirectory = {}, + const QString &buildDirectory = {}) : projectPart{projectPart} , clangVersion{clangVersion} , clangResourceDirectory{clangResourceDirectory} + , projectDirectory(ensurePathWithSlashEnding(projectDirectory)) + , buildDirectory(ensurePathWithSlashEnding(buildDirectory)) , useTweakedHeaderPaths{useTweakedHeaderPaths} {} @@ -49,6 +53,10 @@ private: void tweakHeaderPaths(); + bool isProjectHeaderPath(const QString &path) const; + + static QString ensurePathWithSlashEnding(const QString &path); + public: ProjectExplorer::HeaderPaths builtInHeaderPaths; ProjectExplorer::HeaderPaths systemHeaderPaths; @@ -56,6 +64,8 @@ public: const ProjectPart &projectPart; const QString clangVersion; const QString clangResourceDirectory; + const QString projectDirectory; + const QString buildDirectory; const UseTweakedHeaderPaths useTweakedHeaderPaths; }; diff --git a/tests/unit/unittest/headerpathfilter-test.cpp b/tests/unit/unittest/headerpathfilter-test.cpp index 71f1e66bb5e..9a75399443b 100644 --- a/tests/unit/unittest/headerpathfilter-test.cpp +++ b/tests/unit/unittest/headerpathfilter-test.cpp @@ -43,7 +43,7 @@ MATCHER_P(HasBuiltIn, MATCHER_P(HasSystem, path, 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; } @@ -51,7 +51,7 @@ MATCHER_P(HasSystem, MATCHER_P(HasFramework, path, 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; } @@ -59,7 +59,7 @@ MATCHER_P(HasFramework, MATCHER_P(HasUser, path, 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; } @@ -73,42 +73,60 @@ protected: HeaderPath{"/builtin_path", HeaderPathType::BuiltIn}, HeaderPath{"/system_path", HeaderPathType::System}, 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; } protected: CppTools::ProjectPart projectPart; - CppTools::HeaderPathFilter filter{projectPart, CppTools::UseTweakedHeaderPaths::No}; + CppTools::HeaderPathFilter filter{ + projectPart, CppTools::UseTweakedHeaderPaths::No, {}, {}, "/project", "/build"}; }; TEST_F(HeaderPathFilter, BuiltIn) { filter.process(); - ASSERT_THAT(filter.builtInHeaderPaths, Contains(HasBuiltIn("/builtin_path"))); + ASSERT_THAT(filter.builtInHeaderPaths, ElementsAre(HasBuiltIn("/builtin_path"))); } TEST_F(HeaderPathFilter, System) { 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) { 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(); - 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) @@ -119,9 +137,13 @@ TEST_F(HeaderPathFilter, DontAddInvalidPath) AllOf(Field(&CppTools::HeaderPathFilter::builtInHeaderPaths, ElementsAre(HasBuiltIn("/builtin_path"))), 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, - ElementsAre(HasUser("/user_path"))))); + ElementsAre(HasUser("/build/user_path"), HasUser("/project/user_path"))))); } TEST_F(HeaderPathFilter, ClangHeadersPath)