summaryrefslogtreecommitdiff
path: root/src/libs/cplusplus/CppDocument.cpp
diff options
context:
space:
mode:
authorRoberto Raggi <roberto.raggi@nokia.com>2010-05-12 12:53:16 +0200
committerRoberto Raggi <roberto.raggi@nokia.com>2010-05-14 13:55:21 +0200
commit60f76c96e8cf9b751e6250a9f80d2517adaf7a5b (patch)
tree098e360e59bf66ca8688582da8896a576554a531 /src/libs/cplusplus/CppDocument.cpp
parent140756eef42f4b580c9aecac33c2284d3937bf9f (diff)
downloadqt-creator-60f76c96e8cf9b751e6250a9f80d2517adaf7a5b.tar.gz
Improved LookupItem and get rid of some deprecated code.
Diffstat (limited to 'src/libs/cplusplus/CppDocument.cpp')
-rw-r--r--src/libs/cplusplus/CppDocument.cpp95
1 files changed, 93 insertions, 2 deletions
diff --git a/src/libs/cplusplus/CppDocument.cpp b/src/libs/cplusplus/CppDocument.cpp
index cf4dd07940..2abaf72ef2 100644
--- a/src/libs/cplusplus/CppDocument.cpp
+++ b/src/libs/cplusplus/CppDocument.cpp
@@ -57,6 +57,91 @@ using namespace CPlusPlus;
namespace {
+class FindScopeAt: protected SymbolVisitor
+{
+ TranslationUnit *_unit;
+ unsigned _line;
+ unsigned _column;
+ Scope *_scope;
+
+public:
+ FindScopeAt(TranslationUnit *unit, unsigned line, unsigned column)
+ : _unit(unit), _line(line), _column(column), _scope(0) {}
+
+ Scope *operator()(Symbol *symbol)
+ {
+ accept(symbol);
+ return _scope;
+ }
+
+protected:
+ bool process(ScopedSymbol *symbol)
+ {
+ if (! _scope) {
+ Scope *scope = symbol->members();
+
+ for (unsigned i = 0; i < scope->symbolCount(); ++i) {
+ accept(scope->symbolAt(i));
+
+ if (_scope)
+ return false;
+ }
+
+ unsigned startLine, startColumn;
+ _unit->getPosition(symbol->startOffset(), &startLine, &startColumn);
+
+ if (_line > startLine || (_line == startLine && _column >= startColumn)) {
+ unsigned endLine, endColumn;
+ _unit->getPosition(symbol->endOffset(), &endLine, &endColumn);
+
+ if (_line < endLine || (_line == endLine && _column < endColumn))
+ _scope = scope;
+ }
+ }
+
+ return false;
+ }
+
+ using SymbolVisitor::visit;
+
+ virtual bool preVisit(Symbol *)
+ { return ! _scope; }
+
+ virtual bool visit(UsingNamespaceDirective *) { return false; }
+ virtual bool visit(UsingDeclaration *) { return false; }
+ virtual bool visit(NamespaceAlias *) { return false; }
+ virtual bool visit(Declaration *) { return false; }
+ virtual bool visit(Argument *) { return false; }
+ virtual bool visit(TypenameArgument *) { return false; }
+ virtual bool visit(BaseClass *) { return false; }
+ virtual bool visit(ForwardClassDeclaration *) { return false; }
+
+ virtual bool visit(Enum *symbol)
+ { return process(symbol); }
+
+ virtual bool visit(Function *symbol)
+ { return process(symbol); }
+
+ virtual bool visit(Namespace *symbol)
+ { return process(symbol); }
+
+ virtual bool visit(Class *symbol)
+ { return process(symbol); }
+
+ virtual bool visit(Block *symbol)
+ { return process(symbol); }
+
+ // Objective-C
+ virtual bool visit(ObjCBaseClass *) { return false; }
+ virtual bool visit(ObjCBaseProtocol *) { return false; }
+ virtual bool visit(ObjCClass *) { return false; }
+ virtual bool visit(ObjCForwardClassDeclaration *) { return false; }
+ virtual bool visit(ObjCProtocol *) { return false; }
+ virtual bool visit(ObjCForwardProtocolDeclaration *) { return false; }
+ virtual bool visit(ObjCMethod *) { return false; }
+ virtual bool visit(ObjCPropertyDeclaration *) { return false; }
+};
+
class DocumentDiagnosticClient : public DiagnosticClient
{
enum { MAX_MESSAGE_COUNT = 10 };
@@ -313,6 +398,12 @@ void Document::setGlobalNamespace(Namespace *globalNamespace)
_globalNamespace = globalNamespace;
}
+Scope *Document::scopeAt(unsigned line, unsigned column)
+{
+ FindScopeAt findScopeAt(_translationUnit, line, column);
+ return findScopeAt(_globalNamespace);
+}
+
Symbol *Document::findSymbolAt(unsigned line, unsigned column) const
{
return findSymbolAt(line, column, globalSymbols());
@@ -616,7 +707,7 @@ Symbol *Snapshot::findMatchingDefinition(Symbol *symbol) const
}
LookupContext thisContext(thisDocument, *this);
- const QList<Symbol *> declarationCandidates = thisContext.lookup(symbol->name(), symbol);
+ const QList<Symbol *> declarationCandidates = thisContext.lookup(symbol->name(), symbol->scope());
if (declarationCandidates.isEmpty()) {
qWarning() << "unresolved declaration:" << symbol->fileName() << symbol->line() << symbol->column();
return 0;
@@ -644,7 +735,7 @@ Symbol *Snapshot::findMatchingDefinition(Symbol *symbol) const
QList<Function *> viableFunctions;
foreach (Function *fun, result) {
- const QList<Symbol *> declarations = context.lookup(fun->name(), fun);
+ const QList<Symbol *> declarations = context.lookup(fun->name(), fun->scope());
if (declarations.contains(declaration))
viableFunctions.append(fun);