Commit fa6c3cc1 authored by Christian Kamm's avatar Christian Kamm
Browse files

C++ function signature: Automatically rename argument uses in body.



Task-number: QTCREATORBUG-6132
Change-Id: I7c21e648cfc2e85803a3bf4df0a615a1e276f96a
Reviewed-by: default avatarLeandro Melo <leandro.melo@nokia.com>
parent a0729416
......@@ -34,6 +34,7 @@
#include "cppeditor.h"
#include "cppquickfixassistant.h"
#include "cpplocalsymbols.h"
#include <cplusplus/CppRewriter.h>
#include <cplusplus/ASTPath.h>
......@@ -757,6 +758,7 @@ Utils::ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot, int targ
// build the new parameter declarations
QString newTargetParameters;
bool hadChanges = newParamCount < existingParamCount; // below, additions and changes set this to true as well
QHash<Symbol *, QString> renamedTargetParameters;
for (int newParamIndex = 0; newParamIndex < newParamCount; ++newParamIndex) {
const int existingParamIndex = newParamToSourceParam[newParamIndex];
Symbol *newParam = newFunction->argumentAt(newParamIndex);
......@@ -806,6 +808,10 @@ Utils::ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot, int targ
targetFunctionDeclarator, existingParamIndex))
replacementName = 0;
// track renames
if (replacementName != targetParam->name() && replacementName)
renamedTargetParameters[targetParam] = overview(replacementName);
// need to change the type (and name)?
if (!newParam->type().isEqualTo(sourceParam->type())
&& !newParam->type().isEqualTo(targetParam->type())) {
......@@ -885,6 +891,25 @@ Utils::ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot, int targ
targetFile->startOf(targetFunctionDeclarator->rparen_token),
newTargetParameters);
}
// for function definitions, rename the local usages
FunctionDefinitionAST *targetDefinition = targetDeclaration->asFunctionDefinition();
if (targetDefinition && !renamedTargetParameters.isEmpty()) {
const LocalSymbols localSymbols(targetFile->cppDocument(), targetDefinition);
const int endOfArguments = targetFile->endOf(targetFunctionDeclarator->rparen_token);
QHashIterator<Symbol *, QString> it(renamedTargetParameters);
while (it.hasNext()) {
it.next();
const QList<SemanticInfo::Use> &uses = localSymbols.uses.value(it.key());
foreach (const SemanticInfo::Use &use, uses) {
const int useStart = targetFile->position(use.line, use.column);
if (useStart <= endOfArguments)
continue;
changes.replace(useStart, useStart + use.length, it.value());
}
}
}
}
// sync cv qualification
......
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