Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
locationplan.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 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 "locationplan.h"
5
6#include "aggregate.h"
8#include "config.h"
9#include "functionnode.h"
11#include "node.h"
13#include "propertynode.h"
14#include "textutils.h"
15#include "tree.h"
16#include "typedefnode.h"
17#include "utilities.h"
18
19#include <QtCore/qfileinfo.h>
20
22
23using namespace Qt::StringLiterals;
24
25/*!
26 \struct LocationRules
27 \internal
28 \inmodule QDoc
29 \brief Holds the configuration a format needs to name its published files.
30
31 The rules are a value type captured once per format. They replace the
32 generator statics that the legacy pipeline reads while rendering, so a
33 location can be planned without a generator instance.
34*/
35
36/*!
37 \variable LocationRules::fileExtension
38 \brief The file extension, without a separator, that pages of this format use.
39*/
40
41/*!
42 \variable LocationRules::project
43 \brief The project identity used to prefix example page names.
44*/
45
46/*!
47 \variable LocationRules::outputPrefixes
48 \brief The configured \c{outputprefixes}, keyed by the \c CPP and \c QML genus names.
49
50 QML pages default to the \c{qml-} prefix when the configuration names no
51 QML prefix.
52*/
53
54/*!
55 \variable LocationRules::outputSuffixes
56 \brief The configured \c{outputsuffixes}, keyed by the \c CPP and \c QML genus names.
57*/
58
59/*!
60 \struct PublicationLocation
61 \internal
62 \inmodule QDoc
63 \brief Names where one entity is published in one format.
64*/
65
66/*!
67 \variable PublicationLocation::path
68 \brief The page path relative to the module's publication directory, or the explicit URL.
69*/
70
71/*!
72 \variable PublicationLocation::fragment
73 \brief The anchor within the page, without the \c{#} separator; empty for a page.
74*/
75
76/*!
77 \variable PublicationLocation::explicitUrl
78 \brief Whether \c path is an authored or imported URL that is used verbatim.
79
80 An explicit URL is not split into a page and a fragment, so it survives
81 unchanged however it was spelled.
82*/
83
84/*!
85 \class LocationPlan
86 \internal
87 \inmodule QDoc
88 \brief Plans where a tree's entities are published in one format, before rendering.
89
90 A plan is built from a frozen tree and fixed LocationRules. It records a
91 PublicationLocation for every entity it visits: the page that documents
92 the entity and the fragment that marks it there. Exchange metadata and
93 link resolution read the plan instead of asking a generator, so every
94 consumer sees the same answer and the answer cannot drift while pages
95 render.
96
97 Entities that are not in the plan have no publication location.
98
99 The plan is not thread-safe: file bases are memoized from const queries.
100 Create one plan per thread or synchronize externally.
101*/
102
103/*!
104 \typealias LocationPlan::FileBase
105 \internal
106 \brief A callback that returns a node's file name stem without extension.
107*/
108
109namespace {
110
111QString genusKey(const Node *node)
112{
113 switch (node->genus()) {
114 case Genus::QML:
115 return u"QML"_s;
116 case Genus::CPP:
117 return u"CPP"_s;
118 default:
119 return {};
120 }
121}
122
123/*
124 The entity whose page and anchor \a node is documented under. A function
125 that only exists to document a property resolves to that property, and a
126 typedef that names an enum's underlying type resolves to that enum.
127*/
128const Node *locationTarget(const Node *node)
129{
130 if (node->isFunction()) {
131 const auto *fn = static_cast<const FunctionNode *>(node);
132 switch (fn->metaness()) {
136 return node;
137 default:
138 break;
139 }
140 if (fn->isDtor())
141 return node;
142 if (const auto *p = fn->primaryAssociatedProperty(); p && fn->doc().isEmpty())
143 return p;
144 return node;
145 }
146 if (node->nodeType() == NodeType::Typedef) {
147 const auto *tdef = static_cast<const TypedefNode *>(node);
148 if (tdef->associatedEnum())
149 return tdef->associatedEnum();
150 }
151 return node;
152}
153
154/*
155 The anchor of \a node on its page, without a separator and without
156 following redirects. Page-level entities have none.
157*/
158QString fragmentOf(const Node *node)
159{
160 switch (node->nodeType()) {
161 case NodeType::Function: {
162 const auto *fn = static_cast<const FunctionNode *>(node);
163 switch (fn->metaness()) {
165 return node->name() + "-signal"_L1;
167 return node->name() + "-signal-handler"_L1;
168 case Metaness::QmlMethod: {
169 QString ref = node->name() + "-method"_L1;
170 if (fn->overloadNumber() > 0)
171 ref += u'-' + QString::number(fn->overloadNumber());
172 return ref;
173 }
174 default:
175 if (fn->isDtor())
176 return "dtor."_L1 + fn->name().mid(1);
177 if (fn->overloadNumber() > 0)
178 return TextUtils::cleanAnchorRef(fn->name()) + u'-'
179 + QString::number(fn->overloadNumber());
180 return TextUtils::cleanAnchorRef(fn->name());
181 }
182 }
183 /*
184 Use node->name() instead of the file base as the latter is
185 lower-case. For HTML anchors, we need to preserve the case.
186 */
187 case NodeType::Enum:
189 return node->name() + "-enum"_L1;
192 return node->name() + "-typedef"_L1;
194 return node->name() + "-prop"_L1;
196 if (!node->isPropertyGroup())
197 return {};
198 Q_FALLTHROUGH();
200 if (node->isAttached())
201 return node->name() + "-attached-prop"_L1;
202 return node->name() + "-prop"_L1;
204 return node->name() + "-var"_L1;
205 default:
206 return {};
207 }
208}
209
210} // namespace
211
212/*!
213 Reads the naming configuration from \a config for pages that use
214 \a fileExtension.
215
216 This mirrors the values Generator::initialize() stores in statics, so a
217 plan built from these rules names files exactly as the legacy pipeline does.
218*/
219LocationRules LocationRules::fromConfig(const Config &config, const QString &fileExtension)
220{
221 LocationRules rules;
222 rules.fileExtension = fileExtension;
223 rules.project = config.get(CONFIG_PROJECT).asString();
224 const QStringList prefixes = config.get(CONFIG_OUTPUTPREFIXES).asStringList();
225 for (const QString &prefix : prefixes)
226 rules.outputPrefixes[prefix] =
227 config.get(CONFIG_OUTPUTPREFIXES + Config::dot + prefix).asString();
228 if (!prefixes.contains(u"QML"_s))
229 rules.outputPrefixes[u"QML"_s] = u"qml-"_s;
230 const QStringList suffixes = config.get(CONFIG_OUTPUTSUFFIXES).asStringList();
231 for (const QString &suffix : suffixes)
232 rules.outputSuffixes[suffix] =
233 config.get(CONFIG_OUTPUTSUFFIXES + Config::dot + suffix).asString();
234 return rules;
235}
236
237/*!
238 Returns the file name prefix for \a node. Only non-collection pages carry
239 a prefix, so module and group pages keep their bare names.
240*/
242{
243 if (node->isPageNode() && !node->isCollectionNode())
244 return outputPrefixes.value(genusKey(node));
245 return {};
246}
247
248/*!
249 Returns the file name suffix for \a node. Only pages carry a suffix.
250*/
252{
253 if (node->isPageNode())
254 return outputSuffixes.value(genusKey(node));
255 return {};
256}
257
258/*!
259 Plans the locations of every entity in \a tree under \a rules.
260
261 The walk covers the root namespace and its descendants, the tree's
262 collection nodes, and its proxy aggregates, which is the same set the
263 index export serializes. The tree is only read.
264*/
266{
267 LocationPlan plan(std::move(rules));
268 if (!tree)
269 return plan;
270
271 const std::function<void(const Node *)> walk = [&](const Node *node) {
272 plan.record(node);
273 if (!node->isAggregate())
274 return;
275 const auto *aggregate = static_cast<const Aggregate *>(node);
276 for (const Node *child : aggregate->childNodes())
277 walk(child);
278 };
279 walk(tree->root());
280 for (const CNMap *collections : { &tree->groups(), &tree->modules(), &tree->qmlModules(),
281 &tree->concepts() }) {
282 for (auto it = collections->constBegin(); it != collections->constEnd(); ++it)
283 plan.record(it.value());
284 }
285 for (const Node *proxy : tree->proxies())
286 walk(proxy);
287 return plan;
288}
289
290/*!
291 Plans the entities whose pages are produced under \a policy. Exchange
292 export uses build() instead: its compatibility inventory also includes
293 entities that do not produce a local page.
294*/
296 const InclusionPolicy &policy)
297{
298 auto plan = build(std::move(rules), tree);
299 for (auto it = plan.m_entities.begin(); it != plan.m_entities.end();) {
300 const Node *target = locationTarget(it.key());
301 const Node *page = target;
302 if (!page->isPageNode() && !page->isCollectionNode())
303 page = page->parent();
304
305 bool published = page && page->parent() && page->isPageNode();
306 for (const Node *n = target; published && n; n = n->parent()) {
307 published = n->url().isNull() && !n->isIndexNode() && !n->isExternalPage()
309 }
310 if (published && page->isCollectionNode()) {
311 const auto *collection = static_cast<const CollectionNode *>(page);
312 published = collection->wasSeen() || collection->isGenericCollection();
313 } else if (published && (page->isClassNode() || page->isHeader() || page->isNamespace())) {
314 published = page->docMustBeGenerated();
315 }
316 if (!published) {
317 it = plan.m_entities.erase(it);
318 } else {
319 // fileName preserves an authored non-HTML page suffix, unlike
320 // the historical exchange-index location algorithm.
321 it.value() = { plan.fileName(target), resolveFragment(target), false };
322 ++it;
323 }
324 }
325 return plan;
326}
327
328/*!
329 Builds plans for requested publications in \a output. The compatibility
330 driver exposes only its selected legacy HTML publication as a fallback;
331 historical template format aliases keep their own naming plans.
332*/
333LocationPlanSet LocationPlanSet::build(const Config &config, const Tree *tree,
334 const OutputConfiguration &output)
335{
336 LocationPlanSet result;
337 if (output.hasErrors())
338 return result;
339 const auto addPublication = [&](const QString &format, const QString &extension,
340 const QString &directory) {
341 result.add(format, directory,
342 LocationPlan::buildPublished(LocationRules::fromConfig(config, extension),
343 tree, config.createInclusionPolicy()));
344 };
346 if (config.getOutputFormats().contains(u"HTML"_s))
347 addPublication(u"HTML"_s, u"html"_s, config.getOutputDir(u"HTML"_s));
348 } else {
349 for (const auto &format : output.formats) {
350 if (format.requested)
351 addPublication(format.format, format.fileExtension, output.destination(format));
352 }
353 }
354 return result;
355}
356
357/*!
358 Resolves the full document location of \a node from the caller's
359 \a fileBase and \a fileExtension: the page file name followed by the
360 anchor fragment for a member.
361
362 This is the single home of the legacy location rules. A node with an
363 explicit URL yields that URL. Members resolve through their parent's
364 page, deprecated members through the obsolete sub-page, and functions
365 that only document a property resolve to that property's anchor.
366 Overloaded C++ and QML methods carry their overload number, as the
367 rendered headings do, so a location names one overload and not the
368 first one that shares the name. The fragment is the one
369 resolveFragment() returns. Returns an empty string for an entity that
370 has no page of its own.
371*/
372QString LocationPlan::resolveDocumentLocation(const Node *node, const FileBase &fileBase,
373 const QString &fileExtension)
374{
375 if (node == nullptr)
376 return QString();
377 if (!node->url().isEmpty())
378 return node->url();
379 if (const Node *target = locationTarget(node); target != node)
380 return resolveDocumentLocation(target, fileBase, fileExtension);
381
382 QString parentName;
383
384 if (node->isNamespace()) {
385 /*
386 The root namespace has no name - check for this before creating
387 an attribute containing the location of any documentation.
388 */
389 if (!fileBase(node).isEmpty())
390 parentName = fileBase(node) + QLatin1Char('.') + fileExtension;
391 else
392 return QString();
393 } else if (node->isQmlType()) {
394 return fileBase(node) + QLatin1Char('.') + fileExtension;
395 } else if (node->isTextPageNode() || node->isCollectionNode()) {
396 parentName = fileBase(node) + QLatin1Char('.') + fileExtension;
397 } else if (fileBase(node).isEmpty())
398 return QString();
399
400 if (node->parent()) {
401 // use the parent's name unless the parent is the root namespace
402 if (!node->parent()->isNamespace() || !node->parent()->name().isEmpty())
403 parentName = resolveDocumentLocation(node->parent(), fileBase, fileExtension);
404 }
405
406 switch (node->nodeType()) {
407 case NodeType::Class:
408 case NodeType::Struct:
409 case NodeType::Union:
411 case NodeType::Proxy:
412 parentName = fileBase(node) + QLatin1Char('.') + fileExtension;
413 break;
415 case NodeType::Page:
416 case NodeType::Group:
418 case NodeType::Module:
419 case NodeType::QmlModule: {
420 parentName = fileBase(node);
421 parentName.replace(QLatin1Char('/'), QLatin1Char('-'))
422 .replace(QLatin1Char('.'), QLatin1Char('-'));
423 parentName += QLatin1Char('.') + fileExtension;
424 } break;
425 default:
426 break;
427 }
428
429 const QString fragment = fragmentOf(node);
430 const QString anchorRef = fragment.isEmpty() ? QString() : u'#' + fragment;
431
432 if (!node->isPageNode() && node->isDeprecated())
433 parentName.replace(u'.' + fileExtension, "-obsolete."_L1 + fileExtension);
434
435 return parentName.toLower() + anchorRef;
436}
437
438/*!
439 Resolves the anchor fragment of \a node on its page, without the
440 separator, following the same redirects as resolveDocumentLocation().
441 Returns an empty string for an entity that is a page of its own or has
442 no anchor, such as an anonymous shared comment.
443
444 Rendered headings and planned locations both take their ids from here,
445 so a link planned for an entity lands on the heading the page emits.
446*/
447QString LocationPlan::resolveFragment(const Node *node)
448{
449 if (node == nullptr)
450 return QString();
451 return fragmentOf(locationTarget(node));
452}
453
454/*!
455 Returns the page path for the example source or image \a file.
456*/
457QString LocationPlan::exampleFile(const QString &file) const
458{
459 return Utilities::linkForExampleFile(file, m_rules.project, m_rules.fileExtension);
460}
461
462/*!
463 Returns the file name stem of the page that documents \a node.
464
465 A member resolves through its parent page. A deprecated member carries the
466 \c{-obsolete} suffix because it is documented on the obsolete sub-page, as
467 HtmlGenerator::fileBase() does. A stem already recorded on the node is
468 reused; otherwise the stem is computed from the rules and memoized here
469 without writing to the node.
470*/
472{
473 const bool obsolete = !node->isAggregate() && node->isDeprecated();
474 if (!node->isPageNode() && !node->isCollectionNode()) {
475 node = node->parent();
476 if (!node)
477 return {};
478 }
479
480 QString result;
481 if (node->hasFileNameBase()) {
482 result = node->fileNameBase();
483 } else if (const auto it = m_fileBases.constFind(node); it != m_fileBases.constEnd()) {
484 result = it.value();
485 } else {
486 result = Utilities::computeFileBase(
487 node, m_rules.project, [this](const Node *n) { return m_rules.outputPrefix(n); },
488 [this](const Node *n) { return m_rules.outputSuffix(n); });
489 m_fileBases.insert(node, result);
490 }
491 if (obsolete)
492 result += "-obsolete"_L1;
493 return result;
494}
495
496/*!
497 Returns the file name of the page that documents \a node: its explicit
498 URL if it has one, otherwise fileBase() with the format's extension.
499
500 A text page whose name carries an explicit extension other than HTML
501 keeps that extension, as Generator::fileName() does, so a page the author
502 named with a suffix is written and linked under that suffix. Returns an
503 empty string when the node has no file base.
504*/
506{
507 if (!node->url().isEmpty())
508 return node->url();
509 const QString base = fileBase(node);
510 if (base.isEmpty())
511 return {};
513 const QString suffix = QFileInfo(node->name()).suffix();
514 if (!suffix.isEmpty() && suffix != "html"_L1)
515 return base + u'.' + suffix;
516 }
517 return base + u'.' + m_rules.fileExtension;
518}
519
520void LocationPlan::record(const Node *node)
521{
522 if (const auto location = compute(node))
523 m_entities.insert(node, *location);
524}
525
526std::optional<PublicationLocation> LocationPlan::compute(const Node *node) const
527{
528 if (!node)
529 return std::nullopt;
530 if (!node->url().isEmpty())
531 return PublicationLocation{ node->url(), {}, true };
532 const QString href = resolveDocumentLocation(
533 node, [this](const Node *n) { return fileBase(n); }, m_rules.fileExtension);
534 if (href.isEmpty())
535 return std::nullopt;
536 const qsizetype hash = href.indexOf(u'#');
537 if (hash < 0)
538 return PublicationLocation{ href, {}, false };
539 return PublicationLocation{ href.left(hash), href.mid(hash + 1), false };
540}
541
542QT_END_NAMESPACE
The Config class contains the configuration variables for controlling how qdoc produces documentation...
Definition config.h:95
InclusionPolicy createInclusionPolicy() const
Definition config.cpp:1678
static bool isIncluded(const InclusionPolicy &policy, const NodeContext &context)
static LocationPlanSet build(const Config &config, const Tree *tree, const OutputConfiguration &output)
Builds plans for requested publications in output.
\variable PublicationLocation::path
QString fileName(const Node *node) const
Returns the file name of the page that documents node: its explicit URL if it has one,...
static LocationPlan build(LocationRules rules, const Tree *tree)
Plans the locations of every entity in tree under rules.
static LocationPlan buildPublished(LocationRules rules, const Tree *tree, const InclusionPolicy &policy)
Plans the entities whose pages are produced under policy.
QString fileBase(const Node *node) const
Returns the file name stem of the page that documents node.
This class constructs and maintains a tree of instances of the subclasses of Node.
Definition tree.h:59
#define CONFIG_OUTPUTSUFFIXES
Definition config.h:439
#define CONFIG_OUTPUTPREFIXES
Definition config.h:438
#define CONFIG_PROJECT
Definition config.h:444
NodeType
Definition genustypes.h:165
@ SharedComment
Definition genustypes.h:188
Metaness
Specifies the kind of function a FunctionNode represents.
Definition genustypes.h:242
@ QmlSignalHandler
Definition genustypes.h:256
Combined button and popup list for selecting options.
Holds the configuration a format needs to name its published files.
QString outputSuffix(const Node *node) const
Returns the file name suffix for node.
QString outputPrefix(const Node *node) const
Returns the file name prefix for node.
The Node class is the base class for all the nodes in QDoc's parse tree.
bool isExternalPage() const
Returns true if the node type is ExternalPage.
Definition node.h:100
virtual bool docMustBeGenerated() const
This function is called to perform a test to decide if the node must have documentation generated.
Definition node.h:198
bool isNamespace() const
Returns true if the node type is Namespace.
Definition node.h:110
bool hasFileNameBase() const
Returns true if the node's file name base has been set.
Definition node.h:169
bool isQmlType() const
Returns true if the node type is QmlType or QmlValueType.
Definition node.h:123
bool isHeader() const
Returns true if the node type is HeaderFile.
Definition node.h:106
NodeType nodeType() const override
Returns this node's type.
Definition node.h:82
Genus genus() const override
Returns this node's Genus.
Definition node.h:85
virtual bool isPageNode() const
Returns true if this node represents something that generates a documentation page.
Definition node.h:150
virtual bool isTextPageNode() const
Returns true if the node is a PageNode but not an Aggregate.
Definition node.h:155
virtual bool isAttached() const
Returns true if the QML property or QML method node is marked as attached.
Definition node.h:144
Aggregate * parent() const
Returns the node's parent pointer, that is, the aggregate under which the node is documented.
Definition node.h:211
virtual bool isDeprecated() const
Returns true if this node's status is Deprecated.
Definition node.h:136
virtual bool isAggregate() const
Returns true if this node is an aggregate, which means it inherits Aggregate and can therefore have c...
Definition node.h:138
bool isFunction(Genus g=Genus::DontCare) const
Returns true if this is a FunctionNode and its Genus is set to g.
Definition node.h:101
NodeContext createContext() const
Definition node.cpp:175
virtual bool isPropertyGroup() const
Returns true if the node is a SharedCommentNode for documenting multiple C++ properties or multiple Q...
Definition node.h:153
virtual bool isClassNode() const
Returns true if this is an instance of ClassNode.
Definition node.h:145
virtual bool isCollectionNode() const
Returns true if this is an instance of CollectionNode.
Definition node.h:146
bool isIndexNode() const
Returns true if this node was created from something in an index file.
Definition node.h:107
Adds publication destinations to a resolved output selection.
bool hasErrors() const
Returns true if a selection or layout diagnostic blocks publication.
OutputSelection::Mode Mode
\variable LocationRules::fileExtension