File: CXExtractAPI.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (217 lines) | stat: -rw-r--r-- 7,617 bytes parent folder | download
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
//===- CXExtractAPI.cpp - libclang APIs for manipulating CXAPISet ---------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file defines all libclang APIs related to manipulation CXAPISet
//
//===----------------------------------------------------------------------===//

#include "CXCursor.h"
#include "CXString.h"
#include "CXTranslationUnit.h"
#include "clang-c/CXErrorCode.h"
#include "clang-c/Documentation.h"
#include "clang-c/Index.h"
#include "clang-c/Platform.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/ExtractAPI/API.h"
#include "clang/ExtractAPI/ExtractAPIVisitor.h"
#include "clang/ExtractAPI/Serialization/SymbolGraphSerializer.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/FrontendOptions.h"
#include "clang/Index/USRGeneration.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/CBindingWrapping.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/raw_ostream.h"

using namespace clang;
using namespace clang::extractapi;

namespace {
struct LibClangExtractAPIVisitor
    : ExtractAPIVisitor<LibClangExtractAPIVisitor> {
  using Base = ExtractAPIVisitor<LibClangExtractAPIVisitor>;

  LibClangExtractAPIVisitor(ASTContext &Context, APISet &API)
      : ExtractAPIVisitor<LibClangExtractAPIVisitor>(Context, API) {}

  const RawComment *fetchRawCommentForDecl(const Decl *D) const {
    return Context.getRawCommentForAnyRedecl(D);
  }

  // We need to visit implementations as well to ensure that when a user clicks
  // on a method defined only within the implementation that we can still
  // provide a symbol graph for it.
  bool VisitObjCImplementationDecl(const ObjCImplementationDecl *Decl) {
    if (!shouldDeclBeIncluded(Decl))
      return true;

    const ObjCInterfaceDecl *Interface = Decl->getClassInterface();
    StringRef Name = Interface->getName();
    StringRef USR = API.recordUSR(Decl);
    PresumedLoc Loc =
        Context.getSourceManager().getPresumedLoc(Decl->getLocation());
    LinkageInfo Linkage = Decl->getLinkageAndVisibility();
    DocComment Comment;
    if (auto *RawComment = fetchRawCommentForDecl(Interface))
      Comment = RawComment->getFormattedLines(Context.getSourceManager(),
                                              Context.getDiagnostics());

    // Build declaration fragments and sub-heading by generating them for the
    // interface.
    DeclarationFragments Declaration =
        DeclarationFragmentsBuilder::getFragmentsForObjCInterface(Interface);
    DeclarationFragments SubHeading =
        DeclarationFragmentsBuilder::getSubHeading(Decl);

    // Collect super class information.
    SymbolReference SuperClass;
    if (const auto *SuperClassDecl = Decl->getSuperClass()) {
      SuperClass.Name = SuperClassDecl->getObjCRuntimeNameAsString();
      SuperClass.USR = API.recordUSR(SuperClassDecl);
    }

    ObjCInterfaceRecord *ObjCInterfaceRecord = API.addObjCInterface(
        Name, USR, Loc, AvailabilitySet(Decl), Linkage, Comment, Declaration,
        SubHeading, SuperClass, isInSystemHeader(Decl));

    // Record all methods (selectors). This doesn't include automatically
    // synthesized property methods.
    recordObjCMethods(ObjCInterfaceRecord, Decl->methods());
    recordObjCProperties(ObjCInterfaceRecord, Decl->properties());
    recordObjCInstanceVariables(ObjCInterfaceRecord, Decl->ivars());

    return true;
  }
};
} // namespace

DEFINE_SIMPLE_CONVERSION_FUNCTIONS(APISet, CXAPISet)

static void WalkupFromMostDerivedType(LibClangExtractAPIVisitor &Visitor,
                                      Decl *D);

template <typename DeclTy>
static bool WalkupParentContext(DeclContext *Parent,
                                LibClangExtractAPIVisitor &Visitor) {
  if (auto *D = dyn_cast<DeclTy>(Parent)) {
    WalkupFromMostDerivedType(Visitor, D);
    return true;
  }
  return false;
}

static void WalkupFromMostDerivedType(LibClangExtractAPIVisitor &Visitor,
                                      Decl *D) {
  switch (D->getKind()) {
#define ABSTRACT_DECL(DECL)
#define DECL(CLASS, BASE)                                                      \
  case Decl::CLASS:                                                            \
    Visitor.WalkUpFrom##CLASS##Decl(static_cast<CLASS##Decl *>(D));            \
    break;
#include "clang/AST/DeclNodes.inc"
  }

  for (auto *Parent = D->getDeclContext(); Parent != nullptr;
       Parent = Parent->getParent()) {
    if (WalkupParentContext<ObjCContainerDecl>(Parent, Visitor))
      return;
    if (WalkupParentContext<TagDecl>(Parent, Visitor))
      return;
  }
}

static CXString GenerateCXStringFromSymbolGraphData(llvm::json::Object Obj) {
  llvm::SmallString<0> BackingString;
  llvm::raw_svector_ostream OS(BackingString);
  OS << Value(std::move(Obj));
  return cxstring::createDup(BackingString.str());
}

enum CXErrorCode clang_createAPISet(CXTranslationUnit tu, CXAPISet *out_api) {
  if (cxtu::isNotUsableTU(tu) || !out_api)
    return CXError_InvalidArguments;

  ASTUnit *Unit = cxtu::getASTUnit(tu);

  auto &Ctx = Unit->getASTContext();
  auto Lang = Unit->getInputKind().getLanguage();
  APISet *API = new APISet(Ctx.getTargetInfo().getTriple(), Lang,
                           Unit->getMainFileName().str());
  LibClangExtractAPIVisitor Visitor(Ctx, *API);

  for (auto It = Unit->top_level_begin(); It != Unit->top_level_end(); ++It) {
    Visitor.TraverseDecl(*It);
  }

  *out_api = wrap(API);
  return CXError_Success;
}

void clang_disposeAPISet(CXAPISet api) { delete unwrap(api); }

CXString clang_getSymbolGraphForUSR(const char *usr, CXAPISet api) {
  auto *API = unwrap(api);

  if (auto SGF = SymbolGraphSerializer::serializeSingleSymbolSGF(usr, *API))
    return GenerateCXStringFromSymbolGraphData(std::move(*SGF));

  return cxstring::createNull();
}

CXString clang_getSymbolGraphForCursor(CXCursor cursor) {
  cursor = clang_getCursorReferenced(cursor);
  CXCursorKind Kind = clang_getCursorKind(cursor);
  if (!clang_isDeclaration(Kind))
    return cxstring::createNull();

  const Decl *D = cxcursor::getCursorDecl(cursor);

  if (!D)
    return cxstring::createNull();

  CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
  if (!TU)
    return cxstring::createNull();

  ASTUnit *Unit = cxtu::getASTUnit(TU);

  auto &Ctx = Unit->getASTContext();

  auto Lang = Unit->getInputKind().getLanguage();
  APISet API(Ctx.getTargetInfo().getTriple(), Lang,
             Unit->getMainFileName().str());
  LibClangExtractAPIVisitor Visitor(Ctx, API);

  const Decl *CanonicalDecl = D->getCanonicalDecl();
  CanonicalDecl = CanonicalDecl ? CanonicalDecl : D;

  SmallString<128> USR;
  if (index::generateUSRForDecl(CanonicalDecl, USR))
    return cxstring::createNull();

  WalkupFromMostDerivedType(Visitor, const_cast<Decl *>(CanonicalDecl));
  auto *Record = API.findRecordForUSR(USR);

  if (!Record)
    return cxstring::createNull();

  for (const auto &Fragment : Record->Declaration.getFragments()) {
    if (Fragment.Declaration)
      WalkupFromMostDerivedType(Visitor,
                                const_cast<Decl *>(Fragment.Declaration));
  }

  if (auto SGF = SymbolGraphSerializer::serializeSingleSymbolSGF(USR, API))
    return GenerateCXStringFromSymbolGraphData(std::move(*SGF));

  return cxstring::createNull();
}