blob: 4b69cc8481ddd730201292242909f938199582e6 [file] [log] [blame]
Brian Silverman8fce7482020-01-05 13:18:21 -08001/*----------------------------------------------------------------------------*/
Austin Schuh1e69f942020-11-14 15:06:14 -08002/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */
Brian Silverman8fce7482020-01-05 13:18:21 -08003/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "frc/Utility.h"
9
10#ifndef _WIN32
11#include <cxxabi.h>
12#include <execinfo.h>
13#endif
14
Austin Schuh1e69f942020-11-14 15:06:14 -080015#include <frc/Base.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080016#include <hal/DriverStation.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080017#include <wpi/Path.h>
18#include <wpi/SmallString.h>
19#include <wpi/StackTrace.h>
20#include <wpi/raw_ostream.h>
21
Brian Silverman8fce7482020-01-05 13:18:21 -080022using namespace frc;
23
24bool wpi_assert_impl(bool conditionValue, const wpi::Twine& conditionText,
25 const wpi::Twine& message, wpi::StringRef fileName,
26 int lineNumber, wpi::StringRef funcName) {
27 if (!conditionValue) {
28 wpi::SmallString<128> locBuf;
29 wpi::raw_svector_ostream locStream(locBuf);
30 locStream << funcName << " [" << wpi::sys::path::filename(fileName) << ":"
31 << lineNumber << "]";
32
33 wpi::SmallString<128> errorBuf;
34 wpi::raw_svector_ostream errorStream(errorBuf);
35
36 errorStream << "Assertion \"" << conditionText << "\" ";
37
38 if (message.isTriviallyEmpty() ||
39 (message.isSingleStringRef() && message.getSingleStringRef().empty())) {
40 errorStream << "failed.\n";
41 } else {
42 errorStream << "failed: " << message << "\n";
43 }
44
45 std::string stack = wpi::GetStackTrace(2);
46
47 // Print the error and send it to the DriverStation
48 HAL_SendError(1, 1, 0, errorBuf.c_str(), locBuf.c_str(), stack.c_str(), 1);
49 }
50
51 return conditionValue;
52}
53
54/**
55 * Common error routines for wpi_assertEqual_impl and wpi_assertNotEqual_impl.
56 *
57 * This should not be called directly; it should only be used by
58 * wpi_assertEqual_impl and wpi_assertNotEqual_impl.
59 */
60void wpi_assertEqual_common_impl(const wpi::Twine& valueA,
61 const wpi::Twine& valueB,
62 const wpi::Twine& equalityType,
63 const wpi::Twine& message,
64 wpi::StringRef fileName, int lineNumber,
65 wpi::StringRef funcName) {
66 wpi::SmallString<128> locBuf;
67 wpi::raw_svector_ostream locStream(locBuf);
68 locStream << funcName << " [" << wpi::sys::path::filename(fileName) << ":"
69 << lineNumber << "]";
70
71 wpi::SmallString<128> errorBuf;
72 wpi::raw_svector_ostream errorStream(errorBuf);
73
74 errorStream << "Assertion \"" << valueA << " " << equalityType << " "
75 << valueB << "\" ";
76
77 if (message.isTriviallyEmpty() ||
78 (message.isSingleStringRef() && message.getSingleStringRef().empty())) {
79 errorStream << "failed.\n";
80 } else {
81 errorStream << "failed: " << message << "\n";
82 }
83
84 std::string trace = wpi::GetStackTrace(3);
85
86 // Print the error and send it to the DriverStation
87 HAL_SendError(1, 1, 0, errorBuf.c_str(), locBuf.c_str(), trace.c_str(), 1);
88}
89
90bool wpi_assertEqual_impl(int valueA, int valueB,
91 const wpi::Twine& valueAString,
92 const wpi::Twine& valueBString,
93 const wpi::Twine& message, wpi::StringRef fileName,
94 int lineNumber, wpi::StringRef funcName) {
95 if (!(valueA == valueB)) {
96 wpi_assertEqual_common_impl(valueAString, valueBString, "==", message,
97 fileName, lineNumber, funcName);
98 }
99 return valueA == valueB;
100}
101
102bool wpi_assertNotEqual_impl(int valueA, int valueB,
103 const wpi::Twine& valueAString,
104 const wpi::Twine& valueBString,
105 const wpi::Twine& message, wpi::StringRef fileName,
106 int lineNumber, wpi::StringRef funcName) {
107 if (!(valueA != valueB)) {
108 wpi_assertEqual_common_impl(valueAString, valueBString, "!=", message,
109 fileName, lineNumber, funcName);
110 }
111 return valueA != valueB;
112}