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 = 0) ;
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 0;
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 0;
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 0;
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 0;
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()) {
434 name = N->getNameAsString();
435 }
436 else if (N->isCXXClassMember()) { // for constructors, destructors, operator=, etc. methods
437 name = N->getNameAsString(); // we use this (unefficient) method to Get the name in that case
438 }
439 llvm::raw_string_ostream stream(qual_name);
440 N->getNameForDiagnostic(stream,N->getASTContext().getPrintingPolicy(),true);
441 return true;
442}
443
444void SelectionRules::GetDeclQualName(const clang::Decl* D, std::string& qual_name) const{
445 const clang::NamedDecl* N = static_cast<const clang::NamedDecl*> (D);
446 llvm::raw_string_ostream stream(qual_name);
447 N->getNameForDiagnostic(stream,N->getASTContext().getPrintingPolicy(),true);
448 }
449
450bool SelectionRules::GetFunctionPrototype(const clang::FunctionDecl* F, std::string& prototype) const {
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 (const clang::TagDecl* T
500 = llvm::dyn_cast<clang::TagDecl>(D->getDeclContext()))
501 return T->isClass() || T->isStruct();
502 return false;
503}
504
505
506bool SelectionRules::IsParentClass(const clang::Decl* D, std::string& parent_name, std::string& parent_qual_name) const
507{
508 if (const clang::TagDecl* parent
509 = llvm::dyn_cast<clang::TagDecl>(D->getDeclContext())) {
510 if (parent->isClass()|| parent->isStruct()) {
511 GetDeclName(parent, parent_name, parent_qual_name);
512 return true;
513 }
514 }
515 return false;
516}
517
518bool SelectionRules::GetParentName(const clang::Decl* D, std::string& parent_name, std::string& parent_qual_name) const
519{
520 if (const clang::RecordDecl* parent
521 = llvm::dyn_cast<clang::RecordDecl>(D->getDeclContext())) {
522 GetDeclName(parent, parent_name, parent_qual_name);
523 return true;
524 }
525 return false;
526}
527
528/* This is the method that crashes
529 bool SelectionRules::GetParent(clang::Decl* D, clang::Decl* parent)
530 {
531 clang::DeclContext *ctx = D->GetDeclContext();
532
533 if (ctx->isRecord()){
534 //DEBUG std::cout<<"\n\tDeclContext is Record";
535 parent = llvm::dyn_cast<clang::Decl> (ctx);
536 if (!parent) {
537 return false;
538 }
539 else {
540 return true;
541 }
542 }
543 else return false;
544 }
545 */
546
547
548// isClassSelected checks if a class is selected or not. Thre is a difference between the
549// behaviour of rootcint and genreflex especially with regard to class pattern processing.
550// In genreflex if we have <class pattern = "*" /> this will select all the classes
551// (and structs) found in the header file. In rootcint if we have something similar, i.e.
552// #pragma link C++ class *, we will select only the outer classes - for the nested
553// classes we have to specifie #pragma link C++ class *::*. And yet this is only valid
554// for one level of nesting - if you need it for many levels of nesting, you will
555// probably have to implement it yourself.
556// Here the idea is the following - we traverse the list of class selection rules.
557// For every class we check do we have a class selection rule. We use here the
558// method isSelected() (defined in BaseSelectionRule.cxx). This method returns true
559// only if we have class selection rule which says "Select". Otherwise it returns
560// false - in which case we have to check wether we found a class selection rule
561// which says "Veto" (noName = false and don't Care = false; OR noName = false and
562// don't Care = true and we don't have neither method nor field selection rules -
563// which is for the selection.xml file case). If noName is true than we just continue -
564// this means that the current class selection rule isn't applicable for this class.
565
566const ClassSelectionRule *SelectionRules::IsNamespaceSelected(const clang::Decl* D, const std::string& qual_name) const
567{
568 const clang::NamespaceDecl* N = llvm::dyn_cast<clang::NamespaceDecl> (D); //TagDecl has methods to understand of what kind is the Decl
569 if (N==0) {
570 std::cout<<"\n\tCouldn't cast Decl to NamespaceDecl";
571 return 0;
572 }
573
574 const ClassSelectionRule *selector = 0;
575 int fImplNo = 0;
576 const ClassSelectionRule *explicit_selector = 0;
577 const ClassSelectionRule *specific_pattern_selector = 0;
578 int fFileNo = 0;
579
580 // NOTE: should we separate namespaces from classes in the rules?
581 std::list<ClassSelectionRule>::const_iterator it = fClassSelectionRules.begin();
582 // iterate through all class selection rles
583 std::string name_value;
584 std::string pattern_value;
586 for(; it != fClassSelectionRules.end(); ++it) {
587
588 match = it->Match(N,qual_name,"",IsLinkdefFile());
589
590 if (match != BaseSelectionRule::kNoMatch) {
591 // If we have a match.
592 if (it->GetSelected() == BaseSelectionRule::kYes) {
593 selector = &(*it);
594 if (IsLinkdefFile()){
595 // rootcint prefers explicit rules over pattern rules
596 if (match == BaseSelectionRule::kName) {
597 explicit_selector = &(*it);
598 } else if (match == BaseSelectionRule::kPattern) {
599 // NOTE: weird ...
600 if (it->GetAttributeValue("pattern", pattern_value) &&
601 pattern_value != "*" && pattern_value != "*::*") specific_pattern_selector = &(*it);
602 }
603 }
604 } else if (it->GetSelected() == BaseSelectionRule::kNo) {
605 if (!IsLinkdefFile()) {
606 // in genreflex - we could explicitly select classes from other source files
607 if (match == BaseSelectionRule::kFile) ++fFileNo; // if we have veto because of class defined in other source file -> implicit No
608 else {
609
610#ifdef SELECTION_DEBUG
611 std::cout<<"\tNo returned"<<std::endl;
612#endif
613
614 return 0; // explicit No returned
615 }
616 }
617 if (match == BaseSelectionRule::kPattern) {
618 //this is for the Linkdef selection
619 if (it->GetAttributeValue("pattern", pattern_value) &&
620 (pattern_value == "*" || pattern_value == "*::*")) ++fImplNo;
621 else
622 return 0;
623 }
624 else
625 return 0;
626 }
627 else if (it->GetSelected() == BaseSelectionRule::kDontCare && !(it->HasMethodSelectionRules()) && !(it->HasFieldSelectionRules())) {
628
629#ifdef SELECTION_DEBUG
630 std::cout<<"Empty dontC returned = No"<<std::endl;
631#endif
632
633 return 0;
634 }
635 }
636 }
637 if (IsLinkdefFile()) {
638 // for rootcint explicit (name) Yes is stronger than implicit (pattern) No which is stronger than implicit (pattern) Yes
639
640#ifdef SELECTION_DEBUG
641 std::cout<<"\n\tfYes = "<<fYes<<", fImplNo = "<<fImplNo<<std::endl;
642#endif
643
644 if (explicit_selector) return explicit_selector;
645 else if (specific_pattern_selector) return specific_pattern_selector;
646 else if (fImplNo > 0) return 0;
647 else return selector;
648 }
649 else {
650 // for genreflex explicit Yes is stronger than implicit file No
651
652#ifdef SELECTION_DEBUG
653 std::cout<<"\n\tfYes = "<<fYes<<", fFileNo = "<<fFileNo<<std::endl;
654#endif
655
656 if (selector)
657 return selector;
658 else
659 return 0;
660 }
661
662}
663
664
665const ClassSelectionRule *SelectionRules::IsClassSelected(const clang::Decl* D, const std::string& qual_name, bool includeTypedefRule) const
666{
667 const clang::TagDecl* tagDecl = llvm::dyn_cast<clang::TagDecl> (D); //TagDecl has methods to understand of what kind is the Decl
668 const clang::TypedefNameDecl* typeDefNameDecl = llvm::dyn_cast<clang::TypedefNameDecl> (D);
669
670 if (!tagDecl && !typeDefNameDecl) { // Ill posed
671 ROOT::TMetaUtils::Error("SelectionRules::IsClassSelected",
672 "Cannot cast Decl to TagDecl and Decl is not a typedef.\n");
673 return 0;
674 }
675
676 if (!tagDecl && typeDefNameDecl){ // Let's try to fetch the underlying RecordDecl
677 clang::RecordDecl* recordDecl = ROOT::TMetaUtils::GetUnderlyingRecordDecl(typeDefNameDecl->getUnderlyingType());
678 if (!recordDecl){
679 ROOT::TMetaUtils::Error("SelectionRules::IsClassSelected",
680 "Cannot get RecordDecl behind TypedefDecl.\n");
681 return 0;
682 }
683 tagDecl = recordDecl;
684 }
685
686 // At this point tagDecl must be well defined
687 const bool isLinkDefFile = IsLinkdefFile();
688 if (!( isLinkDefFile || tagDecl->isClass() || tagDecl->isStruct() ))
689 return 0; // Union for Genreflex
690
691 const ClassSelectionRule *selector = 0;
692 int fImplNo = 0;
693 const ClassSelectionRule *explicit_selector = 0;
694 const ClassSelectionRule *specific_pattern_selector = 0;
695 int fFileNo = 0;
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 (match == BaseSelectionRule::kFile) ++fFileNo; // if we have veto because of class defined in other source file -> implicit No
740 else {
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 0;
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 = 0;
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 0;
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 = 0;
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 0;
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 = 0;
857 int fImplNo = 0;
858 const BaseSelectionRule *explicit_selector = 0;
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 0;
882 else {
883 if (selRule.GetAttributeValue("pattern", pattern_value)) {
884 if (pattern_value == "*" || pattern_value == "*::*") ++fImplNo;
885 else
886 return 0;
887 }
888 else
889 return 0;
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 0;
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 = 0;
923 int fImplNo = 0;
924 const BaseSelectionRule *explicit_selector = 0;
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 0;
947 else {
948 if (selRule.GetAttributeValue("pattern", pattern_value)) {
949 if (pattern_value == "*" || pattern_value == "*::*") ++fImplNo;
950 else
951 return 0;
952 }
953 else
954 return 0;
955 }
956 }
957 }
958 }
959
960 if (IsLinkdefFile()) {
961 if (explicit_selector) return explicit_selector;
962 else if (fImplNo > 0) return 0;
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 = 0;
979 int fImplNo = 0;
980 const BaseSelectionRule *explicit_selector = 0;
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 0;
1002 else {
1003 if (it->GetAttributeValue("pattern", pattern_value)) {
1004 if (pattern_value == "*" || pattern_value == "*::*") ++fImplNo;
1005 else
1006 return 0;
1007 }
1008 else
1009 return 0;
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 0;
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 expl_Yes = 0, impl_r_Yes = 0, impl_rr_Yes = 0;
1058 int impl_r_No = 0, impl_rr_No = 0;
1059 const BaseSelectionRule *explicit_r = 0;
1060 const BaseSelectionRule *implicit_r = 0;
1061 const BaseSelectionRule *implicit_rr = 0;
1062
1063 if (D->getKind() == clang::Decl::CXXMethod){
1064 // we first check the explicit rules for the method (in case of constructors and destructors we check the parent)
1065 std::string pat_value;
1066 for(; it != it_end; ++it) {
1068 = it->Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name, prototype, false);
1069
1070 if (match == BaseSelectionRule::kName) {
1071 // here I should implement my implicit/explicit thing
1072 // I have included two levels of implicitness - "A::Get_*" is stronger than "*"
1073 explicit_r = &(*it);
1074 if (it->GetSelected() == BaseSelectionRule::kYes) ++expl_Yes;
1075 else {
1076
1077#ifdef SELECTION_DEBUG
1078 std::cout<<"\tExplicit rule BaseSelectionRule::kNo found"<<std::endl;
1079#endif
1080
1081 return 0; // == explicit BaseSelectionRule::kNo
1082
1083 }
1084 } else if (match == BaseSelectionRule::kPattern) {
1085
1086 if (it->GetAttributeValue("pattern", pat_value)) {
1087 if (pat_value == "*") continue; // we discard the global selection rules
1088
1089 std::string par_name, par_qual_name;
1090 GetParentName(D, par_name, par_qual_name);
1091 std::string par_pat = par_qual_name + "::*";
1092
1093 if (pat_value == par_pat) {
1094 implicit_rr = &(*it);
1095 if (it->GetSelected() == BaseSelectionRule::kYes) {
1096
1097#ifdef SELECTION_DEBUG
1098 std::cout<<"Implicit_rr rule ("<<pat_value<<"), selected = "<<selected<<std::endl;
1099#endif
1100
1101 ++impl_rr_Yes;
1102 }
1103 else {
1104
1105#ifdef SELECTION_DEBUG
1106 std::cout<<"Implicit_rr rule ("<<pat_value<<"), selected = "<<selected<<std::endl;
1107#endif
1108
1109 ++impl_rr_No;
1110 }
1111 }
1112 else {
1113 implicit_r = &(*it);
1114 if (it->GetSelected() == BaseSelectionRule::kYes) {
1115
1116#ifdef SELECTION_DEBUG
1117 std::cout<<"Implicit_r rule ("<<pat_value<<"), selected = "<<selected<<std::endl;
1118#endif
1119
1120 ++impl_r_Yes;
1121 }
1122 else {
1123
1124#ifdef SELECTION_DEBUG
1125 std::cout<<"Implicit_r rule ("<<pat_value<<"), selected = "<<selected<<std::endl;
1126#endif
1127
1128 ++impl_r_No;
1129 }
1130 }
1131 }
1132 }
1133 }
1134 }
1135 if (explicit_r /*&& expl_Yes > 0*/){
1136
1137#ifdef SELECTION_DEBUG
1138 std::cout<<"\tExplicit rule BaseSelectionRule::BaseSelectionRule::kYes found"<<std::endl;
1139#endif
1140
1141 return explicit_r; // if we have excplicit BaseSelectionRule::kYes
1142 }
1143 else if (implicit_rr) {
1144 if (impl_rr_No > 0) {
1145
1146#ifdef SELECTION_DEBUG
1147 std::cout<<"\tImplicit_rr rule BaseSelectionRule::kNo found"<<std::endl;
1148#endif
1149
1150 return 0;
1151 }
1152 else {
1153
1154#ifdef SELECTION_DEBUG
1155 std::cout<<"\tImplicit_rr rule BaseSelectionRule::kYes found"<<std::endl;
1156#endif
1157
1158 return implicit_rr;
1159 }
1160 }
1161 else if (implicit_r) {
1162 if (impl_r_No > 0) {
1163
1164#ifdef SELECTION_DEBUG
1165 std::cout<<"\tImplicit_r rule BaseSelectionRule::kNo found"<<std::endl;
1166#endif
1167
1168 return 0;
1169 }
1170 else {
1171
1172#ifdef SELECTION_DEBUG
1173 std::cout<<"\tImplicit_r rule BaseSelectionRule::kYes found"<<std::endl;
1174#endif
1175
1176 return implicit_r;
1177 }
1178 }
1179 else {
1180
1181#ifdef SELECTION_DEBUG
1182 std::cout<<"\tChecking parent class rules"<<std::endl;
1183#endif
1184 // check parent
1185
1186
1187 std::string parent_name, parent_qual_name;
1188 if (!GetParentName(D, parent_name, parent_qual_name)) return 0;
1189
1190 const BaseSelectionRule *selector = 0;
1191 int fImplNo = 0;
1192 const BaseSelectionRule *explicit_selector = 0;
1193
1194 // the same as with isClass selected
1195 // I wanted to use GetParentDecl and then to pass is to isClassSelected because I didn't wanted to repeat
1196 // code but than GetParentDecl crashes (or returns non-sence Decl) for the built-in structs (__va_*)
1197 std::list<ClassSelectionRule>::const_iterator it = fClassSelectionRules.begin();
1198 std::string name_value;
1199 std::string pattern_value;
1200 for(; it != fClassSelectionRules.end(); ++it) {
1202 = it->Match(llvm::dyn_cast<clang::NamedDecl>(D), parent_qual_name, "", true); // == BaseSelectionRule::kYes
1203
1204 if (match != BaseSelectionRule::kNoMatch) {
1205 if (it->GetSelected() == BaseSelectionRule::kYes) {
1206 selector = &(*it);
1207
1208 if (match == BaseSelectionRule::kName) {
1209 explicit_selector = &(*it);
1210 } else if (match == BaseSelectionRule::kPattern) {
1211 if (it->GetAttributeValue("pattern", pattern_value)) {
1212 // NOTE: weird ...
1213 if (pattern_value != "*" && pattern_value != "*::*") explicit_selector = &(*it);
1214 }
1215 }
1216 }
1217 else { // == BaseSelectionRule::kNo
1218
1219 if (it->GetAttributeValue("pattern", pattern_value)) {
1220 if (pattern_value == "*" || pattern_value == "*::*") ++fImplNo;
1221 else
1222 return 0;
1223 }
1224 else
1225 return 0;
1226 }
1227 }
1228 }
1229
1230#ifdef SELECTION_DEBUG
1231 std::cout<<"\n\tfYes = "<<fYes<<", fImplNo = "<<fImplNo<<std::endl;
1232#endif
1233
1234 if (explicit_selector) {
1235
1236#ifdef SELECTION_DEBUG
1237 std::cout<<"\tReturning Yes"<<std::endl;
1238#endif
1239
1240 return explicit_selector;
1241 }
1242 else if (fImplNo > 0) {
1243#ifdef SELECTION_DEBUG
1244 std::cout<<"\tReturning No"<<std::endl;
1245#endif
1246
1247 return 0;
1248 }
1249 else {
1250
1251#ifdef SELECTION_DEBUG
1252 std::cout<<"\tReturning Yes"<<std::endl;
1253#endif
1254
1255 return selector;
1256 }
1257 }
1258
1259 return 0;
1260
1261}
1262
1263const BaseSelectionRule *SelectionRules::IsMemberSelected(const clang::Decl* D, const std::string& str_name) const
1264{
1265 std::string parent_name;
1266 std::string parent_qual_name;
1267
1268 if (IsParentClass(D))
1269 {
1270 if (!GetParentName(D, parent_name, parent_qual_name)) return 0;
1271
1272 const BaseSelectionRule *selector = 0;
1273 Int_t fImplNo = 0;
1274 const BaseSelectionRule *explicit_selector = 0;
1275 int fFileNo = 0;
1276
1277 //DEBUG std::cout<<"\n\tParent is class";
1278 std::list<ClassSelectionRule>::const_iterator it = fClassSelectionRules.begin();
1279 std::string name_value;
1280 std::string pattern_value;
1281 for(; it != fClassSelectionRules.end(); ++it) {
1283 = it->Match(llvm::dyn_cast<clang::NamedDecl>(D), parent_qual_name, "", false);
1284
1285 if (match != BaseSelectionRule::kNoMatch) {
1286 if (it->GetSelected() == BaseSelectionRule::kYes) {
1287 selector = &(*it);
1288 if (IsLinkdefFile()) {
1289 if (match == BaseSelectionRule::kName) {
1290 explicit_selector = &(*it);
1291 } else if (match == BaseSelectionRule::kPattern) {
1292 if (it->GetAttributeValue("pattern", pattern_value)) {
1293 // NOTE: Weird ... This is a strange definition of explicit.
1294 if (pattern_value != "*" && pattern_value != "*::*") explicit_selector = &(*it);
1295 }
1296 }
1297 }
1298 } else if (it->GetSelected() == BaseSelectionRule::kNo) {
1299 if (!IsLinkdefFile()) {
1300 if (match == BaseSelectionRule::kFile) ++fFileNo;
1301 else {
1302
1303#ifdef SELECTION_DEBUG
1304 std::cout<<"\tNo returned"<<std::endl;
1305#endif
1306
1307 return 0; // in genreflex we can't have that situation
1308 }
1309 }
1310 else {
1311 if (match == BaseSelectionRule::kPattern && it->GetAttributeValue("pattern", pattern_value)) {
1312 if (pattern_value == "*" || pattern_value == "*::*") ++fImplNo;
1313 else
1314 return 0;
1315 }
1316 else
1317 return 0;
1318 }
1319 }
1320 else if (it->GetSelected() == BaseSelectionRule::kDontCare) // - we check the method and field selection rules for the class
1321 {
1322 if (!it->HasMethodSelectionRules() && !it->HasFieldSelectionRules()) {
1323
1324#ifdef SELECTION_DEBUG
1325 std::cout<<"\tNo fields and methods"<<std::endl;
1326#endif
1327
1328 return 0; // == BaseSelectionRule::kNo
1329 }
1330 else {
1331 clang::Decl::Kind kind = D->getKind();
1332 if (kind == clang::Decl::Field || kind == clang::Decl::CXXMethod || kind == clang::Decl::CXXConstructor || kind == clang::Decl::CXXDestructor){
1333 std::list<VariableSelectionRule> members;
1334 std::list<VariableSelectionRule>::iterator mem_it;
1335 std::list<VariableSelectionRule>::iterator mem_it_end;
1336 std::string prototype;
1337
1338 if (kind == clang::Decl::Field) {
1339 members = it->GetFieldSelectionRules();
1340 }
1341 else {
1342 if (const clang::FunctionDecl* F = llvm::dyn_cast<clang::FunctionDecl> (D)){
1343 GetFunctionPrototype(F, prototype);
1344 prototype = str_name + prototype;
1345 }
1346 else{
1347 ROOT::TMetaUtils::Warning("","Warning: could not cast Decl to FunctionDecl\n");
1348 }
1349 members = it->GetMethodSelectionRules();
1350 }
1351 mem_it = members.begin();
1352 mem_it_end = members.end();
1353 for (; mem_it != mem_it_end; ++mem_it) {
1354 if (BaseSelectionRule::kName == mem_it->Match(llvm::dyn_cast<clang::NamedDecl>(D), str_name, prototype, false)) {
1355 if (mem_it->GetSelected() == BaseSelectionRule::kNo) return 0;
1356 }
1357 }
1358 }
1359 }
1360 }
1361 }
1362 }
1363
1364 if (IsLinkdefFile()) {
1365
1366#ifdef SELECTION_DEBUG
1367 std::cout<<"\n\tfYes = "<<fYes<<", fImplNo = "<<fImplNo<<std::endl;
1368#endif
1369
1370 if (explicit_selector) {
1371#ifdef SELECTION_DEBUG
1372 std::cout<<"\tReturning Yes"<<std::endl;
1373#endif
1374
1375 return explicit_selector;
1376 }
1377 else if (fImplNo > 0) {
1378
1379#ifdef SELECTION_DEBUG
1380 std::cout<<"\tReturning No"<<std::endl;
1381#endif
1382
1383 return 0;
1384 }
1385 else {
1386
1387#ifdef SELECTION_DEBUG
1388 std::cout<<"\tReturning Yes"<<std::endl;
1389#endif
1390
1391 return selector;
1392 }
1393 }
1394 else {
1395
1396 return selector;
1397 }
1398 }
1399 else {
1400 return 0;
1401 }
1402}
1403
1405 for(auto&& rule : fClassSelectionRules){
1406 if (BaseSelectionRule::kNo!=rule.GetSelected() && !rule.GetMatchFound() /* && !GetHasFileNameRule() */ ) {
1407 std::string name;
1408 if (rule.GetAttributeValue("pattern", name)) {
1409 // keep it
1410 } else if (rule.GetAttributeValue("name", name)) {
1411 // keept it
1412 } else {
1413 name.clear();
1414 }
1415 std::string file_name_value;
1416 if (!rule.GetAttributeValue("file_name", file_name_value)) file_name_value.clear();
1417
1418 if (!file_name_value.empty()) {
1419 // don't complain about defined_in rules
1420 continue;
1421 }
1422
1423 const char* attrName = "class";
1424 const char* attrVal = nullptr;
1425 if (!name.empty()) attrVal = name.c_str();
1426
1427 ROOT::TMetaUtils::Warning(0,"Unused %s rule: %s\n", attrName, attrVal);
1428 }
1429 }
1430
1431 for(auto&& rule : fVariableSelectionRules){
1432 if (!rule.GetMatchFound() && !GetHasFileNameRule()) {
1433 std::string name;
1434 if (rule.GetAttributeValue("pattern", name)) {
1435 // keep it
1436 } else if (rule.GetAttributeValue("name", name)) {
1437 // keept it
1438 } else {
1439 name.clear();
1440 }
1441 ROOT::TMetaUtils::Warning("","Unused variable rule: %s\n",name.c_str());
1442 if (name.empty()) {
1443 rule.PrintAttributes(std::cout,3);
1444 }
1445 }
1446 }
1447
1448#if defined(R__MUST_REVISIT)
1449#if R__MUST_REVISIT(6,2)
1450ROOT::TMetaUtils::Warning("SelectionRules::AreAllSelectionRulesUsed",
1451"Warnings concerning non matching selection rules are suppressed. An action is to be taken.\n");
1452#endif
1453#endif
1454// for(const auto& selRule: fFunctionSelectionRules) {
1455// if (!selRule.GetMatchFound() && !GetHasFileNameRule()) {
1456// // Here the slow methods can be used
1457// std::string name;
1458// if (selRule.GetAttributeValue("proto_pattern", name)) {
1459// // keep it
1460// } else if (selRule.GetAttributeValue("proto_name", name)) {
1461// // keep it
1462// } else if (selRule.GetAttributeValue("pattern", name)) {
1463// // keep it
1464// } else if (selRule.GetAttributeValue("name", name)) {
1465// // keept it
1466// } else {
1467// name.clear();
1468// }
1469// // Make it soft, no error - just warnings
1470// std::cout<<"Warning - unused function rule: "<<name<<std::endl;
1471// // if (IsSelectionXMLFile()){
1472// // std::cout<<"Warning - unused function rule: "<<name<<std::endl;
1473// // }
1474// // else {
1475// // std::cout<<"Error - unused function rule: "<<name<<std::endl;
1476// // }
1477// if (name.length() == 0) {
1478// selRule.PrintAttributes(std::cout,3);
1479// }
1480// }
1481//
1482// }
1483
1484
1485#if Enums_rules_becomes_useful_for_rootcling
1486 for(auto&& rule : fEnumSelectionRules){
1487 if (!rule.GetMatchFound() && !GetHasFileNameRule()) {
1488 std::string name;
1489 if (rule.GetAttributeValue("pattern", name)) {
1490 // keep it
1491 } else if (rule.GetAttributeValue("name", name)) {
1492 // keept it
1493 } else {
1494 name.clear();
1495 }
1496
1497 ROOT::TMetaUtils::Warning("","Unused enum rule: %s\n",name.c_str());
1498
1499 if (name.empty()){
1500 rule.PrintAttributes(std::cout,3);
1501 }
1502 }
1503 }
1504#endif
1505 return true;
1506}
1507
1508bool SelectionRules::SearchNames(cling::Interpreter &interp)
1509{
1510 // std::cout<<"Searching Names In Selection Rules:"<<std::endl;
1511 for(std::list<ClassSelectionRule>::iterator it = fClassSelectionRules.begin(),
1512 end = fClassSelectionRules.end();
1513 it != end;
1514 ++it) {
1515 if (it->HasAttributeWithName("name")) {
1516 std::string name_value;
1517 it->GetAttributeValue("name", name_value);
1518 // In Class selection rules, we should be interested in scopes.
1519 const clang::Type *typeptr = 0;
1520 const clang::CXXRecordDecl *target
1521 = ROOT::TMetaUtils::ScopeSearch(name_value.c_str(), interp,
1522 true /*diag*/, &typeptr);
1523 if (target) {
1524 it->SetCXXRecordDecl(target,typeptr);
1525 }
1526 }
1527 }
1528 return true;
1529}
1530
1531
1533{
1534 // Fill the cache of every selection rule
1535 for (auto& rule : fClassSelectionRules) rule.FillCache();
1536 for (auto& rule : fFunctionSelectionRules) rule.FillCache();
1537 for (auto& rule : fVariableSelectionRules) rule.FillCache();
1538 for (auto& rule : fEnumSelectionRules) rule.FillCache();
1539}
1540
1541
const clang::CXXRecordDecl * R__ScopeSearch(const char *name, const clang::Type **resultType=0)
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
char name[80]
Definition TGX11.cxx:110
int type
Definition TGX11.cxx:121
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 Info(const char *location, const char *va_(fmt),...)
void Error(const char *location, const char *va_(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)
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'.
void Warning(const char *location, const char *va_(fmt),...)
bool areEqual(const RULE *r1, const RULE *r2, bool moduloNameOrPattern=false)