Skip to content

Commit 2a4a852

Browse files
Reland [clang-repl] Expose setter for triple in IncrementalCompilerBuilder (#84174)
With out-of-process execution the target triple can be different from the one on the host. We need an interface to configure it. Relanding this with cleanup-fixes in the unittest.
1 parent f78129e commit 2a4a852

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

clang/include/clang/Interpreter/Interpreter.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class IncrementalCompilerBuilder {
4848
UserArgs = Args;
4949
}
5050

51+
void SetTargetTriple(std::string TT) { TargetTriple = TT; }
52+
5153
// General C++
5254
llvm::Expected<std::unique_ptr<CompilerInstance>> CreateCpp();
5355

@@ -62,11 +64,12 @@ class IncrementalCompilerBuilder {
6264

6365
private:
6466
static llvm::Expected<std::unique_ptr<CompilerInstance>>
65-
create(std::vector<const char *> &ClangArgv);
67+
create(std::string TT, std::vector<const char *> &ClangArgv);
6668

6769
llvm::Expected<std::unique_ptr<CompilerInstance>> createCuda(bool device);
6870

6971
std::vector<const char *> UserArgs;
72+
std::optional<std::string> TargetTriple;
7073

7174
llvm::StringRef OffloadArch;
7275
llvm::StringRef CudaSDKPath;

clang/lib/Interpreter/Interpreter.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {
132132
} // anonymous namespace
133133

134134
llvm::Expected<std::unique_ptr<CompilerInstance>>
135-
IncrementalCompilerBuilder::create(std::vector<const char *> &ClangArgv) {
135+
IncrementalCompilerBuilder::create(std::string TT,
136+
std::vector<const char *> &ClangArgv) {
136137

137138
// If we don't know ClangArgv0 or the address of main() at this point, try
138139
// to guess it anyway (it's possible on some platforms).
@@ -162,8 +163,7 @@ IncrementalCompilerBuilder::create(std::vector<const char *> &ClangArgv) {
162163
TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
163164
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
164165

165-
driver::Driver Driver(/*MainBinaryName=*/ClangArgv[0],
166-
llvm::sys::getProcessTriple(), Diags);
166+
driver::Driver Driver(/*MainBinaryName=*/ClangArgv[0], TT, Diags);
167167
Driver.setCheckInputsExist(false); // the input comes from mem buffers
168168
llvm::ArrayRef<const char *> RF = llvm::ArrayRef(ClangArgv);
169169
std::unique_ptr<driver::Compilation> Compilation(Driver.BuildCompilation(RF));
@@ -185,7 +185,8 @@ IncrementalCompilerBuilder::CreateCpp() {
185185
Argv.push_back("-xc++");
186186
Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
187187

188-
return IncrementalCompilerBuilder::create(Argv);
188+
std::string TT = TargetTriple ? *TargetTriple : llvm::sys::getProcessTriple();
189+
return IncrementalCompilerBuilder::create(TT, Argv);
189190
}
190191

191192
llvm::Expected<std::unique_ptr<CompilerInstance>>
@@ -213,7 +214,8 @@ IncrementalCompilerBuilder::createCuda(bool device) {
213214

214215
Argv.insert(Argv.end(), UserArgs.begin(), UserArgs.end());
215216

216-
return IncrementalCompilerBuilder::create(Argv);
217+
std::string TT = TargetTriple ? *TargetTriple : llvm::sys::getProcessTriple();
218+
return IncrementalCompilerBuilder::create(TT, Argv);
217219
}
218220

219221
llvm::Expected<std::unique_ptr<CompilerInstance>>

clang/unittests/Interpreter/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS
77
)
88

99
add_clang_unittest(ClangReplInterpreterTests
10+
IncrementalCompilerBuilderTest.cpp
1011
IncrementalProcessingTest.cpp
1112
InterpreterTest.cpp
1213
CodeCompletionTest.cpp
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//=== unittests/Interpreter/IncrementalCompilerBuilderTest.cpp ------------===//
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+
#include "clang/Basic/TargetOptions.h"
10+
#include "clang/Frontend/CompilerInstance.h"
11+
#include "clang/Interpreter/Interpreter.h"
12+
#include "clang/Lex/PreprocessorOptions.h"
13+
#include "llvm/Support/Error.h"
14+
#include "gtest/gtest.h"
15+
16+
using namespace llvm;
17+
using namespace clang;
18+
19+
namespace {
20+
21+
// Usually FrontendAction takes the raw pointers and wraps them back into
22+
// unique_ptrs in InitializeFileRemapping()
23+
static void cleanupRemappedFileBuffers(CompilerInstance &CI) {
24+
for (const auto &RB : CI.getPreprocessorOpts().RemappedFileBuffers) {
25+
delete RB.second;
26+
}
27+
CI.getPreprocessorOpts().clearRemappedFiles();
28+
}
29+
30+
TEST(IncrementalCompilerBuilder, SetCompilerArgs) {
31+
std::vector<const char *> ClangArgv = {"-Xclang", "-ast-dump-all"};
32+
auto CB = clang::IncrementalCompilerBuilder();
33+
CB.SetCompilerArgs(ClangArgv);
34+
auto CI = cantFail(CB.CreateCpp());
35+
EXPECT_TRUE(CI->getFrontendOpts().ASTDumpAll);
36+
cleanupRemappedFileBuffers(*CI);
37+
}
38+
39+
TEST(IncrementalCompilerBuilder, SetTargetTriple) {
40+
auto CB = clang::IncrementalCompilerBuilder();
41+
CB.SetTargetTriple("armv6-none-eabi");
42+
auto CI = cantFail(CB.CreateCpp());
43+
EXPECT_EQ(CI->getTargetOpts().Triple, "armv6-none-unknown-eabi");
44+
cleanupRemappedFileBuffers(*CI);
45+
}
46+
47+
} // end anonymous namespace

0 commit comments

Comments
 (0)