From ec2b103c267a06a66e926f62cd96767b280f5cf5 Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Tue, 2 Jun 2009 17:58:47 +0000 Subject: Import Clang, at r72732. --- include/clang/Frontend/DocumentXML.h | 128 +++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 include/clang/Frontend/DocumentXML.h (limited to 'include/clang/Frontend/DocumentXML.h') diff --git a/include/clang/Frontend/DocumentXML.h b/include/clang/Frontend/DocumentXML.h new file mode 100644 index 0000000000000..99db717190dd6 --- /dev/null +++ b/include/clang/Frontend/DocumentXML.h @@ -0,0 +1,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 +#include +#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 + struct IdMap : llvm::DenseMap {}; + + template<> + struct IdMap : std::map {}; + + template<> + struct IdMap : std::map {}; +} + +//--------------------------------------------------------- +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 + 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 Types; + XML::IdMap Contexts; + XML::IdMap BasicTypes; + XML::IdMap SourceFiles; + XML::IdMap 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 +inline void DocumentXML::addAttribute(const char* pName, const T& value) +{ + Out << ' ' << pName << "=\"" << value << "\""; +} + +//--------------------------------------------------------- + +} //namespace clang + +#endif //LLVM_CLANG_DOCUMENTXML_H -- cgit v1.2.3