Commit Graph

75 Commits

Author SHA1 Message Date
Nathan Sidwell ffa7dc379f PR 17456
More helpful diagnostic on casts between unrelated class hierarchies.

llvm-svn: 227371
2015-01-28 21:31:26 +00:00
Nico Weber b3a9978dc8 Don't let virtual calls and dynamic casts call Sema::MarkVTableUsed().
clang currently calls MarkVTableUsed() for classes that get their virtual
methods called or that participate in a dynamic_cast. This is unnecessary,
since CodeGen only emits vtables when it generates constructor, destructor, and
vtt code. (*)

Note that Sema::MarkVTableUsed() doesn't cause the emission of a vtable.
Its main user-visible effect is that it instantiates virtual member functions
of template classes, to make sure that if codegen decides to write a vtable
all the entries in the vtable are defined.

While this shouldn't change the behavior of codegen (other than being faster),
it does make clang more permissive: virtual methods of templates (in particular
destructors) end up being instantiated less often. In particular, classes that
have members that are smart pointers to incomplete types will now get their
implicit virtual destructor instantiated less frequently. For example, this
used to not compile but does now compile:

    template <typename T> struct OwnPtr {
      ~OwnPtr() { static_assert((sizeof(T) > 0), "TypeMustBeComplete"); }
    };
    class ScriptLoader;
    struct Base { virtual ~Base(); };
    struct Sub : public Base {
      virtual void someFun() const {}
      OwnPtr<ScriptLoader> m_loader;
    };
    void f(Sub *s) { s->someFun(); }

The more permissive behavior matches both gcc (where this is not often
observable, since in practice most things with virtual methods have a key
function, and Sema::DefineUsedVTables() skips vtables for classes with key
functions) and cl (which is my motivation for this change) – this fixes
PR20337.  See this issue and the review thread for some discussions about
optimizations.

This is similar to r213109 in spirit. r225761 was a prerequisite for this
change.

Various tests relied on "a->f()" marking a's vtable as used (in the sema
sense), switch these to just construct a on the stack. This forces
instantiation of the implicit constructor, which will mark the vtable as used.

(*) The exception is -fapple-kext mode: In this mode, qualified calls to
virtual functions (`a->Base::f()`) still go through the vtable, and since the
vtable pointer off this doesn't point to Base's vtable, this needs to reference
Base's vtable directly. To keep this working, keep referencing the vtable for
virtual calls in apple kext mode.

llvm-svn: 227073
2015-01-26 06:23:36 +00:00
David Majnemer e64941fa60 Sema: Check value dependent casts when possible
We know that const_cast<char *>((void)Something) is ill-formed, even if
'Something' is dependent because you can't cast from void to a pointer
type.

This fixes PR21845.

llvm-svn: 224299
2014-12-16 00:46:30 +00:00
Anastasia Stulova 5d8ad8a7b8 [OpenCL] Implemented restrictions for pointer conversions specified in OpenCL v2.0.
OpenCL v2.0 s6.5.5 restricts conversion of pointers to different address spaces:
- the named address spaces (__global, __local, and __private) => __generic - implicitly converted;
- __generic => named - with an explicit cast;
- named <=> named - disallowed;
- __constant <=> any other - disallowed.

llvm-svn: 222834
2014-11-26 15:36:41 +00:00
Roman Divacky d517801493 Implement -Wcast-qual, fixing #13772.
Many thanks to dblaikie for his advices and suggestions!

llvm-svn: 222568
2014-11-21 21:03:10 +00:00
Richard Smith 1133085461 PR20227: materialize a temporary when dynamic_casting a class prvalue to a
reference type.

llvm-svn: 212551
2014-07-08 17:25:14 +00:00
Alp Toker d4a3f0e894 Hide the concept of diagnostic levels from lex, parse and sema
The compilation pipeline doesn't actually need to know about the high-level
concept of diagnostic mappings, and hiding the final computed level presents
several simplifications and other potential benefits.

The only exceptions are opportunistic checks to see whether expensive code
paths can be avoided for diagnostics that are guaranteed to be ignored at a
certain SourceLocation.

This commit formalizes that invariant by introducing and using
DiagnosticsEngine::isIgnored() in place of individual level checks throughout
lex, parse and sema.

llvm-svn: 211005
2014-06-15 23:30:39 +00:00
Nikola Smiljanic 03ff2596cb Refactoring. Remove Owned method from Sema.
llvm-svn: 209812
2014-05-29 14:05:12 +00:00
Nikola Smiljanic 01a7598561 Refactoring. Remove release and take methods from ActionResult. Rename takeAs to getAs.
llvm-svn: 209800
2014-05-29 10:55:11 +00:00
Craig Topper c3ec149bb2 [C++11] Use 'nullptr'. Sema edition.
llvm-svn: 209613
2014-05-26 06:22:03 +00:00
Fariborz Jahanian c70a54366e Objective-C ARC. Add support for toll-free bridge
type ,and bridge attribute, checking with static_cast. 
// rdar://16756639

llvm-svn: 208474
2014-05-10 17:40:11 +00:00
David Majnemer daff37013c AST: Mangle reference temporaries reliably
Summary:
Previously, we would generate a single name for all reference
temporaries and allow LLVM to rename them for us.  Instead, number the
reference temporaries as we build them in Sema.

Reviewers: rsmith

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D3554

llvm-svn: 207776
2014-05-01 17:50:17 +00:00
Fariborz Jahanian f1a22f487e Objective-C. Improve diagnosis of bridging types.
// rdar://16737117

llvm-svn: 207542
2014-04-29 16:12:56 +00:00
Logan Chien af24ad940d Fix diagnostics for C-style cast to function type.
If the C-style type cast is applied to the overloaded
function and the destination type is function type,
then Clang will crash with assertion failure.  For example,

    void foo(int);
    void foo(int, int);
    void bar() {
        typedef void (ft)(int);
        ft p = (ft)foo;
    }

In this case, the overloaded function foo will be cast to
a function type, which should be considered as an error.
But, unfortunately, since the function resolution is using
canonical type, the matched function will be returned, and
result in SEGV.

This patch fixes this issue by removing the assertion and
add some error diagnostics as the one in static_cast.

llvm-svn: 206133
2014-04-13 16:08:24 +00:00
David Majnemer 763584dc64 MS ABI: Tweak pointer-to-member mangling/inheritance model selection
Properly determine the inheritance model when dealing with nullptr:
- If a nullptr template argument is being checked against
  pointer-to-member parameter, nail down an inheritance model.
  N.B. We will chose an inheritance model even if we won't ultimately
  choose the template to instantiate!  Cooky, right?
- Null pointer-to-datamembers have a virtual base table offset of -1,
  not zero. Previously, we chose an offset of 0.

llvm-svn: 200920
2014-02-06 10:59:19 +00:00
David Majnemer 1cdd96d6ad Revert "Revert r199416, "MS ABI: Improve selection of an inheritance model""
This reverts commit r199475 (which reverted r199416) with fixes for the
breakages.

We wouldn't lock an inheritance model if we saw a pointer-to-member
formed as a result of the address-of operator.

llvm-svn: 199482
2014-01-17 09:01:00 +00:00
NAKAMURA Takumi 5c486cf3f3 Revert r199416, "MS ABI: Improve selection of an inheritance model"
It broke tests for targeting x86_64-pc-win32:

    Clang Tools :: clang-modernize/LoopConvert/array.cpp
    Clang :: CodeGenCXX/2010-05-10-Var-DbgInfo.cpp
    Clang :: CodeGenCXX/member-call-parens.cpp
    Clang :: CodeGenCXX/ptr-to-datamember.cpp
    Clang :: SemaTemplate/instantiate-function-1.cpp

llvm-svn: 199475
2014-01-17 07:33:47 +00:00
David Majnemer 37054dd4e7 MS ABI: Improve selection of an inheritance model
The MSVC ABI is rather finicky about the exact representation of it's
pointer-to-member representation.  The exact position of when and where
it will go with one representation versus another appears to be when it
desires the pointer-to-member to be complete.

To properly implement this in clang, do several things:
- Give up on tracking the polymorphic nature of the class.  It isn't
  useful to Sema and is only pertinent when choosing CodeGen-time
  details like whether the field-offset can be 0 instead of -1.
- Insist on locking-in the inheritance model when we ask our
  pointer-to-member type to be complete.  From there, grab the
  underlying CXXRecordDecl and try to make *that* complete.  Once we've
  done this, we can calculate it's inheritance model and apply it using
  an attribute.

N.B. My first bullet point is a lie.  We will eventually care about the
specifics of whether or not a CXXRecordDecl is or is not polymorphic
because MSVC compatible mangling of such things depends on it.  However,
I believe we will handle this in a rather different way.

llvm-svn: 199416
2014-01-16 20:05:57 +00:00
David Majnemer f5b93794b7 Sema: Fix crash during member pointer conversion involving incomplete classes
We would attempt to determine the inheritance relationship between
classes 'A' and 'B' during static_cast if we tried to convert from 'int
A::*' to 'int B::*'.  However, the question "does A derive from B" is
not meaningful when 'A' isn't defined.

Handle this case by requiring that 'A' be defined.

This fixes PR18506.

llvm-svn: 199374
2014-01-16 12:02:55 +00:00
Joey Gouly 8fc32f01dc [OpenCL] Disallow casts between address spaces.
llvm-svn: 199208
2014-01-14 12:47:29 +00:00
Fariborz Jahanian 8c5b4be417 ObjectiveC. Allow toll free bridge cast warnings outside
ARC and in objectiveC/ObjectiveC++ MRR mode as well.
// rdar://15454846

llvm-svn: 195288
2013-11-21 00:39:36 +00:00
Eli Friedman 3ce27103d9 Allow dynamic_cast to void* even with -fno-rtti.
PR17346.

llvm-svn: 191340
2013-09-24 23:21:41 +00:00
Eli Friedman 7127108357 Fix crash with cast of value-dependent expr.
We don't really need to perform semantic analysis on the dependent expression
anyway, so just call the cast dependent.

<rdar://problem/15012610>

llvm-svn: 190981
2013-09-19 01:12:33 +00:00
Enea Zaffanella 76e98feb57 Fix missing source location in CXXTemporaryObjectExpr nodes.
For clarity, renamed (get/set)ParenRange as (get/set)ParenOrBraceRange
in CXXConstructExpr nodes.
Added testcase.

llvm-svn: 190239
2013-09-07 05:49:53 +00:00
Eli Friedman 89fe0d5842 Properly track l-paren of a CXXFucntionalCastExpr.
In addition to storing more useful information in the AST, this
fixes a semantic check in template instantiation which checks whether
the l-paren location is valid.

Fixes PR16903.

llvm-svn: 188495
2013-08-15 22:02:56 +00:00
Arnaud A. de Grandmaison cb6f943ada Check dynamic_cast is not used with -fno-rtti, unless it is a noop or can be resolved statically.
llvm-svn: 187564
2013-08-01 08:28:32 +00:00
Eli Friedman 3fd26b8514 Don't build expressions for invalid casts.
This matches how we normally perform semantic analysis for other sorts
of invalid expressions: it means we don't have to reason about invalid
sub-expressions.

Fixes PR16680.

llvm-svn: 187276
2013-07-26 23:47:47 +00:00
Craig Topper 61ac906bdd Use SmallVectorImpl::reverse_iterator instead of SmallVector to avoid specifying the vector size.
llvm-svn: 185784
2013-07-08 03:55:09 +00:00
Joerg Sonnenberger ffc6d49982 Don't use unnamed local enums as template arguments.
Fixes -Werror bootstrap.

llvm-svn: 185023
2013-06-26 21:31:47 +00:00
Eli Friedman 447f729091 Remove dead code.
llvm-svn: 184453
2013-06-20 18:53:21 +00:00
Andy Gibbs fa5026d9eb Silence 'set but not used' warning when building in release mode using gcc.
llvm-svn: 184299
2013-06-19 13:33:37 +00:00
Richard Smith 82c9b5183f Fix handling of const_cast from prvalue to rvalue reference: such a cast is
only permitted if the source object is of class type, and should materialize a
temporary for the reference to bind to.

llvm-svn: 184017
2013-06-14 22:27:52 +00:00
Hans Wennborg 15439bcf79 Disallow reinterpret_cast from pointer to bool on Windows
This became allowed by accident in r131201, but triggers an assert.
That patch added an exception to allow conversion from pointers to
narrow integral types for MSVC compatibility. However, a pointer can
already be converted to bool in a civilized manner; allowing conversion
via reinterpret_cast is a bad idea.

Fixes PR16222.

llvm-svn: 183394
2013-06-06 09:16:36 +00:00
Ted Kremenek e3dc7f74be Split off casts to void* for -Wint-to-pointer-cast to subgroup -Wint-to-void-pointer-cast.
This change is motivated from user feedback that some APIs use
void* as an opaque "context" object that may not really be a pointer.
Such users want an ability to turn off the warning for casts
to void* while preserving the warning for other cases.

Implements <rdar://problem/14016721>.

llvm-svn: 182884
2013-05-29 21:50:46 +00:00
John McCall d25db7ed0f Grab-bag of bit-field fixes:
- References to ObjC bit-field ivars are bit-field lvalues;
    fixes rdar://13794269, which got me started down this.
  - Introduce Expr::refersToBitField, switch a couple users to
    it where semantically important, and comment the difference
    between this and the existing API.
  - Discourage Expr::getBitField by making it a bit longer and
    less general-sounding.
  - Lock down on const_casts of bit-field gl-values until we
    hear back from the committee as to whether they're allowed.

llvm-svn: 181252
2013-05-06 21:39:12 +00:00
Dmitri Gribenko 8f8930fc01 ArrayRef'ize InitializationSequence constructor and InitializationSequence::Diagnose()
Patch by Robert Wilhelm.

llvm-svn: 181022
2013-05-03 15:05:50 +00:00
Jordan Rose 04a94d1442 Provide a fixit to static_cast for reinterpret_casts within a class hierarchy.
The suggestion was already in the text of the note; this just adds the
actual fixit and the appropriate test cases.

Patch by Alexander Zinenko!

llvm-svn: 178274
2013-03-28 19:09:40 +00:00
John McCall f2abe19dbb Make the -Wreinterpret-base-class logic safe against invalid
declarations at any point. Patch by Alexander Zinenko, and
report by Richard Smith.

llvm-svn: 178098
2013-03-27 00:03:48 +00:00
John McCall cda8083309 Warn about attempts to reinterpret_cast between two types that are
hierarchy-related at a possibly nonzero offset.

Patch by Alexander Zinenko!

llvm-svn: 177698
2013-03-22 02:58:14 +00:00
Fariborz Jahanian f073871f7f objective-C arg: provide fixit support when
c++'s named cast need be replaced for bridge casting.
// rdar://12788838

llvm-svn: 175923
2013-02-22 22:02:53 +00:00
John McCall 6ced97aaae Diagnose loads of 'half' l-values in OpenCL.
Patch by Joey Gouly!

llvm-svn: 174928
2013-02-12 01:29:43 +00:00
Joey Gouly dd7f4566b1 Add a new LangOpt NativeHalfType. This option allows for native half/fp16
operations (as opposed to storage only half/fp16).

Also add some semantic checks for OpenCL half types.

llvm-svn: 173254
2013-01-23 11:56:20 +00:00
Richard Smith 2bf7fdb723 s/CPlusPlus0x/CPlusPlus11/g
llvm-svn: 171367
2013-01-02 11:42:31 +00:00
Fariborz Jahanian b487388be1 objc: DOn't complain if a (SEL) expression is typecast
to (SEL). Fixes // rdar://12859590

llvm-svn: 170058
2012-12-13 00:42:06 +00:00
Chandler Carruth 3a02247dc9 Sort all of Clang's files under 'lib', and fix up the broken headers
uncovered.

This required manually correcting all of the incorrect main-module
headers I could find, and running the new llvm/utils/sort_includes.py
script over the files.

I also manually added quite a few missing headers that were uncovered by
shuffling the order or moving headers up to be main-module-headers.

llvm-svn: 169237
2012-12-04 09:13:33 +00:00
David Blaikie 282ad876bd Implement GCC's -Wint-to-pointer-cast.
This implementation doesn't warn on anything that GCC doesn't warn on with the
exception of templates specializations (GCC doesn't warn, Clang does). The
specific skipped cases (boolean, constant expressions, enums) are open for
debate/adjustment if anyone wants to demonstrate that GCC is being overly
conservative here. The only really obvious false positive I found was in the
Clang regression suite's MPI test - apparently MPI uses specific flag values in
pointer constants. (eg: #define FOO (void*)~0)

llvm-svn: 166039
2012-10-16 18:53:14 +00:00
Benjamin Kramer cc4c49dd63 Now that ASTMultiPtr is nothing more than a array reference, make it a MutableArrayRef.
This required changing all get() calls to data() and using the simpler constructors.

llvm-svn: 162501
2012-08-23 23:38:35 +00:00
Benjamin Kramer 62b95d88dc Rip out remnants of move semantic emulation and smart pointers in Sema.
These were nops for quite a while and only lead to confusion. ASTMultiPtr
now behaves like a proper dumb array reference.

llvm-svn: 162475
2012-08-23 21:35:17 +00:00
Fariborz Jahanian 91f548b04b c: implement gcc's -Wbad-function-cast which warns
on unsafe cast of a c-function call. This is
a C-only option.

llvm-svn: 162109
2012-08-17 17:22:34 +00:00
Fariborz Jahanian 5ad9659688 objective-C: deprecate casts of ObjC's SEL
expressions except to void, void * and their
qualified versions. // rdar://12107381

llvm-svn: 162036
2012-08-16 18:33:47 +00:00