#include "clang/Tooling/Refactoring/Extract/Extract.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprObjC.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/Refactoring/Extract/SourceExtraction.h"
#include <optional>
namespace clang {
namespace tooling {
namespace {
bool isSimpleExpression(const Expr *E) {
if (!E)
return false;
switch (E->IgnoreParenCasts()->getStmtClass()) {
case Stmt::DeclRefExprClass:
case Stmt::PredefinedExprClass:
case Stmt::IntegerLiteralClass:
case Stmt::FloatingLiteralClass:
case Stmt::ImaginaryLiteralClass:
case Stmt::CharacterLiteralClass:
case Stmt::StringLiteralClass:
return true;
default:
return false;
}
}
SourceLocation computeFunctionExtractionLocation(const Decl *D) {
if (isa<CXXMethodDecl>(D)) {
while (const auto *RD = dyn_cast<CXXRecordDecl>(D->getLexicalDeclContext()))
D = RD;
}
return D->getBeginLoc();
}
}
const RefactoringDescriptor &ExtractFunction::describe() {
static const RefactoringDescriptor Descriptor = {
"extract-function",
"Extract Function",
"(WIP action; use with caution!) Extracts code into a new function",
};
return Descriptor;
}
Expected<ExtractFunction>
ExtractFunction::initiate(RefactoringRuleContext &Context,
CodeRangeASTSelection Code,
std::optional<std::string> DeclName) {
if (!Code.isInFunctionLikeBodyOfCode())
return Context.createDiagnosticError(
diag::err_refactor_code_outside_of_function);
if (Code.size() == 1) {
if (isSimpleExpression(dyn_cast<Expr>(Code[0])))
return Context.createDiagnosticError(
diag::err_refactor_extract_simple_expression);
if (const auto *PRE = dyn_cast<ObjCPropertyRefExpr>(Code[0])) {
if (!PRE->isMessagingGetter())
return Context.createDiagnosticError(
diag::err_refactor_extract_prohibited_expression);
}
}
return ExtractFunction(std::move(Code), DeclName);
}
Expected<AtomicChanges>
ExtractFunction::createSourceReplacements(RefactoringRuleContext &Context) {
const Decl *ParentDecl = Code.getFunctionLikeNearestParent();
assert(ParentDecl && "missing parent");
SourceRange ExtractedRange(Code[0]->getBeginLoc(),
Code[Code.size() - 1]->getEndLoc());
ASTContext &AST = Context.getASTContext();
SourceManager &SM = AST.getSourceManager();
const LangOptions &LangOpts = AST.getLangOpts();
Rewriter ExtractedCodeRewriter(SM, LangOpts);
QualType ReturnType = AST.VoidTy;
bool IsExpr = Code.size() == 1 && isa<Expr>(Code[0]);
if (IsExpr) {
ReturnType = cast<Expr>(Code[0])->getType();
}
SourceLocation ExtractedDeclLocation =
computeFunctionExtractionLocation(ParentDecl);
PrintingPolicy PP = AST.getPrintingPolicy();
PP.SuppressStrongLifetime = true;
PP.SuppressLifetimeQualifiers = true;
PP.SuppressUnwrittenScope = true;
ExtractionSemicolonPolicy Semicolons = ExtractionSemicolonPolicy::compute(
Code[Code.size() - 1], ExtractedRange, SM, LangOpts);
AtomicChange Change(SM, ExtractedDeclLocation);
{
std::string ExtractedCode;
llvm::raw_string_ostream OS(ExtractedCode);
OS << "static ";
ReturnType.print(OS, PP, DeclName);
OS << '(';
OS << ')';
OS << " {\n";
if (IsExpr && !ReturnType->isVoidType())
OS << "return ";
OS << ExtractedCodeRewriter.getRewrittenText(ExtractedRange);
if (Semicolons.isNeededInExtractedFunction())
OS << ';';
OS << "\n}\n\n";
auto Err = Change.insert(SM, ExtractedDeclLocation, OS.str());
if (Err)
return std::move(Err);
}
{
std::string ReplacedCode;
llvm::raw_string_ostream OS(ReplacedCode);
OS << DeclName << '(';
OS << ')';
if (Semicolons.isNeededInOriginalFunction())
OS << ';';
auto Err = Change.replace(
SM, CharSourceRange::getTokenRange(ExtractedRange), OS.str());
if (Err)
return std::move(Err);
}
return AtomicChanges{std::move(Change)};
}
}
}