zcov: / include/clang/Frontend/DocumentXML.h


Files: 1 Branches Taken: 60.0% 6 / 10
Generated: 2010-02-10 01:31 Branches Executed: 80.0% 8 / 10
Line Coverage: 100.0% 29 / 29


Programs: 5 Runs 14485


       1                 : //===--- DocumentXML.h - XML document for ASTs ------------------*- C++ -*-===//
       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                 : //
      10                 : // This file implements the XML document class, which provides the means to
      11                 : // dump out the AST in a XML form that exposes type details and other fields.
      12                 : //
      13                 : //===----------------------------------------------------------------------===//
      14                 : 
      15                 : #ifndef LLVM_CLANG_FRONTEND_DOCUMENTXML_H
      16                 : #define LLVM_CLANG_FRONTEND_DOCUMENTXML_H
      17                 : 
      18                 : #include <string>
      19                 : #include <map>
      20                 : #include <stack>
      21                 : #include "clang/AST/Type.h"
      22                 : #include "clang/AST/TypeOrdering.h"
      23                 : #include "llvm/Support/raw_ostream.h"
      24                 : #include "llvm/ADT/DenseMap.h"
      25                 : 
      26                 : namespace clang {
      27                 : 
      28                 : //--------------------------------------------------------- forwards
      29                 : class DeclContext;
      30                 : class Decl;
      31                 : class NamedDecl;
      32                 : class FunctionDecl;
      33                 : class ASTContext;
      34                 : class LabelStmt;
      35                 : 
      36                 : //---------------------------------------------------------
      37                 : namespace XML {
      38                 :   // id maps:
      39                 :   template<class T>
      40                8:   struct IdMap : llvm::DenseMap<T, unsigned> {};
      41                 : 
      42                 :   template<>
      43                2:   struct IdMap<QualType> : std::map<QualType, unsigned, QualTypeOrdering> {};
      44                 : 
      45                 :   template<>
      46                2:   struct IdMap<std::string> : std::map<std::string, unsigned> {};
      47                 : }
      48                 : 
      49                 : //---------------------------------------------------------
      50                1: class DocumentXML {
      51                 : public:
      52                 :   DocumentXML(const std::string& rootName, llvm::raw_ostream& out);
      53                 : 
      54                 :   void initialize(ASTContext &Context);
      55                 :   void PrintDecl(Decl *D);
      56                 :   void PrintStmt(const Stmt *S);    // defined in StmtXML.cpp
      57                 :   void finalize();
      58                 : 
      59                 : 
      60                 :   DocumentXML& addSubNode(const std::string& name);   // also enters the sub node, returns *this
      61                 :   DocumentXML& toParent();                            // returns *this
      62                 : 
      63                 :   void addAttribute(const char* pName, const QualType& pType);
      64                 :   void addAttribute(const char* pName, bool value);
      65                 : 
      66                 :   template<class T>
      67               96:   void addAttribute(const char* pName, const T* value)   {
      68               96:     addPtrAttribute(pName, value);
      69               96:   }
      70                 : 
      71                 :   template<class T>
      72              327:   void addAttribute(const char* pName, T* value) {
                        0: branch 2 not taken
                        0: branch 3 not taken
      73              327:     addPtrAttribute(pName, value);
      74              327:   }
      75                 : 
      76                 :   template<class T>
      77                 :   void addAttribute(const char* pName, const T& value);
      78                 : 
      79                 :   template<class T>
      80                 :   void addAttributeOptional(const char* pName, const T& value);
      81                 : 
      82                 :   void addSourceFileAttribute(const std::string& fileName);
      83                 : 
      84                 :   PresumedLoc addLocation(const SourceLocation& Loc);
      85                 :   void addLocationRange(const SourceRange& R);
      86                 : 
      87                 :   static std::string escapeString(const char* pStr, std::string::size_type len);
      88                 : 
      89                 : private:
      90                 :   DocumentXML(const DocumentXML&);              // not defined
      91                 :   DocumentXML& operator=(const DocumentXML&);   // not defined
      92                 : 
      93                 :   std::stack<std::string> NodeStack;
      94                 :   llvm::raw_ostream& Out;
      95                 :   ASTContext *Ctx;
      96                 :   bool      HasCurrentNodeSubNodes;
      97                 : 
      98                 : 
      99                 :   XML::IdMap<QualType>                 Types;
     100                 :   XML::IdMap<const DeclContext*>       Contexts;
     101                 :   XML::IdMap<const Type*>              BasicTypes;
     102                 :   XML::IdMap<std::string>              SourceFiles;
     103                 :   XML::IdMap<const NamedDecl*>         Decls;
     104                 :   XML::IdMap<const LabelStmt*>         Labels;
     105                 : 
     106                 :   void addContextsRecursively(const DeclContext *DC);
     107                 :   void addTypeRecursively(const Type* pType);
     108                 :   void addTypeRecursively(const QualType& pType);
     109                 : 
     110                 :   void Indent();
     111                 : 
     112                 :   // forced pointer dispatch:
     113                 :   void addPtrAttribute(const char* pName, const Type* pType);
     114                 :   void addPtrAttribute(const char* pName, const NamedDecl* D);
     115                 :   void addPtrAttribute(const char* pName, const DeclContext* D);
     116                 :   void addPtrAttribute(const char* pName, const NamespaceDecl* D);    // disambiguation
     117                 :   void addPtrAttribute(const char* pName, const LabelStmt* L);
     118                 :   void addPtrAttribute(const char* pName, const char* text);
     119                 : 
     120                 :   // defined in TypeXML.cpp:
     121                 :   void addParentTypes(const Type* pType);
     122                 :   void writeTypeToXML(const Type* pType);
     123                 :   void writeTypeToXML(const QualType& pType);
     124                 :   class TypeAdder;
     125                 :   friend class TypeAdder;
     126                 : 
     127                 :   // defined in DeclXML.cpp:
     128                 :   void writeDeclToXML(Decl *D);
     129                 :   class DeclPrinter;
     130                 :   friend class DeclPrinter;
     131                 : 
     132                 :   // for addAttributeOptional:
     133                8:   static bool isDefault(unsigned value)           { return value == 0; }
     134               62:   static bool isDefault(bool value)               { return !value; }
     135                2:   static bool isDefault(Qualifiers::GC value)     { return value == Qualifiers::GCNone; }
     136                 :   static bool isDefault(const std::string& value) { return value.empty(); }
     137                 : };
     138                 : 
     139                 : //--------------------------------------------------------- inlines
     140                 : 
     141                1: inline void DocumentXML::initialize(ASTContext &Context) {
     142                1:   Ctx = &Context;
     143                1: }
     144                 : 
     145                 : //---------------------------------------------------------
     146                 : template<class T>
     147             2422: inline void DocumentXML::addAttribute(const char* pName, const T& value) {
     148             2422:   Out << ' ' << pName << "=\"" << value << "\"";
     149             2422: }
     150                 : 
     151                 : //---------------------------------------------------------
     152               65: inline void DocumentXML::addPtrAttribute(const char* pName, const char* text) {
     153               65:   Out << ' ' << pName << "=\"" << text << "\"";
     154               65: }
     155                 : 
     156                 : //---------------------------------------------------------
     157               11: inline void DocumentXML::addAttribute(const char* pName, bool value) {
                        8: branch 0 taken
                        3: branch 1 taken
     158               11:   addPtrAttribute(pName, value ? "1" : "0");
     159               11: }
     160                 : 
     161                 : //---------------------------------------------------------
     162                 : template<class T>
     163                 : inline void DocumentXML::addAttributeOptional(const char* pName,
     164               72:                                               const T& value) {
                        5: branch 1 taken
                       52: branch 2 taken
                        0: branch 4 not taken
                        7: branch 5 taken
                        0: branch 7 not taken
                        8: branch 8 taken
     165               72:   if (!isDefault(value)) {
     166                5:     addAttribute(pName, value);
     167                 :   }
     168               72: }
     169                 : 
     170                 : //---------------------------------------------------------
     171                 : 
     172                 : } //namespace clang
     173                 : 
     174                 : #endif //LLVM_CLANG_DOCUMENTXML_H

Generated: 2010-02-10 01:31 by zcov