CppTools: Introduce HeaderPathFilter

We went the filtering of the header path outside of the compiler options
builder so merge the PCHs.

Task-number: QTCREATORBUG-21693
Change-Id: Ia1126813a5049e39d7c6e7d60bf449aa17012d02
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
This commit is contained in:
Marco Bubke
2018-12-10 12:07:15 +01:00
parent 2529b36b40
commit ef8de6a384
8 changed files with 526 additions and 3 deletions

View File

@@ -101,7 +101,8 @@ HEADERS += \
usages.h \
cpptools_clangtidychecks.h \
cppmodelmanagerinterface.h \
cppbuiltinmodelmanagersupport.h
cppbuiltinmodelmanagersupport.h \
headerpathfilter.h
SOURCES += \
abstracteditorsupport.cpp \
@@ -187,7 +188,8 @@ SOURCES += \
cppprojectfilecategorizer.cpp \
cppprojectpartchooser.cpp \
wrappablelineedit.cpp \
cppbuiltinmodelmanagersupport.cpp
cppbuiltinmodelmanagersupport.cpp \
headerpathfilter.cpp
FORMS += \
clangdiagnosticconfigswidget.ui \

View File

@@ -13,6 +13,7 @@ HEADERS += \
$$PWD/projectinfo.h \
$$PWD/cppprojectinfogenerator.cpp \
$$PWD/cppprojectpartchooser.h \
$$PWD/headerpathfilter.h
SOURCES += \
$$PWD/cppprojectfile.cpp \
@@ -23,3 +24,4 @@ SOURCES += \
$$PWD/projectinfo.cpp \
$$PWD/cppprojectinfogenerator.cpp \
$$PWD/cppprojectpartchooser.cpp \
$$PWD/headerpathfilter.cpp

View File

@@ -0,0 +1,132 @@
/****************************************************************************
**
** 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 "headerpathfilter.h"
#include <QRegularExpression>
namespace CppTools {
using ProjectExplorer::HeaderPath;
using ProjectExplorer::HeaderPaths;
using ProjectExplorer::HeaderPathType;
void HeaderPathFilter::process()
{
const HeaderPaths &headerPaths = projectPart.headerPaths;
for (const HeaderPath &headerPath : headerPaths)
filterHeaderPath(headerPath);
if (useTweakedHeaderPaths == UseTweakedHeaderPaths::Yes)
tweakHeaderPaths();
}
void HeaderPathFilter::filterHeaderPath(const ProjectExplorer::HeaderPath &headerPath)
{
if (headerPath.path.isEmpty())
return;
switch (headerPath.type) {
case HeaderPathType::BuiltIn:
builtInHeaderPaths.push_back(headerPath);
break;
case HeaderPathType::System:
case HeaderPathType::Framework:
systemHeaderPaths.push_back(headerPath);
break;
case HeaderPathType::User:
userHeaderPaths.push_back(headerPath);
break;
}
}
namespace {
QString clangIncludeDirectory(const QString &clangVersion, const QString &clangResourceDirectory)
{
#ifndef UNIT_TESTS
return Core::ICore::clangIncludeDirectory(clangVersion, clangResourceDirectory);
#else
Q_UNUSED(clangVersion);
Q_UNUSED(clangResourceDirectory);
return CLANG_RESOURCE_DIR;
#endif
}
HeaderPaths::iterator resourceIterator(HeaderPaths &headerPaths, bool isMacOs)
{
// include/c++, include/g++, libc++\include and libc++abi\include
static const QString cppIncludes = R"((.*\/include\/.*(g\+\+|c\+\+).*))"
R"(|(.*libc\+\+\/include))"
R"(|(.*libc\+\+abi\/include))";
static const QRegularExpression includeRegExp("\\A(" + cppIncludes + ")\\z");
// The same as includeRegExp but also matches /usr/local/include
static const QRegularExpression includeRegExpMac("\\A(" + cppIncludes
+ R"(|(\/usr\/local\/include))" + ")\\z");
const QRegularExpression &includePathRegEx = isMacOs ? includeRegExpMac : includeRegExp;
return std::stable_partition(headerPaths.begin(),
headerPaths.end(),
[&](const HeaderPath &headerPath) {
return includePathRegEx.match(headerPath.path).hasMatch();
});
}
bool isClangSystemHeaderPath(const HeaderPath &headerPath)
{
// Always exclude clang system includes (including intrinsics) which do not come with libclang
// that Qt Creator uses for code model.
// For example GCC on macOS uses system clang include path which makes clang code model
// include incorrect system headers.
static const QRegularExpression clangIncludeDir(
R"(\A.*\/lib\d*\/clang\/\d+\.\d+(\.\d+)?\/include\z)");
return clangIncludeDir.match(headerPath.path).hasMatch();
}
void removeClangSystemHeaderPaths(HeaderPaths &headerPaths)
{
auto newEnd = std::remove_if(headerPaths.begin(), headerPaths.end(), isClangSystemHeaderPath);
headerPaths.erase(newEnd, headerPaths.end());
}
} // namespace
void HeaderPathFilter::tweakHeaderPaths()
{
removeClangSystemHeaderPaths(builtInHeaderPaths);
auto split = resourceIterator(builtInHeaderPaths,
projectPart.toolChainTargetTriple.contains("darwin"));
if (!clangVersion.isEmpty()) {
const QString clangIncludePath = clangIncludeDirectory(clangVersion, clangResourceDirectory);
builtInHeaderPaths.insert(split, HeaderPath{clangIncludePath, HeaderPathType::BuiltIn});
}
}
} // namespace CppTools

View File

@@ -0,0 +1,63 @@
/****************************************************************************
**
** 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 "projectpart.h"
namespace CppTools {
enum class UseTweakedHeaderPaths : char { Yes, No };
class HeaderPathFilter
{
public:
HeaderPathFilter(const ProjectPart &projectPart,
UseTweakedHeaderPaths useTweakedHeaderPaths = UseTweakedHeaderPaths::Yes,
const QString &clangVersion = QString(),
const QString &clangResourceDirectory = QString())
: projectPart{projectPart}
, clangVersion{clangVersion}
, clangResourceDirectory{clangResourceDirectory}
, useTweakedHeaderPaths{useTweakedHeaderPaths}
{}
void process();
void filterHeaderPath(const ProjectExplorer::HeaderPath &headerPath);
void tweakHeaderPaths();
public:
ProjectExplorer::HeaderPaths builtInHeaderPaths;
ProjectExplorer::HeaderPaths systemHeaderPaths;
ProjectExplorer::HeaderPaths userHeaderPaths;
const ProjectPart &projectPart;
const QString clangVersion;
const QString clangResourceDirectory;
const UseTweakedHeaderPaths useTweakedHeaderPaths;
};
} // namespace CppTools

View File

@@ -62,6 +62,7 @@
#include <cpptools/usages.h>
#include <projectexplorer/projectmacro.h>
#include <projectexplorer/headerpath.h>
#include <coreplugin/find/searchresultitem.h>
#include <coreplugin/locator/ilocatorfilter.h>
@@ -145,6 +146,32 @@ std::ostream &operator<<(std::ostream &out, const Macro &macro)
return out;
}
static const char *typeToString(const HeaderPathType &type)
{
switch (type) {
case HeaderPathType::User:
return "User";
case HeaderPathType::System:
return "System";
case HeaderPathType::BuiltIn:
return "BuiltIn";
case HeaderPathType::Framework:
return "Framework";
}
return "";
}
std::ostream &operator<<(std::ostream &out, const HeaderPathType &headerPathType)
{
return out << "HeaderPathType::" << typeToString(headerPathType);
}
std::ostream &operator<<(std::ostream &out, const HeaderPath &headerPath)
{
return out << "(" << headerPath.path << ", " << headerPath.type << ")";
}
} // namespace ProjectExplorer
namespace Utils {

View File

@@ -59,9 +59,13 @@ namespace ProjectExplorer {
enum class MacroType;
class Macro;
enum class HeaderPathType;
class HeaderPath;
std::ostream &operator<<(std::ostream &out, const MacroType &type);
std::ostream &operator<<(std::ostream &out, const Macro &macro);
std::ostream &operator<<(std::ostream &out, const HeaderPathType &headerPathType);
std::ostream &operator<<(std::ostream &out, const HeaderPath &headerPath);
} // namespace ClangRefactoring

View File

@@ -0,0 +1,292 @@
/****************************************************************************
**
** 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 <cpptools/headerpathfilter.h>
namespace {
using ProjectExplorer::HeaderPath;
using ProjectExplorer::HeaderPathType;
MATCHER_P(HasBuiltIn,
path,
std::string(negation ? "isn't " : "is ")
+ PrintToString(HeaderPath{QString::fromUtf8(path), HeaderPathType::BuiltIn}))
{
return arg.path == path && arg.type == HeaderPathType::BuiltIn;
}
MATCHER_P(HasSystem,
path,
std::string(negation ? "isn't " : "is ")
+ PrintToString(HeaderPath{QString::fromUtf8(path), HeaderPathType::BuiltIn}))
{
return arg.path == path && arg.type == HeaderPathType::System;
}
MATCHER_P(HasFramework,
path,
std::string(negation ? "isn't " : "is ")
+ PrintToString(HeaderPath{QString::fromUtf8(path), HeaderPathType::BuiltIn}))
{
return arg.path == path && arg.type == HeaderPathType::Framework;
}
MATCHER_P(HasUser,
path,
std::string(negation ? "isn't " : "is ")
+ PrintToString(HeaderPath{QString::fromUtf8(path), HeaderPathType::BuiltIn}))
{
return arg.path == path && arg.type == HeaderPathType::User;
}
class HeaderPathFilter : public testing::Test
{
protected:
HeaderPathFilter()
{
auto headerPaths = {HeaderPath{"", HeaderPathType::BuiltIn},
HeaderPath{"/builtin_path", HeaderPathType::BuiltIn},
HeaderPath{"/system_path", HeaderPathType::System},
HeaderPath{"/framework_path", HeaderPathType::Framework},
HeaderPath{"/user_path", HeaderPathType::User}};
projectPart.headerPaths = headerPaths;
}
protected:
CppTools::ProjectPart projectPart;
CppTools::HeaderPathFilter filter{projectPart, CppTools::UseTweakedHeaderPaths::No};
};
TEST_F(HeaderPathFilter, BuiltIn)
{
filter.process();
ASSERT_THAT(filter.builtInHeaderPaths, Contains(HasBuiltIn("/builtin_path")));
}
TEST_F(HeaderPathFilter, System)
{
filter.process();
ASSERT_THAT(filter.systemHeaderPaths, Contains(HasSystem("/system_path")));
}
TEST_F(HeaderPathFilter, User)
{
filter.process();
ASSERT_THAT(filter.userHeaderPaths, Contains(HasUser("/user_path")));
}
TEST_F(HeaderPathFilter, Framework)
{
filter.process();
ASSERT_THAT(filter.systemHeaderPaths, Contains(HasFramework("/framework_path")));
}
TEST_F(HeaderPathFilter, DontAddInvalidPath)
{
filter.process();
ASSERT_THAT(filter,
AllOf(Field(&CppTools::HeaderPathFilter::builtInHeaderPaths,
ElementsAre(HasBuiltIn("/builtin_path"))),
Field(&CppTools::HeaderPathFilter::systemHeaderPaths,
ElementsAre(HasSystem("/system_path"), HasFramework("/framework_path"))),
Field(&CppTools::HeaderPathFilter::userHeaderPaths,
ElementsAre(HasUser("/user_path")))));
}
TEST_F(HeaderPathFilter, ClangHeadersPath)
{
CppTools::HeaderPathFilter filter{projectPart,
CppTools::UseTweakedHeaderPaths::Yes,
"6.0",
CLANG_RESOURCE_DIR};
filter.process();
ASSERT_THAT(filter.builtInHeaderPaths,
ElementsAre(HasBuiltIn(CLANG_RESOURCE_DIR), HasBuiltIn("/builtin_path")));
}
TEST_F(HeaderPathFilter, ClangHeadersPathWitoutClangVersion)
{
CppTools::HeaderPathFilter filter{projectPart,
CppTools::UseTweakedHeaderPaths::Yes};
filter.process();
ASSERT_THAT(filter.builtInHeaderPaths,
ElementsAre(HasBuiltIn("/builtin_path")));
}
TEST_F(HeaderPathFilter, ClangHeadersAndCppIncludesPathsOrderMacOs)
{
auto builtIns = {HeaderPath{"/usr/include/c++/4.2.1", HeaderPathType::BuiltIn},
HeaderPath{"/usr/include/c++/4.2.1/backward", HeaderPathType::BuiltIn},
HeaderPath{"/usr/local/include", HeaderPathType::BuiltIn},
HeaderPath{"/Applications/Xcode.app/Contents/Developer/Toolchains/"
"XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/include",
HeaderPathType::BuiltIn},
HeaderPath{"/Applications/Xcode.app/Contents/Developer/Toolchains/"
"XcodeDefault.xctoolchain/usr/include",
HeaderPathType::BuiltIn},
HeaderPath{"/usr/include", HeaderPathType::BuiltIn}};
projectPart.toolChainTargetTriple = "x86_64-apple-darwin10";
std::copy(builtIns.begin(),
builtIns.end(),
std::inserter(projectPart.headerPaths, projectPart.headerPaths.begin()));
CppTools::HeaderPathFilter filter{projectPart,
CppTools::UseTweakedHeaderPaths::Yes,
"6.0",
CLANG_RESOURCE_DIR};
filter.process();
ASSERT_THAT(filter.builtInHeaderPaths,
ElementsAre(HasBuiltIn("/usr/include/c++/4.2.1"),
HasBuiltIn("/usr/include/c++/4.2.1/backward"),
HasBuiltIn("/usr/local/include"),
HasBuiltIn(CLANG_RESOURCE_DIR),
HasBuiltIn("/Applications/Xcode.app/Contents/Developer/Toolchains/"
"XcodeDefault.xctoolchain/usr/include"),
HasBuiltIn("/usr/include"),
HasBuiltIn("/builtin_path")));
}
TEST_F(HeaderPathFilter, ClangHeadersAndCppIncludesPathsOrderLinux)
{
auto builtIns = {
HeaderPath{"/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8",
HeaderPathType::BuiltIn},
HeaderPath{"/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/backward",
HeaderPathType::BuiltIn},
HeaderPath{"/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/x86_64-linux-gnu/c++/4.8",
HeaderPathType::BuiltIn},
HeaderPath{"/usr/local/include", HeaderPathType::BuiltIn},
HeaderPath{"/usr/lib/gcc/x86_64-linux-gnu/4.8/include", HeaderPathType::BuiltIn},
HeaderPath{"/usr/include/x86_64-linux-gnu", HeaderPathType::BuiltIn},
HeaderPath{"/usr/include", HeaderPathType::BuiltIn}};
std::copy(builtIns.begin(),
builtIns.end(),
std::inserter(projectPart.headerPaths, projectPart.headerPaths.begin()));
projectPart.toolChainTargetTriple = "x86_64-linux-gnu";
CppTools::HeaderPathFilter filter{projectPart,
CppTools::UseTweakedHeaderPaths::Yes,
"6.0",
CLANG_RESOURCE_DIR};
filter.process();
ASSERT_THAT(filter.builtInHeaderPaths,
ElementsAre(HasBuiltIn(
"/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8"),
HasBuiltIn("/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/"
"c++/4.8/backward"),
HasBuiltIn("/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/"
"x86_64-linux-gnu/c++/4.8"),
HasBuiltIn(CLANG_RESOURCE_DIR),
HasBuiltIn("/usr/local/include"),
HasBuiltIn("/usr/lib/gcc/x86_64-linux-gnu/4.8/include"),
HasBuiltIn("/usr/include/x86_64-linux-gnu"),
HasBuiltIn("/usr/include"),
HasBuiltIn("/builtin_path")));
}
TEST_F(HeaderPathFilter, ClangHeadersAndCppIncludesPathsOrderNoVersion)
{
projectPart.headerPaths = {
HeaderPath{"C:/Qt/Tools/mingw530_32/i686-w64-mingw32/include", HeaderPathType::BuiltIn},
HeaderPath{"C:/Qt/Tools/mingw530_32/i686-w64-mingw32/include/c++", HeaderPathType::BuiltIn},
HeaderPath{"C:/Qt/Tools/mingw530_32/i686-w64-mingw32/include/c++/i686-w64-mingw32",
HeaderPathType::BuiltIn},
HeaderPath{"C:/Qt/Tools/mingw530_32/i686-w64-mingw32/include/c++/backward",
HeaderPathType::BuiltIn}};
projectPart.toolChainTargetTriple = "x86_64-w64-windows-gnu";
CppTools::HeaderPathFilter filter{projectPart,
CppTools::UseTweakedHeaderPaths::Yes,
"6.0",
CLANG_RESOURCE_DIR};
filter.process();
ASSERT_THAT(
filter.builtInHeaderPaths,
ElementsAre(HasBuiltIn("C:/Qt/Tools/mingw530_32/i686-w64-mingw32/include/c++"),
HasBuiltIn(
"C:/Qt/Tools/mingw530_32/i686-w64-mingw32/include/c++/i686-w64-mingw32"),
HasBuiltIn("C:/Qt/Tools/mingw530_32/i686-w64-mingw32/include/c++/backward"),
HasBuiltIn(CLANG_RESOURCE_DIR),
HasBuiltIn("C:/Qt/Tools/mingw530_32/i686-w64-mingw32/include")));
}
TEST_F(HeaderPathFilter, ClangHeadersAndCppIncludesPathsOrderAndroidClang)
{
projectPart.headerPaths = {
HeaderPath{"C:/Users/test/AppData/Local/Android/sdk/ndk-"
"bundle/sysroot/usr/include/i686-linux-android",
HeaderPathType::BuiltIn},
HeaderPath{"C:/Users/test/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-"
"stl/llvm-libc++/include",
HeaderPathType::BuiltIn},
HeaderPath{"C:/Users/test/AppData/Local/Android/sdk/ndk-"
"bundle/sources/android/support/include",
HeaderPathType::BuiltIn},
HeaderPath{"C:/Users/test/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-"
"stl/llvm-libc++abi/include",
HeaderPathType::BuiltIn},
HeaderPath{"C:/Users/test/AppData/Local/Android/sdk/ndk-bundle/sysroot/usr/include",
HeaderPathType::BuiltIn}};
projectPart.toolChainTargetTriple = "i686-linux-android";
CppTools::HeaderPathFilter filter{projectPart,
CppTools::UseTweakedHeaderPaths::Yes,
"6.0",
CLANG_RESOURCE_DIR};
filter.process();
ASSERT_THAT(
filter.builtInHeaderPaths,
ElementsAre(HasBuiltIn("C:/Users/test/AppData/Local/Android/sdk/ndk-"
"bundle/sources/cxx-stl/llvm-libc++/include"),
HasBuiltIn(
"C:/Users/test/AppData/Local/Android/sdk/ndk-"
"bundle/sources/cxx-stl/llvm-libc++abi/include"),
HasBuiltIn(CLANG_RESOURCE_DIR),
HasBuiltIn("C:/Users/test/AppData/Local/Android/sdk/ndk-"
"bundle/sysroot/usr/include/i686-linux-android"),
HasBuiltIn("C:/Users/test/AppData/Local/Android/sdk/ndk-"
"bundle/sources/android/support/include"),
HasBuiltIn("C:/Users/test/AppData/Local/Android/sdk/ndk-"
"bundle/sysroot/usr/include")));
}
} // namespace

View File

@@ -110,7 +110,8 @@ SOURCES += \
builddependenciesstorage-test.cpp \
usedmacrofilter-test.cpp \
pchtasksmerger-test.cpp \
pchtaskqueue-test.cpp
pchtaskqueue-test.cpp \
headerpathfilter-test.cpp
!isEmpty(LIBCLANG_LIBS) {
SOURCES += \