This is an archive of the discontinued LLVM Phabricator instance.

[clang][ExtractAPI] Add support for C++ class templates and concepts
ClosedPublic

Authored by evelez7 on Aug 4 2023, 12:27 AM.

Details

Summary

Add has_template template, DeclarationFragmentBuilder functions, and tests for class templates, specializations/partial specs, and concepts.

Depends on D157007

Diff Detail

Event Timeline

evelez7 created this revision.Aug 4 2023, 12:27 AM
Herald added a project: Restricted Project. · View Herald Transcript
evelez7 requested review of this revision.Aug 4 2023, 12:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 4 2023, 12:27 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
evelez7 updated this revision to Diff 547265.Aug 4 2023, 9:59 AM

Format DeclarationFragments.h, try to address buildbot fail on clang-format

evelez7 updated this revision to Diff 547314.Aug 4 2023, 12:43 PM

Add documentation to template argument name deduction, add missing method declarations to visitor base

evelez7 updated this revision to Diff 547389.Aug 4 2023, 4:06 PM

Abstract the name deduction for generic template args to its own function. It's very helpful in other cases.

evelez7 updated this revision to Diff 547971.Aug 7 2023, 3:04 PM

Refactor template arg name deduction to get as string for FragmentKind::typeIdentifier

evelez7 updated this revision to Diff 548042.Aug 7 2023, 8:11 PM

Add brackets for SerializerBase visit methods

evelez7 updated this revision to Diff 548862.Aug 9 2023, 8:34 PM

Fix template arg identification

dang added a comment.Aug 16 2023, 9:18 AM

Looks mostly good. Quick Question how do we handle inheritance to a template parameter?

clang/include/clang/ExtractAPI/API.h
665

Minor nit, I would prefer for Specialization to be fully spelled out.

clang/include/clang/ExtractAPI/DeclarationFragments.h
191

This is really a model type and should live either in it's own file or in API.h

313

I feel this should be a constructor for Template seeing that this never fails.

clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
435

Where is the cast?

clang/lib/ExtractAPI/DeclarationFragments.cpp
754

is this clang-format formatted?

784

Kinda sad we have to do this. I guess there is no easy way to change the AST to support this?

evelez7 updated this revision to Diff 550937.Aug 16 2023, 4:45 PM
evelez7 marked 5 inline comments as done.

Address review feedback

  • Handle inheriting from a template parameter.
  • Add a new test to check inheriting from a template parameter.
  • Move base class gathering to a new private method for code reuse.
  • Move Template class to API.h and convert getTemplate functions to constructors.
  • Rename all instances of Spec to Specialization.

We didn't handle inheriting from a template parameter, but now we do by serializing the relationship. The parameter does not produce a target USR so it falls back to its name.

clang/include/clang/ExtractAPI/DeclarationFragments.h
191

Moved to API.h

clang/lib/ExtractAPI/DeclarationFragments.cpp
754

Yes.

784

I've just realized that we could just take the the template parameter and insert it arbitrarily but it could easily be the incorrect partial specialization. We'd still need to determine which argument corresponds to the template param.

One optimization is to check if there is only one new param in a partial specialization so that a var template's type is automatically known if it is the new template param, i.e. template<typename T> T x<T, int>

The unfortunate reality is that template parameters are actual Decls (TemplateTypeParmDecl, which do hold the generic's correct name), and TemplateArguments are not. Calling TemplateArgument.getAsDecl() or getAsTemplate always result in assertion errors (when the arg is a template param) since there are no analogous structures. There'd have to be a TemplateTypeArgDecl.

evelez7 updated this revision to Diff 550945.Aug 16 2023, 5:53 PM

Also rename traverseSpec* in SerializerBase.h

dang accepted this revision.Aug 18 2023, 8:02 AM

LGTM

This revision is now accepted and ready to land.Aug 18 2023, 8:02 AM
evelez7 updated this revision to Diff 551591.Aug 18 2023, 11:55 AM

Fix formatting for introduced declarations in DeclarationFragments.h

This revision was landed with ongoing or failed builds.Aug 18 2023, 1:41 PM
This revision was automatically updated to reflect the committed changes.