summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/AsmParser/AsmParserTest.cpp
diff options
context:
space:
mode:
authorQuentin Colombet <qcolombet@apple.com>2016-03-08 00:37:07 +0000
committerQuentin Colombet <qcolombet@apple.com>2016-03-08 00:37:07 +0000
commitdafed5d7d82613ca153c1bee70addb17d6d20c3a (patch)
tree55eff0c029daa02615e5e18769ebe0bcf26f990c /llvm/unittests/AsmParser/AsmParserTest.cpp
parentb1bd398ceb5bead0251ec0f40ba5db0177e4a2df (diff)
downloadbcm5719-llvm-dafed5d7d82613ca153c1bee70addb17d6d20c3a.tar.gz
bcm5719-llvm-dafed5d7d82613ca153c1bee70addb17d6d20c3a.zip
[AsmParser] Expose an API to parse a string starting with a type.
Without actually parsing a type it is difficult to perdict where the type definition ends. In other words, instead of expecting the user of the parser API to hand over only the relevant bits of the string being parsed, take the whole string, parse the type, and get back the number of characters that have been read. This will be used by the MIR testing infrastructure. llvm-svn: 262884
Diffstat (limited to 'llvm/unittests/AsmParser/AsmParserTest.cpp')
-rw-r--r--llvm/unittests/AsmParser/AsmParserTest.cpp143
1 files changed, 143 insertions, 0 deletions
diff --git a/llvm/unittests/AsmParser/AsmParserTest.cpp b/llvm/unittests/AsmParser/AsmParserTest.cpp
index c881c4e7ca4..38810cd7c7a 100644
--- a/llvm/unittests/AsmParser/AsmParserTest.cpp
+++ b/llvm/unittests/AsmParser/AsmParserTest.cpp
@@ -270,6 +270,149 @@ TEST(AsmParserTest, TypeWithSlotMappingParsing) {
Ty = PT->getElementType();
ASSERT_TRUE(Ty->isIntegerTy());
ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
+
+ // Check that we reject types with garbage.
+ Ty = parseType("i32 garbage", Error, M, &Mapping);
+ ASSERT_TRUE(!Ty);
+}
+
+TEST(AsmParserTest, TypeAtBeginningWithSlotMappingParsing) {
+ LLVMContext &Ctx = getGlobalContext();
+ SMDiagnostic Error;
+ StringRef Source =
+ "%st = type { i32, i32 }\n"
+ "@v = common global [50 x %st] zeroinitializer, align 16\n"
+ "%0 = type { i32, i32, i32, i32 }\n"
+ "@g = common global [50 x %0] zeroinitializer, align 16\n"
+ "define void @marker4(i64 %d) {\n"
+ "entry:\n"
+ " %conv = trunc i64 %d to i32\n"
+ " store i32 %conv, i32* getelementptr inbounds "
+ " ([50 x %st], [50 x %st]* @v, i64 0, i64 0, i32 0), align 16\n"
+ " store i32 %conv, i32* getelementptr inbounds "
+ " ([50 x %0], [50 x %0]* @g, i64 0, i64 0, i32 0), align 16\n"
+ " ret void\n"
+ "}";
+ SlotMapping Mapping;
+ auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
+ ASSERT_TRUE(Mod != nullptr);
+ auto &M = *Mod;
+ unsigned Read;
+
+ // Check we properly parse integer types.
+ Type *Ty;
+ Ty = parseTypeAtBeginning("i32", Read, Error, M, &Mapping);
+ ASSERT_TRUE(Ty);
+ ASSERT_TRUE(Ty->isIntegerTy());
+ ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
+ ASSERT_TRUE(Read == 3);
+
+ // Check we properly parse integer types with exotic size.
+ Ty = parseTypeAtBeginning("i13", Read, Error, M, &Mapping);
+ ASSERT_TRUE(Ty);
+ ASSERT_TRUE(Ty->isIntegerTy());
+ ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 13);
+ ASSERT_TRUE(Read == 3);
+
+ // Check we properly parse floating point types.
+ Ty = parseTypeAtBeginning("float", Read, Error, M, &Mapping);
+ ASSERT_TRUE(Ty);
+ ASSERT_TRUE(Ty->isFloatTy());
+ ASSERT_TRUE(Read == 5);
+
+ Ty = parseTypeAtBeginning("double", Read, Error, M, &Mapping);
+ ASSERT_TRUE(Ty);
+ ASSERT_TRUE(Ty->isDoubleTy());
+ ASSERT_TRUE(Read == 6);
+
+ // Check we properly parse struct types.
+ // Named struct.
+ Ty = parseTypeAtBeginning("%st", Read, Error, M, &Mapping);
+ ASSERT_TRUE(Ty);
+ ASSERT_TRUE(Ty->isStructTy());
+ ASSERT_TRUE(Read == 3);
+
+ // Check the details of the struct.
+ StructType *ST = cast<StructType>(Ty);
+ ASSERT_TRUE(ST->getNumElements() == 2);
+ for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
+ Ty = ST->getElementType(i);
+ ASSERT_TRUE(Ty->isIntegerTy());
+ ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
+ }
+
+ // Anonymous struct.
+ Ty = parseTypeAtBeginning("%0", Read, Error, M, &Mapping);
+ ASSERT_TRUE(Ty);
+ ASSERT_TRUE(Ty->isStructTy());
+ ASSERT_TRUE(Read == 2);
+
+ // Check the details of the struct.
+ ST = cast<StructType>(Ty);
+ ASSERT_TRUE(ST->getNumElements() == 4);
+ for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
+ Ty = ST->getElementType(i);
+ ASSERT_TRUE(Ty->isIntegerTy());
+ ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
+ }
+
+ // Check we properly parse vector types.
+ Ty = parseTypeAtBeginning("<5 x i32>", Read, Error, M, &Mapping);
+ ASSERT_TRUE(Ty);
+ ASSERT_TRUE(Ty->isVectorTy());
+ ASSERT_TRUE(Read == 9);
+
+ // Check the details of the vector.
+ VectorType *VT = cast<VectorType>(Ty);
+ ASSERT_TRUE(VT->getNumElements() == 5);
+ ASSERT_TRUE(VT->getBitWidth() == 160);
+ Ty = VT->getElementType();
+ ASSERT_TRUE(Ty->isIntegerTy());
+ ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
+
+ // Opaque struct.
+ Ty = parseTypeAtBeginning("%opaque", Read, Error, M, &Mapping);
+ ASSERT_TRUE(Ty);
+ ASSERT_TRUE(Ty->isStructTy());
+ ASSERT_TRUE(Read == 7);
+
+ ST = cast<StructType>(Ty);
+ ASSERT_TRUE(ST->isOpaque());
+
+ // Check we properly parse pointer types.
+ // One indirection.
+ Ty = parseTypeAtBeginning("i32*", Read, Error, M, &Mapping);
+ ASSERT_TRUE(Ty);
+ ASSERT_TRUE(Ty->isPointerTy());
+ ASSERT_TRUE(Read == 4);
+
+ PointerType *PT = cast<PointerType>(Ty);
+ Ty = PT->getElementType();
+ ASSERT_TRUE(Ty->isIntegerTy());
+ ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
+
+ // Two indirections.
+ Ty = parseTypeAtBeginning("i32**", Read, Error, M, &Mapping);
+ ASSERT_TRUE(Ty);
+ ASSERT_TRUE(Ty->isPointerTy());
+ ASSERT_TRUE(Read == 5);
+
+ PT = cast<PointerType>(Ty);
+ Ty = PT->getElementType();
+ ASSERT_TRUE(Ty->isPointerTy());
+
+ PT = cast<PointerType>(Ty);
+ Ty = PT->getElementType();
+ ASSERT_TRUE(Ty->isIntegerTy());
+ ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
+
+ // Check that we reject types with garbage.
+ Ty = parseTypeAtBeginning("i32 garbage", Read, Error, M, &Mapping);
+ ASSERT_TRUE(Ty);
+ ASSERT_TRUE(Ty->isIntegerTy());
+ ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
+ // We go to the next token, i.e., we read "i32" + ' '.
+ ASSERT_TRUE(Read == 4);
}
} // end anonymous namespace
OpenPOWER on IntegriCloud