blob: 8b2435ba9be12448d40558c53198f77a85ab7149 [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()) {
57 std::string Error;
58 llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
Stephen Hines651f13c2014-04-23 16:59:28 -070059 OutputPath.str().c_str(), Error,
60 llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
Daniel Dunbarf704c612011-02-03 03:45:00 +000061 if (!Error.empty()) {
62 PP.getDiagnostics().Report(
63 clang::diag::warn_fe_cc_print_header_failure) << Error;
64 delete OS;
65 } else {
66 OS->SetUnbuffered();
67 OS->SetUseAtomicWrites(true);
68 OutputFile = OS;
69 OwnsOutputFile = true;
70 }
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000071 }
72
73 PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
Daniel Dunbarda608852011-03-21 19:37:38 +000074 OutputFile, OwnsOutputFile,
Hans Wennborg708002e2013-08-09 00:32:23 +000075 ShowDepth, MSStyle));
Daniel Dunbareef63e02011-02-02 15:41:17 +000076}
77
78void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
79 FileChangeReason Reason,
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +000080 SrcMgr::CharacteristicKind NewFileType,
81 FileID PrevFID) {
Daniel Dunbareef63e02011-02-02 15:41:17 +000082 // Unless we are exiting a #include, make sure to skip ahead to the line the
83 // #include directive was at.
84 PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
85 if (UserLoc.isInvalid())
86 return;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000087
Daniel Dunbareef63e02011-02-02 15:41:17 +000088 // Adjust the current include depth.
89 if (Reason == PPCallbacks::EnterFile) {
90 ++CurrentIncludeDepth;
Sebastian Redl4dddebf2011-04-14 14:07:45 +000091 } else if (Reason == PPCallbacks::ExitFile) {
Daniel Dunbareef63e02011-02-02 15:41:17 +000092 if (CurrentIncludeDepth)
93 --CurrentIncludeDepth;
94
95 // We track when we are done with the predefines by watching for the first
Sebastian Redl4dddebf2011-04-14 14:07:45 +000096 // place where we drop back to a nesting depth of 1.
97 if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
Daniel Dunbareef63e02011-02-02 15:41:17 +000098 HasProcessedPredefines = true;
Sebastian Redl4dddebf2011-04-14 14:07:45 +000099
100 return;
101 } else
102 return;
Daniel Dunbarb376e5e2011-02-02 21:11:24 +0000103
104 // Show the header if we are (a) past the predefines, or (b) showing all
105 // headers and in the predefines at a depth past the initial file and command
106 // line buffers.
107 bool ShowHeader = (HasProcessedPredefines ||
108 (ShowAllHeaders && CurrentIncludeDepth > 2));
109
110 // Dump the header include information we are past the predefines buffer or
111 // are showing all headers.
112 if (ShowHeader && Reason == PPCallbacks::EnterFile) {
Daniel Dunbareef63e02011-02-02 15:41:17 +0000113 // Write to a temporary string to avoid unnecessary flushing on errs().
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000114 SmallString<512> Filename(UserLoc.getFilename());
Hans Wennborg708002e2013-08-09 00:32:23 +0000115 if (!MSStyle)
116 Lexer::Stringify(Filename);
Daniel Dunbareef63e02011-02-02 15:41:17 +0000117
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000118 SmallString<256> Msg;
Hans Wennborg708002e2013-08-09 00:32:23 +0000119 if (MSStyle)
120 Msg += "Note: including file:";
121
Daniel Dunbarda608852011-03-21 19:37:38 +0000122 if (ShowDepth) {
Sebastian Redl4dddebf2011-04-14 14:07:45 +0000123 // The main source file is at depth 1, so skip one dot.
124 for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
Hans Wennborg708002e2013-08-09 00:32:23 +0000125 Msg += MSStyle ? ' ' : '.';
126
127 if (!MSStyle)
128 Msg += ' ';
Daniel Dunbarda608852011-03-21 19:37:38 +0000129 }
Daniel Dunbareef63e02011-02-02 15:41:17 +0000130 Msg += Filename;
131 Msg += '\n';
132
Daniel Dunbarf704c612011-02-03 03:45:00 +0000133 OutputFile->write(Msg.data(), Msg.size());
Daniel Dunbareef63e02011-02-02 15:41:17 +0000134 }
135}