CMakePM: Fix crash in findLinkAt

Fixes: QTCREATORBUG-29715
Change-Id: I4db919c8858631beb573789d1888b3dbee606c50
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Cristian Adam
2023-10-06 17:55:57 +02:00
parent ad680902b2
commit 399e12c973

View File

@@ -310,7 +310,7 @@ void CMakeEditorWidget::findLinkAt(const QTextCursor &cursor,
if (auto project = ProjectTree::currentProject()) { if (auto project = ProjectTree::currentProject()) {
buffer.replace("${CMAKE_SOURCE_DIR}", project->projectDirectory().path()); buffer.replace("${CMAKE_SOURCE_DIR}", project->projectDirectory().path());
if (auto bs = ProjectTree::currentBuildSystem()) { if (auto bs = ProjectTree::currentBuildSystem(); bs->buildConfiguration()) {
buffer.replace("${CMAKE_BINARY_DIR}", bs->buildConfiguration()->buildDirectory().path()); buffer.replace("${CMAKE_BINARY_DIR}", bs->buildConfiguration()->buildDirectory().path());
// Get the path suffix from current source dir to project source dir and apply it // Get the path suffix from current source dir to project source dir and apply it
@@ -327,35 +327,36 @@ void CMakeEditorWidget::findLinkAt(const QTextCursor &cursor,
.path()); .path());
// Check if the symbols is a user defined function or macro // Check if the symbols is a user defined function or macro
const CMakeBuildSystem *cbs = static_cast<const CMakeBuildSystem *>(bs); if (const auto cbs = qobject_cast<const CMakeBuildSystem *>(bs)) {
// Strip variable coating // Strip variable coating
if (buffer.startsWith("${") && buffer.endsWith("}")) if (buffer.startsWith("${") && buffer.endsWith("}"))
buffer = buffer.mid(2, buffer.size() - 3); buffer = buffer.mid(2, buffer.size() - 3);
if (cbs->cmakeSymbolsHash().contains(buffer)) { if (cbs->cmakeSymbolsHash().contains(buffer)) {
link = cbs->cmakeSymbolsHash().value(buffer); link = cbs->cmakeSymbolsHash().value(buffer);
addTextStartEndToLink(link); addTextStartEndToLink(link);
return processLinkCallback(link); return processLinkCallback(link);
} }
// Handle include(CMakeFileWithoutSuffix) and find_package(Package) // Handle include(CMakeFileWithoutSuffix) and find_package(Package)
QString functionName; QString functionName;
if (funcStart > funcEnd) { if (funcStart > funcEnd) {
int funcStartPos = findWordStart(funcStart); int funcStartPos = findWordStart(funcStart);
functionName = textDocument()->textAt(funcStartPos, funcStart - funcStartPos); functionName = textDocument()->textAt(funcStartPos, funcStart - funcStartPos);
struct FunctionToHash struct FunctionToHash
{ {
QString functionName; QString functionName;
const QHash<QString, Utils::Link> &hash; const QHash<QString, Utils::Link> &hash;
} functionToHashes[] = {{"include", cbs->dotCMakeFilesHash()}, } functionToHashes[] = {{"include", cbs->dotCMakeFilesHash()},
{"find_package", cbs->findPackagesFilesHash()}}; {"find_package", cbs->findPackagesFilesHash()}};
for (const auto &pair : functionToHashes) { for (const auto &pair : functionToHashes) {
if (functionName == pair.functionName && pair.hash.contains(buffer)) { if (functionName == pair.functionName && pair.hash.contains(buffer)) {
link = pair.hash.value(buffer); link = pair.hash.value(buffer);
addTextStartEndToLink(link); addTextStartEndToLink(link);
return processLinkCallback(link); return processLinkCallback(link);
}
} }
} }
} }