Author Topic: Code completion using LSP and clangd  (Read 83429 times)

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2574
Re: Code completion using LSP and clangd
« Reply #300 on: March 20, 2023, 05:57:07 am »
since I build and installed clang16, the plug-in keep asking to specify the path to clangd, though it is in the same location as before, and even explicitly specifying the path does not help.


> which clangd
/opt/llvm/bin/clangd

> clangd --version
clangd version 16.0.0 (https://github.com/llvm/llvm-project.git 08d094a0e457360ad8b94b017d2dc277e697ca76)
Features: linux
Platform: x86_64-unknown-linux-gnu

Tell me how you installed it so I can do the same and reproduce the error.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5440
Re: Code completion using LSP and clangd
« Reply #301 on: March 20, 2023, 05:06:03 pm »
I build it like this :

sudo rm -rf /opt/llvmTemp
rm -rf llvm-project
git clone --branch llvmorg-16.0.0 --depth=1 https://github.com/llvm/llvm-project.git

cd llvm-project
sed -i 's/set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default ON)/set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default OFF)/g' ./llvm/runtimes/CMakeLists.txt

mkdir build
cd build

cmake -G Ninja -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/opt/llvmTemp -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld;lldb;openmp" -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt" ../llvm

ninja
sudo ninja install

#sudo 'echo "/opt/llvm/lib" > /etc/ld.so.conf.d/clanglib.conf' \
sudo ldconfig





note the echo line, you need to do this at least once, and off course also add /opt/llvm/bin yo your  PATH

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2574
Re: Code completion using LSP and clangd
« Reply #302 on: March 21, 2023, 06:14:30 pm »
since I build and installed clang16, the plug-in keep asking to specify the path to clangd, though it is in the same location as before, and even explicitly specifying the path does not help.


> which clangd
/opt/llvm/bin/clangd

> clangd --version
clangd version 16.0.0 (https://github.com/llvm/llvm-project.git 08d094a0e457360ad8b94b017d2dc277e697ca76)
Features: linux
Platform: x86_64-unknown-linux-gnu

Fixed svn Head rev 13241.

@Killerbot
Thanks for the instructions.
the fix works for me using Windows sublayer for Linux(WSL). Let us know if it works in your environment.
The problem was: The clangd version (16.0.0) no longer matches the clangd resources lib name (...\lib\clang\16\).
The version number used to be appended to the lib dir name.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5440
Re: Code completion using LSP and clangd
« Reply #303 on: March 22, 2023, 06:23:41 pm »
confirmed solved,
and indeed on my 15.0.7 the dir is called /opt/llvm/lib/clang/15.0.7/

let's see when 16.0.1 comes ....

Offline Trigger

  • Single posting newcomer
  • *
  • Posts: 2
Re: Code completion using LSP and clangd
« Reply #304 on: May 09, 2023, 06:05:26 pm »
Hi!

I've recently started to work with SMT32 microcontrollers, and I discovered that standard Code::Blocks's code completion plugin doesn't work very well in some specific cases like this one:
Code
#define GPIOA               ((GPIO_TypeDef *) GPIOA_BASE)
GPIOA_BASE is not recognized correctly as a pointer to the structure of type GPIO_TypeDef, and Code::Blocks doesn't give any autocomplete suggestions after I type "GPIOA_BASE->" (see https://forums.codeblocks.org/index.php/topic,21823.0.html). So I decided to try this plugin.

So I installed the latest nightly build (06 May 2023 build (13268)) of C::B and clangd 16.0.2. Then I disabled the original code completion plugin, enabled clang_client and set the path to the clang executable. But when I tried to open my STM32 project, I got an error message (see the attached screenshot). It is obvious that there was some encoding problem caused by Cyrillic characters in the project path. Usually in such cases I create symbolic links/directory junctions with appropriate English-only directory names, but in this case I can't fix the problem this way because clangd doesn't work great with symlinks.

I decided to download the sources of C::B and plugins and try to fix the problem myself. And I think I managed to get it to work correctly. I've changed the file plugins/contrib/clangd_client/src/LSPclient/client.cpp. Here is the diff output:
Code
Index: client.cpp
===================================================================
--- client.cpp (revision 13268)
+++ client.cpp (working copy)
@@ -1890,7 +1890,7 @@
         writeClientLog(StdString_Format("<<< Initialize(): %s", stdDirName.c_str()) );
 
         // Set the project folder as the folder containing the commands file. //(ollydbg 2022/10/19) Ticket #75
-        try { Initialize(string_ref(fileUtils.FilePathToURI(dirname)), string_ref(dirname.ToUTF8())); } //(ollydbg 2022/10/19) ticket #75
+        try { Initialize(string_ref(fileUtils.FilePathToURI(dirname.ToUTF8())), string_ref(dirname.ToUTF8())); } //(ollydbg 2022/10/19) ticket #75
         catch(std::exception &err)
         {
             //printf("read error -> %s\nread -> %s\n ", e.what(), read.c_str());

I'm not sure if I've done it right, but it seems that the plugin is working fine. Maybe this information will be helpful for the further development of the clangd-client plugin.

Best regards,
Arseniy.

P.S. Thank you @Pecan and the other developers who works on this plugin. It's a great idea to integrate C::B with clangd!
« Last Edit: May 09, 2023, 08:05:21 pm by Trigger »

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2574
Re: Code completion using LSP and clangd
« Reply #305 on: May 11, 2023, 08:23:30 pm »
@ Trigger
Your fix applied in head rev 13271. Thanks.

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2574
Re: Code completion using LSP and clangd
« Reply #306 on: Yesterday at 06:26:06 pm »
Continuation of discussion from:
https://forums.codeblocks.org/index.php/topic,25383.msg173164.html#msg173164

Can you write a short example that we can use to re-create your problems?
For me, clangd_client does indeed provide the info you are having problems with.
With an example from you, we might be able to either show you how to get the info, or fix the problem.

My os is win7 , clangd.exe is  from latest version LLVM-16.0.5-win64 which  full installed ,compiler is winlibs-x86_64-mcf-seh-gcc-13.1.0-mingw-w64ucrt-11.0.0-r1,no msys . In CB , compiler settings is right  ,  search directories  is right .

My Project to test  clangd_client  is simplest  "hello world" , add several varialbes and simple classes , the program is normally compiling and running . clangd_client can jump to class( I defines myself) declaration and implementation rightly.  But  it  don't  give the right hint of member funtions , and a varialbe of string type it give a hint that is a int type.

if there is any setting and step I omit please instruct me .

Below is a part  info  of LSP messages
Code
LSP diagnostics: main.cpp|:|----Time: 15:16:19.218---- (5 diagnostics)|
E:\workspace\testcode\test1\HelloMingw64\main.cpp|1|note:'iostream' file not found|
E:\workspace\testcode\test1\HelloMingw64\main.cpp|8|warning:Using directive refers to implicitly-defined namespace 'std'|
E:\workspace\testcode\test1\HelloMingw64\main.cpp|12|note:Unknown type name 'string'|
E:\workspace\testcode\test1\HelloMingw64\main.cpp|25|note:Use of undeclared identifier 'cout'|
E:\workspace\testcode\test1\HelloMingw64\main.cpp|25|note:Use of undeclared identifier 'endl'|
LSP diagnostics: Test1.h|:|----Time: 15:16:20.533---- (0 diagnostics)|
LSP diagnostics: Test.h|:|----Time: 15:16:21.815---- (0 diagnostics)|
LSP diagnostics: Test.cpp|:|----Time: 15:16:22.844---- (4 diagnostics)|
E:\workspace\testcode\test1\HelloMingw64\src\Test.cpp|2|note:'iostream' file not found|
E:\workspace\testcode\test1\HelloMingw64\src\Test.cpp|4|warning:Using directive refers to implicitly-defined namespace 'std'|
E:\workspace\testcode\test1\HelloMingw64\src\Test.cpp|18|note:Use of undeclared identifier 'cout'|
E:\workspace\testcode\test1\HelloMingw64\src\Test.cpp|18|note:Use of undeclared identifier 'endl'|
LSP diagnostics: main.cpp|:|----Time: 15:19:41.802---- (5 diagnostics)|
E:\workspace\testcode\test1\HelloMingw64\main.cpp|1|note:'iostream' file not found|
E:\workspace\testcode\test1\HelloMingw64\main.cpp|8|warning:Using directive refers to implicitly-defined namespace 'std'|
E:\workspace\testcode\test1\HelloMingw64\main.cpp|12|note:Unknown type name 'string'|
E:\workspace\testcode\test1\HelloMingw64\main.cpp|26|note:Use of undeclared identifier 'cout'|
E:\workspace\testcode\test1\HelloMingw64\main.cpp|26|note:Use of undeclared identifier 'endl'|
LSP diagnostics: main.cpp|:|----Time: 15:19:51.121---- (12 diagnostics)|
......
......

@ myztmy

In order to be precise, we'll need you to paste your source code here between code tags. Paste the main.cpp code in your response using the "#" icon  above the face icons. For example the "#" icon produces code tags that look like this:
[ code ] your main.cpp source[/ code ] (without the spaces around "code".
The "#" icon and face icons appear when you  click on the "Reply" or "Quote" buttons.

I see that there are enough compile errors and warnings that it would confuse clangd. I think you're missing some #include statements. But we need to see your code first.

Regards
« Last Edit: Yesterday at 06:43:58 pm by Pecan »