Details
Diff Detail
- Repository
- rCTE Clang Tools Extra
Event Timeline
This is pretty bikesheddy, but I wonder what you think about passing Ctx as the first vs last parameter.
First has some advantages (maybe I just read too much Go though):
- it's a short expr, and F(short, long) tends to format better than F(long, short). Particularly with lambdas but also long strings.
- it doesn't interfere with default args
It would be nice if we could be completely uniform here.
- Updated to match the new Context implementation.
- Logger is now stored in a global variable instead of the global Context.
- Removed json-specific RequestContext, now storing all required information in Context.
- reply,replyError and call are freestanding functions now.
Additionally:
- All async methods now take Context by-value and pass it to their Callbacks or return them in futures.
- All sync methods take Context by-ref
To be fair it was just a random thought and I totally agree with your points. I'll update the implementation and place the Context as the first parameter everywhere instead.
Could you take a look at other changes in the patch while I'm at it?
Mostly nits - throughout there's going to be lots of judgement calls about where to propagate context and where not.
No need to get all those "exactly right", but trying to get a feel for what these answers are likely to be.
Most of the interesting stuff is around the logger itself of course :-)
clangd/ClangdServer.cpp | ||
---|---|---|
30–31 | Up to you, but I like the old name better - this class is local so it's OK to be a bit opinionated. | |
30–31 | Yikes, I can see how we got here, but we really don't get to move out of something we received an lvalue-reference to... I think a safer idiom here is turning a whole lambda into a destructor: auto Fulfil = MakeDestructor([&]{ DonePromise.set_value(std::move(Ctx)); }); I'm not sure if LLVM has a shared utility for this, I've seen it in other codebases. Either way we could just define it here. (Or we could copy the context to avoid the awkwardness) | |
199 | You dropped the end of this comment | |
200 | I don't quite understand what this is saying should be better. Is it saying we do these two things together instead of running them in parallel? And why not clone the context instead? | |
clangd/ClangdUnit.cpp | ||
626 | why are we dropping the context here? Reading on... OK, it saves a *lot* of plumbing to avoid attributing this one API call. I still don't totally understand how the CppFile API/lifetime works, so up to you. | |
723 | nit: this wrapping has become *really* weird over time - can we change the comment to /* allow changing OldPreamble */ to try to fix it? | |
clangd/ClangdUnit.h | ||
257 | I think this is a bad merge - signatureHelp has moved to CodeComplete.h | |
clangd/GlobalCompilationDatabase.h | ||
37 | Do we want Ctx in this interface? | |
clangd/JSONRPCDispatcher.h | ||
29–31 | does this class need to be public? Is it staying around for the long haul? | |
41 | Is this temporary? |
clangd/JSONRPCDispatcher.cpp | ||
---|---|---|
22 | Maybe we don't need the Key suffix, as it's in the type... we should spend our verbosity elsewhere | |
23 | RequestTracer? | |
24 | RequestID | |
48 | why? | |
clangd/Logger.cpp | ||
16 | Any need to have an empty logger, rather than just have log() branch? | |
22 | There are no non-global loggers. Just L, or Current, or something? | |
26 | I actually think this would be clearer without the assert - this can statically never be null | |
clangd/Logger.h | ||
29 | why is this not called log()? | |
32–33 | Why does this need to be public? | |
38–39 | nit: these seem like low SNR. Are we actually worried about people trying to move sessions? |
- Pass Context by const-ref instead of by-ref.
- Don't expose global logger, it is only used in log method now.
- Renamed logImpl to log.
- Copy Context in forceReparse's call to scheduleCancelRebuild.
- Renamed Key<> for ID, Out and Span.
- Removed the FIXME
- Got rid of getExisting(RequestSpan)
- Remove EmptyLogger, branch in log instead.
- Renamed GlobalLogger to L
- Addressed review comments.
clangd/ClangdServer.cpp | ||
---|---|---|
199 | Missed that. Thanks. | |
200 | The first implementation did not allow to copy the Context, hence the comment. I've removed it and the context is now copied. | |
clangd/ClangdUnit.cpp | ||
626 | Exactly, plumbing it here is hard, so we're dropping the context here for now. Most of the useful work is still done outside the constructor, so this seems fine. | |
clangd/ClangdUnit.h | ||
257 | It is totally a bad merge. Thanks. | |
clangd/GlobalCompilationDatabase.h | ||
37 | I think we do, but I'd add this in a separate patch. (We're currently not changing the behavior, simply plumbing the Context through all the APIs). | |
clangd/JSONRPCDispatcher.cpp | ||
48 | This was a reminder to myself to discuss whether we want it globally in tracing+logging, not only in JSONOutput. | |
68 | Done. | |
clangd/JSONRPCDispatcher.h | ||
29–31 | I'm not sure. I'd rather keep it private, but we don't have the right abstractions to hide it yet, right? | |
41 | Renamed back to log. | |
clangd/Logger.cpp | ||
22 | This is really just a preference. To me the global prefix is not about distinguishing it from other things in this file, but rather about signalling what is this thing so that I don't have to read the declaration before reading the usages to understand what's going on. Having L or Current requires to be in the context of the file. Updated the code to use L instead. | |
clangd/Logger.h | ||
32–33 | It shouldn't be. Removed. | |
38–39 | If they do, that will lead to a bug (destructor being called twice), so it feels safer to delete the corresponding constructors. |
clangd/ClangdServer.cpp | ||
---|---|---|
30–31 | Thanks for suggestion. I totally agree, callbacks on scope exit are better. I couldn't find an utility like that in LLVM, but I think it's a useful one. I'll add this locally to this file, but maybe we should make this helper public and put it into clangd? |
Just pinging this - there's still places where Context is in a different position (at least in ClangdServer)
clangd/ClangdServer.cpp | ||
---|---|---|
30–31 | Sounds good. I'm wary of adding lots of tiny utility headers - maybe if we squint it could go in Function.h? | |
clangd/ProtocolHandlers.h | ||
32–33 | We should use one name or the other. Fine to do this to keep the patch small, but leave a FIXME? |
- Added a helper that runs functions in destructor.
- Fixed compilation after rebase.
- Got rid of FulfilContextPromiseGuard
- Added a FIXME to remove the Ctx typedef.
I think all the comments are addressed now and this change is ready for another round of review.
Let me know if I missed something.
clangd/ClangdServer.cpp | ||
---|---|---|
30–31 | Then Function.h it is :-) | |
clangd/ProtocolHandlers.h | ||
32–33 | Added a FIXME, will send a follow-up change that removes it after this change lands. |
- Make Context the first parameter everywhere.
- Add a SFINAE check to UniqueFunction's constructor to avoid creating it from non-callable objects.
clangd/Function.h | ||
---|---|---|
44 | We need this check to make ClangdServer::codeComplete overloads work. Without it, overloading thinks UniqueFunction can be constructed from any type, so we end up getting ambig between the two overloads when calling it in a following way: codeComplete(Ctx, File, Pos, CCOpts, OverridenContents). |
These should be the last usages where Context is not a first paramter.
- Make Context the first parameter in findDefinitions and CppFile::rebuild.
- Make Context the first parameter in invokeCodeCompletion.
Thanks! Just style and comment nits left really.
clangd/ClangdServer.cpp | ||
---|---|---|
206–207 | this is a pretty gross return type. (If not, we should consider having the context first, for consistency) | |
560–561 | This wrapping has gone beyond the point of readability for me. | |
clangd/ClangdServer.h | ||
252 | Is this comment obsolete? (and others below) | |
259–264 | (and this comment) | |
308 | ctx to first param | |
332 | ctx to first param | |
334–335 | ctx to first param | |
clangd/ClangdUnit.cpp | ||
616 | (while here, this should really be make_shared) | |
clangd/ClangdUnit.h | ||
175–176 | ctx to first param | |
261–262 | ctx to first param | |
clangd/CodeComplete.cpp | ||
594 | ctx to first param | |
clangd/Function.h | ||
143 | FWIW I think this assert adds more noise than value - this can clearly only be triggered by someone instantiating the template directly, which is probably better avoided using namespace detail or so. | |
157 | I'm struggling to think of cases where moving these guards is the right solution. | |
clangd/GlobalCompilationDatabase.cpp | ||
106–111 | Add a fixme to get a context in here? | |
clangd/JSONRPCDispatcher.cpp | ||
145 | There are *lots* of Context::empty() calls in this file. The idea being that explicit Context::empty() calls are a bit suspect, but these ones aren't really "different" from each other - we may want to provide an extension point to customize it, but there'd only be one. Up to you. | |
148 | I'm not sure this is better than unconditionally attaching the optional ID. | |
clangd/JSONRPCDispatcher.h | ||
29–31 | It's related in that context takes some of the job of JSONOutput, so now we have a messy overlap. | |
clangd/Protocol.h | ||
24 ↗ | (On Diff #126554) | I think tihs is unused? |
unittests/clangd/ClangdTests.cpp | ||
31 | nit: revert? |
- Removed obsolete comments.
- Moved ScopeExitGuard to namespace detail.
- Added FIXMEs to pass context to compilation database.
- Addded a FIXME to JSONOutput.
- Removed unused #include directive.
- Revert accidental whitespace change.
clangd/ClangdServer.cpp | ||
---|---|---|
206–207 | I agree, callback-based APIs look way better though. | |
560–561 | I'd refrain from doing that as this code will go away in the threading patch anyway. Hope that's ok. | |
clangd/ClangdUnit.cpp | ||
616 | It can't, because CppFile's constructor is private. This code is also going away in the threading patch. | |
clangd/Function.h | ||
143 | Moved it to namespace detail. | |
157 | We need moves as we return the class by value from the onScopeGuard function. Neither of these will compile without move constructors: auto Guard = onScopeGuard([]() { /*...*/ }); auto Guard2(onScopeGuard([]() { /*...*/ })); auto Guard3{onScopeGuard([]() { /*...*/ })}; | |
clangd/JSONRPCDispatcher.cpp | ||
145 | Since these usages are easy to spot (almost all uses of Context::empty in JSONRPCDispatcher.cpp), I'd leave them as is for now. It's easy to update them later. | |
148 | I like that we can avoid having a two-level unwrap for RequestID. If we're going down that route, maybe we should have a dedicated struct instead: // name may be bad, just for the sake of example. struct RequestData { llvm::Optional<json::Expr> RequestID; Span RequestSpan; JSONOutput Out; }; Key<RequestData> ReqData; That would also make it clear that we either attach all of those fields or none of them. |
clangd/Function.h | ||
---|---|---|
157 | Of course :-) |
Is this comment obsolete? (and others below)
Ctx is passed by value so I don't understand what it could mean.