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
doc.cpp
Go to the documentation of this file.
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 "doc.h"
5
6#include "atom.h"
7#include "config.h"
8#include "codemarker.h"
9#include "docparser.h"
10#include "docprivate.h"
11#include "generator.h"
12#include "qmltypenode.h"
13#include "quoter.h"
14#include "text.h"
15
16#include <qcryptographichash.h>
17
19
20using namespace Qt::StringLiterals;
21
22DocUtilities &Doc::m_utilities = DocUtilities::instance();
23
24/*!
25 \typedef ArgList
26 \relates Doc
27
28 A list of metacommand arguments that appear in a Doc. Each entry
29 in the list is a <QString, QString> pair (ArgPair):
30
31 \list
32 \li \c {ArgPair.first} - arguments passed to the command.
33 \li \c {ArgPair.second} - optional argument string passed
34 within brackets immediately following the command.
35 \endlist
36*/
37
38/*!
39 Parse the qdoc comment \a source. Build up a list of all the topic
40 commands found including their arguments. This constructor is used
41 when there can be more than one topic command in theqdoc comment.
42 Normally, there is only one topic command in a qdoc comment, but in
43 QML documentation, there is the case where the qdoc \e{qmlproperty}
44 command can appear multiple times in a qdoc comment.
45 */
46Doc::Doc(const Location &start_loc, const Location &end_loc, const QString &source,
47 const QSet<QString> &metaCommandSet, const QSet<QString> &topics)
48{
49 m_priv = new DocPrivate(start_loc, end_loc, source);
50 DocParser parser;
51 parser.parse(source, m_priv, metaCommandSet, topics);
52
53 if (Config::instance().getAtomsDump()) {
54 start_loc.information(u"==== Atoms Structure for block comment starting at %1 ===="_s.arg(
55 start_loc.toString()));
57 end_loc.information(
58 u"==== Ending atoms Structure for block comment ending at %1 ===="_s.arg(
59 end_loc.toString()));
60 }
61}
62
63Doc::Doc(const Doc &doc) : m_priv(nullptr)
64{
65 operator=(doc);
66}
67
69{
70 if (m_priv && m_priv->deref())
71 delete m_priv;
72}
73
74Doc &Doc::operator=(const Doc &doc)
75{
76 if (&doc == this)
77 return *this;
78 if (doc.m_priv)
79 doc.m_priv->ref();
80 if (m_priv && m_priv->deref())
81 delete m_priv;
82 m_priv = doc.m_priv;
83 return *this;
84}
85
86/*!
87 Returns the starting location of a qdoc comment.
88 */
89const Location &Doc::location() const
90{
91 static const Location dummy;
92 return m_priv == nullptr ? dummy : m_priv->m_start_loc;
93}
94
95/*!
96 Returns the starting location of a qdoc comment.
97 */
98const Location &Doc::startLocation() const
99{
100 return location();
101}
102
103const QString &Doc::source() const
104{
105 static QString null;
106 return m_priv == nullptr ? null : m_priv->m_src;
107}
108
109bool Doc::isEmpty() const
110{
111 return m_priv == nullptr || m_priv->m_src.isEmpty();
112}
113
114const Text &Doc::body() const
115{
116 static const Text dummy;
117 return m_priv == nullptr ? dummy : m_priv->m_text;
118}
119
120const Text &Doc::title() const
121{
122 static const Text dummy;
123 return m_priv == nullptr ? dummy : m_priv->m_title;
124}
125
126Text Doc::briefText(bool inclusive) const
127{
129}
130
135
136Text Doc::trimmedBriefText(const QString &className) const
137{
138 QString classNameOnly = className;
139 if (className.contains("::"))
140 classNameOnly = className.split("::").last();
141
142 Text originalText = briefText();
143 Text resultText;
144 const Atom *atom = originalText.firstAtom();
145 if (atom) {
146 QString briefStr;
147 QString whats;
148 /*
149 This code is really ugly. The entire \brief business
150 should be rethought.
151 */
152 while (atom) {
153 if (atom->type() == Atom::AutoLink || atom->type() == Atom::String) {
154 briefStr += atom->string();
155 } else if (atom->type() == Atom::C) {
156 briefStr += Generator::plainCode(atom->string());
157 }
158 atom = atom->next();
159 }
160
161 QStringList w = briefStr.split(QLatin1Char(' '));
162 const qsizetype originalWordCount = w.size();
163 if (!w.isEmpty() && w.first() == "Returns") {
164 } else {
165 if (!w.isEmpty() && w.first() == "The")
166 w.removeFirst();
167
168 if (!w.isEmpty() && (w.first() == className || w.first() == classNameOnly))
169 w.removeFirst();
170
171 if (!w.isEmpty()
172 && ((w.first() == "class") || (w.first() == "function") || (w.first() == "macro")
173 || (w.first() == "widget") || (w.first() == "namespace")
174 || (w.first() == "header")))
175 w.removeFirst();
176
177 if (!w.isEmpty() && (w.first() == "is" || w.first() == "provides"))
178 w.removeFirst();
179
180 if (!w.isEmpty() && (w.first() == "a" || w.first() == "an"))
181 w.removeFirst();
182 }
183
184 whats = w.join(' ');
185
186 if (whats.endsWith(QLatin1Char('.')))
187 whats.truncate(whats.size() - 1);
188
189 // If filler words are stripped, capitalize the sentence start.
190 if (!whats.isEmpty() && w.size() != originalWordCount)
191 whats[0] = whats[0].toUpper();
192
193 // ### move this once \brief is abolished for properties
194 resultText << whats;
195 }
196 return resultText;
197}
198
200{
201 if (m_priv == nullptr || !m_priv->m_hasLegalese)
202 return Text();
203 else
205}
206
208{
209 return m_priv == nullptr ? QSet<QString>() : m_priv->m_params;
210}
211
213{
214 return m_priv == nullptr ? QStringList() : m_priv->m_enumItemList;
215}
216
218{
219 return m_priv == nullptr ? QStringList() : m_priv->m_omitEnumItemList;
220}
221
223{
224 return m_priv == nullptr ? QSet<QString>() : m_priv->m_metacommandsUsed;
225}
226
227/*!
228 Returns true if the set of metacommands used in the doc
229 comment contains \e {internal}.
230 */
231bool Doc::isInternal() const
232{
233 return metaCommandsUsed().contains(QLatin1String("internal"));
234}
235
236/*!
237 Returns true if the set of metacommands used in the doc
238 comment contains \e {reimp}.
239 */
240bool Doc::isMarkedReimp() const
241{
242 return metaCommandsUsed().contains(QLatin1String("reimp"));
243}
244
245/*!
246 Returns true if this documentation was auto-generated by QDoc
247 rather than written by an author.
248 */
249bool Doc::isAutoGenerated() const
250{
251 return m_priv != nullptr && m_priv->m_autoGenerated;
252}
253
254/*!
255 Marks this documentation as auto-generated by QDoc.
256 */
258{
259 if (m_priv == nullptr)
260 m_priv = new DocPrivate;
261 m_priv->m_autoGenerated = true;
262}
263
264/*!
265 Returns the list of arguments passed to the \c{\overload} command.
266 */
268{
269 return metaCommandArgs(u"overload"_s);
270}
271
272/*!
273 Returns a reference to the list of topic commands used in the
274 current qdoc comment. Normally there is only one, but there
275 can be multiple \e{qmlproperty} commands, for example.
276 */
278{
279 return m_priv == nullptr ? TopicList() : m_priv->m_topics;
280}
281
282ArgList Doc::metaCommandArgs(const QString &metacommand) const
283{
284 return m_priv == nullptr ? ArgList() : m_priv->m_metaCommandMap.value(metacommand);
285}
286
288{
289 return m_priv == nullptr ? QList<Text>() : m_priv->m_alsoList;
290}
291
293{
294 return m_priv && m_priv->extra && !m_priv->extra->m_tableOfContents.isEmpty();
295}
296
297bool Doc::hasKeywords() const
298{
299 return m_priv && m_priv->extra && !m_priv->extra->m_keywords.isEmpty();
300}
301
302bool Doc::hasTargets() const
303{
304 return m_priv && m_priv->extra && !m_priv->extra->m_targets.isEmpty();
305}
306
307const QList<Atom *> &Doc::tableOfContents() const
308{
309 if (m_priv == nullptr) {
310 static const QList<Atom *> empty;
311 return empty;
312 }
313 m_priv->constructExtra();
314 return m_priv->extra->m_tableOfContents;
315}
316
317const QList<int> &Doc::tableOfContentsLevels() const
318{
319 if (m_priv == nullptr) {
320 static const QList<int> empty;
321 return empty;
322 }
323 m_priv->constructExtra();
324 return m_priv->extra->m_tableOfContentsLevels;
325}
326
327const QList<Atom *> &Doc::keywords() const
328{
329 if (m_priv == nullptr) {
330 static const QList<Atom *> empty;
331 return empty;
332 }
333 m_priv->constructExtra();
334 return m_priv->extra->m_keywords;
335}
336
337const QList<Atom *> &Doc::targets() const
338{
339 if (m_priv == nullptr) {
340 static const QList<Atom *> empty;
341 return empty;
342 }
343 m_priv->constructExtra();
344 return m_priv->extra->m_targets;
345}
346
348{
349 return m_priv && m_priv->extra ? &m_priv->extra->m_metaMap : nullptr;
350}
351
353{
354 return m_priv && m_priv->extra ? &m_priv->extra->m_comparesWithMap : nullptr;
355}
356
357void Doc::constructExtra() const
358{
359 if (m_priv)
360 m_priv->constructExtra();
361}
362
363void Doc::initialize(FileResolver& file_resolver)
364{
365 Config &config = Config::instance();
366 DocParser::initialize(config, file_resolver);
367
368 const auto &configMacros = config.subVars(CONFIG_MACRO);
369 for (const auto &macroName : configMacros) {
370 QString macroDotName = CONFIG_MACRO + Config::dot + macroName;
371 Macro macro;
372 macro.numParams = -1;
373 const auto &macroConfigVar = config.get(macroDotName);
374 macro.m_defaultDef = macroConfigVar.asString();
375 if (!macro.m_defaultDef.isEmpty()) {
376 macro.m_defaultDefLocation = macroConfigVar.location();
377 macro.numParams = Config::numParams(macro.m_defaultDef);
378 }
379 bool silent = false;
380
381 const auto &macroDotNames = config.subVars(macroDotName);
382 for (const auto &f : macroDotNames) {
383 const auto &macroSubVar = config.get(macroDotName + Config::dot + f);
384 QString def{macroSubVar.asString()};
385 if (!def.isEmpty()) {
386 macro.m_otherDefs.insert(f, def);
387 int m = Config::numParams(def);
388 if (macro.numParams == -1)
389 macro.numParams = m;
390 // .match definition is a regular expression that contains no params
391 else if (macro.numParams != m && f != QLatin1String("match")) {
392 if (!silent) {
393 QString other = QStringLiteral("default");
394 if (macro.m_defaultDef.isEmpty())
395 other = macro.m_otherDefs.constBegin().key();
396 macroSubVar.location().warning(
397 QStringLiteral("Macro '\\%1' takes inconsistent number of "
398 "arguments (%2 %3, %4 %5)")
399 .arg(macroName, f, QString::number(m), other,
400 QString::number(macro.numParams)));
401 silent = true;
402 }
403 if (macro.numParams < m)
404 macro.numParams = m;
405 }
406 }
407 }
408 if (macro.numParams != -1)
409 m_utilities.macroHash.insert(macroName, macro);
410 }
411}
412
413/*!
414 All the heap allocated variables are deleted.
415 */
417{
418 m_utilities.cmdHash.clear();
419 m_utilities.macroHash.clear();
420}
421
422/*!
423 Replaces any asterisks used as a left margin in the comment \a str with
424 spaces then trims the comment syntax from the start and end of the string,
425 leaving only the text content. Updates the \a location to refer to the
426 location of the content in the original file.
427 */
428void Doc::trimCStyleComment(Location &location, QString &str)
429{
430 QString cleaned;
431 Location m = location;
432 bool metMargin = true;
433 int marginColumn = location.columnNo() + 1;
434 int i;
435
436 for (i = 0; i < str.size(); ++i) {
437 if (m.columnNo() == marginColumn) {
438 // Stop cleaning if the expected asterisk was missing.
439 if (str[i] != '*')
440 break;
441 cleaned += ' ';
442 metMargin = true;
443 } else {
444 if (str[i] == '\n') {
445 // Break if the line ends before any asterisks are found.
446 if (!metMargin)
447 break;
448 metMargin = false;
449 }
450 cleaned += str[i];
451 }
452 m.advance(str[i]);
453 }
454 // Only replace the string if a fully cleaned version was created
455 // to avoid producing incomplete or corrupted strings.
456 if (cleaned.size() == str.size())
457 str = std::move(cleaned);
458
459 // Update the location to refer to the start of the comment text.
460 for (int i = 0; i < 3; ++i)
461 location.advance(str[i]);
462
463 // Remove the comment syntax from the start (leading comment marker
464 // and newline) and end (comment marker).
465 str = str.mid(3, str.size() - 5);
466}
467
468void Doc::quoteFromFile(const Location &location, Quoter &quoter, ResolvedFile resolved_file, CodeMarker *marker)
469{
470 // TODO: quoteFromFile should not care about modifying a stateful
471 // quoter from the outside, instead, it should produce a quoter
472 // that allows the caller to retrieve the required information
473 // about the quoted file.
474 //
475 // When changing the way in which quoting works, this kind of
476 // spread resposability should be removed, together with quoteFromFile.
477 quoter.reset();
478
479 QString code;
480 {
481 QFile input_file{resolved_file.get_path()};
482 if (!input_file.open(QFile::ReadOnly))
483 return;
484 code = DocParser::untabifyEtc(QTextStream{&input_file}.readAll());
485 }
486
487 if (!marker)
488 marker = CodeMarker::markerForFileName(resolved_file.get_path());
489 quoter.quoteFromFile(resolved_file.get_path(), code, marker->markedUpCode(code, nullptr, location));
490}
491
492QT_END_NAMESPACE
The Atom class is the fundamental unit for representing documents internally.
Definition atom.h:19
AtomType type() const
Return the type of this atom.
Definition atom.h:146
@ BriefRight
Definition atom.h:27
@ LegaleseRight
Definition atom.h:63
@ DeprecatedLeft
Definition atom.h:37
@ String
Definition atom.h:97
@ BriefLeft
Definition atom.h:26
@ LegaleseLeft
Definition atom.h:62
@ C
Definition atom.h:28
@ AutoLink
Definition atom.h:23
@ DeprecatedRight
Definition atom.h:38
const Atom * next() const
Return the next atom in the atom list.
Definition atom.h:143
The Config class contains the configuration variables for controlling how qdoc produces documentation...
Definition config.h:95
static void initialize(const Config &config, FileResolver &file_resolver)
void ref()
Definition docprivate.h:52
bool m_autoGenerated
Definition docprivate.h:72
Text m_title
Definition docprivate.h:61
DocPrivateExtra * extra
Definition docprivate.h:68
void constructExtra()
Text m_text
Definition docprivate.h:60
bool m_hasLegalese
Definition docprivate.h:71
bool deref()
Definition docprivate.h:53
TopicList m_topics
Definition docprivate.h:69
CommandMap m_metaCommandMap
Definition docprivate.h:67
Definition doc.h:32
QSet< QString > parameterNames() const
Definition doc.cpp:207
Text legaleseText() const
Definition doc.cpp:199
QList< Text > alsoList() const
Definition doc.cpp:287
const Location & location() const
Returns the starting location of a qdoc comment.
Definition doc.cpp:89
Doc & operator=(const Doc &doc)
Definition doc.cpp:74
Doc(const Location &start_loc, const Location &end_loc, const QString &source, const QSet< QString > &metaCommandSet, const QSet< QString > &topics)
Parse the qdoc comment source.
Definition doc.cpp:46
const QList< Atom * > & tableOfContents() const
Definition doc.cpp:307
const Text & title() const
Definition doc.cpp:120
bool isInternal() const
Returns true if the set of metacommands used in the doc comment contains {internal}...
Definition doc.cpp:231
bool hasTableOfContents() const
Definition doc.cpp:292
static void quoteFromFile(const Location &location, Quoter &quoter, ResolvedFile resolved_file, CodeMarker *marker=nullptr)
Definition doc.cpp:468
const QList< Atom * > & keywords() const
Definition doc.cpp:327
~Doc()
Definition doc.cpp:68
const Text & body() const
Definition doc.cpp:114
static void initialize(FileResolver &file_resolver)
Definition doc.cpp:363
bool hasKeywords() const
Definition doc.cpp:297
QStringList omitEnumItemNames() const
Definition doc.cpp:217
const QList< Atom * > & targets() const
Definition doc.cpp:337
QMultiMap< ComparisonCategory, Text > * comparesWithMap() const
Definition doc.cpp:352
const QList< int > & tableOfContentsLevels() const
Definition doc.cpp:317
bool hasTargets() const
Definition doc.cpp:302
Text trimmedBriefText(const QString &className) const
Definition doc.cpp:136
ArgList metaCommandArgs(const QString &metaCommand) const
Definition doc.cpp:282
Text briefText(bool inclusive=false) const
Definition doc.cpp:126
const Location & startLocation() const
Returns the starting location of a qdoc comment.
Definition doc.cpp:98
Text deprecationText() const
Definition doc.cpp:131
bool isMarkedReimp() const
Returns true if the set of metacommands used in the doc comment contains {reimp}.
Definition doc.cpp:240
void markAutoGenerated()
Marks this documentation as auto-generated by QDoc.
Definition doc.cpp:257
bool isAutoGenerated() const
Returns true if this documentation was auto-generated by QDoc rather than written by an author.
Definition doc.cpp:249
static void terminate()
All the heap allocated variables are deleted.
Definition doc.cpp:416
QList< ArgPair > overloadList() const
Returns the list of arguments passed to the {\overload} command.
Definition doc.cpp:267
TopicList topicsUsed() const
Returns a reference to the list of topic commands used in the current qdoc comment.
Definition doc.cpp:277
QStringMultiMap * metaTagMap() const
Definition doc.cpp:347
QSet< QString > metaCommandsUsed() const
Definition doc.cpp:222
bool isEmpty() const
Definition doc.cpp:109
Doc(const Doc &doc)
Definition doc.cpp:63
void constructExtra() const
Definition doc.cpp:357
const QString & source() const
Definition doc.cpp:103
QStringList enumItemNames() const
Definition doc.cpp:212
Encapsulate the logic that QDoc uses to find files whose path is provided by the user and that are re...
The Location class provides a way to mark a location in a file.
Definition location.h:20
int columnNo() const
Returns the current column number.
Definition location.h:51
Definition text.h:12
void dump() const
Prints a human-readable version of the contained atoms to stderr.
Definition text.cpp:206
Text subText(Atom::AtomType left, Atom::AtomType right, const Atom *from=nullptr, bool inclusive=false) const
Definition text.cpp:134
Atom * firstAtom()
Definition text.h:21
Text()
Definition text.cpp:12
#define CONFIG_MACRO
Definition config.h:430
QList< ArgPair > ArgList
Definition doc.h:28
QMultiMap< QString, QString > QStringMultiMap
Definition doc.h:29
Combined button and popup list for selecting options.
QStringMultiMap m_metaMap
Definition docprivate.h:38
QHash_QString_Macro macroHash
QHash_QString_int cmdHash
Represents a file that is reachable by QDoc based on its current configuration.
QList< Topic > TopicList
Definition topic.h:25