Skip to content

Disable _Atomic and __thread keywords on JetBrains IDE only #134585

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

Closed
picnixz opened this issue May 23, 2025 · 7 comments
Closed

Disable _Atomic and __thread keywords on JetBrains IDE only #134585

picnixz opened this issue May 23, 2025 · 7 comments
Labels
build The build process and cross-build interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@picnixz
Copy link
Member

picnixz commented May 23, 2025

Note

I already have a PR ready in case this is accepted

This is really a silly issue and it's really not an issue with CPython. The issue is on CLion's side (https://youtrack.jetbrains.com/issue/CPP-15688/False-error-highlighting-for-C11-atomic-functions). The issue has been there for at least the past decade and nothing seemed to advance on JetBrains' side.

Let's assume that I have a file, say a.h in which I have extern _Atomic(size_t) something;. In another file, I include a.h and use size_t i;. Then CLion complains with "Ambiguous symbol size_t". There is one declaration in a.h and the libc declaration typedef __SIZE_TYPE__ size_t;.

So anything that is _Atomic(T) and uses T elsewhere while including a file that has _Atomic(T) somewhere ends up with that kind of issue. For CPython, this would be extern _Atomic(size_t) _mi_numa_node_count; but there are other occurrences, for instance for uintptr_t elsewhwere.

A similar issue happens with __thread. While CLion recognizes the macro, it suggests to include thread.h to use thread_local instead and the inspection cannot be disabled.

So, now, why would this be actually CPython's responsibility? well.. this is because it really annoys me and hinders me when developing on CLion. Because of _Atomic issues, I have syntax errors everywhere and I can't reliably go through real errors.

For instance, when I inspect the mimalloc bundled files, here's what my IDE would show:

Image

Or, even worse, when I just inspect the _hashopenssl.c module, I have

Image

Considering the sheer amount of size_t across the code base, this is very disturbing. There is however a simple and very portable solution: __JETBRAINS_IDE__.

The __JETBRAINS_IDE__ macro is a macro that is set when CLion is parsing code, so never at runtime or whatever (see https://blog.jetbrains.com/clion/2017/02/clion-2017-1-eap-debugger-fixes-ide-macros-and-new-cmake/). Because of this, we can easily do the following in pyport.h:

/*
 * Disable keywords and operators that are not recognized by CLion.
 *
 * This is only a temporary solution until CLion correctly supports them.
 */
#ifdef __JETBRAINS_IDE__
/*
 * Prevents false positives in CLion's static analysis which incorrectly
 * detects _Atomic(size_t) and _Atomic(uintptr_t) as duplicated declarations
 * of size_t and uintptr_t respectively.
 */
#define _Atomic(x)  x

/* Ignore C99 TLS extension '__thread' keyword. */
#define __thread
#endif

Fortunately, _Atomic T is not used across CPython code base, so we're safe on this side and only _Atomic(T) is replaced by T at CLion parse time. It will not affect VSCode or vim users.

Note that sre.c and sre_lib.h have vim-only comment:

/* vim:ts=4:sw=4:et
*/

We also have in bytecode.c:

// Note that there is some dummy C code at the top and bottom of the file
// to fool text editors like VS Code into believing this is valid C code.
// The actual instruction definitions start at // BEGIN BYTECODES //.
// See Tools/cases_generator/README.md for more information.

So it's not unprecedented that we have some editor-only hacks in the project.

cc @colesbury @erlend-aasland @vstinner @serhiy-storchaka

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

@picnixz picnixz added type-bug An unexpected behavior, bug, or error interpreter-core (Objects, Python, Grammar, and Parser dirs) build The build process and cross-build pending The issue will be closed if no feedback is provided type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels May 23, 2025
@serhiy-storchaka
Copy link
Member

Would adding #include <stddef.h> help?

@picnixz
Copy link
Member Author

picnixz commented May 23, 2025

Err... I never tried. I'm not on my dev session but I'll try this tomorrow. However, I don't think it helps because even the _Atomic declarations at the libc level are highlighted in red for CLion.

@picnixz
Copy link
Member Author

picnixz commented May 24, 2025

So I tried, and indeed it doesn't help. I think the issue is really with CLion parser and I can only suggest this hacky solution. I'm now used to having red marks but navigating through "next error" is still annoying.

@picnixz
Copy link
Member Author

picnixz commented May 24, 2025

I'll open a PR to check with buildbots first (still as a PoC)

@vstinner
Copy link
Member

Did you report the issue to CLion? I would prefer to fix the issue in CLion rather than having to modify Python.

@picnixz
Copy link
Member Author

picnixz commented May 24, 2025

It was reported 6 years ago and nothing has changed since then :(

@picnixz picnixz removed the pending The issue will be closed if no feedback is provided label May 26, 2025
@picnixz
Copy link
Member Author

picnixz commented May 26, 2025

Ok, turns out there is a workaround that works for me. By using ./configure CC=clang, the errors are gone so I'm going to disable this one. I can definitely use clang by default though I'm more used to GCC (well the __thread macro issue persists but this is a really minor one)

@picnixz picnixz closed this as completed May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build The build process and cross-build interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants