24#define fnmatch(glob, path, dummy) PathMatchSpecA(path, glob);
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"
35#include "cling/Interpreter/Interpreter.h"
81 std::cout<<
"Printing Selection Rules:"<<std::endl;
86 std::cout<<
"\tClass sel rule "<<i<<
":"<<std::endl;
91 std::cout<<
"\tNo Class Selection Rules"<<std::endl;
96 std::list<FunctionSelectionRule>::const_iterator it2;
100 std::cout<<
"\tFunction sel rule "<<i<<
":"<<std::endl;
101 std::cout<<
"\t\tSelected: ";
102 switch(it2->GetSelected()){
109 default: std::cout<<
"Unspecified"<<std::endl;
111 it2->PrintAttributes(std::cout,2);
115 std::cout<<
"\tNo function sel rules"<<std::endl;
119 std::list<VariableSelectionRule>::const_iterator it3;
123 std::cout<<
"\tVariable sel rule "<<i<<
":"<<std::endl;
124 std::cout<<
"\t\tSelected: ";
125 switch(it3->GetSelected()){
132 default: std::cout<<
"Unspecified"<<std::endl;
134 it3->PrintAttributes(std::cout,2);
138 std::cout<<
"\tNo variable sel rules"<<std::endl;
142 std::list<EnumSelectionRule>::const_iterator it4;
146 std::cout<<
"\tEnum sel rule "<<i<<
":"<<std::endl;
147 std::cout<<
"\t\tSelected: ";
148 switch(it4->GetSelected()){
155 default: std::cout<<
"Unspecified"<<std::endl;
157 it4->PrintAttributes(std::cout,2);
161 std::cout<<
"\tNo enum sel rules"<<std::endl;
175 std::unordered_map<std::string,RULE*>& storedRules,
176 const std::string& attrName){
177 auto itRetCodePair = storedRules.emplace( attrName, rule );
179 auto storedRule = storedRules[attrName];
181 if (itRetCodePair.second ||
182 storedRule->GetSelected() != rule->GetSelected())
return false;
185 std::stringstream sstr; sstr <<
"Rule:\n";
187 sstr << (
areEqual ?
"Identical " :
"Conflicting ");
188 sstr <<
"rule already stored:\n";
189 storedRule->Print(sstr);
191 "Duplicated rule found.\n%s",sstr.str().c_str());
195template<
class RULESCOLLECTION,
class RULE =
typename RULESCOLLECTION::value_type>
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++;
213 if (0 != nDuplicates){
215 "Duplicates in rules were found.\n");
228 if ((nAttrsPattern != 1 || nAttrsName !=1) &&
237 auto implies = 0 == fnmatch(pattern,
name, FNM_PATHNAME);
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";
261 if (rule.HasAttributeName()) {
262 for (
auto&& intRule : selectionRules){
263 if (intRule.HasAttributePattern() &&
Implies(intRule, rule)) {
275 std::string qual_name;
282 std::string qual_name;
289 std::string qual_name;
306#if defined(R__MUST_REVISIT)
307# if R__MUST_REVISIT(6,4)
308 "Can become no-op once PCMs are available."
312 std::string str_name;
313 std::string qual_name;
336 std::string qual_name;
351 std::string str_name;
352 std::string qual_name;
362 std::string qual_name;
376 clang::Decl::Kind declkind = D->getKind();
379 case clang::Decl::CXXRecord:
380 case clang::Decl::ClassTemplateSpecialization:
381 case clang::Decl::ClassTemplatePartialSpecialization:
384 case clang::Decl::Namespace:
386 case clang::Decl::Enum:
391#if ROOTCLING_NEEDS_FUNCTIONS_SELECTION
394 case clang::Decl::CXXMethod:
395 case clang::Decl::CXXConstructor:
396 case clang::Decl::CXXDestructor: {
402 std::string str_name;
403 std::string qual_name;
411 case clang::Decl::Field:
427 const clang::NamedDecl*
N = llvm::dyn_cast<clang::NamedDecl> (D);
433 if (
N->getIdentifier()) {
434 name =
N->getNameAsString();
436 else if (
N->isCXXClassMember()) {
437 name =
N->getNameAsString();
439 llvm::raw_string_ostream stream(qual_name);
440 N->getNameForDiagnostic(stream,
N->getASTContext().getPrintingPolicy(),
true);
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);
456 const std::vector<std::string> quals={
"*",
"&"};
461 for (
auto I =
F->param_begin(),
E =
F->param_end();
I !=
E; ++
I) {
463 clang::ParmVarDecl*
P = *
I;
471 ROOT::TMetaUtils::ReplaceAll(
type,
"class ",
"");
473 ROOT::TMetaUtils::ReplaceAll(
type,
"restrict",
"");
477 for (
auto& qual : quals){
478 auto pos =
type.find(
" "+qual);
479 if (pos != std::string::npos)
480 type.replace( pos, 2, qual );
490 prototype =
"(" + prototype +
")";
499 if (
const clang::TagDecl*
T
500 = llvm::dyn_cast<clang::TagDecl>(D->getDeclContext()))
501 return T->isClass() ||
T->isStruct();
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);
520 if (
const clang::RecordDecl* parent
521 = llvm::dyn_cast<clang::RecordDecl>(D->getDeclContext())) {
522 GetDeclName(parent, parent_name, parent_qual_name);
568 const clang::NamespaceDecl*
N = llvm::dyn_cast<clang::NamespaceDecl> (D);
570 std::cout<<
"\n\tCouldn't cast Decl to NamespaceDecl";
583 std::string name_value;
584 std::string pattern_value;
597 explicit_selector = &(*it);
600 if (it->GetAttributeValue(
"pattern", pattern_value) &&
601 pattern_value !=
"*" && pattern_value !=
"*::*") specific_pattern_selector = &(*it);
610#ifdef SELECTION_DEBUG
611 std::cout<<
"\tNo returned"<<std::endl;
619 if (it->GetAttributeValue(
"pattern", pattern_value) &&
620 (pattern_value ==
"*" || pattern_value ==
"*::*")) ++fImplNo;
629#ifdef SELECTION_DEBUG
630 std::cout<<
"Empty dontC returned = No"<<std::endl;
640#ifdef SELECTION_DEBUG
641 std::cout<<
"\n\tfYes = "<<fYes<<
", fImplNo = "<<fImplNo<<std::endl;
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;
652#ifdef SELECTION_DEBUG
653 std::cout<<
"\n\tfYes = "<<fYes<<
", fFileNo = "<<fFileNo<<std::endl;
667 const clang::TagDecl* tagDecl = llvm::dyn_cast<clang::TagDecl> (D);
668 const clang::TypedefNameDecl* typeDefNameDecl = llvm::dyn_cast<clang::TypedefNameDecl> (D);
670 if (!tagDecl && !typeDefNameDecl) {
672 "Cannot cast Decl to TagDecl and Decl is not a typedef.\n");
676 if (!tagDecl && typeDefNameDecl){
677 clang::RecordDecl* recordDecl = ROOT::TMetaUtils::GetUnderlyingRecordDecl(typeDefNameDecl->getUnderlyingType());
680 "Cannot get RecordDecl behind TypedefDecl.\n");
683 tagDecl = recordDecl;
688 if (!( isLinkDefFile || tagDecl->isClass() || tagDecl->isStruct() ))
698 bool earlyReturn=
false;
700 const clang::NamedDecl* nDecl(llvm::dyn_cast<clang::NamedDecl>(D));
705 if (
const clang::ClassTemplateSpecializationDecl* ctsd =
706 llvm::dyn_cast_or_null<clang::ClassTemplateSpecializationDecl>(D))
707 if(
const clang::ClassTemplateDecl* ctd = ctsd->getSpecializedTemplate()){
708 const std::string& nArgsToKeep = rule.GetAttributeNArgsToKeep();
709 if (!nArgsToKeep.empty()){
710 fNormCtxt.AddTemplAndNargsToKeep(ctd->getCanonicalDecl(),
711 std::atoi(nArgsToKeep.c_str()));
715 if (earlyReturn)
continue;
724 explicit_selector = &(rule);
728 if (!pattern_value.empty() &&
729 pattern_value !=
"*" &&
730 pattern_value !=
"*::*") specific_pattern_selector = &(rule);
735 if (!isLinkDefFile) {
746 if (!pattern_value.empty() &&
747 (pattern_value ==
"*" || pattern_value ==
"*::*")) ++fImplNo;
760 if (earlyReturn)
return retval;
764 if (explicit_selector)
return explicit_selector;
765 else if (specific_pattern_selector)
return specific_pattern_selector;
766 else if (fImplNo > 0)
return 0;
767 else return selector;
786 for(; it != it_end; ++it) {
804 D->getPrimaryTemplate() !=
nullptr ||
805 llvm::isa<clang::CXXMethodDecl>(D))
return nullptr;
807 std::string prototype;
809 prototype = qual_name + prototype;
858 std::string name_value;
859 std::string pattern_value;
862 = selRule.
Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name,
"",
false);
868 explicit_selector = &selRule;
870 if (selRule.GetAttributeValue(
"pattern", pattern_value)) {
871 explicit_selector=&selRule;
881 if (selRule.GetAttributeValue(
"pattern", pattern_value)) {
882 if (pattern_value ==
"*" || pattern_value ==
"*::*") ++fImplNo;
895#ifdef SELECTION_DEBUG
896 std::cout<<
"\n\tfYes = "<<fYes<<
", fImplNo = "<<fImplNo<<std::endl;
899 if (explicit_selector)
return explicit_selector;
900 else if (fImplNo > 0)
return 0;
901 else return selector;
912 D->getPrimaryTemplate() !=
nullptr ||
913 llvm::isa<clang::CXXMethodDecl>(D))
return nullptr;
915 std::string prototype;
918 prototype = qual_name + prototype;
924 std::string pattern_value;
927 = selRule.
Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name, prototype,
false);
933 explicit_selector = &selRule;
935 if (selRule.GetAttributeValue(
"pattern", pattern_value)) {
936 explicit_selector = &selRule;
946 if (selRule.GetAttributeValue(
"pattern", pattern_value)) {
947 if (pattern_value ==
"*" || pattern_value ==
"*::*") ++fImplNo;
959 if (explicit_selector)
return explicit_selector;
960 else if (fImplNo > 0)
return 0;
961 else return selector;
970 std::list<VariableSelectionRule>::const_iterator it;
971 std::list<VariableSelectionRule>::const_iterator it_end;
980 std::string name_value;
981 std::string pattern_value;
982 for(; it != it_end; ++it) {
984 it->
Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name,
"",
false);
990 explicit_selector = &(*it);
992 it->GetAttributeValue(
"pattern", pattern_value)) {
994 if (pattern_value !=
"*" && pattern_value !=
"*::*") explicit_selector = &(*it);
1001 if (it->GetAttributeValue(
"pattern", pattern_value)) {
1002 if (pattern_value ==
"*" || pattern_value ==
"*::*") ++fImplNo;
1015#ifdef SELECTION_DEBUG
1016 std::cout<<
"\n\tfYes = "<<fYes<<
", fImplNo = "<<fImplNo<<std::endl;
1019 if (explicit_selector)
return explicit_selector;
1020 else if (fImplNo > 0)
return 0;
1021 else return selector;
1045 std::string prototype;
1047 if (
const clang::FunctionDecl*
F = llvm::dyn_cast<clang::FunctionDecl> (D))
1049 prototype = qual_name + prototype;
1051#ifdef SELECTION_DEBUG
1052 std::cout<<
"\tFunction prototype = "<<prototype<<std::endl;
1055 int expl_Yes = 0, impl_r_Yes = 0, impl_rr_Yes = 0;
1056 int impl_r_No = 0, impl_rr_No = 0;
1061 if (D->getKind() == clang::Decl::CXXMethod){
1063 std::string pat_value;
1064 for(; it != it_end; ++it) {
1066 = it->
Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name, prototype,
false);
1071 explicit_r = &(*it);
1075#ifdef SELECTION_DEBUG
1076 std::cout<<
"\tExplicit rule BaseSelectionRule::kNo found"<<std::endl;
1084 if (it->GetAttributeValue(
"pattern", pat_value)) {
1085 if (pat_value ==
"*")
continue;
1087 std::string par_name, par_qual_name;
1089 std::string par_pat = par_qual_name +
"::*";
1091 if (pat_value == par_pat) {
1092 implicit_rr = &(*it);
1095#ifdef SELECTION_DEBUG
1096 std::cout<<
"Implicit_rr rule ("<<pat_value<<
"), selected = "<<selected<<std::endl;
1103#ifdef SELECTION_DEBUG
1104 std::cout<<
"Implicit_rr rule ("<<pat_value<<
"), selected = "<<selected<<std::endl;
1111 implicit_r = &(*it);
1114#ifdef SELECTION_DEBUG
1115 std::cout<<
"Implicit_r rule ("<<pat_value<<
"), selected = "<<selected<<std::endl;
1122#ifdef SELECTION_DEBUG
1123 std::cout<<
"Implicit_r rule ("<<pat_value<<
"), selected = "<<selected<<std::endl;
1135#ifdef SELECTION_DEBUG
1136 std::cout<<
"\tExplicit rule BaseSelectionRule::BaseSelectionRule::kYes found"<<std::endl;
1141 else if (implicit_rr) {
1142 if (impl_rr_No > 0) {
1144#ifdef SELECTION_DEBUG
1145 std::cout<<
"\tImplicit_rr rule BaseSelectionRule::kNo found"<<std::endl;
1152#ifdef SELECTION_DEBUG
1153 std::cout<<
"\tImplicit_rr rule BaseSelectionRule::kYes found"<<std::endl;
1159 else if (implicit_r) {
1160 if (impl_r_No > 0) {
1162#ifdef SELECTION_DEBUG
1163 std::cout<<
"\tImplicit_r rule BaseSelectionRule::kNo found"<<std::endl;
1170#ifdef SELECTION_DEBUG
1171 std::cout<<
"\tImplicit_r rule BaseSelectionRule::kYes found"<<std::endl;
1179#ifdef SELECTION_DEBUG
1180 std::cout<<
"\tChecking parent class rules"<<std::endl;
1185 std::string parent_name, parent_qual_name;
1186 if (!
GetParentName(D, parent_name, parent_qual_name))
return 0;
1196 std::string name_value;
1197 std::string pattern_value;
1200 = it->Match(llvm::dyn_cast<clang::NamedDecl>(D), parent_qual_name,
"",
true);
1207 explicit_selector = &(*it);
1209 if (it->GetAttributeValue(
"pattern", pattern_value)) {
1211 if (pattern_value !=
"*" && pattern_value !=
"*::*") explicit_selector = &(*it);
1217 if (it->GetAttributeValue(
"pattern", pattern_value)) {
1218 if (pattern_value ==
"*" || pattern_value ==
"*::*") ++fImplNo;
1228#ifdef SELECTION_DEBUG
1229 std::cout<<
"\n\tfYes = "<<fYes<<
", fImplNo = "<<fImplNo<<std::endl;
1232 if (explicit_selector) {
1234#ifdef SELECTION_DEBUG
1235 std::cout<<
"\tReturning Yes"<<std::endl;
1238 return explicit_selector;
1240 else if (fImplNo > 0) {
1241#ifdef SELECTION_DEBUG
1242 std::cout<<
"\tReturning No"<<std::endl;
1249#ifdef SELECTION_DEBUG
1250 std::cout<<
"\tReturning Yes"<<std::endl;
1263 std::string parent_name;
1264 std::string parent_qual_name;
1268 if (!
GetParentName(D, parent_name, parent_qual_name))
return 0;
1277 std::string name_value;
1278 std::string pattern_value;
1281 = it->Match(llvm::dyn_cast<clang::NamedDecl>(D), parent_qual_name,
"",
false);
1288 explicit_selector = &(*it);
1290 if (it->GetAttributeValue(
"pattern", pattern_value)) {
1292 if (pattern_value !=
"*" && pattern_value !=
"*::*") explicit_selector = &(*it);
1301#ifdef SELECTION_DEBUG
1302 std::cout<<
"\tNo returned"<<std::endl;
1310 if (pattern_value ==
"*" || pattern_value ==
"*::*") ++fImplNo;
1320 if (!it->HasMethodSelectionRules() && !it->HasFieldSelectionRules()) {
1322#ifdef SELECTION_DEBUG
1323 std::cout<<
"\tNo fields and methods"<<std::endl;
1329 clang::Decl::Kind kind = D->getKind();
1330 if (kind == clang::Decl::Field || kind == clang::Decl::CXXMethod || kind == clang::Decl::CXXConstructor || kind == clang::Decl::CXXDestructor){
1331 std::list<VariableSelectionRule> members;
1332 std::list<VariableSelectionRule>::iterator mem_it;
1333 std::list<VariableSelectionRule>::iterator mem_it_end;
1334 std::string prototype;
1336 if (kind == clang::Decl::Field) {
1337 members = it->GetFieldSelectionRules();
1340 if (
const clang::FunctionDecl*
F = llvm::dyn_cast<clang::FunctionDecl> (D)){
1342 prototype = str_name + prototype;
1347 members = it->GetMethodSelectionRules();
1349 mem_it = members.begin();
1350 mem_it_end = members.end();
1351 for (; mem_it != mem_it_end; ++mem_it) {
1364#ifdef SELECTION_DEBUG
1365 std::cout<<
"\n\tfYes = "<<fYes<<
", fImplNo = "<<fImplNo<<std::endl;
1368 if (explicit_selector) {
1369#ifdef SELECTION_DEBUG
1370 std::cout<<
"\tReturning Yes"<<std::endl;
1373 return explicit_selector;
1375 else if (fImplNo > 0) {
1377#ifdef SELECTION_DEBUG
1378 std::cout<<
"\tReturning No"<<std::endl;
1385#ifdef SELECTION_DEBUG
1386 std::cout<<
"\tReturning Yes"<<std::endl;
1406 if (rule.GetAttributeValue(
"pattern",
name)) {
1408 }
else if (rule.GetAttributeValue(
"name",
name)) {
1413 std::string file_name_value;
1414 if (!rule.GetAttributeValue(
"file_name", file_name_value)) file_name_value.clear();
1416 if (!file_name_value.empty()) {
1421 const char* attrName =
"class";
1422 const char* attrVal =
nullptr;
1423 if (!
name.empty()) attrVal =
name.c_str();
1432 if (rule.GetAttributeValue(
"pattern",
name)) {
1434 }
else if (rule.GetAttributeValue(
"name",
name)) {
1441 rule.PrintAttributes(std::cout,3);
1446#if defined(R__MUST_REVISIT)
1447#if R__MUST_REVISIT(6,2)
1449"Warnings concerning non matching selection rules are suppressed. An action is to be taken.\n");
1483#if Enums_rules_becomes_useful_for_rootcling
1487 if (rule.GetAttributeValue(
"pattern",
name)) {
1489 }
else if (rule.GetAttributeValue(
"name",
name)) {
1498 rule.PrintAttributes(std::cout,3);
1513 if (it->HasAttributeWithName(
"name")) {
1514 std::string name_value;
1515 it->GetAttributeValue(
"name", name_value);
1518 const clang::CXXRecordDecl *target
1519 = ROOT::TMetaUtils::ScopeSearch(name_value.c_str(), interp,
1522 it->SetCXXRecordDecl(target,typeptr);
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)
void Info(const char *location, const char *msgfmt,...)
void Error(const char *location, const char *msgfmt,...)
void Warning(const char *location, const char *msgfmt,...)
Double_t(* Function)(Double_t)
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
const ClassSelectionRule * IsDeclSelected(const clang::RecordDecl *D) const
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
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
void ClearSelectionRules()
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 ClassSelectionRule * IsClassSelected(const clang::Decl *D, const std::string &qual_name) const
const BaseSelectionRule * IsEnumSelected(const clang::EnumDecl *D, const std::string &qual_name) 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
Type
enumeration specifying the integration types.
double Var(const RVec< T > &v)
Get the variance of the elements of an RVec.
bool areEqual(const RULE *r1, const RULE *r2, bool moduloNameOrPattern=false)
void GetNormalizedName(std::string &norm_name, std::string_view name)
Return the normalized name.
constexpr Double_t E()
Base of natural log: