From 46b407f26cf98c494f646609a37d03be4c41e11a Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Wed, 31 Jan 2024 12:59:03 +0900 Subject: Fix SEGV when hovering on symbols after creating a new project. This resolves issue #40. Signed-off-by: Michele Calgaro --- languages/cpp/cppcodecompletion.cpp | 109 ++++++++++++++++++------------------ languages/cpp/cppsupportpart.cpp | 34 ++++++----- 2 files changed, 75 insertions(+), 68 deletions(-) diff --git a/languages/cpp/cppcodecompletion.cpp b/languages/cpp/cppcodecompletion.cpp index 7f97ba1e..d09602c0 100644 --- a/languages/cpp/cppcodecompletion.cpp +++ b/languages/cpp/cppcodecompletion.cpp @@ -2050,14 +2050,13 @@ void CppCodeCompletion::needRecoveryPoints() { if ( this->d->recoveryPoints.isEmpty() ) { kdDebug( 9007 ) << "missing recovery-points for file " << m_activeFileName << " they have to be computed now" << endl; - m_pSupport->backgroundParser() ->lock () - ; + m_pSupport->backgroundParser() ->lock(); std::vector vec; - TranslationUnitAST * ast = *m_pSupport->backgroundParser() ->translationUnit( m_activeFileName ); + ParsedFilePointer pTransUnit = m_pSupport->backgroundParser() ->translationUnit( m_activeFileName ); m_pSupport->backgroundParser() ->unlock(); - if ( !ast ) { + if ( !pTransUnit ) { kdDebug( 9007 ) << "background-parser is missing the translation-unit. The file needs to be reparsed." << endl; m_pSupport->parseFileAndDependencies( m_activeFileName, true ); // m_pSupport->mainWindow() ->statusBar() ->message( i18n( "Background-parser is missing the necessary translation-unit. It will be computed, but this completion will fail." ).arg( m_activeFileName ), 2000 ); @@ -2684,70 +2683,74 @@ void CppCodeCompletion::completeText( bool invokedOnDemand /*= false*/ ) { ///@todo is all this necessary? if ( !recoveredDecl.get() && !recoveredTypeSpec.get() ) { - TranslationUnitAST * ast = *m_pSupport->backgroundParser() ->translationUnit( m_activeFileName ); - if ( AST * node = findNodeAt( ast, line, column ) ) { - kdDebug( 9007 ) << "------------------- AST FOUND --------------------" << endl; - kdDebug( 9007 ) << "node-kind = " << nodeTypeToString( node->nodeType() ) << endl; + ParsedFilePointer pTransUnit = m_pSupport->backgroundParser() ->translationUnit(m_activeFileName); + if (pTransUnit) + { + TranslationUnitAST *ast = *pTransUnit; + if ( AST * node = findNodeAt( ast, line, column ) ) { + kdDebug( 9007 ) << "------------------- AST FOUND --------------------" << endl; + kdDebug( 9007 ) << "node-kind = " << nodeTypeToString( node->nodeType() ) << endl; - if ( FunctionDefinitionAST * def = functionDefinition( node ) ) { - kdDebug( 9007 ) << "------> found a function definition" << endl; + if ( FunctionDefinitionAST * def = functionDefinition( node ) ) { + kdDebug( 9007 ) << "------> found a function definition" << endl; - int startLine, startColumn; - def->getStartPosition( &startLine, &startColumn ); + int startLine, startColumn; + def->getStartPosition( &startLine, &startColumn ); - TQString contents = getText( startLine, startColumn, line, showArguments ? nCol : column ); + TQString contents = getText( startLine, startColumn, line, showArguments ? nCol : column ); - /// @todo remove code duplication - int start_expr = expressionAt( contents, contents.length() ); + /// @todo remove code duplication + int start_expr = expressionAt( contents, contents.length() ); - // kdDebug(9007) << "start_expr = " << start_expr << endl; - if ( start_expr != int( contents.length() ) ) - expr = contents.mid( start_expr, contents.length() - start_expr ).stripWhiteSpace(); + // kdDebug(9007) << "start_expr = " << start_expr << endl; + if ( start_expr != int( contents.length() ) ) + expr = contents.mid( start_expr, contents.length() - start_expr ).stripWhiteSpace(); - if ( expr.startsWith( "TQ_SIGNAL" ) || expr.startsWith( "TQ_SLOT" ) ) { - m_completionMode = expr.startsWith( "TQ_SIGNAL" ) ? SignalCompletion : SlotCompletion; + if ( expr.startsWith( "TQ_SIGNAL" ) || expr.startsWith( "TQ_SLOT" ) ) { + m_completionMode = expr.startsWith( "TQ_SIGNAL" ) ? SignalCompletion : SlotCompletion; - showArguments = false; - int end_expr = start_expr - 1; - while ( end_expr > 0 && contents[ end_expr ].isSpace() ) - --end_expr; + showArguments = false; + int end_expr = start_expr - 1; + while ( end_expr > 0 && contents[ end_expr ].isSpace() ) + --end_expr; - if ( contents[ end_expr ] != ',' ) { - expr = TQString(); + if ( contents[ end_expr ] != ',' ) { + expr = TQString(); + } else { + start_expr = expressionAt( contents, end_expr ); + expr = contents.mid( start_expr, end_expr - start_expr ).stripWhiteSpace(); + } } else { - start_expr = expressionAt( contents, end_expr ); - expr = contents.mid( start_expr, end_expr - start_expr ).stripWhiteSpace(); - } - } else { - int idx = expr.length() - 1; - while ( expr[ idx ].isLetterOrNumber() || expr[ idx ] == '_' ) - --idx; - - if ( idx != int( expr.length() ) - 1 ) { - ++idx; - word = expr.mid( idx ).stripWhiteSpace(); - expr = expr.left( idx ).stripWhiteSpace(); + int idx = expr.length() - 1; + while ( expr[ idx ].isLetterOrNumber() || expr[ idx ] == '_' ) + --idx; + + if ( idx != int( expr.length() ) - 1 ) { + ++idx; + word = expr.mid( idx ).stripWhiteSpace(); + expr = expr.left( idx ).stripWhiteSpace(); + } } - } - ctx = computeContext( def, line, column, startLine, startColumn ); + ctx = computeContext( def, line, column, startLine, startColumn ); - TQStringList scope; - scopeOfNode( def, scope ); - this_type = SimpleType( scope, getIncludeFiles() ); + TQStringList scope; + scopeOfNode( def, scope ); + this_type = SimpleType( scope, getIncludeFiles() ); - if ( scope.size() ) { /* - SimpleVariable var; - var.type = scope; - var.name = "this"; - ctx->add( var );*/ - //kdDebug(9007) << "add variable " << var.name << " with type " << var.type << endl; - } + if ( scope.size() ) { /* + SimpleVariable var; + var.type = scope; + var.name = "this"; + ctx->add( var );*/ + //kdDebug(9007) << "add variable " << var.name << " with type " << var.type << endl; + } - ExpressionInfo exp( expr ); - exp.t = ( ExpressionInfo::Type ) ( ExpressionInfo::NormalExpression | ExpressionInfo::TypeExpression ); - type = evaluateExpression( exp, ctx ); + ExpressionInfo exp( expr ); + exp.t = ( ExpressionInfo::Type ) ( ExpressionInfo::NormalExpression | ExpressionInfo::TypeExpression ); + type = evaluateExpression( exp, ctx ); + } } } } diff --git a/languages/cpp/cppsupportpart.cpp b/languages/cpp/cppsupportpart.cpp index 01b34a43..c94539de 100644 --- a/languages/cpp/cppsupportpart.cpp +++ b/languages/cpp/cppsupportpart.cpp @@ -1699,25 +1699,29 @@ void CppSupportPart::slotNeedTextHint( int line, int column, TQString& textHint return ; m_backgroundParser->lock(); - TranslationUnitAST* ast = *m_backgroundParser->translationUnit( m_activeFileName ); - AST* node = 0; - if ( ast && ( node = findNodeAt( ast, line, column ) ) ) + ParsedFilePointer pTransUnit = m_backgroundParser->translationUnit(m_activeFileName); + if (pTransUnit) { + TranslationUnitAST* ast = *pTransUnit; + AST* node = 0; + if ( ast && ( node = findNodeAt( ast, line, column ) ) ) + { - while ( node && node->nodeType() != NodeType_FunctionDefinition ) - node = node->parent(); + while ( node && node->nodeType() != NodeType_FunctionDefinition ) + node = node->parent(); - if ( node ) - { - int startLine, startColumn; - int endLine, endColumn; - node->getStartPosition( &startLine, &startColumn ); - node->getEndPosition( &endLine, &endColumn ); + if ( node ) + { + int startLine, startColumn; + int endLine, endColumn; + node->getStartPosition( &startLine, &startColumn ); + node->getEndPosition( &endLine, &endColumn ); - if ( !node->text().isNull() ) - textHint = node->text(); - else - textHint = m_activeEditor->textLine( startLine ).simplifyWhiteSpace(); + if ( !node->text().isNull() ) + textHint = node->text(); + else + textHint = m_activeEditor->textLine( startLine ).simplifyWhiteSpace(); + } } } m_backgroundParser->unlock(); -- cgit v1.2.1