Skip to content

[llvm] Mangle all llvm symbols to avoid collisions #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ let package = Package(
.library(
name: "IndexStoreDB",
targets: ["IndexStoreDB"]),
.library(
name: "IndexStoreDB-dynamic",
type: .dynamic,
targets: ["IndexStoreDB"]),
.library(
name: "IndexStoreDB_CXX",
targets: ["IndexStoreDB_Index"]),
Expand Down
64 changes: 64 additions & 0 deletions Utilities/import-llvm
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import errno
import optparse
import os
import re
import shutil
import sys
import subprocess

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

def includes_prefix(source, include_dir):
defs = subprocess.check_output([
'clang', '-std=c++14', '-dM', '-E',
'-I', include_dir,
'-x', 'c++', source])

return "INDEXSTOREDB_PREFIX_H" in defs

def is_llvm_leaf_source(source):
# AIXDataTypes is a false negative.
return subprocess.call("grep '# *include *\"llvm' %s | grep --quiet -v AIXDataTypes" % source, shell=True)

def add_prefix(str):
snippet = '#include "llvm/Config/indexstoredb-prefix.h"\n'

m = re.search(r'#ifndef *([A-Za-z_]*)\n#define *\1 *\n', str)
if m:
return str[:m.end()] + '\n' + snippet + str[m.end():]

i = str.find(r'#include')
if i != -1:
return str[:i] + snippet + str[i:]

# If we ever hit this, we could find the end of the copyright header comment.
print >>sys.stderr, 'error: could not find existing #include or header guards'
sys.exit(1)

def maybe_add_prefix(source, include_dir):
ext = os.path.splitext(source)[1]
if not ext in ['.h', '.cpp', '.c']:
return
if not is_llvm_leaf_source(source):
# Skip files that include other llvm headers; we will add the include to
# the leaf header.
return
if includes_prefix(source, include_dir):
# Skip files that already include the prefix header.
return

note("adding prefix header to %r" % source)
with open(source, 'r+') as file:
new = add_prefix(file.read())
file.seek(0)
file.write(new)
file.truncate()

def main():
parser = optparse.OptionParser("usage: %prog <llvm-source-path>")
(opts, args) = parser.parse_args()
Expand Down Expand Up @@ -131,5 +179,21 @@ def main():
for name in C_imports:
import_c_header(name + '.h')

print ""
print "Adding prefix header includes"

base_dirs = [
os.path.join(sourcekit_srcroot, 'include', 'llvm'),
os.path.join(sourcekit_srcroot, 'include', 'llvm-c'),
os.path.join(sourcekit_srcroot, 'lib', 'LLVMSupport'),
]

include_dir = os.path.join(sourcekit_srcroot, 'include')

for base in base_dirs:
for root, dirs, files in os.walk(base):
for file in files:
maybe_add_prefix(os.path.join(root, file), include_dir)

if __name__ == '__main__':
main()
2 changes: 2 additions & 0 deletions include/llvm-c/DataTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#ifndef LLVM_C_DATATYPES_H
#define LLVM_C_DATATYPES_H

#include "llvm/Config/indexstoredb-prefix.h"

#ifdef __cplusplus
#include <cmath>
#else
Expand Down
2 changes: 2 additions & 0 deletions include/llvm-c/ErrorHandling.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#ifndef LLVM_C_ERROR_HANDLING_H
#define LLVM_C_ERROR_HANDLING_H

#include "llvm/Config/indexstoredb-prefix.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
2 changes: 2 additions & 0 deletions include/llvm/ADT/IntrusiveRefCntPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
#ifndef LLVM_ADT_INTRUSIVEREFCNTPTR_H
#define LLVM_ADT_INTRUSIVEREFCNTPTR_H

#include "llvm/Config/indexstoredb-prefix.h"

#include <atomic>
#include <cassert>
#include <cstddef>
Expand Down
2 changes: 2 additions & 0 deletions include/llvm/ADT/None.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#ifndef LLVM_ADT_NONE_H
#define LLVM_ADT_NONE_H

#include "llvm/Config/indexstoredb-prefix.h"

namespace llvm {
/// A simple null object to allow implicit construction of Optional<T>
/// and similar types without having to spell out the specialization's name.
Expand Down
2 changes: 2 additions & 0 deletions include/llvm/ADT/iterator_range.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifndef LLVM_ADT_ITERATOR_RANGE_H
#define LLVM_ADT_ITERATOR_RANGE_H

#include "llvm/Config/indexstoredb-prefix.h"

#include <iterator>
#include <utility>

Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Config/abi-breaking.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#ifndef LLVM_ABI_BREAKING_CHECKS_H
#define LLVM_ABI_BREAKING_CHECKS_H

#include "llvm/Config/indexstoredb-prefix.h"

/* Define to enable checks that alter the LLVM C++ ABI */
#define LLVM_ENABLE_ABI_BREAKING_CHECKS 0

Expand Down
23 changes: 23 additions & 0 deletions include/llvm/Config/indexstoredb-prefix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*===------- llvm/Config/indexstoredb-prefix.h --------------------*- C -*-===*/
/* */
/* The LLVM Compiler Infrastructure */
/* */
/* This file is distributed under the University of Illinois Open Source */
/* License. See LICENSE.TXT for details. */
/* */
/*===----------------------------------------------------------------------===*/

#ifndef INDEXSTOREDB_PREFIX_H
#define INDEXSTOREDB_PREFIX_H

/* HACK: Rename all of the llvm symbols so that they will not collide if another
* copy of llvm is linked into the same image. The use of llvm within IndexStore
* is purely an implementation detail. Using a source-level rename is a
* workaround for the lack of symbol visibility controls in swiftpm. Ideally we
* could do this with a combination of `-fvisibility=hidden` and `ld -r`.
*/

#define llvm indexstoredb_llvm
#define LLVMEnablePrettyStackTrace indexstoredb_LLVMEnablePrettyStackTrace

#endif // INDEXSTOREDB_PREFIX_H
2 changes: 2 additions & 0 deletions include/llvm/Config/llvm-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#ifndef LLVM_CONFIG_H
#define LLVM_CONFIG_H

#include "llvm/Config/indexstoredb-prefix.h"

/* Define if LLVM_ENABLE_DUMP is enabled */
/* #undef LLVM_ENABLE_DUMP */

Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Support/ARMBuildAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifndef LLVM_SUPPORT_ARMBUILDATTRIBUTES_H
#define LLVM_SUPPORT_ARMBUILDATTRIBUTES_H

#include "llvm/Config/indexstoredb-prefix.h"

namespace llvm {
class StringRef;

Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Support/Capacity.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef LLVM_SUPPORT_CAPACITY_H
#define LLVM_SUPPORT_CAPACITY_H

#include "llvm/Config/indexstoredb-prefix.h"

#include <cstddef>

namespace llvm {
Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Support/ConvertUTF.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
#ifndef LLVM_SUPPORT_CONVERTUTF_H
#define LLVM_SUPPORT_CONVERTUTF_H

#include "llvm/Config/indexstoredb-prefix.h"

#include <cstddef>
#include <string>
#include <system_error>
Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Support/DataTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#ifndef SUPPORT_DATATYPES_H
#define SUPPORT_DATATYPES_H

#include "llvm/Config/indexstoredb-prefix.h"

#define HAVE_INTTYPES_H 1
#define HAVE_STDINT_H 1
#define HAVE_UINT64_T 1
Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Support/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#ifndef LLVM_SUPPORT_DEBUG_H
#define LLVM_SUPPORT_DEBUG_H

#include "llvm/Config/indexstoredb-prefix.h"

namespace llvm {

class raw_ostream;
Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Support/Errc.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#ifndef LLVM_SUPPORT_ERRC_H
#define LLVM_SUPPORT_ERRC_H

#include "llvm/Config/indexstoredb-prefix.h"

#include <system_error>

namespace llvm {
Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Support/Errno.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#ifndef LLVM_SUPPORT_ERRNO_H
#define LLVM_SUPPORT_ERRNO_H

#include "llvm/Config/indexstoredb-prefix.h"

#include <cerrno>
#include <string>
#include <type_traits>
Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Support/Locale.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef LLVM_SUPPORT_LOCALE_H
#define LLVM_SUPPORT_LOCALE_H

#include "llvm/Config/indexstoredb-prefix.h"

namespace llvm {
class StringRef;

Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Support/ManagedStatic.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#ifndef LLVM_SUPPORT_MANAGEDSTATIC_H
#define LLVM_SUPPORT_MANAGEDSTATIC_H

#include "llvm/Config/indexstoredb-prefix.h"

#include <atomic>
#include <cstddef>

Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Support/Signals.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef LLVM_SUPPORT_SIGNALS_H
#define LLVM_SUPPORT_SIGNALS_H

#include "llvm/Config/indexstoredb-prefix.h"

#include <string>

namespace llvm {
Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Support/Unicode.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef LLVM_SUPPORT_UNICODE_H
#define LLVM_SUPPORT_UNICODE_H

#include "llvm/Config/indexstoredb-prefix.h"

namespace llvm {
class StringRef;

Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Support/UniqueLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef LLVM_SUPPORT_UNIQUE_LOCK_H
#define LLVM_SUPPORT_UNIQUE_LOCK_H

#include "llvm/Config/indexstoredb-prefix.h"

#include <cassert>

namespace llvm {
Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Support/Valgrind.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#ifndef LLVM_SUPPORT_VALGRIND_H
#define LLVM_SUPPORT_VALGRIND_H

#include "llvm/Config/indexstoredb-prefix.h"

#include <cstddef>

namespace llvm {
Expand Down
2 changes: 2 additions & 0 deletions include/llvm/Support/WindowsError.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#ifndef LLVM_SUPPORT_WINDOWSERROR_H
#define LLVM_SUPPORT_WINDOWSERROR_H

#include "llvm/Config/indexstoredb-prefix.h"

#include <system_error>

namespace llvm {
Expand Down