1 //===--- HeaderIncludes.cpp - Generate Header Includes --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
15 using namespace clang;
18 class HeaderIncludesCallback : public PPCallbacks {
20 llvm::raw_ostream *OutputFile;
21 unsigned CurrentIncludeDepth;
22 bool HasProcessedPredefines;
28 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
29 llvm::raw_ostream *OutputFile_, bool OwnsOutputFile_,
31 : SM(PP->getSourceManager()), OutputFile(OutputFile_),
32 CurrentIncludeDepth(0), HasProcessedPredefines(false),
33 OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
34 ShowDepth(ShowDepth_) {}
36 ~HeaderIncludesCallback() {
41 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
42 SrcMgr::CharacteristicKind FileType);
46 void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
47 llvm::StringRef OutputPath, bool ShowDepth) {
48 llvm::raw_ostream *OutputFile = &llvm::errs();
49 bool OwnsOutputFile = false;
51 // Open the output file, if used.
52 if (!OutputPath.empty()) {
54 llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
55 OutputPath.str().c_str(), Error, llvm::raw_fd_ostream::F_Append);
57 PP.getDiagnostics().Report(
58 clang::diag::warn_fe_cc_print_header_failure) << Error;
62 OS->SetUseAtomicWrites(true);
64 OwnsOutputFile = true;
68 PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
69 OutputFile, OwnsOutputFile,
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())
82 // Adjust the current include depth.
83 if (Reason == PPCallbacks::EnterFile) {
84 ++CurrentIncludeDepth;
85 } else if (Reason == PPCallbacks::ExitFile) {
86 if (CurrentIncludeDepth)
87 --CurrentIncludeDepth;
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;
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
101 bool ShowHeader = (HasProcessedPredefines ||
102 (ShowAllHeaders && CurrentIncludeDepth > 2));
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);
111 llvm::SmallString<256> Msg;
113 // The main source file is at depth 1, so skip one dot.
114 for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
121 OutputFile->write(Msg.data(), Msg.size());