{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Ignore litgen warnings\n", "\n", "When processing code, litgen may emit some warnings when it encounters code that it cannot process. If possible, you can change the options in order to handle those warnings. \n", "\n", "\n", "## Example code with potential warnings\n", "\n", "However, there may be some warnings that you want to ignore. See the example below, where we use the code from a file name `10_05_10_sample_code.h`.\n", "\n", "Let's load it and display its content:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
// This will trigger a warning:\n",
       "//     Ignoring template function. You might need to set LitgenOptions.fn_template_options\n",
       "//     While parsing a "function_decl", corresponding to this C++ code:\n",
       "//    Position:4:1\n",
       "//        template<typename T> void MyOperation(T value);\n",
       "template<typename T> void MyOperation(T value);\n",
       "\n",
       "\n",
       "struct Foo {};\n",
       "\n",
       "\n",
       "// This will generate a warning:\n",
       "//    operators are supported only when implemented as a member functions\n",
       "// And this operator will not be exported\n",
       "bool operator==(const Foo& v1, const Foo& v2);\n",
       "\n",
       "\n",
       "// litgen does not support C-style function parameters\n",
       "// This function will trigger a warning: "Can't use a function_decl as a param"\n",
       "int CallOperation(int (*functionPtr)(int, int), int a, int b) {\n",
       "    return (*functionPtr)(a, b);\n",
       "}\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import litgen\n", "from litgen.demo import litgen_demo\n", "\n", "with open(\"10_05_10_sample_code.h\") as f:\n", " cpp_code = f.read()\n", "\n", "litgen_demo.show_cpp_code(cpp_code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Warnings emitted by litgen\n", "\n", "Now, let's process it with litgen, and see that we encounter many warnings:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Warning: (SrcmlcppIgnoreElement) A cpp element of type \"function\" was stored as CppUnprocessed. Details follow\n", "Warning: (SrcmlcppIgnoreElement) Can't use a function_decl as a param.\n", "While parsing a \"function_decl\", corresponding to this C++ code:\n", "10_05_10_sample_code.h:21:19\n", " int CallOperation(int (*functionPtr)(int, int), int a, int b) {\n", " ^\n", "\n", "Warning: (LitgenBlockElementException) Warning: (LitgenBlockElementException) operators are supported only when implemented as a member functions\n", "While parsing a \"function_decl\", corresponding to this C++ code:\n", "10_05_10_sample_code.h:16:1\n", " bool operator==(const Foo& v1, const Foo& v2);\n", " ^\n", "\n", "Warning: (LitgenTemplateFunctionIgnore) Ignoring template function MyOperation. You might need to set LitgenOptions.fn_template_options\n", "While parsing a \"function_decl\", corresponding to this C++ code:\n", "10_05_10_sample_code.h:7:1\n", " template void MyOperation(T value);\n", " ^\n", "Warning: (LitgenTemplateFunctionIgnore) Ignoring template function MyOperation. You might need to set LitgenOptions.fn_template_options\n", "While parsing a \"function_decl\", corresponding to this C++ code:\n", "10_05_10_sample_code.h:7:1\n", " template void MyOperation(T value);\n", " ^\n" ] } ], "source": [ "options = litgen.LitgenOptions()\n", "generated_code = litgen.generate_code_for_file(options, \"10_05_10_sample_code.h\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Suppress the warnings\n", "\n", "Once you studied the warnings, and once you know they are harlmess, you can suppress them by filling `options.srcmlcpp_options.ignored_warning_parts`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
####################    <generated_from:10_05_10_sample_code.h>    ####################\n",
       "\n",
       "\n",
       "\n",
       "class Foo:\n",
       "    def __init__(self) -> None:\n",
       "        """Auto-generated default constructor"""\n",
       "        pass\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "####################    </generated_from:10_05_10_sample_code.h>    ####################\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "options = litgen.LitgenOptions()\n", "options.srcmlcpp_options.ignored_warning_parts.append(\"Can't use a function_decl as a param\")\n", "options.srcmlcpp_options.ignored_warning_parts.append(\n", " \"operators are supported only when implemented as a member functions\"\n", ")\n", "options.srcmlcpp_options.ignored_warning_parts.append(\"Ignoring template function MyOperation\")\n", "\n", "# The next line does not emit warnings anymore\n", "generated_code = litgen.generate_code_for_file(options, \"10_05_10_sample_code.h\")\n", "\n", "# The generated code will include only what was correctly converted\n", "litgen_demo.show_python_code(generated_code.stub_code)" ] } ], "metadata": { "kernelspec": { "display_name": "venv311", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.0" } }, "nbformat": 4, "nbformat_minor": 2 }