blob: 9dc107c9d546281a239fdf1e384d207fb9475d80 [file] [log] [blame]
Daniel Dunbar27734fd2011-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
Nico Weberf54146c2016-03-23 18:46:57 +000010#include "clang/Frontend/DependencyOutputOptions.h"
Daniel Dunbar27734fd2011-02-02 15:41:17 +000011#include "clang/Frontend/Utils.h"
12#include "clang/Basic/SourceManager.h"
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000013#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar27734fd2011-02-02 15:41:17 +000014#include "clang/Lex/Preprocessor.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000016#include "llvm/Support/raw_ostream.h"
Daniel Dunbar27734fd2011-02-02 15:41:17 +000017using namespace clang;
18
19namespace {
20class HeaderIncludesCallback : public PPCallbacks {
21 SourceManager &SM;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000022 raw_ostream *OutputFile;
Nico Weberf54146c2016-03-23 18:46:57 +000023 const DependencyOutputOptions &DepOpts;
Daniel Dunbar27734fd2011-02-02 15:41:17 +000024 unsigned CurrentIncludeDepth;
25 bool HasProcessedPredefines;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000026 bool OwnsOutputFile;
27 bool ShowAllHeaders;
Daniel Dunbarfe908a82011-03-21 19:37:38 +000028 bool ShowDepth;
Hans Wennborg0fd62072013-08-09 00:32:23 +000029 bool MSStyle;
Daniel Dunbar27734fd2011-02-02 15:41:17 +000030
31public:
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000032 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
Nico Weber4b5aede2016-03-13 02:44:13 +000033 raw_ostream *OutputFile_,
Nico Weberf54146c2016-03-23 18:46:57 +000034 const DependencyOutputOptions &DepOpts,
Nico Weber4b5aede2016-03-13 02:44:13 +000035 bool OwnsOutputFile_, bool ShowDepth_, bool MSStyle_)
Nico Weberf54146c2016-03-23 18:46:57 +000036 : SM(PP->getSourceManager()), OutputFile(OutputFile_), DepOpts(DepOpts),
37 CurrentIncludeDepth(0), HasProcessedPredefines(false),
38 OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
39 ShowDepth(ShowDepth_), MSStyle(MSStyle_) {}
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000040
Alexander Kornienko34eb2072015-04-11 02:00:23 +000041 ~HeaderIncludesCallback() override {
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000042 if (OwnsOutputFile)
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000043 delete OutputFile;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000044 }
Daniel Dunbar27734fd2011-02-02 15:41:17 +000045
Craig Topperafa7cb32014-03-13 06:07:04 +000046 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
47 SrcMgr::CharacteristicKind FileType,
48 FileID PrevFID) override;
Daniel Dunbar27734fd2011-02-02 15:41:17 +000049};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000050}
Daniel Dunbar27734fd2011-02-02 15:41:17 +000051
Benjamin Kramer2787e452016-05-27 12:52:19 +000052static void PrintHeaderInfo(raw_ostream *OutputFile, StringRef Filename,
Ivan Krasin1193f2c2015-08-13 04:04:37 +000053 bool ShowDepth, unsigned CurrentIncludeDepth,
54 bool MSStyle) {
Benjamin Kramer2787e452016-05-27 12:52:19 +000055 // Write to a temporary string to avoid unnecessary flushing on errs().
56 SmallString<512> Pathname(Filename);
57 if (!MSStyle)
58 Lexer::Stringify(Pathname);
59
60 SmallString<256> Msg;
61 if (MSStyle)
62 Msg += "Note: including file:";
63
64 if (ShowDepth) {
65 // The main source file is at depth 1, so skip one dot.
66 for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
67 Msg += MSStyle ? ' ' : '.';
68
Ivan Krasin1193f2c2015-08-13 04:04:37 +000069 if (!MSStyle)
Benjamin Kramer2787e452016-05-27 12:52:19 +000070 Msg += ' ';
71 }
72 Msg += Pathname;
73 Msg += '\n';
Ivan Krasin1193f2c2015-08-13 04:04:37 +000074
Benjamin Kramer2787e452016-05-27 12:52:19 +000075 *OutputFile << Msg;
76 OutputFile->flush();
Ivan Krasin1193f2c2015-08-13 04:04:37 +000077}
78
79void clang::AttachHeaderIncludeGen(Preprocessor &PP,
Nico Weberf54146c2016-03-23 18:46:57 +000080 const DependencyOutputOptions &DepOpts,
81 bool ShowAllHeaders, StringRef OutputPath,
82 bool ShowDepth, bool MSStyle) {
Erich Keane425f48d2018-05-04 15:58:31 +000083 raw_ostream *OutputFile = &llvm::errs();
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000084 bool OwnsOutputFile = false;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000085
Erich Keane425f48d2018-05-04 15:58:31 +000086 // Choose output stream, when printing in cl.exe /showIncludes style.
87 if (MSStyle) {
88 switch (DepOpts.ShowIncludesDest) {
89 default:
90 llvm_unreachable("Invalid destination for /showIncludes output!");
91 case ShowIncludesDestination::Stderr:
92 OutputFile = &llvm::errs();
93 break;
94 case ShowIncludesDestination::Stdout:
95 OutputFile = &llvm::outs();
96 break;
97 }
98 }
99
Daniel Dunbar1af1d27512011-02-02 21:11:31 +0000100 // Open the output file, if used.
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +0000101 if (!OutputPath.empty()) {
Rafael Espindoladae941a2014-08-25 18:17:04 +0000102 std::error_code EC;
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +0000103 llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
Rafael Espindoladae941a2014-08-25 18:17:04 +0000104 OutputPath.str(), EC, llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
105 if (EC) {
106 PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure)
107 << EC.message();
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +0000108 delete OS;
109 } else {
110 OS->SetUnbuffered();
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +0000111 OutputFile = OS;
112 OwnsOutputFile = true;
113 }
Daniel Dunbar1af1d27512011-02-02 21:11:31 +0000114 }
115
Nico Weberf54146c2016-03-23 18:46:57 +0000116 // Print header info for extra headers, pretending they were discovered by
117 // the regular preprocessor. The primary use case is to support proper
118 // generation of Make / Ninja file dependencies for implicit includes, such
119 // as sanitizer blacklists. It's only important for cl.exe compatibility,
120 // the GNU way to generate rules is -M / -MM / -MD / -MMD.
Benjamin Kramer2787e452016-05-27 12:52:19 +0000121 for (const auto &Header : DepOpts.ExtraDeps)
122 PrintHeaderInfo(OutputFile, Header, ShowDepth, 2, MSStyle);
Nico Weber4b5aede2016-03-13 02:44:13 +0000123 PP.addPPCallbacks(llvm::make_unique<HeaderIncludesCallback>(
Nico Weberf54146c2016-03-23 18:46:57 +0000124 &PP, ShowAllHeaders, OutputFile, DepOpts, OwnsOutputFile, ShowDepth,
Nico Weber4b5aede2016-03-13 02:44:13 +0000125 MSStyle));
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000126}
127
128void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
129 FileChangeReason Reason,
Argyrios Kyrtzidis7a70d2f2011-10-11 17:29:44 +0000130 SrcMgr::CharacteristicKind NewFileType,
131 FileID PrevFID) {
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000132 // Unless we are exiting a #include, make sure to skip ahead to the line the
133 // #include directive was at.
134 PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
135 if (UserLoc.isInvalid())
136 return;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +0000137
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000138 // Adjust the current include depth.
139 if (Reason == PPCallbacks::EnterFile) {
140 ++CurrentIncludeDepth;
Sebastian Redl4e521232011-04-14 14:07:45 +0000141 } else if (Reason == PPCallbacks::ExitFile) {
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000142 if (CurrentIncludeDepth)
143 --CurrentIncludeDepth;
144
145 // We track when we are done with the predefines by watching for the first
Sebastian Redl4e521232011-04-14 14:07:45 +0000146 // place where we drop back to a nesting depth of 1.
Nico Weberf54146c2016-03-23 18:46:57 +0000147 if (CurrentIncludeDepth == 1 && !HasProcessedPredefines) {
148 if (!DepOpts.ShowIncludesPretendHeader.empty()) {
Benjamin Kramer2787e452016-05-27 12:52:19 +0000149 PrintHeaderInfo(OutputFile, DepOpts.ShowIncludesPretendHeader,
Nico Weberf54146c2016-03-23 18:46:57 +0000150 ShowDepth, 2, MSStyle);
151 }
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000152 HasProcessedPredefines = true;
Nico Weberf54146c2016-03-23 18:46:57 +0000153 }
Sebastian Redl4e521232011-04-14 14:07:45 +0000154
155 return;
156 } else
157 return;
Daniel Dunbarfb244852011-02-02 21:11:24 +0000158
159 // Show the header if we are (a) past the predefines, or (b) showing all
160 // headers and in the predefines at a depth past the initial file and command
161 // line buffers.
162 bool ShowHeader = (HasProcessedPredefines ||
163 (ShowAllHeaders && CurrentIncludeDepth > 2));
Nico Weber149d95222016-03-23 18:00:22 +0000164 unsigned IncludeDepth = CurrentIncludeDepth;
165 if (!HasProcessedPredefines)
Nico Weberf54146c2016-03-23 18:46:57 +0000166 --IncludeDepth; // Ignore indent from <built-in>.
167 else if (!DepOpts.ShowIncludesPretendHeader.empty())
168 ++IncludeDepth; // Pretend inclusion by ShowIncludesPretendHeader.
Daniel Dunbarfb244852011-02-02 21:11:24 +0000169
170 // Dump the header include information we are past the predefines buffer or
Nico Weber149d95222016-03-23 18:00:22 +0000171 // are showing all headers and this isn't the magic implicit <command line>
172 // header.
173 // FIXME: Identify headers in a more robust way than comparing their name to
174 // "<command line>" and "<built-in>" in a bunch of places.
175 if (ShowHeader && Reason == PPCallbacks::EnterFile &&
176 UserLoc.getFilename() != StringRef("<command line>")) {
177 PrintHeaderInfo(OutputFile, UserLoc.getFilename(), ShowDepth, IncludeDepth,
178 MSStyle);
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000179 }
180}