llvm.org GIT mirror llvm / b3834e5 unittests / AsmParser / AsmParserTest.cpp
b3834e5

Tree @b3834e5 (Download .tar.gz)

AsmParserTest.cpp @b3834e5raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//===- llvm/unittest/AsmParser/AsmParserTest.cpp - asm parser unittests ---===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#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"
#include "gtest/gtest.h"

using namespace llvm;

namespace {

TEST(AsmParserTest, NullTerminatedInput) {
  LLVMContext Ctx;
  StringRef Source = "; Empty module \n";
  SMDiagnostic Error;
  auto Mod = parseAssemblyString(Source, Error, Ctx);

  EXPECT_TRUE(Mod != nullptr);
  EXPECT_TRUE(Error.getMessage().empty());
}

#ifdef GTEST_HAS_DEATH_TEST
#ifndef NDEBUG

TEST(AsmParserTest, NonNullTerminatedInput) {
  LLVMContext Ctx;
  StringRef Source = "; Empty module \n\1\2";
  SMDiagnostic Error;
  std::unique_ptr<Module> Mod;
  EXPECT_DEATH(Mod = parseAssemblyString(Source.substr(0, Source.size() - 2),
                                         Error, Ctx),
               "Buffer is not null terminated!");
}

#endif
#endif

TEST(AsmParserTest, SlotMappingTest) {
  LLVMContext Ctx;
  StringRef Source = "@0 = global i32 0\n !0 = !{}\n !42 = !{i32 42}";
  SMDiagnostic Error;
  SlotMapping Mapping;
  auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);

  EXPECT_TRUE(Mod != nullptr);
  EXPECT_TRUE(Error.getMessage().empty());

  ASSERT_EQ(Mapping.GlobalValues.size(), 1u);
  EXPECT_TRUE(isa<GlobalVariable>(Mapping.GlobalValues[0]));

  EXPECT_EQ(Mapping.MetadataNodes.size(), 2u);
  EXPECT_EQ(Mapping.MetadataNodes.count(0), 1u);
  EXPECT_EQ(Mapping.MetadataNodes.count(42), 1u);
  EXPECT_EQ(Mapping.MetadataNodes.count(1), 0u);
}

TEST(AsmParserTest, TypeAndConstantValueParsing) {
  LLVMContext Ctx;
  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));

  V = parseConstantValue("i8** undef", Error, M);
  ASSERT_TRUE(V);
  ASSERT_TRUE(isa<UndefValue>(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");
}

TEST(AsmParserTest, TypeAndConstantValueWithSlotMappingParsing) {
  LLVMContext Ctx;
  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;

  const Value *V;
  V = parseConstantValue("i32* getelementptr inbounds ([50 x %st], [50 x %st]* "
                         "@v, i64 0, i64 0, i32 0)",
                         Error, M, &Mapping);
  ASSERT_TRUE(V);
  ASSERT_TRUE(isa<ConstantExpr>(V));

  V = parseConstantValue("i32* getelementptr inbounds ([50 x %0], [50 x %0]* "
                         "@g, i64 0, i64 0, i32 0)",
                         Error, M, &Mapping);
  ASSERT_TRUE(V);
  ASSERT_TRUE(isa<ConstantExpr>(V));
}

TEST(AsmParserTest, TypeWithSlotMappingParsing) {
  LLVMContext Ctx;
  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;

  // Check we properly parse integer types.
  Type *Ty;
  Ty = parseType("i32", Error, M, &Mapping);
  ASSERT_TRUE(Ty);
  ASSERT_TRUE(Ty->isIntegerTy());
  ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);

  // Check we properly parse integer types with exotic size.
  Ty = parseType("i13", Error, M, &Mapping);
  ASSERT_TRUE(Ty);
  ASSERT_TRUE(Ty->isIntegerTy());
  ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 13);

  // Check we properly parse floating point types.
  Ty = parseType("float", Error, M, &Mapping);
  ASSERT_TRUE(Ty);
  ASSERT_TRUE(Ty->isFloatTy());

  Ty = parseType("double", Error, M, &Mapping);
  ASSERT_TRUE(Ty);
  ASSERT_TRUE(Ty->isDoubleTy());

  // Check we properly parse struct types.
  // Named struct.
  Ty = parseType("%st", Error, M, &Mapping);
  ASSERT_TRUE(Ty);
  ASSERT_TRUE(Ty->isStructTy());

  // 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 = parseType("%0", Error, M, &Mapping);
  ASSERT_TRUE(Ty);
  ASSERT_TRUE(Ty->isStructTy());

  // 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 = parseType("<5 x i32>", Error, M, &Mapping);
  ASSERT_TRUE(Ty);
  ASSERT_TRUE(Ty->isVectorTy());

  // 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 = parseType("%opaque", Error, M, &Mapping);
  ASSERT_TRUE(Ty);
  ASSERT_TRUE(Ty->isStructTy());

  ST = cast<StructType>(Ty);
  ASSERT_TRUE(ST->isOpaque());

  // Check we properly parse pointer types.
  // One indirection.
  Ty = parseType("i32*", Error, M, &Mapping);
  ASSERT_TRUE(Ty);
  ASSERT_TRUE(Ty->isPointerTy());

  PointerType *PT = cast<PointerType>(Ty);
  Ty = PT->getElementType();
  ASSERT_TRUE(Ty->isIntegerTy());
  ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);

  // Two indirections.
  Ty = parseType("i32**", Error, M, &Mapping);
  ASSERT_TRUE(Ty);
  ASSERT_TRUE(Ty->isPointerTy());

  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 = parseType("i32 garbage", Error, M, &Mapping);
  ASSERT_TRUE(!Ty);
}

TEST(AsmParserTest, TypeAtBeginningWithSlotMappingParsing) {
  LLVMContext Ctx;
  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