30 bool isSimpleExpression(
const Expr *E) {
33 switch (E->IgnoreParenCasts()->getStmtClass()) {
34 case Stmt::DeclRefExprClass:
35 case Stmt::PredefinedExprClass:
36 case Stmt::IntegerLiteralClass:
37 case Stmt::FloatingLiteralClass:
38 case Stmt::ImaginaryLiteralClass:
39 case Stmt::CharacterLiteralClass:
40 case Stmt::StringLiteralClass:
47 SourceLocation computeFunctionExtractionLocation(
const Decl *D) {
48 if (isa<CXXMethodDecl>(D)) {
51 while (
const auto *RD = dyn_cast<CXXRecordDecl>(D->getLexicalDeclContext()))
54 return D->getBeginLoc();
63 "(WIP action; use with caution!) Extracts code into a new function",
77 diag::err_refactor_code_outside_of_function);
79 if (Code.
size() == 1) {
81 if (isSimpleExpression(dyn_cast<Expr>(Code[0])))
83 diag::err_refactor_extract_simple_expression);
86 if (
const auto *PRE = dyn_cast<ObjCPropertyRefExpr>(Code[0])) {
87 if (!PRE->isMessagingGetter())
89 diag::err_refactor_extract_prohibited_expression);
101 assert(ParentDecl &&
"missing parent");
105 Code[Code.
size() - 1]->getEndLoc());
111 Rewriter ExtractedCodeRewriter(SM, LangOpts);
119 bool IsExpr = Code.
size() == 1 && isa<Expr>(Code[0]);
122 ReturnType = cast<Expr>(Code[0])->getType();
137 computeFunctionExtractionLocation(ParentDecl);
149 Code[Code.
size() - 1], ExtractedRange,
SM, LangOpts);
153 std::string ExtractedCode;
154 llvm::raw_string_ostream OS(ExtractedCode);
157 ReturnType.
print(OS, PP, DeclName);
170 auto Err = Change.insert(SM, ExtractedDeclLocation, OS.str());
172 return std::move(Err);
177 std::string ReplacedCode;
178 llvm::raw_string_ostream OS(ReplacedCode);
180 OS << DeclName <<
'(';
186 auto Err = Change.replace(
189 return std::move(Err);
Defines the clang::ASTContext interface.
A (possibly-)qualified type.
static CharSourceRange getTokenRange(SourceRange R)
Decl - This represents one declaration (or definition), e.g.
std::string getRewrittenText(CharSourceRange Range) const
getRewrittenText - Return the rewritten form of the text in the specified range.
Describes how types, statements, expressions, and declarations should be printed. ...
unsigned SuppressLifetimeQualifiers
When true, suppress printing of lifetime qualifier in ARC.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
llvm::Error createDiagnosticError(SourceLocation Loc, unsigned DiagID)
Creates an llvm::Error value that contains a diagnostic.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
unsigned SuppressStrongLifetime
When true, suppress printing of the __strong lifetime qualifier in ARC.
void print(raw_ostream &OS, const PrintingPolicy &Policy, const Twine &PlaceHolder=Twine(), unsigned Indentation=0) const
The refactoring rule context stores all of the inputs that might be needed by a refactoring action ru...
Encodes a location in the source.
unsigned SuppressUnwrittenScope
Suppress printing parts of scope specifiers that don't need to be written, e.g., for inline or anonym...
ASTContext & getASTContext() const
Dataflow Directional Tag Classes.
Rewriter - This is the main interface to the rewrite buffers.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
A trivial tuple used to represent a source range.
This class handles loading and caching of source files into memory.