From 68f8dbd948dd097375c7e49df54322991625183f Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 3 Feb 2015 09:45:52 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@227956 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../clang/ASTMatchers/ASTMatchersInternal.h | 26 +++++++++++++++++++ unittests/ASTMatchers/ASTMatchersTest.cpp | 8 ++++++ 2 files changed, 34 insertions(+) diff --git a/include/clang/ASTMatchers/ASTMatchersInternal.h b/include/clang/ASTMatchers/ASTMatchersInternal.h index ebe5cddb62..8d8c5e9670 100644 --- a/include/clang/ASTMatchers/ASTMatchersInternal.h +++ b/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::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::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::matchesNode( + const FloatingLiteral &Node) const { + return ExpectedValue.compare(Node.getValue()) == llvm::APFloat::cmpEqual; +} + /// \brief A VariadicDynCastAllOfMatcher object is a /// variadic functor that takes a number of Matcher and returns a /// Matcher that matches TargetT nodes that are matched by all of the diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp index d2e9ee19b2..9cc011d3a6 100644 --- a/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/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) { -- 2.40.0