File: Diagnostics.cpp

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-6~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,418,812 kB
  • sloc: cpp: 5,290,827; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,900; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,892; xml: 953; cs: 573; fortran: 539
file content (79 lines) | stat: -rw-r--r-- 3,012 bytes parent folder | download | duplicates (3)
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
//===- Diagnostics.cpp - C Interface for MLIR Diagnostics -----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "mlir-c/Diagnostics.h"
#include "mlir/CAPI/Diagnostics.h"
#include "mlir/CAPI/IR.h"
#include "mlir/CAPI/Support.h"
#include "mlir/CAPI/Utils.h"
#include "mlir/IR/Diagnostics.h"

using namespace mlir;

void mlirDiagnosticPrint(MlirDiagnostic diagnostic, MlirStringCallback callback,
                         void *userData) {
  detail::CallbackOstream stream(callback, userData);
  unwrap(diagnostic).print(stream);
}

MlirLocation mlirDiagnosticGetLocation(MlirDiagnostic diagnostic) {
  return wrap(unwrap(diagnostic).getLocation());
}

MlirDiagnosticSeverity mlirDiagnosticGetSeverity(MlirDiagnostic diagnostic) {
  switch (unwrap(diagnostic).getSeverity()) {
  case mlir::DiagnosticSeverity::Error:
    return MlirDiagnosticError;
  case mlir::DiagnosticSeverity::Warning:
    return MlirDiagnosticWarning;
  case mlir::DiagnosticSeverity::Note:
    return MlirDiagnosticNote;
  case mlir::DiagnosticSeverity::Remark:
    return MlirDiagnosticRemark;
  }
  llvm_unreachable("unhandled diagnostic severity");
}

// Notes are stored in a vector, so note iterator range is a pair of
// random access iterators, for which it is cheap to compute the size.
intptr_t mlirDiagnosticGetNumNotes(MlirDiagnostic diagnostic) {
  return static_cast<intptr_t>(llvm::size(unwrap(diagnostic).getNotes()));
}

// Notes are stored in a vector, so the iterator is a random access iterator,
// cheap to advance multiple steps at a time.
MlirDiagnostic mlirDiagnosticGetNote(MlirDiagnostic diagnostic, intptr_t pos) {
  return wrap(*std::next(unwrap(diagnostic).getNotes().begin(), pos));
}

static void deleteUserDataNoop(void *userData) {}

MlirDiagnosticHandlerID mlirContextAttachDiagnosticHandler(
    MlirContext context, MlirDiagnosticHandler handler, void *userData,
    void (*deleteUserData)(void *)) {
  assert(handler && "unexpected null diagnostic handler");
  if (deleteUserData == NULL)
    deleteUserData = deleteUserDataNoop;
  std::shared_ptr<void> sharedUserData(userData, deleteUserData);
  DiagnosticEngine::HandlerID id =
      unwrap(context)->getDiagEngine().registerHandler(
          [handler, sharedUserData](Diagnostic &diagnostic) {
            return unwrap(handler(wrap(diagnostic), sharedUserData.get()));
          });
  return static_cast<MlirDiagnosticHandlerID>(id);
}

void mlirContextDetachDiagnosticHandler(MlirContext context,
                                        MlirDiagnosticHandlerID id) {
  unwrap(context)->getDiagEngine().eraseHandler(
      static_cast<DiagnosticEngine::HandlerID>(id));
}

void mlirEmitError(MlirLocation location, const char *message) {
  emitError(unwrap(location)) << message;
}