8#include <QtCore/qdir.h>
9#include <QtCore/qfile.h>
10#include <QtCore/qfileinfo.h>
11#include <QtCore/qjsonarray.h>
12#include <QtCore/qjsondocument.h>
13#include <QtCore/qjsonobject.h>
19using namespace Qt::StringLiterals;
22
23
24
25
26
27
28
29
30
33
34
35
36
37
38
39
42
43
44
45
46
47
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
76
77
78
79
82
83
84
85
86
87
90
91
92
93
94
95
98
99
100
101
102
105
106
107
108
109
112
113
114
117
118
119
120
121
122
123
124
125
126
129
130
131
134
135
136
137
138
139
142
143
144
147
148
149
150
151
152
155
156
157
160
161
162
163
164
165
166
169
170
171
174
175
176
179
180
181
182
183
184
187
188
189
190
191
192
195
196
197
200
201
202
208struct FormatDeclaration
210 bool removed =
false;
211 bool hasExtension =
false;
213 QMap<QString, QString> templates;
222 QMap<QString, FormatDeclaration> formats;
225void report(QVector<Diagnostic> &diagnostics,
const Location &location, QString message,
226 QString details = {})
228 diagnostics.push_back(
232int lineOfOffset(
const QByteArray &text,
int offset)
234 return 1 +
static_cast<
int>(text.left(offset).count(
'\n'));
237QString packageDirectory(
const QString &reference,
const QString &baseDir,
238 const QString &builtinRoot)
240 if (reference == TemplatePackage::builtinId())
241 return QDir::cleanPath(builtinRoot);
242 if (QDir::isAbsolutePath(reference))
243 return QDir::cleanPath(reference);
244 return QDir::cleanPath(QDir(baseDir).absoluteFilePath(reference));
247bool readVersion(
const QJsonObject &object,
const QString &key,
int supported,
248 const QString &subject,
const QString &verb,
const Location &location,
249 QVector<Diagnostic> &diagnostics)
251 const QJsonValue value = object.value(key);
252 if (value.isUndefined()) {
253 report(diagnostics, location,
"Manifest key '%1' is required."_L1.arg(key));
256 const int actual = value.isDouble() ? value.toInt(-1) : -1;
258 report(diagnostics, location,
259 "Manifest key '%1' must be a non-negative integer."_L1.arg(key));
262 if (actual != supported) {
263 report(diagnostics, location,
"%1 %2 is not supported."_L1.arg(subject).arg(actual),
264 "This QDoc %1 %2 %3."_L1.arg(verb, subject.toLower()).arg(supported));
270bool readFormat(
const QString &format,
const QJsonValue &value,
const QString &dir,
271 const Location &location, QMap<QString, FormatDeclaration> &formats,
272 QVector<Diagnostic> &diagnostics)
274 FormatDeclaration declaration;
275 declaration.location = location;
276 if (value.isBool() && !value.toBool()) {
277 declaration.removed =
true;
278 formats.insert(format, declaration);
281 if (!value.isObject()) {
282 report(diagnostics, location,
"Format '%1' must be an object or false."_L1.arg(format),
283 u"Use false to drop an inherited format."_s);
288 const QJsonObject entry = value.toObject();
289 for (
const QString &key : entry.keys()) {
290 if (key !=
"extension"_L1 && key !=
"templates"_L1) {
291 report(diagnostics, location,
292 "Unknown key '%1' for format '%2'."_L1.arg(key, format),
293 u"Supported keys: extension, templates."_s);
297 if (entry.contains(
"extension"_L1)) {
298 const QJsonValue extension = entry.value(
"extension"_L1);
299 const QString text = extension.isString() ? extension.toString() : QString();
300 if (text.isEmpty() || text.contains(u'/') || text.contains(u'\\')) {
301 report(diagnostics, location,
302 "Format '%1' declares an invalid or empty extension."_L1.arg(format),
303 u"Provide a non-empty file extension without path separators."_s);
306 declaration.hasExtension =
true;
307 declaration.extension = text;
310 if (entry.contains(
"templates"_L1)) {
311 const QJsonValue templates = entry.value(
"templates"_L1);
312 if (!templates.isObject()) {
313 report(diagnostics, location,
314 "Format '%1' must map template roles in a 'templates' object."_L1.arg(format));
317 const QStringList roles = TemplatePackage::roles();
318 const QJsonObject mapping = templates.toObject();
319 for (
auto role = mapping.constBegin(); role != mapping.constEnd(); ++role) {
320 if (!roles.contains(role.key())) {
321 report(diagnostics, location,
322 "Unknown template role '%1' for format '%2'."_L1.arg(role.key(), format),
323 "Supported roles: %1."_L1.arg(roles.join(u", "_s)));
327 const QString path = role.value().isString() ? role.value().toString() : QString();
328 if (path.isEmpty()) {
329 report(diagnostics, location,
330 "Format '%1' declares an empty template for role '%2'."_L1
331 .arg(format, role.key()));
335 declaration.templates.insert(role.key(),
336 QDir::cleanPath(QDir(dir).absoluteFilePath(path)));
341 formats.insert(format, declaration);
345bool readManifest(
const QString &dir,
const Location &from,
const QSet<QString> &capabilities,
346 Manifest &manifest, QVector<Diagnostic> &diagnostics)
348 const QString manifestPath = dir + u'/' + TemplatePackage::manifestFileName();
349 if (!QFileInfo(manifestPath).isFile()) {
350 report(diagnostics, from,
351 "Template package '%1' has no %2 manifest."_L1.arg(dir, TemplatePackage::manifestFileName()),
352 u"A package is a directory containing a manifest."_s);
355 QFile file(manifestPath);
356 if (!file.open(QIODevice::ReadOnly)) {
357 report(diagnostics, from,
358 "Cannot read template package manifest '%1': %2"_L1.arg(manifestPath,
359 file.errorString()));
362 const QByteArray raw = file.readAll();
363 const Location location(manifestPath);
364 QJsonParseError parseError;
365 const QJsonDocument document = QJsonDocument::fromJson(raw, &parseError);
366 if (parseError.error != QJsonParseError::NoError) {
368 at
.setLineNo(lineOfOffset(raw, parseError.offset));
369 report(diagnostics, at,
370 "Cannot parse template package manifest: %1"_L1.arg(parseError.errorString()));
373 if (!document.isObject()) {
374 report(diagnostics, location,
375 u"Template package manifest must contain a JSON object."_s);
380 manifest.location = location;
381 const QJsonObject object = document.object();
383 static const QSet<QString> knownKeys{ u"schemaVersion"_s, u"templateApiVersion"_s,
384 u"extends"_s, u"requires"_s, u"formats"_s };
385 for (
const QString &key : object.keys()) {
386 if (!knownKeys.contains(key)) {
387 report(diagnostics, location,
"Unknown manifest key '%1'."_L1.arg(key),
388 u"Supported keys: schemaVersion, templateApiVersion, extends, requires, formats."_s);
392 if (!readVersion(object, u"schemaVersion"_s, TemplatePackage::supportedSchemaVersion,
393 u"Manifest schema version"_s, u"supports"_s, location, diagnostics))
395 if (!readVersion(object, u"templateApiVersion"_s, TemplatePackage::providedTemplateApiVersion,
396 u"Template API version"_s, u"provides"_s, location, diagnostics))
399 if (object.contains(
"extends"_L1)) {
400 const QJsonValue value = object.value(
"extends"_L1);
401 if (!value.isString() || value.toString().isEmpty()) {
402 report(diagnostics, location, u"Manifest key 'extends' must name a parent package."_s,
403 "Use a directory relative to this manifest or '%1'."_L1
404 .arg(TemplatePackage::builtinId()));
407 manifest.extends = value.toString();
410 if (object.contains(
"requires"_L1)) {
411 const QJsonValue value = object.value(
"requires"_L1);
412 if (!value.isArray()) {
413 report(diagnostics, location,
414 u"Manifest key 'requires' must be an array of capability names."_s);
417 const QJsonArray entries = value.toArray();
418 for (
const QJsonValue &entry : entries) {
419 const QString name = entry.isString() ? entry.toString() : QString();
420 if (name.isEmpty()) {
421 report(diagnostics, location,
422 u"Manifest key 'requires' must list non-empty capability names."_s);
424 }
else if (!capabilities.contains(name)) {
425 report(diagnostics, location,
426 "Template package requires capability '%1', which this QDoc does not provide."_L1
433 if (object.contains(
"formats"_L1)) {
434 const QJsonValue value = object.value(
"formats"_L1);
435 if (!value.isObject()) {
436 report(diagnostics, location,
437 u"Manifest key 'formats' must be an object keyed by format identity."_s);
440 const QJsonObject formats = value.toObject();
441 for (
auto it = formats.constBegin(); it != formats.constEnd(); ++it) {
442 if (!OutputSelection::isValidFormatIdentity(it.key())) {
443 report(diagnostics, location,
"Invalid format identity '%1'."_L1.arg(it.key()),
444 u"Format names may contain letters, digits, and underscores only."_s);
448 if (!readFormat(it.key(), it.value(), dir, location, manifest.formats, diagnostics))
459
460
463 return u"qdoc:default"_s;
467
468
471 return u"qdoc-template.json"_s;
475
476
477
478
479
480
483 return { u"page"_s, u"collection"_s, u"cppref"_s, u"qmltype"_s, u"members"_s,
488
489
490
491
492
493
494
495
499 QString reference = builtinId();
500 QString baseDir = config.currentDir();
504 const QStringList values = var.asStringList();
505 if (values.size() > 1) {
506 report(result.diagnostics, result.location,
508 "Name one package directory or '%1'."_L1.arg(builtinId()));
511 reference = values.value(0);
512 if (reference.isEmpty()) {
513 report(result.diagnostics, result.location,
515 "Name a package directory relative to the declaring configuration file, or '%1'."_L1
519 if (!var.path().isEmpty())
520 baseDir = var.path();
523 QVector<Manifest> chain;
527 if (reference.startsWith(
"qdoc:"_L1) && reference != builtinId()) {
528 report(result.diagnostics, from,
529 "Unknown built-in template package '%1'."_L1.arg(reference),
530 "QDoc provides '%1'."_L1.arg(builtinId()));
533 const QString dir = packageDirectory(reference, baseDir, input.builtinRoot);
535 result.id = reference == builtinId() ? builtinId() : dir;
536 if (visited.contains(dir)) {
538 report(result.diagnostics, from,
539 "Template package inheritance cycle: %1."_L1.arg(visited.join(u" extends "_s)),
540 u"A package may extend one parent and may not extend itself, directly or indirectly."_s);
545 if (!readManifest(dir, from, input.engineCapabilities, manifest, result.diagnostics))
547 chain.append(manifest);
548 if (manifest.extends.isEmpty())
550 reference = manifest.extends;
551 baseDir = manifest.dir;
552 from = manifest.location;
555 for (
const Manifest &manifest : std::as_const(chain))
556 result.searchPaths.append(manifest.dir);
558 QMap<QString, Format> effective;
559 for (
auto manifest = chain.crbegin(); manifest != chain.crend(); ++manifest) {
560 for (
auto it = manifest->formats.constBegin(); it != manifest->formats.constEnd(); ++it) {
561 const FormatDeclaration &declaration = it.value();
562 if (declaration.removed) {
563 effective.remove(it.key());
566 Format &format = effective[it.key()];
567 format.format = it.key();
568 format.location = declaration.location;
569 if (declaration.hasExtension) {
570 format.extension = declaration.extension;
571 format.extensionLocation = declaration.location;
573 for (
auto role = declaration.templates.constBegin();
574 role != declaration.templates.constEnd(); ++role) {
575 format.templates.insert(role.key(),
Template{ role.value(), declaration.location });
580 const QStringList allRoles = roles();
581 for (Format &format : effective) {
582 for (
const QString &role : allRoles) {
583 if (!format.templates.contains(role)) {
584 report(result.diagnostics, format.location,
585 "Format '%1' declares no template for role '%2'."_L1.arg(format.format, role),
586 u"Declare every role or inherit it from a parent package."_s);
587 format.broken =
true;
590 for (
auto it = format.templates.constBegin(); it != format.templates.constEnd(); ++it) {
591 if (!QFileInfo(it.value().path).isFile()) {
592 report(result.diagnostics, it.value().location,
593 "Template '%1' for role '%2' of format '%3' does not exist."_L1
594 .arg(it.value().path, it.key(), format.format),
595 u"QDoc does not fall back to an inherited template for a failed override."_s);
596 format.broken =
true;
599 result.formats.append(format);
605
606
609 for (
const auto &diagnostic : diagnostics) {
610 if (diagnostic.kind == Diagnostic::Kind::Error)
617
618
619
620
621
622
625 for (
const auto &entry : formats) {
626 if (entry.format == format)
633
634
635
636
637
638
644 for (
const Format &format : formats) {
646 result.brokenFormats.insert(format.format);
648 result.supportedFormats.insert(format.format);
649 if (!format.extension.isEmpty())
650 result.formatExtensions.insert(format.format, format.extension);
contains all the information for a single config variable in a .qdocconf file.
const Location & location() const
The Config class contains the configuration variables for controlling how qdoc produces documentation...
The Location class provides a way to mark a location in a file.
#define CONFIG_TEMPLATEPACKAGE
Combined button and popup list for selecting options.
Kind
Distinguishes blocking errors from migration advice.
Describes a pipeline without constructing generators or loading packages.
\typealias TemplatePackage::Diagnostic
\variable TemplatePackageInput::engineCapabilities
OutputSelection::Diagnostic Diagnostic
bool hasErrors() const
Returns true if any diagnostic blocks use of the package.
PipelineCapabilities capabilities() const
Returns the template pipeline's capabilities as OutputSelection expects them.
static TemplatePackage resolve(const Config &config, const TemplatePackageInput &input={})
Resolves the package that config selects through {template.package}, or the built-in package when the...
const Format * findFormat(const QString &format) const
Returns the effective entry for the case-sensitive identity format, or {nullptr} if the package does ...