From 3e66eb57a28407675e6bba9c395233cca8cff3a8 Mon Sep 17 00:00:00 2001 From: alandefreitas Date: Tue, 18 Mar 2025 15:14:32 -0300 Subject: [PATCH] support implicit templates #feat fix #868 --- src/lib/AST/ASTVisitor.cpp | 33 +++++++-- src/lib/AST/ASTVisitor.hpp | 10 +++ src/lib/AST/ClangHelpers.cpp | 19 ++++- src/lib/AST/ClangHelpers.hpp | 4 + .../Finalizers/OverloadsFinalizer.cpp | 1 - .../javadoc/copydoc/param-types.adoc | 64 ++++++++++------ .../javadoc/copydoc/param-types.html | 71 ++++++++++++------ .../javadoc/copydoc/param-types.xml | 28 ++++--- .../golden-tests/symbols/function/auto.adoc | 47 ++++++++++++ .../golden-tests/symbols/function/auto.cpp | 7 ++ .../golden-tests/symbols/function/auto.html | 74 +++++++++++++++++++ .../golden-tests/symbols/function/auto.xml | 26 +++++++ .../symbols/function/function-template.adoc | 4 - .../symbols/function/function-template.html | 4 - .../symbols/function/function-template.xml | 37 ++++------ 15 files changed, 334 insertions(+), 95 deletions(-) create mode 100644 test-files/golden-tests/symbols/function/auto.adoc create mode 100644 test-files/golden-tests/symbols/function/auto.cpp create mode 100644 test-files/golden-tests/symbols/function/auto.html create mode 100644 test-files/golden-tests/symbols/function/auto.xml diff --git a/src/lib/AST/ASTVisitor.cpp b/src/lib/AST/ASTVisitor.cpp index 0a22e1300d..95ca287868 100644 --- a/src/lib/AST/ASTVisitor.cpp +++ b/src/lib/AST/ASTVisitor.cpp @@ -562,8 +562,7 @@ populate( std::optional& javadoc, Decl const* D) { - RawComment const* RC = - D->getASTContext().getRawCommentForDeclNoCache(D); + RawComment const* RC = getDocumentation(D); MRDOCS_CHECK_OR(RC, false); comments::FullComment* FC = RC->parse(D->getASTContext(), &sema_.getPreprocessor(), D); @@ -1232,6 +1231,15 @@ ASTVisitor:: populate(TemplateInfo& Template, DeclTy const*, TemplateDeclTy const* TD) { MRDOCS_ASSERT(TD); + MRDOCS_CHECK_OR(!TD->isImplicit()); + if (TemplateParameterList const* TPL = TD->getTemplateParameters(); + !TPL->empty() && + std::ranges::none_of(TPL->asArray(), [](NamedDecl const* ND) { + return !ND->isImplicit(); + })) + { + return; + } TemplateParameterList const* TPL = TD->getTemplateParameters(); populate(Template, TPL); } @@ -1519,14 +1527,29 @@ populate( { return; } - if (TPL->size() > TI.Params.size()) + auto TemplateParameters = TPL->asArray(); + auto ExplicitTemplateParameters = + std::views::filter( + TemplateParameters, + [](NamedDecl const* P) + { + return !P->isImplicit(); + }); + std::size_t const nExplicit = std::ranges::distance( + ExplicitTemplateParameters); + MRDOCS_CHECK_OR(nExplicit); + if (nExplicit > TI.Params.size()) { - TI.Params.resize(TPL->size()); + TI.Params.resize(nExplicit); } - for (std::size_t i = 0; i < TPL->size(); ++i) + auto explicitIt = ExplicitTemplateParameters.begin(); + std::size_t i = 0; + while (explicitIt != ExplicitTemplateParameters.end()) { NamedDecl const* P = TPL->getParam(i); populate(TI.Params[i], P); + ++explicitIt; + ++i; } if (auto* RC = TPL->getRequiresClause()) { diff --git a/src/lib/AST/ASTVisitor.hpp b/src/lib/AST/ASTVisitor.hpp index 48207615b7..c6f717143f 100644 --- a/src/lib/AST/ASTVisitor.hpp +++ b/src/lib/AST/ASTVisitor.hpp @@ -626,6 +626,16 @@ class ASTVisitor void populate(std::optional& Template, DeclTy const* D, TemplateDeclTy const* VTD) { + MRDOCS_CHECK_OR(VTD); + MRDOCS_CHECK_OR(!VTD->isImplicit()); + if (TemplateParameterList const* TPL = VTD->getTemplateParameters(); + !TPL->empty() && + std::ranges::none_of(TPL->asArray(), [](NamedDecl const* ND) { + return !ND->isImplicit(); + })) + { + return; + } if (!Template) { Template.emplace(); diff --git a/src/lib/AST/ClangHelpers.cpp b/src/lib/AST/ClangHelpers.cpp index 1779acf461..9528c77c33 100644 --- a/src/lib/AST/ClangHelpers.cpp +++ b/src/lib/AST/ClangHelpers.cpp @@ -400,11 +400,26 @@ isStaticFileLevelMember(Decl const* D) return false; } +RawComment const* +getDocumentation(Decl const* D) +{ + RawComment const* RC = + D->getASTContext().getRawCommentForDeclNoCache(D); + if (!RC) + { + auto const* TD = dyn_cast(D); + MRDOCS_CHECK_OR(TD, nullptr); + NamedDecl const* ND = TD->getTemplatedDecl(); + MRDOCS_CHECK_OR(ND, nullptr); + RC = ND->getASTContext().getRawCommentForDeclNoCache(ND); + } + return RC; +} + bool isDocumented(Decl const* D) { - return D->getASTContext().getRawCommentForDeclNoCache(D); + return getDocumentation(D) != nullptr; } - } // clang::mrdocs diff --git a/src/lib/AST/ClangHelpers.hpp b/src/lib/AST/ClangHelpers.hpp index e404fca1d3..f30c7ca328 100644 --- a/src/lib/AST/ClangHelpers.hpp +++ b/src/lib/AST/ClangHelpers.hpp @@ -926,6 +926,10 @@ MRDOCS_DECL bool isDocumented(Decl const *D); +MRDOCS_DECL +RawComment const* +getDocumentation(Decl const *D); + template bool isDefinition(DeclTy* D) diff --git a/src/lib/Metadata/Finalizers/OverloadsFinalizer.cpp b/src/lib/Metadata/Finalizers/OverloadsFinalizer.cpp index e98bb24087..28e83af9a5 100644 --- a/src/lib/Metadata/Finalizers/OverloadsFinalizer.cpp +++ b/src/lib/Metadata/Finalizers/OverloadsFinalizer.cpp @@ -51,7 +51,6 @@ findBaseClassPermutation( MRDOCS_CHECK_OR(info, SymbolID::invalid); if (auto* record = dynamic_cast(info)) { - bool overloadsFromBase = false; for (auto const& base: record->Bases) { auto const baseInfo = corpus.find(base.Type->namedSymbol()); diff --git a/test-files/golden-tests/javadoc/copydoc/param-types.adoc b/test-files/golden-tests/javadoc/copydoc/param-types.adoc index 2dd699e3cc..4f67112743 100644 --- a/test-files/golden-tests/javadoc/copydoc/param-types.adoc +++ b/test-files/golden-tests/javadoc/copydoc/param-types.adoc @@ -723,6 +723,17 @@ void [.small]#<># +Auto function + + +[source,cpp,subs="verbatim,replacements,macros,-callouts"] +---- +void +<>(auto a); +---- + +[.small]#<># + Enum param function @@ -756,16 +767,6 @@ void [.small]#<># - -[source,cpp,subs="verbatim,replacements,macros,-callouts"] ----- -template<class a:auto> -void -<>(auto a); ----- - -[.small]#<># - Decltype function @@ -849,6 +850,35 @@ Documentation for a function with a qualified identifier parameter. | The first parameter of g |=== +[#g-0b] +== g + +Auto function + +=== Synopsis + +Declared in `<param‐types.cpp>` + +[source,cpp,subs="verbatim,replacements,macros,-callouts"] +---- +void +g(auto a); +---- + +=== Description + +Documentation for a function with an `auto` parameter. + +=== Parameters + +[cols=2] +|=== +| Name +| Description +| *a* +| The first parameter of g +|=== + [#g-04c] == g @@ -936,20 +966,6 @@ Documentation for the non‐variadic function. | The first parameter of g |=== -[#g-0b] -== g - -=== Synopsis - -Declared in `<param‐types.cpp>` - -[source,cpp,subs="verbatim,replacements,macros,-callouts"] ----- -template<class a:auto> -void -g(auto a); ----- - [#g-0c] == g diff --git a/test-files/golden-tests/javadoc/copydoc/param-types.html b/test-files/golden-tests/javadoc/copydoc/param-types.html index 8e59fabbd9..6eaa6b2d81 100644 --- a/test-files/golden-tests/javadoc/copydoc/param-types.html +++ b/test-files/golden-tests/javadoc/copydoc/param-types.html @@ -916,6 +916,14 @@

Synopses

» more... +Auto function +
+
+void
+g(auto a);
+
+
» more... + Enum param function
 
@@ -940,15 +948,6 @@ 

Synopses

» more... - -
-
-template<class a:auto>
-void
-g(auto a);
-
-
» more... - Decltype function
 
@@ -1067,9 +1066,9 @@ 

Parameters

-

g

+

g

-Enum param function +Auto function
@@ -1080,13 +1079,13 @@

Synopsis

 
 void
-g(testEnum a);
+g(auto a);
 
 

Description

-

Documentation for a function with an enum parameter.

+

Documentation for a function with an auto parameter.

Parameters

@@ -1108,9 +1107,9 @@

Parameters

-

g

+

g

-Variadic function +Enum param function
@@ -1121,13 +1120,13 @@

Synopsis

 
 void
-g(int a, ...);
+g(testEnum a);
 
 

Description

-

Documentation for the variadic function.

+

Documentation for a function with an enum parameter.

Parameters

@@ -1149,9 +1148,9 @@

Parameters

-

g

+

g

-Non-variadic function +Variadic function
@@ -1162,13 +1161,13 @@

Synopsis

 
 void
-g(int a);
+g(int a, ...);
 
 

Description

-

Documentation for the non-variadic function.

+

Documentation for the variadic function.

Parameters

@@ -1190,7 +1189,11 @@

Parameters

-

g

+

g

+
+Non-variadic function + +

Synopsis

@@ -1198,12 +1201,32 @@

Synopsis

Declared in <param-types.cpp>
 
-template<class a:auto>
 void
-g(auto a);
+g(int a);
 
 
+
+

Description

+

Documentation for the non-variadic function.

+
+
+

Parameters

+ + + + + + + + + + + + + +
NameDescription
aThe first parameter of g
+
diff --git a/test-files/golden-tests/javadoc/copydoc/param-types.xml b/test-files/golden-tests/javadoc/copydoc/param-types.xml index 87db46c135..de0134f5a7 100644 --- a/test-files/golden-tests/javadoc/copydoc/param-types.xml +++ b/test-files/golden-tests/javadoc/copydoc/param-types.xml @@ -392,6 +392,25 @@ + + + + + + + + Auto function + + + Documentation for a function with an + auto + parameter. + + + The first parameter of g + + + @@ -447,15 +466,6 @@ - diff --git a/test-files/golden-tests/symbols/function/auto.adoc b/test-files/golden-tests/symbols/function/auto.adoc new file mode 100644 index 0000000000..e2bf8bba8f --- /dev/null +++ b/test-files/golden-tests/symbols/function/auto.adoc @@ -0,0 +1,47 @@ += Reference +:mrdocs: + +[#index] +== Global namespace + +=== Functions + +[cols=2] +|=== +| Name +| Description +| <> +| A function that uses auto +|=== + +[#f] +== f + +A function that uses auto + +=== Synopsis + +Declared in `<auto.cpp>` + +[source,cpp,subs="verbatim,replacements,macros,-callouts"] +---- +int +f(auto value); +---- + +=== Return Value + +The result. + +=== Parameters + +[cols=2] +|=== +| Name +| Description +| *value* +| The value to use. +|=== + + +[.small]#Created with https://www.mrdocs.com[MrDocs]# diff --git a/test-files/golden-tests/symbols/function/auto.cpp b/test-files/golden-tests/symbols/function/auto.cpp new file mode 100644 index 0000000000..7b4fbce90b --- /dev/null +++ b/test-files/golden-tests/symbols/function/auto.cpp @@ -0,0 +1,7 @@ +/** A function that uses auto + + @param value The value to use. + @return The result. + */ +int +f(auto value); diff --git a/test-files/golden-tests/symbols/function/auto.html b/test-files/golden-tests/symbols/function/auto.html new file mode 100644 index 0000000000..9416ed928c --- /dev/null +++ b/test-files/golden-tests/symbols/function/auto.html @@ -0,0 +1,74 @@ + + +Reference + + +
+

Reference

+
+
+

Global namespace

+
+

Functions

+ + + + + + + + + + + +
NameDescription
f A function that uses auto
+ +
+
+
+

f

+
+A function that uses auto + +
+
+
+

Synopsis

+
+Declared in <auto.cpp>
+
+
+int
+f(auto value);
+
+
+
+
+

Return Value

+The result. +
+
+

Parameters

+ + + + + + + + + + + + + +
NameDescription
valueThe value to use.
+
+
+ +
+
+

Created with MrDocs

+
+ + \ No newline at end of file diff --git a/test-files/golden-tests/symbols/function/auto.xml b/test-files/golden-tests/symbols/function/auto.xml new file mode 100644 index 0000000000..920ff40d4a --- /dev/null +++ b/test-files/golden-tests/symbols/function/auto.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + A function that uses auto + + + The result. + + + The value to use. + + + + + diff --git a/test-files/golden-tests/symbols/function/function-template.adoc b/test-files/golden-tests/symbols/function/function-template.adoc index 8031c1f19a..0d4947e26d 100644 --- a/test-files/golden-tests/symbols/function/function-template.adoc +++ b/test-files/golden-tests/symbols/function/function-template.adoc @@ -134,7 +134,6 @@ Declared in `<function‐template.cpp>` [source,cpp,subs="verbatim,replacements,macros,-callouts"] ---- -template<class x:auto> void h0(auto x); ---- @@ -148,9 +147,6 @@ Declared in `<function‐template.cpp>` [source,cpp,subs="verbatim,replacements,macros,-callouts"] ---- -template< - class x:auto, - class auto:2> void h1( auto x, diff --git a/test-files/golden-tests/symbols/function/function-template.html b/test-files/golden-tests/symbols/function/function-template.html index 6bdabace15..46ab5c29df 100644 --- a/test-files/golden-tests/symbols/function/function-template.html +++ b/test-files/golden-tests/symbols/function/function-template.html @@ -167,7 +167,6 @@

Synopsis

Declared in <function-template.cpp>
 
-template<class x:auto>
 void
 h0(auto x);
 
@@ -184,9 +183,6 @@ 

Synopsis

Declared in <function-template.cpp>
 
-template<
-    class x:auto,
-    class auto:2>
 void
 h1(
     auto x,
diff --git a/test-files/golden-tests/symbols/function/function-template.xml b/test-files/golden-tests/symbols/function/function-template.xml
index dddc281af2..dc3e855243 100644
--- a/test-files/golden-tests/symbols/function/function-template.xml
+++ b/test-files/golden-tests/symbols/function/function-template.xml
@@ -55,28 +55,21 @@
       
     
   
-  
-  
+  
+    
+    
+      
+    
+  
+  
+    
+    
+      
+    
+    
+      
+    
+