fesch / Structorizer.Desktop

Structorizer is a little tool which you can use to create Nassi-Schneiderman Diagrams (NSD).
https://structorizer.fisch.lu
GNU General Public License v3.0
65 stars 20 forks source link

On C import macro is expanded in char constant #1130

Closed csrabak closed 8 months ago

csrabak commented 8 months ago

This code when importing is done in Structorizer:

#include "getopt.h"

#define c  2.998e8
#define Z0 376.7

int
main(int argc, char *argv[])
{
    int                 option;
    int                 output_choice = -1;
    opterr = 0;
    while ((option = getopt(argc, argv, "abc?")) != -1)
        switch (option) {
            case 'a':
                output_choice = 1;
                break;
            case 'b':
                output_choice = 2;
                break;
            case 'c':
                output_choice = 3;
                break;
            case '?':
            default:
                return 1;
        }

    return 0;
} 

Fails with an interruption in the importing process with the following diagonostic:

error.lexical in file "D:\Users\csrabak\Work\MyProjects\bug_examp.c"

Preceding source context:
   14:       opterr = 0;
   15:       while ((option =
   16:               getopt(argc, argv, "abc?")) != -1)
   17:           switch (option) {
   18:               case 'a':
   19:                   output_choice = 1;
   20:                   break;
   21:               case 'b':
   22:                   output_choice = 2;
   23:                   break;
   24:               case » '2.998e8':

Found token (Error) (')

Expected: 

The defined constant c in the fourth line of code was expanded in the char 'c' constant of the switch.

codemanyak commented 8 months ago

Indeed. The preparser unduly replaces defines in character and string literals. Hence, the following simple code is importable but will also produce a wrong result:

#include <stdio.h>

#define funny nasty

int main(int argc, char* argv[])
{
    printf("This will be funny.\n");
    return 0;
}

grafik

codemanyak commented 8 months ago

Fix for this issue is prepared to be delivered with version 3.32-17. @csrabak A temporary workaround may be to enable C-specific import option "Convert #defines to constants": grafik

csrabak commented 8 months ago

Thanks for quick fix and workaroud meanwhile!