HeaderIncludeGen.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //===--- HeaderIncludes.cpp - Generate Header Includes --------------------===//
  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. #include "clang/Frontend/Utils.h"
  10. #include "clang/Basic/SourceManager.h"
  11. #include "clang/Frontend/FrontendDiagnostic.h"
  12. #include "clang/Lex/Preprocessor.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. using namespace clang;
  15. namespace {
  16. class HeaderIncludesCallback : public PPCallbacks {
  17. SourceManager &SM;
  18. raw_ostream *OutputFile;
  19. unsigned CurrentIncludeDepth;
  20. bool HasProcessedPredefines;
  21. bool OwnsOutputFile;
  22. bool ShowAllHeaders;
  23. bool ShowDepth;
  24. public:
  25. HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
  26. raw_ostream *OutputFile_, bool OwnsOutputFile_,
  27. bool ShowDepth_)
  28. : SM(PP->getSourceManager()), OutputFile(OutputFile_),
  29. CurrentIncludeDepth(0), HasProcessedPredefines(false),
  30. OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
  31. ShowDepth(ShowDepth_) {}
  32. ~HeaderIncludesCallback() {
  33. if (OwnsOutputFile)
  34. delete OutputFile;
  35. }
  36. virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
  37. SrcMgr::CharacteristicKind FileType,
  38. FileID PrevFID);
  39. };
  40. }
  41. void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
  42. StringRef OutputPath, bool ShowDepth) {
  43. raw_ostream *OutputFile = &llvm::errs();
  44. bool OwnsOutputFile = false;
  45. // Open the output file, if used.
  46. if (!OutputPath.empty()) {
  47. std::string Error;
  48. llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
  49. OutputPath.str().c_str(), Error, llvm::raw_fd_ostream::F_Append);
  50. if (!Error.empty()) {
  51. PP.getDiagnostics().Report(
  52. clang::diag::warn_fe_cc_print_header_failure) << Error;
  53. delete OS;
  54. } else {
  55. OS->SetUnbuffered();
  56. OS->SetUseAtomicWrites(true);
  57. OutputFile = OS;
  58. OwnsOutputFile = true;
  59. }
  60. }
  61. PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
  62. OutputFile, OwnsOutputFile,
  63. ShowDepth));
  64. }
  65. void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
  66. FileChangeReason Reason,
  67. SrcMgr::CharacteristicKind NewFileType,
  68. FileID PrevFID) {
  69. // Unless we are exiting a #include, make sure to skip ahead to the line the
  70. // #include directive was at.
  71. PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
  72. if (UserLoc.isInvalid())
  73. return;
  74. // Adjust the current include depth.
  75. if (Reason == PPCallbacks::EnterFile) {
  76. ++CurrentIncludeDepth;
  77. } else if (Reason == PPCallbacks::ExitFile) {
  78. if (CurrentIncludeDepth)
  79. --CurrentIncludeDepth;
  80. // We track when we are done with the predefines by watching for the first
  81. // place where we drop back to a nesting depth of 1.
  82. if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
  83. HasProcessedPredefines = true;
  84. return;
  85. } else
  86. return;
  87. // Show the header if we are (a) past the predefines, or (b) showing all
  88. // headers and in the predefines at a depth past the initial file and command
  89. // line buffers.
  90. bool ShowHeader = (HasProcessedPredefines ||
  91. (ShowAllHeaders && CurrentIncludeDepth > 2));
  92. // Dump the header include information we are past the predefines buffer or
  93. // are showing all headers.
  94. if (ShowHeader && Reason == PPCallbacks::EnterFile) {
  95. // Write to a temporary string to avoid unnecessary flushing on errs().
  96. llvm::SmallString<512> Filename(UserLoc.getFilename());
  97. Lexer::Stringify(Filename);
  98. llvm::SmallString<256> Msg;
  99. if (ShowDepth) {
  100. // The main source file is at depth 1, so skip one dot.
  101. for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
  102. Msg += '.';
  103. Msg += ' ';
  104. }
  105. Msg += Filename;
  106. Msg += '\n';
  107. OutputFile->write(Msg.data(), Msg.size());
  108. }
  109. }