最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - How to ignore unknown options in Clangd for files outside the project structure? - Stack Overflow

programmeradmin2浏览0评论

I have a CMake that targets Arduino. This involves some command line options to the compiler that clangd is not familiar with.

This would give me an error at the top of any file:

Unknown argument: '-fno-tree-scev-cprop'clang(drv_unknown_argument)

I created a .clangd file in the root of my repo, with the following content:

CompileFlags: 
  Remove: [-fno-tree-scev-cprop, -fno-split-wide-types, -mmcu=]

This removes the error from files in my repo, but for files included from outside the error is still generated. For example if I open Arduino.h, I see the error still:

How can I remove these flags from ALL compile commands, regardless of file? Obviously, I could edit compile_commands.json, but that doesn't seem like the right solution.

I have a CMake that targets Arduino. This involves some command line options to the compiler that clangd is not familiar with.

This would give me an error at the top of any file:

Unknown argument: '-fno-tree-scev-cprop'clang(drv_unknown_argument)

I created a .clangd file in the root of my repo, with the following content:

CompileFlags: 
  Remove: [-fno-tree-scev-cprop, -fno-split-wide-types, -mmcu=]

This removes the error from files in my repo, but for files included from outside the error is still generated. For example if I open Arduino.h, I see the error still:

How can I remove these flags from ALL compile commands, regardless of file? Obviously, I could edit compile_commands.json, but that doesn't seem like the right solution.

Share Improve this question asked Mar 16 at 16:45 Tomáš ZatoTomáš Zato 53.5k63 gold badges310 silver badges828 bronze badges 1
  • I'd edit the JSON. You could make a clever jq oneliner. :P – HolyBlackCat Commented Mar 16 at 16:49
Add a comment  | 

2 Answers 2

Reset to default 0
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-std=c++0x" "-fno-tree-scev-cprop" NOTREE_SUPPORTED)

if(NOTREE_SUPPORTED)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-tree-scev-cprop")
endif()

You can use check_cxx_compiler_flag in your cmake to see whether it's available, and then set the flag if it is...

Clangd config options are applied on a per-file basis to files opened in an editor, by searching for .clangd files in the ancestor directories of the file being opened. In addition, the user config file is also used.

With that in mind, I can think of two methods. Let's suppose Arduino.h is located at /a/b/c/Arduino.h, and the entire subdirectory /a/b contains header files used by this cross compiler.

Method 1

Create /a/b/.clangd containing:

CompileFlags: 
  Remove: [-fno-tree-scev-cprop, -fno-split-wide-types, -mmcu=]

Method 2

Add to your user config file (e.g. ~/.config/clangd/config.yaml) the following:

If:
  PathMatch: /a/b/.*

CompileFlags:
  Remove: [-fno-tree-scev-cprop, -fno-split-wide-types, -mmcu=]

Note that the way If works is that these lines need to be separated from any other contents of the user config file by a --- line (so that the If only applies to this CompileFlags section and not others).

发布评论

评论列表(0)

  1. 暂无评论