blob: 4e605523dc6a9a45c643eb35c1fe8b12d77eb56b [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,
Richard Smithf3f84612017-06-29 02:19:42 +000058 isSystem(FileType),
59 /*IsModuleFile*/false, /*IsMissing*/false);
Ben Langmuir33c80902014-06-30 20:04:14 +000060 }
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,
Julie Hockett96fbe582018-05-10 19:05:36 +000066 const Module *Imported,
67 SrcMgr::CharacteristicKind FileType) override {
Ben Langmuir33c80902014-06-30 20:04:14 +000068 if (!File)
69 DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
70 /*IsSystem*/false, /*IsModuleFile*/false,
71 /*IsMissing*/true);
72 // Files that actually exist are handled by FileChanged.
73 }
74
75 void EndOfMainFile() override {
76 DepCollector.finishedMainFile();
77 }
78};
79
Richard Smith2a6edb32015-08-09 04:46:57 +000080struct DepCollectorMMCallbacks : public ModuleMapCallbacks {
81 DependencyCollector &DepCollector;
82 DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {}
83
84 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
85 bool IsSystem) override {
86 StringRef Filename = Entry.getName();
87 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
88 /*IsSystem*/IsSystem,
89 /*IsModuleFile*/false,
90 /*IsMissing*/false);
91 }
92};
93
Ben Langmuir33c80902014-06-30 20:04:14 +000094struct DepCollectorASTListener : public ASTReaderListener {
95 DependencyCollector &DepCollector;
96 DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { }
97 bool needsInputFileVisitation() override { return true; }
98 bool needsSystemInputFileVisitation() override {
99 return DepCollector.needSystemDependencies();
100 }
Richard Smith216a3bd2015-08-13 17:57:10 +0000101 void visitModuleFile(StringRef Filename,
102 serialization::ModuleKind Kind) override {
Ben Langmuir33c80902014-06-30 20:04:14 +0000103 DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
104 /*IsSystem*/false, /*IsModuleFile*/true,
105 /*IsMissing*/false);
106 }
107 bool visitInputFile(StringRef Filename, bool IsSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000108 bool IsOverridden, bool IsExplicitModule) override {
109 if (IsOverridden || IsExplicitModule)
Ben Langmuir33c80902014-06-30 20:04:14 +0000110 return true;
111
112 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
113 /*IsModuleFile*/false, /*IsMissing*/false);
114 return true;
115 }
116};
117} // end anonymous namespace
118
119void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule,
120 bool IsSystem, bool IsModuleFile,
121 bool IsMissing) {
David Blaikie61b86d42014-11-19 02:56:13 +0000122 if (Seen.insert(Filename).second &&
Ben Langmuir33c80902014-06-30 20:04:14 +0000123 sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
124 Dependencies.push_back(Filename);
125}
126
David Majnemerf0822fb2014-10-27 22:31:50 +0000127static bool isSpecialFilename(StringRef Filename) {
128 return llvm::StringSwitch<bool>(Filename)
129 .Case("<built-in>", true)
130 .Case("<stdin>", true)
131 .Default(false);
132}
133
Ben Langmuir33c80902014-06-30 20:04:14 +0000134bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
135 bool IsSystem, bool IsModuleFile,
136 bool IsMissing) {
David Majnemerf0822fb2014-10-27 22:31:50 +0000137 return !isSpecialFilename(Filename) &&
138 (needSystemDependencies() || !IsSystem);
Ben Langmuir33c80902014-06-30 20:04:14 +0000139}
140
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000141DependencyCollector::~DependencyCollector() { }
Ben Langmuir33c80902014-06-30 20:04:14 +0000142void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
Craig Topperb8a70532014-09-10 04:53:53 +0000143 PP.addPPCallbacks(
144 llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager()));
Richard Smith2a6edb32015-08-09 04:46:57 +0000145 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
146 llvm::make_unique<DepCollectorMMCallbacks>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000147}
148void DependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +0000149 R.addListener(llvm::make_unique<DepCollectorASTListener>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000150}
151
152namespace {
Ben Langmuircb69b572014-03-07 06:40:32 +0000153/// Private implementation for DependencyFileGenerator
154class DFGImpl : public PPCallbacks {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000155 std::vector<std::string> Files;
156 llvm::StringSet<> FilesSet;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000157 const Preprocessor *PP;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000158 std::string OutputFile;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000159 std::vector<std::string> Targets;
Eli Friedman33cd03c2009-05-19 03:35:57 +0000160 bool IncludeSystemHeaders;
161 bool PhonyTarget;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000162 bool AddMissingHeaderDeps;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000163 bool SeenMissingHeader;
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000164 bool IncludeModuleFiles;
Paul Robinsond7214a72015-04-27 18:14:32 +0000165 DependencyOutputFormat OutputFormat;
166
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000167private:
168 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner66a740e2008-10-27 01:19:25 +0000169 SrcMgr::CharacteristicKind FileType);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000170 void OutputDependencyFile();
171
172public:
Ben Langmuircb69b572014-03-07 06:40:32 +0000173 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000174 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000175 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000176 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000177 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000178 SeenMissingHeader(false),
Paul Robinsond7214a72015-04-27 18:14:32 +0000179 IncludeModuleFiles(Opts.IncludeModuleFiles),
Ivan Krasin1193f2c2015-08-13 04:04:37 +0000180 OutputFormat(Opts.OutputFormat) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +0000181 for (const auto &ExtraDep : Opts.ExtraDeps) {
Ivan Krasin1193f2c2015-08-13 04:04:37 +0000182 AddFilename(ExtraDep);
183 }
184 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000185
Craig Topperafa7cb32014-03-13 06:07:04 +0000186 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
187 SrcMgr::CharacteristicKind FileType,
188 FileID PrevFID) override;
Volodymyr Sapsai210f0e82018-05-01 23:59:33 +0000189
190 void FileSkipped(const FileEntry &SkippedFile, const Token &FilenameTok,
191 SrcMgr::CharacteristicKind FileType) override;
192
Craig Topperafa7cb32014-03-13 06:07:04 +0000193 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
194 StringRef FileName, bool IsAngled,
195 CharSourceRange FilenameRange, const FileEntry *File,
196 StringRef SearchPath, StringRef RelativePath,
Julie Hockett96fbe582018-05-10 19:05:36 +0000197 const Module *Imported,
198 SrcMgr::CharacteristicKind FileType) override;
Daniel Dunbarcb9eaf52010-03-23 05:09:10 +0000199
Craig Topperafa7cb32014-03-13 06:07:04 +0000200 void EndOfMainFile() override {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000201 OutputDependencyFile();
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000202 }
Ben Langmuircb69b572014-03-07 06:40:32 +0000203
204 void AddFilename(StringRef Filename);
205 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000206 bool includeModuleFiles() const { return IncludeModuleFiles; }
Ben Langmuircb69b572014-03-07 06:40:32 +0000207};
208
Richard Smith2a6edb32015-08-09 04:46:57 +0000209class DFGMMCallback : public ModuleMapCallbacks {
210 DFGImpl &Parent;
211public:
212 DFGMMCallback(DFGImpl &Parent) : Parent(Parent) {}
213 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
214 bool IsSystem) override {
215 if (!IsSystem || Parent.includeSystemHeaders())
216 Parent.AddFilename(Entry.getName());
217 }
218};
219
Ben Langmuircb69b572014-03-07 06:40:32 +0000220class DFGASTReaderListener : public ASTReaderListener {
221 DFGImpl &Parent;
222public:
223 DFGASTReaderListener(DFGImpl &Parent)
224 : Parent(Parent) { }
Craig Topperafa7cb32014-03-13 06:07:04 +0000225 bool needsInputFileVisitation() override { return true; }
226 bool needsSystemInputFileVisitation() override {
Ben Langmuircb69b572014-03-07 06:40:32 +0000227 return Parent.includeSystemHeaders();
228 }
Richard Smith216a3bd2015-08-13 17:57:10 +0000229 void visitModuleFile(StringRef Filename,
230 serialization::ModuleKind Kind) override;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000231 bool visitInputFile(StringRef Filename, bool isSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000232 bool isOverridden, bool isExplicitModule) override;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000233};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000234}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000235
Ben Langmuircb69b572014-03-07 06:40:32 +0000236DependencyFileGenerator::DependencyFileGenerator(void *Impl)
237: Impl(Impl) { }
238
239DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
240 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
241
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000242 if (Opts.Targets.empty()) {
Daniel Dunbar55b781f2009-11-11 21:44:00 +0000243 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Craig Topper49a27902014-05-22 04:46:25 +0000244 return nullptr;
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000245 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000246
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000247 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedman3781a362011-08-30 23:07:51 +0000248 if (Opts.AddMissingHeaderDeps)
249 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000250
Ben Langmuircb69b572014-03-07 06:40:32 +0000251 DFGImpl *Callback = new DFGImpl(&PP, Opts);
Craig Topperb8a70532014-09-10 04:53:53 +0000252 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback));
Richard Smith2a6edb32015-08-09 04:46:57 +0000253 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
254 llvm::make_unique<DFGMMCallback>(*Callback));
Ben Langmuircb69b572014-03-07 06:40:32 +0000255 return new DependencyFileGenerator(Callback);
256}
257
258void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
259 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
260 assert(I && "missing implementation");
David Blaikie2721c322014-08-10 16:54:39 +0000261 R.addListener(llvm::make_unique<DFGASTReaderListener>(*I));
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000262}
263
264/// FileMatchesDepCriteria - Determine whether the given Filename should be
265/// considered as a dependency.
Ben Langmuircb69b572014-03-07 06:40:32 +0000266bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
267 SrcMgr::CharacteristicKind FileType) {
David Majnemerf0822fb2014-10-27 22:31:50 +0000268 if (isSpecialFilename(Filename))
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000269 return false;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000270
Eli Friedman33cd03c2009-05-19 03:35:57 +0000271 if (IncludeSystemHeaders)
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000272 return true;
273
Richard Smithf3f84612017-06-29 02:19:42 +0000274 return !isSystem(FileType);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000275}
276
Ben Langmuircb69b572014-03-07 06:40:32 +0000277void DFGImpl::FileChanged(SourceLocation Loc,
278 FileChangeReason Reason,
279 SrcMgr::CharacteristicKind FileType,
280 FileID PrevFID) {
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000281 if (Reason != PPCallbacks::EnterFile)
282 return;
Mike Stump11289f42009-09-09 15:08:12 +0000283
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000284 // Dependency generation really does want to go all the way to the
285 // file entry for a source location to find out what is depended on.
286 // We do not want #line markers to affect dependency generation!
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000287 SourceManager &SM = PP->getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattner64d8fc22009-01-28 05:42:38 +0000289 const FileEntry *FE =
Chandler Carruth35f53202011-07-25 16:49:02 +0000290 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Craig Topper49a27902014-05-22 04:46:25 +0000291 if (!FE) return;
Mike Stump11289f42009-09-09 15:08:12 +0000292
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000293 StringRef Filename = FE->getName();
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000294 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000295 return;
296
Douglas Katzman7f43af52015-09-02 21:14:53 +0000297 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000298}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000299
Volodymyr Sapsai210f0e82018-05-01 23:59:33 +0000300void DFGImpl::FileSkipped(const FileEntry &SkippedFile,
301 const Token &FilenameTok,
302 SrcMgr::CharacteristicKind FileType) {
303 StringRef Filename = SkippedFile.getName();
304 if (!FileMatchesDepCriteria(Filename.data(), FileType))
305 return;
306
307 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
308}
309
Ben Langmuircb69b572014-03-07 06:40:32 +0000310void DFGImpl::InclusionDirective(SourceLocation HashLoc,
311 const Token &IncludeTok,
312 StringRef FileName,
313 bool IsAngled,
314 CharSourceRange FilenameRange,
315 const FileEntry *File,
316 StringRef SearchPath,
317 StringRef RelativePath,
Julie Hockett96fbe582018-05-10 19:05:36 +0000318 const Module *Imported,
319 SrcMgr::CharacteristicKind FileType) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000320 if (!File) {
321 if (AddMissingHeaderDeps)
322 AddFilename(FileName);
323 else
324 SeenMissingHeader = true;
325 }
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000326}
327
Ben Langmuircb69b572014-03-07 06:40:32 +0000328void DFGImpl::AddFilename(StringRef Filename) {
David Blaikie61b86d42014-11-19 02:56:13 +0000329 if (FilesSet.insert(Filename).second)
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000330 Files.push_back(Filename);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000331}
332
Paul Robinson64441de2015-05-13 21:18:15 +0000333/// Print the filename, with escaping or quoting that accommodates the three
334/// most likely tools that use dependency files: GNU Make, BSD Make, and
335/// NMake/Jom.
336///
337/// BSD Make is the simplest case: It does no escaping at all. This means
338/// characters that are normally delimiters, i.e. space and # (the comment
339/// character) simply aren't supported in filenames.
340///
341/// GNU Make does allow space and # in filenames, but to avoid being treated
342/// as a delimiter or comment, these must be escaped with a backslash. Because
343/// backslash is itself the escape character, if a backslash appears in a
344/// filename, it should be escaped as well. (As a special case, $ is escaped
345/// as $$, which is the normal Make way to handle the $ character.)
346/// For compatibility with BSD Make and historical practice, if GNU Make
347/// un-escapes characters in a filename but doesn't find a match, it will
348/// retry with the unmodified original string.
349///
350/// GCC tries to accommodate both Make formats by escaping any space or #
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000351/// characters in the original filename, but not escaping backslashes. The
352/// apparent intent is so that filenames with backslashes will be handled
Paul Robinson64441de2015-05-13 21:18:15 +0000353/// correctly by BSD Make, and by GNU Make in its fallback mode of using the
354/// unmodified original string; filenames with # or space characters aren't
355/// supported by BSD Make at all, but will be handled correctly by GNU Make
356/// due to the escaping.
357///
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000358/// A corner case that GCC gets only partly right is when the original filename
359/// has a backslash immediately followed by space or #. GNU Make would expect
360/// this backslash to be escaped; however GCC escapes the original backslash
361/// only when followed by space, not #. It will therefore take a dependency
362/// from a directive such as
363/// #include "a\ b\#c.h"
Paul Robinson64441de2015-05-13 21:18:15 +0000364/// and emit it as
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000365/// a\\\ b\\#c.h
Paul Robinson64441de2015-05-13 21:18:15 +0000366/// which GNU Make will interpret as
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000367/// a\ b\
Paul Robinson64441de2015-05-13 21:18:15 +0000368/// followed by a comment. Failing to find this file, it will fall back to the
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000369/// original string, which probably doesn't exist either; in any case it won't
370/// find
371/// a\ b\#c.h
Paul Robinson64441de2015-05-13 21:18:15 +0000372/// which is the actual filename specified by the include directive.
373///
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000374/// Clang does what GCC does, rather than what GNU Make expects.
Paul Robinson64441de2015-05-13 21:18:15 +0000375///
376/// NMake/Jom has a different set of scary characters, but wraps filespecs in
377/// double-quotes to avoid misinterpreting them; see
Paul Robinsond7214a72015-04-27 18:14:32 +0000378/// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
379/// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
380/// for Windows file-naming info.
381static void PrintFilename(raw_ostream &OS, StringRef Filename,
382 DependencyOutputFormat OutputFormat) {
383 if (OutputFormat == DependencyOutputFormat::NMake) {
384 // Add quotes if needed. These are the characters listed as "special" to
385 // NMake, that are legal in a Windows filespec, and that could cause
386 // misinterpretation of the dependency string.
387 if (Filename.find_first_of(" #${}^!") != StringRef::npos)
388 OS << '\"' << Filename << '\"';
389 else
390 OS << Filename;
391 return;
392 }
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000393 assert(OutputFormat == DependencyOutputFormat::Make);
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000394 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000395 if (Filename[i] == '#') // Handle '#' the broken gcc way.
396 OS << '\\';
397 else if (Filename[i] == ' ') { // Handle space correctly.
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000398 OS << '\\';
Paul Robinson64441de2015-05-13 21:18:15 +0000399 unsigned j = i;
400 while (j > 0 && Filename[--j] == '\\')
401 OS << '\\';
402 } else if (Filename[i] == '$') // $ is escaped by $$.
Benjamin Kramerae611512013-04-02 13:38:48 +0000403 OS << '$';
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000404 OS << Filename[i];
405 }
406}
407
Ben Langmuircb69b572014-03-07 06:40:32 +0000408void DFGImpl::OutputDependencyFile() {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000409 if (SeenMissingHeader) {
Rafael Espindola2a008782014-01-10 21:32:14 +0000410 llvm::sys::fs::remove(OutputFile);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000411 return;
412 }
413
Rafael Espindoladae941a2014-08-25 18:17:04 +0000414 std::error_code EC;
415 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
416 if (EC) {
417 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
418 << EC.message();
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000419 return;
420 }
421
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000422 // Write out the dependency targets, trying to avoid overly long
423 // lines when possible. We try our best to emit exactly the same
424 // dependency file as GCC (4.2), assuming the included files are the
425 // same.
426 const unsigned MaxColumns = 75;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000427 unsigned Columns = 0;
428
Yaron Keren3998a092016-11-16 19:24:10 +0000429 for (StringRef Target : Targets) {
430 unsigned N = Target.size();
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000431 if (Columns == 0) {
432 Columns += N;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000433 } else if (Columns + N + 2 > MaxColumns) {
434 Columns = N + 2;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000435 OS << " \\\n ";
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000436 } else {
437 Columns += N + 1;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000438 OS << ' ';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000439 }
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000440 // Targets already quoted as needed.
Yaron Keren3998a092016-11-16 19:24:10 +0000441 OS << Target;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000442 }
443
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000444 OS << ':';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000445 Columns += 1;
Mike Stump11289f42009-09-09 15:08:12 +0000446
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000447 // Now add each dependency in the order it was seen, but avoiding
448 // duplicates.
Yaron Keren3998a092016-11-16 19:24:10 +0000449 for (StringRef File : Files) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000450 // Start a new line if this would exceed the column limit. Make
451 // sure to leave space for a trailing " \" in case we need to
452 // break the line on the next iteration.
Yaron Keren3998a092016-11-16 19:24:10 +0000453 unsigned N = File.size();
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000454 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000455 OS << " \\\n ";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000456 Columns = 2;
457 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000458 OS << ' ';
Yaron Keren3998a092016-11-16 19:24:10 +0000459 PrintFilename(OS, File, OutputFormat);
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000460 Columns += N + 1;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000461 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000462 OS << '\n';
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000463
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000464 // Create phony targets if requested.
Fariborz Jahanian193f7832011-04-15 18:49:23 +0000465 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000466 // Skip the first entry, this is always the input file itself.
Yaron Kerenfa367402017-01-14 21:12:08 +0000467 for (auto I = Files.begin() + 1, E = Files.end(); I != E; ++I) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000468 OS << '\n';
Yaron Kerenfa367402017-01-14 21:12:08 +0000469 PrintFilename(OS, *I, OutputFormat);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000470 OS << ":\n";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000471 }
472 }
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000473}
474
Ben Langmuircb69b572014-03-07 06:40:32 +0000475bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
Richard Smith216a3bd2015-08-13 17:57:10 +0000476 bool IsSystem, bool IsOverridden,
477 bool IsExplicitModule) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000478 assert(!IsSystem || needsSystemInputFileVisitation());
Richard Smith216a3bd2015-08-13 17:57:10 +0000479 if (IsOverridden || IsExplicitModule)
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000480 return true;
481
Ben Langmuircb69b572014-03-07 06:40:32 +0000482 Parent.AddFilename(Filename);
483 return true;
484}
485
Richard Smith216a3bd2015-08-13 17:57:10 +0000486void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename,
487 serialization::ModuleKind Kind) {
Richard Smithbe9b6c72015-08-13 18:30:25 +0000488 if (Parent.includeModuleFiles())
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000489 Parent.AddFilename(Filename);
490}