1//===- CXExtractAPI.cpp - libclang APIs for manipulating CXAPISet ---------===//
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//
9// This file defines all libclang APIs related to manipulation CXAPISet
10//
11//===----------------------------------------------------------------------===//
12
13#include "CXCursor.h"
14#include "CXString.h"
15#include "CXTranslationUnit.h"
16#include "clang-c/CXErrorCode.h"
17#include "clang-c/Documentation.h"
18#include "clang-c/Index.h"
19#include "clang-c/Platform.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclBase.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/Basic/TargetInfo.h"
24#include "clang/ExtractAPI/API.h"
25#include "clang/ExtractAPI/ExtractAPIVisitor.h"
26#include "clang/ExtractAPI/Serialization/SymbolGraphSerializer.h"
27#include "clang/Frontend/ASTUnit.h"
28#include "clang/Frontend/FrontendOptions.h"
29#include "clang/Index/USRGeneration.h"
30#include "llvm/ADT/SmallString.h"
31#include "llvm/Support/CBindingWrapping.h"
32#include "llvm/Support/Casting.h"
33#include "llvm/Support/JSON.h"
34#include "llvm/Support/raw_ostream.h"
35
36using namespace clang;
37using namespace clang::extractapi;
38
39namespace {
40struct LibClangExtractAPIVisitor
41 : ExtractAPIVisitor<LibClangExtractAPIVisitor> {
42 using Base = ExtractAPIVisitor<LibClangExtractAPIVisitor>;
43
44 LibClangExtractAPIVisitor(ASTContext &Context, APISet &API)
45 : ExtractAPIVisitor<LibClangExtractAPIVisitor>(Context, API) {}
46
47 const RawComment *fetchRawCommentForDecl(const Decl *D) const {
48 return Context.getRawCommentForAnyRedecl(D);
49 }
50
51 // We need to visit implementations as well to ensure that when a user clicks
52 // on a method defined only within the implementation that we can still
53 // provide a symbol graph for it.
54 bool VisitObjCImplementationDecl(const ObjCImplementationDecl *Decl) {
55 if (!shouldDeclBeIncluded(Decl))
56 return true;
57
58 auto *Interface = Decl->getClassInterface();
59
60 if (!VisitObjCInterfaceDecl(Decl: Interface))
61 return false;
62
63 SmallString<128> USR;
64 index::generateUSRForDecl(D: Interface, Buf&: USR);
65
66 if (auto *InterfaceRecord = dyn_cast_if_present<ObjCInterfaceRecord>(
67 Val: API.findRecordForUSR(USR))) {
68 recordObjCMethods(Container: InterfaceRecord, Methods: Decl->methods());
69 recordObjCProperties(Container: InterfaceRecord, Properties: Decl->properties());
70 recordObjCInstanceVariables(Container: InterfaceRecord, Ivars: Decl->ivars());
71 }
72 return true;
73 }
74};
75} // namespace
76
77DEFINE_SIMPLE_CONVERSION_FUNCTIONS(APISet, CXAPISet)
78
79// Visits the Decl D and it's transitive DeclContexts recursively, starting from
80// the outer-most context. This is guaranteed to visit every Decl we need in the
81// right order to generate symbol graph information for D.
82static void WalkupFromMostDerivedType(LibClangExtractAPIVisitor &Visitor,
83 Decl *D) {
84 if (auto *Parent = D->getDeclContext())
85 WalkupFromMostDerivedType(Visitor, D: cast<Decl>(Val: Parent));
86
87 switch (D->getKind()) {
88#define ABSTRACT_DECL(DECL)
89#define DECL(CLASS, BASE) \
90 case Decl::CLASS: \
91 Visitor.WalkUpFrom##CLASS##Decl(static_cast<CLASS##Decl *>(D)); \
92 break;
93#include "clang/AST/DeclNodes.inc"
94 }
95}
96
97static CXString GenerateCXStringFromSymbolGraphData(llvm::json::Object Obj) {
98 llvm::SmallString<0> BackingString;
99 llvm::raw_svector_ostream OS(BackingString);
100 OS << llvm::formatv("{0}", Value(std::move(Obj)));
101 return cxstring::createDup(String: BackingString.str());
102}
103
104enum CXErrorCode clang_createAPISet(CXTranslationUnit tu, CXAPISet *out_api) {
105 if (cxtu::isNotUsableTU(TU: tu) || !out_api)
106 return CXError_InvalidArguments;
107
108 ASTUnit *Unit = cxtu::getASTUnit(TU: tu);
109
110 auto &Ctx = Unit->getASTContext();
111 auto Lang = Unit->getInputKind().getLanguage();
112 APISet *API = new APISet(Ctx.getTargetInfo().getTriple(), Lang,
113 Unit->getMainFileName().str());
114 LibClangExtractAPIVisitor Visitor(Ctx, *API);
115
116 for (auto It = Unit->top_level_begin(); It != Unit->top_level_end(); ++It) {
117 Visitor.TraverseDecl(D: *It);
118 }
119
120 *out_api = wrap(P: API);
121 return CXError_Success;
122}
123
124void clang_disposeAPISet(CXAPISet api) { delete unwrap(P: api); }
125
126CXString clang_getSymbolGraphForUSR(const char *usr, CXAPISet api) {
127 auto *API = unwrap(P: api);
128
129 if (auto SGF = SymbolGraphSerializer::serializeSingleSymbolSGF(USR: usr, API: *API))
130 return GenerateCXStringFromSymbolGraphData(std::move(*SGF));
131
132 return cxstring::createNull();
133}
134
135CXString clang_getSymbolGraphForCursor(CXCursor cursor) {
136 cursor = clang_getCursorReferenced(cursor);
137 CXCursorKind Kind = clang_getCursorKind(cursor);
138 if (!clang_isDeclaration(Kind))
139 return cxstring::createNull();
140
141 const Decl *D = cxcursor::getCursorDecl(Cursor: cursor);
142
143 if (!D)
144 return cxstring::createNull();
145
146 CXTranslationUnit TU = cxcursor::getCursorTU(Cursor: cursor);
147 if (!TU)
148 return cxstring::createNull();
149
150 ASTUnit *Unit = cxtu::getASTUnit(TU);
151
152 auto &Ctx = Unit->getASTContext();
153
154 auto Lang = Unit->getInputKind().getLanguage();
155 APISet API(Ctx.getTargetInfo().getTriple(), Lang,
156 Unit->getMainFileName().str());
157 LibClangExtractAPIVisitor Visitor(Ctx, API);
158
159 const Decl *CanonicalDecl = D->getCanonicalDecl();
160 CanonicalDecl = CanonicalDecl ? CanonicalDecl : D;
161
162 SmallString<128> USR;
163 if (index::generateUSRForDecl(D: CanonicalDecl, Buf&: USR))
164 return cxstring::createNull();
165
166 WalkupFromMostDerivedType(Visitor, D: const_cast<Decl *>(CanonicalDecl));
167 auto *Record = API.findRecordForUSR(USR);
168
169 if (!Record)
170 return cxstring::createNull();
171
172 for (const auto &Fragment : Record->Declaration.getFragments()) {
173 if (Fragment.Declaration)
174 WalkupFromMostDerivedType(Visitor,
175 D: const_cast<Decl *>(Fragment.Declaration));
176 }
177
178 if (auto SGF = SymbolGraphSerializer::serializeSingleSymbolSGF(USR, API))
179 return GenerateCXStringFromSymbolGraphData(std::move(*SGF));
180
181 return cxstring::createNull();
182}
183

source code of clang/tools/libclang/CXExtractAPI.cpp