HeaderIncludeGen.cpp 3.8 KB

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