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 Parses the source file identified by \a filePath and adds its
15 parsed contents to the database. The \a location is used for
16 reporting errors.
17 */
18std::vector<UntiedDocumentation> PureDocParser::parse_qdoc_file(const QString &filePath)
19{
20 QFile in(filePath);
21 if (!in.open(flags: QIODevice::ReadOnly)) {
22 location.error(
23 QStringLiteral("Can't open source file '%1' (%2)").arg(args: filePath, args: strerror(errno)));
24 return {};
25 }
26
27 return processQdocComments(input_file&: in);
28}
29
30/*!
31 This is called by parseSourceFile() to do the actual parsing
32 and tree building. It only processes qdoc comments. It skips
33 everything else.
34 */
35std::vector<UntiedDocumentation> PureDocParser::processQdocComments(QFile& input_file)
36{
37 std::vector<UntiedDocumentation> untied{};
38
39 Tokenizer tokenizer(Location{input_file.fileName()}, input_file);
40
41 const QSet<QString> &commands = CppCodeParser::topic_commands + CppCodeParser::meta_commands;
42
43 int token = tokenizer.getToken();
44 while (token != Tok_Eoi) {
45 if (token != Tok_Doc) {
46 token = tokenizer.getToken();
47 continue;
48 }
49 QString comment = tokenizer.lexeme(); // returns an entire qdoc comment.
50 Location start_loc(tokenizer.location());
51 token = tokenizer.getToken();
52
53 Doc::trimCStyleComment(location&: start_loc, str&: comment);
54 Location end_loc(tokenizer.location());
55
56 // Doc constructor parses the comment.
57 Doc doc(start_loc, end_loc, comment, commands, CppCodeParser::topic_commands);
58 if (doc.topicsUsed().isEmpty()) {
59 doc.location().warning(QStringLiteral("This qdoc comment contains no topic command "
60 "(e.g., '\\%1', '\\%2').")
61 .arg(COMMAND_MODULE, COMMAND_PAGE));
62 continue;
63 }
64
65 if (hasTooManyTopics(doc))
66 continue;
67
68 untied.emplace_back(args: UntiedDocumentation{.documentation: doc, .context: QStringList()});
69 }
70
71 return untied;
72}
73
74QT_END_NAMESPACE
75

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