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.

Pick-to: 6.5 6.4 6.2
Fixes: QTBUG-111340
Change-Id: I599ce384cfaf88a7347583a55976a3b98080435d
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Ulf Hermann 2023-02-22 11:06:54 +01:00
parent 7bf2bddd1f
commit ab9f4d2e26
5 changed files with 6 additions and 39 deletions

View File

@ -56,9 +56,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)
{}
@ -141,6 +140,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) {
@ -2585,17 +2587,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())
@ -3193,21 +3184,6 @@ QString QQmlJSCodeGenerator::conversion(const QQmlJSScope::ConstPtr &from,
return QString();
}
int QQmlJSCodeGenerator::nextJSLine(uint line) const
{
auto findLine = [](int line, const QV4::CompiledData::CodeOffsetToLineAndStatement &entry) {
return entry.line > line;
};
const auto codeToLine
= std::upper_bound(m_context->lineAndStatementNumberMapping.constBegin(),
m_context->lineAndStatementNumberMapping.constEnd(),
line,
findLine);
bool bNoNextLine = m_context->lineAndStatementNumberMapping.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));

View File

@ -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,
@ -288,17 +288,12 @@ private:
return m_typeResolver->jsGlobalObject()->property(u"console"_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;

View File

@ -650,7 +650,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'/',
@ -796,8 +795,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;
}

View File

@ -76,7 +76,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;

View File

@ -27,7 +27,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>