Properly expand macros that have the same name as a keyword or typedef.

If such macros were used within other macros, they would generally not be expanded, due to the order in which operations were evaluated during preprocessing.

This is actually an issue that was fixed by the changes from ORCA/C 2.1.0 to 2.1.1 B3, but then broken again by commit d0b4b75970.

Here is an example with the name of a keyword:

#define X long int
#define long
X x;
int main(void) {
        return sizeof(x); /* should be sizeof(int) */
}

Here is an example with the name of a typedef:

typedef short T;
#define T long
#define X T
X x;
int main(void) {
        return sizeof(x); /* should be sizeof(long) */
}
This commit is contained in:
Stephen Heumann 2022-05-24 22:22:37 -05:00
parent daff1754b2
commit deca73d233
2 changed files with 9 additions and 7 deletions

View File

@ -4873,13 +4873,6 @@ if tokenList <> nil then begin {get a token put back by a macro}
dispose(tPtr);
if token.kind = typedef then {allow for typedefs in a macro}
token.kind := ident;
if token.kind = ident then begin
CopyString(@workString, token.name);
lExpandMacros := expandMacros;
expandMacros := false;
CheckIdentifier;
expandMacros := lExpandMacros;
end; {if}
{ dead code
if token.kind = ident then
if FindSymbol(token,allSpaces,false,false) <> nil then
@ -4926,6 +4919,13 @@ if tokenList <> nil then begin {get a token put back by a macro}
goto 4;
end; {if}
end; {if}
if token.kind = ident then begin
CopyString(@workString, token.name);
lExpandMacros := expandMacros;
expandMacros := false;
CheckIdentifier;
expandMacros := lExpandMacros;
end; {if}
goto 2;
end; {if}
5: {skip white space}

View File

@ -1803,6 +1803,8 @@ int foo(int[42]);
183. The # preprocessor operator would not work correctly on tokens that had been produced by the ## preprocessor operator, tokens that were split over two or more lines using line continuations, or tokens represented using trigraphs.
184. If a macro was declared with the same name as a keyword or a typedef, and that name was also used within another macro, the first macro would not be correctly expanded within invocations of the second macro. (This is a bug that was previously fixed in ORCA/C 2.1.1 B3, but was subsequently reintroduced.)
-- Bugs from C 2.1.0 that have been fixed -----------------------------------
1. In some situations, fread() reread the first 1K or so of the file.