Commit fa7c7c4e authored by Leandro Melo's avatar Leandro Melo

C++: Don't show unused mark for known RAII types

Change-Id: Id552539c6a2cf5d7558adf88bed61a11ab770516
Reviewed-by: default avatarChristian Kamm <christian.d.kamm@nokia.com>
parent dfd58916
......@@ -68,6 +68,7 @@
#include <cpptools/cppqtstyleindenter.h>
#include <cpptools/cppcodestylesettings.h>
#include <cpptools/cpprefactoringchanges.h>
#include <cpptools/cpptoolsreuse.h>
#include <coreplugin/icore.h>
#include <coreplugin/actionmanager/actionmanager.h>
......@@ -1776,6 +1777,11 @@ void CPPEditorWidget::updateSemanticInfo(const SemanticInfo &semanticInfo)
m_renameSelections.clear();
m_currentRenameSelection = NoCurrentRenameSelection;
// We can use the semanticInfo's snapshot (and avoid locking), but not its
// document, since it doesn't contain expanded macros.
LookupContext context(semanticInfo.snapshot.document(file()->fileName()),
semanticInfo.snapshot);
SemanticInfo::LocalUseIterator it(semanticInfo.localUses);
while (it.hasNext()) {
it.next();
......@@ -1791,12 +1797,14 @@ void CPPEditorWidget::updateSemanticInfo(const SemanticInfo &semanticInfo)
}
}
if (uses.size() == 1)
// it's an unused declaration
highlightUses(uses, semanticInfo, &unusedSelections);
else if (good && m_renameSelections.isEmpty())
if (uses.size() == 1) {
if (!CppTools::isOwnershipRAIIType(it.key(), context)) {
// it's an unused declaration
highlightUses(uses, semanticInfo, &unusedSelections);
}
} else if (good && m_renameSelections.isEmpty()) {
highlightUses(uses, semanticInfo, &m_renameSelections);
}
}
if (m_lastSemanticInfo.forced || previousSemanticInfo.revision != semanticInfo.revision) {
......
......@@ -32,9 +32,17 @@
#include "cpptoolsreuse.h"
#include <Symbols.h>
#include <CoreTypes.h>
#include <cplusplus/Overview.h>
#include <cplusplus/LookupContext.h>
#include <QtCore/QSet>
#include <QtGui/QTextDocument>
#include <QtGui/QTextCursor>
using namespace CPlusPlus;
namespace CppTools {
void moveCursorToEndOfIdentifier(QTextCursor *tc) {
......@@ -49,4 +57,50 @@ void moveCursorToEndOfIdentifier(QTextCursor *tc) {
}
}
static bool isOwnershipRAIIName(const QString &name)
{
static QSet<QString> knownNames;
if (knownNames.isEmpty()) {
// Qt
knownNames.insert(QLatin1String("QScopedPointer"));
knownNames.insert(QLatin1String("QScopedArrayPointer"));
knownNames.insert(QLatin1String("QMutexLocker"));
knownNames.insert(QLatin1String("QReadLocker"));
knownNames.insert(QLatin1String("QWriteLocker"));
// Standard C++
knownNames.insert(QLatin1String("auto_ptr"));
knownNames.insert(QLatin1String("unique_ptr"));
// Boost
knownNames.insert(QLatin1String("scoped_ptr"));
knownNames.insert(QLatin1String("scoped_array"));
}
return knownNames.contains(name);
}
bool isOwnershipRAIIType(CPlusPlus::Symbol *symbol, const LookupContext &context)
{
if (!symbol)
return false;
// This is not a "real" comparison of types. What we do is to resolve the symbol
// in question and then try to match its name with already known ones.
if (symbol->isDeclaration()) {
Declaration *declaration = symbol->asDeclaration();
const NamedType *namedType = declaration->type()->asNamedType();
if (namedType) {
ClassOrNamespace *clazz = context.lookupType(namedType->name(),
declaration->enclosingScope());
if (clazz && !clazz->symbols().isEmpty()) {
Overview overview;
Symbol *symbol = clazz->symbols().at(0);
return isOwnershipRAIIName(overview.prettyName(symbol->name()));
}
}
}
return false;
}
} // CppTools
......@@ -37,10 +37,18 @@
QT_FORWARD_DECLARE_CLASS(QTextCursor)
namespace CPlusPlus {
class Symbol;
class LookupContext;
}
namespace CppTools {
void CPPTOOLS_EXPORT moveCursorToEndOfIdentifier(QTextCursor *tc);
bool CPPTOOLS_EXPORT isOwnershipRAIIType(CPlusPlus::Symbol *symbol,
const CPlusPlus::LookupContext &context);
} // CppTools
#endif // CPPTOOLSREUSE_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment