Skip to content

Commit e9f2d64

Browse files
authored
[SYCL][XPTI] Enable code location info when NDEBUG is not defined (#5023)
1 parent d31b42c commit e9f2d64

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

sycl/include/CL/sycl/detail/common.hpp

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,46 @@
2424
__SYCL_INLINE_NAMESPACE(cl) {
2525
namespace sycl {
2626
namespace detail {
27+
28+
#if !defined(NDEBUG) && (_MSC_VER > 1929 || __has_builtin(__builtin_FILE))
29+
#define __CODELOC_FILE_NAME __builtin_FILE()
30+
#else
31+
#define __CODELOC_FILE_NAME nullptr
32+
#endif
33+
34+
#if _MSC_VER > 1929 || __has_builtin(__builtin_FUNCTION)
35+
#define __CODELOC_FUNCTION __builtin_FUNCTION()
36+
#else
37+
#define __CODELOC_FUNCTION nullptr
38+
#endif
39+
40+
#if _MSC_VER > 1929 || __has_builtin(__builtin_LINE)
41+
#define __CODELOC_LINE __builtin_LINE()
42+
#else
43+
#define __CODELOC_LINE 0
44+
#endif
45+
46+
#if _MSC_VER > 1929 || __has_builtin(__builtin_LINE)
47+
#define __CODELOC_COLUMN __builtin_COLUMN()
48+
#else
49+
#define __CODELOC_COLUMN 0
50+
#endif
51+
2752
// Data structure that captures the user code location information using the
2853
// builtin capabilities of the compiler
2954
struct code_location {
30-
#ifdef _MSC_VER
31-
// Since MSVC does not support the required builtins, we
32-
// implement the version with "unknown"s which is handled
33-
// correctly by the instrumentation
34-
static constexpr code_location current(const char *fileName = nullptr,
35-
const char *funcName = nullptr,
36-
unsigned long lineNo = 0,
37-
unsigned long columnNo = 0) noexcept {
38-
return code_location(fileName, funcName, lineNo, columnNo);
39-
}
40-
#else
41-
// FIXME Having a nullptr for fileName here is a short-term solution to
42-
// workaround leak of full paths in builds
4355
static constexpr code_location
44-
current(const char *fileName = nullptr,
45-
const char *funcName = __builtin_FUNCTION(),
46-
unsigned long lineNo = __builtin_LINE(),
47-
unsigned long columnNo = 0) noexcept {
56+
current(const char *fileName = __CODELOC_FILE_NAME,
57+
const char *funcName = __CODELOC_FUNCTION,
58+
unsigned long lineNo = __CODELOC_LINE,
59+
unsigned long columnNo = __CODELOC_COLUMN) noexcept {
4860
return code_location(fileName, funcName, lineNo, columnNo);
4961
}
50-
#endif
62+
63+
#undef __CODELOC_FILE_NAME
64+
#undef __CODELOC_FUNCTION
65+
#undef __CODELOC_LINE
66+
#undef __CODELOC_COLUMN
5167

5268
constexpr code_location(const char *file, const char *func, int line,
5369
int col) noexcept
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %clangxx -fsycl -DNDEBUG %s -o %t.out
2+
// RUN: %t.out
3+
// RUN: %clangxx -fsycl %s -o %t.out
4+
// RUN: %t.out
5+
6+
#include <CL/sycl.hpp>
7+
#include <cassert>
8+
#include <iostream>
9+
10+
int main() {
11+
auto code_loc = sycl::detail::code_location::current();
12+
const char *funcName = "main";
13+
#ifdef NDEBUG
14+
if (code_loc.fileName() != nullptr)
15+
return 1;
16+
if (code_loc.functionName() != funcName)
17+
return 1;
18+
if (code_loc.lineNumber() != 11)
19+
return 1;
20+
if (code_loc.columnNumber() != 19)
21+
return 1;
22+
#else
23+
assert((code_loc.fileName() != nullptr));
24+
std::string str = code_loc.fileName();
25+
assert(((str.find("code_location.cpp") != std::string::npos) &&
26+
"Filename is wrong"));
27+
assert((code_loc.functionName() != nullptr));
28+
str = code_loc.functionName();
29+
assert(
30+
((str.find(funcName) != std::string::npos) && "Function name is wrong"));
31+
assert((code_loc.lineNumber() != 0));
32+
assert(((code_loc.lineNumber() == 11) && "Line number is wrong"));
33+
assert((code_loc.columnNumber() != 0));
34+
assert(((code_loc.columnNumber() == 19) && "Column number is wrong"));
35+
#endif
36+
return 0;
37+
}

0 commit comments

Comments
 (0)