blob: 9123d5321fdbf6f21e439891c62507f79a67a9bf [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
10#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;
Daniel Dunbar27734fd2011-02-02 15:41:17 +000022 unsigned CurrentIncludeDepth;
23 bool HasProcessedPredefines;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000024 bool OwnsOutputFile;
25 bool ShowAllHeaders;
Daniel Dunbarfe908a82011-03-21 19:37:38 +000026 bool ShowDepth;
Hans Wennborg0fd62072013-08-09 00:32:23 +000027 bool MSStyle;
Daniel Dunbar27734fd2011-02-02 15:41:17 +000028
29public:
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000030 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000031 raw_ostream *OutputFile_, bool OwnsOutputFile_,
Hans Wennborg0fd62072013-08-09 00:32:23 +000032 bool ShowDepth_, bool MSStyle_)
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000033 : SM(PP->getSourceManager()), OutputFile(OutputFile_),
34 CurrentIncludeDepth(0), HasProcessedPredefines(false),
Daniel Dunbarfe908a82011-03-21 19:37:38 +000035 OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
Hans Wennborg0fd62072013-08-09 00:32:23 +000036 ShowDepth(ShowDepth_), MSStyle(MSStyle_) {}
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000037
Alexander Kornienko34eb2072015-04-11 02:00:23 +000038 ~HeaderIncludesCallback() override {
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000039 if (OwnsOutputFile)
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000040 delete OutputFile;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000041 }
Daniel Dunbar27734fd2011-02-02 15:41:17 +000042
Craig Topperafa7cb32014-03-13 06:07:04 +000043 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
44 SrcMgr::CharacteristicKind FileType,
45 FileID PrevFID) override;
Daniel Dunbar27734fd2011-02-02 15:41:17 +000046};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000047}
Daniel Dunbar27734fd2011-02-02 15:41:17 +000048
Ivan Krasin1193f2c2015-08-13 04:04:37 +000049static void PrintHeaderInfo(raw_ostream *OutputFile, const char* Filename,
50 bool ShowDepth, unsigned CurrentIncludeDepth,
51 bool MSStyle) {
52 // Write to a temporary string to avoid unnecessary flushing on errs().
53 SmallString<512> Pathname(Filename);
54 if (!MSStyle)
55 Lexer::Stringify(Pathname);
56
57 SmallString<256> Msg;
58 if (MSStyle)
59 Msg += "Note: including file:";
60
61 if (ShowDepth) {
62 // The main source file is at depth 1, so skip one dot.
63 for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
64 Msg += MSStyle ? ' ' : '.';
65
66 if (!MSStyle)
67 Msg += ' ';
68 }
69 Msg += Pathname;
70 Msg += '\n';
71
72 OutputFile->write(Msg.data(), Msg.size());
73 OutputFile->flush();
74}
75
76void clang::AttachHeaderIncludeGen(Preprocessor &PP,
77 const std::vector<std::string> &ExtraHeaders,
78 bool ShowAllHeaders,
Hans Wennborg0fd62072013-08-09 00:32:23 +000079 StringRef OutputPath, bool ShowDepth,
80 bool MSStyle) {
Nico Weber25828952014-07-06 03:04:24 +000081 raw_ostream *OutputFile = MSStyle ? &llvm::outs() : &llvm::errs();
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000082 bool OwnsOutputFile = false;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000083
84 // Open the output file, if used.
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000085 if (!OutputPath.empty()) {
Rafael Espindoladae941a2014-08-25 18:17:04 +000086 std::error_code EC;
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000087 llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
Rafael Espindoladae941a2014-08-25 18:17:04 +000088 OutputPath.str(), EC, llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
89 if (EC) {
90 PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure)
91 << EC.message();
Daniel Dunbar9aa47fc2011-02-03 03:45:00 +000092 delete OS;
93 } else {
94 OS->SetUnbuffered();
95 OS->SetUseAtomicWrites(true);
96 OutputFile = OS;
97 OwnsOutputFile = true;
98 }
Daniel Dunbar1af1d27512011-02-02 21:11:31 +000099 }
100
Ivan Krasin1193f2c2015-08-13 04:04:37 +0000101 // Print header info for extra headers, pretending they were discovered
102 // by the regular preprocessor. The primary use case is to support
103 // proper generation of Make / Ninja file dependencies for implicit includes,
104 // such as sanitizer blacklists. It's only important for cl.exe
105 // compatibility, the GNU way to generate rules is -M / -MM / -MD / -MMD.
106 for (auto Header : ExtraHeaders) {
107 PrintHeaderInfo(OutputFile, Header.c_str(), ShowDepth, 2, MSStyle);
108 }
Craig Topperb8a70532014-09-10 04:53:53 +0000109 PP.addPPCallbacks(llvm::make_unique<HeaderIncludesCallback>(&PP,
110 ShowAllHeaders,
111 OutputFile,
112 OwnsOutputFile,
113 ShowDepth,
114 MSStyle));
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000115}
116
117void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
118 FileChangeReason Reason,
Argyrios Kyrtzidis7a70d2f2011-10-11 17:29:44 +0000119 SrcMgr::CharacteristicKind NewFileType,
120 FileID PrevFID) {
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000121 // Unless we are exiting a #include, make sure to skip ahead to the line the
122 // #include directive was at.
123 PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
124 if (UserLoc.isInvalid())
125 return;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +0000126
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000127 // Adjust the current include depth.
128 if (Reason == PPCallbacks::EnterFile) {
129 ++CurrentIncludeDepth;
Sebastian Redl4e521232011-04-14 14:07:45 +0000130 } else if (Reason == PPCallbacks::ExitFile) {
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000131 if (CurrentIncludeDepth)
132 --CurrentIncludeDepth;
133
134 // We track when we are done with the predefines by watching for the first
Sebastian Redl4e521232011-04-14 14:07:45 +0000135 // place where we drop back to a nesting depth of 1.
136 if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000137 HasProcessedPredefines = true;
Sebastian Redl4e521232011-04-14 14:07:45 +0000138
139 return;
140 } else
141 return;
Daniel Dunbarfb244852011-02-02 21:11:24 +0000142
143 // Show the header if we are (a) past the predefines, or (b) showing all
144 // headers and in the predefines at a depth past the initial file and command
145 // line buffers.
146 bool ShowHeader = (HasProcessedPredefines ||
147 (ShowAllHeaders && CurrentIncludeDepth > 2));
148
149 // Dump the header include information we are past the predefines buffer or
150 // are showing all headers.
151 if (ShowHeader && Reason == PPCallbacks::EnterFile) {
Ivan Krasin1193f2c2015-08-13 04:04:37 +0000152 PrintHeaderInfo(OutputFile, UserLoc.getFilename(),
153 ShowDepth, CurrentIncludeDepth, MSStyle);
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000154 }
155}