//===--- ExtractFunction.cpp -------------------------------------*- C++-*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // Extracts statements to a new function and replaces the statements with a // call to the new function. // Before: // void f(int a) { // [[if(a < 5) // a = 5;]] // } // After: // void extracted(int &a) { // if(a < 5) // a = 5; // } // void f(int a) { // extracted(a); // } // // - Only extract statements // - Extracts from non-templated free functions only. // - Parameters are const only if the declaration was const // - Always passed by l-value reference // - Void return type // - Cannot extract declarations that will be needed in the original function // after extraction. // - Checks for broken control flow (break/continue without loop/switch) // // 1. ExtractFunction is the tweak subclass // - Prepare does basic analysis of the selection and is therefore fast. // Successful prepare doesn't always mean we can apply the tweak. // - Apply does a more detailed analysis and can be slower. In case of // failure, we let the user know that we are unable to perform extraction. // 2. ExtractionZone store information about the range being extracted and the // enclosing function. // 3. NewFunction stores properties of the extracted function and provides // methods for rendering it. // 4. CapturedZoneInfo uses a RecursiveASTVisitor to capture information about // the extraction like declarations, existing return statements, etc. // 5. getExtractedFunction is responsible for analyzing the CapturedZoneInfo and // creating a NewFunction. //===----------------------------------------------------------------------===// #include "AST.h" #include "FindTarget.h" #include "ParsedAST.h" #include "Selection.h" #include "SourceCode.h" #include "refactor/Tweak.h" #include "support/Logger.h" #include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclBase.h" #include "clang/AST/NestedNameSpecifier.h" #include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/Stmt.h" #include "clang/Basic/LangOptions.h" #include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceManager.h" #include "clang/Tooling/Core/Replacement.h" #include "clang/Tooling/Refactoring/Extract/SourceExtraction.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Casting.h" #include "llvm/Support/Error.h" #include "llvm/Support/raw_os_ostream.h" #include <optional> namespace clang { namespace clangd { namespace … // namespace } // namespace clangd } // namespace clang