10 #include <llvm/IR/Module.h> 11 #include <llvm/Support/MemoryBuffer.h> 13 namespace fs = boost::filesystem;
18 std::vector<std::string> arguments, boost::string_view inputCCode,
19 std::unique_ptr<llvm::Module>* toFill) {
20 assert(toFill !=
nullptr &&
"null toFill passed to compileCToLLVM");
21 assert(fs::is_regular_file(ctollvmPath) &&
22 "invalid path passed to compileCToLLVM for ctollvmPath");
26 arguments.push_back(
"-nostdlib");
29 std::vector<fs::path> stdIncludePaths;
31 if (!res) {
return res; }
33 for (
const auto& p : stdIncludePaths) {
34 arguments.push_back(
"-I");
35 arguments.push_back(p.string());
41 std::unique_ptr<llvm::Module> mod;
44 std::vector<std::string> argsToChiCtoLLVM;
45 for (
const auto& arg : arguments) {
46 argsToChiCtoLLVM.push_back(
"-c");
47 argsToChiCtoLLVM.push_back(arg);
50 std::string generatedBitcode;
57 res += ctollvmExe.
start();
60 res += ctollvmExe.
pushToStdIn(inputCCode.data(), inputCCode.size());
63 if (!res) {
return res; }
66 auto errCode = ctollvmExe.
exitCode();
69 res.
addEntry(
"EUKN",
"Failed to Generate IR with clang", {{
"Error", errors}});
72 if (!errors.empty()) {
73 res.
addEntry(
"WUKN",
"Warnings emitted while generating IR with clang",
74 {{
"Warning", errors}});
78 {{
"Error parsing bitcode file generated from clang", &inputCCode[0]}});
79 res += parseBitcodeString(generatedBitcode, llvmContext, toFill);
82 if (*toFill ==
nullptr) {
83 res.
addEntry(
"EUKN",
"Failed to generate IR with clang", {{
"Error", errors}});
84 }
else if (!errors.empty()) {
85 res.
addEntry(
"WUKN",
"Warnings encountered while generating IR with clang",
void attachStringToStdOut(std::string &str)
Attach a string to stdout.
void addEntry(const char *ec, const char *overview, nlohmann::json data)
Add a entry to the result, either a warning or an error.
Defines the Result class and related functions.
Result stdCIncludePaths(std::vector< boost::filesystem::path > &toFill)
Gets the location of the standard C library to include.
int exitCode()
Wait and gets the exit code.
void attachStringToStdErr(std::string &str)
Attach a string to stderr.
Result closeStdIn()
Close the STDIN stream (send an EOF)
Result pushToStdIn(const char *data, size_t size)
Pushes data to the stdin stream of the child process.
Provides an platform-independent abstraction for creating subprocesses.
ScopedContext addScopedContext(const nlohmann::json &data)
Add a context with a scope Example usage: chi::Result res; res.contextJson(); // returns {} { aut...
Result start()
Start the process.
void setArguments(const ForwardIterator &begin, const ForwardIterator &end)
Set the arguments for the program with a begin and end iterator.
Result compileCToLLVM(const boost::filesystem::path &ctollvmPath, llvm::LLVMContext &llvmContext, std::vector< std::string > arguments, boost::string_view inputCCode, std::unique_ptr< llvm::Module > *toFill)
Use chi-ctollvm to compile C source code to a llvm module It also uses stdCIncludePaths to find basic...
The namespace where chigraph lives.
The result object, used for identifiying errors with good diagnostics.