Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 1 | //===--- 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 Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame^] | 10 | #include "clang/Frontend/DependencyOutputOptions.h" |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 11 | #include "clang/Frontend/Utils.h" |
| 12 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 13 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 14 | #include "clang/Lex/Preprocessor.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
| 19 | namespace { |
| 20 | class HeaderIncludesCallback : public PPCallbacks { |
| 21 | SourceManager &SM; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 22 | raw_ostream *OutputFile; |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame^] | 23 | const DependencyOutputOptions &DepOpts; |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 24 | unsigned CurrentIncludeDepth; |
| 25 | bool HasProcessedPredefines; |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 26 | bool OwnsOutputFile; |
| 27 | bool ShowAllHeaders; |
Daniel Dunbar | fe908a8 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 28 | bool ShowDepth; |
Hans Wennborg | 0fd6207 | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 29 | bool MSStyle; |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 30 | |
| 31 | public: |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 32 | HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_, |
Nico Weber | 4b5aede | 2016-03-13 02:44:13 +0000 | [diff] [blame] | 33 | raw_ostream *OutputFile_, |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame^] | 34 | const DependencyOutputOptions &DepOpts, |
Nico Weber | 4b5aede | 2016-03-13 02:44:13 +0000 | [diff] [blame] | 35 | bool OwnsOutputFile_, bool ShowDepth_, bool MSStyle_) |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame^] | 36 | : SM(PP->getSourceManager()), OutputFile(OutputFile_), DepOpts(DepOpts), |
| 37 | CurrentIncludeDepth(0), HasProcessedPredefines(false), |
| 38 | OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_), |
| 39 | ShowDepth(ShowDepth_), MSStyle(MSStyle_) {} |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 40 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 41 | ~HeaderIncludesCallback() override { |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 42 | if (OwnsOutputFile) |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 43 | delete OutputFile; |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 44 | } |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 45 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 46 | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 47 | SrcMgr::CharacteristicKind FileType, |
| 48 | FileID PrevFID) override; |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 49 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 50 | } |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 51 | |
Ivan Krasin | 1193f2c | 2015-08-13 04:04:37 +0000 | [diff] [blame] | 52 | static void PrintHeaderInfo(raw_ostream *OutputFile, const char* Filename, |
| 53 | bool ShowDepth, unsigned CurrentIncludeDepth, |
| 54 | bool MSStyle) { |
| 55 | // 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 | |
| 69 | if (!MSStyle) |
| 70 | Msg += ' '; |
| 71 | } |
| 72 | Msg += Pathname; |
| 73 | Msg += '\n'; |
| 74 | |
| 75 | OutputFile->write(Msg.data(), Msg.size()); |
| 76 | OutputFile->flush(); |
| 77 | } |
| 78 | |
| 79 | void clang::AttachHeaderIncludeGen(Preprocessor &PP, |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame^] | 80 | const DependencyOutputOptions &DepOpts, |
| 81 | bool ShowAllHeaders, StringRef OutputPath, |
| 82 | bool ShowDepth, bool MSStyle) { |
Nico Weber | 2582895 | 2014-07-06 03:04:24 +0000 | [diff] [blame] | 83 | raw_ostream *OutputFile = MSStyle ? &llvm::outs() : &llvm::errs(); |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 84 | bool OwnsOutputFile = false; |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 85 | |
| 86 | // Open the output file, if used. |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 87 | if (!OutputPath.empty()) { |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 88 | std::error_code EC; |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 89 | llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream( |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 90 | OutputPath.str(), EC, llvm::sys::fs::F_Append | llvm::sys::fs::F_Text); |
| 91 | if (EC) { |
| 92 | PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure) |
| 93 | << EC.message(); |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 94 | delete OS; |
| 95 | } else { |
| 96 | OS->SetUnbuffered(); |
Daniel Dunbar | 9aa47fc | 2011-02-03 03:45:00 +0000 | [diff] [blame] | 97 | OutputFile = OS; |
| 98 | OwnsOutputFile = true; |
| 99 | } |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame^] | 102 | // Print header info for extra headers, pretending they were discovered by |
| 103 | // the regular preprocessor. The primary use case is to support proper |
| 104 | // generation of Make / Ninja file dependencies for implicit includes, such |
| 105 | // as sanitizer blacklists. It's only important for cl.exe compatibility, |
| 106 | // the GNU way to generate rules is -M / -MM / -MD / -MMD. |
| 107 | for (auto Header : DepOpts.ExtraDeps) |
| 108 | PrintHeaderInfo(OutputFile, Header.c_str(), ShowDepth, 2, MSStyle); |
Nico Weber | 4b5aede | 2016-03-13 02:44:13 +0000 | [diff] [blame] | 109 | PP.addPPCallbacks(llvm::make_unique<HeaderIncludesCallback>( |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame^] | 110 | &PP, ShowAllHeaders, OutputFile, DepOpts, OwnsOutputFile, ShowDepth, |
Nico Weber | 4b5aede | 2016-03-13 02:44:13 +0000 | [diff] [blame] | 111 | MSStyle)); |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void HeaderIncludesCallback::FileChanged(SourceLocation Loc, |
| 115 | FileChangeReason Reason, |
Argyrios Kyrtzidis | 7a70d2f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 116 | SrcMgr::CharacteristicKind NewFileType, |
| 117 | FileID PrevFID) { |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 118 | // Unless we are exiting a #include, make sure to skip ahead to the line the |
| 119 | // #include directive was at. |
| 120 | PresumedLoc UserLoc = SM.getPresumedLoc(Loc); |
| 121 | if (UserLoc.isInvalid()) |
| 122 | return; |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 123 | |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 124 | // Adjust the current include depth. |
| 125 | if (Reason == PPCallbacks::EnterFile) { |
| 126 | ++CurrentIncludeDepth; |
Sebastian Redl | 4e52123 | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 127 | } else if (Reason == PPCallbacks::ExitFile) { |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 128 | if (CurrentIncludeDepth) |
| 129 | --CurrentIncludeDepth; |
| 130 | |
| 131 | // 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] | 132 | // place where we drop back to a nesting depth of 1. |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame^] | 133 | if (CurrentIncludeDepth == 1 && !HasProcessedPredefines) { |
| 134 | if (!DepOpts.ShowIncludesPretendHeader.empty()) { |
| 135 | PrintHeaderInfo(OutputFile, DepOpts.ShowIncludesPretendHeader.c_str(), |
| 136 | ShowDepth, 2, MSStyle); |
| 137 | } |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 138 | HasProcessedPredefines = true; |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame^] | 139 | } |
Sebastian Redl | 4e52123 | 2011-04-14 14:07:45 +0000 | [diff] [blame] | 140 | |
| 141 | return; |
| 142 | } else |
| 143 | return; |
Daniel Dunbar | fb24485 | 2011-02-02 21:11:24 +0000 | [diff] [blame] | 144 | |
| 145 | // Show the header if we are (a) past the predefines, or (b) showing all |
| 146 | // headers and in the predefines at a depth past the initial file and command |
| 147 | // line buffers. |
| 148 | bool ShowHeader = (HasProcessedPredefines || |
| 149 | (ShowAllHeaders && CurrentIncludeDepth > 2)); |
Nico Weber | 149d9522 | 2016-03-23 18:00:22 +0000 | [diff] [blame] | 150 | unsigned IncludeDepth = CurrentIncludeDepth; |
| 151 | if (!HasProcessedPredefines) |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame^] | 152 | --IncludeDepth; // Ignore indent from <built-in>. |
| 153 | else if (!DepOpts.ShowIncludesPretendHeader.empty()) |
| 154 | ++IncludeDepth; // Pretend inclusion by ShowIncludesPretendHeader. |
Daniel Dunbar | fb24485 | 2011-02-02 21:11:24 +0000 | [diff] [blame] | 155 | |
| 156 | // Dump the header include information we are past the predefines buffer or |
Nico Weber | 149d9522 | 2016-03-23 18:00:22 +0000 | [diff] [blame] | 157 | // are showing all headers and this isn't the magic implicit <command line> |
| 158 | // header. |
| 159 | // FIXME: Identify headers in a more robust way than comparing their name to |
| 160 | // "<command line>" and "<built-in>" in a bunch of places. |
| 161 | if (ShowHeader && Reason == PPCallbacks::EnterFile && |
| 162 | UserLoc.getFilename() != StringRef("<command line>")) { |
| 163 | PrintHeaderInfo(OutputFile, UserLoc.getFilename(), ShowDepth, IncludeDepth, |
| 164 | MSStyle); |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 165 | } |
| 166 | } |