HeaderIncludeGen.cpp revision 221345
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/Frontend/FrontendDiagnostic.h" 13#include "clang/Lex/Preprocessor.h" 14#include "llvm/Support/raw_ostream.h" 15using namespace clang; 16 17namespace { 18class HeaderIncludesCallback : public PPCallbacks { 19 SourceManager &SM; 20 llvm::raw_ostream *OutputFile; 21 unsigned CurrentIncludeDepth; 22 bool HasProcessedPredefines; 23 bool OwnsOutputFile; 24 bool ShowAllHeaders; 25 bool ShowDepth; 26 27public: 28 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_, 29 llvm::raw_ostream *OutputFile_, bool OwnsOutputFile_, 30 bool ShowDepth_) 31 : SM(PP->getSourceManager()), OutputFile(OutputFile_), 32 CurrentIncludeDepth(0), HasProcessedPredefines(false), 33 OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_), 34 ShowDepth(ShowDepth_) {} 35 36 ~HeaderIncludesCallback() { 37 if (OwnsOutputFile) 38 delete OutputFile; 39 } 40 41 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 42 SrcMgr::CharacteristicKind FileType); 43}; 44} 45 46void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders, 47 llvm::StringRef OutputPath, bool ShowDepth) { 48 llvm::raw_ostream *OutputFile = &llvm::errs(); 49 bool OwnsOutputFile = false; 50 51 // Open the output file, if used. 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 } 66 } 67 68 PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders, 69 OutputFile, OwnsOutputFile, 70 ShowDepth)); 71} 72 73void 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; 81 82 // Adjust the current include depth. 83 if (Reason == PPCallbacks::EnterFile) { 84 ++CurrentIncludeDepth; 85 } else if (Reason == PPCallbacks::ExitFile) { 86 if (CurrentIncludeDepth) 87 --CurrentIncludeDepth; 88 89 // We track when we are done with the predefines by watching for the first 90 // place where we drop back to a nesting depth of 1. 91 if (CurrentIncludeDepth == 1 && !HasProcessedPredefines) 92 HasProcessedPredefines = true; 93 94 return; 95 } else 96 return; 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) { 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; 112 if (ShowDepth) { 113 // The main source file is at depth 1, so skip one dot. 114 for (unsigned i = 1; i != CurrentIncludeDepth; ++i) 115 Msg += '.'; 116 Msg += ' '; 117 } 118 Msg += Filename; 119 Msg += '\n'; 120 121 OutputFile->write(Msg.data(), Msg.size()); 122 } 123} 124