From a6cee7f52982f0bd409602a34a42d2416f371047 Mon Sep 17 00:00:00 2001 From: Scott Duensing Date: Sun, 18 Feb 2024 18:20:16 -0600 Subject: [PATCH] Comments should no longer be able to break overlay parser. --- examples/overlay/overlay.c | 5 +++++ tools/overlay/src/overlay.c | 23 ++++++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/examples/overlay/overlay.c b/examples/overlay/overlay.c index 66d5276..96e2dd9 100644 --- a/examples/overlay/overlay.c +++ b/examples/overlay/overlay.c @@ -23,6 +23,11 @@ #define SEGMENT_TEST +/* + */ + /* + Breaking things { with comments! + */ void test(int arg1, int arg2) { } diff --git a/tools/overlay/src/overlay.c b/tools/overlay/src/overlay.c index 42c1e25..ee728f6 100644 --- a/tools/overlay/src/overlay.c +++ b/tools/overlay/src/overlay.c @@ -83,6 +83,8 @@ void parseCFile(char *filename, char *targetFile, FILE *trampoline, char *trampo char *b; bool found; int x; + int comments = 0; + bool inComment = false; int brackets = 0; int crSinceStart = 0; int pos = 0; @@ -93,7 +95,6 @@ void parseCFile(char *filename, char *targetFile, FILE *trampoline, char *trampo * * - It only handles C, not C++. * - It can only handle single-line function definitions with the opening '{' on the same line as the function. - * - It can be borked by comments. * - It always generates trampolines, not just when they're needed. * * Someone should fix it. :-) @@ -124,16 +125,28 @@ void parseCFile(char *filename, char *targetFile, FILE *trampoline, char *trampo // Read next byte from C input file. if ((c = fgetc(in)) == EOF) break; + // Look for '//' comments. + if ((c == '/') && (pos > 0) && (buffer[pos-1] == '/')) inComment = true; + + // Look for '/*' comment start. + if ((c == '*') && (pos > 0) && (buffer[pos-1] == '/')) comments++; + + // Look for '*/' comment end. + if ((c == '/') && (pos > 0) && (buffer[pos-1] == '*')) comments--; + // Count brackets so we know if we're inside a function or not. - if (c == '{') { - brackets++; - crSinceStart = 0; + if ((!inComment) && (comments == 0)) { + if (c == '{') { + brackets++; + crSinceStart = 0; + } + if (c == '}') brackets--; } - if (c == '}') brackets--; // End of line? if ((c == 13) || (c == 10)) { + inComment = false; crSinceStart++; // End the line and trim the tail.