| 1 | // Copyright (C) 2024 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 "parsererror.h" |
| 5 | #include "node.h" |
| 6 | #include "qdocdatabase.h" |
| 7 | #include "config.h" |
| 8 | #include "utilities.h" |
| 9 | |
| 10 | #include <QtCore/qregularexpression.h> |
| 11 | |
| 12 | using namespace Qt::Literals::StringLiterals; |
| 13 | |
| 14 | QT_BEGIN_NAMESPACE |
| 15 | |
| 16 | /*! |
| 17 | \class FnMatchError |
| 18 | \brief Encapsulates information about \fn match error during parsing. |
| 19 | */ |
| 20 | |
| 21 | /*! |
| 22 | \variable FnMatchError::signature |
| 23 | |
| 24 | Signature for the \fn topic that failed to match. |
| 25 | */ |
| 26 | |
| 27 | /*! |
| 28 | \relates FnMatchError |
| 29 | |
| 30 | Returns \c true if any parent of a C++ function represented by |
| 31 | \a signature is documented as \\internal. |
| 32 | */ |
| 33 | bool isParentInternal(const QString &signature) |
| 34 | { |
| 35 | const QRegularExpression scoped_fn{R"((?:\w+(?:<[^>]+>)?::)+~?\w\S*\()"}; |
| 36 | auto match = scoped_fn.match(subject: signature); |
| 37 | if (!match.isValid()) |
| 38 | return false; |
| 39 | |
| 40 | auto scope = match.captured().split(sep: "::"_L1); |
| 41 | scope.removeLast(); // Drop function name |
| 42 | |
| 43 | for (auto &s : scope) |
| 44 | if (qsizetype pos = s.indexOf(ch: '<'); pos >= 0) |
| 45 | s.truncate(pos); |
| 46 | |
| 47 | auto parent = QDocDatabase::qdocDB()->findNodeByNameAndType(path: scope, isMatch: &Node::isCppNode); |
| 48 | if (parent && !(parent->isClassNode() || parent->isNamespace())) { |
| 49 | qCDebug(lcQdoc).noquote() |
| 50 | << "Invalid scope:"<< qPrintable(parent->nodeTypeString()) |
| 51 | << qPrintable(parent->fullName()) |
| 52 | << "for \\fn"<< qPrintable(signature); |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | while (parent) { |
| 57 | if (parent->isInternal()) |
| 58 | return true; |
| 59 | parent = parent->parent(); |
| 60 | } |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | /*! |
| 66 | \class ParserErrorHandler |
| 67 | \brief Processes parser errors and outputs warnings for them. |
| 68 | */ |
| 69 | |
| 70 | /*! |
| 71 | Generates a warning specific to FnMatchError. |
| 72 | |
| 73 | Warnings for internal documentation are omitted. Specifically, this |
| 74 | (omission) happens if: |
| 75 | |
| 76 | \list |
| 77 | \li \c {--showinternal} command line option is \b not |
| 78 | used, and |
| 79 | \li The warning is for an \\fn that is declared |
| 80 | under a namespace/class that is documented as |
| 81 | \\internal. |
| 82 | \endlist |
| 83 | */ |
| 84 | void ParserErrorHandler::operator()(const FnMatchError &e) const |
| 85 | { |
| 86 | if (Config::instance().showInternal() || !isParentInternal(signature: e.signature)) |
| 87 | e.location.warning(message: "Failed to find function when parsing \\fn %1"_L1.arg(args: e.signature)); |
| 88 | } |
| 89 | |
| 90 | QT_END_NAMESPACE |
| 91 |
