Skip to content

Commit 5d6626c

Browse files
committed
Check in test that demonstrates ABI break for std::function.
Our C++03 and C++11 implementations of function are not ABI compatible. I've added a "test" that demonstrates this. llvm-svn: 363092
1 parent 3cef1f7 commit 5d6626c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// -*- C++ -*-
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
// REQUIRES: clang
11+
// XFAIL: *
12+
13+
// This tests is meant to demonstrate an existing ABI bug between the
14+
// C++03 and C++11 implementations of std::function. It is not a real test.
15+
16+
// RUN: %cxx -c %s -o %t.first.o %flags %compile_flags -std=c++03 -g
17+
// RUN: %cxx -c %s -o %t.second.o -DWITH_MAIN %flags %compile_flags -g -std=c++11
18+
// RUN: %cxx -o %t.exe %t.first.o %t.second.o %flags %link_flags -g
19+
// RUN: %run
20+
21+
#include <functional>
22+
#include <cassert>
23+
24+
typedef std::function<void(int)> Func;
25+
26+
Func CreateFunc();
27+
28+
#ifndef WITH_MAIN
29+
// In C++03, the functions call operator, which is a part of the vtable,
30+
// is defined as 'void operator()(int)', but in C++11 it's
31+
// void operator()(int&&)'. So when the C++03 version is passed to C++11 code
32+
// the value of the integer is interpreted as its address.
33+
void test(int x) {
34+
assert(x == 42);
35+
}
36+
Func CreateFunc() {
37+
Func f(&test);
38+
return f;
39+
}
40+
#else
41+
int main() {
42+
Func f = CreateFunc();
43+
f(42);
44+
}
45+
#endif

0 commit comments

Comments
 (0)