aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2023-02-22 11:06:54 +0100
committerUlf Hermann <ulf.hermann@qt.io>2023-02-28 08:56:42 +0100
commitd01c2454a8189fd870cdf9117c9f3a273efd2ccf (patch)
treedea54deca09f665751f0cf3c28f631a9c3d48a55
parent4f7cda0c9fadecbe4c715f515ccc7535b53b28d1 (diff)
QmlCompiler: Drop broken line comments in generated C++
They didn't work because the ordering of instructions is not the same as the ordering of lines. They also weren't very helpful because a single line may result in multiple instructions and vice versa. On top of everything, they also introduced UB via the std::upper_bound call. Rather, just print the name of the function and the place in the file at the beginning of each C++ function. That is much more helpful since we can then just correlate it to the original QML code. For instruction-by-instruction mapping we have to consult the byte code trace anyway. Fixes: QTBUG-111340 Change-Id: I599ce384cfaf88a7347583a55976a3b98080435d Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> (cherry picked from commit ab9f4d2e2614d693c254a027a28824e83c7760f0)
-rw-r--r--src/qmlcompiler/qqmljscodegenerator.cpp32
-rw-r--r--src/qmlcompiler/qqmljscodegenerator_p.h7
-rw-r--r--src/qmlcompiler/qqmljscompiler.cpp4
-rw-r--r--src/qmlcompiler/qqmljscompiler_p.h1
-rw-r--r--src/qmlcompiler/qqmljslintercodegen.cpp1
5 files changed, 6 insertions, 39 deletions
diff --git a/src/qmlcompiler/qqmljscodegenerator.cpp b/src/qmlcompiler/qqmljscodegenerator.cpp
index f8009d6ae7..a14ea33e14 100644
--- a/src/qmlcompiler/qqmljscodegenerator.cpp
+++ b/src/qmlcompiler/qqmljscodegenerator.cpp
@@ -52,9 +52,8 @@ QString QQmlJSCodeGenerator::castTargetName(const QQmlJSScope::ConstPtr &type) c
QQmlJSCodeGenerator::QQmlJSCodeGenerator(const QV4::Compiler::Context *compilerContext,
const QV4::Compiler::JSUnitGenerator *unitGenerator,
const QQmlJSTypeResolver *typeResolver,
- QQmlJSLogger *logger, const QStringList &sourceCodeLines)
+ QQmlJSLogger *logger)
: QQmlJSCompilePass(unitGenerator, typeResolver, logger)
- , m_sourceCodeLines(sourceCodeLines)
, m_context(compilerContext)
{}
@@ -137,6 +136,9 @@ QT_WARNING_POP
QQmlJSAotFunction result;
result.includes.swap(m_includes);
+ result.code += u"// %1 at line %2, column %3\n"_s
+ .arg(m_context->name).arg(m_context->line).arg(m_context->column);
+
QDuplicateTracker<QString> generatedVariables;
for (auto registerIt = m_registerVariables.cbegin(), registerEnd = m_registerVariables.cend();
registerIt != registerEnd; ++registerIt) {
@@ -2252,17 +2254,6 @@ QV4::Moth::ByteCodeHandler::Verdict QQmlJSCodeGenerator::startInstruction(
|| !m_state.accumulatorVariableOut.isEmpty()
|| !isTypeStorable(m_typeResolver, m_state.changedRegister().storedType()));
- const int currentLine = currentSourceLocation().startLine;
- if (currentLine != m_lastLineNumberUsed) {
- const int nextLine = nextJSLine(currentLine);
- for (auto line = currentLine - 1; line < nextLine - 1; ++line) {
- m_body += u"// "_s;
- m_body += m_sourceCodeLines.value(line).trimmed();
- m_body += u'\n';
- }
- m_lastLineNumberUsed = currentLine;
- }
-
// If the instruction has no side effects and doesn't write any register, it's dead.
// We might still need the label, though, and the source code comment.
if (!m_state.hasSideEffects() && changedRegisterVariable().isEmpty())
@@ -2740,21 +2731,6 @@ QString QQmlJSCodeGenerator::conversion(const QQmlJSScope::ConstPtr &from,
return QString();
}
-int QQmlJSCodeGenerator::nextJSLine(uint line) const
-{
- auto findLine = [](uint line, const QV4::CompiledData::CodeOffsetToLine &entry) {
- return entry.line > line;
- };
- const auto codeToLine
- = std::upper_bound(m_context->lineNumberMapping.constBegin(),
- m_context->lineNumberMapping.constEnd(),
- line,
- findLine);
- bool bNoNextLine = m_context->lineNumberMapping.constEnd() == codeToLine;
-
- return static_cast<int>(bNoNextLine ? -1 : codeToLine->line);
-}
-
void QQmlJSCodeGenerator::reject(const QString &thing)
{
setError(u"Cannot generate efficient code for %1"_s.arg(thing));
diff --git a/src/qmlcompiler/qqmljscodegenerator_p.h b/src/qmlcompiler/qqmljscodegenerator_p.h
index fb54ac0ad2..6fdad57831 100644
--- a/src/qmlcompiler/qqmljscodegenerator_p.h
+++ b/src/qmlcompiler/qqmljscodegenerator_p.h
@@ -33,7 +33,7 @@ public:
QQmlJSCodeGenerator(const QV4::Compiler::Context *compilerContext,
const QV4::Compiler::JSUnitGenerator *unitGenerator,
const QQmlJSTypeResolver *typeResolver,
- QQmlJSLogger *logger, const QStringList &sourceCodeLines);
+ QQmlJSLogger *logger);
~QQmlJSCodeGenerator() = default;
QQmlJSAotFunction run(const Function *function, const InstructionAnnotations *annotations,
@@ -270,17 +270,12 @@ private:
return m_typeResolver->jsGlobalObject()->property(u"Math"_s).type();
}
- int nextJSLine(uint line) const;
-
- QStringList m_sourceCodeLines;
-
// map from instruction offset to sequential label number
QHash<int, QString> m_labels;
const QV4::Compiler::Context *m_context = nullptr;
const InstructionAnnotations *m_annotations = nullptr;
- int m_lastLineNumberUsed = -1;
bool m_skipUntilNextLabel = false;
QStringList m_includes;
diff --git a/src/qmlcompiler/qqmljscompiler.cpp b/src/qmlcompiler/qqmljscompiler.cpp
index 49c7c74040..d2e43b6f0b 100644
--- a/src/qmlcompiler/qqmljscompiler.cpp
+++ b/src/qmlcompiler/qqmljscompiler.cpp
@@ -624,7 +624,6 @@ void QQmlJSAotCompiler::setDocument(
m_logger->setFileName(resourcePathInfo.fileName());
m_logger->setCode(irDocument->code);
m_unitGenerator = &irDocument->jsGenerator;
- m_entireSourceCodeLines = irDocument->code.split(u'\n');
QQmlJSScope::Ptr target = QQmlJSScope::create();
QQmlJSImportVisitor visitor(target, m_importer, m_logger,
resourcePathInfo.canonicalPath() + u'/',
@@ -765,8 +764,7 @@ QQmlJSAotFunction QQmlJSAotCompiler::doCompile(
return compileError();
QQmlJSCodeGenerator codegen(
- context, m_unitGenerator, &m_typeResolver, m_logger,
- m_entireSourceCodeLines);
+ context, m_unitGenerator, &m_typeResolver, m_logger);
QQmlJSAotFunction result = codegen.run(function, &typePropagationResult, error);
return error->isValid() ? compileError() : result;
}
diff --git a/src/qmlcompiler/qqmljscompiler_p.h b/src/qmlcompiler/qqmljscompiler_p.h
index c6a60f5a94..45de4abaf1 100644
--- a/src/qmlcompiler/qqmljscompiler_p.h
+++ b/src/qmlcompiler/qqmljscompiler_p.h
@@ -75,7 +75,6 @@ protected:
const QString &message, QtMsgType type, const QQmlJS::SourceLocation &location) const;
QQmlJSTypeResolver m_typeResolver;
- QStringList m_entireSourceCodeLines;
const QString m_resourcePath;
const QStringList m_qmldirFiles;
diff --git a/src/qmlcompiler/qqmljslintercodegen.cpp b/src/qmlcompiler/qqmljslintercodegen.cpp
index dfa474fc46..e362044779 100644
--- a/src/qmlcompiler/qqmljslintercodegen.cpp
+++ b/src/qmlcompiler/qqmljslintercodegen.cpp
@@ -28,7 +28,6 @@ void QQmlJSLinterCodegen::setDocument(const QmlIR::JSCodeGen *codegen,
Q_UNUSED(codegen);
m_document = document;
m_unitGenerator = &document->jsGenerator;
- m_entireSourceCodeLines = document->code.split(u'\n');
}
std::variant<QQmlJSAotFunction, QQmlJS::DiagnosticMessage>