CppEditor: Fix "add definition" quickfix insertion string

... for the edge case of the insertion location being preceded by a
function whose body is a try-catch clause.

Fixes: QTCREATORBUG-14661
Change-Id: Icc7d5283ead81d644231479317214a8cc177493d
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2021-01-28 10:27:12 +01:00
parent ad09faab4e
commit 667d50e1bc
3 changed files with 49 additions and 0 deletions

View File

@@ -154,6 +154,7 @@ private slots:
void test_quickfix_InsertDefFromDecl_macroUsesAtEndOfFile2(); void test_quickfix_InsertDefFromDecl_macroUsesAtEndOfFile2();
void test_quickfix_InsertDefFromDecl_erroneousStatementAtEndOfFile(); void test_quickfix_InsertDefFromDecl_erroneousStatementAtEndOfFile();
void test_quickfix_InsertDefFromDecl_rvalueReference(); void test_quickfix_InsertDefFromDecl_rvalueReference();
void test_quickfix_InsertDefFromDecl_functionTryBlock();
void test_quickfix_InsertDefFromDecl_findImplementationFile(); void test_quickfix_InsertDefFromDecl_findImplementationFile();
void test_quickfix_InsertDefFromDecl_unicodeIdentifier(); void test_quickfix_InsertDefFromDecl_unicodeIdentifier();
void test_quickfix_InsertDefFromDecl_templateClass(); void test_quickfix_InsertDefFromDecl_templateClass();

View File

@@ -4283,6 +4283,45 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_rvalueReference()
QuickFixOperationTest(testDocuments, &factory); QuickFixOperationTest(testDocuments, &factory);
} }
void CppEditorPlugin::test_quickfix_InsertDefFromDecl_functionTryBlock()
{
QList<QuickFixTestDocument::Ptr> testDocuments;
QByteArray original;
QByteArray expected;
// Header File
original = R"(
struct Foo {
void tryCatchFunc();
void @otherFunc();
};
)";
expected = original;
testDocuments << QuickFixTestDocument::create("file.h", original, expected);
// Source File
original = R"(
#include "file.h"
void Foo::tryCatchFunc() try {} catch (...) {}
)";
expected = R"(
#include "file.h"
void Foo::tryCatchFunc() try {} catch (...) {}
void Foo::otherFunc()
{
}
)";
testDocuments << QuickFixTestDocument::create("file.cpp", original, expected);
InsertDefFromDecl factory;
QuickFixOperationTest(testDocuments, &factory);
}
/// Find right implementation file. (QTCREATORBUG-10728) /// Find right implementation file. (QTCREATORBUG-10728)
void CppEditorPlugin::test_quickfix_InsertDefFromDecl_findImplementationFile() void CppEditorPlugin::test_quickfix_InsertDefFromDecl_findImplementationFile()
{ {

View File

@@ -3111,6 +3111,15 @@ public:
// make target lookup context // make target lookup context
Document::Ptr targetDoc = targetFile->cppDocument(); Document::Ptr targetDoc = targetFile->cppDocument();
Scope *targetScope = targetDoc->scopeAt(loc.line(), loc.column()); Scope *targetScope = targetDoc->scopeAt(loc.line(), loc.column());
// Correct scope in case of a function try-block. See QTCREATORBUG-14661.
if (targetScope && targetScope->asBlock()) {
if (Class * const enclosingClass = targetScope->enclosingClass())
targetScope = enclosingClass;
else
targetScope = targetScope->enclosingNamespace();
}
LookupContext targetContext(targetDoc, op->snapshot()); LookupContext targetContext(targetDoc, op->snapshot());
ClassOrNamespace *targetCoN = targetContext.lookupType(targetScope); ClassOrNamespace *targetCoN = targetContext.lookupType(targetScope);
if (!targetCoN) if (!targetCoN)