Skip to content

Commit 5668722

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 30b0946 commit 5668722

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

compiler-rt/include/xray/xray_interface.h

+12-8
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,22 @@
1414
#ifndef XRAY_XRAY_INTERFACE_H
1515
#define XRAY_XRAY_INTERFACE_H
1616

17-
#include <cstddef>
18-
#include <cstdint>
17+
#include <stddef.h>
18+
#include <stdint.h>
1919

20+
#ifdef __cplusplus
2021
extern "C" {
22+
#endif
2123

2224
/// Synchronize this with AsmPrinter::SledKind in LLVM.
23-
enum XRayEntryType {
25+
typedef enum {
2426
ENTRY = 0,
2527
EXIT = 1,
2628
TAIL = 2,
2729
LOG_ARGS_ENTRY = 3,
2830
CUSTOM_EVENT = 4,
2931
TYPED_EVENT = 5,
30-
};
32+
} XRayEntryType;
3133

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

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

7375
/// This removes whatever the currently provided custom event handler is.
7476
/// Returns 1 on success, 0 on error.
@@ -86,12 +88,12 @@ extern int __xray_remove_typedevent_handler();
8688

8789
extern uint16_t __xray_register_event_type(const char *event_type);
8890

89-
enum XRayPatchingStatus {
91+
typedef enum {
9092
NOT_INITIALIZED = 0,
9193
SUCCESS = 1,
9294
ONGOING = 2,
9395
FAILED = 3,
94-
};
96+
} XRayPatchingStatus;
9597

9698
/// This tells XRay to patch the instrumentation points in all currently loaded
9799
/// objects. See XRayPatchingStatus for possible result values.
@@ -173,6 +175,8 @@ extern int32_t __xray_pack_id(int32_t FuncId, int32_t ObjId);
173175
/// Calling __xray_init() more than once is safe across multiple threads.
174176
extern void __xray_init();
175177

178+
#ifdef __cplusplus
176179
} // end extern "C"
180+
#endif
177181

178182
#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)