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; |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 27 | |
| 28 | public: |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 29 | HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 30 | raw_ostream *OutputFile_, bool OwnsOutputFile_, |
Daniel Dunbar | da60885 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 31 | bool ShowDepth_) |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 32 | : SM(PP->getSourceManager()), OutputFile(OutputFile_), |
| 33 | CurrentIncludeDepth(0), HasProcessedPredefines(false), |
Daniel Dunbar | da60885 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 34 | OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_), |
| 35 | ShowDepth(ShowDepth_) {} |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 36 | |
| 37 | ~HeaderIncludesCallback() { |
| 38 | if (OwnsOutputFile) |
Daniel Dunbar | f704c61 | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 39 | delete OutputFile; |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 40 | } |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 41 | |
| 42 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
Argyrios Kyrtzidis | c892c5f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 43 | SrcMgr::CharacteristicKind FileType, |
| 44 | FileID PrevFID); |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 45 | }; |
| 46 | } |
| 47 | |
Daniel Dunbar | b376e5e | 2011-02-02 21:11:24 +0000 | [diff] [blame] | 48 | void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 49 | StringRef OutputPath, bool ShowDepth) { |
| 50 | raw_ostream *OutputFile = &llvm::errs(); |
Daniel Dunbar | f704c61 | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 51 | bool OwnsOutputFile = false; |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 52 | |
| 53 | // Open the output file, if used. |
Daniel Dunbar | f704c61 | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 54 | if (!OutputPath.empty()) { |
| 55 | std::string Error; |
| 56 | llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream( |
| 57 | OutputPath.str().c_str(), Error, llvm::raw_fd_ostream::F_Append); |
| 58 | if (!Error.empty()) { |
| 59 | PP.getDiagnostics().Report( |
| 60 | clang::diag::warn_fe_cc_print_header_failure) << Error; |
| 61 | delete OS; |
| 62 | } else { |
| 63 | OS->SetUnbuffered(); |
| 64 | OS->SetUseAtomicWrites(true); |
| 65 | OutputFile = OS; |
| 66 | OwnsOutputFile = true; |
| 67 | } |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders, |
Daniel Dunbar | da60885 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 71 | OutputFile, OwnsOutputFile, |
| 72 | ShowDepth)); |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void HeaderIncludesCallback::FileChanged(SourceLocation Loc, |
| 76 | FileChangeReason Reason, |
Argyrios Kyrtzidis | c892c5f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 77 | SrcMgr::CharacteristicKind NewFileType, |
| 78 | FileID PrevFID) { |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 79 | // Unless we are exiting a #include, make sure to skip ahead to the line the |
| 80 | // #include directive was at. |
| 81 | PresumedLoc UserLoc = SM.getPresumedLoc(Loc); |
| 82 | if (UserLoc.isInvalid()) |
| 83 | return; |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 84 | |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 85 | // Adjust the current include depth. |
| 86 | if (Reason == PPCallbacks::EnterFile) { |
| 87 | ++CurrentIncludeDepth; |
Sebastian Redl | 4dddebf | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 88 | } else if (Reason == PPCallbacks::ExitFile) { |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 89 | if (CurrentIncludeDepth) |
| 90 | --CurrentIncludeDepth; |
| 91 | |
| 92 | // 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] | 93 | // place where we drop back to a nesting depth of 1. |
| 94 | if (CurrentIncludeDepth == 1 && !HasProcessedPredefines) |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 95 | HasProcessedPredefines = true; |
Sebastian Redl | 4dddebf | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 96 | |
| 97 | return; |
| 98 | } else |
| 99 | return; |
Daniel Dunbar | b376e5e | 2011-02-02 21:11:24 +0000 | [diff] [blame] | 100 | |
| 101 | // Show the header if we are (a) past the predefines, or (b) showing all |
| 102 | // headers and in the predefines at a depth past the initial file and command |
| 103 | // line buffers. |
| 104 | bool ShowHeader = (HasProcessedPredefines || |
| 105 | (ShowAllHeaders && CurrentIncludeDepth > 2)); |
| 106 | |
| 107 | // Dump the header include information we are past the predefines buffer or |
| 108 | // are showing all headers. |
| 109 | if (ShowHeader && Reason == PPCallbacks::EnterFile) { |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 110 | // Write to a temporary string to avoid unnecessary flushing on errs(). |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 111 | SmallString<512> Filename(UserLoc.getFilename()); |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 112 | Lexer::Stringify(Filename); |
| 113 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 114 | SmallString<256> Msg; |
Daniel Dunbar | da60885 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 115 | if (ShowDepth) { |
Sebastian Redl | 4dddebf | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 116 | // The main source file is at depth 1, so skip one dot. |
| 117 | for (unsigned i = 1; i != CurrentIncludeDepth; ++i) |
Daniel Dunbar | da60885 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 118 | Msg += '.'; |
| 119 | Msg += ' '; |
| 120 | } |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 121 | Msg += Filename; |
| 122 | Msg += '\n'; |
| 123 | |
Daniel Dunbar | f704c61 | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 124 | OutputFile->write(Msg.data(), Msg.size()); |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 125 | } |
| 126 | } |