Skip to content

Commit 0d94c4b

Browse files
fix: script for removing unwanted flags from compile_commands.json (#1235)
* fix: script for removing unwanted flags from compile_commands.json
1 parent 37521c1 commit 0d94c4b

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

docs/en/additionalfeatures/clangd_cdt_support.rst

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ In line with this enhancement, we've discontinued support for the standard CDT E
99

1010
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.
1111

12-
You can find more details on the LSP based C/C++ Editor features `here <https://github.com/eclipse-cdt/cdt-lsp/>`_.
12+
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/>`_.
1313

1414
Prerequisites
1515
-------------
@@ -45,6 +45,40 @@ However, if you are dealing with an existing project, please create a `.clangd`
4545
CompilationDatabase: build
4646
Remove: [-m*, -f*]
4747
48+
How to fix Unknown argument error when navigating to the esp-idf components
49+
----------------------------------------------------------------------------------------
50+
51+
If you are seeing the following error markers while navigating to the esp-idf components source code:
52+
53+
.. code-block:: none
54+
55+
Multiple markers at this line
56+
- Unknown argument: '-fno-tree-switch-conversion' [drv_unknown_argument]
57+
- Unknown argument: '-fno-shrink-wrap' [drv_unknown_argument]
58+
- Unknown argument: '-fstrict-volatile-bitfields' [drv_unknown_argument]
59+
60+
Please follow the steps below to fix it:
61+
62+
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>`_.
63+
64+
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>`_:
65+
66+
.. code-block:: cmake
67+
68+
if(EXISTS "${CMAKE_SOURCE_DIR}/fix_compile_commands.py")
69+
add_custom_target(
70+
fix_clangd ALL
71+
COMMAND ${CMAKE_COMMAND} -E echo "Running fix_compile_commands.py..."
72+
COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_SOURCE_DIR}/fix_compile_commands.py
73+
COMMENT "Cleaning compile_commands.json for clangd"
74+
VERBATIM
75+
)
76+
endif()
77+
78+
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.
79+
80+
4. Now, you can navigate to the esp-idf components source code without any errors.
81+
4882
Disable CDT Indexer
4983
-------------------
5084

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
4+
project(blink)
5+
6+
if(EXISTS "${CMAKE_SOURCE_DIR}/fix_compile_commands.py")
7+
add_custom_target(
8+
fix_clangd ALL
9+
COMMAND ${CMAKE_COMMAND} -E echo "Running fix_compile_commands.py..."
10+
COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_SOURCE_DIR}/fix_compile_commands.py
11+
COMMENT "Cleaning compile_commands.json for clangd"
12+
VERBATIM
13+
)
14+
endif()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Author: Kondal Kolipaka <[email protected]>
2+
#
3+
# Copyright 2025 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
4+
# Use is subject to license terms.
5+
6+
"""
7+
This script is used to remove all '-m*' and '-f*' flags from the compile_commands.json file.
8+
"""
9+
10+
import os
11+
import json
12+
import re
13+
14+
def find_compile_commands_json(start_dir):
15+
for root, dirs, files in os.walk(start_dir):
16+
if 'compile_commands.json' in files:
17+
return os.path.join(root, 'compile_commands.json')
18+
return None
19+
20+
def remove_mf_flags(compile_commands):
21+
for entry in compile_commands:
22+
args = entry.get("arguments") or entry.get("command", "").split()
23+
filtered_args = [arg for arg in args if not re.match(r"^-([mf]).*", arg)]
24+
25+
if "arguments" in entry:
26+
entry["arguments"] = filtered_args
27+
elif "command" in entry:
28+
entry["command"] = " ".join(filtered_args)
29+
return compile_commands
30+
31+
def main():
32+
build_dir = os.path.join(os.path.dirname(__file__), "build")
33+
cc_path = find_compile_commands_json(build_dir)
34+
35+
if not cc_path:
36+
print("compile_commands.json not found.")
37+
return
38+
39+
with open(cc_path, "r") as f:
40+
compile_commands = json.load(f)
41+
42+
cleaned_commands = remove_mf_flags(compile_commands)
43+
44+
with open(cc_path, "w") as f:
45+
json.dump(cleaned_commands, f, indent=2)
46+
47+
print(f"Removed all '-m*' and '-f*' flags from: {cc_path}")
48+
49+
if __name__ == "__main__":
50+
main()

0 commit comments

Comments
 (0)