summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Di Sera <luca.disera@qt.io>2023-03-24 13:55:05 +0100
committerLuca Di Sera <luca.disera@qt.io>2023-03-27 11:44:57 +0100
commitf6291e2163c96e9c6d15c0ac52a8313c4580cb8d (patch)
tree6ed8df9c1769e276c28cf02b920b42a5ffd450dc
parent23f0761d22bd972b046614fa173b29555b2d2047 (diff)
downloadqttools-f6291e2163c96e9c6d15c0ac52a8313c4580cb8d.tar.gz
QDoc: Remove `QmlCodeParser::m_parser`
As part of producing a documentation set, QDoc parses a series of user-defined input source files in various languages to extract the user-provided documentation and certain source-code level elements. For QML source files, the `QmlCodeParser` class is used, taking care of reading the QML code and build an internal representation for some of its elements. `QmlCodeParser` uses the `QQmlJs` parsing api to do so. As part of using the `QQmlJs` parsing api, `QmlCodeParser` has to build a `QQmlJs::Engine`, `QQmlJs::Lexer` and `QQmlJsEngine::Parser`. Currently, `QmlCodeParser` stores the engine and parser elements as instance members. For example, for the `QQmlJs::Parser` instance, a member variable `m_parser` is used as storage. Supposedly, this is intended as a way to avoid building the three elements more than once. Building a `QQmlJs::Parser` more than once does not provide any meaningful overhead, moreover not in the code path where it is used. Furthermore, `m_parser` is used only a few times, in `QmlCodeParser::parseSourceFile`, the entry point for parsing a QML source file. Hence, to reduce the scope of `m_parser` and avoid requiring an out-of-scope initialization for it, `m_parser` is now removed in favor of a local instance in `QmlCodeParser::parseSourceFile`. The initialization and deletion of `m_parser` in `QmlCodeParser::initializeParser` and `QmlCodeParser::terminateParser` was removed as a consequence of the removal of the member. As `QmlCodeParser::initializeParser` and `QmlCodeParser::terminateParser` were only taking care of `m_parser`, their implementation is now removed as dead code. As `initializeParser` and `terminateParser`are required to be implemented, by `CodeParser`, `QmlCodeParser` base class, due to their pure virtual nature, an empty implementation was added to "qmlcodeparser.h". Similarly, the constructor for `QmlCodeParser` was only taking care of initializing `m_parser` and is now removed in favor of a defaulted implementation. Change-Id: Id07e6d9cf8083c83482232606fb9a2611dcce420 Reviewed-by: Topi Reiniƶ <topi.reinio@qt.io> Reviewed-by: Paul Wicking <paul.wicking@qt.io>
-rw-r--r--src/qdoc/qmlcodeparser.cpp31
-rw-r--r--src/qdoc/qmlcodeparser.h7
2 files changed, 8 insertions, 30 deletions
diff --git a/src/qdoc/qmlcodeparser.cpp b/src/qdoc/qmlcodeparser.cpp
index 3d8cd5483..f2fcea5d3 100644
--- a/src/qdoc/qmlcodeparser.cpp
+++ b/src/qdoc/qmlcodeparser.cpp
@@ -14,29 +14,6 @@
QT_BEGIN_NAMESPACE
/*!
- Constructs the QML code parser.
- */
-QmlCodeParser::QmlCodeParser() : m_parser(nullptr) { }
-
-/*!
- Initializes the code parser base class.
- Also creates a parser from QQmlJS.
- */
-void QmlCodeParser::initializeParser()
-{
- m_parser = new QQmlJS::Parser(&m_engine);
-}
-
-/*!
- Terminates the QML code parser. Deletes the parser
- created by the constructor.
- */
-void QmlCodeParser::terminateParser()
-{
- delete m_parser;
-}
-
-/*!
Returns "QML".
*/
QString QmlCodeParser::language()
@@ -83,15 +60,17 @@ void QmlCodeParser::parseSourceFile(const Location &location, const QString &fil
QQmlJS::Lexer lexer{&m_engine};
lexer.setCode(newCode, 1);
- if (m_parser->parse()) {
- QQmlJS::AST::UiProgram *ast = m_parser->ast();
+ QQmlJS::Parser parser{&m_engine};
+
+ if (parser.parse()) {
+ QQmlJS::AST::UiProgram *ast = parser.ast();
QmlDocVisitor visitor(filePath, newCode, &m_engine, topic_commands + CodeParser::common_meta_commands,
topic_commands);
QQmlJS::AST::Node::accept(ast, &visitor);
if (visitor.hasError())
Location(filePath).warning("Could not analyze QML file, output is incomplete.");
}
- const auto &messages = m_parser->diagnosticMessages();
+ const auto &messages = parser.diagnosticMessages();
for (const auto &msg : messages) {
qCDebug(lcQdoc, "%s: %d: %d: QML syntax error: %s", qUtf8Printable(filePath),
msg.loc.startLine, msg.loc.startColumn, qUtf8Printable(msg.message));
diff --git a/src/qdoc/qmlcodeparser.h b/src/qdoc/qmlcodeparser.h
index bbf2a1545..239e89473 100644
--- a/src/qdoc/qmlcodeparser.h
+++ b/src/qdoc/qmlcodeparser.h
@@ -20,11 +20,11 @@ class QString;
class QmlCodeParser : public CodeParser
{
public:
- QmlCodeParser();
+ QmlCodeParser() = default;
~QmlCodeParser() override = default;
- void initializeParser() override;
- void terminateParser() override;
+ void initializeParser() override {}
+ void terminateParser() override {}
QString language() override;
QStringList sourceFileNameFilter() override;
void parseSourceFile(const Location &location, const QString &filePath, CppCodeParser&) override;
@@ -34,7 +34,6 @@ public:
private:
QQmlJS::Engine m_engine {};
- QQmlJS::Parser *m_parser { nullptr };
};
QT_END_NAMESPACE