source: trunk/tools/porting/src/tokenreplacements.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 14.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the qt3to4 porting application of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "tokenreplacements.h"
43#include "logger.h"
44#include "portingrules.h"
45
46QT_BEGIN_NAMESPACE
47using namespace TokenEngine;
48
49void addLogSourceEntry(const QString &text, const TokenContainer &tokenContainer, const int index)
50{
51 Logger *logger = Logger::instance();
52 int line = tokenContainer.line(index);
53 int col = tokenContainer.column(index);
54 SourcePointLogEntry *logEntry =
55 new SourcePointLogEntry(QLatin1String("Info"), QLatin1String("Porting"),
56 logger->globalState.value(QLatin1String("currentFileName")),
57 line, col, text);
58 logger->addEntry(logEntry);
59}
60
61void addLogWarning(const QString &text)
62{
63 Logger::instance()->addEntry(new PlainLogEntry(QLatin1String("Warning"), QLatin1String("Porting"), text));
64}
65
66QualifiedNameParser::QualifiedNameParser(const TokenContainer &tokenContainer, const int tokenIndex)
67:tokenContainer(tokenContainer)
68,currentIndex(tokenIndex)
69{
70 Q_ASSERT(isValidIndex(currentIndex));
71}
72
73bool QualifiedNameParser::isPartOfQualifiedName()
74{
75 return ((nextScopeToken(Left) != -1) || (nextScopeToken(Right) != -1));
76}
77
78
79bool QualifiedNameParser::isValidIndex(int index)
80{
81 return (index < tokenContainer.count() && index >= 0);
82}
83
84/*
85 A qualifier is a the leftmost or middle part of a qualified name
86*/
87bool QualifiedNameParser::isQualifier()
88{
89 return (nextScopeToken(Right) != -1);
90}
91
92/*
93 A name is a the rightmost part of a qualified name.
94*/
95bool QualifiedNameParser::isName()
96{
97 return (nextScopeToken(Left) != -1);
98}
99
100/*
101 Peek for a qualifier or name in the given direction
102*/
103int QualifiedNameParser::peek(Direction direction)
104{
105 return nextScopeToken(direction);
106}
107
108/*
109 Look for a qualifier or name in the given direction,update
110 current position if found.
111*/
112int QualifiedNameParser::move(Direction direction)
113{
114 int tokenIndex = nextScopeToken(direction);
115 if(tokenIndex != -1)
116 currentIndex = tokenIndex;
117 return tokenIndex;
118}
119
120/*
121 Looks for "::" starting at currentIndex, returns the token index
122 for it if found. If the first non-whitespace token found is something else,
123 -1 is returned.
124*/
125int QualifiedNameParser::findScopeOperator(Direction direction)
126{
127 int tokenIndex = currentIndex;
128 QByteArray tokenText;
129 //loop until we get a token containing text or we pass the beginning/end of the source
130 tokenIndex += direction;
131 while(tokenText.isEmpty() && isValidIndex(tokenIndex)) {
132 tokenText = tokenContainer.text(tokenIndex).trimmed();
133 if(tokenText==QByteArray("::"))
134 return tokenIndex;
135 tokenIndex += direction;
136 }
137 return -1;
138}
139/*
140 Walks a qualified name. Returns the token index
141 for the next identifer in the qualified name, or -1 if its not found.
142*/
143int QualifiedNameParser::nextScopeToken(Direction direction)
144{
145 int tokenIndex = findScopeOperator(direction);
146 if (tokenIndex == -1)
147 return -1;
148 QByteArray tokenText;
149 //loop until we get a token containing text or we pass the start of the source
150 tokenIndex += direction;
151 while(tokenText.isEmpty() && isValidIndex(tokenIndex)) {
152 tokenText = tokenContainer.text(tokenIndex).trimmed();
153 tokenIndex += direction;
154 }
155 return tokenIndex - direction;
156}
157
158/////////////////////
159GenericTokenReplacement::GenericTokenReplacement(QByteArray oldToken, QByteArray newToken)
160:oldToken(oldToken)
161,newToken(newToken)
162{}
163
164QByteArray GenericTokenReplacement::getReplaceKey()
165{
166 return QByteArray(oldToken);
167}
168
169bool GenericTokenReplacement::doReplace(const TokenContainer &tokenContainer,
170 int index, TextReplacements &textReplacements)
171{
172 QByteArray tokenText = tokenContainer.text(index);
173 if(tokenText == oldToken){
174 addLogSourceEntry(QString::fromLatin1(tokenText + QByteArray(" -> ") + newToken), tokenContainer, index);
175 TokenEngine::Token token = tokenContainer.token(index);
176 textReplacements.insert(newToken, token.start, token.length);
177 return true;
178 }
179 return false;
180
181}
182
183///////////////////
184ClassNameReplacement::ClassNameReplacement(QByteArray oldToken, QByteArray newToken)
185:oldToken(oldToken)
186,newToken(newToken)
187{}
188
189QByteArray ClassNameReplacement::getReplaceKey()
190{
191 return QByteArray(oldToken);
192}
193
194/*
195 Replace a class name token. If the class name is a scope specifier (a "qualifier")
196 in a qualified name, we check if qualified name will be replaced by a porting rule.
197 If so, we don't do the class name replacement.
198*/
199bool ClassNameReplacement::doReplace(const TokenContainer &tokenContainer, int index, TextReplacements &textReplacements)
200{
201 QByteArray tokenText = tokenContainer.text(index);
202 if(tokenText != oldToken)
203 return false;
204
205 QualifiedNameParser nameParser(tokenContainer, index);
206 if(nameParser.isPartOfQualifiedName() &&
207 nameParser.peek(QualifiedNameParser::Right) != -1) {
208 int nameTokenIndex = nameParser.peek(QualifiedNameParser::Right);
209 QByteArray name = tokenContainer.text(nameTokenIndex);
210 TextReplacements textReplacements;
211 QList<TokenReplacement*> tokenReplacements
212 = PortingRules::instance()->getTokenReplacementRules();
213 bool changed = false;
214 foreach(TokenReplacement *tokenReplacement, tokenReplacements) {
215 changed = tokenReplacement->doReplace(tokenContainer, nameTokenIndex, textReplacements);
216 if(changed)
217 break;
218 }
219 if(changed)
220 return false;
221 }
222 addLogSourceEntry(QString::fromLatin1(tokenText + QByteArray(" -> ") + newToken), tokenContainer, index);
223 TokenEngine::Token token = tokenContainer.token(index);
224 textReplacements.insert(newToken, token.start, token.length);
225 return true;
226}
227
228///////////////////
229
230ScopedTokenReplacement::ScopedTokenReplacement(const QByteArray &oldToken,
231 const QByteArray &newToken)
232:newScopedName(newToken)
233{
234 Q_ASSERT(oldToken.contains(QByteArray("::")));
235
236 // Split oldToken into scope and name parts.
237 oldName = oldToken.mid(oldToken.lastIndexOf(':')+1);
238 oldScope = oldToken.mid(0, oldToken.indexOf(':'));
239
240 // Split newToken into scope and name parts, execept if we have a spcial
241 // case like Qt::WType_Modal -> (Qt::WType_Dialog | Qt::WShowModal)
242 if (newToken.count(QByteArray("::")) != 1 || newToken.contains(QByteArray("("))) {
243 newName = newToken;
244 } else {
245 newName = newToken.mid(newToken.lastIndexOf(':')+1);
246 newScope = newToken.mid(0, newToken.indexOf(':'));
247 }
248
249 strictMode = Logger::instance()->globalState.contains(QString::fromLatin1("strictMode"));
250}
251
252bool ScopedTokenReplacement::doReplace(const TokenContainer &tokenContainer, int sourceIndex, TextReplacements &textReplacements)
253{
254 const QByteArray sourceName = tokenContainer.text(sourceIndex);
255
256 // Check if the token texts matches.
257 if (sourceName != oldName)
258 return false;
259
260 // Get token attributes. The attributes are created by the the C++ parser/analyzer.
261 const TokenAttributes *attributes = tokenContainer.tokenAttributes();
262 // If the declaration attribute is set we don't replace.
263 if (!attributes->attribute(sourceIndex, "declaration").isEmpty())
264 return false;
265 // If the unknown (undeclared) attribute is set we don't replace.
266 if (!attributes->attribute(sourceIndex, "unknown").isEmpty())
267 return false;
268 // If nameUse is set we test if the nameUse refers to the correct declaration.
269 // This is done by checking the parentScope attribute, which returns the scope
270 // for the declaration associated with this name use.
271 const bool haveNameUseInfo = !attributes->attribute(sourceIndex, "nameUse").isEmpty();
272 if (haveNameUseInfo) {
273 if (attributes->attribute(sourceIndex, "parentScope") != oldScope)
274 return false;
275 // If the user has specified -strict, we don't replace tokens when we don't have name use info.
276 } else if (strictMode) {
277 return false;
278 }
279
280 // The token might have a qualifier, and in that case we need to check if
281 // we should replace the qualifier as well.
282 QualifiedNameParser nameParser(tokenContainer, sourceIndex);
283
284 // This is a pretty special case, it means that in a qualified
285 // name like aaa::bbb the replacement rule has been triggered for
286 // the aaa part. Since this is not what we'd normally use a
287 // ScopedReplacement for, we just return here.