HeaderIncludeGen.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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(new HeaderIncludesCallback(&PP, ShowAllHeaders,
  65. OutputFile, OwnsOutputFile,
  66. ShowDepth, MSStyle));
  67. }
  68. void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
  69. FileChangeReason Reason,
  70. SrcMgr::CharacteristicKind NewFileType,
  71. FileID PrevFID) {
  72. // Unless we are exiting a #include, make sure to skip ahead to the line the
  73. // #include directive was at.
  74. PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
  75. if (UserLoc.isInvalid())
  76. return;
  77. // Adjust the current include depth.
  78. if (Reason == PPCallbacks::EnterFile) {
  79. ++CurrentIncludeDepth;
  80. } else if (Reason == PPCallbacks::ExitFile) {
  81. if (CurrentIncludeDepth)
  82. --CurrentIncludeDepth;
  83. // We track when we are done with the predefines by watching for the first
  84. // place where we drop back to a nesting depth of 1.
  85. if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
  86. HasProcessedPredefines = true;
  87. return;
  88. } else
  89. return;
  90. // Show the header if we are (a) past the predefines, or (b) showing all
  91. // headers and in the predefines at a depth past the initial file and command
  92. // line buffers.
  93. bool ShowHeader = (HasProcessedPredefines ||
  94. (ShowAllHeaders && CurrentIncludeDepth > 2));
  95. // Dump the header include information we are past the predefines buffer or
  96. // are showing all headers.
  97. if (ShowHeader && Reason == PPCallbacks::EnterFile) {
  98. // Write to a temporary string to avoid unnecessary flushing on errs().
  99. SmallString<512> Filename(UserLoc.getFilename());
  100. if (!MSStyle)
  101. Lexer::Stringify(Filename);
  102. SmallString<256> Msg;
  103. if (MSStyle)
  104. Msg += "Note: including file:";
  105. if (ShowDepth) {
  106. // The main source file is at depth 1, so skip one dot.
  107. for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
  108. Msg += MSStyle ? ' ' : '.';
  109. if (!MSStyle)
  110. Msg += ' ';
  111. }
  112. Msg += Filename;
  113. Msg += '\n';
  114. OutputFile->write(Msg.data(), Msg.size());
  115. OutputFile->flush();
  116. }
  117. }