ClangPchManager: Add UsedMacroFilter

The compiler macros are filtered in system and project macros. Not used
ones are removed. The original order of the macros is retained.

Task-number: QTCREATORBUG-21548
Change-Id: Ic9265866bde033e6a9600f9e6439b1697ab73422
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
This commit is contained in:
Marco Bubke
2018-11-21 20:11:07 +01:00
parent 696dedefa1
commit ce9f503691
27 changed files with 409 additions and 85 deletions

View File

@@ -36,10 +36,10 @@ class CompilerMacro
public:
constexpr CompilerMacro() = default;
CompilerMacro(Utils::SmallString &&key,
Utils::SmallString &&value)
: key(std::move(key)),
value(std::move(value))
CompilerMacro(Utils::SmallString &&key, Utils::SmallString &&value, int index)
: key(std::move(key))
, value(std::move(value))
, index(index)
{}
friend QDataStream &operator<<(QDataStream &out, const CompilerMacro &compilerMacro)
@@ -72,6 +72,7 @@ public:
public:
Utils::SmallString key;
Utils::SmallString value;
int index = 0;
};
using CompilerMacros = std::vector<CompilerMacro>;

View File

@@ -156,9 +156,10 @@ QStringList ProjectUpdater::compilerArguments(CppTools::ProjectPart *projectPart
ClangBackEnd::CompilerMacros ProjectUpdater::createCompilerMacros(const ProjectExplorer::Macros &projectMacros)
{
auto macros = Utils::transform<ClangBackEnd::CompilerMacros>(projectMacros,
[] (const ProjectExplorer::Macro &macro) {
return ClangBackEnd::CompilerMacro{macro.key, macro.value};
int index = 0;
auto macros = Utils::transform<ClangBackEnd::CompilerMacros>(
projectMacros, [&](const ProjectExplorer::Macro &macro) {
return ClangBackEnd::CompilerMacro{macro.key, macro.value, ++index};
});
std::sort(macros.begin(), macros.end());

View File

@@ -33,7 +33,8 @@ HEADERS += \
$$PWD/modifiedtimecheckerinterface.h \
$$PWD/sourceentry.h \
$$PWD/builddependenciesstorage.h \
$$PWD/builddependencygeneratorinterface.h
$$PWD/builddependencygeneratorinterface.h \
$$PWD/usedmacrofilter.h
!isEmpty(LIBTOOLING_LIBS) {
SOURCES += \

View File

@@ -117,9 +117,11 @@ public:
sourceType = SourceType::SystemInclude;
else
sourceType = SourceType::TopSystemInclude;
} else if (isNotInExcludedIncludeUID(fileUID)
&& isInExcludedIncludeUID(sourceFileUID)) {
sourceType = SourceType::TopInclude;
} else if (isNotInExcludedIncludeUID(fileUID)) {
if (isInExcludedIncludeUID(sourceFileUID))
sourceType = SourceType::TopProjectInclude;
else
sourceType = SourceType::ProjectInclude;
}
addInclude({includeId, sourceType, lastModified});

View File

@@ -398,7 +398,7 @@ FilePathIds PchCreator::topIncludeIds(const SourceEntries &includes)
topIncludes.reserve(includes.size());
for (SourceEntry include : includes) {
if (include.sourceType == SourceType::TopInclude)
if (include.sourceType == SourceType::TopProjectInclude)
topIncludes.push_back(include.sourceId);
}

View File

@@ -33,9 +33,10 @@ namespace ClangBackEnd {
enum class SourceType : unsigned char
{
TopInclude,
TopProjectInclude,
TopSystemInclude,
UserInclude,
ProjectInclude,
SystemInclude
};

View File

@@ -0,0 +1,194 @@
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "usedmacro.h"
#include "sourceentry.h"
#include <compilermacro.h>
namespace ClangBackEnd {
class UsedMacroFilter
{
public:
struct Includes
{
FilePathIds project;
FilePathIds system;
};
UsedMacroFilter(const SourceEntries &includes, const UsedMacros &usedMacros)
{
Includes filteredIncludes = filterIncludes(includes);
m_systemUsedMacros = filterUsedMarcos(usedMacros, filteredIncludes.system);
m_projectUsedMacros = filterUsedMarcos(usedMacros, filteredIncludes.project);
}
static Includes filterIncludes(const SourceEntries &includes)
{
Includes result;
result.system.reserve(includes.size());
result.project.reserve(includes.size());
for (SourceEntry include : includes)
filterInclude(include, result);
return result;
}
const Utils::PathStringVector &projectUsedMacros() const { return m_projectUsedMacros; }
const Utils::PathStringVector &systemUsedMacros() const { return m_systemUsedMacros; }
const CompilerMacros &projectCompilerMacros() const { return m_projectCompilerMacros; }
const CompilerMacros &systemCompilerMacros() const { return m_systemCompilerMacros; }
void filter(const CompilerMacros &compilerMacros)
{
CompilerMacros indexedCompilerMacro = compilerMacros;
std::sort(indexedCompilerMacro.begin(),
indexedCompilerMacro.end(),
[](const CompilerMacro &first, const CompilerMacro &second) {
return std::tie(first.key, first.value) < std::tie(second.key, second.value);
});
m_systemCompilerMacros = filtercompilerMacros(indexedCompilerMacro, m_systemUsedMacros);
m_projectCompilerMacros = filtercompilerMacros(indexedCompilerMacro, m_projectUsedMacros);
}
private:
static void filterInclude(SourceEntry include, Includes &result)
{
switch (include.sourceType) {
case SourceType::TopSystemInclude:
case SourceType::SystemInclude:
result.system.emplace_back(include.sourceId);
break;
case SourceType::TopProjectInclude:
case SourceType::ProjectInclude:
result.project.emplace_back(include.sourceId);
break;
case SourceType::UserInclude:
break;
}
}
static Utils::PathStringVector filterUsedMarcos(const UsedMacros &usedMacros,
const FilePathIds &filePathId)
{
class BackInserterIterator : public std::back_insert_iterator<Utils::PathStringVector>
{
public:
BackInserterIterator(Utils::PathStringVector &container)
: std::back_insert_iterator<Utils::PathStringVector>(container)
{}
BackInserterIterator &operator=(const UsedMacro &usedMacro)
{
container->push_back(usedMacro.macroName);
return *this;
}
BackInserterIterator &operator*() { return *this; }
};
struct Compare
{
bool operator()(const UsedMacro &usedMacro, FilePathId filePathId)
{
return usedMacro.filePathId < filePathId;
}
bool operator()(FilePathId filePathId, const UsedMacro &usedMacro)
{
return filePathId < usedMacro.filePathId;
}
};
Utils::PathStringVector filtertedMacros;
filtertedMacros.reserve(usedMacros.size());
std::set_intersection(usedMacros.begin(),
usedMacros.end(),
filePathId.begin(),
filePathId.end(),
BackInserterIterator(filtertedMacros),
Compare{});
std::sort(filtertedMacros.begin(), filtertedMacros.end());
return filtertedMacros;
}
static CompilerMacros filtercompilerMacros(const CompilerMacros &indexedCompilerMacro,
const Utils::PathStringVector &usedMacros)
{
struct Compare
{
bool operator()(const Utils::PathString &usedMacro,
const CompilerMacro &compileMacro)
{
return usedMacro < compileMacro.key;
}
bool operator()(const CompilerMacro &compileMacro,
const Utils::PathString &usedMacro)
{
return compileMacro.key < usedMacro;
}
};
CompilerMacros filtertedCompilerMacros;
filtertedCompilerMacros.reserve(indexedCompilerMacro.size());
std::set_intersection(indexedCompilerMacro.begin(),
indexedCompilerMacro.end(),
usedMacros.begin(),
usedMacros.end(),
std::back_inserter(filtertedCompilerMacros),
Compare{});
std::sort(filtertedCompilerMacros.begin(),
filtertedCompilerMacros.end(),
[](const CompilerMacro &first, const CompilerMacro &second) {
return first.index < second.index;
});
return filtertedCompilerMacros;
}
private:
Utils::PathStringVector m_projectUsedMacros;
Utils::PathStringVector m_systemUsedMacros;
CompilerMacros m_projectCompilerMacros;
CompilerMacros m_systemCompilerMacros;
};
} // namespace ClangBackEnd

View File

@@ -62,8 +62,9 @@ CompilerMacros ProjectPartArtefact::createCompilerMacrosFromDocument(const QJson
CompilerMacros macros;
macros.reserve(object.size());
int index = 0;
for (auto current = object.constBegin(); current != object.constEnd(); ++current)
macros.emplace_back(current.key(), current.value().toString());
macros.emplace_back(current.key(), current.value().toString(), ++index);
std::sort(macros.begin(), macros.end());

View File

@@ -65,13 +65,13 @@ protected:
mockSqliteTransactionBackend};
ClangBackEnd::V2::ProjectPartContainer projectPart1{"ProjectPart1",
{"--yi"},
{{"YI", "1"}},
{{"YI", "1", 1}},
{"/yi"},
{1},
{2}};
ClangBackEnd::V2::ProjectPartContainer projectPart2{"ProjectPart2",
{"--er"},
{{"ER", "2"}},
{{"ER", "2", 1}},
{"/er"},
{1},
{2, 3, 4}};

View File

@@ -168,7 +168,7 @@ TEST_F(BuildDependenciesStorage, AddNewSourceDependenciesTable)
TEST_F(BuildDependenciesStorage, UpdateSources)
{
InSequence s;
SourceEntries entries{{1, SourceType::TopInclude, 10}, {2, SourceType::TopSystemInclude, 20}};
SourceEntries entries{{1, SourceType::TopProjectInclude, 10}, {2, SourceType::TopSystemInclude, 20}};
EXPECT_CALL(updateBuildDependencyTimeStampStatement, write(TypedEq<long long>(10), TypedEq<int>(1)));
EXPECT_CALL(updateSourceTypeStatement, write(TypedEq<uchar>(0), TypedEq<int>(1)));
@@ -205,7 +205,7 @@ TEST_F(BuildDependenciesStorage, FetchDependSourcesWithNonExistingProjectPartRet
TEST_F(BuildDependenciesStorage, FetchDependSourcesWithExistingProjectPartReturnsSourceEntries)
{
SourceEntries sourceEntries{{1, SourceType::TopInclude, 10}, {2, SourceType::TopSystemInclude, 20}};
SourceEntries sourceEntries{{1, SourceType::TopProjectInclude, 10}, {2, SourceType::TopSystemInclude, 20}};
EXPECT_CALL(fetchProjectPartIdStatement, valueReturnInt32(TypedEq<Utils::SmallStringView>("test"))).WillOnce(Return(Utils::optional<int>{20}));
EXPECT_CALL(fetchSourceDependenciesStatement, valuesReturnSourceEntries(_, 22, 20)).WillOnce(Return(sourceEntries));

View File

@@ -66,19 +66,33 @@ protected:
{
setFilePathCache(&filePathCache);
collector.addFile(id(TESTDATA_DIR "/builddependencycollector/project/main.cpp"), {"cc", "-I", TESTDATA_DIR "/builddependencycollector/external", "-I", TESTDATA_DIR "/builddependencycollector/project", "-isystem", TESTDATA_DIR "/builddependencycollector/system"});
collector.addFile(id(TESTDATA_DIR "/builddependencycollector/project/main2.cpp"), {"cc", "-I", TESTDATA_DIR "/builddependencycollector/external", "-I", TESTDATA_DIR "/builddependencycollector/project", "-isystem", TESTDATA_DIR "/builddependencycollector/system"});
collector.addFile(id(TESTDATA_DIR "/builddependencycollector/project/main.cpp"),
{"cc",
"-I",
TESTDATA_DIR "/builddependencycollector/external",
"-I",
TESTDATA_DIR "/builddependencycollector/project",
"-isystem",
TESTDATA_DIR "/builddependencycollector/system"});
collector.addFile(id(TESTDATA_DIR "/builddependencycollector/project/main2.cpp"),
{"cc",
"-I",
TESTDATA_DIR "/builddependencycollector/external",
"-I",
TESTDATA_DIR "/builddependencycollector/project",
"-isystem",
TESTDATA_DIR "/builddependencycollector/system"});
collector.addUnsavedFiles({{{TESTDATA_DIR, "BuildDependencyCollector/project/generated_file.h"}, "#pragma once", {}}});
collector.addUnsavedFiles(
{{{TESTDATA_DIR, "BuildDependencyCollector/project/generated_file.h"},
"#pragma once",
{}}});
collector.setExcludedFilePaths(Utils::clone(excludePaths));
emptyCollector.setExcludedFilePaths(Utils::clone(excludePaths));
}
~BuildDependencyCollector()
{
setFilePathCache(nullptr);
}
~BuildDependencyCollector() { setFilePathCache(nullptr); }
FilePathId id(const Utils::SmallStringView &path) const
{
@@ -100,8 +114,7 @@ protected:
return {id(filePath), fileSize(filePath), lastModified(filePath), false};
}
static
FilePathIds filteredIncludes(const ClangBackEnd::SourceEntries &includes,
static FilePathIds filteredIncludes(const ClangBackEnd::SourceEntries &includes,
ClangBackEnd::SourceType includeType)
{
FilePathIds filteredIncludes;
@@ -114,26 +127,27 @@ protected:
return filteredIncludes;
}
static
FilePathIds topIncludes(const ClangBackEnd::SourceEntries &includes)
static FilePathIds topIncludes(const ClangBackEnd::SourceEntries &includes)
{
return filteredIncludes(includes, ClangBackEnd::SourceType::TopInclude);
return filteredIncludes(includes, ClangBackEnd::SourceType::TopProjectInclude);
}
static
FilePathIds systemTopIncludes(const ClangBackEnd::SourceEntries &includes)
static FilePathIds systemTopIncludes(const ClangBackEnd::SourceEntries &includes)
{
return filteredIncludes(includes, ClangBackEnd::SourceType::TopSystemInclude);
}
static
FilePathIds userIncludes(const ClangBackEnd::SourceEntries &includes)
static FilePathIds userIncludes(const ClangBackEnd::SourceEntries &includes)
{
return filteredIncludes(includes, ClangBackEnd::SourceType::UserInclude);
}
static
FilePathIds allIncludes(const ClangBackEnd::SourceEntries &includes)
static FilePathIds projectPartIncludes(const ClangBackEnd::SourceEntries &includes)
{
return filteredIncludes(includes, ClangBackEnd::SourceType::ProjectInclude);
}
static FilePathIds allIncludes(const ClangBackEnd::SourceEntries &includes)
{
FilePathIds filteredIncludes;
@@ -564,17 +578,17 @@ TEST_F(BuildDependencyCollector, Create)
HasInclude(id(TESTDATA_DIR "/builddependencycollector/project/header2.h"),
SourceType::UserInclude),
HasInclude(id(TESTDATA_DIR "/builddependencycollector/external/external3.h"),
SourceType::TopInclude),
SourceType::TopProjectInclude),
HasInclude(id(TESTDATA_DIR "/builddependencycollector/external/external1.h"),
SourceType::TopInclude),
SourceType::TopProjectInclude),
HasInclude(id(TESTDATA_DIR
"/builddependencycollector/external/indirect_external.h"),
SourceType::UserInclude),
SourceType::ProjectInclude),
HasInclude(id(TESTDATA_DIR
"/builddependencycollector/external/indirect_external2.h"),
SourceType::UserInclude),
SourceType::ProjectInclude),
HasInclude(id(TESTDATA_DIR "/builddependencycollector/external/external2.h"),
SourceType::TopInclude),
SourceType::TopProjectInclude),
HasInclude(id(TESTDATA_DIR "/builddependencycollector/system/system1.h"),
SourceType::TopSystemInclude),
HasInclude(id(TESTDATA_DIR

View File

@@ -1053,12 +1053,14 @@ const char *sourceTypeString(SourceType sourceType)
using ClangBackEnd::SymbolTag;
switch (sourceType) {
case SourceType::TopInclude:
return "TopInclude";
case SourceType::TopProjectInclude:
return "TopProjectInclude";
case SourceType::TopSystemInclude:
return "TopSystemInclude";
case SourceType::SystemInclude:
return "SystemInclude";
case SourceType::ProjectInclude:
return "ProjectInclude";
case SourceType::UserInclude:
return "UserInclude";
}

View File

@@ -100,13 +100,13 @@ protected:
ClangBackEnd::PchCreator creator{environment, database, mockPchManagerClient, mockClangPathWatcher};
ProjectPartContainer projectPart1{"project1",
{"-I", TESTDATA_DIR, "-I", TESTDATA_DIR "/builddependencycollector/external", "-I", TESTDATA_DIR "/builddependencycollector/project", "-isystem", TESTDATA_DIR "/builddependencycollector/system", "-Wno-pragma-once-outside-header"},
{{"DEFINE", "1"}},
{{"DEFINE", "1", 1}},
{TESTDATA_DIR "/builddependencycollector/external", TESTDATA_DIR "/builddependencycollector/project"},
{id(header1Path)},
{id(main1Path)}};
ProjectPartContainer projectPart2{"project2",
{"-I", TESTDATA_DIR, "-I", TESTDATA_DIR "/builddependencycollector/external", "-I", TESTDATA_DIR "/builddependencycollector/project", "-x", "c++-header", "-Wno-pragma-once-outside-header"},
{{"DEFINE", "1"}},
{{"DEFINE", "1", 1}},
{TESTDATA_DIR "/builddependencycollector/external", TESTDATA_DIR "/builddependencycollector/project"},
{id(header2Path)},
{id(main2Path)}};
@@ -153,7 +153,7 @@ TEST_F(PchCreatorSlowTest, CreateProjectPartPchIncludes)
AllOf(
Contains(HasIdAndType(
id(TESTDATA_DIR "/builddependencycollector/project/header2.h"),
SourceType::TopInclude)),
SourceType::TopProjectInclude)),
Contains(HasIdAndType(
id(TESTDATA_DIR "/builddependencycollector/system/system1.h"),
SourceType::TopSystemInclude)),
@@ -164,11 +164,11 @@ TEST_F(PchCreatorSlowTest, CreateProjectPartPchIncludes)
Contains(HasIdAndType(
id(TESTDATA_DIR
"/builddependencycollector/external/external1.h"),
SourceType::TopInclude)),
SourceType::TopProjectInclude)),
Contains(HasIdAndType(
id(TESTDATA_DIR
"/builddependencycollector/external/external2.h"),
SourceType::TopInclude))));
SourceType::TopProjectInclude))));
}
TEST_F(PchCreatorSlowTest, CreateProjectPartPchFileContent)
@@ -294,7 +294,7 @@ TEST_F(PchCreatorVerySlowTest, FaultyProjectPartPchForCreatesNoPchForProjectPart
{
ProjectPartContainer faultyProjectPart{"faultyProject",
{"-I", TESTDATA_DIR},
{{"DEFINE", "1"}},
{{"DEFINE", "1", 1}},
{"/includes"},
{},
{id(TESTDATA_DIR "/builddependencycollector/project/faulty.cpp")}};

View File

@@ -99,7 +99,7 @@ TEST_F(PchManagerClientServerInProcess, SendUpdateProjectPartsMessage)
{
ProjectPartContainer projectPart2{"projectPartId",
{"-x", "c++-header", "-Wno-pragma-once-outside-header"},
{{"DEFINE", "1"}},
{{"DEFINE", "1", 1}},
{"/includes"},
{{1, 1}},
{{1, 2}}};

View File

@@ -82,13 +82,13 @@ protected:
ClangBackEnd::IdPaths idPath{projectPartId1, {1, 2}};
ProjectPartContainer projectPart1{projectPartId1.clone(),
{"-I", TESTDATA_DIR, "-Wno-pragma-once-outside-header"},
{{"DEFINE", "1"}},
{{"DEFINE", "1", 1}},
{"/includes"},
{id(header1Path)},
{id(main1Path)}};
ProjectPartContainer projectPart2{projectPartId2.clone(),
{"-x", "c++-header", "-Wno-pragma-once-outside-header"},
{{"DEFINE", "1"}},
{{"DEFINE", "1", 1}},
{"/includes"},
{id(header2Path)},
{id(main2Path)}};

View File

@@ -45,7 +45,7 @@ protected:
ClangBackEnd::PchTaskGenerator generator{mockBuildDependenciesProvider};
ClangBackEnd::V2::ProjectPartContainer projectPart1{"ProjectPart1",
{"--yi"},
{{"YI","1"}},
{{"YI", "1", 1}},
{"/yi"},
{{1, 1}},
{{1, 2}}};

View File

@@ -56,7 +56,7 @@ TEST(ProjectPartArtefact, CompilerMacros)
ClangBackEnd::ProjectPartArtefact artefact{"", "{\"Foo\":\"1\",\"Bar\":\"42\"}", "", 1};
ASSERT_THAT(artefact.compilerMacros,
UnorderedElementsAre(Eq(CompilerMacro{"Foo", "1"}), Eq(CompilerMacro{"Bar", "42"})));
UnorderedElementsAre(Eq(CompilerMacro{"Foo", "1", 1}), Eq(CompilerMacro{"Bar", "42", 2})));
}
TEST(ProjectPartArtefact, EmptyCompilerMacros)

View File

@@ -46,25 +46,25 @@ protected:
ClangBackEnd::ProjectPartQueue queue{mockTaskScheduler, mockPrecompiledHeaderStorage, mockSqliteTransactionBackend, progressCounter};
ClangBackEnd::V2::ProjectPartContainer projectPart1{"ProjectPart1",
{"--yi"},
{{"YI","1"}},
{{"YI","1", 1}},
{"/yi"},
{1},
{2}};
ClangBackEnd::V2::ProjectPartContainer projectPart2{"ProjectPart2",
{"--er"},
{{"ER","2"}},
{{"ER","2", 1}},
{"/bar"},
{1},
{2}};
ClangBackEnd::V2::ProjectPartContainer projectPart2b{"ProjectPart2",
{"--liang"},
{{"LIANG","3"}},
{{"LIANG","3", 1}},
{"/liang"},
{3},
{2, 4}};
ClangBackEnd::V2::ProjectPartContainer projectPart3{"ProjectPart3",
{"--san"},
{{"SAN","2"}},
{{"SAN","2", 1}},
{"/SAN"},
{1},
{2}};

View File

@@ -49,19 +49,19 @@ protected:
FilePathId thirdSource{13};
ProjectPartContainer projectPartContainer1{"id",
{"-DUNIX", "-O2"},
{{"DEFINE", "1"}},
{{"DEFINE", "1", 1}},
{"/includes"},
{firstHeader, secondHeader},
{firstSource, secondSource}};
ProjectPartContainer updatedProjectPartContainer1{"id",
{"-DUNIX", "-O2"},
{{"DEFINE", "1"}},
{{"DEFINE", "1", 1}},
{"/includes"},
{firstHeader, secondHeader},
{firstSource, secondSource, thirdSource}};
ProjectPartContainer projectPartContainer2{"id2",
{"-DUNIX", "-O2"},
{{"DEFINE", "1"}},
{{"DEFINE", "1", 1}},
{"/includes"},
{firstHeader, secondHeader},
{firstSource, secondSource}};

View File

@@ -128,7 +128,7 @@ protected:
Utils::SmallString projectPartId2;
Utils::PathStringVector headerPaths = {"/path/to/header1.h", "/path/to/header2.h"};
Utils::PathStringVector sourcePaths = {"/path/to/source1.cpp", "/path/to/source2.cpp"};
ClangBackEnd::CompilerMacros compilerMacros = {{"BAR", "1"}, {"FOO", "2"}};
ClangBackEnd::CompilerMacros compilerMacros = {{"BAR", "1", 1}, {"FOO", "2", 2}};
CppTools::ProjectFile header1ProjectFile{QString(headerPaths[0]), CppTools::ProjectFile::CXXHeader};
CppTools::ProjectFile header2ProjectFile{QString(headerPaths[1]), CppTools::ProjectFile::CXXHeader};
CppTools::ProjectFile source1ProjectFile{QString(sourcePaths[0]), CppTools::ProjectFile::CXXSource};
@@ -246,9 +246,9 @@ TEST_F(ProjectUpdater, CreateSortedCompilerMacros)
{
auto paths = updater.createCompilerMacros({{"DEFINE", "1"}, {"FOO", "2"}, {"BAR", "1"}});
ASSERT_THAT(paths, ElementsAre(CompilerMacro{"BAR", "1"},
CompilerMacro{"FOO", "2"},
CompilerMacro{"DEFINE", "1"}));
ASSERT_THAT(paths, ElementsAre(CompilerMacro{"BAR", "1", 1},
CompilerMacro{"FOO", "2", 2},
CompilerMacro{"DEFINE", "1", 3}));
}
TEST_F(ProjectUpdater, CreateSortedIncludeSearchPaths)

View File

@@ -191,7 +191,7 @@ TEST_F(RefactoringClientServerInProcess, SendUpdateProjectPartsMessage)
{
ProjectPartContainer projectPart2{"projectPartId",
{"-x", "c++-header", "-Wno-pragma-once-outside-header"},
{{"DEFINE", "1"}},
{{"DEFINE", "1", 1}},
{"/includes"},
{{1, 1}},
{{1, 2}}};

View File

@@ -326,7 +326,7 @@ TEST_F(RefactoringServer, UpdateProjectPartsCallsSymbolIndexingUpdateProjectPart
{
ProjectPartContainers projectParts{{{"projectPartId",
{"-I", TESTDATA_DIR},
{{"DEFINE", "1"}},
{{"DEFINE", "1", 1}},
{"/includes"},
{filePathId("header1.h")},
{filePathId("main.cpp")}}}};

View File

@@ -166,19 +166,19 @@ protected:
ClangBackEnd::FilePathId generatedFilePathId21;
ProjectPartContainer projectPart1{"project1",
{"-I", TESTDATA_DIR, "-Wno-pragma-once-outside-header"},
{{"BAR", "1"}, {"FOO", "1"}},
{{"BAR", "1", 1}, {"FOO", "1", 2}},
{"/includes"},
{header1PathId},
{main1PathId}};
ProjectPartContainer projectPart2{"project2",
{"-I", TESTDATA_DIR, "-x", "c++-header", "-Wno-pragma-once-outside-header"},
{{"BAR", "1"}, {"FOO", "0"}},
{{"BAR", "1", 1}, {"FOO", "0", 2}},
{"/includes"},
{header2PathId},
{main2PathId}};
ProjectPartContainer projectPart3{"project3",
{"-I", TESTDATA_DIR, "-Wno-pragma-once-outside-header"},
{{"BAR", "1"}, {"FOO", "1"}},
{{"BAR", "1", 1}, {"FOO", "1", 2}},
{"/includes", "/other/includes"},
{header1PathId},
{main1PathId}};
@@ -315,11 +315,11 @@ TEST_F(SymbolIndexer, UpdateProjectPartsCallsUpdateProjectPartsInStorage)
{
EXPECT_CALL(mockSymbolStorage, insertOrUpdateProjectPart(Eq("project1"),
ElementsAre("-I", TESTDATA_DIR, "-Wno-pragma-once-outside-header"),
ElementsAre(CompilerMacro{"BAR", "1"}, CompilerMacro{"FOO", "1"}),
ElementsAre(CompilerMacro{"BAR", "1", 1}, CompilerMacro{"FOO", "1", 2}),
ElementsAre("/includes")));
EXPECT_CALL(mockSymbolStorage, insertOrUpdateProjectPart(Eq("project2"),
ElementsAre("-I", TESTDATA_DIR, "-x", "c++-header", "-Wno-pragma-once-outside-header"),
ElementsAre(CompilerMacro{"BAR", "1"}, CompilerMacro{"FOO", "0"}),
ElementsAre(CompilerMacro{"BAR", "1", 1}, CompilerMacro{"FOO", "0", 2}),
ElementsAre("/includes")));
indexer.updateProjectParts({projectPart1, projectPart2});
@@ -535,7 +535,7 @@ TEST_F(SymbolIndexer, IncludeSearchPathsAreDifferent)
{
ProjectPartContainer projectPart3{"project3",
{"-I", TESTDATA_DIR, "-Wno-pragma-once-outside-header"},
{{"BAR", "1"}, {"FOO", "1"}},
{{"BAR", "1", 1}, {"FOO", "1", 2}},
{"/includes", "/other/includes"},
{header1PathId},
{main1PathId}};

View File

@@ -89,7 +89,7 @@ protected:
PathString main1Path = TESTDATA_DIR "/symbolindexing_main1.cpp";
ProjectPartContainer projectPart1{"project1",
{"cc", "-I", TESTDATA_DIR, "-std=c++1z"},
{{"DEFINE", "1"}},
{{"DEFINE", "1", 1}},
{"/includes"},
{},
{filePathId(main1Path)}};

View File

@@ -180,7 +180,7 @@ TEST_F(SymbolStorage, InsertProjectPart)
TypedEq<Utils::SmallStringView>("[\"/includes\"]")));
EXPECT_CALL(mockDatabase, lastInsertedRowId()).Times(2);
storage.insertOrUpdateProjectPart("project", {"foo"}, {{"FOO", "1"}}, {"/includes"});
storage.insertOrUpdateProjectPart("project", {"foo"}, {{"FOO", "1", 1}}, {"/includes"});
}
TEST_F(SymbolStorage, UpdateProjectPart)
@@ -202,7 +202,7 @@ TEST_F(SymbolStorage, UpdateProjectPart)
TypedEq<Utils::SmallStringView>("project")));
EXPECT_CALL(mockDatabase, lastInsertedRowId());
storage.insertOrUpdateProjectPart("project", {"foo"}, {{"FOO", "1"}}, {"/includes"});
storage.insertOrUpdateProjectPart("project", {"foo"}, {{"FOO", "1", 1}}, {"/includes"});
}
TEST_F(SymbolStorage, UpdateProjectPartSources)

View File

@@ -107,7 +107,8 @@ SOURCES += \
pchtaskgenerator-test.cpp \
compilationdatabaseutils-test.cpp \
builddependenciesprovider-test.cpp \
builddependenciesstorage-test.cpp
builddependenciesstorage-test.cpp \
usedmacrofilter-test.cpp
!isEmpty(LIBCLANG_LIBS) {
SOURCES += \

View File

@@ -0,0 +1,106 @@
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "googletest.h"
#include <usedmacrofilter.h>
namespace {
using ClangBackEnd::FilePathId;
using ClangBackEnd::SourceEntries;
using ClangBackEnd::SourceEntry;
using ClangBackEnd::SourceType;
using ClangBackEnd::UsedMacro;
using ClangBackEnd::UsedMacros;
using ClangBackEnd::CompilerMacro;
using ClangBackEnd::CompilerMacros;
class UsedMacroFilter : public testing::Test
{
protected:
SourceEntries includes{{1, SourceType::UserInclude, 0},
{2, SourceType::SystemInclude, 0},
{3, SourceType::ProjectInclude, 0},
{4, SourceType::TopSystemInclude, 0},
{5, SourceType::TopProjectInclude, 0}};
UsedMacros usedMacros{{"YI", 1}, {"ER", 2}, {"SAN", 3}, {"SE", 4}, {"WU", 5}};
CompilerMacros compileMacros{{"YI", "1", 1},
{"ER", "2", 2},
{"SAN", "3", 3},
{"SE", "4", 4},
{"WU", "5", 5},
{"LIANG", "2", 6}};
};
TEST_F(UsedMacroFilter, SystemIncludes)
{
auto result = ClangBackEnd::UsedMacroFilter::filterIncludes(includes);
ASSERT_THAT(result.system, ElementsAre(FilePathId{2}, FilePathId{4}));
}
TEST_F(UsedMacroFilter, ProjectIncludes)
{
auto result = ClangBackEnd::UsedMacroFilter::filterIncludes(includes);
ASSERT_THAT(result.project, ElementsAre(FilePathId{3}, FilePathId{5}));
}
TEST_F(UsedMacroFilter, SystemUsedMacros)
{
ClangBackEnd::UsedMacroFilter filter(includes, usedMacros);
ASSERT_THAT(filter.systemUsedMacros(), ElementsAre("ER", "SE"));
}
TEST_F(UsedMacroFilter, ProjectUsedMacros)
{
ClangBackEnd::UsedMacroFilter filter(includes, usedMacros);
ASSERT_THAT(filter.projectUsedMacros(), ElementsAre("WU", "SAN"));
}
TEST_F(UsedMacroFilter, SystemCompileMacros)
{
ClangBackEnd::UsedMacroFilter filter(includes, usedMacros);
filter.filter(compileMacros);
ASSERT_THAT(filter.systemCompilerMacros(),
ElementsAre(CompilerMacro{"ER", "2", 2}, CompilerMacro{"SE", "4", 4}));
}
TEST_F(UsedMacroFilter, ProjectCompileMacros)
{
ClangBackEnd::UsedMacroFilter filter(includes, usedMacros);
filter.filter(compileMacros);
ASSERT_THAT(filter.projectCompilerMacros(),
ElementsAre(CompilerMacro{"SAN", "3", 3}, CompilerMacro{"WU", "5", 5}));
}
} // namespace