diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-07-07 14:12:01 +0200 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-07-10 08:32:56 +0000 |
| commit | 1bfd77c92d78c3861d0c09068e6c353020a510f4 (patch) | |
| tree | 48ccb45110bd155df0884be01009b2d7c22fa220 /sources/shiboken2/ApiExtractor/clangparser/clangparser.cpp | |
| parent | b49d3517e6b82eb40d7deff523cd127ba1348eb8 (diff) | |
shiboken2/clangparser: Use std::string_view for code snippet extraction
std: :string_view was added in C++ 17 and fits the purpose.
Remove some outdated code for Clang < 5 on this occasion.
Change-Id: I787f736679421c9080a6cabdef1616efb2c512e9
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/shiboken2/ApiExtractor/clangparser/clangparser.cpp')
| -rw-r--r-- | sources/shiboken2/ApiExtractor/clangparser/clangparser.cpp | 37 |
1 files changed, 18 insertions, 19 deletions
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 |
