CppTools: findMatchingDefinition handles const and volatile

Strict set to true, SymbolFinder::findMatchingDefinition will now also
check, if const and volatile matches.

Changed return type from 'Symbol *' to 'Function *' since only functions
are returned.

Change-Id: Ib55cb12b6c404e94fcefd0613b964e8caa425690
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@digia.com>
This commit is contained in:
Lorenz Haas
2013-05-14 11:04:38 +02:00
committed by Nikolai Kosjar
parent aa3aa7c455
commit 17a81ae106
4 changed files with 22 additions and 31 deletions

View File

@@ -516,56 +516,44 @@ static InsertionLocation nextToSurroundingDefinitions(Declaration *declaration,
// find the declaration's definition // find the declaration's definition
CppTools::SymbolFinder symbolFinder; CppTools::SymbolFinder symbolFinder;
Symbol *definition = symbolFinder.findMatchingDefinition(surroundingFunctionDecl, Function *definitionFunction = symbolFinder.findMatchingDefinition(surroundingFunctionDecl,
changes.snapshot()); changes.snapshot());
if (!definition) if (!definitionFunction)
return noResult; return noResult;
unsigned line, column; unsigned line, column;
if (suffix.isEmpty()) { if (suffix.isEmpty()) {
Function *definitionFunction = definition->asFunction(); Document::Ptr targetDoc = changes.snapshot().document(QString::fromUtf8(definitionFunction->fileName()));
if (!definitionFunction)
return noResult;
Document::Ptr targetDoc = changes.snapshot().document(QString::fromUtf8(definition->fileName()));
if (!targetDoc) if (!targetDoc)
return noResult; return noResult;
targetDoc->translationUnit()->getPosition(definitionFunction->endOffset(), &line, &column); targetDoc->translationUnit()->getPosition(definitionFunction->endOffset(), &line, &column);
} else { } else {
// we don't have an offset to the start of the function definition, so we need to manually find it... // we don't have an offset to the start of the function definition, so we need to manually find it...
CppRefactoringFilePtr targetFile = changes.file(QString::fromUtf8(definition->fileName())); CppRefactoringFilePtr targetFile = changes.file(QString::fromUtf8(definitionFunction->fileName()));
if (!targetFile->isValid()) if (!targetFile->isValid())
return noResult; return noResult;
FindFunctionDefinition finder(targetFile->cppDocument()->translationUnit()); FindFunctionDefinition finder(targetFile->cppDocument()->translationUnit());
FunctionDefinitionAST *functionDefinition = finder(definition->line(), definition->column()); FunctionDefinitionAST *functionDefinition = finder(definitionFunction->line(), definitionFunction->column());
if (!functionDefinition) if (!functionDefinition)
return noResult; return noResult;
targetFile->cppDocument()->translationUnit()->getTokenStartPosition(functionDefinition->firstToken(), &line, &column); targetFile->cppDocument()->translationUnit()->getTokenStartPosition(functionDefinition->firstToken(), &line, &column);
} }
return InsertionLocation(QString::fromUtf8(definition->fileName()), prefix, suffix, line, column); return InsertionLocation(QString::fromUtf8(definitionFunction->fileName()), prefix, suffix, line, column);
} }
QList<InsertionLocation> InsertionPointLocator::methodDefinition( QList<InsertionLocation> InsertionPointLocator::methodDefinition(Declaration *declaration) const
Declaration *declaration) const
{ {
QList<InsertionLocation> result; QList<InsertionLocation> result;
if (!declaration) if (!declaration)
return result; return result;
CppTools::SymbolFinder symbolFinder; CppTools::SymbolFinder symbolFinder;
if (Symbol *s = symbolFinder.findMatchingDefinition(declaration, if (symbolFinder.findMatchingDefinition(declaration, m_refactoringChanges.snapshot(), true))
m_refactoringChanges.snapshot(),
true)) {
if (Function *f = s->asFunction()) {
if (f->isConst() == declaration->type().isConst()
&& f->isVolatile() == declaration->type().isVolatile())
return result; return result;
}
}
const InsertionLocation location = nextToSurroundingDefinitions(declaration, m_refactoringChanges); const InsertionLocation location = nextToSurroundingDefinitions(declaration, m_refactoringChanges);
if (location.isValid()) { if (location.isValid()) {

View File

@@ -95,8 +95,8 @@ SymbolFinder::SymbolFinder()
{} {}
// strict means the returned symbol has to match exactly, // strict means the returned symbol has to match exactly,
// including argument count and argument types // including argument count, argument types, constness and volatileness.
Symbol *SymbolFinder::findMatchingDefinition(Symbol *declaration, Function *SymbolFinder::findMatchingDefinition(Symbol *declaration,
const Snapshot &snapshot, const Snapshot &snapshot,
bool strict) bool strict)
{ {
@@ -189,10 +189,13 @@ Symbol *SymbolFinder::findMatchingDefinition(Symbol *declaration,
break; break;
} }
if (argIt == argc) if (argIt == argc
&& fun->isConst() == declaration->type().isConst()
&& fun->isVolatile() == declaration->type().isVolatile()) {
best = fun; best = fun;
} }
} }
}
if (strict && ! best) if (strict && ! best)
continue; continue;

View File

@@ -46,7 +46,7 @@ class CPPTOOLS_EXPORT SymbolFinder
public: public:
SymbolFinder(); SymbolFinder();
CPlusPlus::Symbol *findMatchingDefinition(CPlusPlus::Symbol *symbol, CPlusPlus::Function *findMatchingDefinition(CPlusPlus::Symbol *symbol,
const CPlusPlus::Snapshot &snapshot, const CPlusPlus::Snapshot &snapshot,
bool strict = false); bool strict = false);

View File

@@ -257,11 +257,11 @@ static Document::Ptr findDefinition(Function *functionDeclaration, int *line)
if (CppTools::CppModelManagerInterface *cppModelManager = CppTools::CppModelManagerInterface::instance()) { if (CppTools::CppModelManagerInterface *cppModelManager = CppTools::CppModelManagerInterface::instance()) {
const Snapshot snapshot = cppModelManager->snapshot(); const Snapshot snapshot = cppModelManager->snapshot();
CppTools::SymbolFinder symbolFinder; CppTools::SymbolFinder symbolFinder;
if (Symbol *def = symbolFinder.findMatchingDefinition(functionDeclaration, snapshot)) { if (Function *fun = symbolFinder.findMatchingDefinition(functionDeclaration, snapshot)) {
if (line) if (line)
*line = def->line(); *line = fun->line();
return snapshot.document(QString::fromUtf8(def->fileName(), def->fileNameLength())); return snapshot.document(QString::fromUtf8(fun->fileName(), fun->fileNameLength()));
} }
} }