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 @@ -162,6 +162,13 @@ MLIR_CAPI_EXPORTED MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller); +/// Creates a name location owned by the given context. Providing null location +/// for childLoc is allowed and if childLoc is null location, then the behavior +/// is the same as having unknown child location. +MLIR_CAPI_EXPORTED MlirLocation mlirLocationNameGet(MlirContext context, + MlirStringRef name, + MlirLocation childLoc); + /// 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 @@ -15,6 +15,7 @@ #include "mlir/IR/Attributes.h" #include "mlir/IR/BuiltinOps.h" #include "mlir/IR/Dialect.h" +#include "mlir/IR/Location.h" #include "mlir/IR/Operation.h" #include "mlir/IR/Types.h" #include "mlir/IR/Verifier.h" @@ -131,6 +132,15 @@ return wrap(Location(CallSiteLoc::get(unwrap(callee), unwrap(caller)))); } +MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name, + MlirLocation childLoc) { + if (mlirLocationIsNull(childLoc)) + return wrap( + Location(NameLoc::get(Identifier::get(unwrap(name), unwrap(context))))); + return wrap(Location(NameLoc::get( + Identifier::get(unwrap(name), unwrap(context)), unwrap(childLoc)))); +} + MlirLocation mlirLocationUnknownGet(MlirContext context) { return wrap(Location(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 @@ -19,6 +19,7 @@ #include "mlir-c/Dialect/Standard.h" #include "mlir-c/IntegerSet.h" #include "mlir-c/Registration.h" +#include "mlir-c/Support.h" #include #include @@ -1703,6 +1704,10 @@ ctx, mlirStringRefCreateFromCString("other-file.c"), 2, 3), fileLineColLoc); mlirEmitError(callSiteLoc, "test diagnostics"); + MlirLocation null = {0}; + MlirLocation nameLoc = + mlirLocationNameGet(ctx, mlirStringRefCreateFromCString("named"), null); + mlirEmitError(nameLoc, "test diagnostics"); mlirContextDetachDiagnosticHandler(ctx, id); mlirEmitError(unknownLoc, "more test diagnostics"); // CHECK-LABEL: @test_diagnostics @@ -1718,6 +1723,10 @@ // CHECK: test diagnostics // CHECK: loc(callsite("other-file.c":2:3 at "file.c":1:2)) // CHECK: >> end of diagnostic (userData: 42) + // CHECK: processing diagnostic (userData: 42) << + // CHECK: test diagnostics + // CHECK: loc("named") + // CHECK: >> end of diagnostic (userData: 42) // CHECK: deleting user data (userData: 42) // CHECK-NOT: processing diagnostic // CHECK: more test diagnostics