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