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" |
Richard Smith | 2a6edb3 | 2015-08-09 04:46:57 +0000 | [diff] [blame] | 21 | #include "clang/Lex/ModuleMap.h" |
Daniel Dunbar | 89d1fdf | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 22 | #include "clang/Lex/PPCallbacks.h" |
| 23 | #include "clang/Lex/Preprocessor.h" |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 24 | #include "clang/Serialization/ASTReader.h" |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringSet.h" |
David Majnemer | f0822fb | 2014-10-27 22:31:50 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringSwitch.h" |
Benjamin Kramer | 33d4330 | 2013-06-13 14:26:04 +0000 | [diff] [blame] | 27 | #include "llvm/Support/FileSystem.h" |
Eli Friedman | f7ca26a | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Path.h" |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace clang; |
| 32 | |
| 33 | namespace { |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 34 | struct DepCollectorPPCallbacks : public PPCallbacks { |
| 35 | DependencyCollector &DepCollector; |
| 36 | SourceManager &SM; |
| 37 | DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM) |
| 38 | : DepCollector(L), SM(SM) { } |
| 39 | |
| 40 | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 41 | SrcMgr::CharacteristicKind FileType, |
| 42 | FileID PrevFID) override { |
| 43 | if (Reason != PPCallbacks::EnterFile) |
| 44 | return; |
| 45 | |
| 46 | // Dependency generation really does want to go all the way to the |
| 47 | // file entry for a source location to find out what is depended on. |
| 48 | // We do not want #line markers to affect dependency generation! |
| 49 | const FileEntry *FE = |
| 50 | SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc))); |
| 51 | if (!FE) |
| 52 | return; |
| 53 | |
Douglas Katzman | 7f43af5 | 2015-09-02 21:14:53 +0000 | [diff] [blame] | 54 | StringRef Filename = |
| 55 | llvm::sys::path::remove_leading_dotslash(FE->getName()); |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 56 | |
| 57 | DepCollector.maybeAddDependency(Filename, /*FromModule*/false, |
| 58 | FileType != SrcMgr::C_User, |
| 59 | /*IsModuleFile*/false, /*IsMissing*/false); |
| 60 | } |
| 61 | |
| 62 | void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
| 63 | StringRef FileName, bool IsAngled, |
| 64 | CharSourceRange FilenameRange, const FileEntry *File, |
| 65 | StringRef SearchPath, StringRef RelativePath, |
| 66 | const Module *Imported) override { |
| 67 | if (!File) |
| 68 | DepCollector.maybeAddDependency(FileName, /*FromModule*/false, |
| 69 | /*IsSystem*/false, /*IsModuleFile*/false, |
| 70 | /*IsMissing*/true); |
| 71 | // Files that actually exist are handled by FileChanged. |
| 72 | } |
| 73 | |
| 74 | void EndOfMainFile() override { |
| 75 | DepCollector.finishedMainFile(); |
| 76 | } |
| 77 | }; |
| 78 | |
Richard Smith | 2a6edb3 | 2015-08-09 04:46:57 +0000 | [diff] [blame] | 79 | struct DepCollectorMMCallbacks : public ModuleMapCallbacks { |
| 80 | DependencyCollector &DepCollector; |
| 81 | DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {} |
| 82 | |
| 83 | void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry, |
| 84 | bool IsSystem) override { |
| 85 | StringRef Filename = Entry.getName(); |
| 86 | DepCollector.maybeAddDependency(Filename, /*FromModule*/false, |
| 87 | /*IsSystem*/IsSystem, |
| 88 | /*IsModuleFile*/false, |
| 89 | /*IsMissing*/false); |
| 90 | } |
| 91 | }; |
| 92 | |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 93 | struct DepCollectorASTListener : public ASTReaderListener { |
| 94 | DependencyCollector &DepCollector; |
| 95 | DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { } |
| 96 | bool needsInputFileVisitation() override { return true; } |
| 97 | bool needsSystemInputFileVisitation() override { |
| 98 | return DepCollector.needSystemDependencies(); |
| 99 | } |
Richard Smith | 216a3bd | 2015-08-13 17:57:10 +0000 | [diff] [blame] | 100 | void visitModuleFile(StringRef Filename, |
| 101 | serialization::ModuleKind Kind) override { |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 102 | DepCollector.maybeAddDependency(Filename, /*FromModule*/true, |
| 103 | /*IsSystem*/false, /*IsModuleFile*/true, |
| 104 | /*IsMissing*/false); |
| 105 | } |
| 106 | bool visitInputFile(StringRef Filename, bool IsSystem, |
Richard Smith | 216a3bd | 2015-08-13 17:57:10 +0000 | [diff] [blame] | 107 | bool IsOverridden, bool IsExplicitModule) override { |
| 108 | if (IsOverridden || IsExplicitModule) |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 109 | return true; |
| 110 | |
| 111 | DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem, |
| 112 | /*IsModuleFile*/false, /*IsMissing*/false); |
| 113 | return true; |
| 114 | } |
| 115 | }; |
| 116 | } // end anonymous namespace |
| 117 | |
| 118 | void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule, |
| 119 | bool IsSystem, bool IsModuleFile, |
| 120 | bool IsMissing) { |
David Blaikie | 61b86d4 | 2014-11-19 02:56:13 +0000 | [diff] [blame] | 121 | if (Seen.insert(Filename).second && |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 122 | sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing)) |
| 123 | Dependencies.push_back(Filename); |
| 124 | } |
| 125 | |
David Majnemer | f0822fb | 2014-10-27 22:31:50 +0000 | [diff] [blame] | 126 | static bool isSpecialFilename(StringRef Filename) { |
| 127 | return llvm::StringSwitch<bool>(Filename) |
| 128 | .Case("<built-in>", true) |
| 129 | .Case("<stdin>", true) |
| 130 | .Default(false); |
| 131 | } |
| 132 | |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 133 | bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule, |
| 134 | bool IsSystem, bool IsModuleFile, |
| 135 | bool IsMissing) { |
David Majnemer | f0822fb | 2014-10-27 22:31:50 +0000 | [diff] [blame] | 136 | return !isSpecialFilename(Filename) && |
| 137 | (needSystemDependencies() || !IsSystem); |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 140 | DependencyCollector::~DependencyCollector() { } |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 141 | void DependencyCollector::attachToPreprocessor(Preprocessor &PP) { |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 142 | PP.addPPCallbacks( |
| 143 | llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager())); |
Richard Smith | 2a6edb3 | 2015-08-09 04:46:57 +0000 | [diff] [blame] | 144 | PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( |
| 145 | llvm::make_unique<DepCollectorMMCallbacks>(*this)); |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 146 | } |
| 147 | void DependencyCollector::attachToASTReader(ASTReader &R) { |
David Blaikie | 2721c32 | 2014-08-10 16:54:39 +0000 | [diff] [blame] | 148 | R.addListener(llvm::make_unique<DepCollectorASTListener>(*this)); |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | namespace { |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 152 | /// Private implementation for DependencyFileGenerator |
| 153 | class DFGImpl : public PPCallbacks { |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 154 | std::vector<std::string> Files; |
| 155 | llvm::StringSet<> FilesSet; |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 156 | const Preprocessor *PP; |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 157 | std::string OutputFile; |
Chris Lattner | fa5880e | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 158 | std::vector<std::string> Targets; |
Eli Friedman | 33cd03c | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 159 | bool IncludeSystemHeaders; |
| 160 | bool PhonyTarget; |
Peter Collingbourne | 77b0e7f2 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 161 | bool AddMissingHeaderDeps; |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 162 | bool SeenMissingHeader; |
Argyrios Kyrtzidis | 6d0753d | 2014-03-14 03:07:38 +0000 | [diff] [blame] | 163 | bool IncludeModuleFiles; |
Paul Robinson | d7214a7 | 2015-04-27 18:14:32 +0000 | [diff] [blame] | 164 | DependencyOutputFormat OutputFormat; |
| 165 | |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 166 | private: |
| 167 | bool FileMatchesDepCriteria(const char *Filename, |
Chris Lattner | 66a740e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 168 | SrcMgr::CharacteristicKind FileType); |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 169 | void OutputDependencyFile(); |
| 170 | |
| 171 | public: |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 172 | DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts) |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 173 | : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets), |
Daniel Dunbar | 89d1fdf | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 174 | IncludeSystemHeaders(Opts.IncludeSystemHeaders), |
Peter Collingbourne | 77b0e7f2 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 175 | PhonyTarget(Opts.UsePhonyTargets), |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 176 | AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), |
Argyrios Kyrtzidis | 6d0753d | 2014-03-14 03:07:38 +0000 | [diff] [blame] | 177 | SeenMissingHeader(false), |
Paul Robinson | d7214a7 | 2015-04-27 18:14:32 +0000 | [diff] [blame] | 178 | IncludeModuleFiles(Opts.IncludeModuleFiles), |
Ivan Krasin | 1193f2c | 2015-08-13 04:04:37 +0000 | [diff] [blame] | 179 | OutputFormat(Opts.OutputFormat) { |
Benjamin Kramer | 2e018ef | 2016-05-27 13:36:58 +0000 | [diff] [blame] | 180 | for (const auto &ExtraDep : Opts.ExtraDeps) { |
Ivan Krasin | 1193f2c | 2015-08-13 04:04:37 +0000 | [diff] [blame] | 181 | AddFilename(ExtraDep); |
| 182 | } |
| 183 | } |
Daniel Dunbar | 52e96cc | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 184 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 185 | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 186 | SrcMgr::CharacteristicKind FileType, |
| 187 | FileID PrevFID) override; |
| 188 | void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
| 189 | StringRef FileName, bool IsAngled, |
| 190 | CharSourceRange FilenameRange, const FileEntry *File, |
| 191 | StringRef SearchPath, StringRef RelativePath, |
| 192 | const Module *Imported) override; |
Daniel Dunbar | cb9eaf5 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 193 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 194 | void EndOfMainFile() override { |
Daniel Dunbar | 52e96cc | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 195 | OutputDependencyFile(); |
Daniel Dunbar | 52e96cc | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 196 | } |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 197 | |
| 198 | void AddFilename(StringRef Filename); |
| 199 | bool includeSystemHeaders() const { return IncludeSystemHeaders; } |
Argyrios Kyrtzidis | 6d0753d | 2014-03-14 03:07:38 +0000 | [diff] [blame] | 200 | bool includeModuleFiles() const { return IncludeModuleFiles; } |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
Richard Smith | 2a6edb3 | 2015-08-09 04:46:57 +0000 | [diff] [blame] | 203 | class DFGMMCallback : public ModuleMapCallbacks { |
| 204 | DFGImpl &Parent; |
| 205 | public: |
| 206 | DFGMMCallback(DFGImpl &Parent) : Parent(Parent) {} |
| 207 | void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry, |
| 208 | bool IsSystem) override { |
| 209 | if (!IsSystem || Parent.includeSystemHeaders()) |
| 210 | Parent.AddFilename(Entry.getName()); |
| 211 | } |
| 212 | }; |
| 213 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 214 | class DFGASTReaderListener : public ASTReaderListener { |
| 215 | DFGImpl &Parent; |
| 216 | public: |
| 217 | DFGASTReaderListener(DFGImpl &Parent) |
| 218 | : Parent(Parent) { } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 219 | bool needsInputFileVisitation() override { return true; } |
| 220 | bool needsSystemInputFileVisitation() override { |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 221 | return Parent.includeSystemHeaders(); |
| 222 | } |
Richard Smith | 216a3bd | 2015-08-13 17:57:10 +0000 | [diff] [blame] | 223 | void visitModuleFile(StringRef Filename, |
| 224 | serialization::ModuleKind Kind) override; |
Argyrios Kyrtzidis | 68ccbe0 | 2014-03-14 02:26:31 +0000 | [diff] [blame] | 225 | bool visitInputFile(StringRef Filename, bool isSystem, |
Richard Smith | 216a3bd | 2015-08-13 17:57:10 +0000 | [diff] [blame] | 226 | bool isOverridden, bool isExplicitModule) override; |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 227 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 228 | } |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 229 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 230 | DependencyFileGenerator::DependencyFileGenerator(void *Impl) |
| 231 | : Impl(Impl) { } |
| 232 | |
| 233 | DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor( |
| 234 | clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) { |
| 235 | |
Daniel Dunbar | 89d1fdf | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 236 | if (Opts.Targets.empty()) { |
Daniel Dunbar | 55b781f | 2009-11-11 21:44:00 +0000 | [diff] [blame] | 237 | PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 238 | return nullptr; |
Daniel Dunbar | 89d1fdf | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 239 | } |
Daniel Dunbar | 52e96cc | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 240 | |
Peter Collingbourne | 77b0e7f2 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 241 | // Disable the "file not found" diagnostic if the -MG option was given. |
Eli Friedman | 3781a36 | 2011-08-30 23:07:51 +0000 | [diff] [blame] | 242 | if (Opts.AddMissingHeaderDeps) |
| 243 | PP.SetSuppressIncludeNotFoundError(true); |
Peter Collingbourne | 77b0e7f2 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 244 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 245 | DFGImpl *Callback = new DFGImpl(&PP, Opts); |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 246 | PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback)); |
Richard Smith | 2a6edb3 | 2015-08-09 04:46:57 +0000 | [diff] [blame] | 247 | PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( |
| 248 | llvm::make_unique<DFGMMCallback>(*Callback)); |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 249 | return new DependencyFileGenerator(Callback); |
| 250 | } |
| 251 | |
| 252 | void DependencyFileGenerator::AttachToASTReader(ASTReader &R) { |
| 253 | DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl); |
| 254 | assert(I && "missing implementation"); |
David Blaikie | 2721c32 | 2014-08-10 16:54:39 +0000 | [diff] [blame] | 255 | R.addListener(llvm::make_unique<DFGASTReaderListener>(*I)); |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /// FileMatchesDepCriteria - Determine whether the given Filename should be |
| 259 | /// considered as a dependency. |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 260 | bool DFGImpl::FileMatchesDepCriteria(const char *Filename, |
| 261 | SrcMgr::CharacteristicKind FileType) { |
David Majnemer | f0822fb | 2014-10-27 22:31:50 +0000 | [diff] [blame] | 262 | if (isSpecialFilename(Filename)) |
Daniel Dunbar | 52e96cc | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 263 | return false; |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 264 | |
Eli Friedman | 33cd03c | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 265 | if (IncludeSystemHeaders) |
Daniel Dunbar | 52e96cc | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 266 | return true; |
| 267 | |
| 268 | return FileType == SrcMgr::C_User; |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 271 | void DFGImpl::FileChanged(SourceLocation Loc, |
| 272 | FileChangeReason Reason, |
| 273 | SrcMgr::CharacteristicKind FileType, |
| 274 | FileID PrevFID) { |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 275 | if (Reason != PPCallbacks::EnterFile) |
| 276 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Daniel Dunbar | 52e96cc | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 278 | // Dependency generation really does want to go all the way to the |
| 279 | // file entry for a source location to find out what is depended on. |
| 280 | // We do not want #line markers to affect dependency generation! |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 281 | SourceManager &SM = PP->getSourceManager(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 64d8fc2 | 2009-01-28 05:42:38 +0000 | [diff] [blame] | 283 | const FileEntry *FE = |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 284 | SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc))); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 285 | if (!FE) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 287 | StringRef Filename = FE->getName(); |
Eli Friedman | f7ca26a | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 288 | if (!FileMatchesDepCriteria(Filename.data(), FileType)) |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 289 | return; |
| 290 | |
Douglas Katzman | 7f43af5 | 2015-09-02 21:14:53 +0000 | [diff] [blame] | 291 | AddFilename(llvm::sys::path::remove_leading_dotslash(Filename)); |
Peter Collingbourne | 77b0e7f2 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 292 | } |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 293 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 294 | void DFGImpl::InclusionDirective(SourceLocation HashLoc, |
| 295 | const Token &IncludeTok, |
| 296 | StringRef FileName, |
| 297 | bool IsAngled, |
| 298 | CharSourceRange FilenameRange, |
| 299 | const FileEntry *File, |
| 300 | StringRef SearchPath, |
| 301 | StringRef RelativePath, |
| 302 | const Module *Imported) { |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 303 | if (!File) { |
| 304 | if (AddMissingHeaderDeps) |
| 305 | AddFilename(FileName); |
| 306 | else |
| 307 | SeenMissingHeader = true; |
| 308 | } |
Peter Collingbourne | 77b0e7f2 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 311 | void DFGImpl::AddFilename(StringRef Filename) { |
David Blaikie | 61b86d4 | 2014-11-19 02:56:13 +0000 | [diff] [blame] | 312 | if (FilesSet.insert(Filename).second) |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 313 | Files.push_back(Filename); |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Paul Robinson | 64441de | 2015-05-13 21:18:15 +0000 | [diff] [blame] | 316 | /// Print the filename, with escaping or quoting that accommodates the three |
| 317 | /// most likely tools that use dependency files: GNU Make, BSD Make, and |
| 318 | /// NMake/Jom. |
| 319 | /// |
| 320 | /// BSD Make is the simplest case: It does no escaping at all. This means |
| 321 | /// characters that are normally delimiters, i.e. space and # (the comment |
| 322 | /// character) simply aren't supported in filenames. |
| 323 | /// |
| 324 | /// GNU Make does allow space and # in filenames, but to avoid being treated |
| 325 | /// as a delimiter or comment, these must be escaped with a backslash. Because |
| 326 | /// backslash is itself the escape character, if a backslash appears in a |
| 327 | /// filename, it should be escaped as well. (As a special case, $ is escaped |
| 328 | /// as $$, which is the normal Make way to handle the $ character.) |
| 329 | /// For compatibility with BSD Make and historical practice, if GNU Make |
| 330 | /// un-escapes characters in a filename but doesn't find a match, it will |
| 331 | /// retry with the unmodified original string. |
| 332 | /// |
| 333 | /// GCC tries to accommodate both Make formats by escaping any space or # |
Paul Robinson | fcdf3e9 | 2015-05-13 22:33:50 +0000 | [diff] [blame] | 334 | /// characters in the original filename, but not escaping backslashes. The |
| 335 | /// apparent intent is so that filenames with backslashes will be handled |
Paul Robinson | 64441de | 2015-05-13 21:18:15 +0000 | [diff] [blame] | 336 | /// correctly by BSD Make, and by GNU Make in its fallback mode of using the |
| 337 | /// unmodified original string; filenames with # or space characters aren't |
| 338 | /// supported by BSD Make at all, but will be handled correctly by GNU Make |
| 339 | /// due to the escaping. |
| 340 | /// |
Paul Robinson | fcdf3e9 | 2015-05-13 22:33:50 +0000 | [diff] [blame] | 341 | /// A corner case that GCC gets only partly right is when the original filename |
| 342 | /// has a backslash immediately followed by space or #. GNU Make would expect |
| 343 | /// this backslash to be escaped; however GCC escapes the original backslash |
| 344 | /// only when followed by space, not #. It will therefore take a dependency |
| 345 | /// from a directive such as |
| 346 | /// #include "a\ b\#c.h" |
Paul Robinson | 64441de | 2015-05-13 21:18:15 +0000 | [diff] [blame] | 347 | /// and emit it as |
Paul Robinson | fcdf3e9 | 2015-05-13 22:33:50 +0000 | [diff] [blame] | 348 | /// a\\\ b\\#c.h |
Paul Robinson | 64441de | 2015-05-13 21:18:15 +0000 | [diff] [blame] | 349 | /// which GNU Make will interpret as |
Paul Robinson | fcdf3e9 | 2015-05-13 22:33:50 +0000 | [diff] [blame] | 350 | /// a\ b\ |
Paul Robinson | 64441de | 2015-05-13 21:18:15 +0000 | [diff] [blame] | 351 | /// followed by a comment. Failing to find this file, it will fall back to the |
Paul Robinson | fcdf3e9 | 2015-05-13 22:33:50 +0000 | [diff] [blame] | 352 | /// original string, which probably doesn't exist either; in any case it won't |
| 353 | /// find |
| 354 | /// a\ b\#c.h |
Paul Robinson | 64441de | 2015-05-13 21:18:15 +0000 | [diff] [blame] | 355 | /// which is the actual filename specified by the include directive. |
| 356 | /// |
Paul Robinson | fcdf3e9 | 2015-05-13 22:33:50 +0000 | [diff] [blame] | 357 | /// Clang does what GCC does, rather than what GNU Make expects. |
Paul Robinson | 64441de | 2015-05-13 21:18:15 +0000 | [diff] [blame] | 358 | /// |
| 359 | /// NMake/Jom has a different set of scary characters, but wraps filespecs in |
| 360 | /// double-quotes to avoid misinterpreting them; see |
Paul Robinson | d7214a7 | 2015-04-27 18:14:32 +0000 | [diff] [blame] | 361 | /// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info, |
| 362 | /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx |
| 363 | /// for Windows file-naming info. |
| 364 | static void PrintFilename(raw_ostream &OS, StringRef Filename, |
| 365 | DependencyOutputFormat OutputFormat) { |
| 366 | if (OutputFormat == DependencyOutputFormat::NMake) { |
| 367 | // Add quotes if needed. These are the characters listed as "special" to |
| 368 | // NMake, that are legal in a Windows filespec, and that could cause |
| 369 | // misinterpretation of the dependency string. |
| 370 | if (Filename.find_first_of(" #${}^!") != StringRef::npos) |
| 371 | OS << '\"' << Filename << '\"'; |
| 372 | else |
| 373 | OS << Filename; |
| 374 | return; |
| 375 | } |
Paul Robinson | fcdf3e9 | 2015-05-13 22:33:50 +0000 | [diff] [blame] | 376 | assert(OutputFormat == DependencyOutputFormat::Make); |
Chris Lattner | 5df2a4e | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 377 | for (unsigned i = 0, e = Filename.size(); i != e; ++i) { |
Paul Robinson | fcdf3e9 | 2015-05-13 22:33:50 +0000 | [diff] [blame] | 378 | if (Filename[i] == '#') // Handle '#' the broken gcc way. |
| 379 | OS << '\\'; |
| 380 | else if (Filename[i] == ' ') { // Handle space correctly. |
Chris Lattner | 5df2a4e | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 381 | OS << '\\'; |
Paul Robinson | 64441de | 2015-05-13 21:18:15 +0000 | [diff] [blame] | 382 | unsigned j = i; |
| 383 | while (j > 0 && Filename[--j] == '\\') |
| 384 | OS << '\\'; |
| 385 | } else if (Filename[i] == '$') // $ is escaped by $$. |
Benjamin Kramer | ae61151 | 2013-04-02 13:38:48 +0000 | [diff] [blame] | 386 | OS << '$'; |
Chris Lattner | 5df2a4e | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 387 | OS << Filename[i]; |
| 388 | } |
| 389 | } |
| 390 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 391 | void DFGImpl::OutputDependencyFile() { |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 392 | if (SeenMissingHeader) { |
Rafael Espindola | 2a00878 | 2014-01-10 21:32:14 +0000 | [diff] [blame] | 393 | llvm::sys::fs::remove(OutputFile); |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 394 | return; |
| 395 | } |
| 396 | |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 397 | std::error_code EC; |
| 398 | llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text); |
| 399 | if (EC) { |
| 400 | PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile |
| 401 | << EC.message(); |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 402 | return; |
| 403 | } |
| 404 | |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 405 | // Write out the dependency targets, trying to avoid overly long |
| 406 | // lines when possible. We try our best to emit exactly the same |
| 407 | // dependency file as GCC (4.2), assuming the included files are the |
| 408 | // same. |
| 409 | const unsigned MaxColumns = 75; |
Chris Lattner | fa5880e | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 410 | unsigned Columns = 0; |
| 411 | |
| 412 | for (std::vector<std::string>::iterator |
| 413 | I = Targets.begin(), E = Targets.end(); I != E; ++I) { |
| 414 | unsigned N = I->length(); |
| 415 | if (Columns == 0) { |
| 416 | Columns += N; |
Chris Lattner | fa5880e | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 417 | } else if (Columns + N + 2 > MaxColumns) { |
| 418 | Columns = N + 2; |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 419 | OS << " \\\n "; |
Chris Lattner | fa5880e | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 420 | } else { |
| 421 | Columns += N + 1; |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 422 | OS << ' '; |
Chris Lattner | fa5880e | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 423 | } |
Chris Lattner | 5df2a4e | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 424 | // Targets already quoted as needed. |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 425 | OS << *I; |
Chris Lattner | fa5880e | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 428 | OS << ':'; |
Chris Lattner | fa5880e | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 429 | Columns += 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 431 | // Now add each dependency in the order it was seen, but avoiding |
| 432 | // duplicates. |
| 433 | for (std::vector<std::string>::iterator I = Files.begin(), |
| 434 | E = Files.end(); I != E; ++I) { |
| 435 | // Start a new line if this would exceed the column limit. Make |
| 436 | // sure to leave space for a trailing " \" in case we need to |
| 437 | // break the line on the next iteration. |
| 438 | unsigned N = I->length(); |
| 439 | if (Columns + (N + 1) + 2 > MaxColumns) { |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 440 | OS << " \\\n "; |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 441 | Columns = 2; |
| 442 | } |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 443 | OS << ' '; |
Paul Robinson | d7214a7 | 2015-04-27 18:14:32 +0000 | [diff] [blame] | 444 | PrintFilename(OS, *I, OutputFormat); |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 445 | Columns += N + 1; |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 446 | } |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 447 | OS << '\n'; |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 448 | |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 449 | // Create phony targets if requested. |
Fariborz Jahanian | 193f783 | 2011-04-15 18:49:23 +0000 | [diff] [blame] | 450 | if (PhonyTarget && !Files.empty()) { |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 451 | // Skip the first entry, this is always the input file itself. |
| 452 | for (std::vector<std::string>::iterator I = Files.begin() + 1, |
| 453 | E = Files.end(); I != E; ++I) { |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 454 | OS << '\n'; |
Paul Robinson | d7214a7 | 2015-04-27 18:14:32 +0000 | [diff] [blame] | 455 | PrintFilename(OS, *I, OutputFormat); |
Peter Collingbourne | 0e7e3fc | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 456 | OS << ":\n"; |
Daniel Dunbar | 966c2e1 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
Daniel Dunbar | fa0caca | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 461 | bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename, |
Richard Smith | 216a3bd | 2015-08-13 17:57:10 +0000 | [diff] [blame] | 462 | bool IsSystem, bool IsOverridden, |
| 463 | bool IsExplicitModule) { |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 464 | assert(!IsSystem || needsSystemInputFileVisitation()); |
Richard Smith | 216a3bd | 2015-08-13 17:57:10 +0000 | [diff] [blame] | 465 | if (IsOverridden || IsExplicitModule) |
Argyrios Kyrtzidis | 68ccbe0 | 2014-03-14 02:26:31 +0000 | [diff] [blame] | 466 | return true; |
| 467 | |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 468 | Parent.AddFilename(Filename); |
| 469 | return true; |
| 470 | } |
| 471 | |
Richard Smith | 216a3bd | 2015-08-13 17:57:10 +0000 | [diff] [blame] | 472 | void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename, |
| 473 | serialization::ModuleKind Kind) { |
Richard Smith | be9b6c7 | 2015-08-13 18:30:25 +0000 | [diff] [blame] | 474 | if (Parent.includeModuleFiles()) |
Argyrios Kyrtzidis | 6d0753d | 2014-03-14 03:07:38 +0000 | [diff] [blame] | 475 | Parent.AddFilename(Filename); |
| 476 | } |