Skip to content

Commit 0697944

Browse files
committed
Add amast-pragma: verbatim-include-std-on(off) support
1 parent 3463f2a commit 0697944

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

libs/pal/posix/pal.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@
3030
* Platform abstraction layer (PAL) API posix implementation
3131
*/
3232

33-
#define _GNU_SOURCE
34-
3533
#include <stdarg.h> /* IWYU pragma: keep */
3634
#include <stdbool.h>
35+
36+
/* amast-pragma: verbatim-include-std-on */
37+
38+
#define _GNU_SOURCE
39+
3740
#include <stdio.h>
3841
#include <stdint.h>
3942

@@ -46,6 +49,8 @@
4649
#include <features.h>
4750
#include <unistd.h>
4851

52+
/* amast-pragma: verbatim-include-std-off */
53+
4954
#include "common/compiler.h"
5055
#include "common/macros.h"
5156
#include "pal/pal.h"

tools/unity/unity.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ struct db {
6161

6262
static struct db m_db = {.src.len = 0, .hdr.len = 0};
6363

64+
static bool is_pragma(const char *str, const char *pragma) {
65+
if (NULL == strstr(str, "amast-pragma")) {
66+
return false;
67+
}
68+
return strstr(str, pragma) != NULL;
69+
}
70+
6471
/* check if the include already exists in the array */
6572
/* cppcheck-suppress-begin constParameter */
6673
static bool include_is_unique(
@@ -86,10 +93,14 @@ static void include_add_unique(
8693
}
8794

8895
/* process a line and detect #include directives */
89-
static void process_content(struct files *db, const char *ln) {
96+
static void process_content(
97+
struct files *db, const char *ln, bool verbatim_include_std
98+
) {
9099
char inc_file[PATH_MAX + 1];
91100
#define AM_LIM AM_STRINGIFY(PATH_MAX)
92-
if (sscanf(ln, "#include <%" AM_LIM "[^>]>%*s", inc_file) == 1) {
101+
if (verbatim_include_std) {
102+
str_lcpy(db->includes_std[db->includes_std_num++], ln, PATH_MAX);
103+
} else if (sscanf(ln, "#include <%" AM_LIM "[^>]>%*s", inc_file) == 1) {
93104
include_add_unique(db->includes_std, &db->includes_std_num, inc_file);
94105
} else if (sscanf(ln, "#include \"%" AM_LIM "[^\"]\"%*s", inc_file) == 1) {
95106
/* ignore user includes */;
@@ -107,8 +118,18 @@ static void read_file(struct files *db, const char *fname) {
107118
}
108119

109120
char line[1024];
121+
bool verbatim_include_std = false;
110122
while (fgets(line, sizeof(line), file)) {
111-
process_content(db, line);
123+
if (verbatim_include_std) {
124+
if (is_pragma(line, "verbatim-include-std-off")) {
125+
verbatim_include_std = false;
126+
continue;
127+
}
128+
} else if (is_pragma(line, "verbatim-include-std-on")) {
129+
verbatim_include_std = true;
130+
continue;
131+
}
132+
process_content(db, line, verbatim_include_std);
112133
}
113134

114135
fclose(file);
@@ -239,7 +260,7 @@ static void file_append(
239260
char (*tests)[PATH_MAX]
240261
) {
241262
/*
242-
* There must only be one main() in the resulting file.
263+
* There must be only one main() in the resulting file.
243264
* So, replace main() with a unique function name
244265
*/
245266
const char *main_fn = "int main(void) {";
@@ -290,7 +311,15 @@ static void add_amast_description(
290311

291312
static void add_amast_includes_std(FILE *f, const struct files *db) {
292313
for (int i = 0; i < db->includes_std_num; ++i) {
293-
fprintf(f, "#include <%s>\n", db->includes_std[i]);
314+
if (strstr(db->includes_std[i], "#include") ||
315+
/* verbatim inclusion */
316+
strstr(db->includes_std[i], "#define")) {
317+
fprintf(f, "%s", db->includes_std[i]);
318+
} else if (db->includes_std[i][0] == '\n') {
319+
fprintf(f, "\n");
320+
} else {
321+
fprintf(f, "#include <%s>\n", db->includes_std[i]);
322+
}
294323
}
295324
fprintf(f, "\n");
296325
}

0 commit comments

Comments
 (0)