diff options
Diffstat (limited to 'sources/shiboken2/ApiExtractor/typesystem.cpp')
-rw-r--r-- | sources/shiboken2/ApiExtractor/typesystem.cpp | 80 |
1 files changed, 45 insertions, 35 deletions
diff --git a/sources/shiboken2/ApiExtractor/typesystem.cpp b/sources/shiboken2/ApiExtractor/typesystem.cpp index 4c522be6c..aa4147d76 100644 --- a/sources/shiboken2/ApiExtractor/typesystem.cpp +++ b/sources/shiboken2/ApiExtractor/typesystem.cpp @@ -29,6 +29,7 @@ #include "typesystem.h" #include "typesystem_p.h" #include "typedatabase.h" +#include "messages.h" #include "reporthandler.h" #include <QtCore/QDir> #include <QtCore/QFile> @@ -2213,6 +2214,46 @@ bool Handler::parseParentOwner(const QXmlStreamReader &, return true; } +bool Handler::readFileSnippet(QXmlStreamAttributes *attributes, CodeSnip *snip) +{ + QString fileName; + QString snippetLabel; + for (int i = attributes->size() - 1; i >= 0; --i) { + const QStringRef name = attributes->at(i).qualifiedName(); + if (name == QLatin1String("file")) { + fileName = attributes->takeAt(i).value().toString(); + } else if (name == snippetAttribute()) { + snippetLabel = attributes->takeAt(i).value().toString(); + } + } + if (fileName.isEmpty()) + return true; + const QString resolved = m_database->modifiedTypesystemFilepath(fileName, m_currentPath); + if (!QFile::exists(resolved)) { + m_error = QLatin1String("File for inject code not exist: ") + + QDir::toNativeSeparators(fileName); + return false; + } + QFile codeFile(resolved); + if (!codeFile.open(QIODevice::Text | QIODevice::ReadOnly)) { + m_error = msgCannotOpenForReading(codeFile); + return false; + } + QString source = fileName; + if (!snippetLabel.isEmpty()) + source += QLatin1String(" (") + snippetLabel + QLatin1Char(')'); + QString content; + QTextStream str(&content); + str << "// ========================================================================\n" + "// START of custom code block [file: " + << source << "]\n" + << extractSnippet(QString::fromUtf8(codeFile.readAll()), snippetLabel) + << "\n// END of custom code block [file: " << source + << "]\n// ========================================================================\n"; + snip->addCode(content); + return true; +} + bool Handler::parseInjectCode(const QXmlStreamReader &, const StackElement &topElement, StackElement* element, QXmlStreamAttributes *attributes) @@ -2227,8 +2268,9 @@ bool Handler::parseInjectCode(const QXmlStreamReader &, TypeSystem::CodeSnipPosition position = TypeSystem::CodeSnipPositionBeginning; TypeSystem::Language lang = TypeSystem::TargetLangCode; - QString fileName; - QString snippetLabel; + CodeSnip snip; + if (!readFileSnippet(attributes, &snip)) + return false; for (int i = attributes->size() - 1; i >= 0; --i) { const QStringRef name = attributes->at(i).qualifiedName(); if (name == classAttribute()) { @@ -2245,43 +2287,11 @@ bool Handler::parseInjectCode(const QXmlStreamReader &, m_error = QStringLiteral("Invalid position: '%1'").arg(value); return false; } - } else if (name == QLatin1String("file")) { - fileName = attributes->takeAt(i).value().toString(); - } else if (name == snippetAttribute()) { - snippetLabel = attributes->takeAt(i).value().toString(); } } - CodeSnip snip; snip.position = position; snip.language = lang; - bool in_file = false; - - // Handler constructor.... - if (m_generate != TypeEntry::GenerateForSubclass && - m_generate != TypeEntry::GenerateNothing && - !fileName.isEmpty()) { - const QString resolved = m_database->modifiedTypesystemFilepath(fileName, m_currentPath); - if (QFile::exists(resolved)) { - QFile codeFile(resolved); - if (codeFile.open(QIODevice::Text | QIODevice::ReadOnly)) { - QString content = QLatin1String("// ========================================================================\n" - "// START of custom code block [file: "); - content += fileName; - content += QLatin1String("]\n"); - content += extractSnippet(QString::fromUtf8(codeFile.readAll()), snippetLabel); - content += QLatin1String("\n// END of custom code block [file: "); - content += fileName; - content += QLatin1String("]\n// ========================================================================\n"); - snip.addCode(content); - in_file = true; - } - } else { - qCWarning(lcShiboken).noquote().nospace() - << "File for inject code not exist: " << QDir::toNativeSeparators(fileName); - } - - } if (snip.language == TypeSystem::Interface && topElement.type != StackElement::InterfaceTypeEntry) { @@ -2298,7 +2308,7 @@ bool Handler::parseInjectCode(const QXmlStreamReader &, FunctionModification &mod = m_contextStack.top()->functionMods.last(); mod.snips << snip; - if (in_file) + if (!snip.code().isEmpty()) mod.modifiers |= FunctionModification::CodeInjection; element->type = StackElement::InjectCodeInFunction; } else if (topElement.type == StackElement::Root) { |