cancel
Showing results for 
Search instead for 
Did you mean: 

Red Squiggles in VSC

ARich.1
Associate III

I have recently started a project in VSC using an STM32 device.  I first used STM32CubeMX to generate code, so that created most of the project.  I am using CMAKE.

Initially there was no issue, and everything worked fine after disabling intellisense.  However, I have now become plagued by the stubborn red squiggles which refuse to disappear whatever I do.  I would prefer to continue using VSC, but will have to ditch it and move to another IDE if this problem does not go away, as I cannot work like this.  Further down the file there are many other red squiggles.  The project compiles fine, no issues there.  An example of the problem:

control.c.jpg

No matter what I do, the first #include has red squiggles under it.  If I change the order of the #includes, then it is always the first one which has the squiggles.  settings.json file:

settings.json.jpg

c_cpp_properties.json file:

c_cpp_properties.json.jpg

Any help would be gratefully appreciated, please.

1 ACCEPTED SOLUTION

Accepted Solutions

I finally found a solution to eradicate this problem.  It appears that clangd can automatically look in some project folders for headers, but not all, especially ones which have not been auto-generated.  Adding the following lines to settings.json stopped the red squiggles.

-I.jpg

Thank you for your help, it has enabled me to learn much more than I would have done otherwise.  Unfortunately, the "Go to Definition" (F12) has stopped working (not as a result of the above additions), so I now have to find out what is causing that.

View solution in original post

14 REPLIES 14
KnarfB
Super User

clangd uses a hidden directory like build\Debug\.cache\clangd\index to store its database. For each indexed file you should see a corresponding .idx file. Maybe the index is not fully build yet?

I get solid underlines for every include (semantic highlighting), no squiggles. But, don't have a recent FreeRTOS project at hand.

hth

KnarfB

ankes
Associate III

What extensions do you have installed in VSCode?

From the screenshots I see that you have various settings for various extensions

  • CMake Tools (by Microsoft, "settings.json")
  • ST Arm clangd (by ST, "settings.json")
  • C/C++ (by Microsoft, "c_cpp_properties.json")

Not all of these extensions play nicely together, as they provide slightly overlapping services. If you hover over the red squiggly line then which extension is it that's reporting a problem?

CMake Tools has the ability to copy the "compile_commands.json" file it generates to another directory. To do this, use "cmake.copyCompileCommands" setting in "settings.json", and set the value to e.g. "${workspaceFolder}". This copies the file to the workspace root, and you can then use "${workspaceFolder}" in ST Arm clangd's "--compile-commands-dir" as well.

A tried-but-frustrating tactic is to disable all extensions, and then enable them one-by-one, configuring one extension at a time to fulfill the purposes it is supposed to. Only add more to the mix when you are sure that the current setup is working as it should. Oftentimes, the "Output" pane of VS Code, and the extension drop-down on the right-hand side are your best friends in diagnosing issues.

ankes_0-1765397088685.png

 

disable all extensions, and then enable them one-by-one

You may want to create a separate profile (File > Preferences > Profile > ) for STM32 projects, based on 'None'.

hth

KnarfB

Thanks.  I have just altered a line in settings.json to the following:

 
"--compile-commands-dir=${workspaceFolder}/build/Debug"


The red squiggles have gone away...for now.  Still worried they may come back though, so I'll take a look at the other response too.

Thank you, that is very helpful.  clangd is the extension reported when I hovered the mouse over the squiggly red lines.

There are several STM32 extensions installed, most of which have been installed automatically by the ST installation.  As you describe, I would prefer to work with the minimal number of extensions possible, so have created a new profile and am now pursuing a policy of installing extensions as I need them.  Red squiggles are gone for now, but am wary they might return.


Re-enabled clangd.  Red squiggles came back.  Am wondering about compile_commands.json, which I cannot find.  In CMakeLists.txt there is this line after project(${CMAKE_PROJECT_NAME}):

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

But elsewhere on the web there are people saying that will not work.  How do I get compile_commands.json to be generated and clangd to use it?  Many thanks in advance.


@ARich.1 wrote:

Re-enabled clangd.  Red squiggles came back.  Am wondering about compile_commands.json, which I cannot find.  In CMakeLists.txt there is this line after project(${CMAKE_PROJECT_NAME}):

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)


This setting configures CMake to generate "compile_commands.json" to the specified build output directory during the "configure" phase. If it is specified after "project(...)" then you should be good to go.

Ensure that the "compile_commands.json" file appears in the output directory. You can find the location from CMakePresets.json: it is the "binaryDir" setting at the "configurePresets" level. Expand that directory in VS Code, and the file should be there. If it is not, then delete CMake's cache folder (manually, from the disk) and re-run configure.

ankes_0-1765476828940.png

If the file does not appear even after this then something else is wrong.

The reason why I asked you to provide the extension list is that when you say "clangd" you can mean three separate things. There is an extension "clangd" from the publisher LLVM which downloads, starts and allows operating the "clangd" binary. Then there's an extension "STM32Cube clangd" (from ST) which depends on "STM32Cube Core", and uses the "cube" command-line program to download and operate a binary called "starm-clangd". It is an ST-flavored version of "clangd" but is not exactly the same thing as LLVM's version. They behave and are controlled differently.

Most notably, the difference between the two extensions is that the ST-flavored variants can automatically find the "compile_commands.json" file from the output directory without you lifting a finger, or changing any settings.

As a next course of action I recommend you to hover over the red squiggles and see what the actual error is. It might have nothing to do with CMake or clangd and can very much be a syntax error or a missing #define somewhere in your codebase.

Note that if you use "starm-clang" toolset then it has a known issue: the CMSIS header files for some STM32 chips are missing the necessary #define sections. I have reported this against the STM32CubeF0 repository but it may also plague others: https://github.com/STMicroelectronics/STM32CubeF0/issues/22

Thank you for your patience.  I have written much embedded code during my time, but am new to VSC and CMake, so please bear with me.

compile_commands.json does exist in the build\Debug directory, and it has lots of entries.

Deleting CMake's cache folder as you describe results in a successful compile and configure. 

After creating a new profile and including just the basic extensions, the list looks like this:

Extensions.jpg

The last one, STM32Cube clangd appears to cause the red squiggle problem.  Disabling it makes the red squiggles go away.  Building will still work fine with no errors without this extension enabled.  Hovering over the red squiggle shows the following:

Red Squiggle.jpg

Presumably clang cannot find FreeRTOSConfig.h, but as I said previously, the red squiggle is applied to whichever header file is placed first in the file.  The directories where header files are saved are referenced in CMakeLists.txt under target_include_directories().  CMake itself appears to have no issue finding them.  If I comment out a header file directory in CMakeList, then CMake complains as expected.

I realise that this is probably a trivial fix to those well versed with VSC, but it is not trivial when you are new to VSC.  Many thanks for your help in advance.

ankes
Associate III

You haven't shared any details of your project structure, where the files are placed, how directories are organized, are you using libraries or some more exotic CMake setups. Thus it is hard to guess where this exact problem might stem from.

Thus a few questions, if you don't mind?

Does this missing header problem persist in every .c file, and with any header, or is it specific to this .c file and any header which is the first in this specific file?

Are there any other problems reported in the same file? For example, at the very very beginning is there a red squiggle which says "too many errors, aborting" or something along those lines?

When clangd is able to resolve the headers it should draw them with a solid underline, like so:

ankes_0-1765489615541.png

Do the other headers in every other .c file appear like this?

In VSCode, close the file which is causing the problem. Then open "Output" pane at the bottom and change the drop-down to "STM32 Cube clangd", and clear the window's content:

ankes_1-1765489825610.png

After doing this open the problematic .c file. You should see output in the pane. Does it provide any error messages or clues as to what might be going wrong?