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/ADT/SmallString.h"
15#include "llvm/Support/raw_ostream.h"
16using namespace clang;
17
18namespace {
19class HeaderIncludesCallback : public PPCallbacks {
20  SourceManager &SM;
21  raw_ostream *OutputFile;
22  unsigned CurrentIncludeDepth;
23  bool HasProcessedPredefines;
24  bool OwnsOutputFile;
25  bool ShowAllHeaders;
26  bool ShowDepth;
27
28public:
29  HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
30                         raw_ostream *OutputFile_, bool OwnsOutputFile_,
31                         bool ShowDepth_)
32    : SM(PP->getSourceManager()), OutputFile(OutputFile_),
33      CurrentIncludeDepth(0), HasProcessedPredefines(false),
34      OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
35      ShowDepth(ShowDepth_) {}
36
37  ~HeaderIncludesCallback() {
38    if (OwnsOutputFile)
39      delete OutputFile;
40  }
41
42  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
43                           SrcMgr::CharacteristicKind FileType,
44                           FileID PrevFID);
45};
46}
47
48void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
49                                   StringRef OutputPath, bool ShowDepth) {
50  raw_ostream *OutputFile = &llvm::errs();
51  bool OwnsOutputFile = false;
52
53  // Open the output file, if used.
54  if (!OutputPath.empty()) {
55    std::string Error;
56    llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
57      OutputPath.str().c_str(), Error, llvm::raw_fd_ostream::F_Append);
58    if (!Error.empty()) {
59      PP.getDiagnostics().Report(
60        clang::diag::warn_fe_cc_print_header_failure) << Error;
61      delete OS;
62    } else {
63      OS->SetUnbuffered();
64      OS->SetUseAtomicWrites(true);
65      OutputFile = OS;
66      OwnsOutputFile = true;
67    }
68  }
69
70  PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
71                                               OutputFile, OwnsOutputFile,
72                                               ShowDepth));
73}
74
75void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
76                                         FileChangeReason Reason,
77                                       SrcMgr::CharacteristicKind NewFileType,
78                                       FileID PrevFID) {
79  // Unless we are exiting a #include, make sure to skip ahead to the line the
80  // #include directive was at.
81  PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
82  if (UserLoc.isInvalid())
83    return;
84
85  // Adjust the current include depth.
86  if (Reason == PPCallbacks::EnterFile) {
87    ++CurrentIncludeDepth;
88  } else if (Reason == PPCallbacks::ExitFile) {
89    if (CurrentIncludeDepth)
90      --CurrentIncludeDepth;
91
92    // We track when we are done with the predefines by watching for the first
93    // place where we drop back to a nesting depth of 1.
94    if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
95      HasProcessedPredefines = true;
96
97    return;
98  } else
99    return;
100
101  // Show the header if we are (a) past the predefines, or (b) showing all
102  // headers and in the predefines at a depth past the initial file and command
103  // line buffers.
104  bool ShowHeader = (HasProcessedPredefines ||
105                     (ShowAllHeaders && CurrentIncludeDepth > 2));
106
107  // Dump the header include information we are past the predefines buffer or
108  // are showing all headers.
109  if (ShowHeader && Reason == PPCallbacks::EnterFile) {
110    // Write to a temporary string to avoid unnecessary flushing on errs().
111    SmallString<512> Filename(UserLoc.getFilename());
112    Lexer::Stringify(Filename);
113
114    SmallString<256> Msg;
115    if (ShowDepth) {
116      // The main source file is at depth 1, so skip one dot.
117      for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
118        Msg += '.';
119      Msg += ' ';
120    }
121    Msg += Filename;
122    Msg += '\n';
123
124    OutputFile->write(Msg.data(), Msg.size());
125  }
126}
127