swift
verify.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/verify.h"
5 
6 #include <QLoggingCategory>
7 #include <QString>
8 #include <QtGlobal>
9 
10 #include "misc/logcategories.h"
11 
12 #ifdef SWIFT_USE_CRASHPAD
13 # include "crashpad/client/simulate_crash.h"
14 #endif
15 
16 #if defined(Q_CC_MSVC)
17 # include <Windows.h>
18 # include <intrin.h>
19 #endif
20 
21 #if defined(Q_CC_CLANG)
22 # if __has_builtin(__builtin_debugtrap)
23 # define SWIFT_BUILTIN_DEBUGTRAP __builtin_debugtrap
24 # elif __has_builtin(__builtin_debugger)
25 # define SWIFT_BUILTIN_DEBUGTRAP __builtin_debugger
26 # endif
27 #endif
28 
29 namespace swift::misc::private_ns
30 {
31  // cppcheck-suppress unusedFunction
32  void failedVerify(const char *condition, const char *filename, int line, const char *context, const char *message,
33  bool audit)
34  {
35  Q_UNUSED(condition)
36  Q_UNUSED(filename)
37  Q_UNUSED(line)
38  Q_UNUSED(context)
39  Q_UNUSED(message)
40  Q_UNUSED(audit)
41 
42 #if defined(QT_DEBUG)
43 # if defined(Q_CC_MSVC)
44  if (!audit || IsDebuggerPresent())
45  {
46  __debugbreak();
47  return;
48  }
49 # elif defined(SWIFT_BUILTIN_DEBUGTRAP)
50  SWIFT_BUILTIN_DEBUGTRAP();
51 # elif defined(Q_PROCESSOR_X86)
52  __asm__ volatile("int $0x03");
53 # elif defined(Q_PROCESSOR_ARM)
54  __asm__ volatile(".inst 0xe7f001f0");
55 # elif defined(Q_OS_UNIX)
56  raise(SIGTRAP);
57 # else
58  Q_ASSERT(false);
59 # endif
60 #endif
61 
62 #if defined(QT_NO_DEBUG) || defined(Q_CC_MSVC)
63  QString log;
64  if (context && message)
65  {
66  log = QStringLiteral("Failed to verify: %1 (%2 in %3) in %4 line %5")
67  .arg(condition, message, context, filename, QString::number(line));
68  }
69  else
70  {
71  log = QStringLiteral("Failed to verify: %1 in %2 line %3").arg(condition, filename, QString::number(line));
72  }
73  QMessageLogger().warning(QLoggingCategory(qPrintable(CLogCategories::verification()))) << log;
74 # if defined(SWIFT_USE_CRASHPAD)
75  CRASHPAD_SIMULATE_CRASH();
76 # endif
77 #endif
78  }
79 } // namespace swift::misc::private_ns
static const QString & verification()
Verification.
Definition: logcategories.h:31