clang-tools  14.0.0git
ExpandAutoType.cpp
Go to the documentation of this file.
1 //===--- ExpandAutoType.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 "XRefs.h"
11 #include "support/Logger.h"
12 #include "clang/AST/Type.h"
13 #include "clang/AST/TypeLoc.h"
14 #include "clang/Basic/LLVM.h"
15 #include "llvm/ADT/None.h"
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/Error.h"
20 #include <AST.h>
21 #include <climits>
22 #include <memory>
23 #include <string>
24 
25 namespace clang {
26 namespace clangd {
27 namespace {
28 
29 /// Expand the "auto" type to the derived type
30 /// Before:
31 /// auto x = Something();
32 /// ^^^^
33 /// After:
34 /// MyClass x = Something();
35 /// ^^^^^^^
36 /// FIXME: Handle decltype as well
37 class ExpandAutoType : public Tweak {
38 public:
39  const char *id() const final;
40  llvm::StringLiteral kind() const override {
42  }
43  bool prepare(const Selection &Inputs) override;
44  Expected<Effect> apply(const Selection &Inputs) override;
45  std::string title() const override;
46 
47 private:
48  /// Cache the AutoTypeLoc, so that we do not need to search twice.
49  llvm::Optional<clang::AutoTypeLoc> CachedLocation;
50 };
51 
52 REGISTER_TWEAK(ExpandAutoType)
53 
54 std::string ExpandAutoType::title() const { return "Expand auto type"; }
55 
56 // Structured bindings must use auto, e.g. `const auto& [a,b,c] = ...;`.
57 // Return whether N (an AutoTypeLoc) is such an auto that must not be expanded.
58 bool isStructuredBindingType(const SelectionTree::Node *N) {
59  // Walk up the TypeLoc chain, because auto may be qualified.
60  while (N && N->ASTNode.get<TypeLoc>())
61  N = N->Parent;
62  // The relevant type is the only direct type child of a Decomposition.
63  return N && N->ASTNode.get<DecompositionDecl>();
64 }
65 
66 // Returns true iff Node is a lambda, and thus should not be expanded. Loc is
67 // the location of the auto type.
68 bool isDeducedAsLambda(const SelectionTree::Node *Node, SourceLocation Loc) {
69  // getDeducedType() does a traversal, which we want to avoid in prepare().
70  // But at least check this isn't auto x = []{...};, which can't ever be
71  // expanded.
72  // (It would be nice if we had an efficient getDeducedType(), instead).
73  for (const auto *It = Node; It; It = It->Parent) {
74  if (const auto *DD = It->ASTNode.get<DeclaratorDecl>()) {
75  if (DD->getTypeSourceInfo() &&
76  DD->getTypeSourceInfo()->getTypeLoc().getBeginLoc() == Loc) {
77  if (auto *RD = DD->getType()->getAsRecordDecl())
78  return RD->isLambda();
79  }
80  }
81  }
82  return false;
83 }
84 
85 // Returns true iff "auto" in Node is really part of the template parameter,
86 // which we cannot expand.
87 bool isTemplateParam(const SelectionTree::Node *Node) {
88  if (Node->Parent)
89  if (Node->Parent->ASTNode.get<NonTypeTemplateParmDecl>())
90  return true;
91  return false;
92 }
93 
94 bool ExpandAutoType::prepare(const Selection& Inputs) {
95  CachedLocation = llvm::None;
96  if (auto *Node = Inputs.ASTSelection.commonAncestor()) {
97  if (auto *TypeNode = Node->ASTNode.get<TypeLoc>()) {
98  if (const AutoTypeLoc Result = TypeNode->getAs<AutoTypeLoc>()) {
99  // Code in apply() does handle 'decltype(auto)' yet.
100  if (!Result.getTypePtr()->isDecltypeAuto() &&
101  !isStructuredBindingType(Node) &&
102  !isDeducedAsLambda(Node, Result.getBeginLoc()) &&
103  !isTemplateParam(Node))
104  CachedLocation = Result;
105  }
106  }
107  }
108 
109  return (bool) CachedLocation;
110 }
111 
112 Expected<Tweak::Effect> ExpandAutoType::apply(const Selection& Inputs) {
113  auto &SrcMgr = Inputs.AST->getSourceManager();
114 
115  llvm::Optional<clang::QualType> DeducedType = getDeducedType(
116  Inputs.AST->getASTContext(), CachedLocation->getBeginLoc());
117 
118  // if we can't resolve the type, return an error message
119  if (DeducedType == llvm::None || (*DeducedType)->isUndeducedAutoType())
120  return error("Could not deduce type for 'auto' type");
121 
122  // if it's a lambda expression, return an error message
123  if (isa<RecordType>(*DeducedType) &&
124  cast<RecordType>(*DeducedType)->getDecl()->isLambda()) {
125  return error("Could not expand type of lambda expression");
126  }
127 
128  // if it's a function expression, return an error message
129  // naively replacing 'auto' with the type will break declarations.
130  // FIXME: there are other types that have similar problems
131  if (DeducedType->getTypePtr()->isFunctionPointerType()) {
132  return error("Could not expand type of function pointer");
133  }
134 
135  std::string PrettyTypeName = printType(*DeducedType,
136  Inputs.ASTSelection.commonAncestor()->getDeclContext());
137 
138  tooling::Replacement
139  Expansion(SrcMgr, CharSourceRange(CachedLocation->getSourceRange(), true),
140  PrettyTypeName);
141 
142  return Effect::mainFileEdit(SrcMgr, tooling::Replacements(Expansion));
143 }
144 
145 } // namespace
146 } // namespace clangd
147 } // namespace clang
XRefs.h
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:28
clang::clangd::error
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
Definition: Logger.h:80
clang::clangd::getDeducedType
llvm::Optional< QualType > getDeducedType(ASTContext &ASTCtx, SourceLocation Loc)
Retrieves the deduced type at a given location (auto, decltype).
Definition: AST.cpp:475
Inputs
ParseInputs Inputs
Definition: TUScheduler.cpp:451
Tweak.h
Logger.h
DeducedType
QualType DeducedType
Definition: AST.cpp:471
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::CodeAction::REFACTOR_KIND
const static llvm::StringLiteral REFACTOR_KIND
Definition: Protocol.h:980
clang::clangd::printType
std::string printType(const QualType QT, const DeclContext &CurContext)
Returns a QualType as string.
Definition: AST.cpp:354
REGISTER_TWEAK
#define REGISTER_TWEAK(Subclass)
Definition: Tweak.h:132
AST.h