Skip to content

Commit

Permalink
[Syntax] Build spanning SimpleDecalration for classes, structs, etc
Browse files Browse the repository at this point in the history
When they are free-standing, e.g. `struct X;` or `struct X {};`.
Although this complicates the common case (of free-standing class
declarations), this ensures the less common case (e.g. `struct X {} a;`)
are handled uniformly and produce similar syntax trees.
  • Loading branch information
ilya-biryukov committed Jan 3, 2020
1 parent 7b4badf commit 04f627f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 21 deletions.
10 changes: 7 additions & 3 deletions clang/lib/Tooling/Syntax/BuildTree.cpp
Expand Up @@ -343,9 +343,13 @@ class BuildTreeVisitor : public RecursiveASTVisitor<BuildTreeVisitor> {
}

bool WalkUpFromTagDecl(TagDecl *C) {
// Avoid building UnknownDeclaration here, syntatically 'struct X {}' and
// similar are part of declaration specifiers and do not introduce a new
// top-level declaration.
// FIXME: build the ClassSpecifier node.
if (C->isFreeStanding()) {
// Class is a declaration specifier and needs a spanning declaration node.
Builder.foldNode(Builder.getRange(C),
new (allocator()) syntax::SimpleDeclaration);
return true;
}
return true;
}

Expand Down
81 changes: 63 additions & 18 deletions clang/unittests/Tooling/Syntax/TreeTest.cpp
Expand Up @@ -590,6 +590,50 @@ namespace foo = a;
|-=
|-a
`-;
)txt"},
// Free-standing classes, must live inside a SimpleDeclaration.
{R"cpp(
sturct X;
struct X {};
struct Y *y1;
struct Y {} *y2;
struct {} *a1;
)cpp",
R"txt(
*: TranslationUnit
|-SimpleDeclaration
| |-sturct
| |-X
| `-;
|-SimpleDeclaration
| |-struct
| |-X
| |-{
| |-}
| `-;
|-SimpleDeclaration
| |-struct
| |-Y
| |-*
| |-y1
| `-;
|-SimpleDeclaration
| |-struct
| |-Y
| |-{
| |-}
| |-*
| |-y2
| `-;
`-SimpleDeclaration
|-struct
|-{
|-}
|-*
|-a1
`-;
)txt"},
{R"cpp(
namespace ns {}
Expand Down Expand Up @@ -646,24 +690,25 @@ template <class T> struct X {
| |-class
| `-T
|->
|-struct
|-X
|-{
|-UsingDeclaration
| |-using
| |-T
| |-::
| |-foo
| `-;
|-UsingDeclaration
| |-using
| |-typename
| |-T
| |-::
| |-bar
| `-;
|-}
`-;
`-SimpleDeclaration
|-struct
|-X
|-{
|-UsingDeclaration
| |-using
| |-T
| |-::
| |-foo
| `-;
|-UsingDeclaration
| |-using
| |-typename
| |-T
| |-::
| |-bar
| `-;
|-}
`-;
)txt"},
{R"cpp(
using type = int;
Expand Down

0 comments on commit 04f627f

Please sign in to comment.