| 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/Basic/TargetInfo.h" |
| 22 |
#include "clang/ExtractAPI/API.h" |
| 23 |
#include "clang/ExtractAPI/ExtractAPIVisitor.h" |
| 24 |
#include "clang/ExtractAPI/Serialization/SymbolGraphSerializer.h" |
| 25 |
#include "clang/Frontend/ASTUnit.h" |
| 26 |
#include "clang/Frontend/FrontendOptions.h" |
| 27 |
#include "clang/Index/USRGeneration.h" |
| 28 |
#include "llvm/ADT/SmallString.h" |
| 29 |
#include "llvm/Support/CBindingWrapping.h" |
| 30 |
#include "llvm/Support/Casting.h" |
| 31 |
#include "llvm/Support/JSON.h" |
| 32 |
#include "llvm/Support/raw_ostream.h" |
| 33 |
|
| 34 |
using namespace clang; |
| 35 |
using namespace clang::extractapi; |
| 36 |
|
| 37 |
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(APISet, CXAPISet) |
| 38 |
|
| 39 |
static void WalkupFromMostDerivedType(ExtractAPIVisitor &Visitor, Decl *D); |
| 40 |
|
| 41 |
template <typename DeclTy> |
| 42 |
static bool WalkupParentContext(DeclContext *Parent, |
| 43 |
ExtractAPIVisitor &Visitor) { |
| 44 |
if (auto *D = dyn_cast<DeclTy>(Parent)) { |
| 45 |
WalkupFromMostDerivedType(Visitor, D); |
| 46 |
return true; |
| 47 |
} |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
static void WalkupFromMostDerivedType(ExtractAPIVisitor &Visitor, Decl *D) { |
| 52 |
switch (D->getKind()) { |
| 53 |
#define ABSTRACT_DECL(DECL) |
| 54 |
#define DECL(CLASS, BASE) \ |
| 55 |
case Decl::CLASS: \ |
| 56 |
Visitor.WalkUpFrom##CLASS##Decl(static_cast<CLASS##Decl *>(D)); \ |
| 57 |
break; |
| 58 |
#include "clang/AST/DeclNodes.inc" |
| 59 |
} |
| 60 |
|
| 61 |
for (auto *Parent = D->getDeclContext(); Parent != nullptr; |
| 62 |
Parent = Parent->getParent()) { |
| 63 |
if (WalkupParentContext<ObjCContainerDecl>(Parent, Visitor)) |
| 64 |
return; |
| 65 |
if (WalkupParentContext<TagDecl>(Parent, Visitor)) |
| 66 |
return; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
static CXString GenerateCXStringFromSymbolGraphData(llvm::json::Object Obj) { |
| 71 |
llvm::SmallString<0> BackingString; |
| 72 |
llvm::raw_svector_ostream OS(BackingString); |
| 73 |
OS << Value(std::move(Obj)); |
| 74 |
return cxstring::createDup(BackingString.str()); |
| 75 |
} |
| 76 |
|
| 77 |
enum CXErrorCode clang_createAPISet(CXTranslationUnit tu, CXAPISet *out_api) { |
| 78 |
if (cxtu::isNotUsableTU(tu) || !out_api) |
| 79 |
return CXError_InvalidArguments; |
| 80 |
|
| 81 |
ASTUnit *Unit = cxtu::getASTUnit(tu); |
| 82 |
|
| 83 |
auto &Ctx = Unit->getASTContext(); |
| 84 |
auto Lang = Unit->getInputKind().getLanguage(); |
| 85 |
APISet *API = new APISet(Ctx.getTargetInfo().getTriple(), Lang, |
| 86 |
Unit->getMainFileName().str()); |
| 87 |
ExtractAPIVisitor Visitor( |
| 88 |
Ctx, [](SourceLocation Loc) { return true; }, *API); |
| 89 |
|
| 90 |
for (auto It = Unit->top_level_begin(); It != Unit->top_level_end(); ++It) { |
| 91 |
Visitor.TraverseDecl(*It); |
| 92 |
} |
| 93 |
|
| 94 |
*out_api = wrap(API); |
| 95 |
return CXError_Success; |
| 96 |
} |
| 97 |
|
| 98 |
void clang_disposeAPISet(CXAPISet api) { delete unwrap(api); } |
| 99 |
|
| 100 |
CXString clang_getSymbolGraphForUSR(const char *usr, CXAPISet api) { |
| 101 |
auto *API = unwrap(api); |
| 102 |
|
| 103 |
if (auto SGF = SymbolGraphSerializer::serializeSingleSymbolSGF(usr, *API)) |
| 104 |
return GenerateCXStringFromSymbolGraphData(std::move(*SGF)); |
| 105 |
|
| 106 |
return cxstring::createNull(); |
| 107 |
} |
| 108 |
|
| 109 |
CXString clang_getSymbolGraphForCursor(CXCursor cursor) { |
| 110 |
CXCursorKind Kind = clang_getCursorKind(cursor); |
| 111 |
if (clang_isDeclaration(Kind)) { |
| 112 |
const Decl *D = cxcursor::getCursorDecl(cursor); |
| 113 |
|
| 114 |
if (!D) |
| 115 |
return cxstring::createNull(); |
| 116 |
|
| 117 |
CXTranslationUnit TU = cxcursor::getCursorTU(cursor); |
| 118 |
if (!TU) |
| 119 |
return cxstring::createNull(); |
| 120 |
|
| 121 |
ASTUnit *Unit = cxtu::getASTUnit(TU); |
| 122 |
|
| 123 |
auto &Ctx = Unit->getASTContext(); |
| 124 |
auto Lang = Unit->getInputKind().getLanguage(); |
| 125 |
APISet API(Ctx.getTargetInfo().getTriple(), Lang, |
| 126 |
Unit->getMainFileName().str()); |
| 127 |
ExtractAPIVisitor Visitor( |
| 128 |
Ctx, [](SourceLocation Loc) { return true; }, API); |
| 129 |
|
| 130 |
SmallString<128> USR; |
| 131 |
if (index::generateUSRForDecl(D, USR)) |
| 132 |
return cxstring::createNull(); |
| 133 |
|
| 134 |
WalkupFromMostDerivedType(Visitor, const_cast<Decl *>(D)); |
| 135 |
auto *Record = API.findRecordForUSR(USR); |
| 136 |
|
| 137 |
if (!Record) |
| 138 |
return cxstring::createNull(); |
| 139 |
|
| 140 |
for (const auto &Fragment : Record->Declaration.getFragments()) { |
| 141 |
if (Fragment.Declaration) |
| 142 |
WalkupFromMostDerivedType(Visitor, |
| 143 |
const_cast<Decl *>(Fragment.Declaration)); |
| 144 |
} |
| 145 |
|
| 146 |
if (auto SGF = SymbolGraphSerializer::serializeSingleSymbolSGF(USR, API)) |
| 147 |
return GenerateCXStringFromSymbolGraphData(std::move(*SGF)); |
| 148 |
} |
| 149 |
|
| 150 |
return cxstring::createNull(); |
| 151 |
} |