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" |
| 12 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 13 | using namespace clang; |
| 14 | |
| 15 | namespace { |
| 16 | class HeaderIncludesCallback : public PPCallbacks { |
| 17 | SourceManager &SM; |
| 18 | unsigned CurrentIncludeDepth; |
Daniel Dunbar | b376e5e | 2011-02-02 21:11:24 +0000 | [diff] [blame^] | 19 | bool ShowAllHeaders; |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 20 | bool HasProcessedPredefines; |
| 21 | |
| 22 | public: |
Daniel Dunbar | b376e5e | 2011-02-02 21:11:24 +0000 | [diff] [blame^] | 23 | HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_) |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 24 | : SM(PP->getSourceManager()), CurrentIncludeDepth(0), |
Daniel Dunbar | b376e5e | 2011-02-02 21:11:24 +0000 | [diff] [blame^] | 25 | ShowAllHeaders(ShowAllHeaders_), HasProcessedPredefines(false) {} |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 26 | |
| 27 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 28 | SrcMgr::CharacteristicKind FileType); |
| 29 | }; |
| 30 | } |
| 31 | |
Daniel Dunbar | b376e5e | 2011-02-02 21:11:24 +0000 | [diff] [blame^] | 32 | void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders, |
| 33 | llvm::StringRef OutputPath) { |
| 34 | PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders)); |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void HeaderIncludesCallback::FileChanged(SourceLocation Loc, |
| 38 | FileChangeReason Reason, |
| 39 | SrcMgr::CharacteristicKind NewFileType) { |
| 40 | // Unless we are exiting a #include, make sure to skip ahead to the line the |
| 41 | // #include directive was at. |
| 42 | PresumedLoc UserLoc = SM.getPresumedLoc(Loc); |
| 43 | if (UserLoc.isInvalid()) |
| 44 | return; |
| 45 | |
| 46 | // Adjust the current include depth. |
| 47 | if (Reason == PPCallbacks::EnterFile) { |
| 48 | ++CurrentIncludeDepth; |
| 49 | } else { |
| 50 | if (CurrentIncludeDepth) |
| 51 | --CurrentIncludeDepth; |
| 52 | |
| 53 | // We track when we are done with the predefines by watching for the first |
| 54 | // place where we drop back to a nesting depth of 0. |
| 55 | if (CurrentIncludeDepth == 0 && !HasProcessedPredefines) |
| 56 | HasProcessedPredefines = true; |
| 57 | } |
Daniel Dunbar | b376e5e | 2011-02-02 21:11:24 +0000 | [diff] [blame^] | 58 | |
| 59 | // Show the header if we are (a) past the predefines, or (b) showing all |
| 60 | // headers and in the predefines at a depth past the initial file and command |
| 61 | // line buffers. |
| 62 | bool ShowHeader = (HasProcessedPredefines || |
| 63 | (ShowAllHeaders && CurrentIncludeDepth > 2)); |
| 64 | |
| 65 | // Dump the header include information we are past the predefines buffer or |
| 66 | // are showing all headers. |
| 67 | if (ShowHeader && Reason == PPCallbacks::EnterFile) { |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 68 | // Write to a temporary string to avoid unnecessary flushing on errs(). |
| 69 | llvm::SmallString<512> Filename(UserLoc.getFilename()); |
| 70 | Lexer::Stringify(Filename); |
| 71 | |
| 72 | llvm::SmallString<256> Msg; |
| 73 | for (unsigned i = 0; i != CurrentIncludeDepth; ++i) |
| 74 | Msg += '.'; |
| 75 | Msg += ' '; |
| 76 | Msg += Filename; |
| 77 | Msg += '\n'; |
| 78 | |
| 79 | llvm::errs() << Msg; |
| 80 | } |
| 81 | } |
| 82 | |