aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clangd/DumpAST.cpp
blob: b0cec65c39fa31dc641b395d43f7d3c4935af395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//===--- DumpAST.cpp - Serialize clang AST to LSP -------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "DumpAST.h"
#include "Protocol.h"
#include "SourceCode.h"
#include "support/Logger.h"
#include "clang/AST/ASTTypeTraits.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/NestedNameSpecifier.h"
#include "clang/AST/PrettyPrinter.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/TextNodeDumper.h"
#include "clang/AST/Type.h"
#include "clang/AST/TypeLoc.h"
#include "clang/Basic/Specifiers.h"
#include "clang/Tooling/Syntax/Tokens.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include <optional>

namespace clang {
namespace clangd {
namespace {

using llvm::raw_ostream;
template <typename Print> std::string toString(const Print &C) {
  std::string Result;
  llvm::raw_string_ostream OS(Result);
  C(OS);
  return std::move(OS.str());
}

bool isInjectedClassName(Decl *D) {
  if (const auto *CRD = llvm::dyn_cast<CXXRecordDecl>(D))
    return CRD->isInjectedClassName();
  return false;
}

class DumpVisitor : public RecursiveASTVisitor<DumpVisitor> {
  using Base = RecursiveASTVisitor<DumpVisitor>;

  const syntax::TokenBuffer &Tokens;
  const ASTContext &Ctx;

  // Pointers are into 'children' vector.
  // They remain valid because while a node is on the stack we only add
  // descendants, not siblings.
  std::vector<ASTNode *> Stack;

  // Generic logic used to handle traversal of all node kinds.

  template <typename T>
  bool traverseNodePre(llvm::StringRef Role, const T &Node) {
    if (Stack.empty()) {
      assert(Root.role.empty());
      Stack.push_back(&Root);
    } else {
      Stack.back()->children.emplace_back();
      Stack.push_back(&Stack.back()->children.back());
    }
    auto &N = *Stack.back();
    N.role = Role.str();
    N.kind = getKind(Node);
    N.detail = getDetail(Node);
    N.range = getRange(Node);
    N.arcana = getArcana(Node);
    return true;
  }
  bool traverseNodePost() {
    assert(!Stack.empty());
    Stack.pop_back();
    return true;
  }
  template <typename T, typename Callable>
  bool traverseNode(llvm::StringRef Role, const T &Node, const Callable &Body) {
    traverseNodePre(Role, Node);
    Body();
    return traverseNodePost();
  }

  // Range: most nodes have getSourceRange(), with a couple of exceptions.
  // We only return it if it's valid at both ends and there are no macros.

  template <typename T> std::optional<Range> getRange(const T &Node) {
    SourceRange SR = getSourceRange(Node);
    auto Spelled = Tokens.spelledForExpanded(Tokens.expandedTokens(SR));
    if (!Spelled)
      return std::nullopt;
    return halfOpenToRange(
        Tokens.sourceManager(),
        CharSourceRange::getCharRange(Spelled->front().location(),
                                      Spelled->back().endLocation()));
  }
  template <typename T, typename = decltype(std::declval<T>().getSourceRange())>
  SourceRange getSourceRange(const T &Node) {
    return Node.getSourceRange();
  }
  template <typename T,
            typename = decltype(std::declval<T *>()->getSourceRange())>
  SourceRange getSourceRange(const T *Node) {
    return Node->getSourceRange();
  }
  // TemplateName doesn't have a real Loc node type.
  SourceRange getSourceRange(const TemplateName &Node) { return SourceRange(); }
  // Attr just uses a weird method name. Maybe we should fix it instead?
  SourceRange getSourceRange(const Attr *Node) { return Node->getRange(); }

  // Kind is usually the class name, without the suffix ("Type" etc).
  // Where there's a set of variants instead, we use the 'Kind' enum values.

  std::string getKind(const Decl *D) { return D->getDeclKindName(); }
  std::string getKind(const Stmt *S) {
    std::string Result = S->getStmtClassName();
    if (llvm::StringRef(Result).ends_with("Stmt") ||
        llvm::StringRef(Result).ends_with("Expr"))
      Result.resize(Result.size() - 4);
    return Result;
  }
  std::string getKind(const TypeLoc &TL) {
    std::string Result;
    if (TL.getTypeLocClass() == TypeLoc::Qualified)
      return "Qualified";
    return TL.getType()->getTypeClassName();
  }
  std::string getKind(const TemplateArgumentLoc &TAL) {
    switch (TAL.getArgument().getKind()) {
#define TEMPLATE_ARGUMENT_KIND(X)                                              \
  case TemplateArgument::X:                                                    \
    return #X
      TEMPLATE_ARGUMENT_KIND(Null);
      TEMPLATE_ARGUMENT_KIND(NullPtr);
      TEMPLATE_ARGUMENT_KIND(Expression);
      TEMPLATE_ARGUMENT_KIND(Integral);
      TEMPLATE_ARGUMENT_KIND(Pack);
      TEMPLATE_ARGUMENT_KIND(Type);
      TEMPLATE_ARGUMENT_KIND(Declaration);
      TEMPLATE_ARGUMENT_KIND(Template);
      TEMPLATE_ARGUMENT_KIND(TemplateExpansion);
#undef TEMPLATE_ARGUMENT_KIND
    }
    llvm_unreachable("Unhandled ArgKind enum");
  }
  std::string getKind(const NestedNameSpecifierLoc &NNSL) {
    assert(NNSL.getNestedNameSpecifier());
    switch (NNSL.getNestedNameSpecifier()->getKind()) {
#define NNS_KIND(X)                                                            \
  case NestedNameSpecifier::X:                                                 \
    return #X
      NNS_KIND(Identifier);
      NNS_KIND(Namespace);
      NNS_KIND(TypeSpec);
      NNS_KIND(TypeSpecWithTemplate);
      NNS_KIND(Global);
      NNS_KIND(Super);
      NNS_KIND(NamespaceAlias);
#undef NNS_KIND
    }
    llvm_unreachable("Unhandled SpecifierKind enum");
  }
  std::string getKind(const CXXCtorInitializer *CCI) {
    if (CCI->isBaseInitializer())
      return "BaseInitializer";
    if (CCI->isDelegatingInitializer())
      return "DelegatingInitializer";
    if (CCI->isAnyMemberInitializer())
      return "MemberInitializer";
    llvm_unreachable("Unhandled CXXCtorInitializer type");
  }
  std::string getKind(const TemplateName &TN) {
    switch (TN.getKind()) {
#define TEMPLATE_KIND(X)                                                       \
  case TemplateName::X:                                                        \
    return #X;
      TEMPLATE_KIND(Template);
      TEMPLATE_KIND(OverloadedTemplate);
      TEMPLATE_KIND(AssumedTemplate);
      TEMPLATE_KIND(QualifiedTemplate);
      TEMPLATE_KIND(DependentTemplate);
      TEMPLATE_KIND(SubstTemplateTemplateParm);
      TEMPLATE_KIND(SubstTemplateTemplateParmPack);
      TEMPLATE_KIND(UsingTemplate);
#undef TEMPLATE_KIND
    }
    llvm_unreachable("Unhandled NameKind enum");
  }
  std::string getKind(const Attr *A) {
    switch (A->getKind()) {
#define ATTR(X)                                                                \
  case attr::X:                                                                \
    return #X;
#include "clang/Basic/AttrList.inc"
#undef ATTR
    }
    llvm_unreachable("Unhandled attr::Kind enum");
  }
  std::string getKind(const CXXBaseSpecifier &CBS) {
    // There aren't really any variants of CXXBaseSpecifier.
    // To avoid special cases in the API/UI, use public/private as the kind.
    return getAccessSpelling(CBS.getAccessSpecifier()).str();
  }
  std::string getKind(const ConceptReference *CR) {
    // Again there are no variants here.
    // Kind is "Concept", role is "reference"
    return "Concept";
  }

  // Detail is the single most important fact about the node.
  // Often this is the name, sometimes a "kind" enum like operators or casts.
  // We should avoid unbounded text, like dumping parameter lists.

  std::string getDetail(const Decl *D) {
    const auto *ND = dyn_cast<NamedDecl>(D);
    if (!ND || llvm::isa_and_nonnull<CXXConstructorDecl>(ND->getAsFunction()) ||
        isa<CXXDestructorDecl>(ND))
      return "";
    std::string Name = toString([&](raw_ostream &OS) { ND->printName(OS); });
    if (Name.empty())
      return "(anonymous)";
    return Name;
  }
  std::string getDetail(const Stmt *S) {
    if (const auto *DRE = dyn_cast<DeclRefExpr>(S))
      return DRE->getNameInfo().getAsString();
    if (const auto *DSDRE = dyn_cast<DependentScopeDeclRefExpr>(S))
      return DSDRE->getNameInfo().getAsString();
    if (const auto *ME = dyn_cast<MemberExpr>(S))
      return ME->getMemberNameInfo().getAsString();
    if (const auto *CE = dyn_cast<CastExpr>(S))
      return CE->getCastKindName();
    if (const auto *BO = dyn_cast<BinaryOperator>(S))
      return BO->getOpcodeStr().str();
    if (const auto *UO = dyn_cast<UnaryOperator>(S))
      return UnaryOperator::getOpcodeStr(UO->getOpcode()).str();
    if (const auto *CCO = dyn_cast<CXXConstructExpr>(S))
      return CCO->getConstructor()->getNameAsString();
    if (const auto *CTE = dyn_cast<CXXThisExpr>(S)) {
      bool Const = CTE->getType()->getPointeeType().isLocalConstQualified();
      if (CTE->isImplicit())
        return Const ? "const, implicit" : "implicit";
      if (Const)
        return "const";
      return "";
    }
    if (isa<IntegerLiteral, FloatingLiteral, FixedPointLiteral,
            CharacterLiteral, ImaginaryLiteral, CXXBoolLiteralExpr>(S))
      return toString([&](raw_ostream &OS) {
        S->printPretty(OS, nullptr, Ctx.getPrintingPolicy());
      });
    if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(S))
      return MTE->isBoundToLvalueReference() ? "lvalue" : "rvalue";
    return "";
  }
  std::string getDetail(const TypeLoc &TL) {
    if (TL.getType().hasLocalQualifiers())
      return TL.getType().getLocalQualifiers().getAsString(
          Ctx.getPrintingPolicy());
    if (const auto *TT = dyn_cast<TagType>(TL.getTypePtr()))
      return getDetail(TT->getDecl());
    if (const auto *DT = dyn_cast<DeducedType>(TL.getTypePtr()))
      if (DT->isDeduced())
        return DT->getDeducedType().getAsString(Ctx.getPrintingPolicy());
    if (const auto *BT = dyn_cast<BuiltinType>(TL.getTypePtr()))
      return BT->getName(Ctx.getPrintingPolicy()).str();
    if (const auto *TTPT = dyn_cast<TemplateTypeParmType>(TL.getTypePtr()))
      return getDetail(TTPT->getDecl());
    if (const auto *TT = dyn_cast<TypedefType>(TL.getTypePtr()))
      return getDetail(TT->getDecl());
    return "";
  }
  std::string getDetail(const NestedNameSpecifierLoc &NNSL) {
    const auto &NNS = *NNSL.getNestedNameSpecifier();
    switch (NNS.getKind()) {
    case NestedNameSpecifier::Identifier:
      return NNS.getAsIdentifier()->getName().str() + "::";
    case NestedNameSpecifier::Namespace:
      return NNS.getAsNamespace()->getNameAsString() + "::";
    case NestedNameSpecifier::NamespaceAlias:
      return NNS.getAsNamespaceAlias()->getNameAsString() + "::";
    default:
      return "";
    }
  }
  std::string getDetail(const CXXCtorInitializer *CCI) {
    if (FieldDecl *FD = CCI->getAnyMember())
      return getDetail(FD);
    if (TypeLoc TL = CCI->getBaseClassLoc())
      return getDetail(TL);
    return "";
  }
  std::string getDetail(const TemplateArgumentLoc &TAL) {
    if (TAL.getArgument().getKind() == TemplateArgument::Integral)
      return toString(TAL.getArgument().getAsIntegral(), 10);
    return "";
  }
  std::string getDetail(const TemplateName &TN) {
    return toString([&](raw_ostream &OS) {
      TN.print(OS, Ctx.getPrintingPolicy(), TemplateName::Qualified::None);
    });
  }
  std::string getDetail(const Attr *A) {
    return A->getAttrName() ? A->getNormalizedFullName() : A->getSpelling();
  }
  std::string getDetail(const CXXBaseSpecifier &CBS) {
    return CBS.isVirtual() ? "virtual" : "";
  }
  std::string getDetail(const ConceptReference *CR) {
    return CR->getNamedConcept()->getNameAsString();
  }

  /// Arcana is produced by TextNodeDumper, for the types it supports.

  template <typename Dump> std::string dump(const Dump &D) {
    return toString([&](raw_ostream &OS) {
      TextNodeDumper Dumper(OS, Ctx, /*ShowColors=*/false);
      D(Dumper);
    });
  }
  template <typename T> std::string getArcana(const T &N) {
    return dump([&](TextNodeDumper &D) { D.Visit(N); });
  }
  std::string getArcana(const NestedNameSpecifierLoc &NNS) { return ""; }
  std::string getArcana(const TemplateName &NNS) { return ""; }
  std::string getArcana(const CXXBaseSpecifier &CBS) { return ""; }
  std::string getArcana(const TemplateArgumentLoc &TAL) {
    return dump([&](TextNodeDumper &D) {
      D.Visit(TAL.getArgument(), TAL.getSourceRange());
    });
  }
  std::string getArcana(const TypeLoc &TL) {
    return dump([&](TextNodeDumper &D) { D.Visit(TL.getType()); });
  }

public:
  ASTNode Root;
  DumpVisitor(const syntax::TokenBuffer &Tokens, const ASTContext &Ctx)
      : Tokens(Tokens), Ctx(Ctx) {}

  // Override traversal to record the nodes we care about.
  // Generally, these are nodes with position information (TypeLoc, not Type).

  bool TraverseDecl(Decl *D) {
    return !D || isInjectedClassName(D) ||
           traverseNode("declaration", D, [&] { Base::TraverseDecl(D); });
  }
  bool TraverseTypeLoc(TypeLoc TL) {
    return !TL || traverseNode("type", TL, [&] { Base::TraverseTypeLoc(TL); });
  }
  bool TraverseTemplateName(const TemplateName &TN) {
    return traverseNode("template name", TN,
                        [&] { Base::TraverseTemplateName(TN); });
  }
  bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &TAL) {
    return traverseNode("template argument", TAL,
                        [&] { Base::TraverseTemplateArgumentLoc(TAL); });
  }
  bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSL) {
    return !NNSL || traverseNode("specifier", NNSL, [&] {
      Base::TraverseNestedNameSpecifierLoc(NNSL);
    });
  }
  bool TraverseConstructorInitializer(CXXCtorInitializer *CCI) {
    return !CCI || traverseNode("constructor initializer", CCI, [&] {
      Base::TraverseConstructorInitializer(CCI);
    });
  }
  bool TraverseAttr(Attr *A) {
    return !A || traverseNode("attribute", A, [&] { Base::TraverseAttr(A); });
  }
  bool TraverseConceptReference(ConceptReference *C) {
    return !C || traverseNode("reference", C,
                              [&] { Base::TraverseConceptReference(C); });
  }
  bool TraverseCXXBaseSpecifier(const CXXBaseSpecifier &CBS) {
    return traverseNode("base", CBS,
                        [&] { Base::TraverseCXXBaseSpecifier(CBS); });
  }
  // Stmt is the same, but this form allows the data recursion optimization.
  bool dataTraverseStmtPre(Stmt *S) {
    return S && traverseNodePre(isa<Expr>(S) ? "expression" : "statement", S);
  }
  bool dataTraverseStmtPost(Stmt *X) { return traverseNodePost(); }

  // QualifiedTypeLoc is handled strangely in RecursiveASTVisitor: the derived
  // TraverseTypeLoc is not called for the inner UnqualTypeLoc.
  // This means we'd never see 'int' in 'const int'! Work around that here.
  // (The reason for the behavior is to avoid traversing the nested Type twice,
  // but we ignore TraverseType anyway).
  bool TraverseQualifiedTypeLoc(QualifiedTypeLoc QTL) {
    return TraverseTypeLoc(QTL.getUnqualifiedLoc());
  }
  // Uninteresting parts of the AST that don't have locations within them.
  bool TraverseNestedNameSpecifier(NestedNameSpecifier *) { return true; }
  bool TraverseType(QualType) { return true; }

  // OpaqueValueExpr blocks traversal, we must explicitly traverse it.
  bool TraverseOpaqueValueExpr(OpaqueValueExpr *E) {
    return TraverseStmt(E->getSourceExpr());
  }
  // We only want to traverse the *syntactic form* to understand the selection.
  bool TraversePseudoObjectExpr(PseudoObjectExpr *E) {
    return TraverseStmt(E->getSyntacticForm());
  }
};

} // namespace

ASTNode dumpAST(const DynTypedNode &N, const syntax::TokenBuffer &Tokens,
                const ASTContext &Ctx) {
  DumpVisitor V(Tokens, Ctx);
  // DynTypedNode only works with const, RecursiveASTVisitor only non-const :-(
  if (const auto *D = N.get<Decl>())
    V.TraverseDecl(const_cast<Decl *>(D));
  else if (const auto *S = N.get<Stmt>())
    V.TraverseStmt(const_cast<Stmt *>(S));
  else if (const auto *NNSL = N.get<NestedNameSpecifierLoc>())
    V.TraverseNestedNameSpecifierLoc(
        *const_cast<NestedNameSpecifierLoc *>(NNSL));
  else if (const auto *NNS = N.get<NestedNameSpecifier>())
    V.TraverseNestedNameSpecifier(const_cast<NestedNameSpecifier *>(NNS));
  else if (const auto *TL = N.get<TypeLoc>())
    V.TraverseTypeLoc(*const_cast<TypeLoc *>(TL));
  else if (const auto *QT = N.get<QualType>())
    V.TraverseType(*const_cast<QualType *>(QT));
  else if (const auto *CCI = N.get<CXXCtorInitializer>())
    V.TraverseConstructorInitializer(const_cast<CXXCtorInitializer *>(CCI));
  else if (const auto *TAL = N.get<TemplateArgumentLoc>())
    V.TraverseTemplateArgumentLoc(*const_cast<TemplateArgumentLoc *>(TAL));
  else if (const auto *CBS = N.get<CXXBaseSpecifier>())
    V.TraverseCXXBaseSpecifier(*const_cast<CXXBaseSpecifier *>(CBS));
  else if (const auto *CR = N.get<ConceptReference>())
    V.TraverseConceptReference(const_cast<ConceptReference *>(CR));
  else
    elog("dumpAST: unhandled DynTypedNode kind {0}",
         N.getNodeKind().asStringRef());
  return std::move(V.Root);
}

} // namespace clangd
} // namespace clang