blob: d60f5333bf5bf7edb899b0ae88ddf4a1c6025762 [file] [log] [blame]
Fangrui Song524b3c12019-03-01 06:49:51 +00001//===-- HeaderIncludeGen.cpp - Generate Header Includes -------------------===//
Daniel Dunbar27734fd2011-02-02 15:41:17 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Dunbar27734fd2011-02-02 15:41:17 +00006//
7//===----------------------------------------------------------------------===//
8
Nico Weberf54146c2016-03-23 18:46:57 +00009#include "clang/Frontend/DependencyOutputOptions.h"
Daniel Dunbar27734fd2011-02-02 15:41:17 +000010#include "clang/Frontend/Utils.h"
11#include "clang/Basic/SourceManager.h"
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000012#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar27734fd2011-02-02 15:41:17 +000013#include "clang/Lex/Preprocessor.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000014#include "llvm/ADT/SmallString.h"
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000015#include "llvm/Support/raw_ostream.h"
Daniel Dunbar27734fd2011-02-02 15:41:17 +000016using namespace clang;
17
18namespace {
19class HeaderIncludesCallback : public PPCallbacks {
20 SourceManager &SM;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000021 raw_ostream *OutputFile;
Nico Weberf54146c2016-03-23 18:46:57 +000022 const DependencyOutputOptions &DepOpts;
Daniel Dunbar27734fd2011-02-02 15:41:17 +000023 unsigned CurrentIncludeDepth;
24 bool HasProcessedPredefines;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000025 bool OwnsOutputFile;
26 bool ShowAllHeaders;
Daniel Dunbarfe908a82011-03-21 19:37:38 +000027 bool ShowDepth;
Hans Wennborg0fd62072013-08-09 00:32:23 +000028 bool MSStyle;
Daniel Dunbar27734fd2011-02-02 15:41:17 +000029
30public:
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000031 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
Nico Weber4b5aede2016-03-13 02:44:13 +000032 raw_ostream *OutputFile_,
Nico Weberf54146c2016-03-23 18:46:57 +000033 const DependencyOutputOptions &DepOpts,
Nico Weber4b5aede2016-03-13 02:44:13 +000034 bool OwnsOutputFile_, bool ShowDepth_, bool MSStyle_)
Nico Weberf54146c2016-03-23 18:46:57 +000035 : SM(PP->getSourceManager()), OutputFile(OutputFile_), DepOpts(DepOpts),
36 CurrentIncludeDepth(0), HasProcessedPredefines(false),
37 OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
38 ShowDepth(ShowDepth_), MSStyle(MSStyle_) {}
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000039
Alexander Kornienko34eb2072015-04-11 02:00:23 +000040 ~HeaderIncludesCallback() override {
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000041 if (OwnsOutputFile)
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000042 delete OutputFile;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000043 }
Daniel Dunbar27734fd2011-02-02 15:41:17 +000044
Craig Topperafa7cb32014-03-13 06:07:04 +000045 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
46 SrcMgr::CharacteristicKind FileType,
47 FileID PrevFID) override;
Daniel Dunbar27734fd2011-02-02 15:41:17 +000048};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000049}
Daniel Dunbar27734fd2011-02-02 15:41:17 +000050
Benjamin Kramer2787e452016-05-27 12:52:19 +000051static void PrintHeaderInfo(raw_ostream *OutputFile, StringRef Filename,
Ivan Krasin1193f2c2015-08-13 04:04:37 +000052 bool ShowDepth, unsigned CurrentIncludeDepth,
53 bool MSStyle) {
Benjamin Kramer2787e452016-05-27 12:52:19 +000054 // Write to a temporary string to avoid unnecessary flushing on errs().
55 SmallString<512> Pathname(Filename);
56 if (!MSStyle)
57 Lexer::Stringify(Pathname);
58
59 SmallString<256> Msg;
60 if (MSStyle)
61 Msg += "Note: including file:";
62
63 if (ShowDepth) {
64 // The main source file is at depth 1, so skip one dot.
65 for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
66 Msg += MSStyle ? ' ' : '.';
67
Ivan Krasin1193f2c2015-08-13 04:04:37 +000068 if (!MSStyle)
Benjamin Kramer2787e452016-05-27 12:52:19 +000069 Msg += ' ';
70 }
71 Msg += Pathname;
72 Msg += '\n';
Ivan Krasin1193f2c2015-08-13 04:04:37 +000073
Benjamin Kramer2787e452016-05-27 12:52:19 +000074 *OutputFile << Msg;
75 OutputFile->flush();
Ivan Krasin1193f2c2015-08-13 04:04:37 +000076}
77
78void clang::AttachHeaderIncludeGen(Preprocessor &PP,
Nico Weberf54146c2016-03-23 18:46:57 +000079 const DependencyOutputOptions &DepOpts,
80 bool ShowAllHeaders, StringRef OutputPath,
81 bool ShowDepth, bool MSStyle) {
Erich Keane425f48d2018-05-04 15:58:31 +000082 raw_ostream *OutputFile = &llvm::errs();
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000083 bool OwnsOutputFile = false;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000084
Erich Keane425f48d2018-05-04 15:58:31 +000085 // Choose output stream, when printing in cl.exe /showIncludes style.
86 if (MSStyle) {
87 switch (DepOpts.ShowIncludesDest) {
88 default:
89 llvm_unreachable("Invalid destination for /showIncludes output!");
90 case ShowIncludesDestination::Stderr:
91 OutputFile = &llvm::errs();
92 break;
93 case ShowIncludesDestination::Stdout:
94 OutputFile = &llvm::outs();
95 break;
96 }
97 }
98
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000099 // Open the output file, if used.
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +0000100 if (!OutputPath.empty()) {
Rafael Espindoladae941a2014-08-25 18:17:04 +0000101 std::error_code EC;
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +0000102 llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
Rafael Espindoladae941a2014-08-25 18:17:04 +0000103 OutputPath.str(), EC, llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
104 if (EC) {
105 PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure)
106 << EC.message();
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +0000107 delete OS;
108 } else {
109 OS->SetUnbuffered();
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +0000110 OutputFile = OS;
111 OwnsOutputFile = true;
112 }
Daniel Dunbar1af1d27512011-02-02 21:11:31 +0000113 }
114
Nico Weberf54146c2016-03-23 18:46:57 +0000115 // Print header info for extra headers, pretending they were discovered by
116 // the regular preprocessor. The primary use case is to support proper
117 // generation of Make / Ninja file dependencies for implicit includes, such
118 // as sanitizer blacklists. It's only important for cl.exe compatibility,
119 // the GNU way to generate rules is -M / -MM / -MD / -MMD.
Benjamin Kramer2787e452016-05-27 12:52:19 +0000120 for (const auto &Header : DepOpts.ExtraDeps)
121 PrintHeaderInfo(OutputFile, Header, ShowDepth, 2, MSStyle);
Nico Weber4b5aede2016-03-13 02:44:13 +0000122 PP.addPPCallbacks(llvm::make_unique<HeaderIncludesCallback>(
Nico Weberf54146c2016-03-23 18:46:57 +0000123 &PP, ShowAllHeaders, OutputFile, DepOpts, OwnsOutputFile, ShowDepth,
Nico Weber4b5aede2016-03-13 02:44:13 +0000124 MSStyle));
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000125}
126
127void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
128 FileChangeReason Reason,
Argyrios Kyrtzidis7a70d2f2011-10-11 17:29:44 +0000129 SrcMgr::CharacteristicKind NewFileType,
130 FileID PrevFID) {
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000131 // Unless we are exiting a #include, make sure to skip ahead to the line the
132 // #include directive was at.
133 PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
134 if (UserLoc.isInvalid())
135 return;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +0000136
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000137 // Adjust the current include depth.
138 if (Reason == PPCallbacks::EnterFile) {
139 ++CurrentIncludeDepth;
Sebastian Redl4e521232011-04-14 14:07:45 +0000140 } else if (Reason == PPCallbacks::ExitFile) {
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000141 if (CurrentIncludeDepth)
142 --CurrentIncludeDepth;
143
144 // We track when we are done with the predefines by watching for the first
Sebastian Redl4e521232011-04-14 14:07:45 +0000145 // place where we drop back to a nesting depth of 1.
Nico Weberf54146c2016-03-23 18:46:57 +0000146 if (CurrentIncludeDepth == 1 && !HasProcessedPredefines) {
147 if (!DepOpts.ShowIncludesPretendHeader.empty()) {
Benjamin Kramer2787e452016-05-27 12:52:19 +0000148 PrintHeaderInfo(OutputFile, DepOpts.ShowIncludesPretendHeader,
Nico Weberf54146c2016-03-23 18:46:57 +0000149 ShowDepth, 2, MSStyle);
150 }
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000151 HasProcessedPredefines = true;
Nico Weberf54146c2016-03-23 18:46:57 +0000152 }
Sebastian Redl4e521232011-04-14 14:07:45 +0000153
154 return;
155 } else
156 return;
Daniel Dunbarfb244852011-02-02 21:11:24 +0000157
158 // Show the header if we are (a) past the predefines, or (b) showing all
159 // headers and in the predefines at a depth past the initial file and command
160 // line buffers.
161 bool ShowHeader = (HasProcessedPredefines ||
162 (ShowAllHeaders && CurrentIncludeDepth > 2));
Nico Weber149d95222016-03-23 18:00:22 +0000163 unsigned IncludeDepth = CurrentIncludeDepth;
164 if (!HasProcessedPredefines)
Nico Weberf54146c2016-03-23 18:46:57 +0000165 --IncludeDepth; // Ignore indent from <built-in>.
166 else if (!DepOpts.ShowIncludesPretendHeader.empty())
167 ++IncludeDepth; // Pretend inclusion by ShowIncludesPretendHeader.
Daniel Dunbarfb244852011-02-02 21:11:24 +0000168
169 // Dump the header include information we are past the predefines buffer or
Nico Weber149d95222016-03-23 18:00:22 +0000170 // are showing all headers and this isn't the magic implicit <command line>
171 // header.
172 // FIXME: Identify headers in a more robust way than comparing their name to
173 // "<command line>" and "<built-in>" in a bunch of places.
174 if (ShowHeader && Reason == PPCallbacks::EnterFile &&
175 UserLoc.getFilename() != StringRef("<command line>")) {
176 PrintHeaderInfo(OutputFile, UserLoc.getFilename(), ShowDepth, IncludeDepth,
177 MSStyle);
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000178 }
179}