|
#include "stdafx.h"
#include "CTagsHierarchyLoader.h"
#include <Model/CTags/CTagsDatabase.h>
#include <Model/CTags/CTagsCppTagFactory.h>
#include <Model/CTags/CTagsCppTypeLoader.h>
#include <list>
using namespace std;
namespace CTags
{
struct MultipleDefinedSymbolException : public std::runtime_error
{
MultipleDefinedSymbolException(const std::string &what)
: runtime_error(what) {}
};
struct UndefinedSymbolException : public std::runtime_error
{
UndefinedSymbolException(const std::string &what)
: runtime_error(what) {}
};
Inheritable& HierarchyLoader::loadHierarchy(Inheritable &type, bool recursive)
{
type.setExtendors(loadSubtypeHierarchy(type, recursive));
type.setParents(loadSupertypeHierarchy(type, recursive));
return type;
}
std::list<smart_ptr<Inheritable> > HierarchyLoader::loadSubtypeHierarchy(const Inheritable &type, bool recursive)
{
return getSubHierarchy(type.getFQN(), recursive);
}
std::list<smart_ptr<Inheritable> > HierarchyLoader::loadSupertypeHierarchy(const Inheritable &type, bool recursive)
{
return getSuperHierarchy(type.getFQN(), recursive);
}
std::list<smart_ptr<Inheritable> > HierarchyLoader::getSuperHierarchy(const std::string &fqn, bool recursive)
{
Tag tag;
try {
tag = getTag(fqn);
} catch(UndefinedSymbolException &) { }
std::list<smart_ptr<Inheritable> > parents;
for(list<string>::const_iterator parent = tag.getInheritance().begin(); parent != tag.getInheritance().end(); ++parent) {
smart_ptr<Inheritable> super;
try {
super = makeRootNode(getTag(*parent));
super->setHierarchyLoader(clone());
if(recursive) {
super->setParents(getSuperHierarchy(*parent, recursive));
}
} catch(UndefinedSymbolException &) {
super = makeUnknown(*parent);
}
parents.push_back(super);
}
return parents;
}
struct InheritsIs : public std::binary_function<Tag, string, bool>
{
result_type operator()(const first_argument_type &tag, const second_argument_type &name) const
{
return find(tag.getInheritance().begin(), tag.getInheritance().end(), name) != tag.getInheritance().end();
}
};
std::list<smart_ptr<Inheritable> > HierarchyLoader::getSubHierarchy(const std::string &fqn, bool recursive)
{
list<Tag> children = getDatabase().selectTagsWhere(make_query(InheritsIs(), fqn));
std::list<smart_ptr<Inheritable> > extendors;
for(list<Tag>::iterator child = children.begin(); child != children.end(); ++child) {
smart_ptr<Inheritable> extendor = makeRootNode(*child);
if(recursive) {
extendor->setExtendors(getSubHierarchy(child->getFQN(), recursive));
}
extendor->setHierarchyLoader(clone());
extendors.push_back(extendor);
}
return extendors;
}
Tag HierarchyLoader::getTag(const std::string &fqn)
{
list<Tag> ctags = getDatabase().selectTagsWhere(make_query(FullQualifiedNameIs(), fqn));
if(ctags.empty()) {
throw UndefinedSymbolException(fqn);
} else if (ctags.size() > 1) {
stringstream ss;
ss << "tag-count " << ctags.size() << "!=1 for tag " << fqn << endl << endl << "found declarations at" << endl;
for(list<Tag>::iterator i = ctags.begin(); i != ctags.end(); i++) {
ss << i->getFQN() << " " << i->getFile() << ":"<< i->getLine() << endl;
}
throw MultipleDefinedSymbolException(ss.str());
}
return ctags.front();
}
smart_ptr<Inheritable> HierarchyLoader::makeRootNode(const Tag &tag)
{
smart_ptr<CppType> inheritable = TypeInfoFactory().createTag(tag);
CppTypeLoader(mWorkspace).load(*inheritable);
return Dynamic_cast<Inheritable>(inheritable);
}
smart_ptr<Inheritable> HierarchyLoader::makeUnknown(const std::string &fqn)
{
smart_ptr<Inheritable> unknown = make_smart_ptr(new Class(fqn, ePublic, TagLocation()));
unknown->setHierarchyLoader(clone());
return unknown;
}
smart_ptr<HierarchyLoader> HierarchyLoader::clone() const
{
return smart_ptr<HierarchyLoader>(new HierarchyLoader(mWorkspace));
}
Database& HierarchyLoader::getDatabase()
{
return *DatabaseManager::instance().getDatabase(mWorkspace);
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.