Logo ROOT  
Reference Guide
Go to the documentation of this file.
1 // @(#)root/utils/src:$Id$
2 // Author: Philippe Canal November 2011 ;
3 // 16/04/2010 and Velislava Spasova.
4 // originated from Zdenek Culik
7 /*************************************************************************
8  * Copyright (C) 1995-2011, Rene Brun and Fons Rademakers. *
9  * All rights reserved. *
10  * *
11  * For the licensing terms see $ROOTSYS/LICENSE. *
12  * For the list of contributors see $ROOTSYS/README/rootcint. *
13  *************************************************************************/
15 #ifndef ROOT__RSCANNER_H__
16 #define ROOT__RSCANNER_H__
18 #include <stack>
20 #include "clang/AST/AST.h"
21 #include "clang/AST/ASTContext.h"
22 #include "clang/AST/DeclGroup.h"
23 #include "clang/AST/DeclFriend.h"
24 #include "clang/AST/Type.h"
25 #include "clang/AST/RecursiveASTVisitor.h"
27 #include "llvm/IR/Module.h"
29 #include "TClingUtils.h"
31 namespace clang {
32  class ClassTemplatePartialSpecializationDecl;
33  class ClassTemplateDecl;
34  class RecordDecl;
35  class Stmt;
36 }
38 namespace cling {
39  class Interpreter;
40 }
42 class SelectionRules;
43 class BaseSelectionRule;
44 class ClassSelectionRule;
46 class RScanner: public clang::RecursiveASTVisitor<RScanner>
47 {
49  bool shouldVisitDecl(clang::NamedDecl *D);
51 public:
53  public:
54  AnnotatedNamespaceDecl(clang::NamespaceDecl *decl, long index, bool rRequestOnlyTClass) :
55  fDecl(decl), fRuleIndex(index), fRequestOnlyTClass(rRequestOnlyTClass){}
56  AnnotatedNamespaceDecl() { /* Nothing to do we do not own the pointer; */}
57  bool RequestOnlyTClass() const { return fRequestOnlyTClass; }
58  const clang::NamespaceDecl* GetNamespaceDecl() const { return fDecl; }
59  operator clang::NamespaceDecl const *() const { return fDecl; }
60  bool operator<(const AnnotatedNamespaceDecl& right) { return fRuleIndex < right.fRuleIndex; }
61  private:
62  const clang::NamespaceDecl *fDecl;
63  long fRuleIndex;
65  };
67  typedef std::vector<AnnotatedNamespaceDecl> NamespaceColl_t;
68  typedef std::vector<ROOT::TMetaUtils::AnnotatedRecordDecl> ClassColl_t;
69  typedef std::vector<const clang::TypedefNameDecl*> TypedefColl_t;
70  typedef std::vector<const clang::FunctionDecl*> FunctionColl_t;
71  typedef std::vector<const clang::VarDecl*> VariableColl_t;
72  typedef std::vector<const clang::EnumDecl*> EnumColl_t;
73  typedef void (*DeclCallback)(const clang::RecordDecl*);
74  typedef std::map<const clang::Decl*,const BaseSelectionRule*> DeclsSelRulesMap_t;
76  enum class EScanType : char {kNormal, kTwoPasses, kOnePCM};
78  RScanner (SelectionRules &rules,
79  EScanType stype,
80  const cling::Interpreter &interpret,
82  unsigned int verbose = 0);
84  // Configure the vistitor to also visit template instantiation.
85  bool shouldVisitTemplateInstantiations() const { return true; }
87  // Don't descend into function bodies.
88  bool TraverseStmt(clang::Stmt*) { return true; }
90  // Don't descend into templates partial specialization (but only instances thereof).
91  bool TraverseClassTemplatePartialSpecializationDecl(clang::ClassTemplatePartialSpecializationDecl*) { return true; }
93  bool VisitEnumDecl(clang::EnumDecl* D); //Visitor for every EnumDecl i.e. enumeration node in the AST
94  bool VisitFieldDecl(clang::FieldDecl* D); //Visitor for e field inside a class
95  bool VisitFunctionDecl(clang::FunctionDecl* D); //Visitor for every FunctionDecl i.e. function node in the AST
96  bool VisitNamespaceDecl(clang::NamespaceDecl* D); // Visitor for every RecordDecl i.e. class node in the AST
97  bool VisitRecordDecl(clang::RecordDecl* D); // Visitor for every RecordDecl i.e. class node in the AST
98  bool VisitTypedefNameDecl(clang::TypedefNameDecl* D); // Visitor for every TypedefNameDecl i.e. class node in the AST
99  bool VisitVarDecl(clang::VarDecl* D); //Visitor for every VarDecl i.e. variable node in the AST
101  bool TreatRecordDeclOrTypedefNameDecl(clang::TypeDecl* typeDecl); //Function called by VisitTypedefNameDecl and VisitRecordDecl
104  bool TraverseDeclContextHelper(clang::DeclContext *DC); // Here is the code magic :) - every Decl
105  // according to its type is processed by the corresponding Visitor method
107  // Set a callback to record which are declared.
110  // Main interface of this class.
111  void Scan(const clang::ASTContext &C);
113  // Utility routines. Most belongs in TMetaUtils and should be shared with rootcling.cxx
114  bool GetDeclName(clang::Decl* D, std::string& name) const;
115  static bool GetDeclQualName(const clang::Decl* D, std::string& qual_name);
116  bool GetFunctionPrototype(clang::Decl* D, std::string& prototype) const;
118  static const char* fgClangDeclKey; // property key used for CLang declaration objects
119  static const char* fgClangFuncKey; // property key for function (demangled) names
123  // public for now, the list of selected classes.
133  const clang::ClassTemplateSpecializationDecl *fDecl;
134  const clang::TypedefNameDecl* fTypedefNameDecl;
135  };
136  std::vector<DelayedAnnotatedRecordDeclInfo> fDelayedAnnotatedRecordDecls;
138  virtual ~RScanner ();
142 private:
145  const clang::Type*,
146  const clang::RecordDecl*,
147  const std::string&,
148  const clang::TypedefNameDecl*,
149  unsigned int indexOffset=0);
150  std::string ConvTemplateArguments(const clang::TemplateArgumentList& list) const;
151  std::string ConvTemplateName(clang::TemplateName& N) const;
152  std::string ConvTemplateParameterList(clang::TemplateParameterList* list) const;
153  std::string ConvTemplateParams(clang::TemplateDecl* D) const;
154  void DeclInfo(clang::Decl* D) const;
155  std::string ExprToStr(clang::Expr* expr) const;
156  std::string FuncParameterList(clang::FunctionDecl* D) const;
157  std::string FuncParameters(clang::FunctionDecl* D) const;
158  std::string GetEnumName(clang::EnumDecl* D) const;
159  std::string GetLocation(clang::Decl* D) const;
160  std::string GetName(clang::Decl* D) const;
161  std::string GetSrcLocation(clang::SourceLocation L) const;
162  unsigned int FuncModifiers(clang::FunctionDecl* D) const;
163  unsigned int fVerboseLevel;
164  unsigned int VarModifiers(clang::VarDecl* D) const;
165  unsigned int Visibility(clang::Decl* D) const;
166  unsigned int VisibilityModifiers(clang::AccessSpecifier access) const;
167  void ShowError(const std::string &msg, const std::string &location = "") const;
168  void ShowInfo(const std::string &msg, const std::string &location = "") const;
169  void ShowTemplateInfo(const std::string &msg, const std::string &location = "") const;
170  void ShowWarning(const std::string &msg, const std::string &location = "") const;
171  static std::map <clang::Decl*, std::string> fgAnonymousClassMap;
172  static std::map <clang::Decl*, std::string> fgAnonymousEnumMap;
173  void UnexpectedDecl(clang::Decl* D,const std::string &txt = "") const;
174  void UnimplementedDecl(clang::Decl* D,const std::string &txt = "");
175  void UnimportantDecl(clang::Decl* D,const std::string &txt = "") const;
176  void UnknownDecl(clang::Decl* D, const std::string &txt = "") const;
177  void UnknownType(clang::QualType qual_type) const;
178  void UnsupportedDecl(clang::Decl* D,const std::string &txt = "") const;
179  void UnsupportedType(clang::QualType qual_type) const;
181  const clang::SourceManager* fSourceManager;
182  const cling::Interpreter &fInterpreter;
183  static const int fgDeclLast = clang::Decl::Var;
184  static const int fgTypeLast = clang::Type::TemplateTypeParm;
185  bool fDeclTable [ fgDeclLast+1 ];
186  clang::Decl * fLastDecl;
188  bool fTypeTable [ fgTypeLast+1 ];
191  static int fgBadClassCounter;
194  std::set<clang::RecordDecl*> fselectedRecordDecls; // Set for O(logN)
195  EScanType fScanType; // Differentiate among different kind of scans
196  bool fFirstPass; // This flag allows to run twice, for example in presence of dict selection and recursive template list manipulations.
197  DeclsSelRulesMap_t fDeclSelRuleMap; // Map decls to selection rules which selected them
199 };
201 #endif /* ROOT__RSCANNER_H__ */
void UnsupportedDecl(clang::Decl *D, const std::string &txt="") const
unsupported - this kind of declaration is probably not used (in current version of C++) ...
Definition: Scanner.cxx:308
std::string ConvTemplateParameterList(clang::TemplateParameterList *list) const
std::map< const clang::Decl *, const BaseSelectionRule * > DeclsSelRulesMap_t
Definition: Scanner.h:74
NamespaceColl_t fSelectedNamespaces
Definition: Scanner.h:125
std::string ConvTemplateName(clang::TemplateName &N) const
Definition: Scanner.cxx:416
bool VisitVarDecl(clang::VarDecl *D)
Definition: Scanner.cxx:888
bool TraverseStmt(clang::Stmt *)
Definition: Scanner.h:88
void UnknownDecl(clang::Decl *D, const std::string &txt="") const
unknown - this kind of declaration was not known to programmer
Definition: Scanner.cxx:286
void UnimportantDecl(clang::Decl *D, const std::string &txt="") const
unimportant - this kind of declaration is not stored into reflex
Definition: Scanner.cxx:319
void(* DeclCallback)(const clang::RecordDecl *)
Definition: Scanner.h:73
void UnimplementedDecl(clang::Decl *D, const std::string &txt="")
information about item, that should be implemented
Definition: Scanner.cxx:326
std::string GetEnumName(clang::EnumDecl *D) const
Definition: Scanner.cxx:377
std::string ConvTemplateArguments(const clang::TemplateArgumentList &list) const
static const int fgTypeLast
Definition: Scanner.h:184
static std::map< clang::Decl *, std::string > fgAnonymousEnumMap
Definition: Scanner.h:172
#define N
const clang::NamespaceDecl * fDecl
Definition: Scanner.h:62
static std::map< clang::Decl *, std::string > fgAnonymousClassMap
Definition: Scanner.h:171
bool GetDeclName(clang::Decl *D, std::string &name) const
Definition: Scanner.cxx:979
clang::Decl * fLastDecl
Definition: Scanner.h:186
static int fgAnonymousEnumCounter
Definition: Scanner.h:190
virtual ~RScanner()
Definition: Scanner.cxx:104
ROOT::TMetaUtils::TNormalizedCtxt & fNormCtxt
Definition: Scanner.h:192
unsigned int fVerboseLevel
Definition: Scanner.h:163
bool GetFunctionPrototype(clang::Decl *D, std::string &prototype) const
Definition: Scanner.cxx:1011
unsigned int VisibilityModifiers(clang::AccessSpecifier access) const
double Var(const RVec< T > &v)
Get the variance of the elements of an RVec.
Definition: RVec.hxx:862
bool fDeclTable[fgDeclLast+1]
Definition: Scanner.h:185
SelectionRules & fSelectionRules
Definition: Scanner.h:193
std::string ConvTemplateParams(clang::TemplateDecl *D) const
std::vector< const clang::EnumDecl * > EnumColl_t
Definition: Scanner.h:72
static const char * fgClangDeclKey
Definition: Scanner.h:118
bool shouldVisitDecl(clang::NamedDecl *D)
Whether we can actually visit this declaration, i.e.
Definition: Scanner.cxx:121
VariableColl_t fSelectedVariables
Definition: Scanner.h:128
static int fgAnonymousClassCounter
Definition: Scanner.h:189
bool VisitTypedefNameDecl(clang::TypedefNameDecl *D)
Visitor for every TypedefNameDecl, i.e.
Definition: Scanner.cxx:844
bool VisitFieldDecl(clang::FieldDecl *D)
Nothing to be done here.
Definition: Scanner.cxx:907
std::vector< const clang::TypedefNameDecl * > TypedefColl_t
Definition: Scanner.h:69
EScanType fScanType
Definition: Scanner.h:195
FunctionColl_t fSelectedFunctions
Definition: Scanner.h:127
bool TraverseClassTemplatePartialSpecializationDecl(clang::ClassTemplatePartialSpecializationDecl *)
Definition: Scanner.h:91
The class representing the collection of selection rules.
std::string GetSrcLocation(clang::SourceLocation L) const
Definition: Scanner.cxx:224
std::vector< DelayedAnnotatedRecordDeclInfo > fDelayedAnnotatedRecordDecls
Definition: Scanner.h:136
DeclCallback SetRecordDeclCallback(DeclCallback callback)
Set the callback to the RecordDecl and return the previous one.
Definition: Scanner.cxx:1078
std::vector< const clang::FunctionDecl * > FunctionColl_t
Definition: Scanner.h:70
const cling::Interpreter & fInterpreter
Definition: Scanner.h:182
RScanner(SelectionRules &rules, EScanType stype, const cling::Interpreter &interpret, ROOT::TMetaUtils::TNormalizedCtxt &normCtxt, unsigned int verbose=0)
Regular constructor setting up the scanner to search for entities matching the &#39;rules&#39;.
Definition: Scanner.cxx:76
std::vector< const clang::VarDecl * > VariableColl_t
Definition: Scanner.h:71
std::string ExprToStr(clang::Expr *expr) const
Definition: Scanner.cxx:401
unsigned int FuncModifiers(clang::FunctionDecl *D) const
static double C[]
const clang::ClassTemplateSpecializationDecl * fDecl
Definition: Scanner.h:133
void AddDelayedAnnotatedRecordDecls()
Definition: Scanner.cxx:810
bool operator<(const AnnotatedNamespaceDecl &right)
Definition: Scanner.h:60
const clang::NamespaceDecl * GetNamespaceDecl() const
Definition: Scanner.h:58
static const int fgDeclLast
Definition: Scanner.h:183
Definition: Scanner.h:76
void ShowTemplateInfo(const std::string &msg, const std::string &location="") const
Definition: Scanner.cxx:214
DeclsSelRulesMap_t fDeclSelRuleMap
Definition: Scanner.h:197
std::string GetName(clang::Decl *D) const
Definition: Scanner.cxx:251
ClassColl_t fSelectedClasses
Definition: Scanner.h:121
enumeration specifying the integration types.
bool shouldVisitTemplateInstantiations() const
Definition: Scanner.h:85
void ShowError(const std::string &msg, const std::string &location="") const
Definition: Scanner.cxx:206
void Scan(const clang::ASTContext &C)
Definition: Scanner.cxx:1047
unsigned int VarModifiers(clang::VarDecl *D) const
bool fFirstPass
Definition: Scanner.h:196
static constexpr double L
bool VisitFunctionDecl(clang::FunctionDecl *D)
Definition: Scanner.cxx:929
const clang::SourceManager * fSourceManager
Definition: Scanner.h:181
void DeclInfo(clang::Decl *D) const
Definition: Scanner.cxx:275
void ShowWarning(const std::string &msg, const std::string &location="") const
Definition: Scanner.cxx:198
bool VisitEnumDecl(clang::EnumDecl *D)
Definition: Scanner.cxx:870
Print a TSeq at the prompt:
Definition: TDatime.h:115
std::string GetLocation(clang::Decl *D) const
Definition: Scanner.cxx:234
bool VisitRecordDecl(clang::RecordDecl *D)
Definition: Scanner.cxx:521
bool fTypeTable[fgTypeLast+1]
Definition: Scanner.h:188
std::string FuncParameterList(clang::FunctionDecl *D) const
Definition: Scanner.cxx:458
std::set< clang::RecordDecl * > fselectedRecordDecls
Definition: Scanner.h:194
void UnexpectedDecl(clang::Decl *D, const std::string &txt="") const
unexpected - this kind of declaration is unexpected (in concrete place)
Definition: Scanner.cxx:297
EnumColl_t fSelectedEnums
Definition: Scanner.h:129
void UnsupportedType(clang::QualType qual_type) const
Definition: Scanner.cxx:368
std::string FuncParameters(clang::FunctionDecl *D) const
Definition: Scanner.cxx:431
int AddAnnotatedRecordDecl(const ClassSelectionRule *, const clang::Type *, const clang::RecordDecl *, const std::string &, const clang::TypedefNameDecl *, unsigned int indexOffset=0)
Definition: Scanner.cxx:534
const ClassSelectionRule * fSelected
Definition: Scanner.h:132
bool RequestOnlyTClass() const
Definition: Scanner.h:57
bool TraverseDeclContextHelper(clang::DeclContext *DC)
Definition: Scanner.cxx:949
AnnotatedNamespaceDecl(clang::NamespaceDecl *decl, long index, bool rRequestOnlyTClass)
Definition: Scanner.h:54
typedef void((*Func_t)())
bool VisitNamespaceDecl(clang::NamespaceDecl *D)
This method visits a namespace node.
Definition: Scanner.cxx:478
const clang::TypedefNameDecl * fTypedefNameDecl
Definition: Scanner.h:134
unsigned int Visibility(clang::Decl *D) const
std::vector< ROOT::TMetaUtils::AnnotatedRecordDecl > ClassColl_t
Definition: Scanner.h:68
std::vector< AnnotatedNamespaceDecl > NamespaceColl_t
Definition: Scanner.h:67
static const char * fgClangFuncKey
Definition: Scanner.h:119
const DeclsSelRulesMap_t & GetDeclsSelRulesMap() const
Definition: Scanner.h:121
void ShowInfo(const std::string &msg, const std::string &location="") const
Definition: Scanner.cxx:190
DeclCallback fRecordDeclCallback
Definition: Scanner.h:187
static bool GetDeclQualName(const clang::Decl *D, std::string &qual_name)
Definition: Scanner.cxx:995
bool TreatRecordDeclOrTypedefNameDecl(clang::TypeDecl *typeDecl)
Definition: Scanner.cxx:607
static int fgBadClassCounter
Definition: Scanner.h:191
char name[80]
Definition: TGX11.cxx:109
void UnknownType(clang::QualType qual_type) const
Definition: Scanner.cxx:359
TypedefColl_t fSelectedTypedefs
Definition: Scanner.h:126