Logo ROOT  
Reference Guide
SelectionRules.h
Go to the documentation of this file.
1 // @(#)root/core/utils:$Id: SelectionRules.h 28529 2009-05-11 16:43:35Z pcanal $
2 // Author: Velislava Spasova September 2010
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2011, Rene Brun, Fons Rademakers and al. *
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 #ifndef R__SELECTIONRULES_H
13 #define R__SELECTIONRULES_H
14 
15 #include <list>
16 #include <string>
17 #include <vector>
18 #include <utility>
19 
20 #include "BaseSelectionRule.h"
21 #include "ClassSelectionRule.h"
22 #include "VariableSelectionRule.h"
23 #include "clang/AST/Decl.h"
24 
25 #include "TClingUtils.h"
26 
27 namespace cling {
28  class Interpreter;
29 }
30 
31 namespace ROOT{
32  namespace TMetaUtils {
33  class TNormalizedCtxt;
34  }
35 }
36 #include <iostream>
38 
39  template<class ASSOCIATIVECONTAINER>
40  inline bool areEqualAttributes(const ASSOCIATIVECONTAINER& c1, const ASSOCIATIVECONTAINER& c2, bool moduloNameOrPattern){
41  if (c1.size() != c2.size()) return false;
42  if (moduloNameOrPattern) {
43  for (auto&& keyValPairC1 : c1){
44  auto keyC1 = keyValPairC1.first;
45  if ("pattern" == keyC1 || "name" == keyC1) continue;
46  auto valC1 = keyValPairC1.second;
47  auto C2It = c2.find(keyC1);
48  if (C2It == c2.end() || valC1 != C2It->second) return false;
49  }
50  }
51  else {
52  return !(c1 != c2);
53  }
54  return true;
55  }
56 
57  template<class RULE>
58  inline bool areEqual(const RULE* r1, const RULE* r2, bool moduloNameOrPattern = false){
59  return areEqualAttributes(r1->GetAttributes(), r2->GetAttributes(), moduloNameOrPattern);
60  }
61 
62  template<class RULESCOLLECTION>
63  inline bool areEqualColl(const RULESCOLLECTION& r1,
64  const RULESCOLLECTION& r2,
65  bool moduloNameOrPattern = false){
66  if (r1.size() != r2.size()) return false;
67  auto rIt1 = r1.begin();
68  auto rIt2 = r2.begin();
69  for (;rIt1!=r1.cend();++rIt1,++rIt2){
70  if (!areEqual(&(*rIt1),&(*rIt2), moduloNameOrPattern)) return false;
71  }
72  return true;
73  }
74  template<>
76  const ClassSelectionRule* r2,
77  bool moduloNameOrPattern){
78  if (!areEqualAttributes(r1->GetAttributes(), r2->GetAttributes(),moduloNameOrPattern)) return false;
79  // Now check fields
82  true)) return false;
83  // On the same footing, now check methods
86  true)) return false;
87  return true;
88  }
89 }
90 
91 
93 
94 public:
95  /// Type of selection file
100  };
101 
102  SelectionRules(cling::Interpreter &interp,
104  const std::vector<std::pair<std::string,std::string>>& namesForExclusion):
106  fHasFileNameRule(false),
107  fRulesCounter(0),
108  fNormCtxt(normCtxt),
109  fInterp(interp) {
110  long counter=1;
111  for (auto& attrValPair : namesForExclusion){
112  ClassSelectionRule csr(counter++, fInterp);
113  csr.SetAttributeValue(attrValPair.first, attrValPair.second);
116  }
117  }
118 
119  void AddClassSelectionRule(const ClassSelectionRule& classSel);
120  bool HasClassSelectionRules() const { return !fClassSelectionRules.empty(); }
121  const std::list<ClassSelectionRule>& GetClassSelectionRules() const {
122  return fClassSelectionRules;
123  }
124 
125  void AddFunctionSelectionRule(const FunctionSelectionRule& funcSel);
127  return !fFunctionSelectionRules.empty();
128  }
129  const std::list<FunctionSelectionRule>& GetFunctionSelectionRules() const {
131  }
132 
134 
136  return !fVariableSelectionRules.empty();
137  }
138  const std::list<VariableSelectionRule>& GetVariableSelectionRules() const {
140  }
141 
142  void AddEnumSelectionRule(const EnumSelectionRule& enumSel);
143  bool HasEnumSelectionRules() const { return !fEnumSelectionRules.empty(); }
144  const std::list<EnumSelectionRule>& GetEnumSelectionRules() const {
145  return fEnumSelectionRules;
146  }
147 
148  void PrintSelectionRules() const; // print all selection rules
149 
150  void ClearSelectionRules(); // clear all selection rules
151 
152  void SetHasFileNameRule(bool file_rule) { fHasFileNameRule = file_rule; }
153  bool GetHasFileNameRule() const { return fHasFileNameRule; }
154 
155  int CheckDuplicates();
156  void Optimize();
157 
158  // These method are called from clr-scan and return true if the Decl selected, false otherwise
159  //const BaseSelectionRule *IsDeclSelected(clang::Decl* D) const;
160  const ClassSelectionRule *IsDeclSelected(const clang::RecordDecl* D) const;
161  const ClassSelectionRule *IsDeclSelected(const clang::TypedefNameDecl* D) const;
162  const ClassSelectionRule *IsDeclSelected(const clang::NamespaceDecl* D) const;
163  const BaseSelectionRule *IsDeclSelected(const clang::EnumDecl* D) const;
164  const BaseSelectionRule *IsDeclSelected(const clang::VarDecl* D) const;
165  const BaseSelectionRule *IsDeclSelected(const clang::FieldDecl* D) const;
166  const BaseSelectionRule *IsDeclSelected(const clang::FunctionDecl* D) const;
167  const BaseSelectionRule *IsDeclSelected(const clang::Decl* D) const;
168 
169  const ClassSelectionRule *IsClassSelected(const clang::Decl* D, const std::string& qual_name) const; // is the class selected
170  const ClassSelectionRule *IsNamespaceSelected(const clang::Decl* D, const std::string& qual_name) const; // is the class selected
171 
172  // is the global function, variable, enum selected - the behavior is different for linkdef.h and selection.xml - that's why
173  // we have two functions
174  const BaseSelectionRule *IsVarSelected(const clang::VarDecl* D, const std::string& qual_name) const;
175  const BaseSelectionRule *IsFunSelected(const clang::FunctionDecl* D, const std::string& qual_name) const;
176  const BaseSelectionRule *IsEnumSelected(const clang::EnumDecl* D, const std::string& qual_name) const;
177  const BaseSelectionRule *IsLinkdefVarSelected(const clang::VarDecl* D, const std::string& qual_name) const;
178  const BaseSelectionRule *IsLinkdefFunSelected(const clang::FunctionDecl* D, const std::string& qual_name) const;
179  const BaseSelectionRule *IsLinkdefEnumSelected(const clang::EnumDecl* D, const std::string& qual_name) const;
180 
181  // is member (field, method, enum) selected; the behavior for linkdef.h methods is different
182  const BaseSelectionRule *IsMemberSelected(const clang::Decl* D, const std::string& str_name) const;
183  const BaseSelectionRule *IsLinkdefMethodSelected(const clang::Decl* D, const std::string& qual_name) const;
184 
185  // Return the number of rules
186  unsigned int Size() const{return fClassSelectionRules.size()+
189  fEnumSelectionRules.size();};
190 
191  // returns true if the parent is class or struct
192  bool IsParentClass(const clang::Decl* D) const;
193 
194  // the same but returns also the parent name and qualified name
195  bool IsParentClass(const clang::Decl* D, std::string& parent_name, std::string& parent_qual_name) const;
196 
197  // returns the parent name and qualified name
198  bool GetParentName(const clang::Decl* D, std::string& parent_name, std::string& parent_qual_name) const;
199 
200 
201  //bool getParent(clang::Decl* D, clang::Decl* parent); - this method would have saved a lot of efforts but it crashes
202  // and I didn't understand why
203 
204  // gets the name and qualified name of the Decl
205  bool GetDeclName(const clang::Decl* D, std::string& name, std::string& qual_name) const;
206 
207  // gets the qualname of the decl, no checks performed
208  void GetDeclQualName(const clang::Decl* D, std::string& qual_name) const;
209 
210  // gets the function prototype if the Decl (if it is global function or method)
211  bool GetFunctionPrototype(const clang::FunctionDecl* F, std::string& prototype) const;
212 
213  bool IsSelectionXMLFile() const {
215  }
216  bool IsLinkdefFile() const {
218  }
220  fSelectionFileType = fileType;
221  }
222 
223  // returns true if all selection rules are used at least once
224  bool AreAllSelectionRulesUsed() const;
225 
226  // Go through all the selections rules and lookup the name if any in the AST.
227  // and force the instantiation of template if any are used in the rules.
228  bool SearchNames(cling::Interpreter &interp);
229 
230  void FillCache(); // Fill the cache of all selection rules
231 
232 private:
233  std::list<ClassSelectionRule> fClassSelectionRules; ///< List of the class selection rules
234  std::list<FunctionSelectionRule> fFunctionSelectionRules; ///< List of the global functions selection rules
235  std::list<VariableSelectionRule> fVariableSelectionRules; ///< List of the global variables selection rules
236  std::list<EnumSelectionRule> fEnumSelectionRules; ///< List of the enums selection rules
237 
239 
240  bool fHasFileNameRule; ///< if we have a file name rule, this should be set to true
241  long int fRulesCounter;
242 
244  cling::Interpreter &fInterp;
245 
246 };
247 
248 #endif
SelectionRules::GetVariableSelectionRules
const std::list< VariableSelectionRule > & GetVariableSelectionRules() const
Definition: SelectionRules.h:138
SelectionRules::GetDeclName
bool GetDeclName(const clang::Decl *D, std::string &name, std::string &qual_name) const
Definition: SelectionRules.cxx:425
SelectionRulesUtils::areEqualAttributes
bool areEqualAttributes(const ASSOCIATIVECONTAINER &c1, const ASSOCIATIVECONTAINER &c2, bool moduloNameOrPattern)
Definition: SelectionRules.h:40
SelectionRules::kLinkdefFile
@ kLinkdefFile
Definition: SelectionRules.h:98
ClassSelectionRule::GetFieldSelectionRules
const std::list< VariableSelectionRule > & GetFieldSelectionRules() const
Definition: ClassSelectionRule.cxx:32
F
#define F(x, y, z)
SelectionRules::GetParentName
bool GetParentName(const clang::Decl *D, std::string &parent_name, std::string &parent_qual_name) const
Definition: SelectionRules.cxx:518
SelectionRules::AddEnumSelectionRule
void AddEnumSelectionRule(const EnumSelectionRule &enumSel)
Definition: SelectionRules.cxx:69
SelectionRules::GetEnumSelectionRules
const std::list< EnumSelectionRule > & GetEnumSelectionRules() const
Definition: SelectionRules.h:144
ClassSelectionRule.h
SelectionRules::SetSelectionFileType
void SetSelectionFileType(ESelectionFileTypes fileType)
Definition: SelectionRules.h:219
SelectionRules::IsNamespaceSelected
const ClassSelectionRule * IsNamespaceSelected(const clang::Decl *D, const std::string &qual_name) const
Definition: SelectionRules.cxx:566
SelectionRules::fNormCtxt
ROOT::TMetaUtils::TNormalizedCtxt & fNormCtxt
Definition: SelectionRules.h:243
BaseSelectionRule.h
BaseSelectionRule::kNo
@ kNo
Definition: BaseSelectionRule.h:45
SelectionRules::IsMemberSelected
const BaseSelectionRule * IsMemberSelected(const clang::Decl *D, const std::string &str_name) const
Definition: SelectionRules.cxx:1261
SelectionRules::GetFunctionSelectionRules
const std::list< FunctionSelectionRule > & GetFunctionSelectionRules() const
Definition: SelectionRules.h:129
SelectionRulesUtils
Definition: SelectionRules.h:37
SelectionRules::IsClassSelected
const ClassSelectionRule * IsClassSelected(const clang::Decl *D, const std::string &qual_name) const
Definition: SelectionRules.cxx:665
SelectionRules::FillCache
void FillCache()
Definition: SelectionRules.cxx:1530
SelectionRules::HasVariableSelectionRules
bool HasVariableSelectionRules() const
Definition: SelectionRules.h:135
ClassSelectionRule::GetMethodSelectionRules
const std::list< FunctionSelectionRule > & GetMethodSelectionRules() const
Definition: ClassSelectionRule.cxx:95
SelectionRules::HasClassSelectionRules
bool HasClassSelectionRules() const
Definition: SelectionRules.h:120
SelectionRules::HasEnumSelectionRules
bool HasEnumSelectionRules() const
Definition: SelectionRules.h:143
SelectionRules::IsSelectionXMLFile
bool IsSelectionXMLFile() const
Definition: SelectionRules.h:213
SelectionRules::IsLinkdefFunSelected
const BaseSelectionRule * IsLinkdefFunSelected(const clang::FunctionDecl *D, const std::string &qual_name) const
Definition: SelectionRules.cxx:908
SelectionRules::AreAllSelectionRulesUsed
bool AreAllSelectionRulesUsed() const
Definition: SelectionRules.cxx:1402
SelectionRules::HasFunctionSelectionRules
bool HasFunctionSelectionRules() const
Definition: SelectionRules.h:126
SelectionRules::fSelectionFileType
ESelectionFileTypes fSelectionFileType
Definition: SelectionRules.h:238
SelectionRules::IsFunSelected
const BaseSelectionRule * IsFunSelected(const clang::FunctionDecl *D, const std::string &qual_name) const
Definition: SelectionRules.cxx:800
SelectionRules::fEnumSelectionRules
std::list< EnumSelectionRule > fEnumSelectionRules
List of the enums selection rules.
Definition: SelectionRules.h:236
SelectionRules::fVariableSelectionRules
std::list< VariableSelectionRule > fVariableSelectionRules
List of the global variables selection rules.
Definition: SelectionRules.h:235
SelectionRules::IsLinkdefFile
bool IsLinkdefFile() const
Definition: SelectionRules.h:216
VariableSelectionRule
Definition: VariableSelectionRule.h:25
BaseSelectionRule::GetAttributes
const AttributesMap_t & GetAttributes() const
Definition: BaseSelectionRule.cxx:150
SelectionRules::PrintSelectionRules
void PrintSelectionRules() const
Definition: SelectionRules.cxx:79
SelectionRules::fFunctionSelectionRules
std::list< FunctionSelectionRule > fFunctionSelectionRules
List of the global functions selection rules.
Definition: SelectionRules.h:234
ClassSelectionRule
Definition: ClassSelectionRule.h:33
SelectionRules::SetHasFileNameRule
void SetHasFileNameRule(bool file_rule)
Definition: SelectionRules.h:152
SelectionRulesUtils::areEqual
bool areEqual(const RULE *r1, const RULE *r2, bool moduloNameOrPattern=false)
Definition: SelectionRules.h:58
SelectionRules::GetFunctionPrototype
bool GetFunctionPrototype(const clang::FunctionDecl *F, std::string &prototype) const
Definition: SelectionRules.cxx:450
SelectionRules::GetDeclQualName
void GetDeclQualName(const clang::Decl *D, std::string &qual_name) const
Definition: SelectionRules.cxx:444
SelectionRulesUtils::areEqualColl
bool areEqualColl(const RULESCOLLECTION &r1, const RULESCOLLECTION &r2, bool moduloNameOrPattern=false)
Definition: SelectionRules.h:63
SelectionRules::kSelectionXMLFile
@ kSelectionXMLFile
Definition: SelectionRules.h:97
SelectionRules::IsLinkdefEnumSelected
const BaseSelectionRule * IsLinkdefEnumSelected(const clang::EnumDecl *D, const std::string &qual_name) const
Definition: SelectionRules.cxx:968
ROOT::TMetaUtils::TNormalizedCtxt
Definition: TClingUtils.h:138
SelectionRules::SearchNames
bool SearchNames(cling::Interpreter &interp)
Definition: SelectionRules.cxx:1506
SelectionRules::AddFunctionSelectionRule
void AddFunctionSelectionRule(const FunctionSelectionRule &funcSel)
Definition: SelectionRules.cxx:49
BaseSelectionRule::SetAttributeValue
void SetAttributeValue(const std::string &attributeName, const std::string &attributeValue)
Definition: BaseSelectionRule.cxx:125
SelectionRules::IsLinkdefMethodSelected
const BaseSelectionRule * IsLinkdefMethodSelected(const clang::Decl *D, const std::string &qual_name) const
Definition: SelectionRules.cxx:1041
SelectionRules::fInterp
cling::Interpreter & fInterp
Definition: SelectionRules.h:244
SelectionRulesUtils::areEqual< ClassSelectionRule >
bool areEqual< ClassSelectionRule >(const ClassSelectionRule *r1, const ClassSelectionRule *r2, bool moduloNameOrPattern)
Definition: SelectionRules.h:75
SelectionRules::IsParentClass
bool IsParentClass(const clang::Decl *D) const
Definition: SelectionRules.cxx:496
SelectionRules::AddClassSelectionRule
void AddClassSelectionRule(const ClassSelectionRule &classSel)
Definition: SelectionRules.cxx:39
SelectionRules::Optimize
void Optimize()
Definition: SelectionRules.cxx:252
BaseSelectionRule::SetSelected
void SetSelected(ESelect sel)
Definition: BaseSelectionRule.cxx:98
SelectionRules::Size
unsigned int Size() const
Definition: SelectionRules.h:186
SelectionRules::fRulesCounter
long int fRulesCounter
Definition: SelectionRules.h:241
SelectionRules::SelectionRules
SelectionRules(cling::Interpreter &interp, ROOT::TMetaUtils::TNormalizedCtxt &normCtxt, const std::vector< std::pair< std::string, std::string >> &namesForExclusion)
Definition: SelectionRules.h:102
SelectionRules::IsDeclSelected
const ClassSelectionRule * IsDeclSelected(const clang::RecordDecl *D) const
Definition: SelectionRules.cxx:273
SelectionRules::ESelectionFileTypes
ESelectionFileTypes
Type of selection file.
Definition: SelectionRules.h:96
name
char name[80]
Definition: TGX11.cxx:110
SelectionRules::GetClassSelectionRules
const std::list< ClassSelectionRule > & GetClassSelectionRules() const
Definition: SelectionRules.h:121
SelectionRules::GetHasFileNameRule
bool GetHasFileNameRule() const
Definition: SelectionRules.h:153
SelectionRules::IsVarSelected
const BaseSelectionRule * IsVarSelected(const clang::VarDecl *D, const std::string &qual_name) const
Definition: SelectionRules.cxx:776
TClingUtils.h
c2
return c2
Definition: legend2.C:14
SelectionRules::fHasFileNameRule
bool fHasFileNameRule
if we have a file name rule, this should be set to true
Definition: SelectionRules.h:240
SelectionRules::kNumSelectionFileTypes
@ kNumSelectionFileTypes
Definition: SelectionRules.h:99
SelectionRules::CheckDuplicates
int CheckDuplicates()
Definition: SelectionRules.cxx:206
VariableSelectionRule.h
SelectionRules::IsLinkdefVarSelected
const BaseSelectionRule * IsLinkdefVarSelected(const clang::VarDecl *D, const std::string &qual_name) const
Definition: SelectionRules.cxx:850
SelectionRules::AddVariableSelectionRule
void AddVariableSelectionRule(const VariableSelectionRule &varSel)
Definition: SelectionRules.cxx:59
ROOT
VSD Structures.
Definition: StringConv.hxx:21
BaseSelectionRule
Definition: BaseSelectionRule.h:38
SelectionRules::IsEnumSelected
const BaseSelectionRule * IsEnumSelected(const clang::EnumDecl *D, const std::string &qual_name) const
Definition: SelectionRules.cxx:829
SelectionRules::ClearSelectionRules
void ClearSelectionRules()
Definition: SelectionRules.cxx:165
SelectionRules
Definition: SelectionRules.h:92
c1
return c1
Definition: legend1.C:41
SelectionRules::fClassSelectionRules
std::list< ClassSelectionRule > fClassSelectionRules
List of the class selection rules.
Definition: SelectionRules.h:233