<div dir="ltr">Hi Alex,<div><br></div><div>That's a huge amount of work. Here are some questions from clangd side:</div><div><br></div><div>- Does the code for the refactoring engine use ASTUnit? Can it be used without that?<br></div><div> The reason I ask is because we currently have a plan to replace ASTUnit with manual preamble/ASTContext management in clangd.</div><div><br></div><div>- Does refactoring engine modify files directly of just provide a list of "diffs" (in some form) to be applied by the editors?</div><div> If they modify the files directly, is there a way to </div><div><br></div><div>- Does refactoring engine have a separate component for testing correctness of the refactoring? Does it have an API to provide conflicts?</div><div><br></div><div>- What data does refactoring engine requires from Indexing API specifically?</div><div><br></div><div>- Why is 'Generate missing definitions' considered a cross-TU action? You actually touch only one file (i.e. the .cpp file where you put the definitions) and the AST for it should be enough for generating definitions.</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 16, 2017 at 1:57 AM, Alex Lorenz via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">Hi,</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">This is a proposal for a new Clang-based refactoring engine (as indicated by Duncan last week <span style="color:#957e35">[</span>1<span style="color:#957e35">]</span>). The new refactoring engine should be based on both the existing code from <a href="http://llvm.org" target="_blank">llvm.org</a> and the code that I've just committed to <a href="http://github.com/apple/swift-clang" target="_blank">github.com/apple/swift-clang</a> <span style="color:#957e35">[</span>2<span style="color:#957e35">]</span> (Please note that it has some outdated designs and APIs that I'd like to replace). This email is split into three different sections:</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> The first section lists the refactoring actions supported by Xcode 9.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> The second section describes the actual proposal.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> The third section outlines an action plan that I would like to follow when submitting patches for the new refactoring engine.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">I'm looking forward to any feedback that you might have!</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:25px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35"># </span>Background</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">At first I would like to provide a little bit of background on the C++/Objective-C refactorings in Xcode. Xcode 9 contains a Clang-based refactoring engine that supports several refactoring operations. The primary operation that it supports is “Rename”. The refactoring engine gathers the renamed symbol occurrences using two distinct mechanisms:</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Local rename. It gathers occurrences for declarations that are declared in a function's body. It is based on existing code from <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">clang-rename</span><span style="color:#957e35">`</span> that was moved over to Clang's <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">Tooling</span><span style="color:#957e35">`</span> library. It was then modified slightly to fit our goals. I also added support for some Objective-C declarations.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Global rename. It gathers occurrences for all other declarations. We decided to put the high-level logic for global rename into Xcode's indexer because Xcode had to support cross-language rename (between Swift and Objective-C). The indexer uses the core engine in Clang’s lib/Index, so Clang is still very much involved in this process. The indexer also delegates low-level “indexed-file” renames to Clang. This lets us gather occurrences for symbolic references that don't have the full location information (like Objective-C selector names), textual matches for occurrences of a matching string in comments, and textual matches for a matching filename in the <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">#include</span><span style="color:#957e35">`</span> directives.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">Xcode 9 also support some local refactoring actions:</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Extract function/method.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Extract duplicate expression.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Add missing switch cases (GIF: <a href="https://reviews.llvm.org/file/data/q2qz53jd7ug4cpx4zlfx/PHID-FILE-aq75y26bgkfvm5e775jt/switchCases" target="_blank">https://reviews.llvm.org/file/<wbr>data/q2qz53jd7ug4cpx4zlfx/<wbr>PHID-FILE-<wbr>aq75y26bgkfvm5e775jt/<wbr>switchCases</a>). It is also available as a fixit.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> [C++] Fill-in method stubs from abstract base classes.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Convert if to switch.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> [ObjC] Fill in protocol stubs. It is also available as a fixit.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> [ObjC] Wrap in NSLocalizedString</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">Another action that Xcode supports is called “Generate Missing Function Definitions” (GIF: <a href="https://reviews.llvm.org/file/data/u4kzt62wjdin5dpl65jl/PHID-FILE-w7jl2vz42b5aybd6oizj/generateFunctionDefinitions" target="_blank">https://reviews.llvm.org/file/<wbr>data/u4kzt62wjdin5dpl65jl/<wbr>PHID-FILE-<wbr>w7jl2vz42b5aybd6oizj/<wbr>generateFunctionDefinitions</a>). This action actually depends on the indexer to determine the set of methods that have to be implemented and where should the generated code go. It’s also the one and only cross-TU refactoring action that is currently supported (without taking "rename" into account).</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:25px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35"># </span>Refactoring Engine Proposal</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">I would like to create a new refactoring engine that will go into Clang's <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">Tooling</span><span style="color:#957e35">`</span> library. The engine's API should be designed with different use cases in mind, and I would like to ensure that the engine accommodates the following three use cases:</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Libclang. It should provide a C-based refactoring API that we can use in Xcode.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Clangd.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> A standalone refactoring tool, <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">clang-refactor</span><span style="color:#957e35">`</span>. This tool will be used for testing. It will also allow users to perform refactoring actions without using other tools.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">The engine should try to follow the following high level goals:</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> It should be easy for others to create, test, and integrate new local refactoring operations.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Refactoring operations should be allowed to fail. The engine should have good error reporting mechanisms.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Initiation via source selection should be quick (if we assume that we have access to a parsed AST) as we'd like to let a user know which actions are available as soon as possible.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> The engine should allow at least a basic level of configurability and customization (users should be able select options for a refactoring action if an action chooses to provide some). In the future we might also want to provide more powerful configuration mechanisms that will allow IDEs to generate custom UIs for different actions.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:19px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">## </span>Initiation</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">I think that there are two equally important modes of initiation that should be supported by Clang's refactoring engine:</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Initiation via source selection. This mode is important for IDEs.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Initiation via AST queries. This mode is important for standalone tools / scripts that want to refactor an entire project.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">Unfortunately our code doesn't provide a good enough abstraction that generalizes well for different actions. I'm still hashing out a better initiation scheme, and I will send a separate RFC with our proposal for initiation in the near future. Let me know if you have any ideas/suggestions!</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:19px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">## </span>Refactoring</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">I would like to propose keeping the two modes that were previously described for the "Rename" operation. This will probably mean that a standalone tool like <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">clang-refactor</span><span style="color:#957e35">`</span> won't support the global rename mode of rename. On the other hand, if Clangd will gain some form of indexing infrastructure (which seems likely given the recent interest in indexing <span style="color:#957e35">[</span>3<span style="color:#957e35">]</span>), it will be able to use the engine's global renaming infrastructure as well. I'm hoping the community will provide some input on what should happen to <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">clang-rename</span><span style="color:#957e35">`</span>, as I'm not sure whether it should stay or be deprecated in favour of new tools.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">Other refactoring operations will primarily work with source modifications. I think that the refactoring engine should provide a set of shared utility components that can be reused by different operations. For example, things like return type deduction for extracted declarations should be accessible to all operations. This kind of design should also make testing easier.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">Certain refactoring actions (e.g. "Fill-in switch cases") work well together with diagnostic fix-its, as they can be presented in an editor and a user can initiate a refactoring operation using a UI that's already familiar to them. I would like to ensure that this kind of mechanism is accessible to refactoring operations that need it. Our current code contains two actions that are implemented in the <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">Edit</span><span style="color:#957e35">`</span> library. They're usable from <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">Sema</span><span style="color:#957e35">`</span>, and we take advantage of that to generate the fix-its during semantic analysis.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:19px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">## </span>Cross-TU Operations & Indexer Queries</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">The majority of refactoring actions that we've implemented perform source transformations in a single translation unit (TU). However, there are some actions that need to work across TU boundaries. Xcode 9 includes one such cross-TU action - “Generate Missing Function Definitions”. A naïve cross-TU solution that I've looked at initially was based on an API that had a series of callbacks between Xcode's indexer and libclang. However, I quickly discovered that this model didn't work that well, as it interfered with the code that loaded and handled different TUs in the indexer.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">Note: while "Rename" is a global operation, it doesn't actually interact with other TUs because source replacements can be derived from the data stored in the project's index. Thus, our implementation doesn't treat it as a cross-TU operation.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">The current implementation of the “Generate Missing Function Definitions" action is based on the "refactoring continuations" API. A refactoring continuation is just a suspended refactoring operation that can be executed in a TU that's different to the one in which the operation was created. Each continuation includes TU-specific state that's automatically managed by Clang, as well as a set of questions about that state. Each individual question is called an "indexer query".</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">Indexer queries are the core structures that enable cross-TU actions. An external component (like Xcode's indexer) has to evaluate them before running the continuation in order for the operation to work correctly. Each query can be described using four different attributes:</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Question: The kind of question that should be answered. Our implementation exposes the following two kinds at the libclang level: <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">Decl_FileThatShouldImplement</span><span style="color:#957e35">`</span> and <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">Decl_IsDefined</span><span style="color:#957e35">`</span>.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Subject: The TU-specific state from the parent continuation that acts as the question's subject.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Result: The value that has to be set by the external indexer component before running the refactoring continuation.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Action: A flag that represents an additional action that has to be performed by the indexer before running the continuation. We use the <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">RunContinuationInTUThatHasThi<wbr>sFile</span><span style="color:#957e35">`</span> action in Xcode's indexer to determine which TU should be loaded and used when running the refactoring continuation.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">Please note that while I mention "Xcode's indexer" quite a lot in this section, our refactoring continuation API is indexer-agnostic, and is designed to work with any indexer. The indexer just has to implement support for the indexer queries that are required for a particular refactoring operation, and the refactoring engine handles everything else. The code sample below shows how our code constructs the refactoring continuation for the "Generate Missing Function Definitions" action:</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">```</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(79,129,135);background-color:rgb(255,255,255)">return continueInExternalASTUnit(</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(79,129,135);background-color:rgb(255,255,255)">/*Indexer query that gets annotated with the RunContinuationInTUThatHasThis<wbr>File action */ fileThatShouldContainImplement<wbr>ationOf(Container),</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(79,129,135);background-color:rgb(255,255,255)">/*Pointer to the continuation function*/ runInImplementationAST,</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(79,129,135);background-color:rgb(255,255,255)">/*A state value*/ Container,</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(79,129,135);background-color:rgb(255,255,255)">/*An additional indexer query over some state*/ filter(llvm::makeArrayRef(<wbr>SelectedMethods), [](const DeclEntity &D) { return !D.isDefined(); }));</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">```</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">In practical terms, the following series of events occur in Xcode when it performs the “Generate Missing Function Definitions” operation:</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> After the user chooses the "Generate Missing Function Definitions” action, Xcode notifies its indexer that it has to perform that action. The indexer then uses libclang to get the refactoring results after running the action's operation in the initiation TU.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Xcode's indexer sees that the results provide a refactoring continuation instead of source replacements, so it starts looking at the attached indexer queries.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Xcode's indexer evaluates each query by looking at the question's kind and the TU-specific state (A set of <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">CXCursor</span><span style="color:#957e35">`</span>s). It sends back the result to libclang using a set of query-agnostic routines that consume different data types.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> After evaluating the queries, Xcode's indexer loads the TU in which the refactoring continuation should run. It knows which TU it has to load by looking at the result of the query that included the <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">RunContinuationInTUThatHasThi<wbr>sFile</span><span style="color:#957e35">`</span> action flag.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Xcode's indexer then invokes the refactoring continuation in the freshly loaded TU. Clang converts the continuation's state from previously serialized TU-independent state to a state that's specific to the new TU, and continues running the refactoring operation with this state.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Xcode's indexer then receives the source replacements produced by the refactoring operation and passes them back to Xcode so that it can apply the replacements to the source.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">I would like to propose an API model that's based on our current "refactoring continuations" API for the cross-TU operations in the new refactoring engine. Clang will provide a C++ API that can be used in a way that's similar to the code presented above. It will also manage all of the interactions with libclang and Clangd, so individual refactoring operations won't have to worry about the fine details of a particular indexer. Furthermore, the new refactoring engine will limit the set of available actions based on the set of queries that are supported by an indexer, so the indexer that chooses to support refactoring continuations won't have to support all of the queries.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:19px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">## </span>Testing & Tooling</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">The <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">clang-refactor</span><span style="color:#957e35">`</span> tool will be used to test the refactoring engine. I would like to propose the following action-agnostic command line interface:</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">```</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(79,129,135);background-color:rgb(255,255,255)">clang-refactor <action> [options] <initiation file> [additional build options]</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">```</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">I think that the tool should use comment-based annotations in the test source files to control things like initiation. For example, given a selection annotation <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">// selection: +1:1 -> +2:12</span><span style="color:#957e35">`</span> on line 12, <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">clang-refactor</span><span style="color:#957e35">`</span> will map it to a selection range of 13:1 -> 14:12 and will initiate a certain refactoring action using that selection.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">In addition to <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">clang-refactor</span><span style="color:#957e35">`</span>, I would also like to provide an additional stress-test tool that will work with entire projects (using compilation databases), and will be capable of:</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Initiating/performing refactoring actions at each token in all of the source files in a project.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Verifying the consistency between the indexer and the refactoring engine to ensure that they have the same model of the source.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">This tool will also come with a script that generates compilation databases for Clang's tests.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">I also have a great little reviewing tool that helps others to visualize the refactoring changes for a particular action, and I'd be more than happy to share it if you're interested. The tool generates a set of HTML pages that contain the sources of the refactoring tests. The pages get annotated to show the changes that were made during refactoring. We found that looking at this visualization instead of the raw <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">CHECK</span><span style="color:#957e35">`</span> lines makes reviewers' life much easier.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:25px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35"># </span>Action Plan</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">Initially I would like to adopt the following high-level plan for the creation of the new refactoring engine:</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Upstream any non-refactoring specific changes from our code.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Move the core of <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">clang-rename</span><span style="color:#957e35">`</span> over to clang (<span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">clang-rename</span><span style="color:#957e35">`</span> should still work as before).</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Start working on the <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">clang-refactor</span><span style="color:#957e35">`</span> tool so that it can use the previously moved code. Move the tests from <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">clang-rename</span><span style="color:#957e35">`</span> over to Clang and convert them to <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">clang-refactor</span><span style="color:#957e35">`</span> tests.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Submit our changes to the core of the <span style="color:#957e35">`</span><span style="font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)">clang-rename</span><span style="color:#957e35">`</span> engine.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Create and test the new global rename component.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Create an AST source selection component. Initiation via source selection will be based on this component.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Create a refactoring initiation API that will be easy to use.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Start submitting our local refactoring actions.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Start working on the libclang API.</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)"><span style="color:#957e35">-</span> Create and submit everything else that remains, e.g. Cross-TU Operations & Indexer Queries.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">Cheers,</div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';background-color:rgb(255,255,255)">Alex</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';color:rgb(19,55,255);background-color:rgb(255,255,255)"><span style="color:#957e35">[</span><span style="color:#000000">1</span><span style="color:#957e35">]:</span><span style="color:#000000"> </span><a href="http://lists.llvm.org/pipermail/cfe-dev/2017-June/054108.html" target="_blank">http://lists.llvm.org/<wbr>pipermail/cfe-dev/2017-June/<wbr>054108.html</a></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';color:rgb(19,55,255);background-color:rgb(255,255,255)"><span style="color:#957e35">[</span><span style="color:#000000">2</span><span style="color:#957e35">]:</span><span style="color:#000000"> </span><a href="https://github.com/apple/swift-clang/commit/9890adfbee8f854732d0093bc8b2a32be1be8844" target="_blank">https://github.com/apple/<wbr>swift-clang/commit/<wbr>9890adfbee8f854732d0093bc8b2a3<wbr>2be1be8844</a></div><div style="margin:0px;font-stretch:normal;font-size:13px;line-height:normal;font-family:'Helvetica Neue';color:rgb(19,55,255);background-color:rgb(255,255,255)"><span style="color:#957e35">[</span><span style="color:#000000">3</span><span style="color:#957e35">]:</span><span style="color:#000000"> </span><a href="http://lists.llvm.org/pipermail/cfe-dev/2017-May/053869.html" target="_blank">http://lists.llvm.org/<wbr>pipermail/cfe-dev/2017-May/<wbr>053869.html</a></div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div></div><br>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>Regards,</div><div>Ilya Biryukov</div></div></div></div></div>
</div>