diff options
author | Douglas Gregor <dgregor@apple.com> | 2009-06-15 19:02:54 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2009-06-15 19:02:54 +0000 |
commit | 038f75abf5abd1e35736dd5c751ba20df1158aaa (patch) | |
tree | 9d24ee58bd5859c13aa1b46b83e3ce86275bf5bd | |
parent | 3b36b66a00b7d5bab71b486a54694f0ae397bddb (diff) |
More XML output support, from Olaf Krzikalla!
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@73402 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/AST/TypeVisitor.h | 50 | ||||
-rw-r--r-- | include/clang/Frontend/DeclContextXML.def | 113 | ||||
-rw-r--r-- | include/clang/Frontend/DeclXML.def | 250 | ||||
-rw-r--r-- | include/clang/Frontend/DocumentXML.def | 75 | ||||
-rw-r--r-- | include/clang/Frontend/DocumentXML.h | 84 | ||||
-rw-r--r-- | include/clang/Frontend/StmtXML.def | 517 | ||||
-rw-r--r-- | include/clang/Frontend/TypeXML.def | 277 | ||||
-rw-r--r-- | lib/Frontend/CMakeLists.txt | 2 | ||||
-rw-r--r-- | lib/Frontend/DeclXML.cpp | 161 | ||||
-rw-r--r-- | lib/Frontend/DocumentXML.cpp | 348 | ||||
-rw-r--r-- | lib/Frontend/StmtXML.cpp | 100 | ||||
-rw-r--r-- | lib/Frontend/TypeXML.cpp | 127 |
12 files changed, 1787 insertions, 317 deletions
diff --git a/include/clang/AST/TypeVisitor.h b/include/clang/AST/TypeVisitor.h new file mode 100644 index 0000000000..a02e39b3f3 --- /dev/null +++ b/include/clang/AST/TypeVisitor.h @@ -0,0 +1,50 @@ +//===--- TypeVisitor.h - Visitor for Stmt subclasses ------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the TypeVisitor interface. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_AST_TYPEVISITOR_H +#define LLVM_CLANG_AST_TYPEVISITOR_H + +#include "clang/AST/Type.h" + +namespace clang { + +#define DISPATCH(CLASS) \ + return static_cast<ImplClass*>(this)->Visit ## CLASS(static_cast<CLASS*>(T)) + +template<typename ImplClass, typename RetTy=void> +class TypeVisitor { +public: + RetTy Visit(Type *T) { + // Top switch stmt: dispatch to VisitFooStmt for each FooStmt. + switch (T->getTypeClass()) { + default: assert(0 && "Unknown type class!"); +#define ABSTRACT_TYPE(CLASS, PARENT) +#define TYPE(CLASS, PARENT) case Type::CLASS: DISPATCH(CLASS##Type); +#include "clang/AST/TypeNodes.def" + } + } + + // If the implementation chooses not to implement a certain visit method, fall + // back on superclass. +#define TYPE(CLASS, PARENT) RetTy Visit##CLASS##Type(CLASS##Type *T) { DISPATCH(PARENT); } +#include "clang/AST/TypeNodes.def" + + // Base case, ignore it. :) + RetTy VisitType(Type*) { return RetTy(); } +}; + +#undef DISPATCH + +} // end namespace clang + +#endif diff --git a/include/clang/Frontend/DeclContextXML.def b/include/clang/Frontend/DeclContextXML.def new file mode 100644 index 0000000000..39ed5f9432 --- /dev/null +++ b/include/clang/Frontend/DeclContextXML.def @@ -0,0 +1,113 @@ +//===-- DeclContextXML.def - Metadata about Context XML nodes ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the XML context info database as written in the +// <ReferenceSection>/<Contexts> sub-nodes of the XML document. Type nodes +// are referred by "context" reference attributes throughout the document. +// A context node never contains sub-nodes. +// The semantics of the attributes and enums are mostly self-documenting +// by looking at the appropriate internally used functions and values. +// The following macros are used: +// +// NODE_XML( CLASS, NAME ) - A node of name NAME denotes a concrete +// context of class CLASS where CLASS is a class name used internally by clang. +// After a NODE_XML the definition of all (optional) attributes of that context +// node and possible sub-nodes follows. +// +// END_NODE_XML - Closes the attribute definition of the current node. +// +// ID_ATTRIBUTE_XML - Context nodes have an "id" attribute containing a +// string, which value uniquely identify that statement. Other nodes may refer +// by "context" attributes to this value. +// +// TYPE_ATTRIBUTE_XML( FN ) - Context nodes may refer to the ids of type +// nodes by a "type" attribute, if they create a type during declaration. +// For instance 'struct S;' creates both a context 'S::' and a type 'S'. +// Contexts and types always have different ids, however declarations and +// contexts may share the same ids. FN is internally used by clang. +// +// ATTRIBUTE_XML( FN, NAME ) - An attribute named NAME. FN is internally +// used by clang. A boolean attribute have the values "0" or "1". +// +// ATTRIBUTE_ENUM[_OPT]_XML( FN, NAME ) - An attribute named NAME. The value +// is an enumeration defined with ENUM_XML macros immediately following after +// that macro. An optional attribute is ommited, if the particular enum is the +// empty string. FN is internally used by clang. +// +// ENUM_XML( VALUE, NAME ) - An enumeration element named NAME. VALUE is +// internally used by clang. +// +// END_ENUM_XML - Closes the enumeration definition of the current attribute. +// +//===----------------------------------------------------------------------===// + +#ifndef TYPE_ATTRIBUTE_XML +# define TYPE_ATTRIBUTE_XML( FN ) ATTRIBUTE_XML(FN, "type") +#endif + +#ifndef CONTEXT_ATTRIBUTE_XML +# define CONTEXT_ATTRIBUTE_XML( FN ) ATTRIBUTE_XML(FN, "context") +#endif + +NODE_XML(TranslationUnitDecl, "TranslationUnit") + ID_ATTRIBUTE_XML +END_NODE_XML + +NODE_XML(FunctionDecl, "Function") + ID_ATTRIBUTE_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") + TYPE_ATTRIBUTE_XML(getType()->getAsFunctionType()) +END_NODE_XML + +NODE_XML(NamespaceDecl, "Namespace") + ID_ATTRIBUTE_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") +END_NODE_XML + +NODE_XML(RecordDecl, "Record") + ID_ATTRIBUTE_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") + TYPE_ATTRIBUTE_XML(getTypeForDecl()) +END_NODE_XML + +NODE_XML(EnumDecl, "Enum") + ID_ATTRIBUTE_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") + TYPE_ATTRIBUTE_XML(getTypeForDecl()) +END_NODE_XML + +NODE_XML(LinkageSpecDecl, "LinkageSpec") + ID_ATTRIBUTE_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_ENUM_OPT_XML(getLanguage(), "lang") + ENUM_XML(LinkageSpecDecl::lang_c, "C") + ENUM_XML(LinkageSpecDecl::lang_cxx, "CXX") + END_ENUM_XML +END_NODE_XML + +//===----------------------------------------------------------------------===// +#undef NODE_XML +#undef ID_ATTRIBUTE_XML +#undef TYPE_ATTRIBUTE_XML +#undef ATTRIBUTE_XML +#undef ATTRIBUTE_SPECIAL_XML +#undef ATTRIBUTE_OPT_XML +#undef ATTRIBUTE_ENUM_XML +#undef ATTRIBUTE_ENUM_OPT_XML +#undef ATTRIBUTE_FILE_LOCATION_XML +#undef ENUM_XML +#undef END_ENUM_XML +#undef END_NODE_XML +#undef SUB_NODE_XML +#undef SUB_NODE_SEQUENCE_XML +#undef SUB_NODE_OPT_XML diff --git a/include/clang/Frontend/DeclXML.def b/include/clang/Frontend/DeclXML.def new file mode 100644 index 0000000000..956d9719f9 --- /dev/null +++ b/include/clang/Frontend/DeclXML.def @@ -0,0 +1,250 @@ +//===-- DeclXML.def - Metadata about Decl XML nodes ------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the XML statement database structure as written in +// <TranslationUnit> sub-nodes of the XML document. +// The semantics of the attributes and enums are mostly self-documenting +// by looking at the appropriate internally used functions and values. +// The following macros are used: +// +// NODE_XML( CLASS, NAME ) - A node of name NAME denotes a concrete +// statement of class CLASS where CLASS is a class name used internally by clang. +// After a NODE_XML the definition of all (optional) attributes of that statement +// node and possible sub-nodes follows. +// +// END_NODE_XML - Closes the attribute definition of the current node. +// +// ID_ATTRIBUTE_XML - Some statement nodes have an "id" attribute containing a +// string, which value uniquely identify that statement. Other nodes may refer +// by reference attributes to this value (currently used only for Label). +// +// TYPE_ATTRIBUTE_XML( FN ) - Type nodes refer to the result type id of an +// expression by a "type" attribute. FN is internally used by clang. +// +// ATTRIBUTE_XML( FN, NAME ) - An attribute named NAME. FN is internally +// used by clang. A boolean attribute have the values "0" or "1". +// +// ATTRIBUTE_SPECIAL_XML( FN, NAME ) - An attribute named NAME which deserves +// a special handling. See the appropriate documentations. +// +// ATTRIBUTE_FILE_LOCATION_XML - A bunch of attributes denoting the location of +// a statement in the source file(s). +// +// ATTRIBUTE_OPT_XML( FN, NAME ) - An optional attribute named NAME. +// Optional attributes are omitted for boolean types, if the value is false, +// for integral types, if the value is null and for strings, +// if the value is the empty string. FN is internally used by clang. +// +// ATTRIBUTE_ENUM[_OPT]_XML( FN, NAME ) - An attribute named NAME. The value +// is an enumeration defined with ENUM_XML macros immediately following after +// that macro. An optional attribute is ommited, if the particular enum is the +// empty string. FN is internally used by clang. +// +// ENUM_XML( VALUE, NAME ) - An enumeration element named NAME. VALUE is +// internally used by clang. +// +// END_ENUM_XML - Closes the enumeration definition of the current attribute. +// +// SUB_NODE_XML( CLASS ) - A mandatory sub-node of class CLASS or its sub-classes. +// +// SUB_NODE_OPT_XML( CLASS ) - An optional sub-node of class CLASS or its sub-classes. +// +// SUB_NODE_SEQUENCE_XML( CLASS ) - Zero or more sub-nodes of class CLASS or +// its sub-classes. +// +//===----------------------------------------------------------------------===// + +#ifndef ATTRIBUTE_FILE_LOCATION_XML +# define ATTRIBUTE_FILE_LOCATION_XML \ + ATTRIBUTE_XML(getFilename(), "file") \ + ATTRIBUTE_XML(getLine(), "line") \ + ATTRIBUTE_XML(getColumn(), "col") \ + ATTRIBUTE_OPT_XML(getFilename(), "endfile") \ + ATTRIBUTE_OPT_XML(getLine(), "endline") \ + ATTRIBUTE_OPT_XML(getColumn(), "endcol") +#endif + +#ifndef TYPE_ATTRIBUTE_XML +# define TYPE_ATTRIBUTE_XML( FN ) ATTRIBUTE_XML(FN, "type") +#endif + +#ifndef CONTEXT_ATTRIBUTE_XML +# define CONTEXT_ATTRIBUTE_XML( FN ) ATTRIBUTE_XML(FN, "context") +#endif + +//NODE_XML(TranslationUnitDecl, "TranslationUnit") +// SUB_NODE_SEQUENCE_XML(Decl) +//END_NODE_XML + +NODE_XML(Decl, "FIXME_Decl") + ATTRIBUTE_FILE_LOCATION_XML +END_NODE_XML + +NODE_XML(FunctionDecl, "Function") + ID_ATTRIBUTE_XML + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") + TYPE_ATTRIBUTE_XML(getType()->getAsFunctionType()->getResultType()) + ATTRIBUTE_XML(getType()->getAsFunctionType(), "function_type") + ATTRIBUTE_ENUM_OPT_XML(getStorageClass(), "storage_class") + ENUM_XML(FunctionDecl::None, "") + ENUM_XML(FunctionDecl::Extern, "extern") + ENUM_XML(FunctionDecl::Static, "static") + ENUM_XML(FunctionDecl::PrivateExtern, "__private_extern__") + END_ENUM_XML + ATTRIBUTE_OPT_XML(isInline(), "inline") + //ATTRIBUTE_OPT_XML(isVariadic(), "variadic") // in the type reference + ATTRIBUTE_XML(getNumParams(), "num_args") + SUB_NODE_SEQUENCE_XML(ParmVarDecl) + //SUB_NODE_OPT_XML("Body") +END_NODE_XML + +NODE_XML(CXXMethodDecl, "CXXMethodDecl") + ID_ATTRIBUTE_XML + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") + TYPE_ATTRIBUTE_XML(getType()->getAsFunctionType()->getResultType()) + ATTRIBUTE_XML(getType()->getAsFunctionType(), "function_type") + ATTRIBUTE_OPT_XML(isInline(), "inline") + ATTRIBUTE_OPT_XML(isStatic(), "static") + ATTRIBUTE_OPT_XML(isVirtual(), "virtual") + ATTRIBUTE_XML(getNumParams(), "num_args") + SUB_NODE_SEQUENCE_XML(ParmVarDecl) + //SUB_NODE_OPT_XML("Body") +END_NODE_XML + +//NODE_XML("Body") +// SUB_NODE_XML(Stmt) +//END_NODE_XML + +NODE_XML(NamespaceDecl, "Namespace") + ID_ATTRIBUTE_XML + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") +END_NODE_XML + +NODE_XML(UsingDirectiveDecl, "UsingDirective") + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") + ATTRIBUTE_XML(getNominatedNamespace(), "ref") +END_NODE_XML + +NODE_XML(NamespaceAliasDecl, "NamespaceAlias") + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") + ATTRIBUTE_XML(getNamespace(), "ref") +END_NODE_XML + +NODE_XML(RecordDecl, "Record") + ID_ATTRIBUTE_XML + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") + ATTRIBUTE_OPT_XML(isDefinition() == false, "forward") + ATTRIBUTE_XML(getTypeForDecl(), "type") // refers to the type this decl creates + SUB_NODE_SEQUENCE_XML(FieldDecl) +END_NODE_XML + +NODE_XML(EnumDecl, "Enum") + ID_ATTRIBUTE_XML + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") + ATTRIBUTE_OPT_XML(isDefinition() == false, "forward") + ATTRIBUTE_SPECIAL_XML(getIntegerType(), "type") // is NULL in pure declarations thus deserves special handling + SUB_NODE_SEQUENCE_XML(EnumConstantDecl) // only present in definition +END_NODE_XML + +NODE_XML(EnumConstantDecl, "EnumConstant") + ID_ATTRIBUTE_XML + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") + TYPE_ATTRIBUTE_XML(getType()) + ATTRIBUTE_XML(getInitVal().toString(10, true), "value") // integer + SUB_NODE_OPT_XML(Expr) // init expr of this constant +END_NODE_XML + +NODE_XML(FieldDecl, "Field") + ID_ATTRIBUTE_XML + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") + TYPE_ATTRIBUTE_XML(getType()) + ATTRIBUTE_OPT_XML(isMutable(), "mutable") + ATTRIBUTE_OPT_XML(isBitField(), "bitfield") + SUB_NODE_OPT_XML(Expr) // init expr of a bit field +END_NODE_XML + +NODE_XML(TypedefDecl, "Typedef") + ID_ATTRIBUTE_XML + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") + TYPE_ATTRIBUTE_XML(getUnderlyingType()) +END_NODE_XML + +NODE_XML(VarDecl, "Var") + ID_ATTRIBUTE_XML + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") + TYPE_ATTRIBUTE_XML(getType()) + ATTRIBUTE_ENUM_OPT_XML(getStorageClass(), "storage_class") + ENUM_XML(VarDecl::None, "") + ENUM_XML(VarDecl::Auto, "auto") + ENUM_XML(VarDecl::Register, "register") + ENUM_XML(VarDecl::Extern, "extern") + ENUM_XML(VarDecl::Static, "static") + ENUM_XML(VarDecl::PrivateExtern, "__private_extern__") + END_ENUM_XML + SUB_NODE_OPT_XML(Expr) // init expr +END_NODE_XML + +NODE_XML(ParmVarDecl, "ParmVar") + ID_ATTRIBUTE_XML + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_XML(getNameAsString(), "name") + TYPE_ATTRIBUTE_XML(getType()) + SUB_NODE_OPT_XML(Expr) // default argument expression +END_NODE_XML + +NODE_XML(LinkageSpecDecl, "LinkageSpec") + ID_ATTRIBUTE_XML + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getDeclContext(), "context") + ATTRIBUTE_ENUM_OPT_XML(getLanguage(), "lang") + ENUM_XML(LinkageSpecDecl::lang_c, "C") + ENUM_XML(LinkageSpecDecl::lang_cxx, "CXX") + END_ENUM_XML +END_NODE_XML + + +//===----------------------------------------------------------------------===// +#undef NODE_XML +#undef ID_ATTRIBUTE_XML +#undef TYPE_ATTRIBUTE_XML +#undef ATTRIBUTE_XML +#undef ATTRIBUTE_SPECIAL_XML +#undef ATTRIBUTE_OPT_XML +#undef ATTRIBUTE_ENUM_XML +#undef ATTRIBUTE_ENUM_OPT_XML +#undef ATTRIBUTE_FILE_LOCATION_XML +#undef ENUM_XML +#undef END_ENUM_XML +#undef END_NODE_XML +#undef SUB_NODE_XML +#undef SUB_NODE_SEQUENCE_XML +#undef SUB_NODE_OPT_XML diff --git a/include/clang/Frontend/DocumentXML.def b/include/clang/Frontend/DocumentXML.def new file mode 100644 index 0000000000..4c52bd8442 --- /dev/null +++ b/include/clang/Frontend/DocumentXML.def @@ -0,0 +1,75 @@ +//===-- DocumentXML.def - Metadata about Document XML nodes -----*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the XML root database structure as written in +// an AST XML document. +// The following macros are used: +// +// NODE_XML( CLASS, NAME ) - A node of name NAME denotes a concrete +// statement of class CLASS where CLASS is a class name used internally by clang. +// After a NODE_XML the definition of all (optional) attributes of that statement +// node and possible sub-nodes follows. +// +// END_NODE_XML - Closes the attribute definition of the current node. +// +// ID_ATTRIBUTE_XML - Some nodes have an "id" attribute containing a +// string, which value uniquely identify the entity represented by that node. +// Other nodes may refer by reference attributes to this value. +// +// ATTRIBUTE_SPECIAL_XML( FN, NAME ) - An attribute named NAME which deserves +// a special handling. See the appropriate documentations. +// +// SUB_NODE_XML( CLASS ) - A mandatory sub-node of class CLASS or its sub-classes. +// +// SUB_NODE_SEQUENCE_XML( CLASS ) - Zero or more sub-nodes of class CLASS or +// its sub-classes. +// +//===----------------------------------------------------------------------===// + +ROOT_NODE_XML("CLANG_XML") + ATTRIBUTE_SPECIAL_XML(ignore, "version") // special retrieving needed + SUB_NODE_XML("TranslationUnit") + SUB_NODE_XML("ReferenceSection") +END_NODE_XML + +NODE_XML("TranslationUnit") + SUB_NODE_SEQUENCE_XML(Decl) +END_NODE_XML + +NODE_XML("ReferenceSection") + SUB_NODE_XML("Types") + SUB_NODE_XML("Contexts") + SUB_NODE_XML("Files") +END_NODE_XML + +NODE_XML("Types") + SUB_NODE_SEQUENCE_XML(Type) +END_NODE_XML + +NODE_XML("Contexts") + SUB_NODE_SEQUENCE_XML(DeclContext) +END_NODE_XML + +NODE_XML("Files") + SUB_NODE_SEQUENCE_XML("File") +END_NODE_XML + +NODE_XML("File") + ID_ATTRIBUTE_XML + ATTRIBUTE_SPECIAL_XML(ignore, "name") // special retrieving needed, denotes the source file name +END_NODE_XML + + +//===----------------------------------------------------------------------===// +#undef NODE_XML +#undef ID_ATTRIBUTE_XML +#undef ATTRIBUTE_SPECIAL_XML +#undef END_NODE_XML +#undef SUB_NODE_XML +#undef SUB_NODE_SEQUENCE_XML diff --git a/include/clang/Frontend/DocumentXML.h b/include/clang/Frontend/DocumentXML.h index 99db717190..4ed11e153c 100644 --- a/include/clang/Frontend/DocumentXML.h +++ b/include/clang/Frontend/DocumentXML.h @@ -17,6 +17,7 @@ #include <string> #include <map> +#include <stack> #include "clang/AST/Type.h" #include "clang/AST/TypeOrdering.h" #include "llvm/Support/raw_ostream.h" @@ -30,6 +31,7 @@ class Decl; class NamedDecl; class FunctionDecl; class ASTContext; +class LabelStmt; //--------------------------------------------------------- namespace XML @@ -50,26 +52,37 @@ 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 + void addAttribute(const char* pName, const QualType& pType); + void addAttribute(const char* pName, bool value); + + template<class T> + void addAttribute(const char* pName, const T* value) + { + addPtrAttribute(pName, value); + } + + template<class T> + void addAttribute(const char* pName, T* value) + { + addPtrAttribute(pName, value); + } + template<class T> void addAttribute(const char* pName, const T& value); - void addTypeAttribute(const QualType& pType); - void addRefAttribute(const NamedDecl* D); + template<class T> + void addAttributeOptional(const char* pName, const T& value); - 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); @@ -81,13 +94,9 @@ private: DocumentXML(const DocumentXML&); // not defined DocumentXML& operator=(const DocumentXML&); // not defined - struct NodeXML; - - NodeXML* Root; - NodeXML* CurrentNode; // always after Root + std::stack<std::string> NodeStack; llvm::raw_ostream& Out; ASTContext *Ctx; - int CurrentIndent; bool HasCurrentNodeSubNodes; @@ -96,15 +105,38 @@ private: XML::IdMap<const Type*> BasicTypes; XML::IdMap<std::string> SourceFiles; XML::IdMap<const NamedDecl*> Decls; + XML::IdMap<const LabelStmt*> Labels; void addContextsRecursively(const DeclContext *DC); - void addBasicTypeRecursively(const Type* pType); + void addTypeRecursively(const Type* pType); void addTypeRecursively(const QualType& pType); - void PrintFunctionDecl(FunctionDecl *FD); - void addDeclIdAttribute(const NamedDecl* D); - void addTypeIdAttribute(const Type* pType); void Indent(); + + // forced pointer dispatch: + void addPtrAttribute(const char* pName, const Type* pType); + void addPtrAttribute(const char* pName, const NamedDecl* D); + void addPtrAttribute(const char* pName, const DeclContext* D); + void addPtrAttribute(const char* pName, const NamespaceDecl* D); // disambiguation + void addPtrAttribute(const char* pName, const LabelStmt* L); + void addPtrAttribute(const char* pName, const char* text); + + // defined in TypeXML.cpp: + void addParentTypes(const Type* pType); + void writeTypeToXML(const Type* pType); + void writeTypeToXML(const QualType& pType); + class TypeAdder; + friend class TypeAdder; + + // defined in DeclXML.cpp: + void writeDeclToXML(Decl *D); + class DeclPrinter; + friend class DeclPrinter; + + // for addAttributeOptional: + static bool isDefault(unsigned value) { return value == 0; } + static bool isDefault(bool value) { return !value; } + static bool isDefault(const std::string& value) { return value.empty(); } }; //--------------------------------------------------------- inlines @@ -122,6 +154,28 @@ inline void DocumentXML::addAttribute(const char* pName, const T& value) } //--------------------------------------------------------- +inline void DocumentXML::addPtrAttribute(const char* pName, const char* text) +{ + Out << ' ' << pName << "=\"" << text << "\""; +} + +//--------------------------------------------------------- +inline void DocumentXML::addAttribute(const char* pName, bool value) +{ + addPtrAttribute(pName, value ? "1" : "0"); +} + +//--------------------------------------------------------- +template<class T> +inline void DocumentXML::addAttributeOptional(const char* pName, const T& value) +{ + if (!isDefault(value)) + { + addAttribute(pName, value); + } +} + +//--------------------------------------------------------- } //namespace clang diff --git a/include/clang/Frontend/StmtXML.def b/include/clang/Frontend/StmtXML.def new file mode 100644 index 0000000000..26430f740d --- /dev/null +++ b/include/clang/Frontend/StmtXML.def @@ -0,0 +1,517 @@ +//===-- StmtXML.def - Metadata about Stmt XML nodes ------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the XML statement database structure as written in +// <TranslationUnit> sub-nodes of the XML document. +// The semantics of the attributes and enums are mostly self-documenting +// by looking at the appropriate internally used functions and values. +// The following macros are used: +// +// NODE_XML( CLASS, NAME ) - A node of name NAME denotes a concrete +// statement of class CLASS where CLASS is a class name used internally by clang. +// After a NODE_XML the definition of all (optional) attributes of that statement +// node and possible sub-nodes follows. +// +// END_NODE_XML - Closes the attribute definition of the current node. +// +// ID_ATTRIBUTE_XML - Some statement nodes have an "id" attribute containing a +// string, which value uniquely identify that statement. Other nodes may refer +// by reference attributes to this value (currently used only for Label). +// +// TYPE_ATTRIBUTE_XML( FN ) - Type nodes refer to the result type id of an +// expression by a "type" attribute. FN is internally used by clang. +// +// ATTRIBUTE_XML( FN, NAME ) - An attribute named NAME. FN is internally +// used by clang. A boolean attribute have the values "0" or "1". +// +// ATTRIBUTE_SPECIAL_XML( FN, NAME ) - An attribute named NAME which deserves +// a special handling. See the appropriate documentations. +// +// ATTRIBUTE_FILE_LOCATION_XML - A bunch of attributes denoting the location of +// a statement in the source file(s). +// +// ATTRIBUTE_OPT_XML( FN, NAME ) - An optional attribute named NAME. +// Optional attributes are omitted for boolean types, if the value is false, +// for integral types, if the value is null and for strings, +// if the value is the empty string. FN is internally used by clang. +// +// ATTRIBUTE_ENUM[_OPT]_XML( FN, NAME ) - An attribute named NAME. The value +// is an enumeration defined with ENUM_XML macros immediately following after +// that macro. An optional attribute is ommited, if the particular enum is the +// empty string. FN is internally used by clang. +// +// ENUM_XML( VALUE, NAME ) - An enumeration element named NAME. VALUE is +// internally used by clang. +// +// END_ENUM_XML - Closes the enumeration definition of the current attribute. +// +// SUB_NODE_XML( CLASS ) - A mandatory sub-node of class CLASS or its sub-classes. +// +// SUB_NODE_OPT_XML( CLASS ) - An optional sub-node of class CLASS or its sub-classes. +// +// SUB_NODE_SEQUENCE_XML( CLASS ) - Zero or more sub-nodes of class CLASS or +// its sub-classes. +// +//===----------------------------------------------------------------------===// + +#ifndef ATTRIBUTE_FILE_LOCATION_XML +# define ATTRIBUTE_FILE_LOCATION_XML \ + ATTRIBUTE_XML(getFilename(), "file") \ + ATTRIBUTE_XML(getLine(), "line") \ + ATTRIBUTE_XML(getColumn(), "col") \ + ATTRIBUTE_OPT_XML(getFilename(), "endfile") \ + ATTRIBUTE_OPT_XML(getLine(), "endline") \ + ATTRIBUTE_OPT_XML(getColumn(), "endcol") +#endif + +#ifndef TYPE_ATTRIBUTE_XML +# define TYPE_ATTRIBUTE_XML( FN ) ATTRIBUTE_XML(FN, "type") +#endif + +#ifndef CONTEXT_ATTRIBUTE_XML +# define CONTEXT_ATTRIBUTE_XML( FN ) ATTRIBUTE_XML(FN, "context") +#endif + + +NODE_XML(NullStmt, "NullStmt") + ATTRIBUTE_FILE_LOCATION_XML +END_NODE_XML + +NODE_XML(CompoundStmt, "CompoundStmt") + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(size(), "num_stmts") + SUB_NODE_SEQUENCE_XML(Stmt) +END_NODE_XML + +NODE_XML(CaseStmt, "CaseStmt") // case expr: body; + ATTRIBUTE_FILE_LOCATION_XML + SUB_NODE_XML(Stmt) // body + SUB_NODE_XML(Expr) // expr + SUB_NODE_XML(Expr) // rhs expr in gc extension: case expr .. expr: body; +END_NODE_XML + +NODE_XML(DefaultStmt, "DefaultStmt") // default: body; + ATTRIBUTE_FILE_LOCATION_XML + SUB_NODE_XML(Stmt) // body +END_NODE_XML + +NODE_XML(LabelStmt, "LabelStmt") // Label: body; + ID_ATTRIBUTE_XML + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getName(), "name") // string + SUB_NODE_XML(Stmt) // body +END_NODE_XML + +NODE_XML(IfStmt, "IfStmt") // if (cond) stmt1; else stmt2; + ATTRIBUTE_FILE_LOCATION_XML + SUB_NODE_XML(Expr) // cond + SUB_NODE_XML(Stmt) // stmt1 + SUB_NODE_XML(Stmt) // stmt2 +END_NODE_XML + +NODE_XML(SwitchStmt, "SwitchStmt") // switch (cond) body; + ATTRIBUTE_FILE_LOCATION_XML + SUB_NODE_XML(Expr) // cond + SUB_NODE_XML(Stmt) // body +END_NODE_XML + +NODE_XML(WhileStmt, "WhileStmt") // while (cond) body; + ATTRIBUTE_FILE_LOCATION_XML + SUB_NODE_XML(Expr) // cond + SUB_NODE_XML(Stmt) // body +END_NODE_XML + +NODE_XML(DoStmt, "DoStmt") // do body while (cond); + ATTRIBUTE_FILE_LOCATION_XML + SUB_NODE_XML(Expr) // cond + SUB_NODE_XML(Stmt) // body +END_NODE_XML + +NODE_XML(ForStmt, "ForStmt") // for (init; cond; inc) body; + ATTRIBUTE_FILE_LOCATION_XML + SUB_NODE_XML(Stmt) // init + SUB_NODE_XML(Expr) // cond + SUB_NODE_XML(Expr) // inc + SUB_NODE_XML(Stmt) // body +END_NODE_XML + +NODE_XML(GotoStmt, "GotoStmt") // goto label; + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getLabel()->getName(), "name") // informal string + ATTRIBUTE_XML(getLabel(), "ref") // id string +END_NODE_XML + +NODE_XML(IndirectGotoStmt, "IndirectGotoStmt") // goto expr; + ATTRIBUTE_FILE_LOCATION_XML + SUB_NODE_XML(Expr) // expr +END_NODE_XML + +NODE_XML(ContinueStmt, "ContinueStmt") // continue + ATTRIBUTE_FILE_LOCATION_XML +END_NODE_XML + +NODE_XML(BreakStmt, "BreakStmt") // break + ATTRIBUTE_FILE_LOCATION_XML +END_NODE_XML + +NODE_XML(ReturnStmt, "ReturnStmt") // return expr; + ATTRIBUTE_FILE_LOCATION_XML + SUB_NODE_XML(Expr) // expr +END_NODE_XML + +NODE_XML(AsmStmt, "AsmStmt") // GNU inline-assembly statement extension + ATTRIBUTE_FILE_LOCATION_XML + // FIXME +END_NODE_XML + +NODE_XML(DeclStmt, "DeclStmt") // a declaration statement + ATTRIBUTE_FILE_LOCATION_XML + SUB_NODE_SEQUENCE_XML(Decl) +END_NODE_XML + +// C++ statements +NODE_XML(CXXTryStmt, "CXXTryStmt") // try CompoundStmt CXXCatchStmt1 CXXCatchStmt2 .. + ATTRIBUTE_FILE_LOCATION_XML + ATTRIBUTE_XML(getNumHandlers(), "num_handlers") + SUB_NODE_XML(CompoundStmt) + SUB_NODE_SEQUENCE_XML(CXXCatchStmt) +END_NODE_XML + +NODE_XML(CXXCatchStmt, "CXXCatchStmt") // catch (decl) Stmt + ATTRIBUTE_FILE_LOCATION_XML + SUB_NODE_XML(VarDecl) + SUB_NODE_XML(Stmt) +END_NODE_XML + +// Expressions +NODE_XML(PredefinedExpr, "PredefinedExpr") + ATTRIBUTE_FILE_LOCATION_XML + TYPE_ATTRIBUTE_XML(getType()) + ATTRIBUTE_ENUM_XML(getIdentType(), "kind") + ENUM_XML(PredefinedExpr::Func, "__func__") + ENUM_XML(PredefinedExpr::Function, "__FUNCTION__") + ENUM_XML(PredefinedExpr::PrettyFunction, "__PRETTY_FUNCTION__") + END_ENUM_XML +END_NODE_XML + +NODE_XML(DeclRefExpr, "DeclRefExpr") // an expression referring to a declared entity + ATTRIBUTE_FILE_LOCATION_XML + TYPE_ATTRIBUTE_XML(getType()) + ATTRIBUTE_XML(getDecl(), "ref") // id string of the declaration + ATTRIBUTE_XML(getDecl()->getNameAsString(), "name") // informal + //ATTRIBUTE_ENUM_XML(getDecl()->getKind(), "kind") // really needed here? +END_NODE_XML + +NODE_XML(IntegerLiteral, "IntegerLiteral") + ATTRIBUTE_FILE_LOCATION_XML + TYPE_ATTRIBUTE_XML(getType()) + ATTRIBUTE_XML(getValue(), "value") // (signed) integer +END_NODE_XML + +NODE_XML(CharacterLiteral, "CharacterLiteral") + ATTRIBUTE_FILE_LOCATION_XML + TYPE_ATTRIBUTE_XML(getType()) + ATTRIBUTE_XML(getValue(), "value") // unsigned +END_NODE_XML + +NODE_XML(FloatingLiteral, "FloatingLiteral") + ATTRIBUTE_FILE_LOCATION_XML + TYPE_ATTRIBUTE_XML(getType()) + // FIXME: output float as written in source (no approximation or the like) + //ATTRIBUTE_XML(getValueAsApproximateDouble(), "value") // float +END_NODE_XML + +NODE_XML(StringLiteral, "StringLiteral") + ATTRIBUTE_FILE_LOCATION_XML + TYPE_ATTRIBUTE_XML(getType()) + ATTRIBUTE_SPECIAL_XML(getStrData(), "value") // string, special handling for escaping needed + ATTRIBUTE_OPT_XML(isWide(), "is_wide") // boolean +END_NODE_XML + +NODE_XML(UnaryOperator, "UnaryOperator") // op(expr) or (expr)op + ATTRIBUTE_FILE_LOCATION_XML + TYPE_ATTRIBUTE_XML(getType()) + ATTRIBUTE_ENUM_XML(getOpcode(), "kind") + ENUM_XML(UnaryOperator::PostInc, "postinc") + ENUM_XML(UnaryOperator::PostDec, "postdec") + ENUM_XML(UnaryOperator::PreInc, "preinc") + ENUM_XML(UnaryOperator::PreDec, "predec") + ENUM_XML(Unary |