HeaderIncludeGen.cpp 4.7 KB

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