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