Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AVR] error: reinterpret_cast from 'const __FlashStringHelper *' (aka 'const __attribute__((address_space(1))) char *') to 'const char *' is not allowed #60982

Closed
KOLANICH opened this issue Feb 24, 2023 · 4 comments
Labels
backend:AVR invalid Resolved as invalid, i.e. not a bug

Comments

@KOLANICH
Copy link
Contributor

KOLANICH commented Feb 24, 2023

//toolchain/hardware/tools/avr/avr/include/avr/pgmspace.h
#ifndef PGM_P
#define PGM_P const char *
// or #define PGM_P __attribute__((address_space(1))) const char * - the same issue
#endif

//toolchain/hardware/arduino/avr/cores/arduino/WString.h
using __FlashStringHelper = __attribute__((address_space(1))) char;

//toolchain/hardware/arduino/avr/cores/arduino/Print.cpp
PGM_P p = reinterpret_cast<PGM_P>(ifsh);
error: reinterpret_cast from 'const __FlashStringHelper *' (aka 'const __attribute__((address_space(1))) char *') to 'const char *' is not allowed

Workaround:

PGM_P p = (PGM_P)(ifsh);

Related:
arduino/ArduinoCore-avr#524
arduino/ArduinoCore-API#184

@benshi001
Copy link
Member

benshi001 commented Feb 27, 2023

I have made contrast between the latest clang and avr-gcc-5.4.0, for the follow C code, both of them accepted.

__flash const char ifsh[4] = {100, 200};
char foo(void) {
        // const char *p = reinterpret_cast<const char *>(ifsh);
        const char *p = (const char *)(ifsh);
        return p[0];
}

But for the following C++ code, neither of clang and avr-gcc-5.4.0 accepted.

__flash const char ifsh[4] = {100, 200};
char foo(void) {
#ifdef __cplusplus
        //const char *p = (const char *)(ifsh);
        const char *p = reinterpret_cast<const char *>(ifsh);
        return p[0];
}

@benshi001
Copy link
Member

It seems convert from program memory pointer to data memory pointer in pure C are allowed by both clang and avr-gcc, but in c++ code are allowed by neither.

@llvmbot
Copy link
Member

llvmbot commented Feb 27, 2023

@llvm/issue-subscribers-c-1

@AaronBallman AaronBallman added the invalid Resolved as invalid, i.e. not a bug label Feb 27, 2023
@AaronBallman
Copy link
Collaborator

This is behaving by design; it was changed in https://reviews.llvm.org/D58346 -- in general, casting away the address space is not a safe operation, so you have to use a C-style cast to force it to "work". Closing the issue as not a bug, but please reopen if you have further information.

@AaronBallman AaronBallman closed this as not planned Won't fix, can't repro, duplicate, stale Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:AVR invalid Resolved as invalid, i.e. not a bug
Projects
None yet
Development

No branches or pull requests

4 participants