diff options
| author | Daniel Jasper <djasper@google.com> | 2015-02-03 09:45:52 +0000 |
|---|---|---|
| committer | Daniel Jasper <djasper@google.com> | 2015-02-03 09:45:52 +0000 |
| commit | d142381cd8eff4600948086992b320fc6b69f358 (patch) | |
| tree | 98e7bbfbfdddf896ec420f281aef71a6a529abba | |
| parent | a57746b87102041d3220d87ec77da31f2f7da465 (diff) | |
| download | bcm5719-llvm-d142381cd8eff4600948086992b320fc6b69f358.tar.gz bcm5719-llvm-d142381cd8eff4600948086992b320fc6b69f358.zip | |
Add some overloads so that floating point literals can be AST matched properly.
I am not entirely sure whether the implemented sematics are ideal. In
particular, should floatLiteral(equals(0.5)) match "0.5f" and should
floatLiteral(equals(0.5f)) match "0.5". With the overloads in this
patch, the answer to both questions is yes, but I am happy to change
that.
llvm-svn: 227956
| -rw-r--r-- | clang/include/clang/ASTMatchers/ASTMatchersInternal.h | 26 | ||||
| -rw-r--r-- | clang/unittests/ASTMatchers/ASTMatchersTest.cpp | 8 |
2 files changed, 34 insertions, 0 deletions
diff --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h index ebe5cddb622..8d8c5e9670e 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h +++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -1456,6 +1456,32 @@ private: const ValueT ExpectedValue; }; +/// \brief Template specializations to easily write matchers for floating point +/// literals. +inline template <> +bool ValueEqualsMatcher<FloatingLiteral, double>::matchesNode( + const FloatingLiteral &Node) const { + if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle) + return Node.getValue().convertToFloat() == ExpectedValue; + if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble) + return Node.getValue().convertToDouble() == ExpectedValue; + return false; +} +inline template <> +bool ValueEqualsMatcher<FloatingLiteral, float>::matchesNode( + const FloatingLiteral &Node) const { + if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle) + return Node.getValue().convertToFloat() == ExpectedValue; + if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble) + return Node.getValue().convertToDouble() == ExpectedValue; + return false; +} +inline template <> +bool ValueEqualsMatcher<FloatingLiteral, llvm::APFloat>::matchesNode( + const FloatingLiteral &Node) const { + return ExpectedValue.compare(Node.getValue()) == llvm::APFloat::cmpEqual; +} + /// \brief A VariadicDynCastAllOfMatcher<SourceT, TargetT> object is a /// variadic functor that takes a number of Matcher<TargetT> and returns a /// Matcher<SourceT> that matches TargetT nodes that are matched by all of the diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp index d2e9ee19b2c..9cc011d3a6a 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -2104,8 +2104,16 @@ TEST(Matcher, FloatLiterals) { EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral)); EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral)); EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral)); + EXPECT_TRUE(matches("double i = 5.0;", floatLiteral(equals(5.0)))); + EXPECT_TRUE(matches("double i = 5.0;", floatLiteral(equals(5.0f)))); + EXPECT_TRUE( + matches("double i = 5.0;", floatLiteral(equals(llvm::APFloat(5.0))))); EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral)); + EXPECT_TRUE(notMatches("double i = 5.0;", floatLiteral(equals(6.0)))); + EXPECT_TRUE(notMatches("double i = 5.0;", floatLiteral(equals(6.0f)))); + EXPECT_TRUE( + notMatches("double i = 5.0;", floatLiteral(equals(llvm::APFloat(6.0))))); } TEST(Matcher, NullPtrLiteral) { |

