Fangrui Song | 524b3c1 | 2019-03-01 06:49:51 +0000 | [diff] [blame] | 1 | //===-- HeaderIncludeGen.cpp - Generate Header Includes -------------------===// |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame] | 9 | #include "clang/Frontend/DependencyOutputOptions.h" |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 10 | #include "clang/Frontend/Utils.h" |
| 11 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 12 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 13 | #include "clang/Lex/Preprocessor.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallString.h" |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 16 | using namespace clang; |
| 17 | |
| 18 | namespace { |
| 19 | class HeaderIncludesCallback : public PPCallbacks { |
| 20 | SourceManager &SM; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 21 | raw_ostream *OutputFile; |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame] | 22 | const DependencyOutputOptions &DepOpts; |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 23 | unsigned CurrentIncludeDepth; |
| 24 | bool HasProcessedPredefines; |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 25 | bool OwnsOutputFile; |
| 26 | bool ShowAllHeaders; |
Daniel Dunbar | fe908a8 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 27 | bool ShowDepth; |
Hans Wennborg | 0fd6207 | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 28 | bool MSStyle; |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 29 | |
| 30 | public: |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 31 | HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_, |
Nico Weber | 4b5aede | 2016-03-13 02:44:13 +0000 | [diff] [blame] | 32 | raw_ostream *OutputFile_, |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame] | 33 | const DependencyOutputOptions &DepOpts, |
Nico Weber | 4b5aede | 2016-03-13 02:44:13 +0000 | [diff] [blame] | 34 | bool OwnsOutputFile_, bool ShowDepth_, bool MSStyle_) |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame] | 35 | : SM(PP->getSourceManager()), OutputFile(OutputFile_), DepOpts(DepOpts), |
| 36 | CurrentIncludeDepth(0), HasProcessedPredefines(false), |
| 37 | OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_), |
| 38 | ShowDepth(ShowDepth_), MSStyle(MSStyle_) {} |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 39 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 40 | ~HeaderIncludesCallback() override { |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 41 | if (OwnsOutputFile) |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 42 | delete OutputFile; |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 43 | } |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 44 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 45 | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 46 | SrcMgr::CharacteristicKind FileType, |
| 47 | FileID PrevFID) override; |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 48 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 49 | } |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 50 | |
Benjamin Kramer | 2787e45 | 2016-05-27 12:52:19 +0000 | [diff] [blame] | 51 | static void PrintHeaderInfo(raw_ostream *OutputFile, StringRef Filename, |
Ivan Krasin | 1193f2c | 2015-08-13 04:04:37 +0000 | [diff] [blame] | 52 | bool ShowDepth, unsigned CurrentIncludeDepth, |
| 53 | bool MSStyle) { |
Benjamin Kramer | 2787e45 | 2016-05-27 12:52:19 +0000 | [diff] [blame] | 54 | // 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 Krasin | 1193f2c | 2015-08-13 04:04:37 +0000 | [diff] [blame] | 68 | if (!MSStyle) |
Benjamin Kramer | 2787e45 | 2016-05-27 12:52:19 +0000 | [diff] [blame] | 69 | Msg += ' '; |
| 70 | } |
| 71 | Msg += Pathname; |
| 72 | Msg += '\n'; |
Ivan Krasin | 1193f2c | 2015-08-13 04:04:37 +0000 | [diff] [blame] | 73 | |
Benjamin Kramer | 2787e45 | 2016-05-27 12:52:19 +0000 | [diff] [blame] | 74 | *OutputFile << Msg; |
| 75 | OutputFile->flush(); |
Ivan Krasin | 1193f2c | 2015-08-13 04:04:37 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void clang::AttachHeaderIncludeGen(Preprocessor &PP, |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame] | 79 | const DependencyOutputOptions &DepOpts, |
| 80 | bool ShowAllHeaders, StringRef OutputPath, |
| 81 | bool ShowDepth, bool MSStyle) { |
Erich Keane | 425f48d | 2018-05-04 15:58:31 +0000 | [diff] [blame] | 82 | raw_ostream *OutputFile = &llvm::errs(); |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 83 | bool OwnsOutputFile = false; |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 84 | |
Erich Keane | 425f48d | 2018-05-04 15:58:31 +0000 | [diff] [blame] | 85 | // 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 Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 99 | // Open the output file, if used. |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 100 | if (!OutputPath.empty()) { |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 101 | std::error_code EC; |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 102 | llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream( |
Fangrui Song | d9b948b | 2019-08-05 05:43:48 +0000 | [diff] [blame] | 103 | OutputPath.str(), EC, |
| 104 | llvm::sys::fs::OF_Append | llvm::sys::fs::OF_Text); |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 105 | if (EC) { |
| 106 | PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure) |
| 107 | << EC.message(); |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 108 | delete OS; |
| 109 | } else { |
| 110 | OS->SetUnbuffered(); |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 111 | OutputFile = OS; |
| 112 | OwnsOutputFile = true; |
| 113 | } |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame] | 116 | // 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 Kramer | 2787e45 | 2016-05-27 12:52:19 +0000 | [diff] [blame] | 121 | for (const auto &Header : DepOpts.ExtraDeps) |
| 122 | PrintHeaderInfo(OutputFile, Header, ShowDepth, 2, MSStyle); |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 123 | PP.addPPCallbacks(std::make_unique<HeaderIncludesCallback>( |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame] | 124 | &PP, ShowAllHeaders, OutputFile, DepOpts, OwnsOutputFile, ShowDepth, |
Nico Weber | 4b5aede | 2016-03-13 02:44:13 +0000 | [diff] [blame] | 125 | MSStyle)); |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void HeaderIncludesCallback::FileChanged(SourceLocation Loc, |
| 129 | FileChangeReason Reason, |
Nico Weber | bcda126 | 2020-02-24 16:07:16 -0500 | [diff] [blame] | 130 | SrcMgr::CharacteristicKind NewFileType, |
| 131 | FileID PrevFID) { |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 132 | // 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 Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 137 | |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 138 | // Adjust the current include depth. |
| 139 | if (Reason == PPCallbacks::EnterFile) { |
| 140 | ++CurrentIncludeDepth; |
Sebastian Redl | 4e52123 | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 141 | } else if (Reason == PPCallbacks::ExitFile) { |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 142 | if (CurrentIncludeDepth) |
| 143 | --CurrentIncludeDepth; |
| 144 | |
| 145 | // We track when we are done with the predefines by watching for the first |
Sebastian Redl | 4e52123 | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 146 | // place where we drop back to a nesting depth of 1. |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame] | 147 | if (CurrentIncludeDepth == 1 && !HasProcessedPredefines) { |
| 148 | if (!DepOpts.ShowIncludesPretendHeader.empty()) { |
Benjamin Kramer | 2787e45 | 2016-05-27 12:52:19 +0000 | [diff] [blame] | 149 | PrintHeaderInfo(OutputFile, DepOpts.ShowIncludesPretendHeader, |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame] | 150 | ShowDepth, 2, MSStyle); |
| 151 | } |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 152 | HasProcessedPredefines = true; |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame] | 153 | } |
Sebastian Redl | 4e52123 | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 154 | |
| 155 | return; |
| 156 | } else |
| 157 | return; |
Daniel Dunbar | fb24485 | 2011-02-02 21:11:24 +0000 | [diff] [blame] | 158 | |
| 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 Weber | 149d9522 | 2016-03-23 18:00:22 +0000 | [diff] [blame] | 164 | unsigned IncludeDepth = CurrentIncludeDepth; |
| 165 | if (!HasProcessedPredefines) |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame] | 166 | --IncludeDepth; // Ignore indent from <built-in>. |
| 167 | else if (!DepOpts.ShowIncludesPretendHeader.empty()) |
| 168 | ++IncludeDepth; // Pretend inclusion by ShowIncludesPretendHeader. |
Daniel Dunbar | fb24485 | 2011-02-02 21:11:24 +0000 | [diff] [blame] | 169 | |
Nico Weber | bcda126 | 2020-02-24 16:07:16 -0500 | [diff] [blame] | 170 | if (!DepOpts.IncludeSystemHeaders && isSystem(NewFileType)) |
| 171 | ShowHeader = false; |
| 172 | |
Daniel Dunbar | fb24485 | 2011-02-02 21:11:24 +0000 | [diff] [blame] | 173 | // Dump the header include information we are past the predefines buffer or |
Nico Weber | 149d9522 | 2016-03-23 18:00:22 +0000 | [diff] [blame] | 174 | // are showing all headers and this isn't the magic implicit <command line> |
| 175 | // header. |
| 176 | // FIXME: Identify headers in a more robust way than comparing their name to |
| 177 | // "<command line>" and "<built-in>" in a bunch of places. |
| 178 | if (ShowHeader && Reason == PPCallbacks::EnterFile && |
| 179 | UserLoc.getFilename() != StringRef("<command line>")) { |
| 180 | PrintHeaderInfo(OutputFile, UserLoc.getFilename(), ShowDepth, IncludeDepth, |
| 181 | MSStyle); |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 182 | } |
| 183 | } |