]> granicus.if.org Git - clang/commitdiff
An extremely hacky version of transparent_union support; it isn't
authorEli Friedman <eli.friedman@gmail.com>
Tue, 2 Sep 2008 05:19:23 +0000 (05:19 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Tue, 2 Sep 2008 05:19:23 +0000 (05:19 +0000)
anywhere near correct in terms of missing cases and missing
diagnostics, but it's good enough to handle the uses in the
Linux system headers, which are currently a constant pain for compiling
applications on Linux.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@55621 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticKinds.def
lib/Sema/SemaDeclAttr.cpp
test/Sema/transparent-union-pointer.c [new file with mode: 0644]

index 95d21126500dd805a89bf6044520f48b58c55b5f..9c90306177c90a9e07abe8fc97b934f530ec794b 100644 (file)
@@ -694,6 +694,9 @@ DIAG(err_attr_wrong_decl, ERROR,
      "'%0' attribute invalid on this declaration, requires typedef or value")
 DIAG(warn_attribute_nonnull_no_pointers, WARNING,
   "'nonnull' attribute applied to function with no pointer arguments")
+DIAG(warn_transparent_union_nonpointer, WARNING,
+  "'transparent_union' attribute support incomplete; only supported for"
+  "pointer unions")
   
 // Clang-Specific Attributes
 DIAG(err_attribute_iboutlet_non_ivar, ERROR,
index eb9888e9a28e91e2a2a9bc458384b11946bfe364..b5737d8b3c61896146a0e5a7e08898ae064814d0 100644 (file)
@@ -725,19 +725,34 @@ static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
     return;
   }
 
-  TypeDecl *decl = dyn_cast<TypeDecl>(d);
-
-  if (!decl || !S.Context.getTypeDeclType(decl)->isUnionType()) {
+  // FIXME: This shouldn't be restricted to typedefs
+  TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
+  if (!TD || !TD->getUnderlyingType()->isUnionType()) {
     S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type,
          "transparent_union", "union");
     return;
   }
 
-  //QualType QTy = Context.getTypeDeclType(decl);
-  //const RecordType *Ty = QTy->getAsUnionType();
+  RecordDecl* RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
+
+  // FIXME: Should we do a check for RD->isDefinition()?
+
+  // FIXME: This isn't supposed to be restricted to pointers, but otherwise
+  // we might silently generate incorrect code; see following code
+  for (int i = 0; i < RD->getNumMembers(); i++) {
+    if (!RD->getMember(i)->getType()->isPointerType()) {
+      S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer);
+      return;
+    }
+  }
 
-// FIXME
-// Ty->addAttr(new TransparentUnionAttr());
+  // FIXME: This is a complete hack; we should be properly propagating
+  // transparent_union through Sema.  That said, this is close enough to
+  // correctly compile all the common cases of transparent_union without
+  // errors or warnings
+  QualType NewTy = S.Context.VoidPtrTy;
+  NewTy.addConst();
+  TD->setUnderlyingType(NewTy);
 }
 
 static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
diff --git a/test/Sema/transparent-union-pointer.c b/test/Sema/transparent-union-pointer.c
new file mode 100644 (file)
index 0000000..58597b1
--- /dev/null
@@ -0,0 +1,14 @@
+// RUN: clang %s -fsyntax-only -verify
+
+typedef union   {
+       union wait *__uptr;
+       int *__iptr;
+} __WAIT_STATUS __attribute__ ((__transparent_union__));
+
+extern int wait (__WAIT_STATUS __stat_loc);
+
+void fastcgi_cleanup() {
+       int status = 0;
+       wait(&status);
+}
+