Skip to content

Commit

Permalink
Find the record name for an anonymous typedef record.
Browse files Browse the repository at this point in the history
  • Loading branch information
danakj committed Sep 13, 2023
1 parent 063c660 commit aba70fb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 6 additions & 1 deletion subdoc/lib/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,12 @@ struct RecordId {
explicit RecordId(std::string name) : name(sus::move(name)) {}
explicit RecordId(std::string_view name) : name(name) {}
explicit RecordId(const clang::RecordDecl& decl)
: name(decl.getNameAsString()) {}
: name([&]() {
if (auto* t = decl.getTypedefNameForAnonDecl())
return t->getNameAsString();
else
return decl.getNameAsString();
}()) {}

std::string name;

Expand Down
9 changes: 8 additions & 1 deletion subdoc/lib/visit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,18 @@ class Visitor : public clang::RecursiveASTVisitor<Visitor> {
}
}

std::string name = [&]() {
if (auto* t = decl->getTypedefNameForAnonDecl())
return t->getNameAsString();
else
return decl->getNameAsString();
}();

Comment comment =
make_db_comment(decl->getASTContext(), raw_comment, decl->getName());
auto re = RecordElement(
iter_namespace_path(decl).collect_vec(), sus::move(comment),
decl->getNameAsString(),
sus::move(name),
iter_record_path(parent_record_decl)
.map([](std::string_view&& v) { return std::string(v); })
.collect_vec(),
Expand Down
13 changes: 13 additions & 0 deletions subdoc/tests/records_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,16 @@ TEST_F(SubDocTest, ReplaceDocSelf) {
EXPECT_TRUE(has_dtor_comment(db, "6:7", "<p>Comment headline S 3.</p>"));
EXPECT_TRUE(has_method_comment(db, "8:7", "<p>Comment headline S 4.</p>"));
}

TEST_F(SubDocTest, AnonStruct) {
auto result = run_code(R"(
/// Comment headline one
typedef struct {} S;
/// Comment headline two
typedef struct {} T;
)");
ASSERT_TRUE(result.is_ok());
subdoc::Database db = sus::move(result).unwrap();
EXPECT_TRUE(has_record_comment(db, "2:5", "<p>Comment headline one</p>"));
EXPECT_TRUE(has_record_comment(db, "4:5", "<p>Comment headline two</p>"));
}

0 comments on commit aba70fb

Please sign in to comment.