forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 2
/
LinkdefReader.cxx
1041 lines (922 loc) · 41.2 KB
/
LinkdefReader.cxx
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @(#)root/core/utils:$Id: LinkdefReader.cxx 41697 2011-11-01 21:03:41Z pcanal $
// Author: Velislava Spasova September 2010
/*************************************************************************
* Copyright (C) 1995-2011, Rene Brun, Fons Rademakers and al. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
//////////////////////////////////////////////////////////////////////////
// //
// LinkdefReader //
// //
// //
// Note: some inconsistency in the way CINT parsed the #pragma: //
// "#pragma link C++ class" is terminated by either a ';' or a newline//
// which ever come first and does NOT support line continuation. //
// "#pragma read ..." is terminated by newline but support line //
// continuation (i.e. '\' followed by newline means to also use the//
// next line. //
// This was change in CINT to consistently ignore the continuation //
// //
// //
//////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <memory>
#include "LinkdefReader.h"
#include "SelectionRules.h"
#include "RConversionRuleParser.h"
#include "llvm/Support/raw_ostream.h"
#include "clang/AST/ASTContext.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/Pragma.h"
#include "cling/Interpreter/CIFactory.h"
#include "cling/Interpreter/Interpreter.h"
std::map<std::string, LinkdefReader::EPragmaNames> LinkdefReader::fgMapPragmaNames;
std::map<std::string, LinkdefReader::ECppNames> LinkdefReader::fgMapCppNames;
struct LinkdefReader::Options {
Options() : fNoStreamer(0), fNoInputOper(0), fUseByteCount(0), fVersionNumber(-1) {}
int fNoStreamer;
int fNoInputOper;
union {
int fUseByteCount;
int fRequestStreamerInfo;
};
int fVersionNumber;
};
/*
This is a static function - which in our context means it is populated only ones
*/
void LinkdefReader::PopulatePragmaMap()
{
if (!(fgMapPragmaNames.empty())) return; // if the map has already been populated, return, else populate it
LinkdefReader::fgMapPragmaNames["TClass"] = kClass;
LinkdefReader::fgMapPragmaNames["class"] = kClass;
LinkdefReader::fgMapPragmaNames["typedef"] = kTypeDef;
LinkdefReader::fgMapPragmaNames["namespace"] = kNamespace;
LinkdefReader::fgMapPragmaNames["function"] = kFunction;
LinkdefReader::fgMapPragmaNames["global"] = kGlobal;
LinkdefReader::fgMapPragmaNames["enum"] = kEnum;
LinkdefReader::fgMapPragmaNames["union"] = kUnion;
LinkdefReader::fgMapPragmaNames["struct"] = kStruct;
LinkdefReader::fgMapPragmaNames["all"] = kAll;
LinkdefReader::fgMapPragmaNames["defined_in"] = kDefinedIn;
LinkdefReader::fgMapPragmaNames["ioctortype"] = kIOCtorType;
LinkdefReader::fgMapPragmaNames["nestedclass"] = kNestedclasses;
LinkdefReader::fgMapPragmaNames["nestedclasses"] = kNestedclasses;
LinkdefReader::fgMapPragmaNames["nestedclasses;"] = kNestedclasses;
LinkdefReader::fgMapPragmaNames["operators"] = kOperators;
LinkdefReader::fgMapPragmaNames["operator"] = kOperators;
// The following are listed here so we can officially ignore them
LinkdefReader::fgMapPragmaNames["nestedtypedefs"] = kIgnore;
LinkdefReader::fgMapPragmaNames["nestedtypedef"] = kIgnore;
}
void LinkdefReader::PopulateCppMap()
{
if (!(fgMapCppNames.empty())) return; // if the map has already been populated, return, else populate it
LinkdefReader::fgMapCppNames["#pragma"] = kPragma;
LinkdefReader::fgMapCppNames["#ifdef"] = kIfdef;
LinkdefReader::fgMapCppNames["#endif"] = kEndif;
LinkdefReader::fgMapCppNames["#if"] = kIf;
LinkdefReader::fgMapCppNames["#else"] = kElse;
}
LinkdefReader::LinkdefReader(cling::Interpreter &interp,
ROOT::TMetaUtils::RConstructorTypes &IOConstructorTypes):
fLine(1), fCount(0), fSelectionRules(nullptr), fIOConstructorTypesPtr(&IOConstructorTypes), fInterp(interp)
{
PopulatePragmaMap();
PopulateCppMap();
}
/*
* The method records that 'include' has been explicitly requested in the linkdef file
* to be added to the dictionary and interpreter.
*/
bool LinkdefReader::AddInclude(const std::string& include)
{
fIncludes += "#include ";
fIncludes += include;
fIncludes += "\n";
return true;
}
/*
* The method that processes the pragma statement.
* Sometimes I had to do strange things to reflect the strange behavior of rootcint
*/
bool LinkdefReader::AddRule(const std::string& ruletype,
const std::string& identifier,
bool linkOn,
bool request_only_tclass,
LinkdefReader::Options *options /* = 0 */)
{
EPragmaNames name = kUnknown;
ROOT::TMetaUtils::Info("LinkdefReader::AddRule", "Ruletype is %s with the identifier %s\n", ruletype.c_str(), identifier.c_str());
auto it = fgMapPragmaNames.find(ruletype);
if (it != fgMapPragmaNames.end()) {
name = it->second;
}
switch (name) {
case kAll:
if (identifier == "globals" || identifier == "global") {
VariableSelectionRule vsr(fCount++, fInterp);
if (linkOn) {
vsr.SetAttributeValue("pattern", "*");
vsr.SetSelected(BaseSelectionRule::kYes);
fSelectionRules->AddVariableSelectionRule(vsr);
} else {
if (fSelectionRules->GetHasFileNameRule()) { // only if we had previous defined_in -> create that
// we don't create anything which is OK - if I don't have a selection rule for something
// this something will not be generated
// This is valid also for the other all ... cases
vsr.SetAttributeValue("pattern", "*");
vsr.SetSelected(BaseSelectionRule::kNo);
fSelectionRules->AddVariableSelectionRule(vsr);
}
}
//else vsr.SetSelected(BaseSelectionRule::kNo);
//fSelectionRules->AddVariableSelectionRule(vsr);
EnumSelectionRule esr(fCount++, fInterp);
if (linkOn) {
esr.SetSelected(BaseSelectionRule::kYes);
esr.SetAttributeValue("pattern", "*");
fSelectionRules->AddEnumSelectionRule(esr);
//EnumSelectionRule esr2; //Problem wih the enums - if I deselect them here
EnumSelectionRule esr2(fCount++, fInterp);
esr2.SetSelected(BaseSelectionRule::kNo);
esr2.SetAttributeValue("pattern", "*::*");
fSelectionRules->AddEnumSelectionRule(esr2);
} else {
if (fSelectionRules->GetHasFileNameRule()) {
esr.SetAttributeValue("pattern", "*");
esr.SetSelected(BaseSelectionRule::kNo);
fSelectionRules->AddEnumSelectionRule(esr);
}
}
} else if (identifier == "functions" || identifier == "function") {
FunctionSelectionRule fsr(fCount++, fInterp);
fsr.SetAttributeValue("pattern", "*");
if (linkOn) {
fsr.SetSelected(BaseSelectionRule::kYes);
fSelectionRules->AddFunctionSelectionRule(fsr);
} else {
if (fSelectionRules->GetHasFileNameRule()) {
fsr.SetSelected(BaseSelectionRule::kNo);
fSelectionRules->AddFunctionSelectionRule(fsr);
}
}
} else if (identifier == "classes" || identifier == "namespaces" ||
identifier == "class" || identifier == "namespace") {
if (linkOn) {
ClassSelectionRule csr3(fCount++, fInterp);
csr3.SetSelected(BaseSelectionRule::kNo);
csr3.SetAttributeValue("pattern", "__va_*"); // don't generate for the built-in classes/structs
fSelectionRules->AddClassSelectionRule(csr3);
ClassSelectionRule csr(fCount++, fInterp), csr2(fCount++, fInterp);
csr.SetAttributeValue("pattern", "*");
csr2.SetAttributeValue("pattern", "*::*");
csr.SetSelected(BaseSelectionRule::kYes);
csr2.SetSelected(BaseSelectionRule::kYes);
fSelectionRules->AddClassSelectionRule(csr);
fSelectionRules->AddClassSelectionRule(csr2);
} else {
if (fSelectionRules->GetHasFileNameRule()) {
ClassSelectionRule csr(fCount++, fInterp), csr2(fCount++, fInterp);
csr.SetAttributeValue("pattern", "*");
csr2.SetAttributeValue("pattern", "*::*");
csr.SetSelected(BaseSelectionRule::kNo);
csr2.SetSelected(BaseSelectionRule::kNo);
fSelectionRules->AddClassSelectionRule(csr);
fSelectionRules->AddClassSelectionRule(csr2);
}
}
} else if (identifier == "typedefs" || identifier == "typedef") {
// Silently ignore
} else {
ROOT::TMetaUtils::Warning("Unimplemented pragma statement: %s\n",identifier.c_str());
return false;
}
break;
case kNestedclasses: {
// we don't really process that one
}
break;
case kDefinedIn: {
fSelectionRules->SetHasFileNameRule(true);
// add selection rules for everything
std::string localIdentifier(identifier);
if (localIdentifier.length() && localIdentifier[0] == '"' && localIdentifier[localIdentifier.length() - 1] == '"') {
localIdentifier = localIdentifier.substr(1, localIdentifier.length() - 2);
}
VariableSelectionRule vsr(fCount++, fInterp);
vsr.SetAttributeValue("pattern", "*");
vsr.SetAttributeValue("file_name", localIdentifier);
if (linkOn) vsr.SetSelected(BaseSelectionRule::kYes);
else vsr.SetSelected(BaseSelectionRule::kNo);
fSelectionRules->AddVariableSelectionRule(vsr);
EnumSelectionRule esr(fCount++, fInterp);
esr.SetAttributeValue("pattern", "*");
esr.SetAttributeValue("file_name", localIdentifier);
if (linkOn) esr.SetSelected(BaseSelectionRule::kYes);
else esr.SetSelected(BaseSelectionRule::kNo);
fSelectionRules->AddEnumSelectionRule(esr);
FunctionSelectionRule fsr(fCount++, fInterp);
fsr.SetAttributeValue("pattern", "*");
fsr.SetAttributeValue("file_name", localIdentifier);
if (linkOn) fsr.SetSelected(BaseSelectionRule::kYes);
else fsr.SetSelected(BaseSelectionRule::kNo);
fSelectionRules->AddFunctionSelectionRule(fsr);
ClassSelectionRule csr(fCount++, fInterp), csr2(fCount++, fInterp);
csr.SetAttributeValue("pattern", "*");
csr2.SetAttributeValue("pattern", "*::*");
csr.SetAttributeValue("file_name", localIdentifier);
csr2.SetAttributeValue("file_name", localIdentifier);
if (linkOn) {
csr.SetSelected(BaseSelectionRule::kYes);
csr2.SetSelected(BaseSelectionRule::kYes);
} else {
csr.SetSelected(BaseSelectionRule::kNo);
csr2.SetSelected(BaseSelectionRule::kNo);
}
csr.SetRequestStreamerInfo(true);
csr2.SetRequestStreamerInfo(true);
fSelectionRules->AddClassSelectionRule(csr);
fSelectionRules->AddClassSelectionRule(csr2);
}
break;
case kFunction: {
std::string localIdentifier(identifier);
bool name_or_proto = false; // if true = name, if flase = proto_name
if (!ProcessFunctionPrototype(localIdentifier, name_or_proto)) {
return false;
}
FunctionSelectionRule fsr(fCount++, fInterp);
if (linkOn) fsr.SetSelected(BaseSelectionRule::kYes);
else fsr.SetSelected(BaseSelectionRule::kNo);
if (localIdentifier.at(localIdentifier.length() - 1) == '*') fsr.SetAttributeValue("pattern", localIdentifier);
else if (name_or_proto) fsr.SetAttributeValue("name", localIdentifier);
else {
int pos = localIdentifier.find("(*)"); //rootcint generates error here but I decided to implement that pattern
if (pos > -1) fsr.SetAttributeValue("proto_pattern", localIdentifier);
else {
// No multiline
ROOT::TMetaUtils::ReplaceAll(localIdentifier, "\\\n", "", true);
// Types: We do not do IO of functions, so it is safe to
// put in some heuristics
ROOT::TMetaUtils::ReplaceAll(localIdentifier, "ULong_t", "unsigned long");
ROOT::TMetaUtils::ReplaceAll(localIdentifier, "Long_t", "long");
ROOT::TMetaUtils::ReplaceAll(localIdentifier, "Int_t", "int");
// Remove space after/before the commas if any
ROOT::TMetaUtils::ReplaceAll(localIdentifier, ", ", ",", true);
ROOT::TMetaUtils::ReplaceAll(localIdentifier, " ,", ",", true);
// Remove any space before/after the ( as well
ROOT::TMetaUtils::ReplaceAll(localIdentifier, " (", "(", true);
ROOT::TMetaUtils::ReplaceAll(localIdentifier, "( ", "(", true);
ROOT::TMetaUtils::ReplaceAll(localIdentifier, " )", ")", true);
fsr.SetAttributeValue("proto_name", localIdentifier);
}
}
fSelectionRules->AddFunctionSelectionRule(fsr);
}
break;
case kOperators: {
std::string localIdentifier(identifier);
if (!ProcessOperators(localIdentifier)) // this creates the proto_pattern
return false;
FunctionSelectionRule fsr(fCount++, fInterp);
if (linkOn) fsr.SetSelected(BaseSelectionRule::kYes);
else fsr.SetSelected(BaseSelectionRule::kNo);
fsr.SetAttributeValue("proto_pattern", localIdentifier);
fSelectionRules->AddFunctionSelectionRule(fsr);
}
break;
case kGlobal: {
VariableSelectionRule vsr(fCount++, fInterp);
if (linkOn) vsr.SetSelected(BaseSelectionRule::kYes);
else vsr.SetSelected(BaseSelectionRule::kNo);
if (IsPatternRule(identifier)) vsr.SetAttributeValue("pattern", identifier);
else vsr.SetAttributeValue("name", identifier);
fSelectionRules->AddVariableSelectionRule(vsr);
}
break;
case kEnum: {
EnumSelectionRule esr(fCount++, fInterp);
if (linkOn) esr.SetSelected(BaseSelectionRule::kYes);
else esr.SetSelected(BaseSelectionRule::kNo);
if (IsPatternRule(identifier)) esr.SetAttributeValue("pattern", identifier);
else esr.SetAttributeValue("name", identifier);
fSelectionRules->AddEnumSelectionRule(esr);
}
break;
case kClass:
case kTypeDef:
case kNamespace:
case kUnion:
case kStruct: {
std::string localIdentifier(identifier);
ClassSelectionRule csr(fCount++, fInterp);
if (request_only_tclass) {
csr.SetRequestOnlyTClass(true);
}
int len = localIdentifier.length();
if (len > 8) { // process class+protected and class+private
const std::string protStr("+protected");
const std::string privStr("+private");
if (localIdentifier.compare(0, protStr.length(), protStr) == 0) {
csr.SetRequestProtected(true);
localIdentifier.erase(0, protStr.length() + 1);
len = localIdentifier.length();
} else if (localIdentifier.compare(0, privStr.length(), privStr) == 0) {
csr.SetRequestPrivate(true);
localIdentifier.erase(0, privStr.length() + 1);
len = localIdentifier.length();
}
}
if (len > 1) { // process the +, -, -! endings of the classes
bool ending = false;
int where = 1;
while (!ending && where < len) {
char last = localIdentifier.at(len - where);
switch (last) {
case ';':
break;
case '+':
csr.SetRequestStreamerInfo(true);
break;
case '!':
csr.SetRequestNoInputOperator(true);
break;
case '-':
csr.SetRequestNoStreamer(true);
break;
case ' ':
case '\t':
break;
default:
ending = true;
}
++where;
}
if (options) {
if (options->fNoStreamer) csr.SetRequestNoStreamer(true);
if (options->fNoInputOper) csr.SetRequestNoInputOperator(true);
if (options->fRequestStreamerInfo) csr.SetRequestStreamerInfo(true);
if (options->fVersionNumber >= 0) csr.SetRequestedVersionNumber(options->fVersionNumber);
}
if (csr.RequestStreamerInfo() && csr.RequestNoStreamer()) {
std::cerr << "Warning: " << localIdentifier << " option + mutual exclusive with -, + prevails\n";
csr.SetRequestNoStreamer(false);
}
if (ending) {
localIdentifier.erase(len - (where - 2)); // We 'consumed' one of the class token
} else {
localIdentifier.erase(len - (where - 1));
}
}
if (linkOn) {
csr.SetSelected(BaseSelectionRule::kYes);
if (localIdentifier == "*") { // rootcint generates error here, but I decided to implement it
ClassSelectionRule csr2(fCount++, fInterp);
csr2.SetSelected(BaseSelectionRule::kYes);
csr2.SetAttributeValue("pattern", "*::*");
fSelectionRules->AddClassSelectionRule(csr2);
ClassSelectionRule csr3(fCount++, fInterp);
csr3.SetSelected(BaseSelectionRule::kNo);
csr3.SetAttributeValue("pattern", "__va_*");
fSelectionRules->AddClassSelectionRule(csr3);
}
} else {
csr.SetSelected(BaseSelectionRule::kNo);
if (localIdentifier == "*") { // rootcint generates error here, but I decided to implement it
ClassSelectionRule csr2(fCount++, fInterp);
csr2.SetSelected(BaseSelectionRule::kNo);
csr2.SetAttributeValue("pattern", "*::*");
fSelectionRules->AddClassSelectionRule(csr2);
EnumSelectionRule esr(fCount++, fInterp); // we need this because of implicit/explicit rules - check my notes on rootcint
esr.SetSelected(BaseSelectionRule::kNo);
esr.SetAttributeValue("pattern", "*::*");
fSelectionRules->AddEnumSelectionRule(esr);
}
// Since the rootcling default is 'off' (we need to explicilty annotate to turn it on), the nested type and function
// should be off by default. Note that anyway, this is not yet relevant since the pcm actually ignore the on/off
// request and contains everything (for now).
// else {
// EnumSelectionRule esr(fCount++); // we need this because of implicit/explicit rules - check my notes on rootcint
// esr.SetSelected(BaseSelectionRule::kNo);
// esr.SetAttributeValue("pattern", localIdentifier+"::*");
// fSelectionRules->AddEnumSelectionRule(esr);
// if (fSelectionRules->GetHasFileNameRule()) {
// FunctionSelectionRule fsr(fCount++); // we need this because of implicit/explicit rules - check my notes on rootcint
// fsr.SetSelected(BaseSelectionRule::kNo);
// std::string value = localIdentifier + "::*";
// fsr.SetAttributeValue("pattern", value);
// fSelectionRules->AddFunctionSelectionRule(fsr);
// }
// }
}
if (IsPatternRule(localIdentifier)) {
csr.SetAttributeValue("pattern", localIdentifier);
}
csr.SetAttributeValue("name", localIdentifier);
if (name == kTypeDef) {
csr.SetAttributeValue("fromTypedef", "true");
}
fSelectionRules->AddClassSelectionRule(csr);
//csr.PrintAttributes(std::cout,3);
}
break;
case kIOCtorType:
// #pragma link C++ IOCtorType typename;
fIOConstructorTypesPtr->push_back(ROOT::TMetaUtils::RConstructorType(identifier.c_str(), fInterp));
break;
case kIgnore:
// All the pragma that were supported in CINT but are currently not relevant for CLING
// (mostly because we do not yet filter the dictionary/pcm).
break;
case kUnknown:
ROOT::TMetaUtils::Warning("Unimplemented pragma statement - it has no effect: %s\n", identifier.c_str());
return false;
break;
}
return true;
}
bool LinkdefReader::IsPatternRule(const std::string &rule_token)
{
int pos = rule_token.find("*");
if (pos > -1) return true;
else return false;
}
/*
* The method records that 'include' has been explicitly requested in the linkdef file
* to be added to the dictionary and interpreter.
*/
bool LinkdefReader::LoadIncludes(std::string &extraIncludes)
{
extraIncludes += fIncludes;
return cling::Interpreter::kSuccess == fInterp.declare(fIncludes);
}
bool LinkdefReader::ProcessFunctionPrototype(std::string &proto, bool &name)
{
int pos1, pos1_1, pos2, pos2_1;
pos1 = proto.find_first_of("(");
pos1_1 = proto.find_last_of("(");
if (pos1 != pos1_1) {
std::cout << "Error at line " << fLine << " - too many ( in function prototype!" << std::endl;
return false;
}
pos2 = proto.find_first_of(")");
pos2_1 = proto.find_last_of(")");
if (pos2 != pos2_1) {
std::cout << "Error at line " << fLine << " - too many ) in function prototype!" << std::endl;
return false;
}
if (pos1 > -1) {
if (pos2 < 0) {
std::cout << "Error at line " << fLine << " - missing ) in function prototype" << std::endl;
return false;
}
if (pos2 < pos1) {
std::cout << "Error at line " << fLine << " - wrong order of ( and ) in function prototype" << std::endl;
return false;
}
// I don't have to escape the *-s because in rootcint there is no pattern recognition
int pos3 = pos1;
while (true) {
pos3 = proto.find(" ", pos3);
if (pos3 > -1) {
proto.erase(pos3, 1);
}
if (pos3 < 0) break;
}
name = false;
} else {
if (pos2 > -1) {
std::cout << "Error at line " << fLine << " - missing ( in function prototype" << std::endl;
return false;
} else {
//std::cout<<"Debug - no prototype, name = true"<<std::endl;
name = true;
}
}
return true;
}
// This function is really very basic - it just checks whether everything is OK with the
// spaces and if the number of opening < matches the number of >.
// But it doesn't catch situations like vector>int<, etc.
bool LinkdefReader::ProcessOperators(std::string &pattern)
{
int pos = -1;
int pos1 = -1, pos2 = -1;
int open_br = 0, close_br = 0;
while (true) {
pos = pattern.find(" ", pos + 1);
pos1 = pattern.find("<", pos1 + 1);
pos2 = pattern.find(">", pos2 + 1);
if ((pos < 0) && (pos1 < 0) && (pos2 < 0)) break;
if (pos1 > -1) ++open_br;
if (pos2 > -1) ++close_br;
if (pos < 0) continue;
char before = '$';
char after = '$';
bool ok1 = false;
bool ok2 = false;
if (pos > 0) before = pattern.at(pos - 1);
if (pos < (int)(pattern.length() - 1)) after = pattern.at(pos + 1);
//std::cout<<"before: "<<before<<", after: "<<after<<", pos: "<<pos<<std::endl;
switch (before) {
case '<':
case ',':
case ' ':
ok1 = true;
break;
default:
ok1 = false;
}
switch (after) {
case '>':
case '<':
case ',':
case ' ':
ok2 = true;
break;
default:
ok2 = false;
}
//std::cout<<"ok1: "<<ok1<<", ok2: "<<ok2<<std::endl;
if (!ok1 && !ok2) {
std::cout << "Error at line " << fLine - 1 << " - extra space" << std::endl;
return false;
}
pattern.erase(pos, 1);
}
if (open_br != close_br) {
std::cout << "Error at line " << fLine << " - number of < doesn't match number of >" << std::endl;
return false;
}
pattern = "operator*(*" + pattern + "*)";
return true;
}
class LinkdefReaderPragmaHandler : public clang::PragmaHandler {
protected:
LinkdefReader &fOwner;
clang::SourceManager &fSourceManager;
public:
LinkdefReaderPragmaHandler(const char *which, LinkdefReader &owner, clang::SourceManager &sm) :
// This handler only cares about "#pragma link"
clang::PragmaHandler(which), fOwner(owner), fSourceManager(sm) {
}
void Error(const char *message, const clang::Token &tok, bool source = true) {
std::cerr << message << " at ";
tok.getLocation().dump(fSourceManager);
if (source) {
std::cerr << ":";
std::cerr << fSourceManager.getCharacterData(tok.getLocation());
}
std::cerr << '\n';
}
bool ProcessOptions(LinkdefReader::Options &options,
clang::Preprocessor &PP,
clang::Token &tok) {
// Constructor parsing:
/* options=...
* possible options:
* nostreamer: set G__NOSTREAMER flag
* noinputoper: set G__NOINPUTOPERATOR flag
* evolution: set G__USEBYTECOUNT flag
* nomap: (ignored by roocling; prevents entry in ROOT's rootmap file)
* stub: (ignored by rootcling was a directly for CINT code generation)
* version(x): sets the version number of the class to x
*/
// We assume that the first toke in option or options
// assert( tok.getIdentifierInfo()->getName() != "option" or "options")
PP.Lex(tok);
if (tok.is(clang::tok::eod) || tok.isNot(clang::tok::equal)) {
Error("Error: the 'options' keyword must be followed by an '='", tok);
return false;
}
PP.Lex(tok);
while (tok.isNot(clang::tok::eod) && tok.isNot(clang::tok::semi)) {
if (!tok.getIdentifierInfo()) {
Error("Error: Malformed version option.", tok);
} else if (tok.getIdentifierInfo()->getName() == "nomap") {
// For rlibmap rather than rootcling
// so ignore
} else if (tok.getIdentifierInfo()->getName() == "nostreamer") options.fNoStreamer = 1;
else if (tok.getIdentifierInfo()->getName() == "noinputoper") options.fNoInputOper = 1;
else if (tok.getIdentifierInfo()->getName() == "evolution") options.fRequestStreamerInfo = 1;
else if (tok.getIdentifierInfo()->getName() == "stub") {
// This was solely for CINT dictionary, ignore for now.
// options.fUseStubs = 1;
} else if (tok.getIdentifierInfo()->getName() == "version") {
clang::Token start = tok;
PP.Lex(tok);
if (tok.is(clang::tok::eod) || tok.isNot(clang::tok::l_paren)) {
Error("Error: missing left parenthesis after version.", start);
return false;
}
PP.Lex(tok);
clang::Token number = tok;
if (tok.isNot(clang::tok::eod)) PP.Lex(tok);
if (tok.is(clang::tok::eod) || tok.isNot(clang::tok::r_paren)) {
Error("Error: missing right parenthesis after version.", start);
return false;
}
if (!number.isLiteral()) {
std::cerr << "Error: Malformed version option, the value is not a non-negative number!";
Error("", tok);
}
std::string verStr(number.getLiteralData(), number.getLength());
bool noDigit = false;
for (std::string::size_type i = 0; i < verStr.size(); ++i)
if (!isdigit(verStr[i])) noDigit = true;
if (noDigit) {
std::cerr << "Error: Malformed version option! \"" << verStr << "\" is not a non-negative number!";
Error("", start);
} else
options.fVersionNumber = atoi(verStr.c_str());
} else {
Error("Warning: ignoring unknown #pragma link option=", tok);
}
PP.Lex(tok);
if (tok.is(clang::tok::eod) || tok.isNot(clang::tok::comma)) {
// no more options, we are done.
break;
}
PP.Lex(tok);
}
return true;
}
};
class PragmaExtraInclude : public LinkdefReaderPragmaHandler {
public:
PragmaExtraInclude(LinkdefReader &owner, clang::SourceManager &sm) :
// This handler only cares about "#pragma link"
LinkdefReaderPragmaHandler("extra_include", owner, sm) {
}
void HandlePragma(clang::Preprocessor &PP,
clang::PragmaIntroducer Introducer,
clang::Token &tok) override {
// Handle a #pragma found by the Preprocessor.
// check whether we care about the pragma - we are a named handler,
// thus this could actually be transformed into an assert:
if (Introducer.Kind != clang::PIK_HashPragma) return; // only #pragma, not C-style.
if (!tok.getIdentifierInfo()) return; // must be "link"
if (tok.getIdentifierInfo()->getName() != "extra_include") return;
PP.Lex(tok);
// if (DClient.hasErrorOccured()) {
// return;
// }
if (tok.is(clang::tok::eod)) {
Error("Warning - lonely pragma statement: ", tok);
return;
}
const char *start = fSourceManager.getCharacterData(tok.getLocation());
clang::Token end;
end.startToken(); // Initialize token.
while (tok.isNot(clang::tok::eod) && tok.isNot(clang::tok::semi)) {
end = tok;
PP.Lex(tok);
}
if (tok.isNot(clang::tok::semi)) {
Error("Error: missing ; at end of rule", tok, false);
return;
}
if (end.is(clang::tok::unknown)) {
Error("Error: Unknown token!", tok);
} else {
llvm::StringRef include(start, fSourceManager.getCharacterData(end.getLocation()) - start + end.getLength());
if (!fOwner.AddInclude(include.str())) {
Error("", tok);
}
}
}
};
class PragmaIoReadInclude : public LinkdefReaderPragmaHandler {
public:
PragmaIoReadInclude(LinkdefReader &owner, clang::SourceManager &sm) :
// This handler only cares about "#pragma link"
LinkdefReaderPragmaHandler("read", owner, sm) {
}
void HandlePragma(clang::Preprocessor &PP,
clang::PragmaIntroducer Introducer,
clang::Token &tok) override {
// Handle a #pragma found by the Preprocessor.
// check whether we care about the pragma - we are a named handler,
// thus this could actually be transformed into an assert:
if (Introducer.Kind != clang::PIK_HashPragma) return; // only #pragma, not C-style.
if (!tok.getIdentifierInfo()) return; // must be "link"
if (tok.getIdentifierInfo()->getName() != "read") return;
PP.Lex(tok);
// if (DClient.hasErrorOccured()) {
// return;
// }
if (tok.is(clang::tok::eod)) {
Error("Warning - lonely pragma statement: ", tok);
return;
}
const char *start = fSourceManager.getCharacterData(tok.getLocation());
clang::Token end;
end.startToken(); // Initialize token.
while (tok.isNot(clang::tok::eod) && tok.isNot(clang::tok::semi)) {
end = tok;
PP.Lex(tok);
}
// Pragma read rule do not need to end in a semi colon
// if (tok.isNot(clang::tok::semi)) {
// Error("Error: missing ; at end of rule",tok);
// return;
// }
if (end.is(clang::tok::unknown)) {
Error("Error: unknown token", tok);
} else {
llvm::StringRef rule_text(start, fSourceManager.getCharacterData(end.getLocation()) - start + end.getLength());
std::string error_string;
ROOT::ProcessReadPragma(rule_text.str().c_str(), error_string);
if (!error_string.empty())
std::cerr << error_string;
//std::cerr << "Warning: #pragma read not yet handled: " << include.str() << "\n";
// if (!fOwner.AddInclude(include))
// {
// Error("",tok);
// }
}
}
};
class PragmaLinkCollector : public LinkdefReaderPragmaHandler {
// Handles:
// #pragma link [spec] options=... class classname[+-!]
//
public:
PragmaLinkCollector(LinkdefReader &owner, clang::SourceManager &sm) :
// This handler only cares about "#pragma link"
LinkdefReaderPragmaHandler("link", owner, sm) {
}
void HandlePragma(clang::Preprocessor &PP,
clang::PragmaIntroducer Introducer,
clang::Token &tok) override {
// Handle a #pragma found by the Preprocessor.
// check whether we care about the pragma - we are a named handler,
// thus this could actually be transformed into an assert:
if (Introducer.Kind != clang::PIK_HashPragma) return; // only #pragma, not C-style.
if (!tok.getIdentifierInfo()) return; // must be "link"
if (tok.getIdentifierInfo()->getName() != "link") return;
PP.Lex(tok);
// if (DClient.hasErrorOccured()) {
// return;
// }
if (tok.is(clang::tok::eod)) {
Error("Warning - lonely pragma statement: ", tok);
return;
}
bool linkOn;
if (tok.isAnyIdentifier()) {
if ((tok.getIdentifierInfo()->getName() == "off")) {
linkOn = false;
} else if ((tok.getIdentifierInfo()->getName() == "C")) {
linkOn = true;
PP.Lex(tok);
if (tok.is(clang::tok::eod) || tok.isNot(clang::tok::plusplus)) {
Error("Error ++ expected after '#pragma link C' at ", tok);
return;
}
} else {
Error("Error #pragma link should be followed by off or C", tok);
return;
}
} else {
Error("Error bad #pragma format. ", tok);
return;
}
PP.Lex(tok);
if (tok.is(clang::tok::eod)) {
Error("Error no arguments after #pragma link C++/off: ", tok);
return;
}
auto identifier = tok.getIdentifierInfo();
if (identifier == nullptr) {
if (linkOn) Error("Error #pragma link C++ should be followed by identifier", tok);
else Error("Error #pragma link off should be followed by identifier", tok);
return;
}
llvm::StringRef type = identifier->getName();
std::unique_ptr<LinkdefReader::Options> options;
if (type == "options" || type == "option") {
options.reset(new LinkdefReader::Options());
if (!ProcessOptions(*options, PP, tok)) {
return;
}
if (tok.getIdentifierInfo()) type = tok.getIdentifierInfo()->getName();
}
PP.LexUnexpandedToken(tok);
const char *start = fSourceManager.getCharacterData(tok.getLocation());
clang::Token end;
end.startToken(); // Initialize token.
while (tok.isNot(clang::tok::eod) && tok.isNot(clang::tok::semi)) {
// PP.DumpToken(tok, true);
// llvm::errs() << "\n";
end = tok;
PP.LexUnexpandedToken(tok);
}
if (tok.isNot(clang::tok::semi)) {
Error("Error: missing ; at end of rule", tok, false);
return;
}
if (end.is(clang::tok::unknown)) {
if (!fOwner.AddRule(type.data(), "", linkOn, false, options.get())) {
Error(type.data(), tok, false);
}
} else {
llvm::StringRef identifier(start, fSourceManager.getCharacterData(end.getLocation()) - start + end.getLength());
if (!fOwner.AddRule(type.str(), identifier.str(), linkOn, false, options.get())) {
Error(type.data(), tok, false);
}
}
// do {
// PP.Lex(tok);
// PP.DumpToken(tok, true);
// llvm::errs() << "\n";
// } while (tok.isNot(clang::tok::eod));
}
};
class PragmaCreateCollector : public LinkdefReaderPragmaHandler {
public:
PragmaCreateCollector(LinkdefReader &owner, clang::SourceManager &sm) :
// This handler only cares about "#pragma create"
LinkdefReaderPragmaHandler("create", owner, sm) {
}
void HandlePragma(clang::Preprocessor &PP,
clang::PragmaIntroducer Introducer,
clang::Token &tok) override {
// Handle a #pragma found by the Preprocessor.
// check whether we care about the pragma - we are a named handler,
// thus this could actually be transformed into an assert:
if (Introducer.Kind != clang::PIK_HashPragma) return; // only #pragma, not C-style.
if (!tok.getIdentifierInfo()) return; // must be "link"
if (tok.getIdentifierInfo()->getName() != "create") return;
PP.Lex(tok);
// if (DClient.hasErrorOccured()) {
// return;
// }
if (tok.is(clang::tok::eod)) {
Error("Warning - lonely pragma statement: ", tok);
return;
}
if ((tok.getIdentifierInfo()->getName() != "TClass")) {
Error("Error: currently only supporting TClass after '#pragma create':", tok);
return;
}
PP.Lex(tok);
const char *start = fSourceManager.getCharacterData(tok.getLocation());
clang::Token end = tok;
while (tok.isNot(clang::tok::eod) && tok.isNot(clang::tok::semi)) {
end = tok;
PP.Lex(tok);
}
if (tok.isNot(clang::tok::semi)) {
Error("Error: missing ; at end of rule", tok, false);
return;
}
llvm::StringRef identifier(start, fSourceManager.getCharacterData(end.getLocation()) - start + end.getLength());
if (!fOwner.AddRule("class", identifier.str(), true, true)) {
Error("", tok);
}
// do {
// PP.Lex(tok);
// PP.DumpToken(tok, true);
// llvm::errs() << "\n";
// } while (tok.isNot(clang::tok::eod));
};
};