Changeset View
Changeset View
Standalone View
Standalone View
plugins/clang/duchain/clanghelpers.cpp
Show All 26 Lines | |||||
27 | #include <language/duchain/parsingenvironment.h> | 27 | #include <language/duchain/parsingenvironment.h> | ||
28 | #include <language/backgroundparser/urlparselock.h> | 28 | #include <language/backgroundparser/urlparselock.h> | ||
29 | 29 | | |||
30 | #include "builder.h" | 30 | #include "builder.h" | ||
31 | #include "parsesession.h" | 31 | #include "parsesession.h" | ||
32 | #include "clangparsingenvironmentfile.h" | 32 | #include "clangparsingenvironmentfile.h" | ||
33 | #include "clangindex.h" | 33 | #include "clangindex.h" | ||
34 | #include "clangducontext.h" | 34 | #include "clangducontext.h" | ||
35 | 35 | #include "util/clangdebug.h" | |||
36 | #include "util/clangtypes.h" | 36 | #include "util/clangtypes.h" | ||
37 | 37 | | |||
38 | #include "libclang_include_path.h" | 38 | #include "libclang_include_path.h" | ||
39 | 39 | | |||
40 | #include <QCoreApplication> | ||||
41 | #include <QDir> | ||||
42 | #include <QFileInfo> | ||||
43 | #include <QRegularExpression> | ||||
44 | | ||||
40 | #include <algorithm> | 45 | #include <algorithm> | ||
41 | 46 | | |||
42 | using namespace KDevelop; | 47 | using namespace KDevelop; | ||
43 | 48 | | |||
44 | namespace { | 49 | namespace { | ||
45 | 50 | | |||
46 | CXChildVisitResult visitCursor(CXCursor cursor, CXCursor, CXClientData data) | 51 | CXChildVisitResult visitCursor(CXCursor cursor, CXCursor, CXClientData data) | ||
47 | { | 52 | { | ||
▲ Show 20 Lines • Show All 290 Lines • ▼ Show 20 Line(s) | |||||
338 | 343 | | |||
339 | bool ClangHelpers::isHeader(const QString& path) | 344 | bool ClangHelpers::isHeader(const QString& path) | ||
340 | { | 345 | { | ||
341 | const auto& extensions = headerExtensions(); | 346 | const auto& extensions = headerExtensions(); | ||
342 | return std::any_of(extensions.constBegin(), extensions.constEnd(), | 347 | return std::any_of(extensions.constBegin(), extensions.constEnd(), | ||
343 | [&](const QString& ext) { return path.endsWith(ext); }); | 348 | [&](const QString& ext) { return path.endsWith(ext); }); | ||
344 | } | 349 | } | ||
345 | 350 | | |||
351 | QString ClangHelpers::clangVersion() | ||||
352 | { | ||||
353 | static const auto clangVersion = []() -> QString { | ||||
354 | // NOTE: The apidocs for clang_getClangVersion() clearly state it shouldn't be used for parsing | ||||
355 | // but there's no other way to retrieve the Clang version at runtime at this point... | ||||
356 | const ClangString version(clang_getClangVersion()); | ||||
357 | clangDebug() << "Full Clang version:" << version; | ||||
358 | | ||||
359 | // samples: | ||||
360 | // clang version 6.0.1 (trunk 321709) (git@github.com:llvm-mirror/llvm.git 5136df4d089a086b70d452160ad5451861269498) | ||||
361 | // clang version 7.0.0-svn341916-1~exp1~20180911115939.26 (branches/release_70) | ||||
362 | QRegularExpression re(QStringLiteral("^clang version (\\d+\\.\\d+\\.\\d+)")); | ||||
363 | const auto match = re.match(version.toString()); | ||||
364 | if (!match.hasMatch()) | ||||
365 | return {}; | ||||
366 | | ||||
367 | return match.captured(1); // return e.g. 7.0.0 | ||||
368 | }(); | ||||
369 | return clangVersion; | ||||
370 | } | ||||
371 | | ||||
346 | QString ClangHelpers::clangBuiltinIncludePath() | 372 | QString ClangHelpers::clangBuiltinIncludePath() | ||
347 | { | 373 | { | ||
348 | static const auto dir = []() -> QString { | 374 | static const auto dir = []() -> QString { | ||
349 | auto dir = qgetenv("KDEV_CLANG_BUILTIN_DIR"); | 375 | auto dir = QString::fromUtf8(qgetenv("KDEV_CLANG_BUILTIN_DIR")); | ||
350 | if (dir.isEmpty()) { | 376 | if (!dir.isEmpty()) { | ||
351 | dir = KDEV_CLANG_BUILTIN_DIR; | 377 | clangDebug() << "Using dir from $KDEV_CLANG_BUILTIN_DIR:" << dir; | ||
378 | return dir; | ||||
352 | } | 379 | } | ||
353 | return QString::fromUtf8(dir); | 380 | | ||
381 | #ifdef Q_OS_WIN32 | ||||
382 | // attempt to use the bundled copy on Windows | ||||
383 | dir = QDir::cleanPath(QStringLiteral("%1/../lib/clang/%2/include") | ||||
384 | .arg(QCoreApplication::applicationDirPath(), clangVersion())); | ||||
385 | clangDebug() << "Trying" << dir; | ||||
386 | if (QFileInfo(dir).isDir()) { | ||||
387 | return dir; | ||||
kossebau: Always confused about those, but should this not be QString::fromLocal8Bit, given it's… | |||||
388 | } | ||||
389 | #endif | ||||
390 | | ||||
391 | clangDebug() << "Using builtin dir:" << KDEV_CLANG_BUILTIN_DIR; | ||||
392 | return QString::fromUtf8(KDEV_CLANG_BUILTIN_DIR); | ||||
354 | }(); | 393 | }(); | ||
355 | return dir; | 394 | return dir; | ||
356 | } | 395 | } |
Always confused about those, but should this not be QString::fromLocal8Bit, given it's consuming a raw string from the system?
Not that there might be actually and non-UTF8 around these days, but given the method exists, let's use it.