aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/TrapReasonBuilder.cpp
blob: 5881229bf747d76f1e527f9d518b3de160b90f15 (plain)
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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file implements TrapReasonBuilder and related classes.
///
//===----------------------------------------------------------------------===//
#include "TrapReasonBuilder.h"

namespace clang {
namespace CodeGen {

TrapReasonBuilder::TrapReasonBuilder(DiagnosticsEngine *DiagObj,
                                     unsigned DiagID, TrapReason &TR)
    : DiagnosticBuilder(DiagObj, SourceLocation(), DiagID), TR(TR) {
  assert(DiagObj->getDiagnosticIDs()->isTrapDiag(DiagID));
}

TrapReasonBuilder::~TrapReasonBuilder() {
  // Store the trap message and category into the TrapReason object.
  getMessage(TR.Message);
  TR.Category = getCategory();

  // Make sure that when `DiagnosticBuilder::~DiagnosticBuilder()`
  // calls `Emit()` that it does nothing.
  Clear();
}

void TrapReasonBuilder::getMessage(SmallVectorImpl<char> &Storage) {
  // Render the Diagnostic
  Diagnostic Info(getDiagnosticsEngine(), *this);
  Info.FormatDiagnostic(Storage);
}

StringRef TrapReasonBuilder::getCategory() {
  auto CategoryID =
      getDiagnosticsEngine()->getDiagnosticIDs()->getCategoryNumberForDiag(
          getDiagID());
  if (CategoryID == 0)
    return "";
  return getDiagnosticsEngine()->getDiagnosticIDs()->getCategoryNameFromID(
      CategoryID);
}
} // namespace CodeGen
} // namespace clang