CppTools: Add pre system include search path per project

Sometimes you want add a pre system include search path per project to
overload some system header for indexing. This can then even be checked in
the repository.

Change-Id: Ib103e5935d0553aa94522ed736f8c4eb2405a093
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
This commit is contained in:
Marco Bubke
2019-04-07 20:46:06 +02:00
parent 3bd653416a
commit 5ab057746c
9 changed files with 143 additions and 106 deletions

View File

@@ -37,6 +37,7 @@
#include <cpptools/compileroptionsbuilder.h>
#include <cpptools/projectpart.h>
#include <projectexplorer/project.h>
namespace {
@@ -53,11 +54,62 @@ using testing::ByMove;
using CppTools::CompilerOptionsBuilder;
using ClangBackEnd::V2::FileContainer;
std::vector<Utils::SmallStringVector> createCommandLines(
const std::vector<CppTools::ProjectPart::Ptr> &projectParts)
{
using Filter = ClangRefactoring::ClangQueryProjectsFindFilter;
std::vector<Utils::SmallStringVector> commandLines;
for (const CppTools::ProjectPart::Ptr &projectPart : projectParts) {
for (const CppTools::ProjectFile &projectFile : projectPart->files) {
Utils::SmallStringVector commandLine = Filter::compilerArguments(projectPart.data(),
projectFile.kind);
commandLine.emplace_back(projectFile.path);
commandLines.push_back(commandLine);
}
}
return commandLines;
}
class ClangQueryProjectFindFilter : public ::testing::Test
{
protected:
void SetUp();
std::unique_ptr<ClangRefactoring::SearchHandle> createSearchHandle();
void SetUp()
{
projectsParts = createProjectParts();
commandLines = createCommandLines(projectsParts);
findFilter.setProjectParts(projectsParts);
findFilter.setUnsavedContent({unsavedContent.clone()});
ON_CALL(mockSearch, startNewSearch(QStringLiteral("Clang Query"), findDeclQueryText))
.WillByDefault(Return(ByMove(createSearchHandle())));
}
std::unique_ptr<ClangRefactoring::SearchHandle> createSearchHandle()
{
auto handle = std::make_unique<NiceMock<MockSearchHandle>>();
handle->setRefactoringServer(&mockRefactoringServer);
return handle;
}
std::vector<CppTools::ProjectPart::Ptr> createProjectParts()
{
auto projectPart1 = CppTools::ProjectPart::Ptr(new CppTools::ProjectPart);
projectPart1->project = &project;
projectPart1->files.append({"/path/to/file1.h", CppTools::ProjectFile::CXXHeader});
projectPart1->files.append({"/path/to/file1.cpp", CppTools::ProjectFile::CXXSource});
auto projectPart2 = CppTools::ProjectPart::Ptr(new CppTools::ProjectPart);
projectPart2->project = &project;
projectPart2->files.append({"/path/to/file2.cpp", CppTools::ProjectFile::CXXSource});
projectPart2->files.append({"/path/to/unsaved.cpp", CppTools::ProjectFile::CXXSource});
projectPart2->files.append({"/path/to/cheader.h", CppTools::ProjectFile::CHeader});
return {projectPart1, projectPart2};
}
protected:
NiceMock<MockRefactoringServer> mockRefactoringServer;
@@ -73,6 +125,7 @@ protected:
ClangBackEnd::V2::FileContainer unsavedContent{{"/path/to", "unsaved.cpp"},
"void f();",
{}};
ProjectExplorer::Project project;
};
TEST_F(ClangQueryProjectFindFilter, SupportedFindFlags)
@@ -175,60 +228,4 @@ TEST_F(ClangQueryProjectFindFilter, CallingRequestSourceRangesAndDiagnostics)
findFilter.requestSourceRangesAndDiagnostics(QString(queryText), QString(exampleContent));
}
std::vector<CppTools::ProjectPart::Ptr> createProjectParts()
{
auto projectPart1 = CppTools::ProjectPart::Ptr(new CppTools::ProjectPart);
projectPart1->files.append({"/path/to/file1.h", CppTools::ProjectFile::CXXHeader});
projectPart1->files.append({"/path/to/file1.cpp", CppTools::ProjectFile::CXXSource});
auto projectPart2 = CppTools::ProjectPart::Ptr(new CppTools::ProjectPart);
projectPart2->files.append({"/path/to/file2.cpp", CppTools::ProjectFile::CXXSource});
projectPart2->files.append({"/path/to/unsaved.cpp", CppTools::ProjectFile::CXXSource});
projectPart2->files.append({"/path/to/cheader.h", CppTools::ProjectFile::CHeader});
return {projectPart1, projectPart2};
}
std::vector<Utils::SmallStringVector>
createCommandLines(const std::vector<CppTools::ProjectPart::Ptr> &projectParts)
{
using Filter = ClangRefactoring::ClangQueryProjectsFindFilter;
std::vector<Utils::SmallStringVector> commandLines;
for (const CppTools::ProjectPart::Ptr &projectPart : projectParts) {
for (const CppTools::ProjectFile &projectFile : projectPart->files) {
Utils::SmallStringVector commandLine = Filter::compilerArguments(projectPart.data(),
projectFile.kind);
commandLine.emplace_back(projectFile.path);
commandLines.push_back(commandLine);
}
}
return commandLines;
}
void ClangQueryProjectFindFilter::SetUp()
{
projectsParts = createProjectParts();
commandLines = createCommandLines(projectsParts);
findFilter.setProjectParts(projectsParts);
findFilter.setUnsavedContent({unsavedContent.clone()});
ON_CALL(mockSearch, startNewSearch(QStringLiteral("Clang Query"), findDeclQueryText))
.WillByDefault(Return(ByMove(createSearchHandle())));
}
std::unique_ptr<ClangRefactoring::SearchHandle> ClangQueryProjectFindFilter::createSearchHandle()
{
auto handle = std::make_unique<NiceMock<MockSearchHandle>>();
handle->setRefactoringServer(&mockRefactoringServer);
return handle;
}
}

View File

@@ -26,6 +26,7 @@
#include "googletest.h"
#include <cpptools/headerpathfilter.h>
#include <projectexplorer/project.h>
namespace {
@@ -80,9 +81,11 @@ protected:
HeaderPath{"/project/user_path", HeaderPathType::User}};
projectPart.headerPaths = headerPaths;
projectPart.project = &project;
}
protected:
ProjectExplorer::Project project;
CppTools::ProjectPart projectPart;
CppTools::HeaderPathFilter filter{
projectPart, CppTools::UseTweakedHeaderPaths::No, {}, {}, "/project", "/build"};
@@ -100,7 +103,8 @@ TEST_F(HeaderPathFilter, System)
filter.process();
ASSERT_THAT(filter.systemHeaderPaths,
ElementsAre(HasSystem("/system_path"),
ElementsAre(HasSystem("/project/.pre_includes"),
HasSystem("/system_path"),
HasFramework("/framework_path"),
HasUser("/outside_project_user_path"),
HasUser("/buildb/user_path"),
@@ -137,7 +141,8 @@ TEST_F(HeaderPathFilter, DontAddInvalidPath)
AllOf(Field(&CppTools::HeaderPathFilter::builtInHeaderPaths,
ElementsAre(HasBuiltIn("/builtin_path"))),
Field(&CppTools::HeaderPathFilter::systemHeaderPaths,
ElementsAre(HasSystem("/system_path"),
ElementsAre(HasSystem("/project/.pre_includes"),
HasSystem("/system_path"),
HasFramework("/framework_path"),
HasUser("/outside_project_user_path"),
HasUser("/buildb/user_path"),

View File

@@ -46,9 +46,10 @@
#include <updategeneratedfilesmessage.h>
#include <updateprojectpartsmessage.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <cpptools/compileroptionsbuilder.h>
#include <cpptools/projectpart.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <utils/algorithm.h>
@@ -85,6 +86,8 @@ protected:
void SetUp() override
{
project.rootProjectDirectoryPath.appendPath("project");
projectPart.project = &project;
projectPart.files.push_back(header1ProjectFile);
projectPart.files.push_back(header2ProjectFile);
projectPart.files.push_back(source1ProjectFile);
@@ -94,6 +97,7 @@ protected:
projectPart.projectMacros = {{"FOO", "2"}, {"BAR", "1"}};
projectPartId = projectPartsStorage.fetchProjectPartId(Utils::SmallString{projectPart.id()});
projectPart2.project = &project;
projectPart2.files.push_back(header2ProjectFile);
projectPart2.files.push_back(header1ProjectFile);
projectPart2.files.push_back(source2ProjectFile);
@@ -115,7 +119,8 @@ protected:
expectedContainer = {projectPartId,
arguments.clone(),
Utils::clone(compilerMacros),
{{CLANG_RESOURCE_DIR, 1, ClangBackEnd::IncludeSearchPathType::BuiltIn}},
{{"project/.pre_includes", 1, ClangBackEnd::IncludeSearchPathType::System},
{CLANG_RESOURCE_DIR, 2, ClangBackEnd::IncludeSearchPathType::BuiltIn}},
{},
{filePathId(headerPaths[1])},
{filePathIds(sourcePaths)},
@@ -125,7 +130,8 @@ protected:
expectedContainer2 = {projectPartId2,
arguments2.clone(),
Utils::clone(compilerMacros),
{{CLANG_RESOURCE_DIR, 1, ClangBackEnd::IncludeSearchPathType::BuiltIn}},
{{"project/.pre_includes", 1, ClangBackEnd::IncludeSearchPathType::System},
{CLANG_RESOURCE_DIR, 2, ClangBackEnd::IncludeSearchPathType::BuiltIn}},
{},
{filePathId(headerPaths[1])},
{filePathIds(sourcePaths)},
@@ -159,6 +165,7 @@ protected:
CppTools::ProjectFile cannotBuildSourceProjectFile{QString("/cannot/build"),
CppTools::ProjectFile::CXXSource};
CppTools::ProjectFile nonActiveProjectFile{QString("/foo"), CppTools::ProjectFile::CXXSource, false};
ProjectExplorer::Project project;
CppTools::ProjectPart projectPart;
CppTools::ProjectPart projectPart2;
CppTools::ProjectPart nonBuildingProjectPart;
@@ -269,6 +276,7 @@ TEST_F(ProjectUpdater, CallStorageInsideTransaction)
{
InSequence s;
CppTools::ProjectPart projectPart;
projectPart.project = &project;
projectPart.displayName = "project";
Utils::SmallString projectPartName = projectPart.id();
MockSqliteTransactionBackend mockSqliteTransactionBackend;
@@ -305,6 +313,7 @@ TEST_F(ProjectUpdater, CreateSortedCompilerMacros)
TEST_F(ProjectUpdater, CreateSortedIncludeSearchPaths)
{
CppTools::ProjectPart projectPart;
projectPart.project = &project;
ProjectExplorer::HeaderPath includePath{"/to/path1", ProjectExplorer::HeaderPathType::User};
ProjectExplorer::HeaderPath includePath2{"/to/path2", ProjectExplorer::HeaderPathType::User};
ProjectExplorer::HeaderPath invalidPath;
@@ -317,13 +326,15 @@ TEST_F(ProjectUpdater, CreateSortedIncludeSearchPaths)
auto paths = updater.createIncludeSearchPaths(projectPart);
ASSERT_THAT(paths.system,
ElementsAre(Eq(IncludeSearchPath{systemPath.path, 1, IncludeSearchPathType::System}),
Eq(IncludeSearchPath{builtInPath.path, 4, IncludeSearchPathType::BuiltIn}),
Eq(IncludeSearchPath{frameworkPath.path, 2, IncludeSearchPathType::Framework}),
Eq(IncludeSearchPath{CLANG_RESOURCE_DIR,
3,
ClangBackEnd::IncludeSearchPathType::BuiltIn})));
ASSERT_THAT(
paths.system,
ElementsAre(Eq(IncludeSearchPath{systemPath.path, 2, IncludeSearchPathType::System}),
Eq(IncludeSearchPath{builtInPath.path, 5, IncludeSearchPathType::BuiltIn}),
Eq(IncludeSearchPath{frameworkPath.path, 3, IncludeSearchPathType::Framework}),
Eq(IncludeSearchPath{"project/.pre_includes", 1, IncludeSearchPathType::System}),
Eq(IncludeSearchPath{CLANG_RESOURCE_DIR,
4,
ClangBackEnd::IncludeSearchPathType::BuiltIn})));
ASSERT_THAT(paths.project,
ElementsAre(Eq(IncludeSearchPath{includePath.path, 2, IncludeSearchPathType::User}),
Eq(IncludeSearchPath{includePath2.path, 1, IncludeSearchPathType::User})));

View File

@@ -38,6 +38,7 @@
#include <cpptools/compileroptionsbuilder.h>
#include <cpptools/projectpart.h>
#include <projectexplorer/project.h>
#include <utils/smallstringvector.h>
@@ -63,7 +64,26 @@ using Utils::SmallStringVector;
class RefactoringClient : public ::testing::Test
{
void SetUp();
void SetUp()
{
using Filter = ClangRefactoring::ClangQueryProjectsFindFilter;
client.setRefactoringEngine(&engine);
projectPart = CppTools::ProjectPart::Ptr(new CppTools::ProjectPart);
projectPart->project = &project;
projectPart->files.push_back(projectFile);
commandLine = Filter::compilerArguments(projectPart.data(), projectFile.kind);
client.setSearchHandle(&mockSearchHandle);
client.setExpectedResultCount(1);
ON_CALL(mockFilePathCaching, filePath(Eq(FilePathId{1})))
.WillByDefault(Return(FilePath(PathString("/path/to/file"))));
ON_CALL(mockFilePathCaching, filePath(Eq(FilePathId{42})))
.WillByDefault(Return(clangBackEndFilePath));
}
protected:
NiceMock<MockFilePathCaching> mockFilePathCaching;
@@ -84,6 +104,7 @@ protected:
Utils::FileName filePath{Utils::FileName::fromString(qStringFilePath)};
ClangBackEnd::FilePath clangBackEndFilePath{qStringFilePath};
SmallStringVector commandLine;
ProjectExplorer::Project project;
CppTools::ProjectPart::Ptr projectPart;
CppTools::ProjectFile projectFile{qStringFilePath, CppTools::ProjectFile::CXXSource};
SourceLocationsForRenamingMessage renameMessage{"symbol",
@@ -228,25 +249,4 @@ TEST_F(RefactoringClient, SetProgress)
client.progress({ClangBackEnd::ProgressType::Indexing, 10, 20});
}
void RefactoringClient::SetUp()
{
using Filter = ClangRefactoring::ClangQueryProjectsFindFilter;
client.setRefactoringEngine(&engine);
projectPart = CppTools::ProjectPart::Ptr(new CppTools::ProjectPart);
projectPart->files.push_back(projectFile);
commandLine = Filter::compilerArguments(projectPart.data(), projectFile.kind);
client.setSearchHandle(&mockSearchHandle);
client.setExpectedResultCount(1);
ON_CALL(mockFilePathCaching, filePath(Eq(FilePathId{1})))
.WillByDefault(Return(FilePath(PathString("/path/to/file"))));
ON_CALL(mockFilePathCaching, filePath(Eq(FilePathId{42})))
.WillByDefault(Return(clangBackEndFilePath));
}
}

View File

@@ -36,6 +36,7 @@
#include <cpptools/compileroptionsbuilder.h>
#include <cpptools/projectpart.h>
#include <projectexplorer/project.h>
#include <utils/smallstringvector.h>
@@ -56,7 +57,17 @@ using Utils::SmallStringVector;
class RefactoringEngine : public ::testing::Test
{
protected:
void SetUp();
void SetUp()
{
projectPart = CppTools::ProjectPart::Ptr(new CppTools::ProjectPart);
projectPart->project = &project;
projectPart->files.push_back(projectFile);
CompilerOptionsBuilder optionsBuilder(*projectPart);
commandLine = Utils::SmallStringVector(
optionsBuilder.build(projectFile.kind, CppTools::UsePrecompiledHeaders::No));
commandLine.push_back(qStringFilePath);
}
protected:
NiceMock<MockFilePathCaching> mockFilePathCaching;
@@ -74,6 +85,7 @@ protected:
Utils::FileName filePath{Utils::FileName::fromString(qStringFilePath)};
ClangBackEnd::FilePath clangBackEndFilePath{qStringFilePath};
SmallStringVector commandLine;
ProjectExplorer::Project project;
CppTools::ProjectPart::Ptr projectPart;
CppTools::ProjectFile projectFile{qStringFilePath, CppTools::ProjectFile::CXXSource};
};
@@ -143,17 +155,5 @@ TEST_F(RefactoringEngine, ServerIsUsableForUsableEngine)
ASSERT_TRUE(mockRefactoringServer.isAvailable());
}
void RefactoringEngine::SetUp()
{
projectPart = CppTools::ProjectPart::Ptr(new CppTools::ProjectPart);
projectPart->files.push_back(projectFile);
CompilerOptionsBuilder optionsBuilder(*projectPart);
commandLine = Utils::SmallStringVector(
optionsBuilder.build(projectFile.kind, CppTools::UsePrecompiledHeaders::No));
commandLine.push_back(qStringFilePath);
}
}

View File

@@ -41,6 +41,8 @@
#include <refactoringprojectupdater.h>
#include <projectexplorer/project.h>
#include <memory>
namespace {
@@ -64,6 +66,7 @@ protected:
ProjectPart::Ptr createProjectPart(const char *name)
{
ProjectPart::Ptr projectPart{new ProjectPart};
projectPart->project = &project;
projectPart->displayName = QString::fromUtf8(name, std::strlen(name));
projectPartId = projectPart->id();
return projectPart;
@@ -80,6 +83,7 @@ protected:
ClangPchManager::PchManagerClient pchManagerClient{mockPchCreationProgressManager,
mockDependencyCreationProgressManager};
MockCppModelManager mockCppModelManager;
ProjectExplorer::Project project;
ClangRefactoring::RefactoringProjectUpdater updater{mockRefactoringServer,
pchManagerClient,
mockCppModelManager,