This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Add support for per-file extra flags
ClosedPublic

Authored by krasimir on Jul 3 2017, 7:46 AM.
Tokens
"Love" token, awarded by thomasjo.

Details

Summary

This patch adds the ability to specify user-defined extra flags per opened file
through the LSP layer. This is a non-standard extension to the protocol.
I've already created a feature request about it for upstream lsp:
https://github.com/Microsoft/language-server-protocol/issues/255

The particular use-case is ycmd, which has a python script for figuring out
extra flags per file:
https://github.com/Valloric/ycmd#flagsforfile-filename-kwargs-

Diff Detail

Repository
rL LLVM

Event Timeline

krasimir created this revision.Jul 3 2017, 7:46 AM
krasimir added a project: Restricted Project.
krasimir added a subscriber: cfe-commits.
ilya-biryukov added inline comments.Jul 4 2017, 1:13 AM
clangd/GlobalCompilationDatabase.cpp
20 ↗(On Diff #105074)

Maybe pass Command by reference to avoid checking for null?
Is there a guideline to use pointers to make things more explicit that I've missed?

21 ↗(On Diff #105074)

There's an assert that Command is always non-null, so !Command is always false.
Maybe remove this check?

23 ↗(On Diff #105074)

There's an assert that checks that CommandLine is never empty.
Maybe remove this if statement?

27 ↗(On Diff #105074)

This way extra flags won't override flags from compilation database, we should put ExtraFlags right before the input files.

clangd will break if it receives CommandLine with more than one input file, so it should be safe to assume that the last arg is the name of the input file, i.e. always add ExtraFlags right before the last arg.

38 ↗(On Diff #105074)

Maybe extract a small helper function to get default CompileCommand and use it both in ClangdUnitStore and here?
Just in case we'll ever want to change the defaults at some point.

47 ↗(On Diff #105074)

This will break for options, containing spaces (e.g. -I"/home/user/my dir with spaces").
There should a function parsing command-line args somewhere in LLVM? Can we avoid splitting command-line args altogether (see next comment)?

58 ↗(On Diff #105074)

Maybe store splitted command-line args in ExtraFlagsForFile?
ycm_extra_conf.py seems to pass splited command-line arguments, maybe change our extension to receive a list of arguments to avoid doing command-line arg splitting?

krasimir updated this revision to Diff 105147.Jul 4 2017, 2:12 AM
krasimir marked 7 inline comments as done.
  • Addess review comments
clangd/GlobalCompilationDatabase.cpp
20 ↗(On Diff #105074)

There's a guideline to use pointers for google style, but I'm not aware for such for llvm style.

ilya-biryukov added inline comments.Jul 4 2017, 4:22 AM
clangd/GlobalCompilationDatabase.cpp
22 ↗(On Diff #105147)

If Command.CommandLine.empty() is true, extra flags will be added before 'clang' in the command-line.
Maybe restore assert(Command.CommandLine.size() >= 2) (i.e. at least a compiler binary and input file should be in the commandline) and remove this if?

60 ↗(On Diff #105147)

Maybe std::move the vector before assignment to avoid copying?

krasimir updated this revision to Diff 105257.Jul 5 2017, 5:42 AM
krasimir marked 2 inline comments as done.
  • Addess review comments
ilya-biryukov edited edge metadata.Jul 5 2017, 5:47 AM

I forgot to submit this last comment yesterday, sorry about that.

clangd/GlobalCompilationDatabase.h
51 ↗(On Diff #105257)

Maybe rename to setExtraFlagsForFile?
The name addExtraFlags... sounds like it appends to the existing list, but we actually replace the flags there.

krasimir updated this revision to Diff 105274.Jul 5 2017, 8:38 AM
  • Address review comment
krasimir marked an inline comment as done.Jul 5 2017, 8:39 AM
This revision is now accepted and ready to land.Jul 5 2017, 9:38 AM
This revision was automatically updated to reflect the committed changes.
thomasjo added a subscriber: thomasjo.