blob: 27011945712a0a1b5fde1ff55f758db84e1720f0 [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"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000014#include "llvm/ADT/SmallString.h"
Daniel Dunbarf704c612011-02-03 03:45:00 +000015#include "llvm/Support/raw_ostream.h"
Daniel Dunbareef63e02011-02-02 15:41:17 +000016using namespace clang;
17
18namespace {
19class HeaderIncludesCallback : public PPCallbacks {
20 SourceManager &SM;
Chris Lattner5f9e2722011-07-23 10:55:15 +000021 raw_ostream *OutputFile;
Daniel Dunbareef63e02011-02-02 15:41:17 +000022 unsigned CurrentIncludeDepth;
23 bool HasProcessedPredefines;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000024 bool OwnsOutputFile;
25 bool ShowAllHeaders;
Daniel Dunbarda608852011-03-21 19:37:38 +000026 bool ShowDepth;
Hans Wennborg708002e2013-08-09 00:32:23 +000027 bool MSStyle;
Daniel Dunbareef63e02011-02-02 15:41:17 +000028
29public:
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000030 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
Chris Lattner5f9e2722011-07-23 10:55:15 +000031 raw_ostream *OutputFile_, bool OwnsOutputFile_,
Hans Wennborg708002e2013-08-09 00:32:23 +000032 bool ShowDepth_, bool MSStyle_)
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000033 : SM(PP->getSourceManager()), OutputFile(OutputFile_),
34 CurrentIncludeDepth(0), HasProcessedPredefines(false),
Daniel Dunbarda608852011-03-21 19:37:38 +000035 OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
Hans Wennborg708002e2013-08-09 00:32:23 +000036 ShowDepth(ShowDepth_), MSStyle(MSStyle_) {}
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000037
38 ~HeaderIncludesCallback() {
39 if (OwnsOutputFile)
Daniel Dunbarf704c612011-02-03 03:45:00 +000040 delete OutputFile;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000041 }
Daniel Dunbareef63e02011-02-02 15:41:17 +000042
Stephen Hines651f13c2014-04-23 16:59:28 -070043 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
44 SrcMgr::CharacteristicKind FileType,
45 FileID PrevFID) override;
Daniel Dunbareef63e02011-02-02 15:41:17 +000046};
47}
48
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000049void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
Hans Wennborg708002e2013-08-09 00:32:23 +000050 StringRef OutputPath, bool ShowDepth,
51 bool MSStyle) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -070052 raw_ostream *OutputFile = MSStyle ? &llvm::outs() : &llvm::errs();
Daniel Dunbarf704c612011-02-03 03:45:00 +000053 bool OwnsOutputFile = false;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000054
55 // Open the output file, if used.
Daniel Dunbarf704c612011-02-03 03:45:00 +000056 if (!OutputPath.empty()) {
Stephen Hines176edba2014-12-01 14:53:08 -080057 std::error_code EC;
Daniel Dunbarf704c612011-02-03 03:45:00 +000058 llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
Stephen Hines176edba2014-12-01 14:53:08 -080059 OutputPath.str(), EC, llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
60 if (EC) {
61 PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure)
62 << EC.message();
Daniel Dunbarf704c612011-02-03 03:45:00 +000063 delete OS;
64 } else {
65 OS->SetUnbuffered();
66 OS->SetUseAtomicWrites(true);
67 OutputFile = OS;
68 OwnsOutputFile = true;
69 }
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000070 }
71
Stephen Hines176edba2014-12-01 14:53:08 -080072 PP.addPPCallbacks(llvm::make_unique<HeaderIncludesCallback>(&PP,
73 ShowAllHeaders,
74 OutputFile,
75 OwnsOutputFile,
76 ShowDepth,
77 MSStyle));
Daniel Dunbareef63e02011-02-02 15:41:17 +000078}
79
80void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
81 FileChangeReason Reason,
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +000082 SrcMgr::CharacteristicKind NewFileType,
83 FileID PrevFID) {
Daniel Dunbareef63e02011-02-02 15:41:17 +000084 // Unless we are exiting a #include, make sure to skip ahead to the line the
85 // #include directive was at.
86 PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
87 if (UserLoc.isInvalid())
88 return;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000089
Daniel Dunbareef63e02011-02-02 15:41:17 +000090 // Adjust the current include depth.
91 if (Reason == PPCallbacks::EnterFile) {
92 ++CurrentIncludeDepth;
Sebastian Redl4dddebf2011-04-14 14:07:45 +000093 } else if (Reason == PPCallbacks::ExitFile) {
Daniel Dunbareef63e02011-02-02 15:41:17 +000094 if (CurrentIncludeDepth)
95 --CurrentIncludeDepth;
96
97 // We track when we are done with the predefines by watching for the first
Sebastian Redl4dddebf2011-04-14 14:07:45 +000098 // place where we drop back to a nesting depth of 1.
99 if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
Daniel Dunbareef63e02011-02-02 15:41:17 +0000100 HasProcessedPredefines = true;
Sebastian Redl4dddebf2011-04-14 14:07:45 +0000101
102 return;
103 } else
104 return;
Daniel Dunbarb376e5e2011-02-02 21:11:24 +0000105
106 // Show the header if we are (a) past the predefines, or (b) showing all
107 // headers and in the predefines at a depth past the initial file and command
108 // line buffers.
109 bool ShowHeader = (HasProcessedPredefines ||
110 (ShowAllHeaders && CurrentIncludeDepth > 2));
111
112 // Dump the header include information we are past the predefines buffer or
113 // are showing all headers.
114 if (ShowHeader && Reason == PPCallbacks::EnterFile) {
Daniel Dunbareef63e02011-02-02 15:41:17 +0000115 // Write to a temporary string to avoid unnecessary flushing on errs().
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000116 SmallString<512> Filename(UserLoc.getFilename());
Hans Wennborg708002e2013-08-09 00:32:23 +0000117 if (!MSStyle)
118 Lexer::Stringify(Filename);
Daniel Dunbareef63e02011-02-02 15:41:17 +0000119
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000120 SmallString<256> Msg;
Hans Wennborg708002e2013-08-09 00:32:23 +0000121 if (MSStyle)
122 Msg += "Note: including file:";
123
Daniel Dunbarda608852011-03-21 19:37:38 +0000124 if (ShowDepth) {
Sebastian Redl4dddebf2011-04-14 14:07:45 +0000125 // The main source file is at depth 1, so skip one dot.
126 for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
Hans Wennborg708002e2013-08-09 00:32:23 +0000127 Msg += MSStyle ? ' ' : '.';
128
129 if (!MSStyle)
130 Msg += ' ';
Daniel Dunbarda608852011-03-21 19:37:38 +0000131 }
Daniel Dunbareef63e02011-02-02 15:41:17 +0000132 Msg += Filename;
133 Msg += '\n';
134
Daniel Dunbarf704c612011-02-03 03:45:00 +0000135 OutputFile->write(Msg.data(), Msg.size());
Stephen Hines176edba2014-12-01 14:53:08 -0800136 OutputFile->flush();
Daniel Dunbareef63e02011-02-02 15:41:17 +0000137 }
138}