Skip to content

Commit

Permalink
Check for C11 atomics in and around VisitQualType
Browse files Browse the repository at this point in the history
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: #293
  • Loading branch information
chrysn committed Sep 18, 2020
1 parent ca54f19 commit 04d23a7
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions c2rust-ast-exporter/src/AstExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ class TypeEncoder final : public TypeVisitor<TypeEncoder> {
astEncoder(ast) {}

void VisitQualType(const QualType &QT) {
if(isa<AtomicType>(QT)) {
// No printC11AtomicError available here and no location
// information either -- should better have been caught at the
// caller, but catching it here is still better than the
// nondescript error messages that would come later.
std::string msg = "C11 Atomics are not supported. No precise "
"location information available. Aborting.";

auto &DiagEngine = Context->getDiagnostics();
// Prefix warnings with `c2rust`, so the user can distinguish
// our warning messages from those generated by clang itself.
const auto ID = DiagEngine.getCustomDiagID(DiagnosticsEngine::Error,
"c2rust: %0");
DiagEngine.Report(SourceLocation::getFromRawEncoding(0), ID).AddString(msg);

abort();
}

if (!QT.isNull()) {
auto s = QT.split();

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

if(isa<AtomicType>(typeForDecl)) {
// This case is especially checked as that's what happens when
// clang's stdatomic.h is traversed. Other callers of VisitQualType
// could get the same check to preserve the location information
// available in Decl but not in Type, but those are more likely not
// to be hit, and can fall back to the less descriptive error from
// inside there.
printC11AtomicError(D);
abort();
}

typeEncoder.VisitQualType(typeForDecl);

return true;
Expand Down

0 comments on commit 04d23a7

Please sign in to comment.