summaryrefslogtreecommitdiffstats
path: root/src/qdoc/qdoc/src/qdoc/puredocparser.cpp
blob: c6de06a9c672669e66d1621a5e7955eeadd32c1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "puredocparser.h"

#include "qdocdatabase.h"
#include "tokenizer.h"

#include <cerrno>

QT_BEGIN_NAMESPACE

/*!
  Parses the source file identified by \a filePath and adds its
  parsed contents to the database. The \a location is used for
  reporting errors.
 */
std::vector<UntiedDocumentation> PureDocParser::parse_qdoc_file(const QString &filePath)
{
    QFile in(filePath);
    if (!in.open(QIODevice::ReadOnly)) {
        location.error(
                QStringLiteral("Can't open source file '%1' (%2)").arg(filePath, strerror(errno)));
        return {};
    }

    return processQdocComments(in);
}

/*!
  This is called by parseSourceFile() to do the actual parsing
  and tree building. It only processes qdoc comments. It skips
  everything else.
 */
std::vector<UntiedDocumentation> PureDocParser::processQdocComments(QFile& input_file)
{
    std::vector<UntiedDocumentation> untied{};

    Tokenizer tokenizer(Location{input_file.fileName()}, input_file);

    const QSet<QString> &commands = CppCodeParser::topic_commands + CppCodeParser::meta_commands;

    int token = tokenizer.getToken();
    while (token != Tok_Eoi) {
        if (token != Tok_Doc) {
            token = tokenizer.getToken();
            continue;
        }
        QString comment = tokenizer.lexeme(); // returns an entire qdoc comment.
        Location start_loc(tokenizer.location());
        token = tokenizer.getToken();

        Doc::trimCStyleComment(start_loc, comment);
        Location end_loc(tokenizer.location());

        // Doc constructor parses the comment.
        Doc doc(start_loc, end_loc, comment, commands, CppCodeParser::topic_commands);
        if (doc.topicsUsed().isEmpty()) {
            doc.location().warning(QStringLiteral("This qdoc comment contains no topic command "
                                                  "(e.g., '\\%1', '\\%2').")
                                           .arg(COMMAND_MODULE, COMMAND_PAGE));
            continue;
        }

        if (hasTooManyTopics(doc))
            continue;

        untied.emplace_back(UntiedDocumentation{doc, QStringList()});
    }

    return untied;
}

QT_END_NAMESPACE