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

Check for C11 atomics in and around VisitQualType #302

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

chrysn
Copy link
Contributor

@chrysn chrysn commented Sep 18, 2020

This does not help towards the support of C11 atomics, but ensures that
the existing error messages about the trouble with them get shown:

  • In VisitQualType at latest, where the source location information is
    already gone so it's not that helpful (but at least it should catch a
    wide range of sources where those types could come from)

  • In VisitTypedefNameDecl, where the typedef _Atomic(...) atomic_...;
    declarations of clang's stdatomic.h run through. It's but one of many
    possible callers, but it's the one where one more check can make the
    difference between a usable and a cryptic error for many cases.

    (And there's no point in cluttering everything with checks as we're soon gonna have atomic support anyway, aren't we? ;-) )

Closes: #293


Testing procedure: Run the minimal example of the closed issue. Instead of the long errors, there's now a concise:

In file included from test.c:1:
/usr/lib/llvm-10/bin/../lib/clang/10.0.1/include/stdatomic.h:76:37: error: c2rust: C11 Atomics are not supported. Aborting.
typedef _Atomic(_Bool)              atomic_bool;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

If it weren't for the front-end check in VisitTypedefNameDecl (and removing that was how I tested the back-end check), the error would be instead:

error: c2rust: C11 Atomics are not supported. No precise location information available. Aborting.

@chrysn
Copy link
Contributor Author

chrysn commented Sep 18, 2020

As with #291, builds pass locally:

$ ./scripts/test_translator.py ./tests
[...]
Test summary:
  unexpected failures: 0
  unexpected successes: 0
  expected failures: 3
  successes: 108

@@ -157,6 +157,24 @@ class TypeEncoder final : public TypeVisitor<TypeEncoder> {
astEncoder(ast) {}

void VisitQualType(const QualType &QT) {
if(isa<AtomicType>(QT)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: looks like this could be QT.isAtomicType().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope -- gives error: ‘const class clang::QualType’ has no member named ‘isAtomicType’. (When applied only here, and also for other locations which was what I first tried as TBH the isa<AtomicType> is something I plainly copy-pasted from other locations in this C++ file).

@@ -2104,6 +2122,17 @@ class TranslateASTVisitor final
cbor_encode_boolean(array, D->isImplicit());
});

if(isa<AtomicType>(typeForDecl)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see previous comment.

@thedataking
Copy link
Contributor

@chrysn thanks for this PR. I fixed the CI so if you push the minor changes I recommend, we should be able to get this tested and merged in.

@chrysn chrysn force-pushed the no-c11-atomics-caught-in-typedef branch from 04d23a7 to 44f4cd2 Compare January 6, 2021 15:56
@chrysn
Copy link
Contributor Author

chrysn commented Jan 6, 2021

Force-pushed with new time stamp to trigger CI rebuild.

@chrysn
Copy link
Contributor Author

chrysn commented Apr 6, 2021

Hm, stuck in CI again. Should I try to get it unstuck where it is, or just rebase onto master and see how things go there?

@fw-immunant
Copy link
Contributor

If you have time to poke at this again, it would be nice to rebase it onto master and make sure everything is green. I've seen our CI being randomly flaky before.

This does not help towards the support of C11 atomics, but ensures that
the existing error messages about the trouble with them get shown:

* In VisitQualType at latest, where the source location information is
  already gone so it's not that helpful (but at least it should catch a
  wide range of sources where those types could come from)

* In VisitTypedefNameDecl, where the `typedef _Atomic(...) atomic_...;`
  declarations of clang's stdatomic.h run through. It's but one of many
  possible callers, but it's the one where one more check can make the
  difference between a usable and a cryptic error for many cases.

Closes: immunant#293
@chrysn chrysn force-pushed the no-c11-atomics-caught-in-typedef branch from 44f4cd2 to 886c219 Compare June 8, 2022 09:18
@chrysn
Copy link
Contributor Author

chrysn commented Jun 8, 2022

Rebased, and checked that the copy-pasted idioms are still around the same way at their source location.

After I've discovered that I've always been running with outdated builds (cargo run -- transpile ... isn't enough, it needs to be cargo build && cargo run because c2rust is accessing c2rust-transpile through target).

The original testing procedure is still accurate. If you want to trigger the "No precise location information" case, try C like

void x(_Atomic(char) *c) { }

I'm sure there are places in the code where this could be caught with location information, but I'd say that constructs like the above are rare anyway, and that it's good to have a fallback in case we missed any other location.

@@ -189,6 +189,24 @@ class TypeEncoder final : public TypeVisitor<TypeEncoder> {
astEncoder(ast) {}

void VisitQualType(const QualType &QT) {
if(isa<AtomicType>(QT)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check needs to be moved inside the if (!QT.isNull()) { conditional below; otherwise we get an internal Clang abort doing isa<AtomicType>(QT) when QT has a null inner pointer.

Copy link
Contributor

@fw-immunant fw-immunant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented inline about issue causing test failures.

@kkysen
Copy link
Contributor

kkysen commented Jun 9, 2022

By the way, the issue with cargo run -- transpile not rebuilding c2rust-transpile and having to do cargo build && cargo run is really annoying and not easy to figure out what's going wrong. Let me see if I can fix that. I had no idea that was happening either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

stdatomic.h raises missing types for AstNode { ... __ATOMIC_SEQ_CST }
4 participants