blob: 45ff1d2e412f3ec1817d2f68dfcd3bf87979b470 [file] [log] [blame]
Daniel Dunbareef63e02011-02-02 15:41:17 +00001//===--- 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"
Daniel Dunbarf704c612011-02-03 03:45:00 +000012#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbareef63e02011-02-02 15:41:17 +000013#include "clang/Lex/Preprocessor.h"
Daniel Dunbarf704c612011-02-03 03:45:00 +000014#include "llvm/Support/raw_ostream.h"
Daniel Dunbareef63e02011-02-02 15:41:17 +000015using namespace clang;
16
17namespace {
18class HeaderIncludesCallback : public PPCallbacks {
19 SourceManager &SM;
Daniel Dunbarf704c612011-02-03 03:45:00 +000020 llvm::raw_ostream *OutputFile;
Daniel Dunbareef63e02011-02-02 15:41:17 +000021 unsigned CurrentIncludeDepth;
22 bool HasProcessedPredefines;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000023 bool OwnsOutputFile;
24 bool ShowAllHeaders;
Daniel Dunbareef63e02011-02-02 15:41:17 +000025
26public:
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000027 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
Daniel Dunbarf704c612011-02-03 03:45:00 +000028 llvm::raw_ostream *OutputFile_, bool OwnsOutputFile_)
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000029 : SM(PP->getSourceManager()), OutputFile(OutputFile_),
30 CurrentIncludeDepth(0), HasProcessedPredefines(false),
31 OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_) {}
32
33 ~HeaderIncludesCallback() {
34 if (OwnsOutputFile)
Daniel Dunbarf704c612011-02-03 03:45:00 +000035 delete OutputFile;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000036 }
Daniel Dunbareef63e02011-02-02 15:41:17 +000037
38 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
39 SrcMgr::CharacteristicKind FileType);
40};
41}
42
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000043void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
44 llvm::StringRef OutputPath) {
Daniel Dunbarf704c612011-02-03 03:45:00 +000045 llvm::raw_ostream *OutputFile = &llvm::errs();
46 bool OwnsOutputFile = false;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000047
48 // Open the output file, if used.
Daniel Dunbarf704c612011-02-03 03:45:00 +000049 if (!OutputPath.empty()) {
50 std::string Error;
51 llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
52 OutputPath.str().c_str(), Error, llvm::raw_fd_ostream::F_Append);
53 if (!Error.empty()) {
54 PP.getDiagnostics().Report(
55 clang::diag::warn_fe_cc_print_header_failure) << Error;
56 delete OS;
57 } else {
58 OS->SetUnbuffered();
59 OS->SetUseAtomicWrites(true);
60 OutputFile = OS;
61 OwnsOutputFile = true;
62 }
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000063 }
64
65 PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
66 OutputFile, OwnsOutputFile));
Daniel Dunbareef63e02011-02-02 15:41:17 +000067}
68
69void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
70 FileChangeReason Reason,
71 SrcMgr::CharacteristicKind NewFileType) {
72 // Unless we are exiting a #include, make sure to skip ahead to the line the
73 // #include directive was at.
74 PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
75 if (UserLoc.isInvalid())
76 return;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000077
Daniel Dunbareef63e02011-02-02 15:41:17 +000078 // Adjust the current include depth.
79 if (Reason == PPCallbacks::EnterFile) {
80 ++CurrentIncludeDepth;
81 } else {
82 if (CurrentIncludeDepth)
83 --CurrentIncludeDepth;
84
85 // We track when we are done with the predefines by watching for the first
86 // place where we drop back to a nesting depth of 0.
87 if (CurrentIncludeDepth == 0 && !HasProcessedPredefines)
88 HasProcessedPredefines = true;
89 }
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000090
91 // Show the header if we are (a) past the predefines, or (b) showing all
92 // headers and in the predefines at a depth past the initial file and command
93 // line buffers.
94 bool ShowHeader = (HasProcessedPredefines ||
95 (ShowAllHeaders && CurrentIncludeDepth > 2));
96
97 // Dump the header include information we are past the predefines buffer or
98 // are showing all headers.
99 if (ShowHeader && Reason == PPCallbacks::EnterFile) {
Daniel Dunbareef63e02011-02-02 15:41:17 +0000100 // Write to a temporary string to avoid unnecessary flushing on errs().
101 llvm::SmallString<512> Filename(UserLoc.getFilename());
102 Lexer::Stringify(Filename);
103
104 llvm::SmallString<256> Msg;
105 for (unsigned i = 0; i != CurrentIncludeDepth; ++i)
106 Msg += '.';
107 Msg += ' ';
108 Msg += Filename;
109 Msg += '\n';
110
Daniel Dunbarf704c612011-02-03 03:45:00 +0000111 OutputFile->write(Msg.data(), Msg.size());
Daniel Dunbareef63e02011-02-02 15:41:17 +0000112 }
113}