From 023fabe89ef5652c6c93021107348308a410e415 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Thu, 1 Feb 2024 21:59:30 +0800 Subject: [PATCH] feat(c): add support for adding source code to the program #24 This commit adds the ability to add source code to the program in the chapi-ast-c module. The `addSource` method now takes a string representation of the source code and adds it to the program. This allows for the inclusion of C header files and other source code files. --- .../main/kotlin/chapi/ast/cast/CAnalyser.kt | 12 +------ .../chapi/ast/cast/CFullIdentListenerTest.kt | 2 -- .../src/test/resources/realworld/SlowMacro.c | 36 ------------------- .../src/test/resources/realworld/SlowMacro2.h | 22 ------------ 4 files changed, 1 insertion(+), 71 deletions(-) delete mode 100644 chapi-ast-c/src/test/resources/realworld/SlowMacro.c delete mode 100644 chapi-ast-c/src/test/resources/realworld/SlowMacro2.h diff --git a/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CAnalyser.kt b/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CAnalyser.kt index 1282e334..5e211c98 100644 --- a/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CAnalyser.kt +++ b/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CAnalyser.kt @@ -24,17 +24,7 @@ open class CAnalyser : Analyser { } /** - * Adds a source code to the program. - * - * This method takes a string representation of the source code and adds it to the program. The source code is read from - * an input stream created from the given string. The input stream is then passed to the LexerSource object, which - * tokenizes the code and adds it to the program's input. - * - * @param code The string representation of the source code to be added. - * - * @throws IOException if an I/O error occurs while reading the source code. - * - * @see LexerSource + * Adds a source code to the program, for example, C header files. */ fun addSource(code: String) { pp.addInput(LexerSource(InputStreamReader(code.byteInputStream()), true)) diff --git a/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt b/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt index 49c940e2..d9f70d76 100644 --- a/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt +++ b/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt @@ -4,10 +4,8 @@ import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.runBlocking -import org.anarres.cpp.* import org.junit.jupiter.api.Test import java.io.File -import java.io.InputStreamReader import kotlin.test.assertEquals diff --git a/chapi-ast-c/src/test/resources/realworld/SlowMacro.c b/chapi-ast-c/src/test/resources/realworld/SlowMacro.c deleted file mode 100644 index 06d011c0..00000000 --- a/chapi-ast-c/src/test/resources/realworld/SlowMacro.c +++ /dev/null @@ -1,36 +0,0 @@ -#ifdef _WIN32 -static BOOL WINAPI -_tls_callback(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { - switch (fdwReason) { -#ifdef JEMALLOC_LAZY_LOCK - case DLL_THREAD_ATTACH: - isthreaded = true; - break; -#endif - case DLL_THREAD_DETACH: - _malloc_thread_cleanup(); - break; - default: - break; - } - return true; -} - -#ifdef read -# undef read -#endif - -#ifdef _MSC_VER -# ifdef _M_IX86 -# pragma comment(linker, "/INCLUDE:__tls_used") -# pragma comment(linker, "/INCLUDE:_tls_callback") -# else -# pragma comment(linker, "/INCLUDE:_tls_used") -# pragma comment(linker, "/INCLUDE:" STRINGIFY(tls_callback) ) -# endif -# pragma section(".CRT$XLY",long,read) -#endif -JEMALLOC_SECTION(".CRT$XLY") JEMALLOC_ATTR(used) -BOOL (WINAPI *const tls_callback)(HINSTANCE hinstDLL, - DWORD fdwReason, LPVOID lpvReserved) = _tls_callback; -#endif diff --git a/chapi-ast-c/src/test/resources/realworld/SlowMacro2.h b/chapi-ast-c/src/test/resources/realworld/SlowMacro2.h deleted file mode 100644 index 25f03023..00000000 --- a/chapi-ast-c/src/test/resources/realworld/SlowMacro2.h +++ /dev/null @@ -1,22 +0,0 @@ -#define MAXASIZE (1 << MAXBITS) -#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) -#define hashstr(t,str) hashpow2(t, (str)->tsv.hash) -#define hashboolean(t,p) hashpow2(t, p) -#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) -#define hashpointer(t,p) hashmod(t, IntPoint(p)) -#define numints cast_int(sizeof(lua_Number)/sizeof(int)) -#define dummynode (&dummynode_) - -#ifndef MINSTRTABSIZE -#define MINSTRTABSIZE 32 -#endif - -#ifndef LUA_MINBUFFER -#define LUA_MINBUFFER 32 -#endif - -#ifndef lua_lock -#define lua_lock(L) ((void) 0) -#define lua_unlock(L) ((void) 0) -#endif -