1 //===--- DependencyFile.cpp - Generate dependency file --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This code generates dependency files.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/Utils.h"
15 #include "clang/Basic/FileManager.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "clang/Frontend/DependencyOutputOptions.h"
18 #include "clang/Frontend/FrontendDiagnostic.h"
19 #include "clang/Lex/DirectoryLookup.h"
20 #include "clang/Lex/LexDiagnostic.h"
21 #include "clang/Lex/PPCallbacks.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "llvm/ADT/StringSet.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
30 class DependencyFileCallback : public PPCallbacks {
31 std::vector<std::string> Files;
32 llvm::StringSet<> FilesSet;
33 const Preprocessor *PP;
34 std::string OutputFile;
35 std::vector<std::string> Targets;
36 bool IncludeSystemHeaders;
38 bool AddMissingHeaderDeps;
39 bool SeenMissingHeader;
41 bool FileMatchesDepCriteria(const char *Filename,
42 SrcMgr::CharacteristicKind FileType);
43 void AddFilename(StringRef Filename);
44 void OutputDependencyFile();
47 DependencyFileCallback(const Preprocessor *_PP,
48 const DependencyOutputOptions &Opts)
49 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
50 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
51 PhonyTarget(Opts.UsePhonyTargets),
52 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
53 SeenMissingHeader(false) {}
55 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
56 SrcMgr::CharacteristicKind FileType,
58 virtual void InclusionDirective(SourceLocation HashLoc,
59 const Token &IncludeTok,
62 const FileEntry *File,
63 SourceLocation EndLoc,
65 StringRef RelativePath);
67 virtual void EndOfMainFile() {
68 OutputDependencyFile();
73 void clang::AttachDependencyFileGen(Preprocessor &PP,
74 const DependencyOutputOptions &Opts) {
75 if (Opts.Targets.empty()) {
76 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
80 // Disable the "file not found" diagnostic if the -MG option was given.
81 if (Opts.AddMissingHeaderDeps)
82 PP.SetSuppressIncludeNotFoundError(true);
84 PP.addPPCallbacks(new DependencyFileCallback(&PP, Opts));
87 /// FileMatchesDepCriteria - Determine whether the given Filename should be
88 /// considered as a dependency.
89 bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
90 SrcMgr::CharacteristicKind FileType) {
91 if (strcmp("<built-in>", Filename) == 0)
94 if (IncludeSystemHeaders)
97 return FileType == SrcMgr::C_User;
100 void DependencyFileCallback::FileChanged(SourceLocation Loc,
101 FileChangeReason Reason,
102 SrcMgr::CharacteristicKind FileType,
104 if (Reason != PPCallbacks::EnterFile)
107 // Dependency generation really does want to go all the way to the
108 // file entry for a source location to find out what is depended on.
109 // We do not want #line markers to affect dependency generation!
110 SourceManager &SM = PP->getSourceManager();
112 const FileEntry *FE =
113 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
116 StringRef Filename = FE->getName();
117 if (!FileMatchesDepCriteria(Filename.data(), FileType))
120 // Remove leading "./" (or ".//" or "././" etc.)
121 while (Filename.size() > 2 && Filename[0] == '.' &&
122 llvm::sys::path::is_separator(Filename[1])) {
123 Filename = Filename.substr(1);
124 while (llvm::sys::path::is_separator(Filename[0]))
125 Filename = Filename.substr(1);
128 AddFilename(Filename);
131 void DependencyFileCallback::InclusionDirective(SourceLocation HashLoc,
132 const Token &IncludeTok,
135 const FileEntry *File,
136 SourceLocation EndLoc,
137 StringRef SearchPath,
138 StringRef RelativePath) {
140 if (AddMissingHeaderDeps)
141 AddFilename(FileName);
143 SeenMissingHeader = true;
147 void DependencyFileCallback::AddFilename(StringRef Filename) {
148 if (FilesSet.insert(Filename))
149 Files.push_back(Filename);
152 /// PrintFilename - GCC escapes spaces, but apparently not ' or " or other
153 /// scary characters.
154 static void PrintFilename(raw_ostream &OS, StringRef Filename) {
155 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
156 if (Filename[i] == ' ')
162 void DependencyFileCallback::OutputDependencyFile() {
163 if (SeenMissingHeader) {
164 llvm::sys::Path(OutputFile).eraseFromDisk();
169 llvm::raw_fd_ostream OS(OutputFile.c_str(), Err);
171 PP->getDiagnostics().Report(diag::err_fe_error_opening)
172 << OutputFile << Err;
176 // Write out the dependency targets, trying to avoid overly long
177 // lines when possible. We try our best to emit exactly the same
178 // dependency file as GCC (4.2), assuming the included files are the
180 const unsigned MaxColumns = 75;
181 unsigned Columns = 0;
183 for (std::vector<std::string>::iterator
184 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
185 unsigned N = I->length();
188 } else if (Columns + N + 2 > MaxColumns) {
195 // Targets already quoted as needed.
202 // Now add each dependency in the order it was seen, but avoiding
204 for (std::vector<std::string>::iterator I = Files.begin(),
205 E = Files.end(); I != E; ++I) {
206 // Start a new line if this would exceed the column limit. Make
207 // sure to leave space for a trailing " \" in case we need to
208 // break the line on the next iteration.
209 unsigned N = I->length();
210 if (Columns + (N + 1) + 2 > MaxColumns) {
215 PrintFilename(OS, *I);
220 // Create phony targets if requested.
221 if (PhonyTarget && !Files.empty()) {
222 // Skip the first entry, this is always the input file itself.
223 for (std::vector<std::string>::iterator I = Files.begin() + 1,
224 E = Files.end(); I != E; ++I) {
226 PrintFilename(OS, *I);