Skip to content

Commit 9b6c813

Browse files
committed
Revert "Reland "[clang-repl] Re-implement clang-interpreter as a test case.""
This reverts commit f0514a4. Test fails on macOS: https://reviews.llvm.org/D107049#2976603
1 parent 2d400db commit 9b6c813

File tree

16 files changed

+168
-201
lines changed

16 files changed

+168
-201
lines changed

clang/docs/ClangFormattedStatus.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ tree in terms of conformance to :doc:`ClangFormat` as of: June 04, 2021 13:01:37
5959
- `1`
6060
- `0`
6161
- :good:`100%`
62+
* - clang/examples/clang-interpreter
63+
- `1`
64+
- `0`
65+
- `1`
66+
- :none:`0%`
6267
* - clang/examples/PrintFunctionNames
6368
- `1`
6469
- `0`

clang/examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ if(NOT CLANG_BUILD_EXAMPLES)
33
set(EXCLUDE_FROM_ALL ON)
44
endif()
55

6+
add_subdirectory(clang-interpreter)
67
add_subdirectory(PrintFunctionNames)
78
add_subdirectory(AnnotateFunctions)
89
add_subdirectory(Attribute)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
set(LLVM_LINK_COMPONENTS
2+
Core
3+
ExecutionEngine
4+
MC
5+
MCJIT
6+
Object
7+
OrcJit
8+
Option
9+
RuntimeDyld
10+
Support
11+
native
12+
)
13+
14+
add_clang_executable(clang-interpreter
15+
main.cpp
16+
)
17+
18+
add_dependencies(clang-interpreter
19+
clang-resource-headers
20+
)
21+
22+
clang_target_link_libraries(clang-interpreter
23+
PRIVATE
24+
clangBasic
25+
clangCodeGen
26+
clangDriver
27+
clangFrontend
28+
clangSerialization
29+
)
30+
31+
export_executable_symbols(clang-interpreter)
32+
33+
if (MSVC)
34+
# Is this a CMake bug that even with export_executable_symbols, Windows
35+
# needs to explictly export the type_info vtable
36+
set_property(TARGET clang-interpreter
37+
APPEND_STRING PROPERTY LINK_FLAGS " /EXPORT:??_7type_info@@6B@")
38+
endif()
39+
40+
function(clang_enable_exceptions TARGET)
41+
# Really have to jump through hoops to enable exception handling independent
42+
# of how LLVM is being built.
43+
if (NOT LLVM_REQUIRES_EH AND NOT LLVM_REQUIRES_RTTI)
44+
if (MSVC)
45+
# /EHs to allow throwing from extern "C"
46+
set(excptnExceptions_ON "/D _HAS_EXCEPTIONS=1 /EHs /wd4714")
47+
set(excptnExceptions_OFF "/D _HAS_EXCEPTIONS=0 /EHs-c-")
48+
set(excptnRTTI_ON "/GR")
49+
set(excptnRTTI_OFF "/GR-")
50+
set(excptnEHRTTIRegEx "(/EHs(-c-?)|_HAS_EXCEPTIONS=(0|1))")
51+
else()
52+
set(excptnExceptions_ON "-fexceptions")
53+
set(excptnExceptions_OFF "-fno-exceptions")
54+
set(excptnRTTI_ON "-frtti")
55+
set(excptnRTTI_OFF "-fno-rtti")
56+
set(excptnEHRTTIRegEx "-f(exceptions|no-exceptions)")
57+
endif()
58+
if (LLVM_REQUIRES_EH)
59+
set(excptnExceptions_DFLT ${excptnExceptions_ON})
60+
else()
61+
set(excptnExceptions_DFLT ${excptnExceptions_OFF})
62+
endif()
63+
if (LLVM_REQUIRES_RTTI)
64+
set(excptnRTTI_DFLT ${excptnRTTI_ON})
65+
else()
66+
set(excptnRTTI_DFLT ${excptnRTTI_OFF})
67+
endif()
68+
69+
# Strip the exception & rtti flags from the target
70+
get_property(addedFlags TARGET ${TARGET} PROPERTY COMPILE_FLAGS)
71+
string(REGEX REPLACE ${excptnEHRTTIRegEx} "" editedFlags "${addedFlags}")
72+
string(REPLACE ${excptnRTTI_OFF} "" editedFlags "${editedFlags}")
73+
set_property(TARGET ${TARGET} PROPERTY COMPILE_FLAGS "${editedFlags}")
74+
75+
get_property(addedFlags TARGET ${TARGET} PROPERTY COMPILE_DEFINITIONS)
76+
string(REGEX REPLACE ${excptnEHRTTIRegEx} "" editedFlags "${addedFlags}")
77+
string(REPLACE ${excptnRTTI_OFF} "" editedFlags "${editedFlags}")
78+
set_property(TARGET ${TARGET} PROPERTY COMPILE_DEFINITIONS "${editedFlags}")
79+
80+
# Re-add the exception & rtti flags from LLVM
81+
set_property(SOURCE main.cpp APPEND_STRING PROPERTY COMPILE_FLAGS
82+
" ${excptnExceptions_DFLT} ${excptnRTTI_DFLT} ")
83+
set_property(SOURCE Manager.cpp APPEND_STRING PROPERTY COMPILE_FLAGS
84+
" ${excptnExceptions_DFLT} ${excptnRTTI_DFLT} ")
85+
86+
# Invoke with exceptions & rtti
87+
set_property(SOURCE Invoke.cpp APPEND_STRING PROPERTY COMPILE_FLAGS
88+
" ${excptnExceptions_ON} ${excptnRTTI_ON} ")
89+
90+
endif()
91+
endfunction(clang_enable_exceptions)
92+
93+
clang_enable_exceptions(clang-interpreter)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
This is an example of Clang based interpreter, for executing standalone C/C++
2+
programs.
3+
4+
It demonstrates the following features:
5+
1. Parsing standard compiler command line arguments using the Driver library.
6+
7+
2. Constructing a Clang compiler instance, using the appropriate arguments
8+
derived in step #1.
9+
10+
3. Invoking the Clang compiler to lex, parse, syntax check, and then generate
11+
LLVM code.
12+
13+
4. Use the LLVM JIT functionality to execute the final module.
14+
15+
5. Intercepting a Win64 library call to allow throwing and catching exceptions
16+
in and from the JIT.
17+
18+
The implementation has many limitations and is not designed to be a full fledged
19+
interpreter. It is designed to demonstrate a simple but functional use of the
20+
Clang compiler libraries.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- examples/clang-interpreter/Test.cxx - Clang C Interpreter Example -===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// Example throwing in and from the JIT (particularly on Win64).
10+
//
11+
// ./bin/clang-interpreter <src>/tools/clang/examples/clang-interpreter/Test.cxx
12+
13+
#include <stdexcept>
14+
#include <stdio.h>
15+
16+
static void ThrowerAnError(const char* Name) {
17+
throw std::runtime_error(Name);
18+
}
19+
20+
int main(int argc, const char** argv) {
21+
for (int I = 0; I < argc; ++I)
22+
printf("arg[%d]='%s'\n", I, argv[I]);
23+
24+
try {
25+
ThrowerAnError("In JIT");
26+
} catch (const std::exception& E) {
27+
printf("Caught: '%s'\n", E.what());
28+
} catch (...) {
29+
printf("Unknown exception\n");
30+
}
31+
ThrowerAnError("From JIT");
32+
return 0;
33+
}

clang/include/clang/Interpreter/Interpreter.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include "clang/Interpreter/PartialTranslationUnit.h"
1818

19-
#include "llvm/ExecutionEngine/JITSymbol.h"
2019
#include "llvm/Support/Error.h"
2120

2221
#include <memory>
@@ -66,8 +65,6 @@ class Interpreter {
6665
return Execute(*PTU);
6766
return llvm::Error::success();
6867
}
69-
llvm::Expected<llvm::JITTargetAddress>
70-
getSymbolAddress(llvm::StringRef UnmangledName) const;
7168
};
7269
} // namespace clang
7370

clang/lib/Interpreter/IncrementalExecutor.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,4 @@ llvm::Error IncrementalExecutor::runCtors() const {
6060
return Jit->initialize(Jit->getMainJITDylib());
6161
}
6262

63-
llvm::Expected<llvm::JITTargetAddress>
64-
IncrementalExecutor::getSymbolAddress(llvm::StringRef UnmangledName) const {
65-
auto Sym = Jit->lookup(UnmangledName);
66-
if (!Sym)
67-
return Sym.takeError();
68-
return Sym->getAddress();
69-
}
70-
7163
} // end namespace clang

clang/lib/Interpreter/IncrementalExecutor.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ class IncrementalExecutor {
4141

4242
llvm::Error addModule(std::unique_ptr<llvm::Module> M);
4343
llvm::Error runCtors() const;
44-
llvm::Expected<llvm::JITTargetAddress>
45-
getSymbolAddress(llvm::StringRef UnmangledName) const;
4644
};
4745

4846
} // end namespace clang

clang/lib/Interpreter/Interpreter.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
using namespace clang;
3636

3737
// FIXME: Figure out how to unify with namespace init_convenience from
38-
// tools/clang-import-test/clang-import-test.cpp
38+
// tools/clang-import-test/clang-import-test.cpp and
39+
// examples/clang-interpreter/main.cpp
3940
namespace {
4041
/// Retrieves the clang CC1 specific flags out of the compilation's jobs.
4142
/// \returns NULL on error.
@@ -222,13 +223,3 @@ llvm::Error Interpreter::Execute(PartialTranslationUnit &T) {
222223

223224
return llvm::Error::success();
224225
}
225-
226-
llvm::Expected<llvm::JITTargetAddress>
227-
Interpreter::getSymbolAddress(llvm::StringRef UnmangledName) const {
228-
if (!IncrExecutor)
229-
return llvm::make_error<llvm::StringError>("Operation failed. "
230-
"No execution engine",
231-
std::error_code());
232-
233-
return IncrExecutor->getSymbolAddress(UnmangledName);
234-
}

clang/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ if (CLANG_BUILD_EXAMPLES)
9696
Attribute
9797
AnnotateFunctions
9898
CallSuperAttr
99+
clang-interpreter
99100
PrintFunctionNames
100101
)
101102
endif ()

clang/test/Misc/interpreter.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: clang-interpreter %s | FileCheck %s
2+
// REQUIRES: native, examples
3+
4+
int printf(const char *, ...);
5+
6+
int main() {
7+
// CHECK: {{Hello world!}}
8+
printf("Hello world!\n");
9+
return 0;
10+
}

clang/test/lit.cfg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171

7272
if config.clang_examples:
7373
config.available_features.add('examples')
74+
tools.append('clang-interpreter')
7475

7576
def have_host_jit_support():
7677
clang_repl_exe = lit.util.which('clang-repl', config.clang_tools_dir)
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
set(LLVM_LINK_COMPONENTS
2-
${LLVM_TARGETS_TO_BUILD}
32
Core
4-
OrcJIT
5-
Support
63
)
74

85
add_clang_unittest(ClangReplInterpreterTests
@@ -15,8 +12,3 @@ target_link_libraries(ClangReplInterpreterTests PUBLIC
1512
clangInterpreter
1613
clangFrontend
1714
)
18-
19-
# Exceptions on Windows are not yet supported.
20-
if(NOT WIN32)
21-
add_subdirectory(ExceptionTests)
22-
endif()

clang/unittests/Interpreter/ExceptionTests/CMakeLists.txt

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)