Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 1 | //===--- 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10 | // This file implements the XML document class, which provides the means to |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 11 | // 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 Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 4980afd | 2010-11-09 04:06:42 +0000 | [diff] [blame] | 20 | #include <cstdio> |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 21 | |
| 22 | namespace clang { |
| 23 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 24 | //--------------------------------------------------------- |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 25 | DocumentXML::DocumentXML(const std::string& rootName, llvm::raw_ostream& out) : |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 26 | Out(out), |
| 27 | Ctx(0), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 28 | HasCurrentNodeSubNodes(false) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 29 | NodeStack.push(rootName); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 30 | Out << "<?xml version=\"1.0\"?>\n<" << rootName; |
| 31 | } |
| 32 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | //--------------------------------------------------------- |
| 34 | DocumentXML& DocumentXML::addSubNode(const std::string& name) { |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 35 | if (!HasCurrentNodeSubNodes) |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 36 | Out << ">\n"; |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 37 | NodeStack.push(name); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 38 | HasCurrentNodeSubNodes = false; |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 39 | Indent(); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 40 | Out << "<" << NodeStack.top(); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 41 | return *this; |
| 42 | } |
| 43 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | //--------------------------------------------------------- |
| 45 | void DocumentXML::Indent() { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 46 | for (size_t i = 0, e = (NodeStack.size() - 1) * 2; i < e; ++i) |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 47 | Out << ' '; |
| 48 | } |
| 49 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | //--------------------------------------------------------- |
| 51 | DocumentXML& DocumentXML::toParent() { |
Mike Stump | 197c8d9 | 2009-09-16 20:41:09 +0000 | [diff] [blame] | 52 | assert(NodeStack.size() > 1 && "too much backtracking"); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 53 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | if (HasCurrentNodeSubNodes) { |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 55 | Indent(); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 56 | Out << "</" << NodeStack.top() << ">\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | } else |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 58 | Out << "/>\n"; |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 59 | NodeStack.pop(); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 60 | HasCurrentNodeSubNodes = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | return *this; |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | //--------------------------------------------------------- |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 65 | namespace { |
| 66 | |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 67 | enum tIdType { ID_NORMAL, ID_FILE, ID_LABEL, ID_LAST }; |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 68 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | unsigned getNewId(tIdType idType) { |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 70 | static unsigned int idCounts[ID_LAST] = { 0 }; |
| 71 | return ++idCounts[idType]; |
| 72 | } |
| 73 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | //--------------------------------------------------------- |
| 75 | inline std::string getPrefixedId(unsigned uId, tIdType idType) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 76 | static const char idPrefix[ID_LAST] = { '_', 'f', 'l' }; |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 77 | char buffer[20]; |
| 78 | char* BufPtr = llvm::utohex_buffer(uId, buffer + 20); |
| 79 | *--BufPtr = idPrefix[idType]; |
| 80 | return BufPtr; |
| 81 | } |
| 82 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | //--------------------------------------------------------- |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 84 | template<class T, class V> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | bool addToMap(T& idMap, const V& value, tIdType idType = ID_NORMAL) { |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 86 | typename T::iterator i = idMap.find(value); |
| 87 | bool toAdd = i == idMap.end(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | if (toAdd) |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 89 | idMap.insert(typename T::value_type(value, getNewId(idType))); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 90 | return toAdd; |
| 91 | } |
| 92 | |
| 93 | } // anon NS |
| 94 | |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 95 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | //--------------------------------------------------------- |
| 97 | std::string DocumentXML::escapeString(const char* pStr, |
| 98 | std::string::size_type len) { |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 99 | 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | else { |
Douglas Gregor | b87b29e | 2010-11-09 04:38:09 +0000 | [diff] [blame^] | 108 | #ifdef LLVM_ON_WIN32 |
| 109 | sprintf(buffer, "\\%03o", C); |
| 110 | #else |
Douglas Gregor | 5e0fb35 | 2010-11-09 03:20:07 +0000 | [diff] [blame] | 111 | snprintf(buffer, sizeof(buffer), "\\%03o", C); |
Douglas Gregor | b87b29e | 2010-11-09 04:38:09 +0000 | [diff] [blame^] | 112 | #endif |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 113 | value += buffer; |
| 114 | } |
| 115 | break; |
| 116 | |
| 117 | case '\n': value += "\\n"; break; |
| 118 | case '\t': value += "\\t"; break; |
| 119 | case '\a': value += "\\a"; break; |
| 120 | case '\b': value += "\\b"; break; |
| 121 | case '\r': value += "\\r"; break; |
| 122 | |
| 123 | case '&': value += "&"; break; |
| 124 | case '<': value += "<"; break; |
| 125 | case '>': value += ">"; break; |
| 126 | case '"': value += """; break; |
| 127 | case '\'': value += "'"; break; |
| 128 | |
| 129 | } |
| 130 | } |
| 131 | return value; |
| 132 | } |
| 133 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | //--------------------------------------------------------- |
| 135 | void DocumentXML::finalize() { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 136 | assert(NodeStack.size() == 1 && "not completely backtracked"); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 137 | |
| 138 | addSubNode("ReferenceSection"); |
| 139 | addSubNode("Types"); |
| 140 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | for (XML::IdMap<QualType>::iterator i = Types.begin(), e = Types.end(); |
| 142 | i != e; ++i) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 143 | if (i->first.hasLocalQualifiers()) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 144 | writeTypeToXML(i->first); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 145 | addAttribute("id", getPrefixedId(i->second, ID_NORMAL)); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 146 | toParent(); |
| 147 | } |
| 148 | } |
| 149 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | for (XML::IdMap<const Type*>::iterator i = BasicTypes.begin(), |
| 151 | e = BasicTypes.end(); i != e; ++i) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 152 | writeTypeToXML(i->first); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 153 | addAttribute("id", getPrefixedId(i->second, ID_NORMAL)); |
| 154 | toParent(); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | toParent().addSubNode("Contexts"); |
| 159 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | for (XML::IdMap<const DeclContext*>::iterator i = Contexts.begin(), |
| 161 | e = Contexts.end(); i != e; ++i) { |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 162 | addSubNode(i->first->getDeclKindName()); |
| 163 | addAttribute("id", getPrefixedId(i->second, ID_NORMAL)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(i->first)) |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 165 | addAttribute("name", ND->getNameAsString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | if (const TagDecl *TD = dyn_cast<TagDecl>(i->first)) |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 167 | addAttribute("type", getPrefixedId(BasicTypes[TD->getTypeForDecl()], ID_NORMAL)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(i->first)) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 169 | addAttribute("type", getPrefixedId(BasicTypes[FD->getType()->getAs<FunctionType>()], ID_NORMAL)); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 170 | |
| 171 | if (const DeclContext* parent = i->first->getParent()) |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 172 | addAttribute("context", parent); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 173 | toParent(); |
| 174 | } |
| 175 | |
| 176 | toParent().addSubNode("Files"); |
| 177 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | for (XML::IdMap<std::string>::iterator i = SourceFiles.begin(), |
| 179 | e = SourceFiles.end(); i != e; ++i) { |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 180 | addSubNode("File"); |
| 181 | addAttribute("id", getPrefixedId(i->second, ID_FILE)); |
| 182 | addAttribute("name", escapeString(i->first.c_str(), i->first.size())); |
| 183 | toParent(); |
| 184 | } |
| 185 | |
| 186 | toParent().toParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 188 | // write the root closing node (which has always subnodes) |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 189 | Out << "</" << NodeStack.top() << ">\n"; |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | //--------------------------------------------------------- |
| 193 | void DocumentXML::addAttribute(const char* pAttributeName, |
| 194 | const QualType& pType) { |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 195 | addTypeRecursively(pType); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 196 | addAttribute(pAttributeName, getPrefixedId(Types[pType], ID_NORMAL)); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | //--------------------------------------------------------- |
| 200 | void DocumentXML::addPtrAttribute(const char* pAttributeName, |
| 201 | const Type* pType) { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 202 | addTypeRecursively(pType); |
| 203 | addAttribute(pAttributeName, getPrefixedId(BasicTypes[pType], ID_NORMAL)); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | //--------------------------------------------------------- |
Douglas Gregor | 4bd98e8 | 2010-05-10 17:43:18 +0000 | [diff] [blame] | 207 | void DocumentXML::addPtrAttribute(const char* pAttributeName, |
| 208 | const NestedNameSpecifier* pNNS) { |
| 209 | switch (pNNS->getKind()) { |
| 210 | case NestedNameSpecifier::Identifier: { |
| 211 | IdentifierInfo *ii = pNNS->getAsIdentifier(); |
| 212 | // FIXME how should we handle those ? |
| 213 | addPtrAttribute(pAttributeName, ii->getName().data()); |
| 214 | break; |
| 215 | } |
| 216 | case NestedNameSpecifier::Namespace: { |
| 217 | addPtrAttribute(pAttributeName, pNNS->getAsNamespace()); |
| 218 | break; |
| 219 | } |
| 220 | case NestedNameSpecifier::TypeSpec: { |
| 221 | addPtrAttribute(pAttributeName, pNNS->getAsType()); |
| 222 | break; |
| 223 | } |
| 224 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 225 | addPtrAttribute(pAttributeName, pNNS->getAsType()); |
| 226 | break; |
| 227 | } |
| 228 | case NestedNameSpecifier::Global: { |
| 229 | addPtrAttribute(pAttributeName, "::"); |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | //--------------------------------------------------------- |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 236 | void DocumentXML::addTypeRecursively(const QualType& pType) |
| 237 | { |
| 238 | if (addToMap(Types, pType)) |
| 239 | { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 240 | addTypeRecursively(pType.getTypePtr()); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 241 | // beautifier: a non-qualified type shall be transparent |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 242 | if (!pType.hasLocalQualifiers()) |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 243 | { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | Types[pType] = BasicTypes[pType.getTypePtr()]; |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | //--------------------------------------------------------- |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 250 | void DocumentXML::addTypeRecursively(const Type* pType) |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 251 | { |
| 252 | if (addToMap(BasicTypes, pType)) |
| 253 | { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 254 | addParentTypes(pType); |
| 255 | /* |
| 256 | // FIXME: doesn't work in the immediate streaming approach |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(pType)) |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 258 | { |
| 259 | addSubNode("VariableArraySizeExpression"); |
| 260 | PrintStmt(VAT->getSizeExpr()); |
| 261 | toParent(); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 262 | } |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 263 | */ |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | //--------------------------------------------------------- |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 268 | void DocumentXML::addPtrAttribute(const char* pName, const DeclContext* DC) |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 269 | { |
| 270 | addContextsRecursively(DC); |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 271 | addAttribute(pName, getPrefixedId(Contexts[DC], ID_NORMAL)); |
| 272 | } |
| 273 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | //--------------------------------------------------------- |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 275 | void DocumentXML::addPtrAttribute(const char* pAttributeName, const NamedDecl* D) |
| 276 | { |
| 277 | if (const DeclContext* DC = dyn_cast<DeclContext>(D)) |
| 278 | { |
| 279 | addContextsRecursively(DC); |
| 280 | addAttribute(pAttributeName, getPrefixedId(Contexts[DC], ID_NORMAL)); |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | addToMap(Decls, D); |
| 285 | addAttribute(pAttributeName, getPrefixedId(Decls[D], ID_NORMAL)); |
| 286 | } |
| 287 | } |
| 288 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | //--------------------------------------------------------- |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 290 | void DocumentXML::addPtrAttribute(const char* pName, const NamespaceDecl* D) |
| 291 | { |
| 292 | addPtrAttribute(pName, static_cast<const DeclContext*>(D)); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | //--------------------------------------------------------- |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 296 | void DocumentXML::addContextsRecursively(const DeclContext *DC) |
| 297 | { |
| 298 | if (DC != 0 && addToMap(Contexts, DC)) |
| 299 | { |
| 300 | addContextsRecursively(DC->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | } |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | //--------------------------------------------------------- |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 305 | void DocumentXML::addSourceFileAttribute(const std::string& fileName) |
| 306 | { |
| 307 | addToMap(SourceFiles, fileName, ID_FILE); |
| 308 | addAttribute("file", getPrefixedId(SourceFiles[fileName], ID_FILE)); |
| 309 | } |
| 310 | |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 311 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | //--------------------------------------------------------- |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 313 | void DocumentXML::addPtrAttribute(const char* pName, const LabelStmt* L) |
| 314 | { |
| 315 | addToMap(Labels, L, ID_LABEL); |
| 316 | addAttribute(pName, getPrefixedId(Labels[L], ID_LABEL)); |
| 317 | } |
| 318 | |
| 319 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | //--------------------------------------------------------- |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 321 | PresumedLoc DocumentXML::addLocation(const SourceLocation& Loc) |
| 322 | { |
| 323 | SourceManager& SM = Ctx->getSourceManager(); |
| 324 | SourceLocation SpellingLoc = SM.getSpellingLoc(Loc); |
| 325 | PresumedLoc PLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | if (!SpellingLoc.isInvalid()) |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 327 | { |
| 328 | PLoc = SM.getPresumedLoc(SpellingLoc); |
| 329 | addSourceFileAttribute(PLoc.getFilename()); |
| 330 | addAttribute("line", PLoc.getLine()); |
| 331 | addAttribute("col", PLoc.getColumn()); |
| 332 | } |
| 333 | // else there is no error in some cases (eg. CXXThisExpr) |
| 334 | return PLoc; |
| 335 | } |
| 336 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | //--------------------------------------------------------- |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 338 | void DocumentXML::addLocationRange(const SourceRange& R) |
| 339 | { |
| 340 | PresumedLoc PStartLoc = addLocation(R.getBegin()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | if (R.getBegin() != R.getEnd()) |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 342 | { |
| 343 | SourceManager& SM = Ctx->getSourceManager(); |
| 344 | SourceLocation SpellingLoc = SM.getSpellingLoc(R.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | if (!SpellingLoc.isInvalid()) |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 346 | { |
| 347 | PresumedLoc PLoc = SM.getPresumedLoc(SpellingLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | if (PStartLoc.isInvalid() || |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 349 | strcmp(PLoc.getFilename(), PStartLoc.getFilename()) != 0) { |
| 350 | addToMap(SourceFiles, PLoc.getFilename(), ID_FILE); |
| 351 | addAttribute("endfile", PLoc.getFilename()); |
| 352 | addAttribute("endline", PLoc.getLine()); |
| 353 | addAttribute("endcol", PLoc.getColumn()); |
| 354 | } else if (PLoc.getLine() != PStartLoc.getLine()) { |
| 355 | addAttribute("endline", PLoc.getLine()); |
| 356 | addAttribute("endcol", PLoc.getColumn()); |
| 357 | } else { |
| 358 | addAttribute("endcol", PLoc.getColumn()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | } |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 | //--------------------------------------------------------- |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 365 | void DocumentXML::PrintDecl(Decl *D) |
| 366 | { |
Douglas Gregor | 038f75a | 2009-06-15 19:02:54 +0000 | [diff] [blame] | 367 | writeDeclToXML(D); |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | //--------------------------------------------------------- |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 371 | } // NS clang |
| 372 | |