Skip to content

Commit 593cfff

Browse files
committed
[compiler-rt][XRay] Make xray_interface.h C compliant
The XRay interface header uses no C++ specific features aside from using the std namespace and including the C++ variant of C headers. Yet, these changes prevent using `xray_interface.h` in external tools relying on C for different reasons. Make this header C compliant by using C headers, removing the std namespace from std::size_t and guard `extern "C"`. To make sure that further changes to not break the interface accidentially, port one test from C++ to C. This requires the C23 standard to officially support the attribute syntax used in this test case. Note that this only resolves this issue for `xray_interface.h`. `xray_records.h` is also not C compliant, but requires more work to port. Fixes #139902 Signed-off-by: Jan André Reuter <[email protected]>
1 parent 5e7bc5e commit 593cfff

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

compiler-rt/include/xray/xray_interface.h

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- xray_interface.h -----------------------------------------*- C++ -*-===//
1+
//===- xray_interface.h ---------------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -14,20 +14,27 @@
1414
#ifndef XRAY_XRAY_INTERFACE_H
1515
#define XRAY_XRAY_INTERFACE_H
1616

17+
#ifdef __cplusplus
1718
#include <cstddef>
1819
#include <cstdint>
20+
#else
21+
#include <stddef.h>
22+
#include <stdint.h>
23+
#endif
1924

25+
#ifdef __cplusplus
2026
extern "C" {
27+
#endif
2128

2229
/// Synchronize this with AsmPrinter::SledKind in LLVM.
23-
enum XRayEntryType {
30+
typedef enum {
2431
ENTRY = 0,
2532
EXIT = 1,
2633
TAIL = 2,
2734
LOG_ARGS_ENTRY = 3,
2835
CUSTOM_EVENT = 4,
2936
TYPED_EVENT = 5,
30-
};
37+
} XRayEntryType;
3138

3239
/// Provide a function to invoke for when instrumentation points are hit. This
3340
/// is a user-visible control surface that overrides the default implementation.
@@ -68,7 +75,7 @@ extern int __xray_set_handler_arg1(void (*entry)(int32_t, XRayEntryType,
6875
extern int __xray_remove_handler_arg1();
6976

7077
/// Provide a function to invoke when XRay encounters a custom event.
71-
extern int __xray_set_customevent_handler(void (*entry)(void *, std::size_t));
78+
extern int __xray_set_customevent_handler(void (*entry)(void *, size_t));
7279

7380
/// This removes whatever the currently provided custom event handler is.
7481
/// Returns 1 on success, 0 on error.
@@ -86,12 +93,12 @@ extern int __xray_remove_typedevent_handler();
8693

8794
extern uint16_t __xray_register_event_type(const char *event_type);
8895

89-
enum XRayPatchingStatus {
96+
typedef enum {
9097
NOT_INITIALIZED = 0,
9198
SUCCESS = 1,
9299
ONGOING = 2,
93100
FAILED = 3,
94-
};
101+
} XRayPatchingStatus;
95102

96103
/// This tells XRay to patch the instrumentation points in all currently loaded
97104
/// objects. See XRayPatchingStatus for possible result values.
@@ -173,6 +180,8 @@ extern int32_t __xray_pack_id(int32_t FuncId, int32_t ObjId);
173180
/// Calling __xray_init() more than once is safe across multiple threads.
174181
extern void __xray_init();
175182

183+
#ifdef __cplusplus
176184
} // end extern "C"
185+
#endif
177186

178187
#endif // XRAY_XRAY_INTERFACE_H
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
// Check that we can patch and un-patch on demand, and that logging gets invoked
22
// appropriately.
33
//
4-
// RUN: %clangxx_xray -fxray-instrument -std=c++11 %s -o %t
4+
// RUN: %clang_xray -fxray-instrument -std=c23 %s -o %t
55
// RUN: env XRAY_OPTIONS="patch_premain=false" %run %t 2>&1 | FileCheck %s
6-
// RUN: %clangxx_xray -fxray-instrument -fno-xray-function-index -std=c++11 %s -o %t
6+
// RUN: %clang_xray -fxray-instrument -fno-xray-function-index -std=c23 %s -o %t
77
// RUN: env XRAY_OPTIONS="patch_premain=false" %run %t 2>&1 | FileCheck %s
88

99
// UNSUPPORTED: target-is-mips64,target-is-mips64el
1010

1111
#include "xray/xray_interface.h"
1212

13-
#include <cstdio>
13+
#include <stdio.h>
1414

1515
bool called = false;
1616

1717
void test_handler(int32_t fid, XRayEntryType type) {
18-
printf("called: %d, type=%d\n", fid, static_cast<int32_t>(type));
18+
printf("called: %d, type=%d\n", fid, (int32_t)(type));
1919
called = true;
2020
}
2121

@@ -28,24 +28,24 @@ int main() {
2828
always_instrument();
2929
// CHECK: always instrumented called
3030
auto status = __xray_patch();
31-
printf("patching status: %d\n", static_cast<int32_t>(status));
31+
printf("patching status: %d\n", (int32_t)status);
3232
// CHECK-NEXT: patching status: 1
3333
always_instrument();
3434
// CHECK-NEXT: called: {{.*}}, type=0
3535
// CHECK-NEXT: always instrumented called
3636
// CHECK-NEXT: called: {{.*}}, type=1
3737
status = __xray_unpatch();
38-
printf("patching status: %d\n", static_cast<int32_t>(status));
38+
printf("patching status: %d\n", (int32_t)status);
3939
// CHECK-NEXT: patching status: 1
4040
always_instrument();
4141
// CHECK-NEXT: always instrumented called
4242
status = __xray_patch();
43-
printf("patching status: %d\n", static_cast<int32_t>(status));
43+
printf("patching status: %d\n", (int32_t)status);
4444
// CHECK-NEXT: patching status: 1
4545
__xray_remove_handler();
4646
always_instrument();
4747
// CHECK-NEXT: always instrumented called
4848
status = __xray_unpatch();
49-
printf("patching status: %d\n", static_cast<int32_t>(status));
49+
printf("patching status: %d\n", (int32_t)status);
5050
// CHECK-NEXT: patching status: 1
5151
}

0 commit comments

Comments
 (0)