Page Menu
Home
Search
Configure Global Search
Log In
Paste
P3160
D15491, support for pre-processor indentation
Active
Public
Actions
Authored by
Campbell Barton (campbellbarton)
on Aug 31 2022, 11:21 AM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Award Token
Tags
None
Subscribers
None
diff --git a/source/blender/makesdna/intern/makesdna.c b/source/blender/makesdna/intern/makesdna.c
index fb5778a6527..36abe970b31 100644
--- a/source/blender/makesdna/intern/makesdna.c
+++ b/source/blender/makesdna/intern/makesdna.c
@@ -506,8 +506,9 @@ static short *add_struct(int namecode)
return sp;
}
-/* Copied from string.c to avoid complicating the compilation process of makesdna. */
-static bool BLI_str_startswith(const char *__restrict str, const char *__restrict start)
+/* Copied from `BLI_str_startswith` string.c
+ * to avoid complicating the compilation process of makesdna. */
+static bool str_startswith(const char *__restrict str, const char *__restrict start)
{
for (; *str && *start; str++, start++) {
if (*str != *start) {
@@ -518,6 +519,41 @@ static bool BLI_str_startswith(const char *__restrict str, const char *__restric
return (*start == '\0');
}
+/**
+ * Check if `str` is a preprocessor string that starts with `start`.
+ * The `start` doesn't need the `#` prefix.
+ * `ifdef VALUE` will match `#ifdef VALUE` as well as `# ifdef VALUE`.
+ */
+static bool match_preproc_prefix(const char *__restrict str, const char *__restrict start)
+{
+ if (*str != '#') {
+ return false;
+ }
+ str++;
+ while (*str == ' ') {
+ str++;
+ }
+ return str_startswith(str, start);
+}
+
+/**
+ * \return The point in `str` that starts with `start` or NULL when not found.
+ *
+ */
+static char *match_preproc_strstr(char *__restrict str, const char *__restrict start)
+{
+ while ((str = strchr(str, '#'))) {
+ str++;
+ while (*str == ' ') {
+ str++;
+ }
+ if (str_startswith(str, start)) {
+ return str;
+ }
+ }
+ return NULL;
+}
+
static int preprocess_include(char *maindata, const int maindata_len)
{
/* NOTE: len + 1, last character is a dummy to prevent
@@ -545,8 +581,9 @@ static int preprocess_include(char *maindata, const int maindata_len)
cp++;
}
- const char *cpp_block_start = "#ifdef __cplusplus";
- const char *cpp_block_end = "#endif";
+ /* No need for leading '#' character. */
+ const char *cpp_block_start = "ifdef __cplusplus";
+ const char *cpp_block_end = "endif";
/* data from temp copy to maindata, remove comments and double spaces */
cp = temp;
@@ -592,8 +629,9 @@ static int preprocess_include(char *maindata, const int maindata_len)
skip_until_closing_brace = false;
}
}
- else if (BLI_str_startswith(cp, cpp_block_start)) {
- char *end_ptr = strstr(cp, cpp_block_end);
+ else if (match_preproc_prefix(cp, cpp_block_start)) {
+ char *end_ptr = match_preproc_strstr(cp, cpp_block_end);
+
if (end_ptr == NULL) {
fprintf(stderr, "Error: '%s' block must end with '%s'\n", cpp_block_start, cpp_block_end);
}
Event Timeline
Campbell Barton (campbellbarton)
created this paste.
Aug 31 2022, 11:21 AM
Campbell Barton (campbellbarton)
mentioned this in
D15491: Nodes: Move NodeTreeRef functionality into node runtime data.
.
Aug 31 2022, 11:30 AM
Log In to Comment