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 raw_ostream *OutputFile;
21 unsigned CurrentIncludeDepth;
22 bool HasProcessedPredefines;
28 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
29 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,
47 void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
48 StringRef OutputPath, bool ShowDepth) {
49 raw_ostream *OutputFile = &llvm::errs();
50 bool OwnsOutputFile = false;
52 // Open the output file, if used.
53 if (!OutputPath.empty()) {
55 llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
56 OutputPath.str().c_str(), Error, llvm::raw_fd_ostream::F_Append);
58 PP.getDiagnostics().Report(
59 clang::diag::warn_fe_cc_print_header_failure) << Error;
63 OS->SetUseAtomicWrites(true);
65 OwnsOutputFile = true;
69 PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
70 OutputFile, OwnsOutputFile,
74 void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
75 FileChangeReason Reason,
76 SrcMgr::CharacteristicKind NewFileType,
78 // Unless we are exiting a #include, make sure to skip ahead to the line the
79 // #include directive was at.
80 PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
81 if (UserLoc.isInvalid())
84 // Adjust the current include depth.
85 if (Reason == PPCallbacks::EnterFile) {
86 ++CurrentIncludeDepth;
87 } else if (Reason == PPCallbacks::ExitFile) {
88 if (CurrentIncludeDepth)
89 --CurrentIncludeDepth;
91 // We track when we are done with the predefines by watching for the first
92 // place where we drop back to a nesting depth of 1.
93 if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
94 HasProcessedPredefines = true;
100 // Show the header if we are (a) past the predefines, or (b) showing all
101 // headers and in the predefines at a depth past the initial file and command
103 bool ShowHeader = (HasProcessedPredefines ||
104 (ShowAllHeaders && CurrentIncludeDepth > 2));
106 // Dump the header include information we are past the predefines buffer or
107 // are showing all headers.
108 if (ShowHeader && Reason == PPCallbacks::EnterFile) {
109 // Write to a temporary string to avoid unnecessary flushing on errs().
110 llvm::SmallString<512> Filename(UserLoc.getFilename());
111 Lexer::Stringify(Filename);
113 llvm::SmallString<256> Msg;
115 // The main source file is at depth 1, so skip one dot.
116 for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
123 OutputFile->write(Msg.data(), Msg.size());