forked from qt-creator/qt-creator
CppHighlighter: highlight macro references.
Change-Id: I7c90957aa67e03a109af0a722160d4e1c759d716 Reviewed-by: Leandro Melo <leandro.melo@nokia.com>
This commit is contained in:
committed by
Leandro Melo
parent
85ce5aba62
commit
85609aff8e
@@ -33,7 +33,7 @@
|
||||
#include "cppchecksymbols.h"
|
||||
#include "cpplocalsymbols.h"
|
||||
|
||||
#include <cplusplus/Overview.h>
|
||||
#include <cplusplus/SimpleLexer.h>
|
||||
|
||||
#include <Names.h>
|
||||
#include <Literals.h>
|
||||
@@ -284,16 +284,21 @@ protected:
|
||||
|
||||
} // end of anonymous namespace
|
||||
|
||||
CheckSymbols::Future CheckSymbols::go(Document::Ptr doc, const LookupContext &context)
|
||||
static bool sortByLinePredicate(const CheckSymbols::Use &lhs, const CheckSymbols::Use &rhs)
|
||||
{
|
||||
return lhs.line < rhs.line;
|
||||
}
|
||||
|
||||
CheckSymbols::Future CheckSymbols::go(Document::Ptr doc, const LookupContext &context, const QList<CheckSymbols::Use> ¯oUses)
|
||||
{
|
||||
QTC_ASSERT(doc, return Future());
|
||||
|
||||
return (new CheckSymbols(doc, context))->start();
|
||||
return (new CheckSymbols(doc, context, macroUses))->start();
|
||||
}
|
||||
|
||||
CheckSymbols::CheckSymbols(Document::Ptr doc, const LookupContext &context)
|
||||
CheckSymbols::CheckSymbols(Document::Ptr doc, const LookupContext &context, const QList<CheckSymbols::Use> ¯oUses)
|
||||
: ASTVisitor(doc->translationUnit()), _doc(doc), _context(context)
|
||||
, _lineOfLastUsage(0)
|
||||
, _lineOfLastUsage(0), _macroUses(macroUses)
|
||||
{
|
||||
CollectSymbols collectTypes(doc, context.snapshot());
|
||||
|
||||
@@ -311,11 +316,13 @@ CheckSymbols::~CheckSymbols()
|
||||
|
||||
void CheckSymbols::run()
|
||||
{
|
||||
qSort(_macroUses.begin(), _macroUses.end(), sortByLinePredicate);
|
||||
_diagnosticMessages.clear();
|
||||
|
||||
if (! isCanceled()) {
|
||||
if (_doc->translationUnit()) {
|
||||
accept(_doc->translationUnit()->ast());
|
||||
_usages << QVector<Use>::fromList(_macroUses);
|
||||
flush();
|
||||
}
|
||||
}
|
||||
@@ -876,6 +883,9 @@ void CheckSymbols::addUse(const Use &use)
|
||||
}
|
||||
}
|
||||
|
||||
while (!_macroUses.isEmpty() && _macroUses.first().line <= use.line)
|
||||
_usages.append(_macroUses.takeFirst());
|
||||
|
||||
_lineOfLastUsage = qMax(_lineOfLastUsage, use.line);
|
||||
_usages.append(use);
|
||||
}
|
||||
@@ -1115,11 +1125,6 @@ bool CheckSymbols::maybeVirtualMethod(const Name *name) const
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool sortByLinePredicate(const CheckSymbols::Use &lhs, const CheckSymbols::Use &rhs)
|
||||
{
|
||||
return lhs.line < rhs.line;
|
||||
}
|
||||
|
||||
void CheckSymbols::flush()
|
||||
{
|
||||
_lineOfLastUsage = 0;
|
||||
|
||||
@@ -72,7 +72,7 @@ public:
|
||||
return future;
|
||||
}
|
||||
|
||||
static Future go(Document::Ptr doc, const LookupContext &context);
|
||||
static Future go(Document::Ptr doc, const LookupContext &context, const QList<Use> ¯oUses);
|
||||
|
||||
static QMap<int, QVector<Use> > chunks(const QFuture<Use> &future, int from, int to)
|
||||
{
|
||||
@@ -94,7 +94,7 @@ protected:
|
||||
using ASTVisitor::visit;
|
||||
using ASTVisitor::endVisit;
|
||||
|
||||
CheckSymbols(Document::Ptr doc, const LookupContext &context);
|
||||
CheckSymbols(Document::Ptr doc, const LookupContext &context, const QList<Use> ¯oUses);
|
||||
|
||||
bool hasVirtualDestructor(Class *klass) const;
|
||||
bool hasVirtualDestructor(ClassOrNamespace *binding) const;
|
||||
@@ -174,6 +174,7 @@ private:
|
||||
QList<AST *> _astStack;
|
||||
QVector<Use> _usages;
|
||||
unsigned _lineOfLastUsage;
|
||||
QList<Use> _macroUses;
|
||||
};
|
||||
|
||||
} // namespace CPlusPlus
|
||||
|
||||
@@ -34,6 +34,10 @@
|
||||
#include "cpphighlightingsupportinternal.h"
|
||||
|
||||
#include <cplusplus/LookupContext.h>
|
||||
#include <cplusplus/SimpleLexer.h>
|
||||
#include <cplusplus/Token.h>
|
||||
#include <cpptools/cpptoolsreuse.h>
|
||||
#include <texteditor/itexteditor.h>
|
||||
|
||||
using namespace CPlusPlus;
|
||||
using namespace CppTools;
|
||||
@@ -52,8 +56,32 @@ QFuture<CppHighlightingSupport::Use> CppHighlightingSupportInternal::highlightin
|
||||
const Document::Ptr &doc,
|
||||
const Snapshot &snapshot) const
|
||||
{
|
||||
//Get macro uses
|
||||
QList<CheckSymbols::Use> macroUses;
|
||||
foreach (Document::MacroUse macro, doc->macroUses()) {
|
||||
const QString name = QString::fromUtf8(macro.macro().name());
|
||||
|
||||
//Filter out QtKeywords
|
||||
if (isQtKeyword(QStringRef(&name)))
|
||||
continue;
|
||||
|
||||
//Filter out C++ keywords
|
||||
SimpleLexer tokenize;
|
||||
tokenize.setQtMocRunEnabled(false);
|
||||
tokenize.setObjCEnabled(false);
|
||||
const QList<Token> tokens = tokenize(name);
|
||||
if (tokens.length() && (tokens.at(0).isKeyword() || tokens.at(0).isObjCAtKeyword()))
|
||||
continue;
|
||||
|
||||
int line, column;
|
||||
editor()->convertPosition(macro.begin(), &line, &column);
|
||||
++column; //Highlighting starts at (column-1) --> compensate here
|
||||
CheckSymbols::Use use(line, column, name.size(), SemanticInfo::MacroUse);
|
||||
macroUses.append(use);
|
||||
}
|
||||
|
||||
LookupContext context(doc, snapshot);
|
||||
return CheckSymbols::go(doc, context);
|
||||
return CheckSymbols::go(doc, context, macroUses);
|
||||
}
|
||||
|
||||
CppHighlightingSupportInternalFactory::~CppHighlightingSupportInternalFactory()
|
||||
|
||||
@@ -52,7 +52,8 @@ public:
|
||||
FieldUse,
|
||||
StaticUse,
|
||||
VirtualMethodUse,
|
||||
LabelUse
|
||||
LabelUse,
|
||||
MacroUse
|
||||
};
|
||||
typedef TextEditor::SemanticHighlighter::Result Use;
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
#include <QSet>
|
||||
#include <QTextDocument>
|
||||
#include <QTextCursor>
|
||||
#include <QStringRef>
|
||||
|
||||
using namespace CPlusPlus;
|
||||
|
||||
@@ -131,5 +132,49 @@ bool isValidIdentifier(const QString &s)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isQtKeyword(const QStringRef &text)
|
||||
{
|
||||
switch (text.length()) {
|
||||
case 4:
|
||||
switch (text.at(0).toLatin1()) {
|
||||
case 'e':
|
||||
if (text == QLatin1String("emit"))
|
||||
return true;
|
||||
break;
|
||||
case 'S':
|
||||
if (text == QLatin1String("SLOT"))
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case 5:
|
||||
if (text.at(0) == QLatin1Char('s') && text == QLatin1String("slots"))
|
||||
return true;
|
||||
break;
|
||||
|
||||
case 6:
|
||||
if (text.at(0) == QLatin1Char('S') && text == QLatin1String("SIGNAL"))
|
||||
return true;
|
||||
break;
|
||||
|
||||
case 7:
|
||||
switch (text.at(0).toLatin1()) {
|
||||
case 's':
|
||||
if (text == QLatin1String("signals"))
|
||||
return true;
|
||||
break;
|
||||
case 'f':
|
||||
if (text == QLatin1String("foreach") || text == QLatin1String("forever"))
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // CppTools
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
#include "cpptools_global.h"
|
||||
|
||||
QT_FORWARD_DECLARE_CLASS(QTextCursor)
|
||||
QT_FORWARD_DECLARE_CLASS(QStringRef)
|
||||
|
||||
namespace CPlusPlus {
|
||||
class Symbol;
|
||||
@@ -52,6 +53,8 @@ bool CPPTOOLS_EXPORT isOwnershipRAIIType(CPlusPlus::Symbol *symbol,
|
||||
|
||||
bool CPPTOOLS_EXPORT isValidIdentifier(const QString &s);
|
||||
|
||||
bool CPPTOOLS_EXPORT isQtKeyword(const QStringRef &text);
|
||||
|
||||
} // CppTools
|
||||
|
||||
#endif // CPPTOOLSREUSE_H
|
||||
|
||||
Reference in New Issue
Block a user