[llvm-branch-commits] [clang] 8000c77 - Make it possible to store a ASTNodeKind in VariantValue

Stephen Kelly via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jan 20 07:49:59 PST 2021


Author: Stephen Kelly
Date: 2021-01-20T15:44:45Z
New Revision: 8000c778532bfe1cc74191e41e19272e54477ed0

URL: https://github.com/llvm/llvm-project/commit/8000c778532bfe1cc74191e41e19272e54477ed0
DIFF: https://github.com/llvm/llvm-project/commit/8000c778532bfe1cc74191e41e19272e54477ed0.diff

LOG: Make it possible to store a ASTNodeKind in VariantValue

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

Added: 
    

Modified: 
    clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
    clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
    clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h b/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
index 140b41dffc40..fa033f49bc90 100644
--- a/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
+++ b/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
@@ -251,6 +251,7 @@ class VariantValue {
   VariantValue(double Double);
   VariantValue(unsigned Unsigned);
   VariantValue(StringRef String);
+  VariantValue(ASTNodeKind NodeKind);
   VariantValue(const VariantMatcher &Matchers);
 
   /// Constructs an \c unsigned value (disambiguation from bool).
@@ -280,6 +281,10 @@ class VariantValue {
   const std::string &getString() const;
   void setString(StringRef String);
 
+  bool isNodeKind() const;
+  const ASTNodeKind &getNodeKind() const;
+  void setNodeKind(ASTNodeKind NodeKind);
+
   /// Matcher value functions.
   bool isMatcher() const;
   const VariantMatcher &getMatcher() const;
@@ -316,7 +321,8 @@ class VariantValue {
     VT_Double,
     VT_Unsigned,
     VT_String,
-    VT_Matcher
+    VT_Matcher,
+    VT_NodeKind
   };
 
   /// All supported value types.
@@ -326,6 +332,7 @@ class VariantValue {
     bool Boolean;
     std::string *String;
     VariantMatcher *Matcher;
+    ASTNodeKind *NodeKind;
   };
 
   ValueType Type;

diff  --git a/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp b/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
index f31dda82a932..d1ecb1e00b91 100644
--- a/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
@@ -268,6 +268,10 @@ VariantValue::VariantValue(StringRef String) : Type(VT_Nothing) {
   setString(String);
 }
 
+VariantValue::VariantValue(ASTNodeKind NodeKind) : Type(VT_Nothing) {
+  setNodeKind(NodeKind);
+}
+
 VariantValue::VariantValue(const VariantMatcher &Matcher) : Type(VT_Nothing) {
   setMatcher(Matcher);
 }
@@ -290,6 +294,9 @@ VariantValue &VariantValue::operator=(const VariantValue &Other) {
   case VT_String:
     setString(Other.getString());
     break;
+  case VT_NodeKind:
+    setNodeKind(Other.getNodeKind());
+    break;
   case VT_Matcher:
     setMatcher(Other.getMatcher());
     break;
@@ -308,6 +315,9 @@ void VariantValue::reset() {
   case VT_Matcher:
     delete Value.Matcher;
     break;
+  case VT_NodeKind:
+    delete Value.NodeKind;
+    break;
   // Cases that do nothing.
   case VT_Boolean:
   case VT_Double:
@@ -378,6 +388,19 @@ void VariantValue::setString(StringRef NewValue) {
   Value.String = new std::string(NewValue);
 }
 
+bool VariantValue::isNodeKind() const { return Type == VT_NodeKind; }
+
+const ASTNodeKind &VariantValue::getNodeKind() const {
+  assert(isNodeKind());
+  return *Value.NodeKind;
+}
+
+void VariantValue::setNodeKind(ASTNodeKind NewValue) {
+  reset();
+  Type = VT_NodeKind;
+  Value.NodeKind = new ASTNodeKind(NewValue);
+}
+
 bool VariantValue::isMatcher() const {
   return Type == VT_Matcher;
 }
@@ -449,6 +472,8 @@ std::string VariantValue::getTypeAsString() const {
   case VT_Boolean: return "Boolean";
   case VT_Double: return "Double";
   case VT_Unsigned: return "Unsigned";
+  case VT_NodeKind:
+    return getNodeKind().asStringRef().str();
   case VT_Nothing: return "Nothing";
   }
   llvm_unreachable("Invalid Type");

diff  --git a/clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp b/clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp
index c08d7fc3ff74..c62a6b385e28 100644
--- a/clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp
+++ b/clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp
@@ -184,6 +184,25 @@ TEST(VariantValueTest, Matcher) {
                   .getTypedMatcher<Stmt>()));
 }
 
+TEST(VariantValueTest, NodeKind) {
+  VariantValue Value = ASTNodeKind::getFromNodeKind<Stmt>();
+  EXPECT_TRUE(Value.isNodeKind());
+  EXPECT_TRUE(Value.getNodeKind().isSame(ASTNodeKind::getFromNodeKind<Stmt>()));
+
+  Value = ASTNodeKind::getFromNodeKind<CXXMethodDecl>();
+  EXPECT_TRUE(Value.isNodeKind());
+  EXPECT_TRUE(Value.getNodeKind().isSame(
+      ASTNodeKind::getFromNodeKind<CXXMethodDecl>()));
+
+  Value.setNodeKind(ASTNodeKind::getFromNodeKind<PointerType>());
+  EXPECT_TRUE(Value.isNodeKind());
+  EXPECT_TRUE(
+      Value.getNodeKind().isSame(ASTNodeKind::getFromNodeKind<PointerType>()));
+
+  Value = 42;
+  EXPECT_TRUE(!Value.isNodeKind());
+}
+
 } // end anonymous namespace
 } // end namespace dynamic
 } // end namespace ast_matchers


        


More information about the llvm-branch-commits mailing list