51 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8///9/// \file10/// This file implements TrapReasonBuilder and related classes.11///12//===----------------------------------------------------------------------===//13#include "TrapReasonBuilder.h"14 15namespace clang {16namespace CodeGen {17 18TrapReasonBuilder::TrapReasonBuilder(DiagnosticsEngine *DiagObj,19 unsigned DiagID, TrapReason &TR)20 : DiagnosticBuilder(DiagObj, SourceLocation(), DiagID), TR(TR) {21 assert(DiagObj->getDiagnosticIDs()->isTrapDiag(DiagID));22}23 24TrapReasonBuilder::~TrapReasonBuilder() {25 // Store the trap message and category into the TrapReason object.26 getMessage(TR.Message);27 TR.Category = getCategory();28 29 // Make sure that when `DiagnosticBuilder::~DiagnosticBuilder()`30 // calls `Emit()` that it does nothing.31 Clear();32}33 34void TrapReasonBuilder::getMessage(SmallVectorImpl<char> &Storage) {35 // Render the Diagnostic36 Diagnostic Info(getDiagnosticsEngine(), *this);37 Info.FormatDiagnostic(Storage);38}39 40StringRef TrapReasonBuilder::getCategory() {41 auto CategoryID =42 getDiagnosticsEngine()->getDiagnosticIDs()->getCategoryNumberForDiag(43 getDiagID());44 if (CategoryID == 0)45 return "";46 return getDiagnosticsEngine()->getDiagnosticIDs()->getCategoryNameFromID(47 CategoryID);48}49} // namespace CodeGen50} // namespace clang51