clangd
clangd copied to clipboard
Help users configure clangd to find standard library headers
One of the most common user questions is "how do I get clangd to resolve my standard library headers". I would like to suggest that clangd do more to help users specify the configuration options necessary to find standard library headers.
The first step would be to detect this scenario. That should be fairly simple: if there are any unresolved includes in the current file, check the spellings of the unresolved includes against a list of known standard library headers; if there is a match, we are in this scenario.
Having detected this scenario, clangd could offer one of two prompts under the following conditions:
- If (
CompileFlags: Compiler:
is not specified in the config file) and ((there is nocompile_commands.json
file) or (the compiler path incompile_commands.json
file is not a full path)): prompt the user to specify the full path of the compiler they are using inCompileFlags: Compiler:
. - If (we have a full path to a driver) and ((the user did not specify
--query-driver
) or (the argument to--query-driver
does not match the actual driver path)): prompt the user to specify--query-driver=<path>
where<path>
is the actual driver path for the current file.
(For full disclosure, I deleted a comment asking a question that's best discussed in a separate issue. A separate issue has been filed for it at https://github.com/clangd/clangd/issues/1897.)
After hours of searching to finally understand the reason clangd wasn't recognizing my system headers, it all came down to allowlisting my compiler with the launch argument --query-driver=C:/mingw32/bin/*
🥴
I have 2 points of feedback following this endeavour:
-
Perhaps it would be helpful renaming
--query-driver
to something more accurate, such as--allow-query-driver
-
Given that
C:/mingw32/bin
is already in my system path... Would it be possible to automatically allowlist the contents of each directory contained in the system path, as they've already been explicitly flagged by the user as being accessible system-wide?
Perhaps it would be helpful renaming
--query-driver
to something more accurate, such as--allow-query-driver
Given that
C:/mingw32/bin
is already in my system path... Would it be possible to automatically allowlist the contents of each directory contained in the system path, as they've already been explicitly flagged by the user as being accessible system-wide?
I think these are both ideas worth discussing/exploring further, and I'm happy to share more thoughts on them. Could I ask that you file separate issues (one for each) please?
I'd like to keep this issue focused on the proposal in the issue description about prompting users to help set up the configuration options currently required by clangd.
By the way, I realized that my proposal in this issue has substantial overlap with an earlier proposal in #539.
Some of the differences are:
- The proposal in #539 limits the prompt to the case where we have a full path to a driver. I think that if we have unresolved standard library headers, a prompt is useful even if we don't have a full path to a driver (the prompt would just have slightly different contents: telling the user that they need to provide a full path to the driver).
- The proposal in https://github.com/clangd/clangd/issues/539 seeks to go further than mine and actually give the user the option to temporarily allow-list the driver directly via the prompt. (I have no objection to this, but it does add implementation complexity so I'd think of it as a further enhancement.)