diff --git a/src/lib/AST/ASTVisitor.cpp b/src/lib/AST/ASTVisitor.cpp index 89390556bc..b9d4b16984 100644 --- a/src/lib/AST/ASTVisitor.cpp +++ b/src/lib/AST/ASTVisitor.cpp @@ -1137,6 +1137,23 @@ populate(TypedefSymbol& I, clang::TypeAliasTemplateDecl const* D) } } +// A variable with no written initializer can still carry an implicit +// initializer in the AST. Any default-constructed class instance (`T t;`) gets +// an implicit construction expression located at the variable name, so the +// extracted source text is the variable name and would cause a spurious `= t` +// to be rendered in the docs. So, we intercept this case. A written `{}`, a +// copy-initialization such as `= x`, and literal initializers are left alone. +static +bool +isImplicitDefaultInit(clang::Expr const* E) +{ + clang::CXXConstructExpr const* ctor = + dyn_cast(E->IgnoreImplicit()); + return ctor != nullptr + && ctor->getNumArgs() == 0 + && ctor->getParenOrBraceRange().isInvalid(); +} + void ASTVisitor:: populate( @@ -1161,7 +1178,8 @@ populate( I.IsConstinit |= D->hasAttr(); I.IsConstexpr |= D->isConstexpr(); I.IsInline |= D->isInline(); - if (clang::Expr const* E = D->getInit()) + if (clang::Expr const* E = D->getInit(); + E && !isImplicitDefaultInit(E)) { populate(I.Initializer, E); } diff --git a/test-files/golden-tests/symbols/variable/default-constructed-instance.cpp b/test-files/golden-tests/symbols/variable/default-constructed-instance.cpp new file mode 100644 index 0000000000..2ed1bf77f2 --- /dev/null +++ b/test-files/golden-tests/symbols/variable/default-constructed-instance.cpp @@ -0,0 +1,13 @@ +struct tag_base {}; + +/** A default-constructed instance of a same-named tag type (see issue #1238). */ +inline constexpr struct flag final : tag_base {} flag; + +/** A copy-initialized variable. */ +inline constexpr auto flag_copy = flag; + +/** A value-initialized variable. */ +inline constexpr tag_base braced{}; + +/** A variable with a literal initializer. */ +inline constexpr int literal = 7; diff --git a/test-files/golden-tests/symbols/variable/default-constructed-instance.xml b/test-files/golden-tests/symbols/variable/default-constructed-instance.xml new file mode 100644 index 0000000000..e9f9bac5d9 --- /dev/null +++ b/test-files/golden-tests/symbols/variable/default-constructed-instance.xml @@ -0,0 +1,211 @@ + + + + + + namespace + //////////////////////////8= + regular + + 5sB+nY7A37FZIfir11OpIKC12Ok= + 8tmm5w1vdZa8PYl9AaZ5HNozN/k= + V/VOefC+Lirvlyv4ml0GPg3tkb8= + 4BBlDC8MjV0Km82gDuPpLniry80= + w7084vhWPkLDZ/7kZBE9YeheM8E= + /2+SAVa0lxHgZkikhVVwJfO2tZY= + + + + flag + + + default-constructed-instance.cpp + default-constructed-instance.cpp + 4 + 18 + 1 + + + record + 5sB+nY7A37FZIfir11OpIKC12Ok= + regular + //////////////////////////8= + + + brief + + text + A default-constructed instance of a same-named tag type (see issue #1238). + + + + struct + 1 + + + + identifier + 8tmm5w1vdZa8PYl9AaZ5HNozN/k= + tag_base + + + + + + + + + + + + + + tag_base + + + default-constructed-instance.cpp + default-constructed-instance.cpp + 1 + 1 + + + record + 8tmm5w1vdZa8PYl9AaZ5HNozN/k= + regular + //////////////////////////8= + struct + 5sB+nY7A37FZIfir11OpIKC12Ok= + + + + + + + + + + + braced + + + default-constructed-instance.cpp + default-constructed-instance.cpp + 10 + 1 + 1 + + + variable + V/VOefC+Lirvlyv4ml0GPg3tkb8= + regular + //////////////////////////8= + + + brief + + text + A value-initialized variable. + + + + + + identifier + 8tmm5w1vdZa8PYl9AaZ5HNozN/k= + tag_base + + + {} + 1 + 1 + + + flag + + + default-constructed-instance.cpp + default-constructed-instance.cpp + 4 + 1 + + + variable + 4BBlDC8MjV0Km82gDuPpLniry80= + regular + //////////////////////////8= + + + identifier + 5sB+nY7A37FZIfir11OpIKC12Ok= + flag + + + 1 + 1 + + + flag_copy + + + default-constructed-instance.cpp + default-constructed-instance.cpp + 7 + 1 + 1 + + + variable + w7084vhWPkLDZ/7kZBE9YeheM8E= + regular + //////////////////////////8= + + + brief + + text + A copy-initialized variable. + + + + + + flag + 1 + 1 + + + literal + + + default-constructed-instance.cpp + default-constructed-instance.cpp + 13 + 1 + 1 + + + variable + /2+SAVa0lxHgZkikhVVwJfO2tZY= + regular + //////////////////////////8= + + + brief + + text + A variable with a literal initializer. + + + + + + identifier + int + + + 7 + 1 + 1 + +