From 886c219f87563635f5cb925a3df4925434e92ceb Mon Sep 17 00:00:00 2001 From: chrysn Date: Fri, 18 Sep 2020 11:36:51 +0200 Subject: [PATCH] Check for C11 atomics in and around VisitQualType 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: https://github.com/immunant/c2rust/issues/293 --- c2rust-ast-exporter/src/AstExporter.cpp | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/c2rust-ast-exporter/src/AstExporter.cpp b/c2rust-ast-exporter/src/AstExporter.cpp index e013bc6e4f..d5e9e7b68e 100644 --- a/c2rust-ast-exporter/src/AstExporter.cpp +++ b/c2rust-ast-exporter/src/AstExporter.cpp @@ -189,6 +189,24 @@ class TypeEncoder final : public TypeVisitor { astEncoder(ast) {} void VisitQualType(const QualType &QT) { + if(isa(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(); @@ -2188,6 +2206,17 @@ class TranslateASTVisitor final cbor_encode_boolean(array, D->isImplicit()); }); + if(isa(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;