ROOT  6.06/09
Reference Guide
Scanner.h
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
5 
6 
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  *************************************************************************/
14 
15 #ifndef ROOT__RSCANNER_H__
16 #define ROOT__RSCANNER_H__
17 
18 #include <stack>
19 
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"
26 
27 #include "llvm/IR/Module.h"
28 
29 #include "TMetaUtils.h"
30 
31 namespace clang {
32  class ClassTemplatePartialSpecializationDecl;
33  class ClassTemplateDecl;
34  class RecordDecl;
35  class Stmt;
36 }
37 
38 namespace cling {
39  class Interpreter;
40 }
41 
42 class SelectionRules;
43 class BaseSelectionRule;
44 
45 class RScanner: public clang::RecursiveASTVisitor<RScanner>
46 {
47 
48 public:
50  public:
51  AnnotatedNamespaceDecl(clang::NamespaceDecl *decl, long index, bool rRequestOnlyTClass) :
52  fDecl(decl), fRuleIndex(index), fRequestOnlyTClass(rRequestOnlyTClass){}
53  AnnotatedNamespaceDecl() { /* Nothing to do we do not own the pointer; */}
54  bool RequestOnlyTClass() const { return fRequestOnlyTClass; }
55  const clang::NamespaceDecl* GetNamespaceDecl() const { return fDecl; }
56  operator clang::NamespaceDecl const *() const { return fDecl; }
57  bool operator<(const AnnotatedNamespaceDecl& right) { return fRuleIndex < right.fRuleIndex; }
58  private:
59  const clang::NamespaceDecl *fDecl;
60  long fRuleIndex;
62  };
63 
64  typedef std::vector<AnnotatedNamespaceDecl> NamespaceColl_t;
65  typedef std::vector<ROOT::TMetaUtils::AnnotatedRecordDecl> ClassColl_t;
66  typedef std::vector<clang::TypedefNameDecl*> TypedefColl_t;
67  typedef std::vector<clang::FunctionDecl*> FunctionColl_t;
68  typedef std::vector<clang::VarDecl*> VariableColl_t;
69  typedef std::vector<clang::EnumDecl*> EnumColl_t;
70  typedef void (*DeclCallback)(const char *type);
71  typedef std::map<clang::Decl*,const BaseSelectionRule*> DeclsSelRulesMap_t;
72 
73  enum class EScanType : char {kNormal, kTwoPasses, kOnePCM};
74 
75  RScanner (SelectionRules &rules,
77  const cling::Interpreter &interpret,
79  unsigned int verbose = 0);
80 
81  // Configure the vistitor to also visit template instantiation.
82  bool shouldVisitTemplateInstantiations() const { return true; }
83 
84  // Don't descend into function bodies.
85  bool TraverseStmt(clang::Stmt*) { return true; }
86 
87  // Don't descend into templates partial specialization (but only instances thereof).
88  bool TraverseClassTemplatePartialSpecializationDecl(clang::ClassTemplatePartialSpecializationDecl*) { return true; }
89 
90  bool VisitEnumDecl(clang::EnumDecl* D); //Visitor for every EnumDecl i.e. enumeration node in the AST
91  bool VisitFieldDecl(clang::FieldDecl* D); //Visitor for e field inside a class
92  bool VisitFunctionDecl(clang::FunctionDecl* D); //Visitor for every FunctionDecl i.e. function node in the AST
93  bool VisitNamespaceDecl(clang::NamespaceDecl* D); // Visitor for every RecordDecl i.e. class node in the AST
94  bool VisitRecordDecl(clang::RecordDecl* D); // Visitor for every RecordDecl i.e. class node in the AST
95  bool VisitTypedefNameDecl(clang::TypedefNameDecl* D); // Visitor for every TypedefNameDecl i.e. class node in the AST
96  bool VisitVarDecl(clang::VarDecl* D); //Visitor for every VarDecl i.e. variable node in the AST
97 
98  bool TreatRecordDeclOrTypedefNameDecl(clang::TypeDecl* typeDecl); //Function called by VisitTypedefNameDecl and VisitRecordDecl
99 
100  bool TraverseDeclContextHelper(clang::DeclContext *DC); // Here is the code magic :) - every Decl
101  // according to its type is processed by the corresponding Visitor method
102 
103  // Set a callback to record which are declared.
105 
106  // Main interface of this class.
107  void Scan(const clang::ASTContext &C);
108 
109  // Utility routines. Most belongs in TMetaUtils and should be shared with rootcling.cxx
110  std::string GetClassName(clang::DeclContext* DC) const;
111  bool GetDeclName(clang::Decl* D, std::string& name) const;
112  bool GetDeclQualName(clang::Decl* D, std::string& qual_name) const;
113  bool GetFunctionPrototype(clang::Decl* D, std::string& prototype) const;
114 
115  static const char* fgClangDeclKey; // property key used for CLang declaration objects
116  static const char* fgClangFuncKey; // property key for function (demangled) names
117 
118  const DeclsSelRulesMap_t& GetDeclsSelRulesMap() const {return fDeclSelRuleMap;};
119 
120  // public for now, the list of selected classes.
121  ClassColl_t fSelectedClasses;
122  NamespaceColl_t fSelectedNamespaces;
123  TypedefColl_t fSelectedTypedefs;
124  FunctionColl_t fSelectedFunctions;
125  VariableColl_t fSelectedVariables;
126  EnumColl_t fSelectedEnums;
127 
128  virtual ~ RScanner ();
129 
130 
131 
132 private:
133 
134  std::string ConvTemplateArguments(const clang::TemplateArgumentList& list) const;
135  std::string ConvTemplateName(clang::TemplateName& N) const;
136  std::string ConvTemplateParameterList(clang::TemplateParameterList* list) const;
137  std::string ConvTemplateParams(clang::TemplateDecl* D) const;
138  void DeclInfo(clang::Decl* D) const;
139  std::string ExprToStr(clang::Expr* expr) const;
140  std::string FuncParameterList(clang::FunctionDecl* D) const;
141  std::string FuncParameters(clang::FunctionDecl* D) const;
142  std::string GetClassName(clang::RecordDecl* D) const;
143  std::string GetEnumName(clang::EnumDecl* D) const;
144  std::string GetLocation(clang::Decl* D) const;
145  std::string GetName(clang::Decl* D) const;
146  std::string GetSrcLocation(clang::SourceLocation L) const;
147  unsigned int FuncModifiers(clang::FunctionDecl* D) const;
148  unsigned int fVerboseLevel;
149  unsigned int VarModifiers(clang::VarDecl* D) const;
150  unsigned int Visibility(clang::Decl* D) const;
151  unsigned int VisibilityModifiers(clang::AccessSpecifier access) const;
152  void ShowError(const std::string &msg, const std::string &location = "") const;
153  void ShowInfo(const std::string &msg, const std::string &location = "") const;
154  void ShowTemplateInfo(const std::string &msg, const std::string &location = "") const;
155  void ShowWarning(const std::string &msg, const std::string &location = "") const;
156  static std::map <clang::Decl*, std::string> fgAnonymousClassMap;
157  static std::map <clang::Decl*, std::string> fgAnonymousEnumMap;
158  void UnexpectedDecl(clang::Decl* D,const std::string &txt = "") const;
159  void UnimplementedDecl(clang::Decl* D,const std::string &txt = "");
160  void UnimplementedType(clang::QualType qual_type);
161  void UnimplementedType (const clang::Type* T);
162  void UnimportantDecl(clang::Decl* D,const std::string &txt = "") const;
163  void UnimportantType(clang::QualType qual_type) const;
164  void UnknownDecl(clang::Decl* D, const std::string &txt = "") const;
165  void UnknownType(clang::QualType qual_type) const;
166  void UnsupportedDecl(clang::Decl* D,const std::string &txt = "") const;
167  void UnsupportedType(clang::QualType qual_type) const;
168 
169  const clang::SourceManager* fSourceManager;
170  const cling::Interpreter &fInterpreter;
171  static const int fgDeclLast = clang::Decl::Var;
172  static const int fgTypeLast = clang::Type::TemplateTypeParm;
173  bool fDeclTable [ fgDeclLast+1 ];
174  clang::Decl * fLastDecl;
176  bool fTypeTable [ fgTypeLast+1 ];
179  static int fgBadClassCounter;
182  std::set<clang::RecordDecl*> fselectedRecordDecls; // Set for O(logN)
183  EScanType fScanType; // Differentiate among different kind of scans
184  bool fFirstPass; // This flag allows to run twice, for example in presence of dict selection and recursive template list manipulations.
185  DeclsSelRulesMap_t fDeclSelRuleMap; // Map decls to selection rules which selected them
186 
187 };
188 
189 #endif /* ROOT__RSCANNER_H__ */
std::string ConvTemplateArguments(const clang::TemplateArgumentList &list) const
NamespaceColl_t fSelectedNamespaces
Definition: Scanner.h:122
bool shouldVisitTemplateInstantiations() const
Definition: Scanner.h:82
bool VisitVarDecl(clang::VarDecl *D)
Definition: Scanner.cxx:1008
void ShowWarning(const std::string &msg, const std::string &location="") const
Definition: Scanner.cxx:207
bool TraverseStmt(clang::Stmt *)
Definition: Scanner.h:85
bool GetDeclQualName(clang::Decl *D, std::string &qual_name) const
Definition: Scanner.cxx:1127
void UnimplementedDecl(clang::Decl *D, const std::string &txt="")
information about item, that should be implemented
Definition: Scanner.cxx:347
std::string GetLocation(clang::Decl *D) const
Definition: Scanner.cxx:255
RooArgList L(const RooAbsArg &v1)
double T(double x)
Definition: ChebyshevPol.h:34
static const int fgTypeLast
Definition: Scanner.h:172
static std::map< clang::Decl *, std::string > fgAnonymousEnumMap
Definition: Scanner.h:157
#define N
void ShowTemplateInfo(const std::string &msg, const std::string &location="") const
Definition: Scanner.cxx:233
void(* DeclCallback)(const char *type)
Definition: Scanner.h:70
const clang::NamespaceDecl * fDecl
Definition: Scanner.h:59
static std::map< clang::Decl *, std::string > fgAnonymousClassMap
Definition: Scanner.h:156
clang::Decl * fLastDecl
Definition: Scanner.h:174
static int fgAnonymousEnumCounter
Definition: Scanner.h:178
virtual ~RScanner()
Definition: Scanner.cxx:122
ROOT::TMetaUtils::TNormalizedCtxt & fNormCtxt
Definition: Scanner.h:180
unsigned int fVerboseLevel
Definition: Scanner.h:148
bool fDeclTable[fgDeclLast+1]
Definition: Scanner.h:173
SelectionRules & fSelectionRules
Definition: Scanner.h:181
void UnexpectedDecl(clang::Decl *D, const std::string &txt="") const
unexpected - this kind of declaration is unexpected (in concrete place)
Definition: Scanner.cxx:318
static const char * fgClangDeclKey
Definition: Scanner.h:115
unsigned int VarModifiers(clang::VarDecl *D) const
VariableColl_t fSelectedVariables
Definition: Scanner.h:125
const char Int_t const char TProof Int_t stype
Definition: TXSlave.cxx:46
void UnimportantDecl(clang::Decl *D, const std::string &txt="") const
unimportant - this kind of declaration is not stored into reflex
Definition: Scanner.cxx:340
static int fgAnonymousClassCounter
Definition: Scanner.h:177
bool VisitTypedefNameDecl(clang::TypedefNameDecl *D)
Visitor for every TypedefNameDecl, i.e.
Definition: Scanner.cxx:970
bool GetFunctionPrototype(clang::Decl *D, std::string &prototype) const
Definition: Scanner.cxx:1143
bool VisitFieldDecl(clang::FieldDecl *D)
Nothing to be done here.
Definition: Scanner.cxx:1024
std::string FuncParameterList(clang::FunctionDecl *D) const
Definition: Scanner.cxx:644
void UnknownDecl(clang::Decl *D, const std::string &txt="") const
unknown - this kind of declaration was not known to programmer
Definition: Scanner.cxx:307
EScanType fScanType
Definition: Scanner.h:183
unsigned int Visibility(clang::Decl *D) const
FunctionColl_t fSelectedFunctions
Definition: Scanner.h:124
bool TraverseClassTemplatePartialSpecializationDecl(clang::ClassTemplatePartialSpecializationDecl *)
Definition: Scanner.h:88
The class representing the collection of selection rules.
std::string GetSrcLocation(clang::SourceLocation L) const
Definition: Scanner.cxx:245
std::vector< clang::FunctionDecl * > FunctionColl_t
Definition: Scanner.h:67
void UnimplementedType(clang::QualType qual_type)
Definition: Scanner.cxx:407
DeclCallback SetRecordDeclCallback(DeclCallback callback)
Set the callback to the RecordDecl and return the previous one.
Definition: Scanner.cxx:1207
const cling::Interpreter & fInterpreter
Definition: Scanner.h:170
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 'rules'.
Definition: Scanner.cxx:94
std::string GetName(clang::Decl *D) const
Definition: Scanner.cxx:272
bool RequestOnlyTClass() const
Definition: Scanner.h:54
static double C[]
std::vector< clang::VarDecl * > VariableColl_t
Definition: Scanner.h:68
const DeclsSelRulesMap_t & GetDeclsSelRulesMap() const
Definition: Scanner.h:118
bool verbose
bool operator<(const AnnotatedNamespaceDecl &right)
Definition: Scanner.h:57
static const int fgDeclLast
Definition: Scanner.h:171
std::vector< clang::EnumDecl * > EnumColl_t
Definition: Scanner.h:69
EScanType
Definition: Scanner.h:73
void ShowError(const std::string &msg, const std::string &location="") const
Definition: Scanner.cxx:221
DeclsSelRulesMap_t fDeclSelRuleMap
Definition: Scanner.h:185
ClassColl_t fSelectedClasses
Definition: Scanner.h:118
std::string GetEnumName(clang::EnumDecl *D) const
Definition: Scanner.cxx:482
std::string ConvTemplateParams(clang::TemplateDecl *D) const
Type
enumeration specifying the integration types.
void ShowInfo(const std::string &msg, const std::string &location="") const
Definition: Scanner.cxx:195
void Scan(const clang::ASTContext &C)
Definition: Scanner.cxx:1179
const clang::NamespaceDecl * GetNamespaceDecl() const
Definition: Scanner.h:55
std::string ExprToStr(clang::Expr *expr) const
Definition: Scanner.cxx:506
bool fFirstPass
Definition: Scanner.h:184
bool VisitFunctionDecl(clang::FunctionDecl *D)
Definition: Scanner.cxx:1052
const clang::SourceManager * fSourceManager
Definition: Scanner.h:169
Definition: TCling.h:48
std::map< clang::Decl *, const BaseSelectionRule * > DeclsSelRulesMap_t
Definition: Scanner.h:71
void DeclInfo(clang::Decl *D) const
Definition: Scanner.cxx:296
int type
Definition: TGX11.cxx:120
bool VisitEnumDecl(clang::EnumDecl *D)
Definition: Scanner.cxx:993
Definition: TDatime.h:114
std::string ConvTemplateName(clang::TemplateName &N) const
Definition: Scanner.cxx:521
bool VisitRecordDecl(clang::RecordDecl *D)
Definition: Scanner.cxx:713
bool fTypeTable[fgTypeLast+1]
Definition: Scanner.h:176
std::set< clang::RecordDecl * > fselectedRecordDecls
Definition: Scanner.h:182
EnumColl_t fSelectedEnums
Definition: Scanner.h:126
#define name(a, b)
Definition: linkTestLib0.cpp:5
bool TraverseDeclContextHelper(clang::DeclContext *DC)
Definition: Scanner.cxx:1069
AnnotatedNamespaceDecl(clang::NamespaceDecl *decl, long index, bool rRequestOnlyTClass)
Definition: Scanner.h:51
typedef void((*Func_t)())
bool VisitNamespaceDecl(clang::NamespaceDecl *D)
This method visits a namespace node.
Definition: Scanner.cxx:664
std::vector< ROOT::TMetaUtils::AnnotatedRecordDecl > ClassColl_t
Definition: Scanner.h:65
void UnknownType(clang::QualType qual_type) const
Definition: Scanner.cxx:382
std::vector< AnnotatedNamespaceDecl > NamespaceColl_t
Definition: Scanner.h:64
unsigned int VisibilityModifiers(clang::AccessSpecifier access) const
static const char * fgClangFuncKey
Definition: Scanner.h:116
std::string FuncParameters(clang::FunctionDecl *D) const
Definition: Scanner.cxx:617
bool GetDeclName(clang::Decl *D, std::string &name) const
Definition: Scanner.cxx:1111
DeclCallback fRecordDeclCallback
Definition: Scanner.h:175
unsigned int FuncModifiers(clang::FunctionDecl *D) const
std::string GetClassName(clang::DeclContext *DC) const
Definition: Scanner.cxx:1099
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:329
bool TreatRecordDeclOrTypedefNameDecl(clang::TypeDecl *typeDecl)
Definition: Scanner.cxx:723
void UnsupportedType(clang::QualType qual_type) const
Definition: Scanner.cxx:391
static int fgBadClassCounter
Definition: Scanner.h:179
void UnimportantType(clang::QualType qual_type) const
unimportant - this kind of declaration is not stored into reflex
Definition: Scanner.cxx:401
std::vector< clang::TypedefNameDecl * > TypedefColl_t
Definition: Scanner.h:66
std::string ConvTemplateParameterList(clang::TemplateParameterList *list) const
TypedefColl_t fSelectedTypedefs
Definition: Scanner.h:123