Skip to content

Commit

Permalink
c-based: fix to handle edge case where "character" returned is a symb…
Browse files Browse the repository at this point in the history
…ol (> 0xff)
  • Loading branch information
jafl committed Aug 18, 2023
1 parent ce46d93 commit 7227383
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
6 changes: 6 additions & 0 deletions Units/parser-java.r/java_enum.java.d/expected.tags
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ FIRST_VALUE input.java /^ FIRST_VALUE,$/;" e enum:C.TrivialEnum file: end:3
FancyEnum input.java /^ FancyEnum(int i) {$/;" m class:C.FancyEnum end:11
FancyEnum input.java /^ public enum FancyEnum {$/;" g class:C end:14
SECOND_VALUE input.java /^ SECOND_VALUE$/;" e enum:C.TrivialEnum file: end:4
StringEnum input.java /^ private StringEnum(String _s) {$/;" m class:StringEnum file: end:25
StringEnum input.java /^public enum StringEnum {$/;" g end:28
TrivialEnum input.java /^ public enum TrivialEnum {$/;" g class:C end:5
X input.java /^ X("X"),$/;" e enum:StringEnum file: end:19
Y input.java /^ Y("Y"),$/;" e enum:StringEnum file: end:20
Z input.java /^ Z("Z");$/;" e enum:StringEnum file: end:21
i input.java /^ private int i;$/;" f class:C.FancyEnum file: end:8
m input.java /^ void m() {$/;" m class:C.FancyEnum end:13
s input.java /^ private final String s;$/;" f class:StringEnum file: end:27
13 changes: 13 additions & 0 deletions Units/parser-java.r/java_enum.java.d/input.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ void m() {
}
}
}

public enum StringEnum {

X("X"),
Y("Y"),
Z("Z");

private StringEnum(String _s) {
s = _s;
}

private final String s;
}
28 changes: 7 additions & 21 deletions parsers/c-based.c
Original file line number Diff line number Diff line change
Expand Up @@ -1486,22 +1486,8 @@ static void skipToMatch (const char *const pair)
while (matchLevel > 0 && (c = skipToNonWhite ()) != EOF)
{
if (CollectingSignature)
{
if (c <= 0xff)
vStringPut (Signature, c);
else
{
switch (c)
{
case CHAR_SYMBOL:
case STRING_SYMBOL:
vStringCat (Signature, cppGetLastCharOrStringContents ());
break;
default:
AssertNotReached();
}
}
}
cStringPut (Signature, c);

if (c == begin)
{
++matchLevel;
Expand Down Expand Up @@ -1586,11 +1572,11 @@ static void readIdentifier (tokenInfo *const token, const int firstChar)

do
{
vStringPut (name, c);
cStringPut (name, c);
if (CollectingSignature)
{
if (!first)
vStringPut (Signature, c);
cStringPut (Signature, c);
first = false;
}
c = cppGetc ();
Expand Down Expand Up @@ -1719,7 +1705,7 @@ static void readOperator (statementInfo *const st)
vStringPut (name, ' ');
whiteSpace = false;
}
vStringPut (name, c);
cStringPut (name, c);
}
c = cppGetc ();
} while (! isOneOf (c, "(;") && c != EOF);
Expand All @@ -1729,7 +1715,7 @@ static void readOperator (statementInfo *const st)
vStringPut (name, ' '); /* always separate operator from keyword */
do
{
vStringPut (name, c);
vStringPut (name, c); /* acceptable are all ascii */

Check warning on line 1718 in parsers/c-based.c

View check run for this annotation

Codecov / codecov/patch

parsers/c-based.c#L1718

Added line #L1718 was not covered by tests
c = cppGetc ();
} while (isOneOf (c, acceptable));
}
Expand Down Expand Up @@ -2310,8 +2296,8 @@ static int parseParens (statementInfo *const st, parenInfo *const info)
do
{
int c = skipToNonWhite ();
vStringPut (Signature, c);

cStringPut (Signature, c);
switch (c)
{
case '^':
Expand Down
28 changes: 28 additions & 0 deletions parsers/cpreprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,16 @@ extern void cppEndStatement (void)
/* This puts a character back into the input queue for the input File. */
extern void cppUngetc (const int c)
{
if (c == STRING_SYMBOL || c == CHAR_SYMBOL)
{
Assert(Cpp.charOrStringContents != NULL);
cppUngetc('"');
cppUngetString(vStringValue(Cpp.charOrStringContents), vStringLength(Cpp.charOrStringContents));
cppUngetc('"');
vStringClear(Cpp.charOrStringContents);
return;
}

if(!Cpp.ungetPointer)
{
// no unget data
Expand Down Expand Up @@ -1520,6 +1530,24 @@ static void conditionMayPut (vString *condition, int c)
vStringPut(condition, c);
}

extern void cStringPut (vString* string, const int c)
{
if (c <= 0xff)
vStringPut (string, c);
else
{
switch (c)
{
case CHAR_SYMBOL:
case STRING_SYMBOL:
vStringCat (string, cppGetLastCharOrStringContents ());
break;
default:
AssertNotReached();

Check warning on line 1546 in parsers/cpreprocessor.c

View check run for this annotation

Codecov / codecov/patch

parsers/cpreprocessor.c#L1545-L1546

Added lines #L1545 - L1546 were not covered by tests
}
}
}

/* This function returns the next character, stripping out comments,
* C pre-processor directives, and the contents of single and double
* quoted strings. In short, strip anything which places a burden upon
Expand Down
1 change: 1 addition & 0 deletions parsers/cpreprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ extern int cppUngetBufferSize();
extern void cppUngetString(const char * string,int len);
extern int cppGetc (void);
extern const vString * cppGetLastCharOrStringContents (void);
extern void cStringPut (vString * string, const int c);

/* Notify the external parser state for the purpose of conditional
* branch choice. The CXX parser stores the block level here. */
Expand Down

0 comments on commit 7227383

Please sign in to comment.