[clang] 007098d - Add support for `nullptr` in SyntaxTrees

Dmitri Gribenko via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 3 06:54:51 PDT 2020


Author: Eduardo Caldas
Date: 2020-06-03T15:52:33+02:00
New Revision: 007098d7e6b8cf2cf1f1142908840a378d60c545

URL: https://github.com/llvm/llvm-project/commit/007098d7e6b8cf2cf1f1142908840a378d60c545
DIFF: https://github.com/llvm/llvm-project/commit/007098d7e6b8cf2cf1f1142908840a378d60c545.diff

LOG: Add support for `nullptr` in SyntaxTrees

Reviewers: gribozavr2

Reviewed By: gribozavr2

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D81092

Added: 
    

Modified: 
    clang/include/clang/Tooling/Syntax/Nodes.h
    clang/lib/Tooling/Syntax/BuildTree.cpp
    clang/lib/Tooling/Syntax/Nodes.cpp
    clang/unittests/Tooling/Syntax/TreeTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Tooling/Syntax/Nodes.h b/clang/include/clang/Tooling/Syntax/Nodes.h
index e240becbf883..ce4dc35977f1 100644
--- a/clang/include/clang/Tooling/Syntax/Nodes.h
+++ b/clang/include/clang/Tooling/Syntax/Nodes.h
@@ -43,6 +43,7 @@ enum class NodeKind : uint16_t {
   PrefixUnaryOperatorExpression,
   PostfixUnaryOperatorExpression,
   BinaryOperatorExpression,
+  CxxNullPtrExpression,
 
   // Statements.
   UnknownStatement,
@@ -112,6 +113,7 @@ enum class NodeRole : uint8_t {
   BinaryOperatorExpression_leftHandSide,
   BinaryOperatorExpression_operatorToken,
   BinaryOperatorExpression_rightHandSide,
+  CxxNullPtrExpression_keyword,
   CaseStatement_value,
   IfStatement_thenStatement,
   IfStatement_elseKeyword,
@@ -166,6 +168,16 @@ class UnknownExpression final : public Expression {
   }
 };
 
+/// C++11 'nullptr' expression.
+class CxxNullPtrExpression final : public Expression {
+public:
+  CxxNullPtrExpression() : Expression(NodeKind::CxxNullPtrExpression) {}
+  static bool classof(const Node *N) {
+    return N->kind() == NodeKind::CxxNullPtrExpression;
+  }
+  syntax::Leaf *nullPtrKeyword();
+};
+
 /// An abstract class for prefix and postfix unary operators.
 class UnaryOperatorExpression : public Expression {
 public:

diff  --git a/clang/lib/Tooling/Syntax/BuildTree.cpp b/clang/lib/Tooling/Syntax/BuildTree.cpp
index 1c473d872034..dcc7b3e19390 100644
--- a/clang/lib/Tooling/Syntax/BuildTree.cpp
+++ b/clang/lib/Tooling/Syntax/BuildTree.cpp
@@ -608,6 +608,14 @@ class BuildTreeVisitor : public RecursiveASTVisitor<BuildTreeVisitor> {
     return true;
   }
 
+  bool WalkUpFromCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *S) {
+    Builder.markChildToken(S->getLocation(),
+                           syntax::NodeRole::CxxNullPtrExpression_keyword);
+    Builder.foldNode(Builder.getExprRange(S),
+                     new (allocator()) syntax::CxxNullPtrExpression, S);
+    return true;
+  }
+
   bool WalkUpFromUnaryOperator(UnaryOperator *S) {
     Builder.markChildToken(
         S->getOperatorLoc(),

diff  --git a/clang/lib/Tooling/Syntax/Nodes.cpp b/clang/lib/Tooling/Syntax/Nodes.cpp
index f0f1a8ed13c2..3311b8df034c 100644
--- a/clang/lib/Tooling/Syntax/Nodes.cpp
+++ b/clang/lib/Tooling/Syntax/Nodes.cpp
@@ -18,6 +18,8 @@ llvm::raw_ostream &syntax::operator<<(llvm::raw_ostream &OS, NodeKind K) {
     return OS << "TranslationUnit";
   case NodeKind::UnknownExpression:
     return OS << "UnknownExpression";
+  case NodeKind::CxxNullPtrExpression:
+    return OS << "CxxNullPtrExpression";
   case NodeKind::PrefixUnaryOperatorExpression:
     return OS << "PrefixUnaryOperatorExpression";
   case NodeKind::PostfixUnaryOperatorExpression:
@@ -116,6 +118,8 @@ llvm::raw_ostream &syntax::operator<<(llvm::raw_ostream &OS, NodeRole R) {
     return OS << "IfStatement_elseKeyword";
   case syntax::NodeRole::IfStatement_elseStatement:
     return OS << "IfStatement_elseStatement";
+  case syntax::NodeRole::CxxNullPtrExpression_keyword:
+    return OS << "CxxNullPtrExpression_keyword";
   case syntax::NodeRole::UnaryOperatorExpression_operatorToken:
     return OS << "UnaryOperatorExpression_operatorToken";
   case syntax::NodeRole::UnaryOperatorExpression_operand:
@@ -158,6 +162,11 @@ llvm::raw_ostream &syntax::operator<<(llvm::raw_ostream &OS, NodeRole R) {
   llvm_unreachable("invalid role");
 }
 
+syntax::Leaf *syntax::CxxNullPtrExpression::nullPtrKeyword() {
+  return llvm::cast_or_null<syntax::Leaf>(
+      findChild(syntax::NodeRole::CxxNullPtrExpression_keyword));
+}
+
 syntax::Expression *syntax::BinaryOperatorExpression::lhs() {
   return llvm::cast_or_null<syntax::Expression>(
       findChild(syntax::NodeRole::BinaryOperatorExpression_leftHandSide));

diff  --git a/clang/unittests/Tooling/Syntax/TreeTest.cpp b/clang/unittests/Tooling/Syntax/TreeTest.cpp
index 24943acf733e..b8a188a1a76b 100644
--- a/clang/unittests/Tooling/Syntax/TreeTest.cpp
+++ b/clang/unittests/Tooling/Syntax/TreeTest.cpp
@@ -670,6 +670,35 @@ void test() {
 )txt");
 }
 
+TEST_P(SyntaxTreeTest, CxxNullPtrLiteral) {
+  if (!GetParam().isCXX11OrLater()) {
+    return;
+  }
+  expectTreeDumpEqual(
+      R"cpp(
+void test() {
+  nullptr;
+}
+    )cpp",
+      R"txt(
+*: TranslationUnit
+`-SimpleDeclaration
+  |-void
+  |-SimpleDeclarator
+  | |-test
+  | `-ParametersAndQualifiers
+  |   |-(
+  |   `-)
+  `-CompoundStatement
+    |-{
+    |-ExpressionStatement
+    | |-CxxNullPtrExpression
+    | | `-nullptr
+    | `-;
+    `-}
+)txt");
+}
+
 TEST_P(SyntaxTreeTest, PostfixUnaryOperator) {
   expectTreeDumpEqual(
       R"cpp(


        


More information about the cfe-commits mailing list