11 #include "clang/AST/Type.h" 12 #include "clang/AST/TypeLoc.h" 13 #include "clang/Basic/LLVM.h" 14 #include "llvm/ADT/None.h" 15 #include "llvm/ADT/Optional.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/Error.h" 23 #include "llvm/ADT/StringExtras.h" 37 class ExpandAutoType :
public Tweak {
39 const char *id() const final;
40 Intent intent()
const override {
return Intent::Refactor;}
41 bool prepare(
const Selection &Inputs)
override;
42 Expected<Effect> apply(
const Selection &Inputs)
override;
43 std::string title()
const override;
47 llvm::Optional<clang::AutoTypeLoc> CachedLocation;
51 const Selection &Inputs);
57 std::string ExpandAutoType::title()
const {
return "Expand auto type"; }
59 bool ExpandAutoType::prepare(
const Selection& Inputs) {
61 if (
auto *Node = Inputs.ASTSelection.commonAncestor()) {
62 if (
auto *TypeNode = Node->ASTNode.get<TypeLoc>()) {
63 if (
const AutoTypeLoc
Result = TypeNode->getAs<AutoTypeLoc>()) {
68 return (
bool) CachedLocation;
71 Expected<Tweak::Effect> ExpandAutoType::apply(
const Selection& Inputs) {
72 auto& SrcMgr = Inputs.AST.getASTContext().getSourceManager();
78 if (DeducedType ==
llvm::None || DeducedType->isNull()) {
79 return createErrorMessage(
"Could not deduce type for 'auto' type", Inputs);
83 if (isa<RecordType>(*DeducedType) and
84 dyn_cast<RecordType>(*DeducedType)->getDecl()->isLambda()) {
85 return createErrorMessage(
"Could not expand type of lambda expression",
92 if (DeducedType->getTypePtr()->isFunctionPointerType()) {
93 return createErrorMessage(
"Could not expand type of function pointer",
97 std::string PrettyTypeName =
printType(*DeducedType,
98 Inputs.ASTSelection.commonAncestor()->getDeclContext());
101 Expansion(SrcMgr, CharSourceRange(CachedLocation->getSourceRange(),
true),
108 const Selection& Inputs) {
109 auto& SrcMgr = Inputs.AST.getASTContext().getSourceManager();
110 std::string ErrorMessage =
112 SrcMgr.getFilename(Inputs.Cursor).str() +
" Line " +
113 std::to_string(SrcMgr.getExpansionLineNumber(Inputs.Cursor));
115 return llvm::createStringError(llvm::inconvertibleErrorCode(),
116 ErrorMessage.c_str());
llvm::Optional< QualType > getDeducedType(ParsedAST &AST, SourceLocation SourceLocationBeg)
Retrieves the deduced type at a given location (auto, decltype).
std::string printType(const QualType QT, const DeclContext &Context)
Returns a QualType as string.
constexpr llvm::StringLiteral Message
#define REGISTER_TWEAK(Subclass)
Documents should not be synced at all.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static Effect applyEdit(tooling::Replacements R)
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result