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()) { |
| 57 | std::string Error; |
| 58 | llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream( |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 59 | OutputPath.str().c_str(), Error, |
| 60 | llvm::sys::fs::F_Append | llvm::sys::fs::F_Text); |
Daniel Dunbar | f704c61 | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 61 | if (!Error.empty()) { |
| 62 | PP.getDiagnostics().Report( |
| 63 | clang::diag::warn_fe_cc_print_header_failure) << Error; |
| 64 | delete OS; |
| 65 | } else { |
| 66 | OS->SetUnbuffered(); |
| 67 | OS->SetUseAtomicWrites(true); |
| 68 | OutputFile = OS; |
| 69 | OwnsOutputFile = true; |
| 70 | } |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders, |
Daniel Dunbar | da60885 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 74 | OutputFile, OwnsOutputFile, |
Hans Wennborg | 708002e | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 75 | ShowDepth, MSStyle)); |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void HeaderIncludesCallback::FileChanged(SourceLocation Loc, |
| 79 | FileChangeReason Reason, |
Argyrios Kyrtzidis | c892c5f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 80 | SrcMgr::CharacteristicKind NewFileType, |
| 81 | FileID PrevFID) { |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 82 | // Unless we are exiting a #include, make sure to skip ahead to the line the |
| 83 | // #include directive was at. |
| 84 | PresumedLoc UserLoc = SM.getPresumedLoc(Loc); |
| 85 | if (UserLoc.isInvalid()) |
| 86 | return; |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 87 | |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 88 | // Adjust the current include depth. |
| 89 | if (Reason == PPCallbacks::EnterFile) { |
| 90 | ++CurrentIncludeDepth; |
Sebastian Redl | 4dddebf | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 91 | } else if (Reason == PPCallbacks::ExitFile) { |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 92 | if (CurrentIncludeDepth) |
| 93 | --CurrentIncludeDepth; |
| 94 | |
| 95 | // 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] | 96 | // place where we drop back to a nesting depth of 1. |
| 97 | if (CurrentIncludeDepth == 1 && !HasProcessedPredefines) |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 98 | HasProcessedPredefines = true; |
Sebastian Redl | 4dddebf | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 99 | |
| 100 | return; |
| 101 | } else |
| 102 | return; |
Daniel Dunbar | b376e5e | 2011-02-02 21:11:24 +0000 | [diff] [blame] | 103 | |
| 104 | // Show the header if we are (a) past the predefines, or (b) showing all |
| 105 | // headers and in the predefines at a depth past the initial file and command |
| 106 | // line buffers. |
| 107 | bool ShowHeader = (HasProcessedPredefines || |
| 108 | (ShowAllHeaders && CurrentIncludeDepth > 2)); |
| 109 | |
| 110 | // Dump the header include information we are past the predefines buffer or |
| 111 | // are showing all headers. |
| 112 | if (ShowHeader && Reason == PPCallbacks::EnterFile) { |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 113 | // Write to a temporary string to avoid unnecessary flushing on errs(). |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 114 | SmallString<512> Filename(UserLoc.getFilename()); |
Hans Wennborg | 708002e | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 115 | if (!MSStyle) |
| 116 | Lexer::Stringify(Filename); |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 117 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 118 | SmallString<256> Msg; |
Hans Wennborg | 708002e | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 119 | if (MSStyle) |
| 120 | Msg += "Note: including file:"; |
| 121 | |
Daniel Dunbar | da60885 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 122 | if (ShowDepth) { |
Sebastian Redl | 4dddebf | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 123 | // The main source file is at depth 1, so skip one dot. |
| 124 | for (unsigned i = 1; i != CurrentIncludeDepth; ++i) |
Hans Wennborg | 708002e | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 125 | Msg += MSStyle ? ' ' : '.'; |
| 126 | |
| 127 | if (!MSStyle) |
| 128 | Msg += ' '; |
Daniel Dunbar | da60885 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 129 | } |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 130 | Msg += Filename; |
| 131 | Msg += '\n'; |
| 132 | |
Daniel Dunbar | f704c61 | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 133 | OutputFile->write(Msg.data(), Msg.size()); |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 134 | } |
| 135 | } |