This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Canonicalize compile flags before applying edits
ClosedPublic

Authored by kadircet on Jul 22 2021, 12:37 AM.

Details

Summary

Pushes input for the compile action to the end while separating with a
-- before applying other manglings. This ensures edits that effect only the
arguments that come after them works, like changing parse language via -x.

Fixes https://github.com/clangd/clangd/issues/555.

Diff Detail

Event Timeline

kadircet created this revision.Jul 22 2021, 12:37 AM
kadircet requested review of this revision.Jul 22 2021, 12:37 AM
sammccall accepted this revision.Jul 22 2021, 3:53 AM
sammccall added inline comments.
clang-tools-extra/clangd/CompileCommands.cpp
213

More than one input is a really annoying error to diagnose, would we want to detect and log it here? Downside is this code runs *really* often.

232–233

These are copies of the rendered arg list, i think many of them we could replace with calls to ArgList.erase before rendering the arg list.
(e.g. syntax-only... if we erased -save-temps and turned off color diagnostics in Compiler.cpp, I think that covers everything)

... but wait, is "rendering the arglist" even possible?

(I want to expand the scope of this patch unneccesarily, but it might be a way to offset the extra costs...)

239

Technically the hasArgNoClaim is stale, we've applied edits to the command since then. Probably fine though, not much use for setting resource-dir. (maybe we should even drop this check)

hasArgNoClaim is probably cheaper than the old scan we were doing, but its impl is crazy: not a simple scan, but also not a simple lookup.

243

OTOH the edits may well set sysroot or isysroot, i'm not sure this is valid :-(

248–249

while here and worrying about efficiency - should we just search for '--' ourselves?
This is an extra copy (due to the ArgsAdjuster interface)

This revision is now accepted and ready to land.Jul 22 2021, 3:53 AM
sammccall added inline comments.Jul 22 2021, 4:42 AM
clang-tools-extra/clangd/CompileCommands.cpp
213

Another option would be to delete all the OPT_INPUTs and append the actual filename (CompileCommand::Filename)

This seems like it would define this class of errors out of existence, maybe?

Layering doesn't make it easy to do that right here, though :-\

kadircet marked 6 inline comments as done.Jul 23 2021, 2:25 AM
kadircet added inline comments.
clang-tools-extra/clangd/CompileCommands.cpp
213

I agree that it is gonna be somewhat noisy. But I think it is gonna effect 2 different workflows:

  • User hits a file with compile flags containing multiple inputs, notices clangd doesn't work, tries to resolve the problem and looks at the logs. since they are not working on the file it is not gonna be noisy in this case and really helpful.
  • They are just going to ignore clangd being broken and move on, In this case we'll really turn the logs into garbage but I don't think user will care.

So maybe we can make it vlog instead, and ensure it only shows up to users that care?

213

yes this sounds great indeed, send out as a separate follow-up in D106639.

232–233

send out D106562 to get rid of these.

239

keeping the Has behaviour just to minimize the impact, happy to drop the check as a follow-up.

kadircet updated this revision to Diff 361133.Jul 23 2021, 2:25 AM
kadircet marked 3 inline comments as done.
  • Address review comments
sammccall accepted this revision.Jul 23 2021, 5:32 AM

Still LG

clang-tools-extra/clangd/CompileCommands.cpp
248–249

hmm, why isn't this Cmd.insert(It, ToAppend.begin(), ToAppend.end()) instead of the loop, to avoid shuffling the tail elements?

Or in fact Cmd.insert(It, std::make_move_iterator(ToAppend.begin()), std::make_move_iterator(ToAppend.end()));?

255

you reverted the others to Has but keps the ArgList reference here.

kadircet updated this revision to Diff 361183.Jul 23 2021, 7:01 AM
kadircet marked 2 inline comments as done.

Change occurence of ArgList usage for canonical prefixes to Has