summaryrefslogtreecommitdiffstats
path: root/include/clang/Frontend/DocumentXML.h
blob: 99db717190dd65404f8e79bc3ee780a8d443d678 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//===--- DocumentXML.h - XML document for ASTs ------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the XML document class, which provides the means to 
// dump out the AST in a XML form that exposes type details and other fields.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_FRONTEND_DOCUMENTXML_H
#define LLVM_CLANG_FRONTEND_DOCUMENTXML_H

#include <string>
#include <map>
#include "clang/AST/Type.h"
#include "clang/AST/TypeOrdering.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/DenseMap.h"

namespace clang {

//--------------------------------------------------------- forwards
class DeclContext;
class Decl;
class NamedDecl;
class FunctionDecl;
class ASTContext;
 
//--------------------------------------------------------- 
namespace XML
{
  // id maps:
  template<class T>
  struct IdMap : llvm::DenseMap<T, unsigned> {};

  template<>
  struct IdMap<QualType> : std::map<QualType, unsigned, QualTypeOrdering> {};

  template<>
  struct IdMap<std::string> : std::map<std::string, unsigned> {};
}

//--------------------------------------------------------- 
class DocumentXML
{
public:
  DocumentXML(const std::string& rootName, llvm::raw_ostream& out);
  ~DocumentXML();

  void initialize(ASTContext &Context);
  void PrintDecl(Decl *D);
  void PrintStmt(const Stmt *S);    // defined in StmtXML.cpp

  void finalize();


  DocumentXML& addSubNode(const std::string& name);   // also enters the sub node, returns *this
  DocumentXML& toParent();                            // returns *this

  template<class T>
  void addAttribute(const char* pName, const T& value);
  
  void addTypeAttribute(const QualType& pType);  
  void addRefAttribute(const NamedDecl* D);

  enum tContextUsage { CONTEXT_AS_CONTEXT, CONTEXT_AS_ID };
  void addContextAttribute(const DeclContext *DC, tContextUsage usage = CONTEXT_AS_CONTEXT);
  void addSourceFileAttribute(const std::string& fileName);

  PresumedLoc addLocation(const SourceLocation& Loc);
  void addLocationRange(const SourceRange& R);

  static std::string escapeString(const char* pStr, std::string::size_type len);

private:
  DocumentXML(const DocumentXML&);              // not defined
  DocumentXML& operator=(const DocumentXML&);   // not defined

  struct NodeXML;

  NodeXML*  Root;
  NodeXML*  CurrentNode;   // always after Root
  llvm::raw_ostream& Out;
  ASTContext *Ctx;
  int       CurrentIndent;
  bool      HasCurrentNodeSubNodes;


  XML::IdMap<QualType>                 Types;
  XML::IdMap<const DeclContext*>       Contexts;
  XML::IdMap<const Type*>              BasicTypes;
  XML::IdMap<std::string>              SourceFiles;
  XML::IdMap<const NamedDecl*>         Decls;

  void addContextsRecursively(const DeclContext *DC);
  void addBasicTypeRecursively(const Type* pType);
  void addTypeRecursively(const QualType& pType);

  void PrintFunctionDecl(FunctionDecl *FD);
  void addDeclIdAttribute(const NamedDecl* D);
  void addTypeIdAttribute(const Type* pType);  
  void Indent();
};

//--------------------------------------------------------- inlines

inline void DocumentXML::initialize(ASTContext &Context) 
{ 
  Ctx = &Context; 
}

//--------------------------------------------------------- 
template<class T>
inline void DocumentXML::addAttribute(const char* pName, const T& value)
{
  Out << ' ' << pName << "=\"" << value << "\"";
}

//--------------------------------------------------------- 

} //namespace clang 

#endif //LLVM_CLANG_DOCUMENTXML_H
OpenPOWER on IntegriCloud