Commit 0f301f86 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

New implementation of CPlusPlus::FindUsages

parent f72a080d
This diff is collapsed.
......@@ -68,67 +68,239 @@ public:
QList<int> references() const;
protected:
using ASTVisitor::visit;
using ASTVisitor::endVisit;
using ASTVisitor::translationUnit;
Scope *switchScope(Scope *scope);
Scope *switchScope(ScopedSymbol *symbol);
QString matchingLine(const Token &tk) const;
void reportResult(unsigned tokenIndex, const Name *name, Scope *scope = 0);
void reportResult(unsigned tokenIndex, const Identifier *id, Scope *scope = 0);
void reportResult(unsigned tokenIndex, const QList<LookupItem> &candidates);
void reportResult(unsigned tokenIndex);
bool checkCandidates(const QList<LookupItem> &candidates) const;
void checkExpression(unsigned startToken, unsigned endToken);
void checkExpression(unsigned startToken, unsigned endToken, Scope *scope = 0);
void ensureNameIsValid(NameAST *ast);
static bool compareFullyQualifiedName(const QList<const Name *> &path, const QList<const Name *> &other);
static bool compareName(const Name *name, const Name *other);
FunctionDefinitionAST *enclosingFunctionDefinition() const;
TemplateDeclarationAST *enclosingTemplateDeclaration() const;
Scope *enclosingScope();
void statement(StatementAST *ast);
void expression(ExpressionAST *ast);
void declaration(DeclarationAST *ast);
const Name *name(NameAST *ast);
void specifier(SpecifierAST *ast);
void ptrOperator(PtrOperatorAST *ast);
void coreDeclarator(CoreDeclaratorAST *ast);
void postfixDeclarator(PostfixDeclaratorAST *ast);
bool preVisit(AST *ast);
void postVisit(AST *);
void objCSelectorArgument(ObjCSelectorArgumentAST *ast);
void attribute(AttributeAST *ast);
void declarator(DeclaratorAST *ast);
void qtPropertyDeclarationItem(QtPropertyDeclarationItemAST *ast);
void qtInterfaceName(QtInterfaceNameAST *ast);
void baseSpecifier(BaseSpecifierAST *ast);
void ctorInitializer(CtorInitializerAST *ast);
void enumerator(EnumeratorAST *ast);
void exceptionSpecification(ExceptionSpecificationAST *ast);
void memInitializer(MemInitializerAST *ast);
void nestedNameSpecifier(NestedNameSpecifierAST *ast);
void newPlacement(NewPlacementAST *ast);
void newArrayDeclarator(NewArrayDeclaratorAST *ast);
void newInitializer(NewInitializerAST *ast);
void newTypeId(NewTypeIdAST *ast);
void cppOperator(OperatorAST *ast);
void parameterDeclarationClause(ParameterDeclarationClauseAST *ast);
void translationUnit(TranslationUnitAST *ast);
void objCProtocolRefs(ObjCProtocolRefsAST *ast);
void objCMessageArgument(ObjCMessageArgumentAST *ast);
void objCTypeName(ObjCTypeNameAST *ast);
void objCInstanceVariablesDeclaration(ObjCInstanceVariablesDeclarationAST *ast);
void objCPropertyAttribute(ObjCPropertyAttributeAST *ast);
void objCMessageArgumentDeclaration(ObjCMessageArgumentDeclarationAST *ast);
void objCMethodPrototype(ObjCMethodPrototypeAST *ast);
void objCSynthesizedProperty(ObjCSynthesizedPropertyAST *ast);
void lambdaIntroducer(LambdaIntroducerAST *ast);
void lambdaCapture(LambdaCaptureAST *ast);
void capture(CaptureAST *ast);
void lambdaDeclarator(LambdaDeclaratorAST *ast);
void trailingReturnType(TrailingReturnTypeAST *ast);
virtual bool visit(NamespaceAST *ast);
// AST
virtual bool visit(ObjCSelectorArgumentAST *ast);
virtual bool visit(AttributeAST *ast);
virtual bool visit(DeclaratorAST *ast);
virtual bool visit(QtPropertyDeclarationItemAST *ast);
virtual bool visit(QtInterfaceNameAST *ast);
virtual bool visit(BaseSpecifierAST *ast);
virtual bool visit(CtorInitializerAST *ast);
virtual bool visit(EnumeratorAST *ast);
virtual bool visit(ExceptionSpecificationAST *ast);
virtual bool visit(MemInitializerAST *ast);
virtual bool visit(MemberAccessAST *ast);
virtual bool visit(NestedNameSpecifierAST *ast);
virtual bool visit(NewPlacementAST *ast);
virtual bool visit(NewArrayDeclaratorAST *ast);
virtual bool visit(NewInitializerAST *ast);
virtual bool visit(NewTypeIdAST *ast);
virtual bool visit(OperatorAST *ast);
virtual bool visit(ParameterDeclarationClauseAST *ast);
virtual bool visit(TranslationUnitAST *ast);
virtual bool visit(ObjCProtocolRefsAST *ast);
virtual bool visit(ObjCMessageArgumentAST *ast);
virtual bool visit(ObjCTypeNameAST *ast);
virtual bool visit(ObjCInstanceVariablesDeclarationAST *ast);
virtual bool visit(ObjCPropertyAttributeAST *ast);
virtual bool visit(ObjCMessageArgumentDeclarationAST *ast);
virtual bool visit(ObjCMethodPrototypeAST *ast);
virtual bool visit(ObjCSynthesizedPropertyAST *ast);
virtual bool visit(LambdaIntroducerAST *ast);
virtual bool visit(LambdaCaptureAST *ast);
virtual bool visit(CaptureAST *ast);
virtual bool visit(LambdaDeclaratorAST *ast);
virtual bool visit(TrailingReturnTypeAST *ast);
// StatementAST
virtual bool visit(QtMemberDeclarationAST *ast);
virtual bool visit(CaseStatementAST *ast);
virtual bool visit(CompoundStatementAST *ast);
virtual bool visit(DeclarationStatementAST *ast);
virtual bool visit(DoStatementAST *ast);
virtual bool visit(ExpressionOrDeclarationStatementAST *ast);
virtual bool visit(ExpressionStatementAST *ast);
virtual bool visit(ForeachStatementAST *ast);
virtual bool visit(ForStatementAST *ast);
virtual bool visit(IfStatementAST *ast);
virtual bool visit(LabeledStatementAST *ast);
virtual bool visit(BreakStatementAST *ast);
virtual bool visit(ContinueStatementAST *ast);
virtual bool visit(GotoStatementAST *ast);
virtual bool visit(ReturnStatementAST *ast);
virtual bool visit(SwitchStatementAST *ast);
virtual bool visit(TryBlockStatementAST *ast);
virtual bool visit(CatchClauseAST *ast);
virtual bool visit(WhileStatementAST *ast);
virtual bool visit(ObjCFastEnumerationAST *ast);
virtual bool visit(ObjCSynchronizedStatementAST *ast);
// ExpressionAST
virtual bool visit(IdExpressionAST *ast);
virtual bool visit(CompoundExpressionAST *ast);
virtual bool visit(CompoundLiteralAST *ast);
virtual bool visit(QtMethodAST *ast);
virtual bool visit(BinaryExpressionAST *ast);
virtual bool visit(CastExpressionAST *ast);
virtual bool visit(ConditionAST *ast);
virtual bool visit(ConditionalExpressionAST *ast);
virtual bool visit(CppCastExpressionAST *ast);
virtual bool visit(DeleteExpressionAST *ast);
virtual bool visit(ArrayInitializerAST *ast);
virtual bool visit(NewExpressionAST *ast);
virtual bool visit(TypeidExpressionAST *ast);
virtual bool visit(TypenameCallExpressionAST *ast);
virtual bool visit(TypeConstructorCallAST *ast);
virtual bool visit(SizeofExpressionAST *ast);
virtual bool visit(NumericLiteralAST *ast);
virtual bool visit(BoolLiteralAST *ast);
virtual bool visit(ThisExpressionAST *ast);
virtual bool visit(NestedExpressionAST *ast);
virtual bool visit(StringLiteralAST *ast);
virtual bool visit(ThrowExpressionAST *ast);
virtual bool visit(TypeIdAST *ast);
virtual bool visit(UnaryExpressionAST *ast);
virtual bool visit(ObjCMessageExpressionAST *ast);
virtual bool visit(ObjCProtocolExpressionAST *ast);
virtual bool visit(ObjCEncodeExpressionAST *ast);
virtual bool visit(ObjCSelectorExpressionAST *ast);
virtual bool visit(LambdaExpressionAST *ast);
virtual bool visit(BracedInitializerAST *ast);
// DeclarationAST
virtual bool visit(SimpleDeclarationAST *ast);
virtual bool visit(EmptyDeclarationAST *ast);
virtual bool visit(AccessDeclarationAST *ast);
virtual bool visit(QtObjectTagAST *ast);
virtual bool visit(QtPrivateSlotAST *ast);
virtual bool visit(QtPropertyDeclarationAST *ast);
virtual bool visit(QtEnumDeclarationAST *ast);
virtual bool visit(QtFlagsDeclarationAST *ast);
virtual bool visit(QtInterfacesDeclarationAST *ast);
virtual bool visit(AsmDefinitionAST *ast);
virtual bool visit(ExceptionDeclarationAST *ast);
virtual bool visit(FunctionDefinitionAST *ast);
virtual bool visit(LinkageBodyAST *ast);
virtual bool visit(LinkageSpecificationAST *ast);
virtual bool visit(NamespaceAST *ast);
virtual bool visit(NamespaceAliasDefinitionAST *ast);
virtual bool visit(ParameterDeclarationAST *ast);
virtual bool visit(TemplateDeclarationAST *ast);
virtual bool visit(TypenameTypeParameterAST *ast);
virtual bool visit(TemplateTypeParameterAST *ast);
virtual bool visit(UsingAST *ast);
virtual bool visit(UsingDirectiveAST *ast);
virtual bool visit(ObjCClassForwardDeclarationAST *ast);
virtual bool visit(ObjCClassDeclarationAST *ast);
virtual bool visit(ObjCProtocolForwardDeclarationAST *ast);
virtual bool visit(ObjCProtocolDeclarationAST *ast);
virtual bool visit(ObjCVisibilityDeclarationAST *ast);
virtual bool visit(ObjCPropertyDeclarationAST *ast);
virtual bool visit(ObjCMethodDeclarationAST *ast);
virtual bool visit(ObjCSynthesizedPropertiesDeclarationAST *ast);
virtual bool visit(ObjCDynamicPropertiesDeclarationAST *ast);
// NameAST
virtual bool visit(ObjCSelectorAST *ast);
virtual bool visit(QualifiedNameAST *ast);
virtual bool visit(EnumeratorAST *ast);
virtual bool visit(OperatorFunctionIdAST *ast);
virtual bool visit(ConversionFunctionIdAST *ast);
virtual bool visit(SimpleNameAST *ast);
virtual bool visit(DestructorNameAST *ast);
virtual bool visit(TemplateIdAST *ast);
virtual bool visit(ParameterDeclarationAST *ast);
virtual bool visit(ExpressionOrDeclarationStatementAST *ast);
virtual bool visit(FunctionDeclaratorAST *ast);
virtual bool visit(SimpleDeclarationAST *);
virtual bool visit(ObjCSelectorAST *ast);
virtual bool visit(QtPropertyDeclarationAST *);
virtual void endVisit(QtPropertyDeclarationAST *);
virtual bool visit(TypenameTypeParameterAST *ast);
virtual bool visit(TemplateTypeParameterAST *ast);
// SpecifierAST
virtual bool visit(SimpleSpecifierAST *ast);
virtual bool visit(AttributeSpecifierAST *ast);
virtual bool visit(TypeofSpecifierAST *ast);
virtual bool visit(ClassSpecifierAST *ast);
virtual bool visit(NamedTypeSpecifierAST *ast);
virtual bool visit(ElaboratedTypeSpecifierAST *ast);
virtual bool visit(EnumSpecifierAST *ast);
virtual bool visit(FunctionDefinitionAST *ast);
// PtrOperatorAST
virtual bool visit(PointerToMemberAST *ast);
virtual bool visit(PointerAST *ast);
virtual bool visit(ReferenceAST *ast);
static bool compareFullyQualifiedName(const QList<const Name *> &path, const QList<const Name *> &other);
static bool compareName(const Name *name, const Name *other);
// PostfixAST
virtual bool visit(CallAST *ast);
virtual bool visit(ArrayAccessAST *ast);
virtual bool visit(PostIncrDecrAST *ast);
virtual bool visit(MemberAccessAST *ast);
// CoreDeclaratorAST
virtual bool visit(DeclaratorIdAST *ast);
virtual bool visit(NestedDeclaratorAST *ast);
// PostfixDeclaratorAST
virtual bool visit(FunctionDeclaratorAST *ast);
virtual bool visit(ArrayDeclaratorAST *ast);
private:
const Identifier *_id;
Symbol *_declSymbol;
QList<const Name *> _declSymbolFullyQualifiedName;
Document::Ptr _doc;
Snapshot _snapshot;
LookupContext _context;
QByteArray _source;
Semantic _sem;
QList<AST *> _astStack;
QList<int> _references;
QList<Usage> _usages;
int _inSimpleDeclaration;
bool _inQProperty;
QSet<unsigned> _processed;
TypeOfExpression typeofExpression;
Scope *_currentScope;
};
} // end of namespace CPlusPlus
#endif // CPLUSPLUS_FINDUSAGES_H
......@@ -739,12 +739,14 @@ void CPPEditor::markSymbolsNow()
setExtraSelections(CodeSemanticsSelection, selections);
}
static QList<int> lazyFindReferences(Scope *scope, QString code, const LookupContext &context)
static QList<int> lazyFindReferences(Scope *scope, QString code, Document::Ptr doc, Snapshot snapshot)
{
TypeOfExpression typeOfExpression;
typeOfExpression.init(context.thisDocument(), context.snapshot(), context.bindings());
if (Symbol *canonicalSymbol = CanonicalSymbol::canonicalSymbol(scope, code, typeOfExpression))
return CppTools::CppModelManagerInterface::instance()->references(canonicalSymbol, context);
snapshot.insert(doc);
typeOfExpression.init(doc, snapshot);
if (Symbol *canonicalSymbol = CanonicalSymbol::canonicalSymbol(scope, code, typeOfExpression)) {
return CppTools::CppModelManagerInterface::instance()->references(canonicalSymbol, typeOfExpression.context());
}
return QList<int>();
}
......@@ -758,12 +760,10 @@ void CPPEditor::markSymbols(const QTextCursor &tc, const SemanticInfo &info)
CanonicalSymbol cs(this, info);
QString expression;
if (Scope *scope = cs.getScopeAndExpression(this, info, tc, &expression)) {
LookupContext context(info.doc, info.snapshot);
m_references.cancel();
m_referencesRevision = info.revision;
m_referencesCursorPosition = position();
m_references = QtConcurrent::run(&lazyFindReferences, scope, expression, context);
m_references = QtConcurrent::run(&lazyFindReferences, scope, expression, info.doc, info.snapshot);
m_referencesWatcher.setFuture(m_references);
} else {
const QList<QTextEdit::ExtraSelection> selections = extraSelections(CodeSemanticsSelection);
......
......@@ -52,6 +52,7 @@
#include <cplusplus/CppDocument.h>
#include <cplusplus/Overview.h>
#include <cplusplus/FindUsages.h>
#include <QtCore/QTime>
#include <QtCore/QTimer>
......@@ -108,18 +109,25 @@ public:
return usages; // skip this document, it's not using symbolId.
}
QByteArray source = snapshot.preprocessedCode(
getSource(fileName, workingCopy), fileName);
Document::Ptr doc;
QByteArray source;
Document::Ptr doc = snapshot.documentFromSource(source, fileName);
doc->tokenize();
if (symbolDocument && fileName == symbolDocument->fileName())
doc = symbolDocument;
else {
source = snapshot.preprocessedCode(getSource(fileName, workingCopy), fileName);
doc = snapshot.documentFromSource(source, fileName);
doc->tokenize();
}
Control *control = doc->control();
if (control->findIdentifier(symbolId->chars(), symbolId->size()) != 0) {
doc->check();
if (doc != symbolDocument)
doc->check();
FindUsages process(doc, snapshot);
process(symbol);
usages = process.usages();
}
......
......@@ -56,6 +56,7 @@
#include "TypeMatcher.h"
#include <map>
#include <set>
#include <algorithm>
using namespace CPlusPlus;
......@@ -770,3 +771,8 @@ const Identifier *Control::objcCopyId() const
const Identifier *Control::objcNonatomicId() const
{ return d->objcNonatomicId; }
bool Control::hasSymbol(Symbol *symbol) const
{
return std::find(d->symbols.begin(), d->symbols.end(), symbol) != d->symbols.end();
}
......@@ -209,6 +209,8 @@ public:
const NumericLiteral *findOrInsertNumericLiteral(const char *chars, unsigned size);
const NumericLiteral *findOrInsertNumericLiteral(const char *chars);
bool hasSymbol(Symbol *symbol) const;
private:
class Data;
friend class Data;
......
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