diff options
author | Nikolai Kosjar <nikolai.kosjar@digia.com> | 2013-06-07 17:42:44 +0200 |
---|---|---|
committer | Nikolai Kosjar <nikolai.kosjar@digia.com> | 2013-06-11 16:32:24 +0200 |
commit | 546d297856726974f5eaef50835d0a4d7ef5c15f (patch) | |
tree | 2c84628853e5521be9396868c167a5a355116804 | |
parent | 50a900e509be2e1f448f29be126ba3b7f8173901 (diff) | |
download | qt-creator-546d297856726974f5eaef50835d0a4d7ef5c15f.tar.gz |
CppTools: Handle operator functions when reformatting */& declarations
Respect also whitespace within the operator names (e.g. "operator=" vs
"operator =").
Change-Id: Ibdfc77e9eebf7e3db31ccce7d8959b226dcda765
Reviewed-by: Christian Kandeler <christian.kandeler@digia.com>
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
3 files changed, 33 insertions, 8 deletions
diff --git a/src/plugins/cpptools/cpppointerdeclarationformatter.cpp b/src/plugins/cpptools/cpppointerdeclarationformatter.cpp index 757f08f5a3..bb4291b5e7 100644 --- a/src/plugins/cpptools/cpppointerdeclarationformatter.cpp +++ b/src/plugins/cpptools/cpppointerdeclarationformatter.cpp @@ -216,7 +216,7 @@ bool PointerDeclarationFormatter::visit(SimpleDeclarationAST *ast) range.end = lastActivationToken; - checkAndRewrite(symbol, range, charactersToRemove); + checkAndRewrite(declarator, symbol, range, charactersToRemove); } return true; } @@ -250,7 +250,7 @@ bool PointerDeclarationFormatter::visit(FunctionDefinitionAST *ast) CHECK_RV(foundBegin, "Declaration without attributes not supported", true); TokenRange range(firstActivationToken, lastActivationToken); - checkAndRewrite(symbol, range); + checkAndRewrite(declarator, symbol, range); return true; } @@ -271,7 +271,7 @@ bool PointerDeclarationFormatter::visit(ParameterDeclarationAST *ast) : ast->lastToken() - 1; TokenRange range(ast->firstToken(), lastActivationToken); - checkAndRewrite(symbol, range); + checkAndRewrite(declarator, symbol, range); return true; } @@ -296,7 +296,7 @@ bool PointerDeclarationFormatter::visit(ForeachStatementAST *ast) : declarator->lastToken() - 1; TokenRange range(firstSpecifier->firstToken(), lastActivationToken); - checkAndRewrite(symbol, range); + checkAndRewrite(declarator, symbol, range); return true; } @@ -359,7 +359,7 @@ void PointerDeclarationFormatter::processIfWhileForStatement(ExpressionAST *expr // Specify activation range TokenRange range(condition->firstToken(), declarator->equal_token - 1); - checkAndRewrite(symbol, range); + checkAndRewrite(declarator, symbol, range); } /*! @@ -369,7 +369,9 @@ void PointerDeclarationFormatter::processIfWhileForStatement(ExpressionAST *expr \param symbol the symbol to be rewritten \param range the substitution range in the file */ -void PointerDeclarationFormatter::checkAndRewrite(Symbol *symbol, TokenRange tokenRange, +void PointerDeclarationFormatter::checkAndRewrite(DeclaratorAST *declarator, + Symbol *symbol, + TokenRange tokenRange, unsigned charactersToRemove) { CHECK_R(tokenRange.end > 0, "TokenRange invalid1"); @@ -408,7 +410,24 @@ void PointerDeclarationFormatter::checkAndRewrite(Symbol *symbol, TokenRange tok || originalDeclaration.contains(QLatin1Char('*')), "No pointer or references"); // Does the rewritten declaration (part) differs from the original source (part)? - QString rewrittenDeclaration = rewriteDeclaration(type, symbol->name()); + QString rewrittenDeclaration; + const Name *name = symbol->name(); + if (name) { + if (name->isOperatorNameId()) { + // Take the operator name from the file instead from the AST, so the white + // spaces within the operator names can be respected, e.g. in "operator =". + const QByteArray operatorText + = m_cppRefactoringFile->textOf(declarator->core_declarator).toLatin1(); + Identifier operatorName(operatorText.constData(), operatorText.size()); + rewrittenDeclaration = rewriteDeclaration(type, &operatorName); + } else { + rewrittenDeclaration = rewriteDeclaration(type, name); + } + } else { + // The declaration will be correctly rewritten for name == 0 (e.g. "int *"). + rewrittenDeclaration = rewriteDeclaration(type, name); + } + rewrittenDeclaration.remove(0, charactersToRemove); CHECK_R(originalDeclaration != rewrittenDeclaration, "Rewritten is same as original"); diff --git a/src/plugins/cpptools/cpppointerdeclarationformatter.h b/src/plugins/cpptools/cpppointerdeclarationformatter.h index 39660f78ef..e8af400435 100644 --- a/src/plugins/cpptools/cpppointerdeclarationformatter.h +++ b/src/plugins/cpptools/cpppointerdeclarationformatter.h @@ -110,7 +110,8 @@ private: }; void processIfWhileForStatement(ExpressionAST *expression, Symbol *symbol); - void checkAndRewrite(Symbol *symbol, TokenRange range, unsigned charactersToRemove = 0); + void checkAndRewrite(DeclaratorAST *declarator, Symbol *symbol, TokenRange range, + unsigned charactersToRemove = 0); QString rewriteDeclaration(FullySpecifiedType type, const Name *name) const; void printCandidate(AST *ast); diff --git a/src/plugins/cpptools/cpppointerdeclarationformatter_test.cpp b/src/plugins/cpptools/cpppointerdeclarationformatter_test.cpp index 70d360e4e2..474a829ac5 100644 --- a/src/plugins/cpptools/cpppointerdeclarationformatter_test.cpp +++ b/src/plugins/cpptools/cpppointerdeclarationformatter_test.cpp @@ -347,6 +347,11 @@ void CppToolsPlugin::test_format_pointerdeclaration_in_simpledeclarations_data() source = QLatin1String("@char bla;"); // Two spaces to get sure nothing is reformatted. QTest::newRow("precondition-fail-no-pointer") << source << stripCursor(source); + + // Respect white space within operator names + QTest::newRow("operators") + << "class C { C@&operator = (const C &); };" + << "class C { C & operator = (const C &); };"; } void CppToolsPlugin::test_format_pointerdeclaration_in_controlflowstatements() |