This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Add fixes for clang "include <foo.h>" diagnostics
ClosedPublic

Authored by sammccall on Nov 27 2021, 3:34 PM.

Details

Summary

Clang doesn't offer these fixes I guess for a couple of reasons:

  • where to insert includes is a formatting concern, and clang shouldn't depend on clang-format
  • the way clang prints diagnostics, we'd show a bunch of basically irrelevant context of "this is where we'd want to insert the include"

Maybe it's possible to hack around 1, but 2 is still a concern.
Meanwhile, bolting this onto include-fixer gets the job done.

Fixes https://github.com/clangd/clangd/issues/355
Fixes https://github.com/clangd/clangd/issues/937

Diff Detail

Event Timeline

sammccall created this revision.Nov 27 2021, 3:34 PM
sammccall requested review of this revision.Nov 27 2021, 3:34 PM
nridge accepted this revision.Nov 29 2021, 12:50 AM

Thanks, this is nice low-hanging fruit.

clang-tools-extra/clangd/Diagnostics.cpp
830

Do we maybe want to assert Info.getFixItHints().empty() here, so that if clang later introduces its own fix-it it gets on our radar and we can decide which one to prefer?

clang-tools-extra/clangd/IncludeFixer.cpp
52

nit: Index or Idx?

495

In the note_include_header_or_declare case, we could pull the second arg from the diagnostic (the name of the symbol) and use it to provide the slightly more detailed hint description that fixUnresolvedName() does. Up to you if you think that's worth doing.

clang-tools-extra/clangd/IncludeFixer.h
43

The added comment describes the behaviour of calling code, not the behaviour of this method. Perhaps it would make more sense in the calling code?

This revision is now accepted and ready to land.Nov 29 2021, 12:50 AM
sammccall marked 4 inline comments as done.Dec 8 2021, 7:06 AM
sammccall added inline comments.
clang-tools-extra/clangd/IncludeFixer.cpp
495

Good point. I shuffled around some code so these strings won't go out of sync.

And they were ("Add include X" vs "Include X") so being stubborn I changed all the existing ones to add match this one (since "include" isn't really a noun)

clang-tools-extra/clangd/IncludeFixer.h
43

Oops, the intent was to document the semantics of returned values.

Reworded and also added docs at contributeFixes.

This revision was automatically updated to reflect the committed changes.
sammccall marked 2 inline comments as done.