forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transform.C
492 lines (414 loc) · 17.5 KB
/
Transform.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------
// RUN: cat %s | %cling -fno-rtti | FileCheck %s
// The test verifies the expected behavior in cling::utils::Transform class,
// which is supposed to provide different transformation of AST nodes and types.
#include "cling/Interpreter/Interpreter.h"
#include "cling/Interpreter/LookupHelper.h"
#include "cling/Utils/AST.h"
#include "clang/AST/Type.h"
#include "clang/AST/ASTContext.h"
#include "llvm/ADT/SmallSet.h"
#include "clang/Sema/Sema.h"
.rawInput 1
#include <vector>
#include <iostream>
typedef double Double32_t;
typedef int Int_t;
typedef long Long_t;
typedef Int_t* IntPtr_t;
typedef Int_t& IntRef_t;
template <typename T> class A {};
template <typename T, typename U> class B {};
template <typename T, typename U> class C {};
typedef C<A<B<Double32_t, Int_t> >, Double32_t > CTD;
typedef C<A<B<const Double32_t, const Int_t> >, Double32_t > CTDConst;
template <typename key, typename value, typename compare_operation = std::less<key>, typename alloc = std::allocator<std::pair<const key, value> > > class cmap { key fKey; const value fValue; alloc fAlloc; public: cmap() : fValue(0) {} };
// : public std::map<key, value, compare_operation, alloc> {
template <typename key, typename value = const key> class mypair { public: key fKey; value fValue; };
#include <string>
namespace Details {
class Impl {};
}
// To insure instantiation.
// typedef std::pair<Details::Impl,std::vector<Details::Impl> > details_pairs;
namespace NS {
template <typename T, int size = 0> class ArrayType {};
template <typename T> class Array {};
template <typename T> class Container {
public:
class Content {};
typedef T Value_t;
typedef Content Content_t;
typedef ::Details::Impl Impl_t;
};
template <typename T> class TDataPoint {};
typedef TDataPoint<float> TDataPointF;
typedef TDataPoint<Double32_t> TDataPointD32;
const int typeN =1;
typedef ArrayType<float, typeN + 1> FArray;
typedef int IntNS_t;
}
// Anonymous namespace
namespace {
class InsideAnonymous {
};
}
using namespace std;
class Embedded_objects {
public:
enum Eenum {
kEnumConst=16
};
class EmbeddedClasses;
typedef EmbeddedClasses EmbeddedTypedef;
class EmbeddedClasses {
public:
class Embedded1 {};
class Embedded2 {};
class Embedded3 {};
class Embedded4 {};
class Embedded5 {};
class Embedded6 {};
};
EmbeddedClasses m_embedded;
EmbeddedClasses::Embedded1 m_emb1;
EmbeddedClasses::Embedded2 m_emb2;
EmbeddedClasses::Embedded3 m_emb3;
EmbeddedTypedef::Embedded4 m_emb4;
Embedded_objects::EmbeddedClasses::Embedded5 m_emb5;
Embedded_objects::EmbeddedTypedef::Embedded6 m_emb6;
typedef std::vector<int> vecint;
vecint* m_iter;
const Eenum m_enum;
typedef vector<int> vecint2;
vecint2* m_iter2;
vector<Double32_t> vd32a;
typedef vector<Double32_t> vecd32t1;
vecd32t1 vd32b;
using vecd32t2 = vector<Double32_t>;
vecd32t2 vd32c;
template <typename T> using myvector = std::vector<T>;
myvector<float> vfa;
// Not yet, the desugar of template alias do not keep the opaque typedef.
// myvector<Double32_t> vd32d;
Double32_t *p1;
template<class T> using ptr = T*;
ptr<float> p2;
// Not yet, the desugar of template alias do not keep the opaque typedef.
// ptr<Double32_t> p3;
typedef B<Int_t,Double32_t> t1;
typedef t1* t2;
typedef t2* t3;
typedef t3 t4[3];
typedef t4 t5[4];
typedef t5& t6;
typedef t1& t7;
typedef t2& t8;
typedef t2 t9;
typedef t5* t10;
t1 d1;
t2 d2;
t2 d2_1[5];
t3 d3;
t4 d4;
t5 d5;
t6 d6;
t7 d7;
t8 d8;
t9 d9;
t10 d10;
};
namespace NS1 {
namespace NS2 {
namespace NS3 {
inline namespace InlinedNamespace {
class InsideInline {};
}
class Point {};
class Inner3 {
public:
Point p1;
NS3::Point p2;
::NS1::NS2::NS3::Point p3;
InsideInline p4;
};
}
}
}
.rawInput 0
const cling::LookupHelper& lookup = gCling->getLookupHelper();
cling::LookupHelper::DiagSetting diags = cling::LookupHelper::WithDiagnostics;
clang::Sema* Sema = &gCling->getSema();
const clang::ASTContext& Ctx = gCling->getSema().getASTContext();
cling::utils::Transform::Config transConfig;
using namespace cling::utils;
transConfig.m_toSkip.insert(Lookup::Named(Sema, "Double32_t"));
using namespace std;
transConfig.m_toSkip.insert(Lookup::Named(Sema, "string"));
transConfig.m_toSkip.insert(Lookup::Named(Sema, "string", Lookup::Namespace(Sema, "std")));
const clang::Type* t = 0;
clang::QualType QT;
using namespace cling::utils;
// Test the behavior on a simple class
lookup.findScope("Details::Impl", diags, &t);
QT = clang::QualType(t, 0);
//QT.getAsString().c_str()
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "Details::Impl"
// Test the behavior for a class inside an anonymous namespace
lookup.findScope("InsideAnonymous", diags, &t);
QT = clang::QualType(t, 0);
//QT.getAsString().c_str()c
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "class (anonymous namespace)::InsideAnonymous"
// The above result is not quite want we want, so the client must using
// the following:
// The scope suppression is required for getting rid of the anonymous part of the name of a class defined in an anonymous namespace.
// This gives us more control vs not using the clang::ElaboratedType and relying on the Policy.SuppressUnwrittenScope which would
// strip both the anonymous and the inline namespace names (and we probably do not want the later to be suppressed).
clang::PrintingPolicy Policy(Ctx.getPrintingPolicy());
Policy.SuppressTagKeyword = true; // Never get the class or struct keyword
Policy.SuppressScope = true; // Force the scope to be coming from a clang::ElaboratedType.
std::string name;
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsStringInternal(name,Policy);
name.c_str()
// CHECK: ({{[^)]+}}) "InsideAnonymous"
// Test desugaring pointers types:
QT = lookup.findType("Int_t*", diags);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK:({{[^)]+}}) "int *"
QT = lookup.findType("const IntPtr_t*", diags);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK:({{[^)]+}}) "int *const *"
// Test desugaring reference (both r- or l- value) types:
QT = lookup.findType("const IntPtr_t&", diags);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK:({{[^)]+}}) "int *const &"
//TODO: QT = lookup.findType("IntPtr_t[32], diags");
// To do: findType does not return the const below:
// Test desugaring reference (both r- or l- value) types:
// QT = lookup.findType("const IntRef_t", diags);
// Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// should print:({{[^)]+}}) "int &const"
// but this is actually an illegal type:
// C++ [dcl.ref]p1:
// Cv-qualified references are ill-formed except when the
// cv-qualifiers are introduced through the use of a typedef
// (7.1.3) or of a template type argument (14.3), in which
// case the cv-qualifiers are ignored.
// So the following is the right behavior:
// Will issue
// "'const' qualifier on reference type 'IntRef_t' (aka 'int &') has no effect"
// thus suppress diagnostics
QT = lookup.findType("const IntRef_t", cling::LookupHelper::NoDiagnostics);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "int &"
// Test desugaring reference (both r- or l- value) types:
QT = lookup.findType("IntRef_t", diags);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK:({{[^)]+}}) "int &"
//Desugar template parameters:
lookup.findScope("A<B<Double32_t, Int_t*> >", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK:({{[^)]+}}) "A<B<Double32_t, int *> >"
lookup.findScope("A<B<Double32_t, std::size_t*> >", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK:({{[^)]+}}) "A<B<Double32_t, unsigned {{long|int}} *> >"
lookup.findScope("CTD", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "C<A<B<Double32_t, int> >, Double32_t>"
lookup.findScope("CTDConst", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "C<A<B<const Double32_t, const int> >, Double32_t>"
lookup.findScope("std::pair<const std::string,int>", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "std::pair<const std::string, int>"
lookup.findScope("NS::Array<NS::ArrayType<double> >", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "NS::Array<NS::ArrayType<double> >"
lookup.findScope("NS::Array<NS::ArrayType<Double32_t> >", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "NS::Array<NS::ArrayType<Double32_t> >"
lookup.findScope("NS::Container<Long_t>::Content", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "NS::Container<long>::Content"
QT = lookup.findType("NS::Container<Long_t>::Value_t", diags);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "long"
lookup.findScope("NS::Container<Long_t>::Content_t", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "NS::Container<long>::Content"
lookup.findScope("NS::Container<Long_t>::Impl_t", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "Details::Impl"
lookup.findScope("NS::Container<Double32_t>::Content", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "NS::Container<Double32_t>::Content"
QT = lookup.findType("NS::Container<Double32_t>::Value_t", diags);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "double"
// Really we would want it to say Double32_t but oh well.
lookup.findScope("NS::Container<Double32_t>::Content_t", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "NS::Container<Double32_t>::Content"
lookup.findScope("NS::Container<Double32_t>::Impl_t", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "Details::Impl"
lookup.findScope("NS::TDataPointF", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "NS::TDataPoint<float>"
lookup.findScope("NS::TDataPointD32", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "NS::TDataPoint<Double32_t>"
lookup.findScope("NS::ArrayType<float,1>", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "NS::ArrayType<float, 1>"
lookup.findScope("NS::FArray", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "NS::ArrayType<float, 2>"
QT = lookup.findType("const NS::IntNS_t", diags);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "const int"
lookup.findScope("vector<Details::Impl>::value_type", diags, &t);
QT = clang::QualType(t, 0);
Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str()
// CHECK: ({{[^)]+}}) "Details::Impl"
const clang::Decl*decl=lookup.findScope("Embedded_objects", diags,&t);
if (decl) {
const clang::CXXRecordDecl *cxxdecl
= llvm::dyn_cast<clang::CXXRecordDecl>(decl);
if (cxxdecl) {
clang::DeclContext::decl_iterator iter = cxxdecl->decls_begin();
while ( *iter ) {
const clang::Decl *mdecl = *iter;
if (const clang::ValueDecl *vd = llvm::dyn_cast<clang::ValueDecl>(mdecl)) {
clang::QualType vdType = vd->getType();
name.clear();
Transform::GetPartiallyDesugaredType(Ctx,vdType,transConfig).getAsStringInternal(name,Policy);
std::cout << name.c_str() << std::endl;
}
++iter;
}
}
}
// CHECK: Embedded_objects::EmbeddedClasses
// CHECK: Embedded_objects::EmbeddedClasses::Embedded1
// CHECK: Embedded_objects::EmbeddedClasses::Embedded2
// CHECK: Embedded_objects::EmbeddedClasses::Embedded3
// CHECK: Embedded_objects::EmbeddedClasses::Embedded4
// CHECK: Embedded_objects::EmbeddedClasses::Embedded5
// CHECK: Embedded_objects::EmbeddedClasses::Embedded6
// CHECK: std::vector<int> *
// CHECK: const Embedded_objects::Eenum
// CHECK: std::vector<int> *
// CHECK: std::vector<Double32_t>
// CHECK: std::vector<Double32_t>
// CHECK: std::vector<Double32_t>
// CHECK: std::vector<float>
// NOT-YET-CHECK: std::vector<Double32_t>
// CHECK: Double32_t *
// CHECK: float *
// NOT-YET-CHECK: Double32_t *
// CHECK: B<int, Double32_t>
// CHECK: B<int, Double32_t> *
// CHECK: B<int, Double32_t> *[5]
// CHECK: B<int, Double32_t> **
// CHECK: B<int, Double32_t> **[3]
// CHECK: B<int, Double32_t> **[4][3]
// CHECK: B<int, Double32_t> **(&)[4][3]
// CHECK: B<int, Double32_t> &
// CHECK: B<int, Double32_t> *&
// CHECK: B<int, Double32_t> *
// CHECK: B<int, Double32_t> **(*)[4][3]
// In the partial desugaring add support for the case where we have a type
// that point to an already completely desugared template instantiation in
// which case the type is a RecordDecl rather than a TemplateInstantationType
decl = lookup.findScope("std::pair<Details::Impl,std::vector<Details::Impl> >", diags,&t);
QT = clang::QualType(t, 0);
std::cout << Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str() << std::endl;
// CHECK: std::pair<Details::Impl, std::vector<Details::Impl> >
if (const clang::RecordDecl *rdecl = llvm::dyn_cast_or_null<clang::RecordDecl>(decl)) {
clang::RecordDecl::field_iterator field_iter = rdecl->field_begin();
// For some reason we can not call field_end:
// cling: root/interpreter/llvm/src/tools/clang/lib/CodeGen/CGCall.cpp:1839: void checkArgMatches(llvm::Value*, unsigned int&, llvm::FunctionType*): Assertion `Elt->getType() == FTy->getParamType(ArgNo)' failed.
// so just 'guess' the size
int i = 0;
while( i < 2 ) {
name.clear();
clang::QualType fdType = field_iter->getType();
Transform::GetPartiallyDesugaredType(Ctx,fdType,transConfig).getAsStringInternal(name,Policy);
std::cout << name.c_str() << std::endl;
++field_iter;
++i;
}
}
// CHECK: Details::Impl
// CHECK: std::vector<Details::Impl, std::allocator<Details::Impl> >
decl=lookup.findScope("NS1::NS2::NS3::Inner3", diags,&t);
if (decl) {
const clang::CXXRecordDecl *cxxdecl
= llvm::dyn_cast<clang::CXXRecordDecl>(decl);
if (cxxdecl) {
clang::DeclContext::decl_iterator iter = cxxdecl->decls_begin();
while ( *iter ) {
const clang::Decl *mdecl = *iter;
if (const clang::ValueDecl *vd = llvm::dyn_cast<clang::ValueDecl>(mdecl)) {
clang::QualType vdType = vd->getType();
name.clear();
Transform::GetPartiallyDesugaredType(Ctx,vdType,transConfig).getAsStringInternal(name,Policy);
std::cout << name.c_str() << std::endl;
}
++iter;
}
}
}
// CHECK: NS1::NS2::NS3::Point
// CHECK: NS1::NS2::NS3::Point
// CHECK: NS1::NS2::NS3::Point
decl = lookup.findScope("cmap<volatile int,volatile int>", diags,&t);
QT = clang::QualType(t, 0);
std::cout << Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str() << std::endl;
if (const clang::RecordDecl *rdecl = llvm::dyn_cast_or_null<clang::RecordDecl>(decl)) {
QT = clang::QualType(rdecl->getTypeForDecl(), 0);
std::cout << Transform::GetPartiallyDesugaredType(Ctx, QT, transConfig).getAsString().c_str() << std::endl;
clang::RecordDecl::field_iterator field_iter = rdecl->field_begin();
// For some reason we can not call field_end:
// cling: root/interpreter/llvm/src/tools/clang/lib/CodeGen/CGCall.cpp:1839: void checkArgMatches(llvm::Value*, unsigned int&, llvm::FunctionType*): Assertion `Elt->getType() == FTy->getParamType(ArgNo)' failed.
// so just 'guess' the size
int i = 0;
while( i < 2 ) {
name.clear();
clang::QualType fdType = field_iter->getType();
Transform::GetPartiallyDesugaredType(Ctx,fdType,transConfig).getAsStringInternal(name,Policy);
std::cout << name.c_str() << std::endl;
++field_iter;
++i;
}
}
// CHECK: cmap<volatile int, volatile int>
// CHECK: cmap<volatile int, volatile int, std::less<volatile int>, std::allocator<std::pair<const volatile int, volatile int> > >
// CHECK: volatile int
// CHECK: const volatile int