Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix modules always being visible when not imported #6

Merged
merged 8 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .vscode/cthulhu.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@
"scope": "c,cpp",
"prefix": "gpl3",
"body": [
"// SPDX-License-Identifier: GPL-3.0-or-later",
"// SPDX-License-Identifier: GPL-3.0-only",
]
},
"LGPL3 SDPX License": {
"scope": "c,cpp",
"prefix": "lgpl3",
"body": [
"// SPDX-License-Identifier: LGPL-3.0-or-later",
"// SPDX-License-Identifier: LGPL-3.0-only",
]
},
}
51 changes: 39 additions & 12 deletions data/examples/config/config.ct
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export struct ConfigFile {
fileSize: usize;

sections: [*]ConfigSection;
sectionCount: usize;
sectionCount: uint;

entries: [*]ConfigEntry;
entryCount: usize;
entryCount: uint;
}

def reportErrorMessage(name: str) {
Expand All @@ -45,6 +45,8 @@ struct Lexer {
current: *char;
}

export def stringViewLength(view: StringView): usize = cast(view.back as uptrdiff) - cast(view.front as uptrdiff);

def isAlpha(letter: char): bool = (letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z');
def isSpace(letter: char): bool = letter == ' ' || letter == '\t' || letter == '\n' || letter == '\r';

Expand All @@ -68,26 +70,23 @@ export def openConfigFile(path: str): ConfigFile {
return .{ };
}

var sections: [*]ConfigSection = libc::malloc(__sizeof(ConfigSection) * 8);
var entries: [*]ConfigEntry = libc::malloc(__sizeof(ConfigEntry) * 16);
var sections: [*]ConfigSection = libc::malloc(__sizeof(ConfigSection) * 64);
var entries: [*]ConfigEntry = libc::malloc(__sizeof(ConfigEntry) * 256);

file.sections = sections;
file.entries = entries;

const innerName = kGlobalSectionName;

const globalName: StringView = .{
front = kGlobalSectionName,
back = &kGlobalSectionName[6]
};

sections[0] = .{
name = globalName,
name = .{
front = kGlobalSectionName,
back = &kGlobalSectionName[6]
},
firstEntry = 0,
lastEntry = 0
};

file.sectionCount = 1;
file.entryCount = 0;

var index: uint = 0;
while index < file.fileSize {
Expand All @@ -100,6 +99,7 @@ export def openConfigFile(path: str): ConfigFile {

if letter == '[' {
const name = readUntil(file.fileView + index, ']');
index = index + stringViewLength(name) + 1;

sections[file.sectionCount] = .{
name = name,
Expand All @@ -108,10 +108,37 @@ export def openConfigFile(path: str): ConfigFile {
};

file.sectionCount = file.sectionCount + 1;

continue;
}

if isAlpha(letter) {
index = index - 1;
var key = readUntil(file.fileView + index, '=');
key.back = &key.back[-1];

index = index + stringViewLength(key);

const value = readUntil(file.fileView + index, '\n');
index = index + stringViewLength(value) + 1;

entries[file.entryCount] = .{
key = key,
value = value
};

sections[file.sectionCount - 1].lastEntry = file.entryCount;

file.entryCount = file.entryCount + 1;

continue;
}

if letter == ';' {
const comment = readUntil(file.fileView + index, '\n');
index = index + stringViewLength(comment) + 1;

continue;
}
}

Expand Down
2 changes: 1 addition & 1 deletion data/examples/config/example.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# root entries
; root entries
key = value

[section1]
Expand Down
28 changes: 24 additions & 4 deletions data/examples/config/main.ct
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
import example::config;
import example::config as cfg;
import cstdlib as libc;
import win32;

@entry(cli)
def main {
var file = config::openConfigFile("example.ini");
var file = cfg::openConfigFile("example.ini");
if file.fileView == libc::null {
cstdlib::printf("Failed to open file\n");
libc::exit(1);
}
cstdlib::printf("sectionCount: %zu, entryCount: %zu\n", file.sectionCount, file.entryCount);
config::closeConfigFile(file);

libc::printf("sectionCount: %zu, entryCount: %zu\n", file.sectionCount, file.entryCount);

var index: uint = 0;
while index < file.sectionCount {
const section = file.sections[index];
const nameLength = cfg::stringViewLength(section.name);
libc::printf("Section %u: %.*s (%u entries)\n", index, nameLength, section.name.front, (section.lastEntry - section.firstEntry) + 1);

var entryIndex = section.firstEntry;
while entryIndex <= section.lastEntry {
const entry = file.entries[entryIndex];
const keyLength = cfg::stringViewLength(entry.key);
const valueLength = cfg::stringViewLength(entry.value);
libc::printf("Key %u: `%.*s` = `%.*s`\n", entryIndex, keyLength, entry.key.front, valueLength, entry.value.front);
entryIndex = entryIndex + 1;
}

index = index + 1;
}

cfg::closeConfigFile(file);
libc::exit(0);
}
2 changes: 1 addition & 1 deletion data/examples/config/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ run: $(MAIN)

ifeq ($(OS),Windows_NT)
$(MAIN): $(SOURCES)
$(CC) /nologo $(BUILDDIR)/module.c /TC /Fe:$(MAIN) /DEBUG:FULL /Zi /Fo:$(BUILDDIR)/main.obj /Fd:$(BUILDDIR)/
$(CC) /nologo $(BUILDDIR)/module.c /TC /Fe:$(MAIN) /DEBUG:FULL /Zi /Fo:$(BUILDDIR)/main.obj /Fd:$(BUILDDIR)/ /fsanitize=address
else
$(MAIN): $(SOURCES)
$(CC) $(BUILDDIR)/module.c -o$(BUILDDIR)/main -Wno-format-security -Wno-format-contains-nul
Expand Down
12 changes: 10 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ lang_pl0 = get_option('lang_pl0').disable_auto_if(meson.is_subproject())
lang_oberon = get_option('lang_oberon').disable_auto_if(meson.is_subproject())
lang_example = get_option('lang_example').disable_auto_if(meson.is_subproject())
lang_cc = get_option('lang_cc').disable_auto_if(meson.is_subproject())
lang_sql = get_option('lang_sql').disable_auto_if(meson.is_subproject())

# all the codegen targets
target_cfamily = get_option('target_cfamily').disable_auto_if(meson.is_subproject())
Expand All @@ -93,6 +94,11 @@ if unit_tests.enabled() and is_release and cc.get_id() == 'gcc'
warning('enabling unit tests in release mode with gcc will result in degraded performance')
endif

libkwargs = {
'install': not meson.is_subproject(),
'build_by_default': not meson.is_subproject()
}

# args for all code
all_args = cc.get_supported_arguments(
# we use flexible array members
Expand All @@ -106,7 +112,8 @@ all_args = cc.get_supported_arguments(
'-fstrict-aliasing',
'-fmerge-all-constants',

'-D_ITERATOR_DEBUG_LEVEL=0'
'-D_ITERATOR_DEBUG_LEVEL=0',
'-D_HAS_EXCEPTIONS=0'
)

has_time_trace = cc.has_argument('-ftime-trace')
Expand Down Expand Up @@ -358,7 +365,8 @@ build_summary = {
'PL/0': lang_pl0.allowed(),
'Oberon-2': lang_oberon.allowed(),
'Example': lang_example.allowed(),
'C': lang_cc.allowed()
'C': lang_cc.allowed(),
'SQL': lang_sql.allowed()
},
'Tools': {
'Notify': tool_notify.allowed(),
Expand Down
5 changes: 5 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ option('lang_cc', type : 'feature',
value : 'disabled'
)

option('lang_sql', type : 'feature',
description : 'build the sql language driver',
value : 'disabled'
)

# interfaces

option('frontend_cli', type : 'feature',
Expand Down
8 changes: 4 additions & 4 deletions src/common/arena/include/arena/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ CT_ARENA_API char *arena_strdup(
CT_NODISCARD CT_ALLOC(arena_free)
RET_NOTNULL
CT_ARENA_API char *arena_strndup(
IN_READS(len) const char *str,
STA_READS(len) const char *str,
size_t len,
IN_NOTNULL arena_t *arena);

Expand All @@ -218,7 +218,7 @@ CT_ARENA_API char *arena_strndup(
CT_NODISCARD CT_ALLOC(arena_free) CT_ALLOC_SIZE(2)
RET_NOTNULL
CT_ARENA_API void *arena_memdup(
IN_READS(size) const void *ptr,
STA_READS(size) const void *ptr,
IN_DOMAIN(>, 0) size_t size,
IN_NOTNULL arena_t *arena);

Expand Down Expand Up @@ -309,7 +309,7 @@ CT_ARENA_API char *arena_opt_strdup(
/// @return the allocated copy of the string
CT_NODISCARD CT_ALLOC(arena_free)
CT_ARENA_API char *arena_opt_strndup(
IN_READS(len) const char *str,
STA_READS(len) const char *str,
IN_DOMAIN(>, 0) size_t len,
IN_NOTNULL arena_t *arena);

Expand All @@ -325,7 +325,7 @@ CT_ARENA_API char *arena_opt_strndup(
/// @return the duplicated memory
CT_NODISCARD CT_ALLOC(arena_free) CT_ALLOC_SIZE(2)
CT_ARENA_API void *arena_opt_memdup(
IN_READS(size) const void *ptr,
STA_READS(size) const void *ptr,
IN_DOMAIN(>, 0) size_t size,
IN_NOTNULL arena_t *arena);

Expand Down
2 changes: 1 addition & 1 deletion src/common/base/include/base/bitset.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ typedef struct bitset_t
///
/// @return the new bitset
CT_NODISCARD
CT_BASE_API bitset_t bitset_of(IN_READS(words) void *data, size_t words);
CT_BASE_API bitset_t bitset_of(STA_READS(words) void *data, size_t words);

/// @brief scan for the next free bit and set it
///
Expand Down
26 changes: 13 additions & 13 deletions src/common/base/include/base/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CT_BEGIN_API
///
/// @retval true if the path is special
CT_NODISCARD CT_PUREFN
CT_BASE_API bool is_path_special(STA_IN_CSTRING const char *path);
CT_BASE_API bool is_path_special(IN_STRING const char *path);

/// @brief hash a pointer value
///
Expand All @@ -40,7 +40,7 @@ CT_BASE_API ctu_hash_t ctu_ptrhash(const void *ptr);
///
/// @return the hash
CT_NODISCARD CT_PUREFN
CT_BASE_API ctu_hash_t str_hash(STA_IN_CSTRING const char *str);
CT_BASE_API ctu_hash_t str_hash(IN_STRING const char *str);

/// @brief hash a string with a provided length
///
Expand Down Expand Up @@ -88,7 +88,7 @@ CT_BASE_API bool ctu_isalnum(int c);
///
/// @return if the strings are equal
CT_NODISCARD CT_PUREFN
CT_BASE_API bool str_equal(STA_IN_CSTRING const char *lhs, STA_IN_CSTRING const char *rhs);
CT_BASE_API bool str_equal(IN_STRING const char *lhs, IN_STRING const char *rhs);

/// @brief copy a string
/// equivalent to strcpy but with safety checks
Expand All @@ -100,7 +100,7 @@ CT_BASE_API bool str_equal(STA_IN_CSTRING const char *lhs, STA_IN_CSTRING const
/// @param size the size of @p dst
///
/// @return the destination
CT_BASE_API char *ctu_strcpy(STA_WRITES(size) char *dst, STA_IN_CSTRING const char *src, size_t size);
CT_BASE_API char *ctu_strcpy(STA_WRITES(size) char *dst, IN_STRING const char *src, size_t size);

/// @brief get the length of a string not including the null terminator
/// equivalent to strlen but with safety checks
Expand All @@ -111,7 +111,7 @@ CT_BASE_API char *ctu_strcpy(STA_WRITES(size) char *dst, STA_IN_CSTRING const ch
///
/// @return the length of the string
CT_NODISCARD CT_PUREFN
CT_BASE_API size_t ctu_strlen(STA_IN_CSTRING const char *str);
CT_BASE_API size_t ctu_strlen(IN_STRING const char *str);

/// @brief check if a string is empty
/// equivalent to strlen(str) == 0
Expand All @@ -123,7 +123,7 @@ CT_BASE_API size_t ctu_strlen(STA_IN_CSTRING const char *str);
/// @retval true if the string is empty
/// @retval false otherwise
CT_NODISCARD CT_PUREFN
CT_BASE_API bool ctu_string_empty(STA_IN_CSTRING const char *str);
CT_BASE_API bool ctu_string_empty(IN_STRING const char *str);

/// @brief compare two strings
/// equivalent to strncmp but with safety checks
Expand All @@ -136,7 +136,7 @@ CT_BASE_API bool ctu_string_empty(STA_IN_CSTRING const char *str);
///
/// @return the comparison result
CT_NODISCARD CT_PUREFN
CT_BASE_API int ctu_strncmp(STA_IN_CSTRING const char *lhs, STA_IN_CSTRING const char *rhs, size_t length);
CT_BASE_API int ctu_strncmp(IN_STRING const char *lhs, IN_STRING const char *rhs, size_t length);

/// @brief compare two strings
/// equivalent to strcmp but with safety checks
Expand All @@ -148,7 +148,7 @@ CT_BASE_API int ctu_strncmp(STA_IN_CSTRING const char *lhs, STA_IN_CSTRING const
///
/// @return the comparison result
CT_NODISCARD CT_PUREFN
CT_BASE_API int ctu_strcmp(STA_IN_CSTRING const char *lhs, STA_IN_CSTRING const char *rhs);
CT_BASE_API int ctu_strcmp(IN_STRING const char *lhs, IN_STRING const char *rhs);

/// @brief copy memory from one location to another
/// equivalent to memcpy but with safety checks
Expand Down Expand Up @@ -196,7 +196,7 @@ CT_BASE_API void ctu_memset(STA_WRITES(size) void *dst, int value, size_t size);
///
/// @return the position of @p needle in @p haystack or null if not found
CT_NODISCARD CT_PUREFN
CT_BASE_API char *ctu_strstr(STA_IN_CSTRING const char *haystack, STA_IN_CSTRING const char *needle);
CT_BASE_API char *ctu_strstr(IN_STRING const char *haystack, IN_STRING const char *needle);

// text api

Expand All @@ -208,7 +208,7 @@ CT_BASE_API char *ctu_strstr(STA_IN_CSTRING const char *haystack, STA_IN_CSTRING
///
/// @return the text object
CT_CONSTFN
CT_BASE_API text_t text_make(STA_NOTNULL char *text, size_t length);
CT_BASE_API text_t text_make(STA_READS(length) char *text, size_t length);

/// @brief create a new owning text array
/// this is a shortcut for
Expand All @@ -220,7 +220,7 @@ CT_BASE_API text_t text_make(STA_NOTNULL char *text, size_t length);
///
/// @return the text object
CT_PUREFN
CT_BASE_API text_t text_from(STA_IN_CSTRING char *text);
CT_BASE_API text_t text_from(IN_STRING char *text);

/// @brief create a new non-owning text array
/// @p text must be at least @p length bytes long
Expand All @@ -230,7 +230,7 @@ CT_BASE_API text_t text_from(STA_IN_CSTRING char *text);
///
/// @return the text object
CT_CONSTFN
CT_BASE_API text_view_t text_view_make(STA_NOTNULL const char *text, size_t length);
CT_BASE_API text_view_t text_view_make(STA_READS(length) const char *text, size_t length);

/// @brief create a new non-owning text array
/// this is a shortcut for
Expand All @@ -242,7 +242,7 @@ CT_BASE_API text_view_t text_view_make(STA_NOTNULL const char *text, size_t leng
///
/// @return the text object
CT_PUREFN
CT_BASE_API text_view_t text_view_from(STA_IN_CSTRING const char *text);
CT_BASE_API text_view_t text_view_from(IN_STRING const char *text);

/// @brief check if two text objects are equal
///
Expand Down
Loading
Loading