|
| 1 | +//===--- ReplaceAutoType.cpp -------------------------------------*- C++-*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +#include "refactor/Tweak.h" |
| 9 | + |
| 10 | +#include "Logger.h" |
| 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" |
| 18 | +#include <climits> |
| 19 | +#include <memory> |
| 20 | +#include <string> |
| 21 | +#include <AST.h> |
| 22 | +#include "XRefs.h" |
| 23 | +#include "llvm/ADT/StringExtras.h" |
| 24 | + |
| 25 | +namespace clang { |
| 26 | +namespace clangd { |
| 27 | + |
| 28 | +/// Expand the "auto" type to the derived type |
| 29 | +/// Before: |
| 30 | +/// auto x = Something(); |
| 31 | +/// ^^^^ |
| 32 | +/// After: |
| 33 | +/// MyClass x = Something(); |
| 34 | +/// ^^^^^^^ |
| 35 | +/// FIXME: Handle decltype as well |
| 36 | +class ExpandAutoType : public Tweak { |
| 37 | +public: |
| 38 | + const char *id() const final; |
| 39 | + Intent intent() const override { return Intent::Refactor;} |
| 40 | + bool prepare(const Selection &Inputs) override; |
| 41 | + Expected<Effect> apply(const Selection &Inputs) override; |
| 42 | + std::string title() const override; |
| 43 | + |
| 44 | +private: |
| 45 | + /// Cache the AutoTypeLoc, so that we do not need to search twice. |
| 46 | + llvm::Optional<clang::AutoTypeLoc> CachedLocation; |
| 47 | + |
| 48 | + /// Create an error message with filename and line number in it |
| 49 | + llvm::Error createErrorMessage(const std::string& Message, |
| 50 | + const Selection &Inputs); |
| 51 | + |
| 52 | +}; |
| 53 | + |
| 54 | +REGISTER_TWEAK(ExpandAutoType) |
| 55 | + |
| 56 | +std::string ExpandAutoType::title() const { return "expand auto type"; } |
| 57 | + |
| 58 | +bool ExpandAutoType::prepare(const Selection& Inputs) { |
| 59 | + CachedLocation = llvm::None; |
| 60 | + if (auto *Node = Inputs.ASTSelection.commonAncestor()) { |
| 61 | + if (auto *TypeNode = Node->ASTNode.get<TypeLoc>()) { |
| 62 | + if (const AutoTypeLoc Result = TypeNode->getAs<AutoTypeLoc>()) { |
| 63 | + CachedLocation = Result; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + return (bool) CachedLocation; |
| 68 | +} |
| 69 | + |
| 70 | +Expected<Tweak::Effect> ExpandAutoType::apply(const Selection& Inputs) { |
| 71 | + auto& SrcMgr = Inputs.AST.getASTContext().getSourceManager(); |
| 72 | + |
| 73 | + llvm::Optional<clang::QualType> DeducedType = |
| 74 | + getDeducedType(Inputs.AST, CachedLocation->getBeginLoc()); |
| 75 | + |
| 76 | + // if we can't resolve the type, return an error message |
| 77 | + if (DeducedType == llvm::None || DeducedType->isNull()) { |
| 78 | + return createErrorMessage("Could not deduce type for 'auto' type", Inputs); |
| 79 | + } |
| 80 | + |
| 81 | + // if it's a lambda expression, return an error message |
| 82 | + if (isa<RecordType>(*DeducedType) and |
| 83 | + dyn_cast<RecordType>(*DeducedType)->getDecl()->isLambda()) { |
| 84 | + return createErrorMessage("Could not expand type of lambda expression", |
| 85 | + Inputs); |
| 86 | + } |
| 87 | + |
| 88 | + // if it's a function expression, return an error message |
| 89 | + // naively replacing 'auto' with the type will break declarations. |
| 90 | + // FIXME: there are other types that have similar problems |
| 91 | + if (DeducedType->getTypePtr()->isFunctionPointerType()) { |
| 92 | + return createErrorMessage("Could not expand type of function pointer", |
| 93 | + Inputs); |
| 94 | + } |
| 95 | + |
| 96 | + std::string PrettyTypeName = printType(*DeducedType, |
| 97 | + Inputs.ASTSelection.commonAncestor()->getDeclContext()); |
| 98 | + |
| 99 | + tooling::Replacement |
| 100 | + Expansion(SrcMgr, CharSourceRange(CachedLocation->getSourceRange(), true), |
| 101 | + PrettyTypeName); |
| 102 | + |
| 103 | + return Tweak::Effect::applyEdit(tooling::Replacements(Expansion)); |
| 104 | +} |
| 105 | + |
| 106 | +llvm::Error ExpandAutoType::createErrorMessage(const std::string& Message, |
| 107 | + const Selection& Inputs) { |
| 108 | + auto& SrcMgr = Inputs.AST.getASTContext().getSourceManager(); |
| 109 | + std::string ErrorMessage = |
| 110 | + Message + ": " + |
| 111 | + SrcMgr.getFilename(Inputs.Cursor).str() + " Line " + |
| 112 | + std::to_string(SrcMgr.getExpansionLineNumber(Inputs.Cursor)); |
| 113 | + |
| 114 | + return llvm::createStringError(llvm::inconvertibleErrorCode(), |
| 115 | + ErrorMessage.c_str()); |
| 116 | +} |
| 117 | + |
| 118 | +} // namespace clangd |
| 119 | +} // namespace clang |
0 commit comments