// 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                                            

#ifndef __include_graph_h__
#define __include_graph_h__

// Visitor, which expands all project-local includes 

#include "clang/Lex/PPCallbacks.h"

#include <map>
using std::map;
#include <set>
using std::set;
#include <iostream>
using std::ostream;
using std::endl;

#include "ACFileID.h"
#include "ACProject.h"
#include "version.h"

class IncludeGraph {

  struct Node {
    ACFileID _unit;
    mutable bool _visited; // for cycle detection
    set<Node*> _includes;
    Node (ACFileID u) : _unit (u), _visited (false) {}
    void dump () const;
  };
  
  // associates a node object to each unit
  typedef map<ACFileID, Node> Map;
  Map _nodes;
  
  // the project to which all this belongs
  ACProject &_project;
  
  // find/create an entry in '_nodes'
  Node &find (ACFileID);
  
  // Checks whether there is a path from node 'a' to 'b' in the include graph
  bool includes (const Node &a, const Node &b) const;

  // collect all units included by some node  
  void included_files (const Node &node, set<ACFileID> &units,
    bool only_project = true) const;
  
  // Reset the 'visited' flag of all nodes in the DAG
  void reset_visited () const;
  
public:
  IncludeGraph (ACProject &p) : _project (p) {}
  // Callback object to add includes to the graph during a preprocessor run.
  class IncludeGraphCallback : public clang::PPCallbacks {
    IncludeGraph &_ig;

    // Callback from clang when it sees an inclusion directive.
    virtual void InclusionDirective (clang::SourceLocation HashLoc,
                                     const clang::Token &IncludeTok,
                                     llvm::StringRef FileName,
                                     bool IsAngled,
                                     clang::CharSourceRange FilenameRange,
#if CLANG_VERSION_NUMBER >= VERSION_NUMBER_18_1_3
                                     clang::OptionalFileEntryRef File,
#else
                                     const clang::FileEntry *File,
#endif
                                     llvm::StringRef SearchPath,
                                     llvm::StringRef RelativePath,
#if CLANG_VERSION_NUMBER >= VERSION_NUMBER_19_1_6
                                     const clang::Module *SuggestedModule,
                                     bool ModuleImported,
#else
                                     const clang::Module *Imported,
#endif
                                     clang::SrcMgr::CharacteristicKind FileType
                                    ) override;

  public:
    IncludeGraphCallback (IncludeGraph &ig) : _ig(ig) {}
  };

  // Checks whether on unit 'a' directly or indirecly includes another unit 'b'
  bool includes (ACFileID a, ACFileID b) const;
  
  // Get all files the are directly or indirectly included
  bool included_files (ACFileID unit, set<ACFileID> &units,
    bool only_project = true) const;

  // Add an edge to the include graph from 'a' to 'b'
  void add_edge (ACFileID a, ACFileID b);
  
  // print all nodes
  void dump () const;
};  	    

#endif // __include_graph_h__
