Module.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //===--- Module.h - Describe a module ---------------------------*- 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 defines the Module class, which describes a module in the source
  11. // code.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Basic/Module.h"
  15. #include "clang/Basic/FileManager.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. using namespace clang;
  18. Module::~Module() {
  19. for (llvm::StringMap<Module *>::iterator I = SubModules.begin(),
  20. IEnd = SubModules.end();
  21. I != IEnd; ++I) {
  22. delete I->getValue();
  23. }
  24. }
  25. std::string Module::getFullModuleName() const {
  26. llvm::SmallVector<StringRef, 2> Names;
  27. // Build up the set of module names (from innermost to outermost).
  28. for (const Module *M = this; M; M = M->Parent)
  29. Names.push_back(M->Name);
  30. std::string Result;
  31. for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(),
  32. IEnd = Names.rend();
  33. I != IEnd; ++I) {
  34. if (!Result.empty())
  35. Result += '.';
  36. Result += *I;
  37. }
  38. return Result;
  39. }
  40. StringRef Module::getTopLevelModuleName() const {
  41. const Module *Top = this;
  42. while (Top->Parent)
  43. Top = Top->Parent;
  44. return Top->Name;
  45. }
  46. void Module::print(llvm::raw_ostream &OS, unsigned Indent) const {
  47. OS.indent(Indent);
  48. if (IsFramework)
  49. OS << "framework ";
  50. if (IsExplicit)
  51. OS << "explicit ";
  52. OS << "module " << Name << " {\n";
  53. if (UmbrellaHeader) {
  54. OS.indent(Indent + 2);
  55. OS << "umbrella \"";
  56. OS.write_escaped(UmbrellaHeader->getName());
  57. OS << "\"\n";
  58. }
  59. for (unsigned I = 0, N = Headers.size(); I != N; ++I) {
  60. OS.indent(Indent + 2);
  61. OS << "header \"";
  62. OS.write_escaped(Headers[I]->getName());
  63. OS << "\"\n";
  64. }
  65. for (llvm::StringMap<Module *>::const_iterator MI = SubModules.begin(),
  66. MIEnd = SubModules.end();
  67. MI != MIEnd; ++MI)
  68. MI->getValue()->print(OS, Indent + 2);
  69. OS.indent(Indent);
  70. OS << "}\n";
  71. }
  72. void Module::dump() const {
  73. print(llvm::errs());
  74. }