<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">Sorry for the late answer. I had it unread in my inbox and was mulling over various aspects to it.</div><div class="gmail_quote">My general problem with the whole thing is that even if your solution is pareto-better (which I personally don't think, but let's assume), I don't think it matters enough to be worth the effort of change.</div>
<div class="gmail_quote">So, I'm not opposed to interface changes, I'm opposed to interface changes that don't provide significant enough value to pay for the cost. I am also not sure which changes exactly we're now talking about - I think it would help if you could split this CL in to the parts that (I hope) are non-controversial, so we have solved the actual problem, and the parts that are controversial.</div>
<div class="gmail_quote"><br></div><div class="gmail_quote">On Wed, May 7, 2014 at 6:07 PM, David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class=""><div class="h5">On Wed, May 7, 2014 at 4:46 AM, Manuel Klimek <<a href="mailto:klimek@google.com">klimek@google.com</a>> wrote:<br>
> On Tue, May 6, 2014 at 6:38 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>> wrote:<br>
>><br>
>> On Tue, May 6, 2014 at 12:42 AM, Manuel Klimek <<a href="mailto:klimek@google.com">klimek@google.com</a>> wrote:<br>
>> > On Mon, May 5, 2014 at 10:54 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>><br>
>> > wrote:<br>
>> >><br>
>> >> On Mon, May 5, 2014 at 8:42 AM, Manuel Klimek <<a href="mailto:klimek@google.com">klimek@google.com</a>><br>
>> >> wrote:<br>
>> >> > On Mon, May 5, 2014 at 5:14 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>><br>
>> >> > wrote:<br>
>> >> >><br>
>> >> >> On Mon, May 5, 2014 at 3:10 AM, Manuel Klimek <<a href="mailto:klimek@google.com">klimek@google.com</a>><br>
>> >> >> wrote:<br>
>> >> >> > On Thu, May 1, 2014 at 10:21 PM, Richard Smith<br>
>> >> >> > <<a href="mailto:richard@metafoo.co.uk">richard@metafoo.co.uk</a>><br>
>> >> >> > wrote:<br>
>> >> >> >><br>
>> >> >> >> On Thu, May 1, 2014 at 1:19 PM, Nico Weber <<a href="mailto:thakis@chromium.org">thakis@chromium.org</a>><br>
>> >> >> >> wrote:<br>
>> >> >> >>><br>
>> >> >> >>> On Thu, May 1, 2014 at 1:17 PM, David Blaikie<br>
>> >> >> >>> <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>><br>
>> >> >> >>> wrote:<br>
>> >> >> >>> > On Thu, May 1, 2014 at 1:12 PM, Richard Smith<br>
>> >> >> >>> > <<a href="mailto:richard@metafoo.co.uk">richard@metafoo.co.uk</a>><br>
>> >> >> >>> > wrote:<br>
>> >> >> >>> >> On Thu, May 1, 2014 at 12:55 PM, Etienne Ollivier<br>
>> >> >> >>> >> <<a href="mailto:eollivier@bsu.edu">eollivier@bsu.edu</a>><br>
>> >> >> >>> >> wrote:<br>
>> >> >> >>> >>><br>
>> >> >> >>> >>> Hello,<br>
>> >> >> >>> >>> I updated my clang repository recently and I an error<br>
>> >> >> >>> >>> appeared<br>
>> >> >> >>> >>> that<br>
>> >> >> >>> >>> was<br>
>> >> >> >>> >>> not<br>
>> >> >> >>> >>> there before:<br>
>> >> >> >>> >>><br>
>> >> >> >>> >>> error: no viable conversion from<br>
>> >> >> >>> >>> 'std::unique_ptr<FrontendActionFactory>'<br>
>> >> >> >>> >>> to<br>
>> >> >> >>> >>> 'clang::tooling::ToolAction *'<br>
>> >> >> >>> >>> return<br>
>> >> >> >>> >>> Tool.run(newFrontendActionFactory<MyPluginASTAction>());<br>
>> >> >> >>> >>><br>
>> >> >> >>> >>> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
>> >> >> >>> >>><br>
>> >> >> >>> >>> It is because newFrontendActionFactory has been changed to<br>
>> >> >> >>> >>> work<br>
>> >> >> >>> >>> with<br>
>> >> >> >>> >>> std::unique_ptr. So if I change my code to<br>
>> >> >> >>> >>> return<br>
>> >> >> >>> >>> Tool.run(&(*newFrontendActionFactory<MyPluginASTAction>()));<br>
>> >> >> >>> >><br>
>> >> >> >>> >><br>
>> >> >> >>> >> You can use .get() rather than the slightly non-obvious &*.<br>
>> >> >> >>> >><br>
>> >> >> >>> >>><br>
>> >> >> >>> >>> it works. The only little problem is that it can be<br>
>> >> >> >>> >>> confusing<br>
>> >> >> >>> >>> for<br>
>> >> >> >>> >>> users<br>
>> >> >> >>> >>> since is not the way it is written in the documentation,<br>
>> >> >> >>> >>> like<br>
>> >> >> >>> >>> on<br>
>> >> >> >>> >>> this<br>
>> >> >> >>> >>> pages:<br>
>> >> >> >>> >>> <a href="http://clang.llvm.org/docs/LibTooling.html" target="_blank">http://clang.llvm.org/docs/LibTooling.html</a><br>
>> >> >> >>> >>> <a href="http://clang.llvm.org/docs/LibASTMatchersTutorial.html" target="_blank">http://clang.llvm.org/docs/LibASTMatchersTutorial.html</a><br>
>> >> >> >>> >><br>
>> >> >> >>> >><br>
>> >> >> >>> >> Thanks, I've updated the documentation.<br>
>> >> >> >>> ><br>
>> >> >> >>> > I'm trying to understand how the ownership used to work/is<br>
>> >> >> >>> > meant<br>
>> >> >> >>> > to<br>
>> >> >> >>> > work now...<br>
>> >> >> >>><br>
>> >> >> >>> The result of newFrontendActionFactory() used to be leaked. Now<br>
>> >> >> >>> it's<br>
>> >> >> >>> freed at the end-of-statement cleanup of the returned<br>
>> >> >> >>> (invisible)<br>
>> >> >> >>> unique_ptr temporary.<br>
>> >> >> >><br>
>> >> >> >><br>
>> >> >> >> Why do we need to heap-allocate the FrontendActionFactory at all?<br>
>> >> >> ><br>
>> >> >> ><br>
>> >> >> > Technically we don't. There's just some ways to create the<br>
>> >> >> > FrontendActionFactory via templated factory functions<br>
>> >> >><br>
>> >> >> The current factories don't seem to make dynamic choices (or even<br>
>> >> >> templated choices) about which type to return (I may've missed<br>
>> >> >> something, though) - and the internal templating could still be<br>
>> >> >> implemented via a ctor template instead, I think.<br>
>> >> ><br>
>> >> ><br>
>> >> > How would it store the pointer to the FactoryT* ConsumerFactory?<br>
>> >><br>
>> >> I'm not sure I understand - it just takes it as a ctor parameter and<br>
>> >> stores it, doesn't it? Same as when the factory function is used.<br>
>> ><br>
>> ><br>
>> > Sure, but then we a templated constructor wouldn't be enough, we'd need<br>
>> > a<br>
>> > templated class. If that's what you meant from the start, I<br>
>> > misunderstood.<br>
>><br>
>> Sorry, yes, I'm not sure what I was trying to say above (I hadn't<br>
>> looked at the code in detail at that point - so I was probably being a<br>
>> bit vague/wrong). But, yes - as you can see in the patches, the types<br>
>> can just be used directly.<br>
>><br>
>> >> You talk about needing these things to not move around - so you can<br>
>> >> point to them - but even that doesn't seem relevant to the<br>
>> >> construction phase. If these factories returned by value and then that<br>
>> >> value was passed by const-ref to Tool.run, what would break?<br>
>> ><br>
>> ><br>
>> > Well, we can't return a class with virtual methods by value, can we?<br>
>> > We'd<br>
>> > need to get rid of the factories if we want to not use pointers (and<br>
>> > you're<br>
>> > doing that in both patches).<br>
>><br>
>> Not necessarily. The factories could return by value:<br>
>><br>
>> struct base { ... };<br>
>> struct d1 : base { ... };<br>
>> struct d2 : base { ... };<br>
>><br>
>> d1 factory() { return d1(); }<br>
>> d2 factory(int) { return d2(); }<br>
>><br>
>> and usage could either be:<br>
>><br>
>> d1 x = factory();<br>
>> d2 y = factory(3);<br>
>><br>
>> or:<br>
>><br>
>> base &x = factory();<br>
>> base &y = factory(3);<br>
><br>
><br>
> Having the factory "leak" what concrete object it passes back defeats the<br>
> purpose of the factory.<br>
<br>
</div></div>Not necessarily - you said one of the benefits was template argument<br>
deduction and I'm just demonstrating that that can be achieved without<br>
dynamic allocation.<br></blockquote><div><br></div><div>I don't see how that argument gets us any further. My arguments are always based on the full context of a specific implementation, not specific theoretical issues.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">
>> But all except 1 caller I touched are just passing the result straight<br>
>> to Tool.run, so this change doesn't affect them. The one caller that<br>
>> did:<br>
><br>
><br>
> The problem with an interface intended for use outside of Clang's tree is<br>
> that I don't think arguing based on what we find in Clang's tree (which are<br>
> mostly tests) is good enough.<br>
<br>
</div>We have 4-5 tools, that seemed like a reasonable bunch of use-cases.<br>
<br>
I'm happy to go and look inside Google (where I assume the vast<br>
majority of tools are so far) and see if we see anything else.<br>
<br>
Are there other things you think would be appropriate to use to evaluate this?<br></blockquote><div><br></div><div>Patch it into the internal code-base, make sure all reverse dependency's tests pass.</div><div> </div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div><div class="h5"><br>
>> unique_ptr<factory> p;<br>
>> if (x)<br>
>> p = newFactory();<br>
>> else if (y)<br>
>> p = newFactory(a);<br>
>> else<br>
>> p = newFactory(b);<br>
>> Tool.run(p);<br>
>><br>
>> and the change was to roll the "Tool.run" call up into the call site,<br>
>> which wasn't overly burdensome.<br>
>><br>
>> >> I don't<br>
>> >> think anything would - the constructor of the FrontendActiorFactories<br>
>> >> don't appear to leak pointers to themselves. So if you prefer the<br>
>> >> factory function syntax, you can keep that. The prototype patches<br>
>> >> attached do not, though (in case you're right about immovability).<br>
>> >><br>
>> >> > Sure, a<br>
>> >> > templated class would work (basically just instantiate the<br>
>> >> > FrontendActionFactoryAdapter), but the problem is that then you'd<br>
>> >> > always<br>
>> >> > need to specify the template argument.<br>
>> >><br>
>> >> Having to specify the parameter doesn't seem terribly burdensome.<br>
>> ><br>
>> > I find having a unique_ptr return not a terrible problem, so I'd argue<br>
>> > it's<br>
>> > trade-offs.<br>
>><br>
>> Sure (though as shown above, this particular issue doesn't have to be<br>
>> traded off - we can still use function templates as builders and get<br>
>> the convenience of template argument deduction if it's important)<br>
>><br>
>> >> > Also, there is an overload<br>
>> >> > newFrontendActionFactory<FrontendActionType>(),<br>
>> >> > and I think it has value that those form similar patterns.<br>
>> >><br>
>> >> Which would be similarly owned directly in the caller and passed by<br>
>> >> reference.<br>
>> >><br>
>> >> Tool.run(SimpleFrontendActionFactory<clang::ento::AnalysisAction>());<br>
>> >><br>
>> >> Various cleanup/modification was required, including making<br>
>> >> FrontendActionFactory's functions const (so the temporary could be<br>
>> >> passed by const ref) - all existing implementations except the weird<br>
>> >> SingleFrontendActionFactory, required no changes. (well, technically<br>
>> >> SingleFrontendActionFactory would've required no changes if I hadn't<br>
>> >> fixed its raw pointer ownership to be a unique_ptr ownership - then<br>
>> >> the unique_ptr had to be mutable)<br>
>> >><br>
>> >> It should be possible to remove the virtual dtor from<br>
>> >> FrontendActionFactory hierarchy too, since it's never polymorphically<br>
>> >> destroyed, only polymorphically used.<br>
>> ><br>
>> ><br>
>> > I would strongly vote against removing something because "it's not<br>
>> > polymorphically destroyed", where we expect users of the library to own<br>
>> > instances of it. It's very easy to introduce a place where it's<br>
>> > polymorphically destroyed.<br>
>><br>
>> The same would be true of any type - we don't put virtual dtors on all<br>
>> types in case someone tries to polymorphically destroy them. Clang has<br>
>> warnings (off by default, though, unfortunately<br>
>> -Wdelete-non-virtual-dtor) that can catch this bug if it's ever<br>
>> written.<br>
><br>
><br>
> The problem is that if the class is to be used / extended by users of a<br>
> library, you limit the users' choices when you have a class that is intended<br>
> to be inherited from, but doesn't have a virtual destructor. If clang was<br>
> only a program, and not a library, I'd basically agree with most points you<br>
> make. Since it is a library, I put a lot of emphasis on what interface a<br>
> potential user gets, how easy it is to write bugs against that interface and<br>
> understand that interface, and how much flexibility the user gets to design<br>
> the software the way they want to.<br>
><br>
> That means, in a library that is exposed to users:<br>
> - a class that's meant to be inherited from by users should have a virtual<br>
> dtor<br>
<br>
</div></div>While I realize the Tooling API is more API than most of Clang/LLVM<br>
(which is all intended to be a library and is used as such to varying<br>
degrees) this isn't necessarily the prevailing attitude. It's come up<br>
a few times that we've wanted to remove virtual dtors from<br>
non-polymorphicalyl owned types but currently warnings stop us from<br>
doing that (-Wvirtual-dtor is on, in part because Google has that on<br>
internally and we didn't have a way to disable that for LLVM without<br>
disabling it for all projects that use LLVM - we now do, so we can<br>
probably turn that warning off in favor of -Wdelete-non-virtual-dtor).<br>
This has been discussed a few times and seems to be a fairly<br>
unambiguous prevailing attitude.<br>
<br>
<a href="http://comments.gmane.org/gmane.comp.compilers.llvm.cvs/184558" target="_blank">http://comments.gmane.org/gmane.comp.compilers.llvm.cv, but now you're saying you only theoretically want to do that?s/184558</a><br>
<br>
<a href="http://comments.gmane.org/gmane.comp.compilers.llvm.cvs/184558" target="_blank">http://comments.gmane.org/gmane.comp.compilers.llvm.cvs/184558</a><br>
<br>
Which reminds me, your concern about bugs can be alleviated, I think,<br>
by making the base dtor protected and the derived classes final.<br></blockquote><div><br></div><div>Following the links I don't see arguments about specific cases where we want *external* users of an interface to keep polymorphical ownership of a class. Because that's my argument, and if you disagree, I'd like to hear an argument on why you think this is not a useful feature to have for users of this specific interface.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">
> - classes and methods shouldn't take ownership, unless they are basically<br>
> glorified containers<br>
<br>
</div>The ownership issues with runTool functions I'm not passing judgment<br>
on - it was merely a mechanical change to reflect the reality of the<br>
API as it stands today. If that reality can be changed/fixed, I'm OK<br>
with that.<br>
<div class=""><br>
>> > Because of that I think having virtual functions<br>
>> > but not having a virtual destructor is an anti-pattern.<br>
>><br>
>> I don't agree - there are lots of types that are generally not<br>
>> polymorphically owned but are polymorphically used in idioms just like<br>
>> this one. Polymorphic usage doesn't imply polymorphic<br>
>> ownership/destruction.<br>
><br>
><br>
> No, it doesn't - but we have style rules because with C++ without rules it<br>
> is so easy to shoot your foot off that even compiler engineers don't use it<br>
> that way... Having a rule to always add a virtual dtor if a class has at<br>
> least one virtual function makes reasoning about code way easier, and not<br>
> writing a bug is always the least costly variant (even when the compiler can<br>
> catch it).<br>
<br>
>> > My main problem with the attached patches is actually that it looks to<br>
>> > me<br>
>> > like they change ownership in the runTool* functions (if I'm not missing<br>
>> > something).<br>
>><br>
>> Strangely enough, I didn't change the ownership of those functions -<br>
>> that API change just reflects the reality of the API. Notice existing<br>
>> callers (before my patch) passed "Factory.create()" (which returns a<br>
>> pointer the caller should take ownership of - most of the<br>
>> implementations are of the form "return new ...") and the<br>
><br>
><br>
> Yes, that was an ownership bug in the existing use cases.<br>
<br>
</div>'was'? Has it been fixed? (I don't see the fix)<br>
<br>
And where's the bug, exactly? (I just reasoned about the ownership<br>
semantics of the code, I haven't tried to understand what the<br>
/intended/ semantics are)<br>
<br>
I assume the Factory.create() functions are intended to return<br>
ownership (should return std::unique_ptr<>). </blockquote><div><br></div><div>Yes.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Or are you thinking that<br>
Factory.create() could return some other kind of RAII wrapper that<br>
actually returns ownership to the Factory so that a singleton factory<br>
could be used more than once as long as each use was non-overlapping?<br>
<div class=""><br>
>> implementation agrees in a somewhat circular way: runToolOnCode<br>
>> creates a ToolInvocation, which builds a SingleFrontendActionFactory<br>
>> which returns the FAction from its 'create()' function... which<br>
>> returns ownership.<br>
>><br>
>><br>
>> At least that's my understanding of the ownership model. (other<br>
>> evidence that create() returns ownership - see Tooling.cpp:259, taking<br>
>> the result of create() and initializing a unique_ptr with it)<br>
><br>
><br>
> Sure, but that means create() returns ownership, not that runTool* should<br>
> take it. I can see the reasons why we would want them to take ownership,<br>
<br>
</div>If create returns ownership, how can runTool* not take ownership?</blockquote><div><br></div><div>std::unique_ptr<FrontendAction> Action(Factory->Create());</div><div>runToolOnCode(Action.get(), "class X {};", ...);</div>
<div><br></div><div>To me this would be the cleanest way to express ownership.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
In<br>
any case, like I said - I was just changing the API to match the<br>
semantics as they stand today. This is already the API contract - and<br>
correct callers are already meeting this contract by passing a<br>
'release()'d unique_ptr to runTool* functions. Changing it to<br>
unique_ptr just makes that more explicit/clear/less error-prone. If<br>
there's some other API design that fixes this in some other way, I'm<br>
OK with that - don't really mind.<br>
<div class=""><br>
> but<br>
> I still am torn, because I value the idea that you can use the same Action<br>
> for multiple runTool* calls.<br>
><br>
> The original question was about the FrontendActionFactory objects though -<br>
> here I am still convinced that our interfaces should not take ownership.<br>
<br>
</div>I'm confused - I haven't been advocating for FrontendActionFactories<br>
to have ownership passed to Tool.run. My change only changed Tool.run<br>
from taking a non-owning pointer to a non-owning reference, in both<br>
cases the callee owns the factory.<br>
<br>
My patch was just to demonstrate an answer to Richard Smith's question:<br>
<div class=""><br>
"Why do we need to heap-allocate the FrontendActionFactory at all?"<br>
<br>
</div>(which is to say, by way of example, "we don't need to heap-allocate<br>
the FrontendActionFactory at all")<br></blockquote><div><br></div><div>well, "for the use cases that are in the clang codebase". To me, interfaces are about making simple things simple for users. If you can patch it into our codebase and make the reverse deps work without making the client code significantly more complex, I'm not opposed to the change (but we might argue about what "more complex" means ...)</div>
<div><br></div><div>Cheers,</div><div>/Manuel</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class=""><div class="h5"><br>
- David<br>
<br>
<br>
><br>
> Cheers,<br>
> /Manuel<br>
><br>
><br>
>><br>
>><br>
>> - David<br>
>><br>
>> ><br>
>> > Cheers,<br>
>> > /Manuel<br>
>> ><br>
>> >><br>
>> >> - David<br>
>> >><br>
>> >> ><br>
>> >> >><br>
>> >> >><br>
>> >> >> > that we want to work<br>
>> >> >> > with pointers (because they have identity - we want the same one<br>
>> >> >> > to<br>
>> >> >> > be<br>
>> >> >> > passable to different runs and work with the data).<br>
>> >> >><br>
>> >> >> In terms of pointer identity - the idea would be to create a<br>
>> >> >> FrontendActionFactory as a direct (rather than unique_ptr) local<br>
>> >> >> variable and pass it to each run call - the object would never move<br>
>> >> >> around, so pointers to it would remain valid throughout its<br>
>> >> >> lifetime.<br>
>> >> >><br>
>> >> >> Or am I picturing something different from what you have in mind?<br>
>> >> >><br>
>> >> >> - David<br>
>> >> >><br>
>> >> >> ><br>
>> >> >> >><br>
>> >> >> >><br>
>> >> >> >> _______________________________________________<br>
>> >> >> >> cfe-dev mailing list<br>
>> >> >> >> <a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
>> >> >> >> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
>> >> >> >><br>
>> >> >> ><br>
>> >> >> ><br>
>> >> >> > _______________________________________________<br>
>> >> >> > cfe-dev mailing list<br>
>> >> >> > <a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
>> >> >> > <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
>> >> >> ><br>
>> >> ><br>
>> >> ><br>
>> ><br>
>> ><br>
><br>
><br>
</div></div></blockquote></div><br></div></div>