HeaderIncludeGen.cpp 4.2 KB

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