blob: a9b61282378dd8c434ca9e0c49af57498eb4a808 [file] [log] [blame]
Daniel Dunbarfa0caca2008-10-24 22:12:41 +00001//===--- DependencyFile.cpp - Generate dependency file --------------------===//
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// This code generates dependency files.
11//
12//===----------------------------------------------------------------------===//
13
Eli Friedman16b7b6f2009-05-19 04:14:29 +000014#include "clang/Frontend/Utils.h"
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000015#include "clang/Basic/FileManager.h"
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +000016#include "clang/Basic/SourceManager.h"
17#include "clang/Frontend/DependencyOutputOptions.h"
18#include "clang/Frontend/FrontendDiagnostic.h"
19#include "clang/Lex/DirectoryLookup.h"
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +000020#include "clang/Lex/LexDiagnostic.h"
Richard Smith2a6edb32015-08-09 04:46:57 +000021#include "clang/Lex/ModuleMap.h"
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +000022#include "clang/Lex/PPCallbacks.h"
23#include "clang/Lex/Preprocessor.h"
Ben Langmuircb69b572014-03-07 06:40:32 +000024#include "clang/Serialization/ASTReader.h"
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000025#include "llvm/ADT/StringSet.h"
David Majnemerf0822fb2014-10-27 22:31:50 +000026#include "llvm/ADT/StringSwitch.h"
Benjamin Kramer33d43302013-06-13 14:26:04 +000027#include "llvm/Support/FileSystem.h"
Eli Friedmanf7ca26a2011-07-08 20:17:28 +000028#include "llvm/Support/Path.h"
Daniel Dunbar966c2e12008-10-27 20:01:06 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000030
31using namespace clang;
32
33namespace {
Ben Langmuir33c80902014-06-30 20:04:14 +000034struct DepCollectorPPCallbacks : public PPCallbacks {
35 DependencyCollector &DepCollector;
36 SourceManager &SM;
37 DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM)
38 : DepCollector(L), SM(SM) { }
39
40 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
41 SrcMgr::CharacteristicKind FileType,
42 FileID PrevFID) override {
43 if (Reason != PPCallbacks::EnterFile)
44 return;
45
46 // Dependency generation really does want to go all the way to the
47 // file entry for a source location to find out what is depended on.
48 // We do not want #line markers to affect dependency generation!
49 const FileEntry *FE =
50 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
51 if (!FE)
52 return;
53
Douglas Katzman7f43af52015-09-02 21:14:53 +000054 StringRef Filename =
55 llvm::sys::path::remove_leading_dotslash(FE->getName());
Ben Langmuir33c80902014-06-30 20:04:14 +000056
57 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
58 FileType != SrcMgr::C_User,
59 /*IsModuleFile*/false, /*IsMissing*/false);
60 }
61
62 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
63 StringRef FileName, bool IsAngled,
64 CharSourceRange FilenameRange, const FileEntry *File,
65 StringRef SearchPath, StringRef RelativePath,
66 const Module *Imported) override {
67 if (!File)
68 DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
69 /*IsSystem*/false, /*IsModuleFile*/false,
70 /*IsMissing*/true);
71 // Files that actually exist are handled by FileChanged.
72 }
73
74 void EndOfMainFile() override {
75 DepCollector.finishedMainFile();
76 }
77};
78
Richard Smith2a6edb32015-08-09 04:46:57 +000079struct DepCollectorMMCallbacks : public ModuleMapCallbacks {
80 DependencyCollector &DepCollector;
81 DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {}
82
83 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
84 bool IsSystem) override {
85 StringRef Filename = Entry.getName();
86 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
87 /*IsSystem*/IsSystem,
88 /*IsModuleFile*/false,
89 /*IsMissing*/false);
90 }
91};
92
Ben Langmuir33c80902014-06-30 20:04:14 +000093struct DepCollectorASTListener : public ASTReaderListener {
94 DependencyCollector &DepCollector;
95 DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { }
96 bool needsInputFileVisitation() override { return true; }
97 bool needsSystemInputFileVisitation() override {
98 return DepCollector.needSystemDependencies();
99 }
Richard Smith216a3bd2015-08-13 17:57:10 +0000100 void visitModuleFile(StringRef Filename,
101 serialization::ModuleKind Kind) override {
Ben Langmuir33c80902014-06-30 20:04:14 +0000102 DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
103 /*IsSystem*/false, /*IsModuleFile*/true,
104 /*IsMissing*/false);
105 }
106 bool visitInputFile(StringRef Filename, bool IsSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000107 bool IsOverridden, bool IsExplicitModule) override {
108 if (IsOverridden || IsExplicitModule)
Ben Langmuir33c80902014-06-30 20:04:14 +0000109 return true;
110
111 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
112 /*IsModuleFile*/false, /*IsMissing*/false);
113 return true;
114 }
115};
116} // end anonymous namespace
117
118void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule,
119 bool IsSystem, bool IsModuleFile,
120 bool IsMissing) {
David Blaikie61b86d42014-11-19 02:56:13 +0000121 if (Seen.insert(Filename).second &&
Ben Langmuir33c80902014-06-30 20:04:14 +0000122 sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
123 Dependencies.push_back(Filename);
124}
125
David Majnemerf0822fb2014-10-27 22:31:50 +0000126static bool isSpecialFilename(StringRef Filename) {
127 return llvm::StringSwitch<bool>(Filename)
128 .Case("<built-in>", true)
129 .Case("<stdin>", true)
130 .Default(false);
131}
132
Ben Langmuir33c80902014-06-30 20:04:14 +0000133bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
134 bool IsSystem, bool IsModuleFile,
135 bool IsMissing) {
David Majnemerf0822fb2014-10-27 22:31:50 +0000136 return !isSpecialFilename(Filename) &&
137 (needSystemDependencies() || !IsSystem);
Ben Langmuir33c80902014-06-30 20:04:14 +0000138}
139
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000140DependencyCollector::~DependencyCollector() { }
Ben Langmuir33c80902014-06-30 20:04:14 +0000141void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
Craig Topperb8a70532014-09-10 04:53:53 +0000142 PP.addPPCallbacks(
143 llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager()));
Richard Smith2a6edb32015-08-09 04:46:57 +0000144 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
145 llvm::make_unique<DepCollectorMMCallbacks>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000146}
147void DependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +0000148 R.addListener(llvm::make_unique<DepCollectorASTListener>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000149}
150
151namespace {
Ben Langmuircb69b572014-03-07 06:40:32 +0000152/// Private implementation for DependencyFileGenerator
153class DFGImpl : public PPCallbacks {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000154 std::vector<std::string> Files;
155 llvm::StringSet<> FilesSet;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000156 const Preprocessor *PP;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000157 std::string OutputFile;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000158 std::vector<std::string> Targets;
Eli Friedman33cd03c2009-05-19 03:35:57 +0000159 bool IncludeSystemHeaders;
160 bool PhonyTarget;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000161 bool AddMissingHeaderDeps;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000162 bool SeenMissingHeader;
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000163 bool IncludeModuleFiles;
Paul Robinsond7214a72015-04-27 18:14:32 +0000164 DependencyOutputFormat OutputFormat;
165
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000166private:
167 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner66a740e2008-10-27 01:19:25 +0000168 SrcMgr::CharacteristicKind FileType);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000169 void OutputDependencyFile();
170
171public:
Ben Langmuircb69b572014-03-07 06:40:32 +0000172 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000173 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000174 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000175 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000176 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000177 SeenMissingHeader(false),
Paul Robinsond7214a72015-04-27 18:14:32 +0000178 IncludeModuleFiles(Opts.IncludeModuleFiles),
Ivan Krasin1193f2c2015-08-13 04:04:37 +0000179 OutputFormat(Opts.OutputFormat) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +0000180 for (const auto &ExtraDep : Opts.ExtraDeps) {
Ivan Krasin1193f2c2015-08-13 04:04:37 +0000181 AddFilename(ExtraDep);
182 }
183 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000184
Craig Topperafa7cb32014-03-13 06:07:04 +0000185 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
186 SrcMgr::CharacteristicKind FileType,
187 FileID PrevFID) override;
188 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
189 StringRef FileName, bool IsAngled,
190 CharSourceRange FilenameRange, const FileEntry *File,
191 StringRef SearchPath, StringRef RelativePath,
192 const Module *Imported) override;
Daniel Dunbarcb9eaf52010-03-23 05:09:10 +0000193
Craig Topperafa7cb32014-03-13 06:07:04 +0000194 void EndOfMainFile() override {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000195 OutputDependencyFile();
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000196 }
Ben Langmuircb69b572014-03-07 06:40:32 +0000197
198 void AddFilename(StringRef Filename);
199 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000200 bool includeModuleFiles() const { return IncludeModuleFiles; }
Ben Langmuircb69b572014-03-07 06:40:32 +0000201};
202
Richard Smith2a6edb32015-08-09 04:46:57 +0000203class DFGMMCallback : public ModuleMapCallbacks {
204 DFGImpl &Parent;
205public:
206 DFGMMCallback(DFGImpl &Parent) : Parent(Parent) {}
207 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
208 bool IsSystem) override {
209 if (!IsSystem || Parent.includeSystemHeaders())
210 Parent.AddFilename(Entry.getName());
211 }
212};
213
Ben Langmuircb69b572014-03-07 06:40:32 +0000214class DFGASTReaderListener : public ASTReaderListener {
215 DFGImpl &Parent;
216public:
217 DFGASTReaderListener(DFGImpl &Parent)
218 : Parent(Parent) { }
Craig Topperafa7cb32014-03-13 06:07:04 +0000219 bool needsInputFileVisitation() override { return true; }
220 bool needsSystemInputFileVisitation() override {
Ben Langmuircb69b572014-03-07 06:40:32 +0000221 return Parent.includeSystemHeaders();
222 }
Richard Smith216a3bd2015-08-13 17:57:10 +0000223 void visitModuleFile(StringRef Filename,
224 serialization::ModuleKind Kind) override;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000225 bool visitInputFile(StringRef Filename, bool isSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000226 bool isOverridden, bool isExplicitModule) override;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000227};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000228}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000229
Ben Langmuircb69b572014-03-07 06:40:32 +0000230DependencyFileGenerator::DependencyFileGenerator(void *Impl)
231: Impl(Impl) { }
232
233DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
234 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
235
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000236 if (Opts.Targets.empty()) {
Daniel Dunbar55b781f2009-11-11 21:44:00 +0000237 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Craig Topper49a27902014-05-22 04:46:25 +0000238 return nullptr;
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000239 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000240
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000241 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedman3781a362011-08-30 23:07:51 +0000242 if (Opts.AddMissingHeaderDeps)
243 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000244
Ben Langmuircb69b572014-03-07 06:40:32 +0000245 DFGImpl *Callback = new DFGImpl(&PP, Opts);
Craig Topperb8a70532014-09-10 04:53:53 +0000246 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback));
Richard Smith2a6edb32015-08-09 04:46:57 +0000247 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
248 llvm::make_unique<DFGMMCallback>(*Callback));
Ben Langmuircb69b572014-03-07 06:40:32 +0000249 return new DependencyFileGenerator(Callback);
250}
251
252void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
253 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
254 assert(I && "missing implementation");
David Blaikie2721c322014-08-10 16:54:39 +0000255 R.addListener(llvm::make_unique<DFGASTReaderListener>(*I));
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000256}
257
258/// FileMatchesDepCriteria - Determine whether the given Filename should be
259/// considered as a dependency.
Ben Langmuircb69b572014-03-07 06:40:32 +0000260bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
261 SrcMgr::CharacteristicKind FileType) {
David Majnemerf0822fb2014-10-27 22:31:50 +0000262 if (isSpecialFilename(Filename))
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000263 return false;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000264
Eli Friedman33cd03c2009-05-19 03:35:57 +0000265 if (IncludeSystemHeaders)
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000266 return true;
267
268 return FileType == SrcMgr::C_User;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000269}
270
Ben Langmuircb69b572014-03-07 06:40:32 +0000271void DFGImpl::FileChanged(SourceLocation Loc,
272 FileChangeReason Reason,
273 SrcMgr::CharacteristicKind FileType,
274 FileID PrevFID) {
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000275 if (Reason != PPCallbacks::EnterFile)
276 return;
Mike Stump11289f42009-09-09 15:08:12 +0000277
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000278 // Dependency generation really does want to go all the way to the
279 // file entry for a source location to find out what is depended on.
280 // We do not want #line markers to affect dependency generation!
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000281 SourceManager &SM = PP->getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000282
Chris Lattner64d8fc22009-01-28 05:42:38 +0000283 const FileEntry *FE =
Chandler Carruth35f53202011-07-25 16:49:02 +0000284 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Craig Topper49a27902014-05-22 04:46:25 +0000285 if (!FE) return;
Mike Stump11289f42009-09-09 15:08:12 +0000286
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000287 StringRef Filename = FE->getName();
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000288 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000289 return;
290
Douglas Katzman7f43af52015-09-02 21:14:53 +0000291 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000292}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000293
Ben Langmuircb69b572014-03-07 06:40:32 +0000294void DFGImpl::InclusionDirective(SourceLocation HashLoc,
295 const Token &IncludeTok,
296 StringRef FileName,
297 bool IsAngled,
298 CharSourceRange FilenameRange,
299 const FileEntry *File,
300 StringRef SearchPath,
301 StringRef RelativePath,
302 const Module *Imported) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000303 if (!File) {
304 if (AddMissingHeaderDeps)
305 AddFilename(FileName);
306 else
307 SeenMissingHeader = true;
308 }
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000309}
310
Ben Langmuircb69b572014-03-07 06:40:32 +0000311void DFGImpl::AddFilename(StringRef Filename) {
David Blaikie61b86d42014-11-19 02:56:13 +0000312 if (FilesSet.insert(Filename).second)
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000313 Files.push_back(Filename);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000314}
315
Paul Robinson64441de2015-05-13 21:18:15 +0000316/// Print the filename, with escaping or quoting that accommodates the three
317/// most likely tools that use dependency files: GNU Make, BSD Make, and
318/// NMake/Jom.
319///
320/// BSD Make is the simplest case: It does no escaping at all. This means
321/// characters that are normally delimiters, i.e. space and # (the comment
322/// character) simply aren't supported in filenames.
323///
324/// GNU Make does allow space and # in filenames, but to avoid being treated
325/// as a delimiter or comment, these must be escaped with a backslash. Because
326/// backslash is itself the escape character, if a backslash appears in a
327/// filename, it should be escaped as well. (As a special case, $ is escaped
328/// as $$, which is the normal Make way to handle the $ character.)
329/// For compatibility with BSD Make and historical practice, if GNU Make
330/// un-escapes characters in a filename but doesn't find a match, it will
331/// retry with the unmodified original string.
332///
333/// GCC tries to accommodate both Make formats by escaping any space or #
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000334/// characters in the original filename, but not escaping backslashes. The
335/// apparent intent is so that filenames with backslashes will be handled
Paul Robinson64441de2015-05-13 21:18:15 +0000336/// correctly by BSD Make, and by GNU Make in its fallback mode of using the
337/// unmodified original string; filenames with # or space characters aren't
338/// supported by BSD Make at all, but will be handled correctly by GNU Make
339/// due to the escaping.
340///
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000341/// A corner case that GCC gets only partly right is when the original filename
342/// has a backslash immediately followed by space or #. GNU Make would expect
343/// this backslash to be escaped; however GCC escapes the original backslash
344/// only when followed by space, not #. It will therefore take a dependency
345/// from a directive such as
346/// #include "a\ b\#c.h"
Paul Robinson64441de2015-05-13 21:18:15 +0000347/// and emit it as
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000348/// a\\\ b\\#c.h
Paul Robinson64441de2015-05-13 21:18:15 +0000349/// which GNU Make will interpret as
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000350/// a\ b\
Paul Robinson64441de2015-05-13 21:18:15 +0000351/// followed by a comment. Failing to find this file, it will fall back to the
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000352/// original string, which probably doesn't exist either; in any case it won't
353/// find
354/// a\ b\#c.h
Paul Robinson64441de2015-05-13 21:18:15 +0000355/// which is the actual filename specified by the include directive.
356///
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000357/// Clang does what GCC does, rather than what GNU Make expects.
Paul Robinson64441de2015-05-13 21:18:15 +0000358///
359/// NMake/Jom has a different set of scary characters, but wraps filespecs in
360/// double-quotes to avoid misinterpreting them; see
Paul Robinsond7214a72015-04-27 18:14:32 +0000361/// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
362/// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
363/// for Windows file-naming info.
364static void PrintFilename(raw_ostream &OS, StringRef Filename,
365 DependencyOutputFormat OutputFormat) {
366 if (OutputFormat == DependencyOutputFormat::NMake) {
367 // Add quotes if needed. These are the characters listed as "special" to
368 // NMake, that are legal in a Windows filespec, and that could cause
369 // misinterpretation of the dependency string.
370 if (Filename.find_first_of(" #${}^!") != StringRef::npos)
371 OS << '\"' << Filename << '\"';
372 else
373 OS << Filename;
374 return;
375 }
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000376 assert(OutputFormat == DependencyOutputFormat::Make);
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000377 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000378 if (Filename[i] == '#') // Handle '#' the broken gcc way.
379 OS << '\\';
380 else if (Filename[i] == ' ') { // Handle space correctly.
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000381 OS << '\\';
Paul Robinson64441de2015-05-13 21:18:15 +0000382 unsigned j = i;
383 while (j > 0 && Filename[--j] == '\\')
384 OS << '\\';
385 } else if (Filename[i] == '$') // $ is escaped by $$.
Benjamin Kramerae611512013-04-02 13:38:48 +0000386 OS << '$';
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000387 OS << Filename[i];
388 }
389}
390
Ben Langmuircb69b572014-03-07 06:40:32 +0000391void DFGImpl::OutputDependencyFile() {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000392 if (SeenMissingHeader) {
Rafael Espindola2a008782014-01-10 21:32:14 +0000393 llvm::sys::fs::remove(OutputFile);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000394 return;
395 }
396
Rafael Espindoladae941a2014-08-25 18:17:04 +0000397 std::error_code EC;
398 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
399 if (EC) {
400 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
401 << EC.message();
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000402 return;
403 }
404
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000405 // Write out the dependency targets, trying to avoid overly long
406 // lines when possible. We try our best to emit exactly the same
407 // dependency file as GCC (4.2), assuming the included files are the
408 // same.
409 const unsigned MaxColumns = 75;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000410 unsigned Columns = 0;
411
412 for (std::vector<std::string>::iterator
413 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
414 unsigned N = I->length();
415 if (Columns == 0) {
416 Columns += N;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000417 } else if (Columns + N + 2 > MaxColumns) {
418 Columns = N + 2;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000419 OS << " \\\n ";
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000420 } else {
421 Columns += N + 1;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000422 OS << ' ';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000423 }
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000424 // Targets already quoted as needed.
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000425 OS << *I;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000426 }
427
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000428 OS << ':';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000429 Columns += 1;
Mike Stump11289f42009-09-09 15:08:12 +0000430
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000431 // Now add each dependency in the order it was seen, but avoiding
432 // duplicates.
433 for (std::vector<std::string>::iterator I = Files.begin(),
434 E = Files.end(); I != E; ++I) {
435 // Start a new line if this would exceed the column limit. Make
436 // sure to leave space for a trailing " \" in case we need to
437 // break the line on the next iteration.
438 unsigned N = I->length();
439 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000440 OS << " \\\n ";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000441 Columns = 2;
442 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000443 OS << ' ';
Paul Robinsond7214a72015-04-27 18:14:32 +0000444 PrintFilename(OS, *I, OutputFormat);
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000445 Columns += N + 1;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000446 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000447 OS << '\n';
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000448
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000449 // Create phony targets if requested.
Fariborz Jahanian193f7832011-04-15 18:49:23 +0000450 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000451 // Skip the first entry, this is always the input file itself.
452 for (std::vector<std::string>::iterator I = Files.begin() + 1,
453 E = Files.end(); I != E; ++I) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000454 OS << '\n';
Paul Robinsond7214a72015-04-27 18:14:32 +0000455 PrintFilename(OS, *I, OutputFormat);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000456 OS << ":\n";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000457 }
458 }
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000459}
460
Ben Langmuircb69b572014-03-07 06:40:32 +0000461bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
Richard Smith216a3bd2015-08-13 17:57:10 +0000462 bool IsSystem, bool IsOverridden,
463 bool IsExplicitModule) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000464 assert(!IsSystem || needsSystemInputFileVisitation());
Richard Smith216a3bd2015-08-13 17:57:10 +0000465 if (IsOverridden || IsExplicitModule)
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000466 return true;
467
Ben Langmuircb69b572014-03-07 06:40:32 +0000468 Parent.AddFilename(Filename);
469 return true;
470}
471
Richard Smith216a3bd2015-08-13 17:57:10 +0000472void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename,
473 serialization::ModuleKind Kind) {
Richard Smithbe9b6c72015-08-13 18:30:25 +0000474 if (Parent.includeModuleFiles())
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000475 Parent.AddFilename(Filename);
476}