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

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2624
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: 5453
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: 2624
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: 5453
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: 2624
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: 2624
Re: Code completion using LSP and clangd
« Reply #306 on: June 07, 2023, 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: June 07, 2023, 06:43:58 pm by Pecan »

Offline myztmy

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Code completion using LSP and clangd
« Reply #307 on: June 09, 2023, 09:53:36 am »
@ 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

I've been struggling with "clangd"  for a whole day!

With another win7 machine I install "GCC 13.1.0 (with POSIX threads) + LLVM/Clang/LLD/LLDB 16.0.5 + MinGW-w64 11.0.0 (UCRT) - release 5" which Clang and Mingw are in the same  bin directory . This time , "clangd" can find the standard lib header files , add a string type variable it can identify the correct type and give the right hint. But it just made me happy for a few seconds. After using class wizzard add a simple class , clangd don't konw the new class and then don't give correct hint ,everything has become a mess again.

Go back to my MinGW-w64 and LLVM separate installation win7 machine , I try to throw clangd.exe into MinGW-w64 bin directory ,  the C::B always Pop up dialog box complain "can't dectect clangd.exe" . Placing clangd.exe in any other directory is no problem. In this machine clangd  although can find user class but can't find standard lib class, also not working properly.

Most of the existing bugs now of "clangd"  also existed  in  "Code completion" of C::B(win) 20.03 release ,  but the latest nightly builds version have fixed "Code completion" almost all bugs except freeze  (I only have used C::B for ten days, and only tested those two C::B versions ). "clangd" seems like "Yesterday Once More" . I guess "clangd" has referenced some code from "Code completion" of older versions. 

Now that you mention "#", let me show you a "funny" image(I really know what you mean ,but the imge explains everything, I don't think I need use "#" icon to post my so simplest source code :P ).

Regards

 
« Last Edit: June 09, 2023, 10:01:08 am by myztmy »

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2624
Re: Code completion using LSP and clangd
« Reply #308 on: June 10, 2023, 07:09:46 pm »
I've been struggling with "clangd"  for a whole day!

With another win7 machine I install "GCC 13.1.0 (with POSIX threads) + LLVM/Clang/LLD/LLDB 16.0.5 + MinGW-w64 11.0.0 (UCRT) - release 5" which Clang and Mingw are in the same  bin directory . This time , "clangd" can find the standard lib header files , add a string type variable it can identify the correct type and give the right hint. But it just made me happy for a few seconds. After using class wizzard add a simple class , clangd don't konw the new class and then don't give correct hint ,everything has become a mess again.

Go back to my MinGW-w64 and LLVM separate installation win7 machine , I try to throw clangd.exe into MinGW-w64 bin directory ,  the C::B always Pop up dialog box complain "can't dectect clangd.exe" . Placing clangd.exe in any other directory is no problem. In this machine clangd  although can find user class but can't find standard lib class, also not working properly.

Most of the existing bugs now of "clangd"  also existed  in  "Code completion" of C::B(win) 20.03 release ,  but the latest nightly builds version have fixed "Code completion" almost all bugs except freeze  (I only have used C::B for ten days, and only tested those two C::B versions ). "clangd" seems like "Yesterday Once More" . I guess "clangd" has referenced some code from "Code completion" of older versions. 

Now that you mention "#", let me show you a "funny" image(I really know what you mean ,but the imge explains everything, I don't think I need use "#" icon to post my so simplest source code :P ).

Regards

Since you will not provide us with your code in a form that we can copy and paste in order to re-create the errors, I'm going to step away from this conversation.

clangd cannot correctly provide you with good information when your code is full of errors.
You will have to read the errors provided by clangd and correct your code before popup info is reliable.

Secondly, moving clangd into arbitrary folders is asking for trouble. The clangd_client plugin provides clangd server with both the location of the clangd executable along with the location of its matching resources. Causing a miss match of these locations also causes clangd to issue incorrect info.

Good luck.
« Last Edit: June 10, 2023, 07:23:18 pm by Pecan »

Offline myztmy

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Code completion using LSP and clangd
« Reply #309 on: June 12, 2023, 10:21:50 am »

Since you will not provide us with your code in a form that we can copy and paste in order to re-create the errors, I'm going to step away from this conversation.

clangd cannot correctly provide you with good information when your code is full of errors.
You will have to read the errors provided by clangd and correct your code before popup info is reliable.

Secondly, moving clangd into arbitrary folders is asking for trouble. The clangd_client plugin provides clangd server with both the location of the clangd executable along with the location of its matching resources. Causing a miss match of these locations also causes clangd to issue incorrect info.

Good luck.

Following is my codes

main.cpp

Code
//main.cpp
#include <iostream>
#include "test.h"

using namespace std;

int main()
{
    test t;
    t.SetCounter(100);
    t.GetCounter();
    cout << "Hello world!" << endl;
    return 0;
}

test.h
Code
#ifndef TEST_H
#define TEST_H
class test
{
    public:
        test();
        virtual ~test();
        unsigned int GetCounter();
        void SetCounter(unsigned int val);
    protected:
    private:
        unsigned int m_Counter;
};
#endif // TEST_H

test.cpp
Code
#include "test.h"

test::test()
{
    //ctor
}

test::~test()
{
    //dtor
}

unsigned int test::GetCounter() { return m_Counter; }
void test::SetCounter(unsigned int val) { m_Counter = val; }
Please refer to the attached image in my previous post for the project structure ,in fact that image have shown  nealy all code .

I feel very sorry for publishing such simple code too late, but if it is useful to you, I hope it can retain your step 8).

Regards




« Last Edit: June 12, 2023, 10:42:18 am by myztmy »

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2624
Re: Code completion using LSP and clangd
« Reply #310 on: June 13, 2023, 07:16:00 pm »
@ myztmy

Here's my compile of your source.
What's the problem you're presenting?
Please be aware that you have to correct the errors presented by clangd (in the LSP messages log) before clangd can give you proper analysis of your code.
« Last Edit: June 13, 2023, 07:23:50 pm by Pecan »

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2624
Re: Code completion using LSP and clangd
« Reply #311 on: August 28, 2023, 11:58:30 pm »
Because there was a major leak in clangd_client, now fixed, users of this  plugin should update from Head rev 13343.

It will also be in the next Nightly.

Thanks, Christo, for catching this, and for the fix.
« Last Edit: August 29, 2023, 12:01:13 am by Pecan »