blob: 75c15831737f789327a1d012231415b8427ab949 [file] [log] [blame]
Douglas Gregoree75c052009-05-21 20:55:50 +00001//===--- DocumentXML.cpp - XML document for ASTs --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mike Stump1eb44332009-09-09 15:08:12 +000010// This file implements the XML document class, which provides the means to
Douglas Gregoree75c052009-05-21 20:55:50 +000011// dump out the AST in a XML form that exposes type details and other fields.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Frontend/DocumentXML.h"
16#include "clang/AST/Decl.h"
Douglas Gregor038f75a2009-06-15 19:02:54 +000017#include "clang/AST/ASTContext.h"
Douglas Gregoree75c052009-05-21 20:55:50 +000018#include "clang/Basic/SourceManager.h"
19#include "llvm/ADT/StringExtras.h"
Douglas Gregor4980afd2010-11-09 04:06:42 +000020#include <cstdio>
Douglas Gregoree75c052009-05-21 20:55:50 +000021
22namespace clang {
23
Mike Stump1eb44332009-09-09 15:08:12 +000024//---------------------------------------------------------
Douglas Gregoree75c052009-05-21 20:55:50 +000025DocumentXML::DocumentXML(const std::string& rootName, llvm::raw_ostream& out) :
Douglas Gregoree75c052009-05-21 20:55:50 +000026 Out(out),
27 Ctx(0),
Mike Stump1eb44332009-09-09 15:08:12 +000028 HasCurrentNodeSubNodes(false) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000029 NodeStack.push(rootName);
Douglas Gregoree75c052009-05-21 20:55:50 +000030 Out << "<?xml version=\"1.0\"?>\n<" << rootName;
31}
32
Mike Stump1eb44332009-09-09 15:08:12 +000033//---------------------------------------------------------
34DocumentXML& DocumentXML::addSubNode(const std::string& name) {
Douglas Gregoree75c052009-05-21 20:55:50 +000035 if (!HasCurrentNodeSubNodes)
Douglas Gregoree75c052009-05-21 20:55:50 +000036 Out << ">\n";
Douglas Gregor038f75a2009-06-15 19:02:54 +000037 NodeStack.push(name);
Douglas Gregoree75c052009-05-21 20:55:50 +000038 HasCurrentNodeSubNodes = false;
Douglas Gregoree75c052009-05-21 20:55:50 +000039 Indent();
Douglas Gregor038f75a2009-06-15 19:02:54 +000040 Out << "<" << NodeStack.top();
Douglas Gregoree75c052009-05-21 20:55:50 +000041 return *this;
42}
43
Mike Stump1eb44332009-09-09 15:08:12 +000044//---------------------------------------------------------
45void DocumentXML::Indent() {
Douglas Gregor038f75a2009-06-15 19:02:54 +000046 for (size_t i = 0, e = (NodeStack.size() - 1) * 2; i < e; ++i)
Douglas Gregoree75c052009-05-21 20:55:50 +000047 Out << ' ';
48}
49
Mike Stump1eb44332009-09-09 15:08:12 +000050//---------------------------------------------------------
51DocumentXML& DocumentXML::toParent() {
Mike Stump197c8d92009-09-16 20:41:09 +000052 assert(NodeStack.size() > 1 && "too much backtracking");
Douglas Gregoree75c052009-05-21 20:55:50 +000053
Mike Stump1eb44332009-09-09 15:08:12 +000054 if (HasCurrentNodeSubNodes) {
Douglas Gregoree75c052009-05-21 20:55:50 +000055 Indent();
Douglas Gregor038f75a2009-06-15 19:02:54 +000056 Out << "</" << NodeStack.top() << ">\n";
Mike Stump1eb44332009-09-09 15:08:12 +000057 } else
Douglas Gregoree75c052009-05-21 20:55:50 +000058 Out << "/>\n";
Douglas Gregor038f75a2009-06-15 19:02:54 +000059 NodeStack.pop();
Douglas Gregoree75c052009-05-21 20:55:50 +000060 HasCurrentNodeSubNodes = true;
Mike Stump1eb44332009-09-09 15:08:12 +000061 return *this;
Douglas Gregoree75c052009-05-21 20:55:50 +000062}
63
Mike Stump1eb44332009-09-09 15:08:12 +000064//---------------------------------------------------------
Douglas Gregoree75c052009-05-21 20:55:50 +000065namespace {
66
Douglas Gregor038f75a2009-06-15 19:02:54 +000067enum tIdType { ID_NORMAL, ID_FILE, ID_LABEL, ID_LAST };
Douglas Gregoree75c052009-05-21 20:55:50 +000068
Mike Stump1eb44332009-09-09 15:08:12 +000069unsigned getNewId(tIdType idType) {
Douglas Gregoree75c052009-05-21 20:55:50 +000070 static unsigned int idCounts[ID_LAST] = { 0 };
71 return ++idCounts[idType];
72}
73
Mike Stump1eb44332009-09-09 15:08:12 +000074//---------------------------------------------------------
75inline std::string getPrefixedId(unsigned uId, tIdType idType) {
Douglas Gregor038f75a2009-06-15 19:02:54 +000076 static const char idPrefix[ID_LAST] = { '_', 'f', 'l' };
Douglas Gregoree75c052009-05-21 20:55:50 +000077 char buffer[20];
78 char* BufPtr = llvm::utohex_buffer(uId, buffer + 20);
79 *--BufPtr = idPrefix[idType];
80 return BufPtr;
81}
82
Mike Stump1eb44332009-09-09 15:08:12 +000083//---------------------------------------------------------
Douglas Gregoree75c052009-05-21 20:55:50 +000084template<class T, class V>
Mike Stump1eb44332009-09-09 15:08:12 +000085bool addToMap(T& idMap, const V& value, tIdType idType = ID_NORMAL) {
Douglas Gregoree75c052009-05-21 20:55:50 +000086 typename T::iterator i = idMap.find(value);
87 bool toAdd = i == idMap.end();
Mike Stump1eb44332009-09-09 15:08:12 +000088 if (toAdd)
Douglas Gregoree75c052009-05-21 20:55:50 +000089 idMap.insert(typename T::value_type(value, getNewId(idType)));
Douglas Gregoree75c052009-05-21 20:55:50 +000090 return toAdd;
91}
92
93} // anon NS
94
Douglas Gregor038f75a2009-06-15 19:02:54 +000095
Mike Stump1eb44332009-09-09 15:08:12 +000096//---------------------------------------------------------
97std::string DocumentXML::escapeString(const char* pStr,
98 std::string::size_type len) {
Douglas Gregoree75c052009-05-21 20:55:50 +000099 std::string value;
100 value.reserve(len + 1);
101 char buffer[16];
102 for (unsigned i = 0; i < len; ++i) {
103 switch (char C = pStr[i]) {
104 default:
105 if (isprint(C))
106 value += C;
Mike Stump1eb44332009-09-09 15:08:12 +0000107 else {
Douglas Gregor5e0fb352010-11-09 03:20:07 +0000108 snprintf(buffer, sizeof(buffer), "\\%03o", C);
Douglas Gregoree75c052009-05-21 20:55:50 +0000109 value += buffer;
110 }
111 break;
112
113 case '\n': value += "\\n"; break;
114 case '\t': value += "\\t"; break;
115 case '\a': value += "\\a"; break;
116 case '\b': value += "\\b"; break;
117 case '\r': value += "\\r"; break;
118
119 case '&': value += "&amp;"; break;
120 case '<': value += "&lt;"; break;
121 case '>': value += "&gt;"; break;
122 case '"': value += "&quot;"; break;
123 case '\'': value += "&apos;"; break;
124
125 }
126 }
127 return value;
128}
129
Mike Stump1eb44332009-09-09 15:08:12 +0000130//---------------------------------------------------------
131void DocumentXML::finalize() {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000132 assert(NodeStack.size() == 1 && "not completely backtracked");
Douglas Gregoree75c052009-05-21 20:55:50 +0000133
134 addSubNode("ReferenceSection");
135 addSubNode("Types");
136
Mike Stump1eb44332009-09-09 15:08:12 +0000137 for (XML::IdMap<QualType>::iterator i = Types.begin(), e = Types.end();
138 i != e; ++i) {
Douglas Gregora4923eb2009-11-16 21:35:15 +0000139 if (i->first.hasLocalQualifiers()) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000140 writeTypeToXML(i->first);
Douglas Gregoree75c052009-05-21 20:55:50 +0000141 addAttribute("id", getPrefixedId(i->second, ID_NORMAL));
Douglas Gregoree75c052009-05-21 20:55:50 +0000142 toParent();
143 }
144 }
145
Mike Stump1eb44332009-09-09 15:08:12 +0000146 for (XML::IdMap<const Type*>::iterator i = BasicTypes.begin(),
147 e = BasicTypes.end(); i != e; ++i) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000148 writeTypeToXML(i->first);
Douglas Gregoree75c052009-05-21 20:55:50 +0000149 addAttribute("id", getPrefixedId(i->second, ID_NORMAL));
150 toParent();
151 }
152
153
154 toParent().addSubNode("Contexts");
155
Mike Stump1eb44332009-09-09 15:08:12 +0000156 for (XML::IdMap<const DeclContext*>::iterator i = Contexts.begin(),
157 e = Contexts.end(); i != e; ++i) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000158 addSubNode(i->first->getDeclKindName());
159 addAttribute("id", getPrefixedId(i->second, ID_NORMAL));
Mike Stump1eb44332009-09-09 15:08:12 +0000160 if (const NamedDecl *ND = dyn_cast<NamedDecl>(i->first))
Douglas Gregoree75c052009-05-21 20:55:50 +0000161 addAttribute("name", ND->getNameAsString());
Mike Stump1eb44332009-09-09 15:08:12 +0000162 if (const TagDecl *TD = dyn_cast<TagDecl>(i->first))
Douglas Gregoree75c052009-05-21 20:55:50 +0000163 addAttribute("type", getPrefixedId(BasicTypes[TD->getTypeForDecl()], ID_NORMAL));
Mike Stump1eb44332009-09-09 15:08:12 +0000164 else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(i->first))
John McCall183700f2009-09-21 23:43:11 +0000165 addAttribute("type", getPrefixedId(BasicTypes[FD->getType()->getAs<FunctionType>()], ID_NORMAL));
Douglas Gregoree75c052009-05-21 20:55:50 +0000166
167 if (const DeclContext* parent = i->first->getParent())
Douglas Gregor038f75a2009-06-15 19:02:54 +0000168 addAttribute("context", parent);
Douglas Gregoree75c052009-05-21 20:55:50 +0000169 toParent();
170 }
171
172 toParent().addSubNode("Files");
173
Mike Stump1eb44332009-09-09 15:08:12 +0000174 for (XML::IdMap<std::string>::iterator i = SourceFiles.begin(),
175 e = SourceFiles.end(); i != e; ++i) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000176 addSubNode("File");
177 addAttribute("id", getPrefixedId(i->second, ID_FILE));
178 addAttribute("name", escapeString(i->first.c_str(), i->first.size()));
179 toParent();
180 }
181
182 toParent().toParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Douglas Gregoree75c052009-05-21 20:55:50 +0000184 // write the root closing node (which has always subnodes)
Douglas Gregor038f75a2009-06-15 19:02:54 +0000185 Out << "</" << NodeStack.top() << ">\n";
Douglas Gregoree75c052009-05-21 20:55:50 +0000186}
187
Mike Stump1eb44332009-09-09 15:08:12 +0000188//---------------------------------------------------------
189void DocumentXML::addAttribute(const char* pAttributeName,
190 const QualType& pType) {
Douglas Gregoree75c052009-05-21 20:55:50 +0000191 addTypeRecursively(pType);
Douglas Gregor038f75a2009-06-15 19:02:54 +0000192 addAttribute(pAttributeName, getPrefixedId(Types[pType], ID_NORMAL));
Douglas Gregoree75c052009-05-21 20:55:50 +0000193}
194
Mike Stump1eb44332009-09-09 15:08:12 +0000195//---------------------------------------------------------
196void DocumentXML::addPtrAttribute(const char* pAttributeName,
197 const Type* pType) {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000198 addTypeRecursively(pType);
199 addAttribute(pAttributeName, getPrefixedId(BasicTypes[pType], ID_NORMAL));
Douglas Gregoree75c052009-05-21 20:55:50 +0000200}
201
Mike Stump1eb44332009-09-09 15:08:12 +0000202//---------------------------------------------------------
Douglas Gregor4bd98e82010-05-10 17:43:18 +0000203void DocumentXML::addPtrAttribute(const char* pAttributeName,
204 const NestedNameSpecifier* pNNS) {
205 switch (pNNS->getKind()) {
206 case NestedNameSpecifier::Identifier: {
207 IdentifierInfo *ii = pNNS->getAsIdentifier();
208 // FIXME how should we handle those ?
209 addPtrAttribute(pAttributeName, ii->getName().data());
210 break;
211 }
212 case NestedNameSpecifier::Namespace: {
213 addPtrAttribute(pAttributeName, pNNS->getAsNamespace());
214 break;
215 }
216 case NestedNameSpecifier::TypeSpec: {
217 addPtrAttribute(pAttributeName, pNNS->getAsType());
218 break;
219 }
220 case NestedNameSpecifier::TypeSpecWithTemplate: {
221 addPtrAttribute(pAttributeName, pNNS->getAsType());
222 break;
223 }
224 case NestedNameSpecifier::Global: {
225 addPtrAttribute(pAttributeName, "::");
226 break;
227 }
228 }
229}
230
231//---------------------------------------------------------
Douglas Gregoree75c052009-05-21 20:55:50 +0000232void DocumentXML::addTypeRecursively(const QualType& pType)
233{
234 if (addToMap(Types, pType))
235 {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000236 addTypeRecursively(pType.getTypePtr());
Douglas Gregoree75c052009-05-21 20:55:50 +0000237 // beautifier: a non-qualified type shall be transparent
Douglas Gregora4923eb2009-11-16 21:35:15 +0000238 if (!pType.hasLocalQualifiers())
Douglas Gregoree75c052009-05-21 20:55:50 +0000239 {
Mike Stump1eb44332009-09-09 15:08:12 +0000240 Types[pType] = BasicTypes[pType.getTypePtr()];
Douglas Gregoree75c052009-05-21 20:55:50 +0000241 }
242 }
243}
244
Mike Stump1eb44332009-09-09 15:08:12 +0000245//---------------------------------------------------------
Douglas Gregor038f75a2009-06-15 19:02:54 +0000246void DocumentXML::addTypeRecursively(const Type* pType)
Douglas Gregoree75c052009-05-21 20:55:50 +0000247{
248 if (addToMap(BasicTypes, pType))
249 {
Douglas Gregor038f75a2009-06-15 19:02:54 +0000250 addParentTypes(pType);
251/*
252 // FIXME: doesn't work in the immediate streaming approach
Mike Stump1eb44332009-09-09 15:08:12 +0000253 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(pType))
Douglas Gregor038f75a2009-06-15 19:02:54 +0000254 {
255 addSubNode("VariableArraySizeExpression");
256 PrintStmt(VAT->getSizeExpr());
257 toParent();
Douglas Gregoree75c052009-05-21 20:55:50 +0000258 }
Douglas Gregor038f75a2009-06-15 19:02:54 +0000259*/
Douglas Gregoree75c052009-05-21 20:55:50 +0000260 }
261}
262
Mike Stump1eb44332009-09-09 15:08:12 +0000263//---------------------------------------------------------
Douglas Gregor038f75a2009-06-15 19:02:54 +0000264void DocumentXML::addPtrAttribute(const char* pName, const DeclContext* DC)
Douglas Gregoree75c052009-05-21 20:55:50 +0000265{
266 addContextsRecursively(DC);
Douglas Gregor038f75a2009-06-15 19:02:54 +0000267 addAttribute(pName, getPrefixedId(Contexts[DC], ID_NORMAL));
268}
269
Mike Stump1eb44332009-09-09 15:08:12 +0000270//---------------------------------------------------------
Douglas Gregor038f75a2009-06-15 19:02:54 +0000271void DocumentXML::addPtrAttribute(const char* pAttributeName, const NamedDecl* D)
272{
273 if (const DeclContext* DC = dyn_cast<DeclContext>(D))
274 {
275 addContextsRecursively(DC);
276 addAttribute(pAttributeName, getPrefixedId(Contexts[DC], ID_NORMAL));
277 }
278 else
279 {
280 addToMap(Decls, D);
281 addAttribute(pAttributeName, getPrefixedId(Decls[D], ID_NORMAL));
282 }
283}
284
Mike Stump1eb44332009-09-09 15:08:12 +0000285//---------------------------------------------------------
Douglas Gregor038f75a2009-06-15 19:02:54 +0000286void DocumentXML::addPtrAttribute(const char* pName, const NamespaceDecl* D)
287{
288 addPtrAttribute(pName, static_cast<const DeclContext*>(D));
Douglas Gregoree75c052009-05-21 20:55:50 +0000289}
290
Mike Stump1eb44332009-09-09 15:08:12 +0000291//---------------------------------------------------------
Douglas Gregoree75c052009-05-21 20:55:50 +0000292void DocumentXML::addContextsRecursively(const DeclContext *DC)
293{
294 if (DC != 0 && addToMap(Contexts, DC))
295 {
296 addContextsRecursively(DC->getParent());
Mike Stump1eb44332009-09-09 15:08:12 +0000297 }
Douglas Gregoree75c052009-05-21 20:55:50 +0000298}
299
Mike Stump1eb44332009-09-09 15:08:12 +0000300//---------------------------------------------------------
Douglas Gregoree75c052009-05-21 20:55:50 +0000301void DocumentXML::addSourceFileAttribute(const std::string& fileName)
302{
303 addToMap(SourceFiles, fileName, ID_FILE);
304 addAttribute("file", getPrefixedId(SourceFiles[fileName], ID_FILE));
305}
306
Douglas Gregor038f75a2009-06-15 19:02:54 +0000307
Mike Stump1eb44332009-09-09 15:08:12 +0000308//---------------------------------------------------------
Douglas Gregor038f75a2009-06-15 19:02:54 +0000309void DocumentXML::addPtrAttribute(const char* pName, const LabelStmt* L)
310{
311 addToMap(Labels, L, ID_LABEL);
312 addAttribute(pName, getPrefixedId(Labels[L], ID_LABEL));
313}
314
315
Mike Stump1eb44332009-09-09 15:08:12 +0000316//---------------------------------------------------------
Douglas Gregoree75c052009-05-21 20:55:50 +0000317PresumedLoc DocumentXML::addLocation(const SourceLocation& Loc)
318{
319 SourceManager& SM = Ctx->getSourceManager();
320 SourceLocation SpellingLoc = SM.getSpellingLoc(Loc);
321 PresumedLoc PLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000322 if (!SpellingLoc.isInvalid())
Douglas Gregoree75c052009-05-21 20:55:50 +0000323 {
324 PLoc = SM.getPresumedLoc(SpellingLoc);
325 addSourceFileAttribute(PLoc.getFilename());
326 addAttribute("line", PLoc.getLine());
327 addAttribute("col", PLoc.getColumn());
328 }
329 // else there is no error in some cases (eg. CXXThisExpr)
330 return PLoc;
331}
332
Mike Stump1eb44332009-09-09 15:08:12 +0000333//---------------------------------------------------------
Douglas Gregoree75c052009-05-21 20:55:50 +0000334void DocumentXML::addLocationRange(const SourceRange& R)
335{
336 PresumedLoc PStartLoc = addLocation(R.getBegin());
Mike Stump1eb44332009-09-09 15:08:12 +0000337 if (R.getBegin() != R.getEnd())
Douglas Gregoree75c052009-05-21 20:55:50 +0000338 {
339 SourceManager& SM = Ctx->getSourceManager();
340 SourceLocation SpellingLoc = SM.getSpellingLoc(R.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000341 if (!SpellingLoc.isInvalid())
Douglas Gregoree75c052009-05-21 20:55:50 +0000342 {
343 PresumedLoc PLoc = SM.getPresumedLoc(SpellingLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000344 if (PStartLoc.isInvalid() ||
Douglas Gregoree75c052009-05-21 20:55:50 +0000345 strcmp(PLoc.getFilename(), PStartLoc.getFilename()) != 0) {
346 addToMap(SourceFiles, PLoc.getFilename(), ID_FILE);
347 addAttribute("endfile", PLoc.getFilename());
348 addAttribute("endline", PLoc.getLine());
349 addAttribute("endcol", PLoc.getColumn());
350 } else if (PLoc.getLine() != PStartLoc.getLine()) {
351 addAttribute("endline", PLoc.getLine());
352 addAttribute("endcol", PLoc.getColumn());
353 } else {
354 addAttribute("endcol", PLoc.getColumn());
Mike Stump1eb44332009-09-09 15:08:12 +0000355 }
Douglas Gregoree75c052009-05-21 20:55:50 +0000356 }
357 }
358}
359
Mike Stump1eb44332009-09-09 15:08:12 +0000360//---------------------------------------------------------
Douglas Gregoree75c052009-05-21 20:55:50 +0000361void DocumentXML::PrintDecl(Decl *D)
362{
Douglas Gregor038f75a2009-06-15 19:02:54 +0000363 writeDeclToXML(D);
Douglas Gregoree75c052009-05-21 20:55:50 +0000364}
365
Mike Stump1eb44332009-09-09 15:08:12 +0000366//---------------------------------------------------------
Douglas Gregoree75c052009-05-21 20:55:50 +0000367} // NS clang
368