reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
//===- unittest/ASTMatchers/Dynamic/VariantValueTest.cpp - VariantValue unit tests -===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===-----------------------------------------------------------------------------===//

#include "../ASTMatchersTest.h"
#include "clang/ASTMatchers/Dynamic/VariantValue.h"
#include "gtest/gtest.h"

namespace clang {
namespace ast_matchers {
namespace dynamic {
namespace {

TEST(VariantValueTest, Unsigned) {
  const unsigned kUnsigned = 17;
  VariantValue Value = kUnsigned;

  EXPECT_TRUE(Value.isUnsigned());
  EXPECT_EQ(kUnsigned, Value.getUnsigned());

  EXPECT_TRUE(Value.hasValue());
  EXPECT_FALSE(Value.isString());
  EXPECT_FALSE(Value.isMatcher());
}

TEST(VariantValueTest, String) {
  const StringRef kString = "string";
  VariantValue Value = kString;

  EXPECT_TRUE(Value.isString());
  EXPECT_EQ(kString, Value.getString());
  EXPECT_EQ("String", Value.getTypeAsString());

  EXPECT_TRUE(Value.hasValue());
  EXPECT_FALSE(Value.isUnsigned());
  EXPECT_FALSE(Value.isMatcher());
}

TEST(VariantValueTest, DynTypedMatcher) {
  VariantValue Value = VariantMatcher::SingleMatcher(stmt());

  EXPECT_TRUE(Value.hasValue());
  EXPECT_FALSE(Value.isUnsigned());
  EXPECT_FALSE(Value.isString());

  EXPECT_TRUE(Value.isMatcher());
  EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Decl>());
  EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<UnaryOperator>());
  EXPECT_EQ("Matcher<Stmt>", Value.getTypeAsString());

  // Can only convert to compatible matchers.
  Value = VariantMatcher::SingleMatcher(recordDecl());
  EXPECT_TRUE(Value.isMatcher());
  EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Decl>());
  EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<UnaryOperator>());
  EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString());

  Value = VariantMatcher::SingleMatcher(ignoringImpCasts(expr()));
  EXPECT_TRUE(Value.isMatcher());
  EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Decl>());
  EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Stmt>());
  EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Expr>());
  EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<IntegerLiteral>());
  EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<GotoStmt>());
  EXPECT_EQ("Matcher<Expr>", Value.getTypeAsString());
}

TEST(VariantValueTest, Assignment) {
  VariantValue Value = StringRef("A");
  EXPECT_TRUE(Value.isString());
  EXPECT_EQ("A", Value.getString());
  EXPECT_TRUE(Value.hasValue());
  EXPECT_FALSE(Value.isBoolean());
  EXPECT_FALSE(Value.isDouble());
  EXPECT_FALSE(Value.isUnsigned());
  EXPECT_FALSE(Value.isMatcher());
  EXPECT_EQ("String", Value.getTypeAsString());

  Value = VariantMatcher::SingleMatcher(recordDecl());
  EXPECT_TRUE(Value.hasValue());
  EXPECT_FALSE(Value.isBoolean());
  EXPECT_FALSE(Value.isDouble());
  EXPECT_FALSE(Value.isUnsigned());
  EXPECT_FALSE(Value.isString());
  EXPECT_TRUE(Value.isMatcher());
  EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Decl>());
  EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<UnaryOperator>());
  EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString());

  Value = true;
  EXPECT_TRUE(Value.isBoolean());
  EXPECT_EQ(true, Value.getBoolean());
  EXPECT_TRUE(Value.hasValue());
  EXPECT_FALSE(Value.isUnsigned());
  EXPECT_FALSE(Value.isMatcher());
  EXPECT_FALSE(Value.isString());

  Value = 3.14;
  EXPECT_TRUE(Value.isDouble());
  EXPECT_EQ(3.14, Value.getDouble());
  EXPECT_TRUE(Value.hasValue());
  EXPECT_FALSE(Value.isBoolean());
  EXPECT_FALSE(Value.isUnsigned());
  EXPECT_FALSE(Value.isMatcher());
  EXPECT_FALSE(Value.isString());

  Value = 17;
  EXPECT_TRUE(Value.isUnsigned());
  EXPECT_EQ(17U, Value.getUnsigned());
  EXPECT_FALSE(Value.isBoolean());
  EXPECT_FALSE(Value.isDouble());
  EXPECT_TRUE(Value.hasValue());
  EXPECT_FALSE(Value.isMatcher());
  EXPECT_FALSE(Value.isString());

  Value = VariantValue();
  EXPECT_FALSE(Value.hasValue());
  EXPECT_FALSE(Value.isBoolean());
  EXPECT_FALSE(Value.isDouble());
  EXPECT_FALSE(Value.isUnsigned());
  EXPECT_FALSE(Value.isString());
  EXPECT_FALSE(Value.isMatcher());
  EXPECT_EQ("Nothing", Value.getTypeAsString());
}

TEST(VariantValueTest, ImplicitBool) {
  VariantValue Value;
  bool IfTrue = false;
  if (Value) {
    IfTrue = true;
  }
  EXPECT_FALSE(IfTrue);
  EXPECT_TRUE(!Value);

  Value = StringRef();
  IfTrue = false;
  if (Value) {
    IfTrue = true;
  }
  EXPECT_TRUE(IfTrue);
  EXPECT_FALSE(!Value);
}

TEST(VariantValueTest, Matcher) {
  EXPECT_TRUE(matches("class X {};", VariantValue(VariantMatcher::SingleMatcher(
                                                      recordDecl(hasName("X"))))
                                         .getMatcher()
                                         .getTypedMatcher<Decl>()));
  EXPECT_TRUE(
      matches("int x;", VariantValue(VariantMatcher::SingleMatcher(varDecl()))
                            .getMatcher()
                            .getTypedMatcher<Decl>()));
  EXPECT_TRUE(
      matches("int foo() { return 1 + 1; }",
              VariantValue(VariantMatcher::SingleMatcher(functionDecl()))
                  .getMatcher()
                  .getTypedMatcher<Decl>()));
  // Can't get the wrong matcher.
  EXPECT_FALSE(VariantValue(VariantMatcher::SingleMatcher(varDecl()))
                   .getMatcher()
                   .hasTypedMatcher<Stmt>());
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
  // Trying to get the wrong matcher fails an assertion in Matcher<T>.  We don't
  // do this test when building with MSVC because its debug C runtime prints the
  // assertion failure message as a wide string, which gtest doesn't understand.
  EXPECT_DEATH(VariantValue(VariantMatcher::SingleMatcher(varDecl()))
                   .getMatcher()
                   .getTypedMatcher<Stmt>(),
               "hasTypedMatcher");
#endif

  EXPECT_FALSE(matches(
      "int x;", VariantValue(VariantMatcher::SingleMatcher(functionDecl()))
                    .getMatcher()
                    .getTypedMatcher<Decl>()));
  EXPECT_FALSE(
      matches("int foo() { return 1 + 1; }",
              VariantValue(VariantMatcher::SingleMatcher(declRefExpr()))
                  .getMatcher()
                  .getTypedMatcher<Stmt>()));
}

} // end anonymous namespace
} // end namespace dynamic
} // end namespace ast_matchers
} // end namespace clang