Skip to content

Commit bbe9a1c

Browse files
authored
Merge pull request #7 from benlangmuir/import
[llvm] Mangle all llvm symbols to avoid collisions
2 parents b1258a6 + d7f3474 commit bbe9a1c

24 files changed

+129
-4
lines changed

Package.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ let package = Package(
88
.library(
99
name: "IndexStoreDB",
1010
targets: ["IndexStoreDB"]),
11-
.library(
12-
name: "IndexStoreDB-dynamic",
13-
type: .dynamic,
14-
targets: ["IndexStoreDB"]),
1511
.library(
1612
name: "IndexStoreDB_CXX",
1713
targets: ["IndexStoreDB_Index"]),

Utilities/import-llvm

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import errno
1010
import optparse
1111
import os
12+
import re
1213
import shutil
1314
import sys
15+
import subprocess
1416

1517
# ADT types.
1618
ADT_imports = ['EpochTracker', 'iterator', 'iterator_range', 'Hashing', 'None', 'Optional',
@@ -78,6 +80,52 @@ def copyfile(src, dst):
7880
note("cp %r %r" % (src, dst))
7981
shutil.copyfile(src, dst)
8082

83+
def includes_prefix(source, include_dir):
84+
defs = subprocess.check_output([
85+
'clang', '-std=c++14', '-dM', '-E',
86+
'-I', include_dir,
87+
'-x', 'c++', source])
88+
89+
return "INDEXSTOREDB_PREFIX_H" in defs
90+
91+
def is_llvm_leaf_source(source):
92+
# AIXDataTypes is a false negative.
93+
return subprocess.call("grep '# *include *\"llvm' %s | grep --quiet -v AIXDataTypes" % source, shell=True)
94+
95+
def add_prefix(str):
96+
snippet = '#include "llvm/Config/indexstoredb-prefix.h"\n'
97+
98+
m = re.search(r'#ifndef *([A-Za-z_]*)\n#define *\1 *\n', str)
99+
if m:
100+
return str[:m.end()] + '\n' + snippet + str[m.end():]
101+
102+
i = str.find(r'#include')
103+
if i != -1:
104+
return str[:i] + snippet + str[i:]
105+
106+
# If we ever hit this, we could find the end of the copyright header comment.
107+
print >>sys.stderr, 'error: could not find existing #include or header guards'
108+
sys.exit(1)
109+
110+
def maybe_add_prefix(source, include_dir):
111+
ext = os.path.splitext(source)[1]
112+
if not ext in ['.h', '.cpp', '.c']:
113+
return
114+
if not is_llvm_leaf_source(source):
115+
# Skip files that include other llvm headers; we will add the include to
116+
# the leaf header.
117+
return
118+
if includes_prefix(source, include_dir):
119+
# Skip files that already include the prefix header.
120+
return
121+
122+
note("adding prefix header to %r" % source)
123+
with open(source, 'r+') as file:
124+
new = add_prefix(file.read())
125+
file.seek(0)
126+
file.write(new)
127+
file.truncate()
128+
81129
def main():
82130
parser = optparse.OptionParser("usage: %prog <llvm-source-path>")
83131
(opts, args) = parser.parse_args()
@@ -131,5 +179,21 @@ def main():
131179
for name in C_imports:
132180
import_c_header(name + '.h')
133181

182+
print ""
183+
print "Adding prefix header includes"
184+
185+
base_dirs = [
186+
os.path.join(sourcekit_srcroot, 'include', 'llvm'),
187+
os.path.join(sourcekit_srcroot, 'include', 'llvm-c'),
188+
os.path.join(sourcekit_srcroot, 'lib', 'LLVMSupport'),
189+
]
190+
191+
include_dir = os.path.join(sourcekit_srcroot, 'include')
192+
193+
for base in base_dirs:
194+
for root, dirs, files in os.walk(base):
195+
for file in files:
196+
maybe_add_prefix(os.path.join(root, file), include_dir)
197+
134198
if __name__ == '__main__':
135199
main()

include/llvm-c/DataTypes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#ifndef LLVM_C_DATATYPES_H
2525
#define LLVM_C_DATATYPES_H
2626

27+
#include "llvm/Config/indexstoredb-prefix.h"
28+
2729
#ifdef __cplusplus
2830
#include <cmath>
2931
#else

include/llvm-c/ErrorHandling.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#ifndef LLVM_C_ERROR_HANDLING_H
1515
#define LLVM_C_ERROR_HANDLING_H
1616

17+
#include "llvm/Config/indexstoredb-prefix.h"
18+
1719
#ifdef __cplusplus
1820
extern "C" {
1921
#endif

include/llvm/ADT/IntrusiveRefCntPtr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
#ifndef LLVM_ADT_INTRUSIVEREFCNTPTR_H
5757
#define LLVM_ADT_INTRUSIVEREFCNTPTR_H
5858

59+
#include "llvm/Config/indexstoredb-prefix.h"
60+
5961
#include <atomic>
6062
#include <cassert>
6163
#include <cstddef>

include/llvm/ADT/None.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#ifndef LLVM_ADT_NONE_H
1717
#define LLVM_ADT_NONE_H
1818

19+
#include "llvm/Config/indexstoredb-prefix.h"
20+
1921
namespace llvm {
2022
/// A simple null object to allow implicit construction of Optional<T>
2123
/// and similar types without having to spell out the specialization's name.

include/llvm/ADT/iterator_range.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#ifndef LLVM_ADT_ITERATOR_RANGE_H
2020
#define LLVM_ADT_ITERATOR_RANGE_H
2121

22+
#include "llvm/Config/indexstoredb-prefix.h"
23+
2224
#include <iterator>
2325
#include <utility>
2426

include/llvm/Config/abi-breaking.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#ifndef LLVM_ABI_BREAKING_CHECKS_H
1313
#define LLVM_ABI_BREAKING_CHECKS_H
1414

15+
#include "llvm/Config/indexstoredb-prefix.h"
16+
1517
/* Define to enable checks that alter the LLVM C++ ABI */
1618
#define LLVM_ENABLE_ABI_BREAKING_CHECKS 0
1719

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*===------- llvm/Config/indexstoredb-prefix.h --------------------*- C -*-===*/
2+
/* */
3+
/* The LLVM Compiler Infrastructure */
4+
/* */
5+
/* This file is distributed under the University of Illinois Open Source */
6+
/* License. See LICENSE.TXT for details. */
7+
/* */
8+
/*===----------------------------------------------------------------------===*/
9+
10+
#ifndef INDEXSTOREDB_PREFIX_H
11+
#define INDEXSTOREDB_PREFIX_H
12+
13+
/* HACK: Rename all of the llvm symbols so that they will not collide if another
14+
* copy of llvm is linked into the same image. The use of llvm within IndexStore
15+
* is purely an implementation detail. Using a source-level rename is a
16+
* workaround for the lack of symbol visibility controls in swiftpm. Ideally we
17+
* could do this with a combination of `-fvisibility=hidden` and `ld -r`.
18+
*/
19+
20+
#define llvm indexstoredb_llvm
21+
#define LLVMEnablePrettyStackTrace indexstoredb_LLVMEnablePrettyStackTrace
22+
23+
#endif // INDEXSTOREDB_PREFIX_H

include/llvm/Config/llvm-config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#ifndef LLVM_CONFIG_H
1515
#define LLVM_CONFIG_H
1616

17+
#include "llvm/Config/indexstoredb-prefix.h"
18+
1719
/* Define if LLVM_ENABLE_DUMP is enabled */
1820
/* #undef LLVM_ENABLE_DUMP */
1921

include/llvm/Support/ARMBuildAttributes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#ifndef LLVM_SUPPORT_ARMBUILDATTRIBUTES_H
2020
#define LLVM_SUPPORT_ARMBUILDATTRIBUTES_H
2121

22+
#include "llvm/Config/indexstoredb-prefix.h"
23+
2224
namespace llvm {
2325
class StringRef;
2426

include/llvm/Support/Capacity.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#ifndef LLVM_SUPPORT_CAPACITY_H
1616
#define LLVM_SUPPORT_CAPACITY_H
1717

18+
#include "llvm/Config/indexstoredb-prefix.h"
19+
1820
#include <cstddef>
1921

2022
namespace llvm {

include/llvm/Support/ConvertUTF.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
#ifndef LLVM_SUPPORT_CONVERTUTF_H
9191
#define LLVM_SUPPORT_CONVERTUTF_H
9292

93+
#include "llvm/Config/indexstoredb-prefix.h"
94+
9395
#include <cstddef>
9496
#include <string>
9597
#include <system_error>

include/llvm/Support/DataTypes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#ifndef SUPPORT_DATATYPES_H
2727
#define SUPPORT_DATATYPES_H
2828

29+
#include "llvm/Config/indexstoredb-prefix.h"
30+
2931
#define HAVE_INTTYPES_H 1
3032
#define HAVE_STDINT_H 1
3133
#define HAVE_UINT64_T 1

include/llvm/Support/Debug.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#ifndef LLVM_SUPPORT_DEBUG_H
3030
#define LLVM_SUPPORT_DEBUG_H
3131

32+
#include "llvm/Config/indexstoredb-prefix.h"
33+
3234
namespace llvm {
3335

3436
class raw_ostream;

include/llvm/Support/Errc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#ifndef LLVM_SUPPORT_ERRC_H
3131
#define LLVM_SUPPORT_ERRC_H
3232

33+
#include "llvm/Config/indexstoredb-prefix.h"
34+
3335
#include <system_error>
3436

3537
namespace llvm {

include/llvm/Support/Errno.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#ifndef LLVM_SUPPORT_ERRNO_H
1515
#define LLVM_SUPPORT_ERRNO_H
1616

17+
#include "llvm/Config/indexstoredb-prefix.h"
18+
1719
#include <cerrno>
1820
#include <string>
1921
#include <type_traits>

include/llvm/Support/Locale.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef LLVM_SUPPORT_LOCALE_H
22
#define LLVM_SUPPORT_LOCALE_H
33

4+
#include "llvm/Config/indexstoredb-prefix.h"
5+
46
namespace llvm {
57
class StringRef;
68

include/llvm/Support/ManagedStatic.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#ifndef LLVM_SUPPORT_MANAGEDSTATIC_H
1515
#define LLVM_SUPPORT_MANAGEDSTATIC_H
1616

17+
#include "llvm/Config/indexstoredb-prefix.h"
18+
1719
#include <atomic>
1820
#include <cstddef>
1921

include/llvm/Support/Signals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#ifndef LLVM_SUPPORT_SIGNALS_H
1616
#define LLVM_SUPPORT_SIGNALS_H
1717

18+
#include "llvm/Config/indexstoredb-prefix.h"
19+
1820
#include <string>
1921

2022
namespace llvm {

include/llvm/Support/Unicode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#ifndef LLVM_SUPPORT_UNICODE_H
1616
#define LLVM_SUPPORT_UNICODE_H
1717

18+
#include "llvm/Config/indexstoredb-prefix.h"
19+
1820
namespace llvm {
1921
class StringRef;
2022

include/llvm/Support/UniqueLock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#ifndef LLVM_SUPPORT_UNIQUE_LOCK_H
1616
#define LLVM_SUPPORT_UNIQUE_LOCK_H
1717

18+
#include "llvm/Config/indexstoredb-prefix.h"
19+
1820
#include <cassert>
1921

2022
namespace llvm {

include/llvm/Support/Valgrind.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#ifndef LLVM_SUPPORT_VALGRIND_H
1717
#define LLVM_SUPPORT_VALGRIND_H
1818

19+
#include "llvm/Config/indexstoredb-prefix.h"
20+
1921
#include <cstddef>
2022

2123
namespace llvm {

include/llvm/Support/WindowsError.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#ifndef LLVM_SUPPORT_WINDOWSERROR_H
1111
#define LLVM_SUPPORT_WINDOWSERROR_H
1212

13+
#include "llvm/Config/indexstoredb-prefix.h"
14+
1315
#include <system_error>
1416

1517
namespace llvm {

0 commit comments

Comments
 (0)