HeaderIncludeGen.cpp revision da60885cb4ee85d167cf704061f10b758067cf5a
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 { 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 0. 91 if (CurrentIncludeDepth == 0 && !HasProcessedPredefines) 92 HasProcessedPredefines = true; 93 } 94 95 // Show the header if we are (a) past the predefines, or (b) showing all 96 // headers and in the predefines at a depth past the initial file and command 97 // line buffers. 98 bool ShowHeader = (HasProcessedPredefines || 99 (ShowAllHeaders && CurrentIncludeDepth > 2)); 100 101 // Dump the header include information we are past the predefines buffer or 102 // are showing all headers. 103 if (ShowHeader && Reason == PPCallbacks::EnterFile) { 104 // Write to a temporary string to avoid unnecessary flushing on errs(). 105 llvm::SmallString<512> Filename(UserLoc.getFilename()); 106 Lexer::Stringify(Filename); 107 108 llvm::SmallString<256> Msg; 109 if (ShowDepth) { 110 for (unsigned i = 0; i != CurrentIncludeDepth; ++i) 111 Msg += '.'; 112 Msg += ' '; 113 } 114 Msg += Filename; 115 Msg += '\n'; 116 117 OutputFile->write(Msg.data(), Msg.size()); 118 } 119} 120