@@ -61,6 +61,13 @@ struct db {
6161
6262static 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 */
6673static 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
291312static 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