1 //===--- HeaderIncludes.cpp - Generate Header Includes --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Frontend/Utils.h"
11 #include "clang/Basic/SourceManager.h"
12 #include "clang/Frontend/FrontendDiagnostic.h"
13 #include "clang/Lex/Preprocessor.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/Support/raw_ostream.h"
16 using namespace clang;
19 class HeaderIncludesCallback : public PPCallbacks {
21 raw_ostream *OutputFile;
22 unsigned CurrentIncludeDepth;
23 bool HasProcessedPredefines;
30 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
31 raw_ostream *OutputFile_, bool OwnsOutputFile_,
32 bool ShowDepth_, bool MSStyle_)
33 : SM(PP->getSourceManager()), OutputFile(OutputFile_),
34 CurrentIncludeDepth(0), HasProcessedPredefines(false),
35 OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
36 ShowDepth(ShowDepth_), MSStyle(MSStyle_) {}
38 ~HeaderIncludesCallback() override {
43 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
44 SrcMgr::CharacteristicKind FileType,
45 FileID PrevFID) override;
49 static void PrintHeaderInfo(raw_ostream *OutputFile, const char* Filename,
50 bool ShowDepth, unsigned CurrentIncludeDepth,
52 // Write to a temporary string to avoid unnecessary flushing on errs().
53 SmallString<512> Pathname(Filename);
55 Lexer::Stringify(Pathname);
59 Msg += "Note: including file:";
62 // The main source file is at depth 1, so skip one dot.
63 for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
64 Msg += MSStyle ? ' ' : '.';
72 OutputFile->write(Msg.data(), Msg.size());
76 void clang::AttachHeaderIncludeGen(Preprocessor &PP,
77 const std::vector<std::string> &ExtraHeaders,
79 StringRef OutputPath, bool ShowDepth,
81 raw_ostream *OutputFile = MSStyle ? &llvm::outs() : &llvm::errs();
82 bool OwnsOutputFile = false;
84 // Open the output file, if used.
85 if (!OutputPath.empty()) {
87 llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
88 OutputPath.str(), EC, llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
90 PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure)
96 OwnsOutputFile = true;
100 // Print header info for extra headers, pretending they were discovered
101 // by the regular preprocessor. The primary use case is to support
102 // proper generation of Make / Ninja file dependencies for implicit includes,
103 // such as sanitizer blacklists. It's only important for cl.exe
104 // compatibility, the GNU way to generate rules is -M / -MM / -MD / -MMD.
105 for (auto Header : ExtraHeaders) {
106 PrintHeaderInfo(OutputFile, Header.c_str(), ShowDepth, 2, MSStyle);
108 PP.addPPCallbacks(llvm::make_unique<HeaderIncludesCallback>(&PP,
116 void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
117 FileChangeReason Reason,
118 SrcMgr::CharacteristicKind NewFileType,
120 // Unless we are exiting a #include, make sure to skip ahead to the line the
121 // #include directive was at.
122 PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
123 if (UserLoc.isInvalid())
126 // Adjust the current include depth.
127 if (Reason == PPCallbacks::EnterFile) {
128 ++CurrentIncludeDepth;
129 } else if (Reason == PPCallbacks::ExitFile) {
130 if (CurrentIncludeDepth)
131 --CurrentIncludeDepth;
133 // We track when we are done with the predefines by watching for the first
134 // place where we drop back to a nesting depth of 1.
135 if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
136 HasProcessedPredefines = true;
142 // Show the header if we are (a) past the predefines, or (b) showing all
143 // headers and in the predefines at a depth past the initial file and command
145 bool ShowHeader = (HasProcessedPredefines ||
146 (ShowAllHeaders && CurrentIncludeDepth > 2));
148 // Dump the header include information we are past the predefines buffer or
149 // are showing all headers.
150 if (ShowHeader && Reason == PPCallbacks::EnterFile) {
151 PrintHeaderInfo(OutputFile, UserLoc.getFilename(),
152 ShowDepth, CurrentIncludeDepth, MSStyle);