diff options
author | Denis Steckelmacher <steckdenis@yahoo.fr> | 2011-07-05 18:12:50 +0200 |
---|---|---|
committer | Denis Steckelmacher <steckdenis@yahoo.fr> | 2011-07-05 18:12:50 +0200 |
commit | 83c322aaa606eebb6c3e1ace46de3bbf3e9ff72f (patch) | |
tree | 4d4549162641657f417b2ce260083f515b32c601 | |
parent | e4fd3e001e8d2d9603154c9a17d361e6e1731450 (diff) |
Little cleanup : avoid copying std::strings around.
-rw-r--r-- | src/core/compiler.cpp | 37 | ||||
-rw-r--r-- | src/core/compiler.h | 10 | ||||
-rw-r--r-- | src/core/program.cpp | 22 | ||||
-rw-r--r-- | src/core/program.h | 4 |
4 files changed, 40 insertions, 33 deletions
diff --git a/src/core/compiler.cpp b/src/core/compiler.cpp index bafeb99..c2bf018 100644 --- a/src/core/compiler.cpp +++ b/src/core/compiler.cpp @@ -15,9 +15,21 @@ using namespace Coal; -Compiler::Compiler(const std::string &options) -: p_valid(false), p_log_stream(p_log), p_log_printer(0) +Compiler::Compiler() +: p_log_stream(p_log), p_log_printer(0) { + +} + +Compiler::~Compiler() +{ + +} + +bool Compiler::setOptions(const std::string &options) +{ + p_options = options; + // Set codegen options clang::CodeGenOptions &codegen_opts = p_compiler.getCodeGenOpts(); codegen_opts.DebugInfo = false; @@ -138,21 +150,11 @@ Compiler::Compiler(const std::string &options) p_compiler.createDiagnostics(0, NULL, p_log_printer); if (!p_compiler.hasDiagnostics()) - return; + return false; p_compiler.getDiagnostics().setWarningsAsErrors(Werror); - p_valid = true; -} - -Compiler::~Compiler() -{ - -} - -bool Compiler::valid() const -{ - return p_valid; + return true; } llvm::Module *Compiler::compile(llvm::MemoryBuffer *source) @@ -183,7 +185,12 @@ llvm::Module *Compiler::compile(llvm::MemoryBuffer *source) return module; } -std::string Compiler::log() const +const std::string &Compiler::log() const { return p_log; } + +const std::string &Compiler::options() const +{ + return p_options; +} diff --git a/src/core/compiler.h b/src/core/compiler.h index ff0223e..3ef8d22 100644 --- a/src/core/compiler.h +++ b/src/core/compiler.h @@ -23,20 +23,20 @@ namespace Coal class Compiler { public: - Compiler(const std::string &options); + Compiler(); ~Compiler(); + bool setOptions(const std::string &options); llvm::Module *compile(llvm::MemoryBuffer *source); - std::string log() const; + const std::string &log() const; + const std::string &options() const; bool valid() const; private: - bool p_valid; - clang::CompilerInstance p_compiler; - std::string p_log; + std::string p_log, p_options; llvm::raw_string_ostream p_log_stream; clang::TextDiagnosticPrinter *p_log_printer; }; diff --git a/src/core/program.cpp b/src/core/program.cpp index 7ec5f58..86cb905 100644 --- a/src/core/program.cpp +++ b/src/core/program.cpp @@ -25,12 +25,16 @@ Program::Program(Context *ctx) { // Retain parent context clRetainContext((cl_context)ctx); + + p_compiler = new Compiler(); } Program::~Program() { clReleaseContext((cl_context)p_ctx); + delete p_compiler; + if (p_devices) std::free((void *)p_devices); } @@ -134,8 +138,6 @@ cl_int Program::build(const char *options, void *user_data, cl_uint num_devices, const cl_device_id *device_list) { - p_options = std::string(options); - // Set device infos if (!p_num_devices) { @@ -151,9 +153,7 @@ cl_int Program::build(const char *options, // Do we need to compile a source ? if (p_type == Source) { - Compiler *compiler = new Compiler(p_options); - - if (!compiler->valid()) + if (!p_compiler->setOptions(options)) { if (pfn_notify) pfn_notify((cl_program)this, user_data); @@ -168,9 +168,7 @@ cl_int Program::build(const char *options, llvm::MemoryBuffer *buffer = llvm::MemoryBuffer::getMemBuffer(s_data, s_name); - p_linked_module = compiler->compile(buffer); - p_log = compiler->log(); - delete compiler; + p_linked_module = p_compiler->compile(buffer); if (!p_linked_module) { @@ -335,13 +333,13 @@ cl_int Program::buildInfo(cl_context_info param_name, break; case CL_PROGRAM_BUILD_OPTIONS: - value = p_options.c_str(); - value_length = p_options.size() + 1; + value = p_compiler->options().c_str(); + value_length = p_compiler->options().size() + 1; break; case CL_PROGRAM_BUILD_LOG: - value = p_log.c_str(); - value_length = p_log.size() + 1; + value = p_compiler->log().c_str(); + value_length = p_compiler->log().size() + 1; break; default: diff --git a/src/core/program.h b/src/core/program.h index cce0cb3..8950562 100644 --- a/src/core/program.h +++ b/src/core/program.h @@ -14,6 +14,7 @@ namespace Coal { class Context; +class Compiler; class Program { @@ -66,13 +67,14 @@ class Program private: Context *p_ctx; + Compiler *p_compiler; unsigned int p_references; Type p_type; State p_state; cl_uint p_num_devices; cl_device_id *p_devices; - std::string p_source, p_options, p_log; + std::string p_source; std::string p_unlinked_binary; llvm::Module *p_linked_module; }; |