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