blob: 363aff76f0224281ba544a52f7f494264c619e5a [file] [log] [blame]
Daniel Dunbarfa0caca2008-10-24 22:12:41 +00001//===--- DependencyFile.cpp - Generate dependency file --------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Dunbarfa0caca2008-10-24 22:12:41 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This code generates dependency files.
10//
11//===----------------------------------------------------------------------===//
12
Eli Friedman16b7b6f2009-05-19 04:14:29 +000013#include "clang/Frontend/Utils.h"
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000014#include "clang/Basic/FileManager.h"
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +000015#include "clang/Basic/SourceManager.h"
16#include "clang/Frontend/DependencyOutputOptions.h"
17#include "clang/Frontend/FrontendDiagnostic.h"
18#include "clang/Lex/DirectoryLookup.h"
Richard Smith2a6edb32015-08-09 04:46:57 +000019#include "clang/Lex/ModuleMap.h"
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +000020#include "clang/Lex/PPCallbacks.h"
21#include "clang/Lex/Preprocessor.h"
Ben Langmuircb69b572014-03-07 06:40:32 +000022#include "clang/Serialization/ASTReader.h"
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000023#include "llvm/ADT/StringSet.h"
David Majnemerf0822fb2014-10-27 22:31:50 +000024#include "llvm/ADT/StringSwitch.h"
Benjamin Kramer33d43302013-06-13 14:26:04 +000025#include "llvm/Support/FileSystem.h"
Eli Friedmanf7ca26a2011-07-08 20:17:28 +000026#include "llvm/Support/Path.h"
Daniel Dunbar966c2e12008-10-27 20:01:06 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000028
29using namespace clang;
30
31namespace {
Ben Langmuir33c80902014-06-30 20:04:14 +000032struct DepCollectorPPCallbacks : public PPCallbacks {
33 DependencyCollector &DepCollector;
34 SourceManager &SM;
35 DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM)
36 : DepCollector(L), SM(SM) { }
37
38 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
39 SrcMgr::CharacteristicKind FileType,
40 FileID PrevFID) override {
41 if (Reason != PPCallbacks::EnterFile)
42 return;
43
44 // Dependency generation really does want to go all the way to the
45 // file entry for a source location to find out what is depended on.
46 // We do not want #line markers to affect dependency generation!
47 const FileEntry *FE =
48 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
49 if (!FE)
50 return;
51
Douglas Katzman7f43af52015-09-02 21:14:53 +000052 StringRef Filename =
53 llvm::sys::path::remove_leading_dotslash(FE->getName());
Ben Langmuir33c80902014-06-30 20:04:14 +000054
55 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
Richard Smithf3f84612017-06-29 02:19:42 +000056 isSystem(FileType),
57 /*IsModuleFile*/false, /*IsMissing*/false);
Ben Langmuir33c80902014-06-30 20:04:14 +000058 }
59
60 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
61 StringRef FileName, bool IsAngled,
62 CharSourceRange FilenameRange, const FileEntry *File,
63 StringRef SearchPath, StringRef RelativePath,
Julie Hockett96fbe582018-05-10 19:05:36 +000064 const Module *Imported,
65 SrcMgr::CharacteristicKind FileType) override {
Ben Langmuir33c80902014-06-30 20:04:14 +000066 if (!File)
67 DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
68 /*IsSystem*/false, /*IsModuleFile*/false,
69 /*IsMissing*/true);
70 // Files that actually exist are handled by FileChanged.
71 }
72
73 void EndOfMainFile() override {
74 DepCollector.finishedMainFile();
75 }
76};
77
Richard Smith2a6edb32015-08-09 04:46:57 +000078struct DepCollectorMMCallbacks : public ModuleMapCallbacks {
79 DependencyCollector &DepCollector;
80 DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {}
81
82 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
83 bool IsSystem) override {
84 StringRef Filename = Entry.getName();
85 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
86 /*IsSystem*/IsSystem,
87 /*IsModuleFile*/false,
88 /*IsMissing*/false);
89 }
90};
91
Ben Langmuir33c80902014-06-30 20:04:14 +000092struct DepCollectorASTListener : public ASTReaderListener {
93 DependencyCollector &DepCollector;
94 DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { }
95 bool needsInputFileVisitation() override { return true; }
96 bool needsSystemInputFileVisitation() override {
97 return DepCollector.needSystemDependencies();
98 }
Richard Smith216a3bd2015-08-13 17:57:10 +000099 void visitModuleFile(StringRef Filename,
100 serialization::ModuleKind Kind) override {
Ben Langmuir33c80902014-06-30 20:04:14 +0000101 DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
102 /*IsSystem*/false, /*IsModuleFile*/true,
103 /*IsMissing*/false);
104 }
105 bool visitInputFile(StringRef Filename, bool IsSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000106 bool IsOverridden, bool IsExplicitModule) override {
107 if (IsOverridden || IsExplicitModule)
Ben Langmuir33c80902014-06-30 20:04:14 +0000108 return true;
109
110 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
111 /*IsModuleFile*/false, /*IsMissing*/false);
112 return true;
113 }
114};
115} // end anonymous namespace
116
117void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule,
118 bool IsSystem, bool IsModuleFile,
119 bool IsMissing) {
David Blaikie61b86d42014-11-19 02:56:13 +0000120 if (Seen.insert(Filename).second &&
Ben Langmuir33c80902014-06-30 20:04:14 +0000121 sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
122 Dependencies.push_back(Filename);
123}
124
David Majnemerf0822fb2014-10-27 22:31:50 +0000125static bool isSpecialFilename(StringRef Filename) {
126 return llvm::StringSwitch<bool>(Filename)
127 .Case("<built-in>", true)
128 .Case("<stdin>", true)
129 .Default(false);
130}
131
Ben Langmuir33c80902014-06-30 20:04:14 +0000132bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
133 bool IsSystem, bool IsModuleFile,
134 bool IsMissing) {
David Majnemerf0822fb2014-10-27 22:31:50 +0000135 return !isSpecialFilename(Filename) &&
136 (needSystemDependencies() || !IsSystem);
Ben Langmuir33c80902014-06-30 20:04:14 +0000137}
138
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000139DependencyCollector::~DependencyCollector() { }
Ben Langmuir33c80902014-06-30 20:04:14 +0000140void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
Craig Topperb8a70532014-09-10 04:53:53 +0000141 PP.addPPCallbacks(
142 llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager()));
Richard Smith2a6edb32015-08-09 04:46:57 +0000143 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
144 llvm::make_unique<DepCollectorMMCallbacks>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000145}
146void DependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +0000147 R.addListener(llvm::make_unique<DepCollectorASTListener>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000148}
149
150namespace {
Ben Langmuircb69b572014-03-07 06:40:32 +0000151/// Private implementation for DependencyFileGenerator
152class DFGImpl : public PPCallbacks {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000153 std::vector<std::string> Files;
154 llvm::StringSet<> FilesSet;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000155 const Preprocessor *PP;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000156 std::string OutputFile;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000157 std::vector<std::string> Targets;
Eli Friedman33cd03c2009-05-19 03:35:57 +0000158 bool IncludeSystemHeaders;
159 bool PhonyTarget;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000160 bool AddMissingHeaderDeps;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000161 bool SeenMissingHeader;
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000162 bool IncludeModuleFiles;
Paul Robinsond7214a72015-04-27 18:14:32 +0000163 DependencyOutputFormat OutputFormat;
David Stenbergd45d7ea2018-05-29 13:07:58 +0000164 unsigned InputFileIndex;
Paul Robinsond7214a72015-04-27 18:14:32 +0000165
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),
David Stenbergd45d7ea2018-05-29 13:07:58 +0000179 OutputFormat(Opts.OutputFormat),
180 InputFileIndex(0) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +0000181 for (const auto &ExtraDep : Opts.ExtraDeps) {
David Stenbergd45d7ea2018-05-29 13:07:58 +0000182 if (AddFilename(ExtraDep))
183 ++InputFileIndex;
Ivan Krasin1193f2c2015-08-13 04:04:37 +0000184 }
185 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000186
Craig Topperafa7cb32014-03-13 06:07:04 +0000187 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
188 SrcMgr::CharacteristicKind FileType,
189 FileID PrevFID) override;
Volodymyr Sapsai210f0e82018-05-01 23:59:33 +0000190
191 void FileSkipped(const FileEntry &SkippedFile, const Token &FilenameTok,
192 SrcMgr::CharacteristicKind FileType) override;
193
Craig Topperafa7cb32014-03-13 06:07:04 +0000194 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
195 StringRef FileName, bool IsAngled,
196 CharSourceRange FilenameRange, const FileEntry *File,
197 StringRef SearchPath, StringRef RelativePath,
Julie Hockett96fbe582018-05-10 19:05:36 +0000198 const Module *Imported,
199 SrcMgr::CharacteristicKind FileType) override;
Daniel Dunbarcb9eaf52010-03-23 05:09:10 +0000200
Volodymyr Sapsaia4c53282018-09-18 23:27:02 +0000201 void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled,
202 const FileEntry *File,
203 SrcMgr::CharacteristicKind FileType) override;
204
Craig Topperafa7cb32014-03-13 06:07:04 +0000205 void EndOfMainFile() override {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000206 OutputDependencyFile();
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000207 }
Ben Langmuircb69b572014-03-07 06:40:32 +0000208
David Stenbergd45d7ea2018-05-29 13:07:58 +0000209 bool AddFilename(StringRef Filename);
Ben Langmuircb69b572014-03-07 06:40:32 +0000210 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000211 bool includeModuleFiles() const { return IncludeModuleFiles; }
Ben Langmuircb69b572014-03-07 06:40:32 +0000212};
213
Richard Smith2a6edb32015-08-09 04:46:57 +0000214class DFGMMCallback : public ModuleMapCallbacks {
215 DFGImpl &Parent;
216public:
217 DFGMMCallback(DFGImpl &Parent) : Parent(Parent) {}
218 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
219 bool IsSystem) override {
220 if (!IsSystem || Parent.includeSystemHeaders())
221 Parent.AddFilename(Entry.getName());
222 }
223};
224
Ben Langmuircb69b572014-03-07 06:40:32 +0000225class DFGASTReaderListener : public ASTReaderListener {
226 DFGImpl &Parent;
227public:
228 DFGASTReaderListener(DFGImpl &Parent)
229 : Parent(Parent) { }
Craig Topperafa7cb32014-03-13 06:07:04 +0000230 bool needsInputFileVisitation() override { return true; }
231 bool needsSystemInputFileVisitation() override {
Ben Langmuircb69b572014-03-07 06:40:32 +0000232 return Parent.includeSystemHeaders();
233 }
Richard Smith216a3bd2015-08-13 17:57:10 +0000234 void visitModuleFile(StringRef Filename,
235 serialization::ModuleKind Kind) override;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000236 bool visitInputFile(StringRef Filename, bool isSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000237 bool isOverridden, bool isExplicitModule) override;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000238};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000239}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000240
Ben Langmuircb69b572014-03-07 06:40:32 +0000241DependencyFileGenerator::DependencyFileGenerator(void *Impl)
242: Impl(Impl) { }
243
244DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
245 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
246
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000247 if (Opts.Targets.empty()) {
Daniel Dunbar55b781f2009-11-11 21:44:00 +0000248 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Craig Topper49a27902014-05-22 04:46:25 +0000249 return nullptr;
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000250 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000251
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000252 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedman3781a362011-08-30 23:07:51 +0000253 if (Opts.AddMissingHeaderDeps)
254 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000255
Ben Langmuircb69b572014-03-07 06:40:32 +0000256 DFGImpl *Callback = new DFGImpl(&PP, Opts);
Craig Topperb8a70532014-09-10 04:53:53 +0000257 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback));
Richard Smith2a6edb32015-08-09 04:46:57 +0000258 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
259 llvm::make_unique<DFGMMCallback>(*Callback));
Ben Langmuircb69b572014-03-07 06:40:32 +0000260 return new DependencyFileGenerator(Callback);
261}
262
263void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
264 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
265 assert(I && "missing implementation");
David Blaikie2721c322014-08-10 16:54:39 +0000266 R.addListener(llvm::make_unique<DFGASTReaderListener>(*I));
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000267}
268
269/// FileMatchesDepCriteria - Determine whether the given Filename should be
270/// considered as a dependency.
Ben Langmuircb69b572014-03-07 06:40:32 +0000271bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
272 SrcMgr::CharacteristicKind FileType) {
David Majnemerf0822fb2014-10-27 22:31:50 +0000273 if (isSpecialFilename(Filename))
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000274 return false;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000275
Eli Friedman33cd03c2009-05-19 03:35:57 +0000276 if (IncludeSystemHeaders)
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000277 return true;
278
Richard Smithf3f84612017-06-29 02:19:42 +0000279 return !isSystem(FileType);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000280}
281
Ben Langmuircb69b572014-03-07 06:40:32 +0000282void DFGImpl::FileChanged(SourceLocation Loc,
283 FileChangeReason Reason,
284 SrcMgr::CharacteristicKind FileType,
285 FileID PrevFID) {
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000286 if (Reason != PPCallbacks::EnterFile)
287 return;
Mike Stump11289f42009-09-09 15:08:12 +0000288
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000289 // Dependency generation really does want to go all the way to the
290 // file entry for a source location to find out what is depended on.
291 // We do not want #line markers to affect dependency generation!
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000292 SourceManager &SM = PP->getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000293
Chris Lattner64d8fc22009-01-28 05:42:38 +0000294 const FileEntry *FE =
Chandler Carruth35f53202011-07-25 16:49:02 +0000295 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Craig Topper49a27902014-05-22 04:46:25 +0000296 if (!FE) return;
Mike Stump11289f42009-09-09 15:08:12 +0000297
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000298 StringRef Filename = FE->getName();
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000299 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000300 return;
301
Douglas Katzman7f43af52015-09-02 21:14:53 +0000302 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000303}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000304
Volodymyr Sapsai210f0e82018-05-01 23:59:33 +0000305void DFGImpl::FileSkipped(const FileEntry &SkippedFile,
306 const Token &FilenameTok,
307 SrcMgr::CharacteristicKind FileType) {
308 StringRef Filename = SkippedFile.getName();
309 if (!FileMatchesDepCriteria(Filename.data(), FileType))
310 return;
311
312 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
313}
314
Ben Langmuircb69b572014-03-07 06:40:32 +0000315void DFGImpl::InclusionDirective(SourceLocation HashLoc,
316 const Token &IncludeTok,
317 StringRef FileName,
318 bool IsAngled,
319 CharSourceRange FilenameRange,
320 const FileEntry *File,
321 StringRef SearchPath,
322 StringRef RelativePath,
Fangrui Song6907ce22018-07-30 19:24:48 +0000323 const Module *Imported,
Julie Hockett96fbe582018-05-10 19:05:36 +0000324 SrcMgr::CharacteristicKind FileType) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000325 if (!File) {
326 if (AddMissingHeaderDeps)
327 AddFilename(FileName);
328 else
329 SeenMissingHeader = true;
330 }
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000331}
332
Volodymyr Sapsaia4c53282018-09-18 23:27:02 +0000333void DFGImpl::HasInclude(SourceLocation Loc, StringRef SpelledFilename,
334 bool IsAngled, const FileEntry *File,
335 SrcMgr::CharacteristicKind FileType) {
336 if (!File)
337 return;
338 StringRef Filename = File->getName();
339 if (!FileMatchesDepCriteria(Filename.data(), FileType))
340 return;
341 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
342}
343
David Stenbergd45d7ea2018-05-29 13:07:58 +0000344bool DFGImpl::AddFilename(StringRef Filename) {
345 if (FilesSet.insert(Filename).second) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000346 Files.push_back(Filename);
David Stenbergd45d7ea2018-05-29 13:07:58 +0000347 return true;
348 }
349 return false;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000350}
351
Paul Robinson64441de2015-05-13 21:18:15 +0000352/// Print the filename, with escaping or quoting that accommodates the three
353/// most likely tools that use dependency files: GNU Make, BSD Make, and
354/// NMake/Jom.
355///
356/// BSD Make is the simplest case: It does no escaping at all. This means
357/// characters that are normally delimiters, i.e. space and # (the comment
358/// character) simply aren't supported in filenames.
359///
360/// GNU Make does allow space and # in filenames, but to avoid being treated
361/// as a delimiter or comment, these must be escaped with a backslash. Because
362/// backslash is itself the escape character, if a backslash appears in a
363/// filename, it should be escaped as well. (As a special case, $ is escaped
364/// as $$, which is the normal Make way to handle the $ character.)
365/// For compatibility with BSD Make and historical practice, if GNU Make
366/// un-escapes characters in a filename but doesn't find a match, it will
367/// retry with the unmodified original string.
368///
369/// GCC tries to accommodate both Make formats by escaping any space or #
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000370/// characters in the original filename, but not escaping backslashes. The
371/// apparent intent is so that filenames with backslashes will be handled
Paul Robinson64441de2015-05-13 21:18:15 +0000372/// correctly by BSD Make, and by GNU Make in its fallback mode of using the
373/// unmodified original string; filenames with # or space characters aren't
374/// supported by BSD Make at all, but will be handled correctly by GNU Make
375/// due to the escaping.
376///
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000377/// A corner case that GCC gets only partly right is when the original filename
378/// has a backslash immediately followed by space or #. GNU Make would expect
379/// this backslash to be escaped; however GCC escapes the original backslash
380/// only when followed by space, not #. It will therefore take a dependency
381/// from a directive such as
382/// #include "a\ b\#c.h"
Paul Robinson64441de2015-05-13 21:18:15 +0000383/// and emit it as
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000384/// a\\\ b\\#c.h
Paul Robinson64441de2015-05-13 21:18:15 +0000385/// which GNU Make will interpret as
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000386/// a\ b\
Paul Robinson64441de2015-05-13 21:18:15 +0000387/// followed by a comment. Failing to find this file, it will fall back to the
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000388/// original string, which probably doesn't exist either; in any case it won't
389/// find
390/// a\ b\#c.h
Paul Robinson64441de2015-05-13 21:18:15 +0000391/// which is the actual filename specified by the include directive.
392///
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000393/// Clang does what GCC does, rather than what GNU Make expects.
Paul Robinson64441de2015-05-13 21:18:15 +0000394///
395/// NMake/Jom has a different set of scary characters, but wraps filespecs in
396/// double-quotes to avoid misinterpreting them; see
Paul Robinsond7214a72015-04-27 18:14:32 +0000397/// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
398/// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
399/// for Windows file-naming info.
400static void PrintFilename(raw_ostream &OS, StringRef Filename,
401 DependencyOutputFormat OutputFormat) {
David Bolvanskyc96cb252018-09-13 14:27:32 +0000402 // Convert filename to platform native path
403 llvm::SmallString<256> NativePath;
404 llvm::sys::path::native(Filename.str(), NativePath);
405
Paul Robinsond7214a72015-04-27 18:14:32 +0000406 if (OutputFormat == DependencyOutputFormat::NMake) {
407 // Add quotes if needed. These are the characters listed as "special" to
408 // NMake, that are legal in a Windows filespec, and that could cause
409 // misinterpretation of the dependency string.
David Bolvanskyc96cb252018-09-13 14:27:32 +0000410 if (NativePath.find_first_of(" #${}^!") != StringRef::npos)
411 OS << '\"' << NativePath << '\"';
Paul Robinsond7214a72015-04-27 18:14:32 +0000412 else
David Bolvanskyc96cb252018-09-13 14:27:32 +0000413 OS << NativePath;
Paul Robinsond7214a72015-04-27 18:14:32 +0000414 return;
415 }
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000416 assert(OutputFormat == DependencyOutputFormat::Make);
David Bolvanskyc96cb252018-09-13 14:27:32 +0000417 for (unsigned i = 0, e = NativePath.size(); i != e; ++i) {
418 if (NativePath[i] == '#') // Handle '#' the broken gcc way.
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000419 OS << '\\';
David Bolvanskyc96cb252018-09-13 14:27:32 +0000420 else if (NativePath[i] == ' ') { // Handle space correctly.
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000421 OS << '\\';
Paul Robinson64441de2015-05-13 21:18:15 +0000422 unsigned j = i;
David Bolvanskyc96cb252018-09-13 14:27:32 +0000423 while (j > 0 && NativePath[--j] == '\\')
Paul Robinson64441de2015-05-13 21:18:15 +0000424 OS << '\\';
David Bolvanskyc96cb252018-09-13 14:27:32 +0000425 } else if (NativePath[i] == '$') // $ is escaped by $$.
Benjamin Kramerae611512013-04-02 13:38:48 +0000426 OS << '$';
David Bolvanskyc96cb252018-09-13 14:27:32 +0000427 OS << NativePath[i];
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000428 }
429}
430
Ben Langmuircb69b572014-03-07 06:40:32 +0000431void DFGImpl::OutputDependencyFile() {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000432 if (SeenMissingHeader) {
Rafael Espindola2a008782014-01-10 21:32:14 +0000433 llvm::sys::fs::remove(OutputFile);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000434 return;
435 }
436
Rafael Espindoladae941a2014-08-25 18:17:04 +0000437 std::error_code EC;
438 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
439 if (EC) {
440 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
441 << EC.message();
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000442 return;
443 }
444
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000445 // Write out the dependency targets, trying to avoid overly long
446 // lines when possible. We try our best to emit exactly the same
447 // dependency file as GCC (4.2), assuming the included files are the
448 // same.
449 const unsigned MaxColumns = 75;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000450 unsigned Columns = 0;
451
Yaron Keren3998a092016-11-16 19:24:10 +0000452 for (StringRef Target : Targets) {
453 unsigned N = Target.size();
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000454 if (Columns == 0) {
455 Columns += N;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000456 } else if (Columns + N + 2 > MaxColumns) {
457 Columns = N + 2;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000458 OS << " \\\n ";
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000459 } else {
460 Columns += N + 1;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000461 OS << ' ';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000462 }
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000463 // Targets already quoted as needed.
Yaron Keren3998a092016-11-16 19:24:10 +0000464 OS << Target;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000465 }
466
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000467 OS << ':';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000468 Columns += 1;
Mike Stump11289f42009-09-09 15:08:12 +0000469
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000470 // Now add each dependency in the order it was seen, but avoiding
471 // duplicates.
Yaron Keren3998a092016-11-16 19:24:10 +0000472 for (StringRef File : Files) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000473 // Start a new line if this would exceed the column limit. Make
474 // sure to leave space for a trailing " \" in case we need to
475 // break the line on the next iteration.
Yaron Keren3998a092016-11-16 19:24:10 +0000476 unsigned N = File.size();
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000477 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000478 OS << " \\\n ";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000479 Columns = 2;
480 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000481 OS << ' ';
Yaron Keren3998a092016-11-16 19:24:10 +0000482 PrintFilename(OS, File, OutputFormat);
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000483 Columns += N + 1;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000484 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000485 OS << '\n';
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000486
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000487 // Create phony targets if requested.
Fariborz Jahanian193f7832011-04-15 18:49:23 +0000488 if (PhonyTarget && !Files.empty()) {
David Stenbergd45d7ea2018-05-29 13:07:58 +0000489 unsigned Index = 0;
490 for (auto I = Files.begin(), E = Files.end(); I != E; ++I) {
491 if (Index++ == InputFileIndex)
492 continue;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000493 OS << '\n';
Yaron Kerenfa367402017-01-14 21:12:08 +0000494 PrintFilename(OS, *I, OutputFormat);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000495 OS << ":\n";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000496 }
497 }
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000498}
499
Ben Langmuircb69b572014-03-07 06:40:32 +0000500bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
Richard Smith216a3bd2015-08-13 17:57:10 +0000501 bool IsSystem, bool IsOverridden,
502 bool IsExplicitModule) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000503 assert(!IsSystem || needsSystemInputFileVisitation());
Richard Smith216a3bd2015-08-13 17:57:10 +0000504 if (IsOverridden || IsExplicitModule)
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000505 return true;
506
Ben Langmuircb69b572014-03-07 06:40:32 +0000507 Parent.AddFilename(Filename);
508 return true;
509}
510
Richard Smith216a3bd2015-08-13 17:57:10 +0000511void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename,
512 serialization::ModuleKind Kind) {
Richard Smithbe9b6c72015-08-13 18:30:25 +0000513 if (Parent.includeModuleFiles())
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000514 Parent.AddFilename(Filename);
515}