Skip to content

Commit b6f7ff8

Browse files
authored
Use llvm_assert/assert.h on all platforms, not just Windows (microsoft#6815)
This makes it possible to define how assert works on all platforms. The header was already being included by String.cpp, and was already designed to work for non-Windows platforms. Also modify the non-Windows llvm_assert to emit the assertion message to stderr and trap. We cannot call standard assert as we are overriding it via include dirs, so there's no way to include the standard one and call it.
1 parent 9ef0481 commit b6f7ff8

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
623623

624624
# HLSL Change Starts - override assert to RaiseException instead of abort
625625
# for better test behavior
626-
if(WIN32 AND NOT UNIX)
627-
include_directories(BEFORE "${LLVM_MAIN_INCLUDE_DIR}/llvm/llvm_assert")
628-
endif()
626+
include_directories(BEFORE "${LLVM_MAIN_INCLUDE_DIR}/llvm/llvm_assert")
629627
# HLSL Change Ends
630628

631629
include_directories( ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})

include/llvm/llvm_assert/assert.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
#ifdef __cplusplus
3232
extern "C" {
3333
#endif
34-
void llvm_assert(const char *_Message, const char *_File, unsigned _Line,
35-
const char *_Function);
34+
void llvm_assert(const char *Message, const char *File, unsigned Line,
35+
const char *Function);
3636
#ifdef __cplusplus
3737
}
3838
#endif
3939

40-
#define assert(_Expression) \
41-
((void)((!!(_Expression)) || \
42-
(llvm_assert(#_Expression, __FILE__, __LINE__, __FUNCTION__), 0)))
40+
#define assert(Expression) \
41+
((void)((!!(Expression)) || \
42+
(llvm_assert(#Expression, __FILE__, __LINE__, __FUNCTION__), 0)))
4343

4444
#endif /* NDEBUG */

lib/Support/assert.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@
1414
#include "dxc/Support/Global.h"
1515
#include "windows.h"
1616

17-
void llvm_assert(const char *_Message, const char *_File, unsigned _Line,
18-
const char *_Function) {
19-
OutputDebugFormatA("Error: assert(%s)\nFile:\n%s(%d)\nFunc:\t%s\n", _Message,
20-
_File, _Line, _Function);
17+
void llvm_assert(const char *Message, const char *File, unsigned Line,
18+
const char *Function) {
19+
OutputDebugFormatA("Error: assert(%s)\nFile:\n%s(%d)\nFunc:\t%s\n", Message,
20+
File, Line, Function);
2121
RaiseException(STATUS_LLVM_ASSERT, 0, 0, 0);
2222
}
2323

2424
#else
2525

26-
#include <assert.h>
26+
#include "llvm/Support/Compiler.h"
27+
#include "llvm/Support/raw_ostream.h"
2728

28-
void llvm_assert(const char *message, const char *, unsigned,
29-
const char *_Function) {
30-
assert(false && message);
29+
void llvm_assert(const char *Message, const char *File, unsigned Line,
30+
const char *Function) {
31+
llvm::errs() << "Error: assert(" << Message << ")\nFile:\n"
32+
<< File << "(" << Line << ")\nFunc:\t" << Function << "\n";
33+
LLVM_BUILTIN_TRAP;
3134
}
3235

3336
#endif

tools/clang/lib/SPIRV/String.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//===----------------------------------------------------------------------===//
99

1010
#include "clang/SPIRV/String.h"
11-
#include "llvm/llvm_assert/assert.h"
11+
#include <assert.h>
1212

1313
namespace clang {
1414
namespace spirv {

0 commit comments

Comments
 (0)