aboutsummaryrefslogtreecommitdiff
path: root/lib/Frontend/StmtXML.cpp
blob: 257c1ccb005b97de15f17dd940d6a440c45293e4 (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
//===--- StmtXML.cpp - XML implementation for Stmt ASTs ------------------===//
//
//                     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 Stmt::dumpXML methods, which dump out the
// AST to an XML document.
//
//===----------------------------------------------------------------------===//

#include "clang/Frontend/DocumentXML.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DeclCXX.h"
#include "clang/Basic/SourceManager.h"
using namespace clang;

//===----------------------------------------------------------------------===//
// StmtXML Visitor
//===----------------------------------------------------------------------===//

namespace  {
  class StmtXML : public StmtVisitor<StmtXML> {
    DocumentXML&  Doc;

    //static const char *getOpcodeStr(UnaryOperator::Opcode Op);
    //static const char *getOpcodeStr(BinaryOperator::Opcode Op);


  void addSpecialAttribute(const char* pName, StringLiteral* Str) {
    Doc.addAttribute(pName, Doc.escapeString(Str->getString().data(),
                                             Str->getString().size()));
  }

  void addSpecialAttribute(const char* pName, SizeOfAlignOfExpr* S) {
    if (S->isArgumentType())
      Doc.addAttribute(pName, S->getArgumentType());
  }

  void addSpecialAttribute(const char* pName, CXXTypeidExpr* S) {
    if (S->isTypeOperand())
      Doc.addAttribute(pName, S->getTypeOperand());
  }


  public:
    StmtXML(DocumentXML& doc)
      : Doc(doc) {
    }

    void DumpSubTree(Stmt *S) {
      if (S) {
        Visit(S);
        if (DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
          for (DeclStmt::decl_iterator DI = DS->decl_begin(),
                 DE = DS->decl_end(); DI != DE; ++DI) {
            Doc.PrintDecl(*DI);
          }
        } else {
          for (Stmt::child_range i = S->children(); i; ++i)
            DumpSubTree(*i);
        }
        Doc.toParent();
      } else {
        Doc.addSubNode("NULL").toParent();
      }
    }


#define NODE_XML( CLASS, NAME )          \
  void Visit##CLASS(CLASS* S)            \
  {                                      \
    typedef CLASS tStmtType;             \
    Doc.addSubNode(NAME);

#define ATTRIBUTE_XML( FN, NAME )         Doc.addAttribute(NAME, S->FN);
#define TYPE_ATTRIBUTE_XML( FN )          ATTRIBUTE_XML(FN, "type")
#define ATTRIBUTE_OPT_XML( FN, NAME )     Doc.addAttributeOptional(NAME, S->FN);
#define ATTRIBUTE_SPECIAL_XML( FN, NAME ) addSpecialAttribute(NAME, S);
#define ATTRIBUTE_FILE_LOCATION_XML       Doc.addLocationRange(S->getSourceRange());


#define ATTRIBUTE_ENUM_XML( FN, NAME )  \
  {                                     \
    const char* pAttributeName = NAME;  \
    const bool optional = false;        \
    switch (S->FN) {                    \
    default: assert(0 && "unknown enum value");

#define ATTRIBUTE_ENUM_OPT_XML( FN, NAME )  \
  {                                         \
    const char* pAttributeName = NAME;      \
    const bool optional = true;             \
    switch (S->FN) {                        \
    default: assert(0 && "unknown enum value");

#define ENUM_XML( VALUE, NAME )         case VALUE: if ((!optional) || NAME[0]) Doc.addAttribute(pAttributeName, NAME); break;
#define END_ENUM_XML                    } }
#define END_NODE_XML                    }

#define ID_ATTRIBUTE_XML                Doc.addAttribute("id", S);
#define SUB_NODE_XML( CLASS )
#define SUB_NODE_SEQUENCE_XML( CLASS )
#define SUB_NODE_OPT_XML( CLASS )

#include "clang/Frontend/StmtXML.def"
  };
}

//===----------------------------------------------------------------------===//
// Stmt method implementations
//===----------------------------------------------------------------------===//

/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
void DocumentXML::PrintStmt(const Stmt *S) {
  StmtXML P(*this);
  P.DumpSubTree(const_cast<Stmt*>(S));
}