blob: b472eaaae0542f86fa55b9c4431350355fd0e758 [file] [log] [blame]
Daniel Dunbar750c3582008-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 Friedmanb09f6e12009-05-19 04:14:29 +000014#include "clang/Frontend/Utils.h"
Chris Lattnerb9c3f962009-01-27 07:57:44 +000015#include "clang/Basic/FileManager.h"
Daniel Dunbar0e0bae82009-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 Collingbournebb527862011-07-12 19:35:15 +000020#include "clang/Lex/LexDiagnostic.h"
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000021#include "clang/Lex/PPCallbacks.h"
22#include "clang/Lex/Preprocessor.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070023#include "clang/Serialization/ASTReader.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000024#include "llvm/ADT/StringSet.h"
Benjamin Kramer0b214902013-06-13 14:26:04 +000025#include "llvm/Support/FileSystem.h"
Eli Friedmana6e023c2011-07-08 20:17:28 +000026#include "llvm/Support/Path.h"
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000028
29using namespace clang;
30
31namespace {
Stephen Hines651f13c2014-04-23 16:59:28 -070032/// Private implementation for DependencyFileGenerator
33class DFGImpl : public PPCallbacks {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000034 std::vector<std::string> Files;
35 llvm::StringSet<> FilesSet;
Daniel Dunbar750c3582008-10-24 22:12:41 +000036 const Preprocessor *PP;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000037 std::string OutputFile;
Chris Lattner02fbb252009-01-11 19:28:34 +000038 std::vector<std::string> Targets;
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000039 bool IncludeSystemHeaders;
40 bool PhonyTarget;
Peter Collingbournebb527862011-07-12 19:35:15 +000041 bool AddMissingHeaderDeps;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000042 bool SeenMissingHeader;
Stephen Hines651f13c2014-04-23 16:59:28 -070043 bool IncludeModuleFiles;
Daniel Dunbar750c3582008-10-24 22:12:41 +000044private:
45 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000046 SrcMgr::CharacteristicKind FileType);
Daniel Dunbar750c3582008-10-24 22:12:41 +000047 void OutputDependencyFile();
48
49public:
Stephen Hines651f13c2014-04-23 16:59:28 -070050 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000051 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000052 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbournebb527862011-07-12 19:35:15 +000053 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000054 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
Stephen Hines651f13c2014-04-23 16:59:28 -070055 SeenMissingHeader(false),
56 IncludeModuleFiles(Opts.IncludeModuleFiles) {}
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000057
Stephen Hines651f13c2014-04-23 16:59:28 -070058 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
59 SrcMgr::CharacteristicKind FileType,
60 FileID PrevFID) override;
61 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
62 StringRef FileName, bool IsAngled,
63 CharSourceRange FilenameRange, const FileEntry *File,
64 StringRef SearchPath, StringRef RelativePath,
65 const Module *Imported) override;
Daniel Dunbardbd82092010-03-23 05:09:10 +000066
Stephen Hines651f13c2014-04-23 16:59:28 -070067 void EndOfMainFile() override {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000068 OutputDependencyFile();
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000069 }
Stephen Hines651f13c2014-04-23 16:59:28 -070070
71 void AddFilename(StringRef Filename);
72 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
73 bool includeModuleFiles() const { return IncludeModuleFiles; }
74};
75
76class DFGASTReaderListener : public ASTReaderListener {
77 DFGImpl &Parent;
78public:
79 DFGASTReaderListener(DFGImpl &Parent)
80 : Parent(Parent) { }
81 bool needsInputFileVisitation() override { return true; }
82 bool needsSystemInputFileVisitation() override {
83 return Parent.includeSystemHeaders();
84 }
85 void visitModuleFile(StringRef Filename) override;
86 bool visitInputFile(StringRef Filename, bool isSystem,
87 bool isOverridden) override;
Daniel Dunbar750c3582008-10-24 22:12:41 +000088};
89}
90
Stephen Hines651f13c2014-04-23 16:59:28 -070091DependencyFileGenerator::DependencyFileGenerator(void *Impl)
92: Impl(Impl) { }
93
94DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
95 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
96
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000097 if (Opts.Targets.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +000098 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Stephen Hines651f13c2014-04-23 16:59:28 -070099 return NULL;
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000100 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000101
Peter Collingbournebb527862011-07-12 19:35:15 +0000102 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedmanf84139a2011-08-30 23:07:51 +0000103 if (Opts.AddMissingHeaderDeps)
104 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbournebb527862011-07-12 19:35:15 +0000105
Stephen Hines651f13c2014-04-23 16:59:28 -0700106 DFGImpl *Callback = new DFGImpl(&PP, Opts);
107 PP.addPPCallbacks(Callback); // PP owns the Callback
108 return new DependencyFileGenerator(Callback);
109}
110
111void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
112 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
113 assert(I && "missing implementation");
114 R.addListener(new DFGASTReaderListener(*I));
Daniel Dunbar750c3582008-10-24 22:12:41 +0000115}
116
117/// FileMatchesDepCriteria - Determine whether the given Filename should be
118/// considered as a dependency.
Stephen Hines651f13c2014-04-23 16:59:28 -0700119bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
120 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000121 if (strcmp("<built-in>", Filename) == 0)
122 return false;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000123
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +0000124 if (IncludeSystemHeaders)
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000125 return true;
126
127 return FileType == SrcMgr::C_User;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000128}
129
Stephen Hines651f13c2014-04-23 16:59:28 -0700130void DFGImpl::FileChanged(SourceLocation Loc,
131 FileChangeReason Reason,
132 SrcMgr::CharacteristicKind FileType,
133 FileID PrevFID) {
Daniel Dunbar750c3582008-10-24 22:12:41 +0000134 if (Reason != PPCallbacks::EnterFile)
135 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000137 // Dependency generation really does want to go all the way to the
138 // file entry for a source location to find out what is depended on.
139 // We do not want #line markers to affect dependency generation!
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000140 SourceManager &SM = PP->getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000142 const FileEntry *FE =
Chandler Carruth40278532011-07-25 16:49:02 +0000143 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000144 if (FE == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Chris Lattner5f9e2722011-07-23 10:55:15 +0000146 StringRef Filename = FE->getName();
Eli Friedmana6e023c2011-07-08 20:17:28 +0000147 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbar750c3582008-10-24 22:12:41 +0000148 return;
149
Eli Friedmana6e023c2011-07-08 20:17:28 +0000150 // Remove leading "./" (or ".//" or "././" etc.)
151 while (Filename.size() > 2 && Filename[0] == '.' &&
152 llvm::sys::path::is_separator(Filename[1])) {
153 Filename = Filename.substr(1);
154 while (llvm::sys::path::is_separator(Filename[0]))
155 Filename = Filename.substr(1);
156 }
157
Peter Collingbournebb527862011-07-12 19:35:15 +0000158 AddFilename(Filename);
159}
Daniel Dunbar750c3582008-10-24 22:12:41 +0000160
Stephen Hines651f13c2014-04-23 16:59:28 -0700161void DFGImpl::InclusionDirective(SourceLocation HashLoc,
162 const Token &IncludeTok,
163 StringRef FileName,
164 bool IsAngled,
165 CharSourceRange FilenameRange,
166 const FileEntry *File,
167 StringRef SearchPath,
168 StringRef RelativePath,
169 const Module *Imported) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000170 if (!File) {
171 if (AddMissingHeaderDeps)
172 AddFilename(FileName);
173 else
174 SeenMissingHeader = true;
175 }
Peter Collingbournebb527862011-07-12 19:35:15 +0000176}
177
Stephen Hines651f13c2014-04-23 16:59:28 -0700178void DFGImpl::AddFilename(StringRef Filename) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000179 if (FilesSet.insert(Filename))
180 Files.push_back(Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000181}
182
Benjamin Kramerddc15c42013-04-02 13:38:48 +0000183/// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or
184/// other scary characters.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000185static void PrintFilename(raw_ostream &OS, StringRef Filename) {
Chris Lattner9d506342011-02-17 02:14:49 +0000186 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
Benjamin Kramerddc15c42013-04-02 13:38:48 +0000187 if (Filename[i] == ' ' || Filename[i] == '#')
Chris Lattner9d506342011-02-17 02:14:49 +0000188 OS << '\\';
Benjamin Kramerddc15c42013-04-02 13:38:48 +0000189 else if (Filename[i] == '$') // $ is escaped by $$.
190 OS << '$';
Chris Lattner9d506342011-02-17 02:14:49 +0000191 OS << Filename[i];
192 }
193}
194
Stephen Hines651f13c2014-04-23 16:59:28 -0700195void DFGImpl::OutputDependencyFile() {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000196 if (SeenMissingHeader) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700197 llvm::sys::fs::remove(OutputFile);
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000198 return;
199 }
200
201 std::string Err;
Stephen Hines651f13c2014-04-23 16:59:28 -0700202 llvm::raw_fd_ostream OS(OutputFile.c_str(), Err, llvm::sys::fs::F_Text);
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000203 if (!Err.empty()) {
204 PP->getDiagnostics().Report(diag::err_fe_error_opening)
205 << OutputFile << Err;
206 return;
207 }
208
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000209 // Write out the dependency targets, trying to avoid overly long
210 // lines when possible. We try our best to emit exactly the same
211 // dependency file as GCC (4.2), assuming the included files are the
212 // same.
213 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000214 unsigned Columns = 0;
215
216 for (std::vector<std::string>::iterator
217 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
218 unsigned N = I->length();
219 if (Columns == 0) {
220 Columns += N;
Chris Lattner02fbb252009-01-11 19:28:34 +0000221 } else if (Columns + N + 2 > MaxColumns) {
222 Columns = N + 2;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000223 OS << " \\\n ";
Chris Lattner02fbb252009-01-11 19:28:34 +0000224 } else {
225 Columns += N + 1;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000226 OS << ' ';
Chris Lattner02fbb252009-01-11 19:28:34 +0000227 }
Chris Lattner9d506342011-02-17 02:14:49 +0000228 // Targets already quoted as needed.
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000229 OS << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000230 }
231
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000232 OS << ':';
Chris Lattner02fbb252009-01-11 19:28:34 +0000233 Columns += 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000235 // Now add each dependency in the order it was seen, but avoiding
236 // duplicates.
237 for (std::vector<std::string>::iterator I = Files.begin(),
238 E = Files.end(); I != E; ++I) {
239 // Start a new line if this would exceed the column limit. Make
240 // sure to leave space for a trailing " \" in case we need to
241 // break the line on the next iteration.
242 unsigned N = I->length();
243 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000244 OS << " \\\n ";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000245 Columns = 2;
246 }
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000247 OS << ' ';
248 PrintFilename(OS, *I);
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000249 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000250 }
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000251 OS << '\n';
Daniel Dunbar750c3582008-10-24 22:12:41 +0000252
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000253 // Create phony targets if requested.
Fariborz Jahanianccad3db2011-04-15 18:49:23 +0000254 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000255 // Skip the first entry, this is always the input file itself.
256 for (std::vector<std::string>::iterator I = Files.begin() + 1,
257 E = Files.end(); I != E; ++I) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000258 OS << '\n';
259 PrintFilename(OS, *I);
260 OS << ":\n";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000261 }
262 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000263}
264
Stephen Hines651f13c2014-04-23 16:59:28 -0700265bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
266 bool IsSystem, bool IsOverridden) {
267 assert(!IsSystem || needsSystemInputFileVisitation());
268 if (IsOverridden)
269 return true;
270
271 Parent.AddFilename(Filename);
272 return true;
273}
274
275void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename) {
276 if (Parent.includeModuleFiles())
277 Parent.AddFilename(Filename);
278}