blob: 3b41d894ec507158e71610e7cf91652eb2c37a28 [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;
Chris Lattner5f9e2722011-07-23 10:55:15 +000020 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 Dunbarda608852011-03-21 19:37:38 +000025 bool ShowDepth;
Daniel Dunbareef63e02011-02-02 15:41:17 +000026
27public:
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000028 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
Chris Lattner5f9e2722011-07-23 10:55:15 +000029 raw_ostream *OutputFile_, bool OwnsOutputFile_,
Daniel Dunbarda608852011-03-21 19:37:38 +000030 bool ShowDepth_)
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000031 : SM(PP->getSourceManager()), OutputFile(OutputFile_),
32 CurrentIncludeDepth(0), HasProcessedPredefines(false),
Daniel Dunbarda608852011-03-21 19:37:38 +000033 OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
34 ShowDepth(ShowDepth_) {}
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000035
36 ~HeaderIncludesCallback() {
37 if (OwnsOutputFile)
Daniel Dunbarf704c612011-02-03 03:45:00 +000038 delete OutputFile;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000039 }
Daniel Dunbareef63e02011-02-02 15:41:17 +000040
41 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +000042 SrcMgr::CharacteristicKind FileType,
43 FileID PrevFID);
Daniel Dunbareef63e02011-02-02 15:41:17 +000044};
45}
46
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000047void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
Chris Lattner5f9e2722011-07-23 10:55:15 +000048 StringRef OutputPath, bool ShowDepth) {
49 raw_ostream *OutputFile = &llvm::errs();
Daniel Dunbarf704c612011-02-03 03:45:00 +000050 bool OwnsOutputFile = false;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000051
52 // Open the output file, if used.
Daniel Dunbarf704c612011-02-03 03:45:00 +000053 if (!OutputPath.empty()) {
54 std::string Error;
55 llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
56 OutputPath.str().c_str(), Error, llvm::raw_fd_ostream::F_Append);
57 if (!Error.empty()) {
58 PP.getDiagnostics().Report(
59 clang::diag::warn_fe_cc_print_header_failure) << Error;
60 delete OS;
61 } else {
62 OS->SetUnbuffered();
63 OS->SetUseAtomicWrites(true);
64 OutputFile = OS;
65 OwnsOutputFile = true;
66 }
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000067 }
68
69 PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
Daniel Dunbarda608852011-03-21 19:37:38 +000070 OutputFile, OwnsOutputFile,
71 ShowDepth));
Daniel Dunbareef63e02011-02-02 15:41:17 +000072}
73
74void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
75 FileChangeReason Reason,
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +000076 SrcMgr::CharacteristicKind NewFileType,
77 FileID PrevFID) {
Daniel Dunbareef63e02011-02-02 15:41:17 +000078 // 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())
82 return;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000083
Daniel Dunbareef63e02011-02-02 15:41:17 +000084 // Adjust the current include depth.
85 if (Reason == PPCallbacks::EnterFile) {
86 ++CurrentIncludeDepth;
Sebastian Redl4dddebf2011-04-14 14:07:45 +000087 } else if (Reason == PPCallbacks::ExitFile) {
Daniel Dunbareef63e02011-02-02 15:41:17 +000088 if (CurrentIncludeDepth)
89 --CurrentIncludeDepth;
90
91 // We track when we are done with the predefines by watching for the first
Sebastian Redl4dddebf2011-04-14 14:07:45 +000092 // place where we drop back to a nesting depth of 1.
93 if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
Daniel Dunbareef63e02011-02-02 15:41:17 +000094 HasProcessedPredefines = true;
Sebastian Redl4dddebf2011-04-14 14:07:45 +000095
96 return;
97 } else
98 return;
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000099
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
102 // line buffers.
103 bool ShowHeader = (HasProcessedPredefines ||
104 (ShowAllHeaders && CurrentIncludeDepth > 2));
105
106 // Dump the header include information we are past the predefines buffer or
107 // are showing all headers.
108 if (ShowHeader && Reason == PPCallbacks::EnterFile) {
Daniel Dunbareef63e02011-02-02 15:41:17 +0000109 // Write to a temporary string to avoid unnecessary flushing on errs().
110 llvm::SmallString<512> Filename(UserLoc.getFilename());
111 Lexer::Stringify(Filename);
112
113 llvm::SmallString<256> Msg;
Daniel Dunbarda608852011-03-21 19:37:38 +0000114 if (ShowDepth) {
Sebastian Redl4dddebf2011-04-14 14:07:45 +0000115 // The main source file is at depth 1, so skip one dot.
116 for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
Daniel Dunbarda608852011-03-21 19:37:38 +0000117 Msg += '.';
118 Msg += ' ';
119 }
Daniel Dunbareef63e02011-02-02 15:41:17 +0000120 Msg += Filename;
121 Msg += '\n';
122
Daniel Dunbarf704c612011-02-03 03:45:00 +0000123 OutputFile->write(Msg.data(), Msg.size());
Daniel Dunbareef63e02011-02-02 15:41:17 +0000124 }
125}