Commit ec5477f3 authored by Kevin Funk's avatar Kevin Funk

clang: Also detect Clang builtin dirs at runtime

Summary:
Changes:
* Add a helper function retrieving the Clang version at runtime
* Auto-detect bundled copy of Clang builtin headers on Windows
* Print lots more debug output during startup

TODO: Wondering if we want to add more code detecting this directory at
runtime on other platforms, too. But for now, only Windows is
interesting for us.

CCBUG: 393779

Reviewers: kossebau, aaronpuchert, brauch

Reviewed By: brauch

Subscribers: brauch, kdevelop-devel

Tags: #kdevelop

Differential Revision: https://phabricator.kde.org/D15955
parent a1e0b6a2
...@@ -177,6 +177,8 @@ ClangSupport::ClangSupport(QObject* parent, const QVariantList& ) ...@@ -177,6 +177,8 @@ ClangSupport::ClangSupport(QObject* parent, const QVariantList& )
, m_refactoring(nullptr) , m_refactoring(nullptr)
, m_index(nullptr) , m_index(nullptr)
{ {
clangDebug() << "Detected Clang version:" << ClangHelpers::clangVersion();
{ {
const auto builtinDir = ClangHelpers::clangBuiltinIncludePath(); const auto builtinDir = ClangHelpers::clangBuiltinIncludePath();
const auto headerToCheck = QLatin1String("cpuid.h"); const auto headerToCheck = QLatin1String("cpuid.h");
......
...@@ -32,11 +32,16 @@ ...@@ -32,11 +32,16 @@
#include "clangparsingenvironmentfile.h" #include "clangparsingenvironmentfile.h"
#include "clangindex.h" #include "clangindex.h"
#include "clangducontext.h" #include "clangducontext.h"
#include "util/clangdebug.h"
#include "util/clangtypes.h" #include "util/clangtypes.h"
#include "libclang_include_path.h" #include "libclang_include_path.h"
#include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
#include <QRegularExpression>
#include <algorithm> #include <algorithm>
using namespace KDevelop; using namespace KDevelop;
...@@ -343,14 +348,48 @@ bool ClangHelpers::isHeader(const QString& path) ...@@ -343,14 +348,48 @@ bool ClangHelpers::isHeader(const QString& path)
[&](const QString& ext) { return path.endsWith(ext); }); [&](const QString& ext) { return path.endsWith(ext); });
} }
QString ClangHelpers::clangVersion()
{
static const auto clangVersion = []() -> QString {
// NOTE: The apidocs for clang_getClangVersion() clearly state it shouldn't be used for parsing
// but there's no other way to retrieve the Clang version at runtime at this point...
const ClangString version(clang_getClangVersion());
clangDebug() << "Full Clang version:" << version;
// samples:
// clang version 6.0.1 (trunk 321709) (git@github.com:llvm-mirror/llvm.git 5136df4d089a086b70d452160ad5451861269498)
// clang version 7.0.0-svn341916-1~exp1~20180911115939.26 (branches/release_70)
QRegularExpression re(QStringLiteral("^clang version (\\d+\\.\\d+\\.\\d+)"));
const auto match = re.match(version.toString());
if (!match.hasMatch())
return {};
return match.captured(1); // return e.g. 7.0.0
}();
return clangVersion;
}
QString ClangHelpers::clangBuiltinIncludePath() QString ClangHelpers::clangBuiltinIncludePath()
{ {
static const auto dir = []() -> QString { static const auto dir = []() -> QString {
auto dir = qgetenv("KDEV_CLANG_BUILTIN_DIR"); auto dir = QString::fromUtf8(qgetenv("KDEV_CLANG_BUILTIN_DIR"));
if (dir.isEmpty()) { if (!dir.isEmpty()) {
dir = KDEV_CLANG_BUILTIN_DIR; clangDebug() << "Using dir from $KDEV_CLANG_BUILTIN_DIR:" << dir;
return dir;
} }
return QString::fromUtf8(dir);
#ifdef Q_OS_WIN32
// attempt to use the bundled copy on Windows
dir = QDir::cleanPath(QStringLiteral("%1/../lib/clang/%2/include")
.arg(QCoreApplication::applicationDirPath(), clangVersion()));
clangDebug() << "Trying" << dir;
if (QFileInfo(dir).isDir()) {
return dir;
}
#endif
clangDebug() << "Using builtin dir:" << KDEV_CLANG_BUILTIN_DIR;
return QString::fromUtf8(KDEV_CLANG_BUILTIN_DIR);
}(); }();
return dir; return dir;
} }
...@@ -100,6 +100,8 @@ KDEVCLANGPRIVATE_EXPORT bool isSource(const QString& path); ...@@ -100,6 +100,8 @@ KDEVCLANGPRIVATE_EXPORT bool isSource(const QString& path);
*/ */
KDEVCLANGPRIVATE_EXPORT bool isHeader(const QString& path); KDEVCLANGPRIVATE_EXPORT bool isHeader(const QString& path);
KDEVCLANGPRIVATE_EXPORT QString clangVersion();
/** /**
* @return The path containing Clang built includes (e.g. stddef.h, stdarg.h, cpuid.h) * @return The path containing Clang built includes (e.g. stddef.h, stdarg.h, cpuid.h)
* *
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment