Skip to content

Commit f967350

Browse files
committed
Fix raw string literals handling in dep. iterator
1 parent 1967c47 commit f967350

7 files changed

Lines changed: 44 additions & 26 deletions

File tree

code/cbuild_api.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ const char * get_argument_or_default (const Arguments *args, const char *key, co
2727
}
2828

2929
bool find_toolchain_by_type (Project *project, Toolchain_Type type, Toolchain_Configuration *out_configuration) {
30-
auto result = lookup_toolchain_by_type(&project->arena, type);
31-
if (not result) return false;
30+
auto [status, result] = lookup_toolchain_by_type(&project->arena, type);
31+
check_status(status);
3232

33-
*out_configuration = *result;
33+
*out_configuration = result;
3434

3535
return true;
3636
}
@@ -51,8 +51,8 @@ void overwrite_toolchain (Project *project, Toolchain_Configuration toolchain) {
5151
}
5252

5353
void set_toolchain (Project *project, Toolchain_Type type) {
54-
auto result = lookup_toolchain_by_type(&project->arena, type);
55-
if (not result) {
54+
auto [status, result] = lookup_toolchain_by_type(&project->arena, type);
55+
if (!status) {
5656
print(&project->arena, "FATAL ERROR: Requested toolchain wasn't found on the system.\n");
5757
exit(EXIT_FAILURE);
5858
}

code/dependency_iterator.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "core.hpp"
66
#include "dependency_iterator.hpp"
77
#include "platform.hpp"
8+
#include "runtime.hpp"
9+
#include "result.hpp"
810

911
Dependency_Iterator::Dependency_Iterator (const File_Mapping *_mapping)
1012
: mapping { _mapping },
@@ -37,7 +39,9 @@ static const char* find_substring (const char *memory, size_t memory_size, const
3739
return nullptr;
3840
}
3941

40-
bool get_next_include_value (Dependency_Iterator *iterator, String *include) {
42+
Result<bool> get_next_include_value (Dependency_Iterator *iterator, String *include) {
43+
use(Status_Code);
44+
4145
auto cursor = iterator->cursor;
4246
auto end = iterator->mapping->memory + iterator->mapping->size;
4347

@@ -82,14 +86,21 @@ bool get_next_include_value (Dependency_Iterator *iterator, String *include) {
8286
auto end_of_safe_word = reinterpret_cast<const char *>(memchr(safe_word, '(', end - safe_word));
8387
if (end_of_safe_word == nullptr) return false;
8488

85-
auto safe_word_length = end_of_safe_word - safe_word;
86-
auto raw_string_end_position = find_substring(end_of_safe_word, end - end_of_safe_word, safe_word, safe_word_length);
87-
assert(*(raw_string_end_position - 1) == ')');
89+
auto safe_word_length = end_of_safe_word - safe_word;
90+
char raw_string_closing_path[64] = { ')' };
91+
copy_memory(raw_string_closing_path + 1, safe_word, safe_word_length);
92+
93+
auto raw_string_closing_path_length = safe_word_length + 1;
94+
auto raw_string_end_position = find_substring(end_of_safe_word, end - end_of_safe_word,
95+
raw_string_closing_path, raw_string_closing_path_length);
96+
if ((raw_string_end_position == nullptr) ||
97+
(raw_string_end_position[raw_string_closing_path_length] != '"')) {
98+
return Invalid_Value; // Unclosed string literal, TODO: report a proper error message
99+
}
88100

89-
auto position = raw_string_end_position + safe_word_length;
90-
assert(*position == '"');
101+
auto position = raw_string_end_position + safe_word_length + 1;
91102

92-
cursor = position + 1;
103+
cursor = position + 1; // closing " for a string literal
93104

94105
continue;
95106
}

code/dependency_iterator.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
struct String;
77
struct File_Mapping;
88

9+
template <typename T> struct Result;
10+
911
struct Dependency_Iterator {
1012
const File_Mapping *mapping;
1113
const char *cursor = nullptr;
@@ -17,4 +19,4 @@ struct Dependency_Iterator {
1719
Iterates over all user-defined #include directives in the mapped source file retrieving the provided value as-is.
1820
Resolution of the retrieved file path is left for the caller.
1921
*/
20-
bool get_next_include_value (Dependency_Iterator *iterator, String *include_value);
22+
Result<bool> get_next_include_value (Dependency_Iterator *iterator, String *include_value);

code/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ int main (int argc, char **argv) {
148148
print_usage(&arena);
149149
}
150150
else {
151-
auto default_toolchain = discover_toolchain(&arena);
152-
if (not default_toolchain) {
151+
auto [status, default_toolchain] = discover_toolchain(&arena);
152+
if (!status) {
153153
print(&arena, "Couldn't find any suitable C/C++ toolchain installed on the system.");
154154
return EXIT_FAILURE;
155155
}

code/result.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,6 @@ struct Result {
8585
bool operator == (Status_Code::Value code) {
8686
return status.value == code;
8787
}
88-
89-
bool operator ! () {
90-
if constexpr (has_not_operator_v<T>) {
91-
return (status != Status_Code::Success) || !this->value;
92-
}
93-
else return (status != Status_Code::Success);
94-
}
9588
};
9689

9790
template <typename T> struct Is_Result: std::false_type {};

code/target_builder.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,13 @@ static Result<Chain_Status> scan_dependency_chains (Memory_Arena *arena, File *s
458458
};
459459

460460
bool chain_has_updates = false;
461-
while (get_next_include_value(&iterator, &include_value)) {
461+
while (true) {
462+
{
463+
auto [status, has_more] = get_next_include_value(&iterator, &include_value);
464+
check_status(status);
465+
if (!has_more) break;
466+
}
467+
462468
File_Path resolved_path;
463469
for (auto prefix: include_directories) {
464470
auto full_path = make_file_path(arena, prefix, include_value);
@@ -539,7 +545,13 @@ static Result<bool> scan_file_dependencies (Memory_Arena *_arena, File *source_f
539545
};
540546

541547
bool chain_has_updates = false;
542-
while (get_next_include_value(&iterator, &include_value)) {
548+
while (true) {
549+
{
550+
auto [status, has_more] = get_next_include_value(&iterator, &include_value);
551+
check_status(status);
552+
if (!has_more) break;
553+
}
554+
543555
auto inner_local = local;
544556

545557
File_Path resolved_path;

code/toolchain_win32.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,10 @@ List<Pair<String, String>> setup_system_sdk (Memory_Arena *arena, Target_Arch ar
347347
add(&local, &existing_environment_values, { "LIB", lib_env_var });
348348

349349
auto sdk = find_windows_sdk(&local);
350-
if (!sdk) return {};
350+
if (!sdk.status) return {};
351351

352352
auto msvc = get_msvc_installation_path(&local);
353-
if (!msvc) return {};
353+
if (!msvc.status) return {};
354354

355355
{
356356
auto sub_local = local;

0 commit comments

Comments
 (0)