Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 1 | //===--- 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 Friedman | b09f6e1 | 2009-05-19 04:14:29 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/Utils.h" |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
| 17 | #include "clang/Frontend/DependencyOutputOptions.h" |
| 18 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 19 | #include "clang/Lex/DirectoryLookup.h" |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 20 | #include "clang/Lex/LexDiagnostic.h" |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 21 | #include "clang/Lex/PPCallbacks.h" |
| 22 | #include "clang/Lex/Preprocessor.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 23 | #include "clang/Serialization/ASTReader.h" |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringSet.h" |
Benjamin Kramer | 0b21490 | 2013-06-13 14:26:04 +0000 | [diff] [blame] | 25 | #include "llvm/Support/FileSystem.h" |
Eli Friedman | a6e023c | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
| 30 | |
| 31 | namespace { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 32 | struct 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 | |
| 52 | StringRef Filename = FE->getName(); |
| 53 | |
| 54 | // Remove leading "./" (or ".//" or "././" etc.) |
| 55 | while (Filename.size() > 2 && Filename[0] == '.' && |
| 56 | llvm::sys::path::is_separator(Filename[1])) { |
| 57 | Filename = Filename.substr(1); |
| 58 | while (llvm::sys::path::is_separator(Filename[0])) |
| 59 | Filename = Filename.substr(1); |
| 60 | } |
| 61 | |
| 62 | DepCollector.maybeAddDependency(Filename, /*FromModule*/false, |
| 63 | FileType != SrcMgr::C_User, |
| 64 | /*IsModuleFile*/false, /*IsMissing*/false); |
| 65 | } |
| 66 | |
| 67 | void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
| 68 | StringRef FileName, bool IsAngled, |
| 69 | CharSourceRange FilenameRange, const FileEntry *File, |
| 70 | StringRef SearchPath, StringRef RelativePath, |
| 71 | const Module *Imported) override { |
| 72 | if (!File) |
| 73 | DepCollector.maybeAddDependency(FileName, /*FromModule*/false, |
| 74 | /*IsSystem*/false, /*IsModuleFile*/false, |
| 75 | /*IsMissing*/true); |
| 76 | // Files that actually exist are handled by FileChanged. |
| 77 | } |
| 78 | |
| 79 | void EndOfMainFile() override { |
| 80 | DepCollector.finishedMainFile(); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | struct DepCollectorASTListener : public ASTReaderListener { |
| 85 | DependencyCollector &DepCollector; |
| 86 | DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { } |
| 87 | bool needsInputFileVisitation() override { return true; } |
| 88 | bool needsSystemInputFileVisitation() override { |
| 89 | return DepCollector.needSystemDependencies(); |
| 90 | } |
| 91 | void visitModuleFile(StringRef Filename) override { |
| 92 | DepCollector.maybeAddDependency(Filename, /*FromModule*/true, |
| 93 | /*IsSystem*/false, /*IsModuleFile*/true, |
| 94 | /*IsMissing*/false); |
| 95 | } |
| 96 | bool visitInputFile(StringRef Filename, bool IsSystem, |
| 97 | bool IsOverridden) override { |
| 98 | if (IsOverridden) |
| 99 | return true; |
| 100 | |
| 101 | DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem, |
| 102 | /*IsModuleFile*/false, /*IsMissing*/false); |
| 103 | return true; |
| 104 | } |
| 105 | }; |
| 106 | } // end anonymous namespace |
| 107 | |
| 108 | void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule, |
| 109 | bool IsSystem, bool IsModuleFile, |
| 110 | bool IsMissing) { |
| 111 | if (Seen.insert(Filename) && |
| 112 | sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing)) |
| 113 | Dependencies.push_back(Filename); |
| 114 | } |
| 115 | |
| 116 | bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule, |
| 117 | bool IsSystem, bool IsModuleFile, |
| 118 | bool IsMissing) { |
| 119 | return Filename != "<built-in>" && (needSystemDependencies() || !IsSystem); |
| 120 | } |
| 121 | |
| 122 | DependencyCollector::~DependencyCollector() { } |
| 123 | void DependencyCollector::attachToPreprocessor(Preprocessor &PP) { |
| 124 | PP.addPPCallbacks(new DepCollectorPPCallbacks(*this, PP.getSourceManager())); |
| 125 | } |
| 126 | void DependencyCollector::attachToASTReader(ASTReader &R) { |
| 127 | R.addListener(new DepCollectorASTListener(*this)); |
| 128 | } |
| 129 | |
| 130 | namespace { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 131 | /// Private implementation for DependencyFileGenerator |
| 132 | class DFGImpl : public PPCallbacks { |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 133 | std::vector<std::string> Files; |
| 134 | llvm::StringSet<> FilesSet; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 135 | const Preprocessor *PP; |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 136 | std::string OutputFile; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 137 | std::vector<std::string> Targets; |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 138 | bool IncludeSystemHeaders; |
| 139 | bool PhonyTarget; |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 140 | bool AddMissingHeaderDeps; |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 141 | bool SeenMissingHeader; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 142 | bool IncludeModuleFiles; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 143 | private: |
| 144 | bool FileMatchesDepCriteria(const char *Filename, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 145 | SrcMgr::CharacteristicKind FileType); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 146 | void OutputDependencyFile(); |
| 147 | |
| 148 | public: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 149 | DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts) |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 150 | : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets), |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 151 | IncludeSystemHeaders(Opts.IncludeSystemHeaders), |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 152 | PhonyTarget(Opts.UsePhonyTargets), |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 153 | AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 154 | SeenMissingHeader(false), |
| 155 | IncludeModuleFiles(Opts.IncludeModuleFiles) {} |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 156 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 157 | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 158 | SrcMgr::CharacteristicKind FileType, |
| 159 | FileID PrevFID) override; |
| 160 | void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
| 161 | StringRef FileName, bool IsAngled, |
| 162 | CharSourceRange FilenameRange, const FileEntry *File, |
| 163 | StringRef SearchPath, StringRef RelativePath, |
| 164 | const Module *Imported) override; |
Daniel Dunbar | dbd8209 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 165 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 166 | void EndOfMainFile() override { |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 167 | OutputDependencyFile(); |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 168 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 169 | |
| 170 | void AddFilename(StringRef Filename); |
| 171 | bool includeSystemHeaders() const { return IncludeSystemHeaders; } |
| 172 | bool includeModuleFiles() const { return IncludeModuleFiles; } |
| 173 | }; |
| 174 | |
| 175 | class DFGASTReaderListener : public ASTReaderListener { |
| 176 | DFGImpl &Parent; |
| 177 | public: |
| 178 | DFGASTReaderListener(DFGImpl &Parent) |
| 179 | : Parent(Parent) { } |
| 180 | bool needsInputFileVisitation() override { return true; } |
| 181 | bool needsSystemInputFileVisitation() override { |
| 182 | return Parent.includeSystemHeaders(); |
| 183 | } |
| 184 | void visitModuleFile(StringRef Filename) override; |
| 185 | bool visitInputFile(StringRef Filename, bool isSystem, |
| 186 | bool isOverridden) override; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 187 | }; |
| 188 | } |
| 189 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 190 | DependencyFileGenerator::DependencyFileGenerator(void *Impl) |
| 191 | : Impl(Impl) { } |
| 192 | |
| 193 | DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor( |
| 194 | clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) { |
| 195 | |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 196 | if (Opts.Targets.empty()) { |
Daniel Dunbar | ca11f61 | 2009-11-11 21:44:00 +0000 | [diff] [blame] | 197 | PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 198 | return nullptr; |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 199 | } |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 200 | |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 201 | // Disable the "file not found" diagnostic if the -MG option was given. |
Eli Friedman | f84139a | 2011-08-30 23:07:51 +0000 | [diff] [blame] | 202 | if (Opts.AddMissingHeaderDeps) |
| 203 | PP.SetSuppressIncludeNotFoundError(true); |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 204 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 205 | DFGImpl *Callback = new DFGImpl(&PP, Opts); |
| 206 | PP.addPPCallbacks(Callback); // PP owns the Callback |
| 207 | return new DependencyFileGenerator(Callback); |
| 208 | } |
| 209 | |
| 210 | void DependencyFileGenerator::AttachToASTReader(ASTReader &R) { |
| 211 | DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl); |
| 212 | assert(I && "missing implementation"); |
| 213 | R.addListener(new DFGASTReaderListener(*I)); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /// FileMatchesDepCriteria - Determine whether the given Filename should be |
| 217 | /// considered as a dependency. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 218 | bool DFGImpl::FileMatchesDepCriteria(const char *Filename, |
| 219 | SrcMgr::CharacteristicKind FileType) { |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 220 | if (strcmp("<built-in>", Filename) == 0) |
| 221 | return false; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 222 | |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 223 | if (IncludeSystemHeaders) |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 224 | return true; |
| 225 | |
| 226 | return FileType == SrcMgr::C_User; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 229 | void DFGImpl::FileChanged(SourceLocation Loc, |
| 230 | FileChangeReason Reason, |
| 231 | SrcMgr::CharacteristicKind FileType, |
| 232 | FileID PrevFID) { |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 233 | if (Reason != PPCallbacks::EnterFile) |
| 234 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 236 | // Dependency generation really does want to go all the way to the |
| 237 | // file entry for a source location to find out what is depended on. |
| 238 | // We do not want #line markers to affect dependency generation! |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 239 | SourceManager &SM = PP->getSourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Chris Lattner | e86e4cd0 | 2009-01-28 05:42:38 +0000 | [diff] [blame] | 241 | const FileEntry *FE = |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 242 | SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc))); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 243 | if (!FE) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 245 | StringRef Filename = FE->getName(); |
Eli Friedman | a6e023c | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 246 | if (!FileMatchesDepCriteria(Filename.data(), FileType)) |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 247 | return; |
| 248 | |
Eli Friedman | a6e023c | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 249 | // Remove leading "./" (or ".//" or "././" etc.) |
| 250 | while (Filename.size() > 2 && Filename[0] == '.' && |
| 251 | llvm::sys::path::is_separator(Filename[1])) { |
| 252 | Filename = Filename.substr(1); |
| 253 | while (llvm::sys::path::is_separator(Filename[0])) |
| 254 | Filename = Filename.substr(1); |
| 255 | } |
| 256 | |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 257 | AddFilename(Filename); |
| 258 | } |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 259 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 260 | void DFGImpl::InclusionDirective(SourceLocation HashLoc, |
| 261 | const Token &IncludeTok, |
| 262 | StringRef FileName, |
| 263 | bool IsAngled, |
| 264 | CharSourceRange FilenameRange, |
| 265 | const FileEntry *File, |
| 266 | StringRef SearchPath, |
| 267 | StringRef RelativePath, |
| 268 | const Module *Imported) { |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 269 | if (!File) { |
| 270 | if (AddMissingHeaderDeps) |
| 271 | AddFilename(FileName); |
| 272 | else |
| 273 | SeenMissingHeader = true; |
| 274 | } |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 277 | void DFGImpl::AddFilename(StringRef Filename) { |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 278 | if (FilesSet.insert(Filename)) |
| 279 | Files.push_back(Filename); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Benjamin Kramer | ddc15c4 | 2013-04-02 13:38:48 +0000 | [diff] [blame] | 282 | /// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or |
| 283 | /// other scary characters. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 284 | static void PrintFilename(raw_ostream &OS, StringRef Filename) { |
Chris Lattner | 9d50634 | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 285 | for (unsigned i = 0, e = Filename.size(); i != e; ++i) { |
Benjamin Kramer | ddc15c4 | 2013-04-02 13:38:48 +0000 | [diff] [blame] | 286 | if (Filename[i] == ' ' || Filename[i] == '#') |
Chris Lattner | 9d50634 | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 287 | OS << '\\'; |
Benjamin Kramer | ddc15c4 | 2013-04-02 13:38:48 +0000 | [diff] [blame] | 288 | else if (Filename[i] == '$') // $ is escaped by $$. |
| 289 | OS << '$'; |
Chris Lattner | 9d50634 | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 290 | OS << Filename[i]; |
| 291 | } |
| 292 | } |
| 293 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 294 | void DFGImpl::OutputDependencyFile() { |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 295 | if (SeenMissingHeader) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 296 | llvm::sys::fs::remove(OutputFile); |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 297 | return; |
| 298 | } |
| 299 | |
| 300 | std::string Err; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 301 | llvm::raw_fd_ostream OS(OutputFile.c_str(), Err, llvm::sys::fs::F_Text); |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 302 | if (!Err.empty()) { |
| 303 | PP->getDiagnostics().Report(diag::err_fe_error_opening) |
| 304 | << OutputFile << Err; |
| 305 | return; |
| 306 | } |
| 307 | |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 308 | // Write out the dependency targets, trying to avoid overly long |
| 309 | // lines when possible. We try our best to emit exactly the same |
| 310 | // dependency file as GCC (4.2), assuming the included files are the |
| 311 | // same. |
| 312 | const unsigned MaxColumns = 75; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 313 | unsigned Columns = 0; |
| 314 | |
| 315 | for (std::vector<std::string>::iterator |
| 316 | I = Targets.begin(), E = Targets.end(); I != E; ++I) { |
| 317 | unsigned N = I->length(); |
| 318 | if (Columns == 0) { |
| 319 | Columns += N; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 320 | } else if (Columns + N + 2 > MaxColumns) { |
| 321 | Columns = N + 2; |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 322 | OS << " \\\n "; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 323 | } else { |
| 324 | Columns += N + 1; |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 325 | OS << ' '; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 326 | } |
Chris Lattner | 9d50634 | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 327 | // Targets already quoted as needed. |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 328 | OS << *I; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 331 | OS << ':'; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 332 | Columns += 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 334 | // Now add each dependency in the order it was seen, but avoiding |
| 335 | // duplicates. |
| 336 | for (std::vector<std::string>::iterator I = Files.begin(), |
| 337 | E = Files.end(); I != E; ++I) { |
| 338 | // Start a new line if this would exceed the column limit. Make |
| 339 | // sure to leave space for a trailing " \" in case we need to |
| 340 | // break the line on the next iteration. |
| 341 | unsigned N = I->length(); |
| 342 | if (Columns + (N + 1) + 2 > MaxColumns) { |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 343 | OS << " \\\n "; |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 344 | Columns = 2; |
| 345 | } |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 346 | OS << ' '; |
| 347 | PrintFilename(OS, *I); |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 348 | Columns += N + 1; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 349 | } |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 350 | OS << '\n'; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 351 | |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 352 | // Create phony targets if requested. |
Fariborz Jahanian | ccad3db | 2011-04-15 18:49:23 +0000 | [diff] [blame] | 353 | if (PhonyTarget && !Files.empty()) { |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 354 | // Skip the first entry, this is always the input file itself. |
| 355 | for (std::vector<std::string>::iterator I = Files.begin() + 1, |
| 356 | E = Files.end(); I != E; ++I) { |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 357 | OS << '\n'; |
| 358 | PrintFilename(OS, *I); |
| 359 | OS << ":\n"; |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 364 | bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename, |
| 365 | bool IsSystem, bool IsOverridden) { |
| 366 | assert(!IsSystem || needsSystemInputFileVisitation()); |
| 367 | if (IsOverridden) |
| 368 | return true; |
| 369 | |
| 370 | Parent.AddFilename(Filename); |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename) { |
| 375 | if (Parent.includeModuleFiles()) |
| 376 | Parent.AddFilename(Filename); |
| 377 | } |