在开发 Qt 应用程序时,vscode 中的 clangd 代码补全给出了错误的提示

问题描述 投票:0回答:1

我在linux下使用VSCodium和gcc编译器开发c++,已经安装了clangd服务器和clangd vscode插件。我的示例代码如下所示。

QByteArray bytearray;
configFile.open(QIODevice::WriteOnly); //configFile is a QFile
configFile.write(bytearray);

Clangd 不会将

open()
函数标记为不正确,但它告诉我对
write()
的调用不正确,并显示以下错误消息。但是,我的项目构建成功了。

No matching member function for call to 'write'clang(ovl_no_viable_member_function_in_call)
qiodevice.h(90, 12): Candidate function not viable: no known conversion from 'QFile' to 'QIODevice' for object argument
qiodevice.h(89, 12): Candidate function not viable: no known conversion from 'QFile' to 'QIODevice' for object argument
qiodevice.h(88, 12): Candidate function not viable: requires 2 arguments, but 1 was provided

QFile类继承了QFileDevice,而QFileDevice又继承了QIODevice,所以

write()
函数也应该存在于QFile中。但 clangd 似乎不识别该功能。 在
qiodevice.h

qint64 write(const char *data, qint64 len);
qint64 write(const char *data);
qint64 write(const QByteArray &data);

我的CMakeLists.txt

cmake_minimum_required(VERSION 3.21)
project(ClevoFanControl VERSION 2.0.4 LANGUAGES CXX)

set(CMAKE_BUILD_TYPE "Debug")

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS on)

include_directories("include")

IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
include_directories("/home/main/Applications/Qt/6.7.2/gcc_64/include")
set(CMAKE_PREFIX_PATH "/home/main/Applications/Qt/6.7.2/gcc_64/lib/cmake")
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Windows")
include_directories("D:\\Applications\\Qt\\6.7.2\\mingw_64\\include")
set(CMAKE_PREFIX_PATH "D:\\Applications\\Qt\\6.7.2\\mingw_64\\lib\\cmake")
set(CMAKE_C_COMPILER "D:\\Applications\\Qt\\Tools\\mingw1120_64\\bin\\gcc.exe") #using custom gcc can cause error
set(CMAKE_CXX_COMPILER "D:\\Applications\\Qt\\Tools\\mingw1120_64\\bin\\g++.exe")
if(CMAKE_BUILD_TYPE MATCHES "Release")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mwindows")
endif()
ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux")

if(CMAKE_BUILD_TYPE MATCHES "Release")
add_definitions(-DQT_NO_DEBUG_OUTPUT)
endif()

find_package(Qt6 REQUIRED COMPONENTS Core Widgets Gui)
add_executable(ClevoFanControl main.cpp ClevoFanControl.cpp ClevoFanControl.qrc ClevoFanControl.rc config.ui monitor.ui)
target_link_libraries(ClevoFanControl PRIVATE Qt6::Core Qt6::Widgets Qt6::Gui)

clangd 日志

I[17:40:57.700] Debian clangd version 14.0.6
I[17:40:57.700] Features: linux+grpc
I[17:40:57.700] PID: 61201
I[17:40:57.700] Working directory: /home/main/User/Program/DevelopingProjects/ClevoFanControl
I[17:40:57.700] argv[0]: /usr/bin/clangd
I[17:40:57.700] Starting LSP over stdin/stdout
I[17:40:57.700] <-- initialize(0)
I[17:40:57.701] --> reply:initialize(0) 0 ms
I[17:40:57.703] <-- initialized
I[17:40:57.709] <-- textDocument/didOpen
I[17:40:57.709] --> textDocument/publishDiagnostics
I[17:40:57.710] Loaded compilation database from /home/main/User/Program/DevelopingProjects/ClevoFanControl/build/compile_commands.json
I[17:40:57.710] ASTWorker building file /home/main/User/Program/DevelopingProjects/ClevoFanControl/ClevoFanControl.cpp version 1 with command 
[/home/main/User/Program/DevelopingProjects/ClevoFanControl/build]
/usr/bin/g++ --driver-mode=g++ -DQT_CORE_LIB -DQT_GUI_LIB -DQT_WIDGETS_LIB -I/home/main/User/Program/DevelopingProjects/ClevoFanControl/build -I/home/main/User/Program/DevelopingProjects/ClevoFanControl -I/home/main/User/Program/DevelopingProjects/ClevoFanControl/build/ClevoFanControl_autogen/include -I/home/main/User/Program/DevelopingProjects/ClevoFanControl/include -isystem /home/main/Applications/Qt/6.7.2/gcc_64/include -isystem /home/main/Applications/Qt/6.7.2/gcc_64/include/QtCore -isystem /home/main/Applications/Qt/6.7.2/gcc_64/mkspecs/linux-g++ -isystem /home/main/Applications/Qt/6.7.2/gcc_64/include/QtWidgets -isystem /home/main/Applications/Qt/6.7.2/gcc_64/include/QtGui -g -fPIC -o CMakeFiles/ClevoFanControl.dir/ClevoFanControl.cpp.o -c -resource-dir=/usr/lib/llvm-14/lib/clang/14.0.6 -- /home/main/User/Program/DevelopingProjects/ClevoFanControl/ClevoFanControl.cpp
I[17:40:57.710] Loaded compilation database from /home/main/User/Program/DevelopingProjects/ClevoFanControl/build/compile_commands.json
I[17:40:57.710] --> window/workDoneProgress/create(0)
I[17:40:57.710] Enqueueing 2 commands for indexing
I[17:40:57.712] --> textDocument/clangd.fileStatus
I[17:40:57.712] <-- reply(0)
I[17:40:57.712] --> $/progress
I[17:40:57.712] --> $/progress
I[17:40:58.090] --> $/progress
I[17:40:58.090] --> $/progress
I[17:40:58.160] <-- textDocument/documentLink(1)
I[17:40:58.161] <-- clangd/inlayHints(2)
I[17:40:58.396] <-- $/cancelRequest
I[17:40:58.408] <-- textDocument/documentLink(3)
I[17:40:58.451] <-- $/setTrace
I[17:40:58.452] unhandled notification $/setTrace
I[17:40:58.452] <-- textDocument/documentSymbol(4)
I[17:40:58.452] <-- textDocument/semanticTokens/full(5)
I[17:40:58.494] <-- textDocument/codeAction(6)
I[17:40:58.559] <-- $/setTrace
I[17:40:58.559] unhandled notification $/setTrace
I[17:40:58.816] <-- $/cancelRequest
I[17:40:58.817] <-- textDocument/codeAction(7)
I[17:40:58.818] <-- $/cancelRequest
I[17:40:58.818] <-- textDocument/codeAction(8)
I[17:40:59.447] <-- $/cancelRequest
I[17:40:59.448] <-- textDocument/codeAction(9)
I[17:40:59.449] <-- textDocument/documentSymbol(10)
I[17:40:59.477] <-- $/setTrace
I[17:40:59.477] unhandled notification $/setTrace
I[17:40:59.874] --> workspace/semanticTokens/refresh(1)
I[17:40:59.874] --> textDocument/clangd.fileStatus
I[17:40:59.875] <-- reply(1)
I[17:40:59.969] --> textDocument/publishDiagnostics
I[17:40:59.971] --> reply:textDocument/documentLink(1) 1810 ms, error: Task was cancelled.
[Error - 17:40:59] Request textDocument/documentLink failed.
[object Object]
I[17:40:59.972] --> reply:clangd/inlayHints(2) 1811 ms
I[17:40:59.972] --> reply:textDocument/documentLink(3) 1563 ms
I[17:40:59.973] --> reply:textDocument/documentSymbol(4) 1521 ms
I[17:40:59.974] --> reply:textDocument/semanticTokens/full(5) 1522 ms
I[17:40:59.975] --> reply:textDocument/codeAction(6) 1481 ms, error: Task was cancelled.
I[17:40:59.975] --> reply:textDocument/codeAction(7) 1157 ms, error: Task was cancelled.
I[17:40:59.975] --> reply:textDocument/codeAction(8) 1156 ms, error: Task was cancelled.
I[17:40:59.975] --> reply:textDocument/codeAction(9) 527 ms
I[17:40:59.975] --> reply:textDocument/documentSymbol(10) 526 ms
I[17:40:59.975] --> textDocument/clangd.fileStatus
[Error - 17:40:59] Request textDocument/codeAction failed.
[object Object]
[Error - 17:40:59] Request textDocument/codeAction failed.
[object Object]
[Error - 17:40:59] Request textDocument/codeAction failed.
[object Object]
I[17:41:00.227] <-- textDocument/codeAction(11)
I[17:41:00.227] --> reply:textDocument/codeAction(11) 0 ms
I[17:41:00.227] --> textDocument/clangd.fileStatus
I[17:41:01.654] <-- textDocument/semanticTokens/full/delta(12)
I[17:41:01.657] --> reply:textDocument/semanticTokens/full/delta(12) 3 ms
I[17:41:01.658] --> textDocument/clangd.fileStatus


操作系统:debian12 Qt:6.7.2 海湾合作委员会:12.2.0

我包括了

qiodevice.h
qfiledevice.h
但没有任何改变。

我希望 clangd 语言服务器给出正确的消息,请帮助我,谢谢。

linux qt gcc clangd
1个回答
0
投票

我终于通过将 clangd 从 v14 更新到 v16 解决了这个问题。看来debian12的软件包

clangd
对我不起作用,而
clangd-16
却很好用。

© www.soinside.com 2019 - 2024. All rights reserved.