LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 897 - ADT/SmallVector.h violates aliasing rules
Summary: ADT/SmallVector.h violates aliasing rules
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Core LLVM classes (show other bugs)
Version: 1.8
Hardware: All All
: P normal
Assignee: Chris Lattner
URL:
Keywords: code-cleanup
Depends on:
Blocks:
 
Reported: 2006-09-05 19:13 PDT by Nick Lewycky
Modified: 2010-02-22 12:55 PST (History)
1 user (show)

See Also:
Fixed By Commit(s):


Attachments
test patch (841 bytes, patch)
2006-10-08 15:22 PDT, Chris Lattner
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nick Lewycky 2006-09-05 19:13:08 PDT
gcc 4.1.2 emits the following warning on SmallVector:

llvm/include/llvm/ADT/SmallVector.h:46: warning: dereferencing type-punned
pointer will break strict-aliasing rules

This appears to be due to the use of a union:

template <typename T>
class SmallVectorImpl {
  T *Begin, *End, *Capacity;
  
  // Allocate raw space for N elements of type T.  If T has a ctor or dtor, we
  // don't want it to be automatically run, so we need to represent the space as
  // something else.  An array of char would work great, but might not be
  // aligned sufficiently.  Instead, we either use GCC extensions, or some
  // number of union instances for the space, which guarantee maximal alignment.
protected:
  union U {
    double D;
    long double LD;
    long long L;
    void *P;
  } FirstEl;
  // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
public:
  // Default ctor - Initialize to empty.
  SmallVectorImpl(unsigned N)
    : Begin((T*)&FirstEl), End((T*)&FirstEl), Capacity((T*)&FirstEl+N) {
  }

Line 46 being the one with where Begin, End and Capacity are being assigned. The
trouble is that you aren't allowed to put a "T" in the union anyways (otherwise,
that would be a neat hack to preserve the type without going through the
constructor).
Comment 1 Chris Lattner 2006-10-08 15:22:02 PDT
Created attachment 408 [details]
test patch

Please try this patch.	If it works (silences the warnings), I'll commit it. 
Thanks.

-Chris
Comment 2 Nick Lewycky 2006-10-08 15:47:56 PDT
Patch works for me.
Comment 3 Chris Lattner 2006-10-08 17:29:29 PDT
Nicholas, thanks for verifying.

Fixed, patch here:
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20061002/038404.html

-Chris