Daniel Dunbar | fa0caca | 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 | 16b7b6f | 2009-05-19 04:14:29 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/Utils.h" |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
Daniel Dunbar | 89d1fdf | 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 | 77b0e7f2 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 20 | #include "clang/Lex/LexDiagnostic.h" |
Daniel Dunbar | 89d1fdf | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 21 | #include "clang/Lex/PPCallbacks.h" |
| 22 | #include "clang/Lex/Preprocessor.h" |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 23 | #include "clang/Serialization/ASTReader.h" |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringSet.h" |
David Majnemer | f0822fb | 2014-10-27 22:31:50 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringSwitch.h" |
Benjamin Kramer | 33d4330 | 2013-06-13 14:26:04 +0000 | [diff] [blame] | 26 | #include "llvm/Support/FileSystem.h" |
Eli Friedman | f7ca26a | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Path.h" |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace clang; |
| 31 | |
| 32 | namespace { |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [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) { |
David Blaikie | 61b86d4 | 2014-11-19 02:56:13 +0000 | [diff] [blame] | 112 | if (Seen.insert(Filename).second && |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 113 | sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing)) |
| 114 | Dependencies.push_back(Filename); |
| 115 | } |
| 116 | |
David Majnemer | f0822fb | 2014-10-27 22:31:50 +0000 | [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 | |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 124 | bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule, |
| 125 | bool IsSystem, bool IsModuleFile, |
| 126 | bool IsMissing) { |
David Majnemer | f0822fb | 2014-10-27 22:31:50 +0000 | [diff] [blame] | 127 | return !isSpecialFilename(Filename) && |
| 128 | (needSystemDependencies() || !IsSystem); |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | DependencyCollector::~DependencyCollector() { } |
| 132 | void DependencyCollector::attachToPreprocessor(Preprocessor &PP) { |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 133 | PP.addPPCallbacks( |
| 134 | llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager())); |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 135 | } |
| 136 | void DependencyCollector::attachToASTReader(ASTReader &R) { |
David Blaikie | 2721c32 | 2014-08-10 16:54:39 +0000 | [diff] [blame] | 137 | R.addListener(llvm::make_unique<DepCollectorASTListener>(*this)); |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | namespace { |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 141 | /// Private implementation for DependencyFileGenerator |
| 142 | class DFGImpl : public PPCallbacks { |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 143 | std::vector<std::string> Files; |
| 144 | llvm::StringSet<> FilesSet; |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 145 | const Preprocessor *PP; |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 146 | std::string OutputFile; |
Chris Lattner | fa5880e | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 147 | std::vector<std::string> Targets; |
Eli Friedman | 33cd03c | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 148 | bool IncludeSystemHeaders; |
| 149 | bool PhonyTarget; |
Peter Collingbourne | 77b0e7f2 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 150 | bool AddMissingHeaderDeps; |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 151 | bool SeenMissingHeader; |
Argyrios Kyrtzidis | 6d0753d | 2014-03-14 03:07:38 +0000 | [diff] [blame] | 152 | bool IncludeModuleFiles; |
Paul Robinson | d7214a7 | 2015-04-27 18:14:32 +0000 | [diff] [blame^] | 153 | DependencyOutputFormat OutputFormat; |
| 154 | |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 155 | private: |
| 156 | bool FileMatchesDepCriteria(const char *Filename, |
Chris Lattner | 66a740e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 157 | SrcMgr::CharacteristicKind FileType); |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 158 | void OutputDependencyFile(); |
| 159 | |
| 160 | public: |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 161 | DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts) |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 162 | : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets), |
Daniel Dunbar | 89d1fdf | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 163 | IncludeSystemHeaders(Opts.IncludeSystemHeaders), |
Peter Collingbourne | 77b0e7f2 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 164 | PhonyTarget(Opts.UsePhonyTargets), |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 165 | AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), |
Argyrios Kyrtzidis | 6d0753d | 2014-03-14 03:07:38 +0000 | [diff] [blame] | 166 | SeenMissingHeader(false), |
Paul Robinson | d7214a7 | 2015-04-27 18:14:32 +0000 | [diff] [blame^] | 167 | IncludeModuleFiles(Opts.IncludeModuleFiles), |
| 168 | OutputFormat(Opts.OutputFormat) {} |
Daniel Dunbar | 52e96cc | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 169 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 170 | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 171 | SrcMgr::CharacteristicKind FileType, |
| 172 | FileID PrevFID) override; |
| 173 | void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
| 174 | StringRef FileName, bool IsAngled, |
| 175 | CharSourceRange FilenameRange, const FileEntry *File, |
| 176 | StringRef SearchPath, StringRef RelativePath, |
| 177 | const Module *Imported) override; |
Daniel Dunbar | cb9eaf5 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 178 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 179 | void EndOfMainFile() override { |
Daniel Dunbar | 52e96cc | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 180 | OutputDependencyFile(); |
Daniel Dunbar | 52e96cc | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 181 | } |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 182 | |
| 183 | void AddFilename(StringRef Filename); |
| 184 | bool includeSystemHeaders() const { return IncludeSystemHeaders; } |
Argyrios Kyrtzidis | 6d0753d | 2014-03-14 03:07:38 +0000 | [diff] [blame] | 185 | bool includeModuleFiles() const { return IncludeModuleFiles; } |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | class DFGASTReaderListener : public ASTReaderListener { |
| 189 | DFGImpl &Parent; |
| 190 | public: |
| 191 | DFGASTReaderListener(DFGImpl &Parent) |
| 192 | : Parent(Parent) { } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 193 | bool needsInputFileVisitation() override { return true; } |
| 194 | bool needsSystemInputFileVisitation() override { |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 195 | return Parent.includeSystemHeaders(); |
| 196 | } |
Argyrios Kyrtzidis | 6d0753d | 2014-03-14 03:07:38 +0000 | [diff] [blame] | 197 | void visitModuleFile(StringRef Filename) override; |
Argyrios Kyrtzidis | 68ccbe0 | 2014-03-14 02:26:31 +0000 | [diff] [blame] | 198 | bool visitInputFile(StringRef Filename, bool isSystem, |
| 199 | bool isOverridden) override; |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 200 | }; |
| 201 | } |
| 202 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 203 | DependencyFileGenerator::DependencyFileGenerator(void *Impl) |
| 204 | : Impl(Impl) { } |
| 205 | |
| 206 | DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor( |
| 207 | clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) { |
| 208 | |
Daniel Dunbar | 89d1fdf | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 209 | if (Opts.Targets.empty()) { |
Daniel Dunbar | 55b781f | 2009-11-11 21:44:00 +0000 | [diff] [blame] | 210 | PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 211 | return nullptr; |
Daniel Dunbar | 89d1fdf | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 212 | } |
Daniel Dunbar | 52e96cc | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 213 | |
Peter Collingbourne | 77b0e7f2 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 214 | // Disable the "file not found" diagnostic if the -MG option was given. |
Eli Friedman | 3781a36 | 2011-08-30 23:07:51 +0000 | [diff] [blame] | 215 | if (Opts.AddMissingHeaderDeps) |
| 216 | PP.SetSuppressIncludeNotFoundError(true); |
Peter Collingbourne | 77b0e7f2 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 217 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 218 | DFGImpl *Callback = new DFGImpl(&PP, Opts); |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 219 | PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback)); |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 220 | return new DependencyFileGenerator(Callback); |
| 221 | } |
| 222 | |
| 223 | void DependencyFileGenerator::AttachToASTReader(ASTReader &R) { |
| 224 | DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl); |
| 225 | assert(I && "missing implementation"); |
David Blaikie | 2721c32 | 2014-08-10 16:54:39 +0000 | [diff] [blame] | 226 | R.addListener(llvm::make_unique<DFGASTReaderListener>(*I)); |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /// FileMatchesDepCriteria - Determine whether the given Filename should be |
| 230 | /// considered as a dependency. |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 231 | bool DFGImpl::FileMatchesDepCriteria(const char *Filename, |
| 232 | SrcMgr::CharacteristicKind FileType) { |
David Majnemer | f0822fb | 2014-10-27 22:31:50 +0000 | [diff] [blame] | 233 | if (isSpecialFilename(Filename)) |
Daniel Dunbar | 52e96cc | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 234 | return false; |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 235 | |
Eli Friedman | 33cd03c | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 236 | if (IncludeSystemHeaders) |
Daniel Dunbar | 52e96cc | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 237 | return true; |
| 238 | |
| 239 | return FileType == SrcMgr::C_User; |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 242 | void DFGImpl::FileChanged(SourceLocation Loc, |
| 243 | FileChangeReason Reason, |
| 244 | SrcMgr::CharacteristicKind FileType, |
| 245 | FileID PrevFID) { |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 246 | if (Reason != PPCallbacks::EnterFile) |
| 247 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Daniel Dunbar | 52e96cc | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 249 | // Dependency generation really does want to go all the way to the |
| 250 | // file entry for a source location to find out what is depended on. |
| 251 | // We do not want #line markers to affect dependency generation! |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 252 | SourceManager &SM = PP->getSourceManager(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 64d8fc2 | 2009-01-28 05:42:38 +0000 | [diff] [blame] | 254 | const FileEntry *FE = |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 255 | SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc))); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 256 | if (!FE) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 258 | StringRef Filename = FE->getName(); |
Eli Friedman | f7ca26a | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 259 | if (!FileMatchesDepCriteria(Filename.data(), FileType)) |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 260 | return; |
| 261 | |
Eli Friedman | f7ca26a | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 262 | // Remove leading "./" (or ".//" or "././" etc.) |
| 263 | while (Filename.size() > 2 && Filename[0] == '.' && |
| 264 | llvm::sys::path::is_separator(Filename[1])) { |
| 265 | Filename = Filename.substr(1); |
| 266 | while (llvm::sys::path::is_separator(Filename[0])) |
| 267 | Filename = Filename.substr(1); |
| 268 | } |
| 269 | |
Peter Collingbourne | 77b0e7f2 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 270 | AddFilename(Filename); |
| 271 | } |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 272 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 273 | void DFGImpl::InclusionDirective(SourceLocation HashLoc, |
| 274 | const Token &IncludeTok, |
| 275 | StringRef FileName, |
| 276 | bool IsAngled, |
| 277 | CharSourceRange FilenameRange, |
| 278 | const FileEntry *File, |
| 279 | StringRef SearchPath, |
| 280 | StringRef RelativePath, |
| 281 | const Module *Imported) { |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 282 | if (!File) { |
| 283 | if (AddMissingHeaderDeps) |
| 284 | AddFilename(FileName); |
| 285 | else |
| 286 | SeenMissingHeader = true; |
| 287 | } |
Peter Collingbourne | 77b0e7f2 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 290 | void DFGImpl::AddFilename(StringRef Filename) { |
David Blaikie | 61b86d4 | 2014-11-19 02:56:13 +0000 | [diff] [blame] | 291 | if (FilesSet.insert(Filename).second) |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 292 | Files.push_back(Filename); |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Benjamin Kramer | ae61151 | 2013-04-02 13:38:48 +0000 | [diff] [blame] | 295 | /// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or |
Paul Robinson | d7214a7 | 2015-04-27 18:14:32 +0000 | [diff] [blame^] | 296 | /// other scary characters. NMake/Jom has a different set of scary characters, |
| 297 | /// but wraps filespecs in double-quotes to avoid misinterpreting them; |
| 298 | /// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info, |
| 299 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx |
| 300 | /// for Windows file-naming info. |
| 301 | static void PrintFilename(raw_ostream &OS, StringRef Filename, |
| 302 | DependencyOutputFormat OutputFormat) { |
| 303 | if (OutputFormat == DependencyOutputFormat::NMake) { |
| 304 | // Add quotes if needed. These are the characters listed as "special" to |
| 305 | // NMake, that are legal in a Windows filespec, and that could cause |
| 306 | // misinterpretation of the dependency string. |
| 307 | if (Filename.find_first_of(" #${}^!") != StringRef::npos) |
| 308 | OS << '\"' << Filename << '\"'; |
| 309 | else |
| 310 | OS << Filename; |
| 311 | return; |
| 312 | } |
Chris Lattner | 5df2a4e | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 313 | for (unsigned i = 0, e = Filename.size(); i != e; ++i) { |
Benjamin Kramer | ae61151 | 2013-04-02 13:38:48 +0000 | [diff] [blame] | 314 | if (Filename[i] == ' ' || Filename[i] == '#') |
Chris Lattner | 5df2a4e | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 315 | OS << '\\'; |
Benjamin Kramer | ae61151 | 2013-04-02 13:38:48 +0000 | [diff] [blame] | 316 | else if (Filename[i] == '$') // $ is escaped by $$. |
| 317 | OS << '$'; |
Chris Lattner | 5df2a4e | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 318 | OS << Filename[i]; |
| 319 | } |
| 320 | } |
| 321 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 322 | void DFGImpl::OutputDependencyFile() { |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 323 | if (SeenMissingHeader) { |
Rafael Espindola | 2a00878 | 2014-01-10 21:32:14 +0000 | [diff] [blame] | 324 | llvm::sys::fs::remove(OutputFile); |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 325 | return; |
| 326 | } |
| 327 | |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 328 | std::error_code EC; |
| 329 | llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text); |
| 330 | if (EC) { |
| 331 | PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile |
| 332 | << EC.message(); |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 333 | return; |
| 334 | } |
| 335 | |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 336 | // Write out the dependency targets, trying to avoid overly long |
| 337 | // lines when possible. We try our best to emit exactly the same |
| 338 | // dependency file as GCC (4.2), assuming the included files are the |
| 339 | // same. |
| 340 | const unsigned MaxColumns = 75; |
Chris Lattner | fa5880e | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 341 | unsigned Columns = 0; |
| 342 | |
| 343 | for (std::vector<std::string>::iterator |
| 344 | I = Targets.begin(), E = Targets.end(); I != E; ++I) { |
| 345 | unsigned N = I->length(); |
| 346 | if (Columns == 0) { |
| 347 | Columns += N; |
Chris Lattner | fa5880e | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 348 | } else if (Columns + N + 2 > MaxColumns) { |
| 349 | Columns = N + 2; |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 350 | OS << " \\\n "; |
Chris Lattner | fa5880e | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 351 | } else { |
| 352 | Columns += N + 1; |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 353 | OS << ' '; |
Chris Lattner | fa5880e | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 354 | } |
Chris Lattner | 5df2a4e | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 355 | // Targets already quoted as needed. |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 356 | OS << *I; |
Chris Lattner | fa5880e | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 359 | OS << ':'; |
Chris Lattner | fa5880e | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 360 | Columns += 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 362 | // Now add each dependency in the order it was seen, but avoiding |
| 363 | // duplicates. |
| 364 | for (std::vector<std::string>::iterator I = Files.begin(), |
| 365 | E = Files.end(); I != E; ++I) { |
| 366 | // Start a new line if this would exceed the column limit. Make |
| 367 | // sure to leave space for a trailing " \" in case we need to |
| 368 | // break the line on the next iteration. |
| 369 | unsigned N = I->length(); |
| 370 | if (Columns + (N + 1) + 2 > MaxColumns) { |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 371 | OS << " \\\n "; |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 372 | Columns = 2; |
| 373 | } |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 374 | OS << ' '; |
Paul Robinson | d7214a7 | 2015-04-27 18:14:32 +0000 | [diff] [blame^] | 375 | PrintFilename(OS, *I, OutputFormat); |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 376 | Columns += N + 1; |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 377 | } |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 378 | OS << '\n'; |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 379 | |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 380 | // Create phony targets if requested. |
Fariborz Jahanian | 193f783 | 2011-04-15 18:49:23 +0000 | [diff] [blame] | 381 | if (PhonyTarget && !Files.empty()) { |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 382 | // Skip the first entry, this is always the input file itself. |
| 383 | for (std::vector<std::string>::iterator I = Files.begin() + 1, |
| 384 | E = Files.end(); I != E; ++I) { |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 385 | OS << '\n'; |
Paul Robinson | d7214a7 | 2015-04-27 18:14:32 +0000 | [diff] [blame^] | 386 | PrintFilename(OS, *I, OutputFormat); |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 387 | OS << ":\n"; |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 392 | bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename, |
Argyrios Kyrtzidis | 68ccbe0 | 2014-03-14 02:26:31 +0000 | [diff] [blame] | 393 | bool IsSystem, bool IsOverridden) { |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 394 | assert(!IsSystem || needsSystemInputFileVisitation()); |
Argyrios Kyrtzidis | 68ccbe0 | 2014-03-14 02:26:31 +0000 | [diff] [blame] | 395 | if (IsOverridden) |
| 396 | return true; |
| 397 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 398 | Parent.AddFilename(Filename); |
| 399 | return true; |
| 400 | } |
| 401 | |
Argyrios Kyrtzidis | 6d0753d | 2014-03-14 03:07:38 +0000 | [diff] [blame] | 402 | void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename) { |
| 403 | if (Parent.includeModuleFiles()) |
| 404 | Parent.AddFilename(Filename); |
| 405 | } |