GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / compilerplugins / clang / store / referencecasting.cxx
blobc957367dc5621433b7b964c0d6e409e1d0658fbd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * Based on LLVM/Clang.
7 * This file is distributed under the University of Illinois Open Source
8 * License. See LICENSE.TXT for details.
12 #include "referencecasting.hxx"
14 #include <clang/AST/Attr.h>
15 #include <iostream>
17 namespace loplugin
21 This is a compile-time checker.
23 Check for cases where we have
24 - two IDL interfaces A and B,
25 - B extends A
26 - we are converting a Reference<B> to a Reference<A>
28 Note that it generates the occasional false positive.
30 Also, it makes clang3.2 crash on about 4 files in the LO codebase.
31 I have logged a bug here:
32 http://llvm.org/bugs/show_bug.cgi?id=15902
36 ReferenceCasting::ReferenceCasting( CompilerInstance& compiler )
37 : Plugin( compiler )
41 void ReferenceCasting::run()
43 TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
46 // This:
47 // static void example_method()
48 // {
49 // css::uno::Reference<B> b;
50 // css::uno::Reference<A>(b, css::uno::UNO_QUERY);
51 // }
52 // Compiles to this AST:
53 // (CompoundStmt 0x205d430 </noel-extra1/libo-clang/compilerplugins/clang/noel1.cxx:17:1, line:20:1>
54 // (DeclStmt 0x20580a8 <line:18:5, col:32>
55 // (0x20530e0 "css::uno::Reference<B> refB =
56 // (CXXConstructExpr 0x2058078 <col:28> 'css::uno::Reference<B>':'class com::sun::star::uno::Reference<class B>''void (void)')"))
57 // (DeclStmt 0x205d418 <line:19:5, col:59>
58 // (0x2058310 "css::uno::Reference<A> refA =
59 // (CXXConstructExpr 0x205d3d8 <col:28, col:58> 'css::uno::Reference<A>':'class com::sun::star::uno::Reference<class A>''void (const class com::sun::star::uno::BaseReference &, enum com::sun::star::uno::UnoReference_Query)'
60 // (ImplicitCastExpr 0x205d3c0 <col:33> 'const class com::sun::star::uno::BaseReference' lvalue <NoOp>
61 // (ImplicitCastExpr 0x205d3a0 <col:33> 'class com::sun::star::uno::BaseReference' lvalue <DerivedToBase (BaseReference)>
62 // (DeclRefExpr 0x20582a0 <col:33> 'css::uno::Reference<B>':'class com::sun::star::uno::Reference<class B>' lvalue Var 0x20530e0 'refB' 'css::uno::Reference<B>':'class com::sun::star::uno::Reference<class B>')))
63 // (DeclRefExpr 0x2058398 <col:39, col:49> 'enum com::sun::star::uno::UnoReference_Query' EnumConstant 0x1831de0 'UNO_QUERY' 'enum com::sun::star::uno::UnoReference_Query'))")))
66 // This:
67 // static void example_method1(css::uno::Reference<A>)
68 // {
69 // }
70 // static void example_method2()
71 // {
72 // css::uno::Reference<B> refB;
73 // example_method1(css::uno::Reference<A>(refB, css::uno::UNO_QUERY));
74 // }
75 // Compiles to this AST:
76 // static void example_method1(css::uno::Reference<A>) (CompoundStmt 0x2a74ee8 </noel-extra1/libo-clang/compilerplugins/clang/noel1.cxx:17:1, line:18:1>)
77 // static void example_method2() (CompoundStmt 0x2a7a650 </noel-extra1/libo-clang/compilerplugins/clang/noel1.cxx:21:1, line:24:1>
78 // (DeclStmt 0x2a7a1a8 <line:22:5, col:32>
79 // (0x2a751e0 "css::uno::Reference<B> refB =
80 // (CXXConstructExpr 0x2a7a178 <col:28> 'css::uno::Reference<B>':'class com::sun::star::uno::Reference<class B>''void (void)')"))
81 // (ExprWithCleanups 0x2a7a638 <line:23:5, col:70> 'void'
82 // (CallExpr 0x2a7a570 <col:5, col:70> 'void'
83 // (ImplicitCastExpr 0x2a7a558 <col:5> 'void (*)(css::uno::Reference<A>)' <FunctionToPointerDecay>
84 // (DeclRefExpr 0x2a7a4d8 <col:5> 'void (css::uno::Reference<A>)' lvalue Function 0x2a6ff00 'example_method1' 'void (css::uno::Reference<A>)'))
85 // (CXXBindTemporaryExpr 0x2a7a618 <col:21, col:69> 'css::uno::Reference<A>':'class com::sun::star::uno::Reference<class A>' (CXXTemporary 0x2a7a610)
86 // (CXXConstructExpr 0x2a7a5d8 <col:21, col:69> 'css::uno::Reference<A>':'class com::sun::star::uno::Reference<class A>''void (const Reference<class A> &)' elidable
87 // (MaterializeTemporaryExpr 0x2a7a5c0 <col:21, col:69> 'const Reference<class A>':'const class com::sun::star::uno::Reference<class A>' lvalue
88 // (ImplicitCastExpr 0x2a7a5a8 <col:21, col:69> 'const Reference<class A>':'const class com::sun::star::uno::Reference<class A>' <NoOp>
89 // (CXXBindTemporaryExpr 0x2a7a4b8 <col:21, col:69> 'css::uno::Reference<A>':'class com::sun::star::uno::Reference<class A>' (CXXTemporary 0x2a7a4b0)
90 // (CXXTemporaryObjectExpr 0x2a7a460 <col:21, col:69> 'css::uno::Reference<A>':'class com::sun::star::uno::Reference<class A>''void (const class com::sun::star::uno::BaseReference &, enum com::sun::star::uno::UnoReference_Query)'
91 // (ImplicitCastExpr 0x2a7a448 <col:44> 'const class com::sun::star::uno::BaseReference' lvalue <NoOp>
92 // (ImplicitCastExpr 0x2a7a428 <col:44> 'class com::sun::star::uno::BaseReference' lvalue <DerivedToBase (BaseReference)>
93 // (DeclRefExpr 0x2a7a340 <col:44> 'css::uno::Reference<B>':'class com::sun::star::uno::Reference<class B>' lvalue Var 0x2a751e0 'refB' 'css::uno::Reference<B>':'class com::sun::star::uno::Reference<class B>')))
94 // (DeclRefExpr 0x2a7a398 <col:50, col:60> 'enum com::sun::star::uno::UnoReference_Query' EnumConstant 0x224ee20 'UNO_QUERY' 'enum com::sun::star::uno::UnoReference_Query'))))))))))
96 static const Type* extractTemplateType(Expr* cce);
98 bool ReferenceCasting::VisitCXXConstructExpr( CXXConstructExpr* cce )
100 // don't bother processing anything in the Reference.h file. Makes my life easier when debugging this.
101 if( compiler.getSourceManager().getFilename( cce->getSourceRange().getBegin() ).find( "Reference.h" ) != StringRef::npos )
102 return true;
104 // look for calls to the Reference<T>(x,UNO_something) constructor
105 if( cce->getConstructor()->getNameInfo().getName().getAsString() != "Reference" )
106 return true;
108 if( cce->getNumArgs() != 2 )
109 return true;
111 // extract the type parameter passed to the template
112 const Type * templateParamType = extractTemplateType(cce);
113 if ( !templateParamType )
114 return true;
116 // extract the type of the first parameter passed to the constructor
117 Expr* constructorArg0 = cce->getArg(0);
118 if( !constructorArg0 )
119 return true;
121 // ignore the Reference(XInterface*,...) constructor
122 if( constructorArg0->getType()->isPointerType() )
123 return true;
125 // drill down the expression tree till we hit the bottom
126 DeclRefExpr* constructorSubArg2;
127 Expr* constructorArg0SubExpr = constructorArg0;
128 for(;;)
130 // if we've hit the member expression we want, break
131 constructorSubArg2 = dyn_cast<DeclRefExpr>( constructorArg0SubExpr );
132 if( constructorSubArg2 )
133 break;
134 CastExpr* tmp1 = dyn_cast<CastExpr>( constructorArg0SubExpr );
135 if( tmp1 ) {
136 constructorArg0SubExpr = tmp1->getSubExpr();
137 continue;
139 MaterializeTemporaryExpr* tmp2 = dyn_cast<MaterializeTemporaryExpr>( constructorArg0SubExpr );
140 if( tmp2 ) {
141 constructorArg0SubExpr = tmp2->GetTemporaryExpr();
142 continue;
144 CXXBindTemporaryExpr* tmp3 = dyn_cast<CXXBindTemporaryExpr>( constructorArg0SubExpr );
145 if( tmp3 ) {
146 constructorArg0SubExpr = tmp3->getSubExpr();
147 continue;
149 CXXTemporaryObjectExpr* tmp4 = dyn_cast<CXXTemporaryObjectExpr>( constructorArg0SubExpr );
150 if( tmp4 ) {
151 constructorArg0SubExpr = tmp4->getArg(0);
152 continue;
154 return true;
157 const Type * tmp3 = extractTemplateType( constructorSubArg2 );
158 if ( !tmp3 )
159 return true;
161 const RecordType* templateParamRT = dyn_cast<RecordType>( templateParamType );
162 const RecordType* constructorArgRT = dyn_cast<RecordType>( tmp3 );
163 if( !templateParamRT || !constructorArgRT )
164 return true;
166 CXXRecordDecl* templateParamRD = dyn_cast<CXXRecordDecl>( templateParamRT->getDecl() );
167 CXXRecordDecl* constructorArgRD = dyn_cast<CXXRecordDecl>( constructorArgRT->getDecl() );
169 if (constructorArgRD->Equals(templateParamRD) || constructorArgRD->isDerivedFrom(templateParamRD))
170 report( DiagnosticsEngine::Warning,
171 "the source reference is already a subtype of the destination reference",
172 cce->getLocStart()) // and the exact position where the message should point
173 << cce->getSourceRange(); // and the full return statement to highlight (optional)
175 return true;
178 static const Type* extractTemplateType(Expr* cce)
180 QualType cceQT = cce->getType();
181 const Type* cceType = cceQT.getTypePtr();
182 const TemplateSpecializationType* cceTST = dyn_cast<TemplateSpecializationType>( cceType );
183 if( !cceTST )
184 return NULL;
185 if( cceTST->getNumArgs() != 1 )
186 return NULL;
187 const TemplateArgument & cceTA = cceTST->getArg(0);
188 QualType templateParamQT = cceTA.getAsType();
189 return templateParamQT.getTypePtr();
193 static Plugin::Registration< ReferenceCasting > X( "referencecasting" );
195 } // namespace
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */