HeaderIncludeGen.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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() override {
  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. static void PrintHeaderInfo(raw_ostream *OutputFile, const char* Filename,
  44. bool ShowDepth, unsigned CurrentIncludeDepth,
  45. bool MSStyle) {
  46. // Write to a temporary string to avoid unnecessary flushing on errs().
  47. SmallString<512> Pathname(Filename);
  48. if (!MSStyle)
  49. Lexer::Stringify(Pathname);
  50. SmallString<256> Msg;
  51. if (MSStyle)
  52. Msg += "Note: including file:";
  53. if (ShowDepth) {
  54. // The main source file is at depth 1, so skip one dot.
  55. for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
  56. Msg += MSStyle ? ' ' : '.';
  57. if (!MSStyle)
  58. Msg += ' ';
  59. }
  60. Msg += Pathname;
  61. Msg += '\n';
  62. OutputFile->write(Msg.data(), Msg.size());
  63. OutputFile->flush();
  64. }
  65. void clang::AttachHeaderIncludeGen(Preprocessor &PP,
  66. const std::vector<std::string> &ExtraHeaders,
  67. bool ShowAllHeaders,
  68. StringRef OutputPath, bool ShowDepth,
  69. bool MSStyle) {
  70. raw_ostream *OutputFile = MSStyle ? &llvm::outs() : &llvm::errs();
  71. bool OwnsOutputFile = false;
  72. // Open the output file, if used.
  73. if (!OutputPath.empty()) {
  74. std::error_code EC;
  75. llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
  76. OutputPath.str(), EC, llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
  77. if (EC) {
  78. PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure)
  79. << EC.message();
  80. delete OS;
  81. } else {
  82. OS->SetUnbuffered();
  83. OS->SetUseAtomicWrites(true);
  84. OutputFile = OS;
  85. OwnsOutputFile = true;
  86. }
  87. }
  88. // Print header info for extra headers, pretending they were discovered
  89. // by the regular preprocessor. The primary use case is to support
  90. // proper generation of Make / Ninja file dependencies for implicit includes,
  91. // such as sanitizer blacklists. It's only important for cl.exe
  92. // compatibility, the GNU way to generate rules is -M / -MM / -MD / -MMD.
  93. for (auto Header : ExtraHeaders) {
  94. PrintHeaderInfo(OutputFile, Header.c_str(), ShowDepth, 2, MSStyle);
  95. }
  96. PP.addPPCallbacks(llvm::make_unique<HeaderIncludesCallback>(&PP,
  97. ShowAllHeaders,
  98. OutputFile,
  99. OwnsOutputFile,
  100. ShowDepth,
  101. MSStyle));
  102. }
  103. void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
  104. FileChangeReason Reason,
  105. SrcMgr::CharacteristicKind NewFileType,
  106. FileID PrevFID) {
  107. // Unless we are exiting a #include, make sure to skip ahead to the line the
  108. // #include directive was at.
  109. PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
  110. if (UserLoc.isInvalid())
  111. return;
  112. // Adjust the current include depth.
  113. if (Reason == PPCallbacks::EnterFile) {
  114. ++CurrentIncludeDepth;
  115. } else if (Reason == PPCallbacks::ExitFile) {
  116. if (CurrentIncludeDepth)
  117. --CurrentIncludeDepth;
  118. // We track when we are done with the predefines by watching for the first
  119. // place where we drop back to a nesting depth of 1.
  120. if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
  121. HasProcessedPredefines = true;
  122. return;
  123. } else
  124. return;
  125. // Show the header if we are (a) past the predefines, or (b) showing all
  126. // headers and in the predefines at a depth past the initial file and command
  127. // line buffers.
  128. bool ShowHeader = (HasProcessedPredefines ||
  129. (ShowAllHeaders && CurrentIncludeDepth > 2));
  130. // Dump the header include information we are past the predefines buffer or
  131. // are showing all headers.
  132. if (ShowHeader && Reason == PPCallbacks::EnterFile) {
  133. PrintHeaderInfo(OutputFile, UserLoc.getFilename(),
  134. ShowDepth, CurrentIncludeDepth, MSStyle);
  135. }
  136. }