1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
// This file is part of the AspectC++ compiler 'ac++'.
// Copyright (C) 1999-2003 The 'ac++' developers (see aspectc.org)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307 USA
#include "IncludeGraph.h"
#include "ACFileID.h"
#ifdef FRONTEND_PUMA
#include "Puma/PreTreeNodes.h"
#include "Puma/PreSonIterator.h"
#include "Puma/CTranslationUnit.h"
using namespace Puma;
#else
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Preprocessor.h"
using namespace clang;
#endif
#ifdef FRONTEND_PUMA
void IncludeGraph::init (CTranslationUnit &tunit) {
// search for #includes
iterateNodes (tunit.cpp_tree ());
}
// Go through the nodes.
void IncludeGraph::iterateNodes (PreTree* node) {
PreSonIterator i (node); // the order is important!
for (i.first (); ! i.isDone (); i.next ())
i.currentItem ()->accept (*this);
}
// handle include directive node
void IncludeGraph::visitPreIncludeDirective_Pre (PreIncludeDirective* node) {
// this_unit is the unit where the include directive is located
Token *this_token = node->startToken ();
Unit *this_unit = (Unit*)this_token->belonging_to ();
// cout << "in " << this_unit->name () << " " << this_token->location () << " "
// << _project.isBelow (this_unit) << endl;
// include if expanded by preprocessor
if (node->daughters () == 1) {
Unit *included_unit = ((PreInclSemNode*)node->daughter (0))->unit ();
// cout << " included: " << included_unit->name () << " " <<
// _project.isBelow (included_unit) << endl;
add_edge (this_unit, included_unit);
}
}
#else
void IncludeGraph::IncludeGraphCallback::InclusionDirective(SourceLocation HashLoc,
const clang::Token &IncludeTok,
StringRef FileName,
bool IsAngled,
CharSourceRange FilenameRange,
const FileEntry *File,
StringRef SearchPath,
StringRef RelativePath,
const Module *Imported) {
if (!File)
return;
Preprocessor &PP = _ig._project.get_compiler_instance()->getPreprocessor();
SourceManager &SM = PP.getSourceManager();
const FileEntry *FromFile =
SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(HashLoc)));
if (FromFile == 0)
return;
_ig.add_edge (FromFile, File);
}
#endif
// Add an edge to the include graph from 'a' to 'b'
void IncludeGraph::add_edge (ACFileID a, ACFileID b) {
Node &this_node = find (a);
Node &included_node = find (b);
this_node._includes.insert (&included_node);
}
// find/create an entry in '_nodes'
IncludeGraph::Node &IncludeGraph::find (ACFileID unit) {
std::pair<Map::iterator,bool> p =
_nodes.insert (Map::value_type (unit, Node (unit)));
return p.first->second;
}
// Checks whether on unit 'a' directly or indirecly includes another unit 'b'
bool IncludeGraph::includes (ACFileID a, ACFileID b) const {
Map::const_iterator a_iter = _nodes.find (a);
Map::const_iterator b_iter = _nodes.find (b);
if (a_iter == _nodes.end () || b_iter == _nodes.end ())
return false;
bool result = includes (a_iter->second, b_iter->second);
// reset 'visited' flags
reset_visited ();
return result;
}
void IncludeGraph::reset_visited () const {
// reset 'visited' flags
Map::const_iterator iter = _nodes.begin ();
while (iter != _nodes.end ()) {
const IncludeGraph::Node &node = iter->second;
node._visited = false;
++iter;
}
}
// Checks whether there is a path from node 'a' to 'b' in the include graph
bool IncludeGraph::includes (const Node &a, const Node &b) const {
// cycle detection
if (a._visited)
return false;
set<Node*>::iterator iter = a._includes.begin ();
a._visited = true;
bool found = false;
while (iter != a._includes.end ()) {
if (*iter == &b || includes (**iter, b)) {
found = true;
break;
}
++iter;
}
return found;
}
void IncludeGraph::included_files (const Node &node,
set<ACFileID> &units, bool only_project) const {
// cycle detection
if (node._visited)
return;
set<Node*>::iterator iter = node._includes.begin ();
node._visited = true;
while (iter != node._includes.end ()) {
ACFileID unit = (*iter)->_unit;
if (!only_project || _project.isBelow (
//#ifdef FRONTEND_PUMA
// (Puma::Unit*)unit // TODO: why isn't the argument const?
//#else
unit.name ().c_str () // TODO: using the name might be inefficient
//#endif
))
units.insert (unit);
included_files (**iter, units);
++iter;
}
}
// Get all files the are directly or indirectly included
bool IncludeGraph::included_files (ACFileID unit,
set<ACFileID> &units, bool only_project) const {
Map::const_iterator iter = _nodes.find (unit);
if (iter == _nodes.end ())
return false;
// collect the included file for this node
included_files (iter->second, units, only_project);
// reset 'visited' flags
reset_visited ();
return true;
}
void IncludeGraph::Node::dump () const {
//#ifdef FRONTEND_PUMA
// cout << "unit " /*<< _unit.get_unit ()*/ << " '" << _unit->name () << "' includes:" << endl;
// set<Node*>::iterator iter = _includes.begin ();
// while (iter != _includes.end ()) {
// cout << " " << (*iter)->_unit->name () << endl;
// ++iter;
// }
//#else
std::cout << "unit " /*<< _unit.get_unit ()*/ << " '" << _unit.name () << "' includes:" << std::endl;
std::set<Node*>::iterator iter = _includes.begin ();
while (iter != _includes.end ()) {
std::cout << " " << (*iter)->_unit.name () << std::endl;
++iter;
}
//#endif
}
void IncludeGraph::dump () const {
Map::const_iterator iter = _nodes.begin ();
while (iter != _nodes.end ()) {
const IncludeGraph::Node &node = iter->second;
node.dump ();
++iter;
}
}
|