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