14 #include "clang/AST/ASTTypeTraits.h"
15 #include "clang/AST/Type.h"
16 #include "llvm/Support/FormatVariadic.h"
17 #include "llvm/Support/ScopedPrinter.h"
18 #include "llvm/Support/raw_ostream.h"
32 class DumpAST :
public Tweak {
34 const char *id() const override final;
36 bool prepare(const Selection &
Inputs)
override {
37 for (
auto N =
Inputs.ASTSelection.commonAncestor(); N && !Node;
39 if (dumpable(N->ASTNode))
41 return Node.hasValue();
43 Expected<Effect> apply(
const Selection &
Inputs)
override;
44 std::string title()
const override {
46 llvm::formatv(
"Dump {0} AST", Node->getNodeKind().asStringRef()));
48 Intent intent()
const override {
return Info; }
49 bool hidden()
const override {
return true; }
52 static bool dumpable(
const ast_type_traits::DynTypedNode &N) {
55 return N.get<
Decl>() || N.get<Stmt>() || N.get<
Type>();
58 llvm::Optional<ast_type_traits::DynTypedNode> Node;
62 llvm::Expected<Tweak::Effect> DumpAST::apply(
const Selection &
Inputs) {
64 llvm::raw_string_ostream
OS(Str);
65 Node->dump(
OS,
Inputs.AST->getASTContext());
66 return Effect::showMessage(std::move(
OS.str()));
85 class ShowSelectionTree :
public Tweak {
87 const char *id() const override final;
89 bool prepare(const Selection &
Inputs)
override {
return true; }
90 Expected<Effect> apply(
const Selection &
Inputs)
override {
91 return Effect::showMessage(llvm::to_string(
Inputs.ASTSelection));
93 std::string title()
const override {
return "Show selection tree"; }
94 Intent intent()
const override {
return Info; }
95 bool hidden()
const override {
return true; }
106 class DumpSymbol :
public Tweak {
107 const char *id() const override final;
108 bool prepare(const Selection &
Inputs)
override {
return true; }
109 Expected<Effect> apply(
const Selection &
Inputs)
override {
111 llvm::raw_string_ostream
Out(Storage);
117 return Effect::showMessage(
Out.str());
119 std::string title()
const override {
return "Dump symbol under the cursor"; }
120 Intent intent()
const override {
return Info; }
121 bool hidden()
const override {
return true; }
134 class DumpRecordLayout :
public Tweak {
136 const char *id() const override final;
138 bool prepare(const Selection &
Inputs)
override {
139 if (
auto *Node =
Inputs.ASTSelection.commonAncestor())
140 if (
auto *D = Node->ASTNode.get<
Decl>())
141 Record = dyn_cast<RecordDecl>(D);
142 return Record &&
Record->isThisDeclarationADefinition() &&
143 !
Record->isDependentType();
145 Expected<Effect> apply(
const Selection &
Inputs)
override {
147 llvm::raw_string_ostream
OS(Str);
148 Inputs.AST->getASTContext().DumpRecordLayout(Record,
OS);
149 return Effect::showMessage(std::move(
OS.str()));
151 std::string title()
const override {
152 return std::string(llvm::formatv(
154 TypeWithKeyword::getTagTypeKindName(
Record->getTagKind())));
156 Intent intent()
const override {
return Info; }
161 bool hidden()
const override {
return true; }
164 const RecordDecl *
Record =
nullptr;