<div dir="ltr"><div dir="ltr">On Mon, Apr 8, 2019 at 4:29 PM Theodore Dubois <<a href="mailto:tbodt@google.com">tbodt@google.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I would appreciate a compiler option to entirely disable this feature.<br>
I agree with the include-what-you-use style, but my LSP editor plugin<br>
has a bug where when a new line is inserted at the top of the file,<br>
the cursor stays on the same line number, so it moves up a line.<br></blockquote><div>Ouch :-( Which editor/plugin is that?</div><div>That makes sense, we need to never insert includes during code completion in that case, and a flag is a reasonable way to configure it (it's not codebase-specific at all).</div><div><br></div><div>Clangd also adds include-insertion fixes to "unknown symbol" diagnostics, so you can still get assistance with #includes that way. (This didn't make the cut for clangd 8 though).</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
~Theodore<br>
<br>
On Mon, Apr 8, 2019 at 1:05 AM Sam McCall via clangd-dev<br>
<<a href="mailto:clangd-dev@lists.llvm.org" target="_blank">clangd-dev@lists.llvm.org</a>> wrote:<br>
><br>
> On Mon, Apr 8, 2019 at 8:36 AM Aneesh Kumar K.V <<a href="mailto:aneesh.kumar@linux.ibm.com" target="_blank">aneesh.kumar@linux.ibm.com</a>> wrote:<br>
>><br>
>> Sam McCall <<a href="mailto:sammccall@google.com" target="_blank">sammccall@google.com</a>> writes:<br>
>><br>
>> > Sorry about that :-( +Eric Liu <<a href="mailto:ioeric@google.com" target="_blank">ioeric@google.com</a>> who may have thoughts.<br>
>> > I don't think we have an option to disable includes today, maybe we should<br>
>> > add one...<br>
>> ><br>
>> > But we don't expect the inserted includes to create compile errors - *this<br>
>> > is probably a bug, it'd be great if you could provide more details* (is it<br>
>> > the right header but spelled wrong, or the wrong header entirely, etc). It<br>
>> > may be that you'd like this feature if it worked properly.<br>
>> ><br>
>><br>
>> Sorry for the delay in response. This mostly is due to the location where<br>
>> the #include line is added. With company-lsp (emacs), it gets added at<br>
>> the beginning of the file and the dependency across headers results in<br>
>> build failures.<br>
><br>
> If you have time, it'd be great to see a breakdown of such an example (which files and symbols are involved, how the compile error comes about).<br>
><br>
> Currently we're assuming:<br>
> 1) each symbol has a single header where its "main" declaration is found<br>
> 2) if you're using a symbol in a file, that header should be included (or the symbol should be forward-declared)<br>
> 3) it's safe and useful to directly include exactly the directly required headers, rather than relying on transitive includes<br>
> This style is called include-what-you-use. Its main advantages: it tends not to break code when #include structure changes, and it's easy to decide what should be #included.<br>
><br>
> Personally, my advice would be to follow this style. If inserting an #include header breaks your compile, it's likely that:<br>
>  - that header is not #including one of its dependencies<br>
>  - you have a circular dependency, which can be resolved with a forward declaration<br>
>  - header guards are missing somewhere<br>
> etc<br>
><br>
> However I do think it might make sense to offer a way to disable include insertion entirely for projects that are not IWYU-style and don't want to be.<br>
><br>
> I do have a pending patch to never insert #includes of files that don't have recognized header guards (#ifdef/#define/#endif). <a href="https://reviews.llvm.org/D60316" rel="noreferrer" target="_blank">https://reviews.llvm.org/D60316</a><br>
> This avoids triggering the feature in *some* cases where it's not safe.<br>
> Happy to look at other heuristics if they're feasible to implement.<br>
><br>
>> One important thing to note is, I am able to build without any<br>
>> error even without the new #include line. Hence not sure why we should add the<br>
>> extra #include when completing function names.<br>
><br>
> 1) Often the relevant header is *transitively* included already, but not directly included. If file A needs symbol C, relying on a transitive A.c -> B.h -> C.h include means that if B stops depending on C, or A stops depending on B, then A will break.<br>
> 2) There are lots of possible behaviors here and they all have downsides, we have to pick one (at least as default).<br>
>   a) never inserting headers: breaks code after many completions<br>
>   b) inserting only when the symbol isn't declared in a transitively included header: unacceptable performance penalty to deserialize all declarations from the preamble, problems with incomplete types<br>
>   c) inserting only when the primary header isn't transitively included: unpredictable behavior in large codebases, still can break compiles in the same way<br>
>   d) IWYU: causes problems in codebases that are not IWYU-clean.<br>
><br>
> Cheers, Sam<br>
><br>
>><br>
>><br>
>> -aneesh<br>
>><br>
> _______________________________________________<br>
> clangd-dev mailing list<br>
> <a href="mailto:clangd-dev@lists.llvm.org" target="_blank">clangd-dev@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/clangd-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/clangd-dev</a><br>
</blockquote></div></div>