| 1 |
/* do not edit automatically generated by mc from mcComment. */ |
| 2 |
/* mcComment.mod provides a module to remember the comments. |
| 3 |
|
| 4 |
Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 5 |
Contributed by Gaius Mulley <[email protected]>. |
| 6 |
|
| 7 |
This file is part of GNU Modula-2. |
| 8 |
|
| 9 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 3, or (at your option) |
| 12 |
any later version. |
| 13 |
|
| 14 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 15 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License |
| 20 |
along with GNU Modula-2; see the file COPYING3. If not see |
| 21 |
<http://www.gnu.org/licenses/>. */ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
#include "system.h" |
| 25 |
#include <stdbool.h> |
| 26 |
# if !defined (PROC_D) |
| 27 |
# define PROC_D |
| 28 |
typedef void (*PROC_t) (void); |
| 29 |
typedef struct { PROC_t proc; } PROC; |
| 30 |
# endif |
| 31 |
|
| 32 |
# if !defined (TRUE) |
| 33 |
# define TRUE (1==1) |
| 34 |
# endif |
| 35 |
|
| 36 |
# if !defined (FALSE) |
| 37 |
# define FALSE (1==0) |
| 38 |
# endif |
| 39 |
|
| 40 |
# include "GStorage.h" |
| 41 |
# include "Gmcrts.h" |
| 42 |
#if defined(__cplusplus) |
| 43 |
# undef NULL |
| 44 |
# define NULL 0 |
| 45 |
#endif |
| 46 |
#define _mcComment_C |
| 47 |
|
| 48 |
#include "GmcComment.h" |
| 49 |
# include "GDynamicStrings.h" |
| 50 |
# include "GStorage.h" |
| 51 |
# include "GnameKey.h" |
| 52 |
# include "GmcDebug.h" |
| 53 |
# include "GASCII.h" |
| 54 |
# include "Glibc.h" |
| 55 |
|
| 56 |
typedef struct mcComment__T1_r mcComment__T1; |
| 57 |
|
| 58 |
typedef enum {mcComment_unknown, mcComment_procedureHeading, mcComment_inBody, mcComment_afterStatement} mcComment_commentType; |
| 59 |
|
| 60 |
typedef mcComment__T1 *mcComment_commentDesc__opaque; |
| 61 |
|
| 62 |
struct mcComment__T1_r { |
| 63 |
mcComment_commentType type; |
| 64 |
DynamicStrings_String content; |
| 65 |
nameKey_Name procName; |
| 66 |
bool used; |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
/* |
| 71 |
initComment - the start of a new comment has been seen by the lexical analyser. |
| 72 |
A new comment block is created and all addText contents are placed |
| 73 |
in this block. onlySpaces indicates whether we have only seen |
| 74 |
spaces on this line. |
| 75 |
*/ |
| 76 |
|
| 77 |
extern "C" mcComment_commentDesc mcComment_initComment (bool onlySpaces); |
| 78 |
|
| 79 |
/* |
| 80 |
addText - cs is a C string (null terminated) which contains comment text. |
| 81 |
This is appended to the comment, cd. |
| 82 |
*/ |
| 83 |
|
| 84 |
extern "C" void mcComment_addText (mcComment_commentDesc cd, void * cs); |
| 85 |
|
| 86 |
/* |
| 87 |
getContent - returns the content of comment, cd. |
| 88 |
*/ |
| 89 |
|
| 90 |
extern "C" DynamicStrings_String mcComment_getContent (mcComment_commentDesc cd); |
| 91 |
|
| 92 |
/* |
| 93 |
getCommentCharStar - returns the C string content of comment, cd. |
| 94 |
*/ |
| 95 |
|
| 96 |
extern "C" void * mcComment_getCommentCharStar (mcComment_commentDesc cd); |
| 97 |
|
| 98 |
/* |
| 99 |
setProcedureComment - changes the type of comment, cd, to a |
| 100 |
procedure heading comment, |
| 101 |
providing it has the procname as the first word. |
| 102 |
*/ |
| 103 |
|
| 104 |
extern "C" void mcComment_setProcedureComment (mcComment_commentDesc cd, nameKey_Name procname); |
| 105 |
|
| 106 |
/* |
| 107 |
getProcedureComment - returns the current procedure comment if available. |
| 108 |
*/ |
| 109 |
|
| 110 |
extern "C" DynamicStrings_String mcComment_getProcedureComment (mcComment_commentDesc cd); |
| 111 |
|
| 112 |
/* |
| 113 |
getAfterStatementComment - returns the current statement after comment if available. |
| 114 |
*/ |
| 115 |
|
| 116 |
extern "C" DynamicStrings_String mcComment_getAfterStatementComment (mcComment_commentDesc cd); |
| 117 |
|
| 118 |
/* |
| 119 |
getInbodyStatementComment - returns the current statement after comment if available. |
| 120 |
*/ |
| 121 |
|
| 122 |
extern "C" DynamicStrings_String mcComment_getInbodyStatementComment (mcComment_commentDesc cd); |
| 123 |
|
| 124 |
/* |
| 125 |
isProcedureComment - returns TRUE if, cd, is a procedure comment. |
| 126 |
*/ |
| 127 |
|
| 128 |
extern "C" bool mcComment_isProcedureComment (mcComment_commentDesc cd); |
| 129 |
|
| 130 |
/* |
| 131 |
isBodyComment - returns TRUE if, cd, is a body comment. |
| 132 |
*/ |
| 133 |
|
| 134 |
extern "C" bool mcComment_isBodyComment (mcComment_commentDesc cd); |
| 135 |
|
| 136 |
/* |
| 137 |
isAfterComment - returns TRUE if, cd, is an after comment. |
| 138 |
*/ |
| 139 |
|
| 140 |
extern "C" bool mcComment_isAfterComment (mcComment_commentDesc cd); |
| 141 |
|
| 142 |
/* |
| 143 |
Min - returns the lower of, a, and, b. |
| 144 |
*/ |
| 145 |
|
| 146 |
static unsigned int Min (unsigned int a, unsigned int b); |
| 147 |
|
| 148 |
/* |
| 149 |
RemoveNewlines - |
| 150 |
*/ |
| 151 |
|
| 152 |
static DynamicStrings_String RemoveNewlines (DynamicStrings_String s); |
| 153 |
|
| 154 |
/* |
| 155 |
seenProcedure - returns TRUE if the name, procName, appears as the first word |
| 156 |
in the comment. |
| 157 |
*/ |
| 158 |
|
| 159 |
static bool seenProcedure (mcComment_commentDesc__opaque cd, nameKey_Name procName); |
| 160 |
|
| 161 |
/* |
| 162 |
dumpComment - |
| 163 |
*/ |
| 164 |
|
| 165 |
static void dumpComment (mcComment_commentDesc__opaque cd); |
| 166 |
|
| 167 |
|
| 168 |
/* |
| 169 |
Min - returns the lower of, a, and, b. |
| 170 |
*/ |
| 171 |
|
| 172 |
static unsigned int Min (unsigned int a, unsigned int b) |
| 173 |
{ |
| 174 |
if (a < b) |
| 175 |
{ |
| 176 |
return a; |
| 177 |
} |
| 178 |
else |
| 179 |
{ |
| 180 |
return b; |
| 181 |
} |
| 182 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 183 |
__builtin_unreachable (); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
/* |
| 188 |
RemoveNewlines - |
| 189 |
*/ |
| 190 |
|
| 191 |
static DynamicStrings_String RemoveNewlines (DynamicStrings_String s) |
| 192 |
{ |
| 193 |
while ((DynamicStrings_Length (s)) > 0) |
| 194 |
{ |
| 195 |
if ((DynamicStrings_char (s, 0)) == ASCII_nl) |
| 196 |
{ |
| 197 |
s = DynamicStrings_RemoveWhitePrefix (DynamicStrings_Slice (s, 1, 0)); |
| 198 |
} |
| 199 |
else |
| 200 |
{ |
| 201 |
return DynamicStrings_RemoveWhitePrefix (s); |
| 202 |
} |
| 203 |
} |
| 204 |
return s; |
| 205 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 206 |
__builtin_unreachable (); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
/* |
| 211 |
seenProcedure - returns TRUE if the name, procName, appears as the first word |
| 212 |
in the comment. |
| 213 |
*/ |
| 214 |
|
| 215 |
static bool seenProcedure (mcComment_commentDesc__opaque cd, nameKey_Name procName) |
| 216 |
{ |
| 217 |
DynamicStrings_String s; |
| 218 |
void * a; |
| 219 |
unsigned int i; |
| 220 |
unsigned int h; |
| 221 |
bool res; |
| 222 |
|
| 223 |
a = nameKey_keyToCharStar (procName); |
| 224 |
s = RemoveNewlines (cd->content); |
| 225 |
s = DynamicStrings_Slice (DynamicStrings_Mark (s), 0, static_cast<int> (Min (DynamicStrings_Length (s), nameKey_lengthKey (procName)))); |
| 226 |
res = DynamicStrings_EqualCharStar (s, a); |
| 227 |
s = DynamicStrings_KillString (s); |
| 228 |
return res; |
| 229 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 230 |
__builtin_unreachable (); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
/* |
| 235 |
dumpComment - |
| 236 |
*/ |
| 237 |
|
| 238 |
static void dumpComment (mcComment_commentDesc__opaque cd) |
| 239 |
{ |
| 240 |
libc_printf ((const char *) "comment : ", 10); |
| 241 |
switch (cd->type) |
| 242 |
{ |
| 243 |
case mcComment_unknown: |
| 244 |
libc_printf ((const char *) "unknown", 7); |
| 245 |
break; |
| 246 |
|
| 247 |
case mcComment_procedureHeading: |
| 248 |
libc_printf ((const char *) "procedureheading", 16); |
| 249 |
break; |
| 250 |
|
| 251 |
case mcComment_inBody: |
| 252 |
libc_printf ((const char *) "inbody", 6); |
| 253 |
break; |
| 254 |
|
| 255 |
case mcComment_afterStatement: |
| 256 |
libc_printf ((const char *) "afterstatement", 14); |
| 257 |
break; |
| 258 |
|
| 259 |
|
| 260 |
default: |
| 261 |
CaseException ("../../gcc/m2/mc/mcComment.def", 20, 1); |
| 262 |
__builtin_unreachable (); |
| 263 |
} |
| 264 |
if (cd->used) |
| 265 |
{ |
| 266 |
libc_printf ((const char *) " used", 5); |
| 267 |
} |
| 268 |
else |
| 269 |
{ |
| 270 |
libc_printf ((const char *) " unused", 7); |
| 271 |
} |
| 272 |
libc_printf ((const char *) " contents = %s\\n", 16, DynamicStrings_string (cd->content)); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
/* |
| 277 |
initComment - the start of a new comment has been seen by the lexical analyser. |
| 278 |
A new comment block is created and all addText contents are placed |
| 279 |
in this block. onlySpaces indicates whether we have only seen |
| 280 |
spaces on this line. |
| 281 |
*/ |
| 282 |
|
| 283 |
extern "C" mcComment_commentDesc mcComment_initComment (bool onlySpaces) |
| 284 |
{ |
| 285 |
mcComment_commentDesc__opaque cd; |
| 286 |
|
| 287 |
Storage_ALLOCATE ((void **) &cd, sizeof (mcComment__T1)); |
| 288 |
mcDebug_assert (cd != NULL); |
| 289 |
if (onlySpaces) |
| 290 |
{ |
| 291 |
cd->type = mcComment_inBody; |
| 292 |
} |
| 293 |
else |
| 294 |
{ |
| 295 |
cd->type = mcComment_afterStatement; |
| 296 |
} |
| 297 |
cd->content = DynamicStrings_InitString ((const char *) "", 0); |
| 298 |
cd->procName = nameKey_NulName; |
| 299 |
cd->used = false; |
| 300 |
return static_cast<mcComment_commentDesc> (cd); |
| 301 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 302 |
__builtin_unreachable (); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
/* |
| 307 |
addText - cs is a C string (null terminated) which contains comment text. |
| 308 |
This is appended to the comment, cd. |
| 309 |
*/ |
| 310 |
|
| 311 |
extern "C" void mcComment_addText (mcComment_commentDesc cd, void * cs) |
| 312 |
{ |
| 313 |
if (cd != NULL) |
| 314 |
{ |
| 315 |
static_cast<mcComment_commentDesc__opaque> (cd)->content = DynamicStrings_ConCat (static_cast<mcComment_commentDesc__opaque> (cd)->content, DynamicStrings_InitStringCharStar (cs)); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
/* |
| 321 |
getContent - returns the content of comment, cd. |
| 322 |
*/ |
| 323 |
|
| 324 |
extern "C" DynamicStrings_String mcComment_getContent (mcComment_commentDesc cd) |
| 325 |
{ |
| 326 |
if (cd != NULL) |
| 327 |
{ |
| 328 |
return static_cast<mcComment_commentDesc__opaque> (cd)->content; |
| 329 |
} |
| 330 |
return static_cast<DynamicStrings_String> (NULL); |
| 331 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 332 |
__builtin_unreachable (); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
/* |
| 337 |
getCommentCharStar - returns the C string content of comment, cd. |
| 338 |
*/ |
| 339 |
|
| 340 |
extern "C" void * mcComment_getCommentCharStar (mcComment_commentDesc cd) |
| 341 |
{ |
| 342 |
DynamicStrings_String s; |
| 343 |
|
| 344 |
s = mcComment_getContent (cd); |
| 345 |
if (s == NULL) |
| 346 |
{ |
| 347 |
return NULL; |
| 348 |
} |
| 349 |
else |
| 350 |
{ |
| 351 |
return DynamicStrings_string (s); |
| 352 |
} |
| 353 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 354 |
__builtin_unreachable (); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
/* |
| 359 |
setProcedureComment - changes the type of comment, cd, to a |
| 360 |
procedure heading comment, |
| 361 |
providing it has the procname as the first word. |
| 362 |
*/ |
| 363 |
|
| 364 |
extern "C" void mcComment_setProcedureComment (mcComment_commentDesc cd, nameKey_Name procname) |
| 365 |
{ |
| 366 |
if (cd != NULL) |
| 367 |
{ |
| 368 |
if (seenProcedure (static_cast<mcComment_commentDesc__opaque> (cd), procname)) |
| 369 |
{ |
| 370 |
static_cast<mcComment_commentDesc__opaque> (cd)->type = mcComment_procedureHeading; |
| 371 |
static_cast<mcComment_commentDesc__opaque> (cd)->procName = procname; |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
/* |
| 378 |
getProcedureComment - returns the current procedure comment if available. |
| 379 |
*/ |
| 380 |
|
| 381 |
extern "C" DynamicStrings_String mcComment_getProcedureComment (mcComment_commentDesc cd) |
| 382 |
{ |
| 383 |
if ((static_cast<mcComment_commentDesc__opaque> (cd)->type == mcComment_procedureHeading) && ! static_cast<mcComment_commentDesc__opaque> (cd)->used) |
| 384 |
{ |
| 385 |
static_cast<mcComment_commentDesc__opaque> (cd)->used = true; |
| 386 |
return static_cast<mcComment_commentDesc__opaque> (cd)->content; |
| 387 |
} |
| 388 |
return static_cast<DynamicStrings_String> (NULL); |
| 389 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 390 |
__builtin_unreachable (); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
/* |
| 395 |
getAfterStatementComment - returns the current statement after comment if available. |
| 396 |
*/ |
| 397 |
|
| 398 |
extern "C" DynamicStrings_String mcComment_getAfterStatementComment (mcComment_commentDesc cd) |
| 399 |
{ |
| 400 |
if ((static_cast<mcComment_commentDesc__opaque> (cd)->type == mcComment_afterStatement) && ! static_cast<mcComment_commentDesc__opaque> (cd)->used) |
| 401 |
{ |
| 402 |
static_cast<mcComment_commentDesc__opaque> (cd)->used = true; |
| 403 |
return static_cast<mcComment_commentDesc__opaque> (cd)->content; |
| 404 |
} |
| 405 |
return static_cast<DynamicStrings_String> (NULL); |
| 406 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 407 |
__builtin_unreachable (); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
/* |
| 412 |
getInbodyStatementComment - returns the current statement after comment if available. |
| 413 |
*/ |
| 414 |
|
| 415 |
extern "C" DynamicStrings_String mcComment_getInbodyStatementComment (mcComment_commentDesc cd) |
| 416 |
{ |
| 417 |
if ((static_cast<mcComment_commentDesc__opaque> (cd)->type == mcComment_inBody) && ! static_cast<mcComment_commentDesc__opaque> (cd)->used) |
| 418 |
{ |
| 419 |
static_cast<mcComment_commentDesc__opaque> (cd)->used = true; |
| 420 |
return static_cast<mcComment_commentDesc__opaque> (cd)->content; |
| 421 |
} |
| 422 |
return static_cast<DynamicStrings_String> (NULL); |
| 423 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 424 |
__builtin_unreachable (); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
/* |
| 429 |
isProcedureComment - returns TRUE if, cd, is a procedure comment. |
| 430 |
*/ |
| 431 |
|
| 432 |
extern "C" bool mcComment_isProcedureComment (mcComment_commentDesc cd) |
| 433 |
{ |
| 434 |
return (cd != NULL) && (static_cast<mcComment_commentDesc__opaque> (cd)->type == mcComment_procedureHeading); |
| 435 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 436 |
__builtin_unreachable (); |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
/* |
| 441 |
isBodyComment - returns TRUE if, cd, is a body comment. |
| 442 |
*/ |
| 443 |
|
| 444 |
extern "C" bool mcComment_isBodyComment (mcComment_commentDesc cd) |
| 445 |
{ |
| 446 |
return (cd != NULL) && (static_cast<mcComment_commentDesc__opaque> (cd)->type == mcComment_inBody); |
| 447 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 448 |
__builtin_unreachable (); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
/* |
| 453 |
isAfterComment - returns TRUE if, cd, is an after comment. |
| 454 |
*/ |
| 455 |
|
| 456 |
extern "C" bool mcComment_isAfterComment (mcComment_commentDesc cd) |
| 457 |
{ |
| 458 |
return (cd != NULL) && (static_cast<mcComment_commentDesc__opaque> (cd)->type == mcComment_afterStatement); |
| 459 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 460 |
__builtin_unreachable (); |
| 461 |
} |
| 462 |
|
| 463 |
extern "C" void _M2_mcComment_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 464 |
{ |
| 465 |
} |
| 466 |
|
| 467 |
extern "C" void _M2_mcComment_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 468 |
{ |
| 469 |
} |