1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "cpptoolsplugin.h"
35 #include "cpprefactoringchanges.h"
36 #include "insertionpointlocator.h"
39 #include <ASTVisitor.h>
40 #include <TranslationUnit.h>
42 #include <coreplugin/icore.h>
43 #include <coreplugin/mimedatabase.h>
45 using namespace CPlusPlus;
46 using namespace CppTools;
50 static QString generate(InsertionPointLocator::AccessSpec xsSpec)
54 case InsertionPointLocator::Public:
55 return QLatin1String("public:\n");
57 case InsertionPointLocator::Protected:
58 return QLatin1String("protected:\n");
60 case InsertionPointLocator::Private:
61 return QLatin1String("private:\n");
63 case InsertionPointLocator::PublicSlot:
64 return QLatin1String("public slots:\n");
66 case InsertionPointLocator::ProtectedSlot:
67 return QLatin1String("protected slots:\n");
69 case InsertionPointLocator::PrivateSlot:
70 return QLatin1String("private slots:\n");
72 case InsertionPointLocator::Signals:
73 return QLatin1String("signals:\n");
77 static int ordering(InsertionPointLocator::AccessSpec xsSpec)
79 static QList<InsertionPointLocator::AccessSpec> order = QList<InsertionPointLocator::AccessSpec>()
80 << InsertionPointLocator::Public
81 << InsertionPointLocator::PublicSlot
82 << InsertionPointLocator::Signals
83 << InsertionPointLocator::Protected
84 << InsertionPointLocator::ProtectedSlot
85 << InsertionPointLocator::PrivateSlot
86 << InsertionPointLocator::Private
89 return order.indexOf(xsSpec);
96 InsertionPointLocator::AccessSpec xsSpec;
101 , xsSpec(InsertionPointLocator::Invalid)
104 AccessRange(unsigned start, unsigned end, InsertionPointLocator::AccessSpec xsSpec)
111 class FindInClass: public ASTVisitor
114 FindInClass(const Document::Ptr &doc, const Class *clazz, InsertionPointLocator::AccessSpec xsSpec)
115 : ASTVisitor(doc->translationUnit())
121 InsertionLocation operator()()
123 _result = InsertionLocation();
125 AST *ast = translationUnit()->ast();
132 using ASTVisitor::visit;
134 bool visit(ClassSpecifierAST *ast)
136 if (!ast->lbrace_token || !ast->rbrace_token)
138 if (!ast->symbol || !ast->symbol->isEqualTo(_clazz))
141 QList<AccessRange> ranges = collectAccessRanges(
142 ast->member_specifier_list,
143 tokenKind(ast->classkey_token) == T_CLASS ? InsertionPointLocator::Private : InsertionPointLocator::Public,
147 unsigned beforeToken = 0;
148 bool needsPrefix = false;
149 bool needsSuffix = false;
150 findMatch(ranges, _xsSpec, beforeToken, needsPrefix, needsSuffix);
152 unsigned line = 0, column = 0;
153 getTokenStartPosition(beforeToken, &line, &column);
157 prefix = generate(_xsSpec);
161 suffix = QLatin1Char('\n');
163 _result = InsertionLocation(_doc->fileName(), prefix, suffix,
168 static void findMatch(const QList<AccessRange> &ranges,
169 InsertionPointLocator::AccessSpec xsSpec,
170 unsigned &beforeToken,
174 Q_ASSERT(!ranges.isEmpty());
175 const int lastIndex = ranges.size() - 1;
177 // try an exact match, and ignore the first (default) access spec:
178 for (int i = lastIndex; i > 0; --i) {
179 const AccessRange &range = ranges.at(i);
180 if (range.xsSpec == xsSpec) {
181 beforeToken = range.end;
183 needsSuffix = (i != lastIndex);
188 // try to find a fitting access spec to insert XXX:
189 for (int i = lastIndex; i > 0; --i) {
190 const AccessRange ¤t = ranges.at(i);
192 if (ordering(xsSpec) > ordering(current.xsSpec)) {
193 beforeToken = current.end;
195 needsSuffix = (i != lastIndex);
201 beforeToken = ranges.first().end;
203 needsSuffix = (ranges.size() != 1);
206 QList<AccessRange> collectAccessRanges(DeclarationListAST *decls,
207 InsertionPointLocator::AccessSpec initialXs,
209 int lastRangeEnd) const
211 QList<AccessRange> ranges;
212 ranges.append(AccessRange(firstRangeStart, lastRangeEnd, initialXs));
214 for (DeclarationListAST *iter = decls; iter; iter = iter->next) {
215 DeclarationAST *decl = iter->value;
217 if (AccessDeclarationAST *xsDecl = decl->asAccessDeclaration()) {
218 const unsigned token = xsDecl->access_specifier_token;
219 int newXsSpec = initialXs;
220 bool isSlot = xsDecl->slots_token
221 && tokenKind(xsDecl->slots_token) == T_Q_SLOTS;
223 switch (tokenKind(token)) {
225 newXsSpec = isSlot ? InsertionPointLocator::PublicSlot
226 : InsertionPointLocator::Public;
230 newXsSpec = isSlot ? InsertionPointLocator::ProtectedSlot
231 : InsertionPointLocator::Protected;
235 newXsSpec = isSlot ? InsertionPointLocator::PrivateSlot
236 : InsertionPointLocator::Private;
240 newXsSpec = InsertionPointLocator::Signals;
244 newXsSpec = ranges.last().xsSpec | InsertionPointLocator::SlotBit;
252 if (newXsSpec != ranges.last().xsSpec) {
253 ranges.last().end = token;
254 ranges.append(AccessRange(token, lastRangeEnd, (InsertionPointLocator::AccessSpec) newXsSpec));
259 ranges.last().end = lastRangeEnd;
266 InsertionPointLocator::AccessSpec _xsSpec;
268 InsertionLocation _result;
271 } // end of anonymous namespace
273 InsertionLocation::InsertionLocation()
278 InsertionLocation::InsertionLocation(const QString &fileName,
279 const QString &prefix,
280 const QString &suffix,
281 unsigned line, unsigned column)
282 : m_fileName(fileName)
289 InsertionPointLocator::InsertionPointLocator(CppRefactoringChanges *refactoringChanges)
290 : m_refactoringChanges(refactoringChanges)
294 InsertionLocation InsertionPointLocator::methodDeclarationInClass(
295 const QString &fileName,
297 AccessSpec xsSpec) const
299 const Document::Ptr doc = m_refactoringChanges->file(fileName).cppDocument();
301 FindInClass find(doc, clazz, xsSpec);
304 return InsertionLocation();
308 static bool isSourceFile(const QString &fileName)
310 const Core::MimeDatabase *mimeDb = Core::ICore::instance()->mimeDatabase();
311 Core::MimeType cSourceTy = mimeDb->findByType(QLatin1String("text/x-csrc"));
312 Core::MimeType cppSourceTy = mimeDb->findByType(QLatin1String("text/x-c++src"));
313 Core::MimeType mSourceTy = mimeDb->findByType(QLatin1String("text/x-objcsrc"));
314 QStringList suffixes = cSourceTy.suffixes();
315 suffixes += cppSourceTy.suffixes();
316 suffixes += mSourceTy.suffixes();
317 QFileInfo fileInfo(fileName);
318 return suffixes.contains(fileInfo.suffix());
321 /// Currently, we return the end of fileName.cpp
322 /// \todo take the definitions of the surrounding declarations into account
323 QList<InsertionLocation> InsertionPointLocator::methodDefinition(
324 Declaration *declaration) const
326 QList<InsertionLocation> result;
330 const QString declFileName = QString::fromUtf8(declaration->fileName(),
331 declaration->fileNameLength());
332 QString target = declFileName;
333 if (!isSourceFile(declFileName)) {
334 Internal::CppToolsPlugin *cpptools = Internal::CppToolsPlugin::instance();
335 QString candidate = cpptools->correspondingHeaderOrSource(declFileName);
336 if (!candidate.isEmpty())
340 Document::Ptr doc = m_refactoringChanges->file(target).cppDocument();
344 Snapshot simplified = m_refactoringChanges->snapshot().simplified(doc);
345 if (Symbol *s = simplified.findMatchingDefinition(declaration)) {
346 if (Function *f = s->asFunction()) {
347 if (f->isConst() == declaration->type().isConst()
348 && f->isVolatile() == declaration->type().isVolatile())
353 TranslationUnit *xUnit = doc->translationUnit();
354 unsigned tokenCount = xUnit->tokenCount();
355 if (tokenCount < 2) // no tokens available
358 unsigned line = 0, column = 0;
359 xUnit->getTokenEndPosition(xUnit->tokenCount() - 2, &line, &column);
361 const QLatin1String prefix("\n\n");
362 result.append(InsertionLocation(target, prefix, QString(), line, column));