1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "puredocparser.h"
5
6#include "qdocdatabase.h"
7#include "tokenizer.h"
8
9#include <cerrno>
10
11QT_BEGIN_NAMESPACE
12
13/*!
14 Returns a list of the kinds of files that the pure doc
15 parser is meant to parse. The elements of the list are
16 file suffixes.
17 */
18QStringList PureDocParser::sourceFileNameFilter()
19{
20 return QStringList() << "*.qdoc"
21 << "*.qtx"
22 << "*.qtt"
23 << "*.js";
24}
25
26/*!
27 Parses the source file identified by \a filePath and adds its
28 parsed contents to the database. The \a location is used for
29 reporting errors.
30 */
31void PureDocParser::parseSourceFile(const Location &location, const QString &filePath, CppCodeParser& cpp_code_parser)
32{
33 QFile in(filePath);
34 if (!in.open(flags: QIODevice::ReadOnly)) {
35 location.error(
36 QStringLiteral("Can't open source file '%1' (%2)").arg(args: filePath, args: strerror(errno)));
37 return;
38 }
39
40 /*
41 The set of open namespaces is cleared before parsing
42 each source file. The word "source" here means cpp file.
43 */
44 m_qdb->clearOpenNamespaces();
45
46 processQdocComments(input_file&: in, cpp_code_parser);
47 in.close();
48}
49
50/*!
51 This is called by parseSourceFile() to do the actual parsing
52 and tree building. It only processes qdoc comments. It skips
53 everything else.
54 */
55void PureDocParser::processQdocComments(QFile& input_file, CppCodeParser& cpp_code_parser)
56{
57 Tokenizer tokenizer(Location{input_file.fileName()}, input_file);
58
59 const QSet<QString> &commands = CppCodeParser::topic_commands + CppCodeParser::meta_commands;
60
61 int token = tokenizer.getToken();
62 while (token != Tok_Eoi) {
63 if (token != Tok_Doc) {
64 token = tokenizer.getToken();
65 continue;
66 }
67 QString comment = tokenizer.lexeme(); // returns an entire qdoc comment.
68 Location start_loc(tokenizer.location());
69 token = tokenizer.getToken();
70
71 Doc::trimCStyleComment(location&: start_loc, str&: comment);
72 Location end_loc(tokenizer.location());
73
74 // Doc constructor parses the comment.
75 Doc doc(start_loc, end_loc, comment, commands, CppCodeParser::topic_commands);
76 const TopicList &topics = doc.topicsUsed();
77 if (topics.isEmpty()) {
78 doc.location().warning(QStringLiteral("This qdoc comment contains no topic command "
79 "(e.g., '\\%1', '\\%2').")
80 .arg(COMMAND_MODULE, COMMAND_PAGE));
81 continue;
82 }
83
84 if (cpp_code_parser.hasTooManyTopics(doc))
85 continue;
86
87 DocList docs;
88 NodeList nodes;
89 QString topic = topics[0].m_topic;
90
91 cpp_code_parser.processTopicArgs(doc, topic, nodes, docs);
92 cpp_code_parser.processMetaCommands(nodes, docs);
93 }
94}
95
96QT_END_NAMESPACE
97

source code of qttools/src/qdoc/qdoc/puredocparser.cpp