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