Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
SelectionRules.cxx
Go to the documentation of this file.
1// @(#)root/core/utils:$Id: SelectionRules.cxx 41697 2011-11-01 21:03:41Z pcanal $
2// Author: Velislava Spasova September 2010
3
4/*************************************************************************
5 * Copyright (C) 1995-2011, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/**
13\class SelectionRules
14The class representing the collection of selection rules.
15*/
16
17#include <iostream>
18#include <sstream>
19#include <algorithm>
20#ifndef WIN32
21#include <fnmatch.h>
22#else
23#include "Shlwapi.h"
24#define fnmatch(glob, path, dummy) PathMatchSpecA(path, glob);
25#endif
26#include "RtypesCore.h"
27#include "SelectionRules.h"
28#include "llvm/Support/raw_ostream.h"
29#include "clang/Basic/SourceLocation.h"
30#include "clang/Basic/SourceManager.h"
31#include "clang/AST/ASTContext.h"
32#include "clang/AST/DeclCXX.h"
33#include "clang/AST/DeclTemplate.h"
34
35#include "cling/Interpreter/Interpreter.h"
36
37const clang::CXXRecordDecl *R__ScopeSearch(const char *name, const clang::Type** resultType = nullptr) ;
38
40{
42 fClassSelectionRules.push_front(classSel);
43 if (!classSel.HasInterpreter())
44 fClassSelectionRules.begin()->SetInterpreter(fInterp);
45 if (classSel.GetIndex() < 0)
46 fClassSelectionRules.begin()->SetIndex(fRulesCounter);
47}
48
50{
52 fFunctionSelectionRules.push_back(funcSel);
53 if (!funcSel.HasInterpreter())
54 fFunctionSelectionRules.begin()->SetInterpreter(fInterp);
55 if (funcSel.GetIndex() < 0)
56 fFunctionSelectionRules.begin()->SetIndex(fRulesCounter);
57}
58
60{
62 fVariableSelectionRules.push_back(varSel);
63 if (!varSel.HasInterpreter())
64 fVariableSelectionRules.begin()->SetInterpreter(fInterp);
65 if (varSel.GetIndex() < 0)
66 fVariableSelectionRules.begin()->SetIndex(fRulesCounter);
67}
68
70{
72 fEnumSelectionRules.push_back(enumSel);
73 if (!enumSel.HasInterpreter())
74 fEnumSelectionRules.begin()->SetInterpreter(fInterp);
75 if (enumSel.GetIndex() < 0)
76 fEnumSelectionRules.begin()->SetIndex( fRulesCounter );
77}
78
80{
81 std::cout<<"Printing Selection Rules:"<<std::endl;
82 if (!fClassSelectionRules.empty()) {
83 int i = 0;
84 for(std::list<ClassSelectionRule>::const_iterator it = fClassSelectionRules.begin();
85 it != fClassSelectionRules.end(); ++it, ++i) {
86 std::cout<<"\tClass sel rule "<<i<<":"<<std::endl;
87 std::cout<< *it;
88 }
89 }
90 else {
91 std::cout<<"\tNo Class Selection Rules"<<std::endl;
92 }
93
94 if (!fFunctionSelectionRules.empty()) {
95 //std::cout<<""<<std::endl;
96 std::list<FunctionSelectionRule>::const_iterator it2;
97 int i = 0;
98
99 for (it2 = fFunctionSelectionRules.begin(); it2 != fFunctionSelectionRules.end(); ++it2, ++i) {
100 std::cout<<"\tFunction sel rule "<<i<<":"<<std::endl;
101 std::cout<<"\t\tSelected: ";
102 switch(it2->GetSelected()){
103 case BaseSelectionRule::kYes: std::cout<<"Yes"<<std::endl;
104 break;
105 case BaseSelectionRule::kNo: std::cout<<"No"<<std::endl;
106 break;
107 case BaseSelectionRule::kDontCare: std::cout<<"Don't Care"<<std::endl;
108 break;
109 default: std::cout<<"Unspecified"<<std::endl;
110 }
111 it2->PrintAttributes(std::cout,2);
112 }
113 }
114 else {
115 std::cout<<"\tNo function sel rules"<<std::endl;
116 }
117
118 if (!fVariableSelectionRules.empty()) {
119 std::list<VariableSelectionRule>::const_iterator it3;
120 int i = 0;
121
122 for (it3 = fVariableSelectionRules.begin(); it3 != fVariableSelectionRules.end(); ++it3, ++i) {
123 std::cout<<"\tVariable sel rule "<<i<<":"<<std::endl;
124 std::cout<<"\t\tSelected: ";
125 switch(it3->GetSelected()){
126 case BaseSelectionRule::kYes: std::cout<<"Yes"<<std::endl;
127 break;
128 case BaseSelectionRule::kNo: std::cout<<"No"<<std::endl;
129 break;
130 case BaseSelectionRule::kDontCare: std::cout<<"Don't Care"<<std::endl;
131 break;
132 default: std::cout<<"Unspecified"<<std::endl;
133 }
134 it3->PrintAttributes(std::cout,2);
135 }
136 }
137 else {
138 std::cout<<"\tNo variable sel rules"<<std::endl;
139 }
140
141 if (!fEnumSelectionRules.empty()) {
142 std::list<EnumSelectionRule>::const_iterator it4;
143 int i = 0;
144
145 for (it4 = fEnumSelectionRules.begin(); it4 != fEnumSelectionRules.end(); ++it4, ++i) {
146 std::cout<<"\tEnum sel rule "<<i<<":"<<std::endl;
147 std::cout<<"\t\tSelected: ";
148 switch(it4->GetSelected()){
149 case BaseSelectionRule::kYes: std::cout<<"Yes"<<std::endl;
150 break;
151 case BaseSelectionRule::kNo: std::cout<<"No"<<std::endl;
152 break;
153 case BaseSelectionRule::kDontCare: std::cout<<"Don't Care"<<std::endl;
154 break;
155 default: std::cout<<"Unspecified"<<std::endl;
156 }
157 it4->PrintAttributes(std::cout,2);
158 }
159 }
160 else {
161 std::cout<<"\tNo enum sel rules"<<std::endl;
162 }
163}
164
166{
167 fClassSelectionRules.clear();
170 fEnumSelectionRules.clear();
171}
172
173template<class RULE>
174static bool HasDuplicate(RULE* rule,
175 std::unordered_map<std::string,RULE*>& storedRules,
176 const std::string& attrName){
177 auto itRetCodePair = storedRules.emplace( attrName, rule );
178
179 auto storedRule = storedRules[attrName];
180
181 if (itRetCodePair.second ||
182 storedRule->GetSelected() != rule->GetSelected()) return false;
183 auto areEqual = SelectionRulesUtils::areEqual(storedRule,rule);
184
185 std::stringstream sstr; sstr << "Rule:\n";
186 rule->Print(sstr);
187 sstr << (areEqual ? "Identical " : "Conflicting ");
188 sstr << "rule already stored:\n";
189 storedRule->Print(sstr);
190 ROOT::TMetaUtils::Warning("SelectionRules::CheckDuplicates",
191 "Duplicated rule found.\n%s",sstr.str().c_str());
192 return !areEqual;
193}
194
195template<class RULESCOLLECTION, class RULE = typename RULESCOLLECTION::value_type>
196static int CheckDuplicatesImp(RULESCOLLECTION& rules){
197 int nDuplicates = 0;
198 std::unordered_map<std::string, RULE*> patterns,names;
199 for (auto&& rule : rules){
200 if (rule.HasAttributeName() && HasDuplicate(&rule,names,rule.GetAttributeName())) nDuplicates++;
201 if (rule.HasAttributePattern() && HasDuplicate(&rule,patterns,rule.GetAttributePattern())) nDuplicates++;
202 }
203 return nDuplicates;
204}
205
207
208 int nDuplicates = 0;
213 if (0 != nDuplicates){
214 ROOT::TMetaUtils::Error("SelectionRules::CheckDuplicates",
215 "Duplicates in rules were found.\n");
216 }
217 return nDuplicates;
218}
219
220static bool Implies(const ClassSelectionRule& patternRule, const ClassSelectionRule& nameRule){
221
222 // Check if these both select or both exclude
223 if (patternRule.GetSelected() != nameRule.GetSelected()) return false;
224
225 // If the two rules are not compatible modulo their name/pattern, bail out
226 auto nAttrsPattern = patternRule.GetAttributes().size();
227 auto nAttrsName = nameRule.GetAttributes().size();
228 if ((nAttrsPattern != 1 || nAttrsName !=1) &&
229 !SelectionRulesUtils::areEqual(&patternRule, &nameRule, true /*moduloNameOrPattern*/)) {
230 return false;
231 }
232
233 auto pattern = patternRule.GetAttributePattern().c_str();
234 auto name = nameRule.GetAttributeName().c_str();
235
236 // Now check if the pattern matches the name
237 auto implies = 0 == fnmatch(pattern, name, FNM_PATHNAME);
238
239 if (implies){
240 static const auto msg = "The pattern rule %s matches the name rule %s. "
241 "Since the name rule has compatible attributes, "
242 "it will be removed: the pattern rule will match the necessary classes if needed.\n";
243
244 ROOT::TMetaUtils::Info("SelectionRules::Optimize", msg, pattern, name);
245 }
246
247
248 return implies;
249
250}
251
253
254 // Remove name rules "implied" by pattern rules
255
256 if (!IsSelectionXMLFile()) return;
257
258 const auto& selectionRules = fClassSelectionRules;
259
260 auto predicate = [&selectionRules](const ClassSelectionRule &rule) -> bool {
261 if (rule.HasAttributeName()) {
262 for (auto&& intRule : selectionRules){
263 if (intRule.HasAttributePattern() && Implies(intRule, rule)) {
264 return true;
265 }
266 }
267 }
268 return false;
269 };
270 fClassSelectionRules.remove_if(predicate);
271}
272
273const ClassSelectionRule *SelectionRules::IsDeclSelected(const clang::RecordDecl *D, bool includeTypedefRule) const
274{
275 std::string qual_name;
276 GetDeclQualName(D,qual_name);
277 return IsClassSelected(D, qual_name, includeTypedefRule);
278}
279
280const ClassSelectionRule *SelectionRules::IsDeclSelected(const clang::TypedefNameDecl *D) const
281{
282 std::string qual_name;
283 GetDeclQualName(D,qual_name);
284 return IsClassSelected(D, qual_name, true);
285}
286
287const ClassSelectionRule *SelectionRules::IsDeclSelected(const clang::NamespaceDecl *D) const
288{
289 std::string qual_name;
290 GetDeclQualName(D,qual_name);
291 return IsNamespaceSelected(D, qual_name);
292}
293
294const BaseSelectionRule *SelectionRules::IsDeclSelected(const clang::EnumDecl *D) const
295{
296 // Currently rootcling does not need any information on enums, except
297 // for the PCM / proto classes that register them to build TEnums without
298 // parsing. This can be removed once (real) PCMs are available.
299 // Note that the code below was *not* properly matching the case
300 // typedef enum { ... } abc;
301 // as the typedef is stored as an anonymous EnumDecl in clang.
302 // It is likely that using a direct lookup on the name would
303 // return the appropriate typedef (and then we would need to
304 // select 'both' the typedef and the anonymous enums.
305
306#if defined(R__MUST_REVISIT)
307# if R__MUST_REVISIT(6,4)
308 "Can become no-op once PCMs are available."
309# endif
310#endif
311
312 std::string str_name; // name of the Decl
313 std::string qual_name; // fully qualified name of the Decl
314 GetDeclName(D, str_name, qual_name);
315
316 if (IsParentClass(D)) {
317 const BaseSelectionRule *selector = IsMemberSelected(D, str_name);
318 if (!selector) // if the parent class is deselected, we could still select the enum
319 return IsEnumSelected(D, qual_name);
320 else // if the parent class is selected so are all nested enums
321 return selector;
322 }
323
324 // Enum is not part of a class
325 else {
326 if (IsLinkdefFile())
327 return IsLinkdefEnumSelected(D, qual_name);
328 return IsEnumSelected(D, qual_name);
329 }
330
331 return nullptr;
332}
333
334const BaseSelectionRule *SelectionRules::IsDeclSelected(const clang::VarDecl* D) const
335{
336 std::string qual_name; // fully qualified name of the Decl
337 GetDeclQualName(D, qual_name);
338
339 if (IsLinkdefFile())
340 return IsLinkdefVarSelected(D, qual_name);
341 else
342 return IsVarSelected(D, qual_name);
343
344}
345
346const BaseSelectionRule *SelectionRules::IsDeclSelected(const clang::FieldDecl* /* D */) const
347{
348 // Currently rootcling does not need any information about fields.
349 return nullptr;
350#if 0
351 std::string str_name; // name of the Decl
352 std::string qual_name; // fully qualified name of the Decl
353 GetDeclName(D, str_name, qual_name);
354
355 return IsMemberSelected(D, str_name);
356#endif
357}
358
359const BaseSelectionRule *SelectionRules::IsDeclSelected(const clang::FunctionDecl* D) const
360{
361 // Implement a simple matching for functions
362 std::string qual_name; // fully qualified name of the Decl
363 GetDeclQualName(D, qual_name);
364 if (IsLinkdefFile())
365 return IsLinkdefFunSelected(D, qual_name);
366 else
367 return IsFunSelected(D, qual_name);
368}
369
370const BaseSelectionRule *SelectionRules::IsDeclSelected(const clang::Decl *D) const
371{
372 if (!D) {
373 return nullptr;
374 }
375
376 clang::Decl::Kind declkind = D->getKind();
377
378 switch (declkind) {
379 case clang::Decl::CXXRecord:
380 case clang::Decl::ClassTemplateSpecialization:
381 case clang::Decl::ClassTemplatePartialSpecialization:
382 // structs, unions and classes are all CXXRecords
383 return IsDeclSelected(llvm::dyn_cast<clang::RecordDecl>(D));
384 case clang::Decl::Namespace:
385 return IsDeclSelected(llvm::dyn_cast<clang::NamespaceDecl>(D));
386 case clang::Decl::Enum:
387 // Enum is part of a class
388 return IsDeclSelected(llvm::dyn_cast<clang::EnumDecl>(D));
389 case clang::Decl::Var:
390 return IsDeclSelected(llvm::dyn_cast<clang::VarDecl>(D));
391#if ROOTCLING_NEEDS_FUNCTIONS_SELECTION
392 case clang::Decl::Function:
393 return IsDeclSelected(llvm::dyn_cast<clang::FunctionDecl>(D));
394 case clang::Decl::CXXMethod:
395 case clang::Decl::CXXConstructor:
396 case clang::Decl::CXXDestructor: {
397 // std::string proto;
398 // if (GetFunctionPrototype(D,proto))
399 // std::cout<<"\n\tFunction prototype: "<<str_name + proto;
400 // else
401 // std::cout<<"Error in prototype formation";
402 std::string str_name; // name of the Decl
403 std::string qual_name; // fully qualified name of the Decl
404 GetDeclName(D, str_name, qual_name);
405 if (IsLinkdefFile()) {
406 return IsLinkdefMethodSelected(D, qual_name);
407 }
408 return IsMemberSelected(D, str_name);
409 }
410#endif
411 case clang::Decl::Field:
412 return IsDeclSelected(llvm::dyn_cast<clang::FieldDecl>(D));
413 default:
414 // Humm we are not treating this case!
415 return nullptr;
416 }
417
418 // std::string str_name; // name of the Decl
419 // std::string qual_name; // fully qualified name of the Decl
420 // GetDeclName(D, str_name, qual_name);
421 // fprintf(stderr,"IsDeclSelected: %s %s\n", str_name.c_str(), qual_name.c_str());
422}
423
424
425bool SelectionRules::GetDeclName(const clang::Decl* D, std::string& name, std::string& qual_name) const
426{
427 const clang::NamedDecl* N = llvm::dyn_cast<clang::NamedDecl> (D);
428
429 if (!N)
430 return false;
431
432 // the identifier is NULL for some special methods like constructors, destructors and operators
433 if (N->getIdentifier() || N->isCXXClassMember()) {
434 name = N->getNameAsString();
435 llvm::raw_string_ostream stream(qual_name);
436 N->getNameForDiagnostic(stream,N->getASTContext().getPrintingPolicy(),true);
437 }
438 return true;
439}
440
441void SelectionRules::GetDeclQualName(const clang::Decl* D, std::string& qual_name) const
442{
443 const clang::NamedDecl* N = static_cast<const clang::NamedDecl*> (D);
444 llvm::raw_string_ostream stream(qual_name);
445 if (N)
446 N->getNameForDiagnostic(stream,N->getASTContext().getPrintingPolicy(),true);
447}
448
449bool SelectionRules::GetFunctionPrototype(const clang::FunctionDecl* F, std::string& prototype) const
450{
451
452 if (!F) {
453 return false;
454 }
455
456 const std::vector<std::string> quals={"*","&"};
457
458 prototype = "";
459 // iterate through all the function parameters
460 std::string type;
461 for (auto I = F->param_begin(), E = F->param_end(); I != E; ++I) {
462
463 clang::ParmVarDecl* P = *I;
464
465 if (prototype != "")
466 prototype += ",";
467
469
470 // We need to get rid of the "class " string if present
472 // We need to get rid of the "restrict " string if present
473 ROOT::TMetaUtils::ReplaceAll(type,"restrict", "");
474
475 // pointers are returned in the form "int *" and I need them in the form "int*"
476 // same for &
477 for (auto& qual : quals){
478 auto pos = type.find(" "+qual);
479 if (pos != std::string::npos)
480 type.replace( pos, 2, qual );
481 }
482// for (auto& qual : quals){
483// if (type.at(type.length()-1) == qual && type.at(type.length()-2) == ' ') {
484// type.at(type.length()-2) = qual;
485// type.erase(type.length()-1);
486// }
487// }
488 prototype += type;
489 }
490 prototype = "(" + prototype + ")";
491 return true;
492
493}
494
495
496bool SelectionRules::IsParentClass(const clang::Decl* D) const
497{
498 //TagDecl has methods to understand of what kind is the Decl - class, struct or union
499 if (!D)
500 return false;
501 if (const clang::TagDecl *T = llvm::dyn_cast<clang::TagDecl>(
502 D->getDeclContext()))
503 return T->isClass() || T->isStruct();
504 return false;
505}
506
507
508bool SelectionRules::IsParentClass(const clang::Decl* D, std::string& parent_name, std::string& parent_qual_name) const
509{
510 if (const clang::TagDecl* parent
511 = llvm::dyn_cast<clang::TagDecl>(D->getDeclContext())) {
512 if (parent->isClass()|| parent->isStruct()) {
513 GetDeclName(parent, parent_name, parent_qual_name);
514 return true;
515 }
516 }
517 return false;
518}
519
520bool SelectionRules::GetParentName(const clang::Decl* D, std::string& parent_name, std::string& parent_qual_name) const
521{
522 if (const clang::RecordDecl* parent
523 = llvm::dyn_cast<clang::RecordDecl>(D->getDeclContext())) {
524 GetDeclName(parent, parent_name, parent_qual_name);
525 return true;
526 }
527 return false;
528}
529
530/* This is the method that crashes
531 bool SelectionRules::GetParent(clang::Decl* D, clang::Decl* parent)
532 {
533 clang::DeclContext *ctx = D->GetDeclContext();
534
535 if (ctx->isRecord()){
536 //DEBUG std::cout<<"\n\tDeclContext is Record";
537 parent = llvm::dyn_cast<clang::Decl> (ctx);
538 if (!parent) {
539 return false;
540 }
541 else {
542 return true;
543 }
544 }
545 else return false;
546 }
547 */
548
549
550// isClassSelected checks if a class is selected or not. Thre is a difference between the
551// behaviour of rootcint and genreflex especially with regard to class pattern processing.
552// In genreflex if we have <class pattern = "*" /> this will select all the classes
553// (and structs) found in the header file. In rootcint if we have something similar, i.e.
554// #pragma link C++ class *, we will select only the outer classes - for the nested
555// classes we have to specifie #pragma link C++ class *::*. And yet this is only valid
556// for one level of nesting - if you need it for many levels of nesting, you will
557// probably have to implement it yourself.
558// Here the idea is the following - we traverse the list of class selection rules.
559// For every class we check do we have a class selection rule. We use here the
560// method isSelected() (defined in BaseSelectionRule.cxx). This method returns true
561// only if we have class selection rule which says "Select". Otherwise it returns
562// false - in which case we have to check wether we found a class selection rule
563// which says "Veto" (noName = false and don't Care = false; OR noName = false and
564// don't Care = true and we don't have neither method nor field selection rules -
565// which is for the selection.xml file case). If noName is true than we just continue -
566// this means that the current class selection rule isn't applicable for this class.
567
568const ClassSelectionRule *SelectionRules::IsNamespaceSelected(const clang::Decl* D, const std::string& qual_name) const
569{
570 const clang::NamespaceDecl* N = llvm::dyn_cast<clang::NamespaceDecl> (D); //TagDecl has methods to understand of what kind is the Decl
571 if (N==nullptr) {
572 std::cout<<"\n\tCouldn't cast Decl to NamespaceDecl";
573 return nullptr;
574 }
575
576 const ClassSelectionRule *selector = nullptr;
577 int fImplNo = 0;
578 const ClassSelectionRule *explicit_selector = nullptr;
579 const ClassSelectionRule *specific_pattern_selector = nullptr;
580
581 // NOTE: should we separate namespaces from classes in the rules?
582 std::list<ClassSelectionRule>::const_iterator it = fClassSelectionRules.begin();
583 // iterate through all class selection rles
584 std::string name_value;
585 std::string pattern_value;
587 for(; it != fClassSelectionRules.end(); ++it) {
588
589 match = it->Match(N,qual_name,"",IsLinkdefFile());
590
591 if (match != BaseSelectionRule::kNoMatch) {
592 // If we have a match.
593 if (it->GetSelected() == BaseSelectionRule::kYes) {
594 selector = &(*it);
595 if (IsLinkdefFile()){
596 // rootcint prefers explicit rules over pattern rules
597 if (match == BaseSelectionRule::kName) {
598 explicit_selector = &(*it);
599 } else if (match == BaseSelectionRule::kPattern) {
600 // NOTE: weird ...
601 if (it->GetAttributeValue("pattern", pattern_value) &&
602 pattern_value != "*" && pattern_value != "*::*") specific_pattern_selector = &(*it);
603 }
604 }
605 } else if (it->GetSelected() == BaseSelectionRule::kNo) {
606 if (!IsLinkdefFile()) {
607 // in genreflex - we could explicitly select classes from other source files
608 // if we have veto because of class defined in other source file -> implicit No
609 if (match != BaseSelectionRule::kFile) {
610
611#ifdef SELECTION_DEBUG
612 std::cout<<"\tNo returned"<<std::endl;
613#endif
614
615 return nullptr; // explicit No returned
616 }
617 }
618 if (match == BaseSelectionRule::kPattern) {
619 //this is for the Linkdef selection
620 if (it->GetAttributeValue("pattern", pattern_value) &&
621 (pattern_value == "*" || pattern_value == "*::*")) ++fImplNo;
622 else
623 return nullptr;
624 }
625 else
626 return nullptr;
627 }
628 else if (it->GetSelected() == BaseSelectionRule::kDontCare && !(it->HasMethodSelectionRules()) && !(it->HasFieldSelectionRules())) {
629
630#ifdef SELECTION_DEBUG
631 std::cout<<"Empty dontC returned = No"<<std::endl;
632#endif
633
634 return nullptr;
635 }
636 }
637 }
638 if (IsLinkdefFile()) {
639 // for rootcint explicit (name) Yes is stronger than implicit (pattern) No which is stronger than implicit (pattern) Yes
640
641#ifdef SELECTION_DEBUG
642 std::cout<<"\n\tfYes = "<<fYes<<", fImplNo = "<<fImplNo<<std::endl;
643#endif
644
645 if (explicit_selector) return explicit_selector;
646 else if (specific_pattern_selector) return specific_pattern_selector;
647 else if (fImplNo > 0) return nullptr;
648 else return selector;
649 }
650 else {
651 // for genreflex explicit Yes is stronger than implicit file No
652
653#ifdef SELECTION_DEBUG
654 std::cout<<"\n\tfYes = "<<fYes<<", fFileNo = "<<fFileNo<<std::endl;
655#endif
656
657 if (selector)
658 return selector;
659 else
660 return nullptr;
661 }
662
663}
664
665
666const ClassSelectionRule *SelectionRules::IsClassSelected(const clang::Decl* D, const std::string& qual_name, bool includeTypedefRule) const
667{
668 const clang::TagDecl* tagDecl = llvm::dyn_cast<clang::TagDecl> (D); //TagDecl has methods to understand of what kind is the Decl
669 const clang::TypedefNameDecl* typeDefNameDecl = llvm::dyn_cast<clang::TypedefNameDecl> (D);
670
671 if (!tagDecl && !typeDefNameDecl) { // Ill posed
672 ROOT::TMetaUtils::Error("SelectionRules::IsClassSelected",
673 "Cannot cast Decl to TagDecl and Decl is not a typedef.\n");
674 return nullptr;
675 }
676
677 if (!tagDecl && typeDefNameDecl){ // Let's try to fetch the underlying RecordDecl
678 clang::RecordDecl* recordDecl = ROOT::TMetaUtils::GetUnderlyingRecordDecl(typeDefNameDecl->getUnderlyingType());
679 if (!recordDecl){
680 ROOT::TMetaUtils::Error("SelectionRules::IsClassSelected",
681 "Cannot get RecordDecl behind TypedefDecl.\n");
682 return nullptr;
683 }
684 tagDecl = recordDecl;
685 }
686
687 // At this point tagDecl must be well defined
688 const bool isLinkDefFile = IsLinkdefFile();
689 if (!( isLinkDefFile || tagDecl->isClass() || tagDecl->isStruct() ))
690 return nullptr; // Union for Genreflex
691
692 const ClassSelectionRule *selector = nullptr;
693 int fImplNo = 0;
694 const ClassSelectionRule *explicit_selector = nullptr;
695 const ClassSelectionRule *specific_pattern_selector = nullptr;
696
697 // iterate through all class selection rles
698 bool earlyReturn=false;
699 const ClassSelectionRule* retval = nullptr;
700 const clang::NamedDecl* nDecl(llvm::dyn_cast<clang::NamedDecl>(D));
701 for(auto& rule : fClassSelectionRules) {
702 if (!includeTypedefRule && rule.IsFromTypedef())
703 continue;
704 BaseSelectionRule::EMatchType match = rule.Match(nDecl, qual_name, "", isLinkDefFile);
705 if (match != BaseSelectionRule::kNoMatch) {
706 // Check if the template must have its arguments manipulated
707 if (const clang::ClassTemplateSpecializationDecl* ctsd =
708 llvm::dyn_cast_or_null<clang::ClassTemplateSpecializationDecl>(D))
709 if(const clang::ClassTemplateDecl* ctd = ctsd->getSpecializedTemplate()){
710 const std::string& nArgsToKeep = rule.GetAttributeNArgsToKeep();
711 if (!nArgsToKeep.empty()){
712 fNormCtxt.AddTemplAndNargsToKeep(ctd->getCanonicalDecl(),
713 std::atoi(nArgsToKeep.c_str()));
714 }
715 }
716
717 if (earlyReturn) continue;
718
719 // If we have a match.
720 selector = &(rule);
721 if (rule.GetSelected() == BaseSelectionRule::kYes) {
722
723 if (isLinkDefFile){
724 // rootcint prefers explicit rules over pattern rules
725 if (match == BaseSelectionRule::kName) {
726 explicit_selector = &(rule);
727 } else if (match == BaseSelectionRule::kPattern) {
728 // NOTE: weird ...
729 const std::string& pattern_value=rule.GetAttributePattern();
730 if (!pattern_value.empty() &&
731 pattern_value != "*" &&
732 pattern_value != "*::*") specific_pattern_selector = &(rule);
733 }
734 }
735 } else if (rule.GetSelected() == BaseSelectionRule::kNo) {
736
737 if (!isLinkDefFile) {
738 // in genreflex - we could explicitly select classes from other source files
739 // if we have veto because of class defined in other source file -> implicit No
740 if (match != BaseSelectionRule::kFile) {
741 retval = selector;
742 earlyReturn=true; // explicit No returned
743 }
744 }
745 if (match == BaseSelectionRule::kPattern) {
746 //this is for the Linkdef selection
747 const std::string& pattern_value=rule.GetAttributePattern();
748 if (!pattern_value.empty() &&
749 (pattern_value == "*" || pattern_value == "*::*")) ++fImplNo;
750 else
751 earlyReturn=true;
752 }
753 else
754 earlyReturn=true;
755 }
756 else if (rule.GetSelected() == BaseSelectionRule::kDontCare && !(rule.HasMethodSelectionRules()) && !(rule.HasFieldSelectionRules())) {
757 earlyReturn=true;
758 }
759 }
760 } // Loop over the rules.
761
762 if (earlyReturn) return retval;
763
764 if (isLinkDefFile) {
765 // for rootcint explicit (name) Yes is stronger than implicit (pattern) No which is stronger than implicit (pattern) Yes
766 if (explicit_selector) return explicit_selector;
767 else if (specific_pattern_selector) return specific_pattern_selector;
768 else if (fImplNo > 0) return nullptr;
769 else return selector;
770 }
771 else {
772 // for genreflex explicit Yes is stronger than implicit file No
773 return selector; // it can be nullptr
774 }
775
776}
777
778const BaseSelectionRule *SelectionRules::IsVarSelected(const clang::VarDecl* D, const std::string& qual_name) const
779{
780 std::list<VariableSelectionRule>::const_iterator it = fVariableSelectionRules.begin();
781 std::list<VariableSelectionRule>::const_iterator it_end = fVariableSelectionRules.end();
782
783 const BaseSelectionRule *selector = nullptr;
784
785 // iterate through all the rules
786 // we call this method only for genrefex variables, functions and enums - it is simpler than the class case:
787 // if we have No - it is veto even if we have explicit yes as well
788 for(; it != it_end; ++it) {
789 if (BaseSelectionRule::kNoMatch != it->Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name, "", false)) {
790 if (it->GetSelected() == BaseSelectionRule::kNo) {
791 // The rule did explicitly request to not select this entity.
792 return nullptr;
793 } else {
794 selector = &(*it);
795 }
796 }
797 }
798
799 return selector;
800}
801
802const BaseSelectionRule *SelectionRules::IsFunSelected(const clang::FunctionDecl *D, const std::string &qual_name) const
803{
804
805 if (fFunctionSelectionRules.size() == 0 ||
806 D->getPrimaryTemplate() != nullptr ||
807 llvm::isa<clang::CXXMethodDecl>(D)) return nullptr;
808
809 std::string prototype;
810 GetFunctionPrototype(D, prototype);
811 prototype = qual_name + prototype;
812
813 const BaseSelectionRule *selector = nullptr;
814 // iterate through all the rules
815 // we call this method only for genrefex variables, functions and enums - it is simpler than the class case:
816 // if we have No - it is veto even if we have explicit yes as well
817 for (const auto & rule : fFunctionSelectionRules) {
818 if (BaseSelectionRule::kNoMatch != rule.Match(D, qual_name, prototype, false)) {
819 if (rule.GetSelected() == BaseSelectionRule::kNo) {
820 // The rule did explicitly request to not select this entity.
821 return nullptr;
822 } else {
823 selector = &(rule);
824 }
825 }
826 }
827
828 return selector;
829}
830
831const BaseSelectionRule *SelectionRules::IsEnumSelected(const clang::EnumDecl* D, const std::string& qual_name) const
832{
833 const BaseSelectionRule *selector = nullptr;
834
835 // iterate through all the rules
836 // we call this method only for genrefex variables, functions and enums - it is simpler than the class case:
837 // if we have No - it is veto even if we have explicit yes as well
838 for(const auto& rule: fEnumSelectionRules) {
839 if (BaseSelectionRule::kNoMatch != rule.Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name, "", false)) {
840 if (rule.GetSelected() == BaseSelectionRule::kNo) {
841 // The rule did explicitly request to not select this entity.
842 return nullptr;
843 } else {
844 selector = &rule;
845 }
846 }
847 }
848
849 return selector;
850}
851
852const BaseSelectionRule *SelectionRules::IsLinkdefVarSelected(const clang::VarDecl* D, const std::string& qual_name) const
853{
854
855
856 const BaseSelectionRule *selector = nullptr;
857 int fImplNo = 0;
858 const BaseSelectionRule *explicit_selector = nullptr;
859
860 std::string name_value;
861 std::string pattern_value;
862 for(auto& selRule: fVariableSelectionRules) {
864 = selRule.Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name, "", false);
865 if (match != BaseSelectionRule::kNoMatch) {
866 if (selRule.GetSelected() == BaseSelectionRule::kYes) {
867 // explicit rules are with stronger priority in rootcint
868 if (IsLinkdefFile()){
869 if (match == BaseSelectionRule::kName) {
870 explicit_selector = &selRule;
871 } else if (match == BaseSelectionRule::kPattern) {
872 if (selRule.GetAttributeValue("pattern", pattern_value)) {
873 explicit_selector=&selRule;
874 // NOTE: Weird ... This is a strange definition of explicit.
875 //if (pattern_value != "*" && pattern_value != "*::*") explicit_selector = selRule;
876 }
877 }
878 }
879 }
880 else {
881 if (!IsLinkdefFile()) return nullptr;
882 else {
883 if (selRule.GetAttributeValue("pattern", pattern_value)) {
884 if (pattern_value == "*" || pattern_value == "*::*") ++fImplNo;
885 else
886 return nullptr;
887 }
888 else
889 return nullptr;
890 }
891 }
892 }
893 }
894
895 if (IsLinkdefFile()) {
896
897#ifdef SELECTION_DEBUG
898 std::cout<<"\n\tfYes = "<<fYes<<", fImplNo = "<<fImplNo<<std::endl;
899#endif
900
901 if (explicit_selector) return explicit_selector;
902 else if (fImplNo > 0) return nullptr;
903 else return selector;
904 }
905 else{
906 return selector;
907 }
908}
909
910const BaseSelectionRule *SelectionRules::IsLinkdefFunSelected(const clang::FunctionDecl* D, const std::string& qual_name) const
911{
912
913 if (fFunctionSelectionRules.size() == 0 ||
914 D->getPrimaryTemplate() != nullptr ||
915 llvm::isa<clang::CXXMethodDecl>(D)) return nullptr;
916
917 std::string prototype;
918
919 GetFunctionPrototype(D, prototype);
920 prototype = qual_name + prototype;
921
922 const BaseSelectionRule *selector = nullptr;
923 int fImplNo = 0;
924 const BaseSelectionRule *explicit_selector = nullptr;
925
926 std::string pattern_value;
927 for(auto& selRule : fFunctionSelectionRules) {
929 = selRule.Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name, prototype, false);
930 if (match != BaseSelectionRule::kNoMatch) {
931 if (selRule.GetSelected() == BaseSelectionRule::kYes) {
932 // explicit rules are with stronger priority in rootcint
933 if (IsLinkdefFile()){
934 if (match == BaseSelectionRule::kName) {
935 explicit_selector = &selRule;
936 } else if (match == BaseSelectionRule::kPattern) {
937 if (selRule.GetAttributeValue("pattern", pattern_value)) {
938 explicit_selector = &selRule;
939 // NOTE: Weird ... This is a strange definition of explicit.
940 //if (pattern_value != "*" && pattern_value != "*::*") explicit_selector = &selRule;
941 }
942 }
943 }
944 }
945 else {
946 if (!IsLinkdefFile()) return nullptr;
947 else {
948 if (selRule.GetAttributeValue("pattern", pattern_value)) {
949 if (pattern_value == "*" || pattern_value == "*::*") ++fImplNo;
950 else
951 return nullptr;
952 }
953 else
954 return nullptr;
955 }
956 }
957 }
958 }
959
960 if (IsLinkdefFile()) {
961 if (explicit_selector) return explicit_selector;
962 else if (fImplNo > 0) return nullptr;
963 else return selector;
964 }
965 else{
966 return selector;
967 }
968}
969
970const BaseSelectionRule *SelectionRules::IsLinkdefEnumSelected(const clang::EnumDecl* D, const std::string& qual_name) const
971{
972 std::list<VariableSelectionRule>::const_iterator it;
973 std::list<VariableSelectionRule>::const_iterator it_end;
974
975 it = fEnumSelectionRules.begin();
976 it_end = fEnumSelectionRules.end();
977
978 const BaseSelectionRule *selector = nullptr;
979 int fImplNo = 0;
980 const BaseSelectionRule *explicit_selector = nullptr;
981
982 std::string name_value;
983 std::string pattern_value;
984 for(; it != it_end; ++it) {
986 it->Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name, "", false);
987 if (match != BaseSelectionRule::kNoMatch) {
988 if (it->GetSelected() == BaseSelectionRule::kYes) {
989 // explicit rules are with stronger priority in rootcint
990 if (IsLinkdefFile()){
991 if (match == BaseSelectionRule::kName){
992 explicit_selector = &(*it);
993 } else if (match == BaseSelectionRule::kPattern &&
994 it->GetAttributeValue("pattern", pattern_value)) {
995 // Note: Weird ... This is a strange definition of explicit.
996 if (pattern_value != "*" && pattern_value != "*::*") explicit_selector = &(*it);
997 }
998 }
999 }
1000 else {
1001 if (!IsLinkdefFile()) return nullptr;
1002 else {
1003 if (it->GetAttributeValue("pattern", pattern_value)) {
1004 if (pattern_value == "*" || pattern_value == "*::*") ++fImplNo;
1005 else
1006 return nullptr;
1007 }
1008 else
1009 return nullptr;
1010 }
1011 }
1012 }
1013 }
1014
1015 if (IsLinkdefFile()) {
1016
1017#ifdef SELECTION_DEBUG
1018 std::cout<<"\n\tfYes = "<<fYes<<", fImplNo = "<<fImplNo<<std::endl;
1019#endif
1020
1021 if (explicit_selector) return explicit_selector;
1022 else if (fImplNo > 0) return nullptr;
1023 else return selector;
1024 }
1025 else{
1026 return selector;
1027 }
1028}
1029
1030// In rootcint we could select and deselect methods independantly of the class/struct/union rules
1031// That's why we first have to check the explicit rules for the functions - to see if there
1032// is rule corresponding to our method.
1033// Which is more - if we have (and we can have) a pattern for the parent class, than a pattern for the
1034// nested class, than a pattern for certain methods in the nested class, than a rule for a
1035// method (name or prototype) in the nested class - the valid rule is the last one.
1036// This is true irrespective of the rules (select/deselect). This is not the case for genreflex -
1037// in genreflex if there is a pattern deselecting something even if we have an explicit rule
1038// to select it, it still will not be selected.
1039// This method (isLinkdefMethodSelected()) might be incomplete (but I didn't have the time to think
1040// of anything better)
1041//
1042
1043const BaseSelectionRule *SelectionRules::IsLinkdefMethodSelected(const clang::Decl* D, const std::string& qual_name) const
1044{
1045 std::list<FunctionSelectionRule>::const_iterator it = fFunctionSelectionRules.begin();
1046 std::list<FunctionSelectionRule>::const_iterator it_end = fFunctionSelectionRules.end();
1047 std::string prototype;
1048
1049 if (const clang::FunctionDecl* F = llvm::dyn_cast<clang::FunctionDecl> (D))
1050 GetFunctionPrototype(F, prototype);
1051 prototype = qual_name + prototype;
1052
1053#ifdef SELECTION_DEBUG
1054 std::cout<<"\tFunction prototype = "<<prototype<<std::endl;
1055#endif
1056
1057 int impl_r_No = 0, impl_rr_No = 0;
1058 const BaseSelectionRule *explicit_r = nullptr;
1059 const BaseSelectionRule *implicit_r = nullptr;
1060 const BaseSelectionRule *implicit_rr = nullptr;
1061
1062 if (D->getKind() == clang::Decl::CXXMethod){
1063 // we first check the explicit rules for the method (in case of constructors and destructors we check the parent)
1064 std::string pat_value;
1065 for(; it != it_end; ++it) {
1067 = it->Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name, prototype, false);
1068
1069 if (match == BaseSelectionRule::kName) {
1070 // here I should implement my implicit/explicit thing
1071 // I have included two levels of implicitness - "A::Get_*" is stronger than "*"
1072 explicit_r = &(*it);
1073 if (it->GetSelected() != BaseSelectionRule::kYes) {
1074
1075#ifdef SELECTION_DEBUG
1076 std::cout<<"\tExplicit rule BaseSelectionRule::kNo found"<<std::endl;
1077#endif
1078
1079 return nullptr; // == explicit BaseSelectionRule::kNo
1080
1081 }
1082 } else if (match == BaseSelectionRule::kPattern) {
1083
1084 if (it->GetAttributeValue("pattern", pat_value)) {
1085 if (pat_value == "*") continue; // we discard the global selection rules
1086
1087 std::string par_name, par_qual_name;
1088 GetParentName(D, par_name, par_qual_name);
1089 std::string par_pat = par_qual_name + "::*";
1090
1091 if (pat_value == par_pat) {
1092 implicit_rr = &(*it);
1093 if (it->GetSelected() == BaseSelectionRule::kYes) {
1094
1095#ifdef SELECTION_DEBUG
1096 std::cout<<"Implicit_rr rule ("<<pat_value<<"), selected = "<<selected<<std::endl;
1097#endif
1098
1099 }
1100 else {
1101
1102#ifdef SELECTION_DEBUG
1103 std::cout<<"Implicit_rr rule ("<<pat_value<<"), selected = "<<selected<<std::endl;
1104#endif
1105
1106 ++impl_rr_No;
1107 }
1108 }
1109 else {
1110 implicit_r = &(*it);
1111 if (it->GetSelected() == BaseSelectionRule::kYes) {
1112
1113#ifdef SELECTION_DEBUG
1114 std::cout<<"Implicit_r rule ("<<pat_value<<"), selected = "<<selected<<std::endl;
1115#endif
1116
1117 }
1118 else {
1119
1120#ifdef SELECTION_DEBUG
1121 std::cout<<"Implicit_r rule ("<<pat_value<<"), selected = "<<selected<<std::endl;
1122#endif
1123
1124 ++impl_r_No;
1125 }
1126 }
1127 }
1128 }
1129 }
1130 }
1131 if (explicit_r){
1132
1133#ifdef SELECTION_DEBUG
1134 std::cout<<"\tExplicit rule BaseSelectionRule::BaseSelectionRule::kYes found"<<std::endl;
1135#endif
1136
1137 return explicit_r; // if we have excplicit BaseSelectionRule::kYes
1138 }
1139 else if (implicit_rr) {
1140 if (impl_rr_No > 0) {
1141
1142#ifdef SELECTION_DEBUG
1143 std::cout<<"\tImplicit_rr rule BaseSelectionRule::kNo found"<<std::endl;
1144#endif
1145
1146 return nullptr;
1147 }
1148 else {
1149
1150#ifdef SELECTION_DEBUG
1151 std::cout<<"\tImplicit_rr rule BaseSelectionRule::kYes found"<<std::endl;
1152#endif
1153
1154 return implicit_rr;
1155 }
1156 }
1157 else if (implicit_r) {
1158 if (impl_r_No > 0) {
1159
1160#ifdef SELECTION_DEBUG
1161 std::cout<<"\tImplicit_r rule BaseSelectionRule::kNo found"<<std::endl;
1162#endif
1163
1164 return nullptr;
1165 }
1166 else {
1167
1168#ifdef SELECTION_DEBUG
1169 std::cout<<"\tImplicit_r rule BaseSelectionRule::kYes found"<<std::endl;
1170#endif
1171
1172 return implicit_r;
1173 }
1174 }
1175 else {
1176
1177#ifdef SELECTION_DEBUG
1178 std::cout<<"\tChecking parent class rules"<<std::endl;
1179#endif
1180 // check parent
1181
1182
1183 std::string parent_name, parent_qual_name;
1184 if (!GetParentName(D, parent_name, parent_qual_name)) return nullptr;
1185
1186 const BaseSelectionRule *selector = nullptr;
1187 int fImplNo = 0;
1188 const BaseSelectionRule *explicit_selector = nullptr;
1189
1190 // the same as with isClass selected
1191 // I wanted to use GetParentDecl and then to pass is to isClassSelected because I didn't wanted to repeat
1192 // code but than GetParentDecl crashes (or returns non-sence Decl) for the built-in structs (__va_*)
1193 std::list<ClassSelectionRule>::const_iterator it = fClassSelectionRules.begin();
1194 std::string name_value;
1195 std::string pattern_value;
1196 for(; it != fClassSelectionRules.end(); ++it) {
1198 = it->Match(llvm::dyn_cast<clang::NamedDecl>(D), parent_qual_name, "", true); // == BaseSelectionRule::kYes
1199
1200 if (match != BaseSelectionRule::kNoMatch) {
1201 if (it->GetSelected() == BaseSelectionRule::kYes) {
1202 selector = &(*it);
1203
1204 if (match == BaseSelectionRule::kName) {
1205 explicit_selector = &(*it);
1206 } else if (match == BaseSelectionRule::kPattern) {
1207 if (it->GetAttributeValue("pattern", pattern_value)) {
1208 // NOTE: weird ...
1209 if (pattern_value != "*" && pattern_value != "*::*") explicit_selector = &(*it);
1210 }
1211 }
1212 }
1213 else { // == BaseSelectionRule::kNo
1214
1215 if (it->GetAttributeValue("pattern", pattern_value)) {
1216 if (pattern_value == "*" || pattern_value == "*::*") ++fImplNo;
1217 else
1218 return nullptr;
1219 }
1220 else
1221 return nullptr;
1222 }
1223 }
1224 }
1225
1226#ifdef SELECTION_DEBUG
1227 std::cout<<"\n\tfYes = "<<fYes<<", fImplNo = "<<fImplNo<<std::endl;
1228#endif
1229
1230 if (explicit_selector) {
1231
1232#ifdef SELECTION_DEBUG
1233 std::cout<<"\tReturning Yes"<<std::endl;
1234#endif
1235
1236 return explicit_selector;
1237 }
1238 else if (fImplNo > 0) {
1239#ifdef SELECTION_DEBUG
1240 std::cout<<"\tReturning No"<<std::endl;
1241#endif
1242
1243 return nullptr;
1244 }
1245 else {
1246
1247#ifdef SELECTION_DEBUG
1248 std::cout<<"\tReturning Yes"<<std::endl;
1249#endif
1250
1251 return selector;
1252 }
1253 }
1254
1255 return nullptr;
1256
1257}
1258
1259const BaseSelectionRule *SelectionRules::IsMemberSelected(const clang::Decl* D, const std::string& str_name) const
1260{
1261 std::string parent_name;
1262 std::string parent_qual_name;
1263
1264 if (IsParentClass(D))
1265 {
1266 if (!GetParentName(D, parent_name, parent_qual_name)) return nullptr;
1267
1268 const BaseSelectionRule *selector = nullptr;
1269 Int_t fImplNo = 0;
1270 const BaseSelectionRule *explicit_selector = nullptr;
1271
1272 //DEBUG std::cout<<"\n\tParent is class";
1273 std::list<ClassSelectionRule>::const_iterator it = fClassSelectionRules.begin();
1274 std::string name_value;
1275 std::string pattern_value;
1276 for(; it != fClassSelectionRules.end(); ++it) {
1278 = it->Match(llvm::dyn_cast<clang::NamedDecl>(D), parent_qual_name, "", false);
1279
1280 if (match != BaseSelectionRule::kNoMatch) {
1281 if (it->GetSelected() == BaseSelectionRule::kYes) {
1282 selector = &(*it);
1283 if (IsLinkdefFile()) {
1284 if (match == BaseSelectionRule::kName) {
1285 explicit_selector = &(*it);
1286 } else if (match == BaseSelectionRule::kPattern) {
1287 if (it->GetAttributeValue("pattern", pattern_value)) {
1288 // NOTE: Weird ... This is a strange definition of explicit.
1289 if (pattern_value != "*" && pattern_value != "*::*") explicit_selector = &(*it);
1290 }
1291 }
1292 }
1293 } else if (it->GetSelected() == BaseSelectionRule::kNo) {
1294 if (!IsLinkdefFile()) {
1295 if (match != BaseSelectionRule::kFile) {
1296
1297#ifdef SELECTION_DEBUG
1298 std::cout<<"\tNo returned"<<std::endl;
1299#endif
1300
1301 return nullptr; // in genreflex we can't have that situation
1302 }
1303 }
1304 else {
1305 if (match == BaseSelectionRule::kPattern && it->GetAttributeValue("pattern", pattern_value)) {
1306 if (pattern_value == "*" || pattern_value == "*::*") ++fImplNo;
1307 else
1308 return nullptr;
1309 }
1310 else
1311 return nullptr;
1312 }
1313 }
1314 else if (it->GetSelected() == BaseSelectionRule::kDontCare) // - we check the method and field selection rules for the class
1315 {
1316 if (!it->HasMethodSelectionRules() && !it->HasFieldSelectionRules()) {
1317
1318#ifdef SELECTION_DEBUG
1319 std::cout<<"\tNo fields and methods"<<std::endl;
1320#endif
1321
1322 return nullptr; // == BaseSelectionRule::kNo
1323 }
1324 else {
1325 clang::Decl::Kind kind = D->getKind();
1326 if (kind == clang::Decl::Field || kind == clang::Decl::CXXMethod || kind == clang::Decl::CXXConstructor || kind == clang::Decl::CXXDestructor){
1327 std::list<VariableSelectionRule> members;
1328 std::list<VariableSelectionRule>::iterator mem_it;
1329 std::list<VariableSelectionRule>::iterator mem_it_end;
1330 std::string prototype;
1331
1332 if (kind == clang::Decl::Field) {
1333 members = it->GetFieldSelectionRules();
1334 }
1335 else {
1336 if (const clang::FunctionDecl* F = llvm::dyn_cast<clang::FunctionDecl> (D)){
1337 GetFunctionPrototype(F, prototype);
1338 prototype = str_name + prototype;
1339 }
1340 else{
1341 ROOT::TMetaUtils::Warning("","Warning: could not cast Decl to FunctionDecl\n");
1342 }
1343 members = it->GetMethodSelectionRules();
1344 }
1345 mem_it = members.begin();
1346 mem_it_end = members.end();
1347 for (; mem_it != mem_it_end; ++mem_it) {
1348 if (BaseSelectionRule::kName == mem_it->Match(llvm::dyn_cast<clang::NamedDecl>(D), str_name, prototype, false)) {
1349 if (mem_it->GetSelected() == BaseSelectionRule::kNo) return nullptr;
1350 }
1351 }
1352 }
1353 }
1354 }
1355 }
1356 }
1357
1358 if (IsLinkdefFile()) {
1359
1360#ifdef SELECTION_DEBUG
1361 std::cout<<"\n\tfYes = "<<fYes<<", fImplNo = "<<fImplNo<<std::endl;
1362#endif
1363
1364 if (explicit_selector) {
1365#ifdef SELECTION_DEBUG
1366 std::cout<<"\tReturning Yes"<<std::endl;
1367#endif
1368
1369 return explicit_selector;
1370 }
1371 else if (fImplNo > 0) {
1372
1373#ifdef SELECTION_DEBUG
1374 std::cout<<"\tReturning No"<<std::endl;
1375#endif
1376
1377 return nullptr;
1378 }
1379 else {
1380
1381#ifdef SELECTION_DEBUG
1382 std::cout<<"\tReturning Yes"<<std::endl;
1383#endif
1384
1385 return selector;
1386 }
1387 }
1388 else {
1389
1390 return selector;
1391 }
1392 }
1393 else {
1394 return nullptr;
1395 }
1396}
1397
1399 for(auto&& rule : fClassSelectionRules){
1400 if (BaseSelectionRule::kNo!=rule.GetSelected() && !rule.GetMatchFound() /* && !GetHasFileNameRule() */ ) {
1401 std::string name;
1402 if (rule.GetAttributeValue("pattern", name)) {
1403 // keep it
1404 } else if (rule.GetAttributeValue("name", name)) {
1405 // keept it
1406 } else {
1407 name.clear();
1408 }
1409 std::string file_name_value;
1410 if (!rule.GetAttributeValue("file_name", file_name_value)) file_name_value.clear();
1411
1412 if (!file_name_value.empty()) {
1413 // don't complain about defined_in rules
1414 continue;
1415 }
1416
1417 const char* attrName = "class";
1418 const char* attrVal = nullptr;
1419 if (!name.empty()) attrVal = name.c_str();
1420
1421 ROOT::TMetaUtils::Warning(nullptr,"Unused %s rule: %s\n", attrName, attrVal);
1422 }
1423 }
1424
1425 for(auto&& rule : fVariableSelectionRules){
1426 if (!rule.GetMatchFound() && !GetHasFileNameRule()) {
1427 std::string name;
1428 if (rule.GetAttributeValue("pattern", name)) {
1429 // keep it
1430 } else if (rule.GetAttributeValue("name", name)) {
1431 // keept it
1432 } else {
1433 name.clear();
1434 }
1435 ROOT::TMetaUtils::Warning("","Unused variable rule: %s\n",name.c_str());
1436 if (name.empty()) {
1437 rule.PrintAttributes(std::cout,3);
1438 }
1439 }
1440 }
1441
1442#if defined(R__MUST_REVISIT)
1443#if R__MUST_REVISIT(6,2)
1444ROOT::TMetaUtils::Warning("SelectionRules::AreAllSelectionRulesUsed",
1445"Warnings concerning non matching selection rules are suppressed. An action is to be taken.\n");
1446#endif
1447#endif
1448// for(const auto& selRule: fFunctionSelectionRules) {
1449// if (!selRule.GetMatchFound() && !GetHasFileNameRule()) {
1450// // Here the slow methods can be used
1451// std::string name;
1452// if (selRule.GetAttributeValue("proto_pattern", name)) {
1453// // keep it
1454// } else if (selRule.GetAttributeValue("proto_name", name)) {
1455// // keep it
1456// } else if (selRule.GetAttributeValue("pattern", name)) {
1457// // keep it
1458// } else if (selRule.GetAttributeValue("name", name)) {
1459// // keept it
1460// } else {
1461// name.clear();
1462// }
1463// // Make it soft, no error - just warnings
1464// std::cout<<"Warning - unused function rule: "<<name<<std::endl;
1465// // if (IsSelectionXMLFile()){
1466// // std::cout<<"Warning - unused function rule: "<<name<<std::endl;
1467// // }
1468// // else {
1469// // std::cout<<"Error - unused function rule: "<<name<<std::endl;
1470// // }
1471// if (name.length() == 0) {
1472// selRule.PrintAttributes(std::cout,3);
1473// }
1474// }
1475//
1476// }
1477
1478
1479#if Enums_rules_becomes_useful_for_rootcling
1480 for(auto&& rule : fEnumSelectionRules){
1481 if (!rule.GetMatchFound() && !GetHasFileNameRule()) {
1482 std::string name;
1483 if (rule.GetAttributeValue("pattern", name)) {
1484 // keep it
1485 } else if (rule.GetAttributeValue("name", name)) {
1486 // keept it
1487 } else {
1488 name.clear();
1489 }
1490
1491 ROOT::TMetaUtils::Warning("","Unused enum rule: %s\n",name.c_str());
1492
1493 if (name.empty()){
1494 rule.PrintAttributes(std::cout,3);
1495 }
1496 }
1497 }
1498#endif
1499 return true;
1500}
1501
1502bool SelectionRules::SearchNames(cling::Interpreter &interp)
1503{
1504 // std::cout<<"Searching Names In Selection Rules:"<<std::endl;
1505 for(std::list<ClassSelectionRule>::iterator it = fClassSelectionRules.begin(),
1506 end = fClassSelectionRules.end();
1507 it != end;
1508 ++it) {
1509 if (it->HasAttributeWithName("name")) {
1510 std::string name_value;
1511 it->GetAttributeValue("name", name_value);
1512 // In Class selection rules, we should be interested in scopes.
1513 const clang::Type *typeptr = nullptr;
1514 const clang::CXXRecordDecl *target
1515 = ROOT::TMetaUtils::ScopeSearch(name_value.c_str(), interp,
1516 true /*diag*/, &typeptr);
1517 if (target) {
1518 it->SetCXXRecordDecl(target,typeptr);
1519 }
1520 }
1521 }
1522 return true;
1523}
1524
1525
1527{
1528 // Fill the cache of every selection rule
1529 for (auto& rule : fClassSelectionRules) rule.FillCache();
1530 for (auto& rule : fFunctionSelectionRules) rule.FillCache();
1531 for (auto& rule : fVariableSelectionRules) rule.FillCache();
1532 for (auto& rule : fEnumSelectionRules) rule.FillCache();
1533}
1534
1535
const clang::CXXRecordDecl * R__ScopeSearch(const char *name, const clang::Type **resultType=nullptr)
static bool Implies(const ClassSelectionRule &patternRule, const ClassSelectionRule &nameRule)
static bool HasDuplicate(RULE *rule, std::unordered_map< std::string, RULE * > &storedRules, const std::string &attrName)
static int CheckDuplicatesImp(RULESCOLLECTION &rules)
#define N
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t target
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:110
const std::string & GetAttributePattern() const
bool HasInterpreter() const
EMatchType Match(const clang::NamedDecl *decl, const std::string &name, const std::string &prototype, bool isLinkdef) const
const std::string & GetAttributeName() const
const AttributesMap_t & GetAttributes() const
ESelect GetSelected() const
void AddTemplAndNargsToKeep(const clang::ClassTemplateDecl *templ, unsigned int i)
const BaseSelectionRule * IsLinkdefVarSelected(const clang::VarDecl *D, const std::string &qual_name) const
void GetDeclQualName(const clang::Decl *D, std::string &qual_name) const
const BaseSelectionRule * IsVarSelected(const clang::VarDecl *D, const std::string &qual_name) const
const BaseSelectionRule * IsFunSelected(const clang::FunctionDecl *D, const std::string &qual_name) const
void AddVariableSelectionRule(const VariableSelectionRule &varSel)
void AddClassSelectionRule(const ClassSelectionRule &classSel)
std::list< VariableSelectionRule > fVariableSelectionRules
List of the global variables selection rules.
bool AreAllSelectionRulesUsed() const
long int fRulesCounter
bool GetFunctionPrototype(const clang::FunctionDecl *F, std::string &prototype) const
bool SearchNames(cling::Interpreter &interp)
bool GetParentName(const clang::Decl *D, std::string &parent_name, std::string &parent_qual_name) const
std::list< FunctionSelectionRule > fFunctionSelectionRules
List of the global functions selection rules.
const BaseSelectionRule * IsLinkdefFunSelected(const clang::FunctionDecl *D, const std::string &qual_name) const
std::list< EnumSelectionRule > fEnumSelectionRules
List of the enums selection rules.
void PrintSelectionRules() const
bool GetHasFileNameRule() const
ROOT::TMetaUtils::TNormalizedCtxt & fNormCtxt
bool IsLinkdefFile() const
bool GetDeclName(const clang::Decl *D, std::string &name, std::string &qual_name) const
void AddEnumSelectionRule(const EnumSelectionRule &enumSel)
const BaseSelectionRule * IsMemberSelected(const clang::Decl *D, const std::string &str_name) const
bool IsSelectionXMLFile() const
bool IsParentClass(const clang::Decl *D) const
const BaseSelectionRule * IsLinkdefEnumSelected(const clang::EnumDecl *D, const std::string &qual_name) const
void AddFunctionSelectionRule(const FunctionSelectionRule &funcSel)
const BaseSelectionRule * IsEnumSelected(const clang::EnumDecl *D, const std::string &qual_name) const
const ClassSelectionRule * IsDeclSelected(const clang::RecordDecl *D, bool includeTypedefRule) const
const ClassSelectionRule * IsNamespaceSelected(const clang::Decl *D, const std::string &qual_name) const
std::list< ClassSelectionRule > fClassSelectionRules
List of the class selection rules.
cling::Interpreter & fInterp
const BaseSelectionRule * IsLinkdefMethodSelected(const clang::Decl *D, const std::string &qual_name) const
const ClassSelectionRule * IsClassSelected(const clang::Decl *D, const std::string &qual_name, bool includeTypedefRule) const
#define F(x, y, z)
#define I(x, y, z)
clang::RecordDecl * GetUnderlyingRecordDecl(clang::QualType type)
void Error(const char *location, const char *fmt,...)
void Info(const char *location, const char *fmt,...)
void GetNormalizedName(std::string &norm_name, const clang::QualType &type, const cling::Interpreter &interpreter, const TNormalizedCtxt &normCtxt)
Return the type name normalized for ROOT, keeping only the ROOT opaque typedef (Double32_t,...
void ReplaceAll(std::string &str, const std::string &from, const std::string &to, bool recurse=false)
void Warning(const char *location, const char *fmt,...)
const clang::CXXRecordDecl * ScopeSearch(const char *name, const cling::Interpreter &gInterp, bool diagnose, const clang::Type **resultType)
Return the scope corresponding to 'name' or std::'name'.
bool areEqual(const RULE *r1, const RULE *r2, bool moduloNameOrPattern=false)