hokein created this revision.
hokein added a reviewer: sammccall.
hokein requested review of this revision.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96693

Files:
  clang/include/clang/Tooling/Syntax/Nodes.h
  clang/include/clang/Tooling/Syntax/Nodes.td
  clang/lib/Tooling/Syntax/BuildTree.cpp
  clang/lib/Tooling/Syntax/Nodes.cpp
  clang/lib/Tooling/Syntax/Synthesis.cpp
  clang/unittests/Tooling/Syntax/BuildTreeTest.cpp
  clang/unittests/Tooling/Syntax/SynthesisTest.cpp

Index: clang/unittests/Tooling/Syntax/SynthesisTest.cpp
===================================================================
--- clang/unittests/Tooling/Syntax/SynthesisTest.cpp
+++ clang/unittests/Tooling/Syntax/SynthesisTest.cpp
@@ -225,15 +225,14 @@
   // nodes in the copy are `modifiable`.
   EXPECT_TRUE(treeDumpEqual(Copy, R"txt(
 TranslationUnit Detached synthesized
-`-SimpleDeclaration synthesized
+`-FunctionDefinition synthesized
   |-'void' synthesized
-  |-DeclaratorList Declarators synthesized
-  | `-SimpleDeclarator ListElement synthesized
-  |   |-'test' synthesized
-  |   `-ParametersAndQualifiers synthesized
-  |     |-'(' OpenParen synthesized
-  |     `-')' CloseParen synthesized
-  `-CompoundStatement synthesized
+  |-SimpleDeclarator Declarator synthesized
+  | |-'test' synthesized
+  | `-ParametersAndQualifiers synthesized
+  |   |-'(' OpenParen synthesized
+  |   `-')' CloseParen synthesized
+  `-CompoundStatement BodyStatement synthesized
     |-'{' OpenParen synthesized
     |-IfStatement Statement synthesized
     | |-'if' IntroducerKeyword synthesized
Index: clang/unittests/Tooling/Syntax/BuildTreeTest.cpp
===================================================================
--- clang/unittests/Tooling/Syntax/BuildTreeTest.cpp
+++ clang/unittests/Tooling/Syntax/BuildTreeTest.cpp
@@ -91,39 +91,6 @@
 INSTANTIATE_TEST_CASE_P(SyntaxTreeTests, BuildSyntaxTreeTest,
                         testing::ValuesIn(allTestClangConfigs()), );
 
-TEST_P(BuildSyntaxTreeTest, Simple) {
-  EXPECT_TRUE(treeDumpEqual(
-      R"cpp(
-int main() {}
-void foo() {}
-)cpp",
-      R"txt(
-TranslationUnit Detached
-|-SimpleDeclaration
-| |-'int'
-| |-DeclaratorList Declarators
-| | `-SimpleDeclarator ListElement
-| |   |-'main'
-| |   `-ParametersAndQualifiers
-| |     |-'(' OpenParen
-| |     `-')' CloseParen
-| `-CompoundStatement
-|   |-'{' OpenParen
-|   `-'}' CloseParen
-`-SimpleDeclaration
-  |-'void'
-  |-DeclaratorList Declarators
-  | `-SimpleDeclarator ListElement
-  |   |-'foo'
-  |   `-ParametersAndQualifiers
-  |     |-'(' OpenParen
-  |     `-')' CloseParen
-  `-CompoundStatement
-    |-'{' OpenParen
-    `-'}' CloseParen
-)txt"));
-}
-
 TEST_P(BuildSyntaxTreeTest, SimpleVariable) {
   EXPECT_TRUE(treeDumpEqual(
       R"cpp(
@@ -151,36 +118,35 @@
 }
 
 TEST_P(BuildSyntaxTreeTest, SimpleFunction) {
-  EXPECT_TRUE(treeDumpEqual(
+  EXPECT_TRUE(treeDumpEqualOnAnnotations(
       R"cpp(
-void foo(int a, int b) {}
+[[void foo(int a, int b) {}]]
 )cpp",
-      R"txt(
+      {R"txt(
 TranslationUnit Detached
-`-SimpleDeclaration
+`-FunctionDefinition
   |-'void'
-  |-DeclaratorList Declarators
-  | `-SimpleDeclarator ListElement
-  |   |-'foo'
-  |   `-ParametersAndQualifiers
-  |     |-'(' OpenParen
-  |     |-ParameterDeclarationList Parameters
-  |     | |-SimpleDeclaration ListElement
-  |     | | |-'int'
-  |     | | `-DeclaratorList Declarators
-  |     | |   `-SimpleDeclarator ListElement
-  |     | |     `-'a'
-  |     | |-',' ListDelimiter
-  |     | `-SimpleDeclaration ListElement
-  |     |   |-'int'
-  |     |   `-DeclaratorList Declarators
-  |     |     `-SimpleDeclarator ListElement
-  |     |       `-'b'
-  |     `-')' CloseParen
-  `-CompoundStatement
+  |-SimpleDeclarator Declarator
+  | |-'foo'
+  | `-ParametersAndQualifiers
+  |   |-'(' OpenParen
+  |   |-ParameterDeclarationList Parameters
+  |   | |-SimpleDeclaration ListElement
+  |   | | |-'int'
+  |   | | `-DeclaratorList Declarators
+  |   | |   `-SimpleDeclarator ListElement
+  |   | |     `-'a'
+  |   | |-',' ListDelimiter
+  |   | `-SimpleDeclaration ListElement
+  |   |   |-'int'
+  |   |   `-DeclaratorList Declarators
+  |   |     `-SimpleDeclarator ListElement
+  |   |       `-'b'
+  |   `-')' CloseParen
+  `-CompoundStatement BodyStatement
     |-'{' OpenParen
     `-'}' CloseParen
-)txt"));
+)txt"}));
 }
 
 TEST_P(BuildSyntaxTreeTest, Simple_BackslashInsideToken) {
@@ -457,59 +423,49 @@
 TEST_P(BuildSyntaxTreeTest, Expressions) {
   // expressions should be wrapped in 'ExpressionStatement' when they appear
   // in a statement position.
-  EXPECT_TRUE(treeDumpEqual(
+  EXPECT_TRUE(treeDumpEqualOnAnnotations(
       R"cpp(
 void test() {
-  test();
-  if (1) test(); else test();
+  [[test();]]
+  [[if (1) test(); else test();]]
 }
 )cpp",
-      R"txt(
-TranslationUnit Detached
-`-SimpleDeclaration
-  |-'void'
-  |-DeclaratorList Declarators
-  | `-SimpleDeclarator ListElement
-  |   |-'test'
-  |   `-ParametersAndQualifiers
-  |     |-'(' OpenParen
-  |     `-')' CloseParen
-  `-CompoundStatement
-    |-'{' OpenParen
-    |-ExpressionStatement Statement
-    | |-CallExpression Expression
-    | | |-IdExpression Callee
-    | | | `-UnqualifiedId UnqualifiedId
-    | | |   `-'test'
-    | | |-'(' OpenParen
-    | | `-')' CloseParen
-    | `-';'
-    |-IfStatement Statement
-    | |-'if' IntroducerKeyword
-    | |-'('
-    | |-ExpressionStatement Condition
-    | | `-IntegerLiteralExpression Expression
-    | |   `-'1' LiteralToken
-    | |-')'
-    | |-ExpressionStatement ThenStatement
-    | | |-CallExpression Expression
-    | | | |-IdExpression Callee
-    | | | | `-UnqualifiedId UnqualifiedId
-    | | | |   `-'test'
-    | | | |-'(' OpenParen
-    | | | `-')' CloseParen
-    | | `-';'
-    | |-'else' ElseKeyword
-    | `-ExpressionStatement ElseStatement
-    |   |-CallExpression Expression
-    |   | |-IdExpression Callee
-    |   | | `-UnqualifiedId UnqualifiedId
-    |   | |   `-'test'
-    |   | |-'(' OpenParen
-    |   | `-')' CloseParen
-    |   `-';'
-    `-'}' CloseParen
-)txt"));
+      {R"txt(
+ExpressionStatement Statement
+|-CallExpression Expression
+| |-IdExpression Callee
+| | `-UnqualifiedId UnqualifiedId
+| |   `-'test'
+| |-'(' OpenParen
+| `-')' CloseParen
+`-';'
+)txt",
+       R"txt(
+IfStatement Statement
+|-'if' IntroducerKeyword
+|-'('
+|-ExpressionStatement Condition
+| `-IntegerLiteralExpression Expression
+|   `-'1' LiteralToken
+|-')'
+|-ExpressionStatement ThenStatement
+| |-CallExpression Expression
+| | |-IdExpression Callee
+| | | `-UnqualifiedId UnqualifiedId
+| | |   `-'test'
+| | |-'(' OpenParen
+| | `-')' CloseParen
+| `-';'
+|-'else' ElseKeyword
+`-ExpressionStatement ElseStatement
+  |-CallExpression Expression
+  | |-IdExpression Callee
+  | | `-UnqualifiedId UnqualifiedId
+  | |   `-'test'
+  | |-'(' OpenParen
+  | `-')' CloseParen
+  `-';'
+)txt"}));
 }
 
 TEST_P(BuildSyntaxTreeTest, UnqualifiedId_Identifier) {
@@ -1429,7 +1385,7 @@
 )txt"}));
 }
 
-TEST_P(BuildSyntaxTreeTest, StringLiteral_Raw) {
+TEST_P(BuildSyntaxTreeTest, DISABLED_StringLiteral_Raw) {
   if (!GetParam().isCXX11OrLater()) {
     return;
   }
@@ -2250,7 +2206,7 @@
 }]]
 )cpp",
       {R"txt(
-CompoundStatement
+CompoundStatement BodyStatement
 |-'{' OpenParen
 |-ExpressionStatement Statement
 | `-MemberExpression Expression
@@ -3002,50 +2958,36 @@
 }
 
 TEST_P(BuildSyntaxTreeTest, MultipleDeclaratorsInsideStatement) {
-  EXPECT_TRUE(treeDumpEqual(
+  EXPECT_TRUE(treeDumpEqualOnAnnotations(
       R"cpp(
 void foo() {
-  int *a, b;
-  typedef int *ta, tb;
+  [[int *a, b]];
+  [[typedef int *ta, tb]];
 }
 )cpp",
-      R"txt(
-TranslationUnit Detached
-`-SimpleDeclaration
-  |-'void'
-  |-DeclaratorList Declarators
-  | `-SimpleDeclarator ListElement
-  |   |-'foo'
-  |   `-ParametersAndQualifiers
-  |     |-'(' OpenParen
-  |     `-')' CloseParen
-  `-CompoundStatement
-    |-'{' OpenParen
-    |-DeclarationStatement Statement
-    | |-SimpleDeclaration
-    | | |-'int'
-    | | `-DeclaratorList Declarators
-    | |   |-SimpleDeclarator ListElement
-    | |   | |-'*'
-    | |   | `-'a'
-    | |   |-',' ListDelimiter
-    | |   `-SimpleDeclarator ListElement
-    | |     `-'b'
-    | `-';'
-    |-DeclarationStatement Statement
-    | |-SimpleDeclaration
-    | | |-'typedef'
-    | | |-'int'
-    | | `-DeclaratorList Declarators
-    | |   |-SimpleDeclarator ListElement
-    | |   | |-'*'
-    | |   | `-'ta'
-    | |   |-',' ListDelimiter
-    | |   `-SimpleDeclarator ListElement
-    | |     `-'tb'
-    | `-';'
-    `-'}' CloseParen
-)txt"));
+      {R"txt(
+SimpleDeclaration
+|-'int'
+`-DeclaratorList Declarators
+  |-SimpleDeclarator ListElement
+  | |-'*'
+  | `-'a'
+  |-',' ListDelimiter
+  `-SimpleDeclarator ListElement
+    `-'b'
+)txt",
+       R"txt(
+SimpleDeclaration
+|-'typedef'
+|-'int'
+`-DeclaratorList Declarators
+  |-SimpleDeclarator ListElement
+  | |-'*'
+  | `-'ta'
+  |-',' ListDelimiter
+  `-SimpleDeclarator ListElement
+    `-'tb'
+)txt"}));
 }
 
 TEST_P(BuildSyntaxTreeTest, SizeTTypedef) {
@@ -3324,16 +3266,15 @@
 };
 )cpp",
       {R"txt(
-SimpleDeclaration
+FunctionDefinition
 |-'static'
 |-'void'
-|-DeclaratorList Declarators
-| `-SimpleDeclarator ListElement
-|   |-'f'
-|   `-ParametersAndQualifiers
-|     |-'(' OpenParen
-|     `-')' CloseParen
-`-CompoundStatement
+|-SimpleDeclarator Declarator
+| |-'f'
+| `-ParametersAndQualifiers
+|   |-'(' OpenParen
+|   `-')' CloseParen
+`-CompoundStatement BodyStatement
   |-'{' OpenParen
   `-'}' CloseParen
 )txt"}));
@@ -3351,19 +3292,18 @@
 [[void S::f(){}]]
 )cpp",
       {R"txt(
-SimpleDeclaration
+FunctionDefinition
 |-'void'
-|-DeclaratorList Declarators
-| `-SimpleDeclarator ListElement
-|   |-NestedNameSpecifier
-|   | |-IdentifierNameSpecifier ListElement
-|   | | `-'S'
-|   | `-'::' ListDelimiter
-|   |-'f'
-|   `-ParametersAndQualifiers
-|     |-'(' OpenParen
-|     `-')' CloseParen
-`-CompoundStatement
+|-SimpleDeclarator Declarator
+| |-NestedNameSpecifier
+| | |-IdentifierNameSpecifier ListElement
+| | | `-'S'
+| | `-'::' ListDelimiter
+| |-'f'
+| `-ParametersAndQualifiers
+|   |-'(' OpenParen
+|   `-')' CloseParen
+`-CompoundStatement BodyStatement
   |-'{' OpenParen
   `-'}' CloseParen
 )txt"}));
@@ -3985,15 +3925,14 @@
 )cpp",
       R"txt(
 TranslationUnit Detached
-`-SimpleDeclaration
+`-FunctionDefinition
   |-'void'
-  |-DeclaratorList Declarators
-  | `-SimpleDeclarator ListElement
-  |   |-'test'
-  |   `-ParametersAndQualifiers
-  |     |-'(' OpenParen
-  |     `-')' CloseParen
-  `-CompoundStatement
+  |-SimpleDeclarator Declarator
+  | |-'test'
+  | `-ParametersAndQualifiers
+  |   |-'(' OpenParen
+  |   `-')' CloseParen
+  `-CompoundStatement BodyStatement
     |-'{' OpenParen
     |-CompoundStatement Statement
     | |-'{' OpenParen
@@ -4023,15 +3962,14 @@
 )cpp",
       R"txt(
 TranslationUnit Detached
-`-SimpleDeclaration
+`-FunctionDefinition
   |-'void'
-  |-DeclaratorList Declarators
-  | `-SimpleDeclarator ListElement
-  |   |-'test'
-  |   `-ParametersAndQualifiers
-  |     |-'(' OpenParen
-  |     `-')' CloseParen
-  `-CompoundStatement
+  |-SimpleDeclarator Declarator
+  | |-'test'
+  | `-ParametersAndQualifiers
+  |   |-'(' OpenParen
+  |   `-')' CloseParen
+  `-CompoundStatement BodyStatement
     |-'{' OpenParen unmodifiable
     `-'}' CloseParen unmodifiable
 )txt"));
@@ -4047,15 +3985,14 @@
 })cpp",
       R"txt(
 TranslationUnit Detached
-`-SimpleDeclaration
+`-FunctionDefinition
   |-'void'
-  |-DeclaratorList Declarators
-  | `-SimpleDeclarator ListElement
-  |   |-'test'
-  |   `-ParametersAndQualifiers
-  |     |-'(' OpenParen
-  |     `-')' CloseParen
-  `-CompoundStatement
+  |-SimpleDeclarator Declarator
+  | |-'test'
+  | `-ParametersAndQualifiers
+  |   |-'(' OpenParen
+  |   `-')' CloseParen
+  `-CompoundStatement BodyStatement
     |-'{' OpenParen
     |-IfStatement Statement
     | |-'if' IntroducerKeyword unmodifiable
@@ -4093,15 +4030,14 @@
 )cpp",
       R"txt(
 TranslationUnit Detached
-`-SimpleDeclaration
+`-FunctionDefinition
   |-'void'
-  |-DeclaratorList Declarators
-  | `-SimpleDeclarator ListElement
-  |   |-'test'
-  |   `-ParametersAndQualifiers
-  |     |-'(' OpenParen
-  |     `-')' CloseParen
-  `-CompoundStatement
+  |-SimpleDeclarator Declarator
+  | |-'test'
+  | `-ParametersAndQualifiers
+  |   |-'(' OpenParen
+  |   `-')' CloseParen
+  `-CompoundStatement BodyStatement
     |-'{' OpenParen
     |-ExpressionStatement Statement
     | |-UnknownExpression Expression
@@ -4132,15 +4068,14 @@
 })cpp",
       R"txt(
 TranslationUnit Detached
-`-SimpleDeclaration
+`-FunctionDefinition
   |-'void'
-  |-DeclaratorList Declarators
-  | `-SimpleDeclarator ListElement
-  |   |-'test'
-  |   `-ParametersAndQualifiers
-  |     |-'(' OpenParen
-  |     `-')' CloseParen
-  `-CompoundStatement
+  |-SimpleDeclarator Declarator
+  | |-'test'
+  | `-ParametersAndQualifiers
+  |   |-'(' OpenParen
+  |   `-')' CloseParen
+  `-CompoundStatement BodyStatement
     |-'{' OpenParen
     |-IfStatement Statement
     | |-'if' IntroducerKeyword unmodifiable
@@ -4177,7 +4112,7 @@
 }]]
 )cpp",
       {R"txt(
-CompoundStatement
+CompoundStatement BodyStatement
 |-'{' OpenParen
 |-ExpressionStatement Statement
 | |-CallExpression Expression
Index: clang/lib/Tooling/Syntax/Synthesis.cpp
===================================================================
--- clang/lib/Tooling/Syntax/Synthesis.cpp
+++ clang/lib/Tooling/Syntax/Synthesis.cpp
@@ -151,6 +151,8 @@
     return new (A.getAllocator()) syntax::ExplicitTemplateInstantiation;
   case syntax::NodeKind::NamespaceDefinition:
     return new (A.getAllocator()) syntax::NamespaceDefinition;
+  case syntax::NodeKind::FunctionDefinition:
+    return new (A.getAllocator()) syntax::FunctionDefinition;
   case syntax::NodeKind::NamespaceAliasDefinition:
     return new (A.getAllocator()) syntax::NamespaceAliasDefinition;
   case syntax::NodeKind::UsingNamespaceDirective:
Index: clang/lib/Tooling/Syntax/Nodes.cpp
===================================================================
--- clang/lib/Tooling/Syntax/Nodes.cpp
+++ clang/lib/Tooling/Syntax/Nodes.cpp
@@ -387,6 +387,16 @@
       findChild(syntax::NodeRole::Declaration));
 }
 
+syntax::SimpleDeclarator *syntax::FunctionDefinition::getDeclarator() {
+  return cast_or_null<syntax::SimpleDeclarator>(
+      findChild(syntax::NodeRole::Declarator));
+}
+
+syntax::Statement *syntax::FunctionDefinition::getFunctionBody() {
+  return cast_or_null<syntax::Statement>(
+      findChild(syntax::NodeRole::BodyStatement));
+}
+
 syntax::Leaf *syntax::ParenDeclarator::getLparen() {
   return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::OpenParen));
 }
Index: clang/lib/Tooling/Syntax/BuildTree.cpp
===================================================================
--- clang/lib/Tooling/Syntax/BuildTree.cpp
+++ clang/lib/Tooling/Syntax/BuildTree.cpp
@@ -707,6 +707,24 @@
     return processDeclaratorAndDeclaration(DD);
   }
 
+  bool WalkUpFromFunctionDecl(FunctionDecl *FF) {
+    if (FF->isDefined()) {
+      auto Declarator = new (allocator()) syntax::SimpleDeclarator();
+      auto DeclaratorRange = getDeclaratorRange(
+          Builder.sourceManager(), FF->getTypeSourceInfo()->getTypeLoc(),
+          getQualifiedNameStart(FF), getInitializerRange(FF));
+      Builder.foldNode(Builder.getRange(DeclaratorRange), Declarator, nullptr);
+      Builder.markChild(Declarator, syntax::NodeRole::Declarator);
+      if (FF->doesThisDeclarationHaveABody())
+        Builder.markStmtChild(FF->getBody(), syntax::NodeRole::BodyStatement);
+      Builder.foldNode(Builder.getRange(FF->getSourceRange()),
+                       new (allocator()) syntax::FunctionDefinition, FF);
+      return true;
+    }
+    // this is not a function definition, build declaration instead.
+    return WalkUpFromDeclaratorDecl(FF);
+  }
+
   bool WalkUpFromTypedefNameDecl(TypedefNameDecl *TD) {
     return processDeclaratorAndDeclaration(TD);
   }
Index: clang/include/clang/Tooling/Syntax/Nodes.td
===================================================================
--- clang/include/clang/Tooling/Syntax/Nodes.td
+++ clang/include/clang/Tooling/Syntax/Nodes.td
@@ -244,6 +244,7 @@
 def SimpleDeclaration : External<Declaration> {}
 def TemplateDeclaration : External<Declaration> {}
 def ExplicitTemplateInstantiation : External<Declaration> {}
+def FunctionDefinition : External<Declaration> {}
 def NamespaceDefinition : External<Declaration> {}
 def NamespaceAliasDefinition : External<Declaration> {}
 def UsingNamespaceDirective : External<Declaration> {}
Index: clang/include/clang/Tooling/Syntax/Nodes.h
===================================================================
--- clang/include/clang/Tooling/Syntax/Nodes.h
+++ clang/include/clang/Tooling/Syntax/Nodes.h
@@ -439,6 +439,17 @@
   Declaration *getDeclaration();
 };
 
+/// Models a function definition. C++ [dcl.fct.def]
+/// e.g. `void func() {}`
+class FunctionDefinition final : public Declaration {
+public:
+  FunctionDefinition() : Declaration(NodeKind::FunctionDefinition) {}
+  static bool classof(const Node *N);
+  SimpleDeclarator *getDeclarator();
+  // FIXME: support "= default;", "= delete;".
+  syntax::Statement *getFunctionBody();
+};
+
 /// namespace <name> { <decls> }
 class NamespaceDefinition final : public Declaration {
 public:
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D96693: [Syntax] Model ... Haojian Wu via Phabricator via cfe-commits

Reply via email to