Skip to content

Commit bee4491

Browse files
Added some control-flow checks (#45)
* Added some control-flow checks * Add empty line to keep the `NEXT`s intact.
1 parent 4073f86 commit bee4491

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Tests/LLVMTests/IRBuilderSpec.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,61 @@ class IRBuilderSpec : XCTestCase {
197197

198198
// IRBUILDERFCMP-NEXT: ret void
199199
builder.buildRetVoid()
200+
200201
// IRBUILDERFCMP-NEXT: }
201202
module.dump()
202203
})
204+
205+
XCTAssert(fileCheckOutput(of: .stderr, withPrefixes: ["CONTROLFLOW"]) {
206+
// CONTROLFLOW: ; ModuleID = 'IRBuilderTest'
207+
// CONTROLFLOW-NEXT: source_filename = "IRBuilderTest"
208+
let module = Module(name: "IRBuilderTest")
209+
let builder = IRBuilder(module: module)
210+
211+
// CONTROLFLOW: define i32 @main() {
212+
let main = builder.addFunction("main",
213+
type: FunctionType(argTypes: [],
214+
returnType: IntType.int32))
215+
216+
// CONTROLFLOW-NEXT: entry:
217+
let entry = main.appendBasicBlock(named: "entry")
218+
builder.positionAtEnd(of: entry)
219+
220+
// CONTROLFLOW-NEXT: %var = alloca i64
221+
let variable = builder.buildAlloca(type: IntType.int64, name: "var")
222+
223+
// CONTROLFLOW-NEXT: store i64 1, i64* %var
224+
builder.buildStore(IntType.int64.constant(1), to: variable)
225+
226+
// CONTROLFLOW-NEXT: %0 = load i64, i64* %var
227+
let load = builder.buildLoad(variable)
228+
229+
// CONTROLFLOW-NEXT: %1 = icmp eq i64 %0, 0
230+
let res = builder.buildICmp(load, IntType.int64.zero(), .eq)
231+
232+
let thenBB = main.appendBasicBlock(named: "then")
233+
let elseBB = main.appendBasicBlock(named: "else")
234+
235+
// CONTROLFLOW-NEXT: br i1 %1, label %then, label %else
236+
builder.buildCondBr(condition: res, then: thenBB, else: elseBB)
237+
238+
// CONTROLFLOW-NEXT:
239+
// CONTROLFLOW-NEXT: then:
240+
builder.positionAtEnd(of: thenBB)
241+
242+
// CONTROLFLOW-NEXT: ret i32 1
243+
builder.buildRet(IntType.int32.constant(1))
244+
245+
// CONTROLFLOW-NEXT:
246+
// CONTROLFLOW-NEXT: else:
247+
builder.positionAtEnd(of: elseBB)
248+
249+
// CONTROLFLOW-NEXT: ret i32 0
250+
builder.buildRet(IntType.int32.constant(0))
251+
252+
// CONTROLFLOW-NEXT: }
253+
module.dump()
254+
})
203255
}
204256

205257
#if !os(macOS)

0 commit comments

Comments
 (0)