summaryrefslogtreecommitdiff
path: root/unittests/AsmParser
diff options
context:
space:
mode:
authorAlex Lorenz <arphaman@gmail.com>2015-07-17 22:07:03 +0000
committerAlex Lorenz <arphaman@gmail.com>2015-07-17 22:07:03 +0000
commit4b50ecbf6a5a9cec8fe11256a7853ff168150328 (patch)
treeb1889be4c825ed6eacbd9aea50f6bcabbfe792a2 /unittests/AsmParser
parentb8850e4b6292f3589b2c59133683e64b2f3b39b5 (diff)
AsmParser: Add a function to parse a standalone constant value.
This commit extends the interface provided by the AsmParser library by adding a function that allows the user to parse a standalone contant value. This change is useful for MIR serialization, as it will allow the MIR Parser to parse the constant values in a machine constant pool. Reviewers: Duncan P. N. Exon Smith Differential Revision: http://reviews.llvm.org/D10280 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@242579 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/AsmParser')
-rw-r--r--unittests/AsmParser/AsmParserTest.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/unittests/AsmParser/AsmParserTest.cpp b/unittests/AsmParser/AsmParserTest.cpp
index 9c2081fa2f2..099f5b5f923 100644
--- a/unittests/AsmParser/AsmParserTest.cpp
+++ b/unittests/AsmParser/AsmParserTest.cpp
@@ -10,6 +10,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/AsmParser/SlotMapping.h"
+#include "llvm/IR/Constants.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/SourceMgr.h"
@@ -64,4 +65,51 @@ TEST(AsmParserTest, SlotMappingTest) {
EXPECT_EQ(Mapping.MetadataNodes.count(1), 0u);
}
+TEST(AsmParserTest, TypeAndConstantValueParsing) {
+ LLVMContext &Ctx = getGlobalContext();
+ SMDiagnostic Error;
+ StringRef Source = "define void @test() {\n entry:\n ret void\n}";
+ auto Mod = parseAssemblyString(Source, Error, Ctx);
+ ASSERT_TRUE(Mod != nullptr);
+ auto &M = *Mod;
+
+ const Value *V;
+ V = parseConstantValue("double 3.5", Error, M);
+ ASSERT_TRUE(V);
+ EXPECT_TRUE(V->getType()->isDoubleTy());
+ ASSERT_TRUE(isa<ConstantFP>(V));
+ EXPECT_TRUE(cast<ConstantFP>(V)->isExactlyValue(3.5));
+
+ V = parseConstantValue("i32 42", Error, M);
+ ASSERT_TRUE(V);
+ EXPECT_TRUE(V->getType()->isIntegerTy());
+ ASSERT_TRUE(isa<ConstantInt>(V));
+ EXPECT_TRUE(cast<ConstantInt>(V)->equalsInt(42));
+
+ V = parseConstantValue("<4 x i32> <i32 0, i32 1, i32 2, i32 3>", Error, M);
+ ASSERT_TRUE(V);
+ EXPECT_TRUE(V->getType()->isVectorTy());
+ ASSERT_TRUE(isa<ConstantDataVector>(V));
+
+ V = parseConstantValue("i32 add (i32 1, i32 2)", Error, M);
+ ASSERT_TRUE(V);
+ ASSERT_TRUE(isa<ConstantInt>(V));
+
+ V = parseConstantValue("i8* blockaddress(@test, %entry)", Error, M);
+ ASSERT_TRUE(V);
+ ASSERT_TRUE(isa<BlockAddress>(V));
+
+ EXPECT_FALSE(parseConstantValue("duble 3.25", Error, M));
+ EXPECT_EQ(Error.getMessage(), "expected type");
+
+ EXPECT_FALSE(parseConstantValue("i32 3.25", Error, M));
+ EXPECT_EQ(Error.getMessage(), "floating point constant invalid for type");
+
+ EXPECT_FALSE(parseConstantValue("i32* @foo", Error, M));
+ EXPECT_EQ(Error.getMessage(), "expected a constant value");
+
+ EXPECT_FALSE(parseConstantValue("i32 3, ", Error, M));
+ EXPECT_EQ(Error.getMessage(), "expected end of string");
+}
+
} // end anonymous namespace