HPCToolkit
PGMDocHandler.hpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // XML adaptor for the program structure file (PGM)
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 #ifndef profxml_PGMDocHandler
61 #define profxml_PGMDocHandler
62 
63 //************************ System Include Files ******************************
64 
65 #include <string>
66 #include <map>
67 
68 //************************* User Include Files *******************************
69 
70 #include "XercesSAX2.hpp"
71 #include "DocHandlerArgs.hpp"
72 
73 #include <lib/prof/Struct-Tree.hpp>
74 
76 
77 //************************ Forward Declarations ******************************
78 
79 //****************************************************************************
80 
81 class PGMDocHandler : public XERCES_CPP_NAMESPACE::DefaultHandler {
82 public:
84  static const char* ToString(Doc_t docty);
85 
86 private:
87  std::map<std::string, Prof::Struct::Proc*> idToProcMap;
88 
89 public:
90 
91  PGMDocHandler(Doc_t ty, Prof::Struct::Tree* structure,
94 
95  void
96  startElement(const XMLCh* const uri, const XMLCh* const name,
97  const XMLCh* const qname,
98  const XERCES_CPP_NAMESPACE::Attributes& attributes);
99  void
100  endElement(const XMLCh* const uri, const XMLCh* const name,
101  const XMLCh* const qname);
102 
103 
104  void
105  getLineAttr(SrcFile::ln& begLn, SrcFile::ln& endLn,
106  const XERCES_CPP_NAMESPACE::Attributes& attributes);
107 
108  //--------------------------------------
109  // SAX2 error handler interface
110  //--------------------------------------
111  void
112  error(const SAXParseException& e);
113 
114  void
115  fatalError(const SAXParseException& e);
116 
117  void
118  warning(const SAXParseException& e);
119 
120 private:
121 
122  class StackEntry_t {
123  public:
125  Prof::Struct::ANode* shadow_ = NULL)
126  : entry(entry_), shadow(shadow_), isLeaf(true) { }
128 
130  getScope() const
131  { return entry; }
132 
134  GetShadow() const
135  { return shadow; }
136 
137  void
139  { entry = x; }
140 
141  void
143  { shadow = x; }
144 
145  // Is this a leaf node?
146  bool
148  { return isLeaf; }
149 
150  void
151  SetLeaf(bool x)
152  { isLeaf = x; }
153 
154  bool
156  { return (entry->type() == Prof::Struct::ANode::TyGroup); }
157 
158  bool
160  { return (IsLeaf() && !IsGroupScope()); }
161 
162  private:
165  bool isLeaf;
166  };
167 
168 
169  StackEntry_t*
170  getStackEntry(unsigned int idx)
171  { return static_cast<StackEntry_t*>(scopeStack.Get(idx)); }
172 
175  {
176  StackEntry_t* entry = static_cast<StackEntry_t*>(scopeStack.Top());
177  return entry->getScope();
178  }
179 
181  getScope(unsigned int idx)
182  {
183  StackEntry_t* entry = static_cast<StackEntry_t*>(scopeStack.Get(idx));
184  return entry->getScope();
185  }
186 
188  getShadowScope(unsigned int idx)
189  {
190  StackEntry_t* entry = static_cast<StackEntry_t*>(scopeStack.Get(idx));
191  return entry->GetShadow();
192  }
193 
194  void
196  {
197  StackEntry_t* entry = new StackEntry_t(scope);
198  scopeStack.Push(entry);
199  }
200 
201  void
203  {
204  StackEntry_t* entry = static_cast<StackEntry_t*>(scopeStack.Pop());
205  delete entry;
206  }
207 
208  // Find the current file scope (include top of stack)
210  findCurrentFile();
211 
212  // Find the enclosing GroupScope stack depth (excluding the top of
213  // stack) or return 0 if not found.
214  unsigned int
216 
217 
218  void
220 
221 private:
225 
226  // variables for constant values during file processing
227  double m_version; // initialized to a negative
228 
229  // current static context
234 
235  unsigned groupNestingLvl;
236 
237  // stack of StackEntry_t representing current scope context. Top of
238  // stack is most deeply nested scope.
240 
241 private:
242  // Note: these cannot be static since the Xerces must be initialized
243  // first.
244 
245  // element names
246  const XMLCh *const elemStructure;
247  const XMLCh *const elemLM;
248  const XMLCh *const elemVariable;
249  const XMLCh *const elemFile;
250  const XMLCh *const elemProc;
251  const XMLCh *const elemAlien;
252  const XMLCh *const elemLoop;
253  const XMLCh *const elemStmt;
254  const XMLCh *const elemGroup;
255 
256  // attribute names
257  const XMLCh *const attrVer;
258  const XMLCh *const attrId;
259  const XMLCh *const attrName;
260  const XMLCh *const attrFile;
261  const XMLCh *const attrLnName;
262  const XMLCh *const attrLine;
263  const XMLCh *const attrVMA;
264 };
265 
266 
267 //****************************************************************************
268 
269 #define PGM_Throw(streamArgs) DIAG_ThrowX(PGMException, streamArgs)
270 
272 public:
273  PGMException(const std::string x,
274  const char* filenm = NULL, unsigned int lineno = 0)
275  : Diagnostics::Exception(x, filenm, lineno)
276  { }
277 
278  virtual std::string
279  message() const
280  { return "HPCToolkitStructure file error [STRUCTException]: " + what(); }
281 
282 private:
283 };
284 
285 #endif // profxml_PGMDocHandler
286 
void getLineAttr(SrcFile::ln &begLn, SrcFile::ln &endLn, const XERCES_CPP_NAMESPACE::Attributes &attributes)
Prof::Struct::ANode * entry
void fatalError(const SAXParseException &e)
unsigned int ln
Definition: SrcFile.hpp:66
Prof::Struct::Proc * m_curProc
void * Top()
const XMLCh *const elemAlien
static const char * ToString(Doc_t docty)
unsigned groupNestingLvl
PGMException(const std::string x, const char *filenm=NULL, unsigned int lineno=0)
Prof::Struct::ANode * shadow
const XMLCh *const elemFile
Prof::Struct::ANode * getScope(unsigned int idx)
StackEntry_t(Prof::Struct::ANode *entry_=NULL, Prof::Struct::ANode *shadow_=NULL)
void Push(void *item)
void pushCurrentScope(Prof::Struct::ANode *scope)
void startElement(const XMLCh *const uri, const XMLCh *const name, const XMLCh *const qname, const XERCES_CPP_NAMESPACE::Attributes &attributes)
const XMLCh *const attrName
const XMLCh *const elemLoop
void endElement(const XMLCh *const uri, const XMLCh *const name, const XMLCh *const qname)
virtual std::string message() const
Prof::Struct::Root * m_curRoot
Prof::Struct::ANode * GetShadow() const
PGMDocHandler(Doc_t ty, Prof::Struct::Tree *structure, DocHandlerArgs &args)
void popCurrentScope()
void SetShadow(Prof::Struct::ANode *x)
Prof::Struct::Tree * m_structure
const XMLCh *const attrVMA
DocHandlerArgs & m_args
void processGroupDocEndTag()
const XMLCh *const attrFile
const XMLCh *const elemVariable
const XMLCh *const elemProc
Prof::Struct::File * m_curFile
void error(const SAXParseException &e)
const XMLCh *const elemStructure
StackEntry_t * getStackEntry(unsigned int idx)
const XMLCh *const attrId
Prof::Struct::ANode * getScope() const
#define NULL
Definition: ElfHelper.cpp:85
Prof::Struct::ANode * getShadowScope(unsigned int idx)
const XMLCh *const elemStmt
std::map< std::string, Prof::Struct::Proc * > idToProcMap
const XMLCh *const elemGroup
const XMLCh *const elemLM
unsigned int findEnclosingGroupScopeDepth()
const XMLCh *const attrVer
Prof::Struct::ANode * getCurrentScope()
void * Get(unsigned int depth)
void warning(const SAXParseException &e)
void SetScope(Prof::Struct::ANode *x)
Prof::Struct::File * findCurrentFile()
Prof::Struct::LM * m_curLM
ANodeTy type() const
void * Pop()
const XMLCh *const attrLnName
PointerStack scopeStack
const XMLCh *const attrLine