Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 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 | |
| 10 | #include "clang/Frontend/Utils.h" |
| 11 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | f704c61 | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 12 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 13 | #include "clang/Lex/Preprocessor.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallString.h" |
Daniel Dunbar | f704c61 | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 16 | using namespace clang; |
| 17 | |
| 18 | namespace { |
| 19 | class HeaderIncludesCallback : public PPCallbacks { |
| 20 | SourceManager &SM; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 21 | raw_ostream *OutputFile; |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 22 | unsigned CurrentIncludeDepth; |
| 23 | bool HasProcessedPredefines; |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 24 | bool OwnsOutputFile; |
| 25 | bool ShowAllHeaders; |
Daniel Dunbar | da60885 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 26 | bool ShowDepth; |
Hans Wennborg | 708002e | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 27 | bool MSStyle; |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 28 | |
| 29 | public: |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 30 | HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 31 | raw_ostream *OutputFile_, bool OwnsOutputFile_, |
Hans Wennborg | 708002e | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 32 | bool ShowDepth_, bool MSStyle_) |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 33 | : SM(PP->getSourceManager()), OutputFile(OutputFile_), |
| 34 | CurrentIncludeDepth(0), HasProcessedPredefines(false), |
Daniel Dunbar | da60885 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 35 | OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_), |
Hans Wennborg | 708002e | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 36 | ShowDepth(ShowDepth_), MSStyle(MSStyle_) {} |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 37 | |
| 38 | ~HeaderIncludesCallback() { |
| 39 | if (OwnsOutputFile) |
Daniel Dunbar | f704c61 | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 40 | delete OutputFile; |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 41 | } |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 42 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 43 | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 44 | SrcMgr::CharacteristicKind FileType, |
| 45 | FileID PrevFID) override; |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 46 | }; |
| 47 | } |
| 48 | |
Daniel Dunbar | b376e5e | 2011-02-02 21:11:24 +0000 | [diff] [blame] | 49 | void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders, |
Hans Wennborg | 708002e | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 50 | StringRef OutputPath, bool ShowDepth, |
| 51 | bool MSStyle) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 52 | raw_ostream *OutputFile = MSStyle ? &llvm::outs() : &llvm::errs(); |
Daniel Dunbar | f704c61 | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 53 | bool OwnsOutputFile = false; |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 54 | |
| 55 | // Open the output file, if used. |
Daniel Dunbar | f704c61 | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 56 | if (!OutputPath.empty()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 57 | std::error_code EC; |
Daniel Dunbar | f704c61 | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 58 | llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream( |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 59 | OutputPath.str(), EC, llvm::sys::fs::F_Append | llvm::sys::fs::F_Text); |
| 60 | if (EC) { |
| 61 | PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure) |
| 62 | << EC.message(); |
Daniel Dunbar | f704c61 | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 63 | delete OS; |
| 64 | } else { |
| 65 | OS->SetUnbuffered(); |
| 66 | OS->SetUseAtomicWrites(true); |
| 67 | OutputFile = OS; |
| 68 | OwnsOutputFile = true; |
| 69 | } |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 72 | PP.addPPCallbacks(llvm::make_unique<HeaderIncludesCallback>(&PP, |
| 73 | ShowAllHeaders, |
| 74 | OutputFile, |
| 75 | OwnsOutputFile, |
| 76 | ShowDepth, |
| 77 | MSStyle)); |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void HeaderIncludesCallback::FileChanged(SourceLocation Loc, |
| 81 | FileChangeReason Reason, |
Argyrios Kyrtzidis | c892c5f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 82 | SrcMgr::CharacteristicKind NewFileType, |
| 83 | FileID PrevFID) { |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 84 | // Unless we are exiting a #include, make sure to skip ahead to the line the |
| 85 | // #include directive was at. |
| 86 | PresumedLoc UserLoc = SM.getPresumedLoc(Loc); |
| 87 | if (UserLoc.isInvalid()) |
| 88 | return; |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 89 | |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 90 | // Adjust the current include depth. |
| 91 | if (Reason == PPCallbacks::EnterFile) { |
| 92 | ++CurrentIncludeDepth; |
Sebastian Redl | 4dddebf | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 93 | } else if (Reason == PPCallbacks::ExitFile) { |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 94 | if (CurrentIncludeDepth) |
| 95 | --CurrentIncludeDepth; |
| 96 | |
| 97 | // We track when we are done with the predefines by watching for the first |
Sebastian Redl | 4dddebf | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 98 | // place where we drop back to a nesting depth of 1. |
| 99 | if (CurrentIncludeDepth == 1 && !HasProcessedPredefines) |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 100 | HasProcessedPredefines = true; |
Sebastian Redl | 4dddebf | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 101 | |
| 102 | return; |
| 103 | } else |
| 104 | return; |
Daniel Dunbar | b376e5e | 2011-02-02 21:11:24 +0000 | [diff] [blame] | 105 | |
| 106 | // Show the header if we are (a) past the predefines, or (b) showing all |
| 107 | // headers and in the predefines at a depth past the initial file and command |
| 108 | // line buffers. |
| 109 | bool ShowHeader = (HasProcessedPredefines || |
| 110 | (ShowAllHeaders && CurrentIncludeDepth > 2)); |
| 111 | |
| 112 | // Dump the header include information we are past the predefines buffer or |
| 113 | // are showing all headers. |
| 114 | if (ShowHeader && Reason == PPCallbacks::EnterFile) { |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 115 | // Write to a temporary string to avoid unnecessary flushing on errs(). |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 116 | SmallString<512> Filename(UserLoc.getFilename()); |
Hans Wennborg | 708002e | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 117 | if (!MSStyle) |
| 118 | Lexer::Stringify(Filename); |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 119 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 120 | SmallString<256> Msg; |
Hans Wennborg | 708002e | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 121 | if (MSStyle) |
| 122 | Msg += "Note: including file:"; |
| 123 | |
Daniel Dunbar | da60885 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 124 | if (ShowDepth) { |
Sebastian Redl | 4dddebf | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 125 | // The main source file is at depth 1, so skip one dot. |
| 126 | for (unsigned i = 1; i != CurrentIncludeDepth; ++i) |
Hans Wennborg | 708002e | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 127 | Msg += MSStyle ? ' ' : '.'; |
| 128 | |
| 129 | if (!MSStyle) |
| 130 | Msg += ' '; |
Daniel Dunbar | da60885 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 131 | } |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 132 | Msg += Filename; |
| 133 | Msg += '\n'; |
| 134 | |
Daniel Dunbar | f704c61 | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 135 | OutputFile->write(Msg.data(), Msg.size()); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 136 | OutputFile->flush(); |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 137 | } |
| 138 | } |