aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken2/ApiExtractor
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2018-11-22 16:56:55 +0100
committerAlexandru Croitor <alexandru.croitor@qt.io>2018-11-28 11:44:41 +0000
commitfce66005075ed04c99a313c243402d7bef2028ca (patch)
tree89a7fcbd73b769f169beab29d1e3e0cb2e0318e1 /sources/shiboken2/ApiExtractor
parentb067f541b46e0544a336629f7c4e2688e9465b67 (diff)
shiboken: Fix file snippet code missing for derived classes
Previously, the files were not read when no code was generated. This caused the code to be missing for methods in classes inheriting from a class in a dependent typesystem (for example, QAbstractSocket inheriting QIODevice). Split out the reading into a helper checking the attributes and always read the snippets. Task-number: PYSIDE-834 Change-Id: Iecc6285422afbf3b3e2ff4846850f8c2dbcabaf1 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/shiboken2/ApiExtractor')
-rw-r--r--sources/shiboken2/ApiExtractor/typesystem.cpp80
-rw-r--r--sources/shiboken2/ApiExtractor/typesystem_p.h1
2 files changed, 46 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) {
diff --git a/sources/shiboken2/ApiExtractor/typesystem_p.h b/sources/shiboken2/ApiExtractor/typesystem_p.h
index e36df5151..3ebdf47d7 100644
--- a/sources/shiboken2/ApiExtractor/typesystem_p.h
+++ b/sources/shiboken2/ApiExtractor/typesystem_p.h
@@ -225,6 +225,7 @@ private:
QXmlStreamAttributes *);
bool parseParentOwner(const QXmlStreamReader &, const StackElement &topElement,
QXmlStreamAttributes *);
+ bool readFileSnippet(QXmlStreamAttributes *attributes, CodeSnip *snip);
bool parseInjectCode(const QXmlStreamReader &, const StackElement &topElement,
StackElement* element, QXmlStreamAttributes *);
bool parseInclude(const QXmlStreamReader &, const StackElement &topElement,