diff options
5 files changed, 42 insertions, 59 deletions
diff --git a/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp b/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp index 263c0a0bb..f00b8dc42 100644 --- a/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp +++ b/sources/shiboken2/ApiExtractor/clangparser/clangbuilder.cpp @@ -574,44 +574,15 @@ void BuilderPrivate::endTemplateTypeAlias(const CXCursor &typeAliasCursor) // CXCursor_EnumConstantDecl, ParmDecl (a = Flag1 | Flag2) QString BuilderPrivate::cursorValueExpression(BaseVisitor *bv, const CXCursor &cursor) const { - BaseVisitor::CodeSnippet snippet = bv->getCodeSnippet(cursor); - const char *equalSign = std::find(snippet.first, snippet.second, '='); - if (equalSign == snippet.second) + const std::string_view snippet = bv->getCodeSnippet(cursor); + auto equalSign = snippet.find('='); + if (equalSign == std::string::npos) return QString(); ++equalSign; - return QString::fromLocal8Bit(equalSign, int(snippet.second - equalSign)).trimmed(); + return QString::fromLocal8Bit(snippet.cbegin() + equalSign, + int(snippet.size() - equalSign)).trimmed(); } -// A hacky reimplementation of clang_EnumDecl_isScoped() for Clang < 5.0 -// which simply checks for a blank-delimited " class " keyword in the enum snippet. - -#define CLANG_NO_ENUMDECL_ISSCOPED \ - (CINDEX_VERSION_MAJOR == 0 && CINDEX_VERSION_MINOR < 43) - -#if CLANG_NO_ENUMDECL_ISSCOPED -static const char *indexOf(const BaseVisitor::CodeSnippet &snippet, const char *needle) -{ - const size_t snippetLength = snippet.first ? size_t(snippet.second - snippet.first) : 0; - const size_t needleLength = strlen(needle); - if (needleLength > snippetLength) - return nullptr; - for (const char *c = snippet.first, *end = snippet.second - needleLength; c < end; ++c) { - if (memcmp(c, needle, needleLength) == 0) - return c; - } - return nullptr; -} - -long clang_EnumDecl_isScoped4(BaseVisitor *bv, const CXCursor &cursor) -{ - BaseVisitor::CodeSnippet snippet = bv->getCodeSnippet(cursor); - const char *classSpec = indexOf(snippet, "class"); - const bool isClass = classSpec && classSpec > snippet.first - && isspace(*(classSpec - 1)) && isspace(*(classSpec + 5)); - return isClass ? 1 : 0; -} -#endif // CLANG_NO_ENUMDECL_ISSCOPED - // Resolve declaration and type of a base class struct TypeDeclaration diff --git a/sources/shiboken2/ApiExtractor/clangparser/clangdebugutils.cpp b/sources/shiboken2/ApiExtractor/clangparser/clangdebugutils.cpp index d6915daab..7123c22d8 100644 --- a/sources/shiboken2/ApiExtractor/clangparser/clangdebugutils.cpp +++ b/sources/shiboken2/ApiExtractor/clangparser/clangdebugutils.cpp @@ -147,4 +147,16 @@ QDebug operator<<(QDebug s, const CXSourceLocation &location) return s; } +QDebug operator<<(QDebug s, const std::string_view &v) +{ + QDebugStateSaver saver(s); + s.nospace(); + s.noquote(); + s << '"'; + for (auto c : v) + s << c; + s << '"'; + return s; +} + #endif // !QT_NO_DEBUG_STREAM diff --git a/sources/shiboken2/ApiExtractor/clangparser/clangdebugutils.h b/sources/shiboken2/ApiExtractor/clangparser/clangdebugutils.h index 2bbe526f7..ae3840fb4 100644 --- a/sources/shiboken2/ApiExtractor/clangparser/clangdebugutils.h +++ b/sources/shiboken2/ApiExtractor/clangparser/clangdebugutils.h @@ -33,6 +33,8 @@ #include <clang-c/Index.h> +#include <string_view> + QT_FORWARD_DECLARE_CLASS(QDebug) QT_FORWARD_DECLARE_CLASS(QString) @@ -43,6 +45,7 @@ QDebug operator<<(QDebug s, CX_CXXAccessSpecifier ac); QDebug operator<<(QDebug s, const CXType &t); QDebug operator<<(QDebug s, const CXCursor &cursor); QDebug operator<<(QDebug s, const CXSourceLocation &location); +QDebug operator<<(QDebug s, const std::string_view &v); // for code snippets #endif // !QT_NO_DEBUG_STREAM #endif // CLANGDEBUGUTILS_H diff --git a/sources/shiboken2/ApiExtractor/clangparser/clangparser.cpp b/sources/shiboken2/ApiExtractor/clangparser/clangparser.cpp index d0c5bc1b8..599114b2f 100644 --- a/sources/shiboken2/ApiExtractor/clangparser/clangparser.cpp +++ b/sources/shiboken2/ApiExtractor/clangparser/clangparser.cpp @@ -1,4 +1,4 @@ -/**************************************************************************** +/**************************************************************************** ** ** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ @@ -48,10 +48,10 @@ QString SourceFileCache::getFileName(CXFile file) return it.value(); } -SourceFileCache::Snippet SourceFileCache::getCodeSnippet(const CXCursor &cursor, - QString *errorMessage) +std::string_view SourceFileCache::getCodeSnippet(const CXCursor &cursor, + QString *errorMessage) { - Snippet result(nullptr, nullptr); + static const char empty[] = ""; if (errorMessage) errorMessage->clear(); @@ -60,12 +60,12 @@ SourceFileCache::Snippet SourceFileCache::getCodeSnippet(const CXCursor &cursor, // Quick check for equal locations: Frequently happens if the code is // the result of a macro expansion if (range.first == range.second) - return result; + return std::string_view(empty, 0); if (range.first.file != range.second.file) { if (errorMessage) *errorMessage = QStringLiteral("Range spans several files"); - return result; + return std::string_view(empty, 0); } auto it = m_fileBufferCache.find(range.first.file); @@ -74,7 +74,7 @@ SourceFileCache::Snippet SourceFileCache::getCodeSnippet(const CXCursor &cursor, if (fileName.isEmpty()) { if (errorMessage) *errorMessage = QStringLiteral("Range has no file"); - return result; + return std::string_view(empty, 0); } QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { @@ -83,7 +83,7 @@ SourceFileCache::Snippet SourceFileCache::getCodeSnippet(const CXCursor &cursor, str << "Cannot open \"" << QDir::toNativeSeparators(fileName) << "\": " << file.errorString(); } - return result; + return std::string_view(empty, 0); } it = m_fileBufferCache.insert(range.first.file, file.readAll()); } @@ -99,11 +99,10 @@ SourceFileCache::Snippet SourceFileCache::getCodeSnippet(const CXCursor &cursor, << QDir::toNativeSeparators(getFileName(range.first.file)) << "\" (" << contents.size() << ')'; } - return result; + return std::string_view(empty, 0); } - result.first = contents.constData() + pos; - result.second = contents.constData() + end; - return result; + + return std::string_view(contents.constData() + pos, end - pos); } BaseVisitor::BaseVisitor() = default; @@ -135,11 +134,11 @@ bool BaseVisitor::cbHandleEndToken(const CXCursor &cursor, StartTokenResult star return result; } -BaseVisitor::CodeSnippet BaseVisitor::getCodeSnippet(const CXCursor &cursor) +std::string_view BaseVisitor::getCodeSnippet(const CXCursor &cursor) { QString errorMessage; - CodeSnippet result = m_fileCache.getCodeSnippet(cursor, &errorMessage); - if (result.first == nullptr && !errorMessage.isEmpty()) { + const std::string_view result = m_fileCache.getCodeSnippet(cursor, &errorMessage); + if (result.empty() && !errorMessage.isEmpty()) { QString message; QTextStream str(&message); str << "Unable to retrieve code snippet \"" << getCursorSpelling(cursor) @@ -151,10 +150,10 @@ BaseVisitor::CodeSnippet BaseVisitor::getCodeSnippet(const CXCursor &cursor) QString BaseVisitor::getCodeSnippetString(const CXCursor &cursor) { - CodeSnippet result = getCodeSnippet(cursor); - return result.first != nullptr - ? QString::fromUtf8(result.first, int(result.second - result.first)) - : QString(); + const std::string_view result = getCodeSnippet(cursor); + return result.empty() + ? QString() + : QString::fromUtf8(result.cbegin(), result.size()); } static CXChildVisitResult diff --git a/sources/shiboken2/ApiExtractor/clangparser/clangparser.h b/sources/shiboken2/ApiExtractor/clangparser/clangparser.h index 825de331c..7e918263a 100644 --- a/sources/shiboken2/ApiExtractor/clangparser/clangparser.h +++ b/sources/shiboken2/ApiExtractor/clangparser/clangparser.h @@ -37,15 +37,15 @@ #include <QtCore/QString> #include <QtCore/QVector> +#include <string_view> + namespace clang { struct Diagnostic; class SourceFileCache { public: - using Snippet = QPair<const char *, const char *>; - - Snippet getCodeSnippet(const CXCursor &cursor, QString *errorMessage = nullptr); + std::string_view getCodeSnippet(const CXCursor &cursor, QString *errorMessage = nullptr); QString getFileName(CXFile file); private: @@ -60,7 +60,6 @@ class BaseVisitor { Q_DISABLE_COPY(BaseVisitor) public: using Diagnostics = QVector<Diagnostic>; - using CodeSnippet = SourceFileCache::Snippet; enum StartTokenResult { Error, Skip, Recurse }; @@ -78,8 +77,7 @@ public: bool cbHandleEndToken(const CXCursor &cursor, StartTokenResult startResult); QString getFileName(CXFile file) { return m_fileCache.getFileName(file); } - - CodeSnippet getCodeSnippet(const CXCursor &cursor); + std::string_view getCodeSnippet(const CXCursor &cursor); QString getCodeSnippetString(const CXCursor &cursor); Diagnostics diagnostics() const; |
