Skip to content

Commit 2598053

Browse files
committed
ada: remove setjmp() longjmp()
setjmp() and longjmp() may cause memory leaks and are better to avoid. The ada parser actually doesn't seem to use them normally, there are proper checks for EOF in the code, and longjmp() is used only when something goes wrong after 1000 hits of EOF. I went through all the loops and all of them seem to check for EOF so there should be no need for setjmp()/longjmp().
1 parent 05e6ab4 commit 2598053

File tree

1 file changed

+43
-67
lines changed

1 file changed

+43
-67
lines changed

parsers/ada.c

Lines changed: 43 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595

9696
#include <string.h> /* to declare strxxx() functions */
9797
#include <ctype.h> /* to define isxxx() macros */
98-
#include <setjmp.h>
9998

10099
#include "parse.h" /* always include */
101100
#include "read.h" /* to define file readLineFromInputFile() */
@@ -104,13 +103,8 @@
104103
#include "debug.h" /* for Assert */
105104
#include "xtag.h"
106105

107-
typedef enum eAdaException
108-
{
109-
EXCEPTION_NONE,
110-
EXCEPTION_EOF
111-
} adaException;
112106

113-
static adaException exception;
107+
static bool eof_reached;
114108

115109
typedef enum eAdaParseMode
116110
{
@@ -301,13 +295,6 @@ static const char *AdaKeywords[] =
301295
"with"
302296
};
303297

304-
/* a jump buffer for fail-safe error prevention */
305-
static jmp_buf eofError;
306-
307-
/* a simple var to keep track of how many times we hit EOF... If we hit it
308-
* say, about 1000 times, we will print an error, jump to the end of the
309-
* program, and store what tags we have */
310-
static int eofCount;
311298

312299
/* variables for managing the input string, position as well as input line
313300
* number and position */
@@ -624,17 +611,8 @@ static void readNewLine (void)
624611
if (line == NULL)
625612
{
626613
lineLen = 0;
627-
exception = EXCEPTION_EOF;
628-
eofCount++;
629-
630-
if (eofCount >= 1000)
631-
{
632-
longjmp (eofError, exception);
633-
}
634-
else
635-
{
636-
return;
637-
}
614+
eof_reached = true;
615+
return;
638616
}
639617

640618
lineLen = strlen (line);
@@ -649,7 +627,7 @@ static void readNewLine (void)
649627
static void movePos (int amount)
650628
{
651629
pos += amount;
652-
if (exception != EXCEPTION_EOF && pos >= lineLen)
630+
if (!eof_reached && pos >= lineLen)
653631
{
654632
readNewLine ();
655633
}
@@ -709,10 +687,10 @@ static bool adaCmp (const char *match)
709687
{
710688
bool status = false;
711689

712-
/* first check to see if line is empty, if it is, throw an exception */
690+
/* first check to see if line is empty */
713691
if (line == NULL)
714692
{
715-
exception = EXCEPTION_EOF;
693+
eof_reached = true;
716694
return status;
717695
}
718696

@@ -735,10 +713,10 @@ static bool adaKeywordCmp (adaKeyword keyword)
735713
{
736714
bool status = false;
737715

738-
/* first check to see if line is empty, if it is, throw an exception */
716+
/* first check to see if line is empty, if it is */
739717
if (line == NULL)
740718
{
741-
exception = EXCEPTION_EOF;
719+
eof_reached = true;
742720
return status;
743721
}
744722

@@ -762,7 +740,7 @@ static void skipUntilWhiteSpace (void)
762740
* check to be true immediately */
763741
skipComments ();
764742

765-
while (exception != EXCEPTION_EOF && !isspace (line[pos]))
743+
while (!eof_reached && !isspace (line[pos]))
766744
{
767745
/* don't use movePos () because if we read in a new line with this function
768746
* we need to stop */
@@ -778,7 +756,7 @@ static void skipUntilWhiteSpace (void)
778756
if (line == NULL)
779757
{
780758
lineLen = 0;
781-
exception = EXCEPTION_EOF;
759+
eof_reached = true;
782760
return;
783761
}
784762

@@ -798,7 +776,7 @@ static void skipWhiteSpace (void)
798776
* check to fail immediately */
799777
skipComments ();
800778

801-
while (exception != EXCEPTION_EOF && isspace (line[pos]))
779+
while (!eof_reached && isspace (line[pos]))
802780
{
803781
movePos (1);
804782

@@ -809,7 +787,7 @@ static void skipWhiteSpace (void)
809787

810788
static void skipComments (void)
811789
{
812-
while (exception != EXCEPTION_EOF && isAdaComment (line, pos, lineLen))
790+
while (!eof_reached && isAdaComment (line, pos, lineLen))
813791
{
814792
readNewLine ();
815793
}
@@ -819,18 +797,18 @@ static void skipComments (void)
819797
* Return false if no string literal (nor char literal) is found. */
820798
static bool skipStringLiteral (void)
821799
{
822-
if (exception != EXCEPTION_EOF && isAdaStringLiteral (line, pos, lineLen))
800+
if (!eof_reached && isAdaStringLiteral (line, pos, lineLen))
823801
{
824802
do {
825803
movePos (1);
826-
} while (exception != EXCEPTION_EOF && !isAdaStringLiteral (line, pos, lineLen));
804+
} while (!eof_reached && !isAdaStringLiteral (line, pos, lineLen));
827805

828806
/* Go to the next char of " */
829807
movePos (1);
830808

831809
return true;
832810
}
833-
else if (exception != EXCEPTION_EOF && isAdaCharLiteral (line, pos, lineLen))
811+
else if (!eof_reached && isAdaCharLiteral (line, pos, lineLen))
834812
{
835813
movePos (3);
836814
return true;
@@ -855,7 +833,7 @@ static void skipPast (const char *past)
855833
skipCommentsAndStringLiteral ();
856834

857835
/* now look for the keyword */
858-
while (exception != EXCEPTION_EOF && !adaCmp (past))
836+
while (!eof_reached && !adaCmp (past))
859837
{
860838
movePos (1);
861839

@@ -871,7 +849,7 @@ static void skipPastKeyword (adaKeyword keyword)
871849
skipComments ();
872850

873851
/* now look for the keyword */
874-
while (exception != EXCEPTION_EOF && !adaKeywordCmp (keyword))
852+
while (!eof_reached && !adaKeywordCmp (keyword))
875853
{
876854
movePos (1);
877855

@@ -888,7 +866,7 @@ static void skipPastWord (void)
888866

889867
/* now increment until we hit a non-word character... Specifically,
890868
* whitespace, '(', ')', ':', and ';' */
891-
while (exception != EXCEPTION_EOF && !isspace (line[pos]) &&
869+
while (!eof_reached && !isspace (line[pos]) &&
892870
line[pos] != '(' && line[pos] != ')' && line[pos] != ':' &&
893871
line[pos] != ';')
894872
{
@@ -906,7 +884,7 @@ static void skipPastWord (void)
906884
if (line == NULL)
907885
{
908886
lineLen = 0;
909-
exception = EXCEPTION_EOF;
887+
eof_reached = true;
910888
return;
911889
}
912890

@@ -927,7 +905,7 @@ static void skipPastLambda (skipCompFn cmpfn, void *data)
927905
skipCommentsAndStringLiteral ();
928906

929907
/* now call the predicate */
930-
while (exception != EXCEPTION_EOF && !cmpfn (data))
908+
while (!eof_reached && !cmpfn (data))
931909
{
932910
movePos (1);
933911

@@ -1019,9 +997,9 @@ static adaTokenInfo *adaParseBlock (adaTokenInfo *parent, adaKind kind)
1019997
skipWhiteSpace ();
1020998

1021999
/* task and protected types are allowed to have discriminants */
1022-
if (exception != EXCEPTION_EOF && line[pos] == '(')
1000+
if (!eof_reached && line[pos] == '(')
10231001
{
1024-
while (exception != EXCEPTION_EOF && line[pos] != ')')
1002+
while (!eof_reached && line[pos] != ')')
10251003
{
10261004
movePos (1);
10271005
adaParseVariables (token, ADA_KIND_AUTOMATIC_VARIABLE);
@@ -1088,7 +1066,7 @@ static adaTokenInfo *adaParseBlock (adaTokenInfo *parent, adaKind kind)
10881066
skipUntilWhiteSpace ();
10891067
}
10901068

1091-
if (exception == EXCEPTION_EOF)
1069+
if (eof_reached)
10921070
{
10931071
freeAdaToken (&parent->children, token);
10941072
token = NULL;
@@ -1123,9 +1101,9 @@ static adaTokenInfo *adaParseSubprogram (adaTokenInfo *parent, adaKind kind)
11231101
skipWhiteSpace ();
11241102

11251103
/* if we find a '(' grab any parameters */
1126-
if (exception != EXCEPTION_EOF && line[pos] == '(' && token != NULL)
1104+
if (!eof_reached && line[pos] == '(' && token != NULL)
11271105
{
1128-
while (exception != EXCEPTION_EOF && line[pos] != ')')
1106+
while (!eof_reached && line[pos] != ')')
11291107
{
11301108
movePos (1);
11311109
tmpToken = adaParseVariables (token, ADA_KIND_AUTOMATIC_VARIABLE);
@@ -1141,9 +1119,9 @@ static adaTokenInfo *adaParseSubprogram (adaTokenInfo *parent, adaKind kind)
11411119
* pair */
11421120
skipWhiteSpace ();
11431121

1144-
if (exception != EXCEPTION_EOF && line[pos] == '(')
1122+
if (!eof_reached && line[pos] == '(')
11451123
{
1146-
while (exception != EXCEPTION_EOF && line[pos] != ')')
1124+
while (!eof_reached && line[pos] != ')')
11471125
{
11481126
movePos (1);
11491127
adaParseVariables (token, ADA_KIND_AUTOMATIC_VARIABLE);
@@ -1156,7 +1134,7 @@ static adaTokenInfo *adaParseSubprogram (adaTokenInfo *parent, adaKind kind)
11561134
/* loop infinitely until we hit a "is", "do" or ";", this will skip over
11571135
* the returns keyword, returned-type for functions as well as any one of a
11581136
* myriad of keyword qualifiers */
1159-
while (exception != EXCEPTION_EOF && token != NULL)
1137+
while (!eof_reached && token != NULL)
11601138
{
11611139
skipWhiteSpace ();
11621140

@@ -1235,11 +1213,11 @@ static adaTokenInfo *adaParseType (adaTokenInfo *parent, adaKind kind)
12351213
movePos (i);
12361214
skipWhiteSpace ();
12371215

1238-
if (exception != EXCEPTION_EOF && line[pos] == '(')
1216+
if (!eof_reached && line[pos] == '(')
12391217
{
12401218
/* in this case there is a discriminant to this type, gather the
12411219
* variables */
1242-
while (exception != EXCEPTION_EOF && line[pos] != ')')
1220+
while (!eof_reached && line[pos] != ')')
12431221
{
12441222
movePos (1);
12451223
adaParseVariables (token, ADA_KIND_AUTOMATIC_VARIABLE);
@@ -1254,7 +1232,7 @@ static adaTokenInfo *adaParseType (adaTokenInfo *parent, adaKind kind)
12541232
{
12551233
skipWhiteSpace ();
12561234
/* check to see if this may be a record or an enumeration */
1257-
if (exception != EXCEPTION_EOF && line[pos] == '(')
1235+
if (!eof_reached && line[pos] == '(')
12581236
{
12591237
movePos (1);
12601238
adaParseVariables (token, ADA_KIND_ENUM_LITERAL);
@@ -1292,7 +1270,7 @@ static adaTokenInfo *adaParseType (adaTokenInfo *parent, adaKind kind)
12921270
{
12931271
/* A and B */
12941272
/* until we hit "end record" we need to gather type variables */
1295-
while (exception != EXCEPTION_EOF)
1273+
while (!eof_reached)
12961274
{
12971275
skipWhiteSpace ();
12981276

@@ -1373,7 +1351,7 @@ static adaTokenInfo *adaParseVariables (adaTokenInfo *parent, adaKind kind)
13731351
* bufLen match */
13741352
buf[bufLen] = '\0';
13751353

1376-
while (exception != EXCEPTION_EOF)
1354+
while (!eof_reached)
13771355
{
13781356
/* make sure that we don't count anything in a comment as being valid to
13791357
* parse */
@@ -1454,7 +1432,7 @@ static adaTokenInfo *adaParseVariables (adaTokenInfo *parent, adaKind kind)
14541432

14551433
/* if we just incremented beyond the length of the current buffer, we need
14561434
* to read in a new line */
1457-
if (exception != EXCEPTION_EOF && bufPos >= bufLen)
1435+
if (!eof_reached && bufPos >= bufLen)
14581436
{
14591437
readNewLine ();
14601438

@@ -1592,14 +1570,14 @@ static adaTokenInfo *adaParse (adaParseMode mode, adaTokenInfo *parent)
15921570
initAdaTokenList (&genericParamsRoot.children);
15931571

15941572
/* if we hit the end of the file, line will be NULL and our skip and match
1595-
* functions will hit this jump buffer with EXCEPTION_EOF */
1596-
while (exception == EXCEPTION_NONE)
1573+
* functions will hit this jump buffer with eof_reached */
1574+
while (!eof_reached)
15971575
{
15981576
/* find the next place to start */
15991577
skipWhiteSpace ();
16001578

16011579
/* check some universal things to check for first */
1602-
if (exception == EXCEPTION_EOF)
1580+
if (eof_reached)
16031581
{
16041582
break;
16051583
}
@@ -1655,7 +1633,7 @@ static adaTokenInfo *adaParse (adaParseMode mode, adaTokenInfo *parent)
16551633
skipWhiteSpace ();
16561634

16571635
/* skip over the "(" until we hit the tag */
1658-
if (exception != EXCEPTION_EOF && line[pos] == '(')
1636+
if (!eof_reached && line[pos] == '(')
16591637
{
16601638
movePos (1);
16611639
skipWhiteSpace ();
@@ -1782,9 +1760,9 @@ static adaTokenInfo *adaParse (adaParseMode mode, adaTokenInfo *parent)
17821760
movePos (i);
17831761

17841762
/* now gather the parameters to this subprogram */
1785-
if (exception != EXCEPTION_EOF && line[pos] == '(')
1763+
if (!eof_reached && line[pos] == '(')
17861764
{
1787-
while (exception != EXCEPTION_EOF && line[pos] != ')')
1765+
while (!eof_reached && line[pos] != ')')
17881766
{
17891767
movePos (1);
17901768
adaParseVariables (genericParamsRoot.children.tail,
@@ -2366,11 +2344,10 @@ static void findAdaTags (void)
23662344
adaTokenInfo *tmp;
23672345

23682346
/* init all global data now */
2369-
exception = EXCEPTION_NONE;
2347+
eof_reached = false;
23702348
line = NULL;
23712349
pos = 0;
23722350
matchLineNum = 0;
2373-
eofCount = 0;
23742351

23752352
/* cannot just set matchFilePos to 0 because the fpos_t is not a simple
23762353
* integer on all systems. */
@@ -2386,12 +2363,11 @@ static void findAdaTags (void)
23862363

23872364
/* read in the first line */
23882365
readNewLine ();
2389-
if (exception == EXCEPTION_EOF)
2366+
if (eof_reached)
23902367
goto out;
23912368

23922369
/* tokenize entire file */
2393-
exception = setjmp (eofError);
2394-
while (exception != EXCEPTION_EOF && adaParse (ADA_ROOT, &root) != NULL);
2370+
while (!eof_reached && adaParse (ADA_ROOT, &root) != NULL);
23952371

23962372
/* store tags */
23972373
tmp = root.children.head;

0 commit comments

Comments
 (0)