diff options
author | Marco Bubke <marco.bubke@qt.io> | 2018-11-21 20:11:07 +0100 |
---|---|---|
committer | Marco Bubke <marco.bubke@qt.io> | 2018-12-03 12:51:25 +0000 |
commit | ce9f503691dd943fb0f376a91b77b1cf4e1bbc9e (patch) | |
tree | ef3b845623d95711c0116f21d8946b523aceefca | |
parent | 696dedefa14d46e4227b4c7b1e68e258ad1b68c8 (diff) |
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>
27 files changed, 410 insertions, 86 deletions
diff --git a/src/libs/clangsupport/compilermacro.h b/src/libs/clangsupport/compilermacro.h index bfd993bd77..ddba1a4076 100644 --- a/src/libs/clangsupport/compilermacro.h +++ b/src/libs/clangsupport/compilermacro.h @@ -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>; diff --git a/src/plugins/clangpchmanager/projectupdater.cpp b/src/plugins/clangpchmanager/projectupdater.cpp index a3fbb6cb62..7929059850 100644 --- a/src/plugins/clangpchmanager/projectupdater.cpp +++ b/src/plugins/clangpchmanager/projectupdater.cpp @@ -156,10 +156,11 @@ QStringList ProjectUpdater::compilerArguments(CppTools::ProjectPart *projectPart ClangBackEnd::CompilerMacros ProjectUpdater::createCompilerMacros(const ProjectExplorer::Macros &projectMacros) { - auto macros = Utils::transform<ClangBackEnd::CompilerMacros>(projectMacros, - [] (const ProjectExplorer::Macro ¯o) { - return ClangBackEnd::CompilerMacro{macro.key, macro.value}; - }); + int index = 0; + auto macros = Utils::transform<ClangBackEnd::CompilerMacros>( + projectMacros, [&](const ProjectExplorer::Macro ¯o) { + return ClangBackEnd::CompilerMacro{macro.key, macro.value, ++index}; + }); std::sort(macros.begin(), macros.end()); diff --git a/src/tools/clangpchmanagerbackend/source/clangpchmanagerbackend-source.pri b/src/tools/clangpchmanagerbackend/source/clangpchmanagerbackend-source.pri index b81b6cd9b5..fa88f59a8e 100644 --- a/src/tools/clangpchmanagerbackend/source/clangpchmanagerbackend-source.pri +++ b/src/tools/clangpchmanagerbackend/source/clangpchmanagerbackend-source.pri @@ -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 += \ diff --git a/src/tools/clangpchmanagerbackend/source/collectbuilddependencypreprocessorcallbacks.h b/src/tools/clangpchmanagerbackend/source/collectbuilddependencypreprocessorcallbacks.h index 0962129cb6..8253092751 100644 --- a/src/tools/clangpchmanagerbackend/source/collectbuilddependencypreprocessorcallbacks.h +++ b/src/tools/clangpchmanagerbackend/source/collectbuilddependencypreprocessorcallbacks.h @@ -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}); diff --git a/src/tools/clangpchmanagerbackend/source/pchcreator.cpp b/src/tools/clangpchmanagerbackend/source/pchcreator.cpp index 88acf8bb69..0e58621792 100644 --- a/src/tools/clangpchmanagerbackend/source/pchcreator.cpp +++ b/src/tools/clangpchmanagerbackend/source/pchcreator.cpp @@ -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); } diff --git a/src/tools/clangpchmanagerbackend/source/sourceentry.h b/src/tools/clangpchmanagerbackend/source/sourceentry.h index 6859e838ad..3cc0fb455a 100644 --- a/src/tools/clangpchmanagerbackend/source/sourceentry.h +++ b/src/tools/clangpchmanagerbackend/source/sourceentry.h @@ -33,9 +33,10 @@ namespace ClangBackEnd { enum class SourceType : unsigned char { - TopInclude, + TopProjectInclude, TopSystemInclude, UserInclude, + ProjectInclude, SystemInclude }; diff --git a/src/tools/clangpchmanagerbackend/source/usedmacrofilter.h b/src/tools/clangpchmanagerbackend/source/usedmacrofilter.h new file mode 100644 index 0000000000..1f6b30e80b --- /dev/null +++ b/src/tools/clangpchmanagerbackend/source/usedmacrofilter.h @@ -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 diff --git a/src/tools/clangrefactoringbackend/source/projectpartartefact.cpp b/src/tools/clangrefactoringbackend/source/projectpartartefact.cpp index 1cbe47e4fb..13856e1411 100644 --- a/src/tools/clangrefactoringbackend/source/projectpartartefact.cpp +++ b/src/tools/clangrefactoringbackend/source/projectpartartefact.cpp @@ -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()); diff --git a/tests/unit/unittest/builddependenciesprovider-test.cpp b/tests/unit/unittest/builddependenciesprovider-test.cpp index 64a9247828..cb39ec2bdf 100644 --- a/tests/unit/unittest/builddependenciesprovider-test.cpp +++ b/tests/unit/unittest/builddependenciesprovider-test.cpp @@ -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}}; diff --git a/tests/unit/unittest/builddependenciesstorage-test.cpp b/tests/unit/unittest/builddependenciesstorage-test.cpp index 77feb21bd7..d69f8b868a 100644 --- a/tests/unit/unittest/builddependenciesstorage-test.cpp +++ b/tests/unit/unittest/builddependenciesstorage-test.cpp @@ -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)); diff --git a/tests/unit/unittest/builddependencycollector-test.cpp b/tests/unit/unittest/builddependencycollector-test.cpp index 5a4a4c1968..7a65361edb 100644 --- a/tests/unit/unittest/builddependencycollector-test.cpp +++ b/tests/unit/unittest/builddependencycollector-test.cpp @@ -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.addUnsavedFiles({{{TESTDATA_DIR, "BuildDependencyCollector/project/generated_file.h"}, "#pragma once", {}}}); + 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.setExcludedFilePaths(Utils::clone(excludePaths)); emptyCollector.setExcludedFilePaths(Utils::clone(excludePaths)); } - ~BuildDependencyCollector() - { - setFilePathCache(nullptr); - } + ~BuildDependencyCollector() { setFilePathCache(nullptr); } FilePathId id(const Utils::SmallStringView &path) const { @@ -90,7 +104,7 @@ protected: return QFileInfo(QString(filePath)).size(); } - static std::time_t lastModified(Utils::SmallStringView filePath) + static std::time_t lastModified(Utils::SmallStringView filePath) { return QFileInfo(QString(filePath)).lastModified().toTime_t(); } @@ -100,9 +114,8 @@ protected: return {id(filePath), fileSize(filePath), lastModified(filePath), false}; } - static - FilePathIds filteredIncludes(const ClangBackEnd::SourceEntries &includes, - ClangBackEnd::SourceType includeType) + static FilePathIds filteredIncludes(const ClangBackEnd::SourceEntries &includes, + ClangBackEnd::SourceType includeType) { FilePathIds filteredIncludes; @@ -114,31 +127,32 @@ 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; for (const ClangBackEnd::SourceEntry &include : includes) - filteredIncludes.push_back(include.sourceId); + filteredIncludes.push_back(include.sourceId); return 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 diff --git a/tests/unit/unittest/gtest-creator-printing.cpp b/tests/unit/unittest/gtest-creator-printing.cpp index b7145eaafc..313777be23 100644 --- a/tests/unit/unittest/gtest-creator-printing.cpp +++ b/tests/unit/unittest/gtest-creator-printing.cpp @@ -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"; } diff --git a/tests/unit/unittest/pchcreator-test.cpp b/tests/unit/unittest/pchcreator-test.cpp index 6ebd07414e..0b4685891c 100644 --- a/tests/unit/unittest/pchcreator-test.cpp +++ b/tests/unit/unittest/pchcreator-test.cpp @@ -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")}}; diff --git a/tests/unit/unittest/pchmanagerclientserverinprocess-test.cpp b/tests/unit/unittest/pchmanagerclientserverinprocess-test.cpp index 2384303b88..13a4ad3437 100644 --- a/tests/unit/unittest/pchmanagerclientserverinprocess-test.cpp +++ b/tests/unit/unittest/pchmanagerclientserverinprocess-test.cpp @@ -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}}}; diff --git a/tests/unit/unittest/pchmanagerserver-test.cpp b/tests/unit/unittest/pchmanagerserver-test.cpp index e6c98ca1d0..77106268d1 100644 --- a/tests/unit/unittest/pchmanagerserver-test.cpp +++ b/tests/unit/unittest/pchmanagerserver-test.cpp @@ -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)}}; diff --git a/tests/unit/unittest/pchtaskgenerator-test.cpp b/tests/unit/unittest/pchtaskgenerator-test.cpp index dcc0e6207e..f65ed8dc41 100644 --- a/tests/unit/unittest/pchtaskgenerator-test.cpp +++ b/tests/unit/unittest/pchtaskgenerator-test.cpp @@ -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}}}; diff --git a/tests/unit/unittest/projectpartartefact-test.cpp b/tests/unit/unittest/projectpartartefact-test.cpp index 065af771a7..f7d3a031a4 100644 --- a/tests/unit/unittest/projectpartartefact-test.cpp +++ b/tests/unit/unittest/projectpartartefact-test.cpp @@ -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) diff --git a/tests/unit/unittest/projectpartqueue-test.cpp b/tests/unit/unittest/projectpartqueue-test.cpp index 03561728fd..0c06a48cd6 100644 --- a/tests/unit/unittest/projectpartqueue-test.cpp +++ b/tests/unit/unittest/projectpartqueue-test.cpp @@ -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}}; diff --git a/tests/unit/unittest/projectparts-test.cpp b/tests/unit/unittest/projectparts-test.cpp index 405b955fed..123a43d28f 100644 --- a/tests/unit/unittest/projectparts-test.cpp +++ b/tests/unit/unittest/projectparts-test.cpp @@ -48,20 +48,20 @@ protected: FilePathId secondSource{12}; FilePathId thirdSource{13}; ProjectPartContainer projectPartContainer1{"id", - {"-DUNIX", "-O2"}, - {{"DEFINE", "1"}}, - {"/includes"}, - {firstHeader, secondHeader}, - {firstSource, secondSource}}; + {"-DUNIX", "-O2"}, + {{"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}}; diff --git a/tests/unit/unittest/projectupdater-test.cpp b/tests/unit/unittest/projectupdater-test.cpp index 96e619b1aa..88f438b82c 100644 --- a/tests/unit/unittest/projectupdater-test.cpp +++ b/tests/unit/unittest/projectupdater-test.cpp @@ -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) diff --git a/tests/unit/unittest/refactoringclientserverinprocess-test.cpp b/tests/unit/unittest/refactoringclientserverinprocess-test.cpp index d3c14ca5e7..55af349bbb 100644 --- a/tests/unit/unittest/refactoringclientserverinprocess-test.cpp +++ b/tests/unit/unittest/refactoringclientserverinprocess-test.cpp @@ -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}}}; diff --git a/tests/unit/unittest/refactoringserver-test.cpp b/tests/unit/unittest/refactoringserver-test.cpp index 975cbfe916..f98d5991fc 100644 --- a/tests/unit/unittest/refactoringserver-test.cpp +++ b/tests/unit/unittest/refactoringserver-test.cpp @@ -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")}}}}; diff --git a/tests/unit/unittest/symbolindexer-test.cpp b/tests/unit/unittest/symbolindexer-test.cpp index f0cbe63c79..37dc4071a0 100644 --- a/tests/unit/unittest/symbolindexer-test.cpp +++ b/tests/unit/unittest/symbolindexer-test.cpp @@ -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}}; diff --git a/tests/unit/unittest/symbolindexing-test.cpp b/tests/unit/unittest/symbolindexing-test.cpp index cd0742e74c..68a2cb8c65 100644 --- a/tests/unit/unittest/symbolindexing-test.cpp +++ b/tests/unit/unittest/symbolindexing-test.cpp @@ -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)}}; diff --git a/tests/unit/unittest/symbolstorage-test.cpp b/tests/unit/unittest/symbolstorage-test.cpp index 9949f464f9..266540c62b 100644 --- a/tests/unit/unittest/symbolstorage-test.cpp +++ b/tests/unit/unittest/symbolstorage-test.cpp @@ -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) diff --git a/tests/unit/unittest/unittest.pro b/tests/unit/unittest/unittest.pro index c67771bd43..7934ea8f3b 100644 --- a/tests/unit/unittest/unittest.pro +++ b/tests/unit/unittest/unittest.pro @@ -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 += \ diff --git a/tests/unit/unittest/usedmacrofilter-test.cpp b/tests/unit/unittest/usedmacrofilter-test.cpp new file mode 100644 index 0000000000..87f1865c2b --- /dev/null +++ b/tests/unit/unittest/usedmacrofilter-test.cpp @@ -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 |