2018-06-05 01:22:20 +08:00
|
|
|
///===-- Representation.cpp - ClangDoc Representation -----------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2018-06-05 01:22:20 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the merging of different types of infos. The data in the
|
|
|
|
// calling Info is preserved during a merge unless that field is empty or
|
|
|
|
// default. In that case, the data from the parameter Info is used to replace
|
|
|
|
// the empty or default data.
|
|
|
|
//
|
|
|
|
// For most fields, the first decl seen provides the data. Exceptions to this
|
|
|
|
// include the location and description fields, which are collections of data on
|
|
|
|
// all decls related to a given definition. All other fields are ignored in new
|
|
|
|
// decls unless the first seen decl didn't, for whatever reason, incorporate
|
|
|
|
// data on that field (e.g. a forward declared class wouldn't have information
|
|
|
|
// on members on the forward declaration, but would have the class name).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "Representation.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
2019-06-29 03:07:56 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-06-05 01:22:20 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace doc {
|
|
|
|
|
2018-08-03 04:10:17 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const SymbolID EmptySID = SymbolID();
|
2018-06-05 01:22:20 +08:00
|
|
|
|
|
|
|
template <typename T>
|
2018-08-03 04:10:17 +08:00
|
|
|
llvm::Expected<std::unique_ptr<Info>>
|
|
|
|
reduce(std::vector<std::unique_ptr<Info>> &Values) {
|
|
|
|
if (Values.empty())
|
|
|
|
return llvm::make_error<llvm::StringError>(" No values to reduce.\n",
|
|
|
|
llvm::inconvertibleErrorCode());
|
2019-08-15 07:52:23 +08:00
|
|
|
std::unique_ptr<Info> Merged = std::make_unique<T>(Values[0]->USR);
|
2018-06-05 01:22:20 +08:00
|
|
|
T *Tmp = static_cast<T *>(Merged.get());
|
|
|
|
for (auto &I : Values)
|
|
|
|
Tmp->merge(std::move(*static_cast<T *>(I.get())));
|
2018-08-03 04:10:17 +08:00
|
|
|
return std::move(Merged);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the index of the matching child in the vector, or -1 if merge is not
|
|
|
|
// necessary.
|
|
|
|
template <typename T>
|
|
|
|
int getChildIndexIfExists(std::vector<T> &Children, T &ChildToMerge) {
|
|
|
|
for (unsigned long I = 0; I < Children.size(); I++) {
|
|
|
|
if (ChildToMerge.USR == Children[I].USR)
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reduceChildren(std::vector<Reference> &Children,
|
|
|
|
std::vector<Reference> &&ChildrenToMerge) {
|
|
|
|
for (auto &ChildToMerge : ChildrenToMerge) {
|
2019-08-13 02:42:46 +08:00
|
|
|
int mergeIdx = getChildIndexIfExists(Children, ChildToMerge);
|
|
|
|
if (mergeIdx == -1) {
|
2018-08-03 04:10:17 +08:00
|
|
|
Children.push_back(std::move(ChildToMerge));
|
2019-08-13 02:42:46 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Children[mergeIdx].merge(std::move(ChildToMerge));
|
2018-08-03 04:10:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void reduceChildren(std::vector<FunctionInfo> &Children,
|
|
|
|
std::vector<FunctionInfo> &&ChildrenToMerge) {
|
|
|
|
for (auto &ChildToMerge : ChildrenToMerge) {
|
|
|
|
int mergeIdx = getChildIndexIfExists(Children, ChildToMerge);
|
|
|
|
if (mergeIdx == -1) {
|
|
|
|
Children.push_back(std::move(ChildToMerge));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Children[mergeIdx].merge(std::move(ChildToMerge));
|
|
|
|
}
|
2018-06-05 01:22:20 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 04:10:17 +08:00
|
|
|
void reduceChildren(std::vector<EnumInfo> &Children,
|
|
|
|
std::vector<EnumInfo> &&ChildrenToMerge) {
|
|
|
|
for (auto &ChildToMerge : ChildrenToMerge) {
|
|
|
|
int mergeIdx = getChildIndexIfExists(Children, ChildToMerge);
|
|
|
|
if (mergeIdx == -1) {
|
|
|
|
Children.push_back(std::move(ChildToMerge));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Children[mergeIdx].merge(std::move(ChildToMerge));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-06-05 01:22:20 +08:00
|
|
|
// Dispatch function.
|
|
|
|
llvm::Expected<std::unique_ptr<Info>>
|
|
|
|
mergeInfos(std::vector<std::unique_ptr<Info>> &Values) {
|
|
|
|
if (Values.empty())
|
|
|
|
return llvm::make_error<llvm::StringError>("No info values to merge.\n",
|
2018-07-21 02:49:55 +08:00
|
|
|
llvm::inconvertibleErrorCode());
|
2018-06-05 01:22:20 +08:00
|
|
|
|
|
|
|
switch (Values[0]->IT) {
|
|
|
|
case InfoType::IT_namespace:
|
|
|
|
return reduce<NamespaceInfo>(Values);
|
|
|
|
case InfoType::IT_record:
|
|
|
|
return reduce<RecordInfo>(Values);
|
|
|
|
case InfoType::IT_enum:
|
|
|
|
return reduce<EnumInfo>(Values);
|
|
|
|
case InfoType::IT_function:
|
|
|
|
return reduce<FunctionInfo>(Values);
|
|
|
|
default:
|
|
|
|
return llvm::make_error<llvm::StringError>("Unexpected info type.\n",
|
|
|
|
llvm::inconvertibleErrorCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 02:42:46 +08:00
|
|
|
bool Reference::mergeable(const Reference &Other) {
|
|
|
|
return RefType == Other.RefType && USR == Other.USR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reference::merge(Reference &&Other) {
|
|
|
|
assert(mergeable(Other));
|
|
|
|
if (Name.empty())
|
|
|
|
Name = Other.Name;
|
|
|
|
if (Path.empty())
|
|
|
|
Path = Other.Path;
|
|
|
|
if (!IsInGlobalNamespace)
|
|
|
|
IsInGlobalNamespace = Other.IsInGlobalNamespace;
|
|
|
|
}
|
|
|
|
|
2018-06-05 01:22:20 +08:00
|
|
|
void Info::mergeBase(Info &&Other) {
|
|
|
|
assert(mergeable(Other));
|
|
|
|
if (USR == EmptySID)
|
|
|
|
USR = Other.USR;
|
|
|
|
if (Name == "")
|
|
|
|
Name = Other.Name;
|
2019-07-13 02:32:00 +08:00
|
|
|
if (Path == "")
|
|
|
|
Path = Other.Path;
|
2018-06-05 01:22:20 +08:00
|
|
|
if (Namespace.empty())
|
|
|
|
Namespace = std::move(Other.Namespace);
|
|
|
|
// Unconditionally extend the description, since each decl may have a comment.
|
|
|
|
std::move(Other.Description.begin(), Other.Description.end(),
|
|
|
|
std::back_inserter(Description));
|
2019-06-29 02:17:58 +08:00
|
|
|
std::sort(Description.begin(), Description.end());
|
|
|
|
auto Last = std::unique(Description.begin(), Description.end());
|
|
|
|
Description.erase(Last, Description.end());
|
2018-06-05 01:22:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Info::mergeable(const Info &Other) {
|
2018-08-03 04:10:17 +08:00
|
|
|
return IT == Other.IT && USR == Other.USR;
|
2018-06-05 01:22:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolInfo::merge(SymbolInfo &&Other) {
|
|
|
|
assert(mergeable(Other));
|
|
|
|
if (!DefLoc)
|
|
|
|
DefLoc = std::move(Other.DefLoc);
|
|
|
|
// Unconditionally extend the list of locations, since we want all of them.
|
|
|
|
std::move(Other.Loc.begin(), Other.Loc.end(), std::back_inserter(Loc));
|
2019-06-29 02:17:58 +08:00
|
|
|
std::sort(Loc.begin(), Loc.end());
|
|
|
|
auto Last = std::unique(Loc.begin(), Loc.end());
|
|
|
|
Loc.erase(Last, Loc.end());
|
2018-06-05 01:22:20 +08:00
|
|
|
mergeBase(std::move(Other));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NamespaceInfo::merge(NamespaceInfo &&Other) {
|
|
|
|
assert(mergeable(Other));
|
2018-08-03 04:10:17 +08:00
|
|
|
// Reduce children if necessary.
|
|
|
|
reduceChildren(ChildNamespaces, std::move(Other.ChildNamespaces));
|
|
|
|
reduceChildren(ChildRecords, std::move(Other.ChildRecords));
|
|
|
|
reduceChildren(ChildFunctions, std::move(Other.ChildFunctions));
|
|
|
|
reduceChildren(ChildEnums, std::move(Other.ChildEnums));
|
2018-06-05 01:22:20 +08:00
|
|
|
mergeBase(std::move(Other));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecordInfo::merge(RecordInfo &&Other) {
|
|
|
|
assert(mergeable(Other));
|
|
|
|
if (!TagType)
|
|
|
|
TagType = Other.TagType;
|
|
|
|
if (Members.empty())
|
|
|
|
Members = std::move(Other.Members);
|
|
|
|
if (Parents.empty())
|
|
|
|
Parents = std::move(Other.Parents);
|
|
|
|
if (VirtualParents.empty())
|
|
|
|
VirtualParents = std::move(Other.VirtualParents);
|
2018-08-03 04:10:17 +08:00
|
|
|
// Reduce children if necessary.
|
|
|
|
reduceChildren(ChildRecords, std::move(Other.ChildRecords));
|
|
|
|
reduceChildren(ChildFunctions, std::move(Other.ChildFunctions));
|
|
|
|
reduceChildren(ChildEnums, std::move(Other.ChildEnums));
|
2018-06-05 01:22:20 +08:00
|
|
|
SymbolInfo::merge(std::move(Other));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnumInfo::merge(EnumInfo &&Other) {
|
|
|
|
assert(mergeable(Other));
|
|
|
|
if (!Scoped)
|
|
|
|
Scoped = Other.Scoped;
|
|
|
|
if (Members.empty())
|
|
|
|
Members = std::move(Other.Members);
|
|
|
|
SymbolInfo::merge(std::move(Other));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionInfo::merge(FunctionInfo &&Other) {
|
|
|
|
assert(mergeable(Other));
|
|
|
|
if (!IsMethod)
|
|
|
|
IsMethod = Other.IsMethod;
|
|
|
|
if (!Access)
|
|
|
|
Access = Other.Access;
|
|
|
|
if (ReturnType.Type.USR == EmptySID && ReturnType.Type.Name == "")
|
|
|
|
ReturnType = std::move(Other.ReturnType);
|
|
|
|
if (Parent.USR == EmptySID && Parent.Name == "")
|
|
|
|
Parent = std::move(Other.Parent);
|
|
|
|
if (Params.empty())
|
|
|
|
Params = std::move(Other.Params);
|
|
|
|
SymbolInfo::merge(std::move(Other));
|
|
|
|
}
|
|
|
|
|
2019-08-07 02:31:46 +08:00
|
|
|
llvm::SmallString<16> Info::extractName() const {
|
2019-06-29 03:07:56 +08:00
|
|
|
if (!Name.empty())
|
|
|
|
return Name;
|
|
|
|
|
|
|
|
switch (IT) {
|
|
|
|
case InfoType::IT_namespace:
|
|
|
|
// Cover the case where the project contains a base namespace called
|
|
|
|
// 'GlobalNamespace' (i.e. a namespace at the same level as the global
|
|
|
|
// namespace, which would conflict with the hard-coded global namespace name
|
|
|
|
// below.)
|
|
|
|
if (Name == "GlobalNamespace" && Namespace.empty())
|
|
|
|
return llvm::SmallString<16>("@GlobalNamespace");
|
|
|
|
// The case of anonymous namespaces is taken care of in serialization,
|
|
|
|
// so here we can safely assume an unnamed namespace is the global
|
|
|
|
// one.
|
|
|
|
return llvm::SmallString<16>("GlobalNamespace");
|
|
|
|
case InfoType::IT_record:
|
|
|
|
return llvm::SmallString<16>("@nonymous_record_" +
|
|
|
|
toHex(llvm::toStringRef(USR)));
|
|
|
|
case InfoType::IT_enum:
|
|
|
|
return llvm::SmallString<16>("@nonymous_enum_" +
|
|
|
|
toHex(llvm::toStringRef(USR)));
|
|
|
|
case InfoType::IT_function:
|
|
|
|
return llvm::SmallString<16>("@nonymous_function_" +
|
|
|
|
toHex(llvm::toStringRef(USR)));
|
|
|
|
case InfoType::IT_default:
|
|
|
|
return llvm::SmallString<16>("@nonymous_" + toHex(llvm::toStringRef(USR)));
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid InfoType.");
|
|
|
|
return llvm::SmallString<16>("");
|
|
|
|
}
|
|
|
|
|
2019-08-07 02:31:46 +08:00
|
|
|
void Index::sort() {
|
|
|
|
std::sort(Children.begin(), Children.end());
|
|
|
|
for (auto &C : Children)
|
|
|
|
C.sort();
|
|
|
|
}
|
|
|
|
|
2019-08-10 01:49:41 +08:00
|
|
|
ClangDocContext::ClangDocContext(tooling::ExecutionContext *ECtx,
|
|
|
|
bool PublicOnly, StringRef OutDirectory,
|
|
|
|
StringRef SourceRoot, StringRef RepositoryUrl,
|
|
|
|
std::vector<std::string> UserStylesheets,
|
|
|
|
std::vector<std::string> JsScripts)
|
|
|
|
: ECtx(ECtx), PublicOnly(PublicOnly), OutDirectory(OutDirectory),
|
2019-08-16 07:20:42 +08:00
|
|
|
UserStylesheets(UserStylesheets), JsScripts(JsScripts) {
|
|
|
|
llvm::SmallString<128> SourceRootDir(SourceRoot);
|
|
|
|
if (SourceRoot.empty())
|
|
|
|
// If no SourceRoot was provided the current path is used as the default
|
|
|
|
llvm::sys::fs::current_path(SourceRootDir);
|
|
|
|
this->SourceRoot = SourceRootDir.str();
|
2019-08-10 01:49:41 +08:00
|
|
|
if (!RepositoryUrl.empty()) {
|
|
|
|
this->RepositoryUrl = RepositoryUrl;
|
|
|
|
if (!RepositoryUrl.empty() && RepositoryUrl.find("http://") != 0 &&
|
|
|
|
RepositoryUrl.find("https://") != 0)
|
|
|
|
this->RepositoryUrl->insert(0, "https://");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-05 01:22:20 +08:00
|
|
|
} // namespace doc
|
|
|
|
} // namespace clang
|