HeaderIncludeGen.cpp 4.4 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 = &llvm::errs();
  47. bool OwnsOutputFile = false;
  48. // Open the output file, if used.
  49. if (!OutputPath.empty()) {
  50. std::string Error;
  51. llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
  52. OutputPath.str().c_str(), Error,
  53. llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
  54. if (!Error.empty()) {
  55. PP.getDiagnostics().Report(
  56. clang::diag::warn_fe_cc_print_header_failure) << Error;
  57. delete OS;
  58. } else {
  59. OS->SetUnbuffered();
  60. OS->SetUseAtomicWrites(true);
  61. OutputFile = OS;
  62. OwnsOutputFile = true;
  63. }
  64. }
  65. PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
  66. OutputFile, OwnsOutputFile,
  67. ShowDepth, MSStyle));
  68. }
  69. void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
  70. FileChangeReason Reason,
  71. SrcMgr::CharacteristicKind NewFileType,
  72. FileID PrevFID) {
  73. // Unless we are exiting a #include, make sure to skip ahead to the line the
  74. // #include directive was at.
  75. PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
  76. if (UserLoc.isInvalid())
  77. return;
  78. // Adjust the current include depth.
  79. if (Reason == PPCallbacks::EnterFile) {
  80. ++CurrentIncludeDepth;
  81. } else if (Reason == PPCallbacks::ExitFile) {
  82. if (CurrentIncludeDepth)
  83. --CurrentIncludeDepth;
  84. // We track when we are done with the predefines by watching for the first
  85. // place where we drop back to a nesting depth of 1.
  86. if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
  87. HasProcessedPredefines = true;
  88. return;
  89. } else
  90. return;
  91. // Show the header if we are (a) past the predefines, or (b) showing all
  92. // headers and in the predefines at a depth past the initial file and command
  93. // line buffers.
  94. bool ShowHeader = (HasProcessedPredefines ||
  95. (ShowAllHeaders && CurrentIncludeDepth > 2));
  96. // Dump the header include information we are past the predefines buffer or
  97. // are showing all headers.
  98. if (ShowHeader && Reason == PPCallbacks::EnterFile) {
  99. // Write to a temporary string to avoid unnecessary flushing on errs().
  100. SmallString<512> Filename(UserLoc.getFilename());
  101. if (!MSStyle)
  102. Lexer::Stringify(Filename);
  103. SmallString<256> Msg;
  104. if (MSStyle)
  105. Msg += "Note: including file:";
  106. if (ShowDepth) {
  107. // The main source file is at depth 1, so skip one dot.
  108. for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
  109. Msg += MSStyle ? ' ' : '.';
  110. if (!MSStyle)
  111. Msg += ' ';
  112. }
  113. Msg += Filename;
  114. Msg += '\n';
  115. OutputFile->write(Msg.data(), Msg.size());
  116. }
  117. }