onqtam
onqtam

Reputation: 4528

Preprocessor problems with VC++6

I am porting my single header library to VC++6.

One macro however errors after its expansion.

When I ran C:\Program Files (x86)\Microsoft Visual Studio\VC98\Bin\VCVARS32.BAT and then tried compiling main.cpp from the command line calling cl.exe - it worked. I also looked at the preprocessor output with /EP and it looked fine.

I created an empty console win32 application and added the single .cpp file which includes my header. I haven't added any options to the project because... well... the IDE crashes on some dialogs because it doesn't run well on Windows 7.

The same .cpp file is compilable with mingw and gcc and anything - its simple portable code.

The relevant code is this:

#define DOCTEST_STR_CONCAT_IMPL(s1, s2) s1##s2
#define DOCTEST_STR_CONCAT(s1, s2) DOCTEST_STR_CONCAT_IMPL(s1, s2)
#define DOCTEST_ANONYMOUS(x) DOCTEST_STR_CONCAT(x, __LINE__)

#define DOCTEST_SUBCASE(name)                                                                      \
    if(const doctest::detail::Subcase & DOCTEST_ANONYMOUS(DOCTEST_AUTOGEN_SUBCASE_) =              \
            doctest::detail::Subcase(name, __FILE__, __LINE__))

// and in main.cpp:
void f() {
    DOCTEST_SUBCASE("") {}
}

and after the preprocessor (from command line with /EP) I get this (which is fine and compiles):

if(const doctest::detail::Subcase & DOCTEST_AUTOGEN_SUBCASE_20 =
    doctest::detail::Subcase("", "C:\\VC6_TESTS\\doctest\\main.cpp", 20)) {}

The error when compiling from within the IDE is this:

C:\VC6_TESTS\doctest\main.cpp(20) : error C2061: syntax error : identifier '__LINE__Var'
C:\VC6_TESTS\doctest\main.cpp(20) : error C2072: 'DOCTEST_AUTOGEN_SUBCASE_' : initialization of a function

Any ideas why from within the IDE I might be getting bad preprocessor behavior and why it would work from the command line? Or atleast a suggestion what to try...

And please don't tell me not to use VC++6 - I know it's 18 years old but it's a matter of honor to port my library.

EDIT:

this turned out to be a compiler bug.

Turning off Edit and Continue from the debug info fixed the problem (the /ZI command line option)

Here is the minimal example code that reproduces the problem:

#define STR_CONCAT_IMPL(s1, s2) s1##s2
#define STR_CONCAT(s1, s2) STR_CONCAT_IMPL(s1, s2)
#define ANONYMOUS(x) STR_CONCAT(x, __LINE__)

struct Subcase
{
    Subcase(const char* name, const char* file, int line) {}
    operator bool() const { return true; }
};

#define MYMACRO(name) if(const Subcase & ANONYMOUS(AUTOGEN_VARIABLE_) = Subcase(name, __FILE__, __LINE__))

int main() {
    MYMACRO("") {}
    return 0;
}

and the error when /ZI is passed:

c:\vc6_tests\doctest\main.cpp(19) : error C2061: syntax error : identifier '__LINE__Var'
c:\vc6_tests\doctest\main.cpp(19) : error C2072: 'AUTOGEN_VARIABLE_' : initialization of a function

If anyone has an idea how to trick the preprocessor/compiler into working for the default project config I would be grateful.

Upvotes: 0

Views: 288

Answers (1)

onqtam
onqtam

Reputation: 4528

There is no solution except not to use a compiler from 1998.

Upvotes: 1

Related Questions