diff --git a/mlir/include/mlir-c/IR.h b/mlir/include/mlir-c/IR.h --- a/mlir/include/mlir-c/IR.h +++ b/mlir/include/mlir-c/IR.h @@ -146,6 +146,10 @@ MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColGet( MlirContext context, MlirStringRef filename, unsigned line, unsigned col); +/// Creates a call site location with a callee and a caller +MLIR_CAPI_EXPORTED MlirLocation mlirLocationCallSiteGet(MlirLocation callee, + MlirLocation caller); + /// Creates a location with unknown position owned by the given context. MLIR_CAPI_EXPORTED MlirLocation mlirLocationUnknownGet(MlirContext context); diff --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp --- a/mlir/lib/CAPI/IR/IR.cpp +++ b/mlir/lib/CAPI/IR/IR.cpp @@ -115,6 +115,10 @@ FileLineColLoc::get(unwrap(filename), line, col, unwrap(context))); } +MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) { + return wrap(CallSiteLoc::get(unwrap(callee), unwrap(caller))); +} + MlirLocation mlirLocationUnknownGet(MlirContext context) { return wrap(UnknownLoc::get(unwrap(context))); } diff --git a/mlir/test/CAPI/ir.c b/mlir/test/CAPI/ir.c --- a/mlir/test/CAPI/ir.c +++ b/mlir/test/CAPI/ir.c @@ -1286,7 +1286,7 @@ MlirLocation loc = mlirDiagnosticGetLocation(diagnostic); mlirLocationPrint(loc, printToStderr, NULL); assert(mlirDiagnosticGetNumNotes(diagnostic) == 0); - fprintf(stderr, ">> end of diagnostic (userData: %d)\n", (int)userData); + fprintf(stderr, "\n>> end of diagnostic (userData: %d)\n", (int)userData); return mlirLogicalResultSuccess(); } @@ -1299,16 +1299,32 @@ MlirContext ctx = mlirContextCreate(); MlirDiagnosticHandlerID id = mlirContextAttachDiagnosticHandler( ctx, errorHandler, (void *)42, deleteUserData); - MlirLocation loc = mlirLocationUnknownGet(ctx); fprintf(stderr, "@test_diagnostics\n"); - mlirEmitError(loc, "test diagnostics"); + MlirLocation unknownLoc = mlirLocationUnknownGet(ctx); + mlirEmitError(unknownLoc, "test diagnostics"); + MlirLocation fileLineColLoc = mlirLocationFileLineColGet( + ctx, mlirStringRefCreateFromCString("file.c"), 1, 2); + mlirEmitError(fileLineColLoc, "test diagnostics"); + MlirLocation callSiteLoc = mlirLocationCallSiteGet( + mlirLocationFileLineColGet( + ctx, mlirStringRefCreateFromCString("other-file.c"), 2, 3), + fileLineColLoc); + mlirEmitError(callSiteLoc, "test diagnostics"); mlirContextDetachDiagnosticHandler(ctx, id); - mlirEmitError(loc, "more test diagnostics"); + mlirEmitError(unknownLoc, "more test diagnostics"); // CHECK-LABEL: @test_diagnostics // CHECK: processing diagnostic (userData: 42) << // CHECK: test diagnostics // CHECK: loc(unknown) // CHECK: >> end of diagnostic (userData: 42) + // CHECK: processing diagnostic (userData: 42) << + // CHECK: test diagnostics + // CHECK: loc("file.c":1:2) + // CHECK: >> end of diagnostic (userData: 42) + // CHECK: processing diagnostic (userData: 42) << + // CHECK: test diagnostics + // CHECK: loc(callsite("other-file.c":2:3 at "file.c":1:2)) + // CHECK: >> end of diagnostic (userData: 42) // CHECK: deleting user data (userData: 42) // CHECK-NOT: processing diagnostic // CHECK: more test diagnostics