mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-17 04:16:46 +00:00

Without this patch, clang will not wrap in an ElaboratedType node types written without a keyword and nested name qualifier, which goes against the intent that we should produce an AST which retains enough details to recover how things are written. The lack of this sugar is incompatible with the intent of the type printer default policy, which is to print types as written, but to fall back and print them fully qualified when they are desugared. An ElaboratedTypeLoc without keyword / NNS uses no storage by itself, but still requires pointer alignment due to pre-existing bug in the TypeLoc buffer handling. --- Troubleshooting list to deal with any breakage seen with this patch: 1) The most likely effect one would see by this patch is a change in how a type is printed. The type printer will, by design and default, print types as written. There are customization options there, but not that many, and they mainly apply to how to print a type that we somehow failed to track how it was written. This patch fixes a problem where we failed to distinguish between a type that was written without any elaborated-type qualifiers, such as a 'struct'/'class' tags and name spacifiers such as 'std::', and one that has been stripped of any 'metadata' that identifies such, the so called canonical types. Example: ``` namespace foo { struct A {}; A a; }; ``` If one were to print the type of `foo::a`, prior to this patch, this would result in `foo::A`. This is how the type printer would have, by default, printed the canonical type of A as well. As soon as you add any name qualifiers to A, the type printer would suddenly start accurately printing the type as written. This patch will make it print it accurately even when written without qualifiers, so we will just print `A` for the initial example, as the user did not really write that `foo::` namespace qualifier. 2) This patch could expose a bug in some AST matcher. Matching types is harder to get right when there is sugar involved. For example, if you want to match a type against being a pointer to some type A, then you have to account for getting a type that is sugar for a pointer to A, or being a pointer to sugar to A, or both! Usually you would get the second part wrong, and this would work for a very simple test where you don't use any name qualifiers, but you would discover is broken when you do. The usual fix is to either use the matcher which strips sugar, which is annoying to use as for example if you match an N level pointer, you have to put N+1 such matchers in there, beginning to end and between all those levels. But in a lot of cases, if the property you want to match is present in the canonical type, it's easier and faster to just match on that... This goes with what is said in 1), if you want to match against the name of a type, and you want the name string to be something stable, perhaps matching on the name of the canonical type is the better choice. 3) This patch could expose a bug in how you get the source range of some TypeLoc. For some reason, a lot of code is using getLocalSourceRange(), which only looks at the given TypeLoc node. This patch introduces a new, and more common TypeLoc node which contains no source locations on itself. This is not an inovation here, and some other, more rare TypeLoc nodes could also have this property, but if you use getLocalSourceRange on them, it's not going to return any valid locations, because it doesn't have any. The right fix here is to always use getSourceRange() or getBeginLoc/getEndLoc which will dive into the inner TypeLoc to get the source range if it doesn't find it on the top level one. You can use getLocalSourceRange if you are really into micro-optimizations and you have some outside knowledge that the TypeLocs you are dealing with will always include some source location. 4) Exposed a bug somewhere in the use of the normal clang type class API, where you have some type, you want to see if that type is some particular kind, you try a `dyn_cast` such as `dyn_cast<TypedefType>` and that fails because now you have an ElaboratedType which has a TypeDefType inside of it, which is what you wanted to match. Again, like 2), this would usually have been tested poorly with some simple tests with no qualifications, and would have been broken had there been any other kind of type sugar, be it an ElaboratedType or a TemplateSpecializationType or a SubstTemplateParmType. The usual fix here is to use `getAs` instead of `dyn_cast`, which will look deeper into the type. Or use `getAsAdjusted` when dealing with TypeLocs. For some reason the API is inconsistent there and on TypeLocs getAs behaves like a dyn_cast. 5) It could be a bug in this patch perhaps. Let me know if you need any help! Signed-off-by: Matheus Izvekov <mizvekov@gmail.com> Differential Revision: https://reviews.llvm.org/D112374
323 lines
8.9 KiB
C++
323 lines
8.9 KiB
C++
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
|
|
// REQUIRES: LP64
|
|
|
|
// ------------ not interpreted as C-style cast ------------
|
|
|
|
struct SimpleValueInit {
|
|
int i;
|
|
};
|
|
|
|
struct InitViaConstructor {
|
|
InitViaConstructor(int i = 7);
|
|
};
|
|
|
|
struct NoValueInit { // expected-note 2 {{candidate constructor (the implicit copy constructor)}} expected-note 2 {{candidate constructor (the implicit move constructor)}}
|
|
NoValueInit(int i, int j); // expected-note 2 {{candidate constructor}}
|
|
};
|
|
|
|
void test_cxx_functional_value_init() {
|
|
(void)SimpleValueInit();
|
|
(void)InitViaConstructor();
|
|
(void)NoValueInit(); // expected-error{{no matching constructor for initialization}}
|
|
}
|
|
|
|
void test_cxx_function_cast_multi() {
|
|
(void)NoValueInit(0, 0);
|
|
(void)NoValueInit(0, 0, 0); // expected-error{{no matching constructor for initialization}}
|
|
(void)int(1, 2); // expected-error{{excess elements in scalar initializer}}
|
|
(void)int({}, 2); // expected-error{{excess elements in scalar initializer}}
|
|
}
|
|
|
|
|
|
// ------------------ everything else --------------------
|
|
|
|
struct A {};
|
|
|
|
// ----------- const_cast --------------
|
|
|
|
typedef char c;
|
|
typedef c *cp;
|
|
typedef cp *cpp;
|
|
typedef cpp *cppp;
|
|
typedef cppp &cpppr;
|
|
typedef const cppp &cpppcr;
|
|
typedef const char cc;
|
|
typedef cc *ccp;
|
|
typedef volatile ccp ccvp;
|
|
typedef ccvp *ccvpp;
|
|
typedef const volatile ccvpp ccvpcvp;
|
|
typedef ccvpcvp *ccvpcvpp;
|
|
typedef int iar[100];
|
|
typedef iar &iarr;
|
|
typedef int (*f)(int);
|
|
|
|
void t_cc()
|
|
{
|
|
ccvpcvpp var = 0;
|
|
// Cast away deep consts and volatiles.
|
|
char ***var2 = cppp(var);
|
|
char ***const &var3 = var2;
|
|
// Const reference to reference.
|
|
char ***&var4 = cpppr(var3);
|
|
// Drop reference. Intentionally without qualifier change.
|
|
char *** var5 = cppp(var4);
|
|
const int ar[100] = {0};
|
|
// Array decay. Intentionally without qualifier change.
|
|
typedef int *intp;
|
|
int *pi = intp(ar);
|
|
f fp = 0;
|
|
// Don't misidentify fn** as a function pointer.
|
|
typedef f *fp_t;
|
|
f *fpp = fp_t(&fp);
|
|
int const A::* const A::*icapcap = 0;
|
|
typedef int A::* A::*iapap_t;
|
|
iapap_t iapap = iapap_t(icapcap);
|
|
}
|
|
|
|
// ----------- static_cast -------------
|
|
|
|
struct B : public A {}; // Single public base.
|
|
struct C1 : public virtual B {}; // Single virtual base.
|
|
struct C2 : public virtual B {};
|
|
struct D : public C1, public C2 {}; // Diamond
|
|
struct E : private A {}; // Single private base.
|
|
struct F : public C1 {}; // Single path to B with virtual.
|
|
struct G1 : public B {};
|
|
struct G2 : public B {};
|
|
struct H : public G1, public G2 {}; // Ambiguous path to B.
|
|
|
|
enum Enum { En1, En2 };
|
|
enum Onom { On1, On2 };
|
|
|
|
struct Co1 { operator int(); };
|
|
struct Co2 { Co2(int); };
|
|
struct Co3 { };
|
|
struct Co4 { Co4(Co3); operator Co3(); };
|
|
|
|
// Explicit implicits
|
|
void t_529_2()
|
|
{
|
|
int i = 1;
|
|
(void)float(i);
|
|
double d = 1.0;
|
|
(void)float(d);
|
|
(void)int(d);
|
|
(void)char(i);
|
|
typedef unsigned long ulong;
|
|
(void)ulong(i);
|
|
(void)int(En1);
|
|
(void)double(En1);
|
|
typedef int &intr;
|
|
(void)intr(i);
|
|
typedef const int &cintr;
|
|
(void)cintr(i);
|
|
|
|
int ar[1];
|
|
typedef const int *cintp;
|
|
(void)cintp(ar);
|
|
typedef void (*pfvv)();
|
|
(void)pfvv(t_529_2);
|
|
|
|
typedef void *voidp;
|
|
(void)voidp(0);
|
|
(void)voidp((int*)0);
|
|
typedef volatile const void *vcvoidp;
|
|
(void)vcvoidp((const int*)0);
|
|
typedef A *Ap;
|
|
(void)Ap((B*)0);
|
|
typedef A &Ar;
|
|
(void)Ar(*((B*)0)); // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
|
|
typedef const B *cBp;
|
|
(void)cBp((C1*)0);
|
|
typedef B &Br;
|
|
(void)Br(*((C1*)0)); // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
|
|
(void)Ap((D*)0);
|
|
typedef const A &cAr;
|
|
(void)cAr(*((D*)0)); // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
|
|
typedef int B::*Bmp;
|
|
(void)Bmp((int A::*)0);
|
|
typedef void (B::*Bmfp)();
|
|
(void)Bmfp((void (A::*)())0);
|
|
(void)Ap((E*)0); // functional-style cast ignores access control
|
|
(void)voidp((const int*)0); // const_cast appended
|
|
|
|
(void)int(Co1());
|
|
(void)Co2(1);
|
|
(void)Co3((Co4)(Co3()));
|
|
|
|
// Bad code below
|
|
//(void)(A*)((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}}
|
|
}
|
|
|
|
// Anything to void
|
|
void t_529_4()
|
|
{
|
|
void(1);
|
|
(void(t_529_4));
|
|
}
|
|
|
|
// Static downcasts
|
|
void t_529_5_8()
|
|
{
|
|
typedef B *Bp;
|
|
(void)Bp((A*)0);
|
|
typedef B &Br;
|
|
(void)Br(*((A*)0));
|
|
typedef const G1 *cG1p;
|
|
(void)cG1p((A*)0);
|
|
typedef const G1 &cG1r;
|
|
(void)cG1r(*((A*)0));
|
|
(void)Bp((const A*)0); // const_cast appended
|
|
(void)Br(*((const A*)0)); // const_cast appended
|
|
typedef E *Ep;
|
|
(void)Ep((A*)0); // access control ignored
|
|
typedef E &Er;
|
|
(void)Er(*((A*)0)); // access control ignored
|
|
|
|
// Bad code below
|
|
|
|
typedef C1 *C1p;
|
|
(void)C1p((A*)0); // expected-error {{cannot cast 'A *' to 'C1p' (aka 'C1 *') via virtual base 'B'}}
|
|
typedef C1 &C1r;
|
|
(void)C1r(*((A*)0)); // expected-error {{cannot cast 'A' to 'C1r' (aka 'C1 &') via virtual base 'B'}}
|
|
typedef D *Dp;
|
|
(void)Dp((A*)0); // expected-error {{cannot cast 'A *' to 'Dp' (aka 'D *') via virtual base 'B'}}
|
|
typedef D &Dr;
|
|
(void)Dr(*((A*)0)); // expected-error {{cannot cast 'A' to 'Dr' (aka 'D &') via virtual base 'B'}}
|
|
typedef H *Hp;
|
|
(void)Hp((A*)0); // expected-error {{ambiguous cast from base 'A' to derived 'H':\n A -> B -> G1 -> struct H\n A -> B -> G2 -> struct H}}
|
|
typedef H &Hr;
|
|
(void)Hr(*((A*)0)); // expected-error {{ambiguous cast from base 'A' to derived 'H':\n A -> B -> G1 -> struct H\n A -> B -> G2 -> struct H}}
|
|
|
|
// TODO: Test DR427. This requires user-defined conversions, though.
|
|
}
|
|
|
|
// Enum conversions
|
|
void t_529_7()
|
|
{
|
|
(void)Enum(1);
|
|
(void)Enum(1.0);
|
|
(void)Onom(En1);
|
|
|
|
// Bad code below
|
|
|
|
(void)Enum((int*)0); // expected-error {{functional-style cast from 'int *' to 'Enum' is not allowed}}
|
|
}
|
|
|
|
// Void pointer to object pointer
|
|
void t_529_10()
|
|
{
|
|
typedef int *intp;
|
|
(void)intp((void*)0);
|
|
typedef const A *cAp;
|
|
(void)cAp((void*)0);
|
|
(void)intp((const void*)0); // const_cast appended
|
|
}
|
|
|
|
// Member pointer upcast.
|
|
void t_529_9()
|
|
{
|
|
typedef int A::*Amp;
|
|
(void)Amp((int B::*)0);
|
|
|
|
// Bad code below
|
|
(void)Amp((int H::*)0); // expected-error {{ambiguous conversion from pointer to member of derived class 'H' to pointer to member of base class 'A':}}
|
|
(void)Amp((int F::*)0); // expected-error {{conversion from pointer to member of class 'F' to pointer to member of class 'A' via virtual base 'B' is not allowed}}
|
|
}
|
|
|
|
// -------- reinterpret_cast -----------
|
|
|
|
enum test { testval = 1 };
|
|
struct structure { int m; };
|
|
typedef void (*fnptr)();
|
|
|
|
// Test conversion between pointer and integral types, as in p3 and p4.
|
|
void integral_conversion()
|
|
{
|
|
typedef void *voidp;
|
|
void *vp = voidp(testval);
|
|
long l = long(vp);
|
|
typedef float *floatp;
|
|
(void)floatp(l);
|
|
fnptr fnp = fnptr(l);
|
|
(void)char(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}}
|
|
(void)long(fnp);
|
|
}
|
|
|
|
void pointer_conversion()
|
|
{
|
|
int *p1 = 0;
|
|
typedef float *floatp;
|
|
float *p2 = floatp(p1);
|
|
typedef structure *structurep;
|
|
structure *p3 = structurep(p2);
|
|
typedef int **ppint;
|
|
typedef ppint *pppint;
|
|
ppint *deep = pppint(p3);
|
|
typedef fnptr fnptrp;
|
|
(void)fnptrp(deep);
|
|
}
|
|
|
|
void constness()
|
|
{
|
|
int ***const ipppc = 0;
|
|
typedef int const *icp_t;
|
|
int const *icp = icp_t(ipppc);
|
|
typedef int *intp;
|
|
(void)intp(icp); // const_cast appended
|
|
typedef int const *const ** intcpcpp;
|
|
intcpcpp icpcpp = intcpcpp(ipppc); // const_cast appended
|
|
int *ip = intp(icpcpp);
|
|
(void)icp_t(ip);
|
|
typedef int const *const *const *intcpcpcp;
|
|
(void)intcpcpcp(ipppc);
|
|
}
|
|
|
|
void fnptrs()
|
|
{
|
|
typedef int (*fnptr2)(int);
|
|
fnptr fp = 0;
|
|
(void)fnptr2(fp);
|
|
typedef void *voidp;
|
|
void *vp = voidp(fp);
|
|
(void)fnptr(vp);
|
|
}
|
|
|
|
void refs()
|
|
{
|
|
long l = 0;
|
|
typedef char &charr;
|
|
char &c = charr(l);
|
|
// Bad: from rvalue
|
|
typedef int &intr;
|
|
(void)intr(&c); // expected-error {{functional-style cast from rvalue to reference type 'intr' (aka 'int &')}}
|
|
}
|
|
|
|
void memptrs()
|
|
{
|
|
const int structure::*psi = 0;
|
|
typedef const float structure::*structurecfmp;
|
|
(void)structurecfmp(psi);
|
|
typedef int structure::*structureimp;
|
|
(void)structureimp(psi); // const_cast appended
|
|
|
|
void (structure::*psf)() = 0;
|
|
typedef int (structure::*structureimfp)();
|
|
(void)structureimfp(psf);
|
|
|
|
typedef void (structure::*structurevmfp)();
|
|
(void)structurevmfp(psi); // expected-error-re {{functional-style cast from 'const int structure::*' to 'structurevmfp' (aka 'void (structure::*)(){{( __attribute__\(\(thiscall\)\))?}}') is not allowed}}
|
|
(void)structureimp(psf); // expected-error-re {{functional-style cast from 'void (structure::*)(){{( __attribute__\(\(thiscall\)\))?}}' to 'structureimp' (aka 'int structure::*') is not allowed}}
|
|
}
|
|
|
|
// ---------------- misc ------------------
|
|
|
|
void crash_on_invalid_1()
|
|
{
|
|
typedef itn Typo; // expected-error {{unknown type name 'itn'}}
|
|
(void)Typo(1); // used to crash
|
|
|
|
typedef int &int_ref;
|
|
(void)int_ref(); // expected-error {{reference to type 'int' requires an initializer}}
|
|
}
|