diff options
author | Zixu Wang <zixu_wang@apple.com> | 2022-03-21 00:53:28 -0700 |
---|---|---|
committer | Zixu Wang <zixu_wang@apple.com> | 2022-03-22 13:21:57 -0700 |
commit | 89f6b26f1beb2c1344f5cfeb34e405128544c76b (patch) | |
tree | 06b93d32cadc4163d407cc6ac7374b32c311a44d /clang/lib/ExtractAPI/API.cpp | |
parent | 57d02900b54bf162ec476da2ce2bd893dcdbe24b (diff) | |
download | llvm-89f6b26f1beb2c1344f5cfeb34e405128544c76b.zip llvm-89f6b26f1beb2c1344f5cfeb34e405128544c76b.tar.gz llvm-89f6b26f1beb2c1344f5cfeb34e405128544c76b.tar.bz2 |
[clang][extract-api] Refactor ExtractAPI and improve docs
- The name SymbolGraph is inappropriate and confusing for the new library
for clang-extract-api. Refactor and rename things to make it clear that
ExtractAPI is the core functionality and SymbolGraph is one serializer
for the API information.
- Add documentation comments to ExtractAPI classes and methods to improve
readability and clearness of the ExtractAPI work.
Differential Revision: https://reviews.llvm.org/D122160
Diffstat (limited to 'clang/lib/ExtractAPI/API.cpp')
-rw-r--r-- | clang/lib/ExtractAPI/API.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/clang/lib/ExtractAPI/API.cpp b/clang/lib/ExtractAPI/API.cpp new file mode 100644 index 0000000..45eef3d --- /dev/null +++ b/clang/lib/ExtractAPI/API.cpp @@ -0,0 +1,83 @@ +//===- ExtractAPI/API.cpp ---------------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// This file implements the APIRecord and derived record structs, +/// and the APISet class. +/// +//===----------------------------------------------------------------------===// + +#include "clang/ExtractAPI/API.h" +#include "clang/AST/CommentCommandTraits.h" +#include "clang/AST/CommentLexer.h" +#include "clang/AST/RawCommentList.h" +#include "clang/Index/USRGeneration.h" +#include "llvm/Support/Allocator.h" + +using namespace clang::extractapi; +using namespace llvm; + +GlobalRecord *APISet::addGlobal(GVKind Kind, StringRef Name, StringRef USR, + PresumedLoc Loc, + const AvailabilityInfo &Availability, + LinkageInfo Linkage, const DocComment &Comment, + DeclarationFragments Fragments, + DeclarationFragments SubHeading, + FunctionSignature Signature) { + auto Result = Globals.insert({Name, nullptr}); + if (Result.second) { + // Create the record if it does not already exist. + auto Record = APIRecordUniquePtr<GlobalRecord>(new (Allocator) GlobalRecord{ + Kind, Name, USR, Loc, Availability, Linkage, Comment, Fragments, + SubHeading, Signature}); + Result.first->second = std::move(Record); + } + return Result.first->second.get(); +} + +GlobalRecord * +APISet::addGlobalVar(StringRef Name, StringRef USR, PresumedLoc Loc, + const AvailabilityInfo &Availability, LinkageInfo Linkage, + const DocComment &Comment, DeclarationFragments Fragments, + DeclarationFragments SubHeading) { + return addGlobal(GVKind::Variable, Name, USR, Loc, Availability, Linkage, + Comment, Fragments, SubHeading, {}); +} + +GlobalRecord * +APISet::addFunction(StringRef Name, StringRef USR, PresumedLoc Loc, + const AvailabilityInfo &Availability, LinkageInfo Linkage, + const DocComment &Comment, DeclarationFragments Fragments, + DeclarationFragments SubHeading, + FunctionSignature Signature) { + return addGlobal(GVKind::Function, Name, USR, Loc, Availability, Linkage, + Comment, Fragments, SubHeading, Signature); +} + +StringRef APISet::recordUSR(const Decl *D) { + SmallString<128> USR; + index::generateUSRForDecl(D, USR); + return copyString(USR); +} + +StringRef APISet::copyString(StringRef String) { + if (String.empty()) + return {}; + + // No need to allocate memory and copy if the string has already been stored. + if (Allocator.identifyObject(String.data())) + return String; + + void *Ptr = Allocator.Allocate(String.size(), 1); + memcpy(Ptr, String.data(), String.size()); + return StringRef(reinterpret_cast<const char *>(Ptr), String.size()); +} + +APIRecord::~APIRecord() {} + +void GlobalRecord::anchor() {} |