testTransform runs a transformation pass on an input file (pathPrefix+".ll") and checks whether it matches the expected output (pathPrefix+".out.ll"). The output is compared with a fuzzy match that ignores some irrelevant lines such as empty lines.
(t *testing.T, pathPrefix string, transform func(mod llvm.Module))
| 29 | // output is compared with a fuzzy match that ignores some irrelevant lines such |
| 30 | // as empty lines. |
| 31 | func testTransform(t *testing.T, pathPrefix string, transform func(mod llvm.Module)) { |
| 32 | // Read the input IR. |
| 33 | ctx := llvm.NewContext() |
| 34 | defer ctx.Dispose() |
| 35 | buf, err := llvm.NewMemoryBufferFromFile(pathPrefix + ".ll") |
| 36 | os.Stat(pathPrefix + ".ll") // make sure this file is tracked by `go test` caching |
| 37 | if err != nil { |
| 38 | t.Fatalf("could not read file %s: %v", pathPrefix+".ll", err) |
| 39 | } |
| 40 | mod, err := ctx.ParseIR(buf) |
| 41 | if err != nil { |
| 42 | t.Fatalf("could not load module:\n%v", err) |
| 43 | } |
| 44 | defer mod.Dispose() |
| 45 | |
| 46 | // Perform the transform. |
| 47 | transform(mod) |
| 48 | |
| 49 | // Check for any incorrect IR. |
| 50 | err = llvm.VerifyModule(mod, llvm.PrintMessageAction) |
| 51 | if err != nil { |
| 52 | t.Fatal("IR verification failed") |
| 53 | } |
| 54 | |
| 55 | // Get the output from the test and filter some irrelevant lines. |
| 56 | actual := mod.String() |
| 57 | actual = actual[strings.Index(actual, "\ntarget datalayout = ")+1:] |
| 58 | |
| 59 | if *update { |
| 60 | err := os.WriteFile(pathPrefix+".out.ll", []byte(actual), 0666) |
| 61 | if err != nil { |
| 62 | t.Error("failed to write out new output:", err) |
| 63 | } |
| 64 | } else { |
| 65 | // Read the expected output IR. |
| 66 | out, err := os.ReadFile(pathPrefix + ".out.ll") |
| 67 | if err != nil { |
| 68 | t.Fatalf("could not read output file %s: %v", pathPrefix+".out.ll", err) |
| 69 | } |
| 70 | |
| 71 | // See whether the transform output matches with the expected output IR. |
| 72 | expected := string(out) |
| 73 | if !fuzzyEqualIR(expected, actual) { |
| 74 | t.Logf("output does not match expected output:\n%s", actual) |
| 75 | t.Fail() |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // fuzzyEqualIR returns true if the two LLVM IR strings passed in are roughly |
| 81 | // equal. That means, only relevant lines are compared (excluding comments |
no test coverage detected