22 #include "llvm/Support/raw_ostream.h"
23 #include "clang/Basic/SourceLocation.h"
24 #include "clang/Basic/SourceManager.h"
25 #include "clang/AST/ASTContext.h"
26 #include "clang/AST/DeclCXX.h"
27 #include "clang/AST/DeclTemplate.h"
29 #include "cling/Interpreter/Interpreter.h"
75 std::cout<<
"Printing Selection Rules:"<<std::endl;
80 std::cout<<
"\tClass sel rule "<<i<<
":"<<std::endl;
85 std::cout<<
"\tNo Class Selection Rules"<<std::endl;
90 std::list<FunctionSelectionRule>::const_iterator it2;
94 std::cout<<
"\tFunction sel rule "<<i<<
":"<<std::endl;
95 std::cout<<
"\t\tSelected: ";
96 switch(it2->GetSelected()){
103 default: std::cout<<
"Unspecified"<<std::endl;
105 it2->PrintAttributes(std::cout,2);
109 std::cout<<
"\tNo function sel rules"<<std::endl;
113 std::list<VariableSelectionRule>::const_iterator it3;
117 std::cout<<
"\tVariable sel rule "<<i<<
":"<<std::endl;
118 std::cout<<
"\t\tSelected: ";
119 switch(it3->GetSelected()){
126 default: std::cout<<
"Unspecified"<<std::endl;
128 it3->PrintAttributes(std::cout,2);
132 std::cout<<
"\tNo variable sel rules"<<std::endl;
136 std::list<EnumSelectionRule>::const_iterator it4;
140 std::cout<<
"\tEnum sel rule "<<i<<
":"<<std::endl;
141 std::cout<<
"\t\tSelected: ";
142 switch(it4->GetSelected()){
149 default: std::cout<<
"Unspecified"<<std::endl;
151 it4->PrintAttributes(std::cout,2);
155 std::cout<<
"\tNo enum sel rules"<<std::endl;
169 std::unordered_map<std::string,RULE*>& storedRules,
170 const std::string& attrName){
171 auto itRetCodePair = storedRules.emplace( attrName, rule );
173 auto storedRule = storedRules[attrName];
175 if (itRetCodePair.second ||
176 storedRule->GetSelected() != rule->GetSelected())
return false;
179 std::stringstream sstr; sstr <<
"Rule:\n";
181 sstr << (
areEqual ?
"Identical " :
"Conflicting ");
182 sstr <<
"rule already stored:\n";
183 storedRule->Print(sstr);
185 "Duplicated rule found.\n%s",sstr.str().c_str());
189 template<
class RULESCOLLECTION,
class RULE =
typename RULESCOLLECTION::value_type>
192 std::unordered_map<std::string, RULE*> patterns,names;
193 for (
auto&& rule : rules){
194 if (rule.HasAttributeName() &&
HasDuplicate(&rule,names,rule.GetAttributeName())) nDuplicates++;
195 if (rule.HasAttributePattern() &&
HasDuplicate(&rule,patterns,rule.GetAttributePattern())) nDuplicates++;
207 if (0 != nDuplicates){
209 "Duplicates in rules were found.\n");
222 if ((nAttrsPattern != 1 || nAttrsName !=1) &&
231 auto implies = 0 == fnmatch(
pattern,
name, FNM_PATHNAME);
234 static const auto msg =
"The pattern rule %s matches the name rule %s. "
235 "Since the name rule has compatible attributes, "
236 "it will be removed: the pattern rule will match the necessary classes if needed.\n";
253 std::list<decltype(ruleIt)> itPositionsToErase;
256 if (ruleIt->HasAttributeName()) {
258 if (intRule.HasAttributePattern() &&
Implies(intRule, *ruleIt)){
259 itPositionsToErase.push_back(ruleIt);
264 itPositionsToErase.unique();
265 for (
auto&& itPositionToErase : itPositionsToErase){
282 std::string patternString;
285 if (classRuleIt->HasAttributeWithName(
"pattern") &&
286 classRuleIt->GetAttributeValue(
"pattern",patternString)){
288 if (patternString.find_last_of(
"*")!=patternString.size()-1){
295 if (classRuleIt->HasAttributeWithName(
"name") &&
296 classRuleIt->GetAttributeValue(
"name",patternString)){
333 std::string qual_name;
340 std::string qual_name;
347 std::string qual_name;
364 #if defined(R__MUST_REVISIT)
365 # if R__MUST_REVISIT(6,4)
366 "Can become no-op once PCMs are available."
370 std::string str_name;
371 std::string qual_name;
394 std::string qual_name;
409 std::string str_name;
410 std::string qual_name;
420 std::string qual_name;
434 clang::Decl::Kind declkind = D->getKind();
437 case clang::Decl::CXXRecord:
438 case clang::Decl::ClassTemplateSpecialization:
439 case clang::Decl::ClassTemplatePartialSpecialization:
447 case clang::Decl::Var:
449 #if ROOTCLING_NEEDS_FUNCTIONS_SELECTION
452 case clang::Decl::CXXMethod:
453 case clang::Decl::CXXConstructor:
454 case clang::Decl::CXXDestructor: {
460 std::string str_name;
461 std::string qual_name;
469 case clang::Decl::Field:
485 const clang::NamedDecl*
N = llvm::dyn_cast<clang::NamedDecl> (D);
491 if (N->getIdentifier()) {
492 name = N->getNameAsString();
494 else if (N->isCXXClassMember()) {
495 name = N->getNameAsString();
497 llvm::raw_string_ostream stream(qual_name);
498 N->getNameForDiagnostic(stream,N->getASTContext().getPrintingPolicy(),
true);
503 const clang::NamedDecl*
N =
static_cast<const clang::NamedDecl*
> (D);
504 llvm::raw_string_ostream stream(qual_name);
505 N->getNameForDiagnostic(stream,N->getASTContext().getPrintingPolicy(),
true);
514 const std::vector<std::string> quals={
"*",
"&"};
519 for (
auto I = F->param_begin(),
E = F->param_end();
I !=
E; ++
I) {
521 clang::ParmVarDecl*
P = *
I;
535 for (
auto& qual : quals){
536 auto pos = type.find(
" "+qual);
537 if (pos != std::string::npos)
538 type.replace( pos, 2, qual );
548 prototype =
"(" + prototype +
")";
557 if (
const clang::TagDecl*
T
558 = llvm::dyn_cast<clang::TagDecl>(D->getDeclContext()))
559 return T->isClass() ||
T->isStruct();
566 if (
const clang::TagDecl* parent
567 = llvm::dyn_cast<clang::TagDecl>(D->getDeclContext())) {
568 if (parent->isClass()|| parent->isStruct()) {
569 GetDeclName(parent, parent_name, parent_qual_name);
578 if (
const clang::RecordDecl* parent
579 = llvm::dyn_cast<clang::RecordDecl>(D->getDeclContext())) {
580 GetDeclName(parent, parent_name, parent_qual_name);
626 const clang::NamespaceDecl*
N = llvm::dyn_cast<clang::NamespaceDecl> (D);
628 std::cout<<
"\n\tCouldn't cast Decl to NamespaceDecl";
641 std::string name_value;
642 std::string pattern_value;
655 explicit_selector = &(*it);
658 if (it->GetAttributeValue(
"pattern", pattern_value) &&
659 pattern_value !=
"*" && pattern_value !=
"*::*") specific_pattern_selector = &(*it);
668 #ifdef SELECTION_DEBUG
669 std::cout<<
"\tNo returned"<<std::endl;
677 if (it->GetAttributeValue(
"pattern", pattern_value) &&
678 (pattern_value ==
"*" || pattern_value ==
"*::*")) ++fImplNo;
687 #ifdef SELECTION_DEBUG
688 std::cout<<
"Empty dontC returned = No"<<std::endl;
698 #ifdef SELECTION_DEBUG
699 std::cout<<
"\n\tfYes = "<<fYes<<
", fImplNo = "<<fImplNo<<std::endl;
702 if (explicit_selector)
return explicit_selector;
703 else if (specific_pattern_selector)
return specific_pattern_selector;
704 else if (fImplNo > 0)
return 0;
705 else return selector;
710 #ifdef SELECTION_DEBUG
711 std::cout<<
"\n\tfYes = "<<fYes<<
", fFileNo = "<<fFileNo<<std::endl;
725 const clang::TagDecl* tagDecl = llvm::dyn_cast<clang::TagDecl> (D);
726 const clang::TypedefNameDecl* typeDefNameDecl = llvm::dyn_cast<clang::TypedefNameDecl> (D);
728 if (!tagDecl && !typeDefNameDecl) {
730 "Cannot cast Decl to TagDecl and Decl is not a typedef.\n");
734 if (!tagDecl && typeDefNameDecl){
738 "Cannot get RecordDecl behind TypedefDecl.\n");
741 tagDecl = recordDecl;
746 if (!( isLinkDefFile || tagDecl->isClass() || tagDecl->isStruct() ))
756 bool earlyReturn=
false;
758 const clang::NamedDecl* nDecl(llvm::dyn_cast<clang::NamedDecl>(D));
763 if (
const clang::ClassTemplateSpecializationDecl* ctsd =
764 llvm::dyn_cast_or_null<clang::ClassTemplateSpecializationDecl>(D))
765 if(
const clang::ClassTemplateDecl* ctd = ctsd->getSpecializedTemplate()){
766 const std::string&
nArgsToKeep = rule.GetAttributeNArgsToKeep();
767 if (!nArgsToKeep.empty()){
769 std::atoi(nArgsToKeep.c_str()));
773 if (earlyReturn)
continue;
782 explicit_selector = &(rule);
786 if (!pattern_value.empty() &&
787 pattern_value !=
"*" &&
788 pattern_value !=
"*::*") specific_pattern_selector = &(rule);
793 if (!isLinkDefFile) {
804 if (!pattern_value.empty() &&
805 (pattern_value ==
"*" || pattern_value ==
"*::*")) ++fImplNo;
818 if (earlyReturn)
return retval;
822 if (explicit_selector)
return explicit_selector;
823 else if (specific_pattern_selector)
return specific_pattern_selector;
824 else if (fImplNo > 0)
return 0;
825 else return selector;
844 for(; it != it_end; ++it) {
862 D->getPrimaryTemplate() !=
nullptr ||
863 llvm::isa<clang::CXXMethodDecl>(D))
return nullptr;
865 std::string prototype;
867 prototype = qual_name + prototype;
916 std::string name_value;
917 std::string pattern_value;
920 = selRule.
Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name,
"",
false);
926 explicit_selector = &selRule;
928 if (selRule.GetAttributeValue(
"pattern", pattern_value)) {
929 explicit_selector=&selRule;
939 if (selRule.GetAttributeValue(
"pattern", pattern_value)) {
940 if (pattern_value ==
"*" || pattern_value ==
"*::*") ++fImplNo;
953 #ifdef SELECTION_DEBUG
954 std::cout<<
"\n\tfYes = "<<fYes<<
", fImplNo = "<<fImplNo<<std::endl;
957 if (explicit_selector)
return explicit_selector;
958 else if (fImplNo > 0)
return 0;
959 else return selector;
970 D->getPrimaryTemplate() !=
nullptr ||
971 llvm::isa<clang::CXXMethodDecl>(D))
return nullptr;
973 std::string prototype;
976 prototype = qual_name + prototype;
982 std::string pattern_value;
985 = selRule.
Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name, prototype,
false);
991 explicit_selector = &selRule;
993 if (selRule.GetAttributeValue(
"pattern", pattern_value)) {
994 explicit_selector = &selRule;
1004 if (selRule.GetAttributeValue(
"pattern", pattern_value)) {
1005 if (pattern_value ==
"*" || pattern_value ==
"*::*") ++fImplNo;
1017 if (explicit_selector)
return explicit_selector;
1018 else if (fImplNo > 0)
return 0;
1019 else return selector;
1028 std::list<VariableSelectionRule>::const_iterator it;
1029 std::list<VariableSelectionRule>::const_iterator it_end;
1038 std::string name_value;
1039 std::string pattern_value;
1040 for(; it != it_end; ++it) {
1042 it->Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name,
"",
false);
1048 explicit_selector = &(*it);
1050 it->GetAttributeValue(
"pattern", pattern_value)) {
1052 if (pattern_value !=
"*" && pattern_value !=
"*::*") explicit_selector = &(*it);
1059 if (it->GetAttributeValue(
"pattern", pattern_value)) {
1060 if (pattern_value ==
"*" || pattern_value ==
"*::*") ++fImplNo;
1073 #ifdef SELECTION_DEBUG
1074 std::cout<<
"\n\tfYes = "<<fYes<<
", fImplNo = "<<fImplNo<<std::endl;
1077 if (explicit_selector)
return explicit_selector;
1078 else if (fImplNo > 0)
return 0;
1079 else return selector;
1103 std::string prototype;
1105 if (
const clang::FunctionDecl*
F = llvm::dyn_cast<clang::FunctionDecl> (D))
1107 prototype = qual_name + prototype;
1109 #ifdef SELECTION_DEBUG
1110 std::cout<<
"\tFunction prototype = "<<prototype<<std::endl;
1113 int expl_Yes = 0, impl_r_Yes = 0, impl_rr_Yes = 0;
1114 int impl_r_No = 0, impl_rr_No = 0;
1119 if (D->getKind() == clang::Decl::CXXMethod){
1121 std::string pat_value;
1122 for(; it != it_end; ++it) {
1124 = it->Match(llvm::dyn_cast<clang::NamedDecl>(D), qual_name, prototype,
false);
1129 explicit_r = &(*it);
1133 #ifdef SELECTION_DEBUG
1134 std::cout<<
"\tExplicit rule BaseSelectionRule::kNo found"<<std::endl;
1142 if (it->GetAttributeValue(
"pattern", pat_value)) {
1143 if (pat_value ==
"*")
continue;
1145 std::string par_name, par_qual_name;
1147 std::string par_pat = par_qual_name +
"::*";
1149 if (pat_value == par_pat) {
1150 implicit_rr = &(*it);
1153 #ifdef SELECTION_DEBUG
1154 std::cout<<
"Implicit_rr rule ("<<pat_value<<
"), selected = "<<selected<<std::endl;
1161 #ifdef SELECTION_DEBUG
1162 std::cout<<
"Implicit_rr rule ("<<pat_value<<
"), selected = "<<selected<<std::endl;
1169 implicit_r = &(*it);
1172 #ifdef SELECTION_DEBUG
1173 std::cout<<
"Implicit_r rule ("<<pat_value<<
"), selected = "<<selected<<std::endl;
1180 #ifdef SELECTION_DEBUG
1181 std::cout<<
"Implicit_r rule ("<<pat_value<<
"), selected = "<<selected<<std::endl;
1193 #ifdef SELECTION_DEBUG
1194 std::cout<<
"\tExplicit rule BaseSelectionRule::BaseSelectionRule::kYes found"<<std::endl;
1199 else if (implicit_rr) {
1200 if (impl_rr_No > 0) {
1202 #ifdef SELECTION_DEBUG
1203 std::cout<<
"\tImplicit_rr rule BaseSelectionRule::kNo found"<<std::endl;
1210 #ifdef SELECTION_DEBUG
1211 std::cout<<
"\tImplicit_rr rule BaseSelectionRule::kYes found"<<std::endl;
1217 else if (implicit_r) {
1218 if (impl_r_No > 0) {
1220 #ifdef SELECTION_DEBUG
1221 std::cout<<
"\tImplicit_r rule BaseSelectionRule::kNo found"<<std::endl;
1228 #ifdef SELECTION_DEBUG
1229 std::cout<<
"\tImplicit_r rule BaseSelectionRule::kYes found"<<std::endl;
1237 #ifdef SELECTION_DEBUG
1238 std::cout<<
"\tChecking parent class rules"<<std::endl;
1243 std::string parent_name, parent_qual_name;
1244 if (!
GetParentName(D, parent_name, parent_qual_name))
return 0;
1254 std::string name_value;
1255 std::string pattern_value;
1258 = it->Match(llvm::dyn_cast<clang::NamedDecl>(D), parent_qual_name,
"",
true);
1265 explicit_selector = &(*it);
1267 if (it->GetAttributeValue(
"pattern", pattern_value)) {
1269 if (pattern_value !=
"*" && pattern_value !=
"*::*") explicit_selector = &(*it);
1275 if (it->GetAttributeValue(
"pattern", pattern_value)) {
1276 if (pattern_value ==
"*" || pattern_value ==
"*::*") ++fImplNo;
1286 #ifdef SELECTION_DEBUG
1287 std::cout<<
"\n\tfYes = "<<fYes<<
", fImplNo = "<<fImplNo<<std::endl;
1290 if (explicit_selector) {
1292 #ifdef SELECTION_DEBUG
1293 std::cout<<
"\tReturning Yes"<<std::endl;
1296 return explicit_selector;
1298 else if (fImplNo > 0) {
1299 #ifdef SELECTION_DEBUG
1300 std::cout<<
"\tReturning No"<<std::endl;
1307 #ifdef SELECTION_DEBUG
1308 std::cout<<
"\tReturning Yes"<<std::endl;
1321 std::string parent_name;
1322 std::string parent_qual_name;
1326 if (!
GetParentName(D, parent_name, parent_qual_name))
return 0;
1335 std::string name_value;
1336 std::string pattern_value;
1339 = it->Match(llvm::dyn_cast<clang::NamedDecl>(D), parent_qual_name,
"",
false);
1346 explicit_selector = &(*it);
1348 if (it->GetAttributeValue(
"pattern", pattern_value)) {
1350 if (pattern_value !=
"*" && pattern_value !=
"*::*") explicit_selector = &(*it);
1359 #ifdef SELECTION_DEBUG
1360 std::cout<<
"\tNo returned"<<std::endl;
1368 if (pattern_value ==
"*" || pattern_value ==
"*::*") ++fImplNo;
1378 if (!it->HasMethodSelectionRules() && !it->HasFieldSelectionRules()) {
1380 #ifdef SELECTION_DEBUG
1381 std::cout<<
"\tNo fields and methods"<<std::endl;
1387 clang::Decl::Kind kind = D->getKind();
1388 if (kind == clang::Decl::Field || kind == clang::Decl::CXXMethod || kind == clang::Decl::CXXConstructor || kind == clang::Decl::CXXDestructor){
1389 std::list<VariableSelectionRule> members;
1390 std::list<VariableSelectionRule>::iterator mem_it;
1391 std::list<VariableSelectionRule>::iterator mem_it_end;
1392 std::string prototype;
1394 if (kind == clang::Decl::Field) {
1395 members = it->GetFieldSelectionRules();
1398 if (
const clang::FunctionDecl*
F = llvm::dyn_cast<clang::FunctionDecl> (D)){
1400 prototype = str_name + prototype;
1405 members = it->GetMethodSelectionRules();
1407 mem_it = members.begin();
1408 mem_it_end = members.end();
1409 for (; mem_it != mem_it_end; ++mem_it) {
1422 #ifdef SELECTION_DEBUG
1423 std::cout<<
"\n\tfYes = "<<fYes<<
", fImplNo = "<<fImplNo<<std::endl;
1426 if (explicit_selector) {
1427 #ifdef SELECTION_DEBUG
1428 std::cout<<
"\tReturning Yes"<<std::endl;
1431 return explicit_selector;
1433 else if (fImplNo > 0) {
1435 #ifdef SELECTION_DEBUG
1436 std::cout<<
"\tReturning No"<<std::endl;
1443 #ifdef SELECTION_DEBUG
1444 std::cout<<
"\tReturning Yes"<<std::endl;
1464 if (rule.GetAttributeValue(
"pattern", name)) {
1466 }
else if (rule.GetAttributeValue(
"name", name)) {
1471 std::string file_name_value;
1472 if (!rule.GetAttributeValue(
"file_name", file_name_value)) file_name_value.clear();
1474 if (!file_name_value.empty()) {
1479 const char* attrName =
nullptr;
1480 const char* attrVal =
nullptr;
1481 if (!file_name_value.empty()) {
1482 attrName =
"file name";
1483 attrVal = file_name_value.c_str();
1486 if (!name.empty()) attrVal = name.c_str();
1495 if (rule.GetAttributeValue(
"pattern", name)) {
1497 }
else if (rule.GetAttributeValue(
"name", name)) {
1504 rule.PrintAttributes(std::cout,3);
1509 #if defined(R__MUST_REVISIT)
1510 #if R__MUST_REVISIT(6,2)
1512 "Warnings concerning non matching selection rules are suppressed. An action is to be taken.\n");
1546 #if Enums_rules_becomes_useful_for_rootcling
1550 if (rule.GetAttributeValue(
"pattern", name)) {
1552 }
else if (rule.GetAttributeValue(
"name", name)) {
1561 rule.PrintAttributes(std::cout,3);
1576 if (it->HasAttributeWithName(
"name")) {
1577 std::string name_value;
1578 it->GetAttributeValue(
"name", name_value);
1581 const clang::CXXRecordDecl *target
1585 it->SetCXXRecordDecl(target,typeptr);
std::list< ClassSelectionRule > fClassSelectionRules
List of the class selection rules.
bool fIsDeep
if –deep option passed from command line, this should be set to true
const AttributesMap_t & GetAttributes() const
const ClassSelectionRule * IsDeclSelected(const clang::RecordDecl *D) const
ROOT::TMetaUtils::TNormalizedCtxt & fNormCtxt
bool IsLinkdefFile() const
const BaseSelectionRule * IsVarSelected(const clang::VarDecl *D, const std::string &qual_name) const
bool GetDeclName(const clang::Decl *D, std::string &name, std::string &qual_name) const
bool GetFunctionPrototype(const clang::FunctionDecl *F, std::string &prototype) const
bool AreAllSelectionRulesUsed() const
bool GetParentName(const clang::Decl *D, std::string &parent_name, std::string &parent_qual_name) const
std::list< VariableSelectionRule > fVariableSelectionRules
List of the global variables selection rules.
const ClassSelectionRule * IsNamespaceSelected(const clang::Decl *D, const std::string &qual_name) const
void PrintSelectionRules() const
void ClearSelectionRules()
const BaseSelectionRule * IsLinkdefFunSelected(const clang::FunctionDecl *D, const std::string &qual_name) const
void AddFunctionSelectionRule(const FunctionSelectionRule &funcSel)
const BaseSelectionRule * IsLinkdefMethodSelected(const clang::Decl *D, const std::string &qual_name) const
void GetDeclQualName(const clang::Decl *D, std::string &qual_name) const
const ClassSelectionRule * IsClassSelected(const clang::Decl *D, const std::string &qual_name) const
const BaseSelectionRule * IsFunSelected(const clang::FunctionDecl *D, const std::string &qual_name) const
static int CheckDuplicatesImp(RULESCOLLECTION &rules)
ESelect GetSelected() const
const clang::CXXRecordDecl * R__ScopeSearch(const char *name, const clang::Type **resultType=0)
void AddVariableSelectionRule(const VariableSelectionRule &varSel)
bool HasInterpreter() const
Type
enumeration specifying the integration types.
EMatchType Match(const clang::NamedDecl *decl, const std::string &name, const std::string &prototype, bool isLinkdef) const
const std::string & GetAttributePattern() const
cling::Interpreter & fInterp
const BaseSelectionRule * IsLinkdefEnumSelected(const clang::EnumDecl *D, const std::string &qual_name) const
const std::string & GetAttributeName() const
static bool HasDuplicate(RULE *rule, std::unordered_map< std::string, RULE * > &storedRules, const std::string &attrName)
MyComplex< T > P(MyComplex< T > z, T c_real, T c_imag)
[MyComplex]
static bool Implies(ClassSelectionRule &patternRule, ClassSelectionRule &nameRule)
std::list< EnumSelectionRule > fEnumSelectionRules
List of the enums selection rules.
bool areEqual(const RULE *r1, const RULE *r2, bool moduloNameOrPattern=false)
bool GetHasFileNameRule() const
void SetSelected(ESelect sel)
void AddClassSelectionRule(const ClassSelectionRule &classSel)
void AddEnumSelectionRule(const EnumSelectionRule &enumSel)
void SetAttributeValue(const std::string &attributeName, const std::string &attributeValue)
const BaseSelectionRule * IsEnumSelected(const clang::EnumDecl *D, const std::string &qual_name) const
bool IsSelectionXMLFile() const
bool IsParentClass(const clang::Decl *D) const
std::list< FunctionSelectionRule > fFunctionSelectionRules
List of the global functions selection rules.
const BaseSelectionRule * IsMemberSelected(const clang::Decl *D, const std::string &str_name) const
bool SearchNames(cling::Interpreter &interp)
const BaseSelectionRule * IsLinkdefVarSelected(const clang::VarDecl *D, const std::string &qual_name) const