Skip to content

Wrong declaration order in generated C (some globals get used before their declaration) #4

@Ace17

Description

@Ace17
// input.cpp
extern const int* pA;
extern const int* pB;
const int* pA = (int*)&pB;
const int* pB = (int*)&pA;
$ clang -c input.cpp -emit-llvm -o test.bc
$ llvm-dis test.bc -o=test.ll
$ llvm-cbe test.ll
$ gcc -w -c test.cbe.c
test.cbe.c:168:30: error: ‘pA’ undeclared here (not in a function)
 uint32_t* pB = ((uint32_t*)(&pA));
                              ^

Here's the relevant part of the faulty generated code:

/* Global Variable Definitions and Initialization */
uint32_t* pB = ((uint32_t*)(&pA));
uint32_t* pA = ((uint32_t*)(&pB));

Maybe we should forward declare (as extern) all global variables?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions