1*67e74705SXin Li //===--- HeaderIncludes.cpp - Generate Header Includes --------------------===//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li
10*67e74705SXin Li #include "clang/Frontend/DependencyOutputOptions.h"
11*67e74705SXin Li #include "clang/Frontend/Utils.h"
12*67e74705SXin Li #include "clang/Basic/SourceManager.h"
13*67e74705SXin Li #include "clang/Frontend/FrontendDiagnostic.h"
14*67e74705SXin Li #include "clang/Lex/Preprocessor.h"
15*67e74705SXin Li #include "llvm/ADT/SmallString.h"
16*67e74705SXin Li #include "llvm/Support/raw_ostream.h"
17*67e74705SXin Li using namespace clang;
18*67e74705SXin Li
19*67e74705SXin Li namespace {
20*67e74705SXin Li class HeaderIncludesCallback : public PPCallbacks {
21*67e74705SXin Li SourceManager &SM;
22*67e74705SXin Li raw_ostream *OutputFile;
23*67e74705SXin Li const DependencyOutputOptions &DepOpts;
24*67e74705SXin Li unsigned CurrentIncludeDepth;
25*67e74705SXin Li bool HasProcessedPredefines;
26*67e74705SXin Li bool OwnsOutputFile;
27*67e74705SXin Li bool ShowAllHeaders;
28*67e74705SXin Li bool ShowDepth;
29*67e74705SXin Li bool MSStyle;
30*67e74705SXin Li
31*67e74705SXin Li public:
HeaderIncludesCallback(const Preprocessor * PP,bool ShowAllHeaders_,raw_ostream * OutputFile_,const DependencyOutputOptions & DepOpts,bool OwnsOutputFile_,bool ShowDepth_,bool MSStyle_)32*67e74705SXin Li HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
33*67e74705SXin Li raw_ostream *OutputFile_,
34*67e74705SXin Li const DependencyOutputOptions &DepOpts,
35*67e74705SXin Li bool OwnsOutputFile_, bool ShowDepth_, bool MSStyle_)
36*67e74705SXin Li : SM(PP->getSourceManager()), OutputFile(OutputFile_), DepOpts(DepOpts),
37*67e74705SXin Li CurrentIncludeDepth(0), HasProcessedPredefines(false),
38*67e74705SXin Li OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
39*67e74705SXin Li ShowDepth(ShowDepth_), MSStyle(MSStyle_) {}
40*67e74705SXin Li
~HeaderIncludesCallback()41*67e74705SXin Li ~HeaderIncludesCallback() override {
42*67e74705SXin Li if (OwnsOutputFile)
43*67e74705SXin Li delete OutputFile;
44*67e74705SXin Li }
45*67e74705SXin Li
46*67e74705SXin Li void FileChanged(SourceLocation Loc, FileChangeReason Reason,
47*67e74705SXin Li SrcMgr::CharacteristicKind FileType,
48*67e74705SXin Li FileID PrevFID) override;
49*67e74705SXin Li };
50*67e74705SXin Li }
51*67e74705SXin Li
PrintHeaderInfo(raw_ostream * OutputFile,StringRef Filename,bool ShowDepth,unsigned CurrentIncludeDepth,bool MSStyle)52*67e74705SXin Li static void PrintHeaderInfo(raw_ostream *OutputFile, StringRef Filename,
53*67e74705SXin Li bool ShowDepth, unsigned CurrentIncludeDepth,
54*67e74705SXin Li bool MSStyle) {
55*67e74705SXin Li // Write to a temporary string to avoid unnecessary flushing on errs().
56*67e74705SXin Li SmallString<512> Pathname(Filename);
57*67e74705SXin Li if (!MSStyle)
58*67e74705SXin Li Lexer::Stringify(Pathname);
59*67e74705SXin Li
60*67e74705SXin Li SmallString<256> Msg;
61*67e74705SXin Li if (MSStyle)
62*67e74705SXin Li Msg += "Note: including file:";
63*67e74705SXin Li
64*67e74705SXin Li if (ShowDepth) {
65*67e74705SXin Li // The main source file is at depth 1, so skip one dot.
66*67e74705SXin Li for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
67*67e74705SXin Li Msg += MSStyle ? ' ' : '.';
68*67e74705SXin Li
69*67e74705SXin Li if (!MSStyle)
70*67e74705SXin Li Msg += ' ';
71*67e74705SXin Li }
72*67e74705SXin Li Msg += Pathname;
73*67e74705SXin Li Msg += '\n';
74*67e74705SXin Li
75*67e74705SXin Li *OutputFile << Msg;
76*67e74705SXin Li OutputFile->flush();
77*67e74705SXin Li }
78*67e74705SXin Li
AttachHeaderIncludeGen(Preprocessor & PP,const DependencyOutputOptions & DepOpts,bool ShowAllHeaders,StringRef OutputPath,bool ShowDepth,bool MSStyle)79*67e74705SXin Li void clang::AttachHeaderIncludeGen(Preprocessor &PP,
80*67e74705SXin Li const DependencyOutputOptions &DepOpts,
81*67e74705SXin Li bool ShowAllHeaders, StringRef OutputPath,
82*67e74705SXin Li bool ShowDepth, bool MSStyle) {
83*67e74705SXin Li raw_ostream *OutputFile = MSStyle ? &llvm::outs() : &llvm::errs();
84*67e74705SXin Li bool OwnsOutputFile = false;
85*67e74705SXin Li
86*67e74705SXin Li // Open the output file, if used.
87*67e74705SXin Li if (!OutputPath.empty()) {
88*67e74705SXin Li std::error_code EC;
89*67e74705SXin Li llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
90*67e74705SXin Li OutputPath.str(), EC, llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
91*67e74705SXin Li if (EC) {
92*67e74705SXin Li PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure)
93*67e74705SXin Li << EC.message();
94*67e74705SXin Li delete OS;
95*67e74705SXin Li } else {
96*67e74705SXin Li OS->SetUnbuffered();
97*67e74705SXin Li OutputFile = OS;
98*67e74705SXin Li OwnsOutputFile = true;
99*67e74705SXin Li }
100*67e74705SXin Li }
101*67e74705SXin Li
102*67e74705SXin Li // Print header info for extra headers, pretending they were discovered by
103*67e74705SXin Li // the regular preprocessor. The primary use case is to support proper
104*67e74705SXin Li // generation of Make / Ninja file dependencies for implicit includes, such
105*67e74705SXin Li // as sanitizer blacklists. It's only important for cl.exe compatibility,
106*67e74705SXin Li // the GNU way to generate rules is -M / -MM / -MD / -MMD.
107*67e74705SXin Li for (const auto &Header : DepOpts.ExtraDeps)
108*67e74705SXin Li PrintHeaderInfo(OutputFile, Header, ShowDepth, 2, MSStyle);
109*67e74705SXin Li PP.addPPCallbacks(llvm::make_unique<HeaderIncludesCallback>(
110*67e74705SXin Li &PP, ShowAllHeaders, OutputFile, DepOpts, OwnsOutputFile, ShowDepth,
111*67e74705SXin Li MSStyle));
112*67e74705SXin Li }
113*67e74705SXin Li
FileChanged(SourceLocation Loc,FileChangeReason Reason,SrcMgr::CharacteristicKind NewFileType,FileID PrevFID)114*67e74705SXin Li void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
115*67e74705SXin Li FileChangeReason Reason,
116*67e74705SXin Li SrcMgr::CharacteristicKind NewFileType,
117*67e74705SXin Li FileID PrevFID) {
118*67e74705SXin Li // Unless we are exiting a #include, make sure to skip ahead to the line the
119*67e74705SXin Li // #include directive was at.
120*67e74705SXin Li PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
121*67e74705SXin Li if (UserLoc.isInvalid())
122*67e74705SXin Li return;
123*67e74705SXin Li
124*67e74705SXin Li // Adjust the current include depth.
125*67e74705SXin Li if (Reason == PPCallbacks::EnterFile) {
126*67e74705SXin Li ++CurrentIncludeDepth;
127*67e74705SXin Li } else if (Reason == PPCallbacks::ExitFile) {
128*67e74705SXin Li if (CurrentIncludeDepth)
129*67e74705SXin Li --CurrentIncludeDepth;
130*67e74705SXin Li
131*67e74705SXin Li // We track when we are done with the predefines by watching for the first
132*67e74705SXin Li // place where we drop back to a nesting depth of 1.
133*67e74705SXin Li if (CurrentIncludeDepth == 1 && !HasProcessedPredefines) {
134*67e74705SXin Li if (!DepOpts.ShowIncludesPretendHeader.empty()) {
135*67e74705SXin Li PrintHeaderInfo(OutputFile, DepOpts.ShowIncludesPretendHeader,
136*67e74705SXin Li ShowDepth, 2, MSStyle);
137*67e74705SXin Li }
138*67e74705SXin Li HasProcessedPredefines = true;
139*67e74705SXin Li }
140*67e74705SXin Li
141*67e74705SXin Li return;
142*67e74705SXin Li } else
143*67e74705SXin Li return;
144*67e74705SXin Li
145*67e74705SXin Li // Show the header if we are (a) past the predefines, or (b) showing all
146*67e74705SXin Li // headers and in the predefines at a depth past the initial file and command
147*67e74705SXin Li // line buffers.
148*67e74705SXin Li bool ShowHeader = (HasProcessedPredefines ||
149*67e74705SXin Li (ShowAllHeaders && CurrentIncludeDepth > 2));
150*67e74705SXin Li unsigned IncludeDepth = CurrentIncludeDepth;
151*67e74705SXin Li if (!HasProcessedPredefines)
152*67e74705SXin Li --IncludeDepth; // Ignore indent from <built-in>.
153*67e74705SXin Li else if (!DepOpts.ShowIncludesPretendHeader.empty())
154*67e74705SXin Li ++IncludeDepth; // Pretend inclusion by ShowIncludesPretendHeader.
155*67e74705SXin Li
156*67e74705SXin Li // Dump the header include information we are past the predefines buffer or
157*67e74705SXin Li // are showing all headers and this isn't the magic implicit <command line>
158*67e74705SXin Li // header.
159*67e74705SXin Li // FIXME: Identify headers in a more robust way than comparing their name to
160*67e74705SXin Li // "<command line>" and "<built-in>" in a bunch of places.
161*67e74705SXin Li if (ShowHeader && Reason == PPCallbacks::EnterFile &&
162*67e74705SXin Li UserLoc.getFilename() != StringRef("<command line>")) {
163*67e74705SXin Li PrintHeaderInfo(OutputFile, UserLoc.getFilename(), ShowDepth, IncludeDepth,
164*67e74705SXin Li MSStyle);
165*67e74705SXin Li }
166*67e74705SXin Li }
167