/tccpp

stripped-down Tiny C Compiler (tcc) for doing OpenCL C preprocessing

Primary LanguageCGNU Lesser General Public License v2.1LGPL-2.1

TCCPP & TCC – Tiny C Compiler

Copyright © 2001-2004 Fabrice Bellard
Copyright © 2013 Florian Ziesche (stripped-down TCCPP version)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

Purpose of this project:
After much thought (and attempts to the contrary), I finally decided that I needed a C preprocessor for the oclraster project to preprocess user provided OpenCL/oclraster shaders/programs.
Note: even though this project can still be compiled and used as a standalone binary/preprocessor, its use in that state is relatively limited, since I’ve removed most of the unnecessary command line and system specific functionality.

in-memory preprocessing sample (mixed C/C++)

string preprocess_code(const int argc, const char** argv, const string& raw_code) {
    // init
    string ret_code = "";
    TCCState* state = tcc_new();
    state->output_type = TCC_OUTPUT_PREPROCESS;
    // let tcc parse the input arguments
    tcc_parse_args(state, argc, argv);
    // in-memory preprocessing
    const uint8_t* code_input = (const uint8_t*)raw_code.c_str();
    tcc_in_memory_preprocess(// the just created tcc state object - thanks to this, tccpp is
                             // multi-threading capable, i.e. you can create a TCCState object for
                             // each thread and then call tcc_in_memory_preprocess from each
                             // thread with their resp. state object
                             state,
                             // code input string + length
                             code_input, raw_code.length(),
                             // whether to print/add the include stack or not
                             true,
                             // the filename that should be used for this in-memory code
                             // - probably only useful when include stack printing is enabled
                             "in_memory_file.c",
                             // preprocessed output (manual string concat)
                             &ret_code,
                             // user provided function that is called for each preprocessed token
                             // here: just add the token to ret/ret_code
                             [](const char* str, void* ret) -> void {
                                 *(string*)ret += str;
                             });
    // cleanup + return
    tcc_delete(state);
    return ret_code;
}