Skip to content

[clang-doc] mangle specialization file names #144617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions clang-tools-extra/clang-doc/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ static llvm::Error decodeRecord(const Record &R, std::optional<Location> &Field,
return llvm::Error::success();
}

static llvm::Error decodeRecord(const Record &R,
std::optional<SmallString<16>> &Field,
llvm::StringRef Blob) {
Field.emplace(Blob);
return llvm::Error::success();
}

static llvm::Error decodeRecord(const Record &R, InfoType &Field,
llvm::StringRef Blob) {
switch (auto IT = static_cast<InfoType>(R[0])) {
Expand Down Expand Up @@ -379,6 +386,8 @@ static llvm::Error parseRecord(const Record &R, unsigned ID,
TemplateSpecializationInfo *I) {
if (ID == TEMPLATE_SPECIALIZATION_OF)
return decodeRecord(R, I->SpecializationOf, Blob);
if (ID == TEMPLATE_SPECIALIZATION_MANGLED_NAME)
return decodeRecord(R, I->MangledName, Blob);
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"invalid field for TemplateParamInfo");
}
Expand Down
7 changes: 6 additions & 1 deletion clang-tools-extra/clang-doc/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ static const llvm::IndexedMap<RecordIdDsc, RecordIdToIndexFunctor>
{TEMPLATE_PARAM_CONTENTS, {"Contents", &genStringAbbrev}},
{TEMPLATE_SPECIALIZATION_OF,
{"SpecializationOf", &genSymbolIdAbbrev}},
{TEMPLATE_SPECIALIZATION_MANGLED_NAME,
{"MangledName", &genStringAbbrev}},
{TYPEDEF_USR, {"USR", &genSymbolIdAbbrev}},
{TYPEDEF_NAME, {"Name", &genStringAbbrev}},
{TYPEDEF_DEFLOCATION, {"DefLocation", &genLocationAbbrev}},
Expand Down Expand Up @@ -263,7 +265,8 @@ static const std::vector<std::pair<BlockId, std::vector<RecordId>>>
// Template Blocks.
{BI_TEMPLATE_BLOCK_ID, {}},
{BI_TEMPLATE_PARAM_BLOCK_ID, {TEMPLATE_PARAM_CONTENTS}},
{BI_TEMPLATE_SPECIALIZATION_BLOCK_ID, {TEMPLATE_SPECIALIZATION_OF}}};
{BI_TEMPLATE_SPECIALIZATION_BLOCK_ID,
{TEMPLATE_SPECIALIZATION_OF, TEMPLATE_SPECIALIZATION_MANGLED_NAME}}};

// AbbreviationMap

Expand Down Expand Up @@ -638,6 +641,8 @@ void ClangDocBitcodeWriter::emitBlock(const TemplateInfo &T) {
void ClangDocBitcodeWriter::emitBlock(const TemplateSpecializationInfo &T) {
StreamSubBlockGuard Block(Stream, BI_TEMPLATE_SPECIALIZATION_BLOCK_ID);
emitRecord(T.SpecializationOf, TEMPLATE_SPECIALIZATION_OF);
if (T.MangledName)
emitRecord(T.MangledName->str(), TEMPLATE_SPECIALIZATION_MANGLED_NAME);
for (const auto &P : T.Params)
emitBlock(P);
}
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clang-doc/BitcodeWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ enum RecordId {
REFERENCE_FIELD,
TEMPLATE_PARAM_CONTENTS,
TEMPLATE_SPECIALIZATION_OF,
TEMPLATE_SPECIALIZATION_MANGLED_NAME,
TYPEDEF_USR,
TYPEDEF_NAME,
TYPEDEF_DEFLOCATION,
Expand Down
9 changes: 8 additions & 1 deletion clang-tools-extra/clang-doc/JSONGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,14 @@ Error JSONGenerator::generateDocs(
CreatedDirs.insert(Path);
}

sys::path::append(Path, Info->getFileBaseName() + ".json");
SmallString<16> FileBaseName = Info->getFileBaseName();
if (Info->IT == InfoType::IT_record) {
if (auto Template = static_cast<RecordInfo *>(Info)->Template;
Template && Template->Specialization &&
Template->Specialization->MangledName)
FileBaseName = Template->Specialization->MangledName.value();
}
sys::path::append(Path, FileBaseName + ".json");
FileToInfos[Path].push_back(Info);
}

Expand Down
3 changes: 3 additions & 0 deletions clang-tools-extra/clang-doc/Representation.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ struct TemplateSpecializationInfo {

// Template parameters applying to the specialized record/function.
std::vector<TemplateParamInfo> Params;

// Used to distinguish class specialization file names.
std::optional<SmallString<16>> MangledName;
};

// Records the template information for a struct or function that is a template
Expand Down
8 changes: 8 additions & 0 deletions clang-tools-extra/clang-doc/Serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "BitcodeWriter.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Comment.h"
#include "clang/AST/Mangle.h"
#include "clang/Index/USRGeneration.h"
#include "clang/Lex/Lexer.h"
#include "llvm/ADT/StringExtras.h"
Expand Down Expand Up @@ -909,6 +910,13 @@ emitInfo(const RecordDecl *D, const FullComment *FC, Location Loc,
RI->Template.emplace();
RI->Template->Specialization.emplace();
auto &Specialization = *RI->Template->Specialization;
auto *Mangler = ItaniumMangleContext::create(
D->getASTContext(), D->getASTContext().getDiagnostics());
std::string MangledName;
llvm::raw_string_ostream Stream(MangledName);
Mangler->mangleCXXVTT(dyn_cast<CXXRecordDecl>(D), Stream);
Specialization.MangledName.emplace(MangledName);
delete Mangler;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to keep this mangler somewhere so that it's not allocated/deleted constantly. If it was kept in the Clang-Doc Context, it'd have to be passed specifically to this emitInfo which would break the pattern we have.


// What this is a specialization of.
auto SpecOf = CTSD->getSpecializedTemplateOrPartial();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: rm -rf %t && mkdir -p %t
// RUN: clang-doc --output=%t --format=json --executor=standalone %s
// RUN: FileCheck %s < %t/GlobalNamespace/_ZTT7MyClassIiE.json

template<typename T> class MyClass;

template<> class MyClass<int>;

// CHECK: "Name": "MyClass",
// CHECK: "Template": {
// CHECK-NEXT: "Specialization": {
// CHECK-NEXT: "Parameters": [
// CHECK-NEXT: "int"
// CHECK-NEXT: ],
// CHECK-NEXT: "SpecializationOf": "{{[0-9A-F]*}}"
Loading