Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion docs/en/additionalfeatures/clangd_cdt_support.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ In line with this enhancement, we've discontinued support for the standard CDT E

The LSP powered C/C++ editor greatly benefits ESP-IDF developers by aligning with the latest language standards and compiler versions, enhancing productivity, and improving code quality.

You can find more details on the LSP based C/C++ Editor features `here <https://github.com/eclipse-cdt/cdt-lsp/>`_.
You can find more details on the LSP based C/C++ Editor features in the `Eclipse CDT-LSP documentation <https://github.com/eclipse-cdt/cdt-lsp/>`_.

Prerequisites
-------------
Expand Down Expand Up @@ -45,6 +45,40 @@ However, if you are dealing with an existing project, please create a `.clangd`
CompilationDatabase: build
Remove: [-m*, -f*]

How to fix Unknown argument error when navigating to the esp-idf components
----------------------------------------------------------------------------------------

If you are seeing the following error markers while navigating to the esp-idf components source code:

.. code-block:: none

Multiple markers at this line
- Unknown argument: '-fno-tree-switch-conversion' [drv_unknown_argument]
- Unknown argument: '-fno-shrink-wrap' [drv_unknown_argument]
- Unknown argument: '-fstrict-volatile-bitfields' [drv_unknown_argument]

Please follow the steps below to fix it:

1. Download the script for `fix_compile_commands.py <https://github.com/espressif/idf-eclipse-plugin/tree/master/resources/resources/fix_compile_commands/fix_compile_commands.py>`_.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix broken link path to the Python script.

The link contains a duplicate /resources/ segment in the path, making it incorrect.

Apply this diff to fix the link:

-1. Download the script for `fix_compile_commands.py <https://github.com/espressif/idf-eclipse-plugin/tree/master/resources/resources/fix_compile_commands/fix_compile_commands.py>`_.
+1. Download the script for `fix_compile_commands.py <https://github.com/espressif/idf-eclipse-plugin/tree/master/resources/fix_compile_commands/fix_compile_commands.py>`_.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
1. Download the script for `fix_compile_commands.py <https://github.com/espressif/idf-eclipse-plugin/tree/master/resources/resources/fix_compile_commands/fix_compile_commands.py>`_.
1. Download the script for `fix_compile_commands.py <https://github.com/espressif/idf-eclipse-plugin/tree/master/resources/fix_compile_commands/fix_compile_commands.py>`_.
🤖 Prompt for AI Agents
In docs/en/additionalfeatures/clangd_cdt_support.rst at line 62, the hyperlink
to the Python script fix_compile_commands.py contains a duplicated /resources/
segment in the URL path. Correct the link by removing the extra /resources/ so
that the URL points to
https://github.com/espressif/idf-eclipse-plugin/tree/master/resources/fix_compile_commands/fix_compile_commands.py,
ensuring the link is valid and directs to the intended script.


2. Invoke the script from the project post build step. Here is example for `CMakeLists.txt <https://github.com/espressif/idf-eclipse-plugin/blob/master/resources/resources/fix_compile_commands/CMakeLists.txt>`_:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix broken link path to the CMakeLists.txt file.

Similar to the Python script link, this also contains a duplicate /resources/ segment.

Apply this diff to fix the link:

-2. Invoke the script from the project post build step. Here is example for `CMakeLists.txt <https://github.com/espressif/idf-eclipse-plugin/blob/master/resources/resources/fix_compile_commands/CMakeLists.txt>`_:
+2. Invoke the script from the project post build step. Here is example for `CMakeLists.txt <https://github.com/espressif/idf-eclipse-plugin/blob/master/resources/fix_compile_commands/CMakeLists.txt>`_:
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
2. Invoke the script from the project post build step. Here is example for `CMakeLists.txt <https://github.com/espressif/idf-eclipse-plugin/blob/master/resources/resources/fix_compile_commands/CMakeLists.txt>`_:
2. Invoke the script from the project post build step. Here is example for `CMakeLists.txt <https://github.com/espressif/idf-eclipse-plugin/blob/master/resources/fix_compile_commands/CMakeLists.txt>`_:
🤖 Prompt for AI Agents
In docs/en/additionalfeatures/clangd_cdt_support.rst at line 64, the link to the
CMakeLists.txt file contains a duplicated /resources/ segment in its path. Fix
this by correcting the URL to remove the extra /resources/ so it matches the
correct path structure, similar to the Python script link.


.. code-block:: cmake

if(EXISTS "${CMAKE_SOURCE_DIR}/fix_compile_commands.py")
add_custom_target(
fix_clangd ALL
COMMAND ${CMAKE_COMMAND} -E echo "Running fix_compile_commands.py..."
COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_SOURCE_DIR}/fix_compile_commands.py
COMMENT "Cleaning compile_commands.json for clangd"
VERBATIM
)
endif()
Comment on lines +68 to +76
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Update CMake example to match the corrected implementation.

The documentation should reflect the fixes needed in the actual CMakeLists.txt file to ensure users get working code.

Apply this diff to show the corrected CMake code:

-     if(EXISTS "${CMAKE_SOURCE_DIR}/fix_compile_commands.py")
+     if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/fix_compile_commands.py")
        add_custom_target(
            fix_clangd ALL
            COMMAND ${CMAKE_COMMAND} -E echo "Running fix_compile_commands.py..."
-           COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_SOURCE_DIR}/fix_compile_commands.py
+           COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_CURRENT_SOURCE_DIR}/fix_compile_commands.py
            COMMENT "Cleaning compile_commands.json for clangd"
            VERBATIM
+           DEPENDS ${CMAKE_PROJECT_NAME}.elf
        )
      endif()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if(EXISTS "${CMAKE_SOURCE_DIR}/fix_compile_commands.py")
add_custom_target(
fix_clangd ALL
COMMAND ${CMAKE_COMMAND} -E echo "Running fix_compile_commands.py..."
COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_SOURCE_DIR}/fix_compile_commands.py
COMMENT "Cleaning compile_commands.json for clangd"
VERBATIM
)
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/fix_compile_commands.py")
add_custom_target(
fix_clangd ALL
COMMAND ${CMAKE_COMMAND} -E echo "Running fix_compile_commands.py..."
COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_CURRENT_SOURCE_DIR}/fix_compile_commands.py
COMMENT "Cleaning compile_commands.json for clangd"
VERBATIM
DEPENDS ${CMAKE_PROJECT_NAME}.elf
)
endif()
🤖 Prompt for AI Agents
In docs/en/additionalfeatures/clangd_cdt_support.rst around lines 68 to 76,
update the CMake example to match the corrected implementation by replacing the
existing snippet with the provided diff. This involves ensuring the
add_custom_target named fix_clangd runs the fix_compile_commands.py script
correctly with proper commands and comments, reflecting the actual fixed
CMakeLists.txt usage for users.


3. Now run the build, the script will remove the -m* and -f* flags from the compile_commands.json file which are unknown to clangd.

4. Now, you can navigate to the esp-idf components source code without any errors.

Disable CDT Indexer
-------------------

Expand Down
14 changes: 14 additions & 0 deletions resources/fix_compile_commands/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(blink)

if(EXISTS "${CMAKE_SOURCE_DIR}/fix_compile_commands.py")
add_custom_target(
fix_clangd ALL
COMMAND ${CMAKE_COMMAND} -E echo "Running fix_compile_commands.py..."
COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_SOURCE_DIR}/fix_compile_commands.py
Comment on lines +6 to +10
Copy link

Copilot AI Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMakeLists resides in a subdirectory, so ${CMAKE_SOURCE_DIR} may not resolve to this folder. Use ${CMAKE_CURRENT_SOURCE_DIR} to reference the script path correctly.

Suggested change
if(EXISTS "${CMAKE_SOURCE_DIR}/fix_compile_commands.py")
add_custom_target(
fix_clangd ALL
COMMAND ${CMAKE_COMMAND} -E echo "Running fix_compile_commands.py..."
COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_SOURCE_DIR}/fix_compile_commands.py
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/fix_compile_commands.py")
add_custom_target(
fix_clangd ALL
COMMAND ${CMAKE_COMMAND} -E echo "Running fix_compile_commands.py..."
COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_CURRENT_SOURCE_DIR}/fix_compile_commands.py

Copilot uses AI. Check for mistakes.
COMMENT "Cleaning compile_commands.json for clangd"
VERBATIM
)
endif()
Comment on lines +6 to +14
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add dependency to ensure compile_commands.json exists before cleaning.

The custom target runs with ALL but doesn't depend on the main build target. This could cause the script to run before compile_commands.json is generated by the build process.

Apply this diff to add proper dependency:

 if(EXISTS "${CMAKE_SOURCE_DIR}/fix_compile_commands.py")
     add_custom_target(
         fix_clangd ALL
         COMMAND ${CMAKE_COMMAND} -E echo "Running fix_compile_commands.py..."
         COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_SOURCE_DIR}/fix_compile_commands.py
         COMMENT "Cleaning compile_commands.json for clangd"
         VERBATIM
+        DEPENDS ${CMAKE_PROJECT_NAME}.elf
     )
 endif()

This ensures the script runs only after the main build target completes and compile_commands.json is available.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if(EXISTS "${CMAKE_SOURCE_DIR}/fix_compile_commands.py")
add_custom_target(
fix_clangd ALL
COMMAND ${CMAKE_COMMAND} -E echo "Running fix_compile_commands.py..."
COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_SOURCE_DIR}/fix_compile_commands.py
COMMENT "Cleaning compile_commands.json for clangd"
VERBATIM
)
endif()
if(EXISTS "${CMAKE_SOURCE_DIR}/fix_compile_commands.py")
add_custom_target(
fix_clangd ALL
COMMAND ${CMAKE_COMMAND} -E echo "Running fix_compile_commands.py..."
COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_SOURCE_DIR}/fix_compile_commands.py
COMMENT "Cleaning compile_commands.json for clangd"
VERBATIM
DEPENDS ${CMAKE_PROJECT_NAME}.elf
)
endif()
🤖 Prompt for AI Agents
In resources/fix_compile_commands/CMakeLists.txt around lines 6 to 14, the
custom target fix_clangd runs unconditionally with ALL but lacks a dependency on
the main build target that generates compile_commands.json. To fix this, add a
dependency to the custom target on the main build target (e.g., the primary
executable or library target) so that fix_clangd runs only after
compile_commands.json is created. Modify the add_custom_target command to
include a DEPENDS clause referencing the main build target.

50 changes: 50 additions & 0 deletions resources/fix_compile_commands/fix_compile_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Author: Kondal Kolipaka <[email protected]>
#
# Copyright 2025 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
# Use is subject to license terms.

"""
This script is used to remove all '-m*' and '-f*' flags from the compile_commands.json file.
"""

import os
import json
import re

def find_compile_commands_json(start_dir):
for root, dirs, files in os.walk(start_dir):
if 'compile_commands.json' in files:
return os.path.join(root, 'compile_commands.json')
return None

def remove_mf_flags(compile_commands):
for entry in compile_commands:
args = entry.get("arguments") or entry.get("command", "").split()
Copy link

Copilot AI Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use shlex.split on the command string instead of str.split() to correctly handle quoted arguments and paths with spaces.

Copilot uses AI. Check for mistakes.
filtered_args = [arg for arg in args if not re.match(r"^-([mf]).*", arg)]

if "arguments" in entry:
entry["arguments"] = filtered_args
elif "command" in entry:
entry["command"] = " ".join(filtered_args)
return compile_commands

def main():
build_dir = os.path.join(os.path.dirname(__file__), "build")
cc_path = find_compile_commands_json(build_dir)

if not cc_path:
print("compile_commands.json not found.")
return

with open(cc_path, "r") as f:
compile_commands = json.load(f)

cleaned_commands = remove_mf_flags(compile_commands)

with open(cc_path, "w") as f:
json.dump(cleaned_commands, f, indent=2)

Comment on lines +39 to +46
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for JSON operations and file I/O.

The script lacks error handling for JSON parsing and file operations, which could cause cryptic failures if the file is corrupted or has permission issues.

Apply this diff to add proper error handling:

-    with open(cc_path, "r") as f:
-        compile_commands = json.load(f)
-
-    cleaned_commands = remove_mf_flags(compile_commands)
-
-    with open(cc_path, "w") as f:
-        json.dump(cleaned_commands, f, indent=2)
+    try:
+        with open(cc_path, "r") as f:
+            compile_commands = json.load(f)
+        
+        if not isinstance(compile_commands, list):
+            print(f"Error: Expected list in {cc_path}, got {type(compile_commands)}")
+            return
+
+        cleaned_commands = remove_mf_flags(compile_commands)
+
+        with open(cc_path, "w") as f:
+            json.dump(cleaned_commands, f, indent=2)
+    except (json.JSONDecodeError, IOError) as e:
+        print(f"Error processing {cc_path}: {e}")
+        return
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
with open(cc_path, "r") as f:
compile_commands = json.load(f)
cleaned_commands = remove_mf_flags(compile_commands)
with open(cc_path, "w") as f:
json.dump(cleaned_commands, f, indent=2)
try:
with open(cc_path, "r") as f:
compile_commands = json.load(f)
if not isinstance(compile_commands, list):
print(f"Error: Expected list in {cc_path}, got {type(compile_commands)}")
return
cleaned_commands = remove_mf_flags(compile_commands)
with open(cc_path, "w") as f:
json.dump(cleaned_commands, f, indent=2)
except (json.JSONDecodeError, IOError) as e:
print(f"Error processing {cc_path}: {e}")
return
🤖 Prompt for AI Agents
In resources/fix_compile_commands/fix_compile_commands.py around lines 39 to 46,
add try-except blocks to handle potential exceptions during file reading, JSON
loading, and file writing operations. Catch exceptions like IOError and
json.JSONDecodeError, log or print meaningful error messages, and ensure the
program fails gracefully instead of crashing with cryptic errors.

print(f"Removed all '-m*' and '-f*' flags from: {cc_path}")

if __name__ == "__main__":
main()
Loading