Skip to content

Commit 535eaa4

Browse files
earnolVladislav Aranov
andauthored
[clang][test] Improve unit tests for Fixed point AST matchers. (#134398)
We have AST matchers for fixed point float numbers since commits 789215d and ff91206. However in those commits the unit tests were not added. Amending the test suit by adding missing tests. Co-authored-by: Vladislav Aranov <[email protected]>
1 parent e1382b3 commit 535eaa4

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

clang/include/clang/AST/Expr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,9 @@ class FixedPointLiteral : public Expr, public APIntStorage {
15641564
/// Returns an empty fixed-point literal.
15651565
static FixedPointLiteral *Create(const ASTContext &C, EmptyShell Empty);
15661566

1567+
/// Returns an internal integer representation of the literal.
1568+
llvm::APInt getValue() const { return APIntStorage::getValue(); }
1569+
15671570
SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
15681571
SourceLocation getEndLoc() const LLVM_READONLY { return Loc; }
15691572

clang/include/clang/ASTMatchers/ASTMatchersInternal.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,9 +1733,10 @@ class ForEachDescendantMatcher : public MatcherInterface<T> {
17331733
template <typename T, typename ValueT>
17341734
class ValueEqualsMatcher : public SingleNodeMatcherInterface<T> {
17351735
static_assert(std::is_base_of<CharacterLiteral, T>::value ||
1736-
std::is_base_of<CXXBoolLiteralExpr, T>::value ||
1737-
std::is_base_of<FloatingLiteral, T>::value ||
1738-
std::is_base_of<IntegerLiteral, T>::value,
1736+
std::is_base_of<CXXBoolLiteralExpr, T>::value ||
1737+
std::is_base_of<FloatingLiteral, T>::value ||
1738+
std::is_base_of<IntegerLiteral, T>::value ||
1739+
std::is_base_of<FixedPointLiteral, T>::value,
17391740
"the node must have a getValue method");
17401741

17411742
public:

clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,67 @@ TEST_P(ASTMatchersTest, FloatLiteral) {
10171017
notMatches("double i = 5.0;", floatLiteral(equals(llvm::APFloat(6.0)))));
10181018
}
10191019

1020+
TEST_P(ASTMatchersTest, FixedPointLiterals) {
1021+
StatementMatcher HasFixedPointLiteral = fixedPointLiteral();
1022+
EXPECT_TRUE(matchesWithFixedpoint("_Fract i = 0.25r;", HasFixedPointLiteral));
1023+
EXPECT_TRUE(
1024+
matchesWithFixedpoint("_Fract i = 0.25hr;", HasFixedPointLiteral));
1025+
EXPECT_TRUE(
1026+
matchesWithFixedpoint("_Fract i = 0.25uhr;", HasFixedPointLiteral));
1027+
EXPECT_TRUE(
1028+
matchesWithFixedpoint("_Fract i = 0.25ur;", HasFixedPointLiteral));
1029+
EXPECT_TRUE(
1030+
matchesWithFixedpoint("_Fract i = 0.25lr;", HasFixedPointLiteral));
1031+
EXPECT_TRUE(
1032+
matchesWithFixedpoint("_Fract i = 0.25ulr;", HasFixedPointLiteral));
1033+
EXPECT_TRUE(matchesWithFixedpoint("_Accum i = 1.25k;", HasFixedPointLiteral));
1034+
EXPECT_TRUE(
1035+
matchesWithFixedpoint("_Accum i = 1.25hk;", HasFixedPointLiteral));
1036+
EXPECT_TRUE(
1037+
matchesWithFixedpoint("_Accum i = 1.25uhk;", HasFixedPointLiteral));
1038+
EXPECT_TRUE(
1039+
matchesWithFixedpoint("_Accum i = 1.25uk;", HasFixedPointLiteral));
1040+
EXPECT_TRUE(
1041+
matchesWithFixedpoint("_Accum i = 1.25lk;", HasFixedPointLiteral));
1042+
EXPECT_TRUE(
1043+
matchesWithFixedpoint("_Accum i = 1.25ulk;", HasFixedPointLiteral));
1044+
EXPECT_TRUE(matchesWithFixedpoint("_Accum decexp1 = 1.575e1k;",
1045+
HasFixedPointLiteral));
1046+
EXPECT_TRUE(
1047+
matchesWithFixedpoint("_Accum hex = 0x1.25fp2k;", HasFixedPointLiteral));
1048+
EXPECT_TRUE(matchesWithFixedpoint("_Sat long _Fract i = 0.25r;",
1049+
HasFixedPointLiteral));
1050+
EXPECT_TRUE(matchesWithFixedpoint("_Sat short _Accum i = 256.0k;",
1051+
HasFixedPointLiteral));
1052+
EXPECT_TRUE(matchesWithFixedpoint(
1053+
"_Accum i = 256.0k;",
1054+
fixedPointLiteral(equals(llvm::APInt(32, 0x800000, true)))));
1055+
EXPECT_TRUE(matchesWithFixedpoint(
1056+
"_Fract i = 0.25ulr;",
1057+
fixedPointLiteral(equals(llvm::APInt(32, 0x40000000, false)))));
1058+
EXPECT_TRUE(matchesWithFixedpoint(
1059+
"_Fract i = 0.5hr;",
1060+
fixedPointLiteral(equals(llvm::APInt(8, 0x40, true)))));
1061+
1062+
EXPECT_TRUE(
1063+
notMatchesWithFixedpoint("short _Accum i = 2u;", HasFixedPointLiteral));
1064+
EXPECT_TRUE(
1065+
notMatchesWithFixedpoint("short _Accum i = 2;", HasFixedPointLiteral));
1066+
EXPECT_TRUE(
1067+
notMatchesWithFixedpoint("_Accum i = 1.25;", HasFixedPointLiteral));
1068+
EXPECT_TRUE(notMatchesWithFixedpoint("_Accum i = (double)(1.25 * 4.5i);",
1069+
HasFixedPointLiteral));
1070+
EXPECT_TRUE(notMatchesWithFixedpoint(
1071+
"_Accum i = 256.0k;",
1072+
fixedPointLiteral(equals(llvm::APInt(32, 0x800001, true)))));
1073+
EXPECT_TRUE(notMatchesWithFixedpoint(
1074+
"_Fract i = 0.25ulr;",
1075+
fixedPointLiteral(equals(llvm::APInt(32, 0x40000001, false)))));
1076+
EXPECT_TRUE(notMatchesWithFixedpoint(
1077+
"_Fract i = 0.5hr;",
1078+
fixedPointLiteral(equals(llvm::APInt(8, 0x41, true)))));
1079+
}
1080+
10201081
TEST_P(ASTMatchersTest, CXXNullPtrLiteralExpr) {
10211082
if (!GetParam().isCXX11OrLater()) {
10221083
return;

clang/unittests/ASTMatchers/ASTMatchersTest.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,20 @@ testing::AssertionResult notMatchesWithOpenMP51(const Twine &Code,
289289
{"-fopenmp=libomp", "-fopenmp-version=51"});
290290
}
291291

292+
template <typename T>
293+
testing::AssertionResult matchesWithFixedpoint(const std::string &Code,
294+
const T &AMatcher) {
295+
return matchesConditionally(Code, AMatcher, true, {"-ffixed-point"},
296+
FileContentMappings(), "input.c");
297+
}
298+
299+
template <typename T>
300+
testing::AssertionResult notMatchesWithFixedpoint(const std::string &Code,
301+
const T &AMatcher) {
302+
return matchesConditionally(Code, AMatcher, false, {"-ffixed-point"},
303+
FileContentMappings(), "input.c");
304+
}
305+
292306
template <typename T>
293307
testing::AssertionResult matchAndVerifyResultConditionally(
294308
const Twine &Code, const T &AMatcher,

0 commit comments

Comments
 (0)