Logo ROOT  
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 #include <vector>
20 #include <string>
21 #include <map>
22 #include <set>
23 
24 #include "clang/AST/AST.h"
25 #include "clang/AST/ASTContext.h"
26 #include "clang/AST/DeclGroup.h"
27 #include "clang/AST/DeclFriend.h"
28 #include "clang/AST/Type.h"
29 #include "clang/AST/RecursiveASTVisitor.h"
30 
31 #include "llvm/IR/Module.h"
32 
33 #include "TClingUtils.h"
34 
35 namespace clang {
36  class ClassTemplatePartialSpecializationDecl;
37  class ClassTemplateDecl;
38  class RecordDecl;
39  class Stmt;
40 }
41 
42 namespace cling {
43  class Interpreter;
44 }
45 
46 class SelectionRules;
47 class BaseSelectionRule;
48 class ClassSelectionRule;
49 
50 class RScanner: public clang::RecursiveASTVisitor<RScanner>
51 {
52 
53  bool shouldVisitDecl(clang::NamedDecl *D);
54 
55 public:
57  public:
58  AnnotatedNamespaceDecl(clang::NamespaceDecl *decl, long index, bool rRequestOnlyTClass) :
59  fDecl(decl), fRuleIndex(index), fRequestOnlyTClass(rRequestOnlyTClass){}
60  AnnotatedNamespaceDecl() { /* Nothing to do we do not own the pointer; */}
61  bool RequestOnlyTClass() const { return fRequestOnlyTClass; }
62  const clang::NamespaceDecl* GetNamespaceDecl() const { return fDecl; }
63  operator clang::NamespaceDecl const *() const { return fDecl; }
64  bool operator<(const AnnotatedNamespaceDecl& right) { return fRuleIndex < right.fRuleIndex; }
65  private:
66  const clang::NamespaceDecl *fDecl;
67  long fRuleIndex;
69  };
70 
71  typedef std::vector<AnnotatedNamespaceDecl> NamespaceColl_t;
72  typedef std::vector<ROOT::TMetaUtils::AnnotatedRecordDecl> ClassColl_t;
73  typedef std::vector<const clang::TypedefNameDecl*> TypedefColl_t;
74  typedef std::vector<const clang::FunctionDecl*> FunctionColl_t;
75  typedef std::vector<const clang::VarDecl*> VariableColl_t;
76  typedef std::vector<const clang::EnumDecl*> EnumColl_t;
77  typedef void (*DeclCallback)(const clang::RecordDecl*);
78  typedef std::map<const clang::Decl*,const BaseSelectionRule*> DeclsSelRulesMap_t;
79 
80  enum class EScanType : char {kNormal, kTwoPasses, kOnePCM};
81 
82  RScanner (SelectionRules &rules,
83  EScanType stype,
84  const cling::Interpreter &interpret,
86  unsigned int verbose = 0);
87 
88  // Configure the vistitor to also visit template instantiation.
89  bool shouldVisitTemplateInstantiations() const { return true; }
90 
91  // Don't descend into function bodies.
92  bool TraverseStmt(clang::Stmt*) { return true; }
93 
94  // Don't descend into templates partial specialization (but only instances thereof).
95  bool TraverseClassTemplatePartialSpecializationDecl(clang::ClassTemplatePartialSpecializationDecl*) { return true; }
96 
97  bool VisitEnumDecl(clang::EnumDecl* D); //Visitor for every EnumDecl i.e. enumeration node in the AST
98  bool VisitFieldDecl(clang::FieldDecl* D); //Visitor for e field inside a class
99  bool VisitFunctionDecl(clang::FunctionDecl* D); //Visitor for every FunctionDecl i.e. function node in the AST
100  bool VisitNamespaceDecl(clang::NamespaceDecl* D); // Visitor for every RecordDecl i.e. class node in the AST
101  bool VisitRecordDecl(clang::RecordDecl* D); // Visitor for every RecordDecl i.e. class node in the AST
102  bool VisitTypedefNameDecl(clang::TypedefNameDecl* D); // Visitor for every TypedefNameDecl i.e. class node in the AST
103  bool VisitVarDecl(clang::VarDecl* D); //Visitor for every VarDecl i.e. variable node in the AST
104 
105  bool TreatRecordDeclOrTypedefNameDecl(clang::TypeDecl* typeDecl); //Function called by VisitTypedefNameDecl and VisitRecordDecl
107 
108  bool TraverseDeclContextHelper(clang::DeclContext *DC); // Here is the code magic :) - every Decl
109  // according to its type is processed by the corresponding Visitor method
110 
111  // Set a callback to record which are declared.
113 
114  // Main interface of this class.
115  void Scan(const clang::ASTContext &C);
116 
117  // Utility routines. Most belongs in TMetaUtils and should be shared with rootcling.cxx
118  bool GetDeclName(clang::Decl* D, std::string& name) const;
119  static bool GetDeclQualName(const clang::Decl* D, std::string& qual_name);
120  bool GetFunctionPrototype(clang::Decl* D, std::string& prototype) const;
121 
122  static const char* fgClangDeclKey; // property key used for CLang declaration objects
123  static const char* fgClangFuncKey; // property key for function (demangled) names
124 
126 
127  // public for now, the list of selected classes.
134 
137  const clang::ClassTemplateSpecializationDecl *fDecl;
138  const clang::TypedefNameDecl* fTypedefNameDecl;
139  };
140  std::vector<DelayedAnnotatedRecordDeclInfo> fDelayedAnnotatedRecordDecls;
141 
142  virtual ~RScanner ();
143 
144 
145 
146 private:
147 
149  const clang::Type*,
150  const clang::RecordDecl*,
151  const std::string&,
152  const clang::TypedefNameDecl*,
153  unsigned int indexOffset=0);
154  std::string ConvTemplateArguments(const clang::TemplateArgumentList& list) const;
155  std::string ConvTemplateName(clang::TemplateName& N) const;
156  std::string ConvTemplateParameterList(clang::TemplateParameterList* list) const;
157  std::string ConvTemplateParams(clang::TemplateDecl* D) const;
158  void DeclInfo(clang::Decl* D) const;
159  std::string ExprToStr(clang::Expr* expr) const;
160  std::string FuncParameterList(clang::FunctionDecl* D) const;
161  std::string FuncParameters(clang::FunctionDecl* D) const;
162  std::string GetEnumName(clang::EnumDecl* D) const;
163  std::string GetLocation(clang::Decl* D) const;
164  std::string GetName(clang::Decl* D) const;
165  std::string GetSrcLocation(clang::SourceLocation L) const;
166  unsigned int FuncModifiers(clang::FunctionDecl* D) const;
167  unsigned int fVerboseLevel;
168  unsigned int VarModifiers(clang::VarDecl* D) const;
169  unsigned int Visibility(clang::Decl* D) const;
170  unsigned int VisibilityModifiers(clang::AccessSpecifier access) const;
171  void ShowError(const std::string &msg, const std::string &location = "") const;
172  void ShowInfo(const std::string &msg, const std::string &location = "") const;
173  void ShowTemplateInfo(const std::string &msg, const std::string &location = "") const;
174  void ShowWarning(const std::string &msg, const std::string &location = "") const;
175  static std::map <clang::Decl*, std::string> fgAnonymousClassMap;
176  static std::map <clang::Decl*, std::string> fgAnonymousEnumMap;
177  void UnexpectedDecl(clang::Decl* D,const std::string &txt = "") const;
178  void UnimplementedDecl(clang::Decl* D,const std::string &txt = "");
179  void UnimportantDecl(clang::Decl* D,const std::string &txt = "") const;
180  void UnknownDecl(clang::Decl* D, const std::string &txt = "") const;
181  void UnknownType(clang::QualType qual_type) const;
182  void UnsupportedDecl(clang::Decl* D,const std::string &txt = "") const;
183  void UnsupportedType(clang::QualType qual_type) const;
184 
185  const clang::SourceManager* fSourceManager;
186  const cling::Interpreter &fInterpreter;
187  static const int fgDeclLast = clang::Decl::Var;
188  static const int fgTypeLast = clang::Type::TemplateTypeParm;
189  bool fDeclTable [ fgDeclLast+1 ];
190  clang::Decl * fLastDecl;
192  bool fTypeTable [ fgTypeLast+1 ];
195  static int fgBadClassCounter;
198  std::set<clang::RecordDecl*> fselectedRecordDecls; // Set for O(logN)
199  EScanType fScanType; // Differentiate among different kind of scans
200  bool fFirstPass; // This flag allows to run twice, for example in presence of dict selection and recursive template list manipulations.
201  DeclsSelRulesMap_t fDeclSelRuleMap; // Map decls to selection rules which selected them
202 
203 };
204 
205 #endif /* ROOT__RSCANNER_H__ */
RScanner::fFirstPass
bool fFirstPass
Definition: Scanner.h:200
ROOT::VecOps::Var
double Var(const RVec< T > &v)
Get the variance of the elements of an RVec.
Definition: RVec.hxx:861
RScanner::ConvTemplateParams
std::string ConvTemplateParams(clang::TemplateDecl *D) const
RScanner::fSelectedEnums
EnumColl_t fSelectedEnums
Definition: Scanner.h:133
RScanner::AnnotatedNamespaceDecl::fRequestOnlyTClass
bool fRequestOnlyTClass
Definition: Scanner.h:68
RScanner::EnumColl_t
std::vector< const clang::EnumDecl * > EnumColl_t
Definition: Scanner.h:76
RScanner::AnnotatedNamespaceDecl::RequestOnlyTClass
bool RequestOnlyTClass() const
Definition: Scanner.h:61
RScanner::DeclsSelRulesMap_t
std::map< const clang::Decl *, const BaseSelectionRule * > DeclsSelRulesMap_t
Definition: Scanner.h:78
RScanner::TraverseDeclContextHelper
bool TraverseDeclContextHelper(clang::DeclContext *DC)
Definition: Scanner.cxx:947
RScanner::ExprToStr
std::string ExprToStr(clang::Expr *expr) const
Definition: Scanner.cxx:401
RScanner::EScanType::kNormal
@ kNormal
RScanner::DeclInfo
void DeclInfo(clang::Decl *D) const
Definition: Scanner.cxx:275
RScanner::fgTypeLast
static const int fgTypeLast
Definition: Scanner.h:188
RScanner::UnexpectedDecl
void UnexpectedDecl(clang::Decl *D, const std::string &txt="") const
unexpected - this kind of declaration is unexpected (in concrete place)
Definition: Scanner.cxx:297
RScanner::fSelectionRules
SelectionRules & fSelectionRules
Definition: Scanner.h:197
RScanner::fgClangFuncKey
static const char * fgClangFuncKey
Definition: Scanner.h:123
RScanner::UnknownDecl
void UnknownDecl(clang::Decl *D, const std::string &txt="") const
unknown - this kind of declaration was not known to programmer
Definition: Scanner.cxx:286
RScanner::fLastDecl
clang::Decl * fLastDecl
Definition: Scanner.h:190
RScanner::NamespaceColl_t
std::vector< AnnotatedNamespaceDecl > NamespaceColl_t
Definition: Scanner.h:71
RScanner::~RScanner
virtual ~RScanner()
Definition: Scanner.cxx:104
RScanner::fgClangDeclKey
static const char * fgClangDeclKey
Definition: Scanner.h:122
RScanner::GetEnumName
std::string GetEnumName(clang::EnumDecl *D) const
Definition: Scanner.cxx:377
RScanner::FuncModifiers
unsigned int FuncModifiers(clang::FunctionDecl *D) const
RScanner::EScanType::kTwoPasses
@ kTwoPasses
RScanner::VisitFieldDecl
bool VisitFieldDecl(clang::FieldDecl *D)
Nothing to be done here.
Definition: Scanner.cxx:905
RScanner::fSourceManager
const clang::SourceManager * fSourceManager
Definition: Scanner.h:185
RScanner::AnnotatedNamespaceDecl::operator<
bool operator<(const AnnotatedNamespaceDecl &right)
Definition: Scanner.h:64
RScanner::DelayedAnnotatedRecordDeclInfo
Definition: Scanner.h:135
RScanner::ShowWarning
void ShowWarning(const std::string &msg, const std::string &location="") const
Definition: Scanner.cxx:198
N
#define N
RScanner::fgAnonymousClassMap
static std::map< clang::Decl *, std::string > fgAnonymousClassMap
Definition: Scanner.h:175
RScanner::FuncParameters
std::string FuncParameters(clang::FunctionDecl *D) const
Definition: Scanner.cxx:431
RScanner::VariableColl_t
std::vector< const clang::VarDecl * > VariableColl_t
Definition: Scanner.h:75
RScanner::fNormCtxt
ROOT::TMetaUtils::TNormalizedCtxt & fNormCtxt
Definition: Scanner.h:196
RScanner::UnsupportedDecl
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
RScanner::EScanType
EScanType
Definition: Scanner.h:80
RScanner::shouldVisitTemplateInstantiations
bool shouldVisitTemplateInstantiations() const
Definition: Scanner.h:89
RScanner::UnimplementedDecl
void UnimplementedDecl(clang::Decl *D, const std::string &txt="")
information about item, that should be implemented
Definition: Scanner.cxx:326
RScanner::VisitTypedefNameDecl
bool VisitTypedefNameDecl(clang::TypedefNameDecl *D)
Visitor for every TypedefNameDecl, i.e.
Definition: Scanner.cxx:842
RScanner::ConvTemplateArguments
std::string ConvTemplateArguments(const clang::TemplateArgumentList &list) const
ROOT::Math::IntegrationOneDim::Type
Type
enumeration specifying the integration types.
Definition: AllIntegrationTypes.h:60
RScanner::UnimportantDecl
void UnimportantDecl(clang::Decl *D, const std::string &txt="") const
unimportant - this kind of declaration is not stored into reflex
Definition: Scanner.cxx:319
RScanner::ShowTemplateInfo
void ShowTemplateInfo(const std::string &msg, const std::string &location="") const
Definition: Scanner.cxx:214
RScanner::fgDeclLast
static const int fgDeclLast
Definition: Scanner.h:187
ROOT::Math::Cephes::C
static double C[]
Definition: SpecFuncCephes.cxx:187
RScanner::VarModifiers
unsigned int VarModifiers(clang::VarDecl *D) const
RScanner::AnnotatedNamespaceDecl::AnnotatedNamespaceDecl
AnnotatedNamespaceDecl()
Definition: Scanner.h:60
TGeant4Unit::L
static constexpr double L
Definition: TGeant4SystemOfUnits.h:123
RScanner::ShowInfo
void ShowInfo(const std::string &msg, const std::string &location="") const
Definition: Scanner.cxx:190
RScanner::fDeclTable
bool fDeclTable[fgDeclLast+1]
Definition: Scanner.h:189
RScanner::TypedefColl_t
std::vector< const clang::TypedefNameDecl * > TypedefColl_t
Definition: Scanner.h:73
RScanner::UnknownType
void UnknownType(clang::QualType qual_type) const
Definition: Scanner.cxx:359
RScanner::ClassColl_t
std::vector< ROOT::TMetaUtils::AnnotatedRecordDecl > ClassColl_t
Definition: Scanner.h:72
RScanner::GetDeclName
bool GetDeclName(clang::Decl *D, std::string &name) const
Definition: Scanner.cxx:977
RScanner::AnnotatedNamespaceDecl::fDecl
const clang::NamespaceDecl * fDecl
Definition: Scanner.h:66
RScanner::fScanType
EScanType fScanType
Definition: Scanner.h:199
RScanner::GetName
std::string GetName(clang::Decl *D) const
Definition: Scanner.cxx:251
RScanner::Scan
void Scan(const clang::ASTContext &C)
Definition: Scanner.cxx:1045
RScanner::GetSrcLocation
std::string GetSrcLocation(clang::SourceLocation L) const
Definition: Scanner.cxx:224
RScanner::ShowError
void ShowError(const std::string &msg, const std::string &location="") const
Definition: Scanner.cxx:206
RScanner::ConvTemplateParameterList
std::string ConvTemplateParameterList(clang::TemplateParameterList *list) const
RScanner::AddDelayedAnnotatedRecordDecls
void AddDelayedAnnotatedRecordDecls()
Definition: Scanner.cxx:808
RScanner::DelayedAnnotatedRecordDeclInfo::fSelected
const ClassSelectionRule * fSelected
Definition: Scanner.h:136
RScanner::AnnotatedNamespaceDecl::AnnotatedNamespaceDecl
AnnotatedNamespaceDecl(clang::NamespaceDecl *decl, long index, bool rRequestOnlyTClass)
Definition: Scanner.h:58
RScanner::VisitNamespaceDecl
bool VisitNamespaceDecl(clang::NamespaceDecl *D)
This method visits a namespace node.
Definition: Scanner.cxx:478
ClassSelectionRule
Definition: ClassSelectionRule.h:33
RScanner::DelayedAnnotatedRecordDeclInfo::fTypedefNameDecl
const clang::TypedefNameDecl * fTypedefNameDecl
Definition: Scanner.h:138
RScanner::fSelectedFunctions
FunctionColl_t fSelectedFunctions
Definition: Scanner.h:131
RScanner::TraverseClassTemplatePartialSpecializationDecl
bool TraverseClassTemplatePartialSpecializationDecl(clang::ClassTemplatePartialSpecializationDecl *)
Definition: Scanner.h:95
RScanner::DelayedAnnotatedRecordDeclInfo::fDecl
const clang::ClassTemplateSpecializationDecl * fDecl
Definition: Scanner.h:137
RScanner::fgAnonymousEnumCounter
static int fgAnonymousEnumCounter
Definition: Scanner.h:194
RScanner::fselectedRecordDecls
std::set< clang::RecordDecl * > fselectedRecordDecls
Definition: Scanner.h:198
RScanner::TreatRecordDeclOrTypedefNameDecl
bool TreatRecordDeclOrTypedefNameDecl(clang::TypeDecl *typeDecl)
Definition: Scanner.cxx:607
void
typedef void((*Func_t)())
ROOT::TMetaUtils::TNormalizedCtxt
Definition: TClingUtils.h:138
RScanner::shouldVisitDecl
bool shouldVisitDecl(clang::NamedDecl *D)
Whether we can actually visit this declaration, i.e.
Definition: Scanner.cxx:121
RScanner::fSelectedClasses
ClassColl_t fSelectedClasses
Definition: Scanner.h:125
RScanner::GetDeclQualName
static bool GetDeclQualName(const clang::Decl *D, std::string &qual_name)
Definition: Scanner.cxx:993
RScanner::GetDeclsSelRulesMap
const DeclsSelRulesMap_t & GetDeclsSelRulesMap() const
Definition: Scanner.h:125
RScanner::DeclCallback
void(* DeclCallback)(const clang::RecordDecl *)
Definition: Scanner.h:77
RScanner::EScanType::kOnePCM
@ kOnePCM
RScanner::ConvTemplateName
std::string ConvTemplateName(clang::TemplateName &N) const
Definition: Scanner.cxx:416
RScanner::Visibility
unsigned int Visibility(clang::Decl *D) const
RScanner::fVerboseLevel
unsigned int fVerboseLevel
Definition: Scanner.h:167
RScanner::fSelectedVariables
VariableColl_t fSelectedVariables
Definition: Scanner.h:132
RScanner
Definition: Scanner.h:50
RScanner::fDelayedAnnotatedRecordDecls
std::vector< DelayedAnnotatedRecordDeclInfo > fDelayedAnnotatedRecordDecls
Definition: Scanner.h:140
clang
Definition: BaseSelectionRule.h:29
RScanner::fTypeTable
bool fTypeTable[fgTypeLast+1]
Definition: Scanner.h:192
RScanner::SetRecordDeclCallback
DeclCallback SetRecordDeclCallback(DeclCallback callback)
Set the callback to the RecordDecl and return the previous one.
Definition: Scanner.cxx:1076
name
char name[80]
Definition: TGX11.cxx:110
RScanner::FuncParameterList
std::string FuncParameterList(clang::FunctionDecl *D) const
Definition: Scanner.cxx:458
TClingUtils.h
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
RScanner::fSelectedNamespaces
NamespaceColl_t fSelectedNamespaces
Definition: Scanner.h:129
RScanner::GetLocation
std::string GetLocation(clang::Decl *D) const
Definition: Scanner.cxx:234
RScanner::AnnotatedNamespaceDecl::fRuleIndex
long fRuleIndex
Definition: Scanner.h:67
RScanner::AnnotatedNamespaceDecl::GetNamespaceDecl
const clang::NamespaceDecl * GetNamespaceDecl() const
Definition: Scanner.h:62
RScanner::FunctionColl_t
std::vector< const clang::FunctionDecl * > FunctionColl_t
Definition: Scanner.h:74
RScanner::VisitVarDecl
bool VisitVarDecl(clang::VarDecl *D)
Definition: Scanner.cxx:886
RScanner::RScanner
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:76
RScanner::VisibilityModifiers
unsigned int VisibilityModifiers(clang::AccessSpecifier access) const
RScanner::fgAnonymousClassCounter
static int fgAnonymousClassCounter
Definition: Scanner.h:193
RScanner::TraverseStmt
bool TraverseStmt(clang::Stmt *)
Definition: Scanner.h:92
RScanner::VisitRecordDecl
bool VisitRecordDecl(clang::RecordDecl *D)
Definition: Scanner.cxx:521
RScanner::fgBadClassCounter
static int fgBadClassCounter
Definition: Scanner.h:195
RScanner::fDeclSelRuleMap
DeclsSelRulesMap_t fDeclSelRuleMap
Definition: Scanner.h:201
RScanner::AddAnnotatedRecordDecl
int AddAnnotatedRecordDecl(const ClassSelectionRule *, const clang::Type *, const clang::RecordDecl *, const std::string &, const clang::TypedefNameDecl *, unsigned int indexOffset=0)
Definition: Scanner.cxx:534
RScanner::fRecordDeclCallback
DeclCallback fRecordDeclCallback
Definition: Scanner.h:191
BaseSelectionRule
Definition: BaseSelectionRule.h:38
RScanner::fSelectedTypedefs
TypedefColl_t fSelectedTypedefs
Definition: Scanner.h:130
RScanner::fInterpreter
const cling::Interpreter & fInterpreter
Definition: Scanner.h:186
RScanner::GetFunctionPrototype
bool GetFunctionPrototype(clang::Decl *D, std::string &prototype) const
Definition: Scanner.cxx:1009
RScanner::AnnotatedNamespaceDecl
Definition: Scanner.h:56
RScanner::VisitFunctionDecl
bool VisitFunctionDecl(clang::FunctionDecl *D)
Definition: Scanner.cxx:927
RScanner::UnsupportedType
void UnsupportedType(clang::QualType qual_type) const
Definition: Scanner.cxx:368
RScanner::fgAnonymousEnumMap
static std::map< clang::Decl *, std::string > fgAnonymousEnumMap
Definition: Scanner.h:176
SelectionRules
Definition: SelectionRules.h:92
RScanner::VisitEnumDecl
bool VisitEnumDecl(clang::EnumDecl *D)
Definition: Scanner.cxx:868