source: trunk/tools/qdoc3/codemarker.cpp@ 356

Last change on this file since 356 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 16.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QMetaObject>
43#include <QDebug>
44#include "codemarker.h"
45#include "config.h"
46#include "node.h"
47
48#include <stdio.h>
49
50QT_BEGIN_NAMESPACE
51
52QString CodeMarker::defaultLang;
53QList<CodeMarker *> CodeMarker::markers;
54
55/*!
56 When a code marker constructs itself, it puts itself into
57 the static list of code markers. All the code markers in
58 the static list get initialized in initialize(), which is
59 not called until after the qdoc configuration file has
60 been read.
61 */
62CodeMarker::CodeMarker()
63 : slow(false)
64{
65 markers.prepend(this);
66}
67
68/*!
69 When a code marker destroys itself, it removes itself from
70 the static list of code markers.
71 */
72CodeMarker::~CodeMarker()
73{
74 markers.removeAll(this);
75}
76
77/*!
78 The only thing a code market initializes is its \e{slow}
79 flag. The \e{slow} flag indicates whether the operations
80 that slow down qdoc are to be performed or not. It is
81 turned off by default.
82 */
83void CodeMarker::initializeMarker(const Config &config)
84{
85 slow = config.getBool(QLatin1String(CONFIG_SLOW));
86}
87
88/*!
89 Terminating a code marker is trivial.
90 */
91void CodeMarker::terminateMarker()
92{
93 // nothing.
94}
95
96/*!
97 All the code markers in the static list are initialized
98 here, after the qdoc configuration file has been loaded.
99 */
100void CodeMarker::initialize(const Config& config)
101{
102 defaultLang = config.getString(QLatin1String(CONFIG_LANGUAGE));
103 QList<CodeMarker *>::ConstIterator m = markers.begin();
104 while (m != markers.end()) {
105 (*m)->initializeMarker(config);
106 ++m;
107 }
108}
109
110/*!
111 All the code markers in the static list are terminated here.
112 */
113void CodeMarker::terminate()
114{
115 QList<CodeMarker *>::ConstIterator m = markers.begin();
116 while (m != markers.end()) {
117 (*m)->terminateMarker();
118 ++m;
119 }
120}
121
122CodeMarker *CodeMarker::markerForCode(const QString& code)
123{
124 CodeMarker *defaultMarker = markerForLanguage(defaultLang);
125 if (defaultMarker != 0 && defaultMarker->recognizeCode(code))
126 return defaultMarker;
127
128 QList<CodeMarker *>::ConstIterator m = markers.begin();
129 while (m != markers.end()) {
130 if ((*m)->recognizeCode(code))
131 return *m;
132 ++m;
133 }
134 return defaultMarker;
135}
136
137CodeMarker *CodeMarker::markerForFileName(const QString& fileName)
138{
139 CodeMarker *defaultMarker = markerForLanguage(defaultLang);
140 int dot = -1;
141 while ((dot = fileName.lastIndexOf(QLatin1Char('.'), dot)) != -1) {
142 QString ext = fileName.mid(dot + 1);
143 if (defaultMarker != 0 && defaultMarker->recognizeExtension(ext))
144 return defaultMarker;
145 QList<CodeMarker *>::ConstIterator m = markers.begin();
146 while (m != markers.end()) {
147 if ((*m)->recognizeExtension(ext))
148 return *m;
149 ++m;
150 }
151 --dot;
152 }
153 return defaultMarker;
154}
155
156CodeMarker *CodeMarker::markerForLanguage(const QString& lang)
157{
158 QList<CodeMarker *>::ConstIterator m = markers.begin();
159 while (m != markers.end()) {
160 if ((*m)->recognizeLanguage(lang))
161 return *m;
162 ++m;
163 }
164 return 0;
165}
166
167const Node *CodeMarker::nodeForString(const QString& string)
168{
169 if (sizeof(const Node *) == sizeof(uint)) {
170 return reinterpret_cast<const Node *>(string.toUInt());
171 } else {
172 return reinterpret_cast<const Node *>(string.toULongLong());
173 }
174}
175
176QString CodeMarker::stringForNode(const Node *node)
177{
178 if (sizeof(const Node *) == sizeof(ulong)) {
179 return QString::number(reinterpret_cast<ulong>(node));
180 } else {
181 return QString::number(reinterpret_cast<qulonglong>(node));
182 }
183}
184
185static const QString samp = QLatin1String("&amp;");
186static const QString slt = QLatin1String("&lt;");
187static const QString sgt = QLatin1String("&gt;");
188static const QString squot = QLatin1String("&quot;");
189
190QString CodeMarker::protect(const QString& str)
191{
192 int n = str.length();
193 QString marked;
194 marked.reserve(n * 2 + 30);
195 const QChar *data = str.constData();
196 for (int i = 0; i != n; ++i) {
197 switch (data[i].unicode()) {
198 case '&': marked += samp; break;
199 case '<': marked += slt; break;
200 case '>': marked += sgt; break;
201 case '"': marked += squot; break;
202 default : marked += data[i];
203 }
204 }
205 return marked;
206}
207
208QString CodeMarker::typified(const QString &string)
209{
210 QString result;
211 QString pendingWord;
212
213 for (int i = 0; i <= string.size(); ++i) {
214 QChar ch;
215 if (i != string.size())
216 ch = string.at(i);
217
218 QChar lower = ch.toLower();
219 if ((lower >= QLatin1Char('a') && lower <= QLatin1Char('z'))
220 || ch.digitValue() >= 0 || ch == QLatin1Char('_')
221 || ch == QLatin1Char(':')) {
222 pendingWord += ch;
223 } else {
224 if (!pendingWord.isEmpty()) {
225 bool isProbablyType = (pendingWord != QLatin1String("const"));
226 if (isProbablyType)
227 result += QLatin1String("<@type>");
228 result += pendingWord;
229 if (isProbablyType)
230 result += QLatin1String("</@type>");
231 }
232 pendingWord.clear();
233
234 switch (ch.unicode()) {
235 case '\0':
236 break;
237 case '&':
238 result += QLatin1String("&amp;");
239 break;
240 case '<':
241 result += QLatin1String("&lt;");
242 break;
243 case '>':
244 result += QLatin1String("&gt;");
245 break;
246 default:
247 result += ch;
248 }
249 }
250 }
251 return result;
252}
253
254QString CodeMarker::taggedNode(const Node *node)
255{
256 QString tag;
257
258 switch (node->type()) {
259 case Node::Namespace:
260 tag = QLatin1String("@namespace");
261 break;
262 case Node::Class:
263 tag = QLatin1String("@class");
264 break;
265 case Node::Enum:
266 tag = QLatin1String("@enum");
267 break;
268 case Node::Typedef:
269 tag = QLatin1String("@typedef");
270 break;
271 case Node::Function:
272 tag = QLatin1String("@function");
273 break;
274 case Node::Property:
275 tag = QLatin1String("@property");
276 break;
277 default:
278 tag = QLatin1String("@unknown");
279 }
280 return QLatin1Char('<') + tag + QLatin1Char('>') + protect(node->name())
281 + QLatin1String("</") + tag + QLatin1Char('>');
282}
283
284QString CodeMarker::linkTag(const Node *node, const QString& body)
285{
286 return QLatin1String("<@link node=\"") + stringForNode(node)
287 + QLatin1String("\">") + body + QLatin1String("</@link>");
288}
289
290QString CodeMarker::sortName(const Node *node)
291{
292 QString nodeName = node->name();
293 int numDigits = 0;
294 for (int i = nodeName.size() - 1; i > 0; --i) {
295 if (nodeName.at(i).digitValue() == -1)
296 break;
297 ++numDigits;
298 }
299
300 // we want 'qint8' to appear before 'qint16'
301 if (numDigits > 0) {
302 for (int i = 0; i < 4 - numDigits; ++i)
303 nodeName.insert(nodeName.size()-numDigits-1, QLatin1String("0"));
304 }
305
306 if (node->type() == Node::Function) {
307 const FunctionNode *func = static_cast<const FunctionNode *>(node);
308 QString sortNo;
309 if (func->metaness() == FunctionNode::Ctor) {
310 sortNo = QLatin1String("C");
311 } else if (func->metaness() == FunctionNode::Dtor) {
312 sortNo = QLatin1String("D");
313 } else {
314 if (nodeName.startsWith(QLatin1String("operator"))
315 && nodeName.length() > 8
316 && !nodeName[8].isLetterOrNumber())
317 sortNo = QLatin1String("F");
318 else
319 sortNo = QLatin1String("E");
320 }
321 return sortNo + nodeName + QLatin1Char(' ')
322 + QString::number(func->overloadNumber(), 36);
323 }
324
325 if (node->type() == Node::Class)
326 return QLatin1Char('A') + nodeName;
327
328 if (node->type() == Node::Property || node->type() == Node::Variable)
329 return QLatin1Char('E') + nodeName;
330
331 return QLatin1Char('B') + nodeName;
332}
333
334void CodeMarker::insert(FastSection &fastSection, Node *node, SynopsisStyle style, Status status)
335{
336 bool inheritedMember = (!node->relates() &&
337 (node->parent() != (const InnerNode *)fastSection.innerNode));
338 bool irrelevant = false;
339
340 if (node->access() == Node::Private) {
341 irrelevant = true;
342 } else if (node->type() == Node::Function) {
343 FunctionNode *func = (FunctionNode *) node;
344 irrelevant = (inheritedMember
345 && (func->metaness() == FunctionNode::Ctor ||
346 func->metaness() == FunctionNode::Dtor));
347 } else if (node->type() == Node::Class || node->type() == Node::Enum
348 || node->type() == Node::Typedef) {
349 irrelevant = (inheritedMember && style != SeparateList);
350 if (!irrelevant && style == Detailed && node->type() == Node::Typedef) {
351 const TypedefNode* typedeffe = static_cast<const TypedefNode*>(node);
352 if (typedeffe->associatedEnum())
353 irrelevant = true;
354 }
355 }
356
357 if (!irrelevant) {
358 if (status == Compat) {
359 irrelevant = (node->status() != Node::Compat);
360 } else if (status == Obsolete) {
361 irrelevant = (node->status() != Node::Obsolete);
362 } else {
363 irrelevant = (node->status() == Node::Compat ||
364 node->status() == Node::Obsolete);
365 }
366 }
367
368 if (!irrelevant) {
369 if (!inheritedMember || style == SeparateList) {
370 QString key = sortName(node);
371 if (!fastSection.memberMap.contains(key))
372 fastSection.memberMap.insert(key, node);
373 } else {
374 if (node->parent()->type() == Node::Class) {
375 if (fastSection.inherited.isEmpty()
376 || fastSection.inherited.last().first != node->parent()) {
377 QPair<ClassNode *, int> p((ClassNode *)node->parent(), 0);
378 fastSection.inherited.append(p);
379 }
380 fastSection.inherited.last().second++;
381 }
382 }
383 }
384}
385
386void CodeMarker::append(QList<Section>& sectionList,
387 const FastSection& fastSection)
388{
389 if (!fastSection.memberMap.isEmpty() ||
390 !fastSection.inherited.isEmpty()) {
391 Section section(fastSection.name,
392 fastSection.singularMember,
393 fastSection.pluralMember);
394 section.members = fastSection.memberMap.values();
395 section.inherited = fastSection.inherited;
396 sectionList.append(section);
397 }
398}
399
400static QString encode(const QString &string)
401{
402#if 0
403 QString result = string;
404
405 for (int i = string.size() - 1; i >= 0; --i) {
406 uint ch = string.at(i).unicode();
407 if (ch > 0xFF)
408 ch = '?';
409 if ((ch - '0') >= 10 && (ch - 'a') >= 26 && (ch - 'A') >= 26
410 && ch != '/' && ch != '(' && ch != ')' && ch != ',' && ch != '*'
411 && ch != '&' && ch != '_' && ch != '<' && ch != '>' && ch != ':'
412 && ch != '~')
413 result.replace(i, 1, QString("%") + QString("%1").arg(ch, 2, 16));
414 }
415 return result;
416#else
417 return string;
418#endif
419}
420
421QStringList CodeMarker::macRefsForNode(const Node *node)
422{
423 QString result = QLatin1String("cpp/");
424 switch (node->type()) {
425 case Node::Class:
426 {
427 const ClassNode *classe = static_cast<const ClassNode *>(node);
428#if 0
429 if (!classe->templateStuff().isEmpty()) {
430 result += QLatin1String("tmplt/");
431 } else
432#endif
433 {
434 result += QLatin1String("cl/");
435 }
436 result += macName(classe); // ### Maybe plainName?
437 }
438 break;
439 case Node::Enum:
440 {
441 QStringList stringList;
442 stringList << encode(result + QLatin1String("tag/") +
443 macName(node));
444 foreach (const QString &enumName, node->doc().enumItemNames()) {
445 // ### Write a plainEnumValue() and use it here
446 stringList << encode(result + QLatin1String("econst/") +
447 macName(node->parent(), enumName));
448 }
449 return stringList;
450 }
451 case Node::Typedef:
452 result += QLatin1String("tdef/") + macName(node);
453 break;
454 case Node::Function:
455 {
456 bool isMacro = false;
457 const FunctionNode *func = static_cast<const FunctionNode *>(node);
458
459 // overloads are too clever for the Xcode documentation browser
460 if (func->isOverload())
461 return QStringList();
462
463 if (func->metaness() == FunctionNode::MacroWithParams
464 || func->metaness() == FunctionNode::MacroWithoutParams) {
465 result += QLatin1String("macro/");
466 isMacro = true;
467#if 0
468 } else if (!func->templateStuff().isEmpty()) {
469 result += QLatin1String("ftmplt/");
470#endif
471 } else if (func->isStatic()) {
472 result += QLatin1String("clm/");
473 } else if (!func->parent()->name().isEmpty()) {
474 result += QLatin1String("instm/");
475 } else {
476 result += QLatin1String("func/");
477 }
478
479 result += macName(func);
480 if (result.endsWith(QLatin1String("()")))
481 result.chop(2);
482#if 0
483 // this code is too clever for the Xcode documentation
484 // browser and/or pbhelpindexer
485 if (!isMacro) {
486 result += "/" + QLatin1String(QMetaObject::normalizedSignature(func->returnType().toLatin1().constData())) + "/(";
487 const QList<Parameter> &params = func->parameters();
488 for (int i = 0; i < params.count(); ++i) {
489 QString type = params.at(i).leftType() + params.at(i).rightType();
490 type = QLatin1String(QMetaObject::normalizedSignature(type.toLatin1().constData()));
491 if (i != 0)
492 result += ",";
493 result += type;
494 }
495 result += ")";
496 }
497#endif
498 }
499 break;
500 case Node::Variable:
501 result += QLatin1String("data/") + macName(node);
502 break;
503 case Node::Property:
504 {
505 NodeList list = static_cast<const PropertyNode*>(node)->functions();
506 QStringList stringList;
507 foreach (const Node *node, list) {
508 stringList += macRefsForNode(node);
509 }
510 return stringList;
511 }
512 case Node::Namespace:
513 case Node::Fake:
514 case Node::Target:
515 default:
516 return QStringList();
517 }
518
519 return QStringList(encode(result));
520}
521
522QString CodeMarker::macName(const Node *node, const QString &name)
523{
524 QString myName = name;
525 if (myName.isEmpty()) {
526 myName = node->name();
527 node = node->parent();
528 }
529
530 if (node->name().isEmpty()) {
531 return QLatin1Char('/') + myName;
532 } else {
533 return plainFullName(node) + QLatin1Char('/') + myName;
534 }
535}
536
537
538QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.